Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
118 99724ed4 2018-03-10 stsp }
119 99724ed4 2018-03-10 stsp
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp {
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp ssize_t n;
127 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp
129 09fe317a 2018-03-11 stsp *content = NULL;
130 09fe317a 2018-03-11 stsp
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
134 09fe317a 2018-03-11 stsp goto done;
135 09fe317a 2018-03-11 stsp }
136 09fe317a 2018-03-11 stsp
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 f02eaa22 2019-03-11 stsp else
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
143 ef99fdb1 2018-03-11 stsp goto done;
144 ef99fdb1 2018-03-11 stsp }
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
148 09fe317a 2018-03-11 stsp goto done;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
153 d10c9b58 2019-02-19 stsp goto done;
154 d10c9b58 2019-02-19 stsp }
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
158 09fe317a 2018-03-11 stsp goto done;
159 09fe317a 2018-03-11 stsp }
160 09fe317a 2018-03-11 stsp
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
165 09fe317a 2018-03-11 stsp goto done;
166 09fe317a 2018-03-11 stsp }
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 09fe317a 2018-03-11 stsp goto done;
170 09fe317a 2018-03-11 stsp }
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
172 09fe317a 2018-03-11 stsp
173 09fe317a 2018-03-11 stsp done:
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp if (err) {
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp return err;
182 09fe317a 2018-03-11 stsp }
183 09fe317a 2018-03-11 stsp
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp {
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp } else {
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp }
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
202 024e9686 2019-05-14 stsp }
203 024e9686 2019-05-14 stsp
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp {
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 65596e15 2018-12-24 stsp
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
221 0c48fee2 2019-03-11 stsp goto done;
222 0c48fee2 2019-03-11 stsp }
223 0c48fee2 2019-03-11 stsp
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp if (err)
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp if (err)
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 86c3caaf 2018-03-09 stsp
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
236 0bb8a95e 2018-03-12 stsp }
237 577ec78f 2018-03-11 stsp
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 86c3caaf 2018-03-09 stsp
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
247 86c3caaf 2018-03-09 stsp goto done;
248 86c3caaf 2018-03-09 stsp }
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
251 86c3caaf 2018-03-09 stsp goto done;
252 86c3caaf 2018-03-09 stsp }
253 86c3caaf 2018-03-09 stsp
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 056e7441 2018-03-11 stsp if (err)
257 056e7441 2018-03-11 stsp goto done;
258 056e7441 2018-03-11 stsp
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 99724ed4 2018-03-10 stsp if (err)
262 86c3caaf 2018-03-09 stsp goto done;
263 86c3caaf 2018-03-09 stsp
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
266 65596e15 2018-12-24 stsp if (err)
267 65596e15 2018-12-24 stsp goto done;
268 65596e15 2018-12-24 stsp
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
271 65596e15 2018-12-24 stsp if (err)
272 65596e15 2018-12-24 stsp goto done;
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
280 99724ed4 2018-03-10 stsp if (err)
281 86c3caaf 2018-03-09 stsp goto done;
282 86c3caaf 2018-03-09 stsp
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
286 ec22038e 2019-03-10 stsp if (err)
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
293 ec22038e 2019-03-10 stsp goto done;
294 ec22038e 2019-03-10 stsp }
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
298 ec22038e 2019-03-10 stsp goto done;
299 ec22038e 2019-03-10 stsp }
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 577ec78f 2018-03-11 stsp if (err)
302 577ec78f 2018-03-11 stsp goto done;
303 577ec78f 2018-03-11 stsp
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
307 1451e70d 2018-03-10 stsp goto done;
308 1451e70d 2018-03-10 stsp }
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 99724ed4 2018-03-10 stsp if (err)
311 1451e70d 2018-03-10 stsp goto done;
312 1451e70d 2018-03-10 stsp
313 86c3caaf 2018-03-09 stsp done:
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 86c3caaf 2018-03-09 stsp {
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
348 6d9d28c3 2018-03-11 stsp goto done;
349 6d9d28c3 2018-03-11 stsp }
350 6d9d28c3 2018-03-11 stsp
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
355 6d9d28c3 2018-03-11 stsp goto done;
356 6d9d28c3 2018-03-11 stsp }
357 6d9d28c3 2018-03-11 stsp
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 6d9d28c3 2018-03-11 stsp if (err)
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
366 6d9d28c3 2018-03-11 stsp goto done;
367 6d9d28c3 2018-03-11 stsp }
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 6d9d28c3 2018-03-11 stsp
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 6d9d28c3 2018-03-11 stsp if (err)
388 6d9d28c3 2018-03-11 stsp goto done;
389 eaccb85f 2018-12-25 stsp
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 93a30277 2018-12-24 stsp if (err)
393 93a30277 2018-12-24 stsp goto done;
394 93a30277 2018-12-24 stsp
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp if (err)
398 c442a90d 2019-03-10 stsp goto done;
399 c442a90d 2019-03-10 stsp
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 eaccb85f 2018-12-25 stsp if (err)
402 c442a90d 2019-03-10 stsp goto done;
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
406 eaccb85f 2018-12-25 stsp goto done;
407 c442a90d 2019-03-10 stsp }
408 eaccb85f 2018-12-25 stsp
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 eaccb85f 2018-12-25 stsp if (err)
411 eaccb85f 2018-12-25 stsp goto done;
412 eaccb85f 2018-12-25 stsp
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 f5baf295 2018-03-11 stsp if (err)
416 6d9d28c3 2018-03-11 stsp goto done;
417 6d9d28c3 2018-03-11 stsp
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
420 50b0790e 2020-09-11 stsp if (err)
421 50b0790e 2020-09-11 stsp goto done;
422 50b0790e 2020-09-11 stsp
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
427 50b0790e 2020-09-11 stsp goto done;
428 50b0790e 2020-09-11 stsp }
429 50b0790e 2020-09-11 stsp
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
432 437adc9d 2020-12-10 yzhong
433 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
435 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
436 437adc9d 2020-12-10 yzhong goto done;
437 437adc9d 2020-12-10 yzhong }
438 6d9d28c3 2018-03-11 stsp done:
439 1d0f4054 2021-06-17 stsp if (repo) {
440 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
441 1d0f4054 2021-06-17 stsp if (err == NULL)
442 1d0f4054 2021-06-17 stsp err = close_err;
443 1d0f4054 2021-06-17 stsp }
444 7ac97322 2018-03-11 stsp free(path_got);
445 056e7441 2018-03-11 stsp free(path_lock);
446 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
447 c442a90d 2019-03-10 stsp free(uuidstr);
448 bd165944 2019-03-10 stsp free(formatstr);
449 6d9d28c3 2018-03-11 stsp if (err) {
450 6d9d28c3 2018-03-11 stsp if (fd != -1)
451 6d9d28c3 2018-03-11 stsp close(fd);
452 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
453 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
454 6d9d28c3 2018-03-11 stsp *worktree = NULL;
455 6d9d28c3 2018-03-11 stsp } else
456 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
457 6d9d28c3 2018-03-11 stsp
458 6d9d28c3 2018-03-11 stsp return err;
459 86c3caaf 2018-03-09 stsp }
460 247140b2 2019-02-05 stsp
461 247140b2 2019-02-05 stsp const struct got_error *
462 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
463 247140b2 2019-02-05 stsp {
464 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
465 aedea96d 2020-10-20 stsp char *worktree_path;
466 86c3caaf 2018-03-09 stsp
467 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
468 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
469 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
470 aedea96d 2020-10-20 stsp
471 aedea96d 2020-10-20 stsp for (;;) {
472 aedea96d 2020-10-20 stsp char *parent_path;
473 aedea96d 2020-10-20 stsp
474 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
475 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
476 aedea96d 2020-10-20 stsp free(worktree_path);
477 247140b2 2019-02-05 stsp return err;
478 aedea96d 2020-10-20 stsp }
479 aedea96d 2020-10-20 stsp if (*worktree) {
480 aedea96d 2020-10-20 stsp free(worktree_path);
481 aedea96d 2020-10-20 stsp return NULL;
482 aedea96d 2020-10-20 stsp }
483 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
484 aedea96d 2020-10-20 stsp break;
485 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
486 aedea96d 2020-10-20 stsp if (err) {
487 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
488 aedea96d 2020-10-20 stsp free(worktree_path);
489 aedea96d 2020-10-20 stsp return err;
490 aedea96d 2020-10-20 stsp }
491 aedea96d 2020-10-20 stsp break;
492 aedea96d 2020-10-20 stsp }
493 aedea96d 2020-10-20 stsp free(worktree_path);
494 aedea96d 2020-10-20 stsp worktree_path = parent_path;
495 aedea96d 2020-10-20 stsp }
496 247140b2 2019-02-05 stsp
497 aedea96d 2020-10-20 stsp free(worktree_path);
498 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
499 247140b2 2019-02-05 stsp }
500 247140b2 2019-02-05 stsp
501 3a6ce05a 2019-02-11 stsp const struct got_error *
502 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
503 86c3caaf 2018-03-09 stsp {
504 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
505 60e40e95 2021-01-22 stsp
506 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
507 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
508 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
509 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
510 a6b21eef 2021-01-21 stsp }
511 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
512 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
513 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
514 60e40e95 2021-01-22 stsp free(worktree->repo_path);
515 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
516 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
517 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
518 60e40e95 2021-01-22 stsp free(worktree->root_path);
519 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
520 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
521 a06ff56f 2021-01-21 naddy free(worktree);
522 3a6ce05a 2019-02-11 stsp return err;
523 86c3caaf 2018-03-09 stsp }
524 86c3caaf 2018-03-09 stsp
525 2fbdb5ae 2018-12-29 stsp const char *
526 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
527 c7f4312f 2019-02-05 stsp {
528 c7f4312f 2019-02-05 stsp return worktree->root_path;
529 c7f4312f 2019-02-05 stsp }
530 c7f4312f 2019-02-05 stsp
531 c7f4312f 2019-02-05 stsp const char *
532 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
533 86c3caaf 2018-03-09 stsp {
534 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
535 49520a32 2018-12-29 stsp }
536 49520a32 2018-12-29 stsp const char *
537 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
538 49520a32 2018-12-29 stsp {
539 f609be2e 2018-12-29 stsp return worktree->path_prefix;
540 e5dc7198 2018-12-29 stsp }
541 e5dc7198 2018-12-29 stsp
542 e5dc7198 2018-12-29 stsp const struct got_error *
543 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
544 e5dc7198 2018-12-29 stsp const char *path_prefix)
545 e5dc7198 2018-12-29 stsp {
546 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
547 e5dc7198 2018-12-29 stsp
548 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
549 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
550 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
551 e5dc7198 2018-12-29 stsp }
552 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
553 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
554 e5dc7198 2018-12-29 stsp free(absprefix);
555 e5dc7198 2018-12-29 stsp return NULL;
556 86c3caaf 2018-03-09 stsp }
557 86c3caaf 2018-03-09 stsp
558 bc70eb79 2019-05-09 stsp const char *
559 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
560 86c3caaf 2018-03-09 stsp {
561 36a38700 2019-05-10 stsp return worktree->head_ref_name;
562 024e9686 2019-05-14 stsp }
563 024e9686 2019-05-14 stsp
564 024e9686 2019-05-14 stsp const struct got_error *
565 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
566 024e9686 2019-05-14 stsp struct got_reference *head_ref)
567 024e9686 2019-05-14 stsp {
568 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
569 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
570 024e9686 2019-05-14 stsp
571 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
572 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
573 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
574 024e9686 2019-05-14 stsp path_got = NULL;
575 024e9686 2019-05-14 stsp goto done;
576 024e9686 2019-05-14 stsp }
577 024e9686 2019-05-14 stsp
578 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
579 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
580 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
581 024e9686 2019-05-14 stsp goto done;
582 024e9686 2019-05-14 stsp }
583 024e9686 2019-05-14 stsp
584 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
585 024e9686 2019-05-14 stsp if (err)
586 024e9686 2019-05-14 stsp goto done;
587 024e9686 2019-05-14 stsp
588 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
589 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
590 024e9686 2019-05-14 stsp done:
591 024e9686 2019-05-14 stsp free(path_got);
592 024e9686 2019-05-14 stsp if (err)
593 024e9686 2019-05-14 stsp free(head_ref_name);
594 024e9686 2019-05-14 stsp return err;
595 507dc3bb 2018-12-29 stsp }
596 507dc3bb 2018-12-29 stsp
597 b72f483a 2019-02-05 stsp struct got_object_id *
598 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
599 507dc3bb 2018-12-29 stsp {
600 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
601 86c3caaf 2018-03-09 stsp }
602 86c3caaf 2018-03-09 stsp
603 507dc3bb 2018-12-29 stsp const struct got_error *
604 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
605 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
606 507dc3bb 2018-12-29 stsp {
607 507dc3bb 2018-12-29 stsp const struct got_error *err;
608 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
609 507dc3bb 2018-12-29 stsp char *id_str = NULL;
610 507dc3bb 2018-12-29 stsp char *path_got = NULL;
611 507dc3bb 2018-12-29 stsp
612 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
613 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
614 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
615 507dc3bb 2018-12-29 stsp path_got = NULL;
616 507dc3bb 2018-12-29 stsp goto done;
617 507dc3bb 2018-12-29 stsp }
618 507dc3bb 2018-12-29 stsp
619 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
620 507dc3bb 2018-12-29 stsp if (err)
621 507dc3bb 2018-12-29 stsp return err;
622 507dc3bb 2018-12-29 stsp
623 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
624 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
625 507dc3bb 2018-12-29 stsp goto done;
626 507dc3bb 2018-12-29 stsp }
627 507dc3bb 2018-12-29 stsp
628 507dc3bb 2018-12-29 stsp /* Record our base commit. */
629 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
630 507dc3bb 2018-12-29 stsp if (err)
631 507dc3bb 2018-12-29 stsp goto done;
632 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 507dc3bb 2018-12-29 stsp if (err)
634 507dc3bb 2018-12-29 stsp goto done;
635 507dc3bb 2018-12-29 stsp
636 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
637 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
638 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
639 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
640 507dc3bb 2018-12-29 stsp goto done;
641 507dc3bb 2018-12-29 stsp }
642 507dc3bb 2018-12-29 stsp done:
643 507dc3bb 2018-12-29 stsp if (obj)
644 507dc3bb 2018-12-29 stsp got_object_close(obj);
645 507dc3bb 2018-12-29 stsp free(id_str);
646 507dc3bb 2018-12-29 stsp free(path_got);
647 507dc3bb 2018-12-29 stsp return err;
648 50b0790e 2020-09-11 stsp }
649 50b0790e 2020-09-11 stsp
650 50b0790e 2020-09-11 stsp const struct got_gotconfig *
651 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
652 50b0790e 2020-09-11 stsp {
653 50b0790e 2020-09-11 stsp return worktree->gotconfig;
654 507dc3bb 2018-12-29 stsp }
655 507dc3bb 2018-12-29 stsp
656 9d31a1d8 2018-03-11 stsp static const struct got_error *
657 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
658 86c3caaf 2018-03-09 stsp {
659 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
660 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
661 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
662 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
663 86c3caaf 2018-03-09 stsp return NULL;
664 21908da4 2019-01-13 stsp }
665 21908da4 2019-01-13 stsp
666 21908da4 2019-01-13 stsp static const struct got_error *
667 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
668 4a1ddfc2 2019-01-12 stsp {
669 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
670 4a1ddfc2 2019-01-12 stsp char *abspath;
671 4a1ddfc2 2019-01-12 stsp
672 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
673 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
674 4a1ddfc2 2019-01-12 stsp
675 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
676 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
677 ddcd8544 2019-03-11 stsp struct stat sb;
678 ddcd8544 2019-03-11 stsp err = NULL;
679 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
680 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
681 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
682 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
683 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
684 ddcd8544 2019-03-11 stsp }
685 ddcd8544 2019-03-11 stsp }
686 4a1ddfc2 2019-01-12 stsp free(abspath);
687 68c76935 2019-02-19 stsp return err;
688 68c76935 2019-02-19 stsp }
689 68c76935 2019-02-19 stsp
690 68c76935 2019-02-19 stsp static const struct got_error *
691 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
692 68c76935 2019-02-19 stsp {
693 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
694 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
695 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
696 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
697 68c76935 2019-02-19 stsp
698 68c76935 2019-02-19 stsp *same = 1;
699 68c76935 2019-02-19 stsp
700 230a42bd 2019-05-11 jcs for (;;) {
701 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
702 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
703 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
704 68c76935 2019-02-19 stsp break;
705 68c76935 2019-02-19 stsp }
706 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
707 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
708 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
709 68c76935 2019-02-19 stsp break;
710 68c76935 2019-02-19 stsp }
711 68c76935 2019-02-19 stsp if (flen1 == 0) {
712 68c76935 2019-02-19 stsp if (flen2 != 0)
713 68c76935 2019-02-19 stsp *same = 0;
714 68c76935 2019-02-19 stsp break;
715 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
716 68c76935 2019-02-19 stsp if (flen1 != 0)
717 68c76935 2019-02-19 stsp *same = 0;
718 68c76935 2019-02-19 stsp break;
719 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
720 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
721 68c76935 2019-02-19 stsp *same = 0;
722 68c76935 2019-02-19 stsp break;
723 68c76935 2019-02-19 stsp }
724 68c76935 2019-02-19 stsp } else {
725 68c76935 2019-02-19 stsp *same = 0;
726 68c76935 2019-02-19 stsp break;
727 68c76935 2019-02-19 stsp }
728 68c76935 2019-02-19 stsp }
729 68c76935 2019-02-19 stsp
730 68c76935 2019-02-19 stsp return err;
731 68c76935 2019-02-19 stsp }
732 68c76935 2019-02-19 stsp
733 68c76935 2019-02-19 stsp static const struct got_error *
734 db590691 2021-06-02 stsp check_files_equal(int *same, FILE *f1, FILE *f2)
735 68c76935 2019-02-19 stsp {
736 68c76935 2019-02-19 stsp struct stat sb;
737 68c76935 2019-02-19 stsp size_t size1, size2;
738 68c76935 2019-02-19 stsp
739 68c76935 2019-02-19 stsp *same = 1;
740 68c76935 2019-02-19 stsp
741 db590691 2021-06-02 stsp if (fstat(fileno(f1), &sb) != 0)
742 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
743 68c76935 2019-02-19 stsp size1 = sb.st_size;
744 68c76935 2019-02-19 stsp
745 db590691 2021-06-02 stsp if (fstat(fileno(f2), &sb) != 0)
746 db590691 2021-06-02 stsp return got_error_from_errno("fstat");
747 68c76935 2019-02-19 stsp size2 = sb.st_size;
748 68c76935 2019-02-19 stsp
749 68c76935 2019-02-19 stsp if (size1 != size2) {
750 68c76935 2019-02-19 stsp *same = 0;
751 68c76935 2019-02-19 stsp return NULL;
752 68c76935 2019-02-19 stsp }
753 68c76935 2019-02-19 stsp
754 db590691 2021-06-02 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
755 db590691 2021-06-02 stsp return got_ferror(f1, GOT_ERR_IO);
756 db590691 2021-06-02 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
757 db590691 2021-06-02 stsp return got_ferror(f2, GOT_ERR_IO);
758 68c76935 2019-02-19 stsp
759 db590691 2021-06-02 stsp return check_file_contents_equal(same, f1, f2);
760 6353ad76 2019-02-08 stsp }
761 6353ad76 2019-02-08 stsp
762 6353ad76 2019-02-08 stsp /*
763 db590691 2021-06-02 stsp * Perform a 3-way merge where the file f_orig acts as the common
764 db590691 2021-06-02 stsp * ancestor, the file f_deriv acts as the first derived version,
765 eec2f5a9 2021-06-03 stsp * and the file f_deriv2 acts as the second derived version.
766 eec2f5a9 2021-06-03 stsp * The merge result will be written to a new file at ondisk_path; any
767 eec2f5a9 2021-06-03 stsp * existing file at this path will be replaced.
768 6353ad76 2019-02-08 stsp */
769 6353ad76 2019-02-08 stsp static const struct got_error *
770 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
771 eec2f5a9 2021-06-03 stsp FILE *f_orig, FILE *f_deriv, FILE *f_deriv2, const char *ondisk_path,
772 07bb0f93 2021-06-02 stsp const char *path, uint16_t st_mode,
773 54d5be07 2021-06-03 stsp const char *label_orig, const char *label_deriv, const char *label_deriv2,
774 fdf3c2d3 2021-06-17 stsp enum got_diff_algorithm diff_algo, struct got_repository *repo,
775 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
776 6353ad76 2019-02-08 stsp {
777 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
778 6353ad76 2019-02-08 stsp int merged_fd = -1;
779 db590691 2021-06-02 stsp FILE *f_merged = NULL;
780 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
781 234035bc 2019-06-01 stsp int overlapcnt = 0;
782 3524bbf9 2020-10-20 stsp char *parent = NULL;
783 6353ad76 2019-02-08 stsp
784 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
785 234035bc 2019-06-01 stsp
786 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
787 3524bbf9 2020-10-20 stsp if (err)
788 3524bbf9 2020-10-20 stsp return err;
789 af54ae4a 2019-02-19 stsp
790 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
791 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
792 3524bbf9 2020-10-20 stsp goto done;
793 3524bbf9 2020-10-20 stsp }
794 af54ae4a 2019-02-19 stsp
795 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
796 6353ad76 2019-02-08 stsp if (err)
797 6353ad76 2019-02-08 stsp goto done;
798 3b9f0f87 2020-07-23 stsp
799 eec2f5a9 2021-06-03 stsp err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig,
800 fdf3c2d3 2021-06-17 stsp f_deriv2, label_deriv, label_orig, label_deriv2, diff_algo);
801 6353ad76 2019-02-08 stsp if (err)
802 6353ad76 2019-02-08 stsp goto done;
803 6353ad76 2019-02-08 stsp
804 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
805 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
806 1ee397ad 2019-07-12 stsp if (err)
807 1ee397ad 2019-07-12 stsp goto done;
808 6353ad76 2019-02-08 stsp
809 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
810 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
811 816dc654 2019-02-16 stsp goto done;
812 68c76935 2019-02-19 stsp }
813 68c76935 2019-02-19 stsp
814 db590691 2021-06-02 stsp f_merged = fdopen(merged_fd, "r");
815 db590691 2021-06-02 stsp if (f_merged == NULL) {
816 db590691 2021-06-02 stsp err = got_error_from_errno("fdopen");
817 db590691 2021-06-02 stsp goto done;
818 db590691 2021-06-02 stsp }
819 db590691 2021-06-02 stsp merged_fd = -1;
820 db590691 2021-06-02 stsp
821 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
822 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
823 db590691 2021-06-02 stsp err = check_files_equal(local_changes_subsumed, f_deriv,
824 db590691 2021-06-02 stsp f_merged);
825 68c76935 2019-02-19 stsp if (err)
826 68c76935 2019-02-19 stsp goto done;
827 816dc654 2019-02-16 stsp }
828 6353ad76 2019-02-08 stsp
829 db590691 2021-06-02 stsp if (fchmod(fileno(f_merged), st_mode) != 0) {
830 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
831 70a0c8ec 2019-02-20 stsp goto done;
832 70a0c8ec 2019-02-20 stsp }
833 70a0c8ec 2019-02-20 stsp
834 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
835 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
836 230a42bd 2019-05-11 jcs ondisk_path);
837 6353ad76 2019-02-08 stsp goto done;
838 6353ad76 2019-02-08 stsp }
839 6353ad76 2019-02-08 stsp done:
840 fdcb7daf 2019-12-15 stsp if (err) {
841 fdcb7daf 2019-12-15 stsp if (merged_path)
842 fdcb7daf 2019-12-15 stsp unlink(merged_path);
843 3b9f0f87 2020-07-23 stsp }
844 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
845 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
846 db590691 2021-06-02 stsp if (f_merged && fclose(f_merged) == EOF && err == NULL)
847 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
848 6353ad76 2019-02-08 stsp free(merged_path);
849 af54ae4a 2019-02-19 stsp free(base_path);
850 3524bbf9 2020-10-20 stsp free(parent);
851 af57b12a 2020-07-23 stsp return err;
852 af57b12a 2020-07-23 stsp }
853 af57b12a 2020-07-23 stsp
854 af57b12a 2020-07-23 stsp static const struct got_error *
855 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
856 af57b12a 2020-07-23 stsp size_t target_len)
857 af57b12a 2020-07-23 stsp {
858 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
859 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
860 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
861 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
862 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
863 af57b12a 2020-07-23 stsp ondisk_path);
864 af57b12a 2020-07-23 stsp return NULL;
865 af57b12a 2020-07-23 stsp }
866 af57b12a 2020-07-23 stsp
867 af57b12a 2020-07-23 stsp /*
868 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
869 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
870 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
871 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
872 993e2a1b 2020-07-23 stsp *
873 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
874 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
875 993e2a1b 2020-07-23 stsp *
876 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
877 993e2a1b 2020-07-23 stsp * has deleted this symlink.
878 11cc08c1 2020-07-23 stsp */
879 11cc08c1 2020-07-23 stsp static const struct got_error *
880 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
881 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
882 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
883 11cc08c1 2020-07-23 stsp {
884 11cc08c1 2020-07-23 stsp const struct got_error *err;
885 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
886 11cc08c1 2020-07-23 stsp FILE *f = NULL;
887 11cc08c1 2020-07-23 stsp
888 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
889 11cc08c1 2020-07-23 stsp if (err)
890 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
891 11cc08c1 2020-07-23 stsp
892 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
893 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
894 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
895 11cc08c1 2020-07-23 stsp goto done;
896 11cc08c1 2020-07-23 stsp }
897 11cc08c1 2020-07-23 stsp
898 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
899 11cc08c1 2020-07-23 stsp if (err)
900 3818e3c4 2020-11-01 naddy goto done;
901 3818e3c4 2020-11-01 naddy
902 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
903 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
904 11cc08c1 2020-07-23 stsp goto done;
905 3818e3c4 2020-11-01 naddy }
906 11cc08c1 2020-07-23 stsp
907 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
908 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
909 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
910 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
911 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
912 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
913 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
914 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
915 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
916 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
917 11cc08c1 2020-07-23 stsp goto done;
918 11cc08c1 2020-07-23 stsp }
919 11cc08c1 2020-07-23 stsp
920 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
921 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
922 11cc08c1 2020-07-23 stsp goto done;
923 11cc08c1 2020-07-23 stsp }
924 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
925 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
926 11cc08c1 2020-07-23 stsp goto done;
927 11cc08c1 2020-07-23 stsp }
928 11cc08c1 2020-07-23 stsp done:
929 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
930 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
931 11cc08c1 2020-07-23 stsp free(path);
932 11cc08c1 2020-07-23 stsp free(id_str);
933 11cc08c1 2020-07-23 stsp free(label_deriv);
934 11cc08c1 2020-07-23 stsp return err;
935 11cc08c1 2020-07-23 stsp }
936 d219f183 2020-07-23 stsp
937 d219f183 2020-07-23 stsp /* forward declaration */
938 d219f183 2020-07-23 stsp static const struct got_error *
939 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
940 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
941 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
942 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
943 11cc08c1 2020-07-23 stsp
944 11cc08c1 2020-07-23 stsp /*
945 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
946 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
947 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
948 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
949 af57b12a 2020-07-23 stsp */
950 af57b12a 2020-07-23 stsp static const struct got_error *
951 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
952 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
953 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
954 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
955 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
956 af57b12a 2020-07-23 stsp {
957 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
958 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
959 af57b12a 2020-07-23 stsp struct stat sb;
960 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
961 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
962 11cc08c1 2020-07-23 stsp int have_local_change = 0;
963 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
964 af57b12a 2020-07-23 stsp
965 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
966 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
967 af57b12a 2020-07-23 stsp
968 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
969 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
970 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
971 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
972 af57b12a 2020-07-23 stsp ondisk_path);
973 af57b12a 2020-07-23 stsp goto done;
974 af57b12a 2020-07-23 stsp }
975 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
976 af57b12a 2020-07-23 stsp
977 526a746f 2020-07-23 stsp if (blob_orig) {
978 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
979 526a746f 2020-07-23 stsp if (err)
980 526a746f 2020-07-23 stsp goto done;
981 526a746f 2020-07-23 stsp }
982 af57b12a 2020-07-23 stsp
983 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
984 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
985 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
986 11cc08c1 2020-07-23 stsp have_local_change = 1;
987 11cc08c1 2020-07-23 stsp
988 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
989 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
990 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
991 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
992 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
993 11cc08c1 2020-07-23 stsp
994 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
995 11cc08c1 2020-07-23 stsp if (ancestor_target) {
996 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
997 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
998 11cc08c1 2020-07-23 stsp path);
999 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1000 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1001 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1002 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1003 11cc08c1 2020-07-23 stsp path);
1004 11cc08c1 2020-07-23 stsp } else {
1005 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1006 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1007 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1008 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1009 11cc08c1 2020-07-23 stsp if (err)
1010 11cc08c1 2020-07-23 stsp goto done;
1011 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1012 11cc08c1 2020-07-23 stsp path);
1013 11cc08c1 2020-07-23 stsp }
1014 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1015 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1016 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1017 af57b12a 2020-07-23 stsp strlen(deriv_target));
1018 af57b12a 2020-07-23 stsp if (err)
1019 af57b12a 2020-07-23 stsp goto done;
1020 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1021 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1022 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1023 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1024 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1025 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1026 ea7786be 2020-07-23 stsp path);
1027 ea7786be 2020-07-23 stsp } else {
1028 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1029 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1030 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1031 ea7786be 2020-07-23 stsp if (err)
1032 ea7786be 2020-07-23 stsp goto done;
1033 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1034 ea7786be 2020-07-23 stsp path);
1035 ea7786be 2020-07-23 stsp }
1036 6353ad76 2019-02-08 stsp }
1037 11cc08c1 2020-07-23 stsp
1038 af57b12a 2020-07-23 stsp done:
1039 af57b12a 2020-07-23 stsp free(ancestor_target);
1040 eec2f5a9 2021-06-03 stsp return err;
1041 eec2f5a9 2021-06-03 stsp }
1042 eec2f5a9 2021-06-03 stsp
1043 eec2f5a9 2021-06-03 stsp static const struct got_error *
1044 eec2f5a9 2021-06-03 stsp dump_symlink_target_path_to_file(FILE **outfile, const char *ondisk_path)
1045 eec2f5a9 2021-06-03 stsp {
1046 eec2f5a9 2021-06-03 stsp const struct got_error *err = NULL;
1047 eec2f5a9 2021-06-03 stsp char target_path[PATH_MAX];
1048 eec2f5a9 2021-06-03 stsp ssize_t target_len;
1049 eec2f5a9 2021-06-03 stsp size_t n;
1050 eec2f5a9 2021-06-03 stsp FILE *f;
1051 eec2f5a9 2021-06-03 stsp
1052 eec2f5a9 2021-06-03 stsp *outfile = NULL;
1053 eec2f5a9 2021-06-03 stsp
1054 eec2f5a9 2021-06-03 stsp f = got_opentemp();
1055 eec2f5a9 2021-06-03 stsp if (f == NULL)
1056 eec2f5a9 2021-06-03 stsp return got_error_from_errno("got_opentemp");
1057 eec2f5a9 2021-06-03 stsp target_len = readlink(ondisk_path, target_path, sizeof(target_path));
1058 eec2f5a9 2021-06-03 stsp if (target_len == -1) {
1059 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("readlink", ondisk_path);
1060 eec2f5a9 2021-06-03 stsp goto done;
1061 eec2f5a9 2021-06-03 stsp }
1062 eec2f5a9 2021-06-03 stsp n = fwrite(target_path, 1, target_len, f);
1063 eec2f5a9 2021-06-03 stsp if (n != target_len) {
1064 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1065 eec2f5a9 2021-06-03 stsp goto done;
1066 eec2f5a9 2021-06-03 stsp }
1067 eec2f5a9 2021-06-03 stsp if (fflush(f) == EOF) {
1068 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fflush");
1069 eec2f5a9 2021-06-03 stsp goto done;
1070 eec2f5a9 2021-06-03 stsp }
1071 eec2f5a9 2021-06-03 stsp if (fseek(f, 0L, SEEK_SET) == -1) {
1072 eec2f5a9 2021-06-03 stsp err = got_ferror(f, GOT_ERR_IO);
1073 eec2f5a9 2021-06-03 stsp goto done;
1074 eec2f5a9 2021-06-03 stsp }
1075 eec2f5a9 2021-06-03 stsp done:
1076 eec2f5a9 2021-06-03 stsp if (err)
1077 eec2f5a9 2021-06-03 stsp fclose(f);
1078 eec2f5a9 2021-06-03 stsp else
1079 eec2f5a9 2021-06-03 stsp *outfile = f;
1080 14c901f1 2019-08-08 stsp return err;
1081 14c901f1 2019-08-08 stsp }
1082 14c901f1 2019-08-08 stsp
1083 14c901f1 2019-08-08 stsp /*
1084 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1085 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1086 14c901f1 2019-08-08 stsp * acts as the second derived version.
1087 14c901f1 2019-08-08 stsp */
1088 14c901f1 2019-08-08 stsp static const struct got_error *
1089 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1090 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1091 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1092 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1093 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1094 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1095 14c901f1 2019-08-08 stsp {
1096 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1097 eec2f5a9 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
1098 67a66647 2021-05-31 stsp char *blob_orig_path = NULL;
1099 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1100 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1101 14c901f1 2019-08-08 stsp
1102 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1103 14c901f1 2019-08-08 stsp
1104 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1105 ed6b5030 2020-10-20 stsp if (err)
1106 ed6b5030 2020-10-20 stsp return err;
1107 14c901f1 2019-08-08 stsp
1108 67a66647 2021-05-31 stsp if (blob_orig) {
1109 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig",
1110 67a66647 2021-05-31 stsp parent) == -1) {
1111 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
1112 67a66647 2021-05-31 stsp base_path = NULL;
1113 67a66647 2021-05-31 stsp goto done;
1114 67a66647 2021-05-31 stsp }
1115 67a66647 2021-05-31 stsp
1116 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1117 67a66647 2021-05-31 stsp if (err)
1118 67a66647 2021-05-31 stsp goto done;
1119 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1120 67a66647 2021-05-31 stsp blob_orig);
1121 67a66647 2021-05-31 stsp if (err)
1122 67a66647 2021-05-31 stsp goto done;
1123 67a66647 2021-05-31 stsp free(base_path);
1124 db590691 2021-06-02 stsp } else {
1125 db590691 2021-06-02 stsp /*
1126 db590691 2021-06-02 stsp * No common ancestor exists. This is an "add vs add" conflict
1127 db590691 2021-06-02 stsp * and we simply use an empty ancestor file to make both files
1128 db590691 2021-06-02 stsp * appear in the merged result in their entirety.
1129 db590691 2021-06-02 stsp */
1130 db590691 2021-06-02 stsp f_orig = got_opentemp();
1131 db590691 2021-06-02 stsp if (f_orig == NULL) {
1132 db590691 2021-06-02 stsp err = got_error_from_errno("got_opentemp");
1133 db590691 2021-06-02 stsp goto done;
1134 db590691 2021-06-02 stsp }
1135 67a66647 2021-05-31 stsp }
1136 67a66647 2021-05-31 stsp
1137 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1139 14c901f1 2019-08-08 stsp base_path = NULL;
1140 14c901f1 2019-08-08 stsp goto done;
1141 14c901f1 2019-08-08 stsp }
1142 14c901f1 2019-08-08 stsp
1143 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 14c901f1 2019-08-08 stsp if (err)
1145 14c901f1 2019-08-08 stsp goto done;
1146 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 14c901f1 2019-08-08 stsp blob_deriv);
1148 14c901f1 2019-08-08 stsp if (err)
1149 14c901f1 2019-08-08 stsp goto done;
1150 14c901f1 2019-08-08 stsp
1151 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 14c901f1 2019-08-08 stsp if (err)
1153 14c901f1 2019-08-08 stsp goto done;
1154 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1155 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1157 14c901f1 2019-08-08 stsp goto done;
1158 eec2f5a9 2021-06-03 stsp }
1159 eec2f5a9 2021-06-03 stsp
1160 eec2f5a9 2021-06-03 stsp /*
1161 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
1162 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
1163 eec2f5a9 2021-06-03 stsp */
1164 eec2f5a9 2021-06-03 stsp if (S_ISLNK(st_mode)) {
1165 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2, ondisk_path);
1166 eec2f5a9 2021-06-03 stsp if (err)
1167 eec2f5a9 2021-06-03 stsp goto done;
1168 eec2f5a9 2021-06-03 stsp } else {
1169 eec2f5a9 2021-06-03 stsp int fd;
1170 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
1171 eec2f5a9 2021-06-03 stsp if (fd == -1) {
1172 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
1173 eec2f5a9 2021-06-03 stsp goto done;
1174 eec2f5a9 2021-06-03 stsp }
1175 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
1176 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
1177 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
1178 eec2f5a9 2021-06-03 stsp close(fd);
1179 eec2f5a9 2021-06-03 stsp goto done;
1180 eec2f5a9 2021-06-03 stsp }
1181 14c901f1 2019-08-08 stsp }
1182 14c901f1 2019-08-08 stsp
1183 07bb0f93 2021-06-02 stsp err = merge_file(local_changes_subsumed, worktree, f_orig, f_deriv,
1184 eec2f5a9 2021-06-03 stsp f_deriv2, ondisk_path, path, st_mode, label_orig, label_deriv,
1185 fdf3c2d3 2021-06-17 stsp NULL, GOT_DIFF_ALGORITHM_MYERS, repo, progress_cb, progress_arg);
1186 14c901f1 2019-08-08 stsp done:
1187 67a66647 2021-05-31 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
1188 67a66647 2021-05-31 stsp err = got_error_from_errno("fclose");
1189 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1190 eec2f5a9 2021-06-03 stsp err = got_error_from_errno("fclose");
1191 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
1192 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1193 14c901f1 2019-08-08 stsp free(base_path);
1194 67a66647 2021-05-31 stsp if (blob_orig_path) {
1195 67a66647 2021-05-31 stsp unlink(blob_orig_path);
1196 67a66647 2021-05-31 stsp free(blob_orig_path);
1197 67a66647 2021-05-31 stsp }
1198 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1199 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1200 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1201 14c901f1 2019-08-08 stsp }
1202 6353ad76 2019-02-08 stsp free(id_str);
1203 818c7501 2019-07-11 stsp free(label_deriv);
1204 ed6b5030 2020-10-20 stsp free(parent);
1205 4a1ddfc2 2019-01-12 stsp return err;
1206 4a1ddfc2 2019-01-12 stsp }
1207 4a1ddfc2 2019-01-12 stsp
1208 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1209 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1210 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1211 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1212 13d9040b 2019-03-27 stsp {
1213 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1214 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1215 13d9040b 2019-03-27 stsp
1216 65b05cec 2020-07-23 stsp *new_iep = NULL;
1217 65b05cec 2020-07-23 stsp
1218 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1219 054041d0 2020-06-24 stsp if (err)
1220 054041d0 2020-06-24 stsp return err;
1221 054041d0 2020-06-24 stsp
1222 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1223 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1224 054041d0 2020-06-24 stsp if (err)
1225 054041d0 2020-06-24 stsp goto done;
1226 054041d0 2020-06-24 stsp
1227 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1228 054041d0 2020-06-24 stsp done:
1229 054041d0 2020-06-24 stsp if (err)
1230 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1231 65b05cec 2020-07-23 stsp else
1232 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1233 13d9040b 2019-03-27 stsp return err;
1234 1ebedb77 2019-10-19 stsp }
1235 1ebedb77 2019-10-19 stsp
1236 1ebedb77 2019-10-19 stsp static mode_t
1237 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1238 1ebedb77 2019-10-19 stsp {
1239 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1240 1ebedb77 2019-10-19 stsp
1241 1ebedb77 2019-10-19 stsp if (executable) {
1242 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1243 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1244 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1245 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1246 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1247 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1248 1ebedb77 2019-10-19 stsp }
1249 1ebedb77 2019-10-19 stsp
1250 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1251 13d9040b 2019-03-27 stsp }
1252 13d9040b 2019-03-27 stsp
1253 8ba819a3 2020-07-23 stsp /* forward declaration */
1254 13d9040b 2019-03-27 stsp static const struct got_error *
1255 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1256 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1257 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1258 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1259 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1260 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1261 8ba819a3 2020-07-23 stsp
1262 5a1fbc73 2020-07-23 stsp /*
1263 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1264 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1265 5a1fbc73 2020-07-23 stsp */
1266 8ba819a3 2020-07-23 stsp static const struct got_error *
1267 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1268 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1269 5a1fbc73 2020-07-23 stsp {
1270 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1271 5a1fbc73 2020-07-23 stsp ssize_t elen;
1272 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1273 5a1fbc73 2020-07-23 stsp int fd;
1274 5a1fbc73 2020-07-23 stsp
1275 c6e8a826 2021-04-05 stsp *did_something = 0;
1276 c6e8a826 2021-04-05 stsp
1277 5a1fbc73 2020-07-23 stsp /*
1278 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1279 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1280 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1281 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1282 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1283 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1284 5a1fbc73 2020-07-23 stsp */
1285 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1286 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1287 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1288 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1289 5a1fbc73 2020-07-23 stsp
1290 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1291 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1292 5a1fbc73 2020-07-23 stsp if (elen == -1)
1293 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1294 5a1fbc73 2020-07-23 stsp
1295 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1296 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1297 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1298 5a1fbc73 2020-07-23 stsp }
1299 5a1fbc73 2020-07-23 stsp
1300 c6e8a826 2021-04-05 stsp *did_something = 1;
1301 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1302 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1303 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1304 5a1fbc73 2020-07-23 stsp return err;
1305 3c1ec782 2020-07-23 stsp }
1306 3c1ec782 2020-07-23 stsp
1307 3c1ec782 2020-07-23 stsp static const struct got_error *
1308 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1309 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1310 3c1ec782 2020-07-23 stsp {
1311 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1312 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1313 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1314 3c1ec782 2020-07-23 stsp
1315 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1316 3c1ec782 2020-07-23 stsp
1317 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1318 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1319 3c1ec782 2020-07-23 stsp return NULL;
1320 3c1ec782 2020-07-23 stsp }
1321 3c1ec782 2020-07-23 stsp
1322 3c1ec782 2020-07-23 stsp /*
1323 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1324 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1325 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1326 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1327 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1328 3c1ec782 2020-07-23 stsp */
1329 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1330 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1331 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1332 ce031e9e 2020-10-20 stsp if (err)
1333 ce031e9e 2020-10-20 stsp return err;
1334 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1335 ce031e9e 2020-10-20 stsp free(parent);
1336 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1337 ce031e9e 2020-10-20 stsp }
1338 ce031e9e 2020-10-20 stsp free(parent);
1339 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1340 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1341 3c1ec782 2020-07-23 stsp free(abspath);
1342 3c1ec782 2020-07-23 stsp return err;
1343 3c1ec782 2020-07-23 stsp }
1344 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1345 3c1ec782 2020-07-23 stsp free(abspath);
1346 3c1ec782 2020-07-23 stsp if (err)
1347 3c1ec782 2020-07-23 stsp return err;
1348 3c1ec782 2020-07-23 stsp } else {
1349 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1350 3c1ec782 2020-07-23 stsp if (err)
1351 3c1ec782 2020-07-23 stsp return err;
1352 3c1ec782 2020-07-23 stsp }
1353 3c1ec782 2020-07-23 stsp
1354 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1355 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1356 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1357 3c1ec782 2020-07-23 stsp return NULL;
1358 3c1ec782 2020-07-23 stsp }
1359 3c1ec782 2020-07-23 stsp
1360 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1361 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1362 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1363 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1364 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1365 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1366 3c1ec782 2020-07-23 stsp
1367 3c1ec782 2020-07-23 stsp free(path_got);
1368 3c1ec782 2020-07-23 stsp return NULL;
1369 5a1fbc73 2020-07-23 stsp }
1370 5a1fbc73 2020-07-23 stsp
1371 5a1fbc73 2020-07-23 stsp static const struct got_error *
1372 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1373 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1374 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1375 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1376 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1377 9d31a1d8 2018-03-11 stsp {
1378 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1379 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1380 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1381 906c123b 2020-07-23 stsp char *path_got = NULL;
1382 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1383 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1384 8ba819a3 2020-07-23 stsp
1385 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1386 2e1fa222 2020-07-23 stsp
1387 8ba819a3 2020-07-23 stsp /*
1388 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1389 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1390 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1391 8ba819a3 2020-07-23 stsp * in the blob object.
1392 8ba819a3 2020-07-23 stsp */
1393 8ba819a3 2020-07-23 stsp do {
1394 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1395 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1396 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1397 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1398 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1399 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1400 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1401 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1402 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1403 3b9f0f87 2020-07-23 stsp progress_arg);
1404 8ba819a3 2020-07-23 stsp }
1405 8ba819a3 2020-07-23 stsp if (len > 0) {
1406 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1407 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1408 8ba819a3 2020-07-23 stsp len - hdrlen);
1409 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1410 8ba819a3 2020-07-23 stsp hdrlen = 0;
1411 8ba819a3 2020-07-23 stsp }
1412 8ba819a3 2020-07-23 stsp } while (len != 0);
1413 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1414 8ba819a3 2020-07-23 stsp
1415 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1416 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1417 3c1ec782 2020-07-23 stsp if (err)
1418 3c1ec782 2020-07-23 stsp return err;
1419 8ba819a3 2020-07-23 stsp
1420 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1421 906c123b 2020-07-23 stsp /* install as a regular file */
1422 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1423 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1424 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1425 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1426 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1427 906c123b 2020-07-23 stsp goto done;
1428 906c123b 2020-07-23 stsp }
1429 906c123b 2020-07-23 stsp
1430 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1431 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1432 c6e8a826 2021-04-05 stsp int symlink_replaced;
1433 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1434 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1435 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1436 c90c8ce3 2020-07-23 stsp goto done;
1437 c90c8ce3 2020-07-23 stsp }
1438 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1439 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1440 5a1fbc73 2020-07-23 stsp if (err)
1441 f35fa46a 2020-07-23 stsp goto done;
1442 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1443 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1444 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1445 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1446 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1447 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1448 c6e8a826 2021-04-05 stsp } else {
1449 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1450 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1451 c6e8a826 2021-04-05 stsp }
1452 f35fa46a 2020-07-23 stsp }
1453 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1454 f35fa46a 2020-07-23 stsp }
1455 f35fa46a 2020-07-23 stsp
1456 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1457 f4994adc 2020-10-20 stsp char *parent;
1458 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1459 f4994adc 2020-10-20 stsp if (err)
1460 f4994adc 2020-10-20 stsp goto done;
1461 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1462 f4994adc 2020-10-20 stsp free(parent);
1463 f35fa46a 2020-07-23 stsp if (err)
1464 f35fa46a 2020-07-23 stsp goto done;
1465 f35fa46a 2020-07-23 stsp /*
1466 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1467 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1468 f35fa46a 2020-07-23 stsp */
1469 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1470 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1471 f35fa46a 2020-07-23 stsp goto done;
1472 f35fa46a 2020-07-23 stsp }
1473 f35fa46a 2020-07-23 stsp }
1474 f35fa46a 2020-07-23 stsp
1475 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1476 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1477 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1478 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1479 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1480 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1481 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1482 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1483 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1484 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1485 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1486 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1487 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1488 8ba819a3 2020-07-23 stsp } else {
1489 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1490 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1491 8ba819a3 2020-07-23 stsp }
1492 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1493 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1494 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1495 8ba819a3 2020-07-23 stsp done:
1496 906c123b 2020-07-23 stsp free(path_got);
1497 8ba819a3 2020-07-23 stsp return err;
1498 8ba819a3 2020-07-23 stsp }
1499 8ba819a3 2020-07-23 stsp
1500 8ba819a3 2020-07-23 stsp static const struct got_error *
1501 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1502 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1503 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1504 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1505 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1506 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1507 8ba819a3 2020-07-23 stsp {
1508 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1509 507dc3bb 2018-12-29 stsp int fd = -1;
1510 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1511 507dc3bb 2018-12-29 stsp int update = 0;
1512 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1513 8ba819a3 2020-07-23 stsp
1514 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1515 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1516 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1517 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1518 f5375317 2020-10-20 stsp char *parent;
1519 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1520 f5375317 2020-10-20 stsp if (err)
1521 f5375317 2020-10-20 stsp return err;
1522 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1523 f5375317 2020-10-20 stsp free(parent);
1524 21908da4 2019-01-13 stsp if (err)
1525 21908da4 2019-01-13 stsp return err;
1526 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1527 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1528 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1529 21908da4 2019-01-13 stsp if (fd == -1)
1530 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1531 230a42bd 2019-05-11 jcs ondisk_path);
1532 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1533 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1534 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1535 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1536 3b9f0f87 2020-07-23 stsp goto done;
1537 3b9f0f87 2020-07-23 stsp }
1538 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1539 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1540 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1541 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1542 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1543 507dc3bb 2018-12-29 stsp goto done;
1544 d70b8e30 2018-12-27 stsp } else {
1545 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1546 507dc3bb 2018-12-29 stsp ondisk_path);
1547 507dc3bb 2018-12-29 stsp if (err)
1548 507dc3bb 2018-12-29 stsp goto done;
1549 507dc3bb 2018-12-29 stsp update = 1;
1550 9d31a1d8 2018-03-11 stsp }
1551 507dc3bb 2018-12-29 stsp } else
1552 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1553 9d31a1d8 2018-03-11 stsp }
1554 9d31a1d8 2018-03-11 stsp
1555 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1556 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1557 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1558 3818e3c4 2020-11-01 naddy goto done;
1559 3818e3c4 2020-11-01 naddy }
1560 3818e3c4 2020-11-01 naddy
1561 bd6aa359 2020-07-23 stsp if (progress_cb) {
1562 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1563 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1564 bd6aa359 2020-07-23 stsp path);
1565 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1566 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1567 bd6aa359 2020-07-23 stsp path);
1568 bd6aa359 2020-07-23 stsp else
1569 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1570 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1571 bd6aa359 2020-07-23 stsp if (err)
1572 bd6aa359 2020-07-23 stsp goto done;
1573 bd6aa359 2020-07-23 stsp }
1574 d7b62c98 2018-12-27 stsp
1575 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1576 9d31a1d8 2018-03-11 stsp do {
1577 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1578 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1579 9d31a1d8 2018-03-11 stsp if (err)
1580 9d31a1d8 2018-03-11 stsp break;
1581 9d31a1d8 2018-03-11 stsp if (len > 0) {
1582 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1583 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1584 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1585 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1586 b87c6f83 2018-12-24 stsp goto done;
1587 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1588 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1589 b87c6f83 2018-12-24 stsp goto done;
1590 9d31a1d8 2018-03-11 stsp }
1591 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1592 9d31a1d8 2018-03-11 stsp }
1593 9d31a1d8 2018-03-11 stsp } while (len != 0);
1594 9d31a1d8 2018-03-11 stsp
1595 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1596 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1597 816dc654 2019-02-16 stsp goto done;
1598 816dc654 2019-02-16 stsp }
1599 9d31a1d8 2018-03-11 stsp
1600 507dc3bb 2018-12-29 stsp if (update) {
1601 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1602 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1603 f6d8c0ac 2020-11-09 stsp goto done;
1604 f6d8c0ac 2020-11-09 stsp }
1605 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1606 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1607 230a42bd 2019-05-11 jcs ondisk_path);
1608 68ed9ba5 2019-02-10 stsp goto done;
1609 68ed9ba5 2019-02-10 stsp }
1610 63df146d 2020-11-09 stsp free(tmppath);
1611 63df146d 2020-11-09 stsp tmppath = NULL;
1612 507dc3bb 2018-12-29 stsp }
1613 507dc3bb 2018-12-29 stsp
1614 9d31a1d8 2018-03-11 stsp done:
1615 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1616 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1617 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1618 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1619 507dc3bb 2018-12-29 stsp free(tmppath);
1620 6353ad76 2019-02-08 stsp return err;
1621 6353ad76 2019-02-08 stsp }
1622 6353ad76 2019-02-08 stsp
1623 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1624 6353ad76 2019-02-08 stsp static const struct got_error *
1625 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1626 7154f6ce 2019-03-27 stsp {
1627 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1628 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1629 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1630 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1631 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1632 7154f6ce 2019-03-27 stsp };
1633 7154f6ce 2019-03-27 stsp int i = 0;
1634 9bdd68dd 2020-01-02 naddy char *line = NULL;
1635 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1636 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1637 7154f6ce 2019-03-27 stsp
1638 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1639 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1640 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1641 7154f6ce 2019-03-27 stsp if (feof(f))
1642 7154f6ce 2019-03-27 stsp break;
1643 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1644 7154f6ce 2019-03-27 stsp break;
1645 7154f6ce 2019-03-27 stsp }
1646 7154f6ce 2019-03-27 stsp
1647 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1648 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1649 19332e6d 2019-05-13 stsp == 0)
1650 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1651 7154f6ce 2019-03-27 stsp else
1652 7154f6ce 2019-03-27 stsp i++;
1653 7154f6ce 2019-03-27 stsp }
1654 7154f6ce 2019-03-27 stsp }
1655 9bdd68dd 2020-01-02 naddy free(line);
1656 7154f6ce 2019-03-27 stsp
1657 7154f6ce 2019-03-27 stsp return err;
1658 e2b1e152 2019-07-13 stsp }
1659 e2b1e152 2019-07-13 stsp
1660 e2b1e152 2019-07-13 stsp static int
1661 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1662 1ebedb77 2019-10-19 stsp {
1663 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1664 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1665 1ebedb77 2019-10-19 stsp }
1666 1ebedb77 2019-10-19 stsp
1667 1ebedb77 2019-10-19 stsp static int
1668 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1669 e2b1e152 2019-07-13 stsp {
1670 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1671 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1672 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1673 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1674 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1675 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1676 c363b2c1 2019-08-03 stsp }
1677 c363b2c1 2019-08-03 stsp
1678 c363b2c1 2019-08-03 stsp static unsigned char
1679 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1680 c363b2c1 2019-08-03 stsp {
1681 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1682 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1683 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1684 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1685 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1686 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1687 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1688 c363b2c1 2019-08-03 stsp default:
1689 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1690 a919d5c4 2020-07-23 stsp }
1691 a919d5c4 2020-07-23 stsp }
1692 a919d5c4 2020-07-23 stsp
1693 a919d5c4 2020-07-23 stsp static const struct got_error *
1694 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1695 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1696 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1697 a919d5c4 2020-07-23 stsp {
1698 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1699 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1700 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1701 a919d5c4 2020-07-23 stsp ssize_t elen;
1702 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1703 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1704 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1705 a919d5c4 2020-07-23 stsp
1706 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1707 a919d5c4 2020-07-23 stsp
1708 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1709 a919d5c4 2020-07-23 stsp do {
1710 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1711 a919d5c4 2020-07-23 stsp if (err)
1712 a919d5c4 2020-07-23 stsp return err;
1713 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1714 a919d5c4 2020-07-23 stsp /*
1715 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1716 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1717 a919d5c4 2020-07-23 stsp */
1718 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1719 a919d5c4 2020-07-23 stsp }
1720 a919d5c4 2020-07-23 stsp if (len > 0) {
1721 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1722 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1723 a919d5c4 2020-07-23 stsp len - hdrlen);
1724 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1725 a919d5c4 2020-07-23 stsp hdrlen = 0;
1726 a919d5c4 2020-07-23 stsp }
1727 a919d5c4 2020-07-23 stsp } while (len != 0);
1728 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1729 a919d5c4 2020-07-23 stsp
1730 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1731 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1732 a919d5c4 2020-07-23 stsp if (elen == -1)
1733 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1734 a919d5c4 2020-07-23 stsp } else {
1735 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1736 a919d5c4 2020-07-23 stsp if (elen == -1)
1737 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1738 c363b2c1 2019-08-03 stsp }
1739 a919d5c4 2020-07-23 stsp
1740 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1741 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1742 a919d5c4 2020-07-23 stsp
1743 a919d5c4 2020-07-23 stsp return NULL;
1744 7154f6ce 2019-03-27 stsp }
1745 7154f6ce 2019-03-27 stsp
1746 7154f6ce 2019-03-27 stsp static const struct got_error *
1747 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1748 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1749 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1750 6353ad76 2019-02-08 stsp {
1751 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1752 6353ad76 2019-02-08 stsp struct got_object_id id;
1753 6353ad76 2019-02-08 stsp size_t hdrlen;
1754 1338848f 2019-12-13 stsp int fd = -1;
1755 6353ad76 2019-02-08 stsp FILE *f = NULL;
1756 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1757 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1758 6353ad76 2019-02-08 stsp size_t flen, blen;
1759 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1760 6353ad76 2019-02-08 stsp
1761 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1762 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1763 6353ad76 2019-02-08 stsp
1764 7f91a133 2019-12-13 stsp /*
1765 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1766 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1767 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1768 7f91a133 2019-12-13 stsp */
1769 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1770 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1771 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1772 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1773 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1774 882ef1b9 2019-12-13 stsp else
1775 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1776 882ef1b9 2019-12-13 stsp goto done;
1777 882ef1b9 2019-12-13 stsp }
1778 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1779 3d35a492 2019-12-13 stsp goto done;
1780 3d35a492 2019-12-13 stsp }
1781 7f91a133 2019-12-13 stsp } else {
1782 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1783 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1784 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1785 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1786 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1787 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1788 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1789 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1790 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1791 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1792 3d35a492 2019-12-13 stsp else
1793 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1794 3d35a492 2019-12-13 stsp goto done;
1795 3d35a492 2019-12-13 stsp }
1796 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1797 1338848f 2019-12-13 stsp goto done;
1798 a378724f 2019-02-10 stsp }
1799 a378724f 2019-02-10 stsp }
1800 6353ad76 2019-02-08 stsp
1801 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1802 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1803 1338848f 2019-12-13 stsp goto done;
1804 3f148bc6 2019-07-27 stsp }
1805 efdd40df 2019-07-27 stsp
1806 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1807 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1808 1338848f 2019-12-13 stsp goto done;
1809 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1810 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1811 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1812 1338848f 2019-12-13 stsp goto done;
1813 d00136be 2019-03-26 stsp }
1814 b8f41171 2019-02-10 stsp
1815 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1816 f179e66d 2020-07-23 stsp goto done;
1817 f179e66d 2020-07-23 stsp
1818 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1819 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1820 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1821 1338848f 2019-12-13 stsp goto done;
1822 f179e66d 2020-07-23 stsp }
1823 6353ad76 2019-02-08 stsp
1824 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1825 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1826 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1827 c363b2c1 2019-08-03 stsp else
1828 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1829 c363b2c1 2019-08-03 stsp
1830 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1831 6353ad76 2019-02-08 stsp if (err)
1832 1338848f 2019-12-13 stsp goto done;
1833 6353ad76 2019-02-08 stsp
1834 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1835 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1836 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1837 a919d5c4 2020-07-23 stsp goto done;
1838 a919d5c4 2020-07-23 stsp }
1839 a919d5c4 2020-07-23 stsp
1840 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1841 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1842 ab0d4361 2019-12-13 stsp if (fd == -1) {
1843 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1844 ab0d4361 2019-12-13 stsp goto done;
1845 ab0d4361 2019-12-13 stsp }
1846 3d35a492 2019-12-13 stsp }
1847 3d35a492 2019-12-13 stsp
1848 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1849 6353ad76 2019-02-08 stsp if (f == NULL) {
1850 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1851 6353ad76 2019-02-08 stsp goto done;
1852 6353ad76 2019-02-08 stsp }
1853 1338848f 2019-12-13 stsp fd = -1;
1854 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1855 656b1f76 2019-05-11 jcs for (;;) {
1856 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1857 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1858 6353ad76 2019-02-08 stsp if (err)
1859 7154f6ce 2019-03-27 stsp goto done;
1860 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1861 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1862 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1863 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1864 7154f6ce 2019-03-27 stsp goto done;
1865 80c5c120 2019-02-19 stsp }
1866 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1867 6353ad76 2019-02-08 stsp if (flen != 0)
1868 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1869 6353ad76 2019-02-08 stsp break;
1870 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1871 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1872 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1873 6353ad76 2019-02-08 stsp break;
1874 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1875 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1876 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1877 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1878 6353ad76 2019-02-08 stsp break;
1879 6353ad76 2019-02-08 stsp }
1880 6353ad76 2019-02-08 stsp } else {
1881 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1882 6353ad76 2019-02-08 stsp break;
1883 6353ad76 2019-02-08 stsp }
1884 6353ad76 2019-02-08 stsp hdrlen = 0;
1885 6353ad76 2019-02-08 stsp }
1886 7154f6ce 2019-03-27 stsp
1887 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1888 7154f6ce 2019-03-27 stsp rewind(f);
1889 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1890 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1891 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1892 6353ad76 2019-02-08 stsp done:
1893 6353ad76 2019-02-08 stsp if (blob)
1894 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1895 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1896 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1897 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1898 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1899 9d31a1d8 2018-03-11 stsp return err;
1900 e2b1e152 2019-07-13 stsp }
1901 e2b1e152 2019-07-13 stsp
1902 e2b1e152 2019-07-13 stsp /*
1903 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1904 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1905 e2b1e152 2019-07-13 stsp */
1906 e2b1e152 2019-07-13 stsp static const struct got_error *
1907 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1908 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1909 e2b1e152 2019-07-13 stsp {
1910 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1911 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1912 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1913 e2b1e152 2019-07-13 stsp
1914 e2b1e152 2019-07-13 stsp return NULL;
1915 9d31a1d8 2018-03-11 stsp }
1916 9d31a1d8 2018-03-11 stsp
1917 9d31a1d8 2018-03-11 stsp static const struct got_error *
1918 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1919 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1920 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1921 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1922 0584f854 2019-04-06 stsp void *progress_arg)
1923 9d31a1d8 2018-03-11 stsp {
1924 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1925 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1926 6353ad76 2019-02-08 stsp char *ondisk_path;
1927 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1928 b8f41171 2019-02-10 stsp struct stat sb;
1929 9d31a1d8 2018-03-11 stsp
1930 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1931 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1932 6353ad76 2019-02-08 stsp
1933 abb4604f 2019-07-27 stsp if (ie) {
1934 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1935 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1936 a76c42e6 2019-08-03 stsp goto done;
1937 a76c42e6 2019-08-03 stsp }
1938 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1939 7f91a133 2019-12-13 stsp repo);
1940 abb4604f 2019-07-27 stsp if (err)
1941 abb4604f 2019-07-27 stsp goto done;
1942 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1943 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1944 c90c8ce3 2020-07-23 stsp } else {
1945 f6764181 2021-09-24 stsp if (stat(ondisk_path, &sb) == -1) {
1946 f6764181 2021-09-24 stsp if (errno != ENOENT) {
1947 f6764181 2021-09-24 stsp err = got_error_from_errno2("stat",
1948 f6764181 2021-09-24 stsp ondisk_path);
1949 f6764181 2021-09-24 stsp goto done;
1950 f6764181 2021-09-24 stsp }
1951 f6764181 2021-09-24 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1952 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1953 f6764181 2021-09-24 stsp } else {
1954 f6764181 2021-09-24 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
1955 f6764181 2021-09-24 stsp status = GOT_STATUS_UNVERSIONED;
1956 f6764181 2021-09-24 stsp else
1957 f6764181 2021-09-24 stsp status = GOT_STATUS_OBSTRUCTED;
1958 f6764181 2021-09-24 stsp }
1959 c90c8ce3 2020-07-23 stsp }
1960 6353ad76 2019-02-08 stsp
1961 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1962 2c41dce7 2021-06-27 stsp if (ie)
1963 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1964 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1965 b8f41171 2019-02-10 stsp goto done;
1966 b8f41171 2019-02-10 stsp }
1967 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1968 a769b60b 2021-06-27 stsp if (ie)
1969 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1970 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1971 5036ab18 2020-04-18 stsp path);
1972 5036ab18 2020-04-18 stsp goto done;
1973 5036ab18 2020-04-18 stsp }
1974 b8f41171 2019-02-10 stsp
1975 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1976 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1977 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1978 c6e8a826 2021-04-05 stsp /*
1979 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1980 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1981 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1982 c6e8a826 2021-04-05 stsp * updating contents of this file.
1983 c6e8a826 2021-04-05 stsp */
1984 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1985 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1986 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1987 c6e8a826 2021-04-05 stsp /* Same commit. */
1988 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1989 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1990 e2b1e152 2019-07-13 stsp if (err)
1991 e2b1e152 2019-07-13 stsp goto done;
1992 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 b8f41171 2019-02-10 stsp path);
1994 6353ad76 2019-02-08 stsp goto done;
1995 a378724f 2019-02-10 stsp }
1996 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1997 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1998 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1999 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
2000 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
2001 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
2002 0f58026f 2021-04-05 stsp if (err)
2003 0f58026f 2021-04-05 stsp goto done;
2004 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
2005 0f58026f 2021-04-05 stsp path);
2006 b8f41171 2019-02-10 stsp goto done;
2007 e2b1e152 2019-07-13 stsp }
2008 9d31a1d8 2018-03-11 stsp }
2009 9d31a1d8 2018-03-11 stsp
2010 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2011 8da9e5f4 2019-01-12 stsp if (err)
2012 6353ad76 2019-02-08 stsp goto done;
2013 512f0d0e 2019-01-02 stsp
2014 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2015 234035bc 2019-06-01 stsp int update_timestamps;
2016 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2017 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2018 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2019 234035bc 2019-06-01 stsp struct got_object_id id2;
2020 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2021 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2022 234035bc 2019-06-01 stsp if (err)
2023 f69721c3 2019-10-21 stsp goto done;
2024 f69721c3 2019-10-21 stsp }
2025 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2026 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2027 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2028 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2029 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2030 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2031 f69721c3 2019-10-21 stsp goto done;
2032 f69721c3 2019-10-21 stsp }
2033 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2034 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2035 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2036 234035bc 2019-06-01 stsp goto done;
2037 f69721c3 2019-10-21 stsp }
2038 234035bc 2019-06-01 stsp }
2039 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2040 36bf999c 2020-07-23 stsp char *link_target;
2041 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2042 36bf999c 2020-07-23 stsp if (err)
2043 36bf999c 2020-07-23 stsp goto done;
2044 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2045 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2046 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2047 36bf999c 2020-07-23 stsp free(link_target);
2048 993e2a1b 2020-07-23 stsp } else {
2049 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2050 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2051 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2052 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2053 993e2a1b 2020-07-23 stsp }
2054 f69721c3 2019-10-21 stsp free(label_orig);
2055 234035bc 2019-06-01 stsp if (blob2)
2056 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2057 909d120e 2019-09-22 stsp if (err)
2058 909d120e 2019-09-22 stsp goto done;
2059 234035bc 2019-06-01 stsp /*
2060 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2061 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2062 234035bc 2019-06-01 stsp * unmodified files again.
2063 234035bc 2019-06-01 stsp */
2064 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2065 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2066 234035bc 2019-06-01 stsp update_timestamps);
2067 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2068 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2069 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2070 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2071 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2072 1ee397ad 2019-07-12 stsp if (err)
2073 1ee397ad 2019-07-12 stsp goto done;
2074 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2075 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2076 13d9040b 2019-03-27 stsp if (err)
2077 13d9040b 2019-03-27 stsp goto done;
2078 13d9040b 2019-03-27 stsp } else {
2079 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2080 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2081 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2082 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2083 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2084 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2085 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2086 2e1fa222 2020-07-23 stsp } else {
2087 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2088 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2089 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2090 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2091 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2092 2e1fa222 2020-07-23 stsp }
2093 13d9040b 2019-03-27 stsp if (err)
2094 13d9040b 2019-03-27 stsp goto done;
2095 65b05cec 2020-07-23 stsp
2096 054041d0 2020-06-24 stsp if (ie) {
2097 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2098 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2099 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2100 054041d0 2020-06-24 stsp } else {
2101 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2102 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2103 054041d0 2020-06-24 stsp &blob->id);
2104 054041d0 2020-06-24 stsp }
2105 13d9040b 2019-03-27 stsp if (err)
2106 13d9040b 2019-03-27 stsp goto done;
2107 65b05cec 2020-07-23 stsp
2108 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2109 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2110 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2111 2e1fa222 2020-07-23 stsp }
2112 13d9040b 2019-03-27 stsp }
2113 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2114 6353ad76 2019-02-08 stsp done:
2115 6353ad76 2019-02-08 stsp free(ondisk_path);
2116 8da9e5f4 2019-01-12 stsp return err;
2117 90285c3b 2019-01-08 stsp }
2118 90285c3b 2019-01-08 stsp
2119 90285c3b 2019-01-08 stsp static const struct got_error *
2120 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2121 90285c3b 2019-01-08 stsp {
2122 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2123 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2124 90285c3b 2019-01-08 stsp
2125 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2126 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2127 90285c3b 2019-01-08 stsp
2128 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2129 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2130 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2131 c97665ae 2019-01-13 stsp } else {
2132 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2133 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2134 1c4cdd89 2021-06-20 stsp if (err)
2135 1c4cdd89 2021-06-20 stsp goto done;
2136 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2137 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2138 fddefe3b 2020-10-20 stsp free(ondisk_path);
2139 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2140 1c4cdd89 2021-06-20 stsp parent = NULL;
2141 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2142 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2143 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2144 fddefe3b 2020-10-20 stsp ondisk_path);
2145 90285c3b 2019-01-08 stsp break;
2146 90285c3b 2019-01-08 stsp }
2147 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2148 1c4cdd89 2021-06-20 stsp if (err)
2149 1c4cdd89 2021-06-20 stsp break;
2150 1c4cdd89 2021-06-20 stsp }
2151 90285c3b 2019-01-08 stsp }
2152 1c4cdd89 2021-06-20 stsp done:
2153 90285c3b 2019-01-08 stsp free(ondisk_path);
2154 1c4cdd89 2021-06-20 stsp free(parent);
2155 90285c3b 2019-01-08 stsp return err;
2156 512f0d0e 2019-01-02 stsp }
2157 512f0d0e 2019-01-02 stsp
2158 708d8e67 2019-03-27 stsp static const struct got_error *
2159 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2160 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2161 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2162 708d8e67 2019-03-27 stsp {
2163 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2164 708d8e67 2019-03-27 stsp unsigned char status;
2165 708d8e67 2019-03-27 stsp struct stat sb;
2166 708d8e67 2019-03-27 stsp char *ondisk_path;
2167 708d8e67 2019-03-27 stsp
2168 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2169 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2170 a76c42e6 2019-08-03 stsp
2171 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2172 708d8e67 2019-03-27 stsp == -1)
2173 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2174 708d8e67 2019-03-27 stsp
2175 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2176 708d8e67 2019-03-27 stsp if (err)
2177 5a58a424 2020-06-23 stsp goto done;
2178 708d8e67 2019-03-27 stsp
2179 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2180 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2181 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2182 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2183 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2184 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2185 993e2a1b 2020-07-23 stsp goto done;
2186 993e2a1b 2020-07-23 stsp }
2187 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2188 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2189 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2190 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2191 993e2a1b 2020-07-23 stsp if (err)
2192 993e2a1b 2020-07-23 stsp goto done;
2193 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2194 993e2a1b 2020-07-23 stsp ie->path);
2195 993e2a1b 2020-07-23 stsp goto done;
2196 993e2a1b 2020-07-23 stsp }
2197 993e2a1b 2020-07-23 stsp
2198 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2199 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2200 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2201 1ee397ad 2019-07-12 stsp if (err)
2202 5a58a424 2020-06-23 stsp goto done;
2203 fc6346c4 2019-03-27 stsp /*
2204 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2205 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2206 fc6346c4 2019-03-27 stsp */
2207 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2208 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2209 fc6346c4 2019-03-27 stsp } else {
2210 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2211 1ee397ad 2019-07-12 stsp if (err)
2212 5a58a424 2020-06-23 stsp goto done;
2213 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2214 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2215 fc6346c4 2019-03-27 stsp if (err)
2216 5a58a424 2020-06-23 stsp goto done;
2217 fc6346c4 2019-03-27 stsp }
2218 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2219 708d8e67 2019-03-27 stsp }
2220 5a58a424 2020-06-23 stsp done:
2221 5a58a424 2020-06-23 stsp free(ondisk_path);
2222 fc6346c4 2019-03-27 stsp return err;
2223 708d8e67 2019-03-27 stsp }
2224 708d8e67 2019-03-27 stsp
2225 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2226 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2227 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2228 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2229 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2230 8da9e5f4 2019-01-12 stsp void *progress_arg;
2231 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2232 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2233 8da9e5f4 2019-01-12 stsp };
2234 8da9e5f4 2019-01-12 stsp
2235 512f0d0e 2019-01-02 stsp static const struct got_error *
2236 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2237 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2238 512f0d0e 2019-01-02 stsp {
2239 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2240 0584f854 2019-04-06 stsp
2241 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2243 512f0d0e 2019-01-02 stsp
2244 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2245 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2246 8da9e5f4 2019-01-12 stsp }
2247 512f0d0e 2019-01-02 stsp
2248 8da9e5f4 2019-01-12 stsp static const struct got_error *
2249 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2250 8da9e5f4 2019-01-12 stsp {
2251 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2252 7a9df742 2019-01-08 stsp
2253 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2254 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2255 0584f854 2019-04-06 stsp
2256 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2257 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2258 9d31a1d8 2018-03-11 stsp }
2259 9d31a1d8 2018-03-11 stsp
2260 9d31a1d8 2018-03-11 stsp static const struct got_error *
2261 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2262 9d31a1d8 2018-03-11 stsp {
2263 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2264 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2265 8da9e5f4 2019-01-12 stsp char *path;
2266 9d31a1d8 2018-03-11 stsp
2267 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2268 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2269 0584f854 2019-04-06 stsp
2270 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2271 63c5ca5d 2019-08-24 stsp return NULL;
2272 63c5ca5d 2019-08-24 stsp
2273 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2274 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2275 8da9e5f4 2019-01-12 stsp == -1)
2276 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2277 9d31a1d8 2018-03-11 stsp
2278 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2279 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2280 8da9e5f4 2019-01-12 stsp else
2281 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2282 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2283 9d31a1d8 2018-03-11 stsp
2284 8da9e5f4 2019-01-12 stsp free(path);
2285 0cd1c46a 2019-03-11 stsp return err;
2286 0cd1c46a 2019-03-11 stsp }
2287 0cd1c46a 2019-03-11 stsp
2288 b2118c49 2020-07-28 stsp const struct got_error *
2289 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2290 b2118c49 2020-07-28 stsp {
2291 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2292 b2118c49 2020-07-28 stsp
2293 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2294 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2295 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2296 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2297 b2118c49 2020-07-28 stsp }
2298 b2118c49 2020-07-28 stsp
2299 b2118c49 2020-07-28 stsp return NULL;
2300 b2118c49 2020-07-28 stsp }
2301 b2118c49 2020-07-28 stsp
2302 818c7501 2019-07-11 stsp static const struct got_error *
2303 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2304 0cd1c46a 2019-03-11 stsp {
2305 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2306 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2307 0cd1c46a 2019-03-11 stsp
2308 517bab73 2019-03-11 stsp *refname = NULL;
2309 517bab73 2019-03-11 stsp
2310 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2311 b2118c49 2020-07-28 stsp if (err)
2312 b2118c49 2020-07-28 stsp return err;
2313 0cd1c46a 2019-03-11 stsp
2314 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2315 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2316 0647c563 2019-03-11 stsp *refname = NULL;
2317 0cd1c46a 2019-03-11 stsp }
2318 517bab73 2019-03-11 stsp free(uuidstr);
2319 517bab73 2019-03-11 stsp return err;
2320 517bab73 2019-03-11 stsp }
2321 517bab73 2019-03-11 stsp
2322 818c7501 2019-07-11 stsp const struct got_error *
2323 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2324 818c7501 2019-07-11 stsp {
2325 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2326 818c7501 2019-07-11 stsp }
2327 818c7501 2019-07-11 stsp
2328 818c7501 2019-07-11 stsp static const struct got_error *
2329 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2330 818c7501 2019-07-11 stsp {
2331 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2332 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2333 818c7501 2019-07-11 stsp }
2334 818c7501 2019-07-11 stsp
2335 818c7501 2019-07-11 stsp static const struct got_error *
2336 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2337 818c7501 2019-07-11 stsp {
2338 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2339 818c7501 2019-07-11 stsp }
2340 818c7501 2019-07-11 stsp
2341 818c7501 2019-07-11 stsp static const struct got_error *
2342 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2343 818c7501 2019-07-11 stsp {
2344 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2345 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2346 818c7501 2019-07-11 stsp }
2347 818c7501 2019-07-11 stsp
2348 818c7501 2019-07-11 stsp static const struct got_error *
2349 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2350 818c7501 2019-07-11 stsp {
2351 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2352 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2353 0ebf8283 2019-07-24 stsp }
2354 0ebf8283 2019-07-24 stsp
2355 0ebf8283 2019-07-24 stsp static const struct got_error *
2356 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2357 0ebf8283 2019-07-24 stsp {
2358 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2359 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2360 0ebf8283 2019-07-24 stsp }
2361 0ebf8283 2019-07-24 stsp
2362 0ebf8283 2019-07-24 stsp static const struct got_error *
2363 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2364 0ebf8283 2019-07-24 stsp {
2365 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2366 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2367 0ebf8283 2019-07-24 stsp }
2368 0ebf8283 2019-07-24 stsp
2369 0ebf8283 2019-07-24 stsp static const struct got_error *
2370 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2371 0ebf8283 2019-07-24 stsp {
2372 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2373 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2374 818c7501 2019-07-11 stsp }
2375 818c7501 2019-07-11 stsp
2376 0ebf8283 2019-07-24 stsp static const struct got_error *
2377 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2378 0ebf8283 2019-07-24 stsp {
2379 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2380 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2381 0ebf8283 2019-07-24 stsp }
2382 818c7501 2019-07-11 stsp
2383 0ebf8283 2019-07-24 stsp const struct got_error *
2384 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2385 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2386 0ebf8283 2019-07-24 stsp {
2387 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2388 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2389 0ebf8283 2019-07-24 stsp *path = NULL;
2390 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2391 0ebf8283 2019-07-24 stsp }
2392 0ebf8283 2019-07-24 stsp return NULL;
2393 f259c4c1 2021-09-24 stsp }
2394 f259c4c1 2021-09-24 stsp
2395 f259c4c1 2021-09-24 stsp static const struct got_error *
2396 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2397 f259c4c1 2021-09-24 stsp {
2398 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2399 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2400 0ebf8283 2019-07-24 stsp }
2401 0ebf8283 2019-07-24 stsp
2402 f259c4c1 2021-09-24 stsp static const struct got_error *
2403 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2404 f259c4c1 2021-09-24 stsp {
2405 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2406 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2407 f259c4c1 2021-09-24 stsp }
2408 f259c4c1 2021-09-24 stsp
2409 517bab73 2019-03-11 stsp /*
2410 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2411 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2412 517bab73 2019-03-11 stsp */
2413 517bab73 2019-03-11 stsp static const struct got_error *
2414 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2415 517bab73 2019-03-11 stsp {
2416 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2417 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2418 517bab73 2019-03-11 stsp char *refname;
2419 517bab73 2019-03-11 stsp
2420 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2421 517bab73 2019-03-11 stsp if (err)
2422 517bab73 2019-03-11 stsp return err;
2423 0cd1c46a 2019-03-11 stsp
2424 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2425 0cd1c46a 2019-03-11 stsp if (err)
2426 0cd1c46a 2019-03-11 stsp goto done;
2427 0cd1c46a 2019-03-11 stsp
2428 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2429 0cd1c46a 2019-03-11 stsp done:
2430 0cd1c46a 2019-03-11 stsp free(refname);
2431 0cd1c46a 2019-03-11 stsp if (ref)
2432 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2433 3e3a69f1 2019-07-25 stsp return err;
2434 3e3a69f1 2019-07-25 stsp }
2435 3e3a69f1 2019-07-25 stsp
2436 3e3a69f1 2019-07-25 stsp static const struct got_error *
2437 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2438 3e3a69f1 2019-07-25 stsp {
2439 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2440 3e3a69f1 2019-07-25 stsp
2441 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2442 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2443 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2444 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2445 3e3a69f1 2019-07-25 stsp }
2446 9d31a1d8 2018-03-11 stsp return err;
2447 9d31a1d8 2018-03-11 stsp }
2448 9d31a1d8 2018-03-11 stsp
2449 3e3a69f1 2019-07-25 stsp
2450 ebf99748 2019-05-09 stsp static const struct got_error *
2451 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2452 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2453 ebf99748 2019-05-09 stsp {
2454 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2455 ebf99748 2019-05-09 stsp FILE *index = NULL;
2456 0cd1c46a 2019-03-11 stsp
2457 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2458 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2459 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2460 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2461 ebf99748 2019-05-09 stsp
2462 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2463 3e3a69f1 2019-07-25 stsp if (err)
2464 ebf99748 2019-05-09 stsp goto done;
2465 ebf99748 2019-05-09 stsp
2466 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2467 ebf99748 2019-05-09 stsp if (index == NULL) {
2468 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2469 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2470 ebf99748 2019-05-09 stsp } else {
2471 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2472 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2473 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2474 ebf99748 2019-05-09 stsp }
2475 ebf99748 2019-05-09 stsp done:
2476 ebf99748 2019-05-09 stsp if (err) {
2477 ebf99748 2019-05-09 stsp free(*fileindex_path);
2478 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2479 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2480 ebf99748 2019-05-09 stsp *fileindex = NULL;
2481 ebf99748 2019-05-09 stsp }
2482 ebf99748 2019-05-09 stsp return err;
2483 ebf99748 2019-05-09 stsp }
2484 c932eeeb 2019-05-22 stsp
2485 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2486 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2487 c932eeeb 2019-05-22 stsp const char *path;
2488 c932eeeb 2019-05-22 stsp size_t path_len;
2489 c932eeeb 2019-05-22 stsp const char *entry_name;
2490 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2491 a484d721 2019-06-10 stsp void *progress_arg;
2492 c932eeeb 2019-05-22 stsp };
2493 ebf99748 2019-05-09 stsp
2494 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2495 c932eeeb 2019-05-22 stsp static const struct got_error *
2496 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2497 c932eeeb 2019-05-22 stsp {
2498 1ee397ad 2019-07-12 stsp const struct got_error *err;
2499 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2500 c932eeeb 2019-05-22 stsp
2501 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2502 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2503 c932eeeb 2019-05-22 stsp return NULL;
2504 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2505 102ff934 2019-06-11 stsp return NULL;
2506 102ff934 2019-06-11 stsp
2507 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2508 a769b60b 2021-06-27 stsp return NULL;
2509 a769b60b 2021-06-27 stsp
2510 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2511 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2512 c932eeeb 2019-05-22 stsp return NULL;
2513 c932eeeb 2019-05-22 stsp
2514 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2515 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2516 edd02c5e 2019-07-11 stsp ie->path);
2517 1ee397ad 2019-07-12 stsp if (err)
2518 1ee397ad 2019-07-12 stsp return err;
2519 1ee397ad 2019-07-12 stsp }
2520 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2521 c932eeeb 2019-05-22 stsp return NULL;
2522 a615e0e7 2020-12-16 stsp }
2523 a615e0e7 2020-12-16 stsp
2524 a615e0e7 2020-12-16 stsp static const struct got_error *
2525 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2526 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2527 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2528 a615e0e7 2020-12-16 stsp {
2529 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2530 a615e0e7 2020-12-16 stsp
2531 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2532 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2533 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2534 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2535 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2536 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2537 a615e0e7 2020-12-16 stsp
2538 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2539 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2540 9c6338c4 2019-06-02 stsp }
2541 9c6338c4 2019-06-02 stsp
2542 9c6338c4 2019-06-02 stsp static const struct got_error *
2543 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2544 9c6338c4 2019-06-02 stsp {
2545 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2546 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2547 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2548 867630bb 2020-01-17 stsp struct timespec timeout;
2549 9c6338c4 2019-06-02 stsp
2550 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2551 9c6338c4 2019-06-02 stsp fileindex_path);
2552 9c6338c4 2019-06-02 stsp if (err)
2553 9c6338c4 2019-06-02 stsp goto done;
2554 9c6338c4 2019-06-02 stsp
2555 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2556 9c6338c4 2019-06-02 stsp if (err)
2557 9c6338c4 2019-06-02 stsp goto done;
2558 9c6338c4 2019-06-02 stsp
2559 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2560 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2561 9c6338c4 2019-06-02 stsp fileindex_path);
2562 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2563 9c6338c4 2019-06-02 stsp }
2564 867630bb 2020-01-17 stsp
2565 867630bb 2020-01-17 stsp /*
2566 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2567 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2568 867630bb 2020-01-17 stsp * was recorded in the file index.
2569 867630bb 2020-01-17 stsp */
2570 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2571 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2572 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2573 9c6338c4 2019-06-02 stsp done:
2574 9c6338c4 2019-06-02 stsp if (new_index)
2575 9c6338c4 2019-06-02 stsp fclose(new_index);
2576 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2577 9c6338c4 2019-06-02 stsp return err;
2578 c932eeeb 2019-05-22 stsp }
2579 6ced4176 2019-07-12 stsp
2580 6ced4176 2019-07-12 stsp static const struct got_error *
2581 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2582 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2583 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2584 6ced4176 2019-07-12 stsp {
2585 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2586 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2587 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2588 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2589 6ced4176 2019-07-12 stsp
2590 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2591 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2592 6ced4176 2019-07-12 stsp *tree_id = NULL;
2593 6ced4176 2019-07-12 stsp
2594 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2595 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2596 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2597 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2598 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2599 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2600 6ced4176 2019-07-12 stsp goto done;
2601 6ced4176 2019-07-12 stsp }
2602 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2603 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2604 6ced4176 2019-07-12 stsp if (err)
2605 6ced4176 2019-07-12 stsp goto done;
2606 6ced4176 2019-07-12 stsp return NULL;
2607 6ced4176 2019-07-12 stsp }
2608 6ced4176 2019-07-12 stsp
2609 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2610 6ced4176 2019-07-12 stsp
2611 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2612 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2613 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2614 6ced4176 2019-07-12 stsp goto done;
2615 6ced4176 2019-07-12 stsp }
2616 6ced4176 2019-07-12 stsp
2617 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2618 6ced4176 2019-07-12 stsp in_repo_path);
2619 6ced4176 2019-07-12 stsp if (err)
2620 6ced4176 2019-07-12 stsp goto done;
2621 6ced4176 2019-07-12 stsp
2622 6ced4176 2019-07-12 stsp free(in_repo_path);
2623 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2624 6ced4176 2019-07-12 stsp
2625 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2626 6ced4176 2019-07-12 stsp if (err)
2627 6ced4176 2019-07-12 stsp goto done;
2628 c932eeeb 2019-05-22 stsp
2629 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2630 6ced4176 2019-07-12 stsp /* Check out a single file. */
2631 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2632 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2633 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2634 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2635 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2636 6ced4176 2019-07-12 stsp goto done;
2637 6ced4176 2019-07-12 stsp }
2638 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2639 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2640 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2641 6ced4176 2019-07-12 stsp goto done;
2642 6ced4176 2019-07-12 stsp }
2643 6ced4176 2019-07-12 stsp } else {
2644 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2645 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2646 6ced4176 2019-07-12 stsp if (err)
2647 6ced4176 2019-07-12 stsp return err;
2648 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2649 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2650 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2651 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2652 6ced4176 2019-07-12 stsp goto done;
2653 6ced4176 2019-07-12 stsp }
2654 6ced4176 2019-07-12 stsp }
2655 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2656 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2657 6ced4176 2019-07-12 stsp } else {
2658 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2659 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2660 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2661 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2662 6ced4176 2019-07-12 stsp goto done;
2663 6ced4176 2019-07-12 stsp }
2664 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2665 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2666 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2667 6ced4176 2019-07-12 stsp goto done;
2668 6ced4176 2019-07-12 stsp }
2669 6ced4176 2019-07-12 stsp }
2670 6ced4176 2019-07-12 stsp done:
2671 6ced4176 2019-07-12 stsp free(id);
2672 6ced4176 2019-07-12 stsp free(in_repo_path);
2673 6ced4176 2019-07-12 stsp if (err) {
2674 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2675 6ced4176 2019-07-12 stsp free(*tree_relpath);
2676 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2677 6ced4176 2019-07-12 stsp free(*tree_id);
2678 6ced4176 2019-07-12 stsp *tree_id = NULL;
2679 6ced4176 2019-07-12 stsp }
2680 07ed472d 2019-07-12 stsp return err;
2681 07ed472d 2019-07-12 stsp }
2682 07ed472d 2019-07-12 stsp
2683 07ed472d 2019-07-12 stsp static const struct got_error *
2684 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2685 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2686 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2687 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2688 07ed472d 2019-07-12 stsp {
2689 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2690 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2691 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2692 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2693 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2694 07ed472d 2019-07-12 stsp
2695 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2696 7f47418f 2019-12-20 stsp if (err) {
2697 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2698 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2699 7f47418f 2019-12-20 stsp goto done;
2700 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2701 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2702 7f47418f 2019-12-20 stsp if (err)
2703 7f47418f 2019-12-20 stsp return err;
2704 7f47418f 2019-12-20 stsp }
2705 07ed472d 2019-07-12 stsp
2706 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2707 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2708 07ed472d 2019-07-12 stsp if (err)
2709 07ed472d 2019-07-12 stsp goto done;
2710 07ed472d 2019-07-12 stsp
2711 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2712 07ed472d 2019-07-12 stsp if (err)
2713 07ed472d 2019-07-12 stsp goto done;
2714 07ed472d 2019-07-12 stsp
2715 07ed472d 2019-07-12 stsp if (entry_name &&
2716 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2717 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2718 07ed472d 2019-07-12 stsp goto done;
2719 07ed472d 2019-07-12 stsp }
2720 07ed472d 2019-07-12 stsp
2721 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2722 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2723 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2724 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2725 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2726 07ed472d 2019-07-12 stsp arg.repo = repo;
2727 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2728 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2729 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2730 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2731 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2732 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2733 07ed472d 2019-07-12 stsp done:
2734 07ed472d 2019-07-12 stsp if (tree)
2735 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2736 07ed472d 2019-07-12 stsp if (commit)
2737 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2738 6ced4176 2019-07-12 stsp return err;
2739 6ced4176 2019-07-12 stsp }
2740 6ced4176 2019-07-12 stsp
2741 9d31a1d8 2018-03-11 stsp const struct got_error *
2742 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2743 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2744 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2745 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2746 9d31a1d8 2018-03-11 stsp {
2747 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2748 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2749 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2750 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2751 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2752 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2753 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2754 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2755 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2756 f2ea84fa 2019-07-27 stsp int entry_type;
2757 f2ea84fa 2019-07-27 stsp char *relpath;
2758 f2ea84fa 2019-07-27 stsp char *entry_name;
2759 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2760 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2761 9d31a1d8 2018-03-11 stsp
2762 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2763 f2ea84fa 2019-07-27 stsp
2764 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2765 9d31a1d8 2018-03-11 stsp if (err)
2766 9d31a1d8 2018-03-11 stsp return err;
2767 93a30277 2018-12-24 stsp
2768 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2769 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2770 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2771 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2772 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2773 f2ea84fa 2019-07-27 stsp goto done;
2774 f2ea84fa 2019-07-27 stsp }
2775 6ced4176 2019-07-12 stsp
2776 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2777 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2778 f2ea84fa 2019-07-27 stsp if (err) {
2779 f2ea84fa 2019-07-27 stsp free(tpd);
2780 6ced4176 2019-07-12 stsp goto done;
2781 6ced4176 2019-07-12 stsp }
2782 f2ea84fa 2019-07-27 stsp
2783 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2784 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2785 f2ea84fa 2019-07-27 stsp if (err) {
2786 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2787 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2788 f2ea84fa 2019-07-27 stsp free(tpd);
2789 f2ea84fa 2019-07-27 stsp goto done;
2790 f2ea84fa 2019-07-27 stsp }
2791 f2ea84fa 2019-07-27 stsp } else
2792 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2793 f2ea84fa 2019-07-27 stsp
2794 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2795 6ced4176 2019-07-12 stsp }
2796 6ced4176 2019-07-12 stsp
2797 51514078 2018-12-25 stsp /*
2798 51514078 2018-12-25 stsp * Read the file index.
2799 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2800 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2801 51514078 2018-12-25 stsp */
2802 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2803 8da9e5f4 2019-01-12 stsp if (err)
2804 c4cdcb68 2019-04-03 stsp goto done;
2805 c4cdcb68 2019-04-03 stsp
2806 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2807 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2808 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2809 f2ea84fa 2019-07-27 stsp
2810 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2811 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2812 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2813 f2ea84fa 2019-07-27 stsp if (err)
2814 f2ea84fa 2019-07-27 stsp break;
2815 f2ea84fa 2019-07-27 stsp
2816 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2817 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2818 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2819 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2820 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2821 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2822 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2823 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2824 f2ea84fa 2019-07-27 stsp if (err)
2825 f2ea84fa 2019-07-27 stsp break;
2826 f2ea84fa 2019-07-27 stsp
2827 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2828 711d9cd9 2019-07-12 stsp }
2829 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2830 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2831 af12c6b9 2019-06-04 stsp err = sync_err;
2832 9d31a1d8 2018-03-11 stsp done:
2833 9c6338c4 2019-06-02 stsp free(fileindex_path);
2834 edfa7d7f 2018-09-11 stsp if (tree)
2835 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2836 9d31a1d8 2018-03-11 stsp if (commit)
2837 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2838 5ade8233 2019-07-12 stsp if (fileindex)
2839 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2840 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2841 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2842 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2843 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2844 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2845 f2ea84fa 2019-07-27 stsp free(tpd);
2846 f2ea84fa 2019-07-27 stsp }
2847 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2848 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2849 9d31a1d8 2018-03-11 stsp err = unlockerr;
2850 f8d1f275 2019-02-04 stsp return err;
2851 f8d1f275 2019-02-04 stsp }
2852 f8d1f275 2019-02-04 stsp
2853 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2854 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2855 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2856 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2857 234035bc 2019-06-01 stsp void *progress_arg;
2858 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2859 234035bc 2019-06-01 stsp void *cancel_arg;
2860 f69721c3 2019-10-21 stsp const char *label_orig;
2861 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2862 234035bc 2019-06-01 stsp };
2863 234035bc 2019-06-01 stsp
2864 234035bc 2019-06-01 stsp static const struct got_error *
2865 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2866 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2867 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2868 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2869 234035bc 2019-06-01 stsp {
2870 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2871 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2872 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2873 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2874 234035bc 2019-06-01 stsp struct stat sb;
2875 234035bc 2019-06-01 stsp unsigned char status;
2876 234035bc 2019-06-01 stsp int local_changes_subsumed;
2877 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2878 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2879 234035bc 2019-06-01 stsp
2880 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2881 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2882 d6c87207 2019-08-02 stsp strlen(path2));
2883 1ee397ad 2019-07-12 stsp if (ie == NULL)
2884 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2885 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2886 234035bc 2019-06-01 stsp
2887 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2888 234035bc 2019-06-01 stsp path2) == -1)
2889 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2890 234035bc 2019-06-01 stsp
2891 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2892 7f91a133 2019-12-13 stsp repo);
2893 234035bc 2019-06-01 stsp if (err)
2894 234035bc 2019-06-01 stsp goto done;
2895 234035bc 2019-06-01 stsp
2896 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2897 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2898 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2899 234035bc 2019-06-01 stsp goto done;
2900 234035bc 2019-06-01 stsp }
2901 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2902 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2903 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2904 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2905 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2906 234035bc 2019-06-01 stsp goto done;
2907 234035bc 2019-06-01 stsp }
2908 234035bc 2019-06-01 stsp
2909 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2910 36bf999c 2020-07-23 stsp char *link_target2;
2911 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2912 36bf999c 2020-07-23 stsp if (err)
2913 36bf999c 2020-07-23 stsp goto done;
2914 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2915 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2916 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2917 36bf999c 2020-07-23 stsp free(link_target2);
2918 af57b12a 2020-07-23 stsp } else {
2919 1af628f4 2021-06-03 stsp int fd;
2920 1af628f4 2021-06-03 stsp
2921 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2922 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2923 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2924 1af628f4 2021-06-03 stsp goto done;
2925 1af628f4 2021-06-03 stsp }
2926 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2927 1af628f4 2021-06-03 stsp f_orig, blob1);
2928 1af628f4 2021-06-03 stsp if (err)
2929 1af628f4 2021-06-03 stsp goto done;
2930 1af628f4 2021-06-03 stsp
2931 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2932 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2933 1af628f4 2021-06-03 stsp goto done;
2934 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2935 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2936 1af628f4 2021-06-03 stsp if (err)
2937 1af628f4 2021-06-03 stsp goto done;
2938 1af628f4 2021-06-03 stsp
2939 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2940 1af628f4 2021-06-03 stsp if (fd == -1) {
2941 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2942 1af628f4 2021-06-03 stsp ondisk_path);
2943 1af628f4 2021-06-03 stsp goto done;
2944 1af628f4 2021-06-03 stsp }
2945 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2946 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2947 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2948 1af628f4 2021-06-03 stsp ondisk_path);
2949 1af628f4 2021-06-03 stsp close(fd);
2950 1af628f4 2021-06-03 stsp goto done;
2951 1af628f4 2021-06-03 stsp }
2952 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2953 1af628f4 2021-06-03 stsp if (err)
2954 1af628f4 2021-06-03 stsp goto done;
2955 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2956 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2957 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2958 1af628f4 2021-06-03 stsp goto done;
2959 1af628f4 2021-06-03 stsp }
2960 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2961 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2962 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2963 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2964 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2965 af57b12a 2020-07-23 stsp }
2966 234035bc 2019-06-01 stsp } else if (blob1) {
2967 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2968 d6c87207 2019-08-02 stsp strlen(path1));
2969 1ee397ad 2019-07-12 stsp if (ie == NULL)
2970 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2971 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2972 2b92fad7 2019-06-02 stsp
2973 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2974 2b92fad7 2019-06-02 stsp path1) == -1)
2975 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2976 2b92fad7 2019-06-02 stsp
2977 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2978 7f91a133 2019-12-13 stsp repo);
2979 2b92fad7 2019-06-02 stsp if (err)
2980 2b92fad7 2019-06-02 stsp goto done;
2981 2b92fad7 2019-06-02 stsp
2982 2b92fad7 2019-06-02 stsp switch (status) {
2983 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2984 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2985 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2986 1ee397ad 2019-07-12 stsp if (err)
2987 1ee397ad 2019-07-12 stsp goto done;
2988 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2989 2b92fad7 2019-06-02 stsp if (err)
2990 2b92fad7 2019-06-02 stsp goto done;
2991 2b92fad7 2019-06-02 stsp if (ie)
2992 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2993 2b92fad7 2019-06-02 stsp break;
2994 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2995 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2996 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2997 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2998 1ee397ad 2019-07-12 stsp if (err)
2999 1ee397ad 2019-07-12 stsp goto done;
3000 2b92fad7 2019-06-02 stsp if (ie)
3001 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3002 2b92fad7 2019-06-02 stsp break;
3003 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
3004 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
3005 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
3006 0a22ca1a 2020-09-23 stsp /*
3007 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
3008 0a22ca1a 2020-09-23 stsp * exists in the repository.
3009 0a22ca1a 2020-09-23 stsp */
3010 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
3011 0a22ca1a 2020-09-23 stsp if (err)
3012 0a22ca1a 2020-09-23 stsp goto done;
3013 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3014 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3015 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3016 0a22ca1a 2020-09-23 stsp if (err)
3017 0a22ca1a 2020-09-23 stsp goto done;
3018 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3019 0a22ca1a 2020-09-23 stsp path1);
3020 0a22ca1a 2020-09-23 stsp if (err)
3021 0a22ca1a 2020-09-23 stsp goto done;
3022 0a22ca1a 2020-09-23 stsp if (ie)
3023 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3024 0a22ca1a 2020-09-23 stsp ie);
3025 0a22ca1a 2020-09-23 stsp } else {
3026 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3027 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3028 0a22ca1a 2020-09-23 stsp }
3029 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3030 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3031 0a22ca1a 2020-09-23 stsp free(id);
3032 0a22ca1a 2020-09-23 stsp if (err)
3033 0a22ca1a 2020-09-23 stsp goto done;
3034 0a22ca1a 2020-09-23 stsp break;
3035 0a22ca1a 2020-09-23 stsp }
3036 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3037 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3038 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3039 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3040 1ee397ad 2019-07-12 stsp if (err)
3041 1ee397ad 2019-07-12 stsp goto done;
3042 2b92fad7 2019-06-02 stsp break;
3043 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3044 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3045 1ee397ad 2019-07-12 stsp if (err)
3046 1ee397ad 2019-07-12 stsp goto done;
3047 2b92fad7 2019-06-02 stsp break;
3048 2b92fad7 2019-06-02 stsp default:
3049 2b92fad7 2019-06-02 stsp break;
3050 2b92fad7 2019-06-02 stsp }
3051 234035bc 2019-06-01 stsp } else if (blob2) {
3052 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3053 234035bc 2019-06-01 stsp path2) == -1)
3054 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3055 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3056 d6c87207 2019-08-02 stsp strlen(path2));
3057 234035bc 2019-06-01 stsp if (ie) {
3058 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3059 7f91a133 2019-12-13 stsp -1, NULL, repo);
3060 234035bc 2019-06-01 stsp if (err)
3061 234035bc 2019-06-01 stsp goto done;
3062 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3063 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3064 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3065 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3066 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3067 1ee397ad 2019-07-12 stsp status, path2);
3068 234035bc 2019-06-01 stsp goto done;
3069 234035bc 2019-06-01 stsp }
3070 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3071 36bf999c 2020-07-23 stsp char *link_target2;
3072 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3073 36bf999c 2020-07-23 stsp blob2);
3074 36bf999c 2020-07-23 stsp if (err)
3075 36bf999c 2020-07-23 stsp goto done;
3076 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3077 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3078 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3079 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3080 36bf999c 2020-07-23 stsp free(link_target2);
3081 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3082 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3083 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3084 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3085 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3086 526a746f 2020-07-23 stsp a->progress_arg);
3087 dfe9fba0 2020-07-23 stsp } else {
3088 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3089 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3090 526a746f 2020-07-23 stsp }
3091 dfe9fba0 2020-07-23 stsp if (err)
3092 dfe9fba0 2020-07-23 stsp goto done;
3093 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3094 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3095 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3096 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3097 234035bc 2019-06-01 stsp if (err)
3098 234035bc 2019-06-01 stsp goto done;
3099 234035bc 2019-06-01 stsp }
3100 234035bc 2019-06-01 stsp } else {
3101 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3102 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3103 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3104 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3105 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3106 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3107 2e1fa222 2020-07-23 stsp } else {
3108 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3109 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3110 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3111 2e1fa222 2020-07-23 stsp }
3112 234035bc 2019-06-01 stsp if (err)
3113 234035bc 2019-06-01 stsp goto done;
3114 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3115 234035bc 2019-06-01 stsp if (err)
3116 234035bc 2019-06-01 stsp goto done;
3117 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3118 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3119 3969253a 2020-03-07 stsp if (err) {
3120 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3121 3969253a 2020-03-07 stsp goto done;
3122 3969253a 2020-03-07 stsp }
3123 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3124 2b92fad7 2019-06-02 stsp if (err) {
3125 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3126 2b92fad7 2019-06-02 stsp goto done;
3127 2b92fad7 2019-06-02 stsp }
3128 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3129 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3130 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3131 2e1fa222 2020-07-23 stsp }
3132 234035bc 2019-06-01 stsp }
3133 234035bc 2019-06-01 stsp }
3134 234035bc 2019-06-01 stsp done:
3135 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3136 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3137 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3138 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3139 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3140 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3141 1af628f4 2021-06-03 stsp free(id_str);
3142 54d5be07 2021-06-03 stsp free(label_deriv2);
3143 234035bc 2019-06-01 stsp free(ondisk_path);
3144 234035bc 2019-06-01 stsp return err;
3145 234035bc 2019-06-01 stsp }
3146 234035bc 2019-06-01 stsp
3147 69de9dd4 2021-09-03 stsp static const struct got_error *
3148 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3149 69de9dd4 2021-09-03 stsp {
3150 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3151 69de9dd4 2021-09-03 stsp
3152 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3153 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3154 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3155 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3156 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3157 69de9dd4 2021-09-03 stsp
3158 69de9dd4 2021-09-03 stsp return NULL;
3159 69de9dd4 2021-09-03 stsp }
3160 69de9dd4 2021-09-03 stsp
3161 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3162 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3163 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3164 234035bc 2019-06-01 stsp struct got_repository *repo;
3165 234035bc 2019-06-01 stsp };
3166 234035bc 2019-06-01 stsp
3167 234035bc 2019-06-01 stsp static const struct got_error *
3168 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3169 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3170 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3171 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3172 234035bc 2019-06-01 stsp {
3173 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3174 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3175 234035bc 2019-06-01 stsp unsigned char status;
3176 234035bc 2019-06-01 stsp struct stat sb;
3177 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3178 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3179 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3180 234035bc 2019-06-01 stsp char *ondisk_path;
3181 234035bc 2019-06-01 stsp
3182 69de9dd4 2021-09-03 stsp if (id == NULL)
3183 69de9dd4 2021-09-03 stsp return NULL;
3184 234035bc 2019-06-01 stsp
3185 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3186 69de9dd4 2021-09-03 stsp if (ie == NULL)
3187 69de9dd4 2021-09-03 stsp return NULL;
3188 69de9dd4 2021-09-03 stsp
3189 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3190 234035bc 2019-06-01 stsp == -1)
3191 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3192 234035bc 2019-06-01 stsp
3193 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3194 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3195 5546d466 2021-09-02 stsp free(ondisk_path);
3196 234035bc 2019-06-01 stsp if (err)
3197 234035bc 2019-06-01 stsp return err;
3198 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3199 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3200 234035bc 2019-06-01 stsp
3201 234035bc 2019-06-01 stsp return NULL;
3202 234035bc 2019-06-01 stsp }
3203 234035bc 2019-06-01 stsp
3204 818c7501 2019-07-11 stsp static const struct got_error *
3205 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3206 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3207 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3208 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3209 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3210 234035bc 2019-06-01 stsp {
3211 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3212 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3213 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3214 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3215 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3216 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3217 234035bc 2019-06-01 stsp
3218 03415a1a 2019-06-02 stsp if (commit_id1) {
3219 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3220 03415a1a 2019-06-02 stsp worktree->path_prefix);
3221 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3222 03415a1a 2019-06-02 stsp goto done;
3223 69d57f3d 2020-07-31 stsp }
3224 69d57f3d 2020-07-31 stsp if (tree_id1) {
3225 69d57f3d 2020-07-31 stsp char *id_str;
3226 03415a1a 2019-06-02 stsp
3227 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3228 03415a1a 2019-06-02 stsp if (err)
3229 03415a1a 2019-06-02 stsp goto done;
3230 f69721c3 2019-10-21 stsp
3231 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3232 f69721c3 2019-10-21 stsp if (err)
3233 f69721c3 2019-10-21 stsp goto done;
3234 f69721c3 2019-10-21 stsp
3235 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3236 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3237 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3238 f69721c3 2019-10-21 stsp free(id_str);
3239 f69721c3 2019-10-21 stsp goto done;
3240 f69721c3 2019-10-21 stsp }
3241 f69721c3 2019-10-21 stsp free(id_str);
3242 03415a1a 2019-06-02 stsp }
3243 234035bc 2019-06-01 stsp
3244 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3245 234035bc 2019-06-01 stsp worktree->path_prefix);
3246 234035bc 2019-06-01 stsp if (err)
3247 234035bc 2019-06-01 stsp goto done;
3248 234035bc 2019-06-01 stsp
3249 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3250 234035bc 2019-06-01 stsp if (err)
3251 234035bc 2019-06-01 stsp goto done;
3252 234035bc 2019-06-01 stsp
3253 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3254 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3255 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3256 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3257 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3258 69de9dd4 2021-09-03 stsp if (err)
3259 69de9dd4 2021-09-03 stsp goto done;
3260 69de9dd4 2021-09-03 stsp
3261 234035bc 2019-06-01 stsp arg.worktree = worktree;
3262 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3263 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3264 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3265 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3266 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3267 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3268 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3269 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3270 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3271 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3272 af12c6b9 2019-06-04 stsp err = sync_err;
3273 234035bc 2019-06-01 stsp done:
3274 234035bc 2019-06-01 stsp if (tree1)
3275 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3276 234035bc 2019-06-01 stsp if (tree2)
3277 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3278 f69721c3 2019-10-21 stsp free(label_orig);
3279 818c7501 2019-07-11 stsp return err;
3280 818c7501 2019-07-11 stsp }
3281 234035bc 2019-06-01 stsp
3282 818c7501 2019-07-11 stsp const struct got_error *
3283 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3284 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3285 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3286 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3287 818c7501 2019-07-11 stsp {
3288 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3289 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3290 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3291 818c7501 2019-07-11 stsp
3292 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3293 818c7501 2019-07-11 stsp if (err)
3294 818c7501 2019-07-11 stsp return err;
3295 818c7501 2019-07-11 stsp
3296 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3297 818c7501 2019-07-11 stsp if (err)
3298 818c7501 2019-07-11 stsp goto done;
3299 818c7501 2019-07-11 stsp
3300 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3301 69de9dd4 2021-09-03 stsp worktree);
3302 818c7501 2019-07-11 stsp if (err)
3303 818c7501 2019-07-11 stsp goto done;
3304 818c7501 2019-07-11 stsp
3305 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3306 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3307 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3308 818c7501 2019-07-11 stsp done:
3309 818c7501 2019-07-11 stsp if (fileindex)
3310 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3311 818c7501 2019-07-11 stsp free(fileindex_path);
3312 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3313 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3314 234035bc 2019-06-01 stsp err = unlockerr;
3315 234035bc 2019-06-01 stsp return err;
3316 234035bc 2019-06-01 stsp }
3317 234035bc 2019-06-01 stsp
3318 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3319 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3320 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3321 927df6b7 2019-02-10 stsp const char *status_path;
3322 927df6b7 2019-02-10 stsp size_t status_path_len;
3323 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3324 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3325 f8d1f275 2019-02-04 stsp void *status_arg;
3326 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3327 f8d1f275 2019-02-04 stsp void *cancel_arg;
3328 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3329 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3330 f2a9dc41 2019-12-13 tracey int report_unchanged;
3331 3143d852 2020-06-25 stsp int no_ignores;
3332 f8d1f275 2019-02-04 stsp };
3333 88d0e355 2019-08-03 stsp
3334 f8d1f275 2019-02-04 stsp static const struct got_error *
3335 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3336 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3337 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3338 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3339 927df6b7 2019-02-10 stsp {
3340 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3341 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3342 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3343 927df6b7 2019-02-10 stsp struct stat sb;
3344 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3345 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3346 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3347 927df6b7 2019-02-10 stsp
3348 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3349 98eaaa12 2019-08-03 stsp if (err)
3350 98eaaa12 2019-08-03 stsp return err;
3351 98eaaa12 2019-08-03 stsp
3352 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3353 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3354 98eaaa12 2019-08-03 stsp return NULL;
3355 98eaaa12 2019-08-03 stsp
3356 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3357 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3358 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3359 98eaaa12 2019-08-03 stsp }
3360 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3361 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3362 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3363 927df6b7 2019-02-10 stsp }
3364 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3365 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3366 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3367 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3368 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3369 98eaaa12 2019-08-03 stsp }
3370 98eaaa12 2019-08-03 stsp
3371 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3372 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3373 927df6b7 2019-02-10 stsp }
3374 927df6b7 2019-02-10 stsp
3375 927df6b7 2019-02-10 stsp static const struct got_error *
3376 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3377 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3378 f8d1f275 2019-02-04 stsp {
3379 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3380 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3381 f8d1f275 2019-02-04 stsp char *abspath;
3382 f8d1f275 2019-02-04 stsp
3383 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3384 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3385 0584f854 2019-04-06 stsp
3386 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3387 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3388 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3389 927df6b7 2019-02-10 stsp return NULL;
3390 927df6b7 2019-02-10 stsp
3391 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3392 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3393 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3394 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3395 f8d1f275 2019-02-04 stsp } else {
3396 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3397 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3398 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3399 f8d1f275 2019-02-04 stsp }
3400 f8d1f275 2019-02-04 stsp
3401 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3402 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3403 f8d1f275 2019-02-04 stsp free(abspath);
3404 9d31a1d8 2018-03-11 stsp return err;
3405 9d31a1d8 2018-03-11 stsp }
3406 f8d1f275 2019-02-04 stsp
3407 f8d1f275 2019-02-04 stsp static const struct got_error *
3408 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3409 f8d1f275 2019-02-04 stsp {
3410 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3411 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3412 2ec1f75b 2019-03-26 stsp unsigned char status;
3413 927df6b7 2019-02-10 stsp
3414 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3415 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3416 0584f854 2019-04-06 stsp
3417 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3418 927df6b7 2019-02-10 stsp return NULL;
3419 927df6b7 2019-02-10 stsp
3420 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3421 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3422 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3423 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3424 2ec1f75b 2019-03-26 stsp else
3425 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3426 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3427 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3428 6841da00 2019-08-08 stsp }
3429 6841da00 2019-08-08 stsp
3430 6841da00 2019-08-08 stsp void
3431 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3432 6841da00 2019-08-08 stsp {
3433 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3434 6841da00 2019-08-08 stsp
3435 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3436 6841da00 2019-08-08 stsp free((char *)pe->path);
3437 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3438 6841da00 2019-08-08 stsp }
3439 6841da00 2019-08-08 stsp
3440 6841da00 2019-08-08 stsp void
3441 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3442 6841da00 2019-08-08 stsp {
3443 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3444 6841da00 2019-08-08 stsp
3445 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3446 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3447 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3448 6841da00 2019-08-08 stsp free((char *)pe->path);
3449 6841da00 2019-08-08 stsp }
3450 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3451 6841da00 2019-08-08 stsp }
3452 6841da00 2019-08-08 stsp
3453 6841da00 2019-08-08 stsp static const struct got_error *
3454 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3455 6841da00 2019-08-08 stsp {
3456 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3457 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3458 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3459 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3460 6841da00 2019-08-08 stsp size_t linesize = 0;
3461 6841da00 2019-08-08 stsp ssize_t linelen;
3462 6841da00 2019-08-08 stsp
3463 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3464 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3465 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3466 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3467 6841da00 2019-08-08 stsp
3468 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3469 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3470 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3471 bd8de430 2019-10-04 stsp
3472 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3473 bd8de430 2019-10-04 stsp if (line[0] == '#')
3474 bd8de430 2019-10-04 stsp continue;
3475 bd8de430 2019-10-04 stsp
3476 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3477 bd8de430 2019-10-04 stsp if (line[0] == '!')
3478 bd8de430 2019-10-04 stsp continue;
3479 bd8de430 2019-10-04 stsp
3480 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3481 6841da00 2019-08-08 stsp line) == -1) {
3482 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3483 6841da00 2019-08-08 stsp goto done;
3484 6841da00 2019-08-08 stsp }
3485 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3486 6841da00 2019-08-08 stsp if (err)
3487 6841da00 2019-08-08 stsp goto done;
3488 6841da00 2019-08-08 stsp }
3489 6841da00 2019-08-08 stsp if (ferror(f)) {
3490 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3491 6841da00 2019-08-08 stsp goto done;
3492 6841da00 2019-08-08 stsp }
3493 6841da00 2019-08-08 stsp
3494 6841da00 2019-08-08 stsp dirpath = strdup(path);
3495 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3496 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3497 6841da00 2019-08-08 stsp goto done;
3498 6841da00 2019-08-08 stsp }
3499 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3500 6841da00 2019-08-08 stsp done:
3501 6841da00 2019-08-08 stsp free(line);
3502 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3503 6841da00 2019-08-08 stsp free(dirpath);
3504 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3505 6841da00 2019-08-08 stsp }
3506 6841da00 2019-08-08 stsp return err;
3507 6841da00 2019-08-08 stsp }
3508 6841da00 2019-08-08 stsp
3509 6841da00 2019-08-08 stsp int
3510 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3511 6841da00 2019-08-08 stsp {
3512 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3513 bd8de430 2019-10-04 stsp
3514 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3515 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3516 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3517 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3518 bd8de430 2019-10-04 stsp
3519 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3520 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3521 6841da00 2019-08-08 stsp
3522 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3523 bd8de430 2019-10-04 stsp continue;
3524 bd8de430 2019-10-04 stsp pattern += 3;
3525 bd8de430 2019-10-04 stsp p = path;
3526 bd8de430 2019-10-04 stsp while (*p) {
3527 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3528 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3529 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3530 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3531 bd8de430 2019-10-04 stsp p++;
3532 bd8de430 2019-10-04 stsp while (*p == '/')
3533 bd8de430 2019-10-04 stsp p++;
3534 bd8de430 2019-10-04 stsp continue;
3535 bd8de430 2019-10-04 stsp }
3536 bd8de430 2019-10-04 stsp return 1;
3537 bd8de430 2019-10-04 stsp }
3538 bd8de430 2019-10-04 stsp }
3539 bd8de430 2019-10-04 stsp }
3540 bd8de430 2019-10-04 stsp
3541 6841da00 2019-08-08 stsp /*
3542 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3543 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3544 6841da00 2019-08-08 stsp * ignores backwards.
3545 6841da00 2019-08-08 stsp */
3546 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3547 6841da00 2019-08-08 stsp while (pe) {
3548 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3549 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3550 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3551 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3552 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3553 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3554 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3555 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3556 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3557 6841da00 2019-08-08 stsp continue;
3558 6841da00 2019-08-08 stsp return 1;
3559 6841da00 2019-08-08 stsp }
3560 6841da00 2019-08-08 stsp }
3561 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3562 6841da00 2019-08-08 stsp }
3563 6841da00 2019-08-08 stsp
3564 6841da00 2019-08-08 stsp return 0;
3565 6841da00 2019-08-08 stsp }
3566 6841da00 2019-08-08 stsp
3567 6841da00 2019-08-08 stsp static const struct got_error *
3568 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3569 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3570 6841da00 2019-08-08 stsp {
3571 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3572 6841da00 2019-08-08 stsp char *ignorespath;
3573 886cec17 2019-12-15 stsp int fd = -1;
3574 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3575 6841da00 2019-08-08 stsp
3576 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3577 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3578 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3579 6841da00 2019-08-08 stsp
3580 886cec17 2019-12-15 stsp if (dirfd != -1) {
3581 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3582 886cec17 2019-12-15 stsp if (fd == -1) {
3583 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3584 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3585 886cec17 2019-12-15 stsp ignorespath);
3586 886cec17 2019-12-15 stsp } else {
3587 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3588 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3589 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3590 886cec17 2019-12-15 stsp ignorespath);
3591 886cec17 2019-12-15 stsp else {
3592 886cec17 2019-12-15 stsp fd = -1;
3593 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3594 886cec17 2019-12-15 stsp }
3595 886cec17 2019-12-15 stsp }
3596 886cec17 2019-12-15 stsp } else {
3597 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3598 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3599 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3600 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3601 886cec17 2019-12-15 stsp ignorespath);
3602 886cec17 2019-12-15 stsp } else
3603 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3604 886cec17 2019-12-15 stsp }
3605 6841da00 2019-08-08 stsp
3606 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3607 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3608 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3609 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3610 6841da00 2019-08-08 stsp free(ignorespath);
3611 6841da00 2019-08-08 stsp return err;
3612 f8d1f275 2019-02-04 stsp }
3613 f8d1f275 2019-02-04 stsp
3614 f8d1f275 2019-02-04 stsp static const struct got_error *
3615 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3616 f8d1f275 2019-02-04 stsp {
3617 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3618 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3619 f8d1f275 2019-02-04 stsp char *path = NULL;
3620 f8d1f275 2019-02-04 stsp
3621 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3622 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3623 0584f854 2019-04-06 stsp
3624 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3625 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3626 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3627 f8d1f275 2019-02-04 stsp } else {
3628 f8d1f275 2019-02-04 stsp path = de->d_name;
3629 f8d1f275 2019-02-04 stsp }
3630 f8d1f275 2019-02-04 stsp
3631 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3632 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3633 ff56836b 2021-07-08 stsp && !match_ignores(a->ignores, path))
3634 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3635 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3636 f8d1f275 2019-02-04 stsp if (parent_path[0])
3637 f8d1f275 2019-02-04 stsp free(path);
3638 b72f483a 2019-02-05 stsp return err;
3639 f8d1f275 2019-02-04 stsp }
3640 f8d1f275 2019-02-04 stsp
3641 347d1d3e 2019-07-12 stsp static const struct got_error *
3642 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3643 3143d852 2020-06-25 stsp {
3644 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3645 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3646 3143d852 2020-06-25 stsp
3647 3143d852 2020-06-25 stsp if (a->no_ignores)
3648 3143d852 2020-06-25 stsp return NULL;
3649 3143d852 2020-06-25 stsp
3650 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3651 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3652 3143d852 2020-06-25 stsp if (err)
3653 3143d852 2020-06-25 stsp return err;
3654 3143d852 2020-06-25 stsp
3655 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3656 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3657 3143d852 2020-06-25 stsp
3658 3143d852 2020-06-25 stsp return err;
3659 3143d852 2020-06-25 stsp }
3660 3143d852 2020-06-25 stsp
3661 3143d852 2020-06-25 stsp static const struct got_error *
3662 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3663 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3664 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3665 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3666 abb4604f 2019-07-27 stsp {
3667 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3668 abb4604f 2019-07-27 stsp struct stat sb;
3669 abb4604f 2019-07-27 stsp
3670 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3671 ff56836b 2021-07-08 stsp return NULL;
3672 ff56836b 2021-07-08 stsp
3673 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3674 abb4604f 2019-07-27 stsp if (ie)
3675 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3676 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3677 abb4604f 2019-07-27 stsp
3678 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3679 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3680 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3681 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3682 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3683 abb4604f 2019-07-27 stsp return NULL;
3684 abb4604f 2019-07-27 stsp }
3685 abb4604f 2019-07-27 stsp
3686 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3687 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3688 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3689 abb4604f 2019-07-27 stsp
3690 abb4604f 2019-07-27 stsp return NULL;
3691 3143d852 2020-06-25 stsp }
3692 3143d852 2020-06-25 stsp
3693 3143d852 2020-06-25 stsp static const struct got_error *
3694 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3695 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3696 3143d852 2020-06-25 stsp {
3697 3143d852 2020-06-25 stsp const struct got_error *err;
3698 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3699 3143d852 2020-06-25 stsp
3700 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3701 3143d852 2020-06-25 stsp ".cvsignore");
3702 3143d852 2020-06-25 stsp if (err)
3703 3143d852 2020-06-25 stsp return err;
3704 3143d852 2020-06-25 stsp
3705 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3706 3143d852 2020-06-25 stsp ".gitignore");
3707 3143d852 2020-06-25 stsp if (err)
3708 3143d852 2020-06-25 stsp return err;
3709 3143d852 2020-06-25 stsp
3710 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3711 3143d852 2020-06-25 stsp if (err) {
3712 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3713 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3714 3143d852 2020-06-25 stsp return err;
3715 3143d852 2020-06-25 stsp }
3716 3143d852 2020-06-25 stsp for (;;) {
3717 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3718 3143d852 2020-06-25 stsp ".cvsignore");
3719 3143d852 2020-06-25 stsp if (err)
3720 3143d852 2020-06-25 stsp break;
3721 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3722 3143d852 2020-06-25 stsp ".gitignore");
3723 3143d852 2020-06-25 stsp if (err)
3724 3143d852 2020-06-25 stsp break;
3725 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3726 3143d852 2020-06-25 stsp if (err) {
3727 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3728 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3729 3143d852 2020-06-25 stsp break;
3730 3143d852 2020-06-25 stsp }
3731 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3732 ff56836b 2021-07-08 stsp break;
3733 b737c85a 2020-06-26 stsp free(parent_path);
3734 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3735 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3736 3143d852 2020-06-25 stsp }
3737 3143d852 2020-06-25 stsp
3738 b737c85a 2020-06-26 stsp free(parent_path);
3739 b737c85a 2020-06-26 stsp free(next_parent_path);
3740 3143d852 2020-06-25 stsp return err;
3741 abb4604f 2019-07-27 stsp }
3742 abb4604f 2019-07-27 stsp
3743 abb4604f 2019-07-27 stsp static const struct got_error *
3744 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3745 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3746 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3747 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3748 f2a9dc41 2019-12-13 tracey int report_unchanged)
3749 f8d1f275 2019-02-04 stsp {
3750 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3751 6fc93f37 2019-12-13 stsp int fd = -1;
3752 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3753 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3754 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3755 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3756 3143d852 2020-06-25 stsp
3757 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3758 f8d1f275 2019-02-04 stsp
3759 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3760 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3761 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3762 8dc303cc 2019-07-27 stsp
3763 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3764 6fc93f37 2019-12-13 stsp if (fd == -1) {
3765 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3766 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3767 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3768 ff56836b 2021-07-08 stsp else {
3769 ff56836b 2021-07-08 stsp if (!no_ignores) {
3770 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3771 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3772 ff56836b 2021-07-08 stsp if (err)
3773 ff56836b 2021-07-08 stsp goto done;
3774 ff56836b 2021-07-08 stsp }
3775 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3776 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3777 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3778 ff56836b 2021-07-08 stsp }
3779 abb4604f 2019-07-27 stsp } else {
3780 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3781 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3782 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3783 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3784 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3785 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3786 abb4604f 2019-07-27 stsp arg.status_path = path;
3787 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3788 abb4604f 2019-07-27 stsp arg.repo = repo;
3789 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3790 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3791 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3792 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3793 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3794 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3795 022fae89 2019-12-06 tracey if (!no_ignores) {
3796 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3797 3143d852 2020-06-25 stsp worktree->root_path, path);
3798 3143d852 2020-06-25 stsp if (err)
3799 3143d852 2020-06-25 stsp goto done;
3800 022fae89 2019-12-06 tracey }
3801 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3802 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3803 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3804 927df6b7 2019-02-10 stsp }
3805 3143d852 2020-06-25 stsp done:
3806 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3807 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3808 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3809 927df6b7 2019-02-10 stsp free(ondisk_path);
3810 347d1d3e 2019-07-12 stsp return err;
3811 347d1d3e 2019-07-12 stsp }
3812 347d1d3e 2019-07-12 stsp
3813 347d1d3e 2019-07-12 stsp const struct got_error *
3814 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3815 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3816 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3817 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3818 347d1d3e 2019-07-12 stsp {
3819 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3820 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3821 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3822 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3823 347d1d3e 2019-07-12 stsp
3824 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3825 347d1d3e 2019-07-12 stsp if (err)
3826 347d1d3e 2019-07-12 stsp return err;
3827 347d1d3e 2019-07-12 stsp
3828 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3829 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3830 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3831 f6343036 2021-06-22 stsp no_ignores, 0);
3832 72ea6654 2019-07-27 stsp if (err)
3833 72ea6654 2019-07-27 stsp break;
3834 72ea6654 2019-07-27 stsp }
3835 f8d1f275 2019-02-04 stsp free(fileindex_path);
3836 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3837 f8d1f275 2019-02-04 stsp return err;
3838 f8d1f275 2019-02-04 stsp }
3839 6c7ab921 2019-03-18 stsp
3840 6c7ab921 2019-03-18 stsp const struct got_error *
3841 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3842 6c7ab921 2019-03-18 stsp const char *arg)
3843 6c7ab921 2019-03-18 stsp {
3844 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3845 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3846 6c7ab921 2019-03-18 stsp size_t len;
3847 00bb5ea0 2020-07-23 stsp struct stat sb;
3848 01740607 2020-11-04 stsp char *abspath = NULL;
3849 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3850 6c7ab921 2019-03-18 stsp
3851 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3852 6c7ab921 2019-03-18 stsp
3853 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3854 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3855 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3856 00bb5ea0 2020-07-23 stsp
3857 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3858 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3859 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3860 00bb5ea0 2020-07-23 stsp goto done;
3861 00bb5ea0 2020-07-23 stsp }
3862 727173c3 2020-11-06 stsp sb.st_mode = 0;
3863 00bb5ea0 2020-07-23 stsp }
3864 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3865 00bb5ea0 2020-07-23 stsp /*
3866 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3867 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3868 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3869 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3870 00bb5ea0 2020-07-23 stsp */
3871 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3872 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3873 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3874 00bb5ea0 2020-07-23 stsp goto done;
3875 00bb5ea0 2020-07-23 stsp }
3876 00bb5ea0 2020-07-23 stsp
3877 00bb5ea0 2020-07-23 stsp }
3878 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3879 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3880 00bb5ea0 2020-07-23 stsp if (err)
3881 d0710d08 2019-07-22 stsp goto done;
3882 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3883 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3884 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3885 00bb5ea0 2020-07-23 stsp goto done;
3886 00bb5ea0 2020-07-23 stsp }
3887 00bb5ea0 2020-07-23 stsp } else {
3888 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3889 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3890 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3891 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3892 00bb5ea0 2020-07-23 stsp goto done;
3893 00bb5ea0 2020-07-23 stsp }
3894 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3895 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3896 01740607 2020-11-04 stsp goto done;
3897 01740607 2020-11-04 stsp }
3898 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3899 01740607 2020-11-04 stsp sizeof(canonpath));
3900 01740607 2020-11-04 stsp if (err)
3901 00bb5ea0 2020-07-23 stsp goto done;
3902 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3903 01740607 2020-11-04 stsp if (resolved == NULL) {
3904 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3905 01740607 2020-11-04 stsp goto done;
3906 00bb5ea0 2020-07-23 stsp }
3907 d0710d08 2019-07-22 stsp }
3908 d0710d08 2019-07-22 stsp }
3909 6c7ab921 2019-03-18 stsp
3910 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3911 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3912 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3913 6c7ab921 2019-03-18 stsp goto done;
3914 6c7ab921 2019-03-18 stsp }
3915 6c7ab921 2019-03-18 stsp
3916 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3917 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3918 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3919 f6d88e1a 2019-05-29 stsp if (err)
3920 f6d88e1a 2019-05-29 stsp goto done;
3921 f6d88e1a 2019-05-29 stsp } else {
3922 f6d88e1a 2019-05-29 stsp path = strdup("");
3923 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3924 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3925 f6d88e1a 2019-05-29 stsp goto done;
3926 f6d88e1a 2019-05-29 stsp }
3927 6c7ab921 2019-03-18 stsp }
3928 6c7ab921 2019-03-18 stsp
3929 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3930 6c7ab921 2019-03-18 stsp len = strlen(path);
3931 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3932 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3933 6c7ab921 2019-03-18 stsp len--;
3934 6c7ab921 2019-03-18 stsp }
3935 6c7ab921 2019-03-18 stsp done:
3936 01740607 2020-11-04 stsp free(abspath);
3937 6c7ab921 2019-03-18 stsp free(resolved);
3938 d0710d08 2019-07-22 stsp free(cwd);
3939 6c7ab921 2019-03-18 stsp if (err == NULL)
3940 6c7ab921 2019-03-18 stsp *wt_path = path;
3941 6c7ab921 2019-03-18 stsp else
3942 6c7ab921 2019-03-18 stsp free(path);
3943 d00136be 2019-03-26 stsp return err;
3944 d00136be 2019-03-26 stsp }
3945 d00136be 2019-03-26 stsp
3946 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3947 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3948 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3949 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3950 4e68cba3 2019-11-23 stsp void *progress_arg;
3951 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3952 4e68cba3 2019-11-23 stsp };
3953 4e68cba3 2019-11-23 stsp
3954 4e68cba3 2019-11-23 stsp static const struct got_error *
3955 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3956 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3957 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3958 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3959 07f5b47a 2019-06-02 stsp {
3960 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3961 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3962 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3963 6d022e97 2019-08-04 stsp struct stat sb;
3964 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3965 07f5b47a 2019-06-02 stsp
3966 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3967 dbb83fbd 2019-12-12 stsp relpath) == -1)
3968 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3969 dbb83fbd 2019-12-12 stsp
3970 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3971 6d022e97 2019-08-04 stsp if (ie) {
3972 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3973 12463d8b 2019-12-13 stsp de_name, a->repo);
3974 6d022e97 2019-08-04 stsp if (err)
3975 dbb83fbd 2019-12-12 stsp goto done;
3976 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3977 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3978 dbb83fbd 2019-12-12 stsp goto done;
3979 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3980 dbb83fbd 2019-12-12 stsp if (err)
3981 dbb83fbd 2019-12-12 stsp goto done;
3982 6d022e97 2019-08-04 stsp }
3983 07f5b47a 2019-06-02 stsp
3984 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3985 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3986 dbb83fbd 2019-12-12 stsp goto done;
3987 4e68cba3 2019-11-23 stsp }
3988 07f5b47a 2019-06-02 stsp
3989 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3990 4e68cba3 2019-11-23 stsp if (err)
3991 4e68cba3 2019-11-23 stsp goto done;
3992 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3993 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3994 3969253a 2020-03-07 stsp if (err) {
3995 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3996 3969253a 2020-03-07 stsp goto done;
3997 3969253a 2020-03-07 stsp }
3998 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3999 07f5b47a 2019-06-02 stsp if (err) {
4000 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
4001 4e68cba3 2019-11-23 stsp goto done;
4002 07f5b47a 2019-06-02 stsp }
4003 4e68cba3 2019-11-23 stsp done:
4004 dbb83fbd 2019-12-12 stsp free(ondisk_path);
4005 dbb83fbd 2019-12-12 stsp if (err)
4006 dbb83fbd 2019-12-12 stsp return err;
4007 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
4008 dbb83fbd 2019-12-12 stsp return NULL;
4009 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
4010 07f5b47a 2019-06-02 stsp }
4011 07f5b47a 2019-06-02 stsp
4012 d00136be 2019-03-26 stsp const struct got_error *
4013 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4014 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4015 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4016 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4017 d00136be 2019-03-26 stsp {
4018 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4019 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4020 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4021 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4022 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4023 d00136be 2019-03-26 stsp
4024 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4025 d00136be 2019-03-26 stsp if (err)
4026 d00136be 2019-03-26 stsp return err;
4027 d00136be 2019-03-26 stsp
4028 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4029 d00136be 2019-03-26 stsp if (err)
4030 d00136be 2019-03-26 stsp goto done;
4031 d00136be 2019-03-26 stsp
4032 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4033 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4034 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4035 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4036 4e68cba3 2019-11-23 stsp saa.repo = repo;
4037 4e68cba3 2019-11-23 stsp
4038 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4039 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4040 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4041 1dd54920 2019-05-11 stsp if (err)
4042 af12c6b9 2019-06-04 stsp break;
4043 1dd54920 2019-05-11 stsp }
4044 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4045 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4046 af12c6b9 2019-06-04 stsp err = sync_err;
4047 d00136be 2019-03-26 stsp done:
4048 fb399478 2019-07-12 stsp free(fileindex_path);
4049 d00136be 2019-03-26 stsp if (fileindex)
4050 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4051 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4052 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4053 d00136be 2019-03-26 stsp err = unlockerr;
4054 6c7ab921 2019-03-18 stsp return err;
4055 6c7ab921 2019-03-18 stsp }
4056 17ed4618 2019-06-02 stsp
4057 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4058 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4059 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4060 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4061 f2a9dc41 2019-12-13 tracey void *progress_arg;
4062 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4063 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4064 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4065 766841c2 2020-08-13 stsp const char *status_codes;
4066 f2a9dc41 2019-12-13 tracey };
4067 f2a9dc41 2019-12-13 tracey
4068 f2a9dc41 2019-12-13 tracey static const struct got_error *
4069 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4070 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4071 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4072 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4073 17ed4618 2019-06-02 stsp {
4074 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4075 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4076 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4077 17ed4618 2019-06-02 stsp struct stat sb;
4078 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4079 17ed4618 2019-06-02 stsp
4080 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4081 17ed4618 2019-06-02 stsp if (ie == NULL)
4082 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4083 2ec1f75b 2019-03-26 stsp
4084 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4085 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4086 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4087 9acbc4fa 2019-08-03 stsp return NULL;
4088 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4089 9acbc4fa 2019-08-03 stsp }
4090 9acbc4fa 2019-08-03 stsp
4091 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4092 f2a9dc41 2019-12-13 tracey relpath) == -1)
4093 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4094 f2a9dc41 2019-12-13 tracey
4095 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4096 12463d8b 2019-12-13 stsp a->repo);
4097 17ed4618 2019-06-02 stsp if (err)
4098 f2a9dc41 2019-12-13 tracey goto done;
4099 17ed4618 2019-06-02 stsp
4100 766841c2 2020-08-13 stsp if (a->status_codes) {
4101 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4102 766841c2 2020-08-13 stsp int i;
4103 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4104 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4105 766841c2 2020-08-13 stsp break;
4106 766841c2 2020-08-13 stsp }
4107 766841c2 2020-08-13 stsp if (i == ncodes) {
4108 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4109 766841c2 2020-08-13 stsp free(ondisk_path);
4110 766841c2 2020-08-13 stsp return NULL;
4111 766841c2 2020-08-13 stsp }
4112 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4113 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4114 766841c2 2020-08-13 stsp static char msg[64];
4115 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4116 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4117 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4118 766841c2 2020-08-13 stsp goto done;
4119 766841c2 2020-08-13 stsp }
4120 766841c2 2020-08-13 stsp }
4121 766841c2 2020-08-13 stsp
4122 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4123 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4124 f2a9dc41 2019-12-13 tracey goto done;
4125 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4126 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4127 f2a9dc41 2019-12-13 tracey goto done;
4128 f2a9dc41 2019-12-13 tracey }
4129 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4130 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4131 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4132 f2a9dc41 2019-12-13 tracey goto done;
4133 f2a9dc41 2019-12-13 tracey }
4134 17ed4618 2019-06-02 stsp }
4135 17ed4618 2019-06-02 stsp
4136 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4137 20a2ad1c 2020-10-20 stsp size_t root_len;
4138 20a2ad1c 2020-10-20 stsp
4139 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4140 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4141 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4142 12463d8b 2019-12-13 stsp ondisk_path);
4143 12463d8b 2019-12-13 stsp goto done;
4144 12463d8b 2019-12-13 stsp }
4145 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4146 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4147 12463d8b 2019-12-13 stsp goto done;
4148 15341bfd 2020-03-05 tracey }
4149 15341bfd 2020-03-05 tracey
4150 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4151 20a2ad1c 2020-10-20 stsp do {
4152 20a2ad1c 2020-10-20 stsp char *parent;
4153 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4154 20a2ad1c 2020-10-20 stsp if (err)
4155 2513f20a 2020-10-20 stsp goto done;
4156 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4157 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4158 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4159 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4160 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4161 20a2ad1c 2020-10-20 stsp ondisk_path);
4162 15341bfd 2020-03-05 tracey break;
4163 15341bfd 2020-03-05 tracey }
4164 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4165 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4166 f2a9dc41 2019-12-13 tracey }
4167 17ed4618 2019-06-02 stsp
4168 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4169 f2a9dc41 2019-12-13 tracey done:
4170 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4171 f2a9dc41 2019-12-13 tracey if (err)
4172 f2a9dc41 2019-12-13 tracey return err;
4173 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4174 f2a9dc41 2019-12-13 tracey return NULL;
4175 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4176 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4177 17ed4618 2019-06-02 stsp }
4178 17ed4618 2019-06-02 stsp
4179 2ec1f75b 2019-03-26 stsp const struct got_error *
4180 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4181 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4182 766841c2 2020-08-13 stsp const char *status_codes,
4183 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4184 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4185 2ec1f75b 2019-03-26 stsp {
4186 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4187 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4188 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4189 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4190 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4191 2ec1f75b 2019-03-26 stsp
4192 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4193 2ec1f75b 2019-03-26 stsp if (err)
4194 2ec1f75b 2019-03-26 stsp return err;
4195 2ec1f75b 2019-03-26 stsp
4196 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4197 2ec1f75b 2019-03-26 stsp if (err)
4198 2ec1f75b 2019-03-26 stsp goto done;
4199 f2a9dc41 2019-12-13 tracey
4200 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4201 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4202 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4203 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4204 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4205 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4206 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4207 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4208 2ec1f75b 2019-03-26 stsp
4209 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4210 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4211 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4212 17ed4618 2019-06-02 stsp if (err)
4213 af12c6b9 2019-06-04 stsp break;
4214 2ec1f75b 2019-03-26 stsp }
4215 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4216 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4217 af12c6b9 2019-06-04 stsp err = sync_err;
4218 2ec1f75b 2019-03-26 stsp done:
4219 fb399478 2019-07-12 stsp free(fileindex_path);
4220 2ec1f75b 2019-03-26 stsp if (fileindex)
4221 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4222 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4223 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4224 2ec1f75b 2019-03-26 stsp err = unlockerr;
4225 33aa809d 2019-08-08 stsp return err;
4226 33aa809d 2019-08-08 stsp }
4227 33aa809d 2019-08-08 stsp
4228 33aa809d 2019-08-08 stsp static const struct got_error *
4229 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4230 33aa809d 2019-08-08 stsp {
4231 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4232 33aa809d 2019-08-08 stsp char *line = NULL;
4233 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4234 33aa809d 2019-08-08 stsp ssize_t linelen;
4235 33aa809d 2019-08-08 stsp
4236 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4237 33aa809d 2019-08-08 stsp if (linelen == -1) {
4238 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4239 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4240 33aa809d 2019-08-08 stsp goto done;
4241 33aa809d 2019-08-08 stsp }
4242 33aa809d 2019-08-08 stsp return NULL;
4243 33aa809d 2019-08-08 stsp }
4244 33aa809d 2019-08-08 stsp if (outfile) {
4245 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4246 33aa809d 2019-08-08 stsp if (n != linelen) {
4247 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4248 33aa809d 2019-08-08 stsp goto done;
4249 33aa809d 2019-08-08 stsp }
4250 33aa809d 2019-08-08 stsp }
4251 33aa809d 2019-08-08 stsp if (rejectfile) {
4252 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4253 33aa809d 2019-08-08 stsp if (n != linelen)
4254 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4255 33aa809d 2019-08-08 stsp }
4256 33aa809d 2019-08-08 stsp done:
4257 33aa809d 2019-08-08 stsp free(line);
4258 2ec1f75b 2019-03-26 stsp return err;
4259 2ec1f75b 2019-03-26 stsp }
4260 1f1abb7e 2019-08-08 stsp
4261 33aa809d 2019-08-08 stsp static const struct got_error *
4262 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4263 33aa809d 2019-08-08 stsp {
4264 33aa809d 2019-08-08 stsp char *line = NULL;
4265 33aa809d 2019-08-08 stsp size_t linesize = 0;
4266 33aa809d 2019-08-08 stsp ssize_t linelen;
4267 33aa809d 2019-08-08 stsp
4268 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4269 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4270 500467ff 2019-09-25 hiltjo if (ferror(f))
4271 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4272 500467ff 2019-09-25 hiltjo return NULL;
4273 500467ff 2019-09-25 hiltjo }
4274 33aa809d 2019-08-08 stsp free(line);
4275 33aa809d 2019-08-08 stsp return NULL;
4276 33aa809d 2019-08-08 stsp }
4277 33aa809d 2019-08-08 stsp
4278 33aa809d 2019-08-08 stsp static const struct got_error *
4279 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4280 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4281 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4282 abc59930 2021-09-05 naddy {
4283 33aa809d 2019-08-08 stsp const struct got_error *err;
4284 33aa809d 2019-08-08 stsp
4285 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4286 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4287 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4288 33aa809d 2019-08-08 stsp if (err)
4289 33aa809d 2019-08-08 stsp return err;
4290 33aa809d 2019-08-08 stsp (*line_cur1)++;
4291 33aa809d 2019-08-08 stsp }
4292 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4293 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4294 33aa809d 2019-08-08 stsp if (rejectfile)
4295 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4296 33aa809d 2019-08-08 stsp else
4297 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4298 33aa809d 2019-08-08 stsp if (err)
4299 33aa809d 2019-08-08 stsp return err;
4300 33aa809d 2019-08-08 stsp (*line_cur2)++;
4301 33aa809d 2019-08-08 stsp }
4302 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4303 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4304 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4305 33aa809d 2019-08-08 stsp if (err)
4306 33aa809d 2019-08-08 stsp return err;
4307 33aa809d 2019-08-08 stsp (*line_cur2)++;
4308 33aa809d 2019-08-08 stsp }
4309 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4310 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4311 33aa809d 2019-08-08 stsp if (rejectfile)
4312 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4313 33aa809d 2019-08-08 stsp else
4314 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4315 33aa809d 2019-08-08 stsp if (err)
4316 33aa809d 2019-08-08 stsp return err;
4317 33aa809d 2019-08-08 stsp (*line_cur1)++;
4318 33aa809d 2019-08-08 stsp }
4319 f1e81a05 2019-08-10 stsp
4320 f1e81a05 2019-08-10 stsp return NULL;
4321 f1e81a05 2019-08-10 stsp }
4322 f1e81a05 2019-08-10 stsp
4323 f1e81a05 2019-08-10 stsp static const struct got_error *
4324 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4325 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4326 f1e81a05 2019-08-10 stsp {
4327 f1e81a05 2019-08-10 stsp const struct got_error *err;
4328 f1e81a05 2019-08-10 stsp
4329 f1e81a05 2019-08-10 stsp if (outfile) {
4330 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4331 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4332 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4333 f1e81a05 2019-08-10 stsp if (err)
4334 f1e81a05 2019-08-10 stsp return err;
4335 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4336 f1e81a05 2019-08-10 stsp }
4337 f1e81a05 2019-08-10 stsp }
4338 f1e81a05 2019-08-10 stsp if (rejectfile) {
4339 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4340 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4341 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4342 f1e81a05 2019-08-10 stsp if (err)
4343 f1e81a05 2019-08-10 stsp return err;
4344 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4345 f1e81a05 2019-08-10 stsp }
4346 33aa809d 2019-08-08 stsp }
4347 33aa809d 2019-08-08 stsp
4348 33aa809d 2019-08-08 stsp return NULL;
4349 33aa809d 2019-08-08 stsp }
4350 33aa809d 2019-08-08 stsp
4351 33aa809d 2019-08-08 stsp static const struct got_error *
4352 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4353 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4354 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4355 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4356 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4357 33aa809d 2019-08-08 stsp {
4358 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4359 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4360 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4361 33aa809d 2019-08-08 stsp FILE *hunkfile;
4362 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4363 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4364 fe621944 2020-11-10 stsp int rc;
4365 33aa809d 2019-08-08 stsp
4366 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4367 33aa809d 2019-08-08 stsp
4368 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4369 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4370 fe621944 2020-11-10 stsp start_old = cc.left.start;
4371 fe621944 2020-11-10 stsp end_old = cc.left.end;
4372 fe621944 2020-11-10 stsp start_new = cc.right.start;
4373 fe621944 2020-11-10 stsp end_new = cc.right.end;
4374 33aa809d 2019-08-08 stsp
4375 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4376 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4377 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4378 33aa809d 2019-08-08 stsp
4379 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4380 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4381 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4382 33aa809d 2019-08-08 stsp
4383 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4384 fe621944 2020-11-10 stsp if (diff_state == NULL)
4385 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4386 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4387 fe621944 2020-11-10 stsp
4388 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4389 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4390 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4391 33aa809d 2019-08-08 stsp goto done;
4392 33aa809d 2019-08-08 stsp }
4393 fe621944 2020-11-10 stsp
4394 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4395 fe621944 2020-11-10 stsp diff_result, &cc);
4396 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4397 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4398 33aa809d 2019-08-08 stsp goto done;
4399 33aa809d 2019-08-08 stsp }
4400 fe621944 2020-11-10 stsp
4401 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4402 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4403 33aa809d 2019-08-08 stsp goto done;
4404 33aa809d 2019-08-08 stsp }
4405 33aa809d 2019-08-08 stsp
4406 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4407 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4408 33aa809d 2019-08-08 stsp if (err)
4409 33aa809d 2019-08-08 stsp goto done;
4410 33aa809d 2019-08-08 stsp
4411 33aa809d 2019-08-08 stsp switch (*choice) {
4412 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4413 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4414 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4415 33aa809d 2019-08-08 stsp break;
4416 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4417 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4418 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4419 33aa809d 2019-08-08 stsp break;
4420 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4421 33aa809d 2019-08-08 stsp break;
4422 33aa809d 2019-08-08 stsp default:
4423 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4424 33aa809d 2019-08-08 stsp break;
4425 33aa809d 2019-08-08 stsp }
4426 33aa809d 2019-08-08 stsp done:
4427 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4428 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4429 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4430 33aa809d 2019-08-08 stsp return err;
4431 33aa809d 2019-08-08 stsp }
4432 33aa809d 2019-08-08 stsp
4433 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4434 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4435 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4436 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4437 1f1abb7e 2019-08-08 stsp void *progress_arg;
4438 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4439 33aa809d 2019-08-08 stsp void *patch_arg;
4440 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4441 f259c4c1 2021-09-24 stsp int unlink_added_files;
4442 1f1abb7e 2019-08-08 stsp };
4443 a129376b 2019-03-28 stsp
4444 e20a8b6f 2019-06-04 stsp static const struct got_error *
4445 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4446 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4447 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4448 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4449 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4450 33aa809d 2019-08-08 stsp {
4451 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4452 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4453 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4454 1ebedb77 2019-10-19 stsp int fd2 = -1;
4455 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4456 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4457 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4458 fe621944 2020-11-10 stsp struct stat sb2;
4459 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4460 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4461 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4462 33aa809d 2019-08-08 stsp
4463 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4464 33aa809d 2019-08-08 stsp
4465 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4466 33aa809d 2019-08-08 stsp if (err)
4467 33aa809d 2019-08-08 stsp return err;
4468 33aa809d 2019-08-08 stsp
4469 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4470 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4471 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4472 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4473 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4474 fa3cef63 2020-07-23 stsp goto done;
4475 fa3cef63 2020-07-23 stsp }
4476 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4477 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4478 fa3cef63 2020-07-23 stsp if (link_len == -1)
4479 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4480 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4481 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4482 12463d8b 2019-12-13 stsp }
4483 12463d8b 2019-12-13 stsp } else {
4484 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4485 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4486 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4487 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4488 fa3cef63 2020-07-23 stsp goto done;
4489 fa3cef63 2020-07-23 stsp }
4490 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4491 fa3cef63 2020-07-23 stsp sizeof(link_target));
4492 fa3cef63 2020-07-23 stsp if (link_len == -1)
4493 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4494 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4495 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4496 12463d8b 2019-12-13 stsp }
4497 1ebedb77 2019-10-19 stsp }
4498 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4499 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4500 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4501 fa3cef63 2020-07-23 stsp goto done;
4502 fa3cef63 2020-07-23 stsp }
4503 1ebedb77 2019-10-19 stsp
4504 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4505 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4506 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4507 fa3cef63 2020-07-23 stsp goto done;
4508 fa3cef63 2020-07-23 stsp }
4509 fa3cef63 2020-07-23 stsp fd2 = -1;
4510 fa3cef63 2020-07-23 stsp } else {
4511 fa3cef63 2020-07-23 stsp size_t n;
4512 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4513 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4514 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4515 fa3cef63 2020-07-23 stsp goto done;
4516 fa3cef63 2020-07-23 stsp }
4517 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4518 fa3cef63 2020-07-23 stsp if (n != link_len) {
4519 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4520 fa3cef63 2020-07-23 stsp goto done;
4521 fa3cef63 2020-07-23 stsp }
4522 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4523 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4524 fa3cef63 2020-07-23 stsp goto done;
4525 fa3cef63 2020-07-23 stsp }
4526 fa3cef63 2020-07-23 stsp rewind(f2);
4527 33aa809d 2019-08-08 stsp }
4528 33aa809d 2019-08-08 stsp
4529 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4530 33aa809d 2019-08-08 stsp if (err)
4531 33aa809d 2019-08-08 stsp goto done;
4532 33aa809d 2019-08-08 stsp
4533 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4534 33aa809d 2019-08-08 stsp if (err)
4535 33aa809d 2019-08-08 stsp goto done;
4536 33aa809d 2019-08-08 stsp
4537 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4538 33aa809d 2019-08-08 stsp if (err)
4539 33aa809d 2019-08-08 stsp goto done;
4540 33aa809d 2019-08-08 stsp
4541 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4542 fe621944 2020-11-10 stsp NULL);
4543 33aa809d 2019-08-08 stsp if (err)
4544 33aa809d 2019-08-08 stsp goto done;
4545 33aa809d 2019-08-08 stsp
4546 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4547 33aa809d 2019-08-08 stsp if (err)
4548 33aa809d 2019-08-08 stsp goto done;
4549 33aa809d 2019-08-08 stsp
4550 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4551 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4552 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4553 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4554 fe621944 2020-11-10 stsp
4555 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4556 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4557 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4558 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4559 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4560 fe621944 2020-11-10 stsp nchanges++;
4561 fe621944 2020-11-10 stsp }
4562 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4563 33aa809d 2019-08-08 stsp int choice;
4564 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4565 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4566 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4567 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4568 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4569 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4570 33aa809d 2019-08-08 stsp if (err)
4571 33aa809d 2019-08-08 stsp goto done;
4572 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4573 33aa809d 2019-08-08 stsp have_content = 1;
4574 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4575 33aa809d 2019-08-08 stsp break;
4576 33aa809d 2019-08-08 stsp }
4577 1ebedb77 2019-10-19 stsp if (have_content) {
4578 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4579 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4580 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4581 1ebedb77 2019-10-19 stsp if (err)
4582 1ebedb77 2019-10-19 stsp goto done;
4583 1ebedb77 2019-10-19 stsp
4584 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4585 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4586 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4587 fa3cef63 2020-07-23 stsp goto done;
4588 fa3cef63 2020-07-23 stsp }
4589 1ebedb77 2019-10-19 stsp }
4590 1ebedb77 2019-10-19 stsp }
4591 33aa809d 2019-08-08 stsp done:
4592 33aa809d 2019-08-08 stsp free(id_str);
4593 33aa809d 2019-08-08 stsp if (blob)
4594 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4595 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4596 fe621944 2020-11-10 stsp if (err == NULL)
4597 fe621944 2020-11-10 stsp err = free_err;
4598 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4599 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4600 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4601 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4602 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4603 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4604 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4605 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4606 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4607 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4608 33aa809d 2019-08-08 stsp if (err || !have_content) {
4609 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4610 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4611 33aa809d 2019-08-08 stsp free(*path_outfile);
4612 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4613 33aa809d 2019-08-08 stsp }
4614 33aa809d 2019-08-08 stsp free(path1);
4615 33aa809d 2019-08-08 stsp return err;
4616 33aa809d 2019-08-08 stsp }
4617 33aa809d 2019-08-08 stsp
4618 33aa809d 2019-08-08 stsp static const struct got_error *
4619 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4620 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4621 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4622 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4623 a129376b 2019-03-28 stsp {
4624 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4625 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4626 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4627 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4628 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4629 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4630 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4631 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4632 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4633 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4634 a129376b 2019-03-28 stsp
4635 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4636 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4637 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4638 d3bcc3d1 2019-08-08 stsp return NULL;
4639 3d69ad8d 2019-08-17 semarie
4640 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4641 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4642 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4643 d3bcc3d1 2019-08-08 stsp
4644 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4645 65084dad 2019-08-08 stsp if (ie == NULL)
4646 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4647 a129376b 2019-03-28 stsp
4648 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4649 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4650 a129376b 2019-03-28 stsp if (err) {
4651 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4652 a129376b 2019-03-28 stsp goto done;
4653 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4654 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4655 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4656 e20a8b6f 2019-06-04 stsp goto done;
4657 e20a8b6f 2019-06-04 stsp }
4658 a129376b 2019-03-28 stsp }
4659 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4660 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4661 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4662 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4663 a129376b 2019-03-28 stsp goto done;
4664 a129376b 2019-03-28 stsp }
4665 a129376b 2019-03-28 stsp } else {
4666 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4667 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4668 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4669 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4670 a129376b 2019-03-28 stsp goto done;
4671 a129376b 2019-03-28 stsp }
4672 a129376b 2019-03-28 stsp } else {
4673 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4674 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4675 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4676 a129376b 2019-03-28 stsp goto done;
4677 a129376b 2019-03-28 stsp }
4678 a129376b 2019-03-28 stsp }
4679 a129376b 2019-03-28 stsp }
4680 a129376b 2019-03-28 stsp
4681 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4682 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4683 a9fa2909 2019-07-27 stsp if (err) {
4684 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4685 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4686 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4687 a9fa2909 2019-07-27 stsp goto done;
4688 a9fa2909 2019-07-27 stsp } else {
4689 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4690 a9fa2909 2019-07-27 stsp if (err)
4691 a9fa2909 2019-07-27 stsp goto done;
4692 a9fa2909 2019-07-27 stsp
4693 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4694 1233e6b6 2020-10-19 stsp if (err)
4695 a9fa2909 2019-07-27 stsp goto done;
4696 a9fa2909 2019-07-27 stsp
4697 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4698 1233e6b6 2020-10-19 stsp free(te_name);
4699 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4700 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4701 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4702 a9fa2909 2019-07-27 stsp goto done;
4703 a9fa2909 2019-07-27 stsp }
4704 a129376b 2019-03-28 stsp }
4705 a129376b 2019-03-28 stsp
4706 a129376b 2019-03-28 stsp switch (status) {
4707 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4708 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4709 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4710 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4711 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4712 33aa809d 2019-08-08 stsp if (err)
4713 33aa809d 2019-08-08 stsp goto done;
4714 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4715 33aa809d 2019-08-08 stsp break;
4716 33aa809d 2019-08-08 stsp }
4717 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4718 1f1abb7e 2019-08-08 stsp ie->path);
4719 1ee397ad 2019-07-12 stsp if (err)
4720 1ee397ad 2019-07-12 stsp goto done;
4721 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4722 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4723 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4724 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4725 f259c4c1 2021-09-24 stsp relpath) == -1) {
4726 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4727 f259c4c1 2021-09-24 stsp goto done;
4728 f259c4c1 2021-09-24 stsp }
4729 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4730 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4731 f259c4c1 2021-09-24 stsp ondisk_path);
4732 f259c4c1 2021-09-24 stsp break;
4733 f259c4c1 2021-09-24 stsp }
4734 f259c4c1 2021-09-24 stsp }
4735 a129376b 2019-03-28 stsp break;
4736 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4737 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4738 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4739 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4740 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4741 33aa809d 2019-08-08 stsp if (err)
4742 33aa809d 2019-08-08 stsp goto done;
4743 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4744 33aa809d 2019-08-08 stsp break;
4745 33aa809d 2019-08-08 stsp }
4746 33aa809d 2019-08-08 stsp /* fall through */
4747 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4748 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4749 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4750 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4751 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4752 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4753 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4754 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4755 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4756 24278f30 2019-08-03 stsp } else
4757 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4758 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4759 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4760 a129376b 2019-03-28 stsp if (err)
4761 65084dad 2019-08-08 stsp goto done;
4762 65084dad 2019-08-08 stsp
4763 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4764 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4765 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4766 a129376b 2019-03-28 stsp goto done;
4767 65084dad 2019-08-08 stsp }
4768 65084dad 2019-08-08 stsp
4769 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4770 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4771 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4772 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4773 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4774 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4775 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4776 33aa809d 2019-08-08 stsp break;
4777 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4778 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4779 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4780 369fd7e5 2020-07-23 stsp path_content);
4781 369fd7e5 2020-07-23 stsp break;
4782 369fd7e5 2020-07-23 stsp }
4783 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4784 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4785 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4786 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4787 369fd7e5 2020-07-23 stsp } else {
4788 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4789 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4790 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4791 369fd7e5 2020-07-23 stsp goto done;
4792 369fd7e5 2020-07-23 stsp }
4793 33aa809d 2019-08-08 stsp }
4794 33aa809d 2019-08-08 stsp } else {
4795 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4796 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4797 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4798 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4799 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4800 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4801 2e1fa222 2020-07-23 stsp } else {
4802 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4803 2e1fa222 2020-07-23 stsp ie->path,
4804 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4805 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4806 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4807 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4808 2e1fa222 2020-07-23 stsp }
4809 a129376b 2019-03-28 stsp if (err)
4810 a129376b 2019-03-28 stsp goto done;
4811 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4812 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4813 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4814 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4815 437adc9d 2020-12-10 yzhong blob->id.sha1,
4816 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4817 2e1fa222 2020-07-23 stsp if (err)
4818 2e1fa222 2020-07-23 stsp goto done;
4819 2e1fa222 2020-07-23 stsp }
4820 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4821 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4822 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4823 33aa809d 2019-08-08 stsp }
4824 a129376b 2019-03-28 stsp }
4825 a129376b 2019-03-28 stsp break;
4826 e20a8b6f 2019-06-04 stsp }
4827 a129376b 2019-03-28 stsp default:
4828 1f1abb7e 2019-08-08 stsp break;
4829 a129376b 2019-03-28 stsp }
4830 a129376b 2019-03-28 stsp done:
4831 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4832 33aa809d 2019-08-08 stsp free(path_content);
4833 e20a8b6f 2019-06-04 stsp free(parent_path);
4834 a129376b 2019-03-28 stsp free(tree_path);
4835 a129376b 2019-03-28 stsp if (blob)
4836 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4837 a129376b 2019-03-28 stsp if (tree)
4838 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4839 a129376b 2019-03-28 stsp free(tree_id);
4840 e20a8b6f 2019-06-04 stsp return err;
4841 e20a8b6f 2019-06-04 stsp }
4842 e20a8b6f 2019-06-04 stsp
4843 e20a8b6f 2019-06-04 stsp const struct got_error *
4844 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4845 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4846 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4847 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4848 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4849 e20a8b6f 2019-06-04 stsp {
4850 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4851 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4852 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4853 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4854 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4855 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4856 e20a8b6f 2019-06-04 stsp
4857 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4858 e20a8b6f 2019-06-04 stsp if (err)
4859 e20a8b6f 2019-06-04 stsp return err;
4860 e20a8b6f 2019-06-04 stsp
4861 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4862 e20a8b6f 2019-06-04 stsp if (err)
4863 e20a8b6f 2019-06-04 stsp goto done;
4864 e20a8b6f 2019-06-04 stsp
4865 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4866 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4867 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4868 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4869 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4870 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4871 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4872 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4873 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4874 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4875 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4876 e20a8b6f 2019-06-04 stsp if (err)
4877 af12c6b9 2019-06-04 stsp break;
4878 e20a8b6f 2019-06-04 stsp }
4879 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4880 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4881 e20a8b6f 2019-06-04 stsp err = sync_err;
4882 af12c6b9 2019-06-04 stsp done:
4883 fb399478 2019-07-12 stsp free(fileindex_path);
4884 a129376b 2019-03-28 stsp if (fileindex)
4885 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4886 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4887 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4888 a129376b 2019-03-28 stsp err = unlockerr;
4889 c4296144 2019-05-09 stsp return err;
4890 c4296144 2019-05-09 stsp }
4891 c4296144 2019-05-09 stsp
4892 cf066bf8 2019-05-09 stsp static void
4893 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4894 cf066bf8 2019-05-09 stsp {
4895 24519714 2019-05-09 stsp free(ct->path);
4896 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4897 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4898 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4899 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4900 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4901 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4902 cf066bf8 2019-05-09 stsp free(ct);
4903 cf066bf8 2019-05-09 stsp }
4904 24519714 2019-05-09 stsp
4905 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4906 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4907 24519714 2019-05-09 stsp struct got_repository *repo;
4908 24519714 2019-05-09 stsp struct got_worktree *worktree;
4909 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4910 5f8a88c6 2019-08-03 stsp int have_staged_files;
4911 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4912 24519714 2019-05-09 stsp };
4913 cf066bf8 2019-05-09 stsp
4914 c4296144 2019-05-09 stsp static const struct got_error *
4915 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4916 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4917 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4918 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4919 c4296144 2019-05-09 stsp {
4920 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4921 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4922 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4923 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4924 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4925 768aea60 2019-05-09 stsp struct stat sb;
4926 c4296144 2019-05-09 stsp
4927 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4928 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4929 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4930 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4931 5f8a88c6 2019-08-03 stsp return NULL;
4932 5f8a88c6 2019-08-03 stsp } else {
4933 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4934 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4935 c4296144 2019-05-09 stsp
4936 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4937 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4938 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4939 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4940 5f8a88c6 2019-08-03 stsp return NULL;
4941 5f8a88c6 2019-08-03 stsp }
4942 0b5cc0d6 2019-05-09 stsp
4943 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4944 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4945 036813ee 2019-05-09 stsp goto done;
4946 036813ee 2019-05-09 stsp }
4947 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4948 036813ee 2019-05-09 stsp parent_path = strdup("");
4949 69960a46 2019-05-09 stsp if (parent_path == NULL)
4950 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4951 69960a46 2019-05-09 stsp } else {
4952 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4953 69960a46 2019-05-09 stsp if (err)
4954 69960a46 2019-05-09 stsp return err;
4955 69960a46 2019-05-09 stsp }
4956 c4296144 2019-05-09 stsp
4957 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4958 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4959 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4960 c4296144 2019-05-09 stsp goto done;
4961 768aea60 2019-05-09 stsp }
4962 768aea60 2019-05-09 stsp
4963 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4964 768aea60 2019-05-09 stsp relpath) == -1) {
4965 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4966 768aea60 2019-05-09 stsp goto done;
4967 768aea60 2019-05-09 stsp }
4968 0aeb8099 2020-07-23 stsp
4969 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4970 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4971 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4972 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4973 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4974 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4975 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4976 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4977 0aeb8099 2020-07-23 stsp break;
4978 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4979 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4980 0aeb8099 2020-07-23 stsp break;
4981 0aeb8099 2020-07-23 stsp default:
4982 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4983 0aeb8099 2020-07-23 stsp goto done;
4984 0aeb8099 2020-07-23 stsp }
4985 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4986 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4987 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4988 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4989 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4990 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4991 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4992 12463d8b 2019-12-13 stsp ct->ondisk_path);
4993 12463d8b 2019-12-13 stsp goto done;
4994 12463d8b 2019-12-13 stsp }
4995 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4996 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4997 768aea60 2019-05-09 stsp goto done;
4998 768aea60 2019-05-09 stsp }
4999 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
5000 c4296144 2019-05-09 stsp }
5001 c4296144 2019-05-09 stsp
5002 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
5003 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
5004 44d03001 2019-05-09 stsp relpath) == -1) {
5005 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5006 44d03001 2019-05-09 stsp goto done;
5007 44d03001 2019-05-09 stsp }
5008 44d03001 2019-05-09 stsp
5009 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
5010 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
5011 35213c7c 2020-07-23 stsp int is_bad_symlink;
5012 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5013 35213c7c 2020-07-23 stsp ssize_t target_len;
5014 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5015 35213c7c 2020-07-23 stsp sizeof(target_path));
5016 35213c7c 2020-07-23 stsp if (target_len == -1) {
5017 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5018 35213c7c 2020-07-23 stsp ct->ondisk_path);
5019 35213c7c 2020-07-23 stsp goto done;
5020 35213c7c 2020-07-23 stsp }
5021 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5022 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5023 35213c7c 2020-07-23 stsp if (err)
5024 35213c7c 2020-07-23 stsp goto done;
5025 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5026 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5027 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5028 35213c7c 2020-07-23 stsp goto done;
5029 35213c7c 2020-07-23 stsp }
5030 35213c7c 2020-07-23 stsp }
5031 35213c7c 2020-07-23 stsp
5032 35213c7c 2020-07-23 stsp
5033 cf066bf8 2019-05-09 stsp ct->status = status;
5034 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5035 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5036 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5037 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5038 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5039 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5040 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5041 b416585c 2019-05-13 stsp goto done;
5042 b416585c 2019-05-13 stsp }
5043 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5044 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5045 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5046 f0b75401 2019-08-03 stsp goto done;
5047 f0b75401 2019-08-03 stsp }
5048 f0b75401 2019-08-03 stsp }
5049 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5050 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5051 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5052 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5053 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5054 036813ee 2019-05-09 stsp goto done;
5055 036813ee 2019-05-09 stsp }
5056 ca2503ea 2019-05-09 stsp }
5057 24519714 2019-05-09 stsp ct->path = strdup(path);
5058 24519714 2019-05-09 stsp if (ct->path == NULL) {
5059 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5060 24519714 2019-05-09 stsp goto done;
5061 24519714 2019-05-09 stsp }
5062 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5063 c4296144 2019-05-09 stsp done:
5064 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5065 ed175427 2019-05-09 stsp free_commitable(ct);
5066 24519714 2019-05-09 stsp free(parent_path);
5067 036813ee 2019-05-09 stsp free(path);
5068 a129376b 2019-03-28 stsp return err;
5069 a129376b 2019-03-28 stsp }
5070 c4296144 2019-05-09 stsp
5071 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5072 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5073 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5074 036813ee 2019-05-09 stsp struct got_repository *);
5075 ed175427 2019-05-09 stsp
5076 ed175427 2019-05-09 stsp static const struct got_error *
5077 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5078 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5079 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5080 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5081 afa376bf 2019-05-09 stsp struct got_repository *repo)
5082 ed175427 2019-05-09 stsp {
5083 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5084 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5085 036813ee 2019-05-09 stsp char *subpath;
5086 ed175427 2019-05-09 stsp
5087 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5088 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5089 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5090 ed175427 2019-05-09 stsp
5091 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5092 ed175427 2019-05-09 stsp if (err)
5093 ed175427 2019-05-09 stsp return err;
5094 ed175427 2019-05-09 stsp
5095 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5096 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5097 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5098 036813ee 2019-05-09 stsp free(subpath);
5099 036813ee 2019-05-09 stsp return err;
5100 036813ee 2019-05-09 stsp }
5101 ed175427 2019-05-09 stsp
5102 036813ee 2019-05-09 stsp static const struct got_error *
5103 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5104 036813ee 2019-05-09 stsp {
5105 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5106 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5107 ed175427 2019-05-09 stsp
5108 036813ee 2019-05-09 stsp *match = 0;
5109 ed175427 2019-05-09 stsp
5110 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5111 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5112 0f63689d 2019-05-10 stsp return NULL;
5113 036813ee 2019-05-09 stsp }
5114 0b5cc0d6 2019-05-09 stsp
5115 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5116 0f63689d 2019-05-10 stsp if (err)
5117 0f63689d 2019-05-10 stsp return err;
5118 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5119 036813ee 2019-05-09 stsp free(ct_parent_path);
5120 036813ee 2019-05-09 stsp return err;
5121 036813ee 2019-05-09 stsp }
5122 0b5cc0d6 2019-05-09 stsp
5123 768aea60 2019-05-09 stsp static mode_t
5124 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5125 768aea60 2019-05-09 stsp {
5126 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5127 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5128 3d9a4ec4 2020-07-23 stsp
5129 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5130 768aea60 2019-05-09 stsp }
5131 768aea60 2019-05-09 stsp
5132 036813ee 2019-05-09 stsp static const struct got_error *
5133 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5134 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5135 036813ee 2019-05-09 stsp {
5136 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5137 ca2503ea 2019-05-09 stsp
5138 036813ee 2019-05-09 stsp *new_te = NULL;
5139 0b5cc0d6 2019-05-09 stsp
5140 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5141 036813ee 2019-05-09 stsp if (err)
5142 036813ee 2019-05-09 stsp goto done;
5143 0b5cc0d6 2019-05-09 stsp
5144 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5145 036813ee 2019-05-09 stsp
5146 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5147 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5148 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5149 5f8a88c6 2019-08-03 stsp else
5150 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5151 036813ee 2019-05-09 stsp done:
5152 036813ee 2019-05-09 stsp if (err && *new_te) {
5153 56e0773d 2019-11-28 stsp free(*new_te);
5154 036813ee 2019-05-09 stsp *new_te = NULL;
5155 036813ee 2019-05-09 stsp }
5156 036813ee 2019-05-09 stsp return err;
5157 036813ee 2019-05-09 stsp }
5158 036813ee 2019-05-09 stsp
5159 036813ee 2019-05-09 stsp static const struct got_error *
5160 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5161 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5162 036813ee 2019-05-09 stsp {
5163 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5164 102b254e 2020-10-19 stsp char *ct_name = NULL;
5165 036813ee 2019-05-09 stsp
5166 036813ee 2019-05-09 stsp *new_te = NULL;
5167 036813ee 2019-05-09 stsp
5168 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5169 036813ee 2019-05-09 stsp if (*new_te == NULL)
5170 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5171 036813ee 2019-05-09 stsp
5172 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5173 102b254e 2020-10-19 stsp if (err)
5174 036813ee 2019-05-09 stsp goto done;
5175 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5176 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5177 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5178 036813ee 2019-05-09 stsp goto done;
5179 036813ee 2019-05-09 stsp }
5180 036813ee 2019-05-09 stsp
5181 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5182 036813ee 2019-05-09 stsp
5183 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5184 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5185 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5186 5f8a88c6 2019-08-03 stsp else
5187 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5188 036813ee 2019-05-09 stsp done:
5189 102b254e 2020-10-19 stsp free(ct_name);
5190 036813ee 2019-05-09 stsp if (err && *new_te) {
5191 56e0773d 2019-11-28 stsp free(*new_te);
5192 036813ee 2019-05-09 stsp *new_te = NULL;
5193 036813ee 2019-05-09 stsp }
5194 036813ee 2019-05-09 stsp return err;
5195 036813ee 2019-05-09 stsp }
5196 036813ee 2019-05-09 stsp
5197 036813ee 2019-05-09 stsp static const struct got_error *
5198 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5199 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5200 036813ee 2019-05-09 stsp {
5201 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5202 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5203 036813ee 2019-05-09 stsp
5204 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5205 036813ee 2019-05-09 stsp if (err)
5206 036813ee 2019-05-09 stsp return err;
5207 036813ee 2019-05-09 stsp if (new_pe == NULL)
5208 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5209 036813ee 2019-05-09 stsp return NULL;
5210 afa376bf 2019-05-09 stsp }
5211 afa376bf 2019-05-09 stsp
5212 afa376bf 2019-05-09 stsp static const struct got_error *
5213 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5214 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5215 afa376bf 2019-05-09 stsp {
5216 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5217 5f8a88c6 2019-08-03 stsp unsigned char status;
5218 5f8a88c6 2019-08-03 stsp
5219 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5220 afa376bf 2019-05-09 stsp ct_path++;
5221 5f8a88c6 2019-08-03 stsp
5222 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5223 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5224 5f8a88c6 2019-08-03 stsp else
5225 5f8a88c6 2019-08-03 stsp status = ct->status;
5226 5f8a88c6 2019-08-03 stsp
5227 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5228 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5229 036813ee 2019-05-09 stsp }
5230 036813ee 2019-05-09 stsp
5231 036813ee 2019-05-09 stsp static const struct got_error *
5232 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5233 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5234 44d03001 2019-05-09 stsp {
5235 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5236 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5237 44d03001 2019-05-09 stsp char *te_path;
5238 44d03001 2019-05-09 stsp
5239 44d03001 2019-05-09 stsp *modified = 0;
5240 44d03001 2019-05-09 stsp
5241 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5242 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5243 44d03001 2019-05-09 stsp te->name) == -1)
5244 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5245 44d03001 2019-05-09 stsp
5246 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5247 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5248 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5249 44d03001 2019-05-09 stsp strlen(te_path));
5250 62d463ca 2020-10-20 naddy if (*modified)
5251 44d03001 2019-05-09 stsp break;
5252 44d03001 2019-05-09 stsp }
5253 44d03001 2019-05-09 stsp
5254 44d03001 2019-05-09 stsp free(te_path);
5255 44d03001 2019-05-09 stsp return err;
5256 44d03001 2019-05-09 stsp }
5257 44d03001 2019-05-09 stsp
5258 44d03001 2019-05-09 stsp static const struct got_error *
5259 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5260 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5261 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5262 036813ee 2019-05-09 stsp {
5263 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5264 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5265 036813ee 2019-05-09 stsp
5266 036813ee 2019-05-09 stsp *ctp = NULL;
5267 036813ee 2019-05-09 stsp
5268 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5269 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5270 036813ee 2019-05-09 stsp char *ct_name = NULL;
5271 036813ee 2019-05-09 stsp int path_matches;
5272 036813ee 2019-05-09 stsp
5273 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5274 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5275 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5276 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5277 5f8a88c6 2019-08-03 stsp continue;
5278 5f8a88c6 2019-08-03 stsp } else {
5279 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5280 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5281 5f8a88c6 2019-08-03 stsp continue;
5282 5f8a88c6 2019-08-03 stsp }
5283 036813ee 2019-05-09 stsp
5284 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5285 036813ee 2019-05-09 stsp continue;
5286 036813ee 2019-05-09 stsp
5287 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5288 62d463ca 2020-10-20 naddy if (err)
5289 036813ee 2019-05-09 stsp return err;
5290 036813ee 2019-05-09 stsp if (!path_matches)
5291 036813ee 2019-05-09 stsp continue;
5292 036813ee 2019-05-09 stsp
5293 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5294 d34b633e 2020-10-19 stsp if (err)
5295 d34b633e 2020-10-19 stsp return err;
5296 d34b633e 2020-10-19 stsp
5297 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5298 d34b633e 2020-10-19 stsp free(ct_name);
5299 036813ee 2019-05-09 stsp continue;
5300 d34b633e 2020-10-19 stsp }
5301 d34b633e 2020-10-19 stsp free(ct_name);
5302 036813ee 2019-05-09 stsp
5303 036813ee 2019-05-09 stsp *ctp = ct;
5304 036813ee 2019-05-09 stsp break;
5305 036813ee 2019-05-09 stsp }
5306 2b496619 2019-07-10 stsp
5307 2b496619 2019-07-10 stsp return err;
5308 2b496619 2019-07-10 stsp }
5309 2b496619 2019-07-10 stsp
5310 2b496619 2019-07-10 stsp static const struct got_error *
5311 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5312 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5313 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5314 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5315 2b496619 2019-07-10 stsp struct got_repository *repo)
5316 2b496619 2019-07-10 stsp {
5317 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5318 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5319 2b496619 2019-07-10 stsp char *subtree_path;
5320 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5321 ba580f68 2020-03-22 stsp int nentries;
5322 2b496619 2019-07-10 stsp
5323 2b496619 2019-07-10 stsp *new_tep = NULL;
5324 2b496619 2019-07-10 stsp
5325 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5326 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5327 2b496619 2019-07-10 stsp child_path) == -1)
5328 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5329 036813ee 2019-05-09 stsp
5330 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5331 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5332 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5333 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5334 56e0773d 2019-11-28 stsp
5335 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5336 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5337 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5338 2b496619 2019-07-10 stsp goto done;
5339 2b496619 2019-07-10 stsp }
5340 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5341 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5342 2b496619 2019-07-10 stsp if (err) {
5343 56e0773d 2019-11-28 stsp free(new_te);
5344 2b496619 2019-07-10 stsp goto done;
5345 2b496619 2019-07-10 stsp }
5346 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5347 2b496619 2019-07-10 stsp done:
5348 56e0773d 2019-11-28 stsp free(id);
5349 2b496619 2019-07-10 stsp free(subtree_path);
5350 2b496619 2019-07-10 stsp if (err == NULL)
5351 2b496619 2019-07-10 stsp *new_tep = new_te;
5352 036813ee 2019-05-09 stsp return err;
5353 036813ee 2019-05-09 stsp }
5354 036813ee 2019-05-09 stsp
5355 036813ee 2019-05-09 stsp static const struct got_error *
5356 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5357 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5358 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5359 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5360 036813ee 2019-05-09 stsp struct got_repository *repo)
5361 036813ee 2019-05-09 stsp {
5362 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5363 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5364 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5365 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5366 036813ee 2019-05-09 stsp
5367 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5368 ba580f68 2020-03-22 stsp *nentries = 0;
5369 036813ee 2019-05-09 stsp
5370 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5371 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5372 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5373 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5374 036813ee 2019-05-09 stsp
5375 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5376 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5377 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5378 036813ee 2019-05-09 stsp continue;
5379 036813ee 2019-05-09 stsp
5380 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5381 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5382 036813ee 2019-05-09 stsp continue;
5383 036813ee 2019-05-09 stsp
5384 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5385 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5386 036813ee 2019-05-09 stsp if (err)
5387 036813ee 2019-05-09 stsp goto done;
5388 036813ee 2019-05-09 stsp
5389 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5390 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5391 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5392 036813ee 2019-05-09 stsp if (err)
5393 036813ee 2019-05-09 stsp goto done;
5394 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5395 afa376bf 2019-05-09 stsp if (err)
5396 afa376bf 2019-05-09 stsp goto done;
5397 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5398 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5399 2b496619 2019-07-10 stsp if (err)
5400 2f51b5b3 2019-05-09 stsp goto done;
5401 ba580f68 2020-03-22 stsp (*nentries)++;
5402 2b496619 2019-07-10 stsp } else {
5403 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5404 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5405 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5406 2b496619 2019-07-10 stsp == NULL) {
5407 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5408 2b496619 2019-07-10 stsp child_path, path_base_tree,
5409 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5410 2b496619 2019-07-10 stsp repo);
5411 2b496619 2019-07-10 stsp if (err)
5412 2b496619 2019-07-10 stsp goto done;
5413 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5414 2b496619 2019-07-10 stsp if (err)
5415 2b496619 2019-07-10 stsp goto done;
5416 ba580f68 2020-03-22 stsp (*nentries)++;
5417 9ba0479c 2019-05-10 stsp }
5418 ed175427 2019-05-09 stsp }
5419 2f51b5b3 2019-05-09 stsp }
5420 2f51b5b3 2019-05-09 stsp
5421 2f51b5b3 2019-05-09 stsp if (base_tree) {
5422 56e0773d 2019-11-28 stsp int i, nbase_entries;
5423 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5424 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5425 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5426 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5427 63c5ca5d 2019-08-24 stsp
5428 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5429 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5430 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5431 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5432 63c5ca5d 2019-08-24 stsp if (err)
5433 63c5ca5d 2019-08-24 stsp goto done;
5434 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5435 63c5ca5d 2019-08-24 stsp if (err)
5436 63c5ca5d 2019-08-24 stsp goto done;
5437 ba580f68 2020-03-22 stsp (*nentries)++;
5438 63c5ca5d 2019-08-24 stsp continue;
5439 63c5ca5d 2019-08-24 stsp }
5440 2f51b5b3 2019-05-09 stsp
5441 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5442 44d03001 2019-05-09 stsp int modified;
5443 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5444 036813ee 2019-05-09 stsp if (err)
5445 036813ee 2019-05-09 stsp goto done;
5446 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5447 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5448 2f51b5b3 2019-05-09 stsp if (err)
5449 2f51b5b3 2019-05-09 stsp goto done;
5450 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5451 44d03001 2019-05-09 stsp if (modified) {
5452 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5453 ba580f68 2020-03-22 stsp int nsubentries;
5454 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5455 ba580f68 2020-03-22 stsp &nsubentries, te,
5456 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5457 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5458 44d03001 2019-05-09 stsp if (err)
5459 44d03001 2019-05-09 stsp goto done;
5460 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5461 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5462 ba580f68 2020-03-22 stsp free(new_id);
5463 ba580f68 2020-03-22 stsp continue;
5464 ba580f68 2020-03-22 stsp }
5465 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5466 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5467 56e0773d 2019-11-28 stsp free(new_id);
5468 44d03001 2019-05-09 stsp }
5469 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5470 036813ee 2019-05-09 stsp if (err)
5471 036813ee 2019-05-09 stsp goto done;
5472 ba580f68 2020-03-22 stsp (*nentries)++;
5473 2f51b5b3 2019-05-09 stsp continue;
5474 036813ee 2019-05-09 stsp }
5475 2f51b5b3 2019-05-09 stsp
5476 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5477 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5478 f66c734c 2019-09-22 stsp if (err)
5479 f66c734c 2019-09-22 stsp goto done;
5480 2f51b5b3 2019-05-09 stsp if (ct) {
5481 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5482 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5483 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5484 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5485 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5486 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5487 2f51b5b3 2019-05-09 stsp if (err)
5488 2f51b5b3 2019-05-09 stsp goto done;
5489 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5490 2f51b5b3 2019-05-09 stsp if (err)
5491 2f51b5b3 2019-05-09 stsp goto done;
5492 ba580f68 2020-03-22 stsp (*nentries)++;
5493 2f51b5b3 2019-05-09 stsp }
5494 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5495 b416585c 2019-05-13 stsp status_arg);
5496 afa376bf 2019-05-09 stsp if (err)
5497 afa376bf 2019-05-09 stsp goto done;
5498 2f51b5b3 2019-05-09 stsp } else {
5499 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5500 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5501 2f51b5b3 2019-05-09 stsp if (err)
5502 2f51b5b3 2019-05-09 stsp goto done;
5503 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5504 2f51b5b3 2019-05-09 stsp if (err)
5505 2f51b5b3 2019-05-09 stsp goto done;
5506 ba580f68 2020-03-22 stsp (*nentries)++;
5507 2f51b5b3 2019-05-09 stsp }
5508 ca2503ea 2019-05-09 stsp }
5509 ed175427 2019-05-09 stsp }
5510 0b5cc0d6 2019-05-09 stsp
5511 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5512 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5513 ed175427 2019-05-09 stsp done:
5514 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5515 2e1fa222 2020-07-23 stsp return err;
5516 2e1fa222 2020-07-23 stsp }
5517 2e1fa222 2020-07-23 stsp
5518 2e1fa222 2020-07-23 stsp static const struct got_error *
5519 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5520 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5521 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5522 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5523 ebf99748 2019-05-09 stsp {
5524 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5525 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5526 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5527 ebf99748 2019-05-09 stsp
5528 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5529 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5530 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5531 ebf99748 2019-05-09 stsp
5532 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5533 437adc9d 2020-12-10 yzhong
5534 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5535 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5536 437adc9d 2020-12-10 yzhong if (err)
5537 437adc9d 2020-12-10 yzhong goto done;
5538 437adc9d 2020-12-10 yzhong
5539 ebf99748 2019-05-09 stsp if (ie) {
5540 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5541 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5542 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5543 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5544 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5545 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5546 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5547 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5548 437adc9d 2020-12-10 yzhong
5549 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5550 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5551 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5552 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5553 72fd46fa 2019-09-06 stsp !have_staged_files);
5554 ebf99748 2019-05-09 stsp } else
5555 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5556 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5557 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5558 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5559 72fd46fa 2019-09-06 stsp !have_staged_files);
5560 ebf99748 2019-05-09 stsp } else {
5561 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5562 ebf99748 2019-05-09 stsp if (err)
5563 437adc9d 2020-12-10 yzhong goto done;
5564 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5565 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5566 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5567 3969253a 2020-03-07 stsp if (err) {
5568 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5569 437adc9d 2020-12-10 yzhong goto done;
5570 3969253a 2020-03-07 stsp }
5571 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5572 3969253a 2020-03-07 stsp if (err) {
5573 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5574 437adc9d 2020-12-10 yzhong goto done;
5575 3969253a 2020-03-07 stsp }
5576 ebf99748 2019-05-09 stsp }
5577 437adc9d 2020-12-10 yzhong free(relpath);
5578 437adc9d 2020-12-10 yzhong relpath = NULL;
5579 ebf99748 2019-05-09 stsp }
5580 437adc9d 2020-12-10 yzhong done:
5581 437adc9d 2020-12-10 yzhong free(relpath);
5582 d56d26ce 2019-05-10 stsp return err;
5583 d56d26ce 2019-05-10 stsp }
5584 735ef5ac 2019-08-03 stsp
5585 d56d26ce 2019-05-10 stsp
5586 d56d26ce 2019-05-10 stsp static const struct got_error *
5587 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5588 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5589 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5590 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5591 735ef5ac 2019-08-03 stsp int ood_errcode)
5592 d56d26ce 2019-05-10 stsp {
5593 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5594 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5595 1a36436d 2019-06-10 stsp
5596 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5597 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5598 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5599 1a36436d 2019-06-10 stsp return NULL;
5600 9bead371 2019-07-28 stsp /*
5601 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5602 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5603 9bead371 2019-07-28 stsp */
5604 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5605 735ef5ac 2019-08-03 stsp in_repo_path);
5606 9bead371 2019-07-28 stsp if (err) {
5607 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5608 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5609 1a36436d 2019-06-10 stsp goto done;
5610 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5611 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5612 1a36436d 2019-06-10 stsp } else {
5613 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5614 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5615 735ef5ac 2019-08-03 stsp in_repo_path);
5616 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5617 1a36436d 2019-06-10 stsp goto done;
5618 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5619 1a36436d 2019-06-10 stsp }
5620 1a36436d 2019-06-10 stsp done:
5621 1a36436d 2019-06-10 stsp free(id);
5622 1a36436d 2019-06-10 stsp return err;
5623 ebf99748 2019-05-09 stsp }
5624 ebf99748 2019-05-09 stsp
5625 c4296144 2019-05-09 stsp const struct got_error *
5626 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5627 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5628 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5629 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5630 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5631 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5632 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5633 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5634 afa376bf 2019-05-09 stsp struct got_repository *repo)
5635 c4296144 2019-05-09 stsp {
5636 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5637 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5638 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5639 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5640 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5641 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5642 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5643 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5644 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5645 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5646 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5647 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5648 c4296144 2019-05-09 stsp
5649 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5650 c4296144 2019-05-09 stsp
5651 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5652 675c7539 2019-05-09 stsp
5653 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5654 588edf97 2019-05-10 stsp if (err)
5655 588edf97 2019-05-10 stsp goto done;
5656 de18fc63 2019-05-09 stsp
5657 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5658 588edf97 2019-05-10 stsp if (err)
5659 588edf97 2019-05-10 stsp goto done;
5660 588edf97 2019-05-10 stsp
5661 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5662 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5663 33ad4cbe 2019-05-12 jcs if (err)
5664 33ad4cbe 2019-05-12 jcs goto done;
5665 33ad4cbe 2019-05-12 jcs }
5666 c4296144 2019-05-09 stsp
5667 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5668 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5669 33ad4cbe 2019-05-12 jcs goto done;
5670 33ad4cbe 2019-05-12 jcs }
5671 33ad4cbe 2019-05-12 jcs
5672 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5673 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5674 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5675 cf066bf8 2019-05-09 stsp char *ondisk_path;
5676 cf066bf8 2019-05-09 stsp
5677 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5678 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5679 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5680 5f8a88c6 2019-08-03 stsp continue;
5681 5f8a88c6 2019-08-03 stsp
5682 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5683 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5684 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5685 cf066bf8 2019-05-09 stsp continue;
5686 cf066bf8 2019-05-09 stsp
5687 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5688 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5689 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5690 cf066bf8 2019-05-09 stsp goto done;
5691 cf066bf8 2019-05-09 stsp }
5692 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5693 2e1fa222 2020-07-23 stsp free(ondisk_path);
5694 2e1fa222 2020-07-23 stsp if (err)
5695 cf066bf8 2019-05-09 stsp goto done;
5696 cf066bf8 2019-05-09 stsp }
5697 036813ee 2019-05-09 stsp
5698 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5699 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5700 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5701 036813ee 2019-05-09 stsp if (err)
5702 036813ee 2019-05-09 stsp goto done;
5703 036813ee 2019-05-09 stsp
5704 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5705 de18fc63 2019-05-09 stsp if (err)
5706 de18fc63 2019-05-09 stsp goto done;
5707 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5708 f259c4c1 2021-09-24 stsp nparents++;
5709 f259c4c1 2021-09-24 stsp if (parent_id2) {
5710 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5711 f259c4c1 2021-09-24 stsp if (err)
5712 f259c4c1 2021-09-24 stsp goto done;
5713 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5714 f259c4c1 2021-09-24 stsp nparents++;
5715 f259c4c1 2021-09-24 stsp }
5716 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5717 f259c4c1 2021-09-24 stsp nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5718 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5719 33ad4cbe 2019-05-12 jcs free(logmsg);
5720 9d40349a 2019-05-09 stsp if (err)
5721 9d40349a 2019-05-09 stsp goto done;
5722 9d40349a 2019-05-09 stsp
5723 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5724 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5725 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5726 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5727 09f5bd90 2019-05-09 stsp goto done;
5728 09f5bd90 2019-05-09 stsp }
5729 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5730 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5731 09f5bd90 2019-05-09 stsp if (err)
5732 09f5bd90 2019-05-09 stsp goto done;
5733 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5734 ebf99748 2019-05-09 stsp if (err)
5735 ebf99748 2019-05-09 stsp goto done;
5736 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5737 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5738 09f5bd90 2019-05-09 stsp goto done;
5739 09f5bd90 2019-05-09 stsp }
5740 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5741 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5742 f2c16586 2019-05-09 stsp if (err)
5743 f2c16586 2019-05-09 stsp goto done;
5744 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5745 f2c16586 2019-05-09 stsp if (err)
5746 f2c16586 2019-05-09 stsp goto done;
5747 f2c16586 2019-05-09 stsp
5748 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5749 09f5bd90 2019-05-09 stsp if (err)
5750 09f5bd90 2019-05-09 stsp goto done;
5751 09f5bd90 2019-05-09 stsp
5752 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5753 ebf99748 2019-05-09 stsp if (err)
5754 ebf99748 2019-05-09 stsp goto done;
5755 c4296144 2019-05-09 stsp done:
5756 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
5757 588edf97 2019-05-10 stsp if (head_tree)
5758 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5759 588edf97 2019-05-10 stsp if (head_commit)
5760 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5761 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5762 2f17228e 2019-05-12 stsp if (head_ref2) {
5763 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5764 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5765 2f17228e 2019-05-12 stsp err = unlockerr;
5766 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5767 39cd0ff6 2019-07-12 stsp }
5768 39cd0ff6 2019-07-12 stsp return err;
5769 39cd0ff6 2019-07-12 stsp }
5770 5c1e53bc 2019-07-28 stsp
5771 5c1e53bc 2019-07-28 stsp static const struct got_error *
5772 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5773 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5774 5c1e53bc 2019-07-28 stsp {
5775 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5776 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5777 39cd0ff6 2019-07-12 stsp
5778 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5779 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5780 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5781 5c1e53bc 2019-07-28 stsp
5782 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5783 5c1e53bc 2019-07-28 stsp ct_path++;
5784 5c1e53bc 2019-07-28 stsp
5785 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5786 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5787 5c1e53bc 2019-07-28 stsp break;
5788 5c1e53bc 2019-07-28 stsp }
5789 5c1e53bc 2019-07-28 stsp
5790 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5791 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5792 5c1e53bc 2019-07-28 stsp
5793 5c1e53bc 2019-07-28 stsp return NULL;
5794 5c1e53bc 2019-07-28 stsp }
5795 5c1e53bc 2019-07-28 stsp
5796 f0b75401 2019-08-03 stsp static const struct got_error *
5797 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5798 f0b75401 2019-08-03 stsp {
5799 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5800 f0b75401 2019-08-03 stsp
5801 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5802 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5803 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5804 f0b75401 2019-08-03 stsp }
5805 f0b75401 2019-08-03 stsp
5806 f0b75401 2019-08-03 stsp return NULL;
5807 f0b75401 2019-08-03 stsp }
5808 f0b75401 2019-08-03 stsp
5809 5f8a88c6 2019-08-03 stsp static const struct got_error *
5810 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5811 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5812 5f8a88c6 2019-08-03 stsp {
5813 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5814 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5815 5f8a88c6 2019-08-03 stsp
5816 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5817 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5818 5f8a88c6 2019-08-03 stsp continue;
5819 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5820 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5821 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5822 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5823 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5824 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5825 5f8a88c6 2019-08-03 stsp }
5826 5f8a88c6 2019-08-03 stsp
5827 5f8a88c6 2019-08-03 stsp return NULL;
5828 5f8a88c6 2019-08-03 stsp }
5829 5f8a88c6 2019-08-03 stsp
5830 39cd0ff6 2019-07-12 stsp const struct got_error *
5831 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5832 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5833 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5834 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5835 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5836 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5837 39cd0ff6 2019-07-12 stsp {
5838 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5839 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5840 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5841 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5842 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5843 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5844 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5845 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5846 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5847 39cd0ff6 2019-07-12 stsp
5848 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5849 39cd0ff6 2019-07-12 stsp
5850 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5851 39cd0ff6 2019-07-12 stsp
5852 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5853 39cd0ff6 2019-07-12 stsp if (err)
5854 39cd0ff6 2019-07-12 stsp goto done;
5855 39cd0ff6 2019-07-12 stsp
5856 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5857 39cd0ff6 2019-07-12 stsp if (err)
5858 39cd0ff6 2019-07-12 stsp goto done;
5859 39cd0ff6 2019-07-12 stsp
5860 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5861 39cd0ff6 2019-07-12 stsp if (err)
5862 39cd0ff6 2019-07-12 stsp goto done;
5863 39cd0ff6 2019-07-12 stsp
5864 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5865 39cd0ff6 2019-07-12 stsp if (err)
5866 39cd0ff6 2019-07-12 stsp goto done;
5867 39cd0ff6 2019-07-12 stsp
5868 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5869 5f8a88c6 2019-08-03 stsp &have_staged_files);
5870 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5871 5f8a88c6 2019-08-03 stsp goto done;
5872 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5873 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5874 5f8a88c6 2019-08-03 stsp if (err)
5875 5f8a88c6 2019-08-03 stsp goto done;
5876 5f8a88c6 2019-08-03 stsp }
5877 5f8a88c6 2019-08-03 stsp
5878 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5879 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5880 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5881 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5882 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5883 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5884 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5885 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5886 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5887 5c1e53bc 2019-07-28 stsp if (err)
5888 5c1e53bc 2019-07-28 stsp goto done;
5889 5c1e53bc 2019-07-28 stsp }
5890 39cd0ff6 2019-07-12 stsp
5891 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5892 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5893 39cd0ff6 2019-07-12 stsp goto done;
5894 5c1e53bc 2019-07-28 stsp }
5895 5c1e53bc 2019-07-28 stsp
5896 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5897 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5898 5c1e53bc 2019-07-28 stsp if (err)
5899 5c1e53bc 2019-07-28 stsp goto done;
5900 39cd0ff6 2019-07-12 stsp }
5901 f0b75401 2019-08-03 stsp
5902 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5903 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5904 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5905 f0b75401 2019-08-03 stsp
5906 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5907 f0b75401 2019-08-03 stsp ct_path++;
5908 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5909 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5910 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5911 b50cabdf 2019-07-12 stsp if (err)
5912 b50cabdf 2019-07-12 stsp goto done;
5913 f0b75401 2019-08-03 stsp
5914 b50cabdf 2019-07-12 stsp }
5915 b50cabdf 2019-07-12 stsp
5916 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5917 f259c4c1 2021-09-24 stsp head_commit_id, NULL, worktree, author, committer,
5918 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5919 39cd0ff6 2019-07-12 stsp if (err)
5920 39cd0ff6 2019-07-12 stsp goto done;
5921 39cd0ff6 2019-07-12 stsp
5922 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5923 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5924 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5925 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5926 39cd0ff6 2019-07-12 stsp err = sync_err;
5927 39cd0ff6 2019-07-12 stsp done:
5928 39cd0ff6 2019-07-12 stsp if (fileindex)
5929 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5930 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5931 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5932 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5933 39cd0ff6 2019-07-12 stsp err = unlockerr;
5934 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5935 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5936 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5937 39cd0ff6 2019-07-12 stsp }
5938 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5939 c4296144 2019-05-09 stsp return err;
5940 8656d6c4 2019-05-20 stsp }
5941 8656d6c4 2019-05-20 stsp
5942 8656d6c4 2019-05-20 stsp const char *
5943 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5944 8656d6c4 2019-05-20 stsp {
5945 8656d6c4 2019-05-20 stsp return ct->path;
5946 8656d6c4 2019-05-20 stsp }
5947 8656d6c4 2019-05-20 stsp
5948 8656d6c4 2019-05-20 stsp unsigned int
5949 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5950 8656d6c4 2019-05-20 stsp {
5951 8656d6c4 2019-05-20 stsp return ct->status;
5952 818c7501 2019-07-11 stsp }
5953 818c7501 2019-07-11 stsp
5954 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5955 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5956 818c7501 2019-07-11 stsp struct got_repository *repo;
5957 818c7501 2019-07-11 stsp };
5958 818c7501 2019-07-11 stsp
5959 818c7501 2019-07-11 stsp static const struct got_error *
5960 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5961 818c7501 2019-07-11 stsp {
5962 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5963 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5964 818c7501 2019-07-11 stsp unsigned char status;
5965 818c7501 2019-07-11 stsp struct stat sb;
5966 818c7501 2019-07-11 stsp char *ondisk_path;
5967 818c7501 2019-07-11 stsp
5968 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5969 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5970 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5971 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5972 818c7501 2019-07-11 stsp
5973 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5974 818c7501 2019-07-11 stsp == -1)
5975 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5976 818c7501 2019-07-11 stsp
5977 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5978 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5979 818c7501 2019-07-11 stsp free(ondisk_path);
5980 818c7501 2019-07-11 stsp if (err)
5981 818c7501 2019-07-11 stsp return err;
5982 818c7501 2019-07-11 stsp
5983 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5984 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5985 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5986 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5987 818c7501 2019-07-11 stsp
5988 818c7501 2019-07-11 stsp return NULL;
5989 818c7501 2019-07-11 stsp }
5990 818c7501 2019-07-11 stsp
5991 818c7501 2019-07-11 stsp const struct got_error *
5992 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5993 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5994 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5995 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5996 818c7501 2019-07-11 stsp {
5997 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5998 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5999 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
6000 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6001 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
6002 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
6003 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
6004 818c7501 2019-07-11 stsp
6005 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6006 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6007 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6008 818c7501 2019-07-11 stsp
6009 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6010 818c7501 2019-07-11 stsp if (err)
6011 818c7501 2019-07-11 stsp return err;
6012 818c7501 2019-07-11 stsp
6013 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6014 818c7501 2019-07-11 stsp if (err)
6015 818c7501 2019-07-11 stsp goto done;
6016 818c7501 2019-07-11 stsp
6017 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6018 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6019 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6020 818c7501 2019-07-11 stsp &ok_arg);
6021 818c7501 2019-07-11 stsp if (err)
6022 818c7501 2019-07-11 stsp goto done;
6023 818c7501 2019-07-11 stsp
6024 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6025 818c7501 2019-07-11 stsp if (err)
6026 818c7501 2019-07-11 stsp goto done;
6027 818c7501 2019-07-11 stsp
6028 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6029 818c7501 2019-07-11 stsp if (err)
6030 818c7501 2019-07-11 stsp goto done;
6031 818c7501 2019-07-11 stsp
6032 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6033 818c7501 2019-07-11 stsp if (err)
6034 818c7501 2019-07-11 stsp goto done;
6035 818c7501 2019-07-11 stsp
6036 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6037 818c7501 2019-07-11 stsp 0);
6038 e51d7b55 2020-01-04 stsp if (err)
6039 e51d7b55 2020-01-04 stsp goto done;
6040 e51d7b55 2020-01-04 stsp
6041 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6042 818c7501 2019-07-11 stsp if (err)
6043 818c7501 2019-07-11 stsp goto done;
6044 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6045 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6046 e51d7b55 2020-01-04 stsp goto done;
6047 e51d7b55 2020-01-04 stsp }
6048 818c7501 2019-07-11 stsp
6049 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6050 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6051 818c7501 2019-07-11 stsp if (err)
6052 818c7501 2019-07-11 stsp goto done;
6053 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6054 818c7501 2019-07-11 stsp if (err)
6055 818c7501 2019-07-11 stsp goto done;
6056 818c7501 2019-07-11 stsp
6057 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6058 818c7501 2019-07-11 stsp
6059 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6060 818c7501 2019-07-11 stsp if (err)
6061 818c7501 2019-07-11 stsp goto done;
6062 818c7501 2019-07-11 stsp
6063 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6064 818c7501 2019-07-11 stsp if (err)
6065 818c7501 2019-07-11 stsp goto done;
6066 818c7501 2019-07-11 stsp
6067 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6068 818c7501 2019-07-11 stsp worktree->base_commit_id);
6069 818c7501 2019-07-11 stsp if (err)
6070 818c7501 2019-07-11 stsp goto done;
6071 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
6072 818c7501 2019-07-11 stsp if (err)
6073 818c7501 2019-07-11 stsp goto done;
6074 818c7501 2019-07-11 stsp
6075 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6076 818c7501 2019-07-11 stsp if (err)
6077 818c7501 2019-07-11 stsp goto done;
6078 818c7501 2019-07-11 stsp done:
6079 818c7501 2019-07-11 stsp free(fileindex_path);
6080 818c7501 2019-07-11 stsp free(tmp_branch_name);
6081 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6082 818c7501 2019-07-11 stsp free(branch_ref_name);
6083 818c7501 2019-07-11 stsp if (branch_ref)
6084 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6085 818c7501 2019-07-11 stsp if (wt_branch)
6086 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6087 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6088 818c7501 2019-07-11 stsp if (err) {
6089 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6090 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6091 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6092 818c7501 2019-07-11 stsp }
6093 818c7501 2019-07-11 stsp if (*tmp_branch) {
6094 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6095 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6096 818c7501 2019-07-11 stsp }
6097 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6098 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6099 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6100 3e3a69f1 2019-07-25 stsp }
6101 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6102 818c7501 2019-07-11 stsp }
6103 818c7501 2019-07-11 stsp return err;
6104 818c7501 2019-07-11 stsp }
6105 818c7501 2019-07-11 stsp
6106 818c7501 2019-07-11 stsp const struct got_error *
6107 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6108 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6109 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6110 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6111 818c7501 2019-07-11 stsp {
6112 818c7501 2019-07-11 stsp const struct got_error *err;
6113 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6114 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6115 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6116 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6117 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6118 818c7501 2019-07-11 stsp
6119 818c7501 2019-07-11 stsp *commit_id = NULL;
6120 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6121 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6122 3e3a69f1 2019-07-25 stsp *branch = NULL;
6123 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6124 3e3a69f1 2019-07-25 stsp
6125 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6126 3e3a69f1 2019-07-25 stsp if (err)
6127 3e3a69f1 2019-07-25 stsp return err;
6128 818c7501 2019-07-11 stsp
6129 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6130 3e3a69f1 2019-07-25 stsp if (err)
6131 f032f1f7 2019-08-04 stsp goto done;
6132 f032f1f7 2019-08-04 stsp
6133 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6134 f032f1f7 2019-08-04 stsp &have_staged_files);
6135 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6136 f032f1f7 2019-08-04 stsp goto done;
6137 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6138 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6139 3e3a69f1 2019-07-25 stsp goto done;
6140 f032f1f7 2019-08-04 stsp }
6141 3e3a69f1 2019-07-25 stsp
6142 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6143 818c7501 2019-07-11 stsp if (err)
6144 f032f1f7 2019-08-04 stsp goto done;
6145 818c7501 2019-07-11 stsp
6146 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6147 818c7501 2019-07-11 stsp if (err)
6148 818c7501 2019-07-11 stsp goto done;
6149 818c7501 2019-07-11 stsp
6150 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6151 818c7501 2019-07-11 stsp if (err)
6152 818c7501 2019-07-11 stsp goto done;
6153 818c7501 2019-07-11 stsp
6154 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6155 818c7501 2019-07-11 stsp if (err)
6156 818c7501 2019-07-11 stsp goto done;
6157 818c7501 2019-07-11 stsp
6158 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6159 818c7501 2019-07-11 stsp if (err)
6160 818c7501 2019-07-11 stsp goto done;
6161 818c7501 2019-07-11 stsp
6162 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
6163 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6164 818c7501 2019-07-11 stsp if (err)
6165 818c7501 2019-07-11 stsp goto done;
6166 818c7501 2019-07-11 stsp
6167 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6168 818c7501 2019-07-11 stsp if (err)
6169 818c7501 2019-07-11 stsp goto done;
6170 818c7501 2019-07-11 stsp
6171 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6172 818c7501 2019-07-11 stsp if (err)
6173 818c7501 2019-07-11 stsp goto done;
6174 818c7501 2019-07-11 stsp
6175 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6176 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6177 818c7501 2019-07-11 stsp if (err)
6178 818c7501 2019-07-11 stsp goto done;
6179 818c7501 2019-07-11 stsp
6180 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6181 818c7501 2019-07-11 stsp if (err)
6182 818c7501 2019-07-11 stsp goto done;
6183 818c7501 2019-07-11 stsp done:
6184 818c7501 2019-07-11 stsp free(commit_ref_name);
6185 818c7501 2019-07-11 stsp free(branch_ref_name);
6186 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6187 818c7501 2019-07-11 stsp if (commit_ref)
6188 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6189 818c7501 2019-07-11 stsp if (branch_ref)
6190 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6191 818c7501 2019-07-11 stsp if (err) {
6192 818c7501 2019-07-11 stsp free(*commit_id);
6193 818c7501 2019-07-11 stsp *commit_id = NULL;
6194 818c7501 2019-07-11 stsp if (*tmp_branch) {
6195 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6196 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6197 818c7501 2019-07-11 stsp }
6198 818c7501 2019-07-11 stsp if (*new_base_branch) {
6199 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6200 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6201 818c7501 2019-07-11 stsp }
6202 818c7501 2019-07-11 stsp if (*branch) {
6203 818c7501 2019-07-11 stsp got_ref_close(*branch);
6204 818c7501 2019-07-11 stsp *branch = NULL;
6205 818c7501 2019-07-11 stsp }
6206 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6207 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6208 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6209 3e3a69f1 2019-07-25 stsp }
6210 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6211 818c7501 2019-07-11 stsp }
6212 818c7501 2019-07-11 stsp return err;
6213 c4296144 2019-05-09 stsp }
6214 818c7501 2019-07-11 stsp
6215 818c7501 2019-07-11 stsp const struct got_error *
6216 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6217 818c7501 2019-07-11 stsp {
6218 818c7501 2019-07-11 stsp const struct got_error *err;
6219 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6220 818c7501 2019-07-11 stsp
6221 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6222 818c7501 2019-07-11 stsp if (err)
6223 818c7501 2019-07-11 stsp return err;
6224 818c7501 2019-07-11 stsp
6225 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6226 818c7501 2019-07-11 stsp free(tmp_branch_name);
6227 818c7501 2019-07-11 stsp return NULL;
6228 818c7501 2019-07-11 stsp }
6229 818c7501 2019-07-11 stsp
6230 818c7501 2019-07-11 stsp static const struct got_error *
6231 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6232 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6233 818c7501 2019-07-11 stsp {
6234 0ebf8283 2019-07-24 stsp *logmsg = arg;
6235 818c7501 2019-07-11 stsp return NULL;
6236 818c7501 2019-07-11 stsp }
6237 818c7501 2019-07-11 stsp
6238 818c7501 2019-07-11 stsp static const struct got_error *
6239 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6240 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6241 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6242 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6243 818c7501 2019-07-11 stsp {
6244 818c7501 2019-07-11 stsp return NULL;
6245 01757395 2019-07-12 stsp }
6246 01757395 2019-07-12 stsp
6247 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6248 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6249 01757395 2019-07-12 stsp void *progress_arg;
6250 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6251 01757395 2019-07-12 stsp };
6252 01757395 2019-07-12 stsp
6253 01757395 2019-07-12 stsp static const struct got_error *
6254 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6255 01757395 2019-07-12 stsp {
6256 01757395 2019-07-12 stsp const struct got_error *err;
6257 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6258 01757395 2019-07-12 stsp char *p;
6259 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6260 01757395 2019-07-12 stsp
6261 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6262 01757395 2019-07-12 stsp if (err)
6263 01757395 2019-07-12 stsp return err;
6264 01757395 2019-07-12 stsp
6265 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6266 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6267 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6268 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6269 01757395 2019-07-12 stsp return NULL;
6270 01757395 2019-07-12 stsp
6271 01757395 2019-07-12 stsp p = strdup(path);
6272 01757395 2019-07-12 stsp if (p == NULL)
6273 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6274 01757395 2019-07-12 stsp
6275 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6276 01757395 2019-07-12 stsp if (err || new == NULL)
6277 01757395 2019-07-12 stsp free(p);
6278 01757395 2019-07-12 stsp return err;
6279 01757395 2019-07-12 stsp }
6280 01757395 2019-07-12 stsp
6281 01757395 2019-07-12 stsp void
6282 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6283 01757395 2019-07-12 stsp {
6284 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6285 01757395 2019-07-12 stsp
6286 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6287 01757395 2019-07-12 stsp free((char *)pe->path);
6288 01757395 2019-07-12 stsp
6289 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6290 818c7501 2019-07-11 stsp }
6291 818c7501 2019-07-11 stsp
6292 0ebf8283 2019-07-24 stsp static const struct got_error *
6293 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6294 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6295 818c7501 2019-07-11 stsp {
6296 818c7501 2019-07-11 stsp const struct got_error *err;
6297 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6298 818c7501 2019-07-11 stsp
6299 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6300 818c7501 2019-07-11 stsp if (err) {
6301 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6302 818c7501 2019-07-11 stsp goto done;
6303 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6304 818c7501 2019-07-11 stsp if (err)
6305 818c7501 2019-07-11 stsp goto done;
6306 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6307 818c7501 2019-07-11 stsp if (err)
6308 818c7501 2019-07-11 stsp goto done;
6309 de05890f 2020-03-05 stsp } else if (is_rebase) {
6310 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6311 818c7501 2019-07-11 stsp int cmp;
6312 818c7501 2019-07-11 stsp
6313 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6314 818c7501 2019-07-11 stsp if (err)
6315 818c7501 2019-07-11 stsp goto done;
6316 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6317 818c7501 2019-07-11 stsp free(stored_id);
6318 818c7501 2019-07-11 stsp if (cmp != 0) {
6319 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6320 818c7501 2019-07-11 stsp goto done;
6321 818c7501 2019-07-11 stsp }
6322 818c7501 2019-07-11 stsp }
6323 0ebf8283 2019-07-24 stsp done:
6324 0ebf8283 2019-07-24 stsp if (commit_ref)
6325 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6326 0ebf8283 2019-07-24 stsp return err;
6327 0ebf8283 2019-07-24 stsp }
6328 0ebf8283 2019-07-24 stsp
6329 0ebf8283 2019-07-24 stsp static const struct got_error *
6330 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6331 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6332 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6333 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6334 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6335 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6336 0ebf8283 2019-07-24 stsp {
6337 0ebf8283 2019-07-24 stsp const struct got_error *err;
6338 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6339 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6340 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6341 818c7501 2019-07-11 stsp
6342 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6343 0ebf8283 2019-07-24 stsp
6344 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6345 0ebf8283 2019-07-24 stsp if (err)
6346 0ebf8283 2019-07-24 stsp return err;
6347 0ebf8283 2019-07-24 stsp
6348 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6349 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6350 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6351 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6352 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6353 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6354 818c7501 2019-07-11 stsp if (commit_ref)
6355 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6356 818c7501 2019-07-11 stsp return err;
6357 818c7501 2019-07-11 stsp }
6358 818c7501 2019-07-11 stsp
6359 818c7501 2019-07-11 stsp const struct got_error *
6360 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6361 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6362 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6363 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6364 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6365 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6366 818c7501 2019-07-11 stsp {
6367 0ebf8283 2019-07-24 stsp const struct got_error *err;
6368 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6369 0ebf8283 2019-07-24 stsp
6370 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6371 0ebf8283 2019-07-24 stsp if (err)
6372 0ebf8283 2019-07-24 stsp return err;
6373 0ebf8283 2019-07-24 stsp
6374 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6375 0ebf8283 2019-07-24 stsp if (err)
6376 0ebf8283 2019-07-24 stsp goto done;
6377 0ebf8283 2019-07-24 stsp
6378 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6379 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6380 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6381 0ebf8283 2019-07-24 stsp done:
6382 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6383 0ebf8283 2019-07-24 stsp return err;
6384 0ebf8283 2019-07-24 stsp }
6385 0ebf8283 2019-07-24 stsp
6386 0ebf8283 2019-07-24 stsp const struct got_error *
6387 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6388 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6389 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6390 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6391 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6392 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6393 0ebf8283 2019-07-24 stsp {
6394 0ebf8283 2019-07-24 stsp const struct got_error *err;
6395 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6396 0ebf8283 2019-07-24 stsp
6397 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6398 0ebf8283 2019-07-24 stsp if (err)
6399 0ebf8283 2019-07-24 stsp return err;
6400 0ebf8283 2019-07-24 stsp
6401 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6402 0ebf8283 2019-07-24 stsp if (err)
6403 0ebf8283 2019-07-24 stsp goto done;
6404 0ebf8283 2019-07-24 stsp
6405 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6406 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6407 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6408 0ebf8283 2019-07-24 stsp done:
6409 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6410 0ebf8283 2019-07-24 stsp return err;
6411 0ebf8283 2019-07-24 stsp }
6412 0ebf8283 2019-07-24 stsp
6413 0ebf8283 2019-07-24 stsp static const struct got_error *
6414 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6415 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6416 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6417 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6418 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6419 0ebf8283 2019-07-24 stsp {
6420 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6421 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6422 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6423 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6424 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6425 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6426 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6427 818c7501 2019-07-11 stsp
6428 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6429 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6430 a0e95631 2019-07-12 stsp
6431 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6432 818c7501 2019-07-11 stsp
6433 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6434 a0e95631 2019-07-12 stsp if (err)
6435 3e3a69f1 2019-07-25 stsp return err;
6436 a0e95631 2019-07-12 stsp
6437 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6438 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6439 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6440 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6441 01757395 2019-07-12 stsp /*
6442 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6443 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6444 6c13b005 2021-09-02 stsp *
6445 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6446 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6447 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6448 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6449 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6450 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6451 01757395 2019-07-12 stsp */
6452 01757395 2019-07-12 stsp if (merged_paths) {
6453 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6454 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6455 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6456 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6457 f2a9dc41 2019-12-13 tracey 0);
6458 01757395 2019-07-12 stsp if (err)
6459 01757395 2019-07-12 stsp goto done;
6460 01757395 2019-07-12 stsp }
6461 01757395 2019-07-12 stsp } else {
6462 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6463 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6464 01757395 2019-07-12 stsp if (err)
6465 01757395 2019-07-12 stsp goto done;
6466 01757395 2019-07-12 stsp }
6467 a0e95631 2019-07-12 stsp
6468 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6469 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6470 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6471 a0e95631 2019-07-12 stsp if (err)
6472 a0e95631 2019-07-12 stsp goto done;
6473 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6474 a0e95631 2019-07-12 stsp goto done;
6475 ff0d2220 2019-07-11 stsp }
6476 818c7501 2019-07-11 stsp
6477 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6478 edd02c5e 2019-07-11 stsp if (err)
6479 edd02c5e 2019-07-11 stsp goto done;
6480 edd02c5e 2019-07-11 stsp
6481 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6482 818c7501 2019-07-11 stsp if (err)
6483 818c7501 2019-07-11 stsp goto done;
6484 818c7501 2019-07-11 stsp
6485 5943eee2 2019-08-13 stsp if (new_logmsg) {
6486 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6487 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6488 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6489 5943eee2 2019-08-13 stsp goto done;
6490 5943eee2 2019-08-13 stsp }
6491 5943eee2 2019-08-13 stsp } else {
6492 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6493 5943eee2 2019-08-13 stsp if (err)
6494 5943eee2 2019-08-13 stsp goto done;
6495 5943eee2 2019-08-13 stsp }
6496 0ebf8283 2019-07-24 stsp
6497 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6498 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6499 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6500 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6501 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6502 a0e95631 2019-07-12 stsp if (err)
6503 a0e95631 2019-07-12 stsp goto done;
6504 a0e95631 2019-07-12 stsp
6505 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6506 a0e95631 2019-07-12 stsp if (err)
6507 a0e95631 2019-07-12 stsp goto done;
6508 a0e95631 2019-07-12 stsp
6509 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6510 a0e95631 2019-07-12 stsp if (err)
6511 a0e95631 2019-07-12 stsp goto done;
6512 a0e95631 2019-07-12 stsp
6513 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6514 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6515 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6516 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6517 a0e95631 2019-07-12 stsp err = sync_err;
6518 818c7501 2019-07-11 stsp done:
6519 a0e95631 2019-07-12 stsp free(fileindex_path);
6520 a0e95631 2019-07-12 stsp free(head_commit_id);
6521 a0e95631 2019-07-12 stsp if (head_ref)
6522 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6523 818c7501 2019-07-11 stsp if (err) {
6524 818c7501 2019-07-11 stsp free(*new_commit_id);
6525 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6526 818c7501 2019-07-11 stsp }
6527 818c7501 2019-07-11 stsp return err;
6528 818c7501 2019-07-11 stsp }
6529 818c7501 2019-07-11 stsp
6530 818c7501 2019-07-11 stsp const struct got_error *
6531 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6532 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6533 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6534 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6535 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6536 0ebf8283 2019-07-24 stsp {
6537 0ebf8283 2019-07-24 stsp const struct got_error *err;
6538 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6539 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6540 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6541 0ebf8283 2019-07-24 stsp
6542 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6543 0ebf8283 2019-07-24 stsp if (err)
6544 0ebf8283 2019-07-24 stsp return err;
6545 0ebf8283 2019-07-24 stsp
6546 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6547 0ebf8283 2019-07-24 stsp if (err)
6548 0ebf8283 2019-07-24 stsp goto done;
6549 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6550 0ebf8283 2019-07-24 stsp if (err)
6551 0ebf8283 2019-07-24 stsp goto done;
6552 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6553 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6554 0ebf8283 2019-07-24 stsp goto done;
6555 0ebf8283 2019-07-24 stsp }
6556 0ebf8283 2019-07-24 stsp
6557 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6558 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6559 0ebf8283 2019-07-24 stsp done:
6560 0ebf8283 2019-07-24 stsp if (commit_ref)
6561 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6562 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6563 0ebf8283 2019-07-24 stsp free(commit_id);
6564 0ebf8283 2019-07-24 stsp return err;
6565 0ebf8283 2019-07-24 stsp }
6566 0ebf8283 2019-07-24 stsp
6567 0ebf8283 2019-07-24 stsp const struct got_error *
6568 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6569 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6570 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6571 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6572 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6573 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6574 0ebf8283 2019-07-24 stsp {
6575 0ebf8283 2019-07-24 stsp const struct got_error *err;
6576 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6577 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6578 0ebf8283 2019-07-24 stsp
6579 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6580 0ebf8283 2019-07-24 stsp if (err)
6581 0ebf8283 2019-07-24 stsp return err;
6582 0ebf8283 2019-07-24 stsp
6583 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6584 0ebf8283 2019-07-24 stsp if (err)
6585 0ebf8283 2019-07-24 stsp goto done;
6586 0ebf8283 2019-07-24 stsp
6587 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6588 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6589 0ebf8283 2019-07-24 stsp done:
6590 0ebf8283 2019-07-24 stsp if (commit_ref)
6591 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6592 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6593 0ebf8283 2019-07-24 stsp return err;
6594 0ebf8283 2019-07-24 stsp }
6595 0ebf8283 2019-07-24 stsp
6596 0ebf8283 2019-07-24 stsp const struct got_error *
6597 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6598 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6599 818c7501 2019-07-11 stsp {
6600 3e3a69f1 2019-07-25 stsp if (fileindex)
6601 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6602 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6603 69844fba 2019-07-11 stsp }
6604 69844fba 2019-07-11 stsp
6605 69844fba 2019-07-11 stsp static const struct got_error *
6606 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6607 69844fba 2019-07-11 stsp {
6608 69844fba 2019-07-11 stsp const struct got_error *err;
6609 69844fba 2019-07-11 stsp struct got_reference *ref;
6610 69844fba 2019-07-11 stsp
6611 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6612 69844fba 2019-07-11 stsp if (err) {
6613 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6614 69844fba 2019-07-11 stsp return NULL;
6615 69844fba 2019-07-11 stsp return err;
6616 69844fba 2019-07-11 stsp }
6617 69844fba 2019-07-11 stsp
6618 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6619 69844fba 2019-07-11 stsp got_ref_close(ref);
6620 69844fba 2019-07-11 stsp return err;
6621 818c7501 2019-07-11 stsp }
6622 818c7501 2019-07-11 stsp
6623 69844fba 2019-07-11 stsp static const struct got_error *
6624 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6625 69844fba 2019-07-11 stsp {
6626 69844fba 2019-07-11 stsp const struct got_error *err;
6627 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6628 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6629 69844fba 2019-07-11 stsp
6630 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6631 69844fba 2019-07-11 stsp if (err)
6632 69844fba 2019-07-11 stsp goto done;
6633 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6634 69844fba 2019-07-11 stsp if (err)
6635 69844fba 2019-07-11 stsp goto done;
6636 69844fba 2019-07-11 stsp
6637 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6638 69844fba 2019-07-11 stsp if (err)
6639 69844fba 2019-07-11 stsp goto done;
6640 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6641 69844fba 2019-07-11 stsp if (err)
6642 69844fba 2019-07-11 stsp goto done;
6643 69844fba 2019-07-11 stsp
6644 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6645 69844fba 2019-07-11 stsp if (err)
6646 69844fba 2019-07-11 stsp goto done;
6647 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6648 69844fba 2019-07-11 stsp if (err)
6649 69844fba 2019-07-11 stsp goto done;
6650 69844fba 2019-07-11 stsp
6651 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6652 69844fba 2019-07-11 stsp if (err)
6653 69844fba 2019-07-11 stsp goto done;
6654 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6655 69844fba 2019-07-11 stsp if (err)
6656 69844fba 2019-07-11 stsp goto done;
6657 69844fba 2019-07-11 stsp
6658 69844fba 2019-07-11 stsp done:
6659 69844fba 2019-07-11 stsp free(tmp_branch_name);
6660 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6661 69844fba 2019-07-11 stsp free(branch_ref_name);
6662 69844fba 2019-07-11 stsp free(commit_ref_name);
6663 e600f124 2021-03-21 stsp return err;
6664 e600f124 2021-03-21 stsp }
6665 e600f124 2021-03-21 stsp
6666 e600f124 2021-03-21 stsp const struct got_error *
6667 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6668 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6669 e600f124 2021-03-21 stsp {
6670 e600f124 2021-03-21 stsp const struct got_error *err;
6671 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6672 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6673 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6674 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6675 e600f124 2021-03-21 stsp char *refname = NULL;
6676 e600f124 2021-03-21 stsp
6677 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6678 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6679 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6680 e600f124 2021-03-21 stsp branch_name += 11;
6681 e600f124 2021-03-21 stsp
6682 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6683 e600f124 2021-03-21 stsp if (err)
6684 e600f124 2021-03-21 stsp return err;
6685 e600f124 2021-03-21 stsp
6686 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6687 e600f124 2021-03-21 stsp new_id_str) == -1) {
6688 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6689 e600f124 2021-03-21 stsp goto done;
6690 e600f124 2021-03-21 stsp }
6691 e600f124 2021-03-21 stsp
6692 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6693 e600f124 2021-03-21 stsp if (err)
6694 e600f124 2021-03-21 stsp goto done;
6695 e600f124 2021-03-21 stsp
6696 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6697 e600f124 2021-03-21 stsp if (err)
6698 e600f124 2021-03-21 stsp goto done;
6699 e600f124 2021-03-21 stsp
6700 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6701 e600f124 2021-03-21 stsp done:
6702 e600f124 2021-03-21 stsp free(new_id_str);
6703 e600f124 2021-03-21 stsp free(refname);
6704 e600f124 2021-03-21 stsp free(old_commit_id);
6705 e600f124 2021-03-21 stsp if (ref)
6706 e600f124 2021-03-21 stsp got_ref_close(ref);
6707 69844fba 2019-07-11 stsp return err;
6708 69844fba 2019-07-11 stsp }
6709 69844fba 2019-07-11 stsp
6710 818c7501 2019-07-11 stsp const struct got_error *
6711 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6712 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6713 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6714 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6715 818c7501 2019-07-11 stsp {
6716 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6717 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6718 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6719 818c7501 2019-07-11 stsp
6720 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6721 818c7501 2019-07-11 stsp if (err)
6722 818c7501 2019-07-11 stsp return err;
6723 e600f124 2021-03-21 stsp
6724 e600f124 2021-03-21 stsp if (create_backup) {
6725 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6726 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6727 e600f124 2021-03-21 stsp if (err)
6728 e600f124 2021-03-21 stsp goto done;
6729 e600f124 2021-03-21 stsp }
6730 818c7501 2019-07-11 stsp
6731 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6732 818c7501 2019-07-11 stsp if (err)
6733 818c7501 2019-07-11 stsp goto done;
6734 818c7501 2019-07-11 stsp
6735 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6736 818c7501 2019-07-11 stsp if (err)
6737 818c7501 2019-07-11 stsp goto done;
6738 818c7501 2019-07-11 stsp
6739 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6740 818c7501 2019-07-11 stsp if (err)
6741 818c7501 2019-07-11 stsp goto done;
6742 818c7501 2019-07-11 stsp
6743 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6744 a615e0e7 2020-12-16 stsp if (err)
6745 a615e0e7 2020-12-16 stsp goto done;
6746 a615e0e7 2020-12-16 stsp
6747 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6748 a615e0e7 2020-12-16 stsp if (err)
6749 a615e0e7 2020-12-16 stsp goto done;
6750 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6751 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6752 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6753 a615e0e7 2020-12-16 stsp err = sync_err;
6754 818c7501 2019-07-11 stsp done:
6755 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6756 a615e0e7 2020-12-16 stsp free(fileindex_path);
6757 818c7501 2019-07-11 stsp free(new_head_commit_id);
6758 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6759 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6760 818c7501 2019-07-11 stsp err = unlockerr;
6761 818c7501 2019-07-11 stsp return err;
6762 818c7501 2019-07-11 stsp }
6763 818c7501 2019-07-11 stsp
6764 818c7501 2019-07-11 stsp const struct got_error *
6765 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6766 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6767 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6768 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6769 818c7501 2019-07-11 stsp {
6770 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6771 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6772 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6773 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6774 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6775 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6776 818c7501 2019-07-11 stsp
6777 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6778 818c7501 2019-07-11 stsp if (err)
6779 818c7501 2019-07-11 stsp return err;
6780 818c7501 2019-07-11 stsp
6781 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6782 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6783 818c7501 2019-07-11 stsp if (err)
6784 818c7501 2019-07-11 stsp goto done;
6785 818c7501 2019-07-11 stsp
6786 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6787 818c7501 2019-07-11 stsp if (err)
6788 818c7501 2019-07-11 stsp goto done;
6789 818c7501 2019-07-11 stsp
6790 818c7501 2019-07-11 stsp /*
6791 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6792 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6793 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6794 818c7501 2019-07-11 stsp */
6795 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6796 818c7501 2019-07-11 stsp if (err)
6797 818c7501 2019-07-11 stsp goto done;
6798 818c7501 2019-07-11 stsp
6799 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6800 818c7501 2019-07-11 stsp if (err)
6801 818c7501 2019-07-11 stsp goto done;
6802 818c7501 2019-07-11 stsp
6803 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6804 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6805 ca355955 2019-07-12 stsp if (err)
6806 ca355955 2019-07-12 stsp goto done;
6807 ca355955 2019-07-12 stsp
6808 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6809 ca355955 2019-07-12 stsp if (err)
6810 ca355955 2019-07-12 stsp goto done;
6811 ca355955 2019-07-12 stsp
6812 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6813 818c7501 2019-07-11 stsp if (err)
6814 818c7501 2019-07-11 stsp goto done;
6815 818c7501 2019-07-11 stsp
6816 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6817 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6818 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6819 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6820 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6821 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6822 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6823 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
6824 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6825 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6826 55bd499d 2019-07-12 stsp if (err)
6827 1f1abb7e 2019-08-08 stsp goto sync;
6828 55bd499d 2019-07-12 stsp
6829 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6830 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6831 ca355955 2019-07-12 stsp sync:
6832 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6833 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6834 ca355955 2019-07-12 stsp err = sync_err;
6835 818c7501 2019-07-11 stsp done:
6836 818c7501 2019-07-11 stsp got_ref_close(resolved);
6837 a3a2faf2 2019-07-12 stsp free(tree_id);
6838 818c7501 2019-07-11 stsp free(commit_id);
6839 0ebf8283 2019-07-24 stsp if (fileindex)
6840 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6841 0ebf8283 2019-07-24 stsp free(fileindex_path);
6842 0ebf8283 2019-07-24 stsp
6843 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6844 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6845 0ebf8283 2019-07-24 stsp err = unlockerr;
6846 0ebf8283 2019-07-24 stsp return err;
6847 0ebf8283 2019-07-24 stsp }
6848 0ebf8283 2019-07-24 stsp
6849 0ebf8283 2019-07-24 stsp const struct got_error *
6850 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6851 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6852 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6853 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6854 0ebf8283 2019-07-24 stsp {
6855 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6856 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6857 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6858 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6859 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6860 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6861 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6862 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6865 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6866 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6867 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6868 0ebf8283 2019-07-24 stsp
6869 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6870 0ebf8283 2019-07-24 stsp if (err)
6871 0ebf8283 2019-07-24 stsp return err;
6872 0ebf8283 2019-07-24 stsp
6873 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6874 0ebf8283 2019-07-24 stsp if (err)
6875 0ebf8283 2019-07-24 stsp goto done;
6876 0ebf8283 2019-07-24 stsp
6877 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6878 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6879 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6880 0ebf8283 2019-07-24 stsp &ok_arg);
6881 0ebf8283 2019-07-24 stsp if (err)
6882 0ebf8283 2019-07-24 stsp goto done;
6883 0ebf8283 2019-07-24 stsp
6884 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6885 0ebf8283 2019-07-24 stsp if (err)
6886 0ebf8283 2019-07-24 stsp goto done;
6887 0ebf8283 2019-07-24 stsp
6888 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6889 0ebf8283 2019-07-24 stsp if (err)
6890 0ebf8283 2019-07-24 stsp goto done;
6891 0ebf8283 2019-07-24 stsp
6892 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6893 0ebf8283 2019-07-24 stsp worktree);
6894 0ebf8283 2019-07-24 stsp if (err)
6895 0ebf8283 2019-07-24 stsp goto done;
6896 0ebf8283 2019-07-24 stsp
6897 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6898 0ebf8283 2019-07-24 stsp 0);
6899 0ebf8283 2019-07-24 stsp if (err)
6900 0ebf8283 2019-07-24 stsp goto done;
6901 0ebf8283 2019-07-24 stsp
6902 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6903 0ebf8283 2019-07-24 stsp if (err)
6904 0ebf8283 2019-07-24 stsp goto done;
6905 0ebf8283 2019-07-24 stsp
6906 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6907 0ebf8283 2019-07-24 stsp if (err)
6908 0ebf8283 2019-07-24 stsp goto done;
6909 0ebf8283 2019-07-24 stsp
6910 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6911 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6912 0ebf8283 2019-07-24 stsp if (err)
6913 0ebf8283 2019-07-24 stsp goto done;
6914 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6915 0ebf8283 2019-07-24 stsp if (err)
6916 0ebf8283 2019-07-24 stsp goto done;
6917 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6918 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6919 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6920 0ebf8283 2019-07-24 stsp goto done;
6921 0ebf8283 2019-07-24 stsp }
6922 0ebf8283 2019-07-24 stsp
6923 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6924 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6925 0ebf8283 2019-07-24 stsp if (err)
6926 0ebf8283 2019-07-24 stsp goto done;
6927 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6928 0ebf8283 2019-07-24 stsp if (err)
6929 0ebf8283 2019-07-24 stsp goto done;
6930 0ebf8283 2019-07-24 stsp
6931 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6932 0ebf8283 2019-07-24 stsp if (err)
6933 0ebf8283 2019-07-24 stsp goto done;
6934 0ebf8283 2019-07-24 stsp done:
6935 0ebf8283 2019-07-24 stsp free(fileindex_path);
6936 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6937 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6938 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6939 0ebf8283 2019-07-24 stsp if (wt_branch)
6940 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6941 0ebf8283 2019-07-24 stsp if (err) {
6942 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6943 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6944 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6945 0ebf8283 2019-07-24 stsp }
6946 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6947 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6948 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6949 0ebf8283 2019-07-24 stsp }
6950 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6951 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6952 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6953 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6954 3e3a69f1 2019-07-25 stsp }
6955 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6956 0ebf8283 2019-07-24 stsp }
6957 0ebf8283 2019-07-24 stsp return err;
6958 0ebf8283 2019-07-24 stsp }
6959 0ebf8283 2019-07-24 stsp
6960 0ebf8283 2019-07-24 stsp const struct got_error *
6961 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6962 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6963 0ebf8283 2019-07-24 stsp {
6964 3e3a69f1 2019-07-25 stsp if (fileindex)
6965 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6966 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6967 0ebf8283 2019-07-24 stsp }
6968 0ebf8283 2019-07-24 stsp
6969 0ebf8283 2019-07-24 stsp const struct got_error *
6970 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6971 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6972 0ebf8283 2019-07-24 stsp {
6973 0ebf8283 2019-07-24 stsp const struct got_error *err;
6974 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6975 0ebf8283 2019-07-24 stsp
6976 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6977 0ebf8283 2019-07-24 stsp if (err)
6978 0ebf8283 2019-07-24 stsp return err;
6979 0ebf8283 2019-07-24 stsp
6980 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6981 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6982 0ebf8283 2019-07-24 stsp return NULL;
6983 0ebf8283 2019-07-24 stsp }
6984 0ebf8283 2019-07-24 stsp
6985 0ebf8283 2019-07-24 stsp const struct got_error *
6986 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6987 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6988 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6989 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6990 0ebf8283 2019-07-24 stsp {
6991 0ebf8283 2019-07-24 stsp const struct got_error *err;
6992 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6993 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6994 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6995 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6996 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6997 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6998 0ebf8283 2019-07-24 stsp
6999 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7000 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7001 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7002 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7003 0ebf8283 2019-07-24 stsp
7004 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
7005 3e3a69f1 2019-07-25 stsp if (err)
7006 3e3a69f1 2019-07-25 stsp return err;
7007 3e3a69f1 2019-07-25 stsp
7008 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7009 3e3a69f1 2019-07-25 stsp if (err)
7010 f032f1f7 2019-08-04 stsp goto done;
7011 f032f1f7 2019-08-04 stsp
7012 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7013 f032f1f7 2019-08-04 stsp &have_staged_files);
7014 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7015 f032f1f7 2019-08-04 stsp goto done;
7016 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7017 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7018 3e3a69f1 2019-07-25 stsp goto done;
7019 f032f1f7 2019-08-04 stsp }
7020 3e3a69f1 2019-07-25 stsp
7021 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7022 0ebf8283 2019-07-24 stsp if (err)
7023 f032f1f7 2019-08-04 stsp goto done;
7024 0ebf8283 2019-07-24 stsp
7025 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7026 0ebf8283 2019-07-24 stsp if (err)
7027 0ebf8283 2019-07-24 stsp goto done;
7028 0ebf8283 2019-07-24 stsp
7029 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7030 0ebf8283 2019-07-24 stsp if (err)
7031 0ebf8283 2019-07-24 stsp goto done;
7032 0ebf8283 2019-07-24 stsp
7033 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7034 0ebf8283 2019-07-24 stsp worktree);
7035 0ebf8283 2019-07-24 stsp if (err)
7036 0ebf8283 2019-07-24 stsp goto done;
7037 0ebf8283 2019-07-24 stsp
7038 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7039 0ebf8283 2019-07-24 stsp if (err)
7040 0ebf8283 2019-07-24 stsp goto done;
7041 0ebf8283 2019-07-24 stsp
7042 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7043 0ebf8283 2019-07-24 stsp if (err)
7044 0ebf8283 2019-07-24 stsp goto done;
7045 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
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_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7050 0ebf8283 2019-07-24 stsp if (err)
7051 0ebf8283 2019-07-24 stsp goto done;
7052 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7053 0ebf8283 2019-07-24 stsp if (err)
7054 0ebf8283 2019-07-24 stsp goto done;
7055 0ebf8283 2019-07-24 stsp
7056 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7057 0ebf8283 2019-07-24 stsp if (err)
7058 0ebf8283 2019-07-24 stsp goto done;
7059 0ebf8283 2019-07-24 stsp done:
7060 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7061 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7062 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7063 0ebf8283 2019-07-24 stsp if (commit_ref)
7064 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7065 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7066 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7067 0ebf8283 2019-07-24 stsp if (err) {
7068 0ebf8283 2019-07-24 stsp free(*commit_id);
7069 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7070 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7071 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7072 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7073 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7074 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7075 0ebf8283 2019-07-24 stsp }
7076 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7077 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7078 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7079 3e3a69f1 2019-07-25 stsp }
7080 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7081 0ebf8283 2019-07-24 stsp }
7082 0ebf8283 2019-07-24 stsp return err;
7083 0ebf8283 2019-07-24 stsp }
7084 0ebf8283 2019-07-24 stsp
7085 0ebf8283 2019-07-24 stsp static const struct got_error *
7086 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7087 0ebf8283 2019-07-24 stsp {
7088 0ebf8283 2019-07-24 stsp const struct got_error *err;
7089 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7090 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7091 0ebf8283 2019-07-24 stsp
7092 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7093 0ebf8283 2019-07-24 stsp if (err)
7094 0ebf8283 2019-07-24 stsp goto done;
7095 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7096 0ebf8283 2019-07-24 stsp if (err)
7097 0ebf8283 2019-07-24 stsp goto done;
7098 0ebf8283 2019-07-24 stsp
7099 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7100 0ebf8283 2019-07-24 stsp worktree);
7101 0ebf8283 2019-07-24 stsp if (err)
7102 0ebf8283 2019-07-24 stsp goto done;
7103 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7104 0ebf8283 2019-07-24 stsp if (err)
7105 0ebf8283 2019-07-24 stsp goto done;
7106 0ebf8283 2019-07-24 stsp
7107 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7108 0ebf8283 2019-07-24 stsp if (err)
7109 0ebf8283 2019-07-24 stsp goto done;
7110 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7111 0ebf8283 2019-07-24 stsp if (err)
7112 0ebf8283 2019-07-24 stsp goto done;
7113 0ebf8283 2019-07-24 stsp
7114 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7115 0ebf8283 2019-07-24 stsp if (err)
7116 0ebf8283 2019-07-24 stsp goto done;
7117 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7118 0ebf8283 2019-07-24 stsp if (err)
7119 0ebf8283 2019-07-24 stsp goto done;
7120 0ebf8283 2019-07-24 stsp done:
7121 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7122 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7123 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7124 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7125 0ebf8283 2019-07-24 stsp return err;
7126 0ebf8283 2019-07-24 stsp }
7127 0ebf8283 2019-07-24 stsp
7128 0ebf8283 2019-07-24 stsp const struct got_error *
7129 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7130 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7131 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7132 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7133 0ebf8283 2019-07-24 stsp {
7134 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7135 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7136 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7137 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7138 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7139 0ebf8283 2019-07-24 stsp
7140 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7141 0ebf8283 2019-07-24 stsp if (err)
7142 0ebf8283 2019-07-24 stsp return err;
7143 0ebf8283 2019-07-24 stsp
7144 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7145 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7146 0ebf8283 2019-07-24 stsp if (err)
7147 0ebf8283 2019-07-24 stsp goto done;
7148 0ebf8283 2019-07-24 stsp
7149 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7150 0ebf8283 2019-07-24 stsp if (err)
7151 0ebf8283 2019-07-24 stsp goto done;
7152 0ebf8283 2019-07-24 stsp
7153 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7154 0ebf8283 2019-07-24 stsp if (err)
7155 0ebf8283 2019-07-24 stsp goto done;
7156 0ebf8283 2019-07-24 stsp
7157 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7158 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7159 0ebf8283 2019-07-24 stsp if (err)
7160 0ebf8283 2019-07-24 stsp goto done;
7161 0ebf8283 2019-07-24 stsp
7162 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7163 0ebf8283 2019-07-24 stsp if (err)
7164 0ebf8283 2019-07-24 stsp goto done;
7165 0ebf8283 2019-07-24 stsp
7166 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7167 0ebf8283 2019-07-24 stsp if (err)
7168 0ebf8283 2019-07-24 stsp goto done;
7169 0ebf8283 2019-07-24 stsp
7170 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7171 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7172 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7173 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7174 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7175 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7176 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7177 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7178 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7179 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
7180 0ebf8283 2019-07-24 stsp if (err)
7181 1f1abb7e 2019-08-08 stsp goto sync;
7182 0ebf8283 2019-07-24 stsp
7183 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7184 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7185 0ebf8283 2019-07-24 stsp sync:
7186 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7187 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7188 0ebf8283 2019-07-24 stsp err = sync_err;
7189 0ebf8283 2019-07-24 stsp done:
7190 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7191 0ebf8283 2019-07-24 stsp free(tree_id);
7192 818c7501 2019-07-11 stsp free(fileindex_path);
7193 818c7501 2019-07-11 stsp
7194 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7195 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7196 818c7501 2019-07-11 stsp err = unlockerr;
7197 818c7501 2019-07-11 stsp return err;
7198 818c7501 2019-07-11 stsp }
7199 0ebf8283 2019-07-24 stsp
7200 0ebf8283 2019-07-24 stsp const struct got_error *
7201 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7202 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7203 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7204 0ebf8283 2019-07-24 stsp {
7205 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7206 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7207 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7208 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7209 0ebf8283 2019-07-24 stsp
7210 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7211 0ebf8283 2019-07-24 stsp if (err)
7212 0ebf8283 2019-07-24 stsp return err;
7213 0ebf8283 2019-07-24 stsp
7214 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7215 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7216 e600f124 2021-03-21 stsp if (err)
7217 e600f124 2021-03-21 stsp goto done;
7218 e600f124 2021-03-21 stsp
7219 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7220 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7221 0ebf8283 2019-07-24 stsp if (err)
7222 0ebf8283 2019-07-24 stsp goto done;
7223 0ebf8283 2019-07-24 stsp
7224 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7225 0ebf8283 2019-07-24 stsp if (err)
7226 0ebf8283 2019-07-24 stsp goto done;
7227 0ebf8283 2019-07-24 stsp
7228 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7229 0ebf8283 2019-07-24 stsp if (err)
7230 0ebf8283 2019-07-24 stsp goto done;
7231 0ebf8283 2019-07-24 stsp
7232 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7233 0ebf8283 2019-07-24 stsp if (err)
7234 0ebf8283 2019-07-24 stsp goto done;
7235 0ebf8283 2019-07-24 stsp
7236 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7237 a615e0e7 2020-12-16 stsp if (err)
7238 a615e0e7 2020-12-16 stsp goto done;
7239 a615e0e7 2020-12-16 stsp
7240 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7241 a615e0e7 2020-12-16 stsp if (err)
7242 a615e0e7 2020-12-16 stsp goto done;
7243 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7244 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7245 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7246 a615e0e7 2020-12-16 stsp err = sync_err;
7247 0ebf8283 2019-07-24 stsp done:
7248 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7249 a615e0e7 2020-12-16 stsp free(fileindex_path);
7250 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7251 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7252 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7253 0ebf8283 2019-07-24 stsp err = unlockerr;
7254 0ebf8283 2019-07-24 stsp return err;
7255 0ebf8283 2019-07-24 stsp }
7256 0ebf8283 2019-07-24 stsp
7257 0ebf8283 2019-07-24 stsp const struct got_error *
7258 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7259 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7260 0ebf8283 2019-07-24 stsp {
7261 0ebf8283 2019-07-24 stsp const struct got_error *err;
7262 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7263 0ebf8283 2019-07-24 stsp
7264 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7265 0ebf8283 2019-07-24 stsp if (err)
7266 0ebf8283 2019-07-24 stsp return err;
7267 0ebf8283 2019-07-24 stsp
7268 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7269 0ebf8283 2019-07-24 stsp if (err)
7270 0ebf8283 2019-07-24 stsp goto done;
7271 0ebf8283 2019-07-24 stsp
7272 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7273 0ebf8283 2019-07-24 stsp done:
7274 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7275 2822a352 2019-10-15 stsp return err;
7276 2822a352 2019-10-15 stsp }
7277 2822a352 2019-10-15 stsp
7278 2822a352 2019-10-15 stsp const struct got_error *
7279 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7280 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7281 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7282 2822a352 2019-10-15 stsp struct got_repository *repo)
7283 2822a352 2019-10-15 stsp {
7284 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7285 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7286 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7287 2822a352 2019-10-15 stsp
7288 2822a352 2019-10-15 stsp *fileindex = NULL;
7289 2822a352 2019-10-15 stsp *branch_ref = NULL;
7290 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7291 2822a352 2019-10-15 stsp
7292 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7293 2822a352 2019-10-15 stsp if (err)
7294 2822a352 2019-10-15 stsp return err;
7295 2822a352 2019-10-15 stsp
7296 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7297 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7298 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7299 2822a352 2019-10-15 stsp "update -b or different branch name required");
7300 2822a352 2019-10-15 stsp goto done;
7301 2822a352 2019-10-15 stsp }
7302 2822a352 2019-10-15 stsp
7303 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7304 2822a352 2019-10-15 stsp if (err)
7305 2822a352 2019-10-15 stsp goto done;
7306 2822a352 2019-10-15 stsp
7307 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7308 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7309 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7310 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7311 2822a352 2019-10-15 stsp &ok_arg);
7312 2822a352 2019-10-15 stsp if (err)
7313 2822a352 2019-10-15 stsp goto done;
7314 2822a352 2019-10-15 stsp
7315 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7316 2822a352 2019-10-15 stsp if (err)
7317 2822a352 2019-10-15 stsp goto done;
7318 2822a352 2019-10-15 stsp
7319 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7320 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7321 2822a352 2019-10-15 stsp done:
7322 2822a352 2019-10-15 stsp if (err) {
7323 2822a352 2019-10-15 stsp if (*branch_ref) {
7324 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7325 2822a352 2019-10-15 stsp *branch_ref = NULL;
7326 2822a352 2019-10-15 stsp }
7327 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7328 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7329 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7330 2822a352 2019-10-15 stsp }
7331 2822a352 2019-10-15 stsp if (*fileindex) {
7332 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7333 2822a352 2019-10-15 stsp *fileindex = NULL;
7334 2822a352 2019-10-15 stsp }
7335 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7336 2822a352 2019-10-15 stsp }
7337 0cb83759 2019-08-03 stsp return err;
7338 0cb83759 2019-08-03 stsp }
7339 0cb83759 2019-08-03 stsp
7340 2822a352 2019-10-15 stsp const struct got_error *
7341 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7342 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7343 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7344 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7345 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7346 2822a352 2019-10-15 stsp {
7347 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7348 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7349 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7350 2822a352 2019-10-15 stsp
7351 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7352 2822a352 2019-10-15 stsp if (err)
7353 2822a352 2019-10-15 stsp goto done;
7354 2822a352 2019-10-15 stsp
7355 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7356 2822a352 2019-10-15 stsp if (err)
7357 2822a352 2019-10-15 stsp goto done;
7358 2822a352 2019-10-15 stsp
7359 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7360 2822a352 2019-10-15 stsp worktree->path_prefix);
7361 2822a352 2019-10-15 stsp if (err)
7362 2822a352 2019-10-15 stsp goto done;
7363 2822a352 2019-10-15 stsp
7364 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7365 2822a352 2019-10-15 stsp if (err)
7366 2822a352 2019-10-15 stsp goto done;
7367 2822a352 2019-10-15 stsp
7368 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7369 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7370 2822a352 2019-10-15 stsp if (err)
7371 2822a352 2019-10-15 stsp goto sync;
7372 2822a352 2019-10-15 stsp
7373 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7374 2822a352 2019-10-15 stsp if (err)
7375 2822a352 2019-10-15 stsp goto sync;
7376 2822a352 2019-10-15 stsp
7377 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7378 6e210706 2021-01-22 stsp if (err)
7379 6e210706 2021-01-22 stsp goto sync;
7380 6e210706 2021-01-22 stsp
7381 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7382 2822a352 2019-10-15 stsp sync:
7383 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7384 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7385 2822a352 2019-10-15 stsp err = sync_err;
7386 2822a352 2019-10-15 stsp
7387 2822a352 2019-10-15 stsp done:
7388 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7389 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7390 2822a352 2019-10-15 stsp err = unlockerr;
7391 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7392 2822a352 2019-10-15 stsp
7393 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7394 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7395 2822a352 2019-10-15 stsp err = unlockerr;
7396 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7397 2822a352 2019-10-15 stsp
7398 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7399 2822a352 2019-10-15 stsp free(fileindex_path);
7400 2822a352 2019-10-15 stsp free(tree_id);
7401 2822a352 2019-10-15 stsp
7402 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7403 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7404 2822a352 2019-10-15 stsp err = unlockerr;
7405 2822a352 2019-10-15 stsp return err;
7406 2822a352 2019-10-15 stsp }
7407 2822a352 2019-10-15 stsp
7408 2822a352 2019-10-15 stsp const struct got_error *
7409 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7410 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7411 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7412 2822a352 2019-10-15 stsp {
7413 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7414 8b692cd0 2019-10-21 stsp
7415 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7416 8b692cd0 2019-10-21 stsp
7417 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7418 8b692cd0 2019-10-21 stsp
7419 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7420 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7421 8b692cd0 2019-10-21 stsp err = unlockerr;
7422 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7423 8b692cd0 2019-10-21 stsp
7424 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7425 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7426 8b692cd0 2019-10-21 stsp err = unlockerr;
7427 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7428 f259c4c1 2021-09-24 stsp
7429 f259c4c1 2021-09-24 stsp return err;
7430 f259c4c1 2021-09-24 stsp }
7431 f259c4c1 2021-09-24 stsp
7432 f259c4c1 2021-09-24 stsp const struct got_error *
7433 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7434 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7435 f259c4c1 2021-09-24 stsp {
7436 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7437 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7438 f259c4c1 2021-09-24 stsp
7439 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7440 f259c4c1 2021-09-24 stsp if (err)
7441 f259c4c1 2021-09-24 stsp goto done;
7442 8b692cd0 2019-10-21 stsp
7443 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7444 f259c4c1 2021-09-24 stsp
7445 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7446 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7447 f259c4c1 2021-09-24 stsp err = sync_err;
7448 f259c4c1 2021-09-24 stsp done:
7449 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7450 f259c4c1 2021-09-24 stsp free(fileindex_path);
7451 8b692cd0 2019-10-21 stsp return err;
7452 2822a352 2019-10-15 stsp }
7453 2822a352 2019-10-15 stsp
7454 f259c4c1 2021-09-24 stsp static const struct got_error *
7455 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7456 f259c4c1 2021-09-24 stsp {
7457 f259c4c1 2021-09-24 stsp const struct got_error *err;
7458 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7459 f259c4c1 2021-09-24 stsp
7460 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7461 f259c4c1 2021-09-24 stsp if (err)
7462 f259c4c1 2021-09-24 stsp goto done;
7463 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7464 f259c4c1 2021-09-24 stsp if (err)
7465 f259c4c1 2021-09-24 stsp goto done;
7466 f259c4c1 2021-09-24 stsp
7467 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7468 f259c4c1 2021-09-24 stsp if (err)
7469 f259c4c1 2021-09-24 stsp goto done;
7470 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7471 f259c4c1 2021-09-24 stsp if (err)
7472 f259c4c1 2021-09-24 stsp goto done;
7473 f259c4c1 2021-09-24 stsp
7474 f259c4c1 2021-09-24 stsp done:
7475 f259c4c1 2021-09-24 stsp free(branch_refname);
7476 f259c4c1 2021-09-24 stsp free(commit_refname);
7477 f259c4c1 2021-09-24 stsp return err;
7478 f259c4c1 2021-09-24 stsp }
7479 f259c4c1 2021-09-24 stsp
7480 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7481 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7482 f259c4c1 2021-09-24 stsp const char *branch_name;
7483 f259c4c1 2021-09-24 stsp };
7484 f259c4c1 2021-09-24 stsp
7485 f259c4c1 2021-09-24 stsp static const struct got_error *
7486 f259c4c1 2021-09-24 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7487 f259c4c1 2021-09-24 stsp void *arg)
7488 f259c4c1 2021-09-24 stsp {
7489 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7490 f259c4c1 2021-09-24 stsp
7491 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7492 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7493 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7494 f259c4c1 2021-09-24 stsp
7495 f259c4c1 2021-09-24 stsp return NULL;
7496 f259c4c1 2021-09-24 stsp }
7497 f259c4c1 2021-09-24 stsp
7498 f259c4c1 2021-09-24 stsp static const struct got_error *
7499 f259c4c1 2021-09-24 stsp merge_status_cb(void *arg, unsigned char status, unsigned char staged_status,
7500 f259c4c1 2021-09-24 stsp const char *path, struct got_object_id *blob_id,
7501 f259c4c1 2021-09-24 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7502 f259c4c1 2021-09-24 stsp int dirfd, const char *de_name)
7503 f259c4c1 2021-09-24 stsp {
7504 f259c4c1 2021-09-24 stsp return NULL;
7505 f259c4c1 2021-09-24 stsp }
7506 f259c4c1 2021-09-24 stsp
7507 f259c4c1 2021-09-24 stsp const struct got_error *
7508 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7509 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7510 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7511 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7512 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7513 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7514 f259c4c1 2021-09-24 stsp {
7515 f259c4c1 2021-09-24 stsp const struct got_error *err;
7516 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7517 f259c4c1 2021-09-24 stsp
7518 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7519 f259c4c1 2021-09-24 stsp if (err)
7520 f259c4c1 2021-09-24 stsp goto done;
7521 f259c4c1 2021-09-24 stsp
7522 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7523 f259c4c1 2021-09-24 stsp worktree);
7524 f259c4c1 2021-09-24 stsp if (err)
7525 f259c4c1 2021-09-24 stsp goto done;
7526 f259c4c1 2021-09-24 stsp
7527 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7528 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7529 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7530 f259c4c1 2021-09-24 stsp done:
7531 f259c4c1 2021-09-24 stsp free(fileindex_path);
7532 f259c4c1 2021-09-24 stsp return err;
7533 f259c4c1 2021-09-24 stsp }
7534 f259c4c1 2021-09-24 stsp
7535 f259c4c1 2021-09-24 stsp const struct got_error *
7536 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7537 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7538 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7539 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7540 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7541 f259c4c1 2021-09-24 stsp {
7542 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7543 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7544 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7545 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7546 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7547 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7548 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7549 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7550 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7551 f259c4c1 2021-09-24 stsp
7552 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7553 f259c4c1 2021-09-24 stsp
7554 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7555 f259c4c1 2021-09-24 stsp
7556 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7557 f259c4c1 2021-09-24 stsp if (err)
7558 f259c4c1 2021-09-24 stsp goto done;
7559 f259c4c1 2021-09-24 stsp
7560 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7561 f259c4c1 2021-09-24 stsp if (err)
7562 f259c4c1 2021-09-24 stsp goto done;
7563 f259c4c1 2021-09-24 stsp
7564 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7565 f259c4c1 2021-09-24 stsp if (err)
7566 f259c4c1 2021-09-24 stsp goto done;
7567 f259c4c1 2021-09-24 stsp
7568 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7569 f259c4c1 2021-09-24 stsp &have_staged_files);
7570 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7571 f259c4c1 2021-09-24 stsp goto done;
7572 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7573 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7574 f259c4c1 2021-09-24 stsp goto done;
7575 f259c4c1 2021-09-24 stsp }
7576 f259c4c1 2021-09-24 stsp
7577 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7578 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7579 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7580 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7581 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7582 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7583 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7584 f259c4c1 2021-09-24 stsp collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7585 f259c4c1 2021-09-24 stsp if (err)
7586 f259c4c1 2021-09-24 stsp goto done;
7587 f259c4c1 2021-09-24 stsp
7588 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7589 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7590 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7591 f259c4c1 2021-09-24 stsp goto done;
7592 f259c4c1 2021-09-24 stsp }
7593 f259c4c1 2021-09-24 stsp
7594 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7595 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7596 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7597 f259c4c1 2021-09-24 stsp
7598 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7599 f259c4c1 2021-09-24 stsp ct_path++;
7600 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7601 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7602 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7603 f259c4c1 2021-09-24 stsp if (err)
7604 f259c4c1 2021-09-24 stsp goto done;
7605 f259c4c1 2021-09-24 stsp
7606 f259c4c1 2021-09-24 stsp }
7607 f259c4c1 2021-09-24 stsp
7608 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7609 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7610 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7611 f259c4c1 2021-09-24 stsp head_commit_id, branch_tip, worktree, author, committer,
7612 f259c4c1 2021-09-24 stsp merge_commit_msg_cb, &mcm_arg, merge_status_cb, NULL, repo);
7613 f259c4c1 2021-09-24 stsp if (err)
7614 f259c4c1 2021-09-24 stsp goto done;
7615 f259c4c1 2021-09-24 stsp
7616 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7617 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7618 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7619 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7620 f259c4c1 2021-09-24 stsp err = sync_err;
7621 f259c4c1 2021-09-24 stsp done:
7622 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7623 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7624 f259c4c1 2021-09-24 stsp free_commitable(ct);
7625 f259c4c1 2021-09-24 stsp }
7626 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7627 f259c4c1 2021-09-24 stsp free(fileindex_path);
7628 f259c4c1 2021-09-24 stsp return err;
7629 f259c4c1 2021-09-24 stsp }
7630 f259c4c1 2021-09-24 stsp
7631 f259c4c1 2021-09-24 stsp const struct got_error *
7632 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7633 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7634 f259c4c1 2021-09-24 stsp {
7635 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7636 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7637 f259c4c1 2021-09-24 stsp
7638 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7639 f259c4c1 2021-09-24 stsp if (err)
7640 f259c4c1 2021-09-24 stsp goto done;
7641 f259c4c1 2021-09-24 stsp
7642 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7643 f259c4c1 2021-09-24 stsp if (err)
7644 f259c4c1 2021-09-24 stsp goto done;
7645 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7646 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7647 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7648 f259c4c1 2021-09-24 stsp err = sync_err;
7649 f259c4c1 2021-09-24 stsp done:
7650 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7651 f259c4c1 2021-09-24 stsp free(fileindex_path);
7652 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7653 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7654 f259c4c1 2021-09-24 stsp err = unlockerr;
7655 f259c4c1 2021-09-24 stsp return err;
7656 f259c4c1 2021-09-24 stsp }
7657 f259c4c1 2021-09-24 stsp
7658 f259c4c1 2021-09-24 stsp const struct got_error *
7659 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7660 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7661 f259c4c1 2021-09-24 stsp {
7662 f259c4c1 2021-09-24 stsp const struct got_error *err;
7663 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7664 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7665 f259c4c1 2021-09-24 stsp
7666 f259c4c1 2021-09-24 stsp *in_progress = 0;
7667 f259c4c1 2021-09-24 stsp
7668 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7669 f259c4c1 2021-09-24 stsp if (err)
7670 f259c4c1 2021-09-24 stsp return err;
7671 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7672 793fcac3 2021-09-24 stsp free(branch_refname);
7673 f259c4c1 2021-09-24 stsp if (err) {
7674 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7675 f259c4c1 2021-09-24 stsp return err;
7676 f259c4c1 2021-09-24 stsp } else
7677 f259c4c1 2021-09-24 stsp *in_progress = 1;
7678 f259c4c1 2021-09-24 stsp
7679 f259c4c1 2021-09-24 stsp return NULL;
7680 f259c4c1 2021-09-24 stsp }
7681 f259c4c1 2021-09-24 stsp
7682 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
7683 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7684 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
7685 f259c4c1 2021-09-24 stsp {
7686 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
7687 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7688 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7689 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7690 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
7691 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7692 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
7693 f259c4c1 2021-09-24 stsp
7694 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7695 f259c4c1 2021-09-24 stsp
7696 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7697 f259c4c1 2021-09-24 stsp if (err)
7698 f259c4c1 2021-09-24 stsp return err;
7699 f259c4c1 2021-09-24 stsp
7700 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7701 f259c4c1 2021-09-24 stsp if (err)
7702 f259c4c1 2021-09-24 stsp goto done;
7703 f259c4c1 2021-09-24 stsp
7704 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
7705 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
7706 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
7707 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7708 f259c4c1 2021-09-24 stsp &ok_arg);
7709 f259c4c1 2021-09-24 stsp if (err)
7710 f259c4c1 2021-09-24 stsp goto done;
7711 f259c4c1 2021-09-24 stsp
7712 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7713 f259c4c1 2021-09-24 stsp if (err)
7714 f259c4c1 2021-09-24 stsp return err;
7715 f259c4c1 2021-09-24 stsp
7716 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7717 f259c4c1 2021-09-24 stsp if (err)
7718 f259c4c1 2021-09-24 stsp return err;
7719 f259c4c1 2021-09-24 stsp
7720 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7721 f259c4c1 2021-09-24 stsp 0);
7722 f259c4c1 2021-09-24 stsp if (err)
7723 f259c4c1 2021-09-24 stsp goto done;
7724 f259c4c1 2021-09-24 stsp
7725 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7726 f259c4c1 2021-09-24 stsp if (err)
7727 f259c4c1 2021-09-24 stsp goto done;
7728 f259c4c1 2021-09-24 stsp
7729 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7730 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7731 f259c4c1 2021-09-24 stsp goto done;
7732 f259c4c1 2021-09-24 stsp }
7733 f259c4c1 2021-09-24 stsp
7734 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
7735 f259c4c1 2021-09-24 stsp if (err)
7736 f259c4c1 2021-09-24 stsp goto done;
7737 f259c4c1 2021-09-24 stsp
7738 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7739 f259c4c1 2021-09-24 stsp if (err)
7740 f259c4c1 2021-09-24 stsp goto done;
7741 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
7742 f259c4c1 2021-09-24 stsp if (err)
7743 f259c4c1 2021-09-24 stsp goto done;
7744 f259c4c1 2021-09-24 stsp
7745 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7746 f259c4c1 2021-09-24 stsp if (err)
7747 f259c4c1 2021-09-24 stsp goto done;
7748 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
7749 f259c4c1 2021-09-24 stsp if (err)
7750 f259c4c1 2021-09-24 stsp goto done;
7751 f259c4c1 2021-09-24 stsp
7752 f259c4c1 2021-09-24 stsp done:
7753 f259c4c1 2021-09-24 stsp free(branch_refname);
7754 f259c4c1 2021-09-24 stsp free(commit_refname);
7755 f259c4c1 2021-09-24 stsp free(fileindex_path);
7756 f259c4c1 2021-09-24 stsp if (branch_ref)
7757 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7758 f259c4c1 2021-09-24 stsp if (commit_ref)
7759 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7760 f259c4c1 2021-09-24 stsp if (wt_branch)
7761 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
7762 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
7763 f259c4c1 2021-09-24 stsp if (err) {
7764 f259c4c1 2021-09-24 stsp if (*fileindex) {
7765 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7766 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7767 f259c4c1 2021-09-24 stsp }
7768 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7769 f259c4c1 2021-09-24 stsp }
7770 f259c4c1 2021-09-24 stsp return err;
7771 f259c4c1 2021-09-24 stsp }
7772 f259c4c1 2021-09-24 stsp
7773 f259c4c1 2021-09-24 stsp const struct got_error *
7774 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
7775 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7776 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7777 f259c4c1 2021-09-24 stsp {
7778 f259c4c1 2021-09-24 stsp const struct got_error *err;
7779 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
7780 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7781 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7782 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7783 f259c4c1 2021-09-24 stsp
7784 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7785 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7786 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7787 f259c4c1 2021-09-24 stsp
7788 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7789 f259c4c1 2021-09-24 stsp if (err)
7790 f259c4c1 2021-09-24 stsp return err;
7791 f259c4c1 2021-09-24 stsp
7792 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7793 f259c4c1 2021-09-24 stsp if (err)
7794 f259c4c1 2021-09-24 stsp goto done;
7795 f259c4c1 2021-09-24 stsp
7796 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7797 f259c4c1 2021-09-24 stsp &have_staged_files);
7798 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7799 f259c4c1 2021-09-24 stsp goto done;
7800 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7801 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7802 f259c4c1 2021-09-24 stsp goto done;
7803 f259c4c1 2021-09-24 stsp }
7804 f259c4c1 2021-09-24 stsp
7805 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7806 f259c4c1 2021-09-24 stsp if (err)
7807 f259c4c1 2021-09-24 stsp goto done;
7808 f259c4c1 2021-09-24 stsp
7809 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7810 f259c4c1 2021-09-24 stsp if (err)
7811 f259c4c1 2021-09-24 stsp goto done;
7812 f259c4c1 2021-09-24 stsp
7813 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7814 f259c4c1 2021-09-24 stsp if (err)
7815 f259c4c1 2021-09-24 stsp goto done;
7816 f259c4c1 2021-09-24 stsp
7817 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
7818 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7819 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
7820 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
7821 f259c4c1 2021-09-24 stsp goto done;
7822 f259c4c1 2021-09-24 stsp }
7823 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7824 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
7825 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
7826 f259c4c1 2021-09-24 stsp goto done;
7827 f259c4c1 2021-09-24 stsp }
7828 f259c4c1 2021-09-24 stsp
7829 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7830 f259c4c1 2021-09-24 stsp if (err)
7831 f259c4c1 2021-09-24 stsp goto done;
7832 f259c4c1 2021-09-24 stsp
7833 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
7834 f259c4c1 2021-09-24 stsp if (err)
7835 f259c4c1 2021-09-24 stsp goto done;
7836 f259c4c1 2021-09-24 stsp done:
7837 f259c4c1 2021-09-24 stsp free(commit_refname);
7838 f259c4c1 2021-09-24 stsp free(branch_refname);
7839 f259c4c1 2021-09-24 stsp free(fileindex_path);
7840 f259c4c1 2021-09-24 stsp if (commit_ref)
7841 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7842 f259c4c1 2021-09-24 stsp if (branch_ref)
7843 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7844 f259c4c1 2021-09-24 stsp if (err) {
7845 f259c4c1 2021-09-24 stsp if (*branch_name) {
7846 f259c4c1 2021-09-24 stsp free(*branch_name);
7847 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7848 f259c4c1 2021-09-24 stsp }
7849 f259c4c1 2021-09-24 stsp free(*branch_tip);
7850 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7851 f259c4c1 2021-09-24 stsp if (*fileindex) {
7852 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7853 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7854 f259c4c1 2021-09-24 stsp }
7855 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7856 f259c4c1 2021-09-24 stsp }
7857 f259c4c1 2021-09-24 stsp return err;
7858 f259c4c1 2021-09-24 stsp }
7859 f259c4c1 2021-09-24 stsp
7860 f259c4c1 2021-09-24 stsp const struct got_error *
7861 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
7862 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7863 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7864 f259c4c1 2021-09-24 stsp {
7865 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7866 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
7867 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7868 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
7869 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
7870 f259c4c1 2021-09-24 stsp
7871 f259c4c1 2021-09-24 stsp err = got_object_id_by_path(&tree_id, repo,
7872 f259c4c1 2021-09-24 stsp worktree->base_commit_id, worktree->path_prefix);
7873 f259c4c1 2021-09-24 stsp if (err)
7874 f259c4c1 2021-09-24 stsp goto done;
7875 f259c4c1 2021-09-24 stsp
7876 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7877 f259c4c1 2021-09-24 stsp if (err)
7878 f259c4c1 2021-09-24 stsp goto done;
7879 f259c4c1 2021-09-24 stsp
7880 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7881 f259c4c1 2021-09-24 stsp if (err)
7882 f259c4c1 2021-09-24 stsp goto done;
7883 f259c4c1 2021-09-24 stsp
7884 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
7885 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
7886 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
7887 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
7888 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
7889 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
7890 f259c4c1 2021-09-24 stsp rfa.repo = repo;
7891 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
7892 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7893 f259c4c1 2021-09-24 stsp revert_file, &rfa, NULL, NULL, 0, 0);
7894 f259c4c1 2021-09-24 stsp if (err)
7895 f259c4c1 2021-09-24 stsp goto sync;
7896 f259c4c1 2021-09-24 stsp
7897 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7898 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7899 f259c4c1 2021-09-24 stsp sync:
7900 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7901 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7902 f259c4c1 2021-09-24 stsp err = sync_err;
7903 f259c4c1 2021-09-24 stsp done:
7904 f259c4c1 2021-09-24 stsp free(tree_id);
7905 f259c4c1 2021-09-24 stsp free(commit_id);
7906 f259c4c1 2021-09-24 stsp if (fileindex)
7907 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7908 f259c4c1 2021-09-24 stsp free(fileindex_path);
7909 f259c4c1 2021-09-24 stsp
7910 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7911 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7912 f259c4c1 2021-09-24 stsp err = unlockerr;
7913 f259c4c1 2021-09-24 stsp return err;
7914 f259c4c1 2021-09-24 stsp }
7915 f259c4c1 2021-09-24 stsp
7916 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7917 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7918 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7919 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7920 2db2652d 2019-08-07 stsp struct got_repository *repo;
7921 2db2652d 2019-08-07 stsp int have_changes;
7922 2db2652d 2019-08-07 stsp };
7923 2db2652d 2019-08-07 stsp
7924 2db2652d 2019-08-07 stsp const struct got_error *
7925 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7926 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7927 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7928 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7929 735ef5ac 2019-08-03 stsp {
7930 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7931 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7932 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7933 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7934 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7935 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7936 8b13ce36 2019-08-08 stsp
7937 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7938 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7939 8b13ce36 2019-08-08 stsp return NULL;
7940 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7941 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7942 735ef5ac 2019-08-03 stsp
7943 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7944 735ef5ac 2019-08-03 stsp if (ie == NULL)
7945 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7946 735ef5ac 2019-08-03 stsp
7947 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7948 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7949 735ef5ac 2019-08-03 stsp relpath) == -1)
7950 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7951 735ef5ac 2019-08-03 stsp
7952 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7953 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7954 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7955 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7956 735ef5ac 2019-08-03 stsp }
7957 735ef5ac 2019-08-03 stsp
7958 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7959 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7960 3aa5969e 2019-08-06 stsp goto done;
7961 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7962 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7963 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7964 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7965 735ef5ac 2019-08-03 stsp goto done;
7966 3aa5969e 2019-08-06 stsp }
7967 735ef5ac 2019-08-03 stsp
7968 2db2652d 2019-08-07 stsp a->have_changes = 1;
7969 2db2652d 2019-08-07 stsp
7970 735ef5ac 2019-08-03 stsp p = in_repo_path;
7971 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7972 735ef5ac 2019-08-03 stsp p++;
7973 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7974 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7975 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7976 735ef5ac 2019-08-03 stsp done:
7977 735ef5ac 2019-08-03 stsp free(in_repo_path);
7978 dc424a06 2019-08-07 stsp return err;
7979 dc424a06 2019-08-07 stsp }
7980 dc424a06 2019-08-07 stsp
7981 2db2652d 2019-08-07 stsp struct stage_path_arg {
7982 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7983 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7984 2db2652d 2019-08-07 stsp struct got_repository *repo;
7985 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7986 2db2652d 2019-08-07 stsp void *status_arg;
7987 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7988 2db2652d 2019-08-07 stsp void *patch_arg;
7989 7b5dc508 2019-10-28 stsp int staged_something;
7990 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7991 2db2652d 2019-08-07 stsp };
7992 2db2652d 2019-08-07 stsp
7993 2db2652d 2019-08-07 stsp static const struct got_error *
7994 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7995 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7996 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7997 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7998 0cb83759 2019-08-03 stsp {
7999 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
8000 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
8001 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
8002 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
8003 0cb83759 2019-08-03 stsp uint32_t stage;
8004 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
8005 0aeb8099 2020-07-23 stsp struct stat sb;
8006 8b13ce36 2019-08-08 stsp
8007 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
8008 8b13ce36 2019-08-08 stsp return NULL;
8009 0cb83759 2019-08-03 stsp
8010 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8011 d3e7c587 2019-08-03 stsp if (ie == NULL)
8012 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8013 0cb83759 2019-08-03 stsp
8014 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8015 2db2652d 2019-08-07 stsp relpath)== -1)
8016 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8017 0cb83759 2019-08-03 stsp
8018 0cb83759 2019-08-03 stsp switch (status) {
8019 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8020 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8021 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8022 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8023 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8024 0aeb8099 2020-07-23 stsp break;
8025 0aeb8099 2020-07-23 stsp }
8026 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8027 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8028 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8029 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8030 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8031 dc424a06 2019-08-07 stsp if (err)
8032 dc424a06 2019-08-07 stsp break;
8033 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8034 dc424a06 2019-08-07 stsp break;
8035 dc424a06 2019-08-07 stsp } else {
8036 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8037 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8038 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8039 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8040 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8041 dc424a06 2019-08-07 stsp break;
8042 dc424a06 2019-08-07 stsp }
8043 dc424a06 2019-08-07 stsp }
8044 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8045 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8046 0cb83759 2019-08-03 stsp if (err)
8047 dc424a06 2019-08-07 stsp break;
8048 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8049 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8050 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8051 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8052 0cb83759 2019-08-03 stsp else
8053 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8054 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8055 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8056 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8057 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8058 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8059 35213c7c 2020-07-23 stsp ssize_t target_len;
8060 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8061 35213c7c 2020-07-23 stsp sizeof(target_path));
8062 35213c7c 2020-07-23 stsp if (target_len == -1) {
8063 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8064 35213c7c 2020-07-23 stsp ondisk_path);
8065 35213c7c 2020-07-23 stsp break;
8066 35213c7c 2020-07-23 stsp }
8067 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8068 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8069 35213c7c 2020-07-23 stsp a->worktree->root_path);
8070 35213c7c 2020-07-23 stsp if (err)
8071 35213c7c 2020-07-23 stsp break;
8072 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8073 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8074 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8075 35213c7c 2020-07-23 stsp break;
8076 35213c7c 2020-07-23 stsp }
8077 35213c7c 2020-07-23 stsp }
8078 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8079 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8080 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8081 35213c7c 2020-07-23 stsp else
8082 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8083 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8084 0aeb8099 2020-07-23 stsp } else {
8085 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8086 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8087 0aeb8099 2020-07-23 stsp }
8088 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8089 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8090 dc424a06 2019-08-07 stsp break;
8091 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8092 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8093 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8094 0cb83759 2019-08-03 stsp break;
8095 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8096 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8097 d3e7c587 2019-08-03 stsp break;
8098 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8099 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8100 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8101 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8102 dc424a06 2019-08-07 stsp if (err)
8103 dc424a06 2019-08-07 stsp break;
8104 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8105 88f33a19 2019-08-08 stsp break;
8106 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8107 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8108 dc424a06 2019-08-07 stsp break;
8109 88f33a19 2019-08-08 stsp }
8110 dc424a06 2019-08-07 stsp }
8111 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8112 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8113 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8114 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8115 dc424a06 2019-08-07 stsp break;
8116 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8117 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8118 12463d8b 2019-12-13 stsp de_name);
8119 0cb83759 2019-08-03 stsp break;
8120 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8121 d3e7c587 2019-08-03 stsp break;
8122 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8123 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8124 ebf48fd5 2019-08-03 stsp break;
8125 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8126 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8127 2a06fe5f 2019-08-24 stsp break;
8128 0cb83759 2019-08-03 stsp default:
8129 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8130 537ac44b 2019-08-03 stsp break;
8131 0cb83759 2019-08-03 stsp }
8132 dc424a06 2019-08-07 stsp
8133 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8134 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8135 dc424a06 2019-08-07 stsp free(path_content);
8136 2db2652d 2019-08-07 stsp free(ondisk_path);
8137 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8138 0ebf8283 2019-07-24 stsp return err;
8139 0ebf8283 2019-07-24 stsp }
8140 0cb83759 2019-08-03 stsp
8141 0cb83759 2019-08-03 stsp const struct got_error *
8142 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8143 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8144 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8145 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8146 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8147 0cb83759 2019-08-03 stsp {
8148 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8149 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8150 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8151 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8152 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8153 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8154 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8155 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8156 0cb83759 2019-08-03 stsp
8157 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8158 0cb83759 2019-08-03 stsp if (err)
8159 0cb83759 2019-08-03 stsp return err;
8160 0cb83759 2019-08-03 stsp
8161 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8162 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8163 735ef5ac 2019-08-03 stsp if (err)
8164 735ef5ac 2019-08-03 stsp goto done;
8165 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8166 735ef5ac 2019-08-03 stsp if (err)
8167 735ef5ac 2019-08-03 stsp goto done;
8168 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8169 0cb83759 2019-08-03 stsp if (err)
8170 0cb83759 2019-08-03 stsp goto done;
8171 0cb83759 2019-08-03 stsp
8172 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8173 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8174 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8175 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8176 2db2652d 2019-08-07 stsp oka.repo = repo;
8177 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8178 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8179 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8180 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
8181 735ef5ac 2019-08-03 stsp if (err)
8182 735ef5ac 2019-08-03 stsp goto done;
8183 735ef5ac 2019-08-03 stsp }
8184 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8185 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8186 2db2652d 2019-08-07 stsp goto done;
8187 2db2652d 2019-08-07 stsp }
8188 735ef5ac 2019-08-03 stsp
8189 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8190 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8191 2db2652d 2019-08-07 stsp spa.repo = repo;
8192 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8193 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8194 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8195 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8196 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8197 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8198 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8199 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8200 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
8201 42005733 2019-08-03 stsp if (err)
8202 2db2652d 2019-08-07 stsp goto done;
8203 7b5dc508 2019-10-28 stsp }
8204 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8205 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8206 7b5dc508 2019-10-28 stsp goto done;
8207 0cb83759 2019-08-03 stsp }
8208 0cb83759 2019-08-03 stsp
8209 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8210 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8211 0cb83759 2019-08-03 stsp err = sync_err;
8212 0cb83759 2019-08-03 stsp done:
8213 735ef5ac 2019-08-03 stsp if (head_ref)
8214 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8215 735ef5ac 2019-08-03 stsp free(head_commit_id);
8216 0cb83759 2019-08-03 stsp free(fileindex_path);
8217 0cb83759 2019-08-03 stsp if (fileindex)
8218 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8219 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8220 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8221 0cb83759 2019-08-03 stsp err = unlockerr;
8222 0cb83759 2019-08-03 stsp return err;
8223 0cb83759 2019-08-03 stsp }
8224 ad493afc 2019-08-03 stsp
8225 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8226 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8227 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8228 ad493afc 2019-08-03 stsp struct got_repository *repo;
8229 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8230 ad493afc 2019-08-03 stsp void *progress_arg;
8231 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8232 2e1f37b0 2019-08-08 stsp void *patch_arg;
8233 ad493afc 2019-08-03 stsp };
8234 2e1f37b0 2019-08-08 stsp
8235 2e1f37b0 2019-08-08 stsp static const struct got_error *
8236 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8237 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8238 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8239 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8240 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8241 2e1f37b0 2019-08-08 stsp {
8242 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8243 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8244 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8245 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8246 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8247 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8248 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8249 2e1f37b0 2019-08-08 stsp
8250 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8251 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8252 2e1f37b0 2019-08-08 stsp
8253 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8254 2e1f37b0 2019-08-08 stsp if (err)
8255 2e1f37b0 2019-08-08 stsp return err;
8256 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8257 2e1f37b0 2019-08-08 stsp if (err)
8258 2e1f37b0 2019-08-08 stsp goto done;
8259 2e1f37b0 2019-08-08 stsp
8260 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8261 2e1f37b0 2019-08-08 stsp if (err)
8262 2e1f37b0 2019-08-08 stsp goto done;
8263 2e1f37b0 2019-08-08 stsp
8264 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8265 2e1f37b0 2019-08-08 stsp if (err)
8266 2e1f37b0 2019-08-08 stsp goto done;
8267 2e1f37b0 2019-08-08 stsp
8268 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
8269 2e1f37b0 2019-08-08 stsp if (err)
8270 2e1f37b0 2019-08-08 stsp goto done;
8271 2e1f37b0 2019-08-08 stsp
8272 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
8273 2e1f37b0 2019-08-08 stsp if (err)
8274 2e1f37b0 2019-08-08 stsp goto done;
8275 ad493afc 2019-08-03 stsp
8276 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8277 2e1f37b0 2019-08-08 stsp if (err)
8278 2e1f37b0 2019-08-08 stsp goto done;
8279 2e1f37b0 2019-08-08 stsp
8280 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8281 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8282 2e1f37b0 2019-08-08 stsp if (err)
8283 2e1f37b0 2019-08-08 stsp goto done;
8284 2e1f37b0 2019-08-08 stsp
8285 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8286 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8287 2e1f37b0 2019-08-08 stsp if (err)
8288 2e1f37b0 2019-08-08 stsp goto done;
8289 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8290 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8291 2e1f37b0 2019-08-08 stsp if (err)
8292 2e1f37b0 2019-08-08 stsp goto done;
8293 2e1f37b0 2019-08-08 stsp
8294 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8295 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8296 2e1f37b0 2019-08-08 stsp goto done;
8297 2e1f37b0 2019-08-08 stsp }
8298 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8299 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8300 2e1f37b0 2019-08-08 stsp goto done;
8301 2e1f37b0 2019-08-08 stsp }
8302 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8303 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8304 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8305 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8306 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8307 fe621944 2020-11-10 stsp nchanges++;
8308 fe621944 2020-11-10 stsp }
8309 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8310 2e1f37b0 2019-08-08 stsp int choice;
8311 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8312 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8313 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8314 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8315 2e1f37b0 2019-08-08 stsp if (err)
8316 2e1f37b0 2019-08-08 stsp goto done;
8317 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8318 2e1f37b0 2019-08-08 stsp have_content = 1;
8319 19e4b907 2019-08-08 stsp else
8320 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8321 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8322 2e1f37b0 2019-08-08 stsp break;
8323 2e1f37b0 2019-08-08 stsp }
8324 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8325 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8326 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8327 2e1f37b0 2019-08-08 stsp done:
8328 2e1f37b0 2019-08-08 stsp free(label1);
8329 2e1f37b0 2019-08-08 stsp if (blob)
8330 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8331 2e1f37b0 2019-08-08 stsp if (staged_blob)
8332 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8333 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8334 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8335 fe621944 2020-11-10 stsp err = free_err;
8336 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8337 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8338 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8339 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8340 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8341 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8342 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8343 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8344 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8345 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8346 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8347 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8348 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8349 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8350 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8351 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8352 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8353 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8354 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8355 2e1f37b0 2019-08-08 stsp }
8356 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8357 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8358 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8359 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8360 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8361 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8362 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8363 2e1f37b0 2019-08-08 stsp }
8364 2e1f37b0 2019-08-08 stsp free(path1);
8365 2e1f37b0 2019-08-08 stsp free(path2);
8366 fda8017d 2020-07-23 stsp return err;
8367 fda8017d 2020-07-23 stsp }
8368 fda8017d 2020-07-23 stsp
8369 fda8017d 2020-07-23 stsp static const struct got_error *
8370 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8371 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8372 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8373 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8374 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8375 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8376 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8377 fda8017d 2020-07-23 stsp {
8378 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8379 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8380 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8381 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8382 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8383 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8384 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8385 fda8017d 2020-07-23 stsp struct stat sb;
8386 fda8017d 2020-07-23 stsp
8387 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8388 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8389 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8390 fda8017d 2020-07-23 stsp if (err)
8391 fda8017d 2020-07-23 stsp return err;
8392 fda8017d 2020-07-23 stsp
8393 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8394 fda8017d 2020-07-23 stsp return NULL;
8395 fda8017d 2020-07-23 stsp
8396 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8397 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8398 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8399 fda8017d 2020-07-23 stsp if (err)
8400 fda8017d 2020-07-23 stsp goto done;
8401 fda8017d 2020-07-23 stsp }
8402 fda8017d 2020-07-23 stsp
8403 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
8404 fda8017d 2020-07-23 stsp if (f == NULL) {
8405 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8406 fda8017d 2020-07-23 stsp path_unstaged_content);
8407 fda8017d 2020-07-23 stsp goto done;
8408 fda8017d 2020-07-23 stsp }
8409 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8410 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8411 fda8017d 2020-07-23 stsp goto done;
8412 fda8017d 2020-07-23 stsp }
8413 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8414 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8415 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8416 fda8017d 2020-07-23 stsp size_t r;
8417 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8418 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8419 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8420 fda8017d 2020-07-23 stsp goto done;
8421 fda8017d 2020-07-23 stsp }
8422 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8423 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8424 fda8017d 2020-07-23 stsp goto done;
8425 fda8017d 2020-07-23 stsp }
8426 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8427 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8428 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8429 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8430 fda8017d 2020-07-23 stsp progress_arg);
8431 fda8017d 2020-07-23 stsp } else {
8432 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8433 67a66647 2021-05-31 stsp
8434 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8435 67a66647 2021-05-31 stsp if (err)
8436 67a66647 2021-05-31 stsp return err;
8437 67a66647 2021-05-31 stsp
8438 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8439 67a66647 2021-05-31 stsp parent) == -1) {
8440 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8441 67a66647 2021-05-31 stsp base_path = NULL;
8442 67a66647 2021-05-31 stsp goto done;
8443 67a66647 2021-05-31 stsp }
8444 67a66647 2021-05-31 stsp
8445 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8446 67a66647 2021-05-31 stsp if (err)
8447 67a66647 2021-05-31 stsp goto done;
8448 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8449 67a66647 2021-05-31 stsp blob_base);
8450 67a66647 2021-05-31 stsp if (err)
8451 67a66647 2021-05-31 stsp goto done;
8452 67a66647 2021-05-31 stsp
8453 eec2f5a9 2021-06-03 stsp /*
8454 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8455 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8456 eec2f5a9 2021-06-03 stsp */
8457 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8458 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8459 eec2f5a9 2021-06-03 stsp ondisk_path);
8460 eec2f5a9 2021-06-03 stsp if (err)
8461 eec2f5a9 2021-06-03 stsp goto done;
8462 eec2f5a9 2021-06-03 stsp } else {
8463 eec2f5a9 2021-06-03 stsp int fd;
8464 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8465 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8466 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8467 eec2f5a9 2021-06-03 stsp goto done;
8468 eec2f5a9 2021-06-03 stsp }
8469 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8470 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8471 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8472 eec2f5a9 2021-06-03 stsp close(fd);
8473 eec2f5a9 2021-06-03 stsp goto done;
8474 eec2f5a9 2021-06-03 stsp }
8475 eec2f5a9 2021-06-03 stsp }
8476 eec2f5a9 2021-06-03 stsp
8477 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8478 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8479 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8480 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8481 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8482 fda8017d 2020-07-23 stsp }
8483 fda8017d 2020-07-23 stsp if (err)
8484 fda8017d 2020-07-23 stsp goto done;
8485 fda8017d 2020-07-23 stsp
8486 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8487 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8488 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8489 2ac8aa02 2020-11-09 stsp } else {
8490 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8491 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8492 2ac8aa02 2020-11-09 stsp }
8493 fda8017d 2020-07-23 stsp done:
8494 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8495 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8496 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8497 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8498 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8499 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8500 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8501 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8502 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8503 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8504 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8505 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8506 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8507 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8508 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8509 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8510 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8511 67a66647 2021-05-31 stsp free(blob_base_path);
8512 67a66647 2021-05-31 stsp free(parent);
8513 67a66647 2021-05-31 stsp free(base_path);
8514 2e1f37b0 2019-08-08 stsp return err;
8515 2e1f37b0 2019-08-08 stsp }
8516 2e1f37b0 2019-08-08 stsp
8517 ad493afc 2019-08-03 stsp static const struct got_error *
8518 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8519 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8520 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8521 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8522 ad493afc 2019-08-03 stsp {
8523 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8524 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8525 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8526 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8527 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8528 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8529 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8530 9bc94a15 2019-08-03 stsp struct stat sb;
8531 ad493afc 2019-08-03 stsp
8532 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8533 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8534 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8535 2e1f37b0 2019-08-08 stsp return NULL;
8536 2e1f37b0 2019-08-08 stsp
8537 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8538 ad493afc 2019-08-03 stsp if (ie == NULL)
8539 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8540 9bc94a15 2019-08-03 stsp
8541 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8542 9bc94a15 2019-08-03 stsp == -1)
8543 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8544 ad493afc 2019-08-03 stsp
8545 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8546 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8547 f69721c3 2019-10-21 stsp if (err)
8548 f69721c3 2019-10-21 stsp goto done;
8549 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8550 f69721c3 2019-10-21 stsp id_str) == -1) {
8551 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8552 f69721c3 2019-10-21 stsp goto done;
8553 f69721c3 2019-10-21 stsp }
8554 f69721c3 2019-10-21 stsp
8555 ad493afc 2019-08-03 stsp switch (staged_status) {
8556 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8557 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8558 ad493afc 2019-08-03 stsp blob_id, 8192);
8559 ad493afc 2019-08-03 stsp if (err)
8560 ad493afc 2019-08-03 stsp break;
8561 ad493afc 2019-08-03 stsp /* fall through */
8562 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8563 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8564 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8565 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8566 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8567 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8568 2e1f37b0 2019-08-08 stsp if (err)
8569 2e1f37b0 2019-08-08 stsp break;
8570 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8571 2e1f37b0 2019-08-08 stsp break;
8572 2e1f37b0 2019-08-08 stsp } else {
8573 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8574 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8575 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8576 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8577 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8578 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8579 2e1f37b0 2019-08-08 stsp }
8580 2e1f37b0 2019-08-08 stsp }
8581 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8582 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8583 ad493afc 2019-08-03 stsp if (err)
8584 ad493afc 2019-08-03 stsp break;
8585 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8586 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8587 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8588 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8589 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8590 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8591 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8592 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8593 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8594 ea7786be 2020-07-23 stsp break;
8595 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8596 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8597 36bf999c 2020-07-23 stsp char *staged_target;
8598 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8599 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8600 36bf999c 2020-07-23 stsp if (err)
8601 36bf999c 2020-07-23 stsp goto done;
8602 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8603 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8604 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8605 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8606 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8607 36bf999c 2020-07-23 stsp free(staged_target);
8608 dfe9fba0 2020-07-23 stsp } else {
8609 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8610 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8611 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8612 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8613 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8614 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8615 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8616 dfe9fba0 2020-07-23 stsp }
8617 ea7786be 2020-07-23 stsp break;
8618 ea7786be 2020-07-23 stsp default:
8619 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8620 ea7786be 2020-07-23 stsp break;
8621 ea7786be 2020-07-23 stsp }
8622 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8623 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8624 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8625 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8626 2ac8aa02 2020-11-09 stsp }
8627 ad493afc 2019-08-03 stsp break;
8628 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8629 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8630 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8631 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8632 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8633 2e1f37b0 2019-08-08 stsp if (err)
8634 2e1f37b0 2019-08-08 stsp break;
8635 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8636 2e1f37b0 2019-08-08 stsp break;
8637 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8638 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8639 2e1f37b0 2019-08-08 stsp break;
8640 2e1f37b0 2019-08-08 stsp }
8641 2e1f37b0 2019-08-08 stsp }
8642 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8643 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8644 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8645 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8646 9bc94a15 2019-08-03 stsp if (err)
8647 9bc94a15 2019-08-03 stsp break;
8648 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8649 ad493afc 2019-08-03 stsp break;
8650 ad493afc 2019-08-03 stsp }
8651 f69721c3 2019-10-21 stsp done:
8652 ad493afc 2019-08-03 stsp free(ondisk_path);
8653 ad493afc 2019-08-03 stsp if (blob_base)
8654 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8655 ad493afc 2019-08-03 stsp if (blob_staged)
8656 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8657 f69721c3 2019-10-21 stsp free(id_str);
8658 f69721c3 2019-10-21 stsp free(label_orig);
8659 ad493afc 2019-08-03 stsp return err;
8660 ad493afc 2019-08-03 stsp }
8661 ad493afc 2019-08-03 stsp
8662 ad493afc 2019-08-03 stsp const struct got_error *
8663 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8664 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8665 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8666 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8667 ad493afc 2019-08-03 stsp struct got_repository *repo)
8668 ad493afc 2019-08-03 stsp {
8669 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8670 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8671 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8672 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8673 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8674 ad493afc 2019-08-03 stsp
8675 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8676 ad493afc 2019-08-03 stsp if (err)
8677 ad493afc 2019-08-03 stsp return err;
8678 ad493afc 2019-08-03 stsp
8679 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8680 ad493afc 2019-08-03 stsp if (err)
8681 ad493afc 2019-08-03 stsp goto done;
8682 ad493afc 2019-08-03 stsp
8683 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8684 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8685 ad493afc 2019-08-03 stsp upa.repo = repo;
8686 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8687 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8688 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8689 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8690 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8691 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8692 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
8693 ad493afc 2019-08-03 stsp if (err)
8694 ad493afc 2019-08-03 stsp goto done;
8695 ad493afc 2019-08-03 stsp }
8696 ad493afc 2019-08-03 stsp
8697 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8698 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8699 ad493afc 2019-08-03 stsp err = sync_err;
8700 ad493afc 2019-08-03 stsp done:
8701 ad493afc 2019-08-03 stsp free(fileindex_path);
8702 ad493afc 2019-08-03 stsp if (fileindex)
8703 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8704 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8705 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8706 ad493afc 2019-08-03 stsp err = unlockerr;
8707 ad493afc 2019-08-03 stsp return err;
8708 ad493afc 2019-08-03 stsp }
8709 b2118c49 2020-07-28 stsp
8710 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8711 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8712 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8713 b2118c49 2020-07-28 stsp void *info_arg;
8714 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8715 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8716 b2118c49 2020-07-28 stsp void *cancel_arg;
8717 b2118c49 2020-07-28 stsp };
8718 b2118c49 2020-07-28 stsp
8719 b2118c49 2020-07-28 stsp static const struct got_error *
8720 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8721 b2118c49 2020-07-28 stsp {
8722 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8723 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8724 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8725 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8726 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8727 b2118c49 2020-07-28 stsp int stage;
8728 b2118c49 2020-07-28 stsp
8729 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8730 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8731 b2118c49 2020-07-28 stsp
8732 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8733 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8734 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8735 b2118c49 2020-07-28 stsp break;
8736 b2118c49 2020-07-28 stsp }
8737 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8738 b2118c49 2020-07-28 stsp return NULL;
8739 b2118c49 2020-07-28 stsp
8740 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8741 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8742 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8743 b2118c49 2020-07-28 stsp }
8744 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8745 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8746 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8747 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8748 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8749 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8750 b2118c49 2020-07-28 stsp }
8751 b2118c49 2020-07-28 stsp
8752 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8753 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8754 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8755 b2118c49 2020-07-28 stsp }
8756 b2118c49 2020-07-28 stsp
8757 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8758 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8759 b2118c49 2020-07-28 stsp }
8760 b2118c49 2020-07-28 stsp
8761 b2118c49 2020-07-28 stsp const struct got_error *
8762 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8763 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8764 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8765 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8766 b2118c49 2020-07-28 stsp
8767 b2118c49 2020-07-28 stsp {
8768 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8769 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8770 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8771 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8772 b2118c49 2020-07-28 stsp
8773 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8774 b2118c49 2020-07-28 stsp if (err)
8775 b2118c49 2020-07-28 stsp return err;
8776 b2118c49 2020-07-28 stsp
8777 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8778 b2118c49 2020-07-28 stsp if (err)
8779 b2118c49 2020-07-28 stsp goto done;
8780 b2118c49 2020-07-28 stsp
8781 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8782 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8783 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8784 b2118c49 2020-07-28 stsp arg.paths = paths;
8785 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8786 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8787 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8788 b2118c49 2020-07-28 stsp &arg);
8789 b2118c49 2020-07-28 stsp done:
8790 b2118c49 2020-07-28 stsp free(fileindex_path);
8791 b2118c49 2020-07-28 stsp if (fileindex)
8792 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8793 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8794 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8795 b2118c49 2020-07-28 stsp err = unlockerr;
8796 b2118c49 2020-07-28 stsp return err;
8797 b2118c49 2020-07-28 stsp }