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 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1946 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1947 c90c8ce3 2020-07-23 stsp }
1948 6353ad76 2019-02-08 stsp
1949 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1950 2c41dce7 2021-06-27 stsp if (ie)
1951 2c41dce7 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1952 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1953 b8f41171 2019-02-10 stsp goto done;
1954 b8f41171 2019-02-10 stsp }
1955 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1956 a769b60b 2021-06-27 stsp if (ie)
1957 a769b60b 2021-06-27 stsp got_fileindex_entry_mark_skipped(ie);
1958 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1959 5036ab18 2020-04-18 stsp path);
1960 5036ab18 2020-04-18 stsp goto done;
1961 5036ab18 2020-04-18 stsp }
1962 b8f41171 2019-02-10 stsp
1963 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1964 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1965 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1966 c6e8a826 2021-04-05 stsp /*
1967 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1968 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1969 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1970 c6e8a826 2021-04-05 stsp * updating contents of this file.
1971 c6e8a826 2021-04-05 stsp */
1972 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1973 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1974 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1975 c6e8a826 2021-04-05 stsp /* Same commit. */
1976 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1977 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1978 e2b1e152 2019-07-13 stsp if (err)
1979 e2b1e152 2019-07-13 stsp goto done;
1980 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1981 b8f41171 2019-02-10 stsp path);
1982 6353ad76 2019-02-08 stsp goto done;
1983 a378724f 2019-02-10 stsp }
1984 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1985 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1986 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1987 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1988 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1989 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1990 0f58026f 2021-04-05 stsp if (err)
1991 0f58026f 2021-04-05 stsp goto done;
1992 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1993 0f58026f 2021-04-05 stsp path);
1994 b8f41171 2019-02-10 stsp goto done;
1995 e2b1e152 2019-07-13 stsp }
1996 9d31a1d8 2018-03-11 stsp }
1997 9d31a1d8 2018-03-11 stsp
1998 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1999 8da9e5f4 2019-01-12 stsp if (err)
2000 6353ad76 2019-02-08 stsp goto done;
2001 512f0d0e 2019-01-02 stsp
2002 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2003 234035bc 2019-06-01 stsp int update_timestamps;
2004 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
2005 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2006 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
2007 234035bc 2019-06-01 stsp struct got_object_id id2;
2008 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2009 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2010 234035bc 2019-06-01 stsp if (err)
2011 f69721c3 2019-10-21 stsp goto done;
2012 f69721c3 2019-10-21 stsp }
2013 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
2014 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
2015 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2016 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
2017 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
2018 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
2019 f69721c3 2019-10-21 stsp goto done;
2020 f69721c3 2019-10-21 stsp }
2021 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2022 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2023 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2024 234035bc 2019-06-01 stsp goto done;
2025 f69721c3 2019-10-21 stsp }
2026 234035bc 2019-06-01 stsp }
2027 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2028 36bf999c 2020-07-23 stsp char *link_target;
2029 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2030 36bf999c 2020-07-23 stsp if (err)
2031 36bf999c 2020-07-23 stsp goto done;
2032 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2033 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2034 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2035 36bf999c 2020-07-23 stsp free(link_target);
2036 993e2a1b 2020-07-23 stsp } else {
2037 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2038 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2039 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2040 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2041 993e2a1b 2020-07-23 stsp }
2042 f69721c3 2019-10-21 stsp free(label_orig);
2043 234035bc 2019-06-01 stsp if (blob2)
2044 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2045 909d120e 2019-09-22 stsp if (err)
2046 909d120e 2019-09-22 stsp goto done;
2047 234035bc 2019-06-01 stsp /*
2048 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2049 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2050 234035bc 2019-06-01 stsp * unmodified files again.
2051 234035bc 2019-06-01 stsp */
2052 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2053 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2054 234035bc 2019-06-01 stsp update_timestamps);
2055 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2056 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2057 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2058 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2059 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2060 1ee397ad 2019-07-12 stsp if (err)
2061 1ee397ad 2019-07-12 stsp goto done;
2062 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2063 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2064 13d9040b 2019-03-27 stsp if (err)
2065 13d9040b 2019-03-27 stsp goto done;
2066 13d9040b 2019-03-27 stsp } else {
2067 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2068 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2069 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2070 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2071 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2072 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2073 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2074 2e1fa222 2020-07-23 stsp } else {
2075 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2076 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2077 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2078 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2079 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2080 2e1fa222 2020-07-23 stsp }
2081 13d9040b 2019-03-27 stsp if (err)
2082 13d9040b 2019-03-27 stsp goto done;
2083 65b05cec 2020-07-23 stsp
2084 054041d0 2020-06-24 stsp if (ie) {
2085 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2086 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2087 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2088 054041d0 2020-06-24 stsp } else {
2089 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2090 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2091 054041d0 2020-06-24 stsp &blob->id);
2092 054041d0 2020-06-24 stsp }
2093 13d9040b 2019-03-27 stsp if (err)
2094 13d9040b 2019-03-27 stsp goto done;
2095 65b05cec 2020-07-23 stsp
2096 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2097 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2098 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2099 2e1fa222 2020-07-23 stsp }
2100 13d9040b 2019-03-27 stsp }
2101 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2102 6353ad76 2019-02-08 stsp done:
2103 6353ad76 2019-02-08 stsp free(ondisk_path);
2104 8da9e5f4 2019-01-12 stsp return err;
2105 90285c3b 2019-01-08 stsp }
2106 90285c3b 2019-01-08 stsp
2107 90285c3b 2019-01-08 stsp static const struct got_error *
2108 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2109 90285c3b 2019-01-08 stsp {
2110 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2111 1c4cdd89 2021-06-20 stsp char *ondisk_path = NULL, *parent = NULL;
2112 90285c3b 2019-01-08 stsp
2113 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2114 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2115 90285c3b 2019-01-08 stsp
2116 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2117 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2118 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2119 c97665ae 2019-01-13 stsp } else {
2120 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2121 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2122 1c4cdd89 2021-06-20 stsp if (err)
2123 1c4cdd89 2021-06-20 stsp goto done;
2124 1c4cdd89 2021-06-20 stsp while (got_path_cmp(parent, root_path,
2125 1c4cdd89 2021-06-20 stsp strlen(parent), root_len) != 0) {
2126 fddefe3b 2020-10-20 stsp free(ondisk_path);
2127 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2128 1c4cdd89 2021-06-20 stsp parent = NULL;
2129 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2130 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2131 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2132 fddefe3b 2020-10-20 stsp ondisk_path);
2133 90285c3b 2019-01-08 stsp break;
2134 90285c3b 2019-01-08 stsp }
2135 1c4cdd89 2021-06-20 stsp err = got_path_dirname(&parent, ondisk_path);
2136 1c4cdd89 2021-06-20 stsp if (err)
2137 1c4cdd89 2021-06-20 stsp break;
2138 1c4cdd89 2021-06-20 stsp }
2139 90285c3b 2019-01-08 stsp }
2140 1c4cdd89 2021-06-20 stsp done:
2141 90285c3b 2019-01-08 stsp free(ondisk_path);
2142 1c4cdd89 2021-06-20 stsp free(parent);
2143 90285c3b 2019-01-08 stsp return err;
2144 512f0d0e 2019-01-02 stsp }
2145 512f0d0e 2019-01-02 stsp
2146 708d8e67 2019-03-27 stsp static const struct got_error *
2147 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2148 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2149 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2150 708d8e67 2019-03-27 stsp {
2151 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2152 708d8e67 2019-03-27 stsp unsigned char status;
2153 708d8e67 2019-03-27 stsp struct stat sb;
2154 708d8e67 2019-03-27 stsp char *ondisk_path;
2155 708d8e67 2019-03-27 stsp
2156 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2157 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2158 a76c42e6 2019-08-03 stsp
2159 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2160 708d8e67 2019-03-27 stsp == -1)
2161 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2162 708d8e67 2019-03-27 stsp
2163 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2164 708d8e67 2019-03-27 stsp if (err)
2165 5a58a424 2020-06-23 stsp goto done;
2166 708d8e67 2019-03-27 stsp
2167 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2168 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2169 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2170 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2171 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2172 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2173 993e2a1b 2020-07-23 stsp goto done;
2174 993e2a1b 2020-07-23 stsp }
2175 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2176 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2177 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2178 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2179 993e2a1b 2020-07-23 stsp if (err)
2180 993e2a1b 2020-07-23 stsp goto done;
2181 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2182 993e2a1b 2020-07-23 stsp ie->path);
2183 993e2a1b 2020-07-23 stsp goto done;
2184 993e2a1b 2020-07-23 stsp }
2185 993e2a1b 2020-07-23 stsp
2186 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2187 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2188 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2189 1ee397ad 2019-07-12 stsp if (err)
2190 5a58a424 2020-06-23 stsp goto done;
2191 fc6346c4 2019-03-27 stsp /*
2192 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2193 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2194 fc6346c4 2019-03-27 stsp */
2195 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2196 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2197 fc6346c4 2019-03-27 stsp } else {
2198 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2199 1ee397ad 2019-07-12 stsp if (err)
2200 5a58a424 2020-06-23 stsp goto done;
2201 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2202 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2203 fc6346c4 2019-03-27 stsp if (err)
2204 5a58a424 2020-06-23 stsp goto done;
2205 fc6346c4 2019-03-27 stsp }
2206 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2207 708d8e67 2019-03-27 stsp }
2208 5a58a424 2020-06-23 stsp done:
2209 5a58a424 2020-06-23 stsp free(ondisk_path);
2210 fc6346c4 2019-03-27 stsp return err;
2211 708d8e67 2019-03-27 stsp }
2212 708d8e67 2019-03-27 stsp
2213 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2214 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2215 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2216 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2217 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2218 8da9e5f4 2019-01-12 stsp void *progress_arg;
2219 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2220 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2221 8da9e5f4 2019-01-12 stsp };
2222 8da9e5f4 2019-01-12 stsp
2223 512f0d0e 2019-01-02 stsp static const struct got_error *
2224 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2225 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2226 512f0d0e 2019-01-02 stsp {
2227 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2228 0584f854 2019-04-06 stsp
2229 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2230 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2231 512f0d0e 2019-01-02 stsp
2232 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2233 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2234 8da9e5f4 2019-01-12 stsp }
2235 512f0d0e 2019-01-02 stsp
2236 8da9e5f4 2019-01-12 stsp static const struct got_error *
2237 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2238 8da9e5f4 2019-01-12 stsp {
2239 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2240 7a9df742 2019-01-08 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 0584f854 2019-04-06 stsp
2244 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2245 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2246 9d31a1d8 2018-03-11 stsp }
2247 9d31a1d8 2018-03-11 stsp
2248 9d31a1d8 2018-03-11 stsp static const struct got_error *
2249 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2250 9d31a1d8 2018-03-11 stsp {
2251 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2252 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2253 8da9e5f4 2019-01-12 stsp char *path;
2254 9d31a1d8 2018-03-11 stsp
2255 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2256 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2257 0584f854 2019-04-06 stsp
2258 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2259 63c5ca5d 2019-08-24 stsp return NULL;
2260 63c5ca5d 2019-08-24 stsp
2261 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2262 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2263 8da9e5f4 2019-01-12 stsp == -1)
2264 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2265 9d31a1d8 2018-03-11 stsp
2266 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2267 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2268 8da9e5f4 2019-01-12 stsp else
2269 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2270 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2271 9d31a1d8 2018-03-11 stsp
2272 8da9e5f4 2019-01-12 stsp free(path);
2273 0cd1c46a 2019-03-11 stsp return err;
2274 0cd1c46a 2019-03-11 stsp }
2275 0cd1c46a 2019-03-11 stsp
2276 b2118c49 2020-07-28 stsp const struct got_error *
2277 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2278 b2118c49 2020-07-28 stsp {
2279 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2280 b2118c49 2020-07-28 stsp
2281 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2282 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2283 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2284 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2285 b2118c49 2020-07-28 stsp }
2286 b2118c49 2020-07-28 stsp
2287 b2118c49 2020-07-28 stsp return NULL;
2288 b2118c49 2020-07-28 stsp }
2289 b2118c49 2020-07-28 stsp
2290 818c7501 2019-07-11 stsp static const struct got_error *
2291 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2292 0cd1c46a 2019-03-11 stsp {
2293 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2294 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2295 0cd1c46a 2019-03-11 stsp
2296 517bab73 2019-03-11 stsp *refname = NULL;
2297 517bab73 2019-03-11 stsp
2298 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2299 b2118c49 2020-07-28 stsp if (err)
2300 b2118c49 2020-07-28 stsp return err;
2301 0cd1c46a 2019-03-11 stsp
2302 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2303 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2304 0647c563 2019-03-11 stsp *refname = NULL;
2305 0cd1c46a 2019-03-11 stsp }
2306 517bab73 2019-03-11 stsp free(uuidstr);
2307 517bab73 2019-03-11 stsp return err;
2308 517bab73 2019-03-11 stsp }
2309 517bab73 2019-03-11 stsp
2310 818c7501 2019-07-11 stsp const struct got_error *
2311 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2312 818c7501 2019-07-11 stsp {
2313 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2314 818c7501 2019-07-11 stsp }
2315 818c7501 2019-07-11 stsp
2316 818c7501 2019-07-11 stsp static const struct got_error *
2317 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2318 818c7501 2019-07-11 stsp {
2319 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2320 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2321 818c7501 2019-07-11 stsp }
2322 818c7501 2019-07-11 stsp
2323 818c7501 2019-07-11 stsp static const struct got_error *
2324 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2325 818c7501 2019-07-11 stsp {
2326 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2327 818c7501 2019-07-11 stsp }
2328 818c7501 2019-07-11 stsp
2329 818c7501 2019-07-11 stsp static const struct got_error *
2330 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2331 818c7501 2019-07-11 stsp {
2332 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2333 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2334 818c7501 2019-07-11 stsp }
2335 818c7501 2019-07-11 stsp
2336 818c7501 2019-07-11 stsp static const struct got_error *
2337 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2338 818c7501 2019-07-11 stsp {
2339 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2340 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2341 0ebf8283 2019-07-24 stsp }
2342 0ebf8283 2019-07-24 stsp
2343 0ebf8283 2019-07-24 stsp static const struct got_error *
2344 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2345 0ebf8283 2019-07-24 stsp {
2346 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2347 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2348 0ebf8283 2019-07-24 stsp }
2349 0ebf8283 2019-07-24 stsp
2350 0ebf8283 2019-07-24 stsp static const struct got_error *
2351 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2352 0ebf8283 2019-07-24 stsp {
2353 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2354 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2355 0ebf8283 2019-07-24 stsp }
2356 0ebf8283 2019-07-24 stsp
2357 0ebf8283 2019-07-24 stsp static const struct got_error *
2358 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2359 0ebf8283 2019-07-24 stsp {
2360 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2361 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2362 818c7501 2019-07-11 stsp }
2363 818c7501 2019-07-11 stsp
2364 0ebf8283 2019-07-24 stsp static const struct got_error *
2365 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2366 0ebf8283 2019-07-24 stsp {
2367 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2368 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2369 0ebf8283 2019-07-24 stsp }
2370 818c7501 2019-07-11 stsp
2371 0ebf8283 2019-07-24 stsp const struct got_error *
2372 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2373 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2374 0ebf8283 2019-07-24 stsp {
2375 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2376 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2377 0ebf8283 2019-07-24 stsp *path = NULL;
2378 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2379 0ebf8283 2019-07-24 stsp }
2380 0ebf8283 2019-07-24 stsp return NULL;
2381 f259c4c1 2021-09-24 stsp }
2382 f259c4c1 2021-09-24 stsp
2383 f259c4c1 2021-09-24 stsp static const struct got_error *
2384 f259c4c1 2021-09-24 stsp get_merge_branch_ref_name(char **refname, struct got_worktree *worktree)
2385 f259c4c1 2021-09-24 stsp {
2386 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2387 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_BRANCH_REF_PREFIX);
2388 0ebf8283 2019-07-24 stsp }
2389 0ebf8283 2019-07-24 stsp
2390 f259c4c1 2021-09-24 stsp static const struct got_error *
2391 f259c4c1 2021-09-24 stsp get_merge_commit_ref_name(char **refname, struct got_worktree *worktree)
2392 f259c4c1 2021-09-24 stsp {
2393 f259c4c1 2021-09-24 stsp return get_ref_name(refname, worktree,
2394 f259c4c1 2021-09-24 stsp GOT_WORKTREE_MERGE_COMMIT_REF_PREFIX);
2395 f259c4c1 2021-09-24 stsp }
2396 f259c4c1 2021-09-24 stsp
2397 517bab73 2019-03-11 stsp /*
2398 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2399 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2400 517bab73 2019-03-11 stsp */
2401 517bab73 2019-03-11 stsp static const struct got_error *
2402 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2403 517bab73 2019-03-11 stsp {
2404 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2405 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2406 517bab73 2019-03-11 stsp char *refname;
2407 517bab73 2019-03-11 stsp
2408 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2409 517bab73 2019-03-11 stsp if (err)
2410 517bab73 2019-03-11 stsp return err;
2411 0cd1c46a 2019-03-11 stsp
2412 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2413 0cd1c46a 2019-03-11 stsp if (err)
2414 0cd1c46a 2019-03-11 stsp goto done;
2415 0cd1c46a 2019-03-11 stsp
2416 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2417 0cd1c46a 2019-03-11 stsp done:
2418 0cd1c46a 2019-03-11 stsp free(refname);
2419 0cd1c46a 2019-03-11 stsp if (ref)
2420 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2421 3e3a69f1 2019-07-25 stsp return err;
2422 3e3a69f1 2019-07-25 stsp }
2423 3e3a69f1 2019-07-25 stsp
2424 3e3a69f1 2019-07-25 stsp static const struct got_error *
2425 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2426 3e3a69f1 2019-07-25 stsp {
2427 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2428 3e3a69f1 2019-07-25 stsp
2429 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2430 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2431 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2432 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2433 3e3a69f1 2019-07-25 stsp }
2434 9d31a1d8 2018-03-11 stsp return err;
2435 9d31a1d8 2018-03-11 stsp }
2436 9d31a1d8 2018-03-11 stsp
2437 3e3a69f1 2019-07-25 stsp
2438 ebf99748 2019-05-09 stsp static const struct got_error *
2439 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2440 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2441 ebf99748 2019-05-09 stsp {
2442 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2443 ebf99748 2019-05-09 stsp FILE *index = NULL;
2444 0cd1c46a 2019-03-11 stsp
2445 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2446 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2447 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2448 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2449 ebf99748 2019-05-09 stsp
2450 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2451 3e3a69f1 2019-07-25 stsp if (err)
2452 ebf99748 2019-05-09 stsp goto done;
2453 ebf99748 2019-05-09 stsp
2454 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2455 ebf99748 2019-05-09 stsp if (index == NULL) {
2456 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2457 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2458 ebf99748 2019-05-09 stsp } else {
2459 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2460 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2461 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2462 ebf99748 2019-05-09 stsp }
2463 ebf99748 2019-05-09 stsp done:
2464 ebf99748 2019-05-09 stsp if (err) {
2465 ebf99748 2019-05-09 stsp free(*fileindex_path);
2466 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2467 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2468 ebf99748 2019-05-09 stsp *fileindex = NULL;
2469 ebf99748 2019-05-09 stsp }
2470 ebf99748 2019-05-09 stsp return err;
2471 ebf99748 2019-05-09 stsp }
2472 c932eeeb 2019-05-22 stsp
2473 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2474 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2475 c932eeeb 2019-05-22 stsp const char *path;
2476 c932eeeb 2019-05-22 stsp size_t path_len;
2477 c932eeeb 2019-05-22 stsp const char *entry_name;
2478 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2479 a484d721 2019-06-10 stsp void *progress_arg;
2480 c932eeeb 2019-05-22 stsp };
2481 ebf99748 2019-05-09 stsp
2482 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2483 c932eeeb 2019-05-22 stsp static const struct got_error *
2484 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2485 c932eeeb 2019-05-22 stsp {
2486 1ee397ad 2019-07-12 stsp const struct got_error *err;
2487 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2488 c932eeeb 2019-05-22 stsp
2489 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2490 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2491 c932eeeb 2019-05-22 stsp return NULL;
2492 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2493 102ff934 2019-06-11 stsp return NULL;
2494 102ff934 2019-06-11 stsp
2495 a769b60b 2021-06-27 stsp if (got_fileindex_entry_was_skipped(ie))
2496 a769b60b 2021-06-27 stsp return NULL;
2497 a769b60b 2021-06-27 stsp
2498 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2499 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2500 c932eeeb 2019-05-22 stsp return NULL;
2501 c932eeeb 2019-05-22 stsp
2502 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2503 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2504 edd02c5e 2019-07-11 stsp ie->path);
2505 1ee397ad 2019-07-12 stsp if (err)
2506 1ee397ad 2019-07-12 stsp return err;
2507 1ee397ad 2019-07-12 stsp }
2508 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2509 c932eeeb 2019-05-22 stsp return NULL;
2510 a615e0e7 2020-12-16 stsp }
2511 a615e0e7 2020-12-16 stsp
2512 a615e0e7 2020-12-16 stsp static const struct got_error *
2513 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2514 abc59930 2021-09-05 naddy struct got_fileindex *fileindex,
2515 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
2516 a615e0e7 2020-12-16 stsp {
2517 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2518 a615e0e7 2020-12-16 stsp
2519 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2520 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2521 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2522 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2523 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2524 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2525 a615e0e7 2020-12-16 stsp
2526 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2527 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2528 9c6338c4 2019-06-02 stsp }
2529 9c6338c4 2019-06-02 stsp
2530 9c6338c4 2019-06-02 stsp static const struct got_error *
2531 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2532 9c6338c4 2019-06-02 stsp {
2533 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2534 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2535 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2536 867630bb 2020-01-17 stsp struct timespec timeout;
2537 9c6338c4 2019-06-02 stsp
2538 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2539 9c6338c4 2019-06-02 stsp fileindex_path);
2540 9c6338c4 2019-06-02 stsp if (err)
2541 9c6338c4 2019-06-02 stsp goto done;
2542 9c6338c4 2019-06-02 stsp
2543 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2544 9c6338c4 2019-06-02 stsp if (err)
2545 9c6338c4 2019-06-02 stsp goto done;
2546 9c6338c4 2019-06-02 stsp
2547 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2548 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2549 9c6338c4 2019-06-02 stsp fileindex_path);
2550 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2551 9c6338c4 2019-06-02 stsp }
2552 867630bb 2020-01-17 stsp
2553 867630bb 2020-01-17 stsp /*
2554 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2555 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2556 867630bb 2020-01-17 stsp * was recorded in the file index.
2557 867630bb 2020-01-17 stsp */
2558 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2559 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2560 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2561 9c6338c4 2019-06-02 stsp done:
2562 9c6338c4 2019-06-02 stsp if (new_index)
2563 9c6338c4 2019-06-02 stsp fclose(new_index);
2564 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2565 9c6338c4 2019-06-02 stsp return err;
2566 c932eeeb 2019-05-22 stsp }
2567 6ced4176 2019-07-12 stsp
2568 6ced4176 2019-07-12 stsp static const struct got_error *
2569 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2570 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2571 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2572 6ced4176 2019-07-12 stsp {
2573 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2574 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2575 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2576 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2577 6ced4176 2019-07-12 stsp
2578 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2579 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2580 6ced4176 2019-07-12 stsp *tree_id = NULL;
2581 6ced4176 2019-07-12 stsp
2582 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2583 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2584 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2585 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2586 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2587 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2588 6ced4176 2019-07-12 stsp goto done;
2589 6ced4176 2019-07-12 stsp }
2590 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2591 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2592 6ced4176 2019-07-12 stsp if (err)
2593 6ced4176 2019-07-12 stsp goto done;
2594 6ced4176 2019-07-12 stsp return NULL;
2595 6ced4176 2019-07-12 stsp }
2596 6ced4176 2019-07-12 stsp
2597 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2598 6ced4176 2019-07-12 stsp
2599 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2600 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2601 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2602 6ced4176 2019-07-12 stsp goto done;
2603 6ced4176 2019-07-12 stsp }
2604 6ced4176 2019-07-12 stsp
2605 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2606 6ced4176 2019-07-12 stsp in_repo_path);
2607 6ced4176 2019-07-12 stsp if (err)
2608 6ced4176 2019-07-12 stsp goto done;
2609 6ced4176 2019-07-12 stsp
2610 6ced4176 2019-07-12 stsp free(in_repo_path);
2611 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2612 6ced4176 2019-07-12 stsp
2613 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2614 6ced4176 2019-07-12 stsp if (err)
2615 6ced4176 2019-07-12 stsp goto done;
2616 c932eeeb 2019-05-22 stsp
2617 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2618 6ced4176 2019-07-12 stsp /* Check out a single file. */
2619 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2620 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2621 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2622 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2623 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2624 6ced4176 2019-07-12 stsp goto done;
2625 6ced4176 2019-07-12 stsp }
2626 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2627 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2628 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2629 6ced4176 2019-07-12 stsp goto done;
2630 6ced4176 2019-07-12 stsp }
2631 6ced4176 2019-07-12 stsp } else {
2632 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2633 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2634 6ced4176 2019-07-12 stsp if (err)
2635 6ced4176 2019-07-12 stsp return err;
2636 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2637 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2638 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2639 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2640 6ced4176 2019-07-12 stsp goto done;
2641 6ced4176 2019-07-12 stsp }
2642 6ced4176 2019-07-12 stsp }
2643 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2644 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2645 6ced4176 2019-07-12 stsp } else {
2646 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2647 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2648 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2649 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2650 6ced4176 2019-07-12 stsp goto done;
2651 6ced4176 2019-07-12 stsp }
2652 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2653 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2654 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2655 6ced4176 2019-07-12 stsp goto done;
2656 6ced4176 2019-07-12 stsp }
2657 6ced4176 2019-07-12 stsp }
2658 6ced4176 2019-07-12 stsp done:
2659 6ced4176 2019-07-12 stsp free(id);
2660 6ced4176 2019-07-12 stsp free(in_repo_path);
2661 6ced4176 2019-07-12 stsp if (err) {
2662 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2663 6ced4176 2019-07-12 stsp free(*tree_relpath);
2664 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2665 6ced4176 2019-07-12 stsp free(*tree_id);
2666 6ced4176 2019-07-12 stsp *tree_id = NULL;
2667 6ced4176 2019-07-12 stsp }
2668 07ed472d 2019-07-12 stsp return err;
2669 07ed472d 2019-07-12 stsp }
2670 07ed472d 2019-07-12 stsp
2671 07ed472d 2019-07-12 stsp static const struct got_error *
2672 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2673 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2674 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2675 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2676 07ed472d 2019-07-12 stsp {
2677 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2678 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2679 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2680 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2681 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2682 07ed472d 2019-07-12 stsp
2683 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2684 7f47418f 2019-12-20 stsp if (err) {
2685 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2686 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2687 7f47418f 2019-12-20 stsp goto done;
2688 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2689 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2690 7f47418f 2019-12-20 stsp if (err)
2691 7f47418f 2019-12-20 stsp return err;
2692 7f47418f 2019-12-20 stsp }
2693 07ed472d 2019-07-12 stsp
2694 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2695 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2696 07ed472d 2019-07-12 stsp if (err)
2697 07ed472d 2019-07-12 stsp goto done;
2698 07ed472d 2019-07-12 stsp
2699 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2700 07ed472d 2019-07-12 stsp if (err)
2701 07ed472d 2019-07-12 stsp goto done;
2702 07ed472d 2019-07-12 stsp
2703 07ed472d 2019-07-12 stsp if (entry_name &&
2704 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2705 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2706 07ed472d 2019-07-12 stsp goto done;
2707 07ed472d 2019-07-12 stsp }
2708 07ed472d 2019-07-12 stsp
2709 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2710 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2711 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2712 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2713 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2714 07ed472d 2019-07-12 stsp arg.repo = repo;
2715 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2716 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2717 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2718 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2719 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2720 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2721 07ed472d 2019-07-12 stsp done:
2722 07ed472d 2019-07-12 stsp if (tree)
2723 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2724 07ed472d 2019-07-12 stsp if (commit)
2725 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2726 6ced4176 2019-07-12 stsp return err;
2727 6ced4176 2019-07-12 stsp }
2728 6ced4176 2019-07-12 stsp
2729 9d31a1d8 2018-03-11 stsp const struct got_error *
2730 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2731 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2732 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2733 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2734 9d31a1d8 2018-03-11 stsp {
2735 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2736 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2737 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2738 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2739 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2740 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2741 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2742 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tree_path_data) entry;
2743 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2744 f2ea84fa 2019-07-27 stsp int entry_type;
2745 f2ea84fa 2019-07-27 stsp char *relpath;
2746 f2ea84fa 2019-07-27 stsp char *entry_name;
2747 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2748 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tree_paths, tree_path_data) tree_paths;
2749 9d31a1d8 2018-03-11 stsp
2750 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_paths);
2751 f2ea84fa 2019-07-27 stsp
2752 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2753 9d31a1d8 2018-03-11 stsp if (err)
2754 9d31a1d8 2018-03-11 stsp return err;
2755 93a30277 2018-12-24 stsp
2756 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2757 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2758 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2759 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2760 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2761 f2ea84fa 2019-07-27 stsp goto done;
2762 f2ea84fa 2019-07-27 stsp }
2763 6ced4176 2019-07-12 stsp
2764 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2765 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2766 f2ea84fa 2019-07-27 stsp if (err) {
2767 f2ea84fa 2019-07-27 stsp free(tpd);
2768 6ced4176 2019-07-12 stsp goto done;
2769 6ced4176 2019-07-12 stsp }
2770 f2ea84fa 2019-07-27 stsp
2771 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2772 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2773 f2ea84fa 2019-07-27 stsp if (err) {
2774 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2775 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2776 f2ea84fa 2019-07-27 stsp free(tpd);
2777 f2ea84fa 2019-07-27 stsp goto done;
2778 f2ea84fa 2019-07-27 stsp }
2779 f2ea84fa 2019-07-27 stsp } else
2780 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2781 f2ea84fa 2019-07-27 stsp
2782 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_paths, tpd, entry);
2783 6ced4176 2019-07-12 stsp }
2784 6ced4176 2019-07-12 stsp
2785 51514078 2018-12-25 stsp /*
2786 51514078 2018-12-25 stsp * Read the file index.
2787 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2788 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2789 51514078 2018-12-25 stsp */
2790 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2791 8da9e5f4 2019-01-12 stsp if (err)
2792 c4cdcb68 2019-04-03 stsp goto done;
2793 c4cdcb68 2019-04-03 stsp
2794 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2795 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2796 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2797 f2ea84fa 2019-07-27 stsp
2798 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2799 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2800 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2801 f2ea84fa 2019-07-27 stsp if (err)
2802 f2ea84fa 2019-07-27 stsp break;
2803 f2ea84fa 2019-07-27 stsp
2804 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2805 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2806 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2807 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2808 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2809 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2810 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2811 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2812 f2ea84fa 2019-07-27 stsp if (err)
2813 f2ea84fa 2019-07-27 stsp break;
2814 f2ea84fa 2019-07-27 stsp
2815 dbdddfee 2021-06-23 naddy tpd = STAILQ_NEXT(tpd, entry);
2816 711d9cd9 2019-07-12 stsp }
2817 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2818 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2819 af12c6b9 2019-06-04 stsp err = sync_err;
2820 9d31a1d8 2018-03-11 stsp done:
2821 9c6338c4 2019-06-02 stsp free(fileindex_path);
2822 edfa7d7f 2018-09-11 stsp if (tree)
2823 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2824 9d31a1d8 2018-03-11 stsp if (commit)
2825 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2826 5ade8233 2019-07-12 stsp if (fileindex)
2827 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2828 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_paths)) {
2829 dbdddfee 2021-06-23 naddy tpd = STAILQ_FIRST(&tree_paths);
2830 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_paths, entry);
2831 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2832 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2833 f2ea84fa 2019-07-27 stsp free(tpd);
2834 f2ea84fa 2019-07-27 stsp }
2835 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2836 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2837 9d31a1d8 2018-03-11 stsp err = unlockerr;
2838 f8d1f275 2019-02-04 stsp return err;
2839 f8d1f275 2019-02-04 stsp }
2840 f8d1f275 2019-02-04 stsp
2841 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2842 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2843 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2844 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2845 234035bc 2019-06-01 stsp void *progress_arg;
2846 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2847 234035bc 2019-06-01 stsp void *cancel_arg;
2848 f69721c3 2019-10-21 stsp const char *label_orig;
2849 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2850 234035bc 2019-06-01 stsp };
2851 234035bc 2019-06-01 stsp
2852 234035bc 2019-06-01 stsp static const struct got_error *
2853 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2854 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2855 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2856 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2857 234035bc 2019-06-01 stsp {
2858 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2859 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2860 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2861 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2862 234035bc 2019-06-01 stsp struct stat sb;
2863 234035bc 2019-06-01 stsp unsigned char status;
2864 234035bc 2019-06-01 stsp int local_changes_subsumed;
2865 1af628f4 2021-06-03 stsp FILE *f_orig = NULL, *f_deriv = NULL, *f_deriv2 = NULL;
2866 54d5be07 2021-06-03 stsp char *id_str = NULL, *label_deriv2 = NULL;
2867 234035bc 2019-06-01 stsp
2868 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2869 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2870 d6c87207 2019-08-02 stsp strlen(path2));
2871 1ee397ad 2019-07-12 stsp if (ie == NULL)
2872 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2873 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2874 234035bc 2019-06-01 stsp
2875 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2876 234035bc 2019-06-01 stsp path2) == -1)
2877 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2878 234035bc 2019-06-01 stsp
2879 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2880 7f91a133 2019-12-13 stsp repo);
2881 234035bc 2019-06-01 stsp if (err)
2882 234035bc 2019-06-01 stsp goto done;
2883 234035bc 2019-06-01 stsp
2884 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2885 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2886 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2887 234035bc 2019-06-01 stsp goto done;
2888 234035bc 2019-06-01 stsp }
2889 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2890 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2891 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2892 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2893 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2894 234035bc 2019-06-01 stsp goto done;
2895 234035bc 2019-06-01 stsp }
2896 234035bc 2019-06-01 stsp
2897 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2898 36bf999c 2020-07-23 stsp char *link_target2;
2899 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2900 36bf999c 2020-07-23 stsp if (err)
2901 36bf999c 2020-07-23 stsp goto done;
2902 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2903 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2904 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2905 36bf999c 2020-07-23 stsp free(link_target2);
2906 af57b12a 2020-07-23 stsp } else {
2907 1af628f4 2021-06-03 stsp int fd;
2908 1af628f4 2021-06-03 stsp
2909 1af628f4 2021-06-03 stsp f_orig = got_opentemp();
2910 1af628f4 2021-06-03 stsp if (f_orig == NULL) {
2911 1af628f4 2021-06-03 stsp err = got_error_from_errno("got_opentemp");
2912 1af628f4 2021-06-03 stsp goto done;
2913 1af628f4 2021-06-03 stsp }
2914 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2915 1af628f4 2021-06-03 stsp f_orig, blob1);
2916 1af628f4 2021-06-03 stsp if (err)
2917 1af628f4 2021-06-03 stsp goto done;
2918 1af628f4 2021-06-03 stsp
2919 54d5be07 2021-06-03 stsp f_deriv2 = got_opentemp();
2920 54d5be07 2021-06-03 stsp if (f_deriv2 == NULL)
2921 1af628f4 2021-06-03 stsp goto done;
2922 1af628f4 2021-06-03 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL,
2923 54d5be07 2021-06-03 stsp f_deriv2, blob2);
2924 1af628f4 2021-06-03 stsp if (err)
2925 1af628f4 2021-06-03 stsp goto done;
2926 1af628f4 2021-06-03 stsp
2927 1af628f4 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
2928 1af628f4 2021-06-03 stsp if (fd == -1) {
2929 1af628f4 2021-06-03 stsp err = got_error_from_errno2("open",
2930 1af628f4 2021-06-03 stsp ondisk_path);
2931 1af628f4 2021-06-03 stsp goto done;
2932 1af628f4 2021-06-03 stsp }
2933 54d5be07 2021-06-03 stsp f_deriv = fdopen(fd, "r");
2934 54d5be07 2021-06-03 stsp if (f_deriv == NULL) {
2935 1af628f4 2021-06-03 stsp err = got_error_from_errno2("fdopen",
2936 1af628f4 2021-06-03 stsp ondisk_path);
2937 1af628f4 2021-06-03 stsp close(fd);
2938 1af628f4 2021-06-03 stsp goto done;
2939 1af628f4 2021-06-03 stsp }
2940 1af628f4 2021-06-03 stsp err = got_object_id_str(&id_str, a->commit_id2);
2941 1af628f4 2021-06-03 stsp if (err)
2942 1af628f4 2021-06-03 stsp goto done;
2943 54d5be07 2021-06-03 stsp if (asprintf(&label_deriv2, "%s: commit %s",
2944 1af628f4 2021-06-03 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
2945 1af628f4 2021-06-03 stsp err = got_error_from_errno("asprintf");
2946 1af628f4 2021-06-03 stsp goto done;
2947 1af628f4 2021-06-03 stsp }
2948 1af628f4 2021-06-03 stsp err = merge_file(&local_changes_subsumed, a->worktree,
2949 1af628f4 2021-06-03 stsp f_orig, f_deriv, f_deriv2, ondisk_path, path2,
2950 54d5be07 2021-06-03 stsp sb.st_mode, a->label_orig, NULL, label_deriv2,
2951 fdf3c2d3 2021-06-17 stsp GOT_DIFF_ALGORITHM_PATIENCE, repo,
2952 fdf3c2d3 2021-06-17 stsp a->progress_cb, a->progress_arg);
2953 af57b12a 2020-07-23 stsp }
2954 234035bc 2019-06-01 stsp } else if (blob1) {
2955 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2956 d6c87207 2019-08-02 stsp strlen(path1));
2957 1ee397ad 2019-07-12 stsp if (ie == NULL)
2958 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2959 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2960 2b92fad7 2019-06-02 stsp
2961 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2962 2b92fad7 2019-06-02 stsp path1) == -1)
2963 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2964 2b92fad7 2019-06-02 stsp
2965 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2966 7f91a133 2019-12-13 stsp repo);
2967 2b92fad7 2019-06-02 stsp if (err)
2968 2b92fad7 2019-06-02 stsp goto done;
2969 2b92fad7 2019-06-02 stsp
2970 2b92fad7 2019-06-02 stsp switch (status) {
2971 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2972 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2973 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2974 1ee397ad 2019-07-12 stsp if (err)
2975 1ee397ad 2019-07-12 stsp goto done;
2976 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2977 2b92fad7 2019-06-02 stsp if (err)
2978 2b92fad7 2019-06-02 stsp goto done;
2979 2b92fad7 2019-06-02 stsp if (ie)
2980 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2981 2b92fad7 2019-06-02 stsp break;
2982 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2983 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
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 if (ie)
2989 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2990 2b92fad7 2019-06-02 stsp break;
2991 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2992 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2993 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2994 0a22ca1a 2020-09-23 stsp /*
2995 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2996 0a22ca1a 2020-09-23 stsp * exists in the repository.
2997 0a22ca1a 2020-09-23 stsp */
2998 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2999 0a22ca1a 2020-09-23 stsp if (err)
3000 0a22ca1a 2020-09-23 stsp goto done;
3001 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
3002 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3003 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
3004 0a22ca1a 2020-09-23 stsp if (err)
3005 0a22ca1a 2020-09-23 stsp goto done;
3006 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
3007 0a22ca1a 2020-09-23 stsp path1);
3008 0a22ca1a 2020-09-23 stsp if (err)
3009 0a22ca1a 2020-09-23 stsp goto done;
3010 0a22ca1a 2020-09-23 stsp if (ie)
3011 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
3012 0a22ca1a 2020-09-23 stsp ie);
3013 0a22ca1a 2020-09-23 stsp } else {
3014 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
3015 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
3016 0a22ca1a 2020-09-23 stsp }
3017 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
3018 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
3019 0a22ca1a 2020-09-23 stsp free(id);
3020 0a22ca1a 2020-09-23 stsp if (err)
3021 0a22ca1a 2020-09-23 stsp goto done;
3022 0a22ca1a 2020-09-23 stsp break;
3023 0a22ca1a 2020-09-23 stsp }
3024 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
3025 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
3026 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3027 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
3028 1ee397ad 2019-07-12 stsp if (err)
3029 1ee397ad 2019-07-12 stsp goto done;
3030 2b92fad7 2019-06-02 stsp break;
3031 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
3032 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
3033 1ee397ad 2019-07-12 stsp if (err)
3034 1ee397ad 2019-07-12 stsp goto done;
3035 2b92fad7 2019-06-02 stsp break;
3036 2b92fad7 2019-06-02 stsp default:
3037 2b92fad7 2019-06-02 stsp break;
3038 2b92fad7 2019-06-02 stsp }
3039 234035bc 2019-06-01 stsp } else if (blob2) {
3040 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3041 234035bc 2019-06-01 stsp path2) == -1)
3042 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3043 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
3044 d6c87207 2019-08-02 stsp strlen(path2));
3045 234035bc 2019-06-01 stsp if (ie) {
3046 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
3047 7f91a133 2019-12-13 stsp -1, NULL, repo);
3048 234035bc 2019-06-01 stsp if (err)
3049 234035bc 2019-06-01 stsp goto done;
3050 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
3051 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
3052 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
3053 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
3054 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
3055 1ee397ad 2019-07-12 stsp status, path2);
3056 234035bc 2019-06-01 stsp goto done;
3057 234035bc 2019-06-01 stsp }
3058 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
3059 36bf999c 2020-07-23 stsp char *link_target2;
3060 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
3061 36bf999c 2020-07-23 stsp blob2);
3062 36bf999c 2020-07-23 stsp if (err)
3063 36bf999c 2020-07-23 stsp goto done;
3064 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
3065 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
3066 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
3067 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
3068 36bf999c 2020-07-23 stsp free(link_target2);
3069 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
3070 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
3071 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
3072 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
3073 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
3074 526a746f 2020-07-23 stsp a->progress_arg);
3075 dfe9fba0 2020-07-23 stsp } else {
3076 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
3077 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
3078 526a746f 2020-07-23 stsp }
3079 dfe9fba0 2020-07-23 stsp if (err)
3080 dfe9fba0 2020-07-23 stsp goto done;
3081 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
3082 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3083 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
3084 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
3085 234035bc 2019-06-01 stsp if (err)
3086 234035bc 2019-06-01 stsp goto done;
3087 234035bc 2019-06-01 stsp }
3088 234035bc 2019-06-01 stsp } else {
3089 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3090 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3091 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3092 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3093 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3094 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3095 2e1fa222 2020-07-23 stsp } else {
3096 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3097 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3098 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3099 2e1fa222 2020-07-23 stsp }
3100 234035bc 2019-06-01 stsp if (err)
3101 234035bc 2019-06-01 stsp goto done;
3102 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3103 234035bc 2019-06-01 stsp if (err)
3104 234035bc 2019-06-01 stsp goto done;
3105 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3106 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3107 3969253a 2020-03-07 stsp if (err) {
3108 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3109 3969253a 2020-03-07 stsp goto done;
3110 3969253a 2020-03-07 stsp }
3111 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3112 2b92fad7 2019-06-02 stsp if (err) {
3113 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3114 2b92fad7 2019-06-02 stsp goto done;
3115 2b92fad7 2019-06-02 stsp }
3116 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3117 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3118 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3119 2e1fa222 2020-07-23 stsp }
3120 234035bc 2019-06-01 stsp }
3121 234035bc 2019-06-01 stsp }
3122 234035bc 2019-06-01 stsp done:
3123 1af628f4 2021-06-03 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
3124 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3125 1af628f4 2021-06-03 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
3126 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3127 1af628f4 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
3128 1af628f4 2021-06-03 stsp err = got_error_from_errno("fclose");
3129 1af628f4 2021-06-03 stsp free(id_str);
3130 54d5be07 2021-06-03 stsp free(label_deriv2);
3131 234035bc 2019-06-01 stsp free(ondisk_path);
3132 234035bc 2019-06-01 stsp return err;
3133 234035bc 2019-06-01 stsp }
3134 234035bc 2019-06-01 stsp
3135 69de9dd4 2021-09-03 stsp static const struct got_error *
3136 69de9dd4 2021-09-03 stsp check_mixed_commits(void *arg, struct got_fileindex_entry *ie)
3137 69de9dd4 2021-09-03 stsp {
3138 69de9dd4 2021-09-03 stsp struct got_worktree *worktree = arg;
3139 69de9dd4 2021-09-03 stsp
3140 abc59930 2021-09-05 naddy /* Reject merges into a work tree with mixed base commits. */
3141 abc59930 2021-09-05 naddy if (got_fileindex_entry_has_commit(ie) &&
3142 69de9dd4 2021-09-03 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
3143 69de9dd4 2021-09-03 stsp SHA1_DIGEST_LENGTH) != 0)
3144 abc59930 2021-09-05 naddy return got_error(GOT_ERR_MIXED_COMMITS);
3145 69de9dd4 2021-09-03 stsp
3146 69de9dd4 2021-09-03 stsp return NULL;
3147 69de9dd4 2021-09-03 stsp }
3148 69de9dd4 2021-09-03 stsp
3149 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg {
3150 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3151 69de9dd4 2021-09-03 stsp struct got_fileindex *fileindex;
3152 234035bc 2019-06-01 stsp struct got_repository *repo;
3153 234035bc 2019-06-01 stsp };
3154 234035bc 2019-06-01 stsp
3155 234035bc 2019-06-01 stsp static const struct got_error *
3156 69de9dd4 2021-09-03 stsp check_merge_conflicts(void *arg, struct got_blob_object *blob1,
3157 69de9dd4 2021-09-03 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3158 69de9dd4 2021-09-03 stsp struct got_object_id *id2, const char *path1, const char *path2,
3159 69de9dd4 2021-09-03 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
3160 234035bc 2019-06-01 stsp {
3161 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3162 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg *a = arg;
3163 234035bc 2019-06-01 stsp unsigned char status;
3164 234035bc 2019-06-01 stsp struct stat sb;
3165 69de9dd4 2021-09-03 stsp struct got_fileindex_entry *ie;
3166 69de9dd4 2021-09-03 stsp const char *path = path2 ? path2 : path1;
3167 69de9dd4 2021-09-03 stsp struct got_object_id *id = id2 ? id2 : id1;
3168 234035bc 2019-06-01 stsp char *ondisk_path;
3169 234035bc 2019-06-01 stsp
3170 69de9dd4 2021-09-03 stsp if (id == NULL)
3171 69de9dd4 2021-09-03 stsp return NULL;
3172 234035bc 2019-06-01 stsp
3173 69de9dd4 2021-09-03 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
3174 69de9dd4 2021-09-03 stsp if (ie == NULL)
3175 69de9dd4 2021-09-03 stsp return NULL;
3176 69de9dd4 2021-09-03 stsp
3177 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3178 234035bc 2019-06-01 stsp == -1)
3179 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3180 234035bc 2019-06-01 stsp
3181 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3182 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3183 5546d466 2021-09-02 stsp free(ondisk_path);
3184 234035bc 2019-06-01 stsp if (err)
3185 234035bc 2019-06-01 stsp return err;
3186 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3187 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3188 234035bc 2019-06-01 stsp
3189 234035bc 2019-06-01 stsp return NULL;
3190 234035bc 2019-06-01 stsp }
3191 234035bc 2019-06-01 stsp
3192 818c7501 2019-07-11 stsp static const struct got_error *
3193 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3194 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3195 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3196 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3197 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3198 234035bc 2019-06-01 stsp {
3199 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3200 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3201 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3202 69de9dd4 2021-09-03 stsp struct check_merge_conflicts_arg cmc_arg;
3203 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3204 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3205 234035bc 2019-06-01 stsp
3206 03415a1a 2019-06-02 stsp if (commit_id1) {
3207 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3208 03415a1a 2019-06-02 stsp worktree->path_prefix);
3209 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3210 03415a1a 2019-06-02 stsp goto done;
3211 69d57f3d 2020-07-31 stsp }
3212 69d57f3d 2020-07-31 stsp if (tree_id1) {
3213 69d57f3d 2020-07-31 stsp char *id_str;
3214 03415a1a 2019-06-02 stsp
3215 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3216 03415a1a 2019-06-02 stsp if (err)
3217 03415a1a 2019-06-02 stsp goto done;
3218 f69721c3 2019-10-21 stsp
3219 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3220 f69721c3 2019-10-21 stsp if (err)
3221 f69721c3 2019-10-21 stsp goto done;
3222 f69721c3 2019-10-21 stsp
3223 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3224 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3225 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3226 f69721c3 2019-10-21 stsp free(id_str);
3227 f69721c3 2019-10-21 stsp goto done;
3228 f69721c3 2019-10-21 stsp }
3229 f69721c3 2019-10-21 stsp free(id_str);
3230 03415a1a 2019-06-02 stsp }
3231 234035bc 2019-06-01 stsp
3232 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3233 234035bc 2019-06-01 stsp worktree->path_prefix);
3234 234035bc 2019-06-01 stsp if (err)
3235 234035bc 2019-06-01 stsp goto done;
3236 234035bc 2019-06-01 stsp
3237 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3238 234035bc 2019-06-01 stsp if (err)
3239 234035bc 2019-06-01 stsp goto done;
3240 234035bc 2019-06-01 stsp
3241 69de9dd4 2021-09-03 stsp cmc_arg.worktree = worktree;
3242 69de9dd4 2021-09-03 stsp cmc_arg.fileindex = fileindex;
3243 69de9dd4 2021-09-03 stsp cmc_arg.repo = repo;
3244 69de9dd4 2021-09-03 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3245 69de9dd4 2021-09-03 stsp check_merge_conflicts, &cmc_arg, 0);
3246 69de9dd4 2021-09-03 stsp if (err)
3247 69de9dd4 2021-09-03 stsp goto done;
3248 69de9dd4 2021-09-03 stsp
3249 234035bc 2019-06-01 stsp arg.worktree = worktree;
3250 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3251 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3252 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3253 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3254 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3255 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3256 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3257 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3258 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3259 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3260 af12c6b9 2019-06-04 stsp err = sync_err;
3261 234035bc 2019-06-01 stsp done:
3262 234035bc 2019-06-01 stsp if (tree1)
3263 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3264 234035bc 2019-06-01 stsp if (tree2)
3265 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3266 f69721c3 2019-10-21 stsp free(label_orig);
3267 818c7501 2019-07-11 stsp return err;
3268 818c7501 2019-07-11 stsp }
3269 234035bc 2019-06-01 stsp
3270 818c7501 2019-07-11 stsp const struct got_error *
3271 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3272 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3273 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3274 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3275 818c7501 2019-07-11 stsp {
3276 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3277 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3278 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3279 818c7501 2019-07-11 stsp
3280 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3281 818c7501 2019-07-11 stsp if (err)
3282 818c7501 2019-07-11 stsp return err;
3283 818c7501 2019-07-11 stsp
3284 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3285 818c7501 2019-07-11 stsp if (err)
3286 818c7501 2019-07-11 stsp goto done;
3287 818c7501 2019-07-11 stsp
3288 69de9dd4 2021-09-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
3289 69de9dd4 2021-09-03 stsp worktree);
3290 818c7501 2019-07-11 stsp if (err)
3291 818c7501 2019-07-11 stsp goto done;
3292 818c7501 2019-07-11 stsp
3293 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3294 f259c4c1 2021-09-24 stsp commit_id2, repo, progress_cb, progress_arg,
3295 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
3296 818c7501 2019-07-11 stsp done:
3297 818c7501 2019-07-11 stsp if (fileindex)
3298 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3299 818c7501 2019-07-11 stsp free(fileindex_path);
3300 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3301 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3302 234035bc 2019-06-01 stsp err = unlockerr;
3303 234035bc 2019-06-01 stsp return err;
3304 234035bc 2019-06-01 stsp }
3305 234035bc 2019-06-01 stsp
3306 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3307 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3308 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3309 927df6b7 2019-02-10 stsp const char *status_path;
3310 927df6b7 2019-02-10 stsp size_t status_path_len;
3311 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3312 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3313 f8d1f275 2019-02-04 stsp void *status_arg;
3314 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3315 f8d1f275 2019-02-04 stsp void *cancel_arg;
3316 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3317 ff56836b 2021-07-08 stsp struct got_pathlist_head *ignores;
3318 f2a9dc41 2019-12-13 tracey int report_unchanged;
3319 3143d852 2020-06-25 stsp int no_ignores;
3320 f8d1f275 2019-02-04 stsp };
3321 88d0e355 2019-08-03 stsp
3322 f8d1f275 2019-02-04 stsp static const struct got_error *
3323 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3324 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3325 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3326 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3327 927df6b7 2019-02-10 stsp {
3328 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3329 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3330 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3331 927df6b7 2019-02-10 stsp struct stat sb;
3332 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3333 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3334 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3335 927df6b7 2019-02-10 stsp
3336 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3337 98eaaa12 2019-08-03 stsp if (err)
3338 98eaaa12 2019-08-03 stsp return err;
3339 98eaaa12 2019-08-03 stsp
3340 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3341 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3342 98eaaa12 2019-08-03 stsp return NULL;
3343 98eaaa12 2019-08-03 stsp
3344 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3345 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3346 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3347 98eaaa12 2019-08-03 stsp }
3348 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3349 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3350 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3351 927df6b7 2019-02-10 stsp }
3352 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3353 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3354 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3355 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3356 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3357 98eaaa12 2019-08-03 stsp }
3358 98eaaa12 2019-08-03 stsp
3359 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3360 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3361 927df6b7 2019-02-10 stsp }
3362 927df6b7 2019-02-10 stsp
3363 927df6b7 2019-02-10 stsp static const struct got_error *
3364 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3365 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3366 f8d1f275 2019-02-04 stsp {
3367 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3368 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3369 f8d1f275 2019-02-04 stsp char *abspath;
3370 f8d1f275 2019-02-04 stsp
3371 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3372 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3373 0584f854 2019-04-06 stsp
3374 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3375 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3376 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3377 927df6b7 2019-02-10 stsp return NULL;
3378 927df6b7 2019-02-10 stsp
3379 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3380 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3381 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3382 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3383 f8d1f275 2019-02-04 stsp } else {
3384 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3385 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3386 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3387 f8d1f275 2019-02-04 stsp }
3388 f8d1f275 2019-02-04 stsp
3389 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3390 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3391 f8d1f275 2019-02-04 stsp free(abspath);
3392 9d31a1d8 2018-03-11 stsp return err;
3393 9d31a1d8 2018-03-11 stsp }
3394 f8d1f275 2019-02-04 stsp
3395 f8d1f275 2019-02-04 stsp static const struct got_error *
3396 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3397 f8d1f275 2019-02-04 stsp {
3398 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3399 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3400 2ec1f75b 2019-03-26 stsp unsigned char status;
3401 927df6b7 2019-02-10 stsp
3402 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3403 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3404 0584f854 2019-04-06 stsp
3405 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3406 927df6b7 2019-02-10 stsp return NULL;
3407 927df6b7 2019-02-10 stsp
3408 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3409 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3410 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3411 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3412 2ec1f75b 2019-03-26 stsp else
3413 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3414 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3415 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3416 6841da00 2019-08-08 stsp }
3417 6841da00 2019-08-08 stsp
3418 6841da00 2019-08-08 stsp void
3419 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3420 6841da00 2019-08-08 stsp {
3421 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3422 6841da00 2019-08-08 stsp
3423 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3424 6841da00 2019-08-08 stsp free((char *)pe->path);
3425 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3426 6841da00 2019-08-08 stsp }
3427 6841da00 2019-08-08 stsp
3428 6841da00 2019-08-08 stsp void
3429 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3430 6841da00 2019-08-08 stsp {
3431 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3432 6841da00 2019-08-08 stsp
3433 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3434 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3435 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3436 6841da00 2019-08-08 stsp free((char *)pe->path);
3437 6841da00 2019-08-08 stsp }
3438 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3439 6841da00 2019-08-08 stsp }
3440 6841da00 2019-08-08 stsp
3441 6841da00 2019-08-08 stsp static const struct got_error *
3442 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3443 6841da00 2019-08-08 stsp {
3444 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3445 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3446 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3447 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3448 6841da00 2019-08-08 stsp size_t linesize = 0;
3449 6841da00 2019-08-08 stsp ssize_t linelen;
3450 6841da00 2019-08-08 stsp
3451 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3452 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3453 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3454 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3455 6841da00 2019-08-08 stsp
3456 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3457 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3458 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3459 bd8de430 2019-10-04 stsp
3460 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3461 bd8de430 2019-10-04 stsp if (line[0] == '#')
3462 bd8de430 2019-10-04 stsp continue;
3463 bd8de430 2019-10-04 stsp
3464 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3465 bd8de430 2019-10-04 stsp if (line[0] == '!')
3466 bd8de430 2019-10-04 stsp continue;
3467 bd8de430 2019-10-04 stsp
3468 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3469 6841da00 2019-08-08 stsp line) == -1) {
3470 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3471 6841da00 2019-08-08 stsp goto done;
3472 6841da00 2019-08-08 stsp }
3473 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3474 6841da00 2019-08-08 stsp if (err)
3475 6841da00 2019-08-08 stsp goto done;
3476 6841da00 2019-08-08 stsp }
3477 6841da00 2019-08-08 stsp if (ferror(f)) {
3478 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3479 6841da00 2019-08-08 stsp goto done;
3480 6841da00 2019-08-08 stsp }
3481 6841da00 2019-08-08 stsp
3482 6841da00 2019-08-08 stsp dirpath = strdup(path);
3483 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3484 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3485 6841da00 2019-08-08 stsp goto done;
3486 6841da00 2019-08-08 stsp }
3487 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3488 6841da00 2019-08-08 stsp done:
3489 6841da00 2019-08-08 stsp free(line);
3490 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3491 6841da00 2019-08-08 stsp free(dirpath);
3492 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3493 6841da00 2019-08-08 stsp }
3494 6841da00 2019-08-08 stsp return err;
3495 6841da00 2019-08-08 stsp }
3496 6841da00 2019-08-08 stsp
3497 6841da00 2019-08-08 stsp int
3498 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3499 6841da00 2019-08-08 stsp {
3500 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3501 bd8de430 2019-10-04 stsp
3502 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3503 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3504 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3505 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3506 bd8de430 2019-10-04 stsp
3507 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3508 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3509 6841da00 2019-08-08 stsp
3510 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3511 bd8de430 2019-10-04 stsp continue;
3512 bd8de430 2019-10-04 stsp pattern += 3;
3513 bd8de430 2019-10-04 stsp p = path;
3514 bd8de430 2019-10-04 stsp while (*p) {
3515 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3516 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3517 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3518 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3519 bd8de430 2019-10-04 stsp p++;
3520 bd8de430 2019-10-04 stsp while (*p == '/')
3521 bd8de430 2019-10-04 stsp p++;
3522 bd8de430 2019-10-04 stsp continue;
3523 bd8de430 2019-10-04 stsp }
3524 bd8de430 2019-10-04 stsp return 1;
3525 bd8de430 2019-10-04 stsp }
3526 bd8de430 2019-10-04 stsp }
3527 bd8de430 2019-10-04 stsp }
3528 bd8de430 2019-10-04 stsp
3529 6841da00 2019-08-08 stsp /*
3530 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3531 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3532 6841da00 2019-08-08 stsp * ignores backwards.
3533 6841da00 2019-08-08 stsp */
3534 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3535 6841da00 2019-08-08 stsp while (pe) {
3536 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3537 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3538 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3539 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3540 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3541 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3542 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3543 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3544 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3545 6841da00 2019-08-08 stsp continue;
3546 6841da00 2019-08-08 stsp return 1;
3547 6841da00 2019-08-08 stsp }
3548 6841da00 2019-08-08 stsp }
3549 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3550 6841da00 2019-08-08 stsp }
3551 6841da00 2019-08-08 stsp
3552 6841da00 2019-08-08 stsp return 0;
3553 6841da00 2019-08-08 stsp }
3554 6841da00 2019-08-08 stsp
3555 6841da00 2019-08-08 stsp static const struct got_error *
3556 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3557 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3558 6841da00 2019-08-08 stsp {
3559 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3560 6841da00 2019-08-08 stsp char *ignorespath;
3561 886cec17 2019-12-15 stsp int fd = -1;
3562 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3563 6841da00 2019-08-08 stsp
3564 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3565 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3566 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3567 6841da00 2019-08-08 stsp
3568 886cec17 2019-12-15 stsp if (dirfd != -1) {
3569 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3570 886cec17 2019-12-15 stsp if (fd == -1) {
3571 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3572 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3573 886cec17 2019-12-15 stsp ignorespath);
3574 886cec17 2019-12-15 stsp } else {
3575 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3576 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3577 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3578 886cec17 2019-12-15 stsp ignorespath);
3579 886cec17 2019-12-15 stsp else {
3580 886cec17 2019-12-15 stsp fd = -1;
3581 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3582 886cec17 2019-12-15 stsp }
3583 886cec17 2019-12-15 stsp }
3584 886cec17 2019-12-15 stsp } else {
3585 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3586 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3587 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3588 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3589 886cec17 2019-12-15 stsp ignorespath);
3590 886cec17 2019-12-15 stsp } else
3591 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3592 886cec17 2019-12-15 stsp }
3593 6841da00 2019-08-08 stsp
3594 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3595 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3596 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3597 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3598 6841da00 2019-08-08 stsp free(ignorespath);
3599 6841da00 2019-08-08 stsp return err;
3600 f8d1f275 2019-02-04 stsp }
3601 f8d1f275 2019-02-04 stsp
3602 f8d1f275 2019-02-04 stsp static const struct got_error *
3603 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3604 f8d1f275 2019-02-04 stsp {
3605 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3606 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3607 f8d1f275 2019-02-04 stsp char *path = NULL;
3608 f8d1f275 2019-02-04 stsp
3609 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3610 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3611 0584f854 2019-04-06 stsp
3612 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3613 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3614 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3615 f8d1f275 2019-02-04 stsp } else {
3616 f8d1f275 2019-02-04 stsp path = de->d_name;
3617 f8d1f275 2019-02-04 stsp }
3618 f8d1f275 2019-02-04 stsp
3619 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3620 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3621 ff56836b 2021-07-08 stsp && !match_ignores(a->ignores, path))
3622 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3623 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3624 f8d1f275 2019-02-04 stsp if (parent_path[0])
3625 f8d1f275 2019-02-04 stsp free(path);
3626 b72f483a 2019-02-05 stsp return err;
3627 f8d1f275 2019-02-04 stsp }
3628 f8d1f275 2019-02-04 stsp
3629 347d1d3e 2019-07-12 stsp static const struct got_error *
3630 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3631 3143d852 2020-06-25 stsp {
3632 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3633 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3634 3143d852 2020-06-25 stsp
3635 3143d852 2020-06-25 stsp if (a->no_ignores)
3636 3143d852 2020-06-25 stsp return NULL;
3637 3143d852 2020-06-25 stsp
3638 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path,
3639 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3640 3143d852 2020-06-25 stsp if (err)
3641 3143d852 2020-06-25 stsp return err;
3642 3143d852 2020-06-25 stsp
3643 ff56836b 2021-07-08 stsp err = add_ignores(a->ignores, a->worktree->root_path, path,
3644 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3645 3143d852 2020-06-25 stsp
3646 3143d852 2020-06-25 stsp return err;
3647 3143d852 2020-06-25 stsp }
3648 3143d852 2020-06-25 stsp
3649 3143d852 2020-06-25 stsp static const struct got_error *
3650 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3651 ff56836b 2021-07-08 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3652 ff56836b 2021-07-08 stsp void *status_arg, struct got_repository *repo, int report_unchanged,
3653 0e33f8e0 2021-09-01 stsp struct got_pathlist_head *ignores, int no_ignores)
3654 abb4604f 2019-07-27 stsp {
3655 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3656 abb4604f 2019-07-27 stsp struct stat sb;
3657 abb4604f 2019-07-27 stsp
3658 0e33f8e0 2021-09-01 stsp if (!no_ignores && match_ignores(ignores, path))
3659 ff56836b 2021-07-08 stsp return NULL;
3660 ff56836b 2021-07-08 stsp
3661 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3662 abb4604f 2019-07-27 stsp if (ie)
3663 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3664 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3665 abb4604f 2019-07-27 stsp
3666 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3667 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3668 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3669 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3670 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3671 abb4604f 2019-07-27 stsp return NULL;
3672 abb4604f 2019-07-27 stsp }
3673 abb4604f 2019-07-27 stsp
3674 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3675 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3676 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3677 abb4604f 2019-07-27 stsp
3678 abb4604f 2019-07-27 stsp return NULL;
3679 3143d852 2020-06-25 stsp }
3680 3143d852 2020-06-25 stsp
3681 3143d852 2020-06-25 stsp static const struct got_error *
3682 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3683 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3684 3143d852 2020-06-25 stsp {
3685 3143d852 2020-06-25 stsp const struct got_error *err;
3686 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3687 3143d852 2020-06-25 stsp
3688 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3689 3143d852 2020-06-25 stsp ".cvsignore");
3690 3143d852 2020-06-25 stsp if (err)
3691 3143d852 2020-06-25 stsp return err;
3692 3143d852 2020-06-25 stsp
3693 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3694 3143d852 2020-06-25 stsp ".gitignore");
3695 3143d852 2020-06-25 stsp if (err)
3696 3143d852 2020-06-25 stsp return err;
3697 3143d852 2020-06-25 stsp
3698 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3699 3143d852 2020-06-25 stsp if (err) {
3700 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3701 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3702 3143d852 2020-06-25 stsp return err;
3703 3143d852 2020-06-25 stsp }
3704 3143d852 2020-06-25 stsp for (;;) {
3705 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3706 3143d852 2020-06-25 stsp ".cvsignore");
3707 3143d852 2020-06-25 stsp if (err)
3708 3143d852 2020-06-25 stsp break;
3709 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3710 3143d852 2020-06-25 stsp ".gitignore");
3711 3143d852 2020-06-25 stsp if (err)
3712 3143d852 2020-06-25 stsp break;
3713 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3714 3143d852 2020-06-25 stsp if (err) {
3715 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3716 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3717 3143d852 2020-06-25 stsp break;
3718 3143d852 2020-06-25 stsp }
3719 ff56836b 2021-07-08 stsp if (got_path_is_root_dir(parent_path))
3720 ff56836b 2021-07-08 stsp break;
3721 b737c85a 2020-06-26 stsp free(parent_path);
3722 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3723 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3724 3143d852 2020-06-25 stsp }
3725 3143d852 2020-06-25 stsp
3726 b737c85a 2020-06-26 stsp free(parent_path);
3727 b737c85a 2020-06-26 stsp free(next_parent_path);
3728 3143d852 2020-06-25 stsp return err;
3729 abb4604f 2019-07-27 stsp }
3730 abb4604f 2019-07-27 stsp
3731 abb4604f 2019-07-27 stsp static const struct got_error *
3732 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3733 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3734 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3735 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3736 f2a9dc41 2019-12-13 tracey int report_unchanged)
3737 f8d1f275 2019-02-04 stsp {
3738 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3739 6fc93f37 2019-12-13 stsp int fd = -1;
3740 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3741 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3742 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3743 ff56836b 2021-07-08 stsp struct got_pathlist_head ignores;
3744 3143d852 2020-06-25 stsp
3745 ff56836b 2021-07-08 stsp TAILQ_INIT(&ignores);
3746 f8d1f275 2019-02-04 stsp
3747 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3748 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3749 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3750 8dc303cc 2019-07-27 stsp
3751 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3752 6fc93f37 2019-12-13 stsp if (fd == -1) {
3753 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3754 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3755 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3756 ff56836b 2021-07-08 stsp else {
3757 ff56836b 2021-07-08 stsp if (!no_ignores) {
3758 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3759 ff56836b 2021-07-08 stsp worktree->root_path, ondisk_path);
3760 ff56836b 2021-07-08 stsp if (err)
3761 ff56836b 2021-07-08 stsp goto done;
3762 ff56836b 2021-07-08 stsp }
3763 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3764 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3765 0e33f8e0 2021-09-01 stsp report_unchanged, &ignores, no_ignores);
3766 ff56836b 2021-07-08 stsp }
3767 abb4604f 2019-07-27 stsp } else {
3768 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3769 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3770 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3771 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3772 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3773 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3774 abb4604f 2019-07-27 stsp arg.status_path = path;
3775 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3776 abb4604f 2019-07-27 stsp arg.repo = repo;
3777 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3778 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3779 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3780 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3781 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3782 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3783 022fae89 2019-12-06 tracey if (!no_ignores) {
3784 ff56836b 2021-07-08 stsp err = add_ignores_from_parent_paths(&ignores,
3785 3143d852 2020-06-25 stsp worktree->root_path, path);
3786 3143d852 2020-06-25 stsp if (err)
3787 3143d852 2020-06-25 stsp goto done;
3788 022fae89 2019-12-06 tracey }
3789 ff56836b 2021-07-08 stsp arg.ignores = &ignores;
3790 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3791 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3792 927df6b7 2019-02-10 stsp }
3793 3143d852 2020-06-25 stsp done:
3794 ff56836b 2021-07-08 stsp free_ignores(&ignores);
3795 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3796 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3797 927df6b7 2019-02-10 stsp free(ondisk_path);
3798 347d1d3e 2019-07-12 stsp return err;
3799 347d1d3e 2019-07-12 stsp }
3800 347d1d3e 2019-07-12 stsp
3801 347d1d3e 2019-07-12 stsp const struct got_error *
3802 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3803 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3804 f6343036 2021-06-22 stsp int no_ignores, got_worktree_status_cb status_cb, void *status_arg,
3805 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3806 347d1d3e 2019-07-12 stsp {
3807 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3808 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3809 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3810 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3811 347d1d3e 2019-07-12 stsp
3812 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3813 347d1d3e 2019-07-12 stsp if (err)
3814 347d1d3e 2019-07-12 stsp return err;
3815 347d1d3e 2019-07-12 stsp
3816 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3817 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3818 f6343036 2021-06-22 stsp status_cb, status_arg, cancel_cb, cancel_arg,
3819 f6343036 2021-06-22 stsp no_ignores, 0);
3820 72ea6654 2019-07-27 stsp if (err)
3821 72ea6654 2019-07-27 stsp break;
3822 72ea6654 2019-07-27 stsp }
3823 f8d1f275 2019-02-04 stsp free(fileindex_path);
3824 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3825 f8d1f275 2019-02-04 stsp return err;
3826 f8d1f275 2019-02-04 stsp }
3827 6c7ab921 2019-03-18 stsp
3828 6c7ab921 2019-03-18 stsp const struct got_error *
3829 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3830 6c7ab921 2019-03-18 stsp const char *arg)
3831 6c7ab921 2019-03-18 stsp {
3832 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3833 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3834 6c7ab921 2019-03-18 stsp size_t len;
3835 00bb5ea0 2020-07-23 stsp struct stat sb;
3836 01740607 2020-11-04 stsp char *abspath = NULL;
3837 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3838 6c7ab921 2019-03-18 stsp
3839 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3840 6c7ab921 2019-03-18 stsp
3841 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3842 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3843 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3844 00bb5ea0 2020-07-23 stsp
3845 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3846 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3847 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3848 00bb5ea0 2020-07-23 stsp goto done;
3849 00bb5ea0 2020-07-23 stsp }
3850 727173c3 2020-11-06 stsp sb.st_mode = 0;
3851 00bb5ea0 2020-07-23 stsp }
3852 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3853 00bb5ea0 2020-07-23 stsp /*
3854 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3855 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3856 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3857 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3858 00bb5ea0 2020-07-23 stsp */
3859 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3860 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3861 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3862 00bb5ea0 2020-07-23 stsp goto done;
3863 00bb5ea0 2020-07-23 stsp }
3864 00bb5ea0 2020-07-23 stsp
3865 00bb5ea0 2020-07-23 stsp }
3866 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3867 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3868 00bb5ea0 2020-07-23 stsp if (err)
3869 d0710d08 2019-07-22 stsp goto done;
3870 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3871 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3872 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3873 00bb5ea0 2020-07-23 stsp goto done;
3874 00bb5ea0 2020-07-23 stsp }
3875 00bb5ea0 2020-07-23 stsp } else {
3876 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3877 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3878 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3879 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3880 00bb5ea0 2020-07-23 stsp goto done;
3881 00bb5ea0 2020-07-23 stsp }
3882 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3883 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3884 01740607 2020-11-04 stsp goto done;
3885 01740607 2020-11-04 stsp }
3886 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3887 01740607 2020-11-04 stsp sizeof(canonpath));
3888 01740607 2020-11-04 stsp if (err)
3889 00bb5ea0 2020-07-23 stsp goto done;
3890 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3891 01740607 2020-11-04 stsp if (resolved == NULL) {
3892 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3893 01740607 2020-11-04 stsp goto done;
3894 00bb5ea0 2020-07-23 stsp }
3895 d0710d08 2019-07-22 stsp }
3896 d0710d08 2019-07-22 stsp }
3897 6c7ab921 2019-03-18 stsp
3898 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3899 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3900 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3901 6c7ab921 2019-03-18 stsp goto done;
3902 6c7ab921 2019-03-18 stsp }
3903 6c7ab921 2019-03-18 stsp
3904 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3905 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3906 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3907 f6d88e1a 2019-05-29 stsp if (err)
3908 f6d88e1a 2019-05-29 stsp goto done;
3909 f6d88e1a 2019-05-29 stsp } else {
3910 f6d88e1a 2019-05-29 stsp path = strdup("");
3911 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3912 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3913 f6d88e1a 2019-05-29 stsp goto done;
3914 f6d88e1a 2019-05-29 stsp }
3915 6c7ab921 2019-03-18 stsp }
3916 6c7ab921 2019-03-18 stsp
3917 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3918 6c7ab921 2019-03-18 stsp len = strlen(path);
3919 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3920 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3921 6c7ab921 2019-03-18 stsp len--;
3922 6c7ab921 2019-03-18 stsp }
3923 6c7ab921 2019-03-18 stsp done:
3924 01740607 2020-11-04 stsp free(abspath);
3925 6c7ab921 2019-03-18 stsp free(resolved);
3926 d0710d08 2019-07-22 stsp free(cwd);
3927 6c7ab921 2019-03-18 stsp if (err == NULL)
3928 6c7ab921 2019-03-18 stsp *wt_path = path;
3929 6c7ab921 2019-03-18 stsp else
3930 6c7ab921 2019-03-18 stsp free(path);
3931 d00136be 2019-03-26 stsp return err;
3932 d00136be 2019-03-26 stsp }
3933 d00136be 2019-03-26 stsp
3934 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3935 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3936 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3937 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3938 4e68cba3 2019-11-23 stsp void *progress_arg;
3939 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3940 4e68cba3 2019-11-23 stsp };
3941 4e68cba3 2019-11-23 stsp
3942 4e68cba3 2019-11-23 stsp static const struct got_error *
3943 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3944 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3945 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3946 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3947 07f5b47a 2019-06-02 stsp {
3948 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3949 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3950 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3951 6d022e97 2019-08-04 stsp struct stat sb;
3952 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3953 07f5b47a 2019-06-02 stsp
3954 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3955 dbb83fbd 2019-12-12 stsp relpath) == -1)
3956 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3957 dbb83fbd 2019-12-12 stsp
3958 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3959 6d022e97 2019-08-04 stsp if (ie) {
3960 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3961 12463d8b 2019-12-13 stsp de_name, a->repo);
3962 6d022e97 2019-08-04 stsp if (err)
3963 dbb83fbd 2019-12-12 stsp goto done;
3964 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3965 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3966 dbb83fbd 2019-12-12 stsp goto done;
3967 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3968 dbb83fbd 2019-12-12 stsp if (err)
3969 dbb83fbd 2019-12-12 stsp goto done;
3970 6d022e97 2019-08-04 stsp }
3971 07f5b47a 2019-06-02 stsp
3972 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3973 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3974 dbb83fbd 2019-12-12 stsp goto done;
3975 4e68cba3 2019-11-23 stsp }
3976 07f5b47a 2019-06-02 stsp
3977 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3978 4e68cba3 2019-11-23 stsp if (err)
3979 4e68cba3 2019-11-23 stsp goto done;
3980 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3981 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3982 3969253a 2020-03-07 stsp if (err) {
3983 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3984 3969253a 2020-03-07 stsp goto done;
3985 3969253a 2020-03-07 stsp }
3986 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3987 07f5b47a 2019-06-02 stsp if (err) {
3988 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3989 4e68cba3 2019-11-23 stsp goto done;
3990 07f5b47a 2019-06-02 stsp }
3991 4e68cba3 2019-11-23 stsp done:
3992 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3993 dbb83fbd 2019-12-12 stsp if (err)
3994 dbb83fbd 2019-12-12 stsp return err;
3995 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3996 dbb83fbd 2019-12-12 stsp return NULL;
3997 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3998 07f5b47a 2019-06-02 stsp }
3999 07f5b47a 2019-06-02 stsp
4000 d00136be 2019-03-26 stsp const struct got_error *
4001 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
4002 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
4003 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4004 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
4005 d00136be 2019-03-26 stsp {
4006 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4007 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
4008 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4009 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4010 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
4011 d00136be 2019-03-26 stsp
4012 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4013 d00136be 2019-03-26 stsp if (err)
4014 d00136be 2019-03-26 stsp return err;
4015 d00136be 2019-03-26 stsp
4016 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4017 d00136be 2019-03-26 stsp if (err)
4018 d00136be 2019-03-26 stsp goto done;
4019 d00136be 2019-03-26 stsp
4020 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
4021 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
4022 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
4023 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
4024 4e68cba3 2019-11-23 stsp saa.repo = repo;
4025 4e68cba3 2019-11-23 stsp
4026 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4027 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4028 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
4029 1dd54920 2019-05-11 stsp if (err)
4030 af12c6b9 2019-06-04 stsp break;
4031 1dd54920 2019-05-11 stsp }
4032 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4033 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4034 af12c6b9 2019-06-04 stsp err = sync_err;
4035 d00136be 2019-03-26 stsp done:
4036 fb399478 2019-07-12 stsp free(fileindex_path);
4037 d00136be 2019-03-26 stsp if (fileindex)
4038 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
4039 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4040 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
4041 d00136be 2019-03-26 stsp err = unlockerr;
4042 6c7ab921 2019-03-18 stsp return err;
4043 6c7ab921 2019-03-18 stsp }
4044 17ed4618 2019-06-02 stsp
4045 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
4046 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
4047 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
4048 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
4049 f2a9dc41 2019-12-13 tracey void *progress_arg;
4050 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
4051 f2a9dc41 2019-12-13 tracey int delete_local_mods;
4052 70e3e7f5 2019-12-13 tracey int keep_on_disk;
4053 766841c2 2020-08-13 stsp const char *status_codes;
4054 f2a9dc41 2019-12-13 tracey };
4055 f2a9dc41 2019-12-13 tracey
4056 f2a9dc41 2019-12-13 tracey static const struct got_error *
4057 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
4058 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
4059 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4060 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4061 17ed4618 2019-06-02 stsp {
4062 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
4063 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
4064 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
4065 17ed4618 2019-06-02 stsp struct stat sb;
4066 20a2ad1c 2020-10-20 stsp char *ondisk_path;
4067 17ed4618 2019-06-02 stsp
4068 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4069 17ed4618 2019-06-02 stsp if (ie == NULL)
4070 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4071 2ec1f75b 2019-03-26 stsp
4072 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
4073 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
4074 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4075 9acbc4fa 2019-08-03 stsp return NULL;
4076 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
4077 9acbc4fa 2019-08-03 stsp }
4078 9acbc4fa 2019-08-03 stsp
4079 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
4080 f2a9dc41 2019-12-13 tracey relpath) == -1)
4081 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
4082 f2a9dc41 2019-12-13 tracey
4083 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
4084 12463d8b 2019-12-13 stsp a->repo);
4085 17ed4618 2019-06-02 stsp if (err)
4086 f2a9dc41 2019-12-13 tracey goto done;
4087 17ed4618 2019-06-02 stsp
4088 766841c2 2020-08-13 stsp if (a->status_codes) {
4089 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
4090 766841c2 2020-08-13 stsp int i;
4091 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4092 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
4093 766841c2 2020-08-13 stsp break;
4094 766841c2 2020-08-13 stsp }
4095 766841c2 2020-08-13 stsp if (i == ncodes) {
4096 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
4097 766841c2 2020-08-13 stsp free(ondisk_path);
4098 766841c2 2020-08-13 stsp return NULL;
4099 766841c2 2020-08-13 stsp }
4100 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
4101 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
4102 766841c2 2020-08-13 stsp static char msg[64];
4103 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
4104 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
4105 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
4106 766841c2 2020-08-13 stsp goto done;
4107 766841c2 2020-08-13 stsp }
4108 766841c2 2020-08-13 stsp }
4109 766841c2 2020-08-13 stsp
4110 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
4111 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
4112 f2a9dc41 2019-12-13 tracey goto done;
4113 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
4114 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
4115 f2a9dc41 2019-12-13 tracey goto done;
4116 f2a9dc41 2019-12-13 tracey }
4117 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
4118 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
4119 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4120 f2a9dc41 2019-12-13 tracey goto done;
4121 f2a9dc41 2019-12-13 tracey }
4122 17ed4618 2019-06-02 stsp }
4123 17ed4618 2019-06-02 stsp
4124 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4125 20a2ad1c 2020-10-20 stsp size_t root_len;
4126 20a2ad1c 2020-10-20 stsp
4127 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4128 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
4129 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
4130 12463d8b 2019-12-13 stsp ondisk_path);
4131 12463d8b 2019-12-13 stsp goto done;
4132 12463d8b 2019-12-13 stsp }
4133 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
4134 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
4135 12463d8b 2019-12-13 stsp goto done;
4136 15341bfd 2020-03-05 tracey }
4137 15341bfd 2020-03-05 tracey
4138 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
4139 20a2ad1c 2020-10-20 stsp do {
4140 20a2ad1c 2020-10-20 stsp char *parent;
4141 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
4142 20a2ad1c 2020-10-20 stsp if (err)
4143 2513f20a 2020-10-20 stsp goto done;
4144 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4145 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4146 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4147 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4148 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4149 20a2ad1c 2020-10-20 stsp ondisk_path);
4150 15341bfd 2020-03-05 tracey break;
4151 15341bfd 2020-03-05 tracey }
4152 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4153 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4154 f2a9dc41 2019-12-13 tracey }
4155 17ed4618 2019-06-02 stsp
4156 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4157 f2a9dc41 2019-12-13 tracey done:
4158 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4159 f2a9dc41 2019-12-13 tracey if (err)
4160 f2a9dc41 2019-12-13 tracey return err;
4161 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4162 f2a9dc41 2019-12-13 tracey return NULL;
4163 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4164 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4165 17ed4618 2019-06-02 stsp }
4166 17ed4618 2019-06-02 stsp
4167 2ec1f75b 2019-03-26 stsp const struct got_error *
4168 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4169 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4170 766841c2 2020-08-13 stsp const char *status_codes,
4171 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4172 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4173 2ec1f75b 2019-03-26 stsp {
4174 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4175 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4176 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4177 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4178 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4179 2ec1f75b 2019-03-26 stsp
4180 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4181 2ec1f75b 2019-03-26 stsp if (err)
4182 2ec1f75b 2019-03-26 stsp return err;
4183 2ec1f75b 2019-03-26 stsp
4184 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4185 2ec1f75b 2019-03-26 stsp if (err)
4186 2ec1f75b 2019-03-26 stsp goto done;
4187 f2a9dc41 2019-12-13 tracey
4188 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4189 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4190 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4191 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4192 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4193 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4194 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4195 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4196 2ec1f75b 2019-03-26 stsp
4197 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4198 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4199 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4200 17ed4618 2019-06-02 stsp if (err)
4201 af12c6b9 2019-06-04 stsp break;
4202 2ec1f75b 2019-03-26 stsp }
4203 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4204 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4205 af12c6b9 2019-06-04 stsp err = sync_err;
4206 2ec1f75b 2019-03-26 stsp done:
4207 fb399478 2019-07-12 stsp free(fileindex_path);
4208 2ec1f75b 2019-03-26 stsp if (fileindex)
4209 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4210 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4211 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4212 2ec1f75b 2019-03-26 stsp err = unlockerr;
4213 33aa809d 2019-08-08 stsp return err;
4214 33aa809d 2019-08-08 stsp }
4215 33aa809d 2019-08-08 stsp
4216 33aa809d 2019-08-08 stsp static const struct got_error *
4217 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4218 33aa809d 2019-08-08 stsp {
4219 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4220 33aa809d 2019-08-08 stsp char *line = NULL;
4221 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4222 33aa809d 2019-08-08 stsp ssize_t linelen;
4223 33aa809d 2019-08-08 stsp
4224 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4225 33aa809d 2019-08-08 stsp if (linelen == -1) {
4226 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4227 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4228 33aa809d 2019-08-08 stsp goto done;
4229 33aa809d 2019-08-08 stsp }
4230 33aa809d 2019-08-08 stsp return NULL;
4231 33aa809d 2019-08-08 stsp }
4232 33aa809d 2019-08-08 stsp if (outfile) {
4233 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4234 33aa809d 2019-08-08 stsp if (n != linelen) {
4235 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4236 33aa809d 2019-08-08 stsp goto done;
4237 33aa809d 2019-08-08 stsp }
4238 33aa809d 2019-08-08 stsp }
4239 33aa809d 2019-08-08 stsp if (rejectfile) {
4240 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4241 33aa809d 2019-08-08 stsp if (n != linelen)
4242 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4243 33aa809d 2019-08-08 stsp }
4244 33aa809d 2019-08-08 stsp done:
4245 33aa809d 2019-08-08 stsp free(line);
4246 2ec1f75b 2019-03-26 stsp return err;
4247 2ec1f75b 2019-03-26 stsp }
4248 1f1abb7e 2019-08-08 stsp
4249 33aa809d 2019-08-08 stsp static const struct got_error *
4250 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4251 33aa809d 2019-08-08 stsp {
4252 33aa809d 2019-08-08 stsp char *line = NULL;
4253 33aa809d 2019-08-08 stsp size_t linesize = 0;
4254 33aa809d 2019-08-08 stsp ssize_t linelen;
4255 33aa809d 2019-08-08 stsp
4256 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4257 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4258 500467ff 2019-09-25 hiltjo if (ferror(f))
4259 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4260 500467ff 2019-09-25 hiltjo return NULL;
4261 500467ff 2019-09-25 hiltjo }
4262 33aa809d 2019-08-08 stsp free(line);
4263 33aa809d 2019-08-08 stsp return NULL;
4264 33aa809d 2019-08-08 stsp }
4265 33aa809d 2019-08-08 stsp
4266 33aa809d 2019-08-08 stsp static const struct got_error *
4267 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4268 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4269 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4270 abc59930 2021-09-05 naddy {
4271 33aa809d 2019-08-08 stsp const struct got_error *err;
4272 33aa809d 2019-08-08 stsp
4273 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4274 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4275 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4276 33aa809d 2019-08-08 stsp if (err)
4277 33aa809d 2019-08-08 stsp return err;
4278 33aa809d 2019-08-08 stsp (*line_cur1)++;
4279 33aa809d 2019-08-08 stsp }
4280 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4281 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4282 33aa809d 2019-08-08 stsp if (rejectfile)
4283 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4284 33aa809d 2019-08-08 stsp else
4285 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4286 33aa809d 2019-08-08 stsp if (err)
4287 33aa809d 2019-08-08 stsp return err;
4288 33aa809d 2019-08-08 stsp (*line_cur2)++;
4289 33aa809d 2019-08-08 stsp }
4290 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4291 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4292 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4293 33aa809d 2019-08-08 stsp if (err)
4294 33aa809d 2019-08-08 stsp return err;
4295 33aa809d 2019-08-08 stsp (*line_cur2)++;
4296 33aa809d 2019-08-08 stsp }
4297 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4298 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4299 33aa809d 2019-08-08 stsp if (rejectfile)
4300 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4301 33aa809d 2019-08-08 stsp else
4302 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4303 33aa809d 2019-08-08 stsp if (err)
4304 33aa809d 2019-08-08 stsp return err;
4305 33aa809d 2019-08-08 stsp (*line_cur1)++;
4306 33aa809d 2019-08-08 stsp }
4307 f1e81a05 2019-08-10 stsp
4308 f1e81a05 2019-08-10 stsp return NULL;
4309 f1e81a05 2019-08-10 stsp }
4310 f1e81a05 2019-08-10 stsp
4311 f1e81a05 2019-08-10 stsp static const struct got_error *
4312 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4313 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4314 f1e81a05 2019-08-10 stsp {
4315 f1e81a05 2019-08-10 stsp const struct got_error *err;
4316 f1e81a05 2019-08-10 stsp
4317 f1e81a05 2019-08-10 stsp if (outfile) {
4318 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4319 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4320 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4321 f1e81a05 2019-08-10 stsp if (err)
4322 f1e81a05 2019-08-10 stsp return err;
4323 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4324 f1e81a05 2019-08-10 stsp }
4325 f1e81a05 2019-08-10 stsp }
4326 f1e81a05 2019-08-10 stsp if (rejectfile) {
4327 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4328 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4329 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4330 f1e81a05 2019-08-10 stsp if (err)
4331 f1e81a05 2019-08-10 stsp return err;
4332 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4333 f1e81a05 2019-08-10 stsp }
4334 33aa809d 2019-08-08 stsp }
4335 33aa809d 2019-08-08 stsp
4336 33aa809d 2019-08-08 stsp return NULL;
4337 33aa809d 2019-08-08 stsp }
4338 33aa809d 2019-08-08 stsp
4339 33aa809d 2019-08-08 stsp static const struct got_error *
4340 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4341 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4342 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4343 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4344 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4345 33aa809d 2019-08-08 stsp {
4346 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4347 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4348 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4349 33aa809d 2019-08-08 stsp FILE *hunkfile;
4350 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4351 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4352 fe621944 2020-11-10 stsp int rc;
4353 33aa809d 2019-08-08 stsp
4354 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4355 33aa809d 2019-08-08 stsp
4356 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4357 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4358 fe621944 2020-11-10 stsp start_old = cc.left.start;
4359 fe621944 2020-11-10 stsp end_old = cc.left.end;
4360 fe621944 2020-11-10 stsp start_new = cc.right.start;
4361 fe621944 2020-11-10 stsp end_new = cc.right.end;
4362 33aa809d 2019-08-08 stsp
4363 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4364 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4365 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4366 33aa809d 2019-08-08 stsp
4367 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4368 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4369 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4370 33aa809d 2019-08-08 stsp
4371 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4372 fe621944 2020-11-10 stsp if (diff_state == NULL)
4373 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4374 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4375 fe621944 2020-11-10 stsp
4376 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4377 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4378 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4379 33aa809d 2019-08-08 stsp goto done;
4380 33aa809d 2019-08-08 stsp }
4381 fe621944 2020-11-10 stsp
4382 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4383 fe621944 2020-11-10 stsp diff_result, &cc);
4384 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4385 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4386 33aa809d 2019-08-08 stsp goto done;
4387 33aa809d 2019-08-08 stsp }
4388 fe621944 2020-11-10 stsp
4389 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4390 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4391 33aa809d 2019-08-08 stsp goto done;
4392 33aa809d 2019-08-08 stsp }
4393 33aa809d 2019-08-08 stsp
4394 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4395 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4396 33aa809d 2019-08-08 stsp if (err)
4397 33aa809d 2019-08-08 stsp goto done;
4398 33aa809d 2019-08-08 stsp
4399 33aa809d 2019-08-08 stsp switch (*choice) {
4400 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4401 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4402 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4403 33aa809d 2019-08-08 stsp break;
4404 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4405 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4406 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4407 33aa809d 2019-08-08 stsp break;
4408 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4409 33aa809d 2019-08-08 stsp break;
4410 33aa809d 2019-08-08 stsp default:
4411 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4412 33aa809d 2019-08-08 stsp break;
4413 33aa809d 2019-08-08 stsp }
4414 33aa809d 2019-08-08 stsp done:
4415 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4416 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4417 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4418 33aa809d 2019-08-08 stsp return err;
4419 33aa809d 2019-08-08 stsp }
4420 33aa809d 2019-08-08 stsp
4421 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4422 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4423 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4424 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4425 1f1abb7e 2019-08-08 stsp void *progress_arg;
4426 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4427 33aa809d 2019-08-08 stsp void *patch_arg;
4428 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4429 f259c4c1 2021-09-24 stsp int unlink_added_files;
4430 1f1abb7e 2019-08-08 stsp };
4431 a129376b 2019-03-28 stsp
4432 e20a8b6f 2019-06-04 stsp static const struct got_error *
4433 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4434 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4435 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4436 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4437 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4438 33aa809d 2019-08-08 stsp {
4439 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4440 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4441 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4442 1ebedb77 2019-10-19 stsp int fd2 = -1;
4443 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4444 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4445 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4446 fe621944 2020-11-10 stsp struct stat sb2;
4447 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4448 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4449 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4450 33aa809d 2019-08-08 stsp
4451 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4452 33aa809d 2019-08-08 stsp
4453 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4454 33aa809d 2019-08-08 stsp if (err)
4455 33aa809d 2019-08-08 stsp return err;
4456 33aa809d 2019-08-08 stsp
4457 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4458 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4459 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4460 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4461 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4462 fa3cef63 2020-07-23 stsp goto done;
4463 fa3cef63 2020-07-23 stsp }
4464 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4465 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4466 fa3cef63 2020-07-23 stsp if (link_len == -1)
4467 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4468 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4469 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4470 12463d8b 2019-12-13 stsp }
4471 12463d8b 2019-12-13 stsp } else {
4472 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4473 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4474 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4475 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4476 fa3cef63 2020-07-23 stsp goto done;
4477 fa3cef63 2020-07-23 stsp }
4478 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4479 fa3cef63 2020-07-23 stsp sizeof(link_target));
4480 fa3cef63 2020-07-23 stsp if (link_len == -1)
4481 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4482 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4483 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4484 12463d8b 2019-12-13 stsp }
4485 1ebedb77 2019-10-19 stsp }
4486 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4487 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4488 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4489 fa3cef63 2020-07-23 stsp goto done;
4490 fa3cef63 2020-07-23 stsp }
4491 1ebedb77 2019-10-19 stsp
4492 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4493 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4494 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4495 fa3cef63 2020-07-23 stsp goto done;
4496 fa3cef63 2020-07-23 stsp }
4497 fa3cef63 2020-07-23 stsp fd2 = -1;
4498 fa3cef63 2020-07-23 stsp } else {
4499 fa3cef63 2020-07-23 stsp size_t n;
4500 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4501 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4502 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4503 fa3cef63 2020-07-23 stsp goto done;
4504 fa3cef63 2020-07-23 stsp }
4505 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4506 fa3cef63 2020-07-23 stsp if (n != link_len) {
4507 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4508 fa3cef63 2020-07-23 stsp goto done;
4509 fa3cef63 2020-07-23 stsp }
4510 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4511 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4512 fa3cef63 2020-07-23 stsp goto done;
4513 fa3cef63 2020-07-23 stsp }
4514 fa3cef63 2020-07-23 stsp rewind(f2);
4515 33aa809d 2019-08-08 stsp }
4516 33aa809d 2019-08-08 stsp
4517 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4518 33aa809d 2019-08-08 stsp if (err)
4519 33aa809d 2019-08-08 stsp goto done;
4520 33aa809d 2019-08-08 stsp
4521 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4522 33aa809d 2019-08-08 stsp if (err)
4523 33aa809d 2019-08-08 stsp goto done;
4524 33aa809d 2019-08-08 stsp
4525 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4526 33aa809d 2019-08-08 stsp if (err)
4527 33aa809d 2019-08-08 stsp goto done;
4528 33aa809d 2019-08-08 stsp
4529 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4530 fe621944 2020-11-10 stsp NULL);
4531 33aa809d 2019-08-08 stsp if (err)
4532 33aa809d 2019-08-08 stsp goto done;
4533 33aa809d 2019-08-08 stsp
4534 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4535 33aa809d 2019-08-08 stsp if (err)
4536 33aa809d 2019-08-08 stsp goto done;
4537 33aa809d 2019-08-08 stsp
4538 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4539 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4540 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4541 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4542 fe621944 2020-11-10 stsp
4543 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4544 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4545 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4546 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4547 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4548 fe621944 2020-11-10 stsp nchanges++;
4549 fe621944 2020-11-10 stsp }
4550 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4551 33aa809d 2019-08-08 stsp int choice;
4552 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4553 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4554 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4555 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4556 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4557 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4558 33aa809d 2019-08-08 stsp if (err)
4559 33aa809d 2019-08-08 stsp goto done;
4560 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4561 33aa809d 2019-08-08 stsp have_content = 1;
4562 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4563 33aa809d 2019-08-08 stsp break;
4564 33aa809d 2019-08-08 stsp }
4565 1ebedb77 2019-10-19 stsp if (have_content) {
4566 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4567 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4568 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4569 1ebedb77 2019-10-19 stsp if (err)
4570 1ebedb77 2019-10-19 stsp goto done;
4571 1ebedb77 2019-10-19 stsp
4572 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4573 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4574 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4575 fa3cef63 2020-07-23 stsp goto done;
4576 fa3cef63 2020-07-23 stsp }
4577 1ebedb77 2019-10-19 stsp }
4578 1ebedb77 2019-10-19 stsp }
4579 33aa809d 2019-08-08 stsp done:
4580 33aa809d 2019-08-08 stsp free(id_str);
4581 33aa809d 2019-08-08 stsp if (blob)
4582 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4583 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4584 fe621944 2020-11-10 stsp if (err == NULL)
4585 fe621944 2020-11-10 stsp err = free_err;
4586 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4587 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4588 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4589 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4590 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4591 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4592 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4593 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4594 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4595 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4596 33aa809d 2019-08-08 stsp if (err || !have_content) {
4597 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4598 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4599 33aa809d 2019-08-08 stsp free(*path_outfile);
4600 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4601 33aa809d 2019-08-08 stsp }
4602 33aa809d 2019-08-08 stsp free(path1);
4603 33aa809d 2019-08-08 stsp return err;
4604 33aa809d 2019-08-08 stsp }
4605 33aa809d 2019-08-08 stsp
4606 33aa809d 2019-08-08 stsp static const struct got_error *
4607 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4608 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4609 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4610 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4611 a129376b 2019-03-28 stsp {
4612 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4613 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4614 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4615 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4616 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4617 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4618 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4619 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4620 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4621 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4622 a129376b 2019-03-28 stsp
4623 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4624 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4625 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4626 d3bcc3d1 2019-08-08 stsp return NULL;
4627 3d69ad8d 2019-08-17 semarie
4628 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4629 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4630 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4631 d3bcc3d1 2019-08-08 stsp
4632 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4633 65084dad 2019-08-08 stsp if (ie == NULL)
4634 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4635 a129376b 2019-03-28 stsp
4636 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4637 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4638 a129376b 2019-03-28 stsp if (err) {
4639 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4640 a129376b 2019-03-28 stsp goto done;
4641 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4642 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4643 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4644 e20a8b6f 2019-06-04 stsp goto done;
4645 e20a8b6f 2019-06-04 stsp }
4646 a129376b 2019-03-28 stsp }
4647 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4648 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4649 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4650 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4651 a129376b 2019-03-28 stsp goto done;
4652 a129376b 2019-03-28 stsp }
4653 a129376b 2019-03-28 stsp } else {
4654 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4655 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4656 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4657 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4658 a129376b 2019-03-28 stsp goto done;
4659 a129376b 2019-03-28 stsp }
4660 a129376b 2019-03-28 stsp } else {
4661 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4662 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4663 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4664 a129376b 2019-03-28 stsp goto done;
4665 a129376b 2019-03-28 stsp }
4666 a129376b 2019-03-28 stsp }
4667 a129376b 2019-03-28 stsp }
4668 a129376b 2019-03-28 stsp
4669 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4670 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4671 a9fa2909 2019-07-27 stsp if (err) {
4672 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4673 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4674 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4675 a9fa2909 2019-07-27 stsp goto done;
4676 a9fa2909 2019-07-27 stsp } else {
4677 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4678 a9fa2909 2019-07-27 stsp if (err)
4679 a9fa2909 2019-07-27 stsp goto done;
4680 a9fa2909 2019-07-27 stsp
4681 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4682 1233e6b6 2020-10-19 stsp if (err)
4683 a9fa2909 2019-07-27 stsp goto done;
4684 a9fa2909 2019-07-27 stsp
4685 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4686 1233e6b6 2020-10-19 stsp free(te_name);
4687 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4688 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4689 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4690 a9fa2909 2019-07-27 stsp goto done;
4691 a9fa2909 2019-07-27 stsp }
4692 a129376b 2019-03-28 stsp }
4693 a129376b 2019-03-28 stsp
4694 a129376b 2019-03-28 stsp switch (status) {
4695 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4696 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4697 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4698 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4699 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4700 33aa809d 2019-08-08 stsp if (err)
4701 33aa809d 2019-08-08 stsp goto done;
4702 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4703 33aa809d 2019-08-08 stsp break;
4704 33aa809d 2019-08-08 stsp }
4705 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4706 1f1abb7e 2019-08-08 stsp ie->path);
4707 1ee397ad 2019-07-12 stsp if (err)
4708 1ee397ad 2019-07-12 stsp goto done;
4709 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4710 f259c4c1 2021-09-24 stsp if (a->unlink_added_files) {
4711 f259c4c1 2021-09-24 stsp if (asprintf(&ondisk_path, "%s/%s",
4712 f259c4c1 2021-09-24 stsp got_worktree_get_root_path(a->worktree),
4713 f259c4c1 2021-09-24 stsp relpath) == -1) {
4714 f259c4c1 2021-09-24 stsp err = got_error_from_errno("asprintf");
4715 f259c4c1 2021-09-24 stsp goto done;
4716 f259c4c1 2021-09-24 stsp }
4717 f259c4c1 2021-09-24 stsp if (unlink(ondisk_path) == -1) {
4718 f259c4c1 2021-09-24 stsp err = got_error_from_errno2("unlink",
4719 f259c4c1 2021-09-24 stsp ondisk_path);
4720 f259c4c1 2021-09-24 stsp break;
4721 f259c4c1 2021-09-24 stsp }
4722 f259c4c1 2021-09-24 stsp }
4723 a129376b 2019-03-28 stsp break;
4724 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4725 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4726 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4727 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4728 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4729 33aa809d 2019-08-08 stsp if (err)
4730 33aa809d 2019-08-08 stsp goto done;
4731 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4732 33aa809d 2019-08-08 stsp break;
4733 33aa809d 2019-08-08 stsp }
4734 33aa809d 2019-08-08 stsp /* fall through */
4735 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4736 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4737 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4738 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4739 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4740 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4741 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4742 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4743 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4744 24278f30 2019-08-03 stsp } else
4745 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4746 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4747 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4748 a129376b 2019-03-28 stsp if (err)
4749 65084dad 2019-08-08 stsp goto done;
4750 65084dad 2019-08-08 stsp
4751 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4752 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4753 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4754 a129376b 2019-03-28 stsp goto done;
4755 65084dad 2019-08-08 stsp }
4756 65084dad 2019-08-08 stsp
4757 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4758 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4759 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4760 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4761 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4762 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4763 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4764 33aa809d 2019-08-08 stsp break;
4765 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4766 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4767 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4768 369fd7e5 2020-07-23 stsp path_content);
4769 369fd7e5 2020-07-23 stsp break;
4770 369fd7e5 2020-07-23 stsp }
4771 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4772 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4773 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4774 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4775 369fd7e5 2020-07-23 stsp } else {
4776 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4777 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4778 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4779 369fd7e5 2020-07-23 stsp goto done;
4780 369fd7e5 2020-07-23 stsp }
4781 33aa809d 2019-08-08 stsp }
4782 33aa809d 2019-08-08 stsp } else {
4783 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4784 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4785 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4786 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4787 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4788 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4789 2e1fa222 2020-07-23 stsp } else {
4790 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4791 2e1fa222 2020-07-23 stsp ie->path,
4792 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4793 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4794 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4795 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4796 2e1fa222 2020-07-23 stsp }
4797 a129376b 2019-03-28 stsp if (err)
4798 a129376b 2019-03-28 stsp goto done;
4799 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4800 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4801 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4802 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4803 437adc9d 2020-12-10 yzhong blob->id.sha1,
4804 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4805 2e1fa222 2020-07-23 stsp if (err)
4806 2e1fa222 2020-07-23 stsp goto done;
4807 2e1fa222 2020-07-23 stsp }
4808 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4809 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4810 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4811 33aa809d 2019-08-08 stsp }
4812 a129376b 2019-03-28 stsp }
4813 a129376b 2019-03-28 stsp break;
4814 e20a8b6f 2019-06-04 stsp }
4815 a129376b 2019-03-28 stsp default:
4816 1f1abb7e 2019-08-08 stsp break;
4817 a129376b 2019-03-28 stsp }
4818 a129376b 2019-03-28 stsp done:
4819 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4820 33aa809d 2019-08-08 stsp free(path_content);
4821 e20a8b6f 2019-06-04 stsp free(parent_path);
4822 a129376b 2019-03-28 stsp free(tree_path);
4823 a129376b 2019-03-28 stsp if (blob)
4824 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4825 a129376b 2019-03-28 stsp if (tree)
4826 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4827 a129376b 2019-03-28 stsp free(tree_id);
4828 e20a8b6f 2019-06-04 stsp return err;
4829 e20a8b6f 2019-06-04 stsp }
4830 e20a8b6f 2019-06-04 stsp
4831 e20a8b6f 2019-06-04 stsp const struct got_error *
4832 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4833 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4834 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4835 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4836 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4837 e20a8b6f 2019-06-04 stsp {
4838 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4839 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4840 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4841 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4842 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4843 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4844 e20a8b6f 2019-06-04 stsp
4845 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4846 e20a8b6f 2019-06-04 stsp if (err)
4847 e20a8b6f 2019-06-04 stsp return err;
4848 e20a8b6f 2019-06-04 stsp
4849 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4850 e20a8b6f 2019-06-04 stsp if (err)
4851 e20a8b6f 2019-06-04 stsp goto done;
4852 e20a8b6f 2019-06-04 stsp
4853 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4854 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4855 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4856 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4857 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4858 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4859 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4860 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
4861 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4862 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4863 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4864 e20a8b6f 2019-06-04 stsp if (err)
4865 af12c6b9 2019-06-04 stsp break;
4866 e20a8b6f 2019-06-04 stsp }
4867 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4868 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4869 e20a8b6f 2019-06-04 stsp err = sync_err;
4870 af12c6b9 2019-06-04 stsp done:
4871 fb399478 2019-07-12 stsp free(fileindex_path);
4872 a129376b 2019-03-28 stsp if (fileindex)
4873 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4874 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4875 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4876 a129376b 2019-03-28 stsp err = unlockerr;
4877 c4296144 2019-05-09 stsp return err;
4878 c4296144 2019-05-09 stsp }
4879 c4296144 2019-05-09 stsp
4880 cf066bf8 2019-05-09 stsp static void
4881 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4882 cf066bf8 2019-05-09 stsp {
4883 24519714 2019-05-09 stsp free(ct->path);
4884 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4885 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4886 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4887 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4888 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4889 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4890 cf066bf8 2019-05-09 stsp free(ct);
4891 cf066bf8 2019-05-09 stsp }
4892 24519714 2019-05-09 stsp
4893 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4894 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4895 24519714 2019-05-09 stsp struct got_repository *repo;
4896 24519714 2019-05-09 stsp struct got_worktree *worktree;
4897 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4898 5f8a88c6 2019-08-03 stsp int have_staged_files;
4899 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4900 24519714 2019-05-09 stsp };
4901 cf066bf8 2019-05-09 stsp
4902 c4296144 2019-05-09 stsp static const struct got_error *
4903 dae2a678 2021-09-01 stsp collect_commitables(void *arg, unsigned char status,
4904 dae2a678 2021-09-01 stsp unsigned char staged_status, const char *relpath,
4905 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4906 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4907 c4296144 2019-05-09 stsp {
4908 dae2a678 2021-09-01 stsp struct collect_commitables_arg *a = arg;
4909 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4910 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4911 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4912 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4913 768aea60 2019-05-09 stsp struct stat sb;
4914 c4296144 2019-05-09 stsp
4915 dae2a678 2021-09-01 stsp if (a->have_staged_files) {
4916 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4917 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4918 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4919 5f8a88c6 2019-08-03 stsp return NULL;
4920 5f8a88c6 2019-08-03 stsp } else {
4921 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4922 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4923 c4296144 2019-05-09 stsp
4924 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4925 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4926 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4927 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4928 5f8a88c6 2019-08-03 stsp return NULL;
4929 5f8a88c6 2019-08-03 stsp }
4930 0b5cc0d6 2019-05-09 stsp
4931 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4932 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4933 036813ee 2019-05-09 stsp goto done;
4934 036813ee 2019-05-09 stsp }
4935 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4936 036813ee 2019-05-09 stsp parent_path = strdup("");
4937 69960a46 2019-05-09 stsp if (parent_path == NULL)
4938 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4939 69960a46 2019-05-09 stsp } else {
4940 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4941 69960a46 2019-05-09 stsp if (err)
4942 69960a46 2019-05-09 stsp return err;
4943 69960a46 2019-05-09 stsp }
4944 c4296144 2019-05-09 stsp
4945 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4946 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4947 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4948 c4296144 2019-05-09 stsp goto done;
4949 768aea60 2019-05-09 stsp }
4950 768aea60 2019-05-09 stsp
4951 dae2a678 2021-09-01 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4952 768aea60 2019-05-09 stsp relpath) == -1) {
4953 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4954 768aea60 2019-05-09 stsp goto done;
4955 768aea60 2019-05-09 stsp }
4956 0aeb8099 2020-07-23 stsp
4957 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4958 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4959 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4960 dae2a678 2021-09-01 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4961 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4962 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4963 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4964 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4965 0aeb8099 2020-07-23 stsp break;
4966 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4967 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4968 0aeb8099 2020-07-23 stsp break;
4969 0aeb8099 2020-07-23 stsp default:
4970 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4971 0aeb8099 2020-07-23 stsp goto done;
4972 0aeb8099 2020-07-23 stsp }
4973 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4974 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4975 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4976 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4977 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4978 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4979 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4980 12463d8b 2019-12-13 stsp ct->ondisk_path);
4981 12463d8b 2019-12-13 stsp goto done;
4982 12463d8b 2019-12-13 stsp }
4983 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4984 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4985 768aea60 2019-05-09 stsp goto done;
4986 768aea60 2019-05-09 stsp }
4987 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4988 c4296144 2019-05-09 stsp }
4989 c4296144 2019-05-09 stsp
4990 dae2a678 2021-09-01 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4991 dae2a678 2021-09-01 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4992 44d03001 2019-05-09 stsp relpath) == -1) {
4993 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4994 44d03001 2019-05-09 stsp goto done;
4995 44d03001 2019-05-09 stsp }
4996 44d03001 2019-05-09 stsp
4997 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4998 dae2a678 2021-09-01 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4999 35213c7c 2020-07-23 stsp int is_bad_symlink;
5000 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
5001 35213c7c 2020-07-23 stsp ssize_t target_len;
5002 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
5003 35213c7c 2020-07-23 stsp sizeof(target_path));
5004 35213c7c 2020-07-23 stsp if (target_len == -1) {
5005 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
5006 35213c7c 2020-07-23 stsp ct->ondisk_path);
5007 35213c7c 2020-07-23 stsp goto done;
5008 35213c7c 2020-07-23 stsp }
5009 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
5010 dae2a678 2021-09-01 stsp target_len, ct->ondisk_path, a->worktree->root_path);
5011 35213c7c 2020-07-23 stsp if (err)
5012 35213c7c 2020-07-23 stsp goto done;
5013 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
5014 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
5015 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
5016 35213c7c 2020-07-23 stsp goto done;
5017 35213c7c 2020-07-23 stsp }
5018 35213c7c 2020-07-23 stsp }
5019 35213c7c 2020-07-23 stsp
5020 35213c7c 2020-07-23 stsp
5021 cf066bf8 2019-05-09 stsp ct->status = status;
5022 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
5023 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
5024 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
5025 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
5026 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
5027 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
5028 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5029 b416585c 2019-05-13 stsp goto done;
5030 b416585c 2019-05-13 stsp }
5031 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
5032 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
5033 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
5034 f0b75401 2019-08-03 stsp goto done;
5035 f0b75401 2019-08-03 stsp }
5036 f0b75401 2019-08-03 stsp }
5037 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5038 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5039 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
5040 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
5041 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5042 036813ee 2019-05-09 stsp goto done;
5043 036813ee 2019-05-09 stsp }
5044 ca2503ea 2019-05-09 stsp }
5045 24519714 2019-05-09 stsp ct->path = strdup(path);
5046 24519714 2019-05-09 stsp if (ct->path == NULL) {
5047 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
5048 24519714 2019-05-09 stsp goto done;
5049 24519714 2019-05-09 stsp }
5050 dae2a678 2021-09-01 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
5051 c4296144 2019-05-09 stsp done:
5052 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
5053 ed175427 2019-05-09 stsp free_commitable(ct);
5054 24519714 2019-05-09 stsp free(parent_path);
5055 036813ee 2019-05-09 stsp free(path);
5056 a129376b 2019-03-28 stsp return err;
5057 a129376b 2019-03-28 stsp }
5058 c4296144 2019-05-09 stsp
5059 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
5060 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
5061 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5062 036813ee 2019-05-09 stsp struct got_repository *);
5063 ed175427 2019-05-09 stsp
5064 ed175427 2019-05-09 stsp static const struct got_error *
5065 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
5066 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
5067 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5068 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5069 afa376bf 2019-05-09 stsp struct got_repository *repo)
5070 ed175427 2019-05-09 stsp {
5071 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
5072 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
5073 036813ee 2019-05-09 stsp char *subpath;
5074 ed175427 2019-05-09 stsp
5075 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
5076 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
5077 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5078 ed175427 2019-05-09 stsp
5079 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
5080 ed175427 2019-05-09 stsp if (err)
5081 ed175427 2019-05-09 stsp return err;
5082 ed175427 2019-05-09 stsp
5083 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
5084 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5085 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
5086 036813ee 2019-05-09 stsp free(subpath);
5087 036813ee 2019-05-09 stsp return err;
5088 036813ee 2019-05-09 stsp }
5089 ed175427 2019-05-09 stsp
5090 036813ee 2019-05-09 stsp static const struct got_error *
5091 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
5092 036813ee 2019-05-09 stsp {
5093 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5094 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
5095 ed175427 2019-05-09 stsp
5096 036813ee 2019-05-09 stsp *match = 0;
5097 ed175427 2019-05-09 stsp
5098 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
5099 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
5100 0f63689d 2019-05-10 stsp return NULL;
5101 036813ee 2019-05-09 stsp }
5102 0b5cc0d6 2019-05-09 stsp
5103 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
5104 0f63689d 2019-05-10 stsp if (err)
5105 0f63689d 2019-05-10 stsp return err;
5106 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
5107 036813ee 2019-05-09 stsp free(ct_parent_path);
5108 036813ee 2019-05-09 stsp return err;
5109 036813ee 2019-05-09 stsp }
5110 0b5cc0d6 2019-05-09 stsp
5111 768aea60 2019-05-09 stsp static mode_t
5112 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
5113 768aea60 2019-05-09 stsp {
5114 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
5115 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
5116 3d9a4ec4 2020-07-23 stsp
5117 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
5118 768aea60 2019-05-09 stsp }
5119 768aea60 2019-05-09 stsp
5120 036813ee 2019-05-09 stsp static const struct got_error *
5121 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
5122 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
5123 036813ee 2019-05-09 stsp {
5124 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5125 ca2503ea 2019-05-09 stsp
5126 036813ee 2019-05-09 stsp *new_te = NULL;
5127 0b5cc0d6 2019-05-09 stsp
5128 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
5129 036813ee 2019-05-09 stsp if (err)
5130 036813ee 2019-05-09 stsp goto done;
5131 0b5cc0d6 2019-05-09 stsp
5132 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5133 036813ee 2019-05-09 stsp
5134 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
5135 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5136 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5137 5f8a88c6 2019-08-03 stsp else
5138 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5139 036813ee 2019-05-09 stsp done:
5140 036813ee 2019-05-09 stsp if (err && *new_te) {
5141 56e0773d 2019-11-28 stsp free(*new_te);
5142 036813ee 2019-05-09 stsp *new_te = NULL;
5143 036813ee 2019-05-09 stsp }
5144 036813ee 2019-05-09 stsp return err;
5145 036813ee 2019-05-09 stsp }
5146 036813ee 2019-05-09 stsp
5147 036813ee 2019-05-09 stsp static const struct got_error *
5148 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5149 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
5150 036813ee 2019-05-09 stsp {
5151 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5152 102b254e 2020-10-19 stsp char *ct_name = NULL;
5153 036813ee 2019-05-09 stsp
5154 036813ee 2019-05-09 stsp *new_te = NULL;
5155 036813ee 2019-05-09 stsp
5156 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
5157 036813ee 2019-05-09 stsp if (*new_te == NULL)
5158 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5159 036813ee 2019-05-09 stsp
5160 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5161 102b254e 2020-10-19 stsp if (err)
5162 036813ee 2019-05-09 stsp goto done;
5163 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5164 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5165 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5166 036813ee 2019-05-09 stsp goto done;
5167 036813ee 2019-05-09 stsp }
5168 036813ee 2019-05-09 stsp
5169 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5170 036813ee 2019-05-09 stsp
5171 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5172 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5173 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5174 5f8a88c6 2019-08-03 stsp else
5175 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5176 036813ee 2019-05-09 stsp done:
5177 102b254e 2020-10-19 stsp free(ct_name);
5178 036813ee 2019-05-09 stsp if (err && *new_te) {
5179 56e0773d 2019-11-28 stsp free(*new_te);
5180 036813ee 2019-05-09 stsp *new_te = NULL;
5181 036813ee 2019-05-09 stsp }
5182 036813ee 2019-05-09 stsp return err;
5183 036813ee 2019-05-09 stsp }
5184 036813ee 2019-05-09 stsp
5185 036813ee 2019-05-09 stsp static const struct got_error *
5186 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5187 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5188 036813ee 2019-05-09 stsp {
5189 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5190 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5191 036813ee 2019-05-09 stsp
5192 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5193 036813ee 2019-05-09 stsp if (err)
5194 036813ee 2019-05-09 stsp return err;
5195 036813ee 2019-05-09 stsp if (new_pe == NULL)
5196 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5197 036813ee 2019-05-09 stsp return NULL;
5198 afa376bf 2019-05-09 stsp }
5199 afa376bf 2019-05-09 stsp
5200 afa376bf 2019-05-09 stsp static const struct got_error *
5201 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5202 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5203 afa376bf 2019-05-09 stsp {
5204 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5205 5f8a88c6 2019-08-03 stsp unsigned char status;
5206 5f8a88c6 2019-08-03 stsp
5207 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5208 afa376bf 2019-05-09 stsp ct_path++;
5209 5f8a88c6 2019-08-03 stsp
5210 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5211 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5212 5f8a88c6 2019-08-03 stsp else
5213 5f8a88c6 2019-08-03 stsp status = ct->status;
5214 5f8a88c6 2019-08-03 stsp
5215 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5216 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5217 036813ee 2019-05-09 stsp }
5218 036813ee 2019-05-09 stsp
5219 036813ee 2019-05-09 stsp static const struct got_error *
5220 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5221 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5222 44d03001 2019-05-09 stsp {
5223 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5224 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5225 44d03001 2019-05-09 stsp char *te_path;
5226 44d03001 2019-05-09 stsp
5227 44d03001 2019-05-09 stsp *modified = 0;
5228 44d03001 2019-05-09 stsp
5229 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5230 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5231 44d03001 2019-05-09 stsp te->name) == -1)
5232 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5233 44d03001 2019-05-09 stsp
5234 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5235 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5236 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5237 44d03001 2019-05-09 stsp strlen(te_path));
5238 62d463ca 2020-10-20 naddy if (*modified)
5239 44d03001 2019-05-09 stsp break;
5240 44d03001 2019-05-09 stsp }
5241 44d03001 2019-05-09 stsp
5242 44d03001 2019-05-09 stsp free(te_path);
5243 44d03001 2019-05-09 stsp return err;
5244 44d03001 2019-05-09 stsp }
5245 44d03001 2019-05-09 stsp
5246 44d03001 2019-05-09 stsp static const struct got_error *
5247 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5248 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5249 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5250 036813ee 2019-05-09 stsp {
5251 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5252 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5253 036813ee 2019-05-09 stsp
5254 036813ee 2019-05-09 stsp *ctp = NULL;
5255 036813ee 2019-05-09 stsp
5256 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5257 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5258 036813ee 2019-05-09 stsp char *ct_name = NULL;
5259 036813ee 2019-05-09 stsp int path_matches;
5260 036813ee 2019-05-09 stsp
5261 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5262 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5263 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5264 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5265 5f8a88c6 2019-08-03 stsp continue;
5266 5f8a88c6 2019-08-03 stsp } else {
5267 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5268 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5269 5f8a88c6 2019-08-03 stsp continue;
5270 5f8a88c6 2019-08-03 stsp }
5271 036813ee 2019-05-09 stsp
5272 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5273 036813ee 2019-05-09 stsp continue;
5274 036813ee 2019-05-09 stsp
5275 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5276 62d463ca 2020-10-20 naddy if (err)
5277 036813ee 2019-05-09 stsp return err;
5278 036813ee 2019-05-09 stsp if (!path_matches)
5279 036813ee 2019-05-09 stsp continue;
5280 036813ee 2019-05-09 stsp
5281 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5282 d34b633e 2020-10-19 stsp if (err)
5283 d34b633e 2020-10-19 stsp return err;
5284 d34b633e 2020-10-19 stsp
5285 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5286 d34b633e 2020-10-19 stsp free(ct_name);
5287 036813ee 2019-05-09 stsp continue;
5288 d34b633e 2020-10-19 stsp }
5289 d34b633e 2020-10-19 stsp free(ct_name);
5290 036813ee 2019-05-09 stsp
5291 036813ee 2019-05-09 stsp *ctp = ct;
5292 036813ee 2019-05-09 stsp break;
5293 036813ee 2019-05-09 stsp }
5294 2b496619 2019-07-10 stsp
5295 2b496619 2019-07-10 stsp return err;
5296 2b496619 2019-07-10 stsp }
5297 2b496619 2019-07-10 stsp
5298 2b496619 2019-07-10 stsp static const struct got_error *
5299 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5300 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5301 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5302 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5303 2b496619 2019-07-10 stsp struct got_repository *repo)
5304 2b496619 2019-07-10 stsp {
5305 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5306 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5307 2b496619 2019-07-10 stsp char *subtree_path;
5308 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5309 ba580f68 2020-03-22 stsp int nentries;
5310 2b496619 2019-07-10 stsp
5311 2b496619 2019-07-10 stsp *new_tep = NULL;
5312 2b496619 2019-07-10 stsp
5313 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5314 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5315 2b496619 2019-07-10 stsp child_path) == -1)
5316 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5317 036813ee 2019-05-09 stsp
5318 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5319 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5320 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5321 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5322 56e0773d 2019-11-28 stsp
5323 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5324 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5325 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5326 2b496619 2019-07-10 stsp goto done;
5327 2b496619 2019-07-10 stsp }
5328 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5329 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5330 2b496619 2019-07-10 stsp if (err) {
5331 56e0773d 2019-11-28 stsp free(new_te);
5332 2b496619 2019-07-10 stsp goto done;
5333 2b496619 2019-07-10 stsp }
5334 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5335 2b496619 2019-07-10 stsp done:
5336 56e0773d 2019-11-28 stsp free(id);
5337 2b496619 2019-07-10 stsp free(subtree_path);
5338 2b496619 2019-07-10 stsp if (err == NULL)
5339 2b496619 2019-07-10 stsp *new_tep = new_te;
5340 036813ee 2019-05-09 stsp return err;
5341 036813ee 2019-05-09 stsp }
5342 036813ee 2019-05-09 stsp
5343 036813ee 2019-05-09 stsp static const struct got_error *
5344 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5345 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5346 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5347 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5348 036813ee 2019-05-09 stsp struct got_repository *repo)
5349 036813ee 2019-05-09 stsp {
5350 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5351 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5352 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5353 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5354 036813ee 2019-05-09 stsp
5355 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5356 ba580f68 2020-03-22 stsp *nentries = 0;
5357 036813ee 2019-05-09 stsp
5358 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5359 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5360 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5361 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5362 036813ee 2019-05-09 stsp
5363 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5364 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5365 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5366 036813ee 2019-05-09 stsp continue;
5367 036813ee 2019-05-09 stsp
5368 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5369 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5370 036813ee 2019-05-09 stsp continue;
5371 036813ee 2019-05-09 stsp
5372 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5373 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5374 036813ee 2019-05-09 stsp if (err)
5375 036813ee 2019-05-09 stsp goto done;
5376 036813ee 2019-05-09 stsp
5377 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5378 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5379 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5380 036813ee 2019-05-09 stsp if (err)
5381 036813ee 2019-05-09 stsp goto done;
5382 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5383 afa376bf 2019-05-09 stsp if (err)
5384 afa376bf 2019-05-09 stsp goto done;
5385 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5386 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5387 2b496619 2019-07-10 stsp if (err)
5388 2f51b5b3 2019-05-09 stsp goto done;
5389 ba580f68 2020-03-22 stsp (*nentries)++;
5390 2b496619 2019-07-10 stsp } else {
5391 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5392 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5393 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5394 2b496619 2019-07-10 stsp == NULL) {
5395 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5396 2b496619 2019-07-10 stsp child_path, path_base_tree,
5397 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5398 2b496619 2019-07-10 stsp repo);
5399 2b496619 2019-07-10 stsp if (err)
5400 2b496619 2019-07-10 stsp goto done;
5401 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5402 2b496619 2019-07-10 stsp if (err)
5403 2b496619 2019-07-10 stsp goto done;
5404 ba580f68 2020-03-22 stsp (*nentries)++;
5405 9ba0479c 2019-05-10 stsp }
5406 ed175427 2019-05-09 stsp }
5407 2f51b5b3 2019-05-09 stsp }
5408 2f51b5b3 2019-05-09 stsp
5409 2f51b5b3 2019-05-09 stsp if (base_tree) {
5410 56e0773d 2019-11-28 stsp int i, nbase_entries;
5411 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5412 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5413 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5414 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5415 63c5ca5d 2019-08-24 stsp
5416 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5417 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5418 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5419 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5420 63c5ca5d 2019-08-24 stsp if (err)
5421 63c5ca5d 2019-08-24 stsp goto done;
5422 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5423 63c5ca5d 2019-08-24 stsp if (err)
5424 63c5ca5d 2019-08-24 stsp goto done;
5425 ba580f68 2020-03-22 stsp (*nentries)++;
5426 63c5ca5d 2019-08-24 stsp continue;
5427 63c5ca5d 2019-08-24 stsp }
5428 2f51b5b3 2019-05-09 stsp
5429 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5430 44d03001 2019-05-09 stsp int modified;
5431 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5432 036813ee 2019-05-09 stsp if (err)
5433 036813ee 2019-05-09 stsp goto done;
5434 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5435 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5436 2f51b5b3 2019-05-09 stsp if (err)
5437 2f51b5b3 2019-05-09 stsp goto done;
5438 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5439 44d03001 2019-05-09 stsp if (modified) {
5440 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5441 ba580f68 2020-03-22 stsp int nsubentries;
5442 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5443 ba580f68 2020-03-22 stsp &nsubentries, te,
5444 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5445 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5446 44d03001 2019-05-09 stsp if (err)
5447 44d03001 2019-05-09 stsp goto done;
5448 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5449 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5450 ba580f68 2020-03-22 stsp free(new_id);
5451 ba580f68 2020-03-22 stsp continue;
5452 ba580f68 2020-03-22 stsp }
5453 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5454 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5455 56e0773d 2019-11-28 stsp free(new_id);
5456 44d03001 2019-05-09 stsp }
5457 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5458 036813ee 2019-05-09 stsp if (err)
5459 036813ee 2019-05-09 stsp goto done;
5460 ba580f68 2020-03-22 stsp (*nentries)++;
5461 2f51b5b3 2019-05-09 stsp continue;
5462 036813ee 2019-05-09 stsp }
5463 2f51b5b3 2019-05-09 stsp
5464 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5465 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5466 f66c734c 2019-09-22 stsp if (err)
5467 f66c734c 2019-09-22 stsp goto done;
5468 2f51b5b3 2019-05-09 stsp if (ct) {
5469 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5470 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5471 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5472 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5473 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5474 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5475 2f51b5b3 2019-05-09 stsp if (err)
5476 2f51b5b3 2019-05-09 stsp goto done;
5477 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5478 2f51b5b3 2019-05-09 stsp if (err)
5479 2f51b5b3 2019-05-09 stsp goto done;
5480 ba580f68 2020-03-22 stsp (*nentries)++;
5481 2f51b5b3 2019-05-09 stsp }
5482 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5483 b416585c 2019-05-13 stsp status_arg);
5484 afa376bf 2019-05-09 stsp if (err)
5485 afa376bf 2019-05-09 stsp goto done;
5486 2f51b5b3 2019-05-09 stsp } else {
5487 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5488 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5489 2f51b5b3 2019-05-09 stsp if (err)
5490 2f51b5b3 2019-05-09 stsp goto done;
5491 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5492 2f51b5b3 2019-05-09 stsp if (err)
5493 2f51b5b3 2019-05-09 stsp goto done;
5494 ba580f68 2020-03-22 stsp (*nentries)++;
5495 2f51b5b3 2019-05-09 stsp }
5496 ca2503ea 2019-05-09 stsp }
5497 ed175427 2019-05-09 stsp }
5498 0b5cc0d6 2019-05-09 stsp
5499 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5500 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5501 ed175427 2019-05-09 stsp done:
5502 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5503 2e1fa222 2020-07-23 stsp return err;
5504 2e1fa222 2020-07-23 stsp }
5505 2e1fa222 2020-07-23 stsp
5506 2e1fa222 2020-07-23 stsp static const struct got_error *
5507 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5508 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5509 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5510 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5511 ebf99748 2019-05-09 stsp {
5512 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5513 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5514 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5515 ebf99748 2019-05-09 stsp
5516 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5517 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5518 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5519 ebf99748 2019-05-09 stsp
5520 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5521 437adc9d 2020-12-10 yzhong
5522 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5523 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5524 437adc9d 2020-12-10 yzhong if (err)
5525 437adc9d 2020-12-10 yzhong goto done;
5526 437adc9d 2020-12-10 yzhong
5527 ebf99748 2019-05-09 stsp if (ie) {
5528 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5529 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5530 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5531 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5532 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5533 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5534 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5535 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5536 437adc9d 2020-12-10 yzhong
5537 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5538 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5539 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5540 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5541 72fd46fa 2019-09-06 stsp !have_staged_files);
5542 ebf99748 2019-05-09 stsp } else
5543 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5544 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5545 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5546 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5547 72fd46fa 2019-09-06 stsp !have_staged_files);
5548 ebf99748 2019-05-09 stsp } else {
5549 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5550 ebf99748 2019-05-09 stsp if (err)
5551 437adc9d 2020-12-10 yzhong goto done;
5552 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5553 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5554 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5555 3969253a 2020-03-07 stsp if (err) {
5556 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5557 437adc9d 2020-12-10 yzhong goto done;
5558 3969253a 2020-03-07 stsp }
5559 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5560 3969253a 2020-03-07 stsp if (err) {
5561 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5562 437adc9d 2020-12-10 yzhong goto done;
5563 3969253a 2020-03-07 stsp }
5564 ebf99748 2019-05-09 stsp }
5565 437adc9d 2020-12-10 yzhong free(relpath);
5566 437adc9d 2020-12-10 yzhong relpath = NULL;
5567 ebf99748 2019-05-09 stsp }
5568 437adc9d 2020-12-10 yzhong done:
5569 437adc9d 2020-12-10 yzhong free(relpath);
5570 d56d26ce 2019-05-10 stsp return err;
5571 d56d26ce 2019-05-10 stsp }
5572 735ef5ac 2019-08-03 stsp
5573 d56d26ce 2019-05-10 stsp
5574 d56d26ce 2019-05-10 stsp static const struct got_error *
5575 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5576 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5577 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5578 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5579 735ef5ac 2019-08-03 stsp int ood_errcode)
5580 d56d26ce 2019-05-10 stsp {
5581 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5582 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5583 1a36436d 2019-06-10 stsp
5584 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5585 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5586 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5587 1a36436d 2019-06-10 stsp return NULL;
5588 9bead371 2019-07-28 stsp /*
5589 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5590 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5591 9bead371 2019-07-28 stsp */
5592 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5593 735ef5ac 2019-08-03 stsp in_repo_path);
5594 9bead371 2019-07-28 stsp if (err) {
5595 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5596 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5597 1a36436d 2019-06-10 stsp goto done;
5598 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5599 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5600 1a36436d 2019-06-10 stsp } else {
5601 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5602 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5603 735ef5ac 2019-08-03 stsp in_repo_path);
5604 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5605 1a36436d 2019-06-10 stsp goto done;
5606 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5607 1a36436d 2019-06-10 stsp }
5608 1a36436d 2019-06-10 stsp done:
5609 1a36436d 2019-06-10 stsp free(id);
5610 1a36436d 2019-06-10 stsp return err;
5611 ebf99748 2019-05-09 stsp }
5612 ebf99748 2019-05-09 stsp
5613 c4296144 2019-05-09 stsp const struct got_error *
5614 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5615 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5616 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id,
5617 f259c4c1 2021-09-24 stsp struct got_object_id *parent_id2,
5618 f259c4c1 2021-09-24 stsp struct got_worktree *worktree,
5619 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5620 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5621 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5622 afa376bf 2019-05-09 stsp struct got_repository *repo)
5623 c4296144 2019-05-09 stsp {
5624 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5625 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5626 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5627 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5628 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5629 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5630 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5631 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5632 f259c4c1 2021-09-24 stsp int nentries, nparents = 0;
5633 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5634 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5635 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5636 c4296144 2019-05-09 stsp
5637 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5638 c4296144 2019-05-09 stsp
5639 dbdddfee 2021-06-23 naddy STAILQ_INIT(&parent_ids);
5640 675c7539 2019-05-09 stsp
5641 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5642 588edf97 2019-05-10 stsp if (err)
5643 588edf97 2019-05-10 stsp goto done;
5644 de18fc63 2019-05-09 stsp
5645 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5646 588edf97 2019-05-10 stsp if (err)
5647 588edf97 2019-05-10 stsp goto done;
5648 588edf97 2019-05-10 stsp
5649 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5650 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5651 33ad4cbe 2019-05-12 jcs if (err)
5652 33ad4cbe 2019-05-12 jcs goto done;
5653 33ad4cbe 2019-05-12 jcs }
5654 c4296144 2019-05-09 stsp
5655 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5656 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5657 33ad4cbe 2019-05-12 jcs goto done;
5658 33ad4cbe 2019-05-12 jcs }
5659 33ad4cbe 2019-05-12 jcs
5660 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5661 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5662 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5663 cf066bf8 2019-05-09 stsp char *ondisk_path;
5664 cf066bf8 2019-05-09 stsp
5665 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5666 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5667 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5668 5f8a88c6 2019-08-03 stsp continue;
5669 5f8a88c6 2019-08-03 stsp
5670 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5671 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5672 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5673 cf066bf8 2019-05-09 stsp continue;
5674 cf066bf8 2019-05-09 stsp
5675 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5676 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5677 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5678 cf066bf8 2019-05-09 stsp goto done;
5679 cf066bf8 2019-05-09 stsp }
5680 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5681 2e1fa222 2020-07-23 stsp free(ondisk_path);
5682 2e1fa222 2020-07-23 stsp if (err)
5683 cf066bf8 2019-05-09 stsp goto done;
5684 cf066bf8 2019-05-09 stsp }
5685 036813ee 2019-05-09 stsp
5686 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5687 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5688 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5689 036813ee 2019-05-09 stsp if (err)
5690 036813ee 2019-05-09 stsp goto done;
5691 036813ee 2019-05-09 stsp
5692 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5693 de18fc63 2019-05-09 stsp if (err)
5694 de18fc63 2019-05-09 stsp goto done;
5695 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5696 f259c4c1 2021-09-24 stsp nparents++;
5697 f259c4c1 2021-09-24 stsp if (parent_id2) {
5698 f259c4c1 2021-09-24 stsp err = got_object_qid_alloc(&pid, parent_id2);
5699 f259c4c1 2021-09-24 stsp if (err)
5700 f259c4c1 2021-09-24 stsp goto done;
5701 f259c4c1 2021-09-24 stsp STAILQ_INSERT_TAIL(&parent_ids, pid, entry);
5702 f259c4c1 2021-09-24 stsp nparents++;
5703 f259c4c1 2021-09-24 stsp }
5704 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5705 f259c4c1 2021-09-24 stsp nparents, author, time(NULL), committer, time(NULL), logmsg, repo);
5706 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5707 33ad4cbe 2019-05-12 jcs free(logmsg);
5708 9d40349a 2019-05-09 stsp if (err)
5709 9d40349a 2019-05-09 stsp goto done;
5710 9d40349a 2019-05-09 stsp
5711 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5712 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5713 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5714 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5715 09f5bd90 2019-05-09 stsp goto done;
5716 09f5bd90 2019-05-09 stsp }
5717 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5718 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5719 09f5bd90 2019-05-09 stsp if (err)
5720 09f5bd90 2019-05-09 stsp goto done;
5721 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5722 ebf99748 2019-05-09 stsp if (err)
5723 ebf99748 2019-05-09 stsp goto done;
5724 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5725 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5726 09f5bd90 2019-05-09 stsp goto done;
5727 09f5bd90 2019-05-09 stsp }
5728 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5729 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5730 f2c16586 2019-05-09 stsp if (err)
5731 f2c16586 2019-05-09 stsp goto done;
5732 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5733 f2c16586 2019-05-09 stsp if (err)
5734 f2c16586 2019-05-09 stsp goto done;
5735 f2c16586 2019-05-09 stsp
5736 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5737 09f5bd90 2019-05-09 stsp if (err)
5738 09f5bd90 2019-05-09 stsp goto done;
5739 09f5bd90 2019-05-09 stsp
5740 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5741 ebf99748 2019-05-09 stsp if (err)
5742 ebf99748 2019-05-09 stsp goto done;
5743 c4296144 2019-05-09 stsp done:
5744 f259c4c1 2021-09-24 stsp got_object_id_queue_free(&parent_ids);
5745 588edf97 2019-05-10 stsp if (head_tree)
5746 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5747 588edf97 2019-05-10 stsp if (head_commit)
5748 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5749 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5750 2f17228e 2019-05-12 stsp if (head_ref2) {
5751 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5752 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5753 2f17228e 2019-05-12 stsp err = unlockerr;
5754 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5755 39cd0ff6 2019-07-12 stsp }
5756 39cd0ff6 2019-07-12 stsp return err;
5757 39cd0ff6 2019-07-12 stsp }
5758 5c1e53bc 2019-07-28 stsp
5759 5c1e53bc 2019-07-28 stsp static const struct got_error *
5760 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5761 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5762 5c1e53bc 2019-07-28 stsp {
5763 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5764 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5765 39cd0ff6 2019-07-12 stsp
5766 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5767 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5768 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5769 5c1e53bc 2019-07-28 stsp
5770 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5771 5c1e53bc 2019-07-28 stsp ct_path++;
5772 5c1e53bc 2019-07-28 stsp
5773 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5774 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5775 5c1e53bc 2019-07-28 stsp break;
5776 5c1e53bc 2019-07-28 stsp }
5777 5c1e53bc 2019-07-28 stsp
5778 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5779 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5780 5c1e53bc 2019-07-28 stsp
5781 5c1e53bc 2019-07-28 stsp return NULL;
5782 5c1e53bc 2019-07-28 stsp }
5783 5c1e53bc 2019-07-28 stsp
5784 f0b75401 2019-08-03 stsp static const struct got_error *
5785 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5786 f0b75401 2019-08-03 stsp {
5787 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5788 f0b75401 2019-08-03 stsp
5789 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5790 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5791 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5792 f0b75401 2019-08-03 stsp }
5793 f0b75401 2019-08-03 stsp
5794 f0b75401 2019-08-03 stsp return NULL;
5795 f0b75401 2019-08-03 stsp }
5796 f0b75401 2019-08-03 stsp
5797 5f8a88c6 2019-08-03 stsp static const struct got_error *
5798 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5799 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5800 5f8a88c6 2019-08-03 stsp {
5801 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5802 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5803 5f8a88c6 2019-08-03 stsp
5804 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5805 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5806 5f8a88c6 2019-08-03 stsp continue;
5807 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5808 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5809 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5810 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5811 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5812 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5813 5f8a88c6 2019-08-03 stsp }
5814 5f8a88c6 2019-08-03 stsp
5815 5f8a88c6 2019-08-03 stsp return NULL;
5816 5f8a88c6 2019-08-03 stsp }
5817 5f8a88c6 2019-08-03 stsp
5818 39cd0ff6 2019-07-12 stsp const struct got_error *
5819 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5820 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5821 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5822 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5823 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5824 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5825 39cd0ff6 2019-07-12 stsp {
5826 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5827 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5828 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5829 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5830 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5831 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5832 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5833 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5834 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5835 39cd0ff6 2019-07-12 stsp
5836 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5837 39cd0ff6 2019-07-12 stsp
5838 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5839 39cd0ff6 2019-07-12 stsp
5840 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5841 39cd0ff6 2019-07-12 stsp if (err)
5842 39cd0ff6 2019-07-12 stsp goto done;
5843 39cd0ff6 2019-07-12 stsp
5844 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5845 39cd0ff6 2019-07-12 stsp if (err)
5846 39cd0ff6 2019-07-12 stsp goto done;
5847 39cd0ff6 2019-07-12 stsp
5848 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5849 39cd0ff6 2019-07-12 stsp if (err)
5850 39cd0ff6 2019-07-12 stsp goto done;
5851 39cd0ff6 2019-07-12 stsp
5852 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5853 39cd0ff6 2019-07-12 stsp if (err)
5854 39cd0ff6 2019-07-12 stsp goto done;
5855 39cd0ff6 2019-07-12 stsp
5856 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5857 5f8a88c6 2019-08-03 stsp &have_staged_files);
5858 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5859 5f8a88c6 2019-08-03 stsp goto done;
5860 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5861 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5862 5f8a88c6 2019-08-03 stsp if (err)
5863 5f8a88c6 2019-08-03 stsp goto done;
5864 5f8a88c6 2019-08-03 stsp }
5865 5f8a88c6 2019-08-03 stsp
5866 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5867 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5868 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5869 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5870 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5871 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5872 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5873 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5874 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5875 5c1e53bc 2019-07-28 stsp if (err)
5876 5c1e53bc 2019-07-28 stsp goto done;
5877 5c1e53bc 2019-07-28 stsp }
5878 39cd0ff6 2019-07-12 stsp
5879 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5880 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5881 39cd0ff6 2019-07-12 stsp goto done;
5882 5c1e53bc 2019-07-28 stsp }
5883 5c1e53bc 2019-07-28 stsp
5884 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5885 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5886 5c1e53bc 2019-07-28 stsp if (err)
5887 5c1e53bc 2019-07-28 stsp goto done;
5888 39cd0ff6 2019-07-12 stsp }
5889 f0b75401 2019-08-03 stsp
5890 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5891 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5892 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5893 f0b75401 2019-08-03 stsp
5894 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5895 f0b75401 2019-08-03 stsp ct_path++;
5896 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5897 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5898 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5899 b50cabdf 2019-07-12 stsp if (err)
5900 b50cabdf 2019-07-12 stsp goto done;
5901 f0b75401 2019-08-03 stsp
5902 b50cabdf 2019-07-12 stsp }
5903 b50cabdf 2019-07-12 stsp
5904 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5905 f259c4c1 2021-09-24 stsp head_commit_id, NULL, worktree, author, committer,
5906 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5907 39cd0ff6 2019-07-12 stsp if (err)
5908 39cd0ff6 2019-07-12 stsp goto done;
5909 39cd0ff6 2019-07-12 stsp
5910 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5911 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5912 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5913 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5914 39cd0ff6 2019-07-12 stsp err = sync_err;
5915 39cd0ff6 2019-07-12 stsp done:
5916 39cd0ff6 2019-07-12 stsp if (fileindex)
5917 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5918 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5919 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5920 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5921 39cd0ff6 2019-07-12 stsp err = unlockerr;
5922 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5923 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5924 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5925 39cd0ff6 2019-07-12 stsp }
5926 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5927 c4296144 2019-05-09 stsp return err;
5928 8656d6c4 2019-05-20 stsp }
5929 8656d6c4 2019-05-20 stsp
5930 8656d6c4 2019-05-20 stsp const char *
5931 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5932 8656d6c4 2019-05-20 stsp {
5933 8656d6c4 2019-05-20 stsp return ct->path;
5934 8656d6c4 2019-05-20 stsp }
5935 8656d6c4 2019-05-20 stsp
5936 8656d6c4 2019-05-20 stsp unsigned int
5937 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5938 8656d6c4 2019-05-20 stsp {
5939 8656d6c4 2019-05-20 stsp return ct->status;
5940 818c7501 2019-07-11 stsp }
5941 818c7501 2019-07-11 stsp
5942 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5943 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5944 818c7501 2019-07-11 stsp struct got_repository *repo;
5945 818c7501 2019-07-11 stsp };
5946 818c7501 2019-07-11 stsp
5947 818c7501 2019-07-11 stsp static const struct got_error *
5948 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5949 818c7501 2019-07-11 stsp {
5950 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5951 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5952 818c7501 2019-07-11 stsp unsigned char status;
5953 818c7501 2019-07-11 stsp struct stat sb;
5954 818c7501 2019-07-11 stsp char *ondisk_path;
5955 818c7501 2019-07-11 stsp
5956 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5957 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5958 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5959 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5960 818c7501 2019-07-11 stsp
5961 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5962 818c7501 2019-07-11 stsp == -1)
5963 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5964 818c7501 2019-07-11 stsp
5965 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5966 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5967 818c7501 2019-07-11 stsp free(ondisk_path);
5968 818c7501 2019-07-11 stsp if (err)
5969 818c7501 2019-07-11 stsp return err;
5970 818c7501 2019-07-11 stsp
5971 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5972 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5973 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5974 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5975 818c7501 2019-07-11 stsp
5976 818c7501 2019-07-11 stsp return NULL;
5977 818c7501 2019-07-11 stsp }
5978 818c7501 2019-07-11 stsp
5979 818c7501 2019-07-11 stsp const struct got_error *
5980 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5981 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5982 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5983 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5984 818c7501 2019-07-11 stsp {
5985 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5986 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5987 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5988 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5989 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5990 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5991 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5992 818c7501 2019-07-11 stsp
5993 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5994 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5995 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5996 818c7501 2019-07-11 stsp
5997 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5998 818c7501 2019-07-11 stsp if (err)
5999 818c7501 2019-07-11 stsp return err;
6000 818c7501 2019-07-11 stsp
6001 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6002 818c7501 2019-07-11 stsp if (err)
6003 818c7501 2019-07-11 stsp goto done;
6004 818c7501 2019-07-11 stsp
6005 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
6006 818c7501 2019-07-11 stsp ok_arg.repo = repo;
6007 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6008 818c7501 2019-07-11 stsp &ok_arg);
6009 818c7501 2019-07-11 stsp if (err)
6010 818c7501 2019-07-11 stsp goto done;
6011 818c7501 2019-07-11 stsp
6012 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6013 818c7501 2019-07-11 stsp if (err)
6014 818c7501 2019-07-11 stsp goto done;
6015 818c7501 2019-07-11 stsp
6016 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6017 818c7501 2019-07-11 stsp if (err)
6018 818c7501 2019-07-11 stsp goto done;
6019 818c7501 2019-07-11 stsp
6020 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
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 = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6025 818c7501 2019-07-11 stsp 0);
6026 e51d7b55 2020-01-04 stsp if (err)
6027 e51d7b55 2020-01-04 stsp goto done;
6028 e51d7b55 2020-01-04 stsp
6029 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
6030 818c7501 2019-07-11 stsp if (err)
6031 818c7501 2019-07-11 stsp goto done;
6032 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
6033 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
6034 e51d7b55 2020-01-04 stsp goto done;
6035 e51d7b55 2020-01-04 stsp }
6036 818c7501 2019-07-11 stsp
6037 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
6038 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
6039 818c7501 2019-07-11 stsp if (err)
6040 818c7501 2019-07-11 stsp goto done;
6041 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
6042 818c7501 2019-07-11 stsp if (err)
6043 818c7501 2019-07-11 stsp goto done;
6044 818c7501 2019-07-11 stsp
6045 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
6046 818c7501 2019-07-11 stsp
6047 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
6048 818c7501 2019-07-11 stsp if (err)
6049 818c7501 2019-07-11 stsp goto done;
6050 818c7501 2019-07-11 stsp
6051 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
6052 818c7501 2019-07-11 stsp if (err)
6053 818c7501 2019-07-11 stsp goto done;
6054 818c7501 2019-07-11 stsp
6055 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6056 818c7501 2019-07-11 stsp worktree->base_commit_id);
6057 818c7501 2019-07-11 stsp if (err)
6058 818c7501 2019-07-11 stsp goto done;
6059 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
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_worktree_set_head_ref(worktree, *tmp_branch);
6064 818c7501 2019-07-11 stsp if (err)
6065 818c7501 2019-07-11 stsp goto done;
6066 818c7501 2019-07-11 stsp done:
6067 818c7501 2019-07-11 stsp free(fileindex_path);
6068 818c7501 2019-07-11 stsp free(tmp_branch_name);
6069 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
6070 818c7501 2019-07-11 stsp free(branch_ref_name);
6071 818c7501 2019-07-11 stsp if (branch_ref)
6072 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6073 818c7501 2019-07-11 stsp if (wt_branch)
6074 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
6075 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
6076 818c7501 2019-07-11 stsp if (err) {
6077 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
6078 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
6079 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
6080 818c7501 2019-07-11 stsp }
6081 818c7501 2019-07-11 stsp if (*tmp_branch) {
6082 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6083 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6084 818c7501 2019-07-11 stsp }
6085 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6086 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6087 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6088 3e3a69f1 2019-07-25 stsp }
6089 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
6090 818c7501 2019-07-11 stsp }
6091 818c7501 2019-07-11 stsp return err;
6092 818c7501 2019-07-11 stsp }
6093 818c7501 2019-07-11 stsp
6094 818c7501 2019-07-11 stsp const struct got_error *
6095 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
6096 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
6097 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
6098 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
6099 818c7501 2019-07-11 stsp {
6100 818c7501 2019-07-11 stsp const struct got_error *err;
6101 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
6102 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6103 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
6104 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6105 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6106 818c7501 2019-07-11 stsp
6107 818c7501 2019-07-11 stsp *commit_id = NULL;
6108 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
6109 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
6110 3e3a69f1 2019-07-25 stsp *branch = NULL;
6111 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6112 3e3a69f1 2019-07-25 stsp
6113 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6114 3e3a69f1 2019-07-25 stsp if (err)
6115 3e3a69f1 2019-07-25 stsp return err;
6116 818c7501 2019-07-11 stsp
6117 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6118 3e3a69f1 2019-07-25 stsp if (err)
6119 f032f1f7 2019-08-04 stsp goto done;
6120 f032f1f7 2019-08-04 stsp
6121 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6122 f032f1f7 2019-08-04 stsp &have_staged_files);
6123 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6124 f032f1f7 2019-08-04 stsp goto done;
6125 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6126 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6127 3e3a69f1 2019-07-25 stsp goto done;
6128 f032f1f7 2019-08-04 stsp }
6129 3e3a69f1 2019-07-25 stsp
6130 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6131 818c7501 2019-07-11 stsp if (err)
6132 f032f1f7 2019-08-04 stsp goto done;
6133 818c7501 2019-07-11 stsp
6134 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6135 818c7501 2019-07-11 stsp if (err)
6136 818c7501 2019-07-11 stsp goto done;
6137 818c7501 2019-07-11 stsp
6138 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6139 818c7501 2019-07-11 stsp if (err)
6140 818c7501 2019-07-11 stsp goto done;
6141 818c7501 2019-07-11 stsp
6142 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6143 818c7501 2019-07-11 stsp if (err)
6144 818c7501 2019-07-11 stsp goto done;
6145 818c7501 2019-07-11 stsp
6146 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
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 = got_ref_open(branch, repo,
6151 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
6152 818c7501 2019-07-11 stsp if (err)
6153 818c7501 2019-07-11 stsp goto done;
6154 818c7501 2019-07-11 stsp
6155 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6156 818c7501 2019-07-11 stsp if (err)
6157 818c7501 2019-07-11 stsp goto done;
6158 818c7501 2019-07-11 stsp
6159 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6160 818c7501 2019-07-11 stsp if (err)
6161 818c7501 2019-07-11 stsp goto done;
6162 818c7501 2019-07-11 stsp
6163 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
6164 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
6165 818c7501 2019-07-11 stsp if (err)
6166 818c7501 2019-07-11 stsp goto done;
6167 818c7501 2019-07-11 stsp
6168 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6169 818c7501 2019-07-11 stsp if (err)
6170 818c7501 2019-07-11 stsp goto done;
6171 818c7501 2019-07-11 stsp done:
6172 818c7501 2019-07-11 stsp free(commit_ref_name);
6173 818c7501 2019-07-11 stsp free(branch_ref_name);
6174 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6175 818c7501 2019-07-11 stsp if (commit_ref)
6176 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6177 818c7501 2019-07-11 stsp if (branch_ref)
6178 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6179 818c7501 2019-07-11 stsp if (err) {
6180 818c7501 2019-07-11 stsp free(*commit_id);
6181 818c7501 2019-07-11 stsp *commit_id = NULL;
6182 818c7501 2019-07-11 stsp if (*tmp_branch) {
6183 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6184 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6185 818c7501 2019-07-11 stsp }
6186 818c7501 2019-07-11 stsp if (*new_base_branch) {
6187 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6188 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6189 818c7501 2019-07-11 stsp }
6190 818c7501 2019-07-11 stsp if (*branch) {
6191 818c7501 2019-07-11 stsp got_ref_close(*branch);
6192 818c7501 2019-07-11 stsp *branch = NULL;
6193 818c7501 2019-07-11 stsp }
6194 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6195 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6196 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6197 3e3a69f1 2019-07-25 stsp }
6198 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6199 818c7501 2019-07-11 stsp }
6200 818c7501 2019-07-11 stsp return err;
6201 c4296144 2019-05-09 stsp }
6202 818c7501 2019-07-11 stsp
6203 818c7501 2019-07-11 stsp const struct got_error *
6204 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6205 818c7501 2019-07-11 stsp {
6206 818c7501 2019-07-11 stsp const struct got_error *err;
6207 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6208 818c7501 2019-07-11 stsp
6209 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6210 818c7501 2019-07-11 stsp if (err)
6211 818c7501 2019-07-11 stsp return err;
6212 818c7501 2019-07-11 stsp
6213 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6214 818c7501 2019-07-11 stsp free(tmp_branch_name);
6215 818c7501 2019-07-11 stsp return NULL;
6216 818c7501 2019-07-11 stsp }
6217 818c7501 2019-07-11 stsp
6218 818c7501 2019-07-11 stsp static const struct got_error *
6219 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6220 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6221 818c7501 2019-07-11 stsp {
6222 0ebf8283 2019-07-24 stsp *logmsg = arg;
6223 818c7501 2019-07-11 stsp return NULL;
6224 818c7501 2019-07-11 stsp }
6225 818c7501 2019-07-11 stsp
6226 818c7501 2019-07-11 stsp static const struct got_error *
6227 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6228 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6229 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6230 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6231 818c7501 2019-07-11 stsp {
6232 818c7501 2019-07-11 stsp return NULL;
6233 01757395 2019-07-12 stsp }
6234 01757395 2019-07-12 stsp
6235 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6236 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6237 01757395 2019-07-12 stsp void *progress_arg;
6238 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6239 01757395 2019-07-12 stsp };
6240 01757395 2019-07-12 stsp
6241 01757395 2019-07-12 stsp static const struct got_error *
6242 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6243 01757395 2019-07-12 stsp {
6244 01757395 2019-07-12 stsp const struct got_error *err;
6245 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6246 01757395 2019-07-12 stsp char *p;
6247 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6248 01757395 2019-07-12 stsp
6249 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6250 01757395 2019-07-12 stsp if (err)
6251 01757395 2019-07-12 stsp return err;
6252 01757395 2019-07-12 stsp
6253 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6254 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6255 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6256 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6257 01757395 2019-07-12 stsp return NULL;
6258 01757395 2019-07-12 stsp
6259 01757395 2019-07-12 stsp p = strdup(path);
6260 01757395 2019-07-12 stsp if (p == NULL)
6261 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6262 01757395 2019-07-12 stsp
6263 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6264 01757395 2019-07-12 stsp if (err || new == NULL)
6265 01757395 2019-07-12 stsp free(p);
6266 01757395 2019-07-12 stsp return err;
6267 01757395 2019-07-12 stsp }
6268 01757395 2019-07-12 stsp
6269 01757395 2019-07-12 stsp void
6270 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6271 01757395 2019-07-12 stsp {
6272 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6273 01757395 2019-07-12 stsp
6274 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6275 01757395 2019-07-12 stsp free((char *)pe->path);
6276 01757395 2019-07-12 stsp
6277 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6278 818c7501 2019-07-11 stsp }
6279 818c7501 2019-07-11 stsp
6280 0ebf8283 2019-07-24 stsp static const struct got_error *
6281 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6282 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6283 818c7501 2019-07-11 stsp {
6284 818c7501 2019-07-11 stsp const struct got_error *err;
6285 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6286 818c7501 2019-07-11 stsp
6287 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6288 818c7501 2019-07-11 stsp if (err) {
6289 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6290 818c7501 2019-07-11 stsp goto done;
6291 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6292 818c7501 2019-07-11 stsp if (err)
6293 818c7501 2019-07-11 stsp goto done;
6294 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6295 818c7501 2019-07-11 stsp if (err)
6296 818c7501 2019-07-11 stsp goto done;
6297 de05890f 2020-03-05 stsp } else if (is_rebase) {
6298 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6299 818c7501 2019-07-11 stsp int cmp;
6300 818c7501 2019-07-11 stsp
6301 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6302 818c7501 2019-07-11 stsp if (err)
6303 818c7501 2019-07-11 stsp goto done;
6304 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6305 818c7501 2019-07-11 stsp free(stored_id);
6306 818c7501 2019-07-11 stsp if (cmp != 0) {
6307 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6308 818c7501 2019-07-11 stsp goto done;
6309 818c7501 2019-07-11 stsp }
6310 818c7501 2019-07-11 stsp }
6311 0ebf8283 2019-07-24 stsp done:
6312 0ebf8283 2019-07-24 stsp if (commit_ref)
6313 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6314 0ebf8283 2019-07-24 stsp return err;
6315 0ebf8283 2019-07-24 stsp }
6316 0ebf8283 2019-07-24 stsp
6317 0ebf8283 2019-07-24 stsp static const struct got_error *
6318 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6319 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6320 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6321 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6322 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6323 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6324 0ebf8283 2019-07-24 stsp {
6325 0ebf8283 2019-07-24 stsp const struct got_error *err;
6326 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6327 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6328 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6329 818c7501 2019-07-11 stsp
6330 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6331 0ebf8283 2019-07-24 stsp
6332 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6333 0ebf8283 2019-07-24 stsp if (err)
6334 0ebf8283 2019-07-24 stsp return err;
6335 0ebf8283 2019-07-24 stsp
6336 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6337 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6338 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6339 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6340 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6341 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6342 818c7501 2019-07-11 stsp if (commit_ref)
6343 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6344 818c7501 2019-07-11 stsp return err;
6345 818c7501 2019-07-11 stsp }
6346 818c7501 2019-07-11 stsp
6347 818c7501 2019-07-11 stsp const struct got_error *
6348 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6349 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6350 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6351 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6352 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6353 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6354 818c7501 2019-07-11 stsp {
6355 0ebf8283 2019-07-24 stsp const struct got_error *err;
6356 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6357 0ebf8283 2019-07-24 stsp
6358 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6359 0ebf8283 2019-07-24 stsp if (err)
6360 0ebf8283 2019-07-24 stsp return err;
6361 0ebf8283 2019-07-24 stsp
6362 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6363 0ebf8283 2019-07-24 stsp if (err)
6364 0ebf8283 2019-07-24 stsp goto done;
6365 0ebf8283 2019-07-24 stsp
6366 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6367 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6368 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6369 0ebf8283 2019-07-24 stsp done:
6370 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6371 0ebf8283 2019-07-24 stsp return err;
6372 0ebf8283 2019-07-24 stsp }
6373 0ebf8283 2019-07-24 stsp
6374 0ebf8283 2019-07-24 stsp const struct got_error *
6375 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6376 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6377 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6378 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6379 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6380 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6381 0ebf8283 2019-07-24 stsp {
6382 0ebf8283 2019-07-24 stsp const struct got_error *err;
6383 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6384 0ebf8283 2019-07-24 stsp
6385 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6386 0ebf8283 2019-07-24 stsp if (err)
6387 0ebf8283 2019-07-24 stsp return err;
6388 0ebf8283 2019-07-24 stsp
6389 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6390 0ebf8283 2019-07-24 stsp if (err)
6391 0ebf8283 2019-07-24 stsp goto done;
6392 0ebf8283 2019-07-24 stsp
6393 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6394 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6395 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6396 0ebf8283 2019-07-24 stsp done:
6397 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6398 0ebf8283 2019-07-24 stsp return err;
6399 0ebf8283 2019-07-24 stsp }
6400 0ebf8283 2019-07-24 stsp
6401 0ebf8283 2019-07-24 stsp static const struct got_error *
6402 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6403 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6404 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6405 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6406 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6407 0ebf8283 2019-07-24 stsp {
6408 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6409 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6410 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6411 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6412 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6413 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6414 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6415 818c7501 2019-07-11 stsp
6416 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6417 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6418 a0e95631 2019-07-12 stsp
6419 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6420 818c7501 2019-07-11 stsp
6421 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6422 a0e95631 2019-07-12 stsp if (err)
6423 3e3a69f1 2019-07-25 stsp return err;
6424 a0e95631 2019-07-12 stsp
6425 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6426 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6427 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6428 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6429 01757395 2019-07-12 stsp /*
6430 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6431 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6432 6c13b005 2021-09-02 stsp *
6433 6c13b005 2021-09-02 stsp * Ideally, merged_paths would contain a list of commitables
6434 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6435 6c13b005 2021-09-02 stsp * However, we would then need carefully keep track of cumulative
6436 6c13b005 2021-09-02 stsp * effects of operations such as file additions and deletions
6437 6c13b005 2021-09-02 stsp * in 'got histedit -f' (folding multiple commits into one),
6438 6c13b005 2021-09-02 stsp * and this extra complexity is not really worth it.
6439 01757395 2019-07-12 stsp */
6440 01757395 2019-07-12 stsp if (merged_paths) {
6441 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6442 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6443 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6444 0e33f8e0 2021-09-01 stsp repo, collect_commitables, &cc_arg, NULL, NULL, 1,
6445 f2a9dc41 2019-12-13 tracey 0);
6446 01757395 2019-07-12 stsp if (err)
6447 01757395 2019-07-12 stsp goto done;
6448 01757395 2019-07-12 stsp }
6449 01757395 2019-07-12 stsp } else {
6450 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6451 0e33f8e0 2021-09-01 stsp collect_commitables, &cc_arg, NULL, NULL, 1, 0);
6452 01757395 2019-07-12 stsp if (err)
6453 01757395 2019-07-12 stsp goto done;
6454 01757395 2019-07-12 stsp }
6455 a0e95631 2019-07-12 stsp
6456 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6457 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6458 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6459 a0e95631 2019-07-12 stsp if (err)
6460 a0e95631 2019-07-12 stsp goto done;
6461 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6462 a0e95631 2019-07-12 stsp goto done;
6463 ff0d2220 2019-07-11 stsp }
6464 818c7501 2019-07-11 stsp
6465 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6466 edd02c5e 2019-07-11 stsp if (err)
6467 edd02c5e 2019-07-11 stsp goto done;
6468 edd02c5e 2019-07-11 stsp
6469 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6470 818c7501 2019-07-11 stsp if (err)
6471 818c7501 2019-07-11 stsp goto done;
6472 818c7501 2019-07-11 stsp
6473 5943eee2 2019-08-13 stsp if (new_logmsg) {
6474 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6475 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6476 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6477 5943eee2 2019-08-13 stsp goto done;
6478 5943eee2 2019-08-13 stsp }
6479 5943eee2 2019-08-13 stsp } else {
6480 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6481 5943eee2 2019-08-13 stsp if (err)
6482 5943eee2 2019-08-13 stsp goto done;
6483 5943eee2 2019-08-13 stsp }
6484 0ebf8283 2019-07-24 stsp
6485 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6486 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6487 f259c4c1 2021-09-24 stsp NULL, worktree, got_object_commit_get_author(orig_commit),
6488 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6489 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6490 a0e95631 2019-07-12 stsp if (err)
6491 a0e95631 2019-07-12 stsp goto done;
6492 a0e95631 2019-07-12 stsp
6493 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6494 a0e95631 2019-07-12 stsp if (err)
6495 a0e95631 2019-07-12 stsp goto done;
6496 a0e95631 2019-07-12 stsp
6497 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6498 a0e95631 2019-07-12 stsp if (err)
6499 a0e95631 2019-07-12 stsp goto done;
6500 a0e95631 2019-07-12 stsp
6501 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6502 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6503 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6504 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6505 a0e95631 2019-07-12 stsp err = sync_err;
6506 818c7501 2019-07-11 stsp done:
6507 a0e95631 2019-07-12 stsp free(fileindex_path);
6508 a0e95631 2019-07-12 stsp free(head_commit_id);
6509 a0e95631 2019-07-12 stsp if (head_ref)
6510 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6511 818c7501 2019-07-11 stsp if (err) {
6512 818c7501 2019-07-11 stsp free(*new_commit_id);
6513 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6514 818c7501 2019-07-11 stsp }
6515 818c7501 2019-07-11 stsp return err;
6516 818c7501 2019-07-11 stsp }
6517 818c7501 2019-07-11 stsp
6518 818c7501 2019-07-11 stsp const struct got_error *
6519 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6520 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6521 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6522 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6523 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6524 0ebf8283 2019-07-24 stsp {
6525 0ebf8283 2019-07-24 stsp const struct got_error *err;
6526 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6527 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6528 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6529 0ebf8283 2019-07-24 stsp
6530 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6531 0ebf8283 2019-07-24 stsp if (err)
6532 0ebf8283 2019-07-24 stsp return err;
6533 0ebf8283 2019-07-24 stsp
6534 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6535 0ebf8283 2019-07-24 stsp if (err)
6536 0ebf8283 2019-07-24 stsp goto done;
6537 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6538 0ebf8283 2019-07-24 stsp if (err)
6539 0ebf8283 2019-07-24 stsp goto done;
6540 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6541 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6542 0ebf8283 2019-07-24 stsp goto done;
6543 0ebf8283 2019-07-24 stsp }
6544 0ebf8283 2019-07-24 stsp
6545 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6546 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6547 0ebf8283 2019-07-24 stsp done:
6548 0ebf8283 2019-07-24 stsp if (commit_ref)
6549 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6550 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6551 0ebf8283 2019-07-24 stsp free(commit_id);
6552 0ebf8283 2019-07-24 stsp return err;
6553 0ebf8283 2019-07-24 stsp }
6554 0ebf8283 2019-07-24 stsp
6555 0ebf8283 2019-07-24 stsp const struct got_error *
6556 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6557 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6558 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6559 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6560 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6561 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6562 0ebf8283 2019-07-24 stsp {
6563 0ebf8283 2019-07-24 stsp const struct got_error *err;
6564 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6565 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6566 0ebf8283 2019-07-24 stsp
6567 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6568 0ebf8283 2019-07-24 stsp if (err)
6569 0ebf8283 2019-07-24 stsp return err;
6570 0ebf8283 2019-07-24 stsp
6571 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6572 0ebf8283 2019-07-24 stsp if (err)
6573 0ebf8283 2019-07-24 stsp goto done;
6574 0ebf8283 2019-07-24 stsp
6575 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6576 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6577 0ebf8283 2019-07-24 stsp done:
6578 0ebf8283 2019-07-24 stsp if (commit_ref)
6579 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6580 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6581 0ebf8283 2019-07-24 stsp return err;
6582 0ebf8283 2019-07-24 stsp }
6583 0ebf8283 2019-07-24 stsp
6584 0ebf8283 2019-07-24 stsp const struct got_error *
6585 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6586 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6587 818c7501 2019-07-11 stsp {
6588 3e3a69f1 2019-07-25 stsp if (fileindex)
6589 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6590 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6591 69844fba 2019-07-11 stsp }
6592 69844fba 2019-07-11 stsp
6593 69844fba 2019-07-11 stsp static const struct got_error *
6594 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6595 69844fba 2019-07-11 stsp {
6596 69844fba 2019-07-11 stsp const struct got_error *err;
6597 69844fba 2019-07-11 stsp struct got_reference *ref;
6598 69844fba 2019-07-11 stsp
6599 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6600 69844fba 2019-07-11 stsp if (err) {
6601 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6602 69844fba 2019-07-11 stsp return NULL;
6603 69844fba 2019-07-11 stsp return err;
6604 69844fba 2019-07-11 stsp }
6605 69844fba 2019-07-11 stsp
6606 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6607 69844fba 2019-07-11 stsp got_ref_close(ref);
6608 69844fba 2019-07-11 stsp return err;
6609 818c7501 2019-07-11 stsp }
6610 818c7501 2019-07-11 stsp
6611 69844fba 2019-07-11 stsp static const struct got_error *
6612 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6613 69844fba 2019-07-11 stsp {
6614 69844fba 2019-07-11 stsp const struct got_error *err;
6615 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6616 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6617 69844fba 2019-07-11 stsp
6618 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6619 69844fba 2019-07-11 stsp if (err)
6620 69844fba 2019-07-11 stsp goto done;
6621 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6622 69844fba 2019-07-11 stsp if (err)
6623 69844fba 2019-07-11 stsp goto done;
6624 69844fba 2019-07-11 stsp
6625 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6626 69844fba 2019-07-11 stsp if (err)
6627 69844fba 2019-07-11 stsp goto done;
6628 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6629 69844fba 2019-07-11 stsp if (err)
6630 69844fba 2019-07-11 stsp goto done;
6631 69844fba 2019-07-11 stsp
6632 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6633 69844fba 2019-07-11 stsp if (err)
6634 69844fba 2019-07-11 stsp goto done;
6635 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6636 69844fba 2019-07-11 stsp if (err)
6637 69844fba 2019-07-11 stsp goto done;
6638 69844fba 2019-07-11 stsp
6639 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6640 69844fba 2019-07-11 stsp if (err)
6641 69844fba 2019-07-11 stsp goto done;
6642 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6643 69844fba 2019-07-11 stsp if (err)
6644 69844fba 2019-07-11 stsp goto done;
6645 69844fba 2019-07-11 stsp
6646 69844fba 2019-07-11 stsp done:
6647 69844fba 2019-07-11 stsp free(tmp_branch_name);
6648 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6649 69844fba 2019-07-11 stsp free(branch_ref_name);
6650 69844fba 2019-07-11 stsp free(commit_ref_name);
6651 e600f124 2021-03-21 stsp return err;
6652 e600f124 2021-03-21 stsp }
6653 e600f124 2021-03-21 stsp
6654 e600f124 2021-03-21 stsp const struct got_error *
6655 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6656 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6657 e600f124 2021-03-21 stsp {
6658 e600f124 2021-03-21 stsp const struct got_error *err;
6659 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6660 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6661 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6662 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6663 e600f124 2021-03-21 stsp char *refname = NULL;
6664 e600f124 2021-03-21 stsp
6665 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6666 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6667 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6668 e600f124 2021-03-21 stsp branch_name += 11;
6669 e600f124 2021-03-21 stsp
6670 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6671 e600f124 2021-03-21 stsp if (err)
6672 e600f124 2021-03-21 stsp return err;
6673 e600f124 2021-03-21 stsp
6674 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6675 e600f124 2021-03-21 stsp new_id_str) == -1) {
6676 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6677 e600f124 2021-03-21 stsp goto done;
6678 e600f124 2021-03-21 stsp }
6679 e600f124 2021-03-21 stsp
6680 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6681 e600f124 2021-03-21 stsp if (err)
6682 e600f124 2021-03-21 stsp goto done;
6683 e600f124 2021-03-21 stsp
6684 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6685 e600f124 2021-03-21 stsp if (err)
6686 e600f124 2021-03-21 stsp goto done;
6687 e600f124 2021-03-21 stsp
6688 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6689 e600f124 2021-03-21 stsp done:
6690 e600f124 2021-03-21 stsp free(new_id_str);
6691 e600f124 2021-03-21 stsp free(refname);
6692 e600f124 2021-03-21 stsp free(old_commit_id);
6693 e600f124 2021-03-21 stsp if (ref)
6694 e600f124 2021-03-21 stsp got_ref_close(ref);
6695 69844fba 2019-07-11 stsp return err;
6696 69844fba 2019-07-11 stsp }
6697 69844fba 2019-07-11 stsp
6698 818c7501 2019-07-11 stsp const struct got_error *
6699 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6700 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6701 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6702 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6703 818c7501 2019-07-11 stsp {
6704 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6705 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6706 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6707 818c7501 2019-07-11 stsp
6708 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6709 818c7501 2019-07-11 stsp if (err)
6710 818c7501 2019-07-11 stsp return err;
6711 e600f124 2021-03-21 stsp
6712 e600f124 2021-03-21 stsp if (create_backup) {
6713 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6714 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6715 e600f124 2021-03-21 stsp if (err)
6716 e600f124 2021-03-21 stsp goto done;
6717 e600f124 2021-03-21 stsp }
6718 818c7501 2019-07-11 stsp
6719 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6720 818c7501 2019-07-11 stsp if (err)
6721 818c7501 2019-07-11 stsp goto done;
6722 818c7501 2019-07-11 stsp
6723 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6724 818c7501 2019-07-11 stsp if (err)
6725 818c7501 2019-07-11 stsp goto done;
6726 818c7501 2019-07-11 stsp
6727 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6728 818c7501 2019-07-11 stsp if (err)
6729 818c7501 2019-07-11 stsp goto done;
6730 818c7501 2019-07-11 stsp
6731 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6732 a615e0e7 2020-12-16 stsp if (err)
6733 a615e0e7 2020-12-16 stsp goto done;
6734 a615e0e7 2020-12-16 stsp
6735 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6736 a615e0e7 2020-12-16 stsp if (err)
6737 a615e0e7 2020-12-16 stsp goto done;
6738 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6739 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6740 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6741 a615e0e7 2020-12-16 stsp err = sync_err;
6742 818c7501 2019-07-11 stsp done:
6743 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6744 a615e0e7 2020-12-16 stsp free(fileindex_path);
6745 818c7501 2019-07-11 stsp free(new_head_commit_id);
6746 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6747 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6748 818c7501 2019-07-11 stsp err = unlockerr;
6749 818c7501 2019-07-11 stsp return err;
6750 818c7501 2019-07-11 stsp }
6751 818c7501 2019-07-11 stsp
6752 818c7501 2019-07-11 stsp const struct got_error *
6753 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6754 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6755 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6756 abc59930 2021-09-05 naddy got_worktree_checkout_cb progress_cb, void *progress_arg)
6757 818c7501 2019-07-11 stsp {
6758 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6759 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6760 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6761 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6762 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6763 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6764 818c7501 2019-07-11 stsp
6765 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6766 818c7501 2019-07-11 stsp if (err)
6767 818c7501 2019-07-11 stsp return err;
6768 818c7501 2019-07-11 stsp
6769 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6770 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6771 818c7501 2019-07-11 stsp if (err)
6772 818c7501 2019-07-11 stsp goto done;
6773 818c7501 2019-07-11 stsp
6774 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6775 818c7501 2019-07-11 stsp if (err)
6776 818c7501 2019-07-11 stsp goto done;
6777 818c7501 2019-07-11 stsp
6778 818c7501 2019-07-11 stsp /*
6779 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6780 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6781 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6782 818c7501 2019-07-11 stsp */
6783 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6784 818c7501 2019-07-11 stsp if (err)
6785 818c7501 2019-07-11 stsp goto done;
6786 818c7501 2019-07-11 stsp
6787 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6788 818c7501 2019-07-11 stsp if (err)
6789 818c7501 2019-07-11 stsp goto done;
6790 818c7501 2019-07-11 stsp
6791 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6792 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6793 ca355955 2019-07-12 stsp if (err)
6794 ca355955 2019-07-12 stsp goto done;
6795 ca355955 2019-07-12 stsp
6796 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6797 ca355955 2019-07-12 stsp if (err)
6798 ca355955 2019-07-12 stsp goto done;
6799 ca355955 2019-07-12 stsp
6800 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6801 818c7501 2019-07-11 stsp if (err)
6802 818c7501 2019-07-11 stsp goto done;
6803 818c7501 2019-07-11 stsp
6804 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6805 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6806 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6807 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6808 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6809 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6810 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6811 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
6812 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6813 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6814 55bd499d 2019-07-12 stsp if (err)
6815 1f1abb7e 2019-08-08 stsp goto sync;
6816 55bd499d 2019-07-12 stsp
6817 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6818 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6819 ca355955 2019-07-12 stsp sync:
6820 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6821 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6822 ca355955 2019-07-12 stsp err = sync_err;
6823 818c7501 2019-07-11 stsp done:
6824 818c7501 2019-07-11 stsp got_ref_close(resolved);
6825 a3a2faf2 2019-07-12 stsp free(tree_id);
6826 818c7501 2019-07-11 stsp free(commit_id);
6827 0ebf8283 2019-07-24 stsp if (fileindex)
6828 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6829 0ebf8283 2019-07-24 stsp free(fileindex_path);
6830 0ebf8283 2019-07-24 stsp
6831 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6832 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6833 0ebf8283 2019-07-24 stsp err = unlockerr;
6834 0ebf8283 2019-07-24 stsp return err;
6835 0ebf8283 2019-07-24 stsp }
6836 0ebf8283 2019-07-24 stsp
6837 0ebf8283 2019-07-24 stsp const struct got_error *
6838 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6839 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6840 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6841 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6842 0ebf8283 2019-07-24 stsp {
6843 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6844 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6845 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6846 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6847 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6848 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6849 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6850 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6851 0ebf8283 2019-07-24 stsp
6852 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6853 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6854 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6855 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6856 0ebf8283 2019-07-24 stsp
6857 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6858 0ebf8283 2019-07-24 stsp if (err)
6859 0ebf8283 2019-07-24 stsp return err;
6860 0ebf8283 2019-07-24 stsp
6861 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6862 0ebf8283 2019-07-24 stsp if (err)
6863 0ebf8283 2019-07-24 stsp goto done;
6864 0ebf8283 2019-07-24 stsp
6865 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6866 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6867 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6868 0ebf8283 2019-07-24 stsp &ok_arg);
6869 0ebf8283 2019-07-24 stsp if (err)
6870 0ebf8283 2019-07-24 stsp goto done;
6871 0ebf8283 2019-07-24 stsp
6872 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6873 0ebf8283 2019-07-24 stsp if (err)
6874 0ebf8283 2019-07-24 stsp goto done;
6875 0ebf8283 2019-07-24 stsp
6876 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6877 0ebf8283 2019-07-24 stsp if (err)
6878 0ebf8283 2019-07-24 stsp goto done;
6879 0ebf8283 2019-07-24 stsp
6880 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6881 0ebf8283 2019-07-24 stsp worktree);
6882 0ebf8283 2019-07-24 stsp if (err)
6883 0ebf8283 2019-07-24 stsp goto done;
6884 0ebf8283 2019-07-24 stsp
6885 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6886 0ebf8283 2019-07-24 stsp 0);
6887 0ebf8283 2019-07-24 stsp if (err)
6888 0ebf8283 2019-07-24 stsp goto done;
6889 0ebf8283 2019-07-24 stsp
6890 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6891 0ebf8283 2019-07-24 stsp if (err)
6892 0ebf8283 2019-07-24 stsp goto done;
6893 0ebf8283 2019-07-24 stsp
6894 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6895 0ebf8283 2019-07-24 stsp if (err)
6896 0ebf8283 2019-07-24 stsp goto done;
6897 0ebf8283 2019-07-24 stsp
6898 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6899 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6900 0ebf8283 2019-07-24 stsp if (err)
6901 0ebf8283 2019-07-24 stsp goto done;
6902 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6903 0ebf8283 2019-07-24 stsp if (err)
6904 0ebf8283 2019-07-24 stsp goto done;
6905 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6906 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6907 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6908 0ebf8283 2019-07-24 stsp goto done;
6909 0ebf8283 2019-07-24 stsp }
6910 0ebf8283 2019-07-24 stsp
6911 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6912 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6913 0ebf8283 2019-07-24 stsp if (err)
6914 0ebf8283 2019-07-24 stsp goto done;
6915 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6916 0ebf8283 2019-07-24 stsp if (err)
6917 0ebf8283 2019-07-24 stsp goto done;
6918 0ebf8283 2019-07-24 stsp
6919 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6920 0ebf8283 2019-07-24 stsp if (err)
6921 0ebf8283 2019-07-24 stsp goto done;
6922 0ebf8283 2019-07-24 stsp done:
6923 0ebf8283 2019-07-24 stsp free(fileindex_path);
6924 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6925 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6926 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6927 0ebf8283 2019-07-24 stsp if (wt_branch)
6928 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6929 0ebf8283 2019-07-24 stsp if (err) {
6930 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6931 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6932 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6933 0ebf8283 2019-07-24 stsp }
6934 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6935 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6936 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6937 0ebf8283 2019-07-24 stsp }
6938 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6939 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6940 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6941 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6942 3e3a69f1 2019-07-25 stsp }
6943 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6944 0ebf8283 2019-07-24 stsp }
6945 0ebf8283 2019-07-24 stsp return err;
6946 0ebf8283 2019-07-24 stsp }
6947 0ebf8283 2019-07-24 stsp
6948 0ebf8283 2019-07-24 stsp const struct got_error *
6949 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6950 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6951 0ebf8283 2019-07-24 stsp {
6952 3e3a69f1 2019-07-25 stsp if (fileindex)
6953 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6954 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6955 0ebf8283 2019-07-24 stsp }
6956 0ebf8283 2019-07-24 stsp
6957 0ebf8283 2019-07-24 stsp const struct got_error *
6958 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6959 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6960 0ebf8283 2019-07-24 stsp {
6961 0ebf8283 2019-07-24 stsp const struct got_error *err;
6962 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6963 0ebf8283 2019-07-24 stsp
6964 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6965 0ebf8283 2019-07-24 stsp if (err)
6966 0ebf8283 2019-07-24 stsp return err;
6967 0ebf8283 2019-07-24 stsp
6968 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6969 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6970 0ebf8283 2019-07-24 stsp return NULL;
6971 0ebf8283 2019-07-24 stsp }
6972 0ebf8283 2019-07-24 stsp
6973 0ebf8283 2019-07-24 stsp const struct got_error *
6974 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6975 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6976 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6977 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6978 0ebf8283 2019-07-24 stsp {
6979 0ebf8283 2019-07-24 stsp const struct got_error *err;
6980 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6981 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6982 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6983 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6984 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6985 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6986 0ebf8283 2019-07-24 stsp
6987 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6988 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6989 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6990 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6991 0ebf8283 2019-07-24 stsp
6992 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6993 3e3a69f1 2019-07-25 stsp if (err)
6994 3e3a69f1 2019-07-25 stsp return err;
6995 3e3a69f1 2019-07-25 stsp
6996 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6997 3e3a69f1 2019-07-25 stsp if (err)
6998 f032f1f7 2019-08-04 stsp goto done;
6999 f032f1f7 2019-08-04 stsp
7000 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7001 f032f1f7 2019-08-04 stsp &have_staged_files);
7002 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
7003 f032f1f7 2019-08-04 stsp goto done;
7004 f032f1f7 2019-08-04 stsp if (have_staged_files) {
7005 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7006 3e3a69f1 2019-07-25 stsp goto done;
7007 f032f1f7 2019-08-04 stsp }
7008 3e3a69f1 2019-07-25 stsp
7009 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7010 0ebf8283 2019-07-24 stsp if (err)
7011 f032f1f7 2019-08-04 stsp goto done;
7012 0ebf8283 2019-07-24 stsp
7013 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7014 0ebf8283 2019-07-24 stsp if (err)
7015 0ebf8283 2019-07-24 stsp goto done;
7016 0ebf8283 2019-07-24 stsp
7017 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7018 0ebf8283 2019-07-24 stsp if (err)
7019 0ebf8283 2019-07-24 stsp goto done;
7020 0ebf8283 2019-07-24 stsp
7021 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7022 0ebf8283 2019-07-24 stsp worktree);
7023 0ebf8283 2019-07-24 stsp if (err)
7024 0ebf8283 2019-07-24 stsp goto done;
7025 0ebf8283 2019-07-24 stsp
7026 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
7027 0ebf8283 2019-07-24 stsp if (err)
7028 0ebf8283 2019-07-24 stsp goto done;
7029 0ebf8283 2019-07-24 stsp
7030 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
7031 0ebf8283 2019-07-24 stsp if (err)
7032 0ebf8283 2019-07-24 stsp goto done;
7033 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
7034 0ebf8283 2019-07-24 stsp if (err)
7035 0ebf8283 2019-07-24 stsp goto done;
7036 0ebf8283 2019-07-24 stsp
7037 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
7038 0ebf8283 2019-07-24 stsp if (err)
7039 0ebf8283 2019-07-24 stsp goto done;
7040 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
7041 0ebf8283 2019-07-24 stsp if (err)
7042 0ebf8283 2019-07-24 stsp goto done;
7043 0ebf8283 2019-07-24 stsp
7044 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
7045 0ebf8283 2019-07-24 stsp if (err)
7046 0ebf8283 2019-07-24 stsp goto done;
7047 0ebf8283 2019-07-24 stsp done:
7048 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7049 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7050 3e3a69f1 2019-07-25 stsp free(fileindex_path);
7051 0ebf8283 2019-07-24 stsp if (commit_ref)
7052 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
7053 0ebf8283 2019-07-24 stsp if (base_commit_ref)
7054 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
7055 0ebf8283 2019-07-24 stsp if (err) {
7056 0ebf8283 2019-07-24 stsp free(*commit_id);
7057 0ebf8283 2019-07-24 stsp *commit_id = NULL;
7058 0ebf8283 2019-07-24 stsp free(*base_commit_id);
7059 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
7060 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
7061 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
7062 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
7063 0ebf8283 2019-07-24 stsp }
7064 3e3a69f1 2019-07-25 stsp if (*fileindex) {
7065 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
7066 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
7067 3e3a69f1 2019-07-25 stsp }
7068 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
7069 0ebf8283 2019-07-24 stsp }
7070 0ebf8283 2019-07-24 stsp return err;
7071 0ebf8283 2019-07-24 stsp }
7072 0ebf8283 2019-07-24 stsp
7073 0ebf8283 2019-07-24 stsp static const struct got_error *
7074 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
7075 0ebf8283 2019-07-24 stsp {
7076 0ebf8283 2019-07-24 stsp const struct got_error *err;
7077 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
7078 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
7079 0ebf8283 2019-07-24 stsp
7080 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
7081 0ebf8283 2019-07-24 stsp if (err)
7082 0ebf8283 2019-07-24 stsp goto done;
7083 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
7084 0ebf8283 2019-07-24 stsp if (err)
7085 0ebf8283 2019-07-24 stsp goto done;
7086 0ebf8283 2019-07-24 stsp
7087 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
7088 0ebf8283 2019-07-24 stsp worktree);
7089 0ebf8283 2019-07-24 stsp if (err)
7090 0ebf8283 2019-07-24 stsp goto done;
7091 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
7092 0ebf8283 2019-07-24 stsp if (err)
7093 0ebf8283 2019-07-24 stsp goto done;
7094 0ebf8283 2019-07-24 stsp
7095 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
7096 0ebf8283 2019-07-24 stsp if (err)
7097 0ebf8283 2019-07-24 stsp goto done;
7098 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
7099 0ebf8283 2019-07-24 stsp if (err)
7100 0ebf8283 2019-07-24 stsp goto done;
7101 0ebf8283 2019-07-24 stsp
7102 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7103 0ebf8283 2019-07-24 stsp if (err)
7104 0ebf8283 2019-07-24 stsp goto done;
7105 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7106 0ebf8283 2019-07-24 stsp if (err)
7107 0ebf8283 2019-07-24 stsp goto done;
7108 0ebf8283 2019-07-24 stsp done:
7109 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
7110 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
7111 0ebf8283 2019-07-24 stsp free(branch_ref_name);
7112 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7113 0ebf8283 2019-07-24 stsp return err;
7114 0ebf8283 2019-07-24 stsp }
7115 0ebf8283 2019-07-24 stsp
7116 0ebf8283 2019-07-24 stsp const struct got_error *
7117 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
7118 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7119 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
7120 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7121 0ebf8283 2019-07-24 stsp {
7122 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7123 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7124 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
7125 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
7126 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
7127 0ebf8283 2019-07-24 stsp
7128 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
7129 0ebf8283 2019-07-24 stsp if (err)
7130 0ebf8283 2019-07-24 stsp return err;
7131 0ebf8283 2019-07-24 stsp
7132 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7133 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
7134 0ebf8283 2019-07-24 stsp if (err)
7135 0ebf8283 2019-07-24 stsp goto done;
7136 0ebf8283 2019-07-24 stsp
7137 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7138 0ebf8283 2019-07-24 stsp if (err)
7139 0ebf8283 2019-07-24 stsp goto done;
7140 0ebf8283 2019-07-24 stsp
7141 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
7142 0ebf8283 2019-07-24 stsp if (err)
7143 0ebf8283 2019-07-24 stsp goto done;
7144 0ebf8283 2019-07-24 stsp
7145 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
7146 0ebf8283 2019-07-24 stsp worktree->path_prefix);
7147 0ebf8283 2019-07-24 stsp if (err)
7148 0ebf8283 2019-07-24 stsp goto done;
7149 0ebf8283 2019-07-24 stsp
7150 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7151 0ebf8283 2019-07-24 stsp if (err)
7152 0ebf8283 2019-07-24 stsp goto done;
7153 0ebf8283 2019-07-24 stsp
7154 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
7155 0ebf8283 2019-07-24 stsp if (err)
7156 0ebf8283 2019-07-24 stsp goto done;
7157 0ebf8283 2019-07-24 stsp
7158 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
7159 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
7160 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
7161 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
7162 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
7163 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
7164 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
7165 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 0;
7166 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7167 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
7168 0ebf8283 2019-07-24 stsp if (err)
7169 1f1abb7e 2019-08-08 stsp goto sync;
7170 0ebf8283 2019-07-24 stsp
7171 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7172 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7173 0ebf8283 2019-07-24 stsp sync:
7174 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7175 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
7176 0ebf8283 2019-07-24 stsp err = sync_err;
7177 0ebf8283 2019-07-24 stsp done:
7178 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7179 0ebf8283 2019-07-24 stsp free(tree_id);
7180 818c7501 2019-07-11 stsp free(fileindex_path);
7181 818c7501 2019-07-11 stsp
7182 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7183 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7184 818c7501 2019-07-11 stsp err = unlockerr;
7185 818c7501 2019-07-11 stsp return err;
7186 818c7501 2019-07-11 stsp }
7187 0ebf8283 2019-07-24 stsp
7188 0ebf8283 2019-07-24 stsp const struct got_error *
7189 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7190 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7191 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7192 0ebf8283 2019-07-24 stsp {
7193 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7194 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7195 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7196 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7197 0ebf8283 2019-07-24 stsp
7198 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7199 0ebf8283 2019-07-24 stsp if (err)
7200 0ebf8283 2019-07-24 stsp return err;
7201 0ebf8283 2019-07-24 stsp
7202 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7203 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7204 e600f124 2021-03-21 stsp if (err)
7205 e600f124 2021-03-21 stsp goto done;
7206 e600f124 2021-03-21 stsp
7207 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7208 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7209 0ebf8283 2019-07-24 stsp if (err)
7210 0ebf8283 2019-07-24 stsp goto done;
7211 0ebf8283 2019-07-24 stsp
7212 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7213 0ebf8283 2019-07-24 stsp if (err)
7214 0ebf8283 2019-07-24 stsp goto done;
7215 0ebf8283 2019-07-24 stsp
7216 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7217 0ebf8283 2019-07-24 stsp if (err)
7218 0ebf8283 2019-07-24 stsp goto done;
7219 0ebf8283 2019-07-24 stsp
7220 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
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 = delete_histedit_refs(worktree, repo);
7225 a615e0e7 2020-12-16 stsp if (err)
7226 a615e0e7 2020-12-16 stsp goto done;
7227 a615e0e7 2020-12-16 stsp
7228 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7229 a615e0e7 2020-12-16 stsp if (err)
7230 a615e0e7 2020-12-16 stsp goto done;
7231 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7232 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7233 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7234 a615e0e7 2020-12-16 stsp err = sync_err;
7235 0ebf8283 2019-07-24 stsp done:
7236 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7237 a615e0e7 2020-12-16 stsp free(fileindex_path);
7238 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7239 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7240 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7241 0ebf8283 2019-07-24 stsp err = unlockerr;
7242 0ebf8283 2019-07-24 stsp return err;
7243 0ebf8283 2019-07-24 stsp }
7244 0ebf8283 2019-07-24 stsp
7245 0ebf8283 2019-07-24 stsp const struct got_error *
7246 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7247 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7248 0ebf8283 2019-07-24 stsp {
7249 0ebf8283 2019-07-24 stsp const struct got_error *err;
7250 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7251 0ebf8283 2019-07-24 stsp
7252 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7253 0ebf8283 2019-07-24 stsp if (err)
7254 0ebf8283 2019-07-24 stsp return err;
7255 0ebf8283 2019-07-24 stsp
7256 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7257 0ebf8283 2019-07-24 stsp if (err)
7258 0ebf8283 2019-07-24 stsp goto done;
7259 0ebf8283 2019-07-24 stsp
7260 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7261 0ebf8283 2019-07-24 stsp done:
7262 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7263 2822a352 2019-10-15 stsp return err;
7264 2822a352 2019-10-15 stsp }
7265 2822a352 2019-10-15 stsp
7266 2822a352 2019-10-15 stsp const struct got_error *
7267 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7268 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7269 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7270 2822a352 2019-10-15 stsp struct got_repository *repo)
7271 2822a352 2019-10-15 stsp {
7272 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7273 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7274 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7275 2822a352 2019-10-15 stsp
7276 2822a352 2019-10-15 stsp *fileindex = NULL;
7277 2822a352 2019-10-15 stsp *branch_ref = NULL;
7278 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7279 2822a352 2019-10-15 stsp
7280 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7281 2822a352 2019-10-15 stsp if (err)
7282 2822a352 2019-10-15 stsp return err;
7283 2822a352 2019-10-15 stsp
7284 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7285 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7286 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7287 2822a352 2019-10-15 stsp "update -b or different branch name required");
7288 2822a352 2019-10-15 stsp goto done;
7289 2822a352 2019-10-15 stsp }
7290 2822a352 2019-10-15 stsp
7291 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7292 2822a352 2019-10-15 stsp if (err)
7293 2822a352 2019-10-15 stsp goto done;
7294 2822a352 2019-10-15 stsp
7295 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7296 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7297 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7298 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7299 2822a352 2019-10-15 stsp &ok_arg);
7300 2822a352 2019-10-15 stsp if (err)
7301 2822a352 2019-10-15 stsp goto done;
7302 2822a352 2019-10-15 stsp
7303 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
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 err = got_ref_open(base_branch_ref, repo,
7308 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7309 2822a352 2019-10-15 stsp done:
7310 2822a352 2019-10-15 stsp if (err) {
7311 2822a352 2019-10-15 stsp if (*branch_ref) {
7312 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7313 2822a352 2019-10-15 stsp *branch_ref = NULL;
7314 2822a352 2019-10-15 stsp }
7315 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7316 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7317 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7318 2822a352 2019-10-15 stsp }
7319 2822a352 2019-10-15 stsp if (*fileindex) {
7320 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7321 2822a352 2019-10-15 stsp *fileindex = NULL;
7322 2822a352 2019-10-15 stsp }
7323 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7324 2822a352 2019-10-15 stsp }
7325 0cb83759 2019-08-03 stsp return err;
7326 0cb83759 2019-08-03 stsp }
7327 0cb83759 2019-08-03 stsp
7328 2822a352 2019-10-15 stsp const struct got_error *
7329 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7330 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7331 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7332 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7333 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7334 2822a352 2019-10-15 stsp {
7335 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7336 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7337 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7338 2822a352 2019-10-15 stsp
7339 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7340 2822a352 2019-10-15 stsp if (err)
7341 2822a352 2019-10-15 stsp goto done;
7342 2822a352 2019-10-15 stsp
7343 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7344 2822a352 2019-10-15 stsp if (err)
7345 2822a352 2019-10-15 stsp goto done;
7346 2822a352 2019-10-15 stsp
7347 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7348 2822a352 2019-10-15 stsp worktree->path_prefix);
7349 2822a352 2019-10-15 stsp if (err)
7350 2822a352 2019-10-15 stsp goto done;
7351 2822a352 2019-10-15 stsp
7352 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7353 2822a352 2019-10-15 stsp if (err)
7354 2822a352 2019-10-15 stsp goto done;
7355 2822a352 2019-10-15 stsp
7356 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7357 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7358 2822a352 2019-10-15 stsp if (err)
7359 2822a352 2019-10-15 stsp goto sync;
7360 2822a352 2019-10-15 stsp
7361 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7362 2822a352 2019-10-15 stsp if (err)
7363 2822a352 2019-10-15 stsp goto sync;
7364 2822a352 2019-10-15 stsp
7365 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7366 6e210706 2021-01-22 stsp if (err)
7367 6e210706 2021-01-22 stsp goto sync;
7368 6e210706 2021-01-22 stsp
7369 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7370 2822a352 2019-10-15 stsp sync:
7371 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7372 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7373 2822a352 2019-10-15 stsp err = sync_err;
7374 2822a352 2019-10-15 stsp
7375 2822a352 2019-10-15 stsp done:
7376 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7377 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7378 2822a352 2019-10-15 stsp err = unlockerr;
7379 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7380 2822a352 2019-10-15 stsp
7381 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7382 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7383 2822a352 2019-10-15 stsp err = unlockerr;
7384 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7385 2822a352 2019-10-15 stsp
7386 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7387 2822a352 2019-10-15 stsp free(fileindex_path);
7388 2822a352 2019-10-15 stsp free(tree_id);
7389 2822a352 2019-10-15 stsp
7390 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7391 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7392 2822a352 2019-10-15 stsp err = unlockerr;
7393 2822a352 2019-10-15 stsp return err;
7394 2822a352 2019-10-15 stsp }
7395 2822a352 2019-10-15 stsp
7396 2822a352 2019-10-15 stsp const struct got_error *
7397 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7398 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7399 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7400 2822a352 2019-10-15 stsp {
7401 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7402 8b692cd0 2019-10-21 stsp
7403 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7404 8b692cd0 2019-10-21 stsp
7405 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7406 8b692cd0 2019-10-21 stsp
7407 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7408 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7409 8b692cd0 2019-10-21 stsp err = unlockerr;
7410 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7411 8b692cd0 2019-10-21 stsp
7412 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7413 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7414 8b692cd0 2019-10-21 stsp err = unlockerr;
7415 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7416 f259c4c1 2021-09-24 stsp
7417 f259c4c1 2021-09-24 stsp return err;
7418 f259c4c1 2021-09-24 stsp }
7419 f259c4c1 2021-09-24 stsp
7420 f259c4c1 2021-09-24 stsp const struct got_error *
7421 f259c4c1 2021-09-24 stsp got_worktree_merge_postpone(struct got_worktree *worktree,
7422 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex)
7423 f259c4c1 2021-09-24 stsp {
7424 f259c4c1 2021-09-24 stsp const struct got_error *err, *sync_err;
7425 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7426 f259c4c1 2021-09-24 stsp
7427 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7428 f259c4c1 2021-09-24 stsp if (err)
7429 f259c4c1 2021-09-24 stsp goto done;
7430 8b692cd0 2019-10-21 stsp
7431 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7432 f259c4c1 2021-09-24 stsp
7433 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_SH);
7434 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7435 f259c4c1 2021-09-24 stsp err = sync_err;
7436 f259c4c1 2021-09-24 stsp done:
7437 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7438 f259c4c1 2021-09-24 stsp free(fileindex_path);
7439 8b692cd0 2019-10-21 stsp return err;
7440 2822a352 2019-10-15 stsp }
7441 2822a352 2019-10-15 stsp
7442 f259c4c1 2021-09-24 stsp static const struct got_error *
7443 f259c4c1 2021-09-24 stsp delete_merge_refs(struct got_worktree *worktree, struct got_repository *repo)
7444 f259c4c1 2021-09-24 stsp {
7445 f259c4c1 2021-09-24 stsp const struct got_error *err;
7446 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7447 f259c4c1 2021-09-24 stsp
7448 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7449 f259c4c1 2021-09-24 stsp if (err)
7450 f259c4c1 2021-09-24 stsp goto done;
7451 f259c4c1 2021-09-24 stsp err = delete_ref(branch_refname, repo);
7452 f259c4c1 2021-09-24 stsp if (err)
7453 f259c4c1 2021-09-24 stsp goto done;
7454 f259c4c1 2021-09-24 stsp
7455 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7456 f259c4c1 2021-09-24 stsp if (err)
7457 f259c4c1 2021-09-24 stsp goto done;
7458 f259c4c1 2021-09-24 stsp err = delete_ref(commit_refname, repo);
7459 f259c4c1 2021-09-24 stsp if (err)
7460 f259c4c1 2021-09-24 stsp goto done;
7461 f259c4c1 2021-09-24 stsp
7462 f259c4c1 2021-09-24 stsp done:
7463 f259c4c1 2021-09-24 stsp free(branch_refname);
7464 f259c4c1 2021-09-24 stsp free(commit_refname);
7465 f259c4c1 2021-09-24 stsp return err;
7466 f259c4c1 2021-09-24 stsp }
7467 f259c4c1 2021-09-24 stsp
7468 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg {
7469 f259c4c1 2021-09-24 stsp struct got_worktree *worktree;
7470 f259c4c1 2021-09-24 stsp const char *branch_name;
7471 f259c4c1 2021-09-24 stsp };
7472 f259c4c1 2021-09-24 stsp
7473 f259c4c1 2021-09-24 stsp static const struct got_error *
7474 f259c4c1 2021-09-24 stsp merge_commit_msg_cb(struct got_pathlist_head *commitable_paths, char **logmsg,
7475 f259c4c1 2021-09-24 stsp void *arg)
7476 f259c4c1 2021-09-24 stsp {
7477 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg *a = arg;
7478 f259c4c1 2021-09-24 stsp
7479 f259c4c1 2021-09-24 stsp if (asprintf(logmsg, "merge %s into %s\n", a->branch_name,
7480 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(a->worktree)) == -1)
7481 f259c4c1 2021-09-24 stsp return got_error_from_errno("asprintf");
7482 f259c4c1 2021-09-24 stsp
7483 f259c4c1 2021-09-24 stsp return NULL;
7484 f259c4c1 2021-09-24 stsp }
7485 f259c4c1 2021-09-24 stsp
7486 f259c4c1 2021-09-24 stsp static const struct got_error *
7487 f259c4c1 2021-09-24 stsp merge_status_cb(void *arg, unsigned char status, unsigned char staged_status,
7488 f259c4c1 2021-09-24 stsp const char *path, struct got_object_id *blob_id,
7489 f259c4c1 2021-09-24 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7490 f259c4c1 2021-09-24 stsp int dirfd, const char *de_name)
7491 f259c4c1 2021-09-24 stsp {
7492 f259c4c1 2021-09-24 stsp return NULL;
7493 f259c4c1 2021-09-24 stsp }
7494 f259c4c1 2021-09-24 stsp
7495 f259c4c1 2021-09-24 stsp const struct got_error *
7496 f259c4c1 2021-09-24 stsp got_worktree_merge_branch(struct got_worktree *worktree,
7497 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex,
7498 f259c4c1 2021-09-24 stsp struct got_object_id *yca_commit_id,
7499 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip,
7500 f259c4c1 2021-09-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
7501 f259c4c1 2021-09-24 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
7502 f259c4c1 2021-09-24 stsp {
7503 f259c4c1 2021-09-24 stsp const struct got_error *err;
7504 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7505 f259c4c1 2021-09-24 stsp
7506 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7507 f259c4c1 2021-09-24 stsp if (err)
7508 f259c4c1 2021-09-24 stsp goto done;
7509 f259c4c1 2021-09-24 stsp
7510 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_mixed_commits,
7511 f259c4c1 2021-09-24 stsp worktree);
7512 f259c4c1 2021-09-24 stsp if (err)
7513 f259c4c1 2021-09-24 stsp goto done;
7514 f259c4c1 2021-09-24 stsp
7515 f259c4c1 2021-09-24 stsp err = merge_files(worktree, fileindex, fileindex_path, yca_commit_id,
7516 f259c4c1 2021-09-24 stsp branch_tip, repo, progress_cb, progress_arg,
7517 f259c4c1 2021-09-24 stsp cancel_cb, cancel_arg);
7518 f259c4c1 2021-09-24 stsp done:
7519 f259c4c1 2021-09-24 stsp free(fileindex_path);
7520 f259c4c1 2021-09-24 stsp return err;
7521 f259c4c1 2021-09-24 stsp }
7522 f259c4c1 2021-09-24 stsp
7523 f259c4c1 2021-09-24 stsp const struct got_error *
7524 f259c4c1 2021-09-24 stsp got_worktree_merge_commit(struct got_object_id **new_commit_id,
7525 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7526 f259c4c1 2021-09-24 stsp const char *author, const char *committer, int allow_bad_symlinks,
7527 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip, const char *branch_name,
7528 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7529 f259c4c1 2021-09-24 stsp {
7530 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL, *sync_err;
7531 f259c4c1 2021-09-24 stsp struct got_pathlist_head commitable_paths;
7532 f259c4c1 2021-09-24 stsp struct collect_commitables_arg cc_arg;
7533 f259c4c1 2021-09-24 stsp struct got_pathlist_entry *pe;
7534 f259c4c1 2021-09-24 stsp struct got_reference *head_ref = NULL;
7535 f259c4c1 2021-09-24 stsp struct got_object_id *head_commit_id = NULL;
7536 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7537 f259c4c1 2021-09-24 stsp struct merge_commit_msg_arg mcm_arg;
7538 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7539 f259c4c1 2021-09-24 stsp
7540 f259c4c1 2021-09-24 stsp *new_commit_id = NULL;
7541 f259c4c1 2021-09-24 stsp
7542 f259c4c1 2021-09-24 stsp TAILQ_INIT(&commitable_paths);
7543 f259c4c1 2021-09-24 stsp
7544 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7545 f259c4c1 2021-09-24 stsp if (err)
7546 f259c4c1 2021-09-24 stsp goto done;
7547 f259c4c1 2021-09-24 stsp
7548 f259c4c1 2021-09-24 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
7549 f259c4c1 2021-09-24 stsp if (err)
7550 f259c4c1 2021-09-24 stsp goto done;
7551 f259c4c1 2021-09-24 stsp
7552 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7553 f259c4c1 2021-09-24 stsp if (err)
7554 f259c4c1 2021-09-24 stsp goto done;
7555 f259c4c1 2021-09-24 stsp
7556 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
7557 f259c4c1 2021-09-24 stsp &have_staged_files);
7558 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7559 f259c4c1 2021-09-24 stsp goto done;
7560 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7561 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_STAGED_PATHS);
7562 f259c4c1 2021-09-24 stsp goto done;
7563 f259c4c1 2021-09-24 stsp }
7564 f259c4c1 2021-09-24 stsp
7565 f259c4c1 2021-09-24 stsp cc_arg.commitable_paths = &commitable_paths;
7566 f259c4c1 2021-09-24 stsp cc_arg.worktree = worktree;
7567 f259c4c1 2021-09-24 stsp cc_arg.fileindex = fileindex;
7568 f259c4c1 2021-09-24 stsp cc_arg.repo = repo;
7569 f259c4c1 2021-09-24 stsp cc_arg.have_staged_files = have_staged_files;
7570 f259c4c1 2021-09-24 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
7571 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7572 f259c4c1 2021-09-24 stsp collect_commitables, &cc_arg, NULL, NULL, 0, 0);
7573 f259c4c1 2021-09-24 stsp if (err)
7574 f259c4c1 2021-09-24 stsp goto done;
7575 f259c4c1 2021-09-24 stsp
7576 f259c4c1 2021-09-24 stsp if (TAILQ_EMPTY(&commitable_paths)) {
7577 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_COMMIT_NO_CHANGES,
7578 f259c4c1 2021-09-24 stsp "merge of %s cannot proceed", branch_name);
7579 f259c4c1 2021-09-24 stsp goto done;
7580 f259c4c1 2021-09-24 stsp }
7581 f259c4c1 2021-09-24 stsp
7582 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7583 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7584 f259c4c1 2021-09-24 stsp const char *ct_path = ct->in_repo_path;
7585 f259c4c1 2021-09-24 stsp
7586 f259c4c1 2021-09-24 stsp while (ct_path[0] == '/')
7587 f259c4c1 2021-09-24 stsp ct_path++;
7588 f259c4c1 2021-09-24 stsp err = check_out_of_date(ct_path, ct->status,
7589 f259c4c1 2021-09-24 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
7590 f259c4c1 2021-09-24 stsp head_commit_id, repo, GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
7591 f259c4c1 2021-09-24 stsp if (err)
7592 f259c4c1 2021-09-24 stsp goto done;
7593 f259c4c1 2021-09-24 stsp
7594 f259c4c1 2021-09-24 stsp }
7595 f259c4c1 2021-09-24 stsp
7596 f259c4c1 2021-09-24 stsp mcm_arg.worktree = worktree;
7597 f259c4c1 2021-09-24 stsp mcm_arg.branch_name = branch_name;
7598 f259c4c1 2021-09-24 stsp err = commit_worktree(new_commit_id, &commitable_paths,
7599 f259c4c1 2021-09-24 stsp head_commit_id, branch_tip, worktree, author, committer,
7600 f259c4c1 2021-09-24 stsp merge_commit_msg_cb, &mcm_arg, merge_status_cb, NULL, repo);
7601 f259c4c1 2021-09-24 stsp if (err)
7602 f259c4c1 2021-09-24 stsp goto done;
7603 f259c4c1 2021-09-24 stsp
7604 f259c4c1 2021-09-24 stsp err = update_fileindex_after_commit(worktree, &commitable_paths,
7605 f259c4c1 2021-09-24 stsp *new_commit_id, fileindex, have_staged_files);
7606 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7607 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7608 f259c4c1 2021-09-24 stsp err = sync_err;
7609 f259c4c1 2021-09-24 stsp done:
7610 f259c4c1 2021-09-24 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
7611 f259c4c1 2021-09-24 stsp struct got_commitable *ct = pe->data;
7612 f259c4c1 2021-09-24 stsp free_commitable(ct);
7613 f259c4c1 2021-09-24 stsp }
7614 f259c4c1 2021-09-24 stsp got_pathlist_free(&commitable_paths);
7615 f259c4c1 2021-09-24 stsp free(fileindex_path);
7616 f259c4c1 2021-09-24 stsp return err;
7617 f259c4c1 2021-09-24 stsp }
7618 f259c4c1 2021-09-24 stsp
7619 f259c4c1 2021-09-24 stsp const struct got_error *
7620 f259c4c1 2021-09-24 stsp got_worktree_merge_complete(struct got_worktree *worktree,
7621 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo)
7622 f259c4c1 2021-09-24 stsp {
7623 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7624 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7625 f259c4c1 2021-09-24 stsp
7626 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7627 f259c4c1 2021-09-24 stsp if (err)
7628 f259c4c1 2021-09-24 stsp goto done;
7629 f259c4c1 2021-09-24 stsp
7630 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7631 f259c4c1 2021-09-24 stsp if (err)
7632 f259c4c1 2021-09-24 stsp goto done;
7633 f259c4c1 2021-09-24 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7634 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7635 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7636 f259c4c1 2021-09-24 stsp err = sync_err;
7637 f259c4c1 2021-09-24 stsp done:
7638 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7639 f259c4c1 2021-09-24 stsp free(fileindex_path);
7640 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7641 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7642 f259c4c1 2021-09-24 stsp err = unlockerr;
7643 f259c4c1 2021-09-24 stsp return err;
7644 f259c4c1 2021-09-24 stsp }
7645 f259c4c1 2021-09-24 stsp
7646 f259c4c1 2021-09-24 stsp const struct got_error *
7647 f259c4c1 2021-09-24 stsp got_worktree_merge_in_progress(int *in_progress, struct got_worktree *worktree,
7648 f259c4c1 2021-09-24 stsp struct got_repository *repo)
7649 f259c4c1 2021-09-24 stsp {
7650 f259c4c1 2021-09-24 stsp const struct got_error *err;
7651 f259c4c1 2021-09-24 stsp char *branch_refname = NULL;
7652 f259c4c1 2021-09-24 stsp struct got_reference *branch_ref = NULL;
7653 f259c4c1 2021-09-24 stsp
7654 f259c4c1 2021-09-24 stsp *in_progress = 0;
7655 f259c4c1 2021-09-24 stsp
7656 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7657 f259c4c1 2021-09-24 stsp if (err)
7658 f259c4c1 2021-09-24 stsp return err;
7659 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7660 793fcac3 2021-09-24 stsp free(branch_refname);
7661 f259c4c1 2021-09-24 stsp if (err) {
7662 f259c4c1 2021-09-24 stsp if (err->code != GOT_ERR_NOT_REF)
7663 f259c4c1 2021-09-24 stsp return err;
7664 f259c4c1 2021-09-24 stsp } else
7665 f259c4c1 2021-09-24 stsp *in_progress = 1;
7666 f259c4c1 2021-09-24 stsp
7667 f259c4c1 2021-09-24 stsp return NULL;
7668 f259c4c1 2021-09-24 stsp }
7669 f259c4c1 2021-09-24 stsp
7670 f259c4c1 2021-09-24 stsp const struct got_error *got_worktree_merge_prepare(
7671 f259c4c1 2021-09-24 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
7672 f259c4c1 2021-09-24 stsp struct got_reference *branch, struct got_repository *repo)
7673 f259c4c1 2021-09-24 stsp {
7674 f259c4c1 2021-09-24 stsp const struct got_error *err = NULL;
7675 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7676 f259c4c1 2021-09-24 stsp char *branch_refname = NULL, *commit_refname = NULL;
7677 f259c4c1 2021-09-24 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
7678 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL;
7679 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *wt_branch_tip = NULL;
7680 f259c4c1 2021-09-24 stsp struct check_rebase_ok_arg ok_arg;
7681 f259c4c1 2021-09-24 stsp
7682 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7683 f259c4c1 2021-09-24 stsp
7684 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7685 f259c4c1 2021-09-24 stsp if (err)
7686 f259c4c1 2021-09-24 stsp return err;
7687 f259c4c1 2021-09-24 stsp
7688 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7689 f259c4c1 2021-09-24 stsp if (err)
7690 f259c4c1 2021-09-24 stsp goto done;
7691 f259c4c1 2021-09-24 stsp
7692 f259c4c1 2021-09-24 stsp /* Preconditions are the same as for rebase. */
7693 f259c4c1 2021-09-24 stsp ok_arg.worktree = worktree;
7694 f259c4c1 2021-09-24 stsp ok_arg.repo = repo;
7695 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7696 f259c4c1 2021-09-24 stsp &ok_arg);
7697 f259c4c1 2021-09-24 stsp if (err)
7698 f259c4c1 2021-09-24 stsp goto done;
7699 f259c4c1 2021-09-24 stsp
7700 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7701 f259c4c1 2021-09-24 stsp if (err)
7702 f259c4c1 2021-09-24 stsp return err;
7703 f259c4c1 2021-09-24 stsp
7704 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7705 f259c4c1 2021-09-24 stsp if (err)
7706 f259c4c1 2021-09-24 stsp return err;
7707 f259c4c1 2021-09-24 stsp
7708 f259c4c1 2021-09-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
7709 f259c4c1 2021-09-24 stsp 0);
7710 f259c4c1 2021-09-24 stsp if (err)
7711 f259c4c1 2021-09-24 stsp goto done;
7712 f259c4c1 2021-09-24 stsp
7713 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
7714 f259c4c1 2021-09-24 stsp if (err)
7715 f259c4c1 2021-09-24 stsp goto done;
7716 f259c4c1 2021-09-24 stsp
7717 f259c4c1 2021-09-24 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
7718 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_MERGE_OUT_OF_DATE);
7719 f259c4c1 2021-09-24 stsp goto done;
7720 f259c4c1 2021-09-24 stsp }
7721 f259c4c1 2021-09-24 stsp
7722 f259c4c1 2021-09-24 stsp err = got_ref_resolve(&branch_tip, repo, branch);
7723 f259c4c1 2021-09-24 stsp if (err)
7724 f259c4c1 2021-09-24 stsp goto done;
7725 f259c4c1 2021-09-24 stsp
7726 f259c4c1 2021-09-24 stsp err = got_ref_alloc_symref(&branch_ref, branch_refname, branch);
7727 f259c4c1 2021-09-24 stsp if (err)
7728 f259c4c1 2021-09-24 stsp goto done;
7729 f259c4c1 2021-09-24 stsp err = got_ref_write(branch_ref, repo);
7730 f259c4c1 2021-09-24 stsp if (err)
7731 f259c4c1 2021-09-24 stsp goto done;
7732 f259c4c1 2021-09-24 stsp
7733 f259c4c1 2021-09-24 stsp err = got_ref_alloc(&commit_ref, commit_refname, branch_tip);
7734 f259c4c1 2021-09-24 stsp if (err)
7735 f259c4c1 2021-09-24 stsp goto done;
7736 f259c4c1 2021-09-24 stsp err = got_ref_write(commit_ref, repo);
7737 f259c4c1 2021-09-24 stsp if (err)
7738 f259c4c1 2021-09-24 stsp goto done;
7739 f259c4c1 2021-09-24 stsp
7740 f259c4c1 2021-09-24 stsp done:
7741 f259c4c1 2021-09-24 stsp free(branch_refname);
7742 f259c4c1 2021-09-24 stsp free(commit_refname);
7743 f259c4c1 2021-09-24 stsp free(fileindex_path);
7744 f259c4c1 2021-09-24 stsp if (branch_ref)
7745 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7746 f259c4c1 2021-09-24 stsp if (commit_ref)
7747 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7748 f259c4c1 2021-09-24 stsp if (wt_branch)
7749 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
7750 f259c4c1 2021-09-24 stsp free(wt_branch_tip);
7751 f259c4c1 2021-09-24 stsp if (err) {
7752 f259c4c1 2021-09-24 stsp if (*fileindex) {
7753 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7754 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7755 f259c4c1 2021-09-24 stsp }
7756 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7757 f259c4c1 2021-09-24 stsp }
7758 f259c4c1 2021-09-24 stsp return err;
7759 f259c4c1 2021-09-24 stsp }
7760 f259c4c1 2021-09-24 stsp
7761 f259c4c1 2021-09-24 stsp const struct got_error *
7762 f259c4c1 2021-09-24 stsp got_worktree_merge_continue(char **branch_name,
7763 f259c4c1 2021-09-24 stsp struct got_object_id **branch_tip, struct got_fileindex **fileindex,
7764 f259c4c1 2021-09-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7765 f259c4c1 2021-09-24 stsp {
7766 f259c4c1 2021-09-24 stsp const struct got_error *err;
7767 f259c4c1 2021-09-24 stsp char *commit_refname = NULL, *branch_refname = NULL;
7768 f259c4c1 2021-09-24 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
7769 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7770 f259c4c1 2021-09-24 stsp int have_staged_files = 0;
7771 f259c4c1 2021-09-24 stsp
7772 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7773 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7774 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7775 f259c4c1 2021-09-24 stsp
7776 f259c4c1 2021-09-24 stsp err = lock_worktree(worktree, LOCK_EX);
7777 f259c4c1 2021-09-24 stsp if (err)
7778 f259c4c1 2021-09-24 stsp return err;
7779 f259c4c1 2021-09-24 stsp
7780 f259c4c1 2021-09-24 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7781 f259c4c1 2021-09-24 stsp if (err)
7782 f259c4c1 2021-09-24 stsp goto done;
7783 f259c4c1 2021-09-24 stsp
7784 f259c4c1 2021-09-24 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
7785 f259c4c1 2021-09-24 stsp &have_staged_files);
7786 f259c4c1 2021-09-24 stsp if (err && err->code != GOT_ERR_CANCELLED)
7787 f259c4c1 2021-09-24 stsp goto done;
7788 f259c4c1 2021-09-24 stsp if (have_staged_files) {
7789 f259c4c1 2021-09-24 stsp err = got_error(GOT_ERR_STAGED_PATHS);
7790 f259c4c1 2021-09-24 stsp goto done;
7791 f259c4c1 2021-09-24 stsp }
7792 f259c4c1 2021-09-24 stsp
7793 f259c4c1 2021-09-24 stsp err = get_merge_branch_ref_name(&branch_refname, worktree);
7794 f259c4c1 2021-09-24 stsp if (err)
7795 f259c4c1 2021-09-24 stsp goto done;
7796 f259c4c1 2021-09-24 stsp
7797 f259c4c1 2021-09-24 stsp err = get_merge_commit_ref_name(&commit_refname, worktree);
7798 f259c4c1 2021-09-24 stsp if (err)
7799 f259c4c1 2021-09-24 stsp goto done;
7800 f259c4c1 2021-09-24 stsp
7801 f259c4c1 2021-09-24 stsp err = got_ref_open(&branch_ref, repo, branch_refname, 0);
7802 f259c4c1 2021-09-24 stsp if (err)
7803 f259c4c1 2021-09-24 stsp goto done;
7804 f259c4c1 2021-09-24 stsp
7805 f259c4c1 2021-09-24 stsp if (!got_ref_is_symbolic(branch_ref)) {
7806 f259c4c1 2021-09-24 stsp err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
7807 f259c4c1 2021-09-24 stsp "%s is not a symbolic reference",
7808 f259c4c1 2021-09-24 stsp got_ref_get_name(branch_ref));
7809 f259c4c1 2021-09-24 stsp goto done;
7810 f259c4c1 2021-09-24 stsp }
7811 f259c4c1 2021-09-24 stsp *branch_name = strdup(got_ref_get_symref_target(branch_ref));
7812 f259c4c1 2021-09-24 stsp if (*branch_name == NULL) {
7813 f259c4c1 2021-09-24 stsp err = got_error_from_errno("strdup");
7814 f259c4c1 2021-09-24 stsp goto done;
7815 f259c4c1 2021-09-24 stsp }
7816 f259c4c1 2021-09-24 stsp
7817 f259c4c1 2021-09-24 stsp err = got_ref_open(&commit_ref, repo, commit_refname, 0);
7818 f259c4c1 2021-09-24 stsp if (err)
7819 f259c4c1 2021-09-24 stsp goto done;
7820 f259c4c1 2021-09-24 stsp
7821 f259c4c1 2021-09-24 stsp err = got_ref_resolve(branch_tip, repo, commit_ref);
7822 f259c4c1 2021-09-24 stsp if (err)
7823 f259c4c1 2021-09-24 stsp goto done;
7824 f259c4c1 2021-09-24 stsp done:
7825 f259c4c1 2021-09-24 stsp free(commit_refname);
7826 f259c4c1 2021-09-24 stsp free(branch_refname);
7827 f259c4c1 2021-09-24 stsp free(fileindex_path);
7828 f259c4c1 2021-09-24 stsp if (commit_ref)
7829 f259c4c1 2021-09-24 stsp got_ref_close(commit_ref);
7830 f259c4c1 2021-09-24 stsp if (branch_ref)
7831 f259c4c1 2021-09-24 stsp got_ref_close(branch_ref);
7832 f259c4c1 2021-09-24 stsp if (err) {
7833 f259c4c1 2021-09-24 stsp if (*branch_name) {
7834 f259c4c1 2021-09-24 stsp free(*branch_name);
7835 f259c4c1 2021-09-24 stsp *branch_name = NULL;
7836 f259c4c1 2021-09-24 stsp }
7837 f259c4c1 2021-09-24 stsp free(*branch_tip);
7838 f259c4c1 2021-09-24 stsp *branch_tip = NULL;
7839 f259c4c1 2021-09-24 stsp if (*fileindex) {
7840 f259c4c1 2021-09-24 stsp got_fileindex_free(*fileindex);
7841 f259c4c1 2021-09-24 stsp *fileindex = NULL;
7842 f259c4c1 2021-09-24 stsp }
7843 f259c4c1 2021-09-24 stsp lock_worktree(worktree, LOCK_SH);
7844 f259c4c1 2021-09-24 stsp }
7845 f259c4c1 2021-09-24 stsp return err;
7846 f259c4c1 2021-09-24 stsp }
7847 f259c4c1 2021-09-24 stsp
7848 f259c4c1 2021-09-24 stsp const struct got_error *
7849 f259c4c1 2021-09-24 stsp got_worktree_merge_abort(struct got_worktree *worktree,
7850 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7851 f259c4c1 2021-09-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7852 f259c4c1 2021-09-24 stsp {
7853 f259c4c1 2021-09-24 stsp const struct got_error *err, *unlockerr, *sync_err;
7854 f259c4c1 2021-09-24 stsp struct got_object_id *commit_id = NULL;
7855 f259c4c1 2021-09-24 stsp char *fileindex_path = NULL;
7856 f259c4c1 2021-09-24 stsp struct revert_file_args rfa;
7857 f259c4c1 2021-09-24 stsp struct got_object_id *tree_id = NULL;
7858 f259c4c1 2021-09-24 stsp
7859 f259c4c1 2021-09-24 stsp err = got_object_id_by_path(&tree_id, repo,
7860 f259c4c1 2021-09-24 stsp worktree->base_commit_id, worktree->path_prefix);
7861 f259c4c1 2021-09-24 stsp if (err)
7862 f259c4c1 2021-09-24 stsp goto done;
7863 f259c4c1 2021-09-24 stsp
7864 f259c4c1 2021-09-24 stsp err = delete_merge_refs(worktree, repo);
7865 f259c4c1 2021-09-24 stsp if (err)
7866 f259c4c1 2021-09-24 stsp goto done;
7867 f259c4c1 2021-09-24 stsp
7868 f259c4c1 2021-09-24 stsp err = get_fileindex_path(&fileindex_path, worktree);
7869 f259c4c1 2021-09-24 stsp if (err)
7870 f259c4c1 2021-09-24 stsp goto done;
7871 f259c4c1 2021-09-24 stsp
7872 f259c4c1 2021-09-24 stsp rfa.worktree = worktree;
7873 f259c4c1 2021-09-24 stsp rfa.fileindex = fileindex;
7874 f259c4c1 2021-09-24 stsp rfa.progress_cb = progress_cb;
7875 f259c4c1 2021-09-24 stsp rfa.progress_arg = progress_arg;
7876 f259c4c1 2021-09-24 stsp rfa.patch_cb = NULL;
7877 f259c4c1 2021-09-24 stsp rfa.patch_arg = NULL;
7878 f259c4c1 2021-09-24 stsp rfa.repo = repo;
7879 f259c4c1 2021-09-24 stsp rfa.unlink_added_files = 1;
7880 f259c4c1 2021-09-24 stsp err = worktree_status(worktree, "", fileindex, repo,
7881 f259c4c1 2021-09-24 stsp revert_file, &rfa, NULL, NULL, 0, 0);
7882 f259c4c1 2021-09-24 stsp if (err)
7883 f259c4c1 2021-09-24 stsp goto sync;
7884 f259c4c1 2021-09-24 stsp
7885 f259c4c1 2021-09-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7886 f259c4c1 2021-09-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
7887 f259c4c1 2021-09-24 stsp sync:
7888 f259c4c1 2021-09-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7889 f259c4c1 2021-09-24 stsp if (sync_err && err == NULL)
7890 f259c4c1 2021-09-24 stsp err = sync_err;
7891 f259c4c1 2021-09-24 stsp done:
7892 f259c4c1 2021-09-24 stsp free(tree_id);
7893 f259c4c1 2021-09-24 stsp free(commit_id);
7894 f259c4c1 2021-09-24 stsp if (fileindex)
7895 f259c4c1 2021-09-24 stsp got_fileindex_free(fileindex);
7896 f259c4c1 2021-09-24 stsp free(fileindex_path);
7897 f259c4c1 2021-09-24 stsp
7898 f259c4c1 2021-09-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7899 f259c4c1 2021-09-24 stsp if (unlockerr && err == NULL)
7900 f259c4c1 2021-09-24 stsp err = unlockerr;
7901 f259c4c1 2021-09-24 stsp return err;
7902 f259c4c1 2021-09-24 stsp }
7903 f259c4c1 2021-09-24 stsp
7904 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7905 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7906 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7907 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7908 2db2652d 2019-08-07 stsp struct got_repository *repo;
7909 2db2652d 2019-08-07 stsp int have_changes;
7910 2db2652d 2019-08-07 stsp };
7911 2db2652d 2019-08-07 stsp
7912 2db2652d 2019-08-07 stsp const struct got_error *
7913 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7914 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7915 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7916 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7917 735ef5ac 2019-08-03 stsp {
7918 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7919 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7920 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7921 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7922 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7923 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7924 8b13ce36 2019-08-08 stsp
7925 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7926 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7927 8b13ce36 2019-08-08 stsp return NULL;
7928 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7929 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7930 735ef5ac 2019-08-03 stsp
7931 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7932 735ef5ac 2019-08-03 stsp if (ie == NULL)
7933 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7934 735ef5ac 2019-08-03 stsp
7935 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7936 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7937 735ef5ac 2019-08-03 stsp relpath) == -1)
7938 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7939 735ef5ac 2019-08-03 stsp
7940 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7941 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7942 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7943 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7944 735ef5ac 2019-08-03 stsp }
7945 735ef5ac 2019-08-03 stsp
7946 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7947 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7948 3aa5969e 2019-08-06 stsp goto done;
7949 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7950 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7951 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7952 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7953 735ef5ac 2019-08-03 stsp goto done;
7954 3aa5969e 2019-08-06 stsp }
7955 735ef5ac 2019-08-03 stsp
7956 2db2652d 2019-08-07 stsp a->have_changes = 1;
7957 2db2652d 2019-08-07 stsp
7958 735ef5ac 2019-08-03 stsp p = in_repo_path;
7959 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7960 735ef5ac 2019-08-03 stsp p++;
7961 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7962 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7963 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7964 735ef5ac 2019-08-03 stsp done:
7965 735ef5ac 2019-08-03 stsp free(in_repo_path);
7966 dc424a06 2019-08-07 stsp return err;
7967 dc424a06 2019-08-07 stsp }
7968 dc424a06 2019-08-07 stsp
7969 2db2652d 2019-08-07 stsp struct stage_path_arg {
7970 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7971 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7972 2db2652d 2019-08-07 stsp struct got_repository *repo;
7973 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7974 2db2652d 2019-08-07 stsp void *status_arg;
7975 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7976 2db2652d 2019-08-07 stsp void *patch_arg;
7977 7b5dc508 2019-10-28 stsp int staged_something;
7978 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7979 2db2652d 2019-08-07 stsp };
7980 2db2652d 2019-08-07 stsp
7981 2db2652d 2019-08-07 stsp static const struct got_error *
7982 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7983 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7984 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7985 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7986 0cb83759 2019-08-03 stsp {
7987 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7988 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7989 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7990 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7991 0cb83759 2019-08-03 stsp uint32_t stage;
7992 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7993 0aeb8099 2020-07-23 stsp struct stat sb;
7994 8b13ce36 2019-08-08 stsp
7995 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7996 8b13ce36 2019-08-08 stsp return NULL;
7997 0cb83759 2019-08-03 stsp
7998 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7999 d3e7c587 2019-08-03 stsp if (ie == NULL)
8000 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8001 0cb83759 2019-08-03 stsp
8002 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
8003 2db2652d 2019-08-07 stsp relpath)== -1)
8004 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
8005 0cb83759 2019-08-03 stsp
8006 0cb83759 2019-08-03 stsp switch (status) {
8007 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
8008 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
8009 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
8010 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
8011 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
8012 0aeb8099 2020-07-23 stsp break;
8013 0aeb8099 2020-07-23 stsp }
8014 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8015 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
8016 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8017 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8018 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
8019 dc424a06 2019-08-07 stsp if (err)
8020 dc424a06 2019-08-07 stsp break;
8021 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
8022 dc424a06 2019-08-07 stsp break;
8023 dc424a06 2019-08-07 stsp } else {
8024 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
8025 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
8026 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
8027 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
8028 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
8029 dc424a06 2019-08-07 stsp break;
8030 dc424a06 2019-08-07 stsp }
8031 dc424a06 2019-08-07 stsp }
8032 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
8033 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
8034 0cb83759 2019-08-03 stsp if (err)
8035 dc424a06 2019-08-07 stsp break;
8036 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8037 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
8038 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
8039 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
8040 0cb83759 2019-08-03 stsp else
8041 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
8042 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8043 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
8044 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
8045 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
8046 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
8047 35213c7c 2020-07-23 stsp ssize_t target_len;
8048 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
8049 35213c7c 2020-07-23 stsp sizeof(target_path));
8050 35213c7c 2020-07-23 stsp if (target_len == -1) {
8051 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
8052 35213c7c 2020-07-23 stsp ondisk_path);
8053 35213c7c 2020-07-23 stsp break;
8054 35213c7c 2020-07-23 stsp }
8055 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
8056 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
8057 35213c7c 2020-07-23 stsp a->worktree->root_path);
8058 35213c7c 2020-07-23 stsp if (err)
8059 35213c7c 2020-07-23 stsp break;
8060 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
8061 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
8062 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
8063 35213c7c 2020-07-23 stsp break;
8064 35213c7c 2020-07-23 stsp }
8065 35213c7c 2020-07-23 stsp }
8066 35213c7c 2020-07-23 stsp if (is_bad_symlink)
8067 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8068 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
8069 35213c7c 2020-07-23 stsp else
8070 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8071 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
8072 0aeb8099 2020-07-23 stsp } else {
8073 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
8074 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
8075 0aeb8099 2020-07-23 stsp }
8076 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8077 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8078 dc424a06 2019-08-07 stsp break;
8079 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8080 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
8081 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
8082 0cb83759 2019-08-03 stsp break;
8083 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
8084 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
8085 d3e7c587 2019-08-03 stsp break;
8086 2db2652d 2019-08-07 stsp if (a->patch_cb) {
8087 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
8088 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
8089 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
8090 dc424a06 2019-08-07 stsp if (err)
8091 dc424a06 2019-08-07 stsp break;
8092 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8093 88f33a19 2019-08-08 stsp break;
8094 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8095 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8096 dc424a06 2019-08-07 stsp break;
8097 88f33a19 2019-08-08 stsp }
8098 dc424a06 2019-08-07 stsp }
8099 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
8100 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
8101 7b5dc508 2019-10-28 stsp a->staged_something = 1;
8102 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
8103 dc424a06 2019-08-07 stsp break;
8104 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
8105 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
8106 12463d8b 2019-12-13 stsp de_name);
8107 0cb83759 2019-08-03 stsp break;
8108 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
8109 d3e7c587 2019-08-03 stsp break;
8110 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
8111 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
8112 ebf48fd5 2019-08-03 stsp break;
8113 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
8114 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
8115 2a06fe5f 2019-08-24 stsp break;
8116 0cb83759 2019-08-03 stsp default:
8117 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
8118 537ac44b 2019-08-03 stsp break;
8119 0cb83759 2019-08-03 stsp }
8120 dc424a06 2019-08-07 stsp
8121 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
8122 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
8123 dc424a06 2019-08-07 stsp free(path_content);
8124 2db2652d 2019-08-07 stsp free(ondisk_path);
8125 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
8126 0ebf8283 2019-07-24 stsp return err;
8127 0ebf8283 2019-07-24 stsp }
8128 0cb83759 2019-08-03 stsp
8129 0cb83759 2019-08-03 stsp const struct got_error *
8130 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
8131 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
8132 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
8133 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8134 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
8135 0cb83759 2019-08-03 stsp {
8136 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8137 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
8138 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8139 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
8140 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
8141 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
8142 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
8143 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
8144 0cb83759 2019-08-03 stsp
8145 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8146 0cb83759 2019-08-03 stsp if (err)
8147 0cb83759 2019-08-03 stsp return err;
8148 0cb83759 2019-08-03 stsp
8149 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
8150 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
8151 735ef5ac 2019-08-03 stsp if (err)
8152 735ef5ac 2019-08-03 stsp goto done;
8153 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
8154 735ef5ac 2019-08-03 stsp if (err)
8155 735ef5ac 2019-08-03 stsp goto done;
8156 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8157 0cb83759 2019-08-03 stsp if (err)
8158 0cb83759 2019-08-03 stsp goto done;
8159 0cb83759 2019-08-03 stsp
8160 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
8161 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
8162 2db2652d 2019-08-07 stsp oka.worktree = worktree;
8163 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
8164 2db2652d 2019-08-07 stsp oka.repo = repo;
8165 2db2652d 2019-08-07 stsp oka.have_changes = 0;
8166 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8167 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8168 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
8169 735ef5ac 2019-08-03 stsp if (err)
8170 735ef5ac 2019-08-03 stsp goto done;
8171 735ef5ac 2019-08-03 stsp }
8172 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
8173 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8174 2db2652d 2019-08-07 stsp goto done;
8175 2db2652d 2019-08-07 stsp }
8176 735ef5ac 2019-08-03 stsp
8177 2db2652d 2019-08-07 stsp spa.worktree = worktree;
8178 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
8179 2db2652d 2019-08-07 stsp spa.repo = repo;
8180 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
8181 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
8182 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
8183 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
8184 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
8185 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
8186 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8187 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8188 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
8189 42005733 2019-08-03 stsp if (err)
8190 2db2652d 2019-08-07 stsp goto done;
8191 7b5dc508 2019-10-28 stsp }
8192 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
8193 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
8194 7b5dc508 2019-10-28 stsp goto done;
8195 0cb83759 2019-08-03 stsp }
8196 0cb83759 2019-08-03 stsp
8197 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8198 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
8199 0cb83759 2019-08-03 stsp err = sync_err;
8200 0cb83759 2019-08-03 stsp done:
8201 735ef5ac 2019-08-03 stsp if (head_ref)
8202 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
8203 735ef5ac 2019-08-03 stsp free(head_commit_id);
8204 0cb83759 2019-08-03 stsp free(fileindex_path);
8205 0cb83759 2019-08-03 stsp if (fileindex)
8206 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
8207 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8208 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
8209 0cb83759 2019-08-03 stsp err = unlockerr;
8210 0cb83759 2019-08-03 stsp return err;
8211 0cb83759 2019-08-03 stsp }
8212 ad493afc 2019-08-03 stsp
8213 ad493afc 2019-08-03 stsp struct unstage_path_arg {
8214 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
8215 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
8216 ad493afc 2019-08-03 stsp struct got_repository *repo;
8217 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
8218 ad493afc 2019-08-03 stsp void *progress_arg;
8219 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
8220 2e1f37b0 2019-08-08 stsp void *patch_arg;
8221 ad493afc 2019-08-03 stsp };
8222 2e1f37b0 2019-08-08 stsp
8223 2e1f37b0 2019-08-08 stsp static const struct got_error *
8224 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
8225 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
8226 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
8227 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
8228 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
8229 2e1f37b0 2019-08-08 stsp {
8230 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
8231 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
8232 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
8233 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
8234 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
8235 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
8236 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
8237 2e1f37b0 2019-08-08 stsp
8238 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8239 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8240 2e1f37b0 2019-08-08 stsp
8241 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
8242 2e1f37b0 2019-08-08 stsp if (err)
8243 2e1f37b0 2019-08-08 stsp return err;
8244 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
8245 2e1f37b0 2019-08-08 stsp if (err)
8246 2e1f37b0 2019-08-08 stsp goto done;
8247 2e1f37b0 2019-08-08 stsp
8248 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
8249 2e1f37b0 2019-08-08 stsp if (err)
8250 2e1f37b0 2019-08-08 stsp goto done;
8251 2e1f37b0 2019-08-08 stsp
8252 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
8253 2e1f37b0 2019-08-08 stsp if (err)
8254 2e1f37b0 2019-08-08 stsp goto done;
8255 2e1f37b0 2019-08-08 stsp
8256 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_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(&path2, &f2, "got-unstage-blob-staged");
8261 2e1f37b0 2019-08-08 stsp if (err)
8262 2e1f37b0 2019-08-08 stsp goto done;
8263 ad493afc 2019-08-03 stsp
8264 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
8265 2e1f37b0 2019-08-08 stsp if (err)
8266 2e1f37b0 2019-08-08 stsp goto done;
8267 2e1f37b0 2019-08-08 stsp
8268 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
8269 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
8270 2e1f37b0 2019-08-08 stsp if (err)
8271 2e1f37b0 2019-08-08 stsp goto done;
8272 2e1f37b0 2019-08-08 stsp
8273 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
8274 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
8275 2e1f37b0 2019-08-08 stsp if (err)
8276 2e1f37b0 2019-08-08 stsp goto done;
8277 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
8278 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
8279 2e1f37b0 2019-08-08 stsp if (err)
8280 2e1f37b0 2019-08-08 stsp goto done;
8281 2e1f37b0 2019-08-08 stsp
8282 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
8283 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
8284 2e1f37b0 2019-08-08 stsp goto done;
8285 2e1f37b0 2019-08-08 stsp }
8286 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
8287 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
8288 2e1f37b0 2019-08-08 stsp goto done;
8289 2e1f37b0 2019-08-08 stsp }
8290 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
8291 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8292 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
8293 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
8294 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
8295 fe621944 2020-11-10 stsp nchanges++;
8296 fe621944 2020-11-10 stsp }
8297 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
8298 2e1f37b0 2019-08-08 stsp int choice;
8299 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
8300 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
8301 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
8302 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
8303 2e1f37b0 2019-08-08 stsp if (err)
8304 2e1f37b0 2019-08-08 stsp goto done;
8305 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
8306 2e1f37b0 2019-08-08 stsp have_content = 1;
8307 19e4b907 2019-08-08 stsp else
8308 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
8309 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
8310 2e1f37b0 2019-08-08 stsp break;
8311 2e1f37b0 2019-08-08 stsp }
8312 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
8313 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
8314 f1e81a05 2019-08-10 stsp outfile, rejectfile);
8315 2e1f37b0 2019-08-08 stsp done:
8316 2e1f37b0 2019-08-08 stsp free(label1);
8317 2e1f37b0 2019-08-08 stsp if (blob)
8318 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
8319 2e1f37b0 2019-08-08 stsp if (staged_blob)
8320 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
8321 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
8322 fe621944 2020-11-10 stsp if (free_err && err == NULL)
8323 fe621944 2020-11-10 stsp err = free_err;
8324 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
8325 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
8326 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
8327 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
8328 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
8329 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
8330 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
8331 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
8332 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
8333 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
8334 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
8335 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
8336 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
8337 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
8338 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
8339 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8340 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
8341 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
8342 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
8343 2e1f37b0 2019-08-08 stsp }
8344 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
8345 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
8346 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
8347 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
8348 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
8349 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
8350 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
8351 2e1f37b0 2019-08-08 stsp }
8352 2e1f37b0 2019-08-08 stsp free(path1);
8353 2e1f37b0 2019-08-08 stsp free(path2);
8354 fda8017d 2020-07-23 stsp return err;
8355 fda8017d 2020-07-23 stsp }
8356 fda8017d 2020-07-23 stsp
8357 fda8017d 2020-07-23 stsp static const struct got_error *
8358 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
8359 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
8360 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
8361 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
8362 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
8363 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8364 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
8365 fda8017d 2020-07-23 stsp {
8366 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
8367 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
8368 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
8369 67a66647 2021-05-31 stsp char *parent = NULL, *base_path = NULL;
8370 67a66647 2021-05-31 stsp char *blob_base_path = NULL;
8371 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
8372 eec2f5a9 2021-06-03 stsp FILE *f = NULL, *f_base = NULL, *f_deriv2 = NULL;
8373 fda8017d 2020-07-23 stsp struct stat sb;
8374 fda8017d 2020-07-23 stsp
8375 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
8376 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
8377 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
8378 fda8017d 2020-07-23 stsp if (err)
8379 fda8017d 2020-07-23 stsp return err;
8380 fda8017d 2020-07-23 stsp
8381 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
8382 fda8017d 2020-07-23 stsp return NULL;
8383 fda8017d 2020-07-23 stsp
8384 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
8385 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
8386 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
8387 fda8017d 2020-07-23 stsp if (err)
8388 fda8017d 2020-07-23 stsp goto done;
8389 fda8017d 2020-07-23 stsp }
8390 fda8017d 2020-07-23 stsp
8391 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
8392 fda8017d 2020-07-23 stsp if (f == NULL) {
8393 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
8394 fda8017d 2020-07-23 stsp path_unstaged_content);
8395 fda8017d 2020-07-23 stsp goto done;
8396 fda8017d 2020-07-23 stsp }
8397 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
8398 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
8399 fda8017d 2020-07-23 stsp goto done;
8400 fda8017d 2020-07-23 stsp }
8401 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
8402 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
8403 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
8404 fda8017d 2020-07-23 stsp size_t r;
8405 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
8406 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
8407 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
8408 fda8017d 2020-07-23 stsp goto done;
8409 fda8017d 2020-07-23 stsp }
8410 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
8411 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
8412 fda8017d 2020-07-23 stsp goto done;
8413 fda8017d 2020-07-23 stsp }
8414 fda8017d 2020-07-23 stsp link_target[r] = '\0';
8415 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
8416 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
8417 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
8418 fda8017d 2020-07-23 stsp progress_arg);
8419 fda8017d 2020-07-23 stsp } else {
8420 fda8017d 2020-07-23 stsp int local_changes_subsumed;
8421 67a66647 2021-05-31 stsp
8422 67a66647 2021-05-31 stsp err = got_path_dirname(&parent, ondisk_path);
8423 67a66647 2021-05-31 stsp if (err)
8424 67a66647 2021-05-31 stsp return err;
8425 67a66647 2021-05-31 stsp
8426 67a66647 2021-05-31 stsp if (asprintf(&base_path, "%s/got-unstage-blob-orig",
8427 67a66647 2021-05-31 stsp parent) == -1) {
8428 67a66647 2021-05-31 stsp err = got_error_from_errno("asprintf");
8429 67a66647 2021-05-31 stsp base_path = NULL;
8430 67a66647 2021-05-31 stsp goto done;
8431 67a66647 2021-05-31 stsp }
8432 67a66647 2021-05-31 stsp
8433 67a66647 2021-05-31 stsp err = got_opentemp_named(&blob_base_path, &f_base, base_path);
8434 67a66647 2021-05-31 stsp if (err)
8435 67a66647 2021-05-31 stsp goto done;
8436 67a66647 2021-05-31 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
8437 67a66647 2021-05-31 stsp blob_base);
8438 67a66647 2021-05-31 stsp if (err)
8439 67a66647 2021-05-31 stsp goto done;
8440 67a66647 2021-05-31 stsp
8441 eec2f5a9 2021-06-03 stsp /*
8442 eec2f5a9 2021-06-03 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
8443 eec2f5a9 2021-06-03 stsp * target path into a temporary file and use that file with diff3.
8444 eec2f5a9 2021-06-03 stsp */
8445 eec2f5a9 2021-06-03 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8446 eec2f5a9 2021-06-03 stsp err = dump_symlink_target_path_to_file(&f_deriv2,
8447 eec2f5a9 2021-06-03 stsp ondisk_path);
8448 eec2f5a9 2021-06-03 stsp if (err)
8449 eec2f5a9 2021-06-03 stsp goto done;
8450 eec2f5a9 2021-06-03 stsp } else {
8451 eec2f5a9 2021-06-03 stsp int fd;
8452 eec2f5a9 2021-06-03 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
8453 eec2f5a9 2021-06-03 stsp if (fd == -1) {
8454 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("open", ondisk_path);
8455 eec2f5a9 2021-06-03 stsp goto done;
8456 eec2f5a9 2021-06-03 stsp }
8457 eec2f5a9 2021-06-03 stsp f_deriv2 = fdopen(fd, "r");
8458 eec2f5a9 2021-06-03 stsp if (f_deriv2 == NULL) {
8459 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fdopen", ondisk_path);
8460 eec2f5a9 2021-06-03 stsp close(fd);
8461 eec2f5a9 2021-06-03 stsp goto done;
8462 eec2f5a9 2021-06-03 stsp }
8463 eec2f5a9 2021-06-03 stsp }
8464 eec2f5a9 2021-06-03 stsp
8465 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
8466 eec2f5a9 2021-06-03 stsp f_base, f, f_deriv2, ondisk_path, ie->path,
8467 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
8468 fdf3c2d3 2021-06-17 stsp label_orig, "unstaged", NULL, GOT_DIFF_ALGORITHM_MYERS,
8469 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
8470 fda8017d 2020-07-23 stsp }
8471 fda8017d 2020-07-23 stsp if (err)
8472 fda8017d 2020-07-23 stsp goto done;
8473 fda8017d 2020-07-23 stsp
8474 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
8475 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
8476 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
8477 2ac8aa02 2020-11-09 stsp } else {
8478 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8479 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8480 2ac8aa02 2020-11-09 stsp }
8481 fda8017d 2020-07-23 stsp done:
8482 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
8483 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
8484 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
8485 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
8486 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
8487 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
8488 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
8489 67a66647 2021-05-31 stsp if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
8490 67a66647 2021-05-31 stsp err = got_error_from_errno2("unlink", blob_base_path);
8491 67a66647 2021-05-31 stsp if (f_base && fclose(f_base) == EOF && err == NULL)
8492 67a66647 2021-05-31 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8493 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
8494 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
8495 eec2f5a9 2021-06-03 stsp if (f_deriv2 && fclose(f_deriv2) == EOF && err == NULL)
8496 eec2f5a9 2021-06-03 stsp err = got_error_from_errno2("fclose", ondisk_path);
8497 fda8017d 2020-07-23 stsp free(path_unstaged_content);
8498 fda8017d 2020-07-23 stsp free(path_new_staged_content);
8499 67a66647 2021-05-31 stsp free(blob_base_path);
8500 67a66647 2021-05-31 stsp free(parent);
8501 67a66647 2021-05-31 stsp free(base_path);
8502 2e1f37b0 2019-08-08 stsp return err;
8503 2e1f37b0 2019-08-08 stsp }
8504 2e1f37b0 2019-08-08 stsp
8505 ad493afc 2019-08-03 stsp static const struct got_error *
8506 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
8507 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
8508 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8509 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8510 ad493afc 2019-08-03 stsp {
8511 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
8512 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
8513 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
8514 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
8515 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
8516 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
8517 ad493afc 2019-08-03 stsp int local_changes_subsumed;
8518 9bc94a15 2019-08-03 stsp struct stat sb;
8519 ad493afc 2019-08-03 stsp
8520 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
8521 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
8522 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
8523 2e1f37b0 2019-08-08 stsp return NULL;
8524 2e1f37b0 2019-08-08 stsp
8525 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
8526 ad493afc 2019-08-03 stsp if (ie == NULL)
8527 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
8528 9bc94a15 2019-08-03 stsp
8529 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
8530 9bc94a15 2019-08-03 stsp == -1)
8531 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
8532 ad493afc 2019-08-03 stsp
8533 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
8534 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
8535 f69721c3 2019-10-21 stsp if (err)
8536 f69721c3 2019-10-21 stsp goto done;
8537 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
8538 f69721c3 2019-10-21 stsp id_str) == -1) {
8539 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
8540 f69721c3 2019-10-21 stsp goto done;
8541 f69721c3 2019-10-21 stsp }
8542 f69721c3 2019-10-21 stsp
8543 ad493afc 2019-08-03 stsp switch (staged_status) {
8544 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
8545 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
8546 ad493afc 2019-08-03 stsp blob_id, 8192);
8547 ad493afc 2019-08-03 stsp if (err)
8548 ad493afc 2019-08-03 stsp break;
8549 ad493afc 2019-08-03 stsp /* fall through */
8550 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
8551 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8552 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
8553 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8554 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8555 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8556 2e1f37b0 2019-08-08 stsp if (err)
8557 2e1f37b0 2019-08-08 stsp break;
8558 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
8559 2e1f37b0 2019-08-08 stsp break;
8560 2e1f37b0 2019-08-08 stsp } else {
8561 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
8562 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
8563 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
8564 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
8565 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
8566 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
8567 2e1f37b0 2019-08-08 stsp }
8568 2e1f37b0 2019-08-08 stsp }
8569 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
8570 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
8571 ad493afc 2019-08-03 stsp if (err)
8572 ad493afc 2019-08-03 stsp break;
8573 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
8574 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
8575 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
8576 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
8577 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
8578 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
8579 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
8580 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8581 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
8582 ea7786be 2020-07-23 stsp break;
8583 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
8584 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
8585 36bf999c 2020-07-23 stsp char *staged_target;
8586 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
8587 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
8588 36bf999c 2020-07-23 stsp if (err)
8589 36bf999c 2020-07-23 stsp goto done;
8590 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
8591 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
8592 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
8593 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
8594 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
8595 36bf999c 2020-07-23 stsp free(staged_target);
8596 dfe9fba0 2020-07-23 stsp } else {
8597 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
8598 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
8599 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
8600 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
8601 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
8602 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
8603 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
8604 dfe9fba0 2020-07-23 stsp }
8605 ea7786be 2020-07-23 stsp break;
8606 ea7786be 2020-07-23 stsp default:
8607 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
8608 ea7786be 2020-07-23 stsp break;
8609 ea7786be 2020-07-23 stsp }
8610 2ac8aa02 2020-11-09 stsp if (err == NULL) {
8611 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
8612 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
8613 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8614 2ac8aa02 2020-11-09 stsp }
8615 ad493afc 2019-08-03 stsp break;
8616 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
8617 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
8618 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
8619 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
8620 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
8621 2e1f37b0 2019-08-08 stsp if (err)
8622 2e1f37b0 2019-08-08 stsp break;
8623 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
8624 2e1f37b0 2019-08-08 stsp break;
8625 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
8626 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
8627 2e1f37b0 2019-08-08 stsp break;
8628 2e1f37b0 2019-08-08 stsp }
8629 2e1f37b0 2019-08-08 stsp }
8630 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
8631 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
8632 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
8633 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
8634 9bc94a15 2019-08-03 stsp if (err)
8635 9bc94a15 2019-08-03 stsp break;
8636 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
8637 ad493afc 2019-08-03 stsp break;
8638 ad493afc 2019-08-03 stsp }
8639 f69721c3 2019-10-21 stsp done:
8640 ad493afc 2019-08-03 stsp free(ondisk_path);
8641 ad493afc 2019-08-03 stsp if (blob_base)
8642 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
8643 ad493afc 2019-08-03 stsp if (blob_staged)
8644 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
8645 f69721c3 2019-10-21 stsp free(id_str);
8646 f69721c3 2019-10-21 stsp free(label_orig);
8647 ad493afc 2019-08-03 stsp return err;
8648 ad493afc 2019-08-03 stsp }
8649 ad493afc 2019-08-03 stsp
8650 ad493afc 2019-08-03 stsp const struct got_error *
8651 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
8652 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
8653 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
8654 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
8655 ad493afc 2019-08-03 stsp struct got_repository *repo)
8656 ad493afc 2019-08-03 stsp {
8657 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
8658 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8659 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
8660 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
8661 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
8662 ad493afc 2019-08-03 stsp
8663 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
8664 ad493afc 2019-08-03 stsp if (err)
8665 ad493afc 2019-08-03 stsp return err;
8666 ad493afc 2019-08-03 stsp
8667 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8668 ad493afc 2019-08-03 stsp if (err)
8669 ad493afc 2019-08-03 stsp goto done;
8670 ad493afc 2019-08-03 stsp
8671 ad493afc 2019-08-03 stsp upa.worktree = worktree;
8672 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
8673 ad493afc 2019-08-03 stsp upa.repo = repo;
8674 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
8675 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
8676 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
8677 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
8678 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
8679 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
8680 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
8681 ad493afc 2019-08-03 stsp if (err)
8682 ad493afc 2019-08-03 stsp goto done;
8683 ad493afc 2019-08-03 stsp }
8684 ad493afc 2019-08-03 stsp
8685 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
8686 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
8687 ad493afc 2019-08-03 stsp err = sync_err;
8688 ad493afc 2019-08-03 stsp done:
8689 ad493afc 2019-08-03 stsp free(fileindex_path);
8690 ad493afc 2019-08-03 stsp if (fileindex)
8691 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
8692 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
8693 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
8694 ad493afc 2019-08-03 stsp err = unlockerr;
8695 ad493afc 2019-08-03 stsp return err;
8696 ad493afc 2019-08-03 stsp }
8697 b2118c49 2020-07-28 stsp
8698 b2118c49 2020-07-28 stsp struct report_file_info_arg {
8699 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
8700 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
8701 b2118c49 2020-07-28 stsp void *info_arg;
8702 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
8703 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
8704 b2118c49 2020-07-28 stsp void *cancel_arg;
8705 b2118c49 2020-07-28 stsp };
8706 b2118c49 2020-07-28 stsp
8707 b2118c49 2020-07-28 stsp static const struct got_error *
8708 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
8709 b2118c49 2020-07-28 stsp {
8710 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
8711 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
8712 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
8713 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8714 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
8715 b2118c49 2020-07-28 stsp int stage;
8716 b2118c49 2020-07-28 stsp
8717 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8718 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8719 b2118c49 2020-07-28 stsp
8720 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8721 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8722 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8723 b2118c49 2020-07-28 stsp break;
8724 b2118c49 2020-07-28 stsp }
8725 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8726 b2118c49 2020-07-28 stsp return NULL;
8727 b2118c49 2020-07-28 stsp
8728 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8729 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8730 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8731 b2118c49 2020-07-28 stsp }
8732 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8733 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8734 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8735 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8736 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8737 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8738 b2118c49 2020-07-28 stsp }
8739 b2118c49 2020-07-28 stsp
8740 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8741 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8742 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8743 b2118c49 2020-07-28 stsp }
8744 b2118c49 2020-07-28 stsp
8745 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8746 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8747 b2118c49 2020-07-28 stsp }
8748 b2118c49 2020-07-28 stsp
8749 b2118c49 2020-07-28 stsp const struct got_error *
8750 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8751 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8752 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8753 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8754 b2118c49 2020-07-28 stsp
8755 b2118c49 2020-07-28 stsp {
8756 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8757 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8758 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8759 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8760 b2118c49 2020-07-28 stsp
8761 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8762 b2118c49 2020-07-28 stsp if (err)
8763 b2118c49 2020-07-28 stsp return err;
8764 b2118c49 2020-07-28 stsp
8765 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8766 b2118c49 2020-07-28 stsp if (err)
8767 b2118c49 2020-07-28 stsp goto done;
8768 b2118c49 2020-07-28 stsp
8769 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8770 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8771 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8772 b2118c49 2020-07-28 stsp arg.paths = paths;
8773 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8774 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8775 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8776 b2118c49 2020-07-28 stsp &arg);
8777 b2118c49 2020-07-28 stsp done:
8778 b2118c49 2020-07-28 stsp free(fileindex_path);
8779 b2118c49 2020-07-28 stsp if (fileindex)
8780 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8781 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8782 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8783 b2118c49 2020-07-28 stsp err = unlockerr;
8784 b2118c49 2020-07-28 stsp return err;
8785 b2118c49 2020-07-28 stsp }