Blame


1 86c3caaf 2018-03-09 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
19 133d2798 2019-01-08 stsp #include <sys/tree.h>
20 86c3caaf 2018-03-09 stsp
21 d1f6d47b 2019-02-04 stsp #include <dirent.h>
22 12ce7a6c 2019-08-12 stsp #include <limits.h>
23 f5c49f82 2019-01-06 stsp #include <stddef.h>
24 86c3caaf 2018-03-09 stsp #include <string.h>
25 86c3caaf 2018-03-09 stsp #include <stdio.h>
26 86c3caaf 2018-03-09 stsp #include <stdlib.h>
27 303e14b5 2019-09-23 stsp #include <time.h>
28 86c3caaf 2018-03-09 stsp #include <fcntl.h>
29 86c3caaf 2018-03-09 stsp #include <errno.h>
30 86c3caaf 2018-03-09 stsp #include <unistd.h>
31 9d31a1d8 2018-03-11 stsp #include <sha1.h>
32 9d31a1d8 2018-03-11 stsp #include <zlib.h>
33 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
34 512f0d0e 2019-01-02 stsp #include <libgen.h>
35 ec22038e 2019-03-10 stsp #include <uuid.h>
36 7154f6ce 2019-03-27 stsp #include <util.h>
37 86c3caaf 2018-03-09 stsp
38 86c3caaf 2018-03-09 stsp #include "got_error.h"
39 86c3caaf 2018-03-09 stsp #include "got_repository.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 9d31a1d8 2018-03-11 stsp #include "got_object.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 e6209546 2019-08-22 stsp #include "got_cancel.h"
44 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 234035bc 2019-06-01 stsp #include "got_diff.h"
47 86c3caaf 2018-03-09 stsp
48 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
51 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
53 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
54 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
55 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
56 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
57 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
58 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
59 9d31a1d8 2018-03-11 stsp
60 9d31a1d8 2018-03-11 stsp #ifndef MIN
61 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 9d31a1d8 2018-03-11 stsp #endif
63 f69721c3 2019-10-21 stsp
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
65 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 86c3caaf 2018-03-09 stsp
67 99724ed4 2018-03-10 stsp static const struct got_error *
68 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
69 99724ed4 2018-03-10 stsp {
70 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
71 99724ed4 2018-03-10 stsp char *path;
72 99724ed4 2018-03-10 stsp
73 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
75 99724ed4 2018-03-10 stsp
76 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
77 99724ed4 2018-03-10 stsp free(path);
78 507dc3bb 2018-12-29 stsp return err;
79 507dc3bb 2018-12-29 stsp }
80 507dc3bb 2018-12-29 stsp
81 507dc3bb 2018-12-29 stsp static const struct got_error *
82 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
83 507dc3bb 2018-12-29 stsp {
84 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
85 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
86 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
87 507dc3bb 2018-12-29 stsp char *path = NULL;
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
91 507dc3bb 2018-12-29 stsp path = NULL;
92 507dc3bb 2018-12-29 stsp goto done;
93 507dc3bb 2018-12-29 stsp }
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
96 507dc3bb 2018-12-29 stsp if (err)
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp
99 507dc3bb 2018-12-29 stsp if (content) {
100 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
101 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
108 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
109 2a57020b 2019-02-20 stsp unlink(tmppath);
110 507dc3bb 2018-12-29 stsp goto done;
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp done:
114 56b63ca4 2021-01-22 stsp if (fclose(tmpfile) == EOF && err == NULL)
115 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
116 230a42bd 2019-05-11 jcs free(tmppath);
117 99724ed4 2018-03-10 stsp return err;
118 99724ed4 2018-03-10 stsp }
119 99724ed4 2018-03-10 stsp
120 09fe317a 2018-03-11 stsp static const struct got_error *
121 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
122 09fe317a 2018-03-11 stsp {
123 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
124 09fe317a 2018-03-11 stsp char *path;
125 09fe317a 2018-03-11 stsp int fd = -1;
126 09fe317a 2018-03-11 stsp ssize_t n;
127 09fe317a 2018-03-11 stsp struct stat sb;
128 09fe317a 2018-03-11 stsp
129 09fe317a 2018-03-11 stsp *content = NULL;
130 09fe317a 2018-03-11 stsp
131 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
133 09fe317a 2018-03-11 stsp path = NULL;
134 09fe317a 2018-03-11 stsp goto done;
135 09fe317a 2018-03-11 stsp }
136 09fe317a 2018-03-11 stsp
137 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
138 09fe317a 2018-03-11 stsp if (fd == -1) {
139 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
140 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 f02eaa22 2019-03-11 stsp else
142 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
143 ef99fdb1 2018-03-11 stsp goto done;
144 ef99fdb1 2018-03-11 stsp }
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
148 09fe317a 2018-03-11 stsp goto done;
149 09fe317a 2018-03-11 stsp }
150 09fe317a 2018-03-11 stsp
151 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
152 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
153 d10c9b58 2019-02-19 stsp goto done;
154 d10c9b58 2019-02-19 stsp }
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
158 09fe317a 2018-03-11 stsp goto done;
159 09fe317a 2018-03-11 stsp }
160 09fe317a 2018-03-11 stsp
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
164 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
165 09fe317a 2018-03-11 stsp goto done;
166 09fe317a 2018-03-11 stsp }
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
169 09fe317a 2018-03-11 stsp goto done;
170 09fe317a 2018-03-11 stsp }
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
172 09fe317a 2018-03-11 stsp
173 09fe317a 2018-03-11 stsp done:
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
176 09fe317a 2018-03-11 stsp free(path);
177 09fe317a 2018-03-11 stsp if (err) {
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp return err;
182 09fe317a 2018-03-11 stsp }
183 09fe317a 2018-03-11 stsp
184 024e9686 2019-05-14 stsp static const struct got_error *
185 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
186 024e9686 2019-05-14 stsp {
187 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
188 024e9686 2019-05-14 stsp char *refstr = NULL;
189 024e9686 2019-05-14 stsp
190 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
191 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
192 024e9686 2019-05-14 stsp if (refstr == NULL)
193 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
194 024e9686 2019-05-14 stsp } else {
195 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
196 024e9686 2019-05-14 stsp if (refstr == NULL)
197 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
198 024e9686 2019-05-14 stsp }
199 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
200 024e9686 2019-05-14 stsp free(refstr);
201 024e9686 2019-05-14 stsp return err;
202 024e9686 2019-05-14 stsp }
203 024e9686 2019-05-14 stsp
204 86c3caaf 2018-03-09 stsp const struct got_error *
205 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
206 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
207 86c3caaf 2018-03-09 stsp {
208 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
209 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
210 ec22038e 2019-03-10 stsp uuid_t uuid;
211 ec22038e 2019-03-10 stsp uint32_t uuid_status;
212 65596e15 2018-12-24 stsp int obj_type;
213 7ac97322 2018-03-11 stsp char *path_got = NULL;
214 1451e70d 2018-03-10 stsp char *formatstr = NULL;
215 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
216 65596e15 2018-12-24 stsp char *basestr = NULL;
217 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
218 65596e15 2018-12-24 stsp
219 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
221 0c48fee2 2019-03-11 stsp goto done;
222 0c48fee2 2019-03-11 stsp }
223 0c48fee2 2019-03-11 stsp
224 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
225 65596e15 2018-12-24 stsp if (err)
226 65596e15 2018-12-24 stsp return err;
227 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
228 65596e15 2018-12-24 stsp if (err)
229 65596e15 2018-12-24 stsp return err;
230 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
232 86c3caaf 2018-03-09 stsp
233 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
234 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
236 0bb8a95e 2018-03-12 stsp }
237 577ec78f 2018-03-11 stsp
238 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
239 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 86c3caaf 2018-03-09 stsp
244 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
245 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
247 86c3caaf 2018-03-09 stsp goto done;
248 86c3caaf 2018-03-09 stsp }
249 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
251 86c3caaf 2018-03-09 stsp goto done;
252 86c3caaf 2018-03-09 stsp }
253 86c3caaf 2018-03-09 stsp
254 056e7441 2018-03-11 stsp /* Create an empty lock file. */
255 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
256 056e7441 2018-03-11 stsp if (err)
257 056e7441 2018-03-11 stsp goto done;
258 056e7441 2018-03-11 stsp
259 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
260 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
261 99724ed4 2018-03-10 stsp if (err)
262 86c3caaf 2018-03-09 stsp goto done;
263 86c3caaf 2018-03-09 stsp
264 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
265 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
266 65596e15 2018-12-24 stsp if (err)
267 65596e15 2018-12-24 stsp goto done;
268 65596e15 2018-12-24 stsp
269 65596e15 2018-12-24 stsp /* Record our base commit. */
270 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
271 65596e15 2018-12-24 stsp if (err)
272 65596e15 2018-12-24 stsp goto done;
273 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 1451e70d 2018-03-10 stsp /* Store path to repository. */
278 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
280 99724ed4 2018-03-10 stsp if (err)
281 86c3caaf 2018-03-09 stsp goto done;
282 86c3caaf 2018-03-09 stsp
283 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
284 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
286 ec22038e 2019-03-10 stsp if (err)
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp
289 ec22038e 2019-03-10 stsp /* Generate UUID. */
290 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
291 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
292 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
293 ec22038e 2019-03-10 stsp goto done;
294 ec22038e 2019-03-10 stsp }
295 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
297 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
298 ec22038e 2019-03-10 stsp goto done;
299 ec22038e 2019-03-10 stsp }
300 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 577ec78f 2018-03-11 stsp if (err)
302 577ec78f 2018-03-11 stsp goto done;
303 577ec78f 2018-03-11 stsp
304 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
305 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
307 1451e70d 2018-03-10 stsp goto done;
308 1451e70d 2018-03-10 stsp }
309 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 99724ed4 2018-03-10 stsp if (err)
311 1451e70d 2018-03-10 stsp goto done;
312 1451e70d 2018-03-10 stsp
313 86c3caaf 2018-03-09 stsp done:
314 65596e15 2018-12-24 stsp free(commit_id);
315 7ac97322 2018-03-11 stsp free(path_got);
316 1451e70d 2018-03-10 stsp free(formatstr);
317 0bb8a95e 2018-03-12 stsp free(absprefix);
318 65596e15 2018-12-24 stsp free(basestr);
319 ec22038e 2019-03-10 stsp free(uuidstr);
320 86c3caaf 2018-03-09 stsp return err;
321 86c3caaf 2018-03-09 stsp }
322 86c3caaf 2018-03-09 stsp
323 247140b2 2019-02-05 stsp static const struct got_error *
324 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
325 86c3caaf 2018-03-09 stsp {
326 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
327 7ac97322 2018-03-11 stsp char *path_got;
328 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
329 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
330 056e7441 2018-03-11 stsp char *path_lock = NULL;
331 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
332 6d9d28c3 2018-03-11 stsp int version, fd = -1;
333 6d9d28c3 2018-03-11 stsp const char *errstr;
334 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
335 c442a90d 2019-03-10 stsp uint32_t uuid_status;
336 6d9d28c3 2018-03-11 stsp
337 6d9d28c3 2018-03-11 stsp *worktree = NULL;
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 7ac97322 2018-03-11 stsp path_got = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
347 056e7441 2018-03-11 stsp path_lock = NULL;
348 6d9d28c3 2018-03-11 stsp goto done;
349 6d9d28c3 2018-03-11 stsp }
350 6d9d28c3 2018-03-11 stsp
351 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 6d9d28c3 2018-03-11 stsp if (fd == -1) {
353 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
355 6d9d28c3 2018-03-11 stsp goto done;
356 6d9d28c3 2018-03-11 stsp }
357 6d9d28c3 2018-03-11 stsp
358 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
359 6d9d28c3 2018-03-11 stsp if (err)
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp
362 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 6d9d28c3 2018-03-11 stsp if (errstr) {
364 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
365 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
366 6d9d28c3 2018-03-11 stsp goto done;
367 6d9d28c3 2018-03-11 stsp }
368 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
374 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
375 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
376 6d9d28c3 2018-03-11 stsp goto done;
377 6d9d28c3 2018-03-11 stsp }
378 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
379 6d9d28c3 2018-03-11 stsp
380 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
381 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
382 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
383 6d9d28c3 2018-03-11 stsp goto done;
384 6d9d28c3 2018-03-11 stsp }
385 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
386 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
387 6d9d28c3 2018-03-11 stsp if (err)
388 6d9d28c3 2018-03-11 stsp goto done;
389 eaccb85f 2018-12-25 stsp
390 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
392 93a30277 2018-12-24 stsp if (err)
393 93a30277 2018-12-24 stsp goto done;
394 93a30277 2018-12-24 stsp
395 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
396 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
397 c442a90d 2019-03-10 stsp if (err)
398 c442a90d 2019-03-10 stsp goto done;
399 c442a90d 2019-03-10 stsp
400 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 eaccb85f 2018-12-25 stsp if (err)
402 c442a90d 2019-03-10 stsp goto done;
403 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
405 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
406 eaccb85f 2018-12-25 stsp goto done;
407 c442a90d 2019-03-10 stsp }
408 eaccb85f 2018-12-25 stsp
409 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
410 eaccb85f 2018-12-25 stsp if (err)
411 eaccb85f 2018-12-25 stsp goto done;
412 eaccb85f 2018-12-25 stsp
413 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 eaccb85f 2018-12-25 stsp base_commit_id_str);
415 f5baf295 2018-03-11 stsp if (err)
416 6d9d28c3 2018-03-11 stsp goto done;
417 6d9d28c3 2018-03-11 stsp
418 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
420 50b0790e 2020-09-11 stsp if (err)
421 50b0790e 2020-09-11 stsp goto done;
422 50b0790e 2020-09-11 stsp
423 50b0790e 2020-09-11 stsp if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 50b0790e 2020-09-11 stsp (*worktree)->root_path,
425 50b0790e 2020-09-11 stsp GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 50b0790e 2020-09-11 stsp err = got_error_from_errno("asprintf");
427 50b0790e 2020-09-11 stsp goto done;
428 50b0790e 2020-09-11 stsp }
429 50b0790e 2020-09-11 stsp
430 50b0790e 2020-09-11 stsp err = got_gotconfig_read(&(*worktree)->gotconfig,
431 50b0790e 2020-09-11 stsp (*worktree)->gotconfig_path);
432 437adc9d 2020-12-10 yzhong
433 437adc9d 2020-12-10 yzhong (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 437adc9d 2020-12-10 yzhong if ((*worktree)->root_fd == -1) {
435 437adc9d 2020-12-10 yzhong err = got_error_from_errno2("open", (*worktree)->root_path);
436 437adc9d 2020-12-10 yzhong goto done;
437 437adc9d 2020-12-10 yzhong }
438 6d9d28c3 2018-03-11 stsp done:
439 eaccb85f 2018-12-25 stsp if (repo)
440 eaccb85f 2018-12-25 stsp got_repo_close(repo);
441 7ac97322 2018-03-11 stsp free(path_got);
442 056e7441 2018-03-11 stsp free(path_lock);
443 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
444 c442a90d 2019-03-10 stsp free(uuidstr);
445 bd165944 2019-03-10 stsp free(formatstr);
446 6d9d28c3 2018-03-11 stsp if (err) {
447 6d9d28c3 2018-03-11 stsp if (fd != -1)
448 6d9d28c3 2018-03-11 stsp close(fd);
449 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
450 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
451 6d9d28c3 2018-03-11 stsp *worktree = NULL;
452 6d9d28c3 2018-03-11 stsp } else
453 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
454 6d9d28c3 2018-03-11 stsp
455 6d9d28c3 2018-03-11 stsp return err;
456 86c3caaf 2018-03-09 stsp }
457 247140b2 2019-02-05 stsp
458 247140b2 2019-02-05 stsp const struct got_error *
459 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
460 247140b2 2019-02-05 stsp {
461 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
462 aedea96d 2020-10-20 stsp char *worktree_path;
463 86c3caaf 2018-03-09 stsp
464 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
465 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
466 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
467 aedea96d 2020-10-20 stsp
468 aedea96d 2020-10-20 stsp for (;;) {
469 aedea96d 2020-10-20 stsp char *parent_path;
470 aedea96d 2020-10-20 stsp
471 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
472 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
473 aedea96d 2020-10-20 stsp free(worktree_path);
474 247140b2 2019-02-05 stsp return err;
475 aedea96d 2020-10-20 stsp }
476 aedea96d 2020-10-20 stsp if (*worktree) {
477 aedea96d 2020-10-20 stsp free(worktree_path);
478 aedea96d 2020-10-20 stsp return NULL;
479 aedea96d 2020-10-20 stsp }
480 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
481 aedea96d 2020-10-20 stsp break;
482 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
483 aedea96d 2020-10-20 stsp if (err) {
484 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
485 aedea96d 2020-10-20 stsp free(worktree_path);
486 aedea96d 2020-10-20 stsp return err;
487 aedea96d 2020-10-20 stsp }
488 aedea96d 2020-10-20 stsp break;
489 aedea96d 2020-10-20 stsp }
490 aedea96d 2020-10-20 stsp free(worktree_path);
491 aedea96d 2020-10-20 stsp worktree_path = parent_path;
492 aedea96d 2020-10-20 stsp }
493 247140b2 2019-02-05 stsp
494 aedea96d 2020-10-20 stsp free(worktree_path);
495 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
496 247140b2 2019-02-05 stsp }
497 247140b2 2019-02-05 stsp
498 3a6ce05a 2019-02-11 stsp const struct got_error *
499 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
500 86c3caaf 2018-03-09 stsp {
501 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
502 60e40e95 2021-01-22 stsp
503 a6b21eef 2021-01-21 stsp if (worktree->lockfd != -1) {
504 08578a35 2021-01-22 stsp if (close(worktree->lockfd) == -1)
505 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
506 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
507 a6b21eef 2021-01-21 stsp }
508 e7abd6b6 2021-01-22 stsp if (close(worktree->root_fd) == -1 && err == NULL)
509 e7abd6b6 2021-01-22 stsp err = got_error_from_errno2("close",
510 e7abd6b6 2021-01-22 stsp got_worktree_get_root_path(worktree));
511 60e40e95 2021-01-22 stsp free(worktree->repo_path);
512 60e40e95 2021-01-22 stsp free(worktree->path_prefix);
513 60e40e95 2021-01-22 stsp free(worktree->base_commit_id);
514 60e40e95 2021-01-22 stsp free(worktree->head_ref_name);
515 60e40e95 2021-01-22 stsp free(worktree->root_path);
516 60e40e95 2021-01-22 stsp free(worktree->gotconfig_path);
517 60e40e95 2021-01-22 stsp got_gotconfig_free(worktree->gotconfig);
518 a06ff56f 2021-01-21 naddy free(worktree);
519 3a6ce05a 2019-02-11 stsp return err;
520 86c3caaf 2018-03-09 stsp }
521 86c3caaf 2018-03-09 stsp
522 2fbdb5ae 2018-12-29 stsp const char *
523 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
524 c7f4312f 2019-02-05 stsp {
525 c7f4312f 2019-02-05 stsp return worktree->root_path;
526 c7f4312f 2019-02-05 stsp }
527 c7f4312f 2019-02-05 stsp
528 c7f4312f 2019-02-05 stsp const char *
529 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
530 86c3caaf 2018-03-09 stsp {
531 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
532 49520a32 2018-12-29 stsp }
533 49520a32 2018-12-29 stsp const char *
534 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
535 49520a32 2018-12-29 stsp {
536 f609be2e 2018-12-29 stsp return worktree->path_prefix;
537 e5dc7198 2018-12-29 stsp }
538 e5dc7198 2018-12-29 stsp
539 e5dc7198 2018-12-29 stsp const struct got_error *
540 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
541 e5dc7198 2018-12-29 stsp const char *path_prefix)
542 e5dc7198 2018-12-29 stsp {
543 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
544 e5dc7198 2018-12-29 stsp
545 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
546 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
547 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
548 e5dc7198 2018-12-29 stsp }
549 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
550 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
551 e5dc7198 2018-12-29 stsp free(absprefix);
552 e5dc7198 2018-12-29 stsp return NULL;
553 86c3caaf 2018-03-09 stsp }
554 86c3caaf 2018-03-09 stsp
555 bc70eb79 2019-05-09 stsp const char *
556 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
557 86c3caaf 2018-03-09 stsp {
558 36a38700 2019-05-10 stsp return worktree->head_ref_name;
559 024e9686 2019-05-14 stsp }
560 024e9686 2019-05-14 stsp
561 024e9686 2019-05-14 stsp const struct got_error *
562 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
563 024e9686 2019-05-14 stsp struct got_reference *head_ref)
564 024e9686 2019-05-14 stsp {
565 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
566 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
567 024e9686 2019-05-14 stsp
568 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
569 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
570 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
571 024e9686 2019-05-14 stsp path_got = NULL;
572 024e9686 2019-05-14 stsp goto done;
573 024e9686 2019-05-14 stsp }
574 024e9686 2019-05-14 stsp
575 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
576 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
577 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
578 024e9686 2019-05-14 stsp goto done;
579 024e9686 2019-05-14 stsp }
580 024e9686 2019-05-14 stsp
581 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
582 024e9686 2019-05-14 stsp if (err)
583 024e9686 2019-05-14 stsp goto done;
584 024e9686 2019-05-14 stsp
585 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
586 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
587 024e9686 2019-05-14 stsp done:
588 024e9686 2019-05-14 stsp free(path_got);
589 024e9686 2019-05-14 stsp if (err)
590 024e9686 2019-05-14 stsp free(head_ref_name);
591 024e9686 2019-05-14 stsp return err;
592 507dc3bb 2018-12-29 stsp }
593 507dc3bb 2018-12-29 stsp
594 b72f483a 2019-02-05 stsp struct got_object_id *
595 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
596 507dc3bb 2018-12-29 stsp {
597 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
598 86c3caaf 2018-03-09 stsp }
599 86c3caaf 2018-03-09 stsp
600 507dc3bb 2018-12-29 stsp const struct got_error *
601 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
602 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
603 507dc3bb 2018-12-29 stsp {
604 507dc3bb 2018-12-29 stsp const struct got_error *err;
605 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
606 507dc3bb 2018-12-29 stsp char *id_str = NULL;
607 507dc3bb 2018-12-29 stsp char *path_got = NULL;
608 507dc3bb 2018-12-29 stsp
609 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
610 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
611 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
612 507dc3bb 2018-12-29 stsp path_got = NULL;
613 507dc3bb 2018-12-29 stsp goto done;
614 507dc3bb 2018-12-29 stsp }
615 507dc3bb 2018-12-29 stsp
616 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
617 507dc3bb 2018-12-29 stsp if (err)
618 507dc3bb 2018-12-29 stsp return err;
619 507dc3bb 2018-12-29 stsp
620 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
621 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
622 507dc3bb 2018-12-29 stsp goto done;
623 507dc3bb 2018-12-29 stsp }
624 507dc3bb 2018-12-29 stsp
625 507dc3bb 2018-12-29 stsp /* Record our base commit. */
626 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
627 507dc3bb 2018-12-29 stsp if (err)
628 507dc3bb 2018-12-29 stsp goto done;
629 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
630 507dc3bb 2018-12-29 stsp if (err)
631 507dc3bb 2018-12-29 stsp goto done;
632 507dc3bb 2018-12-29 stsp
633 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
634 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
635 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
636 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
637 507dc3bb 2018-12-29 stsp goto done;
638 507dc3bb 2018-12-29 stsp }
639 507dc3bb 2018-12-29 stsp done:
640 507dc3bb 2018-12-29 stsp if (obj)
641 507dc3bb 2018-12-29 stsp got_object_close(obj);
642 507dc3bb 2018-12-29 stsp free(id_str);
643 507dc3bb 2018-12-29 stsp free(path_got);
644 507dc3bb 2018-12-29 stsp return err;
645 50b0790e 2020-09-11 stsp }
646 50b0790e 2020-09-11 stsp
647 50b0790e 2020-09-11 stsp const struct got_gotconfig *
648 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
649 50b0790e 2020-09-11 stsp {
650 50b0790e 2020-09-11 stsp return worktree->gotconfig;
651 507dc3bb 2018-12-29 stsp }
652 507dc3bb 2018-12-29 stsp
653 9d31a1d8 2018-03-11 stsp static const struct got_error *
654 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
655 86c3caaf 2018-03-09 stsp {
656 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
657 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
658 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
659 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
660 86c3caaf 2018-03-09 stsp return NULL;
661 21908da4 2019-01-13 stsp }
662 21908da4 2019-01-13 stsp
663 21908da4 2019-01-13 stsp static const struct got_error *
664 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
665 4a1ddfc2 2019-01-12 stsp {
666 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
667 4a1ddfc2 2019-01-12 stsp char *abspath;
668 4a1ddfc2 2019-01-12 stsp
669 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
670 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
671 4a1ddfc2 2019-01-12 stsp
672 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
673 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
674 ddcd8544 2019-03-11 stsp struct stat sb;
675 ddcd8544 2019-03-11 stsp err = NULL;
676 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
677 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
678 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
679 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
680 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
681 ddcd8544 2019-03-11 stsp }
682 ddcd8544 2019-03-11 stsp }
683 4a1ddfc2 2019-01-12 stsp free(abspath);
684 68c76935 2019-02-19 stsp return err;
685 68c76935 2019-02-19 stsp }
686 68c76935 2019-02-19 stsp
687 68c76935 2019-02-19 stsp static const struct got_error *
688 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
689 68c76935 2019-02-19 stsp {
690 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
691 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
692 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
693 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
694 68c76935 2019-02-19 stsp
695 68c76935 2019-02-19 stsp *same = 1;
696 68c76935 2019-02-19 stsp
697 230a42bd 2019-05-11 jcs for (;;) {
698 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
699 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
700 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
701 68c76935 2019-02-19 stsp break;
702 68c76935 2019-02-19 stsp }
703 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
704 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
705 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
706 68c76935 2019-02-19 stsp break;
707 68c76935 2019-02-19 stsp }
708 68c76935 2019-02-19 stsp if (flen1 == 0) {
709 68c76935 2019-02-19 stsp if (flen2 != 0)
710 68c76935 2019-02-19 stsp *same = 0;
711 68c76935 2019-02-19 stsp break;
712 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
713 68c76935 2019-02-19 stsp if (flen1 != 0)
714 68c76935 2019-02-19 stsp *same = 0;
715 68c76935 2019-02-19 stsp break;
716 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
717 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
718 68c76935 2019-02-19 stsp *same = 0;
719 68c76935 2019-02-19 stsp break;
720 68c76935 2019-02-19 stsp }
721 68c76935 2019-02-19 stsp } else {
722 68c76935 2019-02-19 stsp *same = 0;
723 68c76935 2019-02-19 stsp break;
724 68c76935 2019-02-19 stsp }
725 68c76935 2019-02-19 stsp }
726 68c76935 2019-02-19 stsp
727 68c76935 2019-02-19 stsp return err;
728 68c76935 2019-02-19 stsp }
729 68c76935 2019-02-19 stsp
730 68c76935 2019-02-19 stsp static const struct got_error *
731 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
732 68c76935 2019-02-19 stsp {
733 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
734 68c76935 2019-02-19 stsp struct stat sb;
735 68c76935 2019-02-19 stsp size_t size1, size2;
736 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
737 68c76935 2019-02-19 stsp
738 68c76935 2019-02-19 stsp *same = 1;
739 68c76935 2019-02-19 stsp
740 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
741 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
742 68c76935 2019-02-19 stsp goto done;
743 68c76935 2019-02-19 stsp }
744 68c76935 2019-02-19 stsp size1 = sb.st_size;
745 68c76935 2019-02-19 stsp
746 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
747 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
748 68c76935 2019-02-19 stsp goto done;
749 68c76935 2019-02-19 stsp }
750 68c76935 2019-02-19 stsp size2 = sb.st_size;
751 68c76935 2019-02-19 stsp
752 68c76935 2019-02-19 stsp if (size1 != size2) {
753 68c76935 2019-02-19 stsp *same = 0;
754 68c76935 2019-02-19 stsp return NULL;
755 68c76935 2019-02-19 stsp }
756 68c76935 2019-02-19 stsp
757 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
758 68c76935 2019-02-19 stsp if (f1 == NULL)
759 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
760 68c76935 2019-02-19 stsp
761 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
762 68c76935 2019-02-19 stsp if (f2 == NULL) {
763 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
764 68c76935 2019-02-19 stsp goto done;
765 68c76935 2019-02-19 stsp }
766 68c76935 2019-02-19 stsp
767 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
768 68c76935 2019-02-19 stsp done:
769 56b63ca4 2021-01-22 stsp if (f1 && fclose(f1) == EOF && err == NULL)
770 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
771 56b63ca4 2021-01-22 stsp if (f2 && fclose(f2) == EOF && err == NULL)
772 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
773 68c76935 2019-02-19 stsp
774 6353ad76 2019-02-08 stsp return err;
775 6353ad76 2019-02-08 stsp }
776 6353ad76 2019-02-08 stsp
777 6353ad76 2019-02-08 stsp /*
778 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
779 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
780 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
781 6353ad76 2019-02-08 stsp */
782 6353ad76 2019-02-08 stsp static const struct got_error *
783 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
784 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
785 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
786 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
787 f69721c3 2019-10-21 stsp struct got_repository *repo,
788 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
789 6353ad76 2019-02-08 stsp {
790 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
791 6353ad76 2019-02-08 stsp int merged_fd = -1;
792 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
793 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
794 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
795 234035bc 2019-06-01 stsp int overlapcnt = 0;
796 3524bbf9 2020-10-20 stsp char *parent = NULL;
797 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
798 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
799 6353ad76 2019-02-08 stsp
800 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
801 234035bc 2019-06-01 stsp
802 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
803 3524bbf9 2020-10-20 stsp if (err)
804 3524bbf9 2020-10-20 stsp return err;
805 af54ae4a 2019-02-19 stsp
806 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
807 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
808 3524bbf9 2020-10-20 stsp goto done;
809 3524bbf9 2020-10-20 stsp }
810 af54ae4a 2019-02-19 stsp
811 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
812 6353ad76 2019-02-08 stsp if (err)
813 6353ad76 2019-02-08 stsp goto done;
814 6353ad76 2019-02-08 stsp
815 af54ae4a 2019-02-19 stsp free(base_path);
816 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
817 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
818 af54ae4a 2019-02-19 stsp base_path = NULL;
819 af54ae4a 2019-02-19 stsp goto done;
820 af54ae4a 2019-02-19 stsp }
821 af54ae4a 2019-02-19 stsp
822 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
823 6353ad76 2019-02-08 stsp if (err)
824 6353ad76 2019-02-08 stsp goto done;
825 46b6ee73 2019-06-04 stsp if (blob_orig) {
826 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
827 46b6ee73 2019-06-04 stsp blob_orig);
828 1430b4e0 2019-03-27 stsp if (err)
829 1430b4e0 2019-03-27 stsp goto done;
830 1430b4e0 2019-03-27 stsp } else {
831 1430b4e0 2019-03-27 stsp /*
832 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
833 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
834 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
835 1430b4e0 2019-03-27 stsp */
836 1430b4e0 2019-03-27 stsp }
837 6353ad76 2019-02-08 stsp
838 3b9f0f87 2020-07-23 stsp /*
839 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
840 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
841 3b9f0f87 2020-07-23 stsp */
842 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
843 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
844 3b9f0f87 2020-07-23 stsp ssize_t target_len;
845 3b9f0f87 2020-07-23 stsp size_t n;
846 3b9f0f87 2020-07-23 stsp
847 3b9f0f87 2020-07-23 stsp free(base_path);
848 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
849 3b9f0f87 2020-07-23 stsp parent) == -1) {
850 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
851 3b9f0f87 2020-07-23 stsp base_path = NULL;
852 3b9f0f87 2020-07-23 stsp goto done;
853 3b9f0f87 2020-07-23 stsp }
854 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
855 3b9f0f87 2020-07-23 stsp if (err)
856 3b9f0f87 2020-07-23 stsp goto done;
857 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
858 3b9f0f87 2020-07-23 stsp sizeof(target_path));
859 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
860 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
861 3b9f0f87 2020-07-23 stsp goto done;
862 3b9f0f87 2020-07-23 stsp }
863 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
864 3b9f0f87 2020-07-23 stsp if (n != target_len) {
865 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
866 3b9f0f87 2020-07-23 stsp goto done;
867 3b9f0f87 2020-07-23 stsp }
868 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
869 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
870 3b9f0f87 2020-07-23 stsp goto done;
871 3b9f0f87 2020-07-23 stsp }
872 3b9f0f87 2020-07-23 stsp }
873 3b9f0f87 2020-07-23 stsp
874 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
875 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
876 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
877 6353ad76 2019-02-08 stsp if (err)
878 6353ad76 2019-02-08 stsp goto done;
879 6353ad76 2019-02-08 stsp
880 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
881 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
882 1ee397ad 2019-07-12 stsp if (err)
883 1ee397ad 2019-07-12 stsp goto done;
884 6353ad76 2019-02-08 stsp
885 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
886 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
887 816dc654 2019-02-16 stsp goto done;
888 68c76935 2019-02-19 stsp }
889 68c76935 2019-02-19 stsp
890 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
891 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
892 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
893 68c76935 2019-02-19 stsp merged_path);
894 68c76935 2019-02-19 stsp if (err)
895 68c76935 2019-02-19 stsp goto done;
896 816dc654 2019-02-16 stsp }
897 6353ad76 2019-02-08 stsp
898 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
899 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
900 70a0c8ec 2019-02-20 stsp goto done;
901 70a0c8ec 2019-02-20 stsp }
902 70a0c8ec 2019-02-20 stsp
903 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
904 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
905 230a42bd 2019-05-11 jcs ondisk_path);
906 6353ad76 2019-02-08 stsp goto done;
907 6353ad76 2019-02-08 stsp }
908 6353ad76 2019-02-08 stsp done:
909 fdcb7daf 2019-12-15 stsp if (err) {
910 fdcb7daf 2019-12-15 stsp if (merged_path)
911 fdcb7daf 2019-12-15 stsp unlink(merged_path);
912 3b9f0f87 2020-07-23 stsp }
913 3b9f0f87 2020-07-23 stsp if (symlink_path) {
914 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
915 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
916 fdcb7daf 2019-12-15 stsp }
917 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
918 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
919 3b9f0f87 2020-07-23 stsp free(symlink_path);
920 08578a35 2021-01-22 stsp if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
921 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
922 56b63ca4 2021-01-22 stsp if (f_orig && fclose(f_orig) == EOF && err == NULL)
923 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
924 6353ad76 2019-02-08 stsp free(merged_path);
925 af54ae4a 2019-02-19 stsp free(base_path);
926 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
927 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
928 46b6ee73 2019-06-04 stsp free(blob_orig_path);
929 af57b12a 2020-07-23 stsp }
930 3524bbf9 2020-10-20 stsp free(parent);
931 af57b12a 2020-07-23 stsp return err;
932 af57b12a 2020-07-23 stsp }
933 af57b12a 2020-07-23 stsp
934 af57b12a 2020-07-23 stsp static const struct got_error *
935 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
936 af57b12a 2020-07-23 stsp size_t target_len)
937 af57b12a 2020-07-23 stsp {
938 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
939 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
940 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
941 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
942 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
943 af57b12a 2020-07-23 stsp ondisk_path);
944 af57b12a 2020-07-23 stsp return NULL;
945 af57b12a 2020-07-23 stsp }
946 af57b12a 2020-07-23 stsp
947 af57b12a 2020-07-23 stsp /*
948 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
949 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
950 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
951 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
952 993e2a1b 2020-07-23 stsp *
953 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
954 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
955 993e2a1b 2020-07-23 stsp *
956 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
957 993e2a1b 2020-07-23 stsp * has deleted this symlink.
958 11cc08c1 2020-07-23 stsp */
959 11cc08c1 2020-07-23 stsp static const struct got_error *
960 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
961 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
962 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
963 11cc08c1 2020-07-23 stsp {
964 11cc08c1 2020-07-23 stsp const struct got_error *err;
965 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
966 11cc08c1 2020-07-23 stsp FILE *f = NULL;
967 11cc08c1 2020-07-23 stsp
968 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
969 11cc08c1 2020-07-23 stsp if (err)
970 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
971 11cc08c1 2020-07-23 stsp
972 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
973 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
974 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
975 11cc08c1 2020-07-23 stsp goto done;
976 11cc08c1 2020-07-23 stsp }
977 11cc08c1 2020-07-23 stsp
978 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
979 11cc08c1 2020-07-23 stsp if (err)
980 3818e3c4 2020-11-01 naddy goto done;
981 3818e3c4 2020-11-01 naddy
982 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
983 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
984 11cc08c1 2020-07-23 stsp goto done;
985 3818e3c4 2020-11-01 naddy }
986 11cc08c1 2020-07-23 stsp
987 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
988 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
989 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
990 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
991 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
992 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
993 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
994 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
995 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
996 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
997 11cc08c1 2020-07-23 stsp goto done;
998 11cc08c1 2020-07-23 stsp }
999 11cc08c1 2020-07-23 stsp
1000 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
1001 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
1002 11cc08c1 2020-07-23 stsp goto done;
1003 11cc08c1 2020-07-23 stsp }
1004 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
1005 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
1006 11cc08c1 2020-07-23 stsp goto done;
1007 11cc08c1 2020-07-23 stsp }
1008 11cc08c1 2020-07-23 stsp done:
1009 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1010 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
1011 11cc08c1 2020-07-23 stsp free(path);
1012 11cc08c1 2020-07-23 stsp free(id_str);
1013 11cc08c1 2020-07-23 stsp free(label_deriv);
1014 11cc08c1 2020-07-23 stsp return err;
1015 11cc08c1 2020-07-23 stsp }
1016 d219f183 2020-07-23 stsp
1017 d219f183 2020-07-23 stsp /* forward declaration */
1018 d219f183 2020-07-23 stsp static const struct got_error *
1019 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1020 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
1021 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
1022 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
1023 11cc08c1 2020-07-23 stsp
1024 11cc08c1 2020-07-23 stsp /*
1025 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1026 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1027 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1028 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1029 af57b12a 2020-07-23 stsp */
1030 af57b12a 2020-07-23 stsp static const struct got_error *
1031 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1032 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1033 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1034 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1035 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1036 af57b12a 2020-07-23 stsp {
1037 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1038 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1039 af57b12a 2020-07-23 stsp struct stat sb;
1040 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1041 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1042 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1043 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1044 af57b12a 2020-07-23 stsp
1045 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1046 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1047 af57b12a 2020-07-23 stsp
1048 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1049 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1050 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1051 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1052 af57b12a 2020-07-23 stsp ondisk_path);
1053 af57b12a 2020-07-23 stsp goto done;
1054 af57b12a 2020-07-23 stsp }
1055 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1056 af57b12a 2020-07-23 stsp
1057 526a746f 2020-07-23 stsp if (blob_orig) {
1058 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1059 526a746f 2020-07-23 stsp if (err)
1060 526a746f 2020-07-23 stsp goto done;
1061 526a746f 2020-07-23 stsp }
1062 af57b12a 2020-07-23 stsp
1063 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1064 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1065 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1066 11cc08c1 2020-07-23 stsp have_local_change = 1;
1067 11cc08c1 2020-07-23 stsp
1068 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1069 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1070 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1071 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1072 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1073 11cc08c1 2020-07-23 stsp
1074 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1075 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1076 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1077 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1078 11cc08c1 2020-07-23 stsp path);
1079 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1080 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1081 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1082 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1083 11cc08c1 2020-07-23 stsp path);
1084 11cc08c1 2020-07-23 stsp } else {
1085 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1086 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1087 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1088 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1089 11cc08c1 2020-07-23 stsp if (err)
1090 11cc08c1 2020-07-23 stsp goto done;
1091 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1092 11cc08c1 2020-07-23 stsp path);
1093 11cc08c1 2020-07-23 stsp }
1094 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1095 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1096 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1097 af57b12a 2020-07-23 stsp strlen(deriv_target));
1098 af57b12a 2020-07-23 stsp if (err)
1099 af57b12a 2020-07-23 stsp goto done;
1100 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1101 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1102 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1103 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1104 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1105 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1106 ea7786be 2020-07-23 stsp path);
1107 ea7786be 2020-07-23 stsp } else {
1108 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1109 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1110 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1111 ea7786be 2020-07-23 stsp if (err)
1112 ea7786be 2020-07-23 stsp goto done;
1113 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1114 ea7786be 2020-07-23 stsp path);
1115 ea7786be 2020-07-23 stsp }
1116 6353ad76 2019-02-08 stsp }
1117 11cc08c1 2020-07-23 stsp
1118 af57b12a 2020-07-23 stsp done:
1119 af57b12a 2020-07-23 stsp free(ancestor_target);
1120 14c901f1 2019-08-08 stsp return err;
1121 14c901f1 2019-08-08 stsp }
1122 14c901f1 2019-08-08 stsp
1123 14c901f1 2019-08-08 stsp /*
1124 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1125 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1126 14c901f1 2019-08-08 stsp * acts as the second derived version.
1127 14c901f1 2019-08-08 stsp */
1128 14c901f1 2019-08-08 stsp static const struct got_error *
1129 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1130 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1131 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1132 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1133 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1134 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1135 14c901f1 2019-08-08 stsp {
1136 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1137 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1138 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1139 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1140 14c901f1 2019-08-08 stsp
1141 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1142 14c901f1 2019-08-08 stsp
1143 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1144 ed6b5030 2020-10-20 stsp if (err)
1145 ed6b5030 2020-10-20 stsp return err;
1146 14c901f1 2019-08-08 stsp
1147 14c901f1 2019-08-08 stsp free(base_path);
1148 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1149 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1150 14c901f1 2019-08-08 stsp base_path = NULL;
1151 14c901f1 2019-08-08 stsp goto done;
1152 14c901f1 2019-08-08 stsp }
1153 14c901f1 2019-08-08 stsp
1154 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1155 14c901f1 2019-08-08 stsp if (err)
1156 14c901f1 2019-08-08 stsp goto done;
1157 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1158 14c901f1 2019-08-08 stsp blob_deriv);
1159 14c901f1 2019-08-08 stsp if (err)
1160 14c901f1 2019-08-08 stsp goto done;
1161 14c901f1 2019-08-08 stsp
1162 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1163 14c901f1 2019-08-08 stsp if (err)
1164 14c901f1 2019-08-08 stsp goto done;
1165 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1166 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1167 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1168 14c901f1 2019-08-08 stsp goto done;
1169 14c901f1 2019-08-08 stsp }
1170 14c901f1 2019-08-08 stsp
1171 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1172 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1173 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1174 14c901f1 2019-08-08 stsp done:
1175 56b63ca4 2021-01-22 stsp if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1176 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1177 14c901f1 2019-08-08 stsp free(base_path);
1178 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1179 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1180 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1181 14c901f1 2019-08-08 stsp }
1182 6353ad76 2019-02-08 stsp free(id_str);
1183 818c7501 2019-07-11 stsp free(label_deriv);
1184 ed6b5030 2020-10-20 stsp free(parent);
1185 4a1ddfc2 2019-01-12 stsp return err;
1186 4a1ddfc2 2019-01-12 stsp }
1187 4a1ddfc2 2019-01-12 stsp
1188 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1189 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1190 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1191 437adc9d 2020-12-10 yzhong int wt_fd, const char *path, struct got_object_id *blob_id)
1192 13d9040b 2019-03-27 stsp {
1193 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1194 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1195 13d9040b 2019-03-27 stsp
1196 65b05cec 2020-07-23 stsp *new_iep = NULL;
1197 65b05cec 2020-07-23 stsp
1198 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1199 054041d0 2020-06-24 stsp if (err)
1200 054041d0 2020-06-24 stsp return err;
1201 054041d0 2020-06-24 stsp
1202 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(new_ie, wt_fd, path,
1203 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1204 054041d0 2020-06-24 stsp if (err)
1205 054041d0 2020-06-24 stsp goto done;
1206 054041d0 2020-06-24 stsp
1207 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1208 054041d0 2020-06-24 stsp done:
1209 054041d0 2020-06-24 stsp if (err)
1210 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1211 65b05cec 2020-07-23 stsp else
1212 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1213 13d9040b 2019-03-27 stsp return err;
1214 1ebedb77 2019-10-19 stsp }
1215 1ebedb77 2019-10-19 stsp
1216 1ebedb77 2019-10-19 stsp static mode_t
1217 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1218 1ebedb77 2019-10-19 stsp {
1219 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1220 1ebedb77 2019-10-19 stsp
1221 1ebedb77 2019-10-19 stsp if (executable) {
1222 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1223 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1224 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1225 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1226 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1227 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1228 1ebedb77 2019-10-19 stsp }
1229 1ebedb77 2019-10-19 stsp
1230 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1231 13d9040b 2019-03-27 stsp }
1232 13d9040b 2019-03-27 stsp
1233 8ba819a3 2020-07-23 stsp /* forward declaration */
1234 13d9040b 2019-03-27 stsp static const struct got_error *
1235 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1236 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1237 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1238 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1239 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1240 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1241 8ba819a3 2020-07-23 stsp
1242 5a1fbc73 2020-07-23 stsp /*
1243 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1244 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1245 5a1fbc73 2020-07-23 stsp */
1246 8ba819a3 2020-07-23 stsp static const struct got_error *
1247 c6e8a826 2021-04-05 stsp replace_existing_symlink(int *did_something, const char *ondisk_path,
1248 c6e8a826 2021-04-05 stsp const char *target_path, size_t target_len)
1249 5a1fbc73 2020-07-23 stsp {
1250 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1251 5a1fbc73 2020-07-23 stsp ssize_t elen;
1252 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1253 5a1fbc73 2020-07-23 stsp int fd;
1254 5a1fbc73 2020-07-23 stsp
1255 c6e8a826 2021-04-05 stsp *did_something = 0;
1256 c6e8a826 2021-04-05 stsp
1257 5a1fbc73 2020-07-23 stsp /*
1258 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1259 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1260 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1261 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1262 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1263 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1264 5a1fbc73 2020-07-23 stsp */
1265 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1266 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1267 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1268 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1269 5a1fbc73 2020-07-23 stsp
1270 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1271 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1272 5a1fbc73 2020-07-23 stsp if (elen == -1)
1273 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1274 5a1fbc73 2020-07-23 stsp
1275 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1276 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1277 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1278 5a1fbc73 2020-07-23 stsp }
1279 5a1fbc73 2020-07-23 stsp
1280 c6e8a826 2021-04-05 stsp *did_something = 1;
1281 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1282 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1283 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1284 5a1fbc73 2020-07-23 stsp return err;
1285 3c1ec782 2020-07-23 stsp }
1286 3c1ec782 2020-07-23 stsp
1287 3c1ec782 2020-07-23 stsp static const struct got_error *
1288 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1289 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1290 3c1ec782 2020-07-23 stsp {
1291 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1292 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1293 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1294 3c1ec782 2020-07-23 stsp
1295 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1296 3c1ec782 2020-07-23 stsp
1297 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1298 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1299 3c1ec782 2020-07-23 stsp return NULL;
1300 3c1ec782 2020-07-23 stsp }
1301 3c1ec782 2020-07-23 stsp
1302 3c1ec782 2020-07-23 stsp /*
1303 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1304 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1305 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1306 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1307 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1308 3c1ec782 2020-07-23 stsp */
1309 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1310 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1311 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1312 ce031e9e 2020-10-20 stsp if (err)
1313 ce031e9e 2020-10-20 stsp return err;
1314 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1315 ce031e9e 2020-10-20 stsp free(parent);
1316 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1317 ce031e9e 2020-10-20 stsp }
1318 ce031e9e 2020-10-20 stsp free(parent);
1319 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1320 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1321 3c1ec782 2020-07-23 stsp free(abspath);
1322 3c1ec782 2020-07-23 stsp return err;
1323 3c1ec782 2020-07-23 stsp }
1324 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1325 3c1ec782 2020-07-23 stsp free(abspath);
1326 3c1ec782 2020-07-23 stsp if (err)
1327 3c1ec782 2020-07-23 stsp return err;
1328 3c1ec782 2020-07-23 stsp } else {
1329 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1330 3c1ec782 2020-07-23 stsp if (err)
1331 3c1ec782 2020-07-23 stsp return err;
1332 3c1ec782 2020-07-23 stsp }
1333 3c1ec782 2020-07-23 stsp
1334 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1335 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1336 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1337 3c1ec782 2020-07-23 stsp return NULL;
1338 3c1ec782 2020-07-23 stsp }
1339 3c1ec782 2020-07-23 stsp
1340 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1341 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1342 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1343 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1344 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1345 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1346 3c1ec782 2020-07-23 stsp
1347 3c1ec782 2020-07-23 stsp free(path_got);
1348 3c1ec782 2020-07-23 stsp return NULL;
1349 5a1fbc73 2020-07-23 stsp }
1350 5a1fbc73 2020-07-23 stsp
1351 5a1fbc73 2020-07-23 stsp static const struct got_error *
1352 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1353 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1354 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1355 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1356 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1357 9d31a1d8 2018-03-11 stsp {
1358 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1359 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1360 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1361 906c123b 2020-07-23 stsp char *path_got = NULL;
1362 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1363 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1364 8ba819a3 2020-07-23 stsp
1365 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1366 2e1fa222 2020-07-23 stsp
1367 8ba819a3 2020-07-23 stsp /*
1368 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1369 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1370 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1371 8ba819a3 2020-07-23 stsp * in the blob object.
1372 8ba819a3 2020-07-23 stsp */
1373 8ba819a3 2020-07-23 stsp do {
1374 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1375 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1376 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1377 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1378 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1379 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1380 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1381 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1382 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1383 3b9f0f87 2020-07-23 stsp progress_arg);
1384 8ba819a3 2020-07-23 stsp }
1385 8ba819a3 2020-07-23 stsp if (len > 0) {
1386 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1387 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1388 8ba819a3 2020-07-23 stsp len - hdrlen);
1389 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1390 8ba819a3 2020-07-23 stsp hdrlen = 0;
1391 8ba819a3 2020-07-23 stsp }
1392 8ba819a3 2020-07-23 stsp } while (len != 0);
1393 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1394 8ba819a3 2020-07-23 stsp
1395 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1396 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1397 3c1ec782 2020-07-23 stsp if (err)
1398 3c1ec782 2020-07-23 stsp return err;
1399 8ba819a3 2020-07-23 stsp
1400 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1401 906c123b 2020-07-23 stsp /* install as a regular file */
1402 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1403 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1404 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1405 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1406 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1407 906c123b 2020-07-23 stsp goto done;
1408 906c123b 2020-07-23 stsp }
1409 906c123b 2020-07-23 stsp
1410 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1411 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1412 c6e8a826 2021-04-05 stsp int symlink_replaced;
1413 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1414 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1415 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1416 c90c8ce3 2020-07-23 stsp goto done;
1417 c90c8ce3 2020-07-23 stsp }
1418 c6e8a826 2021-04-05 stsp err = replace_existing_symlink(&symlink_replaced,
1419 c6e8a826 2021-04-05 stsp ondisk_path, target_path, target_len);
1420 5a1fbc73 2020-07-23 stsp if (err)
1421 f35fa46a 2020-07-23 stsp goto done;
1422 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1423 c6e8a826 2021-04-05 stsp if (symlink_replaced) {
1424 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1425 c6e8a826 2021-04-05 stsp reverting_versioned_file ?
1426 c6e8a826 2021-04-05 stsp GOT_STATUS_REVERT :
1427 c6e8a826 2021-04-05 stsp GOT_STATUS_UPDATE, path);
1428 c6e8a826 2021-04-05 stsp } else {
1429 c6e8a826 2021-04-05 stsp err = (*progress_cb)(progress_arg,
1430 c6e8a826 2021-04-05 stsp GOT_STATUS_EXISTS, path);
1431 c6e8a826 2021-04-05 stsp }
1432 f35fa46a 2020-07-23 stsp }
1433 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1434 f35fa46a 2020-07-23 stsp }
1435 f35fa46a 2020-07-23 stsp
1436 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1437 f4994adc 2020-10-20 stsp char *parent;
1438 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1439 f4994adc 2020-10-20 stsp if (err)
1440 f4994adc 2020-10-20 stsp goto done;
1441 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1442 f4994adc 2020-10-20 stsp free(parent);
1443 f35fa46a 2020-07-23 stsp if (err)
1444 f35fa46a 2020-07-23 stsp goto done;
1445 f35fa46a 2020-07-23 stsp /*
1446 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1447 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1448 f35fa46a 2020-07-23 stsp */
1449 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1450 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1451 f35fa46a 2020-07-23 stsp goto done;
1452 f35fa46a 2020-07-23 stsp }
1453 f35fa46a 2020-07-23 stsp }
1454 f35fa46a 2020-07-23 stsp
1455 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1456 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1457 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1458 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1459 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1460 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1461 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1462 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1463 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1464 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1465 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1466 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1467 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1468 8ba819a3 2020-07-23 stsp } else {
1469 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1470 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1471 8ba819a3 2020-07-23 stsp }
1472 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1473 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1474 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1475 8ba819a3 2020-07-23 stsp done:
1476 906c123b 2020-07-23 stsp free(path_got);
1477 8ba819a3 2020-07-23 stsp return err;
1478 8ba819a3 2020-07-23 stsp }
1479 8ba819a3 2020-07-23 stsp
1480 8ba819a3 2020-07-23 stsp static const struct got_error *
1481 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1482 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1483 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1484 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1485 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1486 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1487 8ba819a3 2020-07-23 stsp {
1488 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1489 507dc3bb 2018-12-29 stsp int fd = -1;
1490 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1491 507dc3bb 2018-12-29 stsp int update = 0;
1492 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1493 8ba819a3 2020-07-23 stsp
1494 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1495 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1496 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1497 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1498 f5375317 2020-10-20 stsp char *parent;
1499 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1500 f5375317 2020-10-20 stsp if (err)
1501 f5375317 2020-10-20 stsp return err;
1502 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1503 f5375317 2020-10-20 stsp free(parent);
1504 21908da4 2019-01-13 stsp if (err)
1505 21908da4 2019-01-13 stsp return err;
1506 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1507 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1508 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1509 21908da4 2019-01-13 stsp if (fd == -1)
1510 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1511 230a42bd 2019-05-11 jcs ondisk_path);
1512 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1513 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1514 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1515 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1516 3b9f0f87 2020-07-23 stsp goto done;
1517 3b9f0f87 2020-07-23 stsp }
1518 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1519 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1520 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1521 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1522 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1523 507dc3bb 2018-12-29 stsp goto done;
1524 d70b8e30 2018-12-27 stsp } else {
1525 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1526 507dc3bb 2018-12-29 stsp ondisk_path);
1527 507dc3bb 2018-12-29 stsp if (err)
1528 507dc3bb 2018-12-29 stsp goto done;
1529 507dc3bb 2018-12-29 stsp update = 1;
1530 9d31a1d8 2018-03-11 stsp }
1531 507dc3bb 2018-12-29 stsp } else
1532 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1533 9d31a1d8 2018-03-11 stsp }
1534 9d31a1d8 2018-03-11 stsp
1535 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1536 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1537 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1538 3818e3c4 2020-11-01 naddy goto done;
1539 3818e3c4 2020-11-01 naddy }
1540 3818e3c4 2020-11-01 naddy
1541 bd6aa359 2020-07-23 stsp if (progress_cb) {
1542 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1543 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1544 bd6aa359 2020-07-23 stsp path);
1545 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1546 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1547 bd6aa359 2020-07-23 stsp path);
1548 bd6aa359 2020-07-23 stsp else
1549 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1550 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1551 bd6aa359 2020-07-23 stsp if (err)
1552 bd6aa359 2020-07-23 stsp goto done;
1553 bd6aa359 2020-07-23 stsp }
1554 d7b62c98 2018-12-27 stsp
1555 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1556 9d31a1d8 2018-03-11 stsp do {
1557 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1558 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1559 9d31a1d8 2018-03-11 stsp if (err)
1560 9d31a1d8 2018-03-11 stsp break;
1561 9d31a1d8 2018-03-11 stsp if (len > 0) {
1562 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1563 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1564 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1565 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1566 b87c6f83 2018-12-24 stsp goto done;
1567 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1568 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1569 b87c6f83 2018-12-24 stsp goto done;
1570 9d31a1d8 2018-03-11 stsp }
1571 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1572 9d31a1d8 2018-03-11 stsp }
1573 9d31a1d8 2018-03-11 stsp } while (len != 0);
1574 9d31a1d8 2018-03-11 stsp
1575 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1576 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1577 816dc654 2019-02-16 stsp goto done;
1578 816dc654 2019-02-16 stsp }
1579 9d31a1d8 2018-03-11 stsp
1580 507dc3bb 2018-12-29 stsp if (update) {
1581 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1582 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1583 f6d8c0ac 2020-11-09 stsp goto done;
1584 f6d8c0ac 2020-11-09 stsp }
1585 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1586 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1587 230a42bd 2019-05-11 jcs ondisk_path);
1588 68ed9ba5 2019-02-10 stsp goto done;
1589 68ed9ba5 2019-02-10 stsp }
1590 63df146d 2020-11-09 stsp free(tmppath);
1591 63df146d 2020-11-09 stsp tmppath = NULL;
1592 507dc3bb 2018-12-29 stsp }
1593 507dc3bb 2018-12-29 stsp
1594 9d31a1d8 2018-03-11 stsp done:
1595 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1596 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1597 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1598 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1599 507dc3bb 2018-12-29 stsp free(tmppath);
1600 6353ad76 2019-02-08 stsp return err;
1601 6353ad76 2019-02-08 stsp }
1602 6353ad76 2019-02-08 stsp
1603 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1604 6353ad76 2019-02-08 stsp static const struct got_error *
1605 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1606 7154f6ce 2019-03-27 stsp {
1607 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1608 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1609 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1610 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1611 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1612 7154f6ce 2019-03-27 stsp };
1613 7154f6ce 2019-03-27 stsp int i = 0;
1614 9bdd68dd 2020-01-02 naddy char *line = NULL;
1615 9bdd68dd 2020-01-02 naddy size_t linesize = 0;
1616 9bdd68dd 2020-01-02 naddy ssize_t linelen;
1617 7154f6ce 2019-03-27 stsp
1618 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1619 9bdd68dd 2020-01-02 naddy linelen = getline(&line, &linesize, f);
1620 9bdd68dd 2020-01-02 naddy if (linelen == -1) {
1621 7154f6ce 2019-03-27 stsp if (feof(f))
1622 7154f6ce 2019-03-27 stsp break;
1623 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1624 7154f6ce 2019-03-27 stsp break;
1625 7154f6ce 2019-03-27 stsp }
1626 7154f6ce 2019-03-27 stsp
1627 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1628 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1629 19332e6d 2019-05-13 stsp == 0)
1630 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1631 7154f6ce 2019-03-27 stsp else
1632 7154f6ce 2019-03-27 stsp i++;
1633 7154f6ce 2019-03-27 stsp }
1634 7154f6ce 2019-03-27 stsp }
1635 9bdd68dd 2020-01-02 naddy free(line);
1636 7154f6ce 2019-03-27 stsp
1637 7154f6ce 2019-03-27 stsp return err;
1638 e2b1e152 2019-07-13 stsp }
1639 e2b1e152 2019-07-13 stsp
1640 e2b1e152 2019-07-13 stsp static int
1641 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1642 1ebedb77 2019-10-19 stsp {
1643 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1644 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1645 1ebedb77 2019-10-19 stsp }
1646 1ebedb77 2019-10-19 stsp
1647 1ebedb77 2019-10-19 stsp static int
1648 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1649 e2b1e152 2019-07-13 stsp {
1650 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1651 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1652 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1653 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1654 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1655 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1656 c363b2c1 2019-08-03 stsp }
1657 c363b2c1 2019-08-03 stsp
1658 c363b2c1 2019-08-03 stsp static unsigned char
1659 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1660 c363b2c1 2019-08-03 stsp {
1661 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1662 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1663 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1664 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1665 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1666 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1667 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1668 c363b2c1 2019-08-03 stsp default:
1669 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1670 a919d5c4 2020-07-23 stsp }
1671 a919d5c4 2020-07-23 stsp }
1672 a919d5c4 2020-07-23 stsp
1673 a919d5c4 2020-07-23 stsp static const struct got_error *
1674 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1675 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1676 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1677 a919d5c4 2020-07-23 stsp {
1678 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1679 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1680 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1681 a919d5c4 2020-07-23 stsp ssize_t elen;
1682 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1683 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1684 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1685 a919d5c4 2020-07-23 stsp
1686 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1687 a919d5c4 2020-07-23 stsp
1688 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1689 a919d5c4 2020-07-23 stsp do {
1690 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1691 a919d5c4 2020-07-23 stsp if (err)
1692 a919d5c4 2020-07-23 stsp return err;
1693 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1694 a919d5c4 2020-07-23 stsp /*
1695 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1696 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1697 a919d5c4 2020-07-23 stsp */
1698 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1699 a919d5c4 2020-07-23 stsp }
1700 a919d5c4 2020-07-23 stsp if (len > 0) {
1701 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1702 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1703 a919d5c4 2020-07-23 stsp len - hdrlen);
1704 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1705 a919d5c4 2020-07-23 stsp hdrlen = 0;
1706 a919d5c4 2020-07-23 stsp }
1707 a919d5c4 2020-07-23 stsp } while (len != 0);
1708 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1709 a919d5c4 2020-07-23 stsp
1710 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1711 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1712 a919d5c4 2020-07-23 stsp if (elen == -1)
1713 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1714 a919d5c4 2020-07-23 stsp } else {
1715 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1716 a919d5c4 2020-07-23 stsp if (elen == -1)
1717 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1718 c363b2c1 2019-08-03 stsp }
1719 a919d5c4 2020-07-23 stsp
1720 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1721 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1722 a919d5c4 2020-07-23 stsp
1723 a919d5c4 2020-07-23 stsp return NULL;
1724 7154f6ce 2019-03-27 stsp }
1725 7154f6ce 2019-03-27 stsp
1726 7154f6ce 2019-03-27 stsp static const struct got_error *
1727 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1728 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1729 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1730 6353ad76 2019-02-08 stsp {
1731 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1732 6353ad76 2019-02-08 stsp struct got_object_id id;
1733 6353ad76 2019-02-08 stsp size_t hdrlen;
1734 1338848f 2019-12-13 stsp int fd = -1;
1735 6353ad76 2019-02-08 stsp FILE *f = NULL;
1736 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1737 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1738 6353ad76 2019-02-08 stsp size_t flen, blen;
1739 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1740 6353ad76 2019-02-08 stsp
1741 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1742 4cc1f028 2021-03-23 stsp memset(sb, 0, sizeof(*sb));
1743 6353ad76 2019-02-08 stsp
1744 7f91a133 2019-12-13 stsp /*
1745 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1746 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1747 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1748 7f91a133 2019-12-13 stsp */
1749 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1750 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1751 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1752 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1753 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1754 882ef1b9 2019-12-13 stsp else
1755 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1756 882ef1b9 2019-12-13 stsp goto done;
1757 882ef1b9 2019-12-13 stsp }
1758 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1759 3d35a492 2019-12-13 stsp goto done;
1760 3d35a492 2019-12-13 stsp }
1761 7f91a133 2019-12-13 stsp } else {
1762 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1763 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1764 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1765 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1766 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1767 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1768 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1769 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1770 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1771 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1772 3d35a492 2019-12-13 stsp else
1773 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1774 3d35a492 2019-12-13 stsp goto done;
1775 3d35a492 2019-12-13 stsp }
1776 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1777 1338848f 2019-12-13 stsp goto done;
1778 a378724f 2019-02-10 stsp }
1779 a378724f 2019-02-10 stsp }
1780 6353ad76 2019-02-08 stsp
1781 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1782 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1783 1338848f 2019-12-13 stsp goto done;
1784 3f148bc6 2019-07-27 stsp }
1785 efdd40df 2019-07-27 stsp
1786 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1787 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1788 1338848f 2019-12-13 stsp goto done;
1789 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1790 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1791 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1792 1338848f 2019-12-13 stsp goto done;
1793 d00136be 2019-03-26 stsp }
1794 b8f41171 2019-02-10 stsp
1795 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1796 f179e66d 2020-07-23 stsp goto done;
1797 f179e66d 2020-07-23 stsp
1798 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1799 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1800 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1801 1338848f 2019-12-13 stsp goto done;
1802 f179e66d 2020-07-23 stsp }
1803 6353ad76 2019-02-08 stsp
1804 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1805 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1806 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1807 c363b2c1 2019-08-03 stsp else
1808 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1809 c363b2c1 2019-08-03 stsp
1810 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1811 6353ad76 2019-02-08 stsp if (err)
1812 1338848f 2019-12-13 stsp goto done;
1813 6353ad76 2019-02-08 stsp
1814 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1815 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1816 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1817 a919d5c4 2020-07-23 stsp goto done;
1818 a919d5c4 2020-07-23 stsp }
1819 a919d5c4 2020-07-23 stsp
1820 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1821 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1822 ab0d4361 2019-12-13 stsp if (fd == -1) {
1823 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1824 ab0d4361 2019-12-13 stsp goto done;
1825 ab0d4361 2019-12-13 stsp }
1826 3d35a492 2019-12-13 stsp }
1827 3d35a492 2019-12-13 stsp
1828 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1829 6353ad76 2019-02-08 stsp if (f == NULL) {
1830 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1831 6353ad76 2019-02-08 stsp goto done;
1832 6353ad76 2019-02-08 stsp }
1833 1338848f 2019-12-13 stsp fd = -1;
1834 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1835 656b1f76 2019-05-11 jcs for (;;) {
1836 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1837 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1838 6353ad76 2019-02-08 stsp if (err)
1839 7154f6ce 2019-03-27 stsp goto done;
1840 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1841 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1842 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1843 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1844 7154f6ce 2019-03-27 stsp goto done;
1845 80c5c120 2019-02-19 stsp }
1846 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1847 6353ad76 2019-02-08 stsp if (flen != 0)
1848 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1849 6353ad76 2019-02-08 stsp break;
1850 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1851 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1852 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1853 6353ad76 2019-02-08 stsp break;
1854 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1855 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1856 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1857 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1858 6353ad76 2019-02-08 stsp break;
1859 6353ad76 2019-02-08 stsp }
1860 6353ad76 2019-02-08 stsp } else {
1861 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1862 6353ad76 2019-02-08 stsp break;
1863 6353ad76 2019-02-08 stsp }
1864 6353ad76 2019-02-08 stsp hdrlen = 0;
1865 6353ad76 2019-02-08 stsp }
1866 7154f6ce 2019-03-27 stsp
1867 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1868 7154f6ce 2019-03-27 stsp rewind(f);
1869 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1870 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1871 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1872 6353ad76 2019-02-08 stsp done:
1873 6353ad76 2019-02-08 stsp if (blob)
1874 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1875 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1876 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1877 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1878 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1879 9d31a1d8 2018-03-11 stsp return err;
1880 e2b1e152 2019-07-13 stsp }
1881 e2b1e152 2019-07-13 stsp
1882 e2b1e152 2019-07-13 stsp /*
1883 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1884 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1885 e2b1e152 2019-07-13 stsp */
1886 e2b1e152 2019-07-13 stsp static const struct got_error *
1887 437adc9d 2020-12-10 yzhong sync_timestamps(int wt_fd, const char *path, unsigned char status,
1888 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1889 e2b1e152 2019-07-13 stsp {
1890 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1891 437adc9d 2020-12-10 yzhong return got_fileindex_entry_update(ie, wt_fd, path,
1892 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1893 e2b1e152 2019-07-13 stsp
1894 e2b1e152 2019-07-13 stsp return NULL;
1895 9d31a1d8 2018-03-11 stsp }
1896 9d31a1d8 2018-03-11 stsp
1897 9d31a1d8 2018-03-11 stsp static const struct got_error *
1898 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1899 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1900 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1901 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1902 0584f854 2019-04-06 stsp void *progress_arg)
1903 9d31a1d8 2018-03-11 stsp {
1904 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1905 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1906 6353ad76 2019-02-08 stsp char *ondisk_path;
1907 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1908 b8f41171 2019-02-10 stsp struct stat sb;
1909 9d31a1d8 2018-03-11 stsp
1910 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1911 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1912 6353ad76 2019-02-08 stsp
1913 abb4604f 2019-07-27 stsp if (ie) {
1914 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1915 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1916 a76c42e6 2019-08-03 stsp goto done;
1917 a76c42e6 2019-08-03 stsp }
1918 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1919 7f91a133 2019-12-13 stsp repo);
1920 abb4604f 2019-07-27 stsp if (err)
1921 abb4604f 2019-07-27 stsp goto done;
1922 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1923 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1924 c90c8ce3 2020-07-23 stsp } else {
1925 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1926 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1927 c90c8ce3 2020-07-23 stsp }
1928 6353ad76 2019-02-08 stsp
1929 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1930 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1931 b8f41171 2019-02-10 stsp goto done;
1932 b8f41171 2019-02-10 stsp }
1933 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1934 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1935 5036ab18 2020-04-18 stsp path);
1936 5036ab18 2020-04-18 stsp goto done;
1937 5036ab18 2020-04-18 stsp }
1938 b8f41171 2019-02-10 stsp
1939 c6e8a826 2021-04-05 stsp if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1940 c6e8a826 2021-04-05 stsp (S_ISLNK(te->mode) ||
1941 c6e8a826 2021-04-05 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1942 c6e8a826 2021-04-05 stsp /*
1943 c6e8a826 2021-04-05 stsp * This is a regular file or an installed bad symlink.
1944 c6e8a826 2021-04-05 stsp * If the file index indicates that this file is already
1945 c6e8a826 2021-04-05 stsp * up-to-date with respect to the repository we can skip
1946 c6e8a826 2021-04-05 stsp * updating contents of this file.
1947 c6e8a826 2021-04-05 stsp */
1948 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1949 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1950 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1951 c6e8a826 2021-04-05 stsp /* Same commit. */
1952 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1953 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1954 e2b1e152 2019-07-13 stsp if (err)
1955 e2b1e152 2019-07-13 stsp goto done;
1956 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1957 b8f41171 2019-02-10 stsp path);
1958 6353ad76 2019-02-08 stsp goto done;
1959 a378724f 2019-02-10 stsp }
1960 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1961 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1962 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1963 c6e8a826 2021-04-05 stsp /* Different commit but the same blob. */
1964 437adc9d 2020-12-10 yzhong err = sync_timestamps(worktree->root_fd,
1965 437adc9d 2020-12-10 yzhong path, status, ie, &sb);
1966 0f58026f 2021-04-05 stsp if (err)
1967 0f58026f 2021-04-05 stsp goto done;
1968 0f58026f 2021-04-05 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1969 0f58026f 2021-04-05 stsp path);
1970 b8f41171 2019-02-10 stsp goto done;
1971 e2b1e152 2019-07-13 stsp }
1972 9d31a1d8 2018-03-11 stsp }
1973 9d31a1d8 2018-03-11 stsp
1974 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1975 8da9e5f4 2019-01-12 stsp if (err)
1976 6353ad76 2019-02-08 stsp goto done;
1977 512f0d0e 2019-01-02 stsp
1978 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1979 234035bc 2019-06-01 stsp int update_timestamps;
1980 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1981 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1982 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1983 234035bc 2019-06-01 stsp struct got_object_id id2;
1984 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1985 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1986 234035bc 2019-06-01 stsp if (err)
1987 f69721c3 2019-10-21 stsp goto done;
1988 f69721c3 2019-10-21 stsp }
1989 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1990 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1991 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1992 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1993 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1994 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1995 f69721c3 2019-10-21 stsp goto done;
1996 f69721c3 2019-10-21 stsp }
1997 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1998 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1999 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2000 234035bc 2019-06-01 stsp goto done;
2001 f69721c3 2019-10-21 stsp }
2002 234035bc 2019-06-01 stsp }
2003 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2004 36bf999c 2020-07-23 stsp char *link_target;
2005 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
2006 36bf999c 2020-07-23 stsp if (err)
2007 36bf999c 2020-07-23 stsp goto done;
2008 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
2009 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
2010 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
2011 36bf999c 2020-07-23 stsp free(link_target);
2012 993e2a1b 2020-07-23 stsp } else {
2013 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
2014 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
2015 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
2016 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
2017 993e2a1b 2020-07-23 stsp }
2018 f69721c3 2019-10-21 stsp free(label_orig);
2019 234035bc 2019-06-01 stsp if (blob2)
2020 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
2021 909d120e 2019-09-22 stsp if (err)
2022 909d120e 2019-09-22 stsp goto done;
2023 234035bc 2019-06-01 stsp /*
2024 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
2025 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
2026 234035bc 2019-06-01 stsp * unmodified files again.
2027 234035bc 2019-06-01 stsp */
2028 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2029 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
2030 234035bc 2019-06-01 stsp update_timestamps);
2031 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
2032 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2033 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2034 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
2035 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2036 1ee397ad 2019-07-12 stsp if (err)
2037 1ee397ad 2019-07-12 stsp goto done;
2038 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2039 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2040 13d9040b 2019-03-27 stsp if (err)
2041 13d9040b 2019-03-27 stsp goto done;
2042 13d9040b 2019-03-27 stsp } else {
2043 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2044 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2045 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2046 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2047 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2048 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2049 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2050 2e1fa222 2020-07-23 stsp } else {
2051 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2052 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2053 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2054 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2055 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2056 2e1fa222 2020-07-23 stsp }
2057 13d9040b 2019-03-27 stsp if (err)
2058 13d9040b 2019-03-27 stsp goto done;
2059 65b05cec 2020-07-23 stsp
2060 054041d0 2020-06-24 stsp if (ie) {
2061 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
2062 437adc9d 2020-12-10 yzhong worktree->root_fd, path, blob->id.sha1,
2063 437adc9d 2020-12-10 yzhong worktree->base_commit_id->sha1, 1);
2064 054041d0 2020-06-24 stsp } else {
2065 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2066 437adc9d 2020-12-10 yzhong worktree->base_commit_id, worktree->root_fd, path,
2067 054041d0 2020-06-24 stsp &blob->id);
2068 054041d0 2020-06-24 stsp }
2069 13d9040b 2019-03-27 stsp if (err)
2070 13d9040b 2019-03-27 stsp goto done;
2071 65b05cec 2020-07-23 stsp
2072 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2073 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2074 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2075 2e1fa222 2020-07-23 stsp }
2076 13d9040b 2019-03-27 stsp }
2077 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2078 6353ad76 2019-02-08 stsp done:
2079 6353ad76 2019-02-08 stsp free(ondisk_path);
2080 8da9e5f4 2019-01-12 stsp return err;
2081 90285c3b 2019-01-08 stsp }
2082 90285c3b 2019-01-08 stsp
2083 90285c3b 2019-01-08 stsp static const struct got_error *
2084 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2085 90285c3b 2019-01-08 stsp {
2086 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2087 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2088 90285c3b 2019-01-08 stsp
2089 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2090 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2091 90285c3b 2019-01-08 stsp
2092 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2093 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2094 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2095 c97665ae 2019-01-13 stsp } else {
2096 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2097 fddefe3b 2020-10-20 stsp do {
2098 fddefe3b 2020-10-20 stsp char *parent;
2099 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2100 fddefe3b 2020-10-20 stsp if (err)
2101 2513f20a 2020-10-20 stsp break;
2102 fddefe3b 2020-10-20 stsp free(ondisk_path);
2103 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2104 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2105 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2106 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2107 fddefe3b 2020-10-20 stsp ondisk_path);
2108 90285c3b 2019-01-08 stsp break;
2109 90285c3b 2019-01-08 stsp }
2110 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2111 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2112 90285c3b 2019-01-08 stsp }
2113 90285c3b 2019-01-08 stsp free(ondisk_path);
2114 90285c3b 2019-01-08 stsp return err;
2115 512f0d0e 2019-01-02 stsp }
2116 512f0d0e 2019-01-02 stsp
2117 708d8e67 2019-03-27 stsp static const struct got_error *
2118 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2119 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2120 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2121 708d8e67 2019-03-27 stsp {
2122 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2123 708d8e67 2019-03-27 stsp unsigned char status;
2124 708d8e67 2019-03-27 stsp struct stat sb;
2125 708d8e67 2019-03-27 stsp char *ondisk_path;
2126 708d8e67 2019-03-27 stsp
2127 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2128 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2129 a76c42e6 2019-08-03 stsp
2130 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2131 708d8e67 2019-03-27 stsp == -1)
2132 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2133 708d8e67 2019-03-27 stsp
2134 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2135 708d8e67 2019-03-27 stsp if (err)
2136 5a58a424 2020-06-23 stsp goto done;
2137 708d8e67 2019-03-27 stsp
2138 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2139 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2140 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2141 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2142 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2143 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2144 993e2a1b 2020-07-23 stsp goto done;
2145 993e2a1b 2020-07-23 stsp }
2146 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2147 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2148 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2149 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2150 993e2a1b 2020-07-23 stsp if (err)
2151 993e2a1b 2020-07-23 stsp goto done;
2152 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2153 993e2a1b 2020-07-23 stsp ie->path);
2154 993e2a1b 2020-07-23 stsp goto done;
2155 993e2a1b 2020-07-23 stsp }
2156 993e2a1b 2020-07-23 stsp
2157 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2158 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2159 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2160 1ee397ad 2019-07-12 stsp if (err)
2161 5a58a424 2020-06-23 stsp goto done;
2162 fc6346c4 2019-03-27 stsp /*
2163 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2164 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2165 fc6346c4 2019-03-27 stsp */
2166 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, worktree->root_fd,
2167 437adc9d 2020-12-10 yzhong ie->path, NULL, NULL, 0);
2168 fc6346c4 2019-03-27 stsp } else {
2169 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2170 1ee397ad 2019-07-12 stsp if (err)
2171 5a58a424 2020-06-23 stsp goto done;
2172 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2173 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2174 fc6346c4 2019-03-27 stsp if (err)
2175 5a58a424 2020-06-23 stsp goto done;
2176 fc6346c4 2019-03-27 stsp }
2177 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2178 708d8e67 2019-03-27 stsp }
2179 5a58a424 2020-06-23 stsp done:
2180 5a58a424 2020-06-23 stsp free(ondisk_path);
2181 fc6346c4 2019-03-27 stsp return err;
2182 708d8e67 2019-03-27 stsp }
2183 708d8e67 2019-03-27 stsp
2184 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2185 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2186 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2187 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2188 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2189 8da9e5f4 2019-01-12 stsp void *progress_arg;
2190 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2191 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2192 8da9e5f4 2019-01-12 stsp };
2193 8da9e5f4 2019-01-12 stsp
2194 512f0d0e 2019-01-02 stsp static const struct got_error *
2195 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2196 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2197 512f0d0e 2019-01-02 stsp {
2198 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2199 0584f854 2019-04-06 stsp
2200 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2201 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2202 512f0d0e 2019-01-02 stsp
2203 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2204 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2205 8da9e5f4 2019-01-12 stsp }
2206 512f0d0e 2019-01-02 stsp
2207 8da9e5f4 2019-01-12 stsp static const struct got_error *
2208 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2209 8da9e5f4 2019-01-12 stsp {
2210 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2211 7a9df742 2019-01-08 stsp
2212 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2213 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2214 0584f854 2019-04-06 stsp
2215 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2216 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2217 9d31a1d8 2018-03-11 stsp }
2218 9d31a1d8 2018-03-11 stsp
2219 9d31a1d8 2018-03-11 stsp static const struct got_error *
2220 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2221 9d31a1d8 2018-03-11 stsp {
2222 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2223 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2224 8da9e5f4 2019-01-12 stsp char *path;
2225 9d31a1d8 2018-03-11 stsp
2226 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2227 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2228 0584f854 2019-04-06 stsp
2229 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2230 63c5ca5d 2019-08-24 stsp return NULL;
2231 63c5ca5d 2019-08-24 stsp
2232 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2233 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2234 8da9e5f4 2019-01-12 stsp == -1)
2235 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2236 9d31a1d8 2018-03-11 stsp
2237 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2238 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2239 8da9e5f4 2019-01-12 stsp else
2240 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2241 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2242 9d31a1d8 2018-03-11 stsp
2243 8da9e5f4 2019-01-12 stsp free(path);
2244 0cd1c46a 2019-03-11 stsp return err;
2245 0cd1c46a 2019-03-11 stsp }
2246 0cd1c46a 2019-03-11 stsp
2247 b2118c49 2020-07-28 stsp const struct got_error *
2248 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2249 b2118c49 2020-07-28 stsp {
2250 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2251 b2118c49 2020-07-28 stsp
2252 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2253 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2254 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2255 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2256 b2118c49 2020-07-28 stsp }
2257 b2118c49 2020-07-28 stsp
2258 b2118c49 2020-07-28 stsp return NULL;
2259 b2118c49 2020-07-28 stsp }
2260 b2118c49 2020-07-28 stsp
2261 818c7501 2019-07-11 stsp static const struct got_error *
2262 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2263 0cd1c46a 2019-03-11 stsp {
2264 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2265 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2266 0cd1c46a 2019-03-11 stsp
2267 517bab73 2019-03-11 stsp *refname = NULL;
2268 517bab73 2019-03-11 stsp
2269 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2270 b2118c49 2020-07-28 stsp if (err)
2271 b2118c49 2020-07-28 stsp return err;
2272 0cd1c46a 2019-03-11 stsp
2273 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2274 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2275 0647c563 2019-03-11 stsp *refname = NULL;
2276 0cd1c46a 2019-03-11 stsp }
2277 517bab73 2019-03-11 stsp free(uuidstr);
2278 517bab73 2019-03-11 stsp return err;
2279 517bab73 2019-03-11 stsp }
2280 517bab73 2019-03-11 stsp
2281 818c7501 2019-07-11 stsp const struct got_error *
2282 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2283 818c7501 2019-07-11 stsp {
2284 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2285 818c7501 2019-07-11 stsp }
2286 818c7501 2019-07-11 stsp
2287 818c7501 2019-07-11 stsp static const struct got_error *
2288 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2289 818c7501 2019-07-11 stsp {
2290 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2291 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2292 818c7501 2019-07-11 stsp }
2293 818c7501 2019-07-11 stsp
2294 818c7501 2019-07-11 stsp static const struct got_error *
2295 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2296 818c7501 2019-07-11 stsp {
2297 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2298 818c7501 2019-07-11 stsp }
2299 818c7501 2019-07-11 stsp
2300 818c7501 2019-07-11 stsp static const struct got_error *
2301 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2302 818c7501 2019-07-11 stsp {
2303 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2304 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2305 818c7501 2019-07-11 stsp }
2306 818c7501 2019-07-11 stsp
2307 818c7501 2019-07-11 stsp static const struct got_error *
2308 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2309 818c7501 2019-07-11 stsp {
2310 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2311 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2312 0ebf8283 2019-07-24 stsp }
2313 0ebf8283 2019-07-24 stsp
2314 0ebf8283 2019-07-24 stsp static const struct got_error *
2315 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2316 0ebf8283 2019-07-24 stsp {
2317 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2318 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2319 0ebf8283 2019-07-24 stsp }
2320 0ebf8283 2019-07-24 stsp
2321 0ebf8283 2019-07-24 stsp static const struct got_error *
2322 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2323 0ebf8283 2019-07-24 stsp {
2324 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2325 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2326 0ebf8283 2019-07-24 stsp }
2327 0ebf8283 2019-07-24 stsp
2328 0ebf8283 2019-07-24 stsp static const struct got_error *
2329 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2330 0ebf8283 2019-07-24 stsp {
2331 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2332 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2333 818c7501 2019-07-11 stsp }
2334 818c7501 2019-07-11 stsp
2335 0ebf8283 2019-07-24 stsp static const struct got_error *
2336 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2337 0ebf8283 2019-07-24 stsp {
2338 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2339 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2340 0ebf8283 2019-07-24 stsp }
2341 818c7501 2019-07-11 stsp
2342 0ebf8283 2019-07-24 stsp const struct got_error *
2343 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2344 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2345 0ebf8283 2019-07-24 stsp {
2346 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2347 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2348 0ebf8283 2019-07-24 stsp *path = NULL;
2349 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2350 0ebf8283 2019-07-24 stsp }
2351 0ebf8283 2019-07-24 stsp return NULL;
2352 0ebf8283 2019-07-24 stsp }
2353 0ebf8283 2019-07-24 stsp
2354 517bab73 2019-03-11 stsp /*
2355 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2356 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2357 517bab73 2019-03-11 stsp */
2358 517bab73 2019-03-11 stsp static const struct got_error *
2359 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2360 517bab73 2019-03-11 stsp {
2361 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2362 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2363 517bab73 2019-03-11 stsp char *refname;
2364 517bab73 2019-03-11 stsp
2365 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2366 517bab73 2019-03-11 stsp if (err)
2367 517bab73 2019-03-11 stsp return err;
2368 0cd1c46a 2019-03-11 stsp
2369 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2370 0cd1c46a 2019-03-11 stsp if (err)
2371 0cd1c46a 2019-03-11 stsp goto done;
2372 0cd1c46a 2019-03-11 stsp
2373 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2374 0cd1c46a 2019-03-11 stsp done:
2375 0cd1c46a 2019-03-11 stsp free(refname);
2376 0cd1c46a 2019-03-11 stsp if (ref)
2377 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2378 3e3a69f1 2019-07-25 stsp return err;
2379 3e3a69f1 2019-07-25 stsp }
2380 3e3a69f1 2019-07-25 stsp
2381 3e3a69f1 2019-07-25 stsp static const struct got_error *
2382 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2383 3e3a69f1 2019-07-25 stsp {
2384 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2385 3e3a69f1 2019-07-25 stsp
2386 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2387 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2388 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2389 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2390 3e3a69f1 2019-07-25 stsp }
2391 9d31a1d8 2018-03-11 stsp return err;
2392 9d31a1d8 2018-03-11 stsp }
2393 9d31a1d8 2018-03-11 stsp
2394 3e3a69f1 2019-07-25 stsp
2395 ebf99748 2019-05-09 stsp static const struct got_error *
2396 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2397 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2398 ebf99748 2019-05-09 stsp {
2399 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2400 ebf99748 2019-05-09 stsp FILE *index = NULL;
2401 0cd1c46a 2019-03-11 stsp
2402 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2403 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2404 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2405 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2406 ebf99748 2019-05-09 stsp
2407 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2408 3e3a69f1 2019-07-25 stsp if (err)
2409 ebf99748 2019-05-09 stsp goto done;
2410 ebf99748 2019-05-09 stsp
2411 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2412 ebf99748 2019-05-09 stsp if (index == NULL) {
2413 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2414 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2415 ebf99748 2019-05-09 stsp } else {
2416 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2417 56b63ca4 2021-01-22 stsp if (fclose(index) == EOF && err == NULL)
2418 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2419 ebf99748 2019-05-09 stsp }
2420 ebf99748 2019-05-09 stsp done:
2421 ebf99748 2019-05-09 stsp if (err) {
2422 ebf99748 2019-05-09 stsp free(*fileindex_path);
2423 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2424 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2425 ebf99748 2019-05-09 stsp *fileindex = NULL;
2426 ebf99748 2019-05-09 stsp }
2427 ebf99748 2019-05-09 stsp return err;
2428 ebf99748 2019-05-09 stsp }
2429 c932eeeb 2019-05-22 stsp
2430 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2431 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2432 c932eeeb 2019-05-22 stsp const char *path;
2433 c932eeeb 2019-05-22 stsp size_t path_len;
2434 c932eeeb 2019-05-22 stsp const char *entry_name;
2435 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2436 a484d721 2019-06-10 stsp void *progress_arg;
2437 c932eeeb 2019-05-22 stsp };
2438 ebf99748 2019-05-09 stsp
2439 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2440 c932eeeb 2019-05-22 stsp static const struct got_error *
2441 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2442 c932eeeb 2019-05-22 stsp {
2443 1ee397ad 2019-07-12 stsp const struct got_error *err;
2444 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2445 c932eeeb 2019-05-22 stsp
2446 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2447 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2448 c932eeeb 2019-05-22 stsp return NULL;
2449 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2450 102ff934 2019-06-11 stsp return NULL;
2451 102ff934 2019-06-11 stsp
2452 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2453 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2454 c932eeeb 2019-05-22 stsp return NULL;
2455 c932eeeb 2019-05-22 stsp
2456 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2457 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2458 edd02c5e 2019-07-11 stsp ie->path);
2459 1ee397ad 2019-07-12 stsp if (err)
2460 1ee397ad 2019-07-12 stsp return err;
2461 1ee397ad 2019-07-12 stsp }
2462 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2463 c932eeeb 2019-05-22 stsp return NULL;
2464 a615e0e7 2020-12-16 stsp }
2465 a615e0e7 2020-12-16 stsp
2466 a615e0e7 2020-12-16 stsp static const struct got_error *
2467 a615e0e7 2020-12-16 stsp bump_base_commit_id_everywhere(struct got_worktree *worktree,
2468 a615e0e7 2020-12-16 stsp struct got_fileindex *fileindex,
2469 a615e0e7 2020-12-16 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2470 a615e0e7 2020-12-16 stsp {
2471 a615e0e7 2020-12-16 stsp struct bump_base_commit_id_arg bbc_arg;
2472 a615e0e7 2020-12-16 stsp
2473 a615e0e7 2020-12-16 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2474 a615e0e7 2020-12-16 stsp bbc_arg.entry_name = NULL;
2475 a615e0e7 2020-12-16 stsp bbc_arg.path = "";
2476 a615e0e7 2020-12-16 stsp bbc_arg.path_len = 0;
2477 a615e0e7 2020-12-16 stsp bbc_arg.progress_cb = progress_cb;
2478 a615e0e7 2020-12-16 stsp bbc_arg.progress_arg = progress_arg;
2479 a615e0e7 2020-12-16 stsp
2480 a615e0e7 2020-12-16 stsp return got_fileindex_for_each_entry_safe(fileindex,
2481 a615e0e7 2020-12-16 stsp bump_base_commit_id, &bbc_arg);
2482 9c6338c4 2019-06-02 stsp }
2483 9c6338c4 2019-06-02 stsp
2484 9c6338c4 2019-06-02 stsp static const struct got_error *
2485 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2486 9c6338c4 2019-06-02 stsp {
2487 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2488 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2489 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2490 867630bb 2020-01-17 stsp struct timespec timeout;
2491 9c6338c4 2019-06-02 stsp
2492 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2493 9c6338c4 2019-06-02 stsp fileindex_path);
2494 9c6338c4 2019-06-02 stsp if (err)
2495 9c6338c4 2019-06-02 stsp goto done;
2496 9c6338c4 2019-06-02 stsp
2497 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2498 9c6338c4 2019-06-02 stsp if (err)
2499 9c6338c4 2019-06-02 stsp goto done;
2500 9c6338c4 2019-06-02 stsp
2501 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2502 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2503 9c6338c4 2019-06-02 stsp fileindex_path);
2504 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2505 9c6338c4 2019-06-02 stsp }
2506 867630bb 2020-01-17 stsp
2507 867630bb 2020-01-17 stsp /*
2508 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2509 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2510 867630bb 2020-01-17 stsp * was recorded in the file index.
2511 867630bb 2020-01-17 stsp */
2512 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2513 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2514 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2515 9c6338c4 2019-06-02 stsp done:
2516 9c6338c4 2019-06-02 stsp if (new_index)
2517 9c6338c4 2019-06-02 stsp fclose(new_index);
2518 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2519 9c6338c4 2019-06-02 stsp return err;
2520 c932eeeb 2019-05-22 stsp }
2521 6ced4176 2019-07-12 stsp
2522 6ced4176 2019-07-12 stsp static const struct got_error *
2523 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2524 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2525 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2526 6ced4176 2019-07-12 stsp {
2527 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2528 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2529 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2530 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2531 6ced4176 2019-07-12 stsp
2532 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2533 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2534 6ced4176 2019-07-12 stsp *tree_id = NULL;
2535 6ced4176 2019-07-12 stsp
2536 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2537 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2538 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2539 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2540 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2541 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2542 6ced4176 2019-07-12 stsp goto done;
2543 6ced4176 2019-07-12 stsp }
2544 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2545 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2546 6ced4176 2019-07-12 stsp if (err)
2547 6ced4176 2019-07-12 stsp goto done;
2548 6ced4176 2019-07-12 stsp return NULL;
2549 6ced4176 2019-07-12 stsp }
2550 6ced4176 2019-07-12 stsp
2551 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2552 6ced4176 2019-07-12 stsp
2553 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2554 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2555 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2556 6ced4176 2019-07-12 stsp goto done;
2557 6ced4176 2019-07-12 stsp }
2558 6ced4176 2019-07-12 stsp
2559 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2560 6ced4176 2019-07-12 stsp in_repo_path);
2561 6ced4176 2019-07-12 stsp if (err)
2562 6ced4176 2019-07-12 stsp goto done;
2563 6ced4176 2019-07-12 stsp
2564 6ced4176 2019-07-12 stsp free(in_repo_path);
2565 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2566 6ced4176 2019-07-12 stsp
2567 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2568 6ced4176 2019-07-12 stsp if (err)
2569 6ced4176 2019-07-12 stsp goto done;
2570 c932eeeb 2019-05-22 stsp
2571 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2572 6ced4176 2019-07-12 stsp /* Check out a single file. */
2573 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2574 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2575 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2576 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2577 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2578 6ced4176 2019-07-12 stsp goto done;
2579 6ced4176 2019-07-12 stsp }
2580 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2581 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2582 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2583 6ced4176 2019-07-12 stsp goto done;
2584 6ced4176 2019-07-12 stsp }
2585 6ced4176 2019-07-12 stsp } else {
2586 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2587 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2588 6ced4176 2019-07-12 stsp if (err)
2589 6ced4176 2019-07-12 stsp return err;
2590 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2591 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2592 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2593 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2594 6ced4176 2019-07-12 stsp goto done;
2595 6ced4176 2019-07-12 stsp }
2596 6ced4176 2019-07-12 stsp }
2597 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2598 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2599 6ced4176 2019-07-12 stsp } else {
2600 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2601 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2602 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2603 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2604 6ced4176 2019-07-12 stsp goto done;
2605 6ced4176 2019-07-12 stsp }
2606 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2607 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2608 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2609 6ced4176 2019-07-12 stsp goto done;
2610 6ced4176 2019-07-12 stsp }
2611 6ced4176 2019-07-12 stsp }
2612 6ced4176 2019-07-12 stsp done:
2613 6ced4176 2019-07-12 stsp free(id);
2614 6ced4176 2019-07-12 stsp free(in_repo_path);
2615 6ced4176 2019-07-12 stsp if (err) {
2616 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2617 6ced4176 2019-07-12 stsp free(*tree_relpath);
2618 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2619 6ced4176 2019-07-12 stsp free(*tree_id);
2620 6ced4176 2019-07-12 stsp *tree_id = NULL;
2621 6ced4176 2019-07-12 stsp }
2622 07ed472d 2019-07-12 stsp return err;
2623 07ed472d 2019-07-12 stsp }
2624 07ed472d 2019-07-12 stsp
2625 07ed472d 2019-07-12 stsp static const struct got_error *
2626 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2627 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2628 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2629 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2630 07ed472d 2019-07-12 stsp {
2631 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2632 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2633 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2634 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2635 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2636 07ed472d 2019-07-12 stsp
2637 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2638 7f47418f 2019-12-20 stsp if (err) {
2639 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2640 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2641 7f47418f 2019-12-20 stsp goto done;
2642 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2643 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2644 7f47418f 2019-12-20 stsp if (err)
2645 7f47418f 2019-12-20 stsp return err;
2646 7f47418f 2019-12-20 stsp }
2647 07ed472d 2019-07-12 stsp
2648 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2649 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2650 07ed472d 2019-07-12 stsp if (err)
2651 07ed472d 2019-07-12 stsp goto done;
2652 07ed472d 2019-07-12 stsp
2653 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2654 07ed472d 2019-07-12 stsp if (err)
2655 07ed472d 2019-07-12 stsp goto done;
2656 07ed472d 2019-07-12 stsp
2657 07ed472d 2019-07-12 stsp if (entry_name &&
2658 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2659 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2660 07ed472d 2019-07-12 stsp goto done;
2661 07ed472d 2019-07-12 stsp }
2662 07ed472d 2019-07-12 stsp
2663 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2664 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2665 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2666 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2667 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2668 07ed472d 2019-07-12 stsp arg.repo = repo;
2669 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2670 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2671 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2672 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2673 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2674 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2675 07ed472d 2019-07-12 stsp done:
2676 07ed472d 2019-07-12 stsp if (tree)
2677 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2678 07ed472d 2019-07-12 stsp if (commit)
2679 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2680 6ced4176 2019-07-12 stsp return err;
2681 6ced4176 2019-07-12 stsp }
2682 6ced4176 2019-07-12 stsp
2683 9d31a1d8 2018-03-11 stsp const struct got_error *
2684 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2685 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2686 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2687 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2688 9d31a1d8 2018-03-11 stsp {
2689 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2690 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2691 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2692 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2693 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2694 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2695 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2696 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2697 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2698 f2ea84fa 2019-07-27 stsp int entry_type;
2699 f2ea84fa 2019-07-27 stsp char *relpath;
2700 f2ea84fa 2019-07-27 stsp char *entry_name;
2701 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2702 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2703 9d31a1d8 2018-03-11 stsp
2704 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2705 f2ea84fa 2019-07-27 stsp
2706 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2707 9d31a1d8 2018-03-11 stsp if (err)
2708 9d31a1d8 2018-03-11 stsp return err;
2709 93a30277 2018-12-24 stsp
2710 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2711 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2712 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2713 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2714 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2715 f2ea84fa 2019-07-27 stsp goto done;
2716 f2ea84fa 2019-07-27 stsp }
2717 6ced4176 2019-07-12 stsp
2718 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2719 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2720 f2ea84fa 2019-07-27 stsp if (err) {
2721 f2ea84fa 2019-07-27 stsp free(tpd);
2722 6ced4176 2019-07-12 stsp goto done;
2723 6ced4176 2019-07-12 stsp }
2724 f2ea84fa 2019-07-27 stsp
2725 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2726 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2727 f2ea84fa 2019-07-27 stsp if (err) {
2728 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2729 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2730 f2ea84fa 2019-07-27 stsp free(tpd);
2731 f2ea84fa 2019-07-27 stsp goto done;
2732 f2ea84fa 2019-07-27 stsp }
2733 f2ea84fa 2019-07-27 stsp } else
2734 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2735 f2ea84fa 2019-07-27 stsp
2736 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2737 6ced4176 2019-07-12 stsp }
2738 6ced4176 2019-07-12 stsp
2739 51514078 2018-12-25 stsp /*
2740 51514078 2018-12-25 stsp * Read the file index.
2741 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2742 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2743 51514078 2018-12-25 stsp */
2744 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2745 8da9e5f4 2019-01-12 stsp if (err)
2746 c4cdcb68 2019-04-03 stsp goto done;
2747 c4cdcb68 2019-04-03 stsp
2748 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2749 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2750 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2751 f2ea84fa 2019-07-27 stsp
2752 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2753 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2754 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2755 f2ea84fa 2019-07-27 stsp if (err)
2756 f2ea84fa 2019-07-27 stsp break;
2757 f2ea84fa 2019-07-27 stsp
2758 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2759 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2760 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2761 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2762 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2763 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2764 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2765 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2766 f2ea84fa 2019-07-27 stsp if (err)
2767 f2ea84fa 2019-07-27 stsp break;
2768 f2ea84fa 2019-07-27 stsp
2769 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2770 711d9cd9 2019-07-12 stsp }
2771 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2772 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2773 af12c6b9 2019-06-04 stsp err = sync_err;
2774 9d31a1d8 2018-03-11 stsp done:
2775 9c6338c4 2019-06-02 stsp free(fileindex_path);
2776 edfa7d7f 2018-09-11 stsp if (tree)
2777 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2778 9d31a1d8 2018-03-11 stsp if (commit)
2779 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2780 5ade8233 2019-07-12 stsp if (fileindex)
2781 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2782 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2783 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2784 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2785 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2786 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2787 f2ea84fa 2019-07-27 stsp free(tpd);
2788 f2ea84fa 2019-07-27 stsp }
2789 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2790 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2791 9d31a1d8 2018-03-11 stsp err = unlockerr;
2792 f8d1f275 2019-02-04 stsp return err;
2793 f8d1f275 2019-02-04 stsp }
2794 f8d1f275 2019-02-04 stsp
2795 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2796 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2797 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2798 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2799 234035bc 2019-06-01 stsp void *progress_arg;
2800 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2801 234035bc 2019-06-01 stsp void *cancel_arg;
2802 f69721c3 2019-10-21 stsp const char *label_orig;
2803 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2804 234035bc 2019-06-01 stsp };
2805 234035bc 2019-06-01 stsp
2806 234035bc 2019-06-01 stsp static const struct got_error *
2807 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2808 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2809 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2810 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2811 234035bc 2019-06-01 stsp {
2812 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2813 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2814 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2815 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2816 234035bc 2019-06-01 stsp struct stat sb;
2817 234035bc 2019-06-01 stsp unsigned char status;
2818 234035bc 2019-06-01 stsp int local_changes_subsumed;
2819 234035bc 2019-06-01 stsp
2820 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2821 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2822 d6c87207 2019-08-02 stsp strlen(path2));
2823 1ee397ad 2019-07-12 stsp if (ie == NULL)
2824 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2825 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2826 234035bc 2019-06-01 stsp
2827 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2828 234035bc 2019-06-01 stsp path2) == -1)
2829 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2830 234035bc 2019-06-01 stsp
2831 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2832 7f91a133 2019-12-13 stsp repo);
2833 234035bc 2019-06-01 stsp if (err)
2834 234035bc 2019-06-01 stsp goto done;
2835 234035bc 2019-06-01 stsp
2836 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2837 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2838 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2839 234035bc 2019-06-01 stsp goto done;
2840 234035bc 2019-06-01 stsp }
2841 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2842 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2843 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2844 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2845 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2846 234035bc 2019-06-01 stsp goto done;
2847 234035bc 2019-06-01 stsp }
2848 234035bc 2019-06-01 stsp
2849 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2850 36bf999c 2020-07-23 stsp char *link_target2;
2851 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2852 36bf999c 2020-07-23 stsp if (err)
2853 36bf999c 2020-07-23 stsp goto done;
2854 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2855 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2856 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2857 36bf999c 2020-07-23 stsp free(link_target2);
2858 af57b12a 2020-07-23 stsp } else {
2859 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2860 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2861 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2862 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2863 af57b12a 2020-07-23 stsp }
2864 234035bc 2019-06-01 stsp } else if (blob1) {
2865 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2866 d6c87207 2019-08-02 stsp strlen(path1));
2867 1ee397ad 2019-07-12 stsp if (ie == NULL)
2868 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2869 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2870 2b92fad7 2019-06-02 stsp
2871 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2872 2b92fad7 2019-06-02 stsp path1) == -1)
2873 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2874 2b92fad7 2019-06-02 stsp
2875 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2876 7f91a133 2019-12-13 stsp repo);
2877 2b92fad7 2019-06-02 stsp if (err)
2878 2b92fad7 2019-06-02 stsp goto done;
2879 2b92fad7 2019-06-02 stsp
2880 2b92fad7 2019-06-02 stsp switch (status) {
2881 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2882 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2883 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2884 1ee397ad 2019-07-12 stsp if (err)
2885 1ee397ad 2019-07-12 stsp goto done;
2886 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2887 2b92fad7 2019-06-02 stsp if (err)
2888 2b92fad7 2019-06-02 stsp goto done;
2889 2b92fad7 2019-06-02 stsp if (ie)
2890 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2891 2b92fad7 2019-06-02 stsp break;
2892 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2893 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2894 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2895 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2896 1ee397ad 2019-07-12 stsp if (err)
2897 1ee397ad 2019-07-12 stsp goto done;
2898 2b92fad7 2019-06-02 stsp if (ie)
2899 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2900 2b92fad7 2019-06-02 stsp break;
2901 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2902 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2903 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2904 0a22ca1a 2020-09-23 stsp /*
2905 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2906 0a22ca1a 2020-09-23 stsp * exists in the repository.
2907 0a22ca1a 2020-09-23 stsp */
2908 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2909 0a22ca1a 2020-09-23 stsp if (err)
2910 0a22ca1a 2020-09-23 stsp goto done;
2911 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2912 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2913 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2914 0a22ca1a 2020-09-23 stsp if (err)
2915 0a22ca1a 2020-09-23 stsp goto done;
2916 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2917 0a22ca1a 2020-09-23 stsp path1);
2918 0a22ca1a 2020-09-23 stsp if (err)
2919 0a22ca1a 2020-09-23 stsp goto done;
2920 0a22ca1a 2020-09-23 stsp if (ie)
2921 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2922 0a22ca1a 2020-09-23 stsp ie);
2923 0a22ca1a 2020-09-23 stsp } else {
2924 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2925 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2926 0a22ca1a 2020-09-23 stsp }
2927 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2928 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2929 0a22ca1a 2020-09-23 stsp free(id);
2930 0a22ca1a 2020-09-23 stsp if (err)
2931 0a22ca1a 2020-09-23 stsp goto done;
2932 0a22ca1a 2020-09-23 stsp break;
2933 0a22ca1a 2020-09-23 stsp }
2934 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2935 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2936 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2937 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2938 1ee397ad 2019-07-12 stsp if (err)
2939 1ee397ad 2019-07-12 stsp goto done;
2940 2b92fad7 2019-06-02 stsp break;
2941 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2942 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2943 1ee397ad 2019-07-12 stsp if (err)
2944 1ee397ad 2019-07-12 stsp goto done;
2945 2b92fad7 2019-06-02 stsp break;
2946 2b92fad7 2019-06-02 stsp default:
2947 2b92fad7 2019-06-02 stsp break;
2948 2b92fad7 2019-06-02 stsp }
2949 234035bc 2019-06-01 stsp } else if (blob2) {
2950 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2951 234035bc 2019-06-01 stsp path2) == -1)
2952 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2953 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2954 d6c87207 2019-08-02 stsp strlen(path2));
2955 234035bc 2019-06-01 stsp if (ie) {
2956 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2957 7f91a133 2019-12-13 stsp -1, NULL, repo);
2958 234035bc 2019-06-01 stsp if (err)
2959 234035bc 2019-06-01 stsp goto done;
2960 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2961 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2962 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2963 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2964 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2965 1ee397ad 2019-07-12 stsp status, path2);
2966 234035bc 2019-06-01 stsp goto done;
2967 234035bc 2019-06-01 stsp }
2968 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2969 36bf999c 2020-07-23 stsp char *link_target2;
2970 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2971 36bf999c 2020-07-23 stsp blob2);
2972 36bf999c 2020-07-23 stsp if (err)
2973 36bf999c 2020-07-23 stsp goto done;
2974 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2975 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2976 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2977 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2978 36bf999c 2020-07-23 stsp free(link_target2);
2979 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2980 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2981 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2982 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2983 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2984 526a746f 2020-07-23 stsp a->progress_arg);
2985 dfe9fba0 2020-07-23 stsp } else {
2986 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2987 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2988 526a746f 2020-07-23 stsp }
2989 dfe9fba0 2020-07-23 stsp if (err)
2990 dfe9fba0 2020-07-23 stsp goto done;
2991 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2992 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2993 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, blob2->id.sha1,
2994 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2995 234035bc 2019-06-01 stsp if (err)
2996 234035bc 2019-06-01 stsp goto done;
2997 234035bc 2019-06-01 stsp }
2998 234035bc 2019-06-01 stsp } else {
2999 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
3000 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
3001 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
3002 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
3003 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
3004 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
3005 2e1fa222 2020-07-23 stsp } else {
3006 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
3007 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3008 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
3009 2e1fa222 2020-07-23 stsp }
3010 234035bc 2019-06-01 stsp if (err)
3011 234035bc 2019-06-01 stsp goto done;
3012 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
3013 234035bc 2019-06-01 stsp if (err)
3014 234035bc 2019-06-01 stsp goto done;
3015 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
3016 437adc9d 2020-12-10 yzhong a->worktree->root_fd, path2, NULL, NULL, 1);
3017 3969253a 2020-03-07 stsp if (err) {
3018 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3019 3969253a 2020-03-07 stsp goto done;
3020 3969253a 2020-03-07 stsp }
3021 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3022 2b92fad7 2019-06-02 stsp if (err) {
3023 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
3024 2b92fad7 2019-06-02 stsp goto done;
3025 2b92fad7 2019-06-02 stsp }
3026 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
3027 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
3028 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
3029 2e1fa222 2020-07-23 stsp }
3030 234035bc 2019-06-01 stsp }
3031 234035bc 2019-06-01 stsp }
3032 234035bc 2019-06-01 stsp done:
3033 234035bc 2019-06-01 stsp free(ondisk_path);
3034 234035bc 2019-06-01 stsp return err;
3035 234035bc 2019-06-01 stsp }
3036 234035bc 2019-06-01 stsp
3037 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
3038 234035bc 2019-06-01 stsp struct got_worktree *worktree;
3039 234035bc 2019-06-01 stsp struct got_repository *repo;
3040 234035bc 2019-06-01 stsp };
3041 234035bc 2019-06-01 stsp
3042 234035bc 2019-06-01 stsp static const struct got_error *
3043 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3044 234035bc 2019-06-01 stsp {
3045 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
3046 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
3047 234035bc 2019-06-01 stsp unsigned char status;
3048 234035bc 2019-06-01 stsp struct stat sb;
3049 234035bc 2019-06-01 stsp char *ondisk_path;
3050 234035bc 2019-06-01 stsp
3051 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
3052 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3053 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
3054 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3055 234035bc 2019-06-01 stsp
3056 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3057 234035bc 2019-06-01 stsp == -1)
3058 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3059 234035bc 2019-06-01 stsp
3060 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3061 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3062 234035bc 2019-06-01 stsp if (err)
3063 234035bc 2019-06-01 stsp return err;
3064 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3065 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3066 234035bc 2019-06-01 stsp
3067 234035bc 2019-06-01 stsp return NULL;
3068 234035bc 2019-06-01 stsp }
3069 234035bc 2019-06-01 stsp
3070 818c7501 2019-07-11 stsp static const struct got_error *
3071 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3072 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3073 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3074 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3075 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3076 234035bc 2019-06-01 stsp {
3077 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3078 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3079 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3080 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3081 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3082 234035bc 2019-06-01 stsp
3083 03415a1a 2019-06-02 stsp if (commit_id1) {
3084 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3085 03415a1a 2019-06-02 stsp worktree->path_prefix);
3086 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3087 03415a1a 2019-06-02 stsp goto done;
3088 69d57f3d 2020-07-31 stsp }
3089 69d57f3d 2020-07-31 stsp if (tree_id1) {
3090 69d57f3d 2020-07-31 stsp char *id_str;
3091 03415a1a 2019-06-02 stsp
3092 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3093 03415a1a 2019-06-02 stsp if (err)
3094 03415a1a 2019-06-02 stsp goto done;
3095 f69721c3 2019-10-21 stsp
3096 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3097 f69721c3 2019-10-21 stsp if (err)
3098 f69721c3 2019-10-21 stsp goto done;
3099 f69721c3 2019-10-21 stsp
3100 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3101 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3102 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3103 f69721c3 2019-10-21 stsp free(id_str);
3104 f69721c3 2019-10-21 stsp goto done;
3105 f69721c3 2019-10-21 stsp }
3106 f69721c3 2019-10-21 stsp free(id_str);
3107 03415a1a 2019-06-02 stsp }
3108 234035bc 2019-06-01 stsp
3109 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3110 234035bc 2019-06-01 stsp worktree->path_prefix);
3111 234035bc 2019-06-01 stsp if (err)
3112 234035bc 2019-06-01 stsp goto done;
3113 234035bc 2019-06-01 stsp
3114 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3115 234035bc 2019-06-01 stsp if (err)
3116 234035bc 2019-06-01 stsp goto done;
3117 234035bc 2019-06-01 stsp
3118 234035bc 2019-06-01 stsp arg.worktree = worktree;
3119 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3120 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3121 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3122 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3123 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3124 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3125 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3126 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3127 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3128 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3129 af12c6b9 2019-06-04 stsp err = sync_err;
3130 234035bc 2019-06-01 stsp done:
3131 234035bc 2019-06-01 stsp if (tree1)
3132 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3133 234035bc 2019-06-01 stsp if (tree2)
3134 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3135 f69721c3 2019-10-21 stsp free(label_orig);
3136 818c7501 2019-07-11 stsp return err;
3137 818c7501 2019-07-11 stsp }
3138 234035bc 2019-06-01 stsp
3139 818c7501 2019-07-11 stsp const struct got_error *
3140 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3141 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3142 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3143 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3144 818c7501 2019-07-11 stsp {
3145 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3146 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3147 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3148 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3149 818c7501 2019-07-11 stsp
3150 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3151 818c7501 2019-07-11 stsp if (err)
3152 818c7501 2019-07-11 stsp return err;
3153 818c7501 2019-07-11 stsp
3154 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3155 818c7501 2019-07-11 stsp if (err)
3156 818c7501 2019-07-11 stsp goto done;
3157 818c7501 2019-07-11 stsp
3158 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3159 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3160 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3161 818c7501 2019-07-11 stsp &mok_arg);
3162 818c7501 2019-07-11 stsp if (err)
3163 818c7501 2019-07-11 stsp goto done;
3164 818c7501 2019-07-11 stsp
3165 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3166 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3167 818c7501 2019-07-11 stsp done:
3168 818c7501 2019-07-11 stsp if (fileindex)
3169 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3170 818c7501 2019-07-11 stsp free(fileindex_path);
3171 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3172 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3173 234035bc 2019-06-01 stsp err = unlockerr;
3174 234035bc 2019-06-01 stsp return err;
3175 234035bc 2019-06-01 stsp }
3176 234035bc 2019-06-01 stsp
3177 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3178 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3179 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3180 927df6b7 2019-02-10 stsp const char *status_path;
3181 927df6b7 2019-02-10 stsp size_t status_path_len;
3182 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3183 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3184 f8d1f275 2019-02-04 stsp void *status_arg;
3185 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3186 f8d1f275 2019-02-04 stsp void *cancel_arg;
3187 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3188 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3189 f2a9dc41 2019-12-13 tracey int report_unchanged;
3190 3143d852 2020-06-25 stsp int no_ignores;
3191 f8d1f275 2019-02-04 stsp };
3192 88d0e355 2019-08-03 stsp
3193 f8d1f275 2019-02-04 stsp static const struct got_error *
3194 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3195 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3196 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3197 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3198 927df6b7 2019-02-10 stsp {
3199 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3200 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3201 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3202 927df6b7 2019-02-10 stsp struct stat sb;
3203 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3204 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3205 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3206 927df6b7 2019-02-10 stsp
3207 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3208 98eaaa12 2019-08-03 stsp if (err)
3209 98eaaa12 2019-08-03 stsp return err;
3210 98eaaa12 2019-08-03 stsp
3211 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3212 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3213 98eaaa12 2019-08-03 stsp return NULL;
3214 98eaaa12 2019-08-03 stsp
3215 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3216 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3217 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3218 98eaaa12 2019-08-03 stsp }
3219 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3220 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3221 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3222 927df6b7 2019-02-10 stsp }
3223 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3224 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3225 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3226 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3227 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3228 98eaaa12 2019-08-03 stsp }
3229 98eaaa12 2019-08-03 stsp
3230 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3231 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3232 927df6b7 2019-02-10 stsp }
3233 927df6b7 2019-02-10 stsp
3234 927df6b7 2019-02-10 stsp static const struct got_error *
3235 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3236 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3237 f8d1f275 2019-02-04 stsp {
3238 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3239 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3240 f8d1f275 2019-02-04 stsp char *abspath;
3241 f8d1f275 2019-02-04 stsp
3242 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3243 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3244 0584f854 2019-04-06 stsp
3245 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3246 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3247 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3248 927df6b7 2019-02-10 stsp return NULL;
3249 927df6b7 2019-02-10 stsp
3250 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3251 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3252 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3253 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3254 f8d1f275 2019-02-04 stsp } else {
3255 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3256 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3257 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3258 f8d1f275 2019-02-04 stsp }
3259 f8d1f275 2019-02-04 stsp
3260 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3261 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3262 f8d1f275 2019-02-04 stsp free(abspath);
3263 9d31a1d8 2018-03-11 stsp return err;
3264 9d31a1d8 2018-03-11 stsp }
3265 f8d1f275 2019-02-04 stsp
3266 f8d1f275 2019-02-04 stsp static const struct got_error *
3267 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3268 f8d1f275 2019-02-04 stsp {
3269 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3270 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3271 2ec1f75b 2019-03-26 stsp unsigned char status;
3272 927df6b7 2019-02-10 stsp
3273 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3274 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3275 0584f854 2019-04-06 stsp
3276 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3277 927df6b7 2019-02-10 stsp return NULL;
3278 927df6b7 2019-02-10 stsp
3279 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3280 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3281 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3282 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3283 2ec1f75b 2019-03-26 stsp else
3284 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3285 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3286 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3287 6841da00 2019-08-08 stsp }
3288 6841da00 2019-08-08 stsp
3289 6841da00 2019-08-08 stsp void
3290 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3291 6841da00 2019-08-08 stsp {
3292 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3293 6841da00 2019-08-08 stsp
3294 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3295 6841da00 2019-08-08 stsp free((char *)pe->path);
3296 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3297 6841da00 2019-08-08 stsp }
3298 6841da00 2019-08-08 stsp
3299 6841da00 2019-08-08 stsp void
3300 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3301 6841da00 2019-08-08 stsp {
3302 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3303 6841da00 2019-08-08 stsp
3304 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3305 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3306 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3307 6841da00 2019-08-08 stsp free((char *)pe->path);
3308 6841da00 2019-08-08 stsp }
3309 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3310 6841da00 2019-08-08 stsp }
3311 6841da00 2019-08-08 stsp
3312 6841da00 2019-08-08 stsp static const struct got_error *
3313 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3314 6841da00 2019-08-08 stsp {
3315 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3316 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3317 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3318 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3319 6841da00 2019-08-08 stsp size_t linesize = 0;
3320 6841da00 2019-08-08 stsp ssize_t linelen;
3321 6841da00 2019-08-08 stsp
3322 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3323 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3324 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3325 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3326 6841da00 2019-08-08 stsp
3327 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3328 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3329 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3330 bd8de430 2019-10-04 stsp
3331 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3332 bd8de430 2019-10-04 stsp if (line[0] == '#')
3333 bd8de430 2019-10-04 stsp continue;
3334 bd8de430 2019-10-04 stsp
3335 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3336 bd8de430 2019-10-04 stsp if (line[0] == '!')
3337 bd8de430 2019-10-04 stsp continue;
3338 bd8de430 2019-10-04 stsp
3339 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3340 6841da00 2019-08-08 stsp line) == -1) {
3341 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3342 6841da00 2019-08-08 stsp goto done;
3343 6841da00 2019-08-08 stsp }
3344 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3345 6841da00 2019-08-08 stsp if (err)
3346 6841da00 2019-08-08 stsp goto done;
3347 6841da00 2019-08-08 stsp }
3348 6841da00 2019-08-08 stsp if (ferror(f)) {
3349 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3350 6841da00 2019-08-08 stsp goto done;
3351 6841da00 2019-08-08 stsp }
3352 6841da00 2019-08-08 stsp
3353 6841da00 2019-08-08 stsp dirpath = strdup(path);
3354 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3355 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3356 6841da00 2019-08-08 stsp goto done;
3357 6841da00 2019-08-08 stsp }
3358 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3359 6841da00 2019-08-08 stsp done:
3360 6841da00 2019-08-08 stsp free(line);
3361 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3362 6841da00 2019-08-08 stsp free(dirpath);
3363 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3364 6841da00 2019-08-08 stsp }
3365 6841da00 2019-08-08 stsp return err;
3366 6841da00 2019-08-08 stsp }
3367 6841da00 2019-08-08 stsp
3368 6841da00 2019-08-08 stsp int
3369 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3370 6841da00 2019-08-08 stsp {
3371 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3372 bd8de430 2019-10-04 stsp
3373 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3374 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3375 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3376 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3377 bd8de430 2019-10-04 stsp
3378 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3379 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3380 6841da00 2019-08-08 stsp
3381 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3382 bd8de430 2019-10-04 stsp continue;
3383 bd8de430 2019-10-04 stsp pattern += 3;
3384 bd8de430 2019-10-04 stsp p = path;
3385 bd8de430 2019-10-04 stsp while (*p) {
3386 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3387 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3388 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3389 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3390 bd8de430 2019-10-04 stsp p++;
3391 bd8de430 2019-10-04 stsp while (*p == '/')
3392 bd8de430 2019-10-04 stsp p++;
3393 bd8de430 2019-10-04 stsp continue;
3394 bd8de430 2019-10-04 stsp }
3395 bd8de430 2019-10-04 stsp return 1;
3396 bd8de430 2019-10-04 stsp }
3397 bd8de430 2019-10-04 stsp }
3398 bd8de430 2019-10-04 stsp }
3399 bd8de430 2019-10-04 stsp
3400 6841da00 2019-08-08 stsp /*
3401 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3402 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3403 6841da00 2019-08-08 stsp * ignores backwards.
3404 6841da00 2019-08-08 stsp */
3405 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3406 6841da00 2019-08-08 stsp while (pe) {
3407 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3408 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3409 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3410 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3411 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3412 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3413 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3414 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3415 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3416 6841da00 2019-08-08 stsp continue;
3417 6841da00 2019-08-08 stsp return 1;
3418 6841da00 2019-08-08 stsp }
3419 6841da00 2019-08-08 stsp }
3420 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3421 6841da00 2019-08-08 stsp }
3422 6841da00 2019-08-08 stsp
3423 6841da00 2019-08-08 stsp return 0;
3424 6841da00 2019-08-08 stsp }
3425 6841da00 2019-08-08 stsp
3426 6841da00 2019-08-08 stsp static const struct got_error *
3427 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3428 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3429 6841da00 2019-08-08 stsp {
3430 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3431 6841da00 2019-08-08 stsp char *ignorespath;
3432 886cec17 2019-12-15 stsp int fd = -1;
3433 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3434 6841da00 2019-08-08 stsp
3435 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3436 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3437 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3438 6841da00 2019-08-08 stsp
3439 886cec17 2019-12-15 stsp if (dirfd != -1) {
3440 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3441 886cec17 2019-12-15 stsp if (fd == -1) {
3442 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3443 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3444 886cec17 2019-12-15 stsp ignorespath);
3445 886cec17 2019-12-15 stsp } else {
3446 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3447 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3448 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3449 886cec17 2019-12-15 stsp ignorespath);
3450 886cec17 2019-12-15 stsp else {
3451 886cec17 2019-12-15 stsp fd = -1;
3452 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3453 886cec17 2019-12-15 stsp }
3454 886cec17 2019-12-15 stsp }
3455 886cec17 2019-12-15 stsp } else {
3456 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3457 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3458 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3459 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3460 886cec17 2019-12-15 stsp ignorespath);
3461 886cec17 2019-12-15 stsp } else
3462 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3463 886cec17 2019-12-15 stsp }
3464 6841da00 2019-08-08 stsp
3465 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3466 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3467 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3468 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3469 6841da00 2019-08-08 stsp free(ignorespath);
3470 6841da00 2019-08-08 stsp return err;
3471 f8d1f275 2019-02-04 stsp }
3472 f8d1f275 2019-02-04 stsp
3473 f8d1f275 2019-02-04 stsp static const struct got_error *
3474 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3475 f8d1f275 2019-02-04 stsp {
3476 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3477 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3478 f8d1f275 2019-02-04 stsp char *path = NULL;
3479 f8d1f275 2019-02-04 stsp
3480 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3481 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3482 0584f854 2019-04-06 stsp
3483 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3484 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3485 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3486 f8d1f275 2019-02-04 stsp } else {
3487 f8d1f275 2019-02-04 stsp path = de->d_name;
3488 f8d1f275 2019-02-04 stsp }
3489 f8d1f275 2019-02-04 stsp
3490 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3491 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3492 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3493 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3494 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3495 f8d1f275 2019-02-04 stsp if (parent_path[0])
3496 f8d1f275 2019-02-04 stsp free(path);
3497 b72f483a 2019-02-05 stsp return err;
3498 f8d1f275 2019-02-04 stsp }
3499 f8d1f275 2019-02-04 stsp
3500 347d1d3e 2019-07-12 stsp static const struct got_error *
3501 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3502 3143d852 2020-06-25 stsp {
3503 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3504 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3505 3143d852 2020-06-25 stsp
3506 3143d852 2020-06-25 stsp if (a->no_ignores)
3507 3143d852 2020-06-25 stsp return NULL;
3508 3143d852 2020-06-25 stsp
3509 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3510 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3511 3143d852 2020-06-25 stsp if (err)
3512 3143d852 2020-06-25 stsp return err;
3513 3143d852 2020-06-25 stsp
3514 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3515 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3516 3143d852 2020-06-25 stsp
3517 3143d852 2020-06-25 stsp return err;
3518 3143d852 2020-06-25 stsp }
3519 3143d852 2020-06-25 stsp
3520 3143d852 2020-06-25 stsp static const struct got_error *
3521 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3522 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3523 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3524 abb4604f 2019-07-27 stsp {
3525 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3526 abb4604f 2019-07-27 stsp struct stat sb;
3527 abb4604f 2019-07-27 stsp
3528 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3529 abb4604f 2019-07-27 stsp if (ie)
3530 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3531 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3532 abb4604f 2019-07-27 stsp
3533 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3534 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3535 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3536 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3537 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3538 abb4604f 2019-07-27 stsp return NULL;
3539 abb4604f 2019-07-27 stsp }
3540 abb4604f 2019-07-27 stsp
3541 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3542 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3543 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3544 abb4604f 2019-07-27 stsp
3545 abb4604f 2019-07-27 stsp return NULL;
3546 3143d852 2020-06-25 stsp }
3547 3143d852 2020-06-25 stsp
3548 3143d852 2020-06-25 stsp static const struct got_error *
3549 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3550 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3551 3143d852 2020-06-25 stsp {
3552 3143d852 2020-06-25 stsp const struct got_error *err;
3553 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3554 3143d852 2020-06-25 stsp
3555 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3556 3143d852 2020-06-25 stsp ".cvsignore");
3557 3143d852 2020-06-25 stsp if (err)
3558 3143d852 2020-06-25 stsp return err;
3559 3143d852 2020-06-25 stsp
3560 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3561 3143d852 2020-06-25 stsp ".gitignore");
3562 3143d852 2020-06-25 stsp if (err)
3563 3143d852 2020-06-25 stsp return err;
3564 3143d852 2020-06-25 stsp
3565 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3566 3143d852 2020-06-25 stsp if (err) {
3567 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3568 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3569 3143d852 2020-06-25 stsp return err;
3570 3143d852 2020-06-25 stsp }
3571 3143d852 2020-06-25 stsp for (;;) {
3572 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3573 3143d852 2020-06-25 stsp ".cvsignore");
3574 3143d852 2020-06-25 stsp if (err)
3575 3143d852 2020-06-25 stsp break;
3576 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3577 3143d852 2020-06-25 stsp ".gitignore");
3578 3143d852 2020-06-25 stsp if (err)
3579 3143d852 2020-06-25 stsp break;
3580 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3581 3143d852 2020-06-25 stsp if (err) {
3582 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3583 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3584 3143d852 2020-06-25 stsp break;
3585 3143d852 2020-06-25 stsp }
3586 b737c85a 2020-06-26 stsp free(parent_path);
3587 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3588 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3589 3143d852 2020-06-25 stsp }
3590 3143d852 2020-06-25 stsp
3591 b737c85a 2020-06-26 stsp free(parent_path);
3592 b737c85a 2020-06-26 stsp free(next_parent_path);
3593 3143d852 2020-06-25 stsp return err;
3594 abb4604f 2019-07-27 stsp }
3595 abb4604f 2019-07-27 stsp
3596 abb4604f 2019-07-27 stsp static const struct got_error *
3597 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3598 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3599 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3600 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3601 f2a9dc41 2019-12-13 tracey int report_unchanged)
3602 f8d1f275 2019-02-04 stsp {
3603 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3604 6fc93f37 2019-12-13 stsp int fd = -1;
3605 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3606 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3607 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3608 3143d852 2020-06-25 stsp
3609 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3610 f8d1f275 2019-02-04 stsp
3611 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3612 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3613 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3614 8dc303cc 2019-07-27 stsp
3615 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3616 6fc93f37 2019-12-13 stsp if (fd == -1) {
3617 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3618 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3619 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3620 abb4604f 2019-07-27 stsp else
3621 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3622 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3623 f2a9dc41 2019-12-13 tracey report_unchanged);
3624 abb4604f 2019-07-27 stsp } else {
3625 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3626 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3627 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3628 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3629 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3630 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3631 abb4604f 2019-07-27 stsp arg.status_path = path;
3632 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3633 abb4604f 2019-07-27 stsp arg.repo = repo;
3634 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3635 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3636 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3637 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3638 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3639 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3640 022fae89 2019-12-06 tracey if (!no_ignores) {
3641 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3642 3143d852 2020-06-25 stsp worktree->root_path, path);
3643 3143d852 2020-06-25 stsp if (err)
3644 3143d852 2020-06-25 stsp goto done;
3645 022fae89 2019-12-06 tracey }
3646 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3647 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3648 927df6b7 2019-02-10 stsp }
3649 3143d852 2020-06-25 stsp done:
3650 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3651 08578a35 2021-01-22 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3652 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3653 927df6b7 2019-02-10 stsp free(ondisk_path);
3654 347d1d3e 2019-07-12 stsp return err;
3655 347d1d3e 2019-07-12 stsp }
3656 347d1d3e 2019-07-12 stsp
3657 347d1d3e 2019-07-12 stsp const struct got_error *
3658 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3659 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3660 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3661 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3662 347d1d3e 2019-07-12 stsp {
3663 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3664 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3665 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3666 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3667 347d1d3e 2019-07-12 stsp
3668 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3669 347d1d3e 2019-07-12 stsp if (err)
3670 347d1d3e 2019-07-12 stsp return err;
3671 347d1d3e 2019-07-12 stsp
3672 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3673 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3674 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3675 72ea6654 2019-07-27 stsp if (err)
3676 72ea6654 2019-07-27 stsp break;
3677 72ea6654 2019-07-27 stsp }
3678 f8d1f275 2019-02-04 stsp free(fileindex_path);
3679 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3680 f8d1f275 2019-02-04 stsp return err;
3681 f8d1f275 2019-02-04 stsp }
3682 6c7ab921 2019-03-18 stsp
3683 6c7ab921 2019-03-18 stsp const struct got_error *
3684 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3685 6c7ab921 2019-03-18 stsp const char *arg)
3686 6c7ab921 2019-03-18 stsp {
3687 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3688 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3689 6c7ab921 2019-03-18 stsp size_t len;
3690 00bb5ea0 2020-07-23 stsp struct stat sb;
3691 01740607 2020-11-04 stsp char *abspath = NULL;
3692 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3693 6c7ab921 2019-03-18 stsp
3694 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3695 6c7ab921 2019-03-18 stsp
3696 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3697 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3698 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3699 00bb5ea0 2020-07-23 stsp
3700 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3701 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3702 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3703 00bb5ea0 2020-07-23 stsp goto done;
3704 00bb5ea0 2020-07-23 stsp }
3705 727173c3 2020-11-06 stsp sb.st_mode = 0;
3706 00bb5ea0 2020-07-23 stsp }
3707 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3708 00bb5ea0 2020-07-23 stsp /*
3709 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3710 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3711 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3712 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3713 00bb5ea0 2020-07-23 stsp */
3714 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3715 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3716 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3717 00bb5ea0 2020-07-23 stsp goto done;
3718 00bb5ea0 2020-07-23 stsp }
3719 00bb5ea0 2020-07-23 stsp
3720 00bb5ea0 2020-07-23 stsp }
3721 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3722 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3723 00bb5ea0 2020-07-23 stsp if (err)
3724 d0710d08 2019-07-22 stsp goto done;
3725 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3726 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3727 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3728 00bb5ea0 2020-07-23 stsp goto done;
3729 00bb5ea0 2020-07-23 stsp }
3730 00bb5ea0 2020-07-23 stsp } else {
3731 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3732 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3733 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3734 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3735 00bb5ea0 2020-07-23 stsp goto done;
3736 00bb5ea0 2020-07-23 stsp }
3737 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3738 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3739 01740607 2020-11-04 stsp goto done;
3740 01740607 2020-11-04 stsp }
3741 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3742 01740607 2020-11-04 stsp sizeof(canonpath));
3743 01740607 2020-11-04 stsp if (err)
3744 00bb5ea0 2020-07-23 stsp goto done;
3745 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3746 01740607 2020-11-04 stsp if (resolved == NULL) {
3747 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3748 01740607 2020-11-04 stsp goto done;
3749 00bb5ea0 2020-07-23 stsp }
3750 d0710d08 2019-07-22 stsp }
3751 d0710d08 2019-07-22 stsp }
3752 6c7ab921 2019-03-18 stsp
3753 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3754 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3755 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3756 6c7ab921 2019-03-18 stsp goto done;
3757 6c7ab921 2019-03-18 stsp }
3758 6c7ab921 2019-03-18 stsp
3759 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3760 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3761 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3762 f6d88e1a 2019-05-29 stsp if (err)
3763 f6d88e1a 2019-05-29 stsp goto done;
3764 f6d88e1a 2019-05-29 stsp } else {
3765 f6d88e1a 2019-05-29 stsp path = strdup("");
3766 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3767 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3768 f6d88e1a 2019-05-29 stsp goto done;
3769 f6d88e1a 2019-05-29 stsp }
3770 6c7ab921 2019-03-18 stsp }
3771 6c7ab921 2019-03-18 stsp
3772 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3773 6c7ab921 2019-03-18 stsp len = strlen(path);
3774 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3775 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3776 6c7ab921 2019-03-18 stsp len--;
3777 6c7ab921 2019-03-18 stsp }
3778 6c7ab921 2019-03-18 stsp done:
3779 01740607 2020-11-04 stsp free(abspath);
3780 6c7ab921 2019-03-18 stsp free(resolved);
3781 d0710d08 2019-07-22 stsp free(cwd);
3782 6c7ab921 2019-03-18 stsp if (err == NULL)
3783 6c7ab921 2019-03-18 stsp *wt_path = path;
3784 6c7ab921 2019-03-18 stsp else
3785 6c7ab921 2019-03-18 stsp free(path);
3786 d00136be 2019-03-26 stsp return err;
3787 d00136be 2019-03-26 stsp }
3788 d00136be 2019-03-26 stsp
3789 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3790 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3791 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3792 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3793 4e68cba3 2019-11-23 stsp void *progress_arg;
3794 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3795 4e68cba3 2019-11-23 stsp };
3796 4e68cba3 2019-11-23 stsp
3797 4e68cba3 2019-11-23 stsp static const struct got_error *
3798 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3799 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3800 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3801 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3802 07f5b47a 2019-06-02 stsp {
3803 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3804 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3805 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3806 6d022e97 2019-08-04 stsp struct stat sb;
3807 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3808 07f5b47a 2019-06-02 stsp
3809 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3810 dbb83fbd 2019-12-12 stsp relpath) == -1)
3811 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3812 dbb83fbd 2019-12-12 stsp
3813 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3814 6d022e97 2019-08-04 stsp if (ie) {
3815 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3816 12463d8b 2019-12-13 stsp de_name, a->repo);
3817 6d022e97 2019-08-04 stsp if (err)
3818 dbb83fbd 2019-12-12 stsp goto done;
3819 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3820 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3821 dbb83fbd 2019-12-12 stsp goto done;
3822 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3823 dbb83fbd 2019-12-12 stsp if (err)
3824 dbb83fbd 2019-12-12 stsp goto done;
3825 6d022e97 2019-08-04 stsp }
3826 07f5b47a 2019-06-02 stsp
3827 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3828 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3829 dbb83fbd 2019-12-12 stsp goto done;
3830 4e68cba3 2019-11-23 stsp }
3831 07f5b47a 2019-06-02 stsp
3832 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3833 4e68cba3 2019-11-23 stsp if (err)
3834 4e68cba3 2019-11-23 stsp goto done;
3835 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3836 437adc9d 2020-12-10 yzhong relpath, NULL, NULL, 1);
3837 3969253a 2020-03-07 stsp if (err) {
3838 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3839 3969253a 2020-03-07 stsp goto done;
3840 3969253a 2020-03-07 stsp }
3841 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3842 07f5b47a 2019-06-02 stsp if (err) {
3843 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3844 4e68cba3 2019-11-23 stsp goto done;
3845 07f5b47a 2019-06-02 stsp }
3846 4e68cba3 2019-11-23 stsp done:
3847 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3848 dbb83fbd 2019-12-12 stsp if (err)
3849 dbb83fbd 2019-12-12 stsp return err;
3850 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3851 dbb83fbd 2019-12-12 stsp return NULL;
3852 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3853 07f5b47a 2019-06-02 stsp }
3854 07f5b47a 2019-06-02 stsp
3855 d00136be 2019-03-26 stsp const struct got_error *
3856 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3857 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3858 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3859 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3860 d00136be 2019-03-26 stsp {
3861 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3862 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3863 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3864 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3865 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3866 d00136be 2019-03-26 stsp
3867 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3868 d00136be 2019-03-26 stsp if (err)
3869 d00136be 2019-03-26 stsp return err;
3870 d00136be 2019-03-26 stsp
3871 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3872 d00136be 2019-03-26 stsp if (err)
3873 d00136be 2019-03-26 stsp goto done;
3874 d00136be 2019-03-26 stsp
3875 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3876 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3877 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3878 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3879 4e68cba3 2019-11-23 stsp saa.repo = repo;
3880 4e68cba3 2019-11-23 stsp
3881 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3882 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3883 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3884 1dd54920 2019-05-11 stsp if (err)
3885 af12c6b9 2019-06-04 stsp break;
3886 1dd54920 2019-05-11 stsp }
3887 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3888 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3889 af12c6b9 2019-06-04 stsp err = sync_err;
3890 d00136be 2019-03-26 stsp done:
3891 fb399478 2019-07-12 stsp free(fileindex_path);
3892 d00136be 2019-03-26 stsp if (fileindex)
3893 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3894 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3895 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3896 d00136be 2019-03-26 stsp err = unlockerr;
3897 6c7ab921 2019-03-18 stsp return err;
3898 6c7ab921 2019-03-18 stsp }
3899 17ed4618 2019-06-02 stsp
3900 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3901 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3902 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3903 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3904 f2a9dc41 2019-12-13 tracey void *progress_arg;
3905 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3906 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3907 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3908 766841c2 2020-08-13 stsp const char *status_codes;
3909 f2a9dc41 2019-12-13 tracey };
3910 f2a9dc41 2019-12-13 tracey
3911 f2a9dc41 2019-12-13 tracey static const struct got_error *
3912 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3913 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3914 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3915 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3916 17ed4618 2019-06-02 stsp {
3917 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3918 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3919 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3920 17ed4618 2019-06-02 stsp struct stat sb;
3921 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3922 17ed4618 2019-06-02 stsp
3923 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3924 17ed4618 2019-06-02 stsp if (ie == NULL)
3925 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3926 2ec1f75b 2019-03-26 stsp
3927 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3928 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3929 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3930 9acbc4fa 2019-08-03 stsp return NULL;
3931 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3932 9acbc4fa 2019-08-03 stsp }
3933 9acbc4fa 2019-08-03 stsp
3934 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3935 f2a9dc41 2019-12-13 tracey relpath) == -1)
3936 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3937 f2a9dc41 2019-12-13 tracey
3938 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3939 12463d8b 2019-12-13 stsp a->repo);
3940 17ed4618 2019-06-02 stsp if (err)
3941 f2a9dc41 2019-12-13 tracey goto done;
3942 17ed4618 2019-06-02 stsp
3943 766841c2 2020-08-13 stsp if (a->status_codes) {
3944 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3945 766841c2 2020-08-13 stsp int i;
3946 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3947 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3948 766841c2 2020-08-13 stsp break;
3949 766841c2 2020-08-13 stsp }
3950 766841c2 2020-08-13 stsp if (i == ncodes) {
3951 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3952 766841c2 2020-08-13 stsp free(ondisk_path);
3953 766841c2 2020-08-13 stsp return NULL;
3954 766841c2 2020-08-13 stsp }
3955 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3956 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3957 766841c2 2020-08-13 stsp static char msg[64];
3958 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3959 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3960 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3961 766841c2 2020-08-13 stsp goto done;
3962 766841c2 2020-08-13 stsp }
3963 766841c2 2020-08-13 stsp }
3964 766841c2 2020-08-13 stsp
3965 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3966 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3967 f2a9dc41 2019-12-13 tracey goto done;
3968 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3969 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3970 f2a9dc41 2019-12-13 tracey goto done;
3971 f2a9dc41 2019-12-13 tracey }
3972 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3973 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3974 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3975 f2a9dc41 2019-12-13 tracey goto done;
3976 f2a9dc41 2019-12-13 tracey }
3977 17ed4618 2019-06-02 stsp }
3978 17ed4618 2019-06-02 stsp
3979 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3980 20a2ad1c 2020-10-20 stsp size_t root_len;
3981 20a2ad1c 2020-10-20 stsp
3982 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3983 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3984 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3985 12463d8b 2019-12-13 stsp ondisk_path);
3986 12463d8b 2019-12-13 stsp goto done;
3987 12463d8b 2019-12-13 stsp }
3988 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3989 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3990 12463d8b 2019-12-13 stsp goto done;
3991 15341bfd 2020-03-05 tracey }
3992 15341bfd 2020-03-05 tracey
3993 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
3994 20a2ad1c 2020-10-20 stsp do {
3995 20a2ad1c 2020-10-20 stsp char *parent;
3996 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
3997 20a2ad1c 2020-10-20 stsp if (err)
3998 2513f20a 2020-10-20 stsp goto done;
3999 20a2ad1c 2020-10-20 stsp free(ondisk_path);
4000 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
4001 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
4002 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
4003 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
4004 20a2ad1c 2020-10-20 stsp ondisk_path);
4005 15341bfd 2020-03-05 tracey break;
4006 15341bfd 2020-03-05 tracey }
4007 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4008 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
4009 f2a9dc41 2019-12-13 tracey }
4010 17ed4618 2019-06-02 stsp
4011 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
4012 f2a9dc41 2019-12-13 tracey done:
4013 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4014 f2a9dc41 2019-12-13 tracey if (err)
4015 f2a9dc41 2019-12-13 tracey return err;
4016 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
4017 f2a9dc41 2019-12-13 tracey return NULL;
4018 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4019 f2a9dc41 2019-12-13 tracey staged_status, relpath);
4020 17ed4618 2019-06-02 stsp }
4021 17ed4618 2019-06-02 stsp
4022 2ec1f75b 2019-03-26 stsp const struct got_error *
4023 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
4024 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
4025 766841c2 2020-08-13 stsp const char *status_codes,
4026 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
4027 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
4028 2ec1f75b 2019-03-26 stsp {
4029 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
4030 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
4031 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
4032 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4033 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
4034 2ec1f75b 2019-03-26 stsp
4035 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
4036 2ec1f75b 2019-03-26 stsp if (err)
4037 2ec1f75b 2019-03-26 stsp return err;
4038 2ec1f75b 2019-03-26 stsp
4039 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4040 2ec1f75b 2019-03-26 stsp if (err)
4041 2ec1f75b 2019-03-26 stsp goto done;
4042 f2a9dc41 2019-12-13 tracey
4043 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
4044 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
4045 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
4046 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
4047 f2a9dc41 2019-12-13 tracey sda.repo = repo;
4048 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
4049 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
4050 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
4051 2ec1f75b 2019-03-26 stsp
4052 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
4053 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
4054 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4055 17ed4618 2019-06-02 stsp if (err)
4056 af12c6b9 2019-06-04 stsp break;
4057 2ec1f75b 2019-03-26 stsp }
4058 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4059 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4060 af12c6b9 2019-06-04 stsp err = sync_err;
4061 2ec1f75b 2019-03-26 stsp done:
4062 fb399478 2019-07-12 stsp free(fileindex_path);
4063 2ec1f75b 2019-03-26 stsp if (fileindex)
4064 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4065 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4066 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4067 2ec1f75b 2019-03-26 stsp err = unlockerr;
4068 33aa809d 2019-08-08 stsp return err;
4069 33aa809d 2019-08-08 stsp }
4070 33aa809d 2019-08-08 stsp
4071 33aa809d 2019-08-08 stsp static const struct got_error *
4072 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4073 33aa809d 2019-08-08 stsp {
4074 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4075 33aa809d 2019-08-08 stsp char *line = NULL;
4076 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4077 33aa809d 2019-08-08 stsp ssize_t linelen;
4078 33aa809d 2019-08-08 stsp
4079 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4080 33aa809d 2019-08-08 stsp if (linelen == -1) {
4081 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4082 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4083 33aa809d 2019-08-08 stsp goto done;
4084 33aa809d 2019-08-08 stsp }
4085 33aa809d 2019-08-08 stsp return NULL;
4086 33aa809d 2019-08-08 stsp }
4087 33aa809d 2019-08-08 stsp if (outfile) {
4088 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4089 33aa809d 2019-08-08 stsp if (n != linelen) {
4090 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4091 33aa809d 2019-08-08 stsp goto done;
4092 33aa809d 2019-08-08 stsp }
4093 33aa809d 2019-08-08 stsp }
4094 33aa809d 2019-08-08 stsp if (rejectfile) {
4095 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4096 33aa809d 2019-08-08 stsp if (n != linelen)
4097 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4098 33aa809d 2019-08-08 stsp }
4099 33aa809d 2019-08-08 stsp done:
4100 33aa809d 2019-08-08 stsp free(line);
4101 2ec1f75b 2019-03-26 stsp return err;
4102 2ec1f75b 2019-03-26 stsp }
4103 1f1abb7e 2019-08-08 stsp
4104 33aa809d 2019-08-08 stsp static const struct got_error *
4105 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4106 33aa809d 2019-08-08 stsp {
4107 33aa809d 2019-08-08 stsp char *line = NULL;
4108 33aa809d 2019-08-08 stsp size_t linesize = 0;
4109 33aa809d 2019-08-08 stsp ssize_t linelen;
4110 33aa809d 2019-08-08 stsp
4111 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4112 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4113 500467ff 2019-09-25 hiltjo if (ferror(f))
4114 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4115 500467ff 2019-09-25 hiltjo return NULL;
4116 500467ff 2019-09-25 hiltjo }
4117 33aa809d 2019-08-08 stsp free(line);
4118 33aa809d 2019-08-08 stsp return NULL;
4119 33aa809d 2019-08-08 stsp }
4120 33aa809d 2019-08-08 stsp
4121 33aa809d 2019-08-08 stsp static const struct got_error *
4122 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4123 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4124 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4125 33aa809d 2019-08-08 stsp {
4126 33aa809d 2019-08-08 stsp const struct got_error *err;
4127 33aa809d 2019-08-08 stsp
4128 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4129 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4130 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4131 33aa809d 2019-08-08 stsp if (err)
4132 33aa809d 2019-08-08 stsp return err;
4133 33aa809d 2019-08-08 stsp (*line_cur1)++;
4134 33aa809d 2019-08-08 stsp }
4135 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4136 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4137 33aa809d 2019-08-08 stsp if (rejectfile)
4138 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4139 33aa809d 2019-08-08 stsp else
4140 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4141 33aa809d 2019-08-08 stsp if (err)
4142 33aa809d 2019-08-08 stsp return err;
4143 33aa809d 2019-08-08 stsp (*line_cur2)++;
4144 33aa809d 2019-08-08 stsp }
4145 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4146 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4147 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4148 33aa809d 2019-08-08 stsp if (err)
4149 33aa809d 2019-08-08 stsp return err;
4150 33aa809d 2019-08-08 stsp (*line_cur2)++;
4151 33aa809d 2019-08-08 stsp }
4152 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4153 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4154 33aa809d 2019-08-08 stsp if (rejectfile)
4155 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4156 33aa809d 2019-08-08 stsp else
4157 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4158 33aa809d 2019-08-08 stsp if (err)
4159 33aa809d 2019-08-08 stsp return err;
4160 33aa809d 2019-08-08 stsp (*line_cur1)++;
4161 33aa809d 2019-08-08 stsp }
4162 f1e81a05 2019-08-10 stsp
4163 f1e81a05 2019-08-10 stsp return NULL;
4164 f1e81a05 2019-08-10 stsp }
4165 f1e81a05 2019-08-10 stsp
4166 f1e81a05 2019-08-10 stsp static const struct got_error *
4167 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4168 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4169 f1e81a05 2019-08-10 stsp {
4170 f1e81a05 2019-08-10 stsp const struct got_error *err;
4171 f1e81a05 2019-08-10 stsp
4172 f1e81a05 2019-08-10 stsp if (outfile) {
4173 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4174 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4175 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4176 f1e81a05 2019-08-10 stsp if (err)
4177 f1e81a05 2019-08-10 stsp return err;
4178 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4179 f1e81a05 2019-08-10 stsp }
4180 f1e81a05 2019-08-10 stsp }
4181 f1e81a05 2019-08-10 stsp if (rejectfile) {
4182 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4183 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4184 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4185 f1e81a05 2019-08-10 stsp if (err)
4186 f1e81a05 2019-08-10 stsp return err;
4187 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4188 f1e81a05 2019-08-10 stsp }
4189 33aa809d 2019-08-08 stsp }
4190 33aa809d 2019-08-08 stsp
4191 33aa809d 2019-08-08 stsp return NULL;
4192 33aa809d 2019-08-08 stsp }
4193 33aa809d 2019-08-08 stsp
4194 33aa809d 2019-08-08 stsp static const struct got_error *
4195 fe621944 2020-11-10 stsp apply_or_reject_change(int *choice, int *nchunks_used,
4196 fe621944 2020-11-10 stsp struct diff_result *diff_result, int n,
4197 fe621944 2020-11-10 stsp const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4198 fe621944 2020-11-10 stsp FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4199 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4200 33aa809d 2019-08-08 stsp {
4201 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4202 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4203 fe621944 2020-11-10 stsp int start_old, end_old, start_new, end_new;
4204 33aa809d 2019-08-08 stsp FILE *hunkfile;
4205 fe621944 2020-11-10 stsp struct diff_output_unidiff_state *diff_state;
4206 fe621944 2020-11-10 stsp struct diff_input_info diff_info;
4207 fe621944 2020-11-10 stsp int rc;
4208 33aa809d 2019-08-08 stsp
4209 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4210 33aa809d 2019-08-08 stsp
4211 fe621944 2020-11-10 stsp /* Get changed line numbers without context lines for copy_change(). */
4212 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4213 fe621944 2020-11-10 stsp start_old = cc.left.start;
4214 fe621944 2020-11-10 stsp end_old = cc.left.end;
4215 fe621944 2020-11-10 stsp start_new = cc.right.start;
4216 fe621944 2020-11-10 stsp end_new = cc.right.end;
4217 33aa809d 2019-08-08 stsp
4218 fe621944 2020-11-10 stsp /* Get the same change with context lines for display. */
4219 fe621944 2020-11-10 stsp memset(&cc, 0, sizeof(cc));
4220 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4221 33aa809d 2019-08-08 stsp
4222 fe621944 2020-11-10 stsp memset(&diff_info, 0, sizeof(diff_info));
4223 fe621944 2020-11-10 stsp diff_info.left_path = relpath;
4224 fe621944 2020-11-10 stsp diff_info.right_path = relpath;
4225 33aa809d 2019-08-08 stsp
4226 fe621944 2020-11-10 stsp diff_state = diff_output_unidiff_state_alloc();
4227 fe621944 2020-11-10 stsp if (diff_state == NULL)
4228 fe621944 2020-11-10 stsp return got_error_set_errno(ENOMEM,
4229 fe621944 2020-11-10 stsp "diff_output_unidiff_state_alloc");
4230 fe621944 2020-11-10 stsp
4231 fe621944 2020-11-10 stsp hunkfile = got_opentemp();
4232 fe621944 2020-11-10 stsp if (hunkfile == NULL) {
4233 fe621944 2020-11-10 stsp err = got_error_from_errno("got_opentemp");
4234 33aa809d 2019-08-08 stsp goto done;
4235 33aa809d 2019-08-08 stsp }
4236 fe621944 2020-11-10 stsp
4237 fe621944 2020-11-10 stsp rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4238 fe621944 2020-11-10 stsp diff_result, &cc);
4239 fe621944 2020-11-10 stsp if (rc != DIFF_RC_OK) {
4240 fe621944 2020-11-10 stsp err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4241 33aa809d 2019-08-08 stsp goto done;
4242 33aa809d 2019-08-08 stsp }
4243 fe621944 2020-11-10 stsp
4244 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4245 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4246 33aa809d 2019-08-08 stsp goto done;
4247 33aa809d 2019-08-08 stsp }
4248 33aa809d 2019-08-08 stsp
4249 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4250 fe621944 2020-11-10 stsp hunkfile, changeno, nchanges);
4251 33aa809d 2019-08-08 stsp if (err)
4252 33aa809d 2019-08-08 stsp goto done;
4253 33aa809d 2019-08-08 stsp
4254 33aa809d 2019-08-08 stsp switch (*choice) {
4255 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4256 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4257 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4258 33aa809d 2019-08-08 stsp break;
4259 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4260 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4261 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4262 33aa809d 2019-08-08 stsp break;
4263 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4264 33aa809d 2019-08-08 stsp break;
4265 33aa809d 2019-08-08 stsp default:
4266 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4267 33aa809d 2019-08-08 stsp break;
4268 33aa809d 2019-08-08 stsp }
4269 33aa809d 2019-08-08 stsp done:
4270 fe621944 2020-11-10 stsp diff_output_unidiff_state_free(diff_state);
4271 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4272 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4273 33aa809d 2019-08-08 stsp return err;
4274 33aa809d 2019-08-08 stsp }
4275 33aa809d 2019-08-08 stsp
4276 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4277 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4278 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4279 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4280 1f1abb7e 2019-08-08 stsp void *progress_arg;
4281 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4282 33aa809d 2019-08-08 stsp void *patch_arg;
4283 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4284 1f1abb7e 2019-08-08 stsp };
4285 a129376b 2019-03-28 stsp
4286 e20a8b6f 2019-06-04 stsp static const struct got_error *
4287 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4288 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4289 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4290 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4291 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4292 33aa809d 2019-08-08 stsp {
4293 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
4294 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4295 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4296 1ebedb77 2019-10-19 stsp int fd2 = -1;
4297 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4298 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4299 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4300 fe621944 2020-11-10 stsp struct stat sb2;
4301 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
4302 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4303 fe621944 2020-11-10 stsp int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4304 33aa809d 2019-08-08 stsp
4305 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4306 33aa809d 2019-08-08 stsp
4307 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4308 33aa809d 2019-08-08 stsp if (err)
4309 33aa809d 2019-08-08 stsp return err;
4310 33aa809d 2019-08-08 stsp
4311 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4312 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4313 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4314 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4315 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4316 fa3cef63 2020-07-23 stsp goto done;
4317 fa3cef63 2020-07-23 stsp }
4318 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4319 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4320 fa3cef63 2020-07-23 stsp if (link_len == -1)
4321 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4322 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4323 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4324 12463d8b 2019-12-13 stsp }
4325 12463d8b 2019-12-13 stsp } else {
4326 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4327 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4328 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4329 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4330 fa3cef63 2020-07-23 stsp goto done;
4331 fa3cef63 2020-07-23 stsp }
4332 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4333 fa3cef63 2020-07-23 stsp sizeof(link_target));
4334 fa3cef63 2020-07-23 stsp if (link_len == -1)
4335 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4336 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4337 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4338 12463d8b 2019-12-13 stsp }
4339 1ebedb77 2019-10-19 stsp }
4340 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4341 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4342 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4343 fa3cef63 2020-07-23 stsp goto done;
4344 fa3cef63 2020-07-23 stsp }
4345 1ebedb77 2019-10-19 stsp
4346 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4347 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4348 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4349 fa3cef63 2020-07-23 stsp goto done;
4350 fa3cef63 2020-07-23 stsp }
4351 fa3cef63 2020-07-23 stsp fd2 = -1;
4352 fa3cef63 2020-07-23 stsp } else {
4353 fa3cef63 2020-07-23 stsp size_t n;
4354 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4355 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4356 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4357 fa3cef63 2020-07-23 stsp goto done;
4358 fa3cef63 2020-07-23 stsp }
4359 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4360 fa3cef63 2020-07-23 stsp if (n != link_len) {
4361 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4362 fa3cef63 2020-07-23 stsp goto done;
4363 fa3cef63 2020-07-23 stsp }
4364 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4365 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4366 fa3cef63 2020-07-23 stsp goto done;
4367 fa3cef63 2020-07-23 stsp }
4368 fa3cef63 2020-07-23 stsp rewind(f2);
4369 33aa809d 2019-08-08 stsp }
4370 33aa809d 2019-08-08 stsp
4371 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4372 33aa809d 2019-08-08 stsp if (err)
4373 33aa809d 2019-08-08 stsp goto done;
4374 33aa809d 2019-08-08 stsp
4375 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4376 33aa809d 2019-08-08 stsp if (err)
4377 33aa809d 2019-08-08 stsp goto done;
4378 33aa809d 2019-08-08 stsp
4379 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4380 33aa809d 2019-08-08 stsp if (err)
4381 33aa809d 2019-08-08 stsp goto done;
4382 33aa809d 2019-08-08 stsp
4383 64453f7e 2020-11-21 stsp err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4384 fe621944 2020-11-10 stsp NULL);
4385 33aa809d 2019-08-08 stsp if (err)
4386 33aa809d 2019-08-08 stsp goto done;
4387 33aa809d 2019-08-08 stsp
4388 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4389 33aa809d 2019-08-08 stsp if (err)
4390 33aa809d 2019-08-08 stsp goto done;
4391 33aa809d 2019-08-08 stsp
4392 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4393 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4394 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4395 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4396 fe621944 2020-11-10 stsp
4397 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
4398 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4399 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
4400 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
4401 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
4402 fe621944 2020-11-10 stsp nchanges++;
4403 fe621944 2020-11-10 stsp }
4404 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4405 33aa809d 2019-08-08 stsp int choice;
4406 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
4407 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
4408 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
4409 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4410 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4411 fe621944 2020-11-10 stsp ++i, nchanges, patch_cb, patch_arg);
4412 33aa809d 2019-08-08 stsp if (err)
4413 33aa809d 2019-08-08 stsp goto done;
4414 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4415 33aa809d 2019-08-08 stsp have_content = 1;
4416 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4417 33aa809d 2019-08-08 stsp break;
4418 33aa809d 2019-08-08 stsp }
4419 1ebedb77 2019-10-19 stsp if (have_content) {
4420 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4421 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4422 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4423 1ebedb77 2019-10-19 stsp if (err)
4424 1ebedb77 2019-10-19 stsp goto done;
4425 1ebedb77 2019-10-19 stsp
4426 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4427 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4428 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4429 fa3cef63 2020-07-23 stsp goto done;
4430 fa3cef63 2020-07-23 stsp }
4431 1ebedb77 2019-10-19 stsp }
4432 1ebedb77 2019-10-19 stsp }
4433 33aa809d 2019-08-08 stsp done:
4434 33aa809d 2019-08-08 stsp free(id_str);
4435 33aa809d 2019-08-08 stsp if (blob)
4436 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4437 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
4438 fe621944 2020-11-10 stsp if (err == NULL)
4439 fe621944 2020-11-10 stsp err = free_err;
4440 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4441 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4442 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4443 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4444 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4445 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4446 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4447 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4448 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4449 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4450 33aa809d 2019-08-08 stsp if (err || !have_content) {
4451 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4452 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4453 33aa809d 2019-08-08 stsp free(*path_outfile);
4454 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4455 33aa809d 2019-08-08 stsp }
4456 33aa809d 2019-08-08 stsp free(path1);
4457 33aa809d 2019-08-08 stsp return err;
4458 33aa809d 2019-08-08 stsp }
4459 33aa809d 2019-08-08 stsp
4460 33aa809d 2019-08-08 stsp static const struct got_error *
4461 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4462 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4463 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4464 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4465 a129376b 2019-03-28 stsp {
4466 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4467 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4468 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4469 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4470 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4471 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4472 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4473 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4474 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4475 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4476 a129376b 2019-03-28 stsp
4477 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4478 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4479 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4480 d3bcc3d1 2019-08-08 stsp return NULL;
4481 3d69ad8d 2019-08-17 semarie
4482 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4483 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4484 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4485 d3bcc3d1 2019-08-08 stsp
4486 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4487 65084dad 2019-08-08 stsp if (ie == NULL)
4488 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4489 a129376b 2019-03-28 stsp
4490 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4491 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4492 a129376b 2019-03-28 stsp if (err) {
4493 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4494 a129376b 2019-03-28 stsp goto done;
4495 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4496 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4497 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4498 e20a8b6f 2019-06-04 stsp goto done;
4499 e20a8b6f 2019-06-04 stsp }
4500 a129376b 2019-03-28 stsp }
4501 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4502 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4503 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4504 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4505 a129376b 2019-03-28 stsp goto done;
4506 a129376b 2019-03-28 stsp }
4507 a129376b 2019-03-28 stsp } else {
4508 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4509 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4510 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4511 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4512 a129376b 2019-03-28 stsp goto done;
4513 a129376b 2019-03-28 stsp }
4514 a129376b 2019-03-28 stsp } else {
4515 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4516 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4517 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4518 a129376b 2019-03-28 stsp goto done;
4519 a129376b 2019-03-28 stsp }
4520 a129376b 2019-03-28 stsp }
4521 a129376b 2019-03-28 stsp }
4522 a129376b 2019-03-28 stsp
4523 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4524 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4525 a9fa2909 2019-07-27 stsp if (err) {
4526 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4527 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4528 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4529 a9fa2909 2019-07-27 stsp goto done;
4530 a9fa2909 2019-07-27 stsp } else {
4531 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4532 a9fa2909 2019-07-27 stsp if (err)
4533 a9fa2909 2019-07-27 stsp goto done;
4534 a9fa2909 2019-07-27 stsp
4535 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4536 1233e6b6 2020-10-19 stsp if (err)
4537 a9fa2909 2019-07-27 stsp goto done;
4538 a9fa2909 2019-07-27 stsp
4539 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4540 1233e6b6 2020-10-19 stsp free(te_name);
4541 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4542 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4543 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4544 a9fa2909 2019-07-27 stsp goto done;
4545 a9fa2909 2019-07-27 stsp }
4546 a129376b 2019-03-28 stsp }
4547 a129376b 2019-03-28 stsp
4548 a129376b 2019-03-28 stsp switch (status) {
4549 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4550 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4551 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4552 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4553 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4554 33aa809d 2019-08-08 stsp if (err)
4555 33aa809d 2019-08-08 stsp goto done;
4556 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4557 33aa809d 2019-08-08 stsp break;
4558 33aa809d 2019-08-08 stsp }
4559 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4560 1f1abb7e 2019-08-08 stsp ie->path);
4561 1ee397ad 2019-07-12 stsp if (err)
4562 1ee397ad 2019-07-12 stsp goto done;
4563 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4564 a129376b 2019-03-28 stsp break;
4565 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4566 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4567 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4568 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4569 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4570 33aa809d 2019-08-08 stsp if (err)
4571 33aa809d 2019-08-08 stsp goto done;
4572 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4573 33aa809d 2019-08-08 stsp break;
4574 33aa809d 2019-08-08 stsp }
4575 33aa809d 2019-08-08 stsp /* fall through */
4576 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4577 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4578 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4579 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4580 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4581 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4582 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4583 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4584 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4585 24278f30 2019-08-03 stsp } else
4586 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4587 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4588 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4589 a129376b 2019-03-28 stsp if (err)
4590 65084dad 2019-08-08 stsp goto done;
4591 65084dad 2019-08-08 stsp
4592 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4593 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4594 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4595 a129376b 2019-03-28 stsp goto done;
4596 65084dad 2019-08-08 stsp }
4597 65084dad 2019-08-08 stsp
4598 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4599 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4600 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4601 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4602 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4603 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4604 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4605 33aa809d 2019-08-08 stsp break;
4606 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4607 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4608 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4609 369fd7e5 2020-07-23 stsp path_content);
4610 369fd7e5 2020-07-23 stsp break;
4611 369fd7e5 2020-07-23 stsp }
4612 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4613 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4614 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4615 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4616 369fd7e5 2020-07-23 stsp } else {
4617 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4618 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4619 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4620 369fd7e5 2020-07-23 stsp goto done;
4621 369fd7e5 2020-07-23 stsp }
4622 33aa809d 2019-08-08 stsp }
4623 33aa809d 2019-08-08 stsp } else {
4624 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4625 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4626 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4627 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4628 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4629 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4630 2e1fa222 2020-07-23 stsp } else {
4631 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4632 2e1fa222 2020-07-23 stsp ie->path,
4633 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4634 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4635 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4636 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4637 2e1fa222 2020-07-23 stsp }
4638 a129376b 2019-03-28 stsp if (err)
4639 a129376b 2019-03-28 stsp goto done;
4640 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4641 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4642 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4643 437adc9d 2020-12-10 yzhong a->worktree->root_fd, relpath,
4644 437adc9d 2020-12-10 yzhong blob->id.sha1,
4645 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4646 2e1fa222 2020-07-23 stsp if (err)
4647 2e1fa222 2020-07-23 stsp goto done;
4648 2e1fa222 2020-07-23 stsp }
4649 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4650 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4651 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4652 33aa809d 2019-08-08 stsp }
4653 a129376b 2019-03-28 stsp }
4654 a129376b 2019-03-28 stsp break;
4655 e20a8b6f 2019-06-04 stsp }
4656 a129376b 2019-03-28 stsp default:
4657 1f1abb7e 2019-08-08 stsp break;
4658 a129376b 2019-03-28 stsp }
4659 a129376b 2019-03-28 stsp done:
4660 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4661 33aa809d 2019-08-08 stsp free(path_content);
4662 e20a8b6f 2019-06-04 stsp free(parent_path);
4663 a129376b 2019-03-28 stsp free(tree_path);
4664 a129376b 2019-03-28 stsp if (blob)
4665 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4666 a129376b 2019-03-28 stsp if (tree)
4667 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4668 a129376b 2019-03-28 stsp free(tree_id);
4669 e20a8b6f 2019-06-04 stsp return err;
4670 e20a8b6f 2019-06-04 stsp }
4671 e20a8b6f 2019-06-04 stsp
4672 e20a8b6f 2019-06-04 stsp const struct got_error *
4673 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4674 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4675 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4676 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4677 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4678 e20a8b6f 2019-06-04 stsp {
4679 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4680 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4681 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4682 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4683 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4684 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4685 e20a8b6f 2019-06-04 stsp
4686 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4687 e20a8b6f 2019-06-04 stsp if (err)
4688 e20a8b6f 2019-06-04 stsp return err;
4689 e20a8b6f 2019-06-04 stsp
4690 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4691 e20a8b6f 2019-06-04 stsp if (err)
4692 e20a8b6f 2019-06-04 stsp goto done;
4693 e20a8b6f 2019-06-04 stsp
4694 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4695 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4696 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4697 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4698 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4699 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4700 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4701 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4702 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4703 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4704 e20a8b6f 2019-06-04 stsp if (err)
4705 af12c6b9 2019-06-04 stsp break;
4706 e20a8b6f 2019-06-04 stsp }
4707 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4708 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4709 e20a8b6f 2019-06-04 stsp err = sync_err;
4710 af12c6b9 2019-06-04 stsp done:
4711 fb399478 2019-07-12 stsp free(fileindex_path);
4712 a129376b 2019-03-28 stsp if (fileindex)
4713 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4714 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4715 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4716 a129376b 2019-03-28 stsp err = unlockerr;
4717 c4296144 2019-05-09 stsp return err;
4718 c4296144 2019-05-09 stsp }
4719 c4296144 2019-05-09 stsp
4720 cf066bf8 2019-05-09 stsp static void
4721 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4722 cf066bf8 2019-05-09 stsp {
4723 24519714 2019-05-09 stsp free(ct->path);
4724 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4725 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4726 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4727 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4728 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4729 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4730 cf066bf8 2019-05-09 stsp free(ct);
4731 cf066bf8 2019-05-09 stsp }
4732 24519714 2019-05-09 stsp
4733 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4734 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4735 24519714 2019-05-09 stsp struct got_repository *repo;
4736 24519714 2019-05-09 stsp struct got_worktree *worktree;
4737 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4738 5f8a88c6 2019-08-03 stsp int have_staged_files;
4739 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4740 24519714 2019-05-09 stsp };
4741 cf066bf8 2019-05-09 stsp
4742 c4296144 2019-05-09 stsp static const struct got_error *
4743 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4744 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4745 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4746 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4747 c4296144 2019-05-09 stsp {
4748 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4749 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4750 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4751 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4752 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4753 768aea60 2019-05-09 stsp struct stat sb;
4754 c4296144 2019-05-09 stsp
4755 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4756 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4757 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4758 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4759 5f8a88c6 2019-08-03 stsp return NULL;
4760 5f8a88c6 2019-08-03 stsp } else {
4761 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4762 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4763 c4296144 2019-05-09 stsp
4764 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4765 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4766 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4767 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4768 5f8a88c6 2019-08-03 stsp return NULL;
4769 5f8a88c6 2019-08-03 stsp }
4770 0b5cc0d6 2019-05-09 stsp
4771 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4772 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4773 036813ee 2019-05-09 stsp goto done;
4774 036813ee 2019-05-09 stsp }
4775 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4776 036813ee 2019-05-09 stsp parent_path = strdup("");
4777 69960a46 2019-05-09 stsp if (parent_path == NULL)
4778 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4779 69960a46 2019-05-09 stsp } else {
4780 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4781 69960a46 2019-05-09 stsp if (err)
4782 69960a46 2019-05-09 stsp return err;
4783 69960a46 2019-05-09 stsp }
4784 c4296144 2019-05-09 stsp
4785 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4786 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4787 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4788 c4296144 2019-05-09 stsp goto done;
4789 768aea60 2019-05-09 stsp }
4790 768aea60 2019-05-09 stsp
4791 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4792 768aea60 2019-05-09 stsp relpath) == -1) {
4793 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4794 768aea60 2019-05-09 stsp goto done;
4795 768aea60 2019-05-09 stsp }
4796 0aeb8099 2020-07-23 stsp
4797 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4798 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4799 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4800 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4801 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4802 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4803 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4804 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4805 0aeb8099 2020-07-23 stsp break;
4806 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4807 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4808 0aeb8099 2020-07-23 stsp break;
4809 0aeb8099 2020-07-23 stsp default:
4810 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4811 0aeb8099 2020-07-23 stsp goto done;
4812 0aeb8099 2020-07-23 stsp }
4813 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4814 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4815 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4816 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4817 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4818 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4819 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4820 12463d8b 2019-12-13 stsp ct->ondisk_path);
4821 12463d8b 2019-12-13 stsp goto done;
4822 12463d8b 2019-12-13 stsp }
4823 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4824 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4825 768aea60 2019-05-09 stsp goto done;
4826 768aea60 2019-05-09 stsp }
4827 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4828 c4296144 2019-05-09 stsp }
4829 c4296144 2019-05-09 stsp
4830 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4831 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4832 44d03001 2019-05-09 stsp relpath) == -1) {
4833 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4834 44d03001 2019-05-09 stsp goto done;
4835 44d03001 2019-05-09 stsp }
4836 44d03001 2019-05-09 stsp
4837 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4838 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4839 35213c7c 2020-07-23 stsp int is_bad_symlink;
4840 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4841 35213c7c 2020-07-23 stsp ssize_t target_len;
4842 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4843 35213c7c 2020-07-23 stsp sizeof(target_path));
4844 35213c7c 2020-07-23 stsp if (target_len == -1) {
4845 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4846 35213c7c 2020-07-23 stsp ct->ondisk_path);
4847 35213c7c 2020-07-23 stsp goto done;
4848 35213c7c 2020-07-23 stsp }
4849 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4850 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4851 35213c7c 2020-07-23 stsp if (err)
4852 35213c7c 2020-07-23 stsp goto done;
4853 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4854 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4855 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4856 35213c7c 2020-07-23 stsp goto done;
4857 35213c7c 2020-07-23 stsp }
4858 35213c7c 2020-07-23 stsp }
4859 35213c7c 2020-07-23 stsp
4860 35213c7c 2020-07-23 stsp
4861 cf066bf8 2019-05-09 stsp ct->status = status;
4862 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4863 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4864 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4865 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4866 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4867 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4868 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4869 b416585c 2019-05-13 stsp goto done;
4870 b416585c 2019-05-13 stsp }
4871 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4872 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4873 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4874 f0b75401 2019-08-03 stsp goto done;
4875 f0b75401 2019-08-03 stsp }
4876 f0b75401 2019-08-03 stsp }
4877 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4878 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4879 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4880 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4881 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4882 036813ee 2019-05-09 stsp goto done;
4883 036813ee 2019-05-09 stsp }
4884 ca2503ea 2019-05-09 stsp }
4885 24519714 2019-05-09 stsp ct->path = strdup(path);
4886 24519714 2019-05-09 stsp if (ct->path == NULL) {
4887 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4888 24519714 2019-05-09 stsp goto done;
4889 24519714 2019-05-09 stsp }
4890 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4891 c4296144 2019-05-09 stsp done:
4892 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4893 ed175427 2019-05-09 stsp free_commitable(ct);
4894 24519714 2019-05-09 stsp free(parent_path);
4895 036813ee 2019-05-09 stsp free(path);
4896 a129376b 2019-03-28 stsp return err;
4897 a129376b 2019-03-28 stsp }
4898 c4296144 2019-05-09 stsp
4899 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4900 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4901 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4902 036813ee 2019-05-09 stsp struct got_repository *);
4903 ed175427 2019-05-09 stsp
4904 ed175427 2019-05-09 stsp static const struct got_error *
4905 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4906 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4907 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4908 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4909 afa376bf 2019-05-09 stsp struct got_repository *repo)
4910 ed175427 2019-05-09 stsp {
4911 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4912 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4913 036813ee 2019-05-09 stsp char *subpath;
4914 ed175427 2019-05-09 stsp
4915 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4916 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4917 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4918 ed175427 2019-05-09 stsp
4919 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4920 ed175427 2019-05-09 stsp if (err)
4921 ed175427 2019-05-09 stsp return err;
4922 ed175427 2019-05-09 stsp
4923 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4924 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4925 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4926 036813ee 2019-05-09 stsp free(subpath);
4927 036813ee 2019-05-09 stsp return err;
4928 036813ee 2019-05-09 stsp }
4929 ed175427 2019-05-09 stsp
4930 036813ee 2019-05-09 stsp static const struct got_error *
4931 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4932 036813ee 2019-05-09 stsp {
4933 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4934 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4935 ed175427 2019-05-09 stsp
4936 036813ee 2019-05-09 stsp *match = 0;
4937 ed175427 2019-05-09 stsp
4938 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4939 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4940 0f63689d 2019-05-10 stsp return NULL;
4941 036813ee 2019-05-09 stsp }
4942 0b5cc0d6 2019-05-09 stsp
4943 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4944 0f63689d 2019-05-10 stsp if (err)
4945 0f63689d 2019-05-10 stsp return err;
4946 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4947 036813ee 2019-05-09 stsp free(ct_parent_path);
4948 036813ee 2019-05-09 stsp return err;
4949 036813ee 2019-05-09 stsp }
4950 0b5cc0d6 2019-05-09 stsp
4951 768aea60 2019-05-09 stsp static mode_t
4952 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4953 768aea60 2019-05-09 stsp {
4954 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4955 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4956 3d9a4ec4 2020-07-23 stsp
4957 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4958 768aea60 2019-05-09 stsp }
4959 768aea60 2019-05-09 stsp
4960 036813ee 2019-05-09 stsp static const struct got_error *
4961 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4962 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4963 036813ee 2019-05-09 stsp {
4964 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4965 ca2503ea 2019-05-09 stsp
4966 036813ee 2019-05-09 stsp *new_te = NULL;
4967 0b5cc0d6 2019-05-09 stsp
4968 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4969 036813ee 2019-05-09 stsp if (err)
4970 036813ee 2019-05-09 stsp goto done;
4971 0b5cc0d6 2019-05-09 stsp
4972 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4973 036813ee 2019-05-09 stsp
4974 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4975 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4976 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4977 5f8a88c6 2019-08-03 stsp else
4978 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4979 036813ee 2019-05-09 stsp done:
4980 036813ee 2019-05-09 stsp if (err && *new_te) {
4981 56e0773d 2019-11-28 stsp free(*new_te);
4982 036813ee 2019-05-09 stsp *new_te = NULL;
4983 036813ee 2019-05-09 stsp }
4984 036813ee 2019-05-09 stsp return err;
4985 036813ee 2019-05-09 stsp }
4986 036813ee 2019-05-09 stsp
4987 036813ee 2019-05-09 stsp static const struct got_error *
4988 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4989 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4990 036813ee 2019-05-09 stsp {
4991 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4992 102b254e 2020-10-19 stsp char *ct_name = NULL;
4993 036813ee 2019-05-09 stsp
4994 036813ee 2019-05-09 stsp *new_te = NULL;
4995 036813ee 2019-05-09 stsp
4996 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4997 036813ee 2019-05-09 stsp if (*new_te == NULL)
4998 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4999 036813ee 2019-05-09 stsp
5000 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
5001 102b254e 2020-10-19 stsp if (err)
5002 036813ee 2019-05-09 stsp goto done;
5003 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5004 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
5005 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5006 036813ee 2019-05-09 stsp goto done;
5007 036813ee 2019-05-09 stsp }
5008 036813ee 2019-05-09 stsp
5009 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
5010 036813ee 2019-05-09 stsp
5011 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
5012 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
5013 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
5014 5f8a88c6 2019-08-03 stsp else
5015 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5016 036813ee 2019-05-09 stsp done:
5017 102b254e 2020-10-19 stsp free(ct_name);
5018 036813ee 2019-05-09 stsp if (err && *new_te) {
5019 56e0773d 2019-11-28 stsp free(*new_te);
5020 036813ee 2019-05-09 stsp *new_te = NULL;
5021 036813ee 2019-05-09 stsp }
5022 036813ee 2019-05-09 stsp return err;
5023 036813ee 2019-05-09 stsp }
5024 036813ee 2019-05-09 stsp
5025 036813ee 2019-05-09 stsp static const struct got_error *
5026 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
5027 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
5028 036813ee 2019-05-09 stsp {
5029 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5030 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
5031 036813ee 2019-05-09 stsp
5032 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5033 036813ee 2019-05-09 stsp if (err)
5034 036813ee 2019-05-09 stsp return err;
5035 036813ee 2019-05-09 stsp if (new_pe == NULL)
5036 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
5037 036813ee 2019-05-09 stsp return NULL;
5038 afa376bf 2019-05-09 stsp }
5039 afa376bf 2019-05-09 stsp
5040 afa376bf 2019-05-09 stsp static const struct got_error *
5041 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
5042 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
5043 afa376bf 2019-05-09 stsp {
5044 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
5045 5f8a88c6 2019-08-03 stsp unsigned char status;
5046 5f8a88c6 2019-08-03 stsp
5047 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
5048 afa376bf 2019-05-09 stsp ct_path++;
5049 5f8a88c6 2019-08-03 stsp
5050 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5051 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
5052 5f8a88c6 2019-08-03 stsp else
5053 5f8a88c6 2019-08-03 stsp status = ct->status;
5054 5f8a88c6 2019-08-03 stsp
5055 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5056 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5057 036813ee 2019-05-09 stsp }
5058 036813ee 2019-05-09 stsp
5059 036813ee 2019-05-09 stsp static const struct got_error *
5060 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
5061 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5062 44d03001 2019-05-09 stsp {
5063 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
5064 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
5065 44d03001 2019-05-09 stsp char *te_path;
5066 44d03001 2019-05-09 stsp
5067 44d03001 2019-05-09 stsp *modified = 0;
5068 44d03001 2019-05-09 stsp
5069 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5070 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5071 44d03001 2019-05-09 stsp te->name) == -1)
5072 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5073 44d03001 2019-05-09 stsp
5074 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5075 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5076 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5077 44d03001 2019-05-09 stsp strlen(te_path));
5078 62d463ca 2020-10-20 naddy if (*modified)
5079 44d03001 2019-05-09 stsp break;
5080 44d03001 2019-05-09 stsp }
5081 44d03001 2019-05-09 stsp
5082 44d03001 2019-05-09 stsp free(te_path);
5083 44d03001 2019-05-09 stsp return err;
5084 44d03001 2019-05-09 stsp }
5085 44d03001 2019-05-09 stsp
5086 44d03001 2019-05-09 stsp static const struct got_error *
5087 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5088 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5089 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5090 036813ee 2019-05-09 stsp {
5091 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5092 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5093 036813ee 2019-05-09 stsp
5094 036813ee 2019-05-09 stsp *ctp = NULL;
5095 036813ee 2019-05-09 stsp
5096 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5097 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5098 036813ee 2019-05-09 stsp char *ct_name = NULL;
5099 036813ee 2019-05-09 stsp int path_matches;
5100 036813ee 2019-05-09 stsp
5101 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5102 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5103 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5104 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5105 5f8a88c6 2019-08-03 stsp continue;
5106 5f8a88c6 2019-08-03 stsp } else {
5107 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5108 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5109 5f8a88c6 2019-08-03 stsp continue;
5110 5f8a88c6 2019-08-03 stsp }
5111 036813ee 2019-05-09 stsp
5112 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5113 036813ee 2019-05-09 stsp continue;
5114 036813ee 2019-05-09 stsp
5115 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5116 62d463ca 2020-10-20 naddy if (err)
5117 036813ee 2019-05-09 stsp return err;
5118 036813ee 2019-05-09 stsp if (!path_matches)
5119 036813ee 2019-05-09 stsp continue;
5120 036813ee 2019-05-09 stsp
5121 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5122 d34b633e 2020-10-19 stsp if (err)
5123 d34b633e 2020-10-19 stsp return err;
5124 d34b633e 2020-10-19 stsp
5125 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5126 d34b633e 2020-10-19 stsp free(ct_name);
5127 036813ee 2019-05-09 stsp continue;
5128 d34b633e 2020-10-19 stsp }
5129 d34b633e 2020-10-19 stsp free(ct_name);
5130 036813ee 2019-05-09 stsp
5131 036813ee 2019-05-09 stsp *ctp = ct;
5132 036813ee 2019-05-09 stsp break;
5133 036813ee 2019-05-09 stsp }
5134 2b496619 2019-07-10 stsp
5135 2b496619 2019-07-10 stsp return err;
5136 2b496619 2019-07-10 stsp }
5137 2b496619 2019-07-10 stsp
5138 2b496619 2019-07-10 stsp static const struct got_error *
5139 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5140 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5141 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5142 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5143 2b496619 2019-07-10 stsp struct got_repository *repo)
5144 2b496619 2019-07-10 stsp {
5145 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5146 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5147 2b496619 2019-07-10 stsp char *subtree_path;
5148 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5149 ba580f68 2020-03-22 stsp int nentries;
5150 2b496619 2019-07-10 stsp
5151 2b496619 2019-07-10 stsp *new_tep = NULL;
5152 2b496619 2019-07-10 stsp
5153 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5154 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5155 2b496619 2019-07-10 stsp child_path) == -1)
5156 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5157 036813ee 2019-05-09 stsp
5158 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5159 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5160 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5161 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5162 56e0773d 2019-11-28 stsp
5163 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, 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 2b496619 2019-07-10 stsp goto done;
5167 2b496619 2019-07-10 stsp }
5168 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5169 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5170 2b496619 2019-07-10 stsp if (err) {
5171 56e0773d 2019-11-28 stsp free(new_te);
5172 2b496619 2019-07-10 stsp goto done;
5173 2b496619 2019-07-10 stsp }
5174 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5175 2b496619 2019-07-10 stsp done:
5176 56e0773d 2019-11-28 stsp free(id);
5177 2b496619 2019-07-10 stsp free(subtree_path);
5178 2b496619 2019-07-10 stsp if (err == NULL)
5179 2b496619 2019-07-10 stsp *new_tep = new_te;
5180 036813ee 2019-05-09 stsp return err;
5181 036813ee 2019-05-09 stsp }
5182 036813ee 2019-05-09 stsp
5183 036813ee 2019-05-09 stsp static const struct got_error *
5184 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5185 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5186 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5187 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5188 036813ee 2019-05-09 stsp struct got_repository *repo)
5189 036813ee 2019-05-09 stsp {
5190 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5191 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5192 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5193 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5194 036813ee 2019-05-09 stsp
5195 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5196 ba580f68 2020-03-22 stsp *nentries = 0;
5197 036813ee 2019-05-09 stsp
5198 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5199 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5200 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5201 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5202 036813ee 2019-05-09 stsp
5203 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5204 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5205 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5206 036813ee 2019-05-09 stsp continue;
5207 036813ee 2019-05-09 stsp
5208 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5209 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5210 036813ee 2019-05-09 stsp continue;
5211 036813ee 2019-05-09 stsp
5212 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5213 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5214 036813ee 2019-05-09 stsp if (err)
5215 036813ee 2019-05-09 stsp goto done;
5216 036813ee 2019-05-09 stsp
5217 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5218 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5219 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5220 036813ee 2019-05-09 stsp if (err)
5221 036813ee 2019-05-09 stsp goto done;
5222 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5223 afa376bf 2019-05-09 stsp if (err)
5224 afa376bf 2019-05-09 stsp goto done;
5225 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5226 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5227 2b496619 2019-07-10 stsp if (err)
5228 2f51b5b3 2019-05-09 stsp goto done;
5229 ba580f68 2020-03-22 stsp (*nentries)++;
5230 2b496619 2019-07-10 stsp } else {
5231 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5232 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5233 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5234 2b496619 2019-07-10 stsp == NULL) {
5235 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5236 2b496619 2019-07-10 stsp child_path, path_base_tree,
5237 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5238 2b496619 2019-07-10 stsp repo);
5239 2b496619 2019-07-10 stsp if (err)
5240 2b496619 2019-07-10 stsp goto done;
5241 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5242 2b496619 2019-07-10 stsp if (err)
5243 2b496619 2019-07-10 stsp goto done;
5244 ba580f68 2020-03-22 stsp (*nentries)++;
5245 9ba0479c 2019-05-10 stsp }
5246 ed175427 2019-05-09 stsp }
5247 2f51b5b3 2019-05-09 stsp }
5248 2f51b5b3 2019-05-09 stsp
5249 2f51b5b3 2019-05-09 stsp if (base_tree) {
5250 56e0773d 2019-11-28 stsp int i, nbase_entries;
5251 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5252 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5253 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5254 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5255 63c5ca5d 2019-08-24 stsp
5256 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5257 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5258 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5259 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5260 63c5ca5d 2019-08-24 stsp if (err)
5261 63c5ca5d 2019-08-24 stsp goto done;
5262 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5263 63c5ca5d 2019-08-24 stsp if (err)
5264 63c5ca5d 2019-08-24 stsp goto done;
5265 ba580f68 2020-03-22 stsp (*nentries)++;
5266 63c5ca5d 2019-08-24 stsp continue;
5267 63c5ca5d 2019-08-24 stsp }
5268 2f51b5b3 2019-05-09 stsp
5269 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5270 44d03001 2019-05-09 stsp int modified;
5271 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5272 036813ee 2019-05-09 stsp if (err)
5273 036813ee 2019-05-09 stsp goto done;
5274 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5275 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5276 2f51b5b3 2019-05-09 stsp if (err)
5277 2f51b5b3 2019-05-09 stsp goto done;
5278 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5279 44d03001 2019-05-09 stsp if (modified) {
5280 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5281 ba580f68 2020-03-22 stsp int nsubentries;
5282 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5283 ba580f68 2020-03-22 stsp &nsubentries, te,
5284 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5285 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5286 44d03001 2019-05-09 stsp if (err)
5287 44d03001 2019-05-09 stsp goto done;
5288 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5289 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5290 ba580f68 2020-03-22 stsp free(new_id);
5291 ba580f68 2020-03-22 stsp continue;
5292 ba580f68 2020-03-22 stsp }
5293 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5294 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5295 56e0773d 2019-11-28 stsp free(new_id);
5296 44d03001 2019-05-09 stsp }
5297 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5298 036813ee 2019-05-09 stsp if (err)
5299 036813ee 2019-05-09 stsp goto done;
5300 ba580f68 2020-03-22 stsp (*nentries)++;
5301 2f51b5b3 2019-05-09 stsp continue;
5302 036813ee 2019-05-09 stsp }
5303 2f51b5b3 2019-05-09 stsp
5304 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5305 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5306 f66c734c 2019-09-22 stsp if (err)
5307 f66c734c 2019-09-22 stsp goto done;
5308 2f51b5b3 2019-05-09 stsp if (ct) {
5309 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5310 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5311 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5312 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5313 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5314 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5315 2f51b5b3 2019-05-09 stsp if (err)
5316 2f51b5b3 2019-05-09 stsp goto done;
5317 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5318 2f51b5b3 2019-05-09 stsp if (err)
5319 2f51b5b3 2019-05-09 stsp goto done;
5320 ba580f68 2020-03-22 stsp (*nentries)++;
5321 2f51b5b3 2019-05-09 stsp }
5322 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5323 b416585c 2019-05-13 stsp status_arg);
5324 afa376bf 2019-05-09 stsp if (err)
5325 afa376bf 2019-05-09 stsp goto done;
5326 2f51b5b3 2019-05-09 stsp } else {
5327 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5328 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5329 2f51b5b3 2019-05-09 stsp if (err)
5330 2f51b5b3 2019-05-09 stsp goto done;
5331 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5332 2f51b5b3 2019-05-09 stsp if (err)
5333 2f51b5b3 2019-05-09 stsp goto done;
5334 ba580f68 2020-03-22 stsp (*nentries)++;
5335 2f51b5b3 2019-05-09 stsp }
5336 ca2503ea 2019-05-09 stsp }
5337 ed175427 2019-05-09 stsp }
5338 0b5cc0d6 2019-05-09 stsp
5339 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5340 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5341 ed175427 2019-05-09 stsp done:
5342 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5343 2e1fa222 2020-07-23 stsp return err;
5344 2e1fa222 2020-07-23 stsp }
5345 2e1fa222 2020-07-23 stsp
5346 2e1fa222 2020-07-23 stsp static const struct got_error *
5347 437adc9d 2020-12-10 yzhong update_fileindex_after_commit(struct got_worktree *worktree,
5348 437adc9d 2020-12-10 yzhong struct got_pathlist_head *commitable_paths,
5349 437adc9d 2020-12-10 yzhong struct got_object_id *new_base_commit_id,
5350 437adc9d 2020-12-10 yzhong struct got_fileindex *fileindex, int have_staged_files)
5351 ebf99748 2019-05-09 stsp {
5352 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5353 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5354 437adc9d 2020-12-10 yzhong char *relpath = NULL;
5355 ebf99748 2019-05-09 stsp
5356 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5357 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5358 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5359 ebf99748 2019-05-09 stsp
5360 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5361 437adc9d 2020-12-10 yzhong
5362 437adc9d 2020-12-10 yzhong err = got_path_skip_common_ancestor(&relpath,
5363 437adc9d 2020-12-10 yzhong worktree->root_path, ct->ondisk_path);
5364 437adc9d 2020-12-10 yzhong if (err)
5365 437adc9d 2020-12-10 yzhong goto done;
5366 437adc9d 2020-12-10 yzhong
5367 ebf99748 2019-05-09 stsp if (ie) {
5368 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5369 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5370 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5371 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5372 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5373 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5374 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5375 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5376 437adc9d 2020-12-10 yzhong
5377 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5378 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5379 437adc9d 2020-12-10 yzhong ct->staged_blob_id->sha1,
5380 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5381 72fd46fa 2019-09-06 stsp !have_staged_files);
5382 ebf99748 2019-05-09 stsp } else
5383 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5384 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath,
5385 437adc9d 2020-12-10 yzhong ct->blob_id->sha1,
5386 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5387 72fd46fa 2019-09-06 stsp !have_staged_files);
5388 ebf99748 2019-05-09 stsp } else {
5389 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5390 ebf99748 2019-05-09 stsp if (err)
5391 437adc9d 2020-12-10 yzhong goto done;
5392 437adc9d 2020-12-10 yzhong err = got_fileindex_entry_update(ie,
5393 437adc9d 2020-12-10 yzhong worktree->root_fd, relpath, ct->blob_id->sha1,
5394 437adc9d 2020-12-10 yzhong new_base_commit_id->sha1, 1);
5395 3969253a 2020-03-07 stsp if (err) {
5396 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5397 437adc9d 2020-12-10 yzhong goto done;
5398 3969253a 2020-03-07 stsp }
5399 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5400 3969253a 2020-03-07 stsp if (err) {
5401 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5402 437adc9d 2020-12-10 yzhong goto done;
5403 3969253a 2020-03-07 stsp }
5404 ebf99748 2019-05-09 stsp }
5405 437adc9d 2020-12-10 yzhong free(relpath);
5406 437adc9d 2020-12-10 yzhong relpath = NULL;
5407 ebf99748 2019-05-09 stsp }
5408 437adc9d 2020-12-10 yzhong done:
5409 437adc9d 2020-12-10 yzhong free(relpath);
5410 d56d26ce 2019-05-10 stsp return err;
5411 d56d26ce 2019-05-10 stsp }
5412 735ef5ac 2019-08-03 stsp
5413 d56d26ce 2019-05-10 stsp
5414 d56d26ce 2019-05-10 stsp static const struct got_error *
5415 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5416 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5417 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5418 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5419 735ef5ac 2019-08-03 stsp int ood_errcode)
5420 d56d26ce 2019-05-10 stsp {
5421 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5422 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5423 1a36436d 2019-06-10 stsp
5424 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5425 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5426 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5427 1a36436d 2019-06-10 stsp return NULL;
5428 9bead371 2019-07-28 stsp /*
5429 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5430 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5431 9bead371 2019-07-28 stsp */
5432 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5433 735ef5ac 2019-08-03 stsp in_repo_path);
5434 9bead371 2019-07-28 stsp if (err) {
5435 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5436 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5437 1a36436d 2019-06-10 stsp goto done;
5438 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5439 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5440 1a36436d 2019-06-10 stsp } else {
5441 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5442 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5443 735ef5ac 2019-08-03 stsp in_repo_path);
5444 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5445 1a36436d 2019-06-10 stsp goto done;
5446 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5447 1a36436d 2019-06-10 stsp }
5448 1a36436d 2019-06-10 stsp done:
5449 1a36436d 2019-06-10 stsp free(id);
5450 1a36436d 2019-06-10 stsp return err;
5451 ebf99748 2019-05-09 stsp }
5452 ebf99748 2019-05-09 stsp
5453 c4296144 2019-05-09 stsp const struct got_error *
5454 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5455 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5456 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5457 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5458 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5459 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5460 afa376bf 2019-05-09 stsp struct got_repository *repo)
5461 c4296144 2019-05-09 stsp {
5462 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5463 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5464 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5465 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5466 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5467 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5468 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5469 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5470 ba580f68 2020-03-22 stsp int nentries;
5471 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5472 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5473 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5474 c4296144 2019-05-09 stsp
5475 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5476 c4296144 2019-05-09 stsp
5477 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5478 675c7539 2019-05-09 stsp
5479 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5480 588edf97 2019-05-10 stsp if (err)
5481 588edf97 2019-05-10 stsp goto done;
5482 de18fc63 2019-05-09 stsp
5483 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5484 588edf97 2019-05-10 stsp if (err)
5485 588edf97 2019-05-10 stsp goto done;
5486 588edf97 2019-05-10 stsp
5487 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5488 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5489 33ad4cbe 2019-05-12 jcs if (err)
5490 33ad4cbe 2019-05-12 jcs goto done;
5491 33ad4cbe 2019-05-12 jcs }
5492 c4296144 2019-05-09 stsp
5493 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5494 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5495 33ad4cbe 2019-05-12 jcs goto done;
5496 33ad4cbe 2019-05-12 jcs }
5497 33ad4cbe 2019-05-12 jcs
5498 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5499 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5500 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5501 cf066bf8 2019-05-09 stsp char *ondisk_path;
5502 cf066bf8 2019-05-09 stsp
5503 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5504 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5505 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5506 5f8a88c6 2019-08-03 stsp continue;
5507 5f8a88c6 2019-08-03 stsp
5508 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5509 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5510 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5511 cf066bf8 2019-05-09 stsp continue;
5512 cf066bf8 2019-05-09 stsp
5513 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5514 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5515 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5516 cf066bf8 2019-05-09 stsp goto done;
5517 cf066bf8 2019-05-09 stsp }
5518 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5519 2e1fa222 2020-07-23 stsp free(ondisk_path);
5520 2e1fa222 2020-07-23 stsp if (err)
5521 cf066bf8 2019-05-09 stsp goto done;
5522 cf066bf8 2019-05-09 stsp }
5523 036813ee 2019-05-09 stsp
5524 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5525 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5526 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5527 036813ee 2019-05-09 stsp if (err)
5528 036813ee 2019-05-09 stsp goto done;
5529 036813ee 2019-05-09 stsp
5530 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5531 de18fc63 2019-05-09 stsp if (err)
5532 de18fc63 2019-05-09 stsp goto done;
5533 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5534 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5535 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5536 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5537 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5538 33ad4cbe 2019-05-12 jcs free(logmsg);
5539 9d40349a 2019-05-09 stsp if (err)
5540 9d40349a 2019-05-09 stsp goto done;
5541 9d40349a 2019-05-09 stsp
5542 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5543 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5544 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5545 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5546 09f5bd90 2019-05-09 stsp goto done;
5547 09f5bd90 2019-05-09 stsp }
5548 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5549 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5550 09f5bd90 2019-05-09 stsp if (err)
5551 09f5bd90 2019-05-09 stsp goto done;
5552 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5553 ebf99748 2019-05-09 stsp if (err)
5554 ebf99748 2019-05-09 stsp goto done;
5555 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5556 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5557 09f5bd90 2019-05-09 stsp goto done;
5558 09f5bd90 2019-05-09 stsp }
5559 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5560 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5561 f2c16586 2019-05-09 stsp if (err)
5562 f2c16586 2019-05-09 stsp goto done;
5563 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5564 f2c16586 2019-05-09 stsp if (err)
5565 f2c16586 2019-05-09 stsp goto done;
5566 f2c16586 2019-05-09 stsp
5567 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5568 09f5bd90 2019-05-09 stsp if (err)
5569 09f5bd90 2019-05-09 stsp goto done;
5570 09f5bd90 2019-05-09 stsp
5571 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5572 ebf99748 2019-05-09 stsp if (err)
5573 ebf99748 2019-05-09 stsp goto done;
5574 c4296144 2019-05-09 stsp done:
5575 588edf97 2019-05-10 stsp if (head_tree)
5576 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5577 588edf97 2019-05-10 stsp if (head_commit)
5578 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5579 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5580 2f17228e 2019-05-12 stsp if (head_ref2) {
5581 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5582 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5583 2f17228e 2019-05-12 stsp err = unlockerr;
5584 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5585 39cd0ff6 2019-07-12 stsp }
5586 39cd0ff6 2019-07-12 stsp return err;
5587 39cd0ff6 2019-07-12 stsp }
5588 5c1e53bc 2019-07-28 stsp
5589 5c1e53bc 2019-07-28 stsp static const struct got_error *
5590 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5591 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5592 5c1e53bc 2019-07-28 stsp {
5593 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5594 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5595 39cd0ff6 2019-07-12 stsp
5596 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5597 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5598 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5599 5c1e53bc 2019-07-28 stsp
5600 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5601 5c1e53bc 2019-07-28 stsp ct_path++;
5602 5c1e53bc 2019-07-28 stsp
5603 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5604 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5605 5c1e53bc 2019-07-28 stsp break;
5606 5c1e53bc 2019-07-28 stsp }
5607 5c1e53bc 2019-07-28 stsp
5608 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5609 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5610 5c1e53bc 2019-07-28 stsp
5611 5c1e53bc 2019-07-28 stsp return NULL;
5612 5c1e53bc 2019-07-28 stsp }
5613 5c1e53bc 2019-07-28 stsp
5614 f0b75401 2019-08-03 stsp static const struct got_error *
5615 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5616 f0b75401 2019-08-03 stsp {
5617 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5618 f0b75401 2019-08-03 stsp
5619 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5620 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5621 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5622 f0b75401 2019-08-03 stsp }
5623 f0b75401 2019-08-03 stsp
5624 f0b75401 2019-08-03 stsp return NULL;
5625 f0b75401 2019-08-03 stsp }
5626 f0b75401 2019-08-03 stsp
5627 5f8a88c6 2019-08-03 stsp static const struct got_error *
5628 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5629 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5630 5f8a88c6 2019-08-03 stsp {
5631 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5632 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5633 5f8a88c6 2019-08-03 stsp
5634 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5635 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5636 5f8a88c6 2019-08-03 stsp continue;
5637 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5638 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5639 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5640 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5641 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5642 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5643 5f8a88c6 2019-08-03 stsp }
5644 5f8a88c6 2019-08-03 stsp
5645 5f8a88c6 2019-08-03 stsp return NULL;
5646 5f8a88c6 2019-08-03 stsp }
5647 5f8a88c6 2019-08-03 stsp
5648 39cd0ff6 2019-07-12 stsp const struct got_error *
5649 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5650 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5651 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5652 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5653 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5654 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5655 39cd0ff6 2019-07-12 stsp {
5656 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5657 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5658 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5659 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5660 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5661 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5662 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5663 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5664 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5665 39cd0ff6 2019-07-12 stsp
5666 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5667 39cd0ff6 2019-07-12 stsp
5668 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5669 39cd0ff6 2019-07-12 stsp
5670 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5671 39cd0ff6 2019-07-12 stsp if (err)
5672 39cd0ff6 2019-07-12 stsp goto done;
5673 39cd0ff6 2019-07-12 stsp
5674 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5675 39cd0ff6 2019-07-12 stsp if (err)
5676 39cd0ff6 2019-07-12 stsp goto done;
5677 39cd0ff6 2019-07-12 stsp
5678 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5679 39cd0ff6 2019-07-12 stsp if (err)
5680 39cd0ff6 2019-07-12 stsp goto done;
5681 39cd0ff6 2019-07-12 stsp
5682 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5683 39cd0ff6 2019-07-12 stsp if (err)
5684 39cd0ff6 2019-07-12 stsp goto done;
5685 39cd0ff6 2019-07-12 stsp
5686 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5687 5f8a88c6 2019-08-03 stsp &have_staged_files);
5688 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5689 5f8a88c6 2019-08-03 stsp goto done;
5690 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5691 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5692 5f8a88c6 2019-08-03 stsp if (err)
5693 5f8a88c6 2019-08-03 stsp goto done;
5694 5f8a88c6 2019-08-03 stsp }
5695 5f8a88c6 2019-08-03 stsp
5696 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5697 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5698 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5699 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5700 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5701 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5702 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5703 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5704 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5705 5c1e53bc 2019-07-28 stsp if (err)
5706 5c1e53bc 2019-07-28 stsp goto done;
5707 5c1e53bc 2019-07-28 stsp }
5708 39cd0ff6 2019-07-12 stsp
5709 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5710 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5711 39cd0ff6 2019-07-12 stsp goto done;
5712 5c1e53bc 2019-07-28 stsp }
5713 5c1e53bc 2019-07-28 stsp
5714 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5715 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5716 5c1e53bc 2019-07-28 stsp if (err)
5717 5c1e53bc 2019-07-28 stsp goto done;
5718 39cd0ff6 2019-07-12 stsp }
5719 f0b75401 2019-08-03 stsp
5720 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5721 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5722 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5723 f0b75401 2019-08-03 stsp
5724 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5725 f0b75401 2019-08-03 stsp ct_path++;
5726 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5727 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5728 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5729 b50cabdf 2019-07-12 stsp if (err)
5730 b50cabdf 2019-07-12 stsp goto done;
5731 f0b75401 2019-08-03 stsp
5732 b50cabdf 2019-07-12 stsp }
5733 b50cabdf 2019-07-12 stsp
5734 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5735 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5736 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5737 39cd0ff6 2019-07-12 stsp if (err)
5738 39cd0ff6 2019-07-12 stsp goto done;
5739 39cd0ff6 2019-07-12 stsp
5740 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
5741 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, have_staged_files);
5742 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5743 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5744 39cd0ff6 2019-07-12 stsp err = sync_err;
5745 39cd0ff6 2019-07-12 stsp done:
5746 39cd0ff6 2019-07-12 stsp if (fileindex)
5747 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5748 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5749 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5750 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5751 39cd0ff6 2019-07-12 stsp err = unlockerr;
5752 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5753 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5754 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5755 39cd0ff6 2019-07-12 stsp }
5756 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5757 c4296144 2019-05-09 stsp return err;
5758 8656d6c4 2019-05-20 stsp }
5759 8656d6c4 2019-05-20 stsp
5760 8656d6c4 2019-05-20 stsp const char *
5761 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5762 8656d6c4 2019-05-20 stsp {
5763 8656d6c4 2019-05-20 stsp return ct->path;
5764 8656d6c4 2019-05-20 stsp }
5765 8656d6c4 2019-05-20 stsp
5766 8656d6c4 2019-05-20 stsp unsigned int
5767 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5768 8656d6c4 2019-05-20 stsp {
5769 8656d6c4 2019-05-20 stsp return ct->status;
5770 818c7501 2019-07-11 stsp }
5771 818c7501 2019-07-11 stsp
5772 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5773 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5774 818c7501 2019-07-11 stsp struct got_repository *repo;
5775 818c7501 2019-07-11 stsp };
5776 818c7501 2019-07-11 stsp
5777 818c7501 2019-07-11 stsp static const struct got_error *
5778 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5779 818c7501 2019-07-11 stsp {
5780 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5781 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5782 818c7501 2019-07-11 stsp unsigned char status;
5783 818c7501 2019-07-11 stsp struct stat sb;
5784 818c7501 2019-07-11 stsp char *ondisk_path;
5785 818c7501 2019-07-11 stsp
5786 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5787 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5788 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5789 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5790 818c7501 2019-07-11 stsp
5791 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5792 818c7501 2019-07-11 stsp == -1)
5793 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5794 818c7501 2019-07-11 stsp
5795 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5796 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5797 818c7501 2019-07-11 stsp free(ondisk_path);
5798 818c7501 2019-07-11 stsp if (err)
5799 818c7501 2019-07-11 stsp return err;
5800 818c7501 2019-07-11 stsp
5801 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5802 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5803 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5804 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5805 818c7501 2019-07-11 stsp
5806 818c7501 2019-07-11 stsp return NULL;
5807 818c7501 2019-07-11 stsp }
5808 818c7501 2019-07-11 stsp
5809 818c7501 2019-07-11 stsp const struct got_error *
5810 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5811 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5812 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5813 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5814 818c7501 2019-07-11 stsp {
5815 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5816 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5817 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5818 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5819 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5820 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5821 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5822 818c7501 2019-07-11 stsp
5823 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5824 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5825 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5826 818c7501 2019-07-11 stsp
5827 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5828 818c7501 2019-07-11 stsp if (err)
5829 818c7501 2019-07-11 stsp return err;
5830 818c7501 2019-07-11 stsp
5831 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5832 818c7501 2019-07-11 stsp if (err)
5833 818c7501 2019-07-11 stsp goto done;
5834 818c7501 2019-07-11 stsp
5835 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5836 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5837 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5838 818c7501 2019-07-11 stsp &ok_arg);
5839 818c7501 2019-07-11 stsp if (err)
5840 818c7501 2019-07-11 stsp goto done;
5841 818c7501 2019-07-11 stsp
5842 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5843 818c7501 2019-07-11 stsp if (err)
5844 818c7501 2019-07-11 stsp goto done;
5845 818c7501 2019-07-11 stsp
5846 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5847 818c7501 2019-07-11 stsp if (err)
5848 818c7501 2019-07-11 stsp goto done;
5849 818c7501 2019-07-11 stsp
5850 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5851 818c7501 2019-07-11 stsp if (err)
5852 818c7501 2019-07-11 stsp goto done;
5853 818c7501 2019-07-11 stsp
5854 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5855 818c7501 2019-07-11 stsp 0);
5856 e51d7b55 2020-01-04 stsp if (err)
5857 e51d7b55 2020-01-04 stsp goto done;
5858 e51d7b55 2020-01-04 stsp
5859 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5860 818c7501 2019-07-11 stsp if (err)
5861 818c7501 2019-07-11 stsp goto done;
5862 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5863 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5864 e51d7b55 2020-01-04 stsp goto done;
5865 e51d7b55 2020-01-04 stsp }
5866 818c7501 2019-07-11 stsp
5867 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5868 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5869 818c7501 2019-07-11 stsp if (err)
5870 818c7501 2019-07-11 stsp goto done;
5871 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5872 818c7501 2019-07-11 stsp if (err)
5873 818c7501 2019-07-11 stsp goto done;
5874 818c7501 2019-07-11 stsp
5875 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5876 818c7501 2019-07-11 stsp
5877 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5878 818c7501 2019-07-11 stsp if (err)
5879 818c7501 2019-07-11 stsp goto done;
5880 818c7501 2019-07-11 stsp
5881 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5882 818c7501 2019-07-11 stsp if (err)
5883 818c7501 2019-07-11 stsp goto done;
5884 818c7501 2019-07-11 stsp
5885 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5886 818c7501 2019-07-11 stsp worktree->base_commit_id);
5887 818c7501 2019-07-11 stsp if (err)
5888 818c7501 2019-07-11 stsp goto done;
5889 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5890 818c7501 2019-07-11 stsp if (err)
5891 818c7501 2019-07-11 stsp goto done;
5892 818c7501 2019-07-11 stsp
5893 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5894 818c7501 2019-07-11 stsp if (err)
5895 818c7501 2019-07-11 stsp goto done;
5896 818c7501 2019-07-11 stsp done:
5897 818c7501 2019-07-11 stsp free(fileindex_path);
5898 818c7501 2019-07-11 stsp free(tmp_branch_name);
5899 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5900 818c7501 2019-07-11 stsp free(branch_ref_name);
5901 818c7501 2019-07-11 stsp if (branch_ref)
5902 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5903 818c7501 2019-07-11 stsp if (wt_branch)
5904 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5905 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5906 818c7501 2019-07-11 stsp if (err) {
5907 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5908 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5909 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5910 818c7501 2019-07-11 stsp }
5911 818c7501 2019-07-11 stsp if (*tmp_branch) {
5912 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5913 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5914 818c7501 2019-07-11 stsp }
5915 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5916 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5917 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5918 3e3a69f1 2019-07-25 stsp }
5919 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5920 818c7501 2019-07-11 stsp }
5921 818c7501 2019-07-11 stsp return err;
5922 818c7501 2019-07-11 stsp }
5923 818c7501 2019-07-11 stsp
5924 818c7501 2019-07-11 stsp const struct got_error *
5925 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5926 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5927 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5928 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5929 818c7501 2019-07-11 stsp {
5930 818c7501 2019-07-11 stsp const struct got_error *err;
5931 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5932 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5933 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5934 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5935 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5936 818c7501 2019-07-11 stsp
5937 818c7501 2019-07-11 stsp *commit_id = NULL;
5938 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5939 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5940 3e3a69f1 2019-07-25 stsp *branch = NULL;
5941 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5942 3e3a69f1 2019-07-25 stsp
5943 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5944 3e3a69f1 2019-07-25 stsp if (err)
5945 3e3a69f1 2019-07-25 stsp return err;
5946 818c7501 2019-07-11 stsp
5947 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5948 3e3a69f1 2019-07-25 stsp if (err)
5949 f032f1f7 2019-08-04 stsp goto done;
5950 f032f1f7 2019-08-04 stsp
5951 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5952 f032f1f7 2019-08-04 stsp &have_staged_files);
5953 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5954 f032f1f7 2019-08-04 stsp goto done;
5955 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5956 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5957 3e3a69f1 2019-07-25 stsp goto done;
5958 f032f1f7 2019-08-04 stsp }
5959 3e3a69f1 2019-07-25 stsp
5960 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5961 818c7501 2019-07-11 stsp if (err)
5962 f032f1f7 2019-08-04 stsp goto done;
5963 818c7501 2019-07-11 stsp
5964 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5965 818c7501 2019-07-11 stsp if (err)
5966 818c7501 2019-07-11 stsp goto done;
5967 818c7501 2019-07-11 stsp
5968 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5969 818c7501 2019-07-11 stsp if (err)
5970 818c7501 2019-07-11 stsp goto done;
5971 818c7501 2019-07-11 stsp
5972 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5973 818c7501 2019-07-11 stsp if (err)
5974 818c7501 2019-07-11 stsp goto done;
5975 818c7501 2019-07-11 stsp
5976 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5977 818c7501 2019-07-11 stsp if (err)
5978 818c7501 2019-07-11 stsp goto done;
5979 818c7501 2019-07-11 stsp
5980 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5981 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5982 818c7501 2019-07-11 stsp if (err)
5983 818c7501 2019-07-11 stsp goto done;
5984 818c7501 2019-07-11 stsp
5985 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5986 818c7501 2019-07-11 stsp if (err)
5987 818c7501 2019-07-11 stsp goto done;
5988 818c7501 2019-07-11 stsp
5989 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5990 818c7501 2019-07-11 stsp if (err)
5991 818c7501 2019-07-11 stsp goto done;
5992 818c7501 2019-07-11 stsp
5993 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5994 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5995 818c7501 2019-07-11 stsp if (err)
5996 818c7501 2019-07-11 stsp goto done;
5997 818c7501 2019-07-11 stsp
5998 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5999 818c7501 2019-07-11 stsp if (err)
6000 818c7501 2019-07-11 stsp goto done;
6001 818c7501 2019-07-11 stsp done:
6002 818c7501 2019-07-11 stsp free(commit_ref_name);
6003 818c7501 2019-07-11 stsp free(branch_ref_name);
6004 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6005 818c7501 2019-07-11 stsp if (commit_ref)
6006 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6007 818c7501 2019-07-11 stsp if (branch_ref)
6008 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
6009 818c7501 2019-07-11 stsp if (err) {
6010 818c7501 2019-07-11 stsp free(*commit_id);
6011 818c7501 2019-07-11 stsp *commit_id = NULL;
6012 818c7501 2019-07-11 stsp if (*tmp_branch) {
6013 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
6014 818c7501 2019-07-11 stsp *tmp_branch = NULL;
6015 818c7501 2019-07-11 stsp }
6016 818c7501 2019-07-11 stsp if (*new_base_branch) {
6017 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
6018 818c7501 2019-07-11 stsp *new_base_branch = NULL;
6019 818c7501 2019-07-11 stsp }
6020 818c7501 2019-07-11 stsp if (*branch) {
6021 818c7501 2019-07-11 stsp got_ref_close(*branch);
6022 818c7501 2019-07-11 stsp *branch = NULL;
6023 818c7501 2019-07-11 stsp }
6024 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6025 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6026 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6027 3e3a69f1 2019-07-25 stsp }
6028 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
6029 818c7501 2019-07-11 stsp }
6030 818c7501 2019-07-11 stsp return err;
6031 c4296144 2019-05-09 stsp }
6032 818c7501 2019-07-11 stsp
6033 818c7501 2019-07-11 stsp const struct got_error *
6034 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6035 818c7501 2019-07-11 stsp {
6036 818c7501 2019-07-11 stsp const struct got_error *err;
6037 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
6038 818c7501 2019-07-11 stsp
6039 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6040 818c7501 2019-07-11 stsp if (err)
6041 818c7501 2019-07-11 stsp return err;
6042 818c7501 2019-07-11 stsp
6043 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6044 818c7501 2019-07-11 stsp free(tmp_branch_name);
6045 818c7501 2019-07-11 stsp return NULL;
6046 818c7501 2019-07-11 stsp }
6047 818c7501 2019-07-11 stsp
6048 818c7501 2019-07-11 stsp static const struct got_error *
6049 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6050 818c7501 2019-07-11 stsp char **logmsg, void *arg)
6051 818c7501 2019-07-11 stsp {
6052 0ebf8283 2019-07-24 stsp *logmsg = arg;
6053 818c7501 2019-07-11 stsp return NULL;
6054 818c7501 2019-07-11 stsp }
6055 818c7501 2019-07-11 stsp
6056 818c7501 2019-07-11 stsp static const struct got_error *
6057 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6058 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6059 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6060 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6061 818c7501 2019-07-11 stsp {
6062 818c7501 2019-07-11 stsp return NULL;
6063 01757395 2019-07-12 stsp }
6064 01757395 2019-07-12 stsp
6065 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
6066 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
6067 01757395 2019-07-12 stsp void *progress_arg;
6068 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
6069 01757395 2019-07-12 stsp };
6070 01757395 2019-07-12 stsp
6071 01757395 2019-07-12 stsp static const struct got_error *
6072 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
6073 01757395 2019-07-12 stsp {
6074 01757395 2019-07-12 stsp const struct got_error *err;
6075 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
6076 01757395 2019-07-12 stsp char *p;
6077 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
6078 01757395 2019-07-12 stsp
6079 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
6080 01757395 2019-07-12 stsp if (err)
6081 01757395 2019-07-12 stsp return err;
6082 01757395 2019-07-12 stsp
6083 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
6084 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6085 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6086 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6087 01757395 2019-07-12 stsp return NULL;
6088 01757395 2019-07-12 stsp
6089 01757395 2019-07-12 stsp p = strdup(path);
6090 01757395 2019-07-12 stsp if (p == NULL)
6091 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6092 01757395 2019-07-12 stsp
6093 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6094 01757395 2019-07-12 stsp if (err || new == NULL)
6095 01757395 2019-07-12 stsp free(p);
6096 01757395 2019-07-12 stsp return err;
6097 01757395 2019-07-12 stsp }
6098 01757395 2019-07-12 stsp
6099 01757395 2019-07-12 stsp void
6100 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6101 01757395 2019-07-12 stsp {
6102 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6103 01757395 2019-07-12 stsp
6104 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6105 01757395 2019-07-12 stsp free((char *)pe->path);
6106 01757395 2019-07-12 stsp
6107 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6108 818c7501 2019-07-11 stsp }
6109 818c7501 2019-07-11 stsp
6110 0ebf8283 2019-07-24 stsp static const struct got_error *
6111 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6112 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6113 818c7501 2019-07-11 stsp {
6114 818c7501 2019-07-11 stsp const struct got_error *err;
6115 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6116 818c7501 2019-07-11 stsp
6117 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6118 818c7501 2019-07-11 stsp if (err) {
6119 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6120 818c7501 2019-07-11 stsp goto done;
6121 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6122 818c7501 2019-07-11 stsp if (err)
6123 818c7501 2019-07-11 stsp goto done;
6124 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6125 818c7501 2019-07-11 stsp if (err)
6126 818c7501 2019-07-11 stsp goto done;
6127 de05890f 2020-03-05 stsp } else if (is_rebase) {
6128 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6129 818c7501 2019-07-11 stsp int cmp;
6130 818c7501 2019-07-11 stsp
6131 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6132 818c7501 2019-07-11 stsp if (err)
6133 818c7501 2019-07-11 stsp goto done;
6134 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6135 818c7501 2019-07-11 stsp free(stored_id);
6136 818c7501 2019-07-11 stsp if (cmp != 0) {
6137 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6138 818c7501 2019-07-11 stsp goto done;
6139 818c7501 2019-07-11 stsp }
6140 818c7501 2019-07-11 stsp }
6141 0ebf8283 2019-07-24 stsp done:
6142 0ebf8283 2019-07-24 stsp if (commit_ref)
6143 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6144 0ebf8283 2019-07-24 stsp return err;
6145 0ebf8283 2019-07-24 stsp }
6146 0ebf8283 2019-07-24 stsp
6147 0ebf8283 2019-07-24 stsp static const struct got_error *
6148 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6149 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6150 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6151 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6152 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6153 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6154 0ebf8283 2019-07-24 stsp {
6155 0ebf8283 2019-07-24 stsp const struct got_error *err;
6156 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6157 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6158 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6159 818c7501 2019-07-11 stsp
6160 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6161 0ebf8283 2019-07-24 stsp
6162 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6163 0ebf8283 2019-07-24 stsp if (err)
6164 0ebf8283 2019-07-24 stsp return err;
6165 0ebf8283 2019-07-24 stsp
6166 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6167 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6168 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6169 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6170 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6171 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6172 818c7501 2019-07-11 stsp if (commit_ref)
6173 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6174 818c7501 2019-07-11 stsp return err;
6175 818c7501 2019-07-11 stsp }
6176 818c7501 2019-07-11 stsp
6177 818c7501 2019-07-11 stsp const struct got_error *
6178 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6179 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6180 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6181 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6182 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6183 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6184 818c7501 2019-07-11 stsp {
6185 0ebf8283 2019-07-24 stsp const struct got_error *err;
6186 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6187 0ebf8283 2019-07-24 stsp
6188 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6189 0ebf8283 2019-07-24 stsp if (err)
6190 0ebf8283 2019-07-24 stsp return err;
6191 0ebf8283 2019-07-24 stsp
6192 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6193 0ebf8283 2019-07-24 stsp if (err)
6194 0ebf8283 2019-07-24 stsp goto done;
6195 0ebf8283 2019-07-24 stsp
6196 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6197 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6198 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6199 0ebf8283 2019-07-24 stsp done:
6200 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6201 0ebf8283 2019-07-24 stsp return err;
6202 0ebf8283 2019-07-24 stsp }
6203 0ebf8283 2019-07-24 stsp
6204 0ebf8283 2019-07-24 stsp const struct got_error *
6205 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6206 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6207 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6208 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6209 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6210 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6211 0ebf8283 2019-07-24 stsp {
6212 0ebf8283 2019-07-24 stsp const struct got_error *err;
6213 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6214 0ebf8283 2019-07-24 stsp
6215 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6216 0ebf8283 2019-07-24 stsp if (err)
6217 0ebf8283 2019-07-24 stsp return err;
6218 0ebf8283 2019-07-24 stsp
6219 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6220 0ebf8283 2019-07-24 stsp if (err)
6221 0ebf8283 2019-07-24 stsp goto done;
6222 0ebf8283 2019-07-24 stsp
6223 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6224 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6225 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6226 0ebf8283 2019-07-24 stsp done:
6227 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6228 0ebf8283 2019-07-24 stsp return err;
6229 0ebf8283 2019-07-24 stsp }
6230 0ebf8283 2019-07-24 stsp
6231 0ebf8283 2019-07-24 stsp static const struct got_error *
6232 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6233 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6234 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6235 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6236 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6237 0ebf8283 2019-07-24 stsp {
6238 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6239 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6240 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6241 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6242 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6243 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6244 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6245 818c7501 2019-07-11 stsp
6246 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6247 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6248 a0e95631 2019-07-12 stsp
6249 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6250 818c7501 2019-07-11 stsp
6251 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6252 a0e95631 2019-07-12 stsp if (err)
6253 3e3a69f1 2019-07-25 stsp return err;
6254 a0e95631 2019-07-12 stsp
6255 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6256 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6257 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6258 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6259 01757395 2019-07-12 stsp /*
6260 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6261 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6262 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6263 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6264 01757395 2019-07-12 stsp */
6265 01757395 2019-07-12 stsp if (merged_paths) {
6266 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6267 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6268 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6269 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6270 f2a9dc41 2019-12-13 tracey 0);
6271 01757395 2019-07-12 stsp if (err)
6272 01757395 2019-07-12 stsp goto done;
6273 01757395 2019-07-12 stsp }
6274 01757395 2019-07-12 stsp } else {
6275 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6276 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6277 01757395 2019-07-12 stsp if (err)
6278 01757395 2019-07-12 stsp goto done;
6279 01757395 2019-07-12 stsp }
6280 a0e95631 2019-07-12 stsp
6281 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6282 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6283 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6284 a0e95631 2019-07-12 stsp if (err)
6285 a0e95631 2019-07-12 stsp goto done;
6286 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6287 a0e95631 2019-07-12 stsp goto done;
6288 ff0d2220 2019-07-11 stsp }
6289 818c7501 2019-07-11 stsp
6290 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6291 edd02c5e 2019-07-11 stsp if (err)
6292 edd02c5e 2019-07-11 stsp goto done;
6293 edd02c5e 2019-07-11 stsp
6294 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6295 818c7501 2019-07-11 stsp if (err)
6296 818c7501 2019-07-11 stsp goto done;
6297 818c7501 2019-07-11 stsp
6298 5943eee2 2019-08-13 stsp if (new_logmsg) {
6299 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6300 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6301 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6302 5943eee2 2019-08-13 stsp goto done;
6303 5943eee2 2019-08-13 stsp }
6304 5943eee2 2019-08-13 stsp } else {
6305 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6306 5943eee2 2019-08-13 stsp if (err)
6307 5943eee2 2019-08-13 stsp goto done;
6308 5943eee2 2019-08-13 stsp }
6309 0ebf8283 2019-07-24 stsp
6310 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6311 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6312 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6313 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6314 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6315 a0e95631 2019-07-12 stsp if (err)
6316 a0e95631 2019-07-12 stsp goto done;
6317 a0e95631 2019-07-12 stsp
6318 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6319 a0e95631 2019-07-12 stsp if (err)
6320 a0e95631 2019-07-12 stsp goto done;
6321 a0e95631 2019-07-12 stsp
6322 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6323 a0e95631 2019-07-12 stsp if (err)
6324 a0e95631 2019-07-12 stsp goto done;
6325 a0e95631 2019-07-12 stsp
6326 437adc9d 2020-12-10 yzhong err = update_fileindex_after_commit(worktree, &commitable_paths,
6327 437adc9d 2020-12-10 yzhong *new_commit_id, fileindex, 0);
6328 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6329 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6330 a0e95631 2019-07-12 stsp err = sync_err;
6331 818c7501 2019-07-11 stsp done:
6332 a0e95631 2019-07-12 stsp free(fileindex_path);
6333 a0e95631 2019-07-12 stsp free(head_commit_id);
6334 a0e95631 2019-07-12 stsp if (head_ref)
6335 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6336 818c7501 2019-07-11 stsp if (err) {
6337 818c7501 2019-07-11 stsp free(*new_commit_id);
6338 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6339 818c7501 2019-07-11 stsp }
6340 818c7501 2019-07-11 stsp return err;
6341 818c7501 2019-07-11 stsp }
6342 818c7501 2019-07-11 stsp
6343 818c7501 2019-07-11 stsp const struct got_error *
6344 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6345 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6346 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6347 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6348 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6349 0ebf8283 2019-07-24 stsp {
6350 0ebf8283 2019-07-24 stsp const struct got_error *err;
6351 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6352 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6353 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6354 0ebf8283 2019-07-24 stsp
6355 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6356 0ebf8283 2019-07-24 stsp if (err)
6357 0ebf8283 2019-07-24 stsp return err;
6358 0ebf8283 2019-07-24 stsp
6359 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6360 0ebf8283 2019-07-24 stsp if (err)
6361 0ebf8283 2019-07-24 stsp goto done;
6362 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6363 0ebf8283 2019-07-24 stsp if (err)
6364 0ebf8283 2019-07-24 stsp goto done;
6365 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6366 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6367 0ebf8283 2019-07-24 stsp goto done;
6368 0ebf8283 2019-07-24 stsp }
6369 0ebf8283 2019-07-24 stsp
6370 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6371 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6372 0ebf8283 2019-07-24 stsp done:
6373 0ebf8283 2019-07-24 stsp if (commit_ref)
6374 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6375 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6376 0ebf8283 2019-07-24 stsp free(commit_id);
6377 0ebf8283 2019-07-24 stsp return err;
6378 0ebf8283 2019-07-24 stsp }
6379 0ebf8283 2019-07-24 stsp
6380 0ebf8283 2019-07-24 stsp const struct got_error *
6381 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6382 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6383 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6384 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6385 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6386 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6387 0ebf8283 2019-07-24 stsp {
6388 0ebf8283 2019-07-24 stsp const struct got_error *err;
6389 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6390 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6391 0ebf8283 2019-07-24 stsp
6392 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6393 0ebf8283 2019-07-24 stsp if (err)
6394 0ebf8283 2019-07-24 stsp return err;
6395 0ebf8283 2019-07-24 stsp
6396 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6397 0ebf8283 2019-07-24 stsp if (err)
6398 0ebf8283 2019-07-24 stsp goto done;
6399 0ebf8283 2019-07-24 stsp
6400 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6401 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6402 0ebf8283 2019-07-24 stsp done:
6403 0ebf8283 2019-07-24 stsp if (commit_ref)
6404 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6405 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6406 0ebf8283 2019-07-24 stsp return err;
6407 0ebf8283 2019-07-24 stsp }
6408 0ebf8283 2019-07-24 stsp
6409 0ebf8283 2019-07-24 stsp const struct got_error *
6410 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6411 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6412 818c7501 2019-07-11 stsp {
6413 3e3a69f1 2019-07-25 stsp if (fileindex)
6414 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6415 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6416 69844fba 2019-07-11 stsp }
6417 69844fba 2019-07-11 stsp
6418 69844fba 2019-07-11 stsp static const struct got_error *
6419 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6420 69844fba 2019-07-11 stsp {
6421 69844fba 2019-07-11 stsp const struct got_error *err;
6422 69844fba 2019-07-11 stsp struct got_reference *ref;
6423 69844fba 2019-07-11 stsp
6424 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6425 69844fba 2019-07-11 stsp if (err) {
6426 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6427 69844fba 2019-07-11 stsp return NULL;
6428 69844fba 2019-07-11 stsp return err;
6429 69844fba 2019-07-11 stsp }
6430 69844fba 2019-07-11 stsp
6431 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6432 69844fba 2019-07-11 stsp got_ref_close(ref);
6433 69844fba 2019-07-11 stsp return err;
6434 818c7501 2019-07-11 stsp }
6435 818c7501 2019-07-11 stsp
6436 69844fba 2019-07-11 stsp static const struct got_error *
6437 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6438 69844fba 2019-07-11 stsp {
6439 69844fba 2019-07-11 stsp const struct got_error *err;
6440 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6441 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6442 69844fba 2019-07-11 stsp
6443 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6444 69844fba 2019-07-11 stsp if (err)
6445 69844fba 2019-07-11 stsp goto done;
6446 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6447 69844fba 2019-07-11 stsp if (err)
6448 69844fba 2019-07-11 stsp goto done;
6449 69844fba 2019-07-11 stsp
6450 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6451 69844fba 2019-07-11 stsp if (err)
6452 69844fba 2019-07-11 stsp goto done;
6453 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6454 69844fba 2019-07-11 stsp if (err)
6455 69844fba 2019-07-11 stsp goto done;
6456 69844fba 2019-07-11 stsp
6457 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6458 69844fba 2019-07-11 stsp if (err)
6459 69844fba 2019-07-11 stsp goto done;
6460 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6461 69844fba 2019-07-11 stsp if (err)
6462 69844fba 2019-07-11 stsp goto done;
6463 69844fba 2019-07-11 stsp
6464 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6465 69844fba 2019-07-11 stsp if (err)
6466 69844fba 2019-07-11 stsp goto done;
6467 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6468 69844fba 2019-07-11 stsp if (err)
6469 69844fba 2019-07-11 stsp goto done;
6470 69844fba 2019-07-11 stsp
6471 69844fba 2019-07-11 stsp done:
6472 69844fba 2019-07-11 stsp free(tmp_branch_name);
6473 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6474 69844fba 2019-07-11 stsp free(branch_ref_name);
6475 69844fba 2019-07-11 stsp free(commit_ref_name);
6476 e600f124 2021-03-21 stsp return err;
6477 e600f124 2021-03-21 stsp }
6478 e600f124 2021-03-21 stsp
6479 e600f124 2021-03-21 stsp const struct got_error *
6480 e600f124 2021-03-21 stsp create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6481 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id, struct got_repository *repo)
6482 e600f124 2021-03-21 stsp {
6483 e600f124 2021-03-21 stsp const struct got_error *err;
6484 e600f124 2021-03-21 stsp struct got_reference *ref = NULL;
6485 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
6486 e600f124 2021-03-21 stsp const char *branch_name = NULL;
6487 e600f124 2021-03-21 stsp char *new_id_str = NULL;
6488 e600f124 2021-03-21 stsp char *refname = NULL;
6489 e600f124 2021-03-21 stsp
6490 e600f124 2021-03-21 stsp branch_name = got_ref_get_name(branch);
6491 e600f124 2021-03-21 stsp if (strncmp(branch_name, "refs/heads/", 11) != 0)
6492 e600f124 2021-03-21 stsp return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6493 e600f124 2021-03-21 stsp branch_name += 11;
6494 e600f124 2021-03-21 stsp
6495 e600f124 2021-03-21 stsp err = got_object_id_str(&new_id_str, new_commit_id);
6496 e600f124 2021-03-21 stsp if (err)
6497 e600f124 2021-03-21 stsp return err;
6498 e600f124 2021-03-21 stsp
6499 e600f124 2021-03-21 stsp if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6500 e600f124 2021-03-21 stsp new_id_str) == -1) {
6501 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
6502 e600f124 2021-03-21 stsp goto done;
6503 e600f124 2021-03-21 stsp }
6504 e600f124 2021-03-21 stsp
6505 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, branch);
6506 e600f124 2021-03-21 stsp if (err)
6507 e600f124 2021-03-21 stsp goto done;
6508 e600f124 2021-03-21 stsp
6509 e600f124 2021-03-21 stsp err = got_ref_alloc(&ref, refname, old_commit_id);
6510 e600f124 2021-03-21 stsp if (err)
6511 e600f124 2021-03-21 stsp goto done;
6512 e600f124 2021-03-21 stsp
6513 e600f124 2021-03-21 stsp err = got_ref_write(ref, repo);
6514 e600f124 2021-03-21 stsp done:
6515 e600f124 2021-03-21 stsp free(new_id_str);
6516 e600f124 2021-03-21 stsp free(refname);
6517 e600f124 2021-03-21 stsp free(old_commit_id);
6518 e600f124 2021-03-21 stsp if (ref)
6519 e600f124 2021-03-21 stsp got_ref_close(ref);
6520 69844fba 2019-07-11 stsp return err;
6521 69844fba 2019-07-11 stsp }
6522 69844fba 2019-07-11 stsp
6523 818c7501 2019-07-11 stsp const struct got_error *
6524 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6525 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6526 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6527 e600f124 2021-03-21 stsp struct got_repository *repo, int create_backup)
6528 818c7501 2019-07-11 stsp {
6529 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
6530 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6531 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
6532 818c7501 2019-07-11 stsp
6533 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6534 818c7501 2019-07-11 stsp if (err)
6535 818c7501 2019-07-11 stsp return err;
6536 e600f124 2021-03-21 stsp
6537 e600f124 2021-03-21 stsp if (create_backup) {
6538 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6539 e600f124 2021-03-21 stsp rebased_branch, new_head_commit_id, repo);
6540 e600f124 2021-03-21 stsp if (err)
6541 e600f124 2021-03-21 stsp goto done;
6542 e600f124 2021-03-21 stsp }
6543 818c7501 2019-07-11 stsp
6544 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6545 818c7501 2019-07-11 stsp if (err)
6546 818c7501 2019-07-11 stsp goto done;
6547 818c7501 2019-07-11 stsp
6548 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6549 818c7501 2019-07-11 stsp if (err)
6550 818c7501 2019-07-11 stsp goto done;
6551 818c7501 2019-07-11 stsp
6552 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6553 818c7501 2019-07-11 stsp if (err)
6554 818c7501 2019-07-11 stsp goto done;
6555 818c7501 2019-07-11 stsp
6556 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6557 a615e0e7 2020-12-16 stsp if (err)
6558 a615e0e7 2020-12-16 stsp goto done;
6559 a615e0e7 2020-12-16 stsp
6560 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
6561 a615e0e7 2020-12-16 stsp if (err)
6562 a615e0e7 2020-12-16 stsp goto done;
6563 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6564 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6565 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
6566 a615e0e7 2020-12-16 stsp err = sync_err;
6567 818c7501 2019-07-11 stsp done:
6568 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
6569 a615e0e7 2020-12-16 stsp free(fileindex_path);
6570 818c7501 2019-07-11 stsp free(new_head_commit_id);
6571 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6572 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6573 818c7501 2019-07-11 stsp err = unlockerr;
6574 818c7501 2019-07-11 stsp return err;
6575 818c7501 2019-07-11 stsp }
6576 818c7501 2019-07-11 stsp
6577 818c7501 2019-07-11 stsp const struct got_error *
6578 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6579 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6580 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6581 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6582 818c7501 2019-07-11 stsp {
6583 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6584 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6585 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6586 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6587 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6588 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6589 818c7501 2019-07-11 stsp
6590 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6591 818c7501 2019-07-11 stsp if (err)
6592 818c7501 2019-07-11 stsp return err;
6593 818c7501 2019-07-11 stsp
6594 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6595 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6596 818c7501 2019-07-11 stsp if (err)
6597 818c7501 2019-07-11 stsp goto done;
6598 818c7501 2019-07-11 stsp
6599 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6600 818c7501 2019-07-11 stsp if (err)
6601 818c7501 2019-07-11 stsp goto done;
6602 818c7501 2019-07-11 stsp
6603 818c7501 2019-07-11 stsp /*
6604 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6605 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6606 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6607 818c7501 2019-07-11 stsp */
6608 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6609 818c7501 2019-07-11 stsp if (err)
6610 818c7501 2019-07-11 stsp goto done;
6611 818c7501 2019-07-11 stsp
6612 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6613 818c7501 2019-07-11 stsp if (err)
6614 818c7501 2019-07-11 stsp goto done;
6615 818c7501 2019-07-11 stsp
6616 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6617 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6618 ca355955 2019-07-12 stsp if (err)
6619 ca355955 2019-07-12 stsp goto done;
6620 ca355955 2019-07-12 stsp
6621 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6622 ca355955 2019-07-12 stsp if (err)
6623 ca355955 2019-07-12 stsp goto done;
6624 ca355955 2019-07-12 stsp
6625 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6626 818c7501 2019-07-11 stsp if (err)
6627 818c7501 2019-07-11 stsp goto done;
6628 818c7501 2019-07-11 stsp
6629 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6630 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6631 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6632 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6633 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6634 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6635 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6636 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6637 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6638 55bd499d 2019-07-12 stsp if (err)
6639 1f1abb7e 2019-08-08 stsp goto sync;
6640 55bd499d 2019-07-12 stsp
6641 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6642 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6643 ca355955 2019-07-12 stsp sync:
6644 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6645 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6646 ca355955 2019-07-12 stsp err = sync_err;
6647 818c7501 2019-07-11 stsp done:
6648 818c7501 2019-07-11 stsp got_ref_close(resolved);
6649 a3a2faf2 2019-07-12 stsp free(tree_id);
6650 818c7501 2019-07-11 stsp free(commit_id);
6651 0ebf8283 2019-07-24 stsp if (fileindex)
6652 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6653 0ebf8283 2019-07-24 stsp free(fileindex_path);
6654 0ebf8283 2019-07-24 stsp
6655 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6656 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6657 0ebf8283 2019-07-24 stsp err = unlockerr;
6658 0ebf8283 2019-07-24 stsp return err;
6659 0ebf8283 2019-07-24 stsp }
6660 0ebf8283 2019-07-24 stsp
6661 0ebf8283 2019-07-24 stsp const struct got_error *
6662 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6663 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6664 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6665 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6666 0ebf8283 2019-07-24 stsp {
6667 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6668 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6669 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6670 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6671 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6672 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6673 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6674 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6675 0ebf8283 2019-07-24 stsp
6676 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6677 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6678 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6679 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6680 0ebf8283 2019-07-24 stsp
6681 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6682 0ebf8283 2019-07-24 stsp if (err)
6683 0ebf8283 2019-07-24 stsp return err;
6684 0ebf8283 2019-07-24 stsp
6685 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6686 0ebf8283 2019-07-24 stsp if (err)
6687 0ebf8283 2019-07-24 stsp goto done;
6688 0ebf8283 2019-07-24 stsp
6689 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6690 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6691 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6692 0ebf8283 2019-07-24 stsp &ok_arg);
6693 0ebf8283 2019-07-24 stsp if (err)
6694 0ebf8283 2019-07-24 stsp goto done;
6695 0ebf8283 2019-07-24 stsp
6696 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6697 0ebf8283 2019-07-24 stsp if (err)
6698 0ebf8283 2019-07-24 stsp goto done;
6699 0ebf8283 2019-07-24 stsp
6700 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6701 0ebf8283 2019-07-24 stsp if (err)
6702 0ebf8283 2019-07-24 stsp goto done;
6703 0ebf8283 2019-07-24 stsp
6704 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6705 0ebf8283 2019-07-24 stsp worktree);
6706 0ebf8283 2019-07-24 stsp if (err)
6707 0ebf8283 2019-07-24 stsp goto done;
6708 0ebf8283 2019-07-24 stsp
6709 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6710 0ebf8283 2019-07-24 stsp 0);
6711 0ebf8283 2019-07-24 stsp if (err)
6712 0ebf8283 2019-07-24 stsp goto done;
6713 0ebf8283 2019-07-24 stsp
6714 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6715 0ebf8283 2019-07-24 stsp if (err)
6716 0ebf8283 2019-07-24 stsp goto done;
6717 0ebf8283 2019-07-24 stsp
6718 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6719 0ebf8283 2019-07-24 stsp if (err)
6720 0ebf8283 2019-07-24 stsp goto done;
6721 0ebf8283 2019-07-24 stsp
6722 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6723 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6724 0ebf8283 2019-07-24 stsp if (err)
6725 0ebf8283 2019-07-24 stsp goto done;
6726 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6727 0ebf8283 2019-07-24 stsp if (err)
6728 0ebf8283 2019-07-24 stsp goto done;
6729 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6730 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6731 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6732 0ebf8283 2019-07-24 stsp goto done;
6733 0ebf8283 2019-07-24 stsp }
6734 0ebf8283 2019-07-24 stsp
6735 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6736 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6737 0ebf8283 2019-07-24 stsp if (err)
6738 0ebf8283 2019-07-24 stsp goto done;
6739 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6740 0ebf8283 2019-07-24 stsp if (err)
6741 0ebf8283 2019-07-24 stsp goto done;
6742 0ebf8283 2019-07-24 stsp
6743 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6744 0ebf8283 2019-07-24 stsp if (err)
6745 0ebf8283 2019-07-24 stsp goto done;
6746 0ebf8283 2019-07-24 stsp done:
6747 0ebf8283 2019-07-24 stsp free(fileindex_path);
6748 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6749 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6750 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6751 0ebf8283 2019-07-24 stsp if (wt_branch)
6752 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6753 0ebf8283 2019-07-24 stsp if (err) {
6754 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6755 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6756 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6757 0ebf8283 2019-07-24 stsp }
6758 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6759 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6760 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6761 0ebf8283 2019-07-24 stsp }
6762 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6763 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6764 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6765 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6766 3e3a69f1 2019-07-25 stsp }
6767 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6768 0ebf8283 2019-07-24 stsp }
6769 0ebf8283 2019-07-24 stsp return err;
6770 0ebf8283 2019-07-24 stsp }
6771 0ebf8283 2019-07-24 stsp
6772 0ebf8283 2019-07-24 stsp const struct got_error *
6773 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6774 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6775 0ebf8283 2019-07-24 stsp {
6776 3e3a69f1 2019-07-25 stsp if (fileindex)
6777 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6778 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6779 0ebf8283 2019-07-24 stsp }
6780 0ebf8283 2019-07-24 stsp
6781 0ebf8283 2019-07-24 stsp const struct got_error *
6782 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6783 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6784 0ebf8283 2019-07-24 stsp {
6785 0ebf8283 2019-07-24 stsp const struct got_error *err;
6786 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6787 0ebf8283 2019-07-24 stsp
6788 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6789 0ebf8283 2019-07-24 stsp if (err)
6790 0ebf8283 2019-07-24 stsp return err;
6791 0ebf8283 2019-07-24 stsp
6792 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6793 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6794 0ebf8283 2019-07-24 stsp return NULL;
6795 0ebf8283 2019-07-24 stsp }
6796 0ebf8283 2019-07-24 stsp
6797 0ebf8283 2019-07-24 stsp const struct got_error *
6798 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6799 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6800 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6801 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6802 0ebf8283 2019-07-24 stsp {
6803 0ebf8283 2019-07-24 stsp const struct got_error *err;
6804 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6805 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6806 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6807 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6808 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6809 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6810 0ebf8283 2019-07-24 stsp
6811 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6812 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6813 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6814 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6815 0ebf8283 2019-07-24 stsp
6816 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6817 3e3a69f1 2019-07-25 stsp if (err)
6818 3e3a69f1 2019-07-25 stsp return err;
6819 3e3a69f1 2019-07-25 stsp
6820 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6821 3e3a69f1 2019-07-25 stsp if (err)
6822 f032f1f7 2019-08-04 stsp goto done;
6823 f032f1f7 2019-08-04 stsp
6824 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6825 f032f1f7 2019-08-04 stsp &have_staged_files);
6826 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6827 f032f1f7 2019-08-04 stsp goto done;
6828 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6829 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6830 3e3a69f1 2019-07-25 stsp goto done;
6831 f032f1f7 2019-08-04 stsp }
6832 3e3a69f1 2019-07-25 stsp
6833 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6834 0ebf8283 2019-07-24 stsp if (err)
6835 f032f1f7 2019-08-04 stsp goto done;
6836 0ebf8283 2019-07-24 stsp
6837 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6838 0ebf8283 2019-07-24 stsp if (err)
6839 0ebf8283 2019-07-24 stsp goto done;
6840 0ebf8283 2019-07-24 stsp
6841 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6842 0ebf8283 2019-07-24 stsp if (err)
6843 0ebf8283 2019-07-24 stsp goto done;
6844 0ebf8283 2019-07-24 stsp
6845 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6846 0ebf8283 2019-07-24 stsp worktree);
6847 0ebf8283 2019-07-24 stsp if (err)
6848 0ebf8283 2019-07-24 stsp goto done;
6849 0ebf8283 2019-07-24 stsp
6850 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6851 0ebf8283 2019-07-24 stsp if (err)
6852 0ebf8283 2019-07-24 stsp goto done;
6853 0ebf8283 2019-07-24 stsp
6854 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6855 0ebf8283 2019-07-24 stsp if (err)
6856 0ebf8283 2019-07-24 stsp goto done;
6857 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6858 0ebf8283 2019-07-24 stsp if (err)
6859 0ebf8283 2019-07-24 stsp goto done;
6860 0ebf8283 2019-07-24 stsp
6861 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6862 0ebf8283 2019-07-24 stsp if (err)
6863 0ebf8283 2019-07-24 stsp goto done;
6864 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6865 0ebf8283 2019-07-24 stsp if (err)
6866 0ebf8283 2019-07-24 stsp goto done;
6867 0ebf8283 2019-07-24 stsp
6868 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6869 0ebf8283 2019-07-24 stsp if (err)
6870 0ebf8283 2019-07-24 stsp goto done;
6871 0ebf8283 2019-07-24 stsp done:
6872 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6873 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6874 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6875 0ebf8283 2019-07-24 stsp if (commit_ref)
6876 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6877 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6878 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6879 0ebf8283 2019-07-24 stsp if (err) {
6880 0ebf8283 2019-07-24 stsp free(*commit_id);
6881 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6882 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6883 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6884 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6885 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6886 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6887 0ebf8283 2019-07-24 stsp }
6888 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6889 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6890 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6891 3e3a69f1 2019-07-25 stsp }
6892 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6893 0ebf8283 2019-07-24 stsp }
6894 0ebf8283 2019-07-24 stsp return err;
6895 0ebf8283 2019-07-24 stsp }
6896 0ebf8283 2019-07-24 stsp
6897 0ebf8283 2019-07-24 stsp static const struct got_error *
6898 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6899 0ebf8283 2019-07-24 stsp {
6900 0ebf8283 2019-07-24 stsp const struct got_error *err;
6901 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6902 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6903 0ebf8283 2019-07-24 stsp
6904 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6905 0ebf8283 2019-07-24 stsp if (err)
6906 0ebf8283 2019-07-24 stsp goto done;
6907 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6908 0ebf8283 2019-07-24 stsp if (err)
6909 0ebf8283 2019-07-24 stsp goto done;
6910 0ebf8283 2019-07-24 stsp
6911 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6912 0ebf8283 2019-07-24 stsp worktree);
6913 0ebf8283 2019-07-24 stsp if (err)
6914 0ebf8283 2019-07-24 stsp goto done;
6915 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, 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 = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6920 0ebf8283 2019-07-24 stsp if (err)
6921 0ebf8283 2019-07-24 stsp goto done;
6922 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6923 0ebf8283 2019-07-24 stsp if (err)
6924 0ebf8283 2019-07-24 stsp goto done;
6925 0ebf8283 2019-07-24 stsp
6926 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6927 0ebf8283 2019-07-24 stsp if (err)
6928 0ebf8283 2019-07-24 stsp goto done;
6929 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6930 0ebf8283 2019-07-24 stsp if (err)
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp done:
6933 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6934 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6935 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6936 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6937 0ebf8283 2019-07-24 stsp return err;
6938 0ebf8283 2019-07-24 stsp }
6939 0ebf8283 2019-07-24 stsp
6940 0ebf8283 2019-07-24 stsp const struct got_error *
6941 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6942 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6943 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6944 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6945 0ebf8283 2019-07-24 stsp {
6946 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6947 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6948 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6949 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6950 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6951 0ebf8283 2019-07-24 stsp
6952 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6953 0ebf8283 2019-07-24 stsp if (err)
6954 0ebf8283 2019-07-24 stsp return err;
6955 0ebf8283 2019-07-24 stsp
6956 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6957 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6958 0ebf8283 2019-07-24 stsp if (err)
6959 0ebf8283 2019-07-24 stsp goto done;
6960 0ebf8283 2019-07-24 stsp
6961 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6962 0ebf8283 2019-07-24 stsp if (err)
6963 0ebf8283 2019-07-24 stsp goto done;
6964 0ebf8283 2019-07-24 stsp
6965 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6966 0ebf8283 2019-07-24 stsp if (err)
6967 0ebf8283 2019-07-24 stsp goto done;
6968 0ebf8283 2019-07-24 stsp
6969 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6970 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6971 0ebf8283 2019-07-24 stsp if (err)
6972 0ebf8283 2019-07-24 stsp goto done;
6973 0ebf8283 2019-07-24 stsp
6974 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6975 0ebf8283 2019-07-24 stsp if (err)
6976 0ebf8283 2019-07-24 stsp goto done;
6977 0ebf8283 2019-07-24 stsp
6978 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6979 0ebf8283 2019-07-24 stsp if (err)
6980 0ebf8283 2019-07-24 stsp goto done;
6981 0ebf8283 2019-07-24 stsp
6982 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6983 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6984 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6985 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6986 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6987 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6988 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6989 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6990 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6991 0ebf8283 2019-07-24 stsp if (err)
6992 1f1abb7e 2019-08-08 stsp goto sync;
6993 0ebf8283 2019-07-24 stsp
6994 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6995 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6996 0ebf8283 2019-07-24 stsp sync:
6997 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6998 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6999 0ebf8283 2019-07-24 stsp err = sync_err;
7000 0ebf8283 2019-07-24 stsp done:
7001 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
7002 0ebf8283 2019-07-24 stsp free(tree_id);
7003 818c7501 2019-07-11 stsp free(fileindex_path);
7004 818c7501 2019-07-11 stsp
7005 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7006 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
7007 818c7501 2019-07-11 stsp err = unlockerr;
7008 818c7501 2019-07-11 stsp return err;
7009 818c7501 2019-07-11 stsp }
7010 0ebf8283 2019-07-24 stsp
7011 0ebf8283 2019-07-24 stsp const struct got_error *
7012 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
7013 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7014 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
7015 0ebf8283 2019-07-24 stsp {
7016 a615e0e7 2020-12-16 stsp const struct got_error *err, *unlockerr, *sync_err;
7017 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
7018 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
7019 a615e0e7 2020-12-16 stsp char *fileindex_path = NULL;
7020 0ebf8283 2019-07-24 stsp
7021 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7022 0ebf8283 2019-07-24 stsp if (err)
7023 0ebf8283 2019-07-24 stsp return err;
7024 0ebf8283 2019-07-24 stsp
7025 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
7026 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
7027 e600f124 2021-03-21 stsp if (err)
7028 e600f124 2021-03-21 stsp goto done;
7029 e600f124 2021-03-21 stsp
7030 e600f124 2021-03-21 stsp err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7031 e600f124 2021-03-21 stsp resolved, new_head_commit_id, repo);
7032 0ebf8283 2019-07-24 stsp if (err)
7033 0ebf8283 2019-07-24 stsp goto done;
7034 0ebf8283 2019-07-24 stsp
7035 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
7036 0ebf8283 2019-07-24 stsp if (err)
7037 0ebf8283 2019-07-24 stsp goto done;
7038 0ebf8283 2019-07-24 stsp
7039 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
7040 0ebf8283 2019-07-24 stsp if (err)
7041 0ebf8283 2019-07-24 stsp goto done;
7042 0ebf8283 2019-07-24 stsp
7043 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
7044 0ebf8283 2019-07-24 stsp if (err)
7045 0ebf8283 2019-07-24 stsp goto done;
7046 0ebf8283 2019-07-24 stsp
7047 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
7048 a615e0e7 2020-12-16 stsp if (err)
7049 a615e0e7 2020-12-16 stsp goto done;
7050 a615e0e7 2020-12-16 stsp
7051 a615e0e7 2020-12-16 stsp err = get_fileindex_path(&fileindex_path, worktree);
7052 a615e0e7 2020-12-16 stsp if (err)
7053 a615e0e7 2020-12-16 stsp goto done;
7054 a615e0e7 2020-12-16 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7055 a615e0e7 2020-12-16 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7056 a615e0e7 2020-12-16 stsp if (sync_err && err == NULL)
7057 a615e0e7 2020-12-16 stsp err = sync_err;
7058 0ebf8283 2019-07-24 stsp done:
7059 a615e0e7 2020-12-16 stsp got_fileindex_free(fileindex);
7060 a615e0e7 2020-12-16 stsp free(fileindex_path);
7061 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
7062 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7063 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
7064 0ebf8283 2019-07-24 stsp err = unlockerr;
7065 0ebf8283 2019-07-24 stsp return err;
7066 0ebf8283 2019-07-24 stsp }
7067 0ebf8283 2019-07-24 stsp
7068 0ebf8283 2019-07-24 stsp const struct got_error *
7069 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7070 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7071 0ebf8283 2019-07-24 stsp {
7072 0ebf8283 2019-07-24 stsp const struct got_error *err;
7073 0ebf8283 2019-07-24 stsp char *commit_ref_name;
7074 0ebf8283 2019-07-24 stsp
7075 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7076 0ebf8283 2019-07-24 stsp if (err)
7077 0ebf8283 2019-07-24 stsp return err;
7078 0ebf8283 2019-07-24 stsp
7079 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7080 0ebf8283 2019-07-24 stsp if (err)
7081 0ebf8283 2019-07-24 stsp goto done;
7082 0ebf8283 2019-07-24 stsp
7083 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
7084 0ebf8283 2019-07-24 stsp done:
7085 0ebf8283 2019-07-24 stsp free(commit_ref_name);
7086 2822a352 2019-10-15 stsp return err;
7087 2822a352 2019-10-15 stsp }
7088 2822a352 2019-10-15 stsp
7089 2822a352 2019-10-15 stsp const struct got_error *
7090 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7091 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7092 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
7093 2822a352 2019-10-15 stsp struct got_repository *repo)
7094 2822a352 2019-10-15 stsp {
7095 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
7096 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7097 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
7098 2822a352 2019-10-15 stsp
7099 2822a352 2019-10-15 stsp *fileindex = NULL;
7100 2822a352 2019-10-15 stsp *branch_ref = NULL;
7101 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7102 2822a352 2019-10-15 stsp
7103 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
7104 2822a352 2019-10-15 stsp if (err)
7105 2822a352 2019-10-15 stsp return err;
7106 2822a352 2019-10-15 stsp
7107 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7108 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
7109 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
7110 2822a352 2019-10-15 stsp "update -b or different branch name required");
7111 2822a352 2019-10-15 stsp goto done;
7112 2822a352 2019-10-15 stsp }
7113 2822a352 2019-10-15 stsp
7114 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
7115 2822a352 2019-10-15 stsp if (err)
7116 2822a352 2019-10-15 stsp goto done;
7117 2822a352 2019-10-15 stsp
7118 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
7119 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
7120 2822a352 2019-10-15 stsp ok_arg.repo = repo;
7121 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7122 2822a352 2019-10-15 stsp &ok_arg);
7123 2822a352 2019-10-15 stsp if (err)
7124 2822a352 2019-10-15 stsp goto done;
7125 2822a352 2019-10-15 stsp
7126 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
7127 2822a352 2019-10-15 stsp if (err)
7128 2822a352 2019-10-15 stsp goto done;
7129 2822a352 2019-10-15 stsp
7130 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
7131 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
7132 2822a352 2019-10-15 stsp done:
7133 2822a352 2019-10-15 stsp if (err) {
7134 2822a352 2019-10-15 stsp if (*branch_ref) {
7135 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
7136 2822a352 2019-10-15 stsp *branch_ref = NULL;
7137 2822a352 2019-10-15 stsp }
7138 2822a352 2019-10-15 stsp if (*base_branch_ref) {
7139 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
7140 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
7141 2822a352 2019-10-15 stsp }
7142 2822a352 2019-10-15 stsp if (*fileindex) {
7143 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
7144 2822a352 2019-10-15 stsp *fileindex = NULL;
7145 2822a352 2019-10-15 stsp }
7146 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
7147 2822a352 2019-10-15 stsp }
7148 0cb83759 2019-08-03 stsp return err;
7149 0cb83759 2019-08-03 stsp }
7150 0cb83759 2019-08-03 stsp
7151 2822a352 2019-10-15 stsp const struct got_error *
7152 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
7153 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7154 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7155 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7156 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7157 2822a352 2019-10-15 stsp {
7158 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7159 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
7160 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
7161 2822a352 2019-10-15 stsp
7162 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7163 2822a352 2019-10-15 stsp if (err)
7164 2822a352 2019-10-15 stsp goto done;
7165 2822a352 2019-10-15 stsp
7166 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7167 2822a352 2019-10-15 stsp if (err)
7168 2822a352 2019-10-15 stsp goto done;
7169 2822a352 2019-10-15 stsp
7170 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7171 2822a352 2019-10-15 stsp worktree->path_prefix);
7172 2822a352 2019-10-15 stsp if (err)
7173 2822a352 2019-10-15 stsp goto done;
7174 2822a352 2019-10-15 stsp
7175 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7176 2822a352 2019-10-15 stsp if (err)
7177 2822a352 2019-10-15 stsp goto done;
7178 2822a352 2019-10-15 stsp
7179 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7180 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7181 2822a352 2019-10-15 stsp if (err)
7182 2822a352 2019-10-15 stsp goto sync;
7183 2822a352 2019-10-15 stsp
7184 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7185 2822a352 2019-10-15 stsp if (err)
7186 2822a352 2019-10-15 stsp goto sync;
7187 2822a352 2019-10-15 stsp
7188 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7189 6e210706 2021-01-22 stsp if (err)
7190 6e210706 2021-01-22 stsp goto sync;
7191 6e210706 2021-01-22 stsp
7192 6e210706 2021-01-22 stsp err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7193 2822a352 2019-10-15 stsp sync:
7194 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7195 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7196 2822a352 2019-10-15 stsp err = sync_err;
7197 2822a352 2019-10-15 stsp
7198 2822a352 2019-10-15 stsp done:
7199 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7200 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7201 2822a352 2019-10-15 stsp err = unlockerr;
7202 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7203 2822a352 2019-10-15 stsp
7204 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7205 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7206 2822a352 2019-10-15 stsp err = unlockerr;
7207 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7208 2822a352 2019-10-15 stsp
7209 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7210 2822a352 2019-10-15 stsp free(fileindex_path);
7211 2822a352 2019-10-15 stsp free(tree_id);
7212 2822a352 2019-10-15 stsp
7213 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7214 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7215 2822a352 2019-10-15 stsp err = unlockerr;
7216 2822a352 2019-10-15 stsp return err;
7217 2822a352 2019-10-15 stsp }
7218 2822a352 2019-10-15 stsp
7219 2822a352 2019-10-15 stsp const struct got_error *
7220 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7221 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7222 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7223 2822a352 2019-10-15 stsp {
7224 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7225 8b692cd0 2019-10-21 stsp
7226 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7227 8b692cd0 2019-10-21 stsp
7228 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7229 8b692cd0 2019-10-21 stsp
7230 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7231 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7232 8b692cd0 2019-10-21 stsp err = unlockerr;
7233 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7234 8b692cd0 2019-10-21 stsp
7235 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7236 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7237 8b692cd0 2019-10-21 stsp err = unlockerr;
7238 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7239 8b692cd0 2019-10-21 stsp
7240 8b692cd0 2019-10-21 stsp return err;
7241 2822a352 2019-10-15 stsp }
7242 2822a352 2019-10-15 stsp
7243 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7244 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7245 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7246 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7247 2db2652d 2019-08-07 stsp struct got_repository *repo;
7248 2db2652d 2019-08-07 stsp int have_changes;
7249 2db2652d 2019-08-07 stsp };
7250 2db2652d 2019-08-07 stsp
7251 2db2652d 2019-08-07 stsp const struct got_error *
7252 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7253 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7254 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7255 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7256 735ef5ac 2019-08-03 stsp {
7257 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7258 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7259 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7260 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7261 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7262 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7263 8b13ce36 2019-08-08 stsp
7264 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7265 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7266 8b13ce36 2019-08-08 stsp return NULL;
7267 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7268 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7269 735ef5ac 2019-08-03 stsp
7270 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7271 735ef5ac 2019-08-03 stsp if (ie == NULL)
7272 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7273 735ef5ac 2019-08-03 stsp
7274 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7275 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7276 735ef5ac 2019-08-03 stsp relpath) == -1)
7277 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7278 735ef5ac 2019-08-03 stsp
7279 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7280 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7281 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7282 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7283 735ef5ac 2019-08-03 stsp }
7284 735ef5ac 2019-08-03 stsp
7285 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7286 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7287 3aa5969e 2019-08-06 stsp goto done;
7288 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7289 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7290 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7291 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7292 735ef5ac 2019-08-03 stsp goto done;
7293 3aa5969e 2019-08-06 stsp }
7294 735ef5ac 2019-08-03 stsp
7295 2db2652d 2019-08-07 stsp a->have_changes = 1;
7296 2db2652d 2019-08-07 stsp
7297 735ef5ac 2019-08-03 stsp p = in_repo_path;
7298 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7299 735ef5ac 2019-08-03 stsp p++;
7300 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7301 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7302 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7303 735ef5ac 2019-08-03 stsp done:
7304 735ef5ac 2019-08-03 stsp free(in_repo_path);
7305 dc424a06 2019-08-07 stsp return err;
7306 dc424a06 2019-08-07 stsp }
7307 dc424a06 2019-08-07 stsp
7308 2db2652d 2019-08-07 stsp struct stage_path_arg {
7309 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7310 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7311 2db2652d 2019-08-07 stsp struct got_repository *repo;
7312 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7313 2db2652d 2019-08-07 stsp void *status_arg;
7314 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7315 2db2652d 2019-08-07 stsp void *patch_arg;
7316 7b5dc508 2019-10-28 stsp int staged_something;
7317 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7318 2db2652d 2019-08-07 stsp };
7319 2db2652d 2019-08-07 stsp
7320 2db2652d 2019-08-07 stsp static const struct got_error *
7321 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7322 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7323 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7324 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7325 0cb83759 2019-08-03 stsp {
7326 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7327 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7328 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7329 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7330 0cb83759 2019-08-03 stsp uint32_t stage;
7331 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7332 0aeb8099 2020-07-23 stsp struct stat sb;
7333 8b13ce36 2019-08-08 stsp
7334 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7335 8b13ce36 2019-08-08 stsp return NULL;
7336 0cb83759 2019-08-03 stsp
7337 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7338 d3e7c587 2019-08-03 stsp if (ie == NULL)
7339 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7340 0cb83759 2019-08-03 stsp
7341 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7342 2db2652d 2019-08-07 stsp relpath)== -1)
7343 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7344 0cb83759 2019-08-03 stsp
7345 0cb83759 2019-08-03 stsp switch (status) {
7346 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7347 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7348 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7349 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7350 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7351 0aeb8099 2020-07-23 stsp break;
7352 0aeb8099 2020-07-23 stsp }
7353 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7354 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7355 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7356 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7357 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7358 dc424a06 2019-08-07 stsp if (err)
7359 dc424a06 2019-08-07 stsp break;
7360 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7361 dc424a06 2019-08-07 stsp break;
7362 dc424a06 2019-08-07 stsp } else {
7363 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7364 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7365 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7366 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7367 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7368 dc424a06 2019-08-07 stsp break;
7369 dc424a06 2019-08-07 stsp }
7370 dc424a06 2019-08-07 stsp }
7371 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7372 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7373 0cb83759 2019-08-03 stsp if (err)
7374 dc424a06 2019-08-07 stsp break;
7375 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7376 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7377 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7378 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7379 0cb83759 2019-08-03 stsp else
7380 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7381 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7382 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7383 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7384 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7385 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7386 35213c7c 2020-07-23 stsp ssize_t target_len;
7387 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7388 35213c7c 2020-07-23 stsp sizeof(target_path));
7389 35213c7c 2020-07-23 stsp if (target_len == -1) {
7390 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7391 35213c7c 2020-07-23 stsp ondisk_path);
7392 35213c7c 2020-07-23 stsp break;
7393 35213c7c 2020-07-23 stsp }
7394 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7395 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7396 35213c7c 2020-07-23 stsp a->worktree->root_path);
7397 35213c7c 2020-07-23 stsp if (err)
7398 35213c7c 2020-07-23 stsp break;
7399 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7400 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7401 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7402 35213c7c 2020-07-23 stsp break;
7403 35213c7c 2020-07-23 stsp }
7404 35213c7c 2020-07-23 stsp }
7405 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7406 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7407 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7408 35213c7c 2020-07-23 stsp else
7409 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7410 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7411 0aeb8099 2020-07-23 stsp } else {
7412 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7413 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7414 0aeb8099 2020-07-23 stsp }
7415 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7416 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7417 dc424a06 2019-08-07 stsp break;
7418 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7419 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7420 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7421 0cb83759 2019-08-03 stsp break;
7422 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7423 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7424 d3e7c587 2019-08-03 stsp break;
7425 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7426 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7427 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7428 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7429 dc424a06 2019-08-07 stsp if (err)
7430 dc424a06 2019-08-07 stsp break;
7431 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7432 88f33a19 2019-08-08 stsp break;
7433 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7434 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7435 dc424a06 2019-08-07 stsp break;
7436 88f33a19 2019-08-08 stsp }
7437 dc424a06 2019-08-07 stsp }
7438 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7439 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7440 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7441 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7442 dc424a06 2019-08-07 stsp break;
7443 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7444 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7445 12463d8b 2019-12-13 stsp de_name);
7446 0cb83759 2019-08-03 stsp break;
7447 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7448 d3e7c587 2019-08-03 stsp break;
7449 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7450 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7451 ebf48fd5 2019-08-03 stsp break;
7452 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7453 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7454 2a06fe5f 2019-08-24 stsp break;
7455 0cb83759 2019-08-03 stsp default:
7456 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7457 537ac44b 2019-08-03 stsp break;
7458 0cb83759 2019-08-03 stsp }
7459 dc424a06 2019-08-07 stsp
7460 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7461 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7462 dc424a06 2019-08-07 stsp free(path_content);
7463 2db2652d 2019-08-07 stsp free(ondisk_path);
7464 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7465 0ebf8283 2019-07-24 stsp return err;
7466 0ebf8283 2019-07-24 stsp }
7467 0cb83759 2019-08-03 stsp
7468 0cb83759 2019-08-03 stsp const struct got_error *
7469 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7470 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7471 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7472 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7473 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7474 0cb83759 2019-08-03 stsp {
7475 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7476 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7477 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7478 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7479 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7480 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7481 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7482 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7483 0cb83759 2019-08-03 stsp
7484 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7485 0cb83759 2019-08-03 stsp if (err)
7486 0cb83759 2019-08-03 stsp return err;
7487 0cb83759 2019-08-03 stsp
7488 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7489 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7490 735ef5ac 2019-08-03 stsp if (err)
7491 735ef5ac 2019-08-03 stsp goto done;
7492 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7493 735ef5ac 2019-08-03 stsp if (err)
7494 735ef5ac 2019-08-03 stsp goto done;
7495 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7496 0cb83759 2019-08-03 stsp if (err)
7497 0cb83759 2019-08-03 stsp goto done;
7498 0cb83759 2019-08-03 stsp
7499 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7500 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7501 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7502 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7503 2db2652d 2019-08-07 stsp oka.repo = repo;
7504 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7505 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7506 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7507 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7508 735ef5ac 2019-08-03 stsp if (err)
7509 735ef5ac 2019-08-03 stsp goto done;
7510 735ef5ac 2019-08-03 stsp }
7511 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7512 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7513 2db2652d 2019-08-07 stsp goto done;
7514 2db2652d 2019-08-07 stsp }
7515 735ef5ac 2019-08-03 stsp
7516 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7517 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7518 2db2652d 2019-08-07 stsp spa.repo = repo;
7519 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7520 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7521 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7522 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7523 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7524 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7525 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7526 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7527 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7528 42005733 2019-08-03 stsp if (err)
7529 2db2652d 2019-08-07 stsp goto done;
7530 7b5dc508 2019-10-28 stsp }
7531 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7532 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7533 7b5dc508 2019-10-28 stsp goto done;
7534 0cb83759 2019-08-03 stsp }
7535 0cb83759 2019-08-03 stsp
7536 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7537 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7538 0cb83759 2019-08-03 stsp err = sync_err;
7539 0cb83759 2019-08-03 stsp done:
7540 735ef5ac 2019-08-03 stsp if (head_ref)
7541 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7542 735ef5ac 2019-08-03 stsp free(head_commit_id);
7543 0cb83759 2019-08-03 stsp free(fileindex_path);
7544 0cb83759 2019-08-03 stsp if (fileindex)
7545 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7546 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7547 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7548 0cb83759 2019-08-03 stsp err = unlockerr;
7549 0cb83759 2019-08-03 stsp return err;
7550 0cb83759 2019-08-03 stsp }
7551 ad493afc 2019-08-03 stsp
7552 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7553 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7554 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7555 ad493afc 2019-08-03 stsp struct got_repository *repo;
7556 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7557 ad493afc 2019-08-03 stsp void *progress_arg;
7558 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7559 2e1f37b0 2019-08-08 stsp void *patch_arg;
7560 ad493afc 2019-08-03 stsp };
7561 2e1f37b0 2019-08-08 stsp
7562 2e1f37b0 2019-08-08 stsp static const struct got_error *
7563 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7564 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7565 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7566 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7567 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7568 2e1f37b0 2019-08-08 stsp {
7569 fe621944 2020-11-10 stsp const struct got_error *err, *free_err;
7570 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7571 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7572 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7573 fe621944 2020-11-10 stsp struct got_diffreg_result *diffreg_result = NULL;
7574 fe621944 2020-11-10 stsp int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7575 fe621944 2020-11-10 stsp int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7576 2e1f37b0 2019-08-08 stsp
7577 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7578 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7579 2e1f37b0 2019-08-08 stsp
7580 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7581 2e1f37b0 2019-08-08 stsp if (err)
7582 2e1f37b0 2019-08-08 stsp return err;
7583 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7584 2e1f37b0 2019-08-08 stsp if (err)
7585 2e1f37b0 2019-08-08 stsp goto done;
7586 2e1f37b0 2019-08-08 stsp
7587 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7588 2e1f37b0 2019-08-08 stsp if (err)
7589 2e1f37b0 2019-08-08 stsp goto done;
7590 2e1f37b0 2019-08-08 stsp
7591 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7592 2e1f37b0 2019-08-08 stsp if (err)
7593 2e1f37b0 2019-08-08 stsp goto done;
7594 2e1f37b0 2019-08-08 stsp
7595 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7596 2e1f37b0 2019-08-08 stsp if (err)
7597 2e1f37b0 2019-08-08 stsp goto done;
7598 2e1f37b0 2019-08-08 stsp
7599 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7600 2e1f37b0 2019-08-08 stsp if (err)
7601 2e1f37b0 2019-08-08 stsp goto done;
7602 ad493afc 2019-08-03 stsp
7603 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7604 2e1f37b0 2019-08-08 stsp if (err)
7605 2e1f37b0 2019-08-08 stsp goto done;
7606 2e1f37b0 2019-08-08 stsp
7607 fe621944 2020-11-10 stsp err = got_diff_files(&diffreg_result, f1, label1, f2,
7608 64453f7e 2020-11-21 stsp path2, 3, 0, 1, NULL);
7609 2e1f37b0 2019-08-08 stsp if (err)
7610 2e1f37b0 2019-08-08 stsp goto done;
7611 2e1f37b0 2019-08-08 stsp
7612 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7613 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7614 2e1f37b0 2019-08-08 stsp if (err)
7615 2e1f37b0 2019-08-08 stsp goto done;
7616 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7617 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7618 2e1f37b0 2019-08-08 stsp if (err)
7619 2e1f37b0 2019-08-08 stsp goto done;
7620 2e1f37b0 2019-08-08 stsp
7621 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7622 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7623 2e1f37b0 2019-08-08 stsp goto done;
7624 2e1f37b0 2019-08-08 stsp }
7625 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7626 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7627 2e1f37b0 2019-08-08 stsp goto done;
7628 2e1f37b0 2019-08-08 stsp }
7629 fe621944 2020-11-10 stsp /* Count the number of actual changes in the diff result. */
7630 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7631 fe621944 2020-11-10 stsp struct diff_chunk_context cc = {};
7632 fe621944 2020-11-10 stsp diff_chunk_context_load_change(&cc, &nchunks_used,
7633 fe621944 2020-11-10 stsp diffreg_result->result, n, 0);
7634 fe621944 2020-11-10 stsp nchanges++;
7635 fe621944 2020-11-10 stsp }
7636 fe621944 2020-11-10 stsp for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7637 2e1f37b0 2019-08-08 stsp int choice;
7638 fe621944 2020-11-10 stsp err = apply_or_reject_change(&choice, &nchunks_used,
7639 fe621944 2020-11-10 stsp diffreg_result->result, n, relpath, f1, f2,
7640 fe621944 2020-11-10 stsp &line_cur1, &line_cur2,
7641 fe621944 2020-11-10 stsp outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7642 2e1f37b0 2019-08-08 stsp if (err)
7643 2e1f37b0 2019-08-08 stsp goto done;
7644 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7645 2e1f37b0 2019-08-08 stsp have_content = 1;
7646 19e4b907 2019-08-08 stsp else
7647 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7648 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7649 2e1f37b0 2019-08-08 stsp break;
7650 2e1f37b0 2019-08-08 stsp }
7651 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7652 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7653 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7654 2e1f37b0 2019-08-08 stsp done:
7655 2e1f37b0 2019-08-08 stsp free(label1);
7656 2e1f37b0 2019-08-08 stsp if (blob)
7657 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7658 2e1f37b0 2019-08-08 stsp if (staged_blob)
7659 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7660 fe621944 2020-11-10 stsp free_err = got_diffreg_result_free(diffreg_result);
7661 fe621944 2020-11-10 stsp if (free_err && err == NULL)
7662 fe621944 2020-11-10 stsp err = free_err;
7663 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7664 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7665 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7666 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7667 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7668 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7669 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7670 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7671 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7672 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7673 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7674 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7675 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7676 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7677 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7678 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7679 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7680 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7681 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7682 2e1f37b0 2019-08-08 stsp }
7683 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7684 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7685 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7686 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7687 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7688 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7689 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7690 2e1f37b0 2019-08-08 stsp }
7691 2e1f37b0 2019-08-08 stsp free(path1);
7692 2e1f37b0 2019-08-08 stsp free(path2);
7693 fda8017d 2020-07-23 stsp return err;
7694 fda8017d 2020-07-23 stsp }
7695 fda8017d 2020-07-23 stsp
7696 fda8017d 2020-07-23 stsp static const struct got_error *
7697 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7698 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7699 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7700 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7701 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7702 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7703 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7704 fda8017d 2020-07-23 stsp {
7705 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7706 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7707 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7708 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7709 fda8017d 2020-07-23 stsp FILE *f = NULL;
7710 fda8017d 2020-07-23 stsp struct stat sb;
7711 fda8017d 2020-07-23 stsp
7712 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7713 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7714 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7715 fda8017d 2020-07-23 stsp if (err)
7716 fda8017d 2020-07-23 stsp return err;
7717 fda8017d 2020-07-23 stsp
7718 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7719 fda8017d 2020-07-23 stsp return NULL;
7720 fda8017d 2020-07-23 stsp
7721 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7722 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7723 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7724 fda8017d 2020-07-23 stsp if (err)
7725 fda8017d 2020-07-23 stsp goto done;
7726 fda8017d 2020-07-23 stsp }
7727 fda8017d 2020-07-23 stsp
7728 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7729 fda8017d 2020-07-23 stsp if (f == NULL) {
7730 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7731 fda8017d 2020-07-23 stsp path_unstaged_content);
7732 fda8017d 2020-07-23 stsp goto done;
7733 fda8017d 2020-07-23 stsp }
7734 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7735 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7736 fda8017d 2020-07-23 stsp goto done;
7737 fda8017d 2020-07-23 stsp }
7738 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7739 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7740 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7741 fda8017d 2020-07-23 stsp size_t r;
7742 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7743 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7744 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7745 fda8017d 2020-07-23 stsp goto done;
7746 fda8017d 2020-07-23 stsp }
7747 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7748 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7749 fda8017d 2020-07-23 stsp goto done;
7750 fda8017d 2020-07-23 stsp }
7751 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7752 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7753 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7754 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7755 fda8017d 2020-07-23 stsp progress_arg);
7756 fda8017d 2020-07-23 stsp } else {
7757 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7758 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7759 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7760 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7761 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7762 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7763 fda8017d 2020-07-23 stsp }
7764 fda8017d 2020-07-23 stsp if (err)
7765 fda8017d 2020-07-23 stsp goto done;
7766 fda8017d 2020-07-23 stsp
7767 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7768 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7769 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7770 2ac8aa02 2020-11-09 stsp } else {
7771 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7772 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7773 2ac8aa02 2020-11-09 stsp }
7774 fda8017d 2020-07-23 stsp done:
7775 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7776 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7777 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7778 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7779 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7780 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7781 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7782 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
7783 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7784 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7785 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7786 2e1f37b0 2019-08-08 stsp return err;
7787 2e1f37b0 2019-08-08 stsp }
7788 2e1f37b0 2019-08-08 stsp
7789 ad493afc 2019-08-03 stsp static const struct got_error *
7790 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7791 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7792 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7793 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7794 ad493afc 2019-08-03 stsp {
7795 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7796 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7797 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7798 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7799 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7800 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7801 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7802 9bc94a15 2019-08-03 stsp struct stat sb;
7803 ad493afc 2019-08-03 stsp
7804 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7805 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7806 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7807 2e1f37b0 2019-08-08 stsp return NULL;
7808 2e1f37b0 2019-08-08 stsp
7809 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7810 ad493afc 2019-08-03 stsp if (ie == NULL)
7811 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7812 9bc94a15 2019-08-03 stsp
7813 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7814 9bc94a15 2019-08-03 stsp == -1)
7815 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7816 ad493afc 2019-08-03 stsp
7817 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7818 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7819 f69721c3 2019-10-21 stsp if (err)
7820 f69721c3 2019-10-21 stsp goto done;
7821 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7822 f69721c3 2019-10-21 stsp id_str) == -1) {
7823 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7824 f69721c3 2019-10-21 stsp goto done;
7825 f69721c3 2019-10-21 stsp }
7826 f69721c3 2019-10-21 stsp
7827 ad493afc 2019-08-03 stsp switch (staged_status) {
7828 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7829 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7830 ad493afc 2019-08-03 stsp blob_id, 8192);
7831 ad493afc 2019-08-03 stsp if (err)
7832 ad493afc 2019-08-03 stsp break;
7833 ad493afc 2019-08-03 stsp /* fall through */
7834 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7835 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7836 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7837 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7838 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7839 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7840 2e1f37b0 2019-08-08 stsp if (err)
7841 2e1f37b0 2019-08-08 stsp break;
7842 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7843 2e1f37b0 2019-08-08 stsp break;
7844 2e1f37b0 2019-08-08 stsp } else {
7845 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7846 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7847 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7848 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7849 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7850 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7851 2e1f37b0 2019-08-08 stsp }
7852 2e1f37b0 2019-08-08 stsp }
7853 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7854 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7855 ad493afc 2019-08-03 stsp if (err)
7856 ad493afc 2019-08-03 stsp break;
7857 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7858 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7859 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7860 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7861 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7862 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7863 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7864 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7865 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7866 ea7786be 2020-07-23 stsp break;
7867 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7868 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7869 36bf999c 2020-07-23 stsp char *staged_target;
7870 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7871 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7872 36bf999c 2020-07-23 stsp if (err)
7873 36bf999c 2020-07-23 stsp goto done;
7874 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7875 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7876 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7877 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7878 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7879 36bf999c 2020-07-23 stsp free(staged_target);
7880 dfe9fba0 2020-07-23 stsp } else {
7881 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7882 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7883 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7884 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7885 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7886 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7887 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7888 dfe9fba0 2020-07-23 stsp }
7889 ea7786be 2020-07-23 stsp break;
7890 ea7786be 2020-07-23 stsp default:
7891 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7892 ea7786be 2020-07-23 stsp break;
7893 ea7786be 2020-07-23 stsp }
7894 2ac8aa02 2020-11-09 stsp if (err == NULL) {
7895 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7896 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7897 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7898 2ac8aa02 2020-11-09 stsp }
7899 ad493afc 2019-08-03 stsp break;
7900 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7901 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7902 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7903 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7904 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7905 2e1f37b0 2019-08-08 stsp if (err)
7906 2e1f37b0 2019-08-08 stsp break;
7907 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7908 2e1f37b0 2019-08-08 stsp break;
7909 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7910 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7911 2e1f37b0 2019-08-08 stsp break;
7912 2e1f37b0 2019-08-08 stsp }
7913 2e1f37b0 2019-08-08 stsp }
7914 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7915 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7916 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7917 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7918 9bc94a15 2019-08-03 stsp if (err)
7919 9bc94a15 2019-08-03 stsp break;
7920 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7921 ad493afc 2019-08-03 stsp break;
7922 ad493afc 2019-08-03 stsp }
7923 f69721c3 2019-10-21 stsp done:
7924 ad493afc 2019-08-03 stsp free(ondisk_path);
7925 ad493afc 2019-08-03 stsp if (blob_base)
7926 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7927 ad493afc 2019-08-03 stsp if (blob_staged)
7928 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7929 f69721c3 2019-10-21 stsp free(id_str);
7930 f69721c3 2019-10-21 stsp free(label_orig);
7931 ad493afc 2019-08-03 stsp return err;
7932 ad493afc 2019-08-03 stsp }
7933 ad493afc 2019-08-03 stsp
7934 ad493afc 2019-08-03 stsp const struct got_error *
7935 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7936 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7937 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7938 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7939 ad493afc 2019-08-03 stsp struct got_repository *repo)
7940 ad493afc 2019-08-03 stsp {
7941 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7942 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7943 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7944 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7945 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7946 ad493afc 2019-08-03 stsp
7947 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7948 ad493afc 2019-08-03 stsp if (err)
7949 ad493afc 2019-08-03 stsp return err;
7950 ad493afc 2019-08-03 stsp
7951 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7952 ad493afc 2019-08-03 stsp if (err)
7953 ad493afc 2019-08-03 stsp goto done;
7954 ad493afc 2019-08-03 stsp
7955 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7956 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7957 ad493afc 2019-08-03 stsp upa.repo = repo;
7958 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7959 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7960 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7961 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7962 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7963 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7964 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7965 ad493afc 2019-08-03 stsp if (err)
7966 ad493afc 2019-08-03 stsp goto done;
7967 ad493afc 2019-08-03 stsp }
7968 ad493afc 2019-08-03 stsp
7969 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7970 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7971 ad493afc 2019-08-03 stsp err = sync_err;
7972 ad493afc 2019-08-03 stsp done:
7973 ad493afc 2019-08-03 stsp free(fileindex_path);
7974 ad493afc 2019-08-03 stsp if (fileindex)
7975 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7976 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7977 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7978 ad493afc 2019-08-03 stsp err = unlockerr;
7979 ad493afc 2019-08-03 stsp return err;
7980 ad493afc 2019-08-03 stsp }
7981 b2118c49 2020-07-28 stsp
7982 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7983 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7984 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7985 b2118c49 2020-07-28 stsp void *info_arg;
7986 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7987 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7988 b2118c49 2020-07-28 stsp void *cancel_arg;
7989 b2118c49 2020-07-28 stsp };
7990 b2118c49 2020-07-28 stsp
7991 b2118c49 2020-07-28 stsp static const struct got_error *
7992 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7993 b2118c49 2020-07-28 stsp {
7994 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7995 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7996 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7997 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7998 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7999 b2118c49 2020-07-28 stsp int stage;
8000 b2118c49 2020-07-28 stsp
8001 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8002 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
8003 b2118c49 2020-07-28 stsp
8004 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
8005 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8006 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
8007 b2118c49 2020-07-28 stsp break;
8008 b2118c49 2020-07-28 stsp }
8009 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
8010 b2118c49 2020-07-28 stsp return NULL;
8011 b2118c49 2020-07-28 stsp
8012 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
8013 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8014 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
8015 b2118c49 2020-07-28 stsp }
8016 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
8017 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8018 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
8019 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8020 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
8021 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
8022 b2118c49 2020-07-28 stsp }
8023 b2118c49 2020-07-28 stsp
8024 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
8025 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8026 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
8027 b2118c49 2020-07-28 stsp }
8028 b2118c49 2020-07-28 stsp
8029 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8030 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8031 b2118c49 2020-07-28 stsp }
8032 b2118c49 2020-07-28 stsp
8033 b2118c49 2020-07-28 stsp const struct got_error *
8034 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
8035 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
8036 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
8037 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
8038 b2118c49 2020-07-28 stsp
8039 b2118c49 2020-07-28 stsp {
8040 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
8041 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
8042 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
8043 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
8044 b2118c49 2020-07-28 stsp
8045 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
8046 b2118c49 2020-07-28 stsp if (err)
8047 b2118c49 2020-07-28 stsp return err;
8048 b2118c49 2020-07-28 stsp
8049 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
8050 b2118c49 2020-07-28 stsp if (err)
8051 b2118c49 2020-07-28 stsp goto done;
8052 b2118c49 2020-07-28 stsp
8053 b2118c49 2020-07-28 stsp arg.worktree = worktree;
8054 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
8055 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
8056 b2118c49 2020-07-28 stsp arg.paths = paths;
8057 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
8058 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
8059 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8060 b2118c49 2020-07-28 stsp &arg);
8061 b2118c49 2020-07-28 stsp done:
8062 b2118c49 2020-07-28 stsp free(fileindex_path);
8063 b2118c49 2020-07-28 stsp if (fileindex)
8064 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
8065 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
8066 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
8067 b2118c49 2020-07-28 stsp err = unlockerr;
8068 b2118c49 2020-07-28 stsp return err;
8069 b2118c49 2020-07-28 stsp }