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 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && 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 6d9d28c3 2018-03-11 stsp done:
433 eaccb85f 2018-12-25 stsp if (repo)
434 eaccb85f 2018-12-25 stsp got_repo_close(repo);
435 7ac97322 2018-03-11 stsp free(path_got);
436 056e7441 2018-03-11 stsp free(path_lock);
437 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
438 c442a90d 2019-03-10 stsp free(uuidstr);
439 bd165944 2019-03-10 stsp free(formatstr);
440 6d9d28c3 2018-03-11 stsp if (err) {
441 6d9d28c3 2018-03-11 stsp if (fd != -1)
442 6d9d28c3 2018-03-11 stsp close(fd);
443 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
444 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
445 6d9d28c3 2018-03-11 stsp *worktree = NULL;
446 6d9d28c3 2018-03-11 stsp } else
447 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
448 6d9d28c3 2018-03-11 stsp
449 6d9d28c3 2018-03-11 stsp return err;
450 86c3caaf 2018-03-09 stsp }
451 247140b2 2019-02-05 stsp
452 247140b2 2019-02-05 stsp const struct got_error *
453 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
454 247140b2 2019-02-05 stsp {
455 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
456 86c3caaf 2018-03-09 stsp
457 247140b2 2019-02-05 stsp do {
458 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
459 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
460 247140b2 2019-02-05 stsp return err;
461 247140b2 2019-02-05 stsp if (*worktree)
462 247140b2 2019-02-05 stsp return NULL;
463 247140b2 2019-02-05 stsp path = dirname(path);
464 d1542a27 2019-02-05 stsp if (path == NULL)
465 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
466 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
467 247140b2 2019-02-05 stsp
468 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
469 247140b2 2019-02-05 stsp }
470 247140b2 2019-02-05 stsp
471 3a6ce05a 2019-02-11 stsp const struct got_error *
472 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
473 86c3caaf 2018-03-09 stsp {
474 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
475 cde76477 2018-03-11 stsp free(worktree->repo_path);
476 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
477 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
478 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
479 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
480 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
481 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
482 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
483 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
484 50b0790e 2020-09-11 stsp free(worktree->gotconfig_path);
485 50b0790e 2020-09-11 stsp got_gotconfig_free(worktree->gotconfig);
486 6d9d28c3 2018-03-11 stsp free(worktree);
487 3a6ce05a 2019-02-11 stsp return err;
488 86c3caaf 2018-03-09 stsp }
489 86c3caaf 2018-03-09 stsp
490 2fbdb5ae 2018-12-29 stsp const char *
491 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
492 c7f4312f 2019-02-05 stsp {
493 c7f4312f 2019-02-05 stsp return worktree->root_path;
494 c7f4312f 2019-02-05 stsp }
495 c7f4312f 2019-02-05 stsp
496 c7f4312f 2019-02-05 stsp const char *
497 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
498 86c3caaf 2018-03-09 stsp {
499 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
500 49520a32 2018-12-29 stsp }
501 49520a32 2018-12-29 stsp const char *
502 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
503 49520a32 2018-12-29 stsp {
504 f609be2e 2018-12-29 stsp return worktree->path_prefix;
505 e5dc7198 2018-12-29 stsp }
506 e5dc7198 2018-12-29 stsp
507 e5dc7198 2018-12-29 stsp const struct got_error *
508 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 e5dc7198 2018-12-29 stsp const char *path_prefix)
510 e5dc7198 2018-12-29 stsp {
511 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
512 e5dc7198 2018-12-29 stsp
513 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
514 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
516 e5dc7198 2018-12-29 stsp }
517 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
518 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
519 e5dc7198 2018-12-29 stsp free(absprefix);
520 e5dc7198 2018-12-29 stsp return NULL;
521 86c3caaf 2018-03-09 stsp }
522 86c3caaf 2018-03-09 stsp
523 bc70eb79 2019-05-09 stsp const char *
524 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
525 86c3caaf 2018-03-09 stsp {
526 36a38700 2019-05-10 stsp return worktree->head_ref_name;
527 024e9686 2019-05-14 stsp }
528 024e9686 2019-05-14 stsp
529 024e9686 2019-05-14 stsp const struct got_error *
530 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
531 024e9686 2019-05-14 stsp struct got_reference *head_ref)
532 024e9686 2019-05-14 stsp {
533 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
534 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
535 024e9686 2019-05-14 stsp
536 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
538 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
539 024e9686 2019-05-14 stsp path_got = NULL;
540 024e9686 2019-05-14 stsp goto done;
541 024e9686 2019-05-14 stsp }
542 024e9686 2019-05-14 stsp
543 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
544 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
545 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
546 024e9686 2019-05-14 stsp goto done;
547 024e9686 2019-05-14 stsp }
548 024e9686 2019-05-14 stsp
549 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
550 024e9686 2019-05-14 stsp if (err)
551 024e9686 2019-05-14 stsp goto done;
552 024e9686 2019-05-14 stsp
553 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
554 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
555 024e9686 2019-05-14 stsp done:
556 024e9686 2019-05-14 stsp free(path_got);
557 024e9686 2019-05-14 stsp if (err)
558 024e9686 2019-05-14 stsp free(head_ref_name);
559 024e9686 2019-05-14 stsp return err;
560 507dc3bb 2018-12-29 stsp }
561 507dc3bb 2018-12-29 stsp
562 b72f483a 2019-02-05 stsp struct got_object_id *
563 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
564 507dc3bb 2018-12-29 stsp {
565 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
566 86c3caaf 2018-03-09 stsp }
567 86c3caaf 2018-03-09 stsp
568 507dc3bb 2018-12-29 stsp const struct got_error *
569 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
571 507dc3bb 2018-12-29 stsp {
572 507dc3bb 2018-12-29 stsp const struct got_error *err;
573 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
574 507dc3bb 2018-12-29 stsp char *id_str = NULL;
575 507dc3bb 2018-12-29 stsp char *path_got = NULL;
576 507dc3bb 2018-12-29 stsp
577 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
579 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
580 507dc3bb 2018-12-29 stsp path_got = NULL;
581 507dc3bb 2018-12-29 stsp goto done;
582 507dc3bb 2018-12-29 stsp }
583 507dc3bb 2018-12-29 stsp
584 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
585 507dc3bb 2018-12-29 stsp if (err)
586 507dc3bb 2018-12-29 stsp return err;
587 507dc3bb 2018-12-29 stsp
588 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
590 507dc3bb 2018-12-29 stsp goto done;
591 507dc3bb 2018-12-29 stsp }
592 507dc3bb 2018-12-29 stsp
593 507dc3bb 2018-12-29 stsp /* Record our base commit. */
594 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
595 507dc3bb 2018-12-29 stsp if (err)
596 507dc3bb 2018-12-29 stsp goto done;
597 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
598 507dc3bb 2018-12-29 stsp if (err)
599 507dc3bb 2018-12-29 stsp goto done;
600 507dc3bb 2018-12-29 stsp
601 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
602 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
603 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
604 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
605 507dc3bb 2018-12-29 stsp goto done;
606 507dc3bb 2018-12-29 stsp }
607 507dc3bb 2018-12-29 stsp done:
608 507dc3bb 2018-12-29 stsp if (obj)
609 507dc3bb 2018-12-29 stsp got_object_close(obj);
610 507dc3bb 2018-12-29 stsp free(id_str);
611 507dc3bb 2018-12-29 stsp free(path_got);
612 507dc3bb 2018-12-29 stsp return err;
613 50b0790e 2020-09-11 stsp }
614 50b0790e 2020-09-11 stsp
615 50b0790e 2020-09-11 stsp const struct got_gotconfig *
616 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
617 50b0790e 2020-09-11 stsp {
618 50b0790e 2020-09-11 stsp return worktree->gotconfig;
619 507dc3bb 2018-12-29 stsp }
620 507dc3bb 2018-12-29 stsp
621 9d31a1d8 2018-03-11 stsp static const struct got_error *
622 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
623 86c3caaf 2018-03-09 stsp {
624 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
625 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
626 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
627 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
628 86c3caaf 2018-03-09 stsp return NULL;
629 21908da4 2019-01-13 stsp }
630 21908da4 2019-01-13 stsp
631 21908da4 2019-01-13 stsp static const struct got_error *
632 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
633 4a1ddfc2 2019-01-12 stsp {
634 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
635 4a1ddfc2 2019-01-12 stsp char *abspath;
636 4a1ddfc2 2019-01-12 stsp
637 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
638 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
639 4a1ddfc2 2019-01-12 stsp
640 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
641 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
642 ddcd8544 2019-03-11 stsp struct stat sb;
643 ddcd8544 2019-03-11 stsp err = NULL;
644 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
645 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
646 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
647 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
648 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
649 ddcd8544 2019-03-11 stsp }
650 ddcd8544 2019-03-11 stsp }
651 4a1ddfc2 2019-01-12 stsp free(abspath);
652 68c76935 2019-02-19 stsp return err;
653 68c76935 2019-02-19 stsp }
654 68c76935 2019-02-19 stsp
655 68c76935 2019-02-19 stsp static const struct got_error *
656 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
657 68c76935 2019-02-19 stsp {
658 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
659 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
660 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
661 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
662 68c76935 2019-02-19 stsp
663 68c76935 2019-02-19 stsp *same = 1;
664 68c76935 2019-02-19 stsp
665 230a42bd 2019-05-11 jcs for (;;) {
666 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
667 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
668 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
669 68c76935 2019-02-19 stsp break;
670 68c76935 2019-02-19 stsp }
671 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
672 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
673 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
674 68c76935 2019-02-19 stsp break;
675 68c76935 2019-02-19 stsp }
676 68c76935 2019-02-19 stsp if (flen1 == 0) {
677 68c76935 2019-02-19 stsp if (flen2 != 0)
678 68c76935 2019-02-19 stsp *same = 0;
679 68c76935 2019-02-19 stsp break;
680 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
681 68c76935 2019-02-19 stsp if (flen1 != 0)
682 68c76935 2019-02-19 stsp *same = 0;
683 68c76935 2019-02-19 stsp break;
684 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
685 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
686 68c76935 2019-02-19 stsp *same = 0;
687 68c76935 2019-02-19 stsp break;
688 68c76935 2019-02-19 stsp }
689 68c76935 2019-02-19 stsp } else {
690 68c76935 2019-02-19 stsp *same = 0;
691 68c76935 2019-02-19 stsp break;
692 68c76935 2019-02-19 stsp }
693 68c76935 2019-02-19 stsp }
694 68c76935 2019-02-19 stsp
695 68c76935 2019-02-19 stsp return err;
696 68c76935 2019-02-19 stsp }
697 68c76935 2019-02-19 stsp
698 68c76935 2019-02-19 stsp static const struct got_error *
699 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
700 68c76935 2019-02-19 stsp {
701 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
702 68c76935 2019-02-19 stsp struct stat sb;
703 68c76935 2019-02-19 stsp size_t size1, size2;
704 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
705 68c76935 2019-02-19 stsp
706 68c76935 2019-02-19 stsp *same = 1;
707 68c76935 2019-02-19 stsp
708 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
709 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
710 68c76935 2019-02-19 stsp goto done;
711 68c76935 2019-02-19 stsp }
712 68c76935 2019-02-19 stsp size1 = sb.st_size;
713 68c76935 2019-02-19 stsp
714 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
715 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
716 68c76935 2019-02-19 stsp goto done;
717 68c76935 2019-02-19 stsp }
718 68c76935 2019-02-19 stsp size2 = sb.st_size;
719 68c76935 2019-02-19 stsp
720 68c76935 2019-02-19 stsp if (size1 != size2) {
721 68c76935 2019-02-19 stsp *same = 0;
722 68c76935 2019-02-19 stsp return NULL;
723 68c76935 2019-02-19 stsp }
724 68c76935 2019-02-19 stsp
725 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
726 68c76935 2019-02-19 stsp if (f1 == NULL)
727 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
728 68c76935 2019-02-19 stsp
729 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
730 68c76935 2019-02-19 stsp if (f2 == NULL) {
731 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
732 68c76935 2019-02-19 stsp goto done;
733 68c76935 2019-02-19 stsp }
734 68c76935 2019-02-19 stsp
735 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
736 68c76935 2019-02-19 stsp done:
737 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
738 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
739 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
740 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
741 68c76935 2019-02-19 stsp
742 6353ad76 2019-02-08 stsp return err;
743 6353ad76 2019-02-08 stsp }
744 6353ad76 2019-02-08 stsp
745 6353ad76 2019-02-08 stsp /*
746 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
747 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
748 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
749 6353ad76 2019-02-08 stsp */
750 6353ad76 2019-02-08 stsp static const struct got_error *
751 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
752 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
753 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
754 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
755 f69721c3 2019-10-21 stsp struct got_repository *repo,
756 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
757 6353ad76 2019-02-08 stsp {
758 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
759 6353ad76 2019-02-08 stsp int merged_fd = -1;
760 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
761 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
762 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
763 234035bc 2019-06-01 stsp int overlapcnt = 0;
764 af54ae4a 2019-02-19 stsp char *parent;
765 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
766 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
767 6353ad76 2019-02-08 stsp
768 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
769 234035bc 2019-06-01 stsp
770 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
771 af54ae4a 2019-02-19 stsp if (parent == NULL)
772 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
773 af54ae4a 2019-02-19 stsp
774 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
775 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
776 af54ae4a 2019-02-19 stsp
777 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
778 6353ad76 2019-02-08 stsp if (err)
779 6353ad76 2019-02-08 stsp goto done;
780 6353ad76 2019-02-08 stsp
781 af54ae4a 2019-02-19 stsp free(base_path);
782 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
783 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
784 af54ae4a 2019-02-19 stsp base_path = NULL;
785 af54ae4a 2019-02-19 stsp goto done;
786 af54ae4a 2019-02-19 stsp }
787 af54ae4a 2019-02-19 stsp
788 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
789 6353ad76 2019-02-08 stsp if (err)
790 6353ad76 2019-02-08 stsp goto done;
791 46b6ee73 2019-06-04 stsp if (blob_orig) {
792 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
793 46b6ee73 2019-06-04 stsp blob_orig);
794 1430b4e0 2019-03-27 stsp if (err)
795 1430b4e0 2019-03-27 stsp goto done;
796 1430b4e0 2019-03-27 stsp } else {
797 1430b4e0 2019-03-27 stsp /*
798 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
799 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
800 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
801 1430b4e0 2019-03-27 stsp */
802 1430b4e0 2019-03-27 stsp }
803 6353ad76 2019-02-08 stsp
804 3b9f0f87 2020-07-23 stsp /*
805 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
806 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
807 3b9f0f87 2020-07-23 stsp */
808 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
809 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
810 3b9f0f87 2020-07-23 stsp ssize_t target_len;
811 3b9f0f87 2020-07-23 stsp size_t n;
812 3b9f0f87 2020-07-23 stsp
813 3b9f0f87 2020-07-23 stsp free(base_path);
814 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
815 3b9f0f87 2020-07-23 stsp parent) == -1) {
816 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
817 3b9f0f87 2020-07-23 stsp base_path = NULL;
818 3b9f0f87 2020-07-23 stsp goto done;
819 3b9f0f87 2020-07-23 stsp }
820 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
821 3b9f0f87 2020-07-23 stsp if (err)
822 3b9f0f87 2020-07-23 stsp goto done;
823 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
824 3b9f0f87 2020-07-23 stsp sizeof(target_path));
825 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
826 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
827 3b9f0f87 2020-07-23 stsp goto done;
828 3b9f0f87 2020-07-23 stsp }
829 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
830 3b9f0f87 2020-07-23 stsp if (n != target_len) {
831 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
832 3b9f0f87 2020-07-23 stsp goto done;
833 3b9f0f87 2020-07-23 stsp }
834 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
835 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
836 3b9f0f87 2020-07-23 stsp goto done;
837 3b9f0f87 2020-07-23 stsp }
838 3b9f0f87 2020-07-23 stsp }
839 3b9f0f87 2020-07-23 stsp
840 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
841 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
842 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
843 6353ad76 2019-02-08 stsp if (err)
844 6353ad76 2019-02-08 stsp goto done;
845 6353ad76 2019-02-08 stsp
846 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
847 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
848 1ee397ad 2019-07-12 stsp if (err)
849 1ee397ad 2019-07-12 stsp goto done;
850 6353ad76 2019-02-08 stsp
851 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
852 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
853 816dc654 2019-02-16 stsp goto done;
854 68c76935 2019-02-19 stsp }
855 68c76935 2019-02-19 stsp
856 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
857 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
858 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
859 68c76935 2019-02-19 stsp merged_path);
860 68c76935 2019-02-19 stsp if (err)
861 68c76935 2019-02-19 stsp goto done;
862 816dc654 2019-02-16 stsp }
863 6353ad76 2019-02-08 stsp
864 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
865 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
866 70a0c8ec 2019-02-20 stsp goto done;
867 70a0c8ec 2019-02-20 stsp }
868 70a0c8ec 2019-02-20 stsp
869 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
870 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
871 230a42bd 2019-05-11 jcs ondisk_path);
872 6353ad76 2019-02-08 stsp goto done;
873 6353ad76 2019-02-08 stsp }
874 6353ad76 2019-02-08 stsp done:
875 fdcb7daf 2019-12-15 stsp if (err) {
876 fdcb7daf 2019-12-15 stsp if (merged_path)
877 fdcb7daf 2019-12-15 stsp unlink(merged_path);
878 3b9f0f87 2020-07-23 stsp }
879 3b9f0f87 2020-07-23 stsp if (symlink_path) {
880 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
881 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
882 fdcb7daf 2019-12-15 stsp }
883 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
884 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
885 3b9f0f87 2020-07-23 stsp free(symlink_path);
886 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
887 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
888 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
889 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
890 6353ad76 2019-02-08 stsp free(merged_path);
891 af54ae4a 2019-02-19 stsp free(base_path);
892 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
893 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
894 46b6ee73 2019-06-04 stsp free(blob_orig_path);
895 af57b12a 2020-07-23 stsp }
896 af57b12a 2020-07-23 stsp return err;
897 af57b12a 2020-07-23 stsp }
898 af57b12a 2020-07-23 stsp
899 af57b12a 2020-07-23 stsp static const struct got_error *
900 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
901 af57b12a 2020-07-23 stsp size_t target_len)
902 af57b12a 2020-07-23 stsp {
903 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
904 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
905 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
906 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
907 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
908 af57b12a 2020-07-23 stsp ondisk_path);
909 af57b12a 2020-07-23 stsp return NULL;
910 af57b12a 2020-07-23 stsp }
911 af57b12a 2020-07-23 stsp
912 af57b12a 2020-07-23 stsp /*
913 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
914 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
915 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
916 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
917 993e2a1b 2020-07-23 stsp *
918 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
919 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
920 993e2a1b 2020-07-23 stsp *
921 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
922 993e2a1b 2020-07-23 stsp * has deleted this symlink.
923 11cc08c1 2020-07-23 stsp */
924 11cc08c1 2020-07-23 stsp static const struct got_error *
925 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
926 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
927 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
928 11cc08c1 2020-07-23 stsp {
929 11cc08c1 2020-07-23 stsp const struct got_error *err;
930 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
931 11cc08c1 2020-07-23 stsp FILE *f = NULL;
932 11cc08c1 2020-07-23 stsp
933 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
934 11cc08c1 2020-07-23 stsp if (err)
935 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
936 11cc08c1 2020-07-23 stsp
937 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
938 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
939 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
940 11cc08c1 2020-07-23 stsp goto done;
941 11cc08c1 2020-07-23 stsp }
942 11cc08c1 2020-07-23 stsp
943 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
944 11cc08c1 2020-07-23 stsp if (err)
945 11cc08c1 2020-07-23 stsp goto done;
946 11cc08c1 2020-07-23 stsp
947 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
948 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
949 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
950 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
951 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
952 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
953 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
954 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
955 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
956 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
957 11cc08c1 2020-07-23 stsp goto done;
958 11cc08c1 2020-07-23 stsp }
959 11cc08c1 2020-07-23 stsp
960 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
961 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
962 11cc08c1 2020-07-23 stsp goto done;
963 11cc08c1 2020-07-23 stsp }
964 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
965 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
966 11cc08c1 2020-07-23 stsp goto done;
967 11cc08c1 2020-07-23 stsp }
968 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
969 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
970 11cc08c1 2020-07-23 stsp goto done;
971 11cc08c1 2020-07-23 stsp }
972 11cc08c1 2020-07-23 stsp done:
973 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
974 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
975 11cc08c1 2020-07-23 stsp free(path);
976 11cc08c1 2020-07-23 stsp free(id_str);
977 11cc08c1 2020-07-23 stsp free(label_deriv);
978 11cc08c1 2020-07-23 stsp return err;
979 11cc08c1 2020-07-23 stsp }
980 d219f183 2020-07-23 stsp
981 d219f183 2020-07-23 stsp /* forward declaration */
982 d219f183 2020-07-23 stsp static const struct got_error *
983 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
984 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
985 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
986 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
987 11cc08c1 2020-07-23 stsp
988 11cc08c1 2020-07-23 stsp /*
989 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
990 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
991 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
992 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
993 af57b12a 2020-07-23 stsp */
994 af57b12a 2020-07-23 stsp static const struct got_error *
995 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
996 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
997 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
998 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
999 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1000 af57b12a 2020-07-23 stsp {
1001 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1002 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1003 af57b12a 2020-07-23 stsp struct stat sb;
1004 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1005 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1006 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1007 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1008 af57b12a 2020-07-23 stsp
1009 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1010 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1011 af57b12a 2020-07-23 stsp
1012 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1013 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1014 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1015 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1016 af57b12a 2020-07-23 stsp ondisk_path);
1017 af57b12a 2020-07-23 stsp goto done;
1018 af57b12a 2020-07-23 stsp }
1019 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1020 af57b12a 2020-07-23 stsp
1021 526a746f 2020-07-23 stsp if (blob_orig) {
1022 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1023 526a746f 2020-07-23 stsp if (err)
1024 526a746f 2020-07-23 stsp goto done;
1025 526a746f 2020-07-23 stsp }
1026 af57b12a 2020-07-23 stsp
1027 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1028 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1029 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1030 11cc08c1 2020-07-23 stsp have_local_change = 1;
1031 11cc08c1 2020-07-23 stsp
1032 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1033 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1034 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1035 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1036 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1037 11cc08c1 2020-07-23 stsp
1038 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1039 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1040 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1041 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1042 11cc08c1 2020-07-23 stsp path);
1043 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1044 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1045 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1046 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1047 11cc08c1 2020-07-23 stsp path);
1048 11cc08c1 2020-07-23 stsp } else {
1049 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1050 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1051 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1052 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1053 11cc08c1 2020-07-23 stsp if (err)
1054 11cc08c1 2020-07-23 stsp goto done;
1055 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1056 11cc08c1 2020-07-23 stsp path);
1057 11cc08c1 2020-07-23 stsp }
1058 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1059 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1060 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1061 af57b12a 2020-07-23 stsp strlen(deriv_target));
1062 af57b12a 2020-07-23 stsp if (err)
1063 af57b12a 2020-07-23 stsp goto done;
1064 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1065 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1066 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1067 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1068 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1069 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1070 ea7786be 2020-07-23 stsp path);
1071 ea7786be 2020-07-23 stsp } else {
1072 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1073 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1074 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1075 ea7786be 2020-07-23 stsp if (err)
1076 ea7786be 2020-07-23 stsp goto done;
1077 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1078 ea7786be 2020-07-23 stsp path);
1079 ea7786be 2020-07-23 stsp }
1080 6353ad76 2019-02-08 stsp }
1081 11cc08c1 2020-07-23 stsp
1082 af57b12a 2020-07-23 stsp done:
1083 af57b12a 2020-07-23 stsp free(ancestor_target);
1084 14c901f1 2019-08-08 stsp return err;
1085 14c901f1 2019-08-08 stsp }
1086 14c901f1 2019-08-08 stsp
1087 14c901f1 2019-08-08 stsp /*
1088 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1089 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1090 14c901f1 2019-08-08 stsp * acts as the second derived version.
1091 14c901f1 2019-08-08 stsp */
1092 14c901f1 2019-08-08 stsp static const struct got_error *
1093 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1094 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1095 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1096 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1097 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1098 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1099 14c901f1 2019-08-08 stsp {
1100 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1101 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1102 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1103 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
1104 14c901f1 2019-08-08 stsp
1105 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1106 14c901f1 2019-08-08 stsp
1107 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
1108 14c901f1 2019-08-08 stsp if (parent == NULL)
1109 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
1110 14c901f1 2019-08-08 stsp
1111 14c901f1 2019-08-08 stsp free(base_path);
1112 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1113 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1114 14c901f1 2019-08-08 stsp base_path = NULL;
1115 14c901f1 2019-08-08 stsp goto done;
1116 14c901f1 2019-08-08 stsp }
1117 14c901f1 2019-08-08 stsp
1118 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1119 14c901f1 2019-08-08 stsp if (err)
1120 14c901f1 2019-08-08 stsp goto done;
1121 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1122 14c901f1 2019-08-08 stsp blob_deriv);
1123 14c901f1 2019-08-08 stsp if (err)
1124 14c901f1 2019-08-08 stsp goto done;
1125 14c901f1 2019-08-08 stsp
1126 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1127 14c901f1 2019-08-08 stsp if (err)
1128 14c901f1 2019-08-08 stsp goto done;
1129 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1130 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1131 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1132 14c901f1 2019-08-08 stsp goto done;
1133 14c901f1 2019-08-08 stsp }
1134 14c901f1 2019-08-08 stsp
1135 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1136 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1137 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1138 14c901f1 2019-08-08 stsp done:
1139 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1140 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1141 14c901f1 2019-08-08 stsp free(base_path);
1142 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1143 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1144 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1145 14c901f1 2019-08-08 stsp }
1146 6353ad76 2019-02-08 stsp free(id_str);
1147 818c7501 2019-07-11 stsp free(label_deriv);
1148 4a1ddfc2 2019-01-12 stsp return err;
1149 4a1ddfc2 2019-01-12 stsp }
1150 4a1ddfc2 2019-01-12 stsp
1151 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1152 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1153 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1154 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1155 13d9040b 2019-03-27 stsp {
1156 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1157 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1158 13d9040b 2019-03-27 stsp
1159 65b05cec 2020-07-23 stsp *new_iep = NULL;
1160 65b05cec 2020-07-23 stsp
1161 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1162 054041d0 2020-06-24 stsp if (err)
1163 054041d0 2020-06-24 stsp return err;
1164 054041d0 2020-06-24 stsp
1165 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1166 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1167 054041d0 2020-06-24 stsp if (err)
1168 054041d0 2020-06-24 stsp goto done;
1169 054041d0 2020-06-24 stsp
1170 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1171 054041d0 2020-06-24 stsp done:
1172 054041d0 2020-06-24 stsp if (err)
1173 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1174 65b05cec 2020-07-23 stsp else
1175 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1176 13d9040b 2019-03-27 stsp return err;
1177 1ebedb77 2019-10-19 stsp }
1178 1ebedb77 2019-10-19 stsp
1179 1ebedb77 2019-10-19 stsp static mode_t
1180 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1181 1ebedb77 2019-10-19 stsp {
1182 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1183 1ebedb77 2019-10-19 stsp
1184 1ebedb77 2019-10-19 stsp if (executable) {
1185 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1186 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1187 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1188 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1189 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1190 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1191 1ebedb77 2019-10-19 stsp }
1192 1ebedb77 2019-10-19 stsp
1193 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1194 13d9040b 2019-03-27 stsp }
1195 13d9040b 2019-03-27 stsp
1196 8ba819a3 2020-07-23 stsp /* forward declaration */
1197 13d9040b 2019-03-27 stsp static const struct got_error *
1198 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1199 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1200 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1201 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1202 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1203 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1204 8ba819a3 2020-07-23 stsp
1205 5a1fbc73 2020-07-23 stsp /*
1206 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1207 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1208 5a1fbc73 2020-07-23 stsp */
1209 8ba819a3 2020-07-23 stsp static const struct got_error *
1210 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1211 5a1fbc73 2020-07-23 stsp size_t target_len)
1212 5a1fbc73 2020-07-23 stsp {
1213 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1214 5a1fbc73 2020-07-23 stsp ssize_t elen;
1215 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1216 5a1fbc73 2020-07-23 stsp int fd;
1217 5a1fbc73 2020-07-23 stsp
1218 5a1fbc73 2020-07-23 stsp /*
1219 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1220 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1221 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1222 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1223 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1224 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1225 5a1fbc73 2020-07-23 stsp */
1226 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1227 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1228 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1229 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1230 5a1fbc73 2020-07-23 stsp
1231 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1232 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1233 5a1fbc73 2020-07-23 stsp if (elen == -1)
1234 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1235 5a1fbc73 2020-07-23 stsp
1236 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1237 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1238 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1239 5a1fbc73 2020-07-23 stsp }
1240 5a1fbc73 2020-07-23 stsp
1241 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1242 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1243 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1244 5a1fbc73 2020-07-23 stsp return err;
1245 3c1ec782 2020-07-23 stsp }
1246 3c1ec782 2020-07-23 stsp
1247 3c1ec782 2020-07-23 stsp static const struct got_error *
1248 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1249 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1250 3c1ec782 2020-07-23 stsp {
1251 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1252 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1253 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1254 3c1ec782 2020-07-23 stsp
1255 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1256 3c1ec782 2020-07-23 stsp
1257 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1258 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1259 3c1ec782 2020-07-23 stsp return NULL;
1260 3c1ec782 2020-07-23 stsp }
1261 3c1ec782 2020-07-23 stsp
1262 3c1ec782 2020-07-23 stsp /*
1263 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1264 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1265 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1266 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1267 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1268 3c1ec782 2020-07-23 stsp */
1269 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1270 3c1ec782 2020-07-23 stsp char *abspath;
1271 3c1ec782 2020-07-23 stsp char *parent = dirname(ondisk_path);
1272 3c1ec782 2020-07-23 stsp if (parent == NULL)
1273 3c1ec782 2020-07-23 stsp return got_error_from_errno2("dirname", ondisk_path);
1274 3c1ec782 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1)
1275 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1276 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1277 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1278 3c1ec782 2020-07-23 stsp free(abspath);
1279 3c1ec782 2020-07-23 stsp return err;
1280 3c1ec782 2020-07-23 stsp }
1281 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1282 3c1ec782 2020-07-23 stsp free(abspath);
1283 3c1ec782 2020-07-23 stsp if (err)
1284 3c1ec782 2020-07-23 stsp return err;
1285 3c1ec782 2020-07-23 stsp } else {
1286 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1287 3c1ec782 2020-07-23 stsp if (err)
1288 3c1ec782 2020-07-23 stsp return err;
1289 3c1ec782 2020-07-23 stsp }
1290 3c1ec782 2020-07-23 stsp
1291 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1292 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1293 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1294 3c1ec782 2020-07-23 stsp return NULL;
1295 3c1ec782 2020-07-23 stsp }
1296 3c1ec782 2020-07-23 stsp
1297 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1298 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1299 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1300 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1301 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1302 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1303 3c1ec782 2020-07-23 stsp
1304 3c1ec782 2020-07-23 stsp free(path_got);
1305 3c1ec782 2020-07-23 stsp return NULL;
1306 5a1fbc73 2020-07-23 stsp }
1307 5a1fbc73 2020-07-23 stsp
1308 5a1fbc73 2020-07-23 stsp static const struct got_error *
1309 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1310 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1311 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1312 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1313 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1314 9d31a1d8 2018-03-11 stsp {
1315 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1316 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1317 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1318 906c123b 2020-07-23 stsp char *path_got = NULL;
1319 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1320 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1321 8ba819a3 2020-07-23 stsp
1322 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1323 2e1fa222 2020-07-23 stsp
1324 8ba819a3 2020-07-23 stsp /*
1325 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1326 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1327 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1328 8ba819a3 2020-07-23 stsp * in the blob object.
1329 8ba819a3 2020-07-23 stsp */
1330 8ba819a3 2020-07-23 stsp do {
1331 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1332 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1333 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1334 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1335 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1336 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1337 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1338 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1339 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1340 3b9f0f87 2020-07-23 stsp progress_arg);
1341 8ba819a3 2020-07-23 stsp }
1342 8ba819a3 2020-07-23 stsp if (len > 0) {
1343 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1344 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1345 8ba819a3 2020-07-23 stsp len - hdrlen);
1346 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1347 8ba819a3 2020-07-23 stsp hdrlen = 0;
1348 8ba819a3 2020-07-23 stsp }
1349 8ba819a3 2020-07-23 stsp } while (len != 0);
1350 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1351 8ba819a3 2020-07-23 stsp
1352 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1353 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1354 3c1ec782 2020-07-23 stsp if (err)
1355 3c1ec782 2020-07-23 stsp return err;
1356 8ba819a3 2020-07-23 stsp
1357 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1358 906c123b 2020-07-23 stsp /* install as a regular file */
1359 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1360 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1361 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1362 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1363 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1364 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1365 906c123b 2020-07-23 stsp goto done;
1366 906c123b 2020-07-23 stsp }
1367 906c123b 2020-07-23 stsp
1368 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1369 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1370 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1371 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1372 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1373 c90c8ce3 2020-07-23 stsp goto done;
1374 c90c8ce3 2020-07-23 stsp }
1375 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1376 5a1fbc73 2020-07-23 stsp target_path, target_len);
1377 5a1fbc73 2020-07-23 stsp if (err)
1378 f35fa46a 2020-07-23 stsp goto done;
1379 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1380 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1381 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1382 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1383 6e1eade5 2020-07-23 stsp path);
1384 f35fa46a 2020-07-23 stsp }
1385 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1386 f35fa46a 2020-07-23 stsp }
1387 f35fa46a 2020-07-23 stsp
1388 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1389 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1390 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1391 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1392 f35fa46a 2020-07-23 stsp ondisk_path);
1393 f35fa46a 2020-07-23 stsp goto done;
1394 f35fa46a 2020-07-23 stsp }
1395 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1396 f35fa46a 2020-07-23 stsp if (err)
1397 f35fa46a 2020-07-23 stsp goto done;
1398 f35fa46a 2020-07-23 stsp /*
1399 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1400 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1401 f35fa46a 2020-07-23 stsp */
1402 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1403 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1404 f35fa46a 2020-07-23 stsp goto done;
1405 f35fa46a 2020-07-23 stsp }
1406 f35fa46a 2020-07-23 stsp }
1407 f35fa46a 2020-07-23 stsp
1408 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1409 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1410 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1411 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1412 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1413 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1414 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1415 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1416 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1417 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1418 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1419 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1420 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1421 8ba819a3 2020-07-23 stsp } else {
1422 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1423 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1424 8ba819a3 2020-07-23 stsp }
1425 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1426 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1427 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1428 8ba819a3 2020-07-23 stsp done:
1429 906c123b 2020-07-23 stsp free(path_got);
1430 8ba819a3 2020-07-23 stsp return err;
1431 8ba819a3 2020-07-23 stsp }
1432 8ba819a3 2020-07-23 stsp
1433 8ba819a3 2020-07-23 stsp static const struct got_error *
1434 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1435 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1436 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1437 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1438 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1439 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1440 8ba819a3 2020-07-23 stsp {
1441 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1442 507dc3bb 2018-12-29 stsp int fd = -1;
1443 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1444 507dc3bb 2018-12-29 stsp int update = 0;
1445 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1446 8ba819a3 2020-07-23 stsp
1447 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1448 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1449 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1450 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1451 21908da4 2019-01-13 stsp char *parent = dirname(path);
1452 21908da4 2019-01-13 stsp if (parent == NULL)
1453 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1454 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1455 21908da4 2019-01-13 stsp if (err)
1456 21908da4 2019-01-13 stsp return err;
1457 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1458 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1459 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1460 21908da4 2019-01-13 stsp if (fd == -1)
1461 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1462 230a42bd 2019-05-11 jcs ondisk_path);
1463 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1464 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1465 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1466 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1467 3b9f0f87 2020-07-23 stsp goto done;
1468 3b9f0f87 2020-07-23 stsp }
1469 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1470 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1471 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1472 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1473 507dc3bb 2018-12-29 stsp goto done;
1474 d70b8e30 2018-12-27 stsp } else {
1475 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1476 507dc3bb 2018-12-29 stsp ondisk_path);
1477 507dc3bb 2018-12-29 stsp if (err)
1478 507dc3bb 2018-12-29 stsp goto done;
1479 507dc3bb 2018-12-29 stsp update = 1;
1480 9d31a1d8 2018-03-11 stsp }
1481 507dc3bb 2018-12-29 stsp } else
1482 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1483 9d31a1d8 2018-03-11 stsp }
1484 9d31a1d8 2018-03-11 stsp
1485 bd6aa359 2020-07-23 stsp if (progress_cb) {
1486 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1487 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1488 bd6aa359 2020-07-23 stsp path);
1489 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1490 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1491 bd6aa359 2020-07-23 stsp path);
1492 bd6aa359 2020-07-23 stsp else
1493 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1494 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1495 bd6aa359 2020-07-23 stsp if (err)
1496 bd6aa359 2020-07-23 stsp goto done;
1497 bd6aa359 2020-07-23 stsp }
1498 d7b62c98 2018-12-27 stsp
1499 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1500 9d31a1d8 2018-03-11 stsp do {
1501 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1502 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1503 9d31a1d8 2018-03-11 stsp if (err)
1504 9d31a1d8 2018-03-11 stsp break;
1505 9d31a1d8 2018-03-11 stsp if (len > 0) {
1506 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1507 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1508 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1509 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1510 b87c6f83 2018-12-24 stsp goto done;
1511 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1512 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1513 b87c6f83 2018-12-24 stsp goto done;
1514 9d31a1d8 2018-03-11 stsp }
1515 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1516 9d31a1d8 2018-03-11 stsp }
1517 9d31a1d8 2018-03-11 stsp } while (len != 0);
1518 9d31a1d8 2018-03-11 stsp
1519 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1520 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1521 816dc654 2019-02-16 stsp goto done;
1522 816dc654 2019-02-16 stsp }
1523 9d31a1d8 2018-03-11 stsp
1524 507dc3bb 2018-12-29 stsp if (update) {
1525 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1526 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1527 230a42bd 2019-05-11 jcs ondisk_path);
1528 2a57020b 2019-02-20 stsp unlink(tmppath);
1529 68ed9ba5 2019-02-10 stsp goto done;
1530 68ed9ba5 2019-02-10 stsp }
1531 68ed9ba5 2019-02-10 stsp }
1532 ba8a0d4d 2019-02-10 stsp
1533 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1534 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1535 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1536 1ebedb77 2019-10-19 stsp goto done;
1537 507dc3bb 2018-12-29 stsp }
1538 507dc3bb 2018-12-29 stsp
1539 9d31a1d8 2018-03-11 stsp done:
1540 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1541 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1542 507dc3bb 2018-12-29 stsp free(tmppath);
1543 6353ad76 2019-02-08 stsp return err;
1544 6353ad76 2019-02-08 stsp }
1545 6353ad76 2019-02-08 stsp
1546 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1547 6353ad76 2019-02-08 stsp static const struct got_error *
1548 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1549 7154f6ce 2019-03-27 stsp {
1550 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1551 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1552 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1553 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1554 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1555 7154f6ce 2019-03-27 stsp };
1556 7154f6ce 2019-03-27 stsp int i = 0;
1557 7154f6ce 2019-03-27 stsp char *line;
1558 7154f6ce 2019-03-27 stsp size_t len;
1559 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1560 7154f6ce 2019-03-27 stsp
1561 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1562 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1563 7154f6ce 2019-03-27 stsp if (line == NULL) {
1564 7154f6ce 2019-03-27 stsp if (feof(f))
1565 7154f6ce 2019-03-27 stsp break;
1566 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1567 7154f6ce 2019-03-27 stsp break;
1568 7154f6ce 2019-03-27 stsp }
1569 7154f6ce 2019-03-27 stsp
1570 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1571 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1572 19332e6d 2019-05-13 stsp == 0)
1573 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1574 7154f6ce 2019-03-27 stsp else
1575 7154f6ce 2019-03-27 stsp i++;
1576 7154f6ce 2019-03-27 stsp }
1577 7154f6ce 2019-03-27 stsp }
1578 7154f6ce 2019-03-27 stsp
1579 7154f6ce 2019-03-27 stsp return err;
1580 e2b1e152 2019-07-13 stsp }
1581 e2b1e152 2019-07-13 stsp
1582 e2b1e152 2019-07-13 stsp static int
1583 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1584 1ebedb77 2019-10-19 stsp {
1585 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1586 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1587 1ebedb77 2019-10-19 stsp }
1588 1ebedb77 2019-10-19 stsp
1589 1ebedb77 2019-10-19 stsp static int
1590 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1591 e2b1e152 2019-07-13 stsp {
1592 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1593 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1594 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1595 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1596 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1597 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1598 c363b2c1 2019-08-03 stsp }
1599 c363b2c1 2019-08-03 stsp
1600 c363b2c1 2019-08-03 stsp static unsigned char
1601 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1602 c363b2c1 2019-08-03 stsp {
1603 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1604 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1605 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1606 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1607 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1608 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1609 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1610 c363b2c1 2019-08-03 stsp default:
1611 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1612 a919d5c4 2020-07-23 stsp }
1613 a919d5c4 2020-07-23 stsp }
1614 a919d5c4 2020-07-23 stsp
1615 a919d5c4 2020-07-23 stsp static const struct got_error *
1616 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1617 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1618 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1619 a919d5c4 2020-07-23 stsp {
1620 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1621 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1622 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1623 a919d5c4 2020-07-23 stsp ssize_t elen;
1624 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1625 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1626 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1627 a919d5c4 2020-07-23 stsp
1628 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1629 a919d5c4 2020-07-23 stsp
1630 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1631 a919d5c4 2020-07-23 stsp do {
1632 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1633 a919d5c4 2020-07-23 stsp if (err)
1634 a919d5c4 2020-07-23 stsp return err;
1635 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1636 a919d5c4 2020-07-23 stsp /*
1637 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1638 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1639 a919d5c4 2020-07-23 stsp */
1640 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1641 a919d5c4 2020-07-23 stsp }
1642 a919d5c4 2020-07-23 stsp if (len > 0) {
1643 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1644 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1645 a919d5c4 2020-07-23 stsp len - hdrlen);
1646 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1647 a919d5c4 2020-07-23 stsp hdrlen = 0;
1648 a919d5c4 2020-07-23 stsp }
1649 a919d5c4 2020-07-23 stsp } while (len != 0);
1650 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1651 a919d5c4 2020-07-23 stsp
1652 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1653 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1654 a919d5c4 2020-07-23 stsp if (elen == -1)
1655 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1656 a919d5c4 2020-07-23 stsp } else {
1657 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1658 a919d5c4 2020-07-23 stsp if (elen == -1)
1659 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1660 c363b2c1 2019-08-03 stsp }
1661 a919d5c4 2020-07-23 stsp
1662 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1663 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1664 a919d5c4 2020-07-23 stsp
1665 a919d5c4 2020-07-23 stsp return NULL;
1666 7154f6ce 2019-03-27 stsp }
1667 7154f6ce 2019-03-27 stsp
1668 7154f6ce 2019-03-27 stsp static const struct got_error *
1669 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1670 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1671 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1672 6353ad76 2019-02-08 stsp {
1673 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1674 6353ad76 2019-02-08 stsp struct got_object_id id;
1675 6353ad76 2019-02-08 stsp size_t hdrlen;
1676 1338848f 2019-12-13 stsp int fd = -1;
1677 6353ad76 2019-02-08 stsp FILE *f = NULL;
1678 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1679 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1680 6353ad76 2019-02-08 stsp size_t flen, blen;
1681 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1682 6353ad76 2019-02-08 stsp
1683 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1684 6353ad76 2019-02-08 stsp
1685 7f91a133 2019-12-13 stsp /*
1686 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1687 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1688 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1689 7f91a133 2019-12-13 stsp */
1690 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1691 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1692 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1693 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1694 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1695 882ef1b9 2019-12-13 stsp else
1696 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1697 882ef1b9 2019-12-13 stsp goto done;
1698 882ef1b9 2019-12-13 stsp }
1699 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1700 3d35a492 2019-12-13 stsp goto done;
1701 3d35a492 2019-12-13 stsp }
1702 7f91a133 2019-12-13 stsp } else {
1703 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1704 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1705 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1706 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1707 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1708 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1709 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1710 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1711 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1712 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1713 3d35a492 2019-12-13 stsp else
1714 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1715 3d35a492 2019-12-13 stsp goto done;
1716 3d35a492 2019-12-13 stsp }
1717 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1718 1338848f 2019-12-13 stsp goto done;
1719 a378724f 2019-02-10 stsp }
1720 a378724f 2019-02-10 stsp }
1721 6353ad76 2019-02-08 stsp
1722 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1723 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1724 1338848f 2019-12-13 stsp goto done;
1725 3f148bc6 2019-07-27 stsp }
1726 efdd40df 2019-07-27 stsp
1727 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1728 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1729 1338848f 2019-12-13 stsp goto done;
1730 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1731 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1732 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1733 1338848f 2019-12-13 stsp goto done;
1734 d00136be 2019-03-26 stsp }
1735 b8f41171 2019-02-10 stsp
1736 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1737 f179e66d 2020-07-23 stsp goto done;
1738 f179e66d 2020-07-23 stsp
1739 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1740 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1741 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1742 1338848f 2019-12-13 stsp goto done;
1743 f179e66d 2020-07-23 stsp }
1744 6353ad76 2019-02-08 stsp
1745 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1746 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1747 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1748 c363b2c1 2019-08-03 stsp else
1749 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1750 c363b2c1 2019-08-03 stsp
1751 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1752 6353ad76 2019-02-08 stsp if (err)
1753 1338848f 2019-12-13 stsp goto done;
1754 6353ad76 2019-02-08 stsp
1755 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1756 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1757 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1758 a919d5c4 2020-07-23 stsp goto done;
1759 a919d5c4 2020-07-23 stsp }
1760 a919d5c4 2020-07-23 stsp
1761 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1762 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1763 ab0d4361 2019-12-13 stsp if (fd == -1) {
1764 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1765 ab0d4361 2019-12-13 stsp goto done;
1766 ab0d4361 2019-12-13 stsp }
1767 3d35a492 2019-12-13 stsp }
1768 3d35a492 2019-12-13 stsp
1769 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1770 6353ad76 2019-02-08 stsp if (f == NULL) {
1771 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1772 6353ad76 2019-02-08 stsp goto done;
1773 6353ad76 2019-02-08 stsp }
1774 1338848f 2019-12-13 stsp fd = -1;
1775 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1776 656b1f76 2019-05-11 jcs for (;;) {
1777 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1778 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1779 6353ad76 2019-02-08 stsp if (err)
1780 7154f6ce 2019-03-27 stsp goto done;
1781 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1782 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1783 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1784 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1785 7154f6ce 2019-03-27 stsp goto done;
1786 80c5c120 2019-02-19 stsp }
1787 6353ad76 2019-02-08 stsp if (blen == 0) {
1788 6353ad76 2019-02-08 stsp if (flen != 0)
1789 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1790 6353ad76 2019-02-08 stsp break;
1791 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1792 6353ad76 2019-02-08 stsp if (blen != 0)
1793 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1794 6353ad76 2019-02-08 stsp break;
1795 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1796 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1797 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1798 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1799 6353ad76 2019-02-08 stsp break;
1800 6353ad76 2019-02-08 stsp }
1801 6353ad76 2019-02-08 stsp } else {
1802 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1803 6353ad76 2019-02-08 stsp break;
1804 6353ad76 2019-02-08 stsp }
1805 6353ad76 2019-02-08 stsp hdrlen = 0;
1806 6353ad76 2019-02-08 stsp }
1807 7154f6ce 2019-03-27 stsp
1808 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1809 7154f6ce 2019-03-27 stsp rewind(f);
1810 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1811 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1812 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1813 6353ad76 2019-02-08 stsp done:
1814 6353ad76 2019-02-08 stsp if (blob)
1815 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1816 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1817 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1818 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1819 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1820 9d31a1d8 2018-03-11 stsp return err;
1821 e2b1e152 2019-07-13 stsp }
1822 e2b1e152 2019-07-13 stsp
1823 e2b1e152 2019-07-13 stsp /*
1824 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1825 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1826 e2b1e152 2019-07-13 stsp */
1827 e2b1e152 2019-07-13 stsp static const struct got_error *
1828 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1829 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1830 e2b1e152 2019-07-13 stsp {
1831 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1832 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1833 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1834 e2b1e152 2019-07-13 stsp
1835 e2b1e152 2019-07-13 stsp return NULL;
1836 9d31a1d8 2018-03-11 stsp }
1837 9d31a1d8 2018-03-11 stsp
1838 9d31a1d8 2018-03-11 stsp static const struct got_error *
1839 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1840 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1841 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1842 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1843 0584f854 2019-04-06 stsp void *progress_arg)
1844 9d31a1d8 2018-03-11 stsp {
1845 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1846 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1847 6353ad76 2019-02-08 stsp char *ondisk_path;
1848 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1849 b8f41171 2019-02-10 stsp struct stat sb;
1850 9d31a1d8 2018-03-11 stsp
1851 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1852 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1853 6353ad76 2019-02-08 stsp
1854 abb4604f 2019-07-27 stsp if (ie) {
1855 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1856 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1857 a76c42e6 2019-08-03 stsp goto done;
1858 a76c42e6 2019-08-03 stsp }
1859 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1860 7f91a133 2019-12-13 stsp repo);
1861 abb4604f 2019-07-27 stsp if (err)
1862 abb4604f 2019-07-27 stsp goto done;
1863 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1864 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1865 c90c8ce3 2020-07-23 stsp } else {
1866 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1867 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1868 c90c8ce3 2020-07-23 stsp }
1869 6353ad76 2019-02-08 stsp
1870 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1871 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1872 b8f41171 2019-02-10 stsp goto done;
1873 b8f41171 2019-02-10 stsp }
1874 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1875 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1876 5036ab18 2020-04-18 stsp path);
1877 5036ab18 2020-04-18 stsp goto done;
1878 5036ab18 2020-04-18 stsp }
1879 b8f41171 2019-02-10 stsp
1880 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1881 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1882 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1883 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1884 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1885 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1886 e2b1e152 2019-07-13 stsp if (err)
1887 e2b1e152 2019-07-13 stsp goto done;
1888 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1889 b8f41171 2019-02-10 stsp path);
1890 6353ad76 2019-02-08 stsp goto done;
1891 a378724f 2019-02-10 stsp }
1892 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1893 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1894 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1895 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1896 b8f41171 2019-02-10 stsp goto done;
1897 e2b1e152 2019-07-13 stsp }
1898 9d31a1d8 2018-03-11 stsp }
1899 9d31a1d8 2018-03-11 stsp
1900 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1901 8da9e5f4 2019-01-12 stsp if (err)
1902 6353ad76 2019-02-08 stsp goto done;
1903 512f0d0e 2019-01-02 stsp
1904 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1905 234035bc 2019-06-01 stsp int update_timestamps;
1906 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1907 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1908 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1909 234035bc 2019-06-01 stsp struct got_object_id id2;
1910 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1911 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1912 234035bc 2019-06-01 stsp if (err)
1913 f69721c3 2019-10-21 stsp goto done;
1914 f69721c3 2019-10-21 stsp }
1915 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1916 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1917 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1918 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1919 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1920 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1921 f69721c3 2019-10-21 stsp goto done;
1922 f69721c3 2019-10-21 stsp }
1923 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1924 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1925 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1926 234035bc 2019-06-01 stsp goto done;
1927 f69721c3 2019-10-21 stsp }
1928 234035bc 2019-06-01 stsp }
1929 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1930 36bf999c 2020-07-23 stsp char *link_target;
1931 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1932 36bf999c 2020-07-23 stsp if (err)
1933 36bf999c 2020-07-23 stsp goto done;
1934 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1935 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1936 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1937 36bf999c 2020-07-23 stsp free(link_target);
1938 993e2a1b 2020-07-23 stsp } else {
1939 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1940 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1941 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1942 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1943 993e2a1b 2020-07-23 stsp }
1944 f69721c3 2019-10-21 stsp free(label_orig);
1945 234035bc 2019-06-01 stsp if (blob2)
1946 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1947 909d120e 2019-09-22 stsp if (err)
1948 909d120e 2019-09-22 stsp goto done;
1949 234035bc 2019-06-01 stsp /*
1950 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1951 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1952 234035bc 2019-06-01 stsp * unmodified files again.
1953 234035bc 2019-06-01 stsp */
1954 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1955 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1956 234035bc 2019-06-01 stsp update_timestamps);
1957 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1958 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1959 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1960 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1961 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1962 1ee397ad 2019-07-12 stsp if (err)
1963 1ee397ad 2019-07-12 stsp goto done;
1964 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1965 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1966 13d9040b 2019-03-27 stsp if (err)
1967 13d9040b 2019-03-27 stsp goto done;
1968 13d9040b 2019-03-27 stsp } else {
1969 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1970 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
1971 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
1972 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
1973 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
1974 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1975 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1976 2e1fa222 2020-07-23 stsp } else {
1977 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1978 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
1979 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
1980 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
1981 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
1982 2e1fa222 2020-07-23 stsp }
1983 13d9040b 2019-03-27 stsp if (err)
1984 13d9040b 2019-03-27 stsp goto done;
1985 65b05cec 2020-07-23 stsp
1986 054041d0 2020-06-24 stsp if (ie) {
1987 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1988 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1989 054041d0 2020-06-24 stsp } else {
1990 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
1991 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1992 054041d0 2020-06-24 stsp &blob->id);
1993 054041d0 2020-06-24 stsp }
1994 13d9040b 2019-03-27 stsp if (err)
1995 13d9040b 2019-03-27 stsp goto done;
1996 65b05cec 2020-07-23 stsp
1997 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
1998 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
1999 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2000 2e1fa222 2020-07-23 stsp }
2001 13d9040b 2019-03-27 stsp }
2002 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2003 6353ad76 2019-02-08 stsp done:
2004 6353ad76 2019-02-08 stsp free(ondisk_path);
2005 8da9e5f4 2019-01-12 stsp return err;
2006 90285c3b 2019-01-08 stsp }
2007 90285c3b 2019-01-08 stsp
2008 90285c3b 2019-01-08 stsp static const struct got_error *
2009 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2010 90285c3b 2019-01-08 stsp {
2011 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2012 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2013 90285c3b 2019-01-08 stsp
2014 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2015 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2016 90285c3b 2019-01-08 stsp
2017 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2018 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2019 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2020 c97665ae 2019-01-13 stsp } else {
2021 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
2022 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
2023 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
2024 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2025 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2026 230a42bd 2019-05-11 jcs parent);
2027 90285c3b 2019-01-08 stsp break;
2028 90285c3b 2019-01-08 stsp }
2029 90285c3b 2019-01-08 stsp parent = dirname(parent);
2030 90285c3b 2019-01-08 stsp }
2031 90285c3b 2019-01-08 stsp }
2032 90285c3b 2019-01-08 stsp free(ondisk_path);
2033 90285c3b 2019-01-08 stsp return err;
2034 512f0d0e 2019-01-02 stsp }
2035 512f0d0e 2019-01-02 stsp
2036 708d8e67 2019-03-27 stsp static const struct got_error *
2037 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2038 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2039 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2040 708d8e67 2019-03-27 stsp {
2041 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2042 708d8e67 2019-03-27 stsp unsigned char status;
2043 708d8e67 2019-03-27 stsp struct stat sb;
2044 708d8e67 2019-03-27 stsp char *ondisk_path;
2045 708d8e67 2019-03-27 stsp
2046 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2047 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2048 a76c42e6 2019-08-03 stsp
2049 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2050 708d8e67 2019-03-27 stsp == -1)
2051 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2052 708d8e67 2019-03-27 stsp
2053 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2054 708d8e67 2019-03-27 stsp if (err)
2055 5a58a424 2020-06-23 stsp goto done;
2056 708d8e67 2019-03-27 stsp
2057 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2058 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2059 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2060 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2061 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2062 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2063 993e2a1b 2020-07-23 stsp goto done;
2064 993e2a1b 2020-07-23 stsp }
2065 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2066 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2067 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2068 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2069 993e2a1b 2020-07-23 stsp if (err)
2070 993e2a1b 2020-07-23 stsp goto done;
2071 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2072 993e2a1b 2020-07-23 stsp ie->path);
2073 993e2a1b 2020-07-23 stsp goto done;
2074 993e2a1b 2020-07-23 stsp }
2075 993e2a1b 2020-07-23 stsp
2076 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2077 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2078 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2079 1ee397ad 2019-07-12 stsp if (err)
2080 5a58a424 2020-06-23 stsp goto done;
2081 fc6346c4 2019-03-27 stsp /*
2082 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2083 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2084 fc6346c4 2019-03-27 stsp */
2085 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2086 fc6346c4 2019-03-27 stsp 0);
2087 fc6346c4 2019-03-27 stsp } else {
2088 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2089 1ee397ad 2019-07-12 stsp if (err)
2090 5a58a424 2020-06-23 stsp goto done;
2091 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2092 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2093 fc6346c4 2019-03-27 stsp if (err)
2094 5a58a424 2020-06-23 stsp goto done;
2095 fc6346c4 2019-03-27 stsp }
2096 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2097 708d8e67 2019-03-27 stsp }
2098 5a58a424 2020-06-23 stsp done:
2099 5a58a424 2020-06-23 stsp free(ondisk_path);
2100 fc6346c4 2019-03-27 stsp return err;
2101 708d8e67 2019-03-27 stsp }
2102 708d8e67 2019-03-27 stsp
2103 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2104 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2105 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2106 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2107 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2108 8da9e5f4 2019-01-12 stsp void *progress_arg;
2109 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2110 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2111 8da9e5f4 2019-01-12 stsp };
2112 8da9e5f4 2019-01-12 stsp
2113 512f0d0e 2019-01-02 stsp static const struct got_error *
2114 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2115 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2116 512f0d0e 2019-01-02 stsp {
2117 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2118 0584f854 2019-04-06 stsp
2119 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2120 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2121 512f0d0e 2019-01-02 stsp
2122 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2123 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2124 8da9e5f4 2019-01-12 stsp }
2125 512f0d0e 2019-01-02 stsp
2126 8da9e5f4 2019-01-12 stsp static const struct got_error *
2127 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2128 8da9e5f4 2019-01-12 stsp {
2129 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2130 7a9df742 2019-01-08 stsp
2131 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2132 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2133 0584f854 2019-04-06 stsp
2134 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2135 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2136 9d31a1d8 2018-03-11 stsp }
2137 9d31a1d8 2018-03-11 stsp
2138 9d31a1d8 2018-03-11 stsp static const struct got_error *
2139 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2140 9d31a1d8 2018-03-11 stsp {
2141 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2142 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2143 8da9e5f4 2019-01-12 stsp char *path;
2144 9d31a1d8 2018-03-11 stsp
2145 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2146 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2147 0584f854 2019-04-06 stsp
2148 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2149 63c5ca5d 2019-08-24 stsp return NULL;
2150 63c5ca5d 2019-08-24 stsp
2151 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2152 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2153 8da9e5f4 2019-01-12 stsp == -1)
2154 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2155 9d31a1d8 2018-03-11 stsp
2156 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2157 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2158 8da9e5f4 2019-01-12 stsp else
2159 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2160 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2161 9d31a1d8 2018-03-11 stsp
2162 8da9e5f4 2019-01-12 stsp free(path);
2163 0cd1c46a 2019-03-11 stsp return err;
2164 0cd1c46a 2019-03-11 stsp }
2165 0cd1c46a 2019-03-11 stsp
2166 b2118c49 2020-07-28 stsp const struct got_error *
2167 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2168 b2118c49 2020-07-28 stsp {
2169 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2170 b2118c49 2020-07-28 stsp
2171 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2172 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2173 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2174 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2175 b2118c49 2020-07-28 stsp }
2176 b2118c49 2020-07-28 stsp
2177 b2118c49 2020-07-28 stsp return NULL;
2178 b2118c49 2020-07-28 stsp }
2179 b2118c49 2020-07-28 stsp
2180 818c7501 2019-07-11 stsp static const struct got_error *
2181 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2182 0cd1c46a 2019-03-11 stsp {
2183 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2184 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2185 0cd1c46a 2019-03-11 stsp
2186 517bab73 2019-03-11 stsp *refname = NULL;
2187 517bab73 2019-03-11 stsp
2188 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2189 b2118c49 2020-07-28 stsp if (err)
2190 b2118c49 2020-07-28 stsp return err;
2191 0cd1c46a 2019-03-11 stsp
2192 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2193 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2194 0647c563 2019-03-11 stsp *refname = NULL;
2195 0cd1c46a 2019-03-11 stsp }
2196 517bab73 2019-03-11 stsp free(uuidstr);
2197 517bab73 2019-03-11 stsp return err;
2198 517bab73 2019-03-11 stsp }
2199 517bab73 2019-03-11 stsp
2200 818c7501 2019-07-11 stsp const struct got_error *
2201 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2202 818c7501 2019-07-11 stsp {
2203 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2204 818c7501 2019-07-11 stsp }
2205 818c7501 2019-07-11 stsp
2206 818c7501 2019-07-11 stsp static const struct got_error *
2207 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2208 818c7501 2019-07-11 stsp {
2209 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2210 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2211 818c7501 2019-07-11 stsp }
2212 818c7501 2019-07-11 stsp
2213 818c7501 2019-07-11 stsp static const struct got_error *
2214 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2215 818c7501 2019-07-11 stsp {
2216 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2217 818c7501 2019-07-11 stsp }
2218 818c7501 2019-07-11 stsp
2219 818c7501 2019-07-11 stsp static const struct got_error *
2220 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2221 818c7501 2019-07-11 stsp {
2222 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2223 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2224 818c7501 2019-07-11 stsp }
2225 818c7501 2019-07-11 stsp
2226 818c7501 2019-07-11 stsp static const struct got_error *
2227 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2228 818c7501 2019-07-11 stsp {
2229 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2230 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2231 0ebf8283 2019-07-24 stsp }
2232 0ebf8283 2019-07-24 stsp
2233 0ebf8283 2019-07-24 stsp static const struct got_error *
2234 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2235 0ebf8283 2019-07-24 stsp {
2236 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2237 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2238 0ebf8283 2019-07-24 stsp }
2239 0ebf8283 2019-07-24 stsp
2240 0ebf8283 2019-07-24 stsp static const struct got_error *
2241 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2242 0ebf8283 2019-07-24 stsp {
2243 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2244 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2245 0ebf8283 2019-07-24 stsp }
2246 0ebf8283 2019-07-24 stsp
2247 0ebf8283 2019-07-24 stsp static const struct got_error *
2248 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2249 0ebf8283 2019-07-24 stsp {
2250 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2251 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2252 818c7501 2019-07-11 stsp }
2253 818c7501 2019-07-11 stsp
2254 0ebf8283 2019-07-24 stsp static const struct got_error *
2255 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2256 0ebf8283 2019-07-24 stsp {
2257 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2258 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2259 0ebf8283 2019-07-24 stsp }
2260 818c7501 2019-07-11 stsp
2261 0ebf8283 2019-07-24 stsp const struct got_error *
2262 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2263 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2264 0ebf8283 2019-07-24 stsp {
2265 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2266 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2267 0ebf8283 2019-07-24 stsp *path = NULL;
2268 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2269 0ebf8283 2019-07-24 stsp }
2270 0ebf8283 2019-07-24 stsp return NULL;
2271 0ebf8283 2019-07-24 stsp }
2272 0ebf8283 2019-07-24 stsp
2273 517bab73 2019-03-11 stsp /*
2274 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2275 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2276 517bab73 2019-03-11 stsp */
2277 517bab73 2019-03-11 stsp static const struct got_error *
2278 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2279 517bab73 2019-03-11 stsp {
2280 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2281 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2282 517bab73 2019-03-11 stsp char *refname;
2283 517bab73 2019-03-11 stsp
2284 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2285 517bab73 2019-03-11 stsp if (err)
2286 517bab73 2019-03-11 stsp return err;
2287 0cd1c46a 2019-03-11 stsp
2288 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2289 0cd1c46a 2019-03-11 stsp if (err)
2290 0cd1c46a 2019-03-11 stsp goto done;
2291 0cd1c46a 2019-03-11 stsp
2292 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2293 0cd1c46a 2019-03-11 stsp done:
2294 0cd1c46a 2019-03-11 stsp free(refname);
2295 0cd1c46a 2019-03-11 stsp if (ref)
2296 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2297 3e3a69f1 2019-07-25 stsp return err;
2298 3e3a69f1 2019-07-25 stsp }
2299 3e3a69f1 2019-07-25 stsp
2300 3e3a69f1 2019-07-25 stsp static const struct got_error *
2301 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2302 3e3a69f1 2019-07-25 stsp {
2303 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2304 3e3a69f1 2019-07-25 stsp
2305 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2306 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2307 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2308 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2309 3e3a69f1 2019-07-25 stsp }
2310 9d31a1d8 2018-03-11 stsp return err;
2311 9d31a1d8 2018-03-11 stsp }
2312 9d31a1d8 2018-03-11 stsp
2313 3e3a69f1 2019-07-25 stsp
2314 ebf99748 2019-05-09 stsp static const struct got_error *
2315 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2316 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2317 ebf99748 2019-05-09 stsp {
2318 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2319 ebf99748 2019-05-09 stsp FILE *index = NULL;
2320 0cd1c46a 2019-03-11 stsp
2321 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2322 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2323 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2324 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2325 ebf99748 2019-05-09 stsp
2326 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2327 3e3a69f1 2019-07-25 stsp if (err)
2328 ebf99748 2019-05-09 stsp goto done;
2329 ebf99748 2019-05-09 stsp
2330 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2331 ebf99748 2019-05-09 stsp if (index == NULL) {
2332 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2333 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2334 ebf99748 2019-05-09 stsp } else {
2335 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2336 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2337 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2338 ebf99748 2019-05-09 stsp }
2339 ebf99748 2019-05-09 stsp done:
2340 ebf99748 2019-05-09 stsp if (err) {
2341 ebf99748 2019-05-09 stsp free(*fileindex_path);
2342 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2343 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2344 ebf99748 2019-05-09 stsp *fileindex = NULL;
2345 ebf99748 2019-05-09 stsp }
2346 ebf99748 2019-05-09 stsp return err;
2347 ebf99748 2019-05-09 stsp }
2348 c932eeeb 2019-05-22 stsp
2349 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2350 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2351 c932eeeb 2019-05-22 stsp const char *path;
2352 c932eeeb 2019-05-22 stsp size_t path_len;
2353 c932eeeb 2019-05-22 stsp const char *entry_name;
2354 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2355 a484d721 2019-06-10 stsp void *progress_arg;
2356 c932eeeb 2019-05-22 stsp };
2357 ebf99748 2019-05-09 stsp
2358 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2359 c932eeeb 2019-05-22 stsp static const struct got_error *
2360 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2361 c932eeeb 2019-05-22 stsp {
2362 1ee397ad 2019-07-12 stsp const struct got_error *err;
2363 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2364 c932eeeb 2019-05-22 stsp
2365 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2366 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2367 c932eeeb 2019-05-22 stsp return NULL;
2368 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2369 102ff934 2019-06-11 stsp return NULL;
2370 102ff934 2019-06-11 stsp
2371 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2372 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2373 c932eeeb 2019-05-22 stsp return NULL;
2374 c932eeeb 2019-05-22 stsp
2375 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2376 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2377 edd02c5e 2019-07-11 stsp ie->path);
2378 1ee397ad 2019-07-12 stsp if (err)
2379 1ee397ad 2019-07-12 stsp return err;
2380 1ee397ad 2019-07-12 stsp }
2381 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2382 c932eeeb 2019-05-22 stsp return NULL;
2383 9c6338c4 2019-06-02 stsp }
2384 9c6338c4 2019-06-02 stsp
2385 9c6338c4 2019-06-02 stsp static const struct got_error *
2386 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2387 9c6338c4 2019-06-02 stsp {
2388 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2389 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2390 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2391 867630bb 2020-01-17 stsp struct timespec timeout;
2392 9c6338c4 2019-06-02 stsp
2393 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2394 9c6338c4 2019-06-02 stsp fileindex_path);
2395 9c6338c4 2019-06-02 stsp if (err)
2396 9c6338c4 2019-06-02 stsp goto done;
2397 9c6338c4 2019-06-02 stsp
2398 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2399 9c6338c4 2019-06-02 stsp if (err)
2400 9c6338c4 2019-06-02 stsp goto done;
2401 9c6338c4 2019-06-02 stsp
2402 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2403 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2404 9c6338c4 2019-06-02 stsp fileindex_path);
2405 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2406 9c6338c4 2019-06-02 stsp }
2407 867630bb 2020-01-17 stsp
2408 867630bb 2020-01-17 stsp /*
2409 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2410 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2411 867630bb 2020-01-17 stsp * was recorded in the file index.
2412 867630bb 2020-01-17 stsp */
2413 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2414 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2415 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2416 9c6338c4 2019-06-02 stsp done:
2417 9c6338c4 2019-06-02 stsp if (new_index)
2418 9c6338c4 2019-06-02 stsp fclose(new_index);
2419 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2420 9c6338c4 2019-06-02 stsp return err;
2421 c932eeeb 2019-05-22 stsp }
2422 6ced4176 2019-07-12 stsp
2423 6ced4176 2019-07-12 stsp static const struct got_error *
2424 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2425 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2426 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2427 6ced4176 2019-07-12 stsp {
2428 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2429 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2430 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2431 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2432 6ced4176 2019-07-12 stsp
2433 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2434 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2435 6ced4176 2019-07-12 stsp *tree_id = NULL;
2436 6ced4176 2019-07-12 stsp
2437 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2438 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2439 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2440 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2441 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2442 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2443 6ced4176 2019-07-12 stsp goto done;
2444 6ced4176 2019-07-12 stsp }
2445 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2446 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2447 6ced4176 2019-07-12 stsp if (err)
2448 6ced4176 2019-07-12 stsp goto done;
2449 6ced4176 2019-07-12 stsp return NULL;
2450 6ced4176 2019-07-12 stsp }
2451 6ced4176 2019-07-12 stsp
2452 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2453 6ced4176 2019-07-12 stsp
2454 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2455 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2456 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2457 6ced4176 2019-07-12 stsp goto done;
2458 6ced4176 2019-07-12 stsp }
2459 6ced4176 2019-07-12 stsp
2460 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2461 6ced4176 2019-07-12 stsp in_repo_path);
2462 6ced4176 2019-07-12 stsp if (err)
2463 6ced4176 2019-07-12 stsp goto done;
2464 6ced4176 2019-07-12 stsp
2465 6ced4176 2019-07-12 stsp free(in_repo_path);
2466 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2467 6ced4176 2019-07-12 stsp
2468 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2469 6ced4176 2019-07-12 stsp if (err)
2470 6ced4176 2019-07-12 stsp goto done;
2471 c932eeeb 2019-05-22 stsp
2472 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2473 6ced4176 2019-07-12 stsp /* Check out a single file. */
2474 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2475 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2476 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2477 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2478 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2479 6ced4176 2019-07-12 stsp goto done;
2480 6ced4176 2019-07-12 stsp }
2481 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2482 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2483 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2484 6ced4176 2019-07-12 stsp goto done;
2485 6ced4176 2019-07-12 stsp }
2486 6ced4176 2019-07-12 stsp } else {
2487 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2488 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2489 6ced4176 2019-07-12 stsp if (err)
2490 6ced4176 2019-07-12 stsp return err;
2491 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2492 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2493 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2494 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2495 6ced4176 2019-07-12 stsp goto done;
2496 6ced4176 2019-07-12 stsp }
2497 6ced4176 2019-07-12 stsp }
2498 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2499 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2500 6ced4176 2019-07-12 stsp } else {
2501 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2502 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2503 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2504 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2505 6ced4176 2019-07-12 stsp goto done;
2506 6ced4176 2019-07-12 stsp }
2507 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2508 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2509 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2510 6ced4176 2019-07-12 stsp goto done;
2511 6ced4176 2019-07-12 stsp }
2512 6ced4176 2019-07-12 stsp }
2513 6ced4176 2019-07-12 stsp done:
2514 6ced4176 2019-07-12 stsp free(id);
2515 6ced4176 2019-07-12 stsp free(in_repo_path);
2516 6ced4176 2019-07-12 stsp if (err) {
2517 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2518 6ced4176 2019-07-12 stsp free(*tree_relpath);
2519 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2520 6ced4176 2019-07-12 stsp free(*tree_id);
2521 6ced4176 2019-07-12 stsp *tree_id = NULL;
2522 6ced4176 2019-07-12 stsp }
2523 07ed472d 2019-07-12 stsp return err;
2524 07ed472d 2019-07-12 stsp }
2525 07ed472d 2019-07-12 stsp
2526 07ed472d 2019-07-12 stsp static const struct got_error *
2527 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2528 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2529 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2530 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2531 07ed472d 2019-07-12 stsp {
2532 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2533 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2534 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2535 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2536 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2537 07ed472d 2019-07-12 stsp
2538 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2539 7f47418f 2019-12-20 stsp if (err) {
2540 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2541 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2542 7f47418f 2019-12-20 stsp goto done;
2543 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2544 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2545 7f47418f 2019-12-20 stsp if (err)
2546 7f47418f 2019-12-20 stsp return err;
2547 7f47418f 2019-12-20 stsp }
2548 07ed472d 2019-07-12 stsp
2549 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2550 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2551 07ed472d 2019-07-12 stsp if (err)
2552 07ed472d 2019-07-12 stsp goto done;
2553 07ed472d 2019-07-12 stsp
2554 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2555 07ed472d 2019-07-12 stsp if (err)
2556 07ed472d 2019-07-12 stsp goto done;
2557 07ed472d 2019-07-12 stsp
2558 07ed472d 2019-07-12 stsp if (entry_name &&
2559 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2560 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2561 07ed472d 2019-07-12 stsp goto done;
2562 07ed472d 2019-07-12 stsp }
2563 07ed472d 2019-07-12 stsp
2564 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2565 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2566 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2567 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2568 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2569 07ed472d 2019-07-12 stsp arg.repo = repo;
2570 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2571 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2572 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2573 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2574 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2575 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2576 07ed472d 2019-07-12 stsp done:
2577 07ed472d 2019-07-12 stsp if (tree)
2578 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2579 07ed472d 2019-07-12 stsp if (commit)
2580 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2581 6ced4176 2019-07-12 stsp return err;
2582 6ced4176 2019-07-12 stsp }
2583 6ced4176 2019-07-12 stsp
2584 9d31a1d8 2018-03-11 stsp const struct got_error *
2585 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2586 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2587 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2588 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2589 9d31a1d8 2018-03-11 stsp {
2590 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2591 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2592 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2593 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2594 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2595 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2596 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2597 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2598 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2599 f2ea84fa 2019-07-27 stsp int entry_type;
2600 f2ea84fa 2019-07-27 stsp char *relpath;
2601 f2ea84fa 2019-07-27 stsp char *entry_name;
2602 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2603 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2604 9d31a1d8 2018-03-11 stsp
2605 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2606 f2ea84fa 2019-07-27 stsp
2607 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2608 9d31a1d8 2018-03-11 stsp if (err)
2609 9d31a1d8 2018-03-11 stsp return err;
2610 93a30277 2018-12-24 stsp
2611 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2612 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2613 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2614 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2615 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2616 f2ea84fa 2019-07-27 stsp goto done;
2617 f2ea84fa 2019-07-27 stsp }
2618 6ced4176 2019-07-12 stsp
2619 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2620 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2621 f2ea84fa 2019-07-27 stsp if (err) {
2622 f2ea84fa 2019-07-27 stsp free(tpd);
2623 6ced4176 2019-07-12 stsp goto done;
2624 6ced4176 2019-07-12 stsp }
2625 f2ea84fa 2019-07-27 stsp
2626 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2627 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2628 f2ea84fa 2019-07-27 stsp if (err) {
2629 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2630 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2631 f2ea84fa 2019-07-27 stsp free(tpd);
2632 f2ea84fa 2019-07-27 stsp goto done;
2633 f2ea84fa 2019-07-27 stsp }
2634 f2ea84fa 2019-07-27 stsp } else
2635 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2636 f2ea84fa 2019-07-27 stsp
2637 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2638 6ced4176 2019-07-12 stsp }
2639 6ced4176 2019-07-12 stsp
2640 51514078 2018-12-25 stsp /*
2641 51514078 2018-12-25 stsp * Read the file index.
2642 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2643 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2644 51514078 2018-12-25 stsp */
2645 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2646 8da9e5f4 2019-01-12 stsp if (err)
2647 c4cdcb68 2019-04-03 stsp goto done;
2648 c4cdcb68 2019-04-03 stsp
2649 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2650 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2651 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2652 f2ea84fa 2019-07-27 stsp
2653 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2654 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2655 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2656 f2ea84fa 2019-07-27 stsp if (err)
2657 f2ea84fa 2019-07-27 stsp break;
2658 f2ea84fa 2019-07-27 stsp
2659 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2660 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2661 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2662 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2663 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2664 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2665 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2666 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2667 f2ea84fa 2019-07-27 stsp if (err)
2668 f2ea84fa 2019-07-27 stsp break;
2669 f2ea84fa 2019-07-27 stsp
2670 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2671 711d9cd9 2019-07-12 stsp }
2672 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2673 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2674 af12c6b9 2019-06-04 stsp err = sync_err;
2675 9d31a1d8 2018-03-11 stsp done:
2676 9c6338c4 2019-06-02 stsp free(fileindex_path);
2677 edfa7d7f 2018-09-11 stsp if (tree)
2678 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2679 9d31a1d8 2018-03-11 stsp if (commit)
2680 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2681 5ade8233 2019-07-12 stsp if (fileindex)
2682 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2683 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2684 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2685 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2686 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2687 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2688 f2ea84fa 2019-07-27 stsp free(tpd);
2689 f2ea84fa 2019-07-27 stsp }
2690 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2691 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2692 9d31a1d8 2018-03-11 stsp err = unlockerr;
2693 f8d1f275 2019-02-04 stsp return err;
2694 f8d1f275 2019-02-04 stsp }
2695 f8d1f275 2019-02-04 stsp
2696 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2697 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2698 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2699 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2700 234035bc 2019-06-01 stsp void *progress_arg;
2701 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2702 234035bc 2019-06-01 stsp void *cancel_arg;
2703 f69721c3 2019-10-21 stsp const char *label_orig;
2704 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2705 234035bc 2019-06-01 stsp };
2706 234035bc 2019-06-01 stsp
2707 234035bc 2019-06-01 stsp static const struct got_error *
2708 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2709 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2710 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2711 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2712 234035bc 2019-06-01 stsp {
2713 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2714 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2715 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2716 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2717 234035bc 2019-06-01 stsp struct stat sb;
2718 234035bc 2019-06-01 stsp unsigned char status;
2719 234035bc 2019-06-01 stsp int local_changes_subsumed;
2720 234035bc 2019-06-01 stsp
2721 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2722 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2723 d6c87207 2019-08-02 stsp strlen(path2));
2724 1ee397ad 2019-07-12 stsp if (ie == NULL)
2725 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2726 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2727 234035bc 2019-06-01 stsp
2728 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2729 234035bc 2019-06-01 stsp path2) == -1)
2730 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2731 234035bc 2019-06-01 stsp
2732 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2733 7f91a133 2019-12-13 stsp repo);
2734 234035bc 2019-06-01 stsp if (err)
2735 234035bc 2019-06-01 stsp goto done;
2736 234035bc 2019-06-01 stsp
2737 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2738 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2739 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2740 234035bc 2019-06-01 stsp goto done;
2741 234035bc 2019-06-01 stsp }
2742 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2743 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2744 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2745 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2746 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2747 234035bc 2019-06-01 stsp goto done;
2748 234035bc 2019-06-01 stsp }
2749 234035bc 2019-06-01 stsp
2750 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2751 36bf999c 2020-07-23 stsp char *link_target2;
2752 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2753 36bf999c 2020-07-23 stsp if (err)
2754 36bf999c 2020-07-23 stsp goto done;
2755 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2756 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2757 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2758 36bf999c 2020-07-23 stsp free(link_target2);
2759 af57b12a 2020-07-23 stsp } else {
2760 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2761 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2762 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2763 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2764 af57b12a 2020-07-23 stsp }
2765 234035bc 2019-06-01 stsp } else if (blob1) {
2766 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2767 d6c87207 2019-08-02 stsp strlen(path1));
2768 1ee397ad 2019-07-12 stsp if (ie == NULL)
2769 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2770 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2771 2b92fad7 2019-06-02 stsp
2772 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2773 2b92fad7 2019-06-02 stsp path1) == -1)
2774 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2775 2b92fad7 2019-06-02 stsp
2776 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2777 7f91a133 2019-12-13 stsp repo);
2778 2b92fad7 2019-06-02 stsp if (err)
2779 2b92fad7 2019-06-02 stsp goto done;
2780 2b92fad7 2019-06-02 stsp
2781 2b92fad7 2019-06-02 stsp switch (status) {
2782 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2783 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2784 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2785 1ee397ad 2019-07-12 stsp if (err)
2786 1ee397ad 2019-07-12 stsp goto done;
2787 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2788 2b92fad7 2019-06-02 stsp if (err)
2789 2b92fad7 2019-06-02 stsp goto done;
2790 2b92fad7 2019-06-02 stsp if (ie)
2791 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2792 2b92fad7 2019-06-02 stsp break;
2793 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2794 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2795 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2796 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2797 1ee397ad 2019-07-12 stsp if (err)
2798 1ee397ad 2019-07-12 stsp goto done;
2799 2b92fad7 2019-06-02 stsp if (ie)
2800 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2801 2b92fad7 2019-06-02 stsp break;
2802 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2803 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2804 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2805 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2806 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2807 1ee397ad 2019-07-12 stsp if (err)
2808 1ee397ad 2019-07-12 stsp goto done;
2809 2b92fad7 2019-06-02 stsp break;
2810 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2811 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2812 1ee397ad 2019-07-12 stsp if (err)
2813 1ee397ad 2019-07-12 stsp goto done;
2814 2b92fad7 2019-06-02 stsp break;
2815 2b92fad7 2019-06-02 stsp default:
2816 2b92fad7 2019-06-02 stsp break;
2817 2b92fad7 2019-06-02 stsp }
2818 234035bc 2019-06-01 stsp } else if (blob2) {
2819 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2820 234035bc 2019-06-01 stsp path2) == -1)
2821 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2822 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2823 d6c87207 2019-08-02 stsp strlen(path2));
2824 234035bc 2019-06-01 stsp if (ie) {
2825 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2826 7f91a133 2019-12-13 stsp -1, NULL, repo);
2827 234035bc 2019-06-01 stsp if (err)
2828 234035bc 2019-06-01 stsp goto done;
2829 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2830 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2831 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2832 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2833 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2834 1ee397ad 2019-07-12 stsp status, path2);
2835 234035bc 2019-06-01 stsp goto done;
2836 234035bc 2019-06-01 stsp }
2837 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2838 36bf999c 2020-07-23 stsp char *link_target2;
2839 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2840 36bf999c 2020-07-23 stsp blob2);
2841 36bf999c 2020-07-23 stsp if (err)
2842 36bf999c 2020-07-23 stsp goto done;
2843 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2844 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2845 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2846 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2847 36bf999c 2020-07-23 stsp free(link_target2);
2848 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2849 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2850 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2851 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2852 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2853 526a746f 2020-07-23 stsp a->progress_arg);
2854 dfe9fba0 2020-07-23 stsp } else {
2855 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2856 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2857 526a746f 2020-07-23 stsp }
2858 dfe9fba0 2020-07-23 stsp if (err)
2859 dfe9fba0 2020-07-23 stsp goto done;
2860 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2861 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2862 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2863 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2864 234035bc 2019-06-01 stsp if (err)
2865 234035bc 2019-06-01 stsp goto done;
2866 234035bc 2019-06-01 stsp }
2867 234035bc 2019-06-01 stsp } else {
2868 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2869 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2870 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2871 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2872 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2873 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2874 2e1fa222 2020-07-23 stsp } else {
2875 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2876 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2877 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2878 2e1fa222 2020-07-23 stsp }
2879 234035bc 2019-06-01 stsp if (err)
2880 234035bc 2019-06-01 stsp goto done;
2881 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2882 234035bc 2019-06-01 stsp if (err)
2883 234035bc 2019-06-01 stsp goto done;
2884 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2885 3969253a 2020-03-07 stsp NULL, NULL, 1);
2886 3969253a 2020-03-07 stsp if (err) {
2887 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2888 3969253a 2020-03-07 stsp goto done;
2889 3969253a 2020-03-07 stsp }
2890 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2891 2b92fad7 2019-06-02 stsp if (err) {
2892 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2893 2b92fad7 2019-06-02 stsp goto done;
2894 2b92fad7 2019-06-02 stsp }
2895 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2896 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2897 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2898 2e1fa222 2020-07-23 stsp }
2899 234035bc 2019-06-01 stsp }
2900 234035bc 2019-06-01 stsp }
2901 234035bc 2019-06-01 stsp done:
2902 234035bc 2019-06-01 stsp free(ondisk_path);
2903 234035bc 2019-06-01 stsp return err;
2904 234035bc 2019-06-01 stsp }
2905 234035bc 2019-06-01 stsp
2906 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2907 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2908 234035bc 2019-06-01 stsp struct got_repository *repo;
2909 234035bc 2019-06-01 stsp };
2910 234035bc 2019-06-01 stsp
2911 234035bc 2019-06-01 stsp static const struct got_error *
2912 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2913 234035bc 2019-06-01 stsp {
2914 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2915 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2916 234035bc 2019-06-01 stsp unsigned char status;
2917 234035bc 2019-06-01 stsp struct stat sb;
2918 234035bc 2019-06-01 stsp char *ondisk_path;
2919 234035bc 2019-06-01 stsp
2920 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2921 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2922 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2923 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2924 234035bc 2019-06-01 stsp
2925 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2926 234035bc 2019-06-01 stsp == -1)
2927 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2928 234035bc 2019-06-01 stsp
2929 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2930 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2931 234035bc 2019-06-01 stsp if (err)
2932 234035bc 2019-06-01 stsp return err;
2933 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2934 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2935 234035bc 2019-06-01 stsp
2936 234035bc 2019-06-01 stsp return NULL;
2937 234035bc 2019-06-01 stsp }
2938 234035bc 2019-06-01 stsp
2939 818c7501 2019-07-11 stsp static const struct got_error *
2940 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2941 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2942 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2943 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2944 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2945 234035bc 2019-06-01 stsp {
2946 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2947 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2948 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2949 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2950 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2951 234035bc 2019-06-01 stsp
2952 03415a1a 2019-06-02 stsp if (commit_id1) {
2953 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2954 03415a1a 2019-06-02 stsp worktree->path_prefix);
2955 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2956 03415a1a 2019-06-02 stsp goto done;
2957 69d57f3d 2020-07-31 stsp }
2958 69d57f3d 2020-07-31 stsp if (tree_id1) {
2959 69d57f3d 2020-07-31 stsp char *id_str;
2960 03415a1a 2019-06-02 stsp
2961 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2962 03415a1a 2019-06-02 stsp if (err)
2963 03415a1a 2019-06-02 stsp goto done;
2964 f69721c3 2019-10-21 stsp
2965 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2966 f69721c3 2019-10-21 stsp if (err)
2967 f69721c3 2019-10-21 stsp goto done;
2968 f69721c3 2019-10-21 stsp
2969 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2970 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2971 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2972 f69721c3 2019-10-21 stsp free(id_str);
2973 f69721c3 2019-10-21 stsp goto done;
2974 f69721c3 2019-10-21 stsp }
2975 f69721c3 2019-10-21 stsp free(id_str);
2976 03415a1a 2019-06-02 stsp }
2977 234035bc 2019-06-01 stsp
2978 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2979 234035bc 2019-06-01 stsp worktree->path_prefix);
2980 234035bc 2019-06-01 stsp if (err)
2981 234035bc 2019-06-01 stsp goto done;
2982 234035bc 2019-06-01 stsp
2983 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2984 234035bc 2019-06-01 stsp if (err)
2985 234035bc 2019-06-01 stsp goto done;
2986 234035bc 2019-06-01 stsp
2987 234035bc 2019-06-01 stsp arg.worktree = worktree;
2988 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2989 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2990 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2991 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2992 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2993 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2994 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2995 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2996 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2997 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2998 af12c6b9 2019-06-04 stsp err = sync_err;
2999 234035bc 2019-06-01 stsp done:
3000 234035bc 2019-06-01 stsp if (tree1)
3001 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3002 234035bc 2019-06-01 stsp if (tree2)
3003 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3004 f69721c3 2019-10-21 stsp free(label_orig);
3005 818c7501 2019-07-11 stsp return err;
3006 818c7501 2019-07-11 stsp }
3007 234035bc 2019-06-01 stsp
3008 818c7501 2019-07-11 stsp const struct got_error *
3009 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3010 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3011 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3012 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3013 818c7501 2019-07-11 stsp {
3014 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3015 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3016 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3017 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3018 818c7501 2019-07-11 stsp
3019 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3020 818c7501 2019-07-11 stsp if (err)
3021 818c7501 2019-07-11 stsp return err;
3022 818c7501 2019-07-11 stsp
3023 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3024 818c7501 2019-07-11 stsp if (err)
3025 818c7501 2019-07-11 stsp goto done;
3026 818c7501 2019-07-11 stsp
3027 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3028 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3029 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3030 818c7501 2019-07-11 stsp &mok_arg);
3031 818c7501 2019-07-11 stsp if (err)
3032 818c7501 2019-07-11 stsp goto done;
3033 818c7501 2019-07-11 stsp
3034 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3035 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3036 818c7501 2019-07-11 stsp done:
3037 818c7501 2019-07-11 stsp if (fileindex)
3038 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3039 818c7501 2019-07-11 stsp free(fileindex_path);
3040 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3041 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3042 234035bc 2019-06-01 stsp err = unlockerr;
3043 234035bc 2019-06-01 stsp return err;
3044 234035bc 2019-06-01 stsp }
3045 234035bc 2019-06-01 stsp
3046 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3047 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3048 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3049 927df6b7 2019-02-10 stsp const char *status_path;
3050 927df6b7 2019-02-10 stsp size_t status_path_len;
3051 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3052 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3053 f8d1f275 2019-02-04 stsp void *status_arg;
3054 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3055 f8d1f275 2019-02-04 stsp void *cancel_arg;
3056 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3057 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3058 f2a9dc41 2019-12-13 tracey int report_unchanged;
3059 3143d852 2020-06-25 stsp int no_ignores;
3060 f8d1f275 2019-02-04 stsp };
3061 88d0e355 2019-08-03 stsp
3062 f8d1f275 2019-02-04 stsp static const struct got_error *
3063 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3064 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3065 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3066 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3067 927df6b7 2019-02-10 stsp {
3068 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3069 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3070 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3071 927df6b7 2019-02-10 stsp struct stat sb;
3072 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3073 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3074 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3075 927df6b7 2019-02-10 stsp
3076 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3077 98eaaa12 2019-08-03 stsp if (err)
3078 98eaaa12 2019-08-03 stsp return err;
3079 98eaaa12 2019-08-03 stsp
3080 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3081 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3082 98eaaa12 2019-08-03 stsp return NULL;
3083 98eaaa12 2019-08-03 stsp
3084 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3085 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3086 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3087 98eaaa12 2019-08-03 stsp }
3088 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3089 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3090 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3091 927df6b7 2019-02-10 stsp }
3092 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3093 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3094 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3095 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3096 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3097 98eaaa12 2019-08-03 stsp }
3098 98eaaa12 2019-08-03 stsp
3099 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3100 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3101 927df6b7 2019-02-10 stsp }
3102 927df6b7 2019-02-10 stsp
3103 927df6b7 2019-02-10 stsp static const struct got_error *
3104 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3105 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3106 f8d1f275 2019-02-04 stsp {
3107 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3108 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3109 f8d1f275 2019-02-04 stsp char *abspath;
3110 f8d1f275 2019-02-04 stsp
3111 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3112 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3113 0584f854 2019-04-06 stsp
3114 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3115 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3116 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3117 927df6b7 2019-02-10 stsp return NULL;
3118 927df6b7 2019-02-10 stsp
3119 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3120 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3121 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3122 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3123 f8d1f275 2019-02-04 stsp } else {
3124 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3125 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3126 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3127 f8d1f275 2019-02-04 stsp }
3128 f8d1f275 2019-02-04 stsp
3129 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3130 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3131 f8d1f275 2019-02-04 stsp free(abspath);
3132 9d31a1d8 2018-03-11 stsp return err;
3133 9d31a1d8 2018-03-11 stsp }
3134 f8d1f275 2019-02-04 stsp
3135 f8d1f275 2019-02-04 stsp static const struct got_error *
3136 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3137 f8d1f275 2019-02-04 stsp {
3138 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3139 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3140 2ec1f75b 2019-03-26 stsp unsigned char status;
3141 927df6b7 2019-02-10 stsp
3142 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3143 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3144 0584f854 2019-04-06 stsp
3145 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3146 927df6b7 2019-02-10 stsp return NULL;
3147 927df6b7 2019-02-10 stsp
3148 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3149 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3150 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3151 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3152 2ec1f75b 2019-03-26 stsp else
3153 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3154 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3155 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3156 6841da00 2019-08-08 stsp }
3157 6841da00 2019-08-08 stsp
3158 6841da00 2019-08-08 stsp void
3159 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3160 6841da00 2019-08-08 stsp {
3161 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3162 6841da00 2019-08-08 stsp
3163 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3164 6841da00 2019-08-08 stsp free((char *)pe->path);
3165 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3166 6841da00 2019-08-08 stsp }
3167 6841da00 2019-08-08 stsp
3168 6841da00 2019-08-08 stsp void
3169 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3170 6841da00 2019-08-08 stsp {
3171 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3172 6841da00 2019-08-08 stsp
3173 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3174 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3175 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3176 6841da00 2019-08-08 stsp free((char *)pe->path);
3177 6841da00 2019-08-08 stsp }
3178 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3179 6841da00 2019-08-08 stsp }
3180 6841da00 2019-08-08 stsp
3181 6841da00 2019-08-08 stsp static const struct got_error *
3182 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3183 6841da00 2019-08-08 stsp {
3184 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3185 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3186 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3187 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3188 6841da00 2019-08-08 stsp size_t linesize = 0;
3189 6841da00 2019-08-08 stsp ssize_t linelen;
3190 6841da00 2019-08-08 stsp
3191 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3192 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3193 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3194 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3195 6841da00 2019-08-08 stsp
3196 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3197 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3198 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3199 bd8de430 2019-10-04 stsp
3200 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3201 bd8de430 2019-10-04 stsp if (line[0] == '#')
3202 bd8de430 2019-10-04 stsp continue;
3203 bd8de430 2019-10-04 stsp
3204 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3205 bd8de430 2019-10-04 stsp if (line[0] == '!')
3206 bd8de430 2019-10-04 stsp continue;
3207 bd8de430 2019-10-04 stsp
3208 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3209 6841da00 2019-08-08 stsp line) == -1) {
3210 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3211 6841da00 2019-08-08 stsp goto done;
3212 6841da00 2019-08-08 stsp }
3213 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3214 6841da00 2019-08-08 stsp if (err)
3215 6841da00 2019-08-08 stsp goto done;
3216 6841da00 2019-08-08 stsp }
3217 6841da00 2019-08-08 stsp if (ferror(f)) {
3218 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3219 6841da00 2019-08-08 stsp goto done;
3220 6841da00 2019-08-08 stsp }
3221 6841da00 2019-08-08 stsp
3222 6841da00 2019-08-08 stsp dirpath = strdup(path);
3223 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3224 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3225 6841da00 2019-08-08 stsp goto done;
3226 6841da00 2019-08-08 stsp }
3227 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3228 6841da00 2019-08-08 stsp done:
3229 6841da00 2019-08-08 stsp free(line);
3230 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3231 6841da00 2019-08-08 stsp free(dirpath);
3232 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3233 6841da00 2019-08-08 stsp }
3234 6841da00 2019-08-08 stsp return err;
3235 6841da00 2019-08-08 stsp }
3236 6841da00 2019-08-08 stsp
3237 6841da00 2019-08-08 stsp int
3238 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3239 6841da00 2019-08-08 stsp {
3240 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3241 bd8de430 2019-10-04 stsp
3242 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3243 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3244 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3245 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3246 bd8de430 2019-10-04 stsp
3247 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3248 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3249 6841da00 2019-08-08 stsp
3250 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3251 bd8de430 2019-10-04 stsp continue;
3252 bd8de430 2019-10-04 stsp pattern += 3;
3253 bd8de430 2019-10-04 stsp p = path;
3254 bd8de430 2019-10-04 stsp while (*p) {
3255 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3256 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3257 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3258 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3259 bd8de430 2019-10-04 stsp p++;
3260 bd8de430 2019-10-04 stsp while (*p == '/')
3261 bd8de430 2019-10-04 stsp p++;
3262 bd8de430 2019-10-04 stsp continue;
3263 bd8de430 2019-10-04 stsp }
3264 bd8de430 2019-10-04 stsp return 1;
3265 bd8de430 2019-10-04 stsp }
3266 bd8de430 2019-10-04 stsp }
3267 bd8de430 2019-10-04 stsp }
3268 bd8de430 2019-10-04 stsp
3269 6841da00 2019-08-08 stsp /*
3270 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3271 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3272 6841da00 2019-08-08 stsp * ignores backwards.
3273 6841da00 2019-08-08 stsp */
3274 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3275 6841da00 2019-08-08 stsp while (pe) {
3276 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3277 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3278 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3279 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3280 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3281 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3282 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3283 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3284 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3285 6841da00 2019-08-08 stsp continue;
3286 6841da00 2019-08-08 stsp return 1;
3287 6841da00 2019-08-08 stsp }
3288 6841da00 2019-08-08 stsp }
3289 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3290 6841da00 2019-08-08 stsp }
3291 6841da00 2019-08-08 stsp
3292 6841da00 2019-08-08 stsp return 0;
3293 6841da00 2019-08-08 stsp }
3294 6841da00 2019-08-08 stsp
3295 6841da00 2019-08-08 stsp static const struct got_error *
3296 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3297 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3298 6841da00 2019-08-08 stsp {
3299 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3300 6841da00 2019-08-08 stsp char *ignorespath;
3301 886cec17 2019-12-15 stsp int fd = -1;
3302 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3303 6841da00 2019-08-08 stsp
3304 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3305 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3306 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3307 6841da00 2019-08-08 stsp
3308 886cec17 2019-12-15 stsp if (dirfd != -1) {
3309 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3310 886cec17 2019-12-15 stsp if (fd == -1) {
3311 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3312 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3313 886cec17 2019-12-15 stsp ignorespath);
3314 886cec17 2019-12-15 stsp } else {
3315 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3316 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3317 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3318 886cec17 2019-12-15 stsp ignorespath);
3319 886cec17 2019-12-15 stsp else {
3320 886cec17 2019-12-15 stsp fd = -1;
3321 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3322 886cec17 2019-12-15 stsp }
3323 886cec17 2019-12-15 stsp }
3324 886cec17 2019-12-15 stsp } else {
3325 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3326 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3327 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3328 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3329 886cec17 2019-12-15 stsp ignorespath);
3330 886cec17 2019-12-15 stsp } else
3331 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3332 886cec17 2019-12-15 stsp }
3333 6841da00 2019-08-08 stsp
3334 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3335 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3336 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3337 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3338 6841da00 2019-08-08 stsp free(ignorespath);
3339 6841da00 2019-08-08 stsp return err;
3340 f8d1f275 2019-02-04 stsp }
3341 f8d1f275 2019-02-04 stsp
3342 f8d1f275 2019-02-04 stsp static const struct got_error *
3343 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3344 f8d1f275 2019-02-04 stsp {
3345 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3346 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3347 f8d1f275 2019-02-04 stsp char *path = NULL;
3348 f8d1f275 2019-02-04 stsp
3349 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3350 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3351 0584f854 2019-04-06 stsp
3352 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3353 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3354 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3355 f8d1f275 2019-02-04 stsp } else {
3356 f8d1f275 2019-02-04 stsp path = de->d_name;
3357 f8d1f275 2019-02-04 stsp }
3358 f8d1f275 2019-02-04 stsp
3359 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3360 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3361 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3362 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3363 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3364 f8d1f275 2019-02-04 stsp if (parent_path[0])
3365 f8d1f275 2019-02-04 stsp free(path);
3366 b72f483a 2019-02-05 stsp return err;
3367 f8d1f275 2019-02-04 stsp }
3368 f8d1f275 2019-02-04 stsp
3369 347d1d3e 2019-07-12 stsp static const struct got_error *
3370 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3371 3143d852 2020-06-25 stsp {
3372 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3373 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3374 3143d852 2020-06-25 stsp
3375 3143d852 2020-06-25 stsp if (a->no_ignores)
3376 3143d852 2020-06-25 stsp return NULL;
3377 3143d852 2020-06-25 stsp
3378 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3379 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3380 3143d852 2020-06-25 stsp if (err)
3381 3143d852 2020-06-25 stsp return err;
3382 3143d852 2020-06-25 stsp
3383 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3384 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3385 3143d852 2020-06-25 stsp
3386 3143d852 2020-06-25 stsp return err;
3387 3143d852 2020-06-25 stsp }
3388 3143d852 2020-06-25 stsp
3389 3143d852 2020-06-25 stsp static const struct got_error *
3390 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3391 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3392 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3393 abb4604f 2019-07-27 stsp {
3394 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3395 abb4604f 2019-07-27 stsp struct stat sb;
3396 abb4604f 2019-07-27 stsp
3397 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3398 abb4604f 2019-07-27 stsp if (ie)
3399 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3400 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3401 abb4604f 2019-07-27 stsp
3402 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3403 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3404 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3405 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3406 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3407 abb4604f 2019-07-27 stsp return NULL;
3408 abb4604f 2019-07-27 stsp }
3409 abb4604f 2019-07-27 stsp
3410 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3411 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3412 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3413 abb4604f 2019-07-27 stsp
3414 abb4604f 2019-07-27 stsp return NULL;
3415 3143d852 2020-06-25 stsp }
3416 3143d852 2020-06-25 stsp
3417 3143d852 2020-06-25 stsp static const struct got_error *
3418 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3419 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3420 3143d852 2020-06-25 stsp {
3421 3143d852 2020-06-25 stsp const struct got_error *err;
3422 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3423 3143d852 2020-06-25 stsp
3424 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3425 3143d852 2020-06-25 stsp ".cvsignore");
3426 3143d852 2020-06-25 stsp if (err)
3427 3143d852 2020-06-25 stsp return err;
3428 3143d852 2020-06-25 stsp
3429 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3430 3143d852 2020-06-25 stsp ".gitignore");
3431 3143d852 2020-06-25 stsp if (err)
3432 3143d852 2020-06-25 stsp return err;
3433 3143d852 2020-06-25 stsp
3434 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3435 3143d852 2020-06-25 stsp if (err) {
3436 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3437 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3438 3143d852 2020-06-25 stsp return err;
3439 3143d852 2020-06-25 stsp }
3440 3143d852 2020-06-25 stsp for (;;) {
3441 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3442 3143d852 2020-06-25 stsp ".cvsignore");
3443 3143d852 2020-06-25 stsp if (err)
3444 3143d852 2020-06-25 stsp break;
3445 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3446 3143d852 2020-06-25 stsp ".gitignore");
3447 3143d852 2020-06-25 stsp if (err)
3448 3143d852 2020-06-25 stsp break;
3449 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3450 3143d852 2020-06-25 stsp if (err) {
3451 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3452 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3453 3143d852 2020-06-25 stsp break;
3454 3143d852 2020-06-25 stsp }
3455 b737c85a 2020-06-26 stsp free(parent_path);
3456 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3457 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3458 3143d852 2020-06-25 stsp }
3459 3143d852 2020-06-25 stsp
3460 b737c85a 2020-06-26 stsp free(parent_path);
3461 b737c85a 2020-06-26 stsp free(next_parent_path);
3462 3143d852 2020-06-25 stsp return err;
3463 abb4604f 2019-07-27 stsp }
3464 abb4604f 2019-07-27 stsp
3465 abb4604f 2019-07-27 stsp static const struct got_error *
3466 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3467 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3468 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3469 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3470 f2a9dc41 2019-12-13 tracey int report_unchanged)
3471 f8d1f275 2019-02-04 stsp {
3472 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3473 6fc93f37 2019-12-13 stsp int fd = -1;
3474 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3475 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3476 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3477 3143d852 2020-06-25 stsp
3478 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3479 f8d1f275 2019-02-04 stsp
3480 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3481 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3482 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3483 8dc303cc 2019-07-27 stsp
3484 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3485 6fc93f37 2019-12-13 stsp if (fd == -1) {
3486 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3487 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3488 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3489 abb4604f 2019-07-27 stsp else
3490 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3491 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3492 f2a9dc41 2019-12-13 tracey report_unchanged);
3493 abb4604f 2019-07-27 stsp } else {
3494 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3495 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3496 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3497 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3498 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3499 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3500 abb4604f 2019-07-27 stsp arg.status_path = path;
3501 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3502 abb4604f 2019-07-27 stsp arg.repo = repo;
3503 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3504 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3505 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3506 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3507 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3508 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3509 022fae89 2019-12-06 tracey if (!no_ignores) {
3510 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3511 3143d852 2020-06-25 stsp worktree->root_path, path);
3512 3143d852 2020-06-25 stsp if (err)
3513 3143d852 2020-06-25 stsp goto done;
3514 022fae89 2019-12-06 tracey }
3515 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3516 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3517 927df6b7 2019-02-10 stsp }
3518 3143d852 2020-06-25 stsp done:
3519 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3520 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3521 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3522 927df6b7 2019-02-10 stsp free(ondisk_path);
3523 347d1d3e 2019-07-12 stsp return err;
3524 347d1d3e 2019-07-12 stsp }
3525 347d1d3e 2019-07-12 stsp
3526 347d1d3e 2019-07-12 stsp const struct got_error *
3527 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3528 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3529 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3530 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3531 347d1d3e 2019-07-12 stsp {
3532 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3533 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3534 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3535 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3536 347d1d3e 2019-07-12 stsp
3537 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3538 347d1d3e 2019-07-12 stsp if (err)
3539 347d1d3e 2019-07-12 stsp return err;
3540 347d1d3e 2019-07-12 stsp
3541 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3542 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3543 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3544 72ea6654 2019-07-27 stsp if (err)
3545 72ea6654 2019-07-27 stsp break;
3546 72ea6654 2019-07-27 stsp }
3547 f8d1f275 2019-02-04 stsp free(fileindex_path);
3548 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3549 f8d1f275 2019-02-04 stsp return err;
3550 f8d1f275 2019-02-04 stsp }
3551 6c7ab921 2019-03-18 stsp
3552 6c7ab921 2019-03-18 stsp const struct got_error *
3553 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3554 6c7ab921 2019-03-18 stsp const char *arg)
3555 6c7ab921 2019-03-18 stsp {
3556 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3557 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3558 6c7ab921 2019-03-18 stsp size_t len;
3559 00bb5ea0 2020-07-23 stsp struct stat sb;
3560 6c7ab921 2019-03-18 stsp
3561 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3562 6c7ab921 2019-03-18 stsp
3563 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3564 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3565 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3566 00bb5ea0 2020-07-23 stsp
3567 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3568 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3569 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3570 00bb5ea0 2020-07-23 stsp goto done;
3571 00bb5ea0 2020-07-23 stsp }
3572 00bb5ea0 2020-07-23 stsp }
3573 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3574 00bb5ea0 2020-07-23 stsp /*
3575 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3576 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3577 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3578 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3579 00bb5ea0 2020-07-23 stsp */
3580 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3581 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3582 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3583 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3584 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3585 00bb5ea0 2020-07-23 stsp goto done;
3586 00bb5ea0 2020-07-23 stsp }
3587 00bb5ea0 2020-07-23 stsp
3588 00bb5ea0 2020-07-23 stsp }
3589 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3590 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3591 00bb5ea0 2020-07-23 stsp if (err)
3592 d0710d08 2019-07-22 stsp goto done;
3593 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3594 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3595 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3596 00bb5ea0 2020-07-23 stsp goto done;
3597 00bb5ea0 2020-07-23 stsp }
3598 00bb5ea0 2020-07-23 stsp } else {
3599 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3600 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3601 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3602 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3603 00bb5ea0 2020-07-23 stsp goto done;
3604 00bb5ea0 2020-07-23 stsp }
3605 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3606 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3607 00bb5ea0 2020-07-23 stsp goto done;
3608 00bb5ea0 2020-07-23 stsp }
3609 d0710d08 2019-07-22 stsp }
3610 d0710d08 2019-07-22 stsp }
3611 6c7ab921 2019-03-18 stsp
3612 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3613 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3614 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3615 6c7ab921 2019-03-18 stsp goto done;
3616 6c7ab921 2019-03-18 stsp }
3617 6c7ab921 2019-03-18 stsp
3618 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3619 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3620 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3621 f6d88e1a 2019-05-29 stsp if (err)
3622 f6d88e1a 2019-05-29 stsp goto done;
3623 f6d88e1a 2019-05-29 stsp } else {
3624 f6d88e1a 2019-05-29 stsp path = strdup("");
3625 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3626 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3627 f6d88e1a 2019-05-29 stsp goto done;
3628 f6d88e1a 2019-05-29 stsp }
3629 6c7ab921 2019-03-18 stsp }
3630 6c7ab921 2019-03-18 stsp
3631 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3632 6c7ab921 2019-03-18 stsp len = strlen(path);
3633 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3634 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3635 6c7ab921 2019-03-18 stsp len--;
3636 6c7ab921 2019-03-18 stsp }
3637 6c7ab921 2019-03-18 stsp done:
3638 6c7ab921 2019-03-18 stsp free(resolved);
3639 d0710d08 2019-07-22 stsp free(cwd);
3640 6c7ab921 2019-03-18 stsp if (err == NULL)
3641 6c7ab921 2019-03-18 stsp *wt_path = path;
3642 6c7ab921 2019-03-18 stsp else
3643 6c7ab921 2019-03-18 stsp free(path);
3644 d00136be 2019-03-26 stsp return err;
3645 d00136be 2019-03-26 stsp }
3646 d00136be 2019-03-26 stsp
3647 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3648 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3649 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3650 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3651 4e68cba3 2019-11-23 stsp void *progress_arg;
3652 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3653 4e68cba3 2019-11-23 stsp };
3654 4e68cba3 2019-11-23 stsp
3655 4e68cba3 2019-11-23 stsp static const struct got_error *
3656 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3657 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3658 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3659 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3660 07f5b47a 2019-06-02 stsp {
3661 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3662 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3663 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3664 6d022e97 2019-08-04 stsp struct stat sb;
3665 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3666 07f5b47a 2019-06-02 stsp
3667 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3668 dbb83fbd 2019-12-12 stsp relpath) == -1)
3669 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3670 dbb83fbd 2019-12-12 stsp
3671 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3672 6d022e97 2019-08-04 stsp if (ie) {
3673 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3674 12463d8b 2019-12-13 stsp de_name, a->repo);
3675 6d022e97 2019-08-04 stsp if (err)
3676 dbb83fbd 2019-12-12 stsp goto done;
3677 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3678 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3679 dbb83fbd 2019-12-12 stsp goto done;
3680 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3681 dbb83fbd 2019-12-12 stsp if (err)
3682 dbb83fbd 2019-12-12 stsp goto done;
3683 6d022e97 2019-08-04 stsp }
3684 07f5b47a 2019-06-02 stsp
3685 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3686 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3687 dbb83fbd 2019-12-12 stsp goto done;
3688 4e68cba3 2019-11-23 stsp }
3689 07f5b47a 2019-06-02 stsp
3690 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3691 4e68cba3 2019-11-23 stsp if (err)
3692 4e68cba3 2019-11-23 stsp goto done;
3693 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3694 3969253a 2020-03-07 stsp if (err) {
3695 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3696 3969253a 2020-03-07 stsp goto done;
3697 3969253a 2020-03-07 stsp }
3698 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3699 07f5b47a 2019-06-02 stsp if (err) {
3700 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3701 4e68cba3 2019-11-23 stsp goto done;
3702 07f5b47a 2019-06-02 stsp }
3703 4e68cba3 2019-11-23 stsp done:
3704 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3705 dbb83fbd 2019-12-12 stsp if (err)
3706 dbb83fbd 2019-12-12 stsp return err;
3707 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3708 dbb83fbd 2019-12-12 stsp return NULL;
3709 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3710 07f5b47a 2019-06-02 stsp }
3711 07f5b47a 2019-06-02 stsp
3712 d00136be 2019-03-26 stsp const struct got_error *
3713 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3714 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3715 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3716 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3717 d00136be 2019-03-26 stsp {
3718 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3719 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3720 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3721 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3722 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3723 d00136be 2019-03-26 stsp
3724 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3725 d00136be 2019-03-26 stsp if (err)
3726 d00136be 2019-03-26 stsp return err;
3727 d00136be 2019-03-26 stsp
3728 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3729 d00136be 2019-03-26 stsp if (err)
3730 d00136be 2019-03-26 stsp goto done;
3731 d00136be 2019-03-26 stsp
3732 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3733 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3734 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3735 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3736 4e68cba3 2019-11-23 stsp saa.repo = repo;
3737 4e68cba3 2019-11-23 stsp
3738 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3739 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3740 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3741 1dd54920 2019-05-11 stsp if (err)
3742 af12c6b9 2019-06-04 stsp break;
3743 1dd54920 2019-05-11 stsp }
3744 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3745 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3746 af12c6b9 2019-06-04 stsp err = sync_err;
3747 d00136be 2019-03-26 stsp done:
3748 fb399478 2019-07-12 stsp free(fileindex_path);
3749 d00136be 2019-03-26 stsp if (fileindex)
3750 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3751 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3752 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3753 d00136be 2019-03-26 stsp err = unlockerr;
3754 6c7ab921 2019-03-18 stsp return err;
3755 6c7ab921 2019-03-18 stsp }
3756 17ed4618 2019-06-02 stsp
3757 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3758 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3759 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3760 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3761 f2a9dc41 2019-12-13 tracey void *progress_arg;
3762 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3763 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3764 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3765 766841c2 2020-08-13 stsp const char *status_codes;
3766 f2a9dc41 2019-12-13 tracey };
3767 f2a9dc41 2019-12-13 tracey
3768 f2a9dc41 2019-12-13 tracey static const struct got_error *
3769 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3770 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3771 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3772 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3773 17ed4618 2019-06-02 stsp {
3774 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3775 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3776 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3777 17ed4618 2019-06-02 stsp struct stat sb;
3778 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3779 17ed4618 2019-06-02 stsp
3780 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3781 17ed4618 2019-06-02 stsp if (ie == NULL)
3782 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3783 2ec1f75b 2019-03-26 stsp
3784 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3785 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3786 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3787 9acbc4fa 2019-08-03 stsp return NULL;
3788 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3789 9acbc4fa 2019-08-03 stsp }
3790 9acbc4fa 2019-08-03 stsp
3791 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3792 f2a9dc41 2019-12-13 tracey relpath) == -1)
3793 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3794 f2a9dc41 2019-12-13 tracey
3795 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3796 12463d8b 2019-12-13 stsp a->repo);
3797 17ed4618 2019-06-02 stsp if (err)
3798 f2a9dc41 2019-12-13 tracey goto done;
3799 17ed4618 2019-06-02 stsp
3800 766841c2 2020-08-13 stsp if (a->status_codes) {
3801 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3802 766841c2 2020-08-13 stsp int i;
3803 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3804 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3805 766841c2 2020-08-13 stsp break;
3806 766841c2 2020-08-13 stsp }
3807 766841c2 2020-08-13 stsp if (i == ncodes) {
3808 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3809 766841c2 2020-08-13 stsp free(ondisk_path);
3810 766841c2 2020-08-13 stsp return NULL;
3811 766841c2 2020-08-13 stsp }
3812 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3813 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3814 766841c2 2020-08-13 stsp static char msg[64];
3815 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3816 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3817 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3818 766841c2 2020-08-13 stsp goto done;
3819 766841c2 2020-08-13 stsp }
3820 766841c2 2020-08-13 stsp }
3821 766841c2 2020-08-13 stsp
3822 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3823 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3824 f2a9dc41 2019-12-13 tracey goto done;
3825 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3826 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3827 f2a9dc41 2019-12-13 tracey goto done;
3828 f2a9dc41 2019-12-13 tracey }
3829 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3830 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3831 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3832 f2a9dc41 2019-12-13 tracey goto done;
3833 f2a9dc41 2019-12-13 tracey }
3834 17ed4618 2019-06-02 stsp }
3835 17ed4618 2019-06-02 stsp
3836 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3837 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3838 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3839 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3840 12463d8b 2019-12-13 stsp ondisk_path);
3841 12463d8b 2019-12-13 stsp goto done;
3842 12463d8b 2019-12-13 stsp }
3843 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3844 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3845 12463d8b 2019-12-13 stsp goto done;
3846 15341bfd 2020-03-05 tracey }
3847 15341bfd 2020-03-05 tracey
3848 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3849 15341bfd 2020-03-05 tracey
3850 15341bfd 2020-03-05 tracey if (parent == NULL) {
3851 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3852 15341bfd 2020-03-05 tracey goto done;
3853 15341bfd 2020-03-05 tracey }
3854 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3855 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3856 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3857 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3858 15341bfd 2020-03-05 tracey parent);
3859 15341bfd 2020-03-05 tracey break;
3860 15341bfd 2020-03-05 tracey }
3861 15341bfd 2020-03-05 tracey parent = dirname(parent);
3862 15341bfd 2020-03-05 tracey if (parent == NULL) {
3863 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3864 15341bfd 2020-03-05 tracey goto done;
3865 15341bfd 2020-03-05 tracey }
3866 12463d8b 2019-12-13 stsp }
3867 f2a9dc41 2019-12-13 tracey }
3868 17ed4618 2019-06-02 stsp
3869 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3870 f2a9dc41 2019-12-13 tracey done:
3871 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3872 f2a9dc41 2019-12-13 tracey if (err)
3873 f2a9dc41 2019-12-13 tracey return err;
3874 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3875 f2a9dc41 2019-12-13 tracey return NULL;
3876 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3877 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3878 17ed4618 2019-06-02 stsp }
3879 17ed4618 2019-06-02 stsp
3880 2ec1f75b 2019-03-26 stsp const struct got_error *
3881 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3882 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3883 766841c2 2020-08-13 stsp const char *status_codes,
3884 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3885 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3886 2ec1f75b 2019-03-26 stsp {
3887 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3888 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3889 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3890 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3891 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3892 2ec1f75b 2019-03-26 stsp
3893 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3894 2ec1f75b 2019-03-26 stsp if (err)
3895 2ec1f75b 2019-03-26 stsp return err;
3896 2ec1f75b 2019-03-26 stsp
3897 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3898 2ec1f75b 2019-03-26 stsp if (err)
3899 2ec1f75b 2019-03-26 stsp goto done;
3900 f2a9dc41 2019-12-13 tracey
3901 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3902 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3903 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3904 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3905 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3906 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3907 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3908 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
3909 2ec1f75b 2019-03-26 stsp
3910 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3911 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3912 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3913 17ed4618 2019-06-02 stsp if (err)
3914 af12c6b9 2019-06-04 stsp break;
3915 2ec1f75b 2019-03-26 stsp }
3916 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3917 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3918 af12c6b9 2019-06-04 stsp err = sync_err;
3919 2ec1f75b 2019-03-26 stsp done:
3920 fb399478 2019-07-12 stsp free(fileindex_path);
3921 2ec1f75b 2019-03-26 stsp if (fileindex)
3922 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3923 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3924 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3925 2ec1f75b 2019-03-26 stsp err = unlockerr;
3926 33aa809d 2019-08-08 stsp return err;
3927 33aa809d 2019-08-08 stsp }
3928 33aa809d 2019-08-08 stsp
3929 33aa809d 2019-08-08 stsp static const struct got_error *
3930 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3931 33aa809d 2019-08-08 stsp {
3932 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3933 33aa809d 2019-08-08 stsp char *line = NULL;
3934 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3935 33aa809d 2019-08-08 stsp ssize_t linelen;
3936 33aa809d 2019-08-08 stsp
3937 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3938 33aa809d 2019-08-08 stsp if (linelen == -1) {
3939 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3940 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3941 33aa809d 2019-08-08 stsp goto done;
3942 33aa809d 2019-08-08 stsp }
3943 33aa809d 2019-08-08 stsp return NULL;
3944 33aa809d 2019-08-08 stsp }
3945 33aa809d 2019-08-08 stsp if (outfile) {
3946 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3947 33aa809d 2019-08-08 stsp if (n != linelen) {
3948 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3949 33aa809d 2019-08-08 stsp goto done;
3950 33aa809d 2019-08-08 stsp }
3951 33aa809d 2019-08-08 stsp }
3952 33aa809d 2019-08-08 stsp if (rejectfile) {
3953 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3954 33aa809d 2019-08-08 stsp if (n != linelen)
3955 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3956 33aa809d 2019-08-08 stsp }
3957 33aa809d 2019-08-08 stsp done:
3958 33aa809d 2019-08-08 stsp free(line);
3959 2ec1f75b 2019-03-26 stsp return err;
3960 2ec1f75b 2019-03-26 stsp }
3961 1f1abb7e 2019-08-08 stsp
3962 33aa809d 2019-08-08 stsp static const struct got_error *
3963 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3964 33aa809d 2019-08-08 stsp {
3965 33aa809d 2019-08-08 stsp char *line = NULL;
3966 33aa809d 2019-08-08 stsp size_t linesize = 0;
3967 33aa809d 2019-08-08 stsp ssize_t linelen;
3968 33aa809d 2019-08-08 stsp
3969 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3970 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3971 500467ff 2019-09-25 hiltjo if (ferror(f))
3972 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3973 500467ff 2019-09-25 hiltjo return NULL;
3974 500467ff 2019-09-25 hiltjo }
3975 33aa809d 2019-08-08 stsp free(line);
3976 33aa809d 2019-08-08 stsp return NULL;
3977 33aa809d 2019-08-08 stsp }
3978 33aa809d 2019-08-08 stsp
3979 33aa809d 2019-08-08 stsp static const struct got_error *
3980 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3981 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3982 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3983 33aa809d 2019-08-08 stsp {
3984 33aa809d 2019-08-08 stsp const struct got_error *err;
3985 33aa809d 2019-08-08 stsp
3986 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3987 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3988 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3989 33aa809d 2019-08-08 stsp if (err)
3990 33aa809d 2019-08-08 stsp return err;
3991 33aa809d 2019-08-08 stsp (*line_cur1)++;
3992 33aa809d 2019-08-08 stsp }
3993 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3994 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3995 33aa809d 2019-08-08 stsp if (rejectfile)
3996 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3997 33aa809d 2019-08-08 stsp else
3998 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3999 33aa809d 2019-08-08 stsp if (err)
4000 33aa809d 2019-08-08 stsp return err;
4001 33aa809d 2019-08-08 stsp (*line_cur2)++;
4002 33aa809d 2019-08-08 stsp }
4003 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4004 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4005 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4006 33aa809d 2019-08-08 stsp if (err)
4007 33aa809d 2019-08-08 stsp return err;
4008 33aa809d 2019-08-08 stsp (*line_cur2)++;
4009 33aa809d 2019-08-08 stsp }
4010 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4011 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4012 33aa809d 2019-08-08 stsp if (rejectfile)
4013 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4014 33aa809d 2019-08-08 stsp else
4015 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4016 33aa809d 2019-08-08 stsp if (err)
4017 33aa809d 2019-08-08 stsp return err;
4018 33aa809d 2019-08-08 stsp (*line_cur1)++;
4019 33aa809d 2019-08-08 stsp }
4020 f1e81a05 2019-08-10 stsp
4021 f1e81a05 2019-08-10 stsp return NULL;
4022 f1e81a05 2019-08-10 stsp }
4023 f1e81a05 2019-08-10 stsp
4024 f1e81a05 2019-08-10 stsp static const struct got_error *
4025 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4026 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4027 f1e81a05 2019-08-10 stsp {
4028 f1e81a05 2019-08-10 stsp const struct got_error *err;
4029 f1e81a05 2019-08-10 stsp
4030 f1e81a05 2019-08-10 stsp if (outfile) {
4031 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4032 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4033 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4034 f1e81a05 2019-08-10 stsp if (err)
4035 f1e81a05 2019-08-10 stsp return err;
4036 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4037 f1e81a05 2019-08-10 stsp }
4038 f1e81a05 2019-08-10 stsp }
4039 f1e81a05 2019-08-10 stsp if (rejectfile) {
4040 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4041 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4042 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4043 f1e81a05 2019-08-10 stsp if (err)
4044 f1e81a05 2019-08-10 stsp return err;
4045 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4046 f1e81a05 2019-08-10 stsp }
4047 33aa809d 2019-08-08 stsp }
4048 33aa809d 2019-08-08 stsp
4049 33aa809d 2019-08-08 stsp return NULL;
4050 33aa809d 2019-08-08 stsp }
4051 33aa809d 2019-08-08 stsp
4052 33aa809d 2019-08-08 stsp static const struct got_error *
4053 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4054 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4055 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4056 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
4057 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4058 33aa809d 2019-08-08 stsp {
4059 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4060 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4061 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4062 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4063 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4064 33aa809d 2019-08-08 stsp long pos1, pos2;
4065 33aa809d 2019-08-08 stsp FILE *hunkfile;
4066 33aa809d 2019-08-08 stsp
4067 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4068 33aa809d 2019-08-08 stsp
4069 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4070 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4071 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4072 33aa809d 2019-08-08 stsp
4073 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4074 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4075 33aa809d 2019-08-08 stsp
4076 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4077 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4078 33aa809d 2019-08-08 stsp
4079 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4080 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4081 33aa809d 2019-08-08 stsp goto done;
4082 33aa809d 2019-08-08 stsp }
4083 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4084 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4085 33aa809d 2019-08-08 stsp goto done;
4086 33aa809d 2019-08-08 stsp }
4087 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4088 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4089 33aa809d 2019-08-08 stsp goto done;
4090 33aa809d 2019-08-08 stsp }
4091 33aa809d 2019-08-08 stsp
4092 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4093 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4094 33aa809d 2019-08-08 stsp if (err)
4095 33aa809d 2019-08-08 stsp goto done;
4096 33aa809d 2019-08-08 stsp
4097 33aa809d 2019-08-08 stsp switch (*choice) {
4098 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4099 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4100 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4101 33aa809d 2019-08-08 stsp break;
4102 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4103 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4104 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4105 33aa809d 2019-08-08 stsp break;
4106 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4107 33aa809d 2019-08-08 stsp break;
4108 33aa809d 2019-08-08 stsp default:
4109 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4110 33aa809d 2019-08-08 stsp break;
4111 33aa809d 2019-08-08 stsp }
4112 33aa809d 2019-08-08 stsp done:
4113 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4114 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4115 33aa809d 2019-08-08 stsp return err;
4116 33aa809d 2019-08-08 stsp }
4117 33aa809d 2019-08-08 stsp
4118 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4119 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4120 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4121 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4122 1f1abb7e 2019-08-08 stsp void *progress_arg;
4123 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4124 33aa809d 2019-08-08 stsp void *patch_arg;
4125 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4126 1f1abb7e 2019-08-08 stsp };
4127 a129376b 2019-03-28 stsp
4128 e20a8b6f 2019-06-04 stsp static const struct got_error *
4129 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4130 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4131 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4132 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4133 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4134 33aa809d 2019-08-08 stsp {
4135 33aa809d 2019-08-08 stsp const struct got_error *err;
4136 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4137 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4138 1ebedb77 2019-10-19 stsp int fd2 = -1;
4139 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4140 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4141 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4142 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4143 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4144 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4145 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4146 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4147 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4148 33aa809d 2019-08-08 stsp int n = 0;
4149 33aa809d 2019-08-08 stsp
4150 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4151 33aa809d 2019-08-08 stsp
4152 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4153 33aa809d 2019-08-08 stsp if (err)
4154 33aa809d 2019-08-08 stsp return err;
4155 33aa809d 2019-08-08 stsp
4156 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4157 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4158 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4159 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4160 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4161 fa3cef63 2020-07-23 stsp goto done;
4162 fa3cef63 2020-07-23 stsp }
4163 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4164 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4165 fa3cef63 2020-07-23 stsp if (link_len == -1)
4166 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4167 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4168 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4169 12463d8b 2019-12-13 stsp }
4170 12463d8b 2019-12-13 stsp } else {
4171 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4172 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4173 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4174 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4175 fa3cef63 2020-07-23 stsp goto done;
4176 fa3cef63 2020-07-23 stsp }
4177 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4178 fa3cef63 2020-07-23 stsp sizeof(link_target));
4179 fa3cef63 2020-07-23 stsp if (link_len == -1)
4180 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4181 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4182 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4183 12463d8b 2019-12-13 stsp }
4184 1ebedb77 2019-10-19 stsp }
4185 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4186 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4187 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4188 fa3cef63 2020-07-23 stsp goto done;
4189 fa3cef63 2020-07-23 stsp }
4190 1ebedb77 2019-10-19 stsp
4191 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4192 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4193 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4194 fa3cef63 2020-07-23 stsp goto done;
4195 fa3cef63 2020-07-23 stsp }
4196 fa3cef63 2020-07-23 stsp fd2 = -1;
4197 fa3cef63 2020-07-23 stsp } else {
4198 fa3cef63 2020-07-23 stsp size_t n;
4199 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4200 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4201 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4202 fa3cef63 2020-07-23 stsp goto done;
4203 fa3cef63 2020-07-23 stsp }
4204 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4205 fa3cef63 2020-07-23 stsp if (n != link_len) {
4206 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4207 fa3cef63 2020-07-23 stsp goto done;
4208 fa3cef63 2020-07-23 stsp }
4209 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4210 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4211 fa3cef63 2020-07-23 stsp goto done;
4212 fa3cef63 2020-07-23 stsp }
4213 fa3cef63 2020-07-23 stsp rewind(f2);
4214 33aa809d 2019-08-08 stsp }
4215 33aa809d 2019-08-08 stsp
4216 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4217 33aa809d 2019-08-08 stsp if (err)
4218 33aa809d 2019-08-08 stsp goto done;
4219 33aa809d 2019-08-08 stsp
4220 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4221 33aa809d 2019-08-08 stsp if (err)
4222 33aa809d 2019-08-08 stsp goto done;
4223 33aa809d 2019-08-08 stsp
4224 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4225 33aa809d 2019-08-08 stsp if (err)
4226 33aa809d 2019-08-08 stsp goto done;
4227 33aa809d 2019-08-08 stsp
4228 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4229 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4230 33aa809d 2019-08-08 stsp goto done;
4231 33aa809d 2019-08-08 stsp }
4232 33aa809d 2019-08-08 stsp
4233 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4234 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4235 33aa809d 2019-08-08 stsp if (err)
4236 33aa809d 2019-08-08 stsp goto done;
4237 33aa809d 2019-08-08 stsp
4238 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4239 33aa809d 2019-08-08 stsp if (err)
4240 33aa809d 2019-08-08 stsp goto done;
4241 33aa809d 2019-08-08 stsp
4242 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4243 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4244 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4245 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4246 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4247 33aa809d 2019-08-08 stsp int choice;
4248 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4249 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4250 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4251 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4252 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4253 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4254 33aa809d 2019-08-08 stsp if (err)
4255 33aa809d 2019-08-08 stsp goto done;
4256 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4257 33aa809d 2019-08-08 stsp have_content = 1;
4258 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4259 33aa809d 2019-08-08 stsp break;
4260 33aa809d 2019-08-08 stsp }
4261 1ebedb77 2019-10-19 stsp if (have_content) {
4262 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4263 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4264 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4265 1ebedb77 2019-10-19 stsp if (err)
4266 1ebedb77 2019-10-19 stsp goto done;
4267 1ebedb77 2019-10-19 stsp
4268 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4269 fa3cef63 2020-07-23 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4270 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("chmod", path2);
4271 fa3cef63 2020-07-23 stsp goto done;
4272 fa3cef63 2020-07-23 stsp }
4273 1ebedb77 2019-10-19 stsp }
4274 1ebedb77 2019-10-19 stsp }
4275 33aa809d 2019-08-08 stsp done:
4276 33aa809d 2019-08-08 stsp free(id_str);
4277 33aa809d 2019-08-08 stsp if (blob)
4278 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4279 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4280 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4281 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4282 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4283 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4284 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4285 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4286 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4287 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4288 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4289 33aa809d 2019-08-08 stsp if (err || !have_content) {
4290 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4291 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4292 33aa809d 2019-08-08 stsp free(*path_outfile);
4293 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4294 33aa809d 2019-08-08 stsp }
4295 33aa809d 2019-08-08 stsp free(args);
4296 33aa809d 2019-08-08 stsp if (ds) {
4297 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4298 33aa809d 2019-08-08 stsp free(ds);
4299 33aa809d 2019-08-08 stsp }
4300 33aa809d 2019-08-08 stsp if (changes)
4301 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4302 33aa809d 2019-08-08 stsp free(path1);
4303 33aa809d 2019-08-08 stsp return err;
4304 33aa809d 2019-08-08 stsp }
4305 33aa809d 2019-08-08 stsp
4306 33aa809d 2019-08-08 stsp static const struct got_error *
4307 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4308 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4309 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4310 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4311 a129376b 2019-03-28 stsp {
4312 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4313 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4314 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4315 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4316 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4317 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4318 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4319 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4320 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4321 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4322 a129376b 2019-03-28 stsp
4323 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4324 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4325 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4326 d3bcc3d1 2019-08-08 stsp return NULL;
4327 3d69ad8d 2019-08-17 semarie
4328 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4329 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4330 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4331 d3bcc3d1 2019-08-08 stsp
4332 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4333 65084dad 2019-08-08 stsp if (ie == NULL)
4334 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4335 a129376b 2019-03-28 stsp
4336 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4337 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4338 a129376b 2019-03-28 stsp if (err) {
4339 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4340 a129376b 2019-03-28 stsp goto done;
4341 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4342 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4343 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4344 e20a8b6f 2019-06-04 stsp goto done;
4345 e20a8b6f 2019-06-04 stsp }
4346 a129376b 2019-03-28 stsp }
4347 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4348 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4349 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4350 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4351 a129376b 2019-03-28 stsp goto done;
4352 a129376b 2019-03-28 stsp }
4353 a129376b 2019-03-28 stsp } else {
4354 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4355 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4356 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4357 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4358 a129376b 2019-03-28 stsp goto done;
4359 a129376b 2019-03-28 stsp }
4360 a129376b 2019-03-28 stsp } else {
4361 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4362 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4363 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4364 a129376b 2019-03-28 stsp goto done;
4365 a129376b 2019-03-28 stsp }
4366 a129376b 2019-03-28 stsp }
4367 a129376b 2019-03-28 stsp }
4368 a129376b 2019-03-28 stsp
4369 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4370 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4371 a9fa2909 2019-07-27 stsp if (err) {
4372 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4373 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4374 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4375 a9fa2909 2019-07-27 stsp goto done;
4376 a9fa2909 2019-07-27 stsp } else {
4377 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4378 a9fa2909 2019-07-27 stsp if (err)
4379 a9fa2909 2019-07-27 stsp goto done;
4380 a9fa2909 2019-07-27 stsp
4381 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
4382 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
4383 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
4384 a9fa2909 2019-07-27 stsp goto done;
4385 a9fa2909 2019-07-27 stsp }
4386 a9fa2909 2019-07-27 stsp
4387 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4388 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4389 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4390 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4391 a9fa2909 2019-07-27 stsp goto done;
4392 a9fa2909 2019-07-27 stsp }
4393 a129376b 2019-03-28 stsp }
4394 a129376b 2019-03-28 stsp
4395 a129376b 2019-03-28 stsp switch (status) {
4396 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4397 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4398 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4399 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4400 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4401 33aa809d 2019-08-08 stsp if (err)
4402 33aa809d 2019-08-08 stsp goto done;
4403 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4404 33aa809d 2019-08-08 stsp break;
4405 33aa809d 2019-08-08 stsp }
4406 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4407 1f1abb7e 2019-08-08 stsp ie->path);
4408 1ee397ad 2019-07-12 stsp if (err)
4409 1ee397ad 2019-07-12 stsp goto done;
4410 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4411 a129376b 2019-03-28 stsp break;
4412 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4413 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4414 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4415 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4416 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4417 33aa809d 2019-08-08 stsp if (err)
4418 33aa809d 2019-08-08 stsp goto done;
4419 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4420 33aa809d 2019-08-08 stsp break;
4421 33aa809d 2019-08-08 stsp }
4422 33aa809d 2019-08-08 stsp /* fall through */
4423 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4424 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4425 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4426 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4427 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4428 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4429 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4430 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4431 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4432 24278f30 2019-08-03 stsp } else
4433 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4434 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4435 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4436 a129376b 2019-03-28 stsp if (err)
4437 65084dad 2019-08-08 stsp goto done;
4438 65084dad 2019-08-08 stsp
4439 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4440 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4441 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4442 a129376b 2019-03-28 stsp goto done;
4443 65084dad 2019-08-08 stsp }
4444 65084dad 2019-08-08 stsp
4445 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4446 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4447 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4448 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4449 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4450 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4451 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4452 33aa809d 2019-08-08 stsp break;
4453 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4454 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4455 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4456 369fd7e5 2020-07-23 stsp path_content);
4457 369fd7e5 2020-07-23 stsp break;
4458 369fd7e5 2020-07-23 stsp }
4459 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4460 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4461 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4462 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4463 369fd7e5 2020-07-23 stsp } else {
4464 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4465 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4466 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4467 369fd7e5 2020-07-23 stsp goto done;
4468 369fd7e5 2020-07-23 stsp }
4469 33aa809d 2019-08-08 stsp }
4470 33aa809d 2019-08-08 stsp } else {
4471 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4472 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4473 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4474 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4475 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4476 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4477 2e1fa222 2020-07-23 stsp } else {
4478 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4479 2e1fa222 2020-07-23 stsp ie->path,
4480 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4481 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4482 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4483 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4484 2e1fa222 2020-07-23 stsp }
4485 a129376b 2019-03-28 stsp if (err)
4486 a129376b 2019-03-28 stsp goto done;
4487 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4488 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4489 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4490 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4491 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4492 2e1fa222 2020-07-23 stsp if (err)
4493 2e1fa222 2020-07-23 stsp goto done;
4494 2e1fa222 2020-07-23 stsp }
4495 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4496 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4497 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4498 33aa809d 2019-08-08 stsp }
4499 a129376b 2019-03-28 stsp }
4500 a129376b 2019-03-28 stsp break;
4501 e20a8b6f 2019-06-04 stsp }
4502 a129376b 2019-03-28 stsp default:
4503 1f1abb7e 2019-08-08 stsp break;
4504 a129376b 2019-03-28 stsp }
4505 a129376b 2019-03-28 stsp done:
4506 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4507 33aa809d 2019-08-08 stsp free(path_content);
4508 e20a8b6f 2019-06-04 stsp free(parent_path);
4509 a129376b 2019-03-28 stsp free(tree_path);
4510 a129376b 2019-03-28 stsp if (blob)
4511 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4512 a129376b 2019-03-28 stsp if (tree)
4513 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4514 a129376b 2019-03-28 stsp free(tree_id);
4515 e20a8b6f 2019-06-04 stsp return err;
4516 e20a8b6f 2019-06-04 stsp }
4517 e20a8b6f 2019-06-04 stsp
4518 e20a8b6f 2019-06-04 stsp const struct got_error *
4519 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4520 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4521 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4522 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4523 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4524 e20a8b6f 2019-06-04 stsp {
4525 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4526 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4527 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4528 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4529 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4530 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4531 e20a8b6f 2019-06-04 stsp
4532 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4533 e20a8b6f 2019-06-04 stsp if (err)
4534 e20a8b6f 2019-06-04 stsp return err;
4535 e20a8b6f 2019-06-04 stsp
4536 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4537 e20a8b6f 2019-06-04 stsp if (err)
4538 e20a8b6f 2019-06-04 stsp goto done;
4539 e20a8b6f 2019-06-04 stsp
4540 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4541 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4542 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4543 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4544 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4545 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4546 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4547 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4548 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4549 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4550 e20a8b6f 2019-06-04 stsp if (err)
4551 af12c6b9 2019-06-04 stsp break;
4552 e20a8b6f 2019-06-04 stsp }
4553 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4554 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4555 e20a8b6f 2019-06-04 stsp err = sync_err;
4556 af12c6b9 2019-06-04 stsp done:
4557 fb399478 2019-07-12 stsp free(fileindex_path);
4558 a129376b 2019-03-28 stsp if (fileindex)
4559 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4560 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4561 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4562 a129376b 2019-03-28 stsp err = unlockerr;
4563 c4296144 2019-05-09 stsp return err;
4564 c4296144 2019-05-09 stsp }
4565 c4296144 2019-05-09 stsp
4566 cf066bf8 2019-05-09 stsp static void
4567 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4568 cf066bf8 2019-05-09 stsp {
4569 24519714 2019-05-09 stsp free(ct->path);
4570 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4571 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4572 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4573 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4574 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4575 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4576 cf066bf8 2019-05-09 stsp free(ct);
4577 cf066bf8 2019-05-09 stsp }
4578 24519714 2019-05-09 stsp
4579 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4580 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4581 24519714 2019-05-09 stsp struct got_repository *repo;
4582 24519714 2019-05-09 stsp struct got_worktree *worktree;
4583 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4584 5f8a88c6 2019-08-03 stsp int have_staged_files;
4585 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4586 24519714 2019-05-09 stsp };
4587 cf066bf8 2019-05-09 stsp
4588 c4296144 2019-05-09 stsp static const struct got_error *
4589 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4590 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4591 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4592 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4593 c4296144 2019-05-09 stsp {
4594 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4595 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4596 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4597 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4598 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4599 768aea60 2019-05-09 stsp struct stat sb;
4600 c4296144 2019-05-09 stsp
4601 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4602 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4603 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4604 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4605 5f8a88c6 2019-08-03 stsp return NULL;
4606 5f8a88c6 2019-08-03 stsp } else {
4607 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4608 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4609 c4296144 2019-05-09 stsp
4610 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4611 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4612 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4613 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4614 5f8a88c6 2019-08-03 stsp return NULL;
4615 5f8a88c6 2019-08-03 stsp }
4616 0b5cc0d6 2019-05-09 stsp
4617 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4618 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4619 036813ee 2019-05-09 stsp goto done;
4620 036813ee 2019-05-09 stsp }
4621 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4622 036813ee 2019-05-09 stsp parent_path = strdup("");
4623 69960a46 2019-05-09 stsp if (parent_path == NULL)
4624 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4625 69960a46 2019-05-09 stsp } else {
4626 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4627 69960a46 2019-05-09 stsp if (err)
4628 69960a46 2019-05-09 stsp return err;
4629 69960a46 2019-05-09 stsp }
4630 c4296144 2019-05-09 stsp
4631 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4632 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4633 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4634 c4296144 2019-05-09 stsp goto done;
4635 768aea60 2019-05-09 stsp }
4636 768aea60 2019-05-09 stsp
4637 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4638 768aea60 2019-05-09 stsp relpath) == -1) {
4639 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4640 768aea60 2019-05-09 stsp goto done;
4641 768aea60 2019-05-09 stsp }
4642 0aeb8099 2020-07-23 stsp
4643 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4644 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4645 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4646 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4647 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4648 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4649 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4650 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4651 0aeb8099 2020-07-23 stsp break;
4652 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4653 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4654 0aeb8099 2020-07-23 stsp break;
4655 0aeb8099 2020-07-23 stsp default:
4656 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4657 0aeb8099 2020-07-23 stsp goto done;
4658 0aeb8099 2020-07-23 stsp }
4659 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4660 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4661 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4662 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4663 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4664 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4665 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4666 12463d8b 2019-12-13 stsp ct->ondisk_path);
4667 12463d8b 2019-12-13 stsp goto done;
4668 12463d8b 2019-12-13 stsp }
4669 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4670 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4671 768aea60 2019-05-09 stsp goto done;
4672 768aea60 2019-05-09 stsp }
4673 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4674 c4296144 2019-05-09 stsp }
4675 c4296144 2019-05-09 stsp
4676 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4677 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4678 44d03001 2019-05-09 stsp relpath) == -1) {
4679 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4680 44d03001 2019-05-09 stsp goto done;
4681 44d03001 2019-05-09 stsp }
4682 44d03001 2019-05-09 stsp
4683 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4684 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4685 35213c7c 2020-07-23 stsp int is_bad_symlink;
4686 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4687 35213c7c 2020-07-23 stsp ssize_t target_len;
4688 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4689 35213c7c 2020-07-23 stsp sizeof(target_path));
4690 35213c7c 2020-07-23 stsp if (target_len == -1) {
4691 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4692 35213c7c 2020-07-23 stsp ct->ondisk_path);
4693 35213c7c 2020-07-23 stsp goto done;
4694 35213c7c 2020-07-23 stsp }
4695 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4696 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4697 35213c7c 2020-07-23 stsp if (err)
4698 35213c7c 2020-07-23 stsp goto done;
4699 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4700 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4701 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4702 35213c7c 2020-07-23 stsp goto done;
4703 35213c7c 2020-07-23 stsp }
4704 35213c7c 2020-07-23 stsp }
4705 35213c7c 2020-07-23 stsp
4706 35213c7c 2020-07-23 stsp
4707 cf066bf8 2019-05-09 stsp ct->status = status;
4708 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4709 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4710 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4711 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4712 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4713 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4714 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4715 b416585c 2019-05-13 stsp goto done;
4716 b416585c 2019-05-13 stsp }
4717 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4718 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4719 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4720 f0b75401 2019-08-03 stsp goto done;
4721 f0b75401 2019-08-03 stsp }
4722 f0b75401 2019-08-03 stsp }
4723 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4724 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4725 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4726 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4727 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4728 036813ee 2019-05-09 stsp goto done;
4729 036813ee 2019-05-09 stsp }
4730 ca2503ea 2019-05-09 stsp }
4731 24519714 2019-05-09 stsp ct->path = strdup(path);
4732 24519714 2019-05-09 stsp if (ct->path == NULL) {
4733 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4734 24519714 2019-05-09 stsp goto done;
4735 24519714 2019-05-09 stsp }
4736 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4737 c4296144 2019-05-09 stsp done:
4738 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4739 ed175427 2019-05-09 stsp free_commitable(ct);
4740 24519714 2019-05-09 stsp free(parent_path);
4741 036813ee 2019-05-09 stsp free(path);
4742 a129376b 2019-03-28 stsp return err;
4743 a129376b 2019-03-28 stsp }
4744 c4296144 2019-05-09 stsp
4745 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4746 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4747 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4748 036813ee 2019-05-09 stsp struct got_repository *);
4749 ed175427 2019-05-09 stsp
4750 ed175427 2019-05-09 stsp static const struct got_error *
4751 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4752 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4753 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4754 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4755 afa376bf 2019-05-09 stsp struct got_repository *repo)
4756 ed175427 2019-05-09 stsp {
4757 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4758 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4759 036813ee 2019-05-09 stsp char *subpath;
4760 ed175427 2019-05-09 stsp
4761 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4762 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4763 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4764 ed175427 2019-05-09 stsp
4765 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4766 ed175427 2019-05-09 stsp if (err)
4767 ed175427 2019-05-09 stsp return err;
4768 ed175427 2019-05-09 stsp
4769 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4770 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4771 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4772 036813ee 2019-05-09 stsp free(subpath);
4773 036813ee 2019-05-09 stsp return err;
4774 036813ee 2019-05-09 stsp }
4775 ed175427 2019-05-09 stsp
4776 036813ee 2019-05-09 stsp static const struct got_error *
4777 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4778 036813ee 2019-05-09 stsp {
4779 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4780 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4781 ed175427 2019-05-09 stsp
4782 036813ee 2019-05-09 stsp *match = 0;
4783 ed175427 2019-05-09 stsp
4784 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4785 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4786 0f63689d 2019-05-10 stsp return NULL;
4787 036813ee 2019-05-09 stsp }
4788 0b5cc0d6 2019-05-09 stsp
4789 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4790 0f63689d 2019-05-10 stsp if (err)
4791 0f63689d 2019-05-10 stsp return err;
4792 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4793 036813ee 2019-05-09 stsp free(ct_parent_path);
4794 036813ee 2019-05-09 stsp return err;
4795 036813ee 2019-05-09 stsp }
4796 0b5cc0d6 2019-05-09 stsp
4797 768aea60 2019-05-09 stsp static mode_t
4798 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4799 768aea60 2019-05-09 stsp {
4800 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4801 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4802 3d9a4ec4 2020-07-23 stsp
4803 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4804 768aea60 2019-05-09 stsp }
4805 768aea60 2019-05-09 stsp
4806 036813ee 2019-05-09 stsp static const struct got_error *
4807 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4808 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4809 036813ee 2019-05-09 stsp {
4810 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4811 ca2503ea 2019-05-09 stsp
4812 036813ee 2019-05-09 stsp *new_te = NULL;
4813 0b5cc0d6 2019-05-09 stsp
4814 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4815 036813ee 2019-05-09 stsp if (err)
4816 036813ee 2019-05-09 stsp goto done;
4817 0b5cc0d6 2019-05-09 stsp
4818 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4819 036813ee 2019-05-09 stsp
4820 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4821 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4822 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4823 5f8a88c6 2019-08-03 stsp else
4824 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4825 036813ee 2019-05-09 stsp done:
4826 036813ee 2019-05-09 stsp if (err && *new_te) {
4827 56e0773d 2019-11-28 stsp free(*new_te);
4828 036813ee 2019-05-09 stsp *new_te = NULL;
4829 036813ee 2019-05-09 stsp }
4830 036813ee 2019-05-09 stsp return err;
4831 036813ee 2019-05-09 stsp }
4832 036813ee 2019-05-09 stsp
4833 036813ee 2019-05-09 stsp static const struct got_error *
4834 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4835 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4836 036813ee 2019-05-09 stsp {
4837 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4838 036813ee 2019-05-09 stsp char *ct_name;
4839 036813ee 2019-05-09 stsp
4840 036813ee 2019-05-09 stsp *new_te = NULL;
4841 036813ee 2019-05-09 stsp
4842 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4843 036813ee 2019-05-09 stsp if (*new_te == NULL)
4844 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4845 036813ee 2019-05-09 stsp
4846 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4847 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4848 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4849 036813ee 2019-05-09 stsp goto done;
4850 036813ee 2019-05-09 stsp }
4851 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4852 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4853 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4854 036813ee 2019-05-09 stsp goto done;
4855 036813ee 2019-05-09 stsp }
4856 036813ee 2019-05-09 stsp
4857 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4858 036813ee 2019-05-09 stsp
4859 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4860 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4861 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4862 5f8a88c6 2019-08-03 stsp else
4863 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4864 036813ee 2019-05-09 stsp done:
4865 036813ee 2019-05-09 stsp if (err && *new_te) {
4866 56e0773d 2019-11-28 stsp free(*new_te);
4867 036813ee 2019-05-09 stsp *new_te = NULL;
4868 036813ee 2019-05-09 stsp }
4869 036813ee 2019-05-09 stsp return err;
4870 036813ee 2019-05-09 stsp }
4871 036813ee 2019-05-09 stsp
4872 036813ee 2019-05-09 stsp static const struct got_error *
4873 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4874 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4875 036813ee 2019-05-09 stsp {
4876 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4877 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4878 036813ee 2019-05-09 stsp
4879 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4880 036813ee 2019-05-09 stsp if (err)
4881 036813ee 2019-05-09 stsp return err;
4882 036813ee 2019-05-09 stsp if (new_pe == NULL)
4883 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4884 036813ee 2019-05-09 stsp return NULL;
4885 afa376bf 2019-05-09 stsp }
4886 afa376bf 2019-05-09 stsp
4887 afa376bf 2019-05-09 stsp static const struct got_error *
4888 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4889 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4890 afa376bf 2019-05-09 stsp {
4891 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4892 5f8a88c6 2019-08-03 stsp unsigned char status;
4893 5f8a88c6 2019-08-03 stsp
4894 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4895 afa376bf 2019-05-09 stsp ct_path++;
4896 5f8a88c6 2019-08-03 stsp
4897 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4898 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4899 5f8a88c6 2019-08-03 stsp else
4900 5f8a88c6 2019-08-03 stsp status = ct->status;
4901 5f8a88c6 2019-08-03 stsp
4902 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4903 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4904 036813ee 2019-05-09 stsp }
4905 036813ee 2019-05-09 stsp
4906 036813ee 2019-05-09 stsp static const struct got_error *
4907 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4908 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4909 44d03001 2019-05-09 stsp {
4910 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4911 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4912 44d03001 2019-05-09 stsp char *te_path;
4913 44d03001 2019-05-09 stsp
4914 44d03001 2019-05-09 stsp *modified = 0;
4915 44d03001 2019-05-09 stsp
4916 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4917 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4918 44d03001 2019-05-09 stsp te->name) == -1)
4919 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4920 44d03001 2019-05-09 stsp
4921 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4922 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4923 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4924 44d03001 2019-05-09 stsp strlen(te_path));
4925 44d03001 2019-05-09 stsp if (*modified)
4926 44d03001 2019-05-09 stsp break;
4927 44d03001 2019-05-09 stsp }
4928 44d03001 2019-05-09 stsp
4929 44d03001 2019-05-09 stsp free(te_path);
4930 44d03001 2019-05-09 stsp return err;
4931 44d03001 2019-05-09 stsp }
4932 44d03001 2019-05-09 stsp
4933 44d03001 2019-05-09 stsp static const struct got_error *
4934 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4935 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4936 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4937 036813ee 2019-05-09 stsp {
4938 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4939 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4940 036813ee 2019-05-09 stsp
4941 036813ee 2019-05-09 stsp *ctp = NULL;
4942 036813ee 2019-05-09 stsp
4943 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4944 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4945 036813ee 2019-05-09 stsp char *ct_name = NULL;
4946 036813ee 2019-05-09 stsp int path_matches;
4947 036813ee 2019-05-09 stsp
4948 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4949 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4950 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4951 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4952 5f8a88c6 2019-08-03 stsp continue;
4953 5f8a88c6 2019-08-03 stsp } else {
4954 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4955 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4956 5f8a88c6 2019-08-03 stsp continue;
4957 5f8a88c6 2019-08-03 stsp }
4958 036813ee 2019-05-09 stsp
4959 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4960 036813ee 2019-05-09 stsp continue;
4961 036813ee 2019-05-09 stsp
4962 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4963 036813ee 2019-05-09 stsp if (err)
4964 036813ee 2019-05-09 stsp return err;
4965 036813ee 2019-05-09 stsp if (!path_matches)
4966 036813ee 2019-05-09 stsp continue;
4967 036813ee 2019-05-09 stsp
4968 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4969 036813ee 2019-05-09 stsp if (ct_name == NULL)
4970 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4971 036813ee 2019-05-09 stsp
4972 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4973 036813ee 2019-05-09 stsp continue;
4974 036813ee 2019-05-09 stsp
4975 036813ee 2019-05-09 stsp *ctp = ct;
4976 036813ee 2019-05-09 stsp break;
4977 036813ee 2019-05-09 stsp }
4978 2b496619 2019-07-10 stsp
4979 2b496619 2019-07-10 stsp return err;
4980 2b496619 2019-07-10 stsp }
4981 2b496619 2019-07-10 stsp
4982 2b496619 2019-07-10 stsp static const struct got_error *
4983 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4984 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4985 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4986 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4987 2b496619 2019-07-10 stsp struct got_repository *repo)
4988 2b496619 2019-07-10 stsp {
4989 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4990 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4991 2b496619 2019-07-10 stsp char *subtree_path;
4992 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4993 ba580f68 2020-03-22 stsp int nentries;
4994 2b496619 2019-07-10 stsp
4995 2b496619 2019-07-10 stsp *new_tep = NULL;
4996 2b496619 2019-07-10 stsp
4997 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4998 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4999 2b496619 2019-07-10 stsp child_path) == -1)
5000 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5001 036813ee 2019-05-09 stsp
5002 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5003 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5004 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5005 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5006 56e0773d 2019-11-28 stsp
5007 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5008 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5009 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5010 2b496619 2019-07-10 stsp goto done;
5011 2b496619 2019-07-10 stsp }
5012 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5013 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5014 2b496619 2019-07-10 stsp if (err) {
5015 56e0773d 2019-11-28 stsp free(new_te);
5016 2b496619 2019-07-10 stsp goto done;
5017 2b496619 2019-07-10 stsp }
5018 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5019 2b496619 2019-07-10 stsp done:
5020 56e0773d 2019-11-28 stsp free(id);
5021 2b496619 2019-07-10 stsp free(subtree_path);
5022 2b496619 2019-07-10 stsp if (err == NULL)
5023 2b496619 2019-07-10 stsp *new_tep = new_te;
5024 036813ee 2019-05-09 stsp return err;
5025 036813ee 2019-05-09 stsp }
5026 036813ee 2019-05-09 stsp
5027 036813ee 2019-05-09 stsp static const struct got_error *
5028 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5029 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5030 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5031 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5032 036813ee 2019-05-09 stsp struct got_repository *repo)
5033 036813ee 2019-05-09 stsp {
5034 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5035 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5036 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5037 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5038 036813ee 2019-05-09 stsp
5039 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5040 ba580f68 2020-03-22 stsp *nentries = 0;
5041 036813ee 2019-05-09 stsp
5042 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5043 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5044 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5045 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5046 036813ee 2019-05-09 stsp
5047 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5048 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5049 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5050 036813ee 2019-05-09 stsp continue;
5051 036813ee 2019-05-09 stsp
5052 f2b0a8b0 2020-07-31 stsp if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5053 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
5054 036813ee 2019-05-09 stsp continue;
5055 036813ee 2019-05-09 stsp
5056 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5057 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5058 036813ee 2019-05-09 stsp if (err)
5059 036813ee 2019-05-09 stsp goto done;
5060 036813ee 2019-05-09 stsp
5061 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5062 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5063 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5064 036813ee 2019-05-09 stsp if (err)
5065 036813ee 2019-05-09 stsp goto done;
5066 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5067 afa376bf 2019-05-09 stsp if (err)
5068 afa376bf 2019-05-09 stsp goto done;
5069 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5070 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5071 2b496619 2019-07-10 stsp if (err)
5072 2f51b5b3 2019-05-09 stsp goto done;
5073 ba580f68 2020-03-22 stsp (*nentries)++;
5074 2b496619 2019-07-10 stsp } else {
5075 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5076 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5077 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5078 2b496619 2019-07-10 stsp == NULL) {
5079 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5080 2b496619 2019-07-10 stsp child_path, path_base_tree,
5081 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5082 2b496619 2019-07-10 stsp repo);
5083 2b496619 2019-07-10 stsp if (err)
5084 2b496619 2019-07-10 stsp goto done;
5085 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5086 2b496619 2019-07-10 stsp if (err)
5087 2b496619 2019-07-10 stsp goto done;
5088 ba580f68 2020-03-22 stsp (*nentries)++;
5089 9ba0479c 2019-05-10 stsp }
5090 ed175427 2019-05-09 stsp }
5091 2f51b5b3 2019-05-09 stsp }
5092 2f51b5b3 2019-05-09 stsp
5093 2f51b5b3 2019-05-09 stsp if (base_tree) {
5094 56e0773d 2019-11-28 stsp int i, nbase_entries;
5095 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5096 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5097 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5098 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5099 63c5ca5d 2019-08-24 stsp
5100 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5101 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5102 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5103 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5104 63c5ca5d 2019-08-24 stsp if (err)
5105 63c5ca5d 2019-08-24 stsp goto done;
5106 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5107 63c5ca5d 2019-08-24 stsp if (err)
5108 63c5ca5d 2019-08-24 stsp goto done;
5109 ba580f68 2020-03-22 stsp (*nentries)++;
5110 63c5ca5d 2019-08-24 stsp continue;
5111 63c5ca5d 2019-08-24 stsp }
5112 2f51b5b3 2019-05-09 stsp
5113 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5114 44d03001 2019-05-09 stsp int modified;
5115 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5116 036813ee 2019-05-09 stsp if (err)
5117 036813ee 2019-05-09 stsp goto done;
5118 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5119 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5120 2f51b5b3 2019-05-09 stsp if (err)
5121 2f51b5b3 2019-05-09 stsp goto done;
5122 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5123 44d03001 2019-05-09 stsp if (modified) {
5124 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5125 ba580f68 2020-03-22 stsp int nsubentries;
5126 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5127 ba580f68 2020-03-22 stsp &nsubentries, te,
5128 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5129 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5130 44d03001 2019-05-09 stsp if (err)
5131 44d03001 2019-05-09 stsp goto done;
5132 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5133 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5134 ba580f68 2020-03-22 stsp free(new_id);
5135 ba580f68 2020-03-22 stsp continue;
5136 ba580f68 2020-03-22 stsp }
5137 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5138 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5139 56e0773d 2019-11-28 stsp free(new_id);
5140 44d03001 2019-05-09 stsp }
5141 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5142 036813ee 2019-05-09 stsp if (err)
5143 036813ee 2019-05-09 stsp goto done;
5144 ba580f68 2020-03-22 stsp (*nentries)++;
5145 2f51b5b3 2019-05-09 stsp continue;
5146 036813ee 2019-05-09 stsp }
5147 2f51b5b3 2019-05-09 stsp
5148 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5149 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5150 f66c734c 2019-09-22 stsp if (err)
5151 f66c734c 2019-09-22 stsp goto done;
5152 2f51b5b3 2019-05-09 stsp if (ct) {
5153 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5154 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5155 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5156 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5157 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5158 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5159 2f51b5b3 2019-05-09 stsp if (err)
5160 2f51b5b3 2019-05-09 stsp goto done;
5161 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5162 2f51b5b3 2019-05-09 stsp if (err)
5163 2f51b5b3 2019-05-09 stsp goto done;
5164 ba580f68 2020-03-22 stsp (*nentries)++;
5165 2f51b5b3 2019-05-09 stsp }
5166 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5167 b416585c 2019-05-13 stsp status_arg);
5168 afa376bf 2019-05-09 stsp if (err)
5169 afa376bf 2019-05-09 stsp goto done;
5170 2f51b5b3 2019-05-09 stsp } else {
5171 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5172 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5173 2f51b5b3 2019-05-09 stsp if (err)
5174 2f51b5b3 2019-05-09 stsp goto done;
5175 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5176 2f51b5b3 2019-05-09 stsp if (err)
5177 2f51b5b3 2019-05-09 stsp goto done;
5178 ba580f68 2020-03-22 stsp (*nentries)++;
5179 2f51b5b3 2019-05-09 stsp }
5180 ca2503ea 2019-05-09 stsp }
5181 ed175427 2019-05-09 stsp }
5182 0b5cc0d6 2019-05-09 stsp
5183 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5184 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5185 ed175427 2019-05-09 stsp done:
5186 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5187 2e1fa222 2020-07-23 stsp return err;
5188 2e1fa222 2020-07-23 stsp }
5189 2e1fa222 2020-07-23 stsp
5190 2e1fa222 2020-07-23 stsp static const struct got_error *
5191 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5192 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5193 72fd46fa 2019-09-06 stsp int have_staged_files)
5194 ebf99748 2019-05-09 stsp {
5195 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5196 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5197 ebf99748 2019-05-09 stsp
5198 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5199 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5200 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5201 ebf99748 2019-05-09 stsp
5202 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5203 ebf99748 2019-05-09 stsp if (ie) {
5204 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5205 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5206 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5207 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5208 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5209 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5210 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5211 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5212 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5213 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5214 72fd46fa 2019-09-06 stsp !have_staged_files);
5215 ebf99748 2019-05-09 stsp } else
5216 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5217 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5218 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5219 72fd46fa 2019-09-06 stsp !have_staged_files);
5220 ebf99748 2019-05-09 stsp } else {
5221 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5222 ebf99748 2019-05-09 stsp if (err)
5223 af12c6b9 2019-06-04 stsp break;
5224 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5225 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5226 3969253a 2020-03-07 stsp if (err) {
5227 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5228 3969253a 2020-03-07 stsp break;
5229 3969253a 2020-03-07 stsp }
5230 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5231 3969253a 2020-03-07 stsp if (err) {
5232 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5233 af12c6b9 2019-06-04 stsp break;
5234 3969253a 2020-03-07 stsp }
5235 ebf99748 2019-05-09 stsp }
5236 ebf99748 2019-05-09 stsp }
5237 d56d26ce 2019-05-10 stsp return err;
5238 d56d26ce 2019-05-10 stsp }
5239 735ef5ac 2019-08-03 stsp
5240 d56d26ce 2019-05-10 stsp
5241 d56d26ce 2019-05-10 stsp static const struct got_error *
5242 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5243 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5244 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5245 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5246 735ef5ac 2019-08-03 stsp int ood_errcode)
5247 d56d26ce 2019-05-10 stsp {
5248 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5249 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5250 1a36436d 2019-06-10 stsp
5251 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5252 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5253 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5254 1a36436d 2019-06-10 stsp return NULL;
5255 9bead371 2019-07-28 stsp /*
5256 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5257 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5258 9bead371 2019-07-28 stsp */
5259 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5260 735ef5ac 2019-08-03 stsp in_repo_path);
5261 9bead371 2019-07-28 stsp if (err) {
5262 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5263 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5264 1a36436d 2019-06-10 stsp goto done;
5265 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5266 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5267 1a36436d 2019-06-10 stsp } else {
5268 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5269 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5270 735ef5ac 2019-08-03 stsp in_repo_path);
5271 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5272 1a36436d 2019-06-10 stsp goto done;
5273 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5274 1a36436d 2019-06-10 stsp }
5275 1a36436d 2019-06-10 stsp done:
5276 1a36436d 2019-06-10 stsp free(id);
5277 1a36436d 2019-06-10 stsp return err;
5278 ebf99748 2019-05-09 stsp }
5279 ebf99748 2019-05-09 stsp
5280 c4296144 2019-05-09 stsp const struct got_error *
5281 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5282 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5283 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5284 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5285 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5286 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5287 afa376bf 2019-05-09 stsp struct got_repository *repo)
5288 c4296144 2019-05-09 stsp {
5289 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5290 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5291 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5292 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5293 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5294 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5295 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5296 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5297 ba580f68 2020-03-22 stsp int nentries;
5298 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5299 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5300 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5301 c4296144 2019-05-09 stsp
5302 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5303 c4296144 2019-05-09 stsp
5304 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5305 675c7539 2019-05-09 stsp
5306 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5307 588edf97 2019-05-10 stsp if (err)
5308 588edf97 2019-05-10 stsp goto done;
5309 de18fc63 2019-05-09 stsp
5310 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5311 588edf97 2019-05-10 stsp if (err)
5312 588edf97 2019-05-10 stsp goto done;
5313 588edf97 2019-05-10 stsp
5314 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5315 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5316 33ad4cbe 2019-05-12 jcs if (err)
5317 33ad4cbe 2019-05-12 jcs goto done;
5318 33ad4cbe 2019-05-12 jcs }
5319 c4296144 2019-05-09 stsp
5320 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5321 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5322 33ad4cbe 2019-05-12 jcs goto done;
5323 33ad4cbe 2019-05-12 jcs }
5324 33ad4cbe 2019-05-12 jcs
5325 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5326 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5327 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5328 cf066bf8 2019-05-09 stsp char *ondisk_path;
5329 cf066bf8 2019-05-09 stsp
5330 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5331 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5332 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5333 5f8a88c6 2019-08-03 stsp continue;
5334 5f8a88c6 2019-08-03 stsp
5335 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5336 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5337 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5338 cf066bf8 2019-05-09 stsp continue;
5339 cf066bf8 2019-05-09 stsp
5340 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5341 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5342 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5343 cf066bf8 2019-05-09 stsp goto done;
5344 cf066bf8 2019-05-09 stsp }
5345 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5346 2e1fa222 2020-07-23 stsp free(ondisk_path);
5347 2e1fa222 2020-07-23 stsp if (err)
5348 cf066bf8 2019-05-09 stsp goto done;
5349 cf066bf8 2019-05-09 stsp }
5350 036813ee 2019-05-09 stsp
5351 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5352 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5353 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5354 036813ee 2019-05-09 stsp if (err)
5355 036813ee 2019-05-09 stsp goto done;
5356 036813ee 2019-05-09 stsp
5357 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5358 de18fc63 2019-05-09 stsp if (err)
5359 de18fc63 2019-05-09 stsp goto done;
5360 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5361 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5362 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5363 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5364 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5365 33ad4cbe 2019-05-12 jcs free(logmsg);
5366 9d40349a 2019-05-09 stsp if (err)
5367 9d40349a 2019-05-09 stsp goto done;
5368 9d40349a 2019-05-09 stsp
5369 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5370 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5371 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5372 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5373 09f5bd90 2019-05-09 stsp goto done;
5374 09f5bd90 2019-05-09 stsp }
5375 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5376 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5377 09f5bd90 2019-05-09 stsp if (err)
5378 09f5bd90 2019-05-09 stsp goto done;
5379 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5380 ebf99748 2019-05-09 stsp if (err)
5381 ebf99748 2019-05-09 stsp goto done;
5382 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5383 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5384 09f5bd90 2019-05-09 stsp goto done;
5385 09f5bd90 2019-05-09 stsp }
5386 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5387 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5388 f2c16586 2019-05-09 stsp if (err)
5389 f2c16586 2019-05-09 stsp goto done;
5390 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5391 f2c16586 2019-05-09 stsp if (err)
5392 f2c16586 2019-05-09 stsp goto done;
5393 f2c16586 2019-05-09 stsp
5394 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5395 09f5bd90 2019-05-09 stsp if (err)
5396 09f5bd90 2019-05-09 stsp goto done;
5397 09f5bd90 2019-05-09 stsp
5398 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5399 ebf99748 2019-05-09 stsp if (err)
5400 ebf99748 2019-05-09 stsp goto done;
5401 c4296144 2019-05-09 stsp done:
5402 588edf97 2019-05-10 stsp if (head_tree)
5403 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5404 588edf97 2019-05-10 stsp if (head_commit)
5405 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5406 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5407 2f17228e 2019-05-12 stsp if (head_ref2) {
5408 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5409 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5410 2f17228e 2019-05-12 stsp err = unlockerr;
5411 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5412 39cd0ff6 2019-07-12 stsp }
5413 39cd0ff6 2019-07-12 stsp return err;
5414 39cd0ff6 2019-07-12 stsp }
5415 5c1e53bc 2019-07-28 stsp
5416 5c1e53bc 2019-07-28 stsp static const struct got_error *
5417 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5418 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5419 5c1e53bc 2019-07-28 stsp {
5420 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5421 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5422 39cd0ff6 2019-07-12 stsp
5423 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5424 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5425 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5426 5c1e53bc 2019-07-28 stsp
5427 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5428 5c1e53bc 2019-07-28 stsp ct_path++;
5429 5c1e53bc 2019-07-28 stsp
5430 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5431 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5432 5c1e53bc 2019-07-28 stsp break;
5433 5c1e53bc 2019-07-28 stsp }
5434 5c1e53bc 2019-07-28 stsp
5435 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5436 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5437 5c1e53bc 2019-07-28 stsp
5438 5c1e53bc 2019-07-28 stsp return NULL;
5439 5c1e53bc 2019-07-28 stsp }
5440 5c1e53bc 2019-07-28 stsp
5441 f0b75401 2019-08-03 stsp static const struct got_error *
5442 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5443 f0b75401 2019-08-03 stsp {
5444 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5445 f0b75401 2019-08-03 stsp
5446 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5447 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5448 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5449 f0b75401 2019-08-03 stsp }
5450 f0b75401 2019-08-03 stsp
5451 f0b75401 2019-08-03 stsp return NULL;
5452 f0b75401 2019-08-03 stsp }
5453 f0b75401 2019-08-03 stsp
5454 5f8a88c6 2019-08-03 stsp static const struct got_error *
5455 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5456 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5457 5f8a88c6 2019-08-03 stsp {
5458 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5459 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5460 5f8a88c6 2019-08-03 stsp
5461 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5462 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5463 5f8a88c6 2019-08-03 stsp continue;
5464 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5465 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5466 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5467 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5468 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5469 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5470 5f8a88c6 2019-08-03 stsp }
5471 5f8a88c6 2019-08-03 stsp
5472 5f8a88c6 2019-08-03 stsp return NULL;
5473 5f8a88c6 2019-08-03 stsp }
5474 5f8a88c6 2019-08-03 stsp
5475 39cd0ff6 2019-07-12 stsp const struct got_error *
5476 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5477 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5478 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5479 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5480 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5481 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5482 39cd0ff6 2019-07-12 stsp {
5483 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5484 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5485 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5486 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5487 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5488 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5489 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5490 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5491 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5492 39cd0ff6 2019-07-12 stsp
5493 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5494 39cd0ff6 2019-07-12 stsp
5495 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5496 39cd0ff6 2019-07-12 stsp
5497 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5498 39cd0ff6 2019-07-12 stsp if (err)
5499 39cd0ff6 2019-07-12 stsp goto done;
5500 39cd0ff6 2019-07-12 stsp
5501 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5502 39cd0ff6 2019-07-12 stsp if (err)
5503 39cd0ff6 2019-07-12 stsp goto done;
5504 39cd0ff6 2019-07-12 stsp
5505 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5506 39cd0ff6 2019-07-12 stsp if (err)
5507 39cd0ff6 2019-07-12 stsp goto done;
5508 39cd0ff6 2019-07-12 stsp
5509 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5510 39cd0ff6 2019-07-12 stsp if (err)
5511 39cd0ff6 2019-07-12 stsp goto done;
5512 39cd0ff6 2019-07-12 stsp
5513 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5514 5f8a88c6 2019-08-03 stsp &have_staged_files);
5515 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5516 5f8a88c6 2019-08-03 stsp goto done;
5517 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5518 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5519 5f8a88c6 2019-08-03 stsp if (err)
5520 5f8a88c6 2019-08-03 stsp goto done;
5521 5f8a88c6 2019-08-03 stsp }
5522 5f8a88c6 2019-08-03 stsp
5523 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5524 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5525 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5526 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5527 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5528 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5529 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5530 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5531 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5532 5c1e53bc 2019-07-28 stsp if (err)
5533 5c1e53bc 2019-07-28 stsp goto done;
5534 5c1e53bc 2019-07-28 stsp }
5535 39cd0ff6 2019-07-12 stsp
5536 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5537 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5538 39cd0ff6 2019-07-12 stsp goto done;
5539 5c1e53bc 2019-07-28 stsp }
5540 5c1e53bc 2019-07-28 stsp
5541 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5542 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5543 5c1e53bc 2019-07-28 stsp if (err)
5544 5c1e53bc 2019-07-28 stsp goto done;
5545 39cd0ff6 2019-07-12 stsp }
5546 f0b75401 2019-08-03 stsp
5547 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5548 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5549 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5550 f0b75401 2019-08-03 stsp
5551 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5552 f0b75401 2019-08-03 stsp ct_path++;
5553 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5554 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5555 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5556 b50cabdf 2019-07-12 stsp if (err)
5557 b50cabdf 2019-07-12 stsp goto done;
5558 f0b75401 2019-08-03 stsp
5559 b50cabdf 2019-07-12 stsp }
5560 b50cabdf 2019-07-12 stsp
5561 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5562 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5563 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5564 39cd0ff6 2019-07-12 stsp if (err)
5565 39cd0ff6 2019-07-12 stsp goto done;
5566 39cd0ff6 2019-07-12 stsp
5567 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5568 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5569 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5570 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5571 39cd0ff6 2019-07-12 stsp err = sync_err;
5572 39cd0ff6 2019-07-12 stsp done:
5573 39cd0ff6 2019-07-12 stsp if (fileindex)
5574 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5575 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5576 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5577 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5578 39cd0ff6 2019-07-12 stsp err = unlockerr;
5579 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5580 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5581 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5582 39cd0ff6 2019-07-12 stsp }
5583 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5584 c4296144 2019-05-09 stsp return err;
5585 8656d6c4 2019-05-20 stsp }
5586 8656d6c4 2019-05-20 stsp
5587 8656d6c4 2019-05-20 stsp const char *
5588 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5589 8656d6c4 2019-05-20 stsp {
5590 8656d6c4 2019-05-20 stsp return ct->path;
5591 8656d6c4 2019-05-20 stsp }
5592 8656d6c4 2019-05-20 stsp
5593 8656d6c4 2019-05-20 stsp unsigned int
5594 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5595 8656d6c4 2019-05-20 stsp {
5596 8656d6c4 2019-05-20 stsp return ct->status;
5597 818c7501 2019-07-11 stsp }
5598 818c7501 2019-07-11 stsp
5599 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5600 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5601 818c7501 2019-07-11 stsp struct got_repository *repo;
5602 818c7501 2019-07-11 stsp };
5603 818c7501 2019-07-11 stsp
5604 818c7501 2019-07-11 stsp static const struct got_error *
5605 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5606 818c7501 2019-07-11 stsp {
5607 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5608 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5609 818c7501 2019-07-11 stsp unsigned char status;
5610 818c7501 2019-07-11 stsp struct stat sb;
5611 818c7501 2019-07-11 stsp char *ondisk_path;
5612 818c7501 2019-07-11 stsp
5613 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5614 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5615 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5616 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5617 818c7501 2019-07-11 stsp
5618 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5619 818c7501 2019-07-11 stsp == -1)
5620 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5621 818c7501 2019-07-11 stsp
5622 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5623 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5624 818c7501 2019-07-11 stsp free(ondisk_path);
5625 818c7501 2019-07-11 stsp if (err)
5626 818c7501 2019-07-11 stsp return err;
5627 818c7501 2019-07-11 stsp
5628 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5629 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5630 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5631 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5632 818c7501 2019-07-11 stsp
5633 818c7501 2019-07-11 stsp return NULL;
5634 818c7501 2019-07-11 stsp }
5635 818c7501 2019-07-11 stsp
5636 818c7501 2019-07-11 stsp const struct got_error *
5637 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5638 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5639 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5640 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5641 818c7501 2019-07-11 stsp {
5642 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5643 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5644 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5645 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5646 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5647 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5648 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5649 818c7501 2019-07-11 stsp
5650 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5651 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5652 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5653 818c7501 2019-07-11 stsp
5654 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5655 818c7501 2019-07-11 stsp if (err)
5656 818c7501 2019-07-11 stsp return err;
5657 818c7501 2019-07-11 stsp
5658 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5659 818c7501 2019-07-11 stsp if (err)
5660 818c7501 2019-07-11 stsp goto done;
5661 818c7501 2019-07-11 stsp
5662 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5663 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5664 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5665 818c7501 2019-07-11 stsp &ok_arg);
5666 818c7501 2019-07-11 stsp if (err)
5667 818c7501 2019-07-11 stsp goto done;
5668 818c7501 2019-07-11 stsp
5669 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5670 818c7501 2019-07-11 stsp if (err)
5671 818c7501 2019-07-11 stsp goto done;
5672 818c7501 2019-07-11 stsp
5673 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5674 818c7501 2019-07-11 stsp if (err)
5675 818c7501 2019-07-11 stsp goto done;
5676 818c7501 2019-07-11 stsp
5677 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5678 818c7501 2019-07-11 stsp if (err)
5679 818c7501 2019-07-11 stsp goto done;
5680 818c7501 2019-07-11 stsp
5681 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5682 818c7501 2019-07-11 stsp 0);
5683 e51d7b55 2020-01-04 stsp if (err)
5684 e51d7b55 2020-01-04 stsp goto done;
5685 e51d7b55 2020-01-04 stsp
5686 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5687 818c7501 2019-07-11 stsp if (err)
5688 818c7501 2019-07-11 stsp goto done;
5689 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5690 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5691 e51d7b55 2020-01-04 stsp goto done;
5692 e51d7b55 2020-01-04 stsp }
5693 818c7501 2019-07-11 stsp
5694 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5695 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5696 818c7501 2019-07-11 stsp if (err)
5697 818c7501 2019-07-11 stsp goto done;
5698 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5699 818c7501 2019-07-11 stsp if (err)
5700 818c7501 2019-07-11 stsp goto done;
5701 818c7501 2019-07-11 stsp
5702 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5703 818c7501 2019-07-11 stsp
5704 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5705 818c7501 2019-07-11 stsp if (err)
5706 818c7501 2019-07-11 stsp goto done;
5707 818c7501 2019-07-11 stsp
5708 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5709 818c7501 2019-07-11 stsp if (err)
5710 818c7501 2019-07-11 stsp goto done;
5711 818c7501 2019-07-11 stsp
5712 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5713 818c7501 2019-07-11 stsp worktree->base_commit_id);
5714 818c7501 2019-07-11 stsp if (err)
5715 818c7501 2019-07-11 stsp goto done;
5716 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5717 818c7501 2019-07-11 stsp if (err)
5718 818c7501 2019-07-11 stsp goto done;
5719 818c7501 2019-07-11 stsp
5720 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5721 818c7501 2019-07-11 stsp if (err)
5722 818c7501 2019-07-11 stsp goto done;
5723 818c7501 2019-07-11 stsp done:
5724 818c7501 2019-07-11 stsp free(fileindex_path);
5725 818c7501 2019-07-11 stsp free(tmp_branch_name);
5726 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5727 818c7501 2019-07-11 stsp free(branch_ref_name);
5728 818c7501 2019-07-11 stsp if (branch_ref)
5729 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5730 818c7501 2019-07-11 stsp if (wt_branch)
5731 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5732 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5733 818c7501 2019-07-11 stsp if (err) {
5734 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5735 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5736 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5737 818c7501 2019-07-11 stsp }
5738 818c7501 2019-07-11 stsp if (*tmp_branch) {
5739 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5740 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5741 818c7501 2019-07-11 stsp }
5742 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5743 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5744 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5745 3e3a69f1 2019-07-25 stsp }
5746 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5747 818c7501 2019-07-11 stsp }
5748 818c7501 2019-07-11 stsp return err;
5749 818c7501 2019-07-11 stsp }
5750 818c7501 2019-07-11 stsp
5751 818c7501 2019-07-11 stsp const struct got_error *
5752 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5753 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5754 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5755 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5756 818c7501 2019-07-11 stsp {
5757 818c7501 2019-07-11 stsp const struct got_error *err;
5758 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5759 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5760 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5761 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5762 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5763 818c7501 2019-07-11 stsp
5764 818c7501 2019-07-11 stsp *commit_id = NULL;
5765 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5766 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5767 3e3a69f1 2019-07-25 stsp *branch = NULL;
5768 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5769 3e3a69f1 2019-07-25 stsp
5770 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5771 3e3a69f1 2019-07-25 stsp if (err)
5772 3e3a69f1 2019-07-25 stsp return err;
5773 818c7501 2019-07-11 stsp
5774 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5775 3e3a69f1 2019-07-25 stsp if (err)
5776 f032f1f7 2019-08-04 stsp goto done;
5777 f032f1f7 2019-08-04 stsp
5778 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5779 f032f1f7 2019-08-04 stsp &have_staged_files);
5780 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5781 f032f1f7 2019-08-04 stsp goto done;
5782 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5783 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5784 3e3a69f1 2019-07-25 stsp goto done;
5785 f032f1f7 2019-08-04 stsp }
5786 3e3a69f1 2019-07-25 stsp
5787 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5788 818c7501 2019-07-11 stsp if (err)
5789 f032f1f7 2019-08-04 stsp goto done;
5790 818c7501 2019-07-11 stsp
5791 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5792 818c7501 2019-07-11 stsp if (err)
5793 818c7501 2019-07-11 stsp goto done;
5794 818c7501 2019-07-11 stsp
5795 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5796 818c7501 2019-07-11 stsp if (err)
5797 818c7501 2019-07-11 stsp goto done;
5798 818c7501 2019-07-11 stsp
5799 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5800 818c7501 2019-07-11 stsp if (err)
5801 818c7501 2019-07-11 stsp goto done;
5802 818c7501 2019-07-11 stsp
5803 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5804 818c7501 2019-07-11 stsp if (err)
5805 818c7501 2019-07-11 stsp goto done;
5806 818c7501 2019-07-11 stsp
5807 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5808 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5809 818c7501 2019-07-11 stsp if (err)
5810 818c7501 2019-07-11 stsp goto done;
5811 818c7501 2019-07-11 stsp
5812 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5813 818c7501 2019-07-11 stsp if (err)
5814 818c7501 2019-07-11 stsp goto done;
5815 818c7501 2019-07-11 stsp
5816 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5817 818c7501 2019-07-11 stsp if (err)
5818 818c7501 2019-07-11 stsp goto done;
5819 818c7501 2019-07-11 stsp
5820 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5821 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5822 818c7501 2019-07-11 stsp if (err)
5823 818c7501 2019-07-11 stsp goto done;
5824 818c7501 2019-07-11 stsp
5825 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5826 818c7501 2019-07-11 stsp if (err)
5827 818c7501 2019-07-11 stsp goto done;
5828 818c7501 2019-07-11 stsp done:
5829 818c7501 2019-07-11 stsp free(commit_ref_name);
5830 818c7501 2019-07-11 stsp free(branch_ref_name);
5831 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5832 818c7501 2019-07-11 stsp if (commit_ref)
5833 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5834 818c7501 2019-07-11 stsp if (branch_ref)
5835 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5836 818c7501 2019-07-11 stsp if (err) {
5837 818c7501 2019-07-11 stsp free(*commit_id);
5838 818c7501 2019-07-11 stsp *commit_id = NULL;
5839 818c7501 2019-07-11 stsp if (*tmp_branch) {
5840 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5841 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5842 818c7501 2019-07-11 stsp }
5843 818c7501 2019-07-11 stsp if (*new_base_branch) {
5844 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5845 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5846 818c7501 2019-07-11 stsp }
5847 818c7501 2019-07-11 stsp if (*branch) {
5848 818c7501 2019-07-11 stsp got_ref_close(*branch);
5849 818c7501 2019-07-11 stsp *branch = NULL;
5850 818c7501 2019-07-11 stsp }
5851 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5852 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5853 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5854 3e3a69f1 2019-07-25 stsp }
5855 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5856 818c7501 2019-07-11 stsp }
5857 818c7501 2019-07-11 stsp return err;
5858 c4296144 2019-05-09 stsp }
5859 818c7501 2019-07-11 stsp
5860 818c7501 2019-07-11 stsp const struct got_error *
5861 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5862 818c7501 2019-07-11 stsp {
5863 818c7501 2019-07-11 stsp const struct got_error *err;
5864 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5865 818c7501 2019-07-11 stsp
5866 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5867 818c7501 2019-07-11 stsp if (err)
5868 818c7501 2019-07-11 stsp return err;
5869 818c7501 2019-07-11 stsp
5870 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5871 818c7501 2019-07-11 stsp free(tmp_branch_name);
5872 818c7501 2019-07-11 stsp return NULL;
5873 818c7501 2019-07-11 stsp }
5874 818c7501 2019-07-11 stsp
5875 818c7501 2019-07-11 stsp static const struct got_error *
5876 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5877 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5878 818c7501 2019-07-11 stsp {
5879 0ebf8283 2019-07-24 stsp *logmsg = arg;
5880 818c7501 2019-07-11 stsp return NULL;
5881 818c7501 2019-07-11 stsp }
5882 818c7501 2019-07-11 stsp
5883 818c7501 2019-07-11 stsp static const struct got_error *
5884 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5885 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5886 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5887 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5888 818c7501 2019-07-11 stsp {
5889 818c7501 2019-07-11 stsp return NULL;
5890 01757395 2019-07-12 stsp }
5891 01757395 2019-07-12 stsp
5892 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5893 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5894 01757395 2019-07-12 stsp void *progress_arg;
5895 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5896 01757395 2019-07-12 stsp };
5897 01757395 2019-07-12 stsp
5898 01757395 2019-07-12 stsp static const struct got_error *
5899 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5900 01757395 2019-07-12 stsp {
5901 01757395 2019-07-12 stsp const struct got_error *err;
5902 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5903 01757395 2019-07-12 stsp char *p;
5904 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5905 01757395 2019-07-12 stsp
5906 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5907 01757395 2019-07-12 stsp if (err)
5908 01757395 2019-07-12 stsp return err;
5909 01757395 2019-07-12 stsp
5910 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5911 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5912 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5913 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5914 01757395 2019-07-12 stsp return NULL;
5915 01757395 2019-07-12 stsp
5916 01757395 2019-07-12 stsp p = strdup(path);
5917 01757395 2019-07-12 stsp if (p == NULL)
5918 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5919 01757395 2019-07-12 stsp
5920 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5921 01757395 2019-07-12 stsp if (err || new == NULL)
5922 01757395 2019-07-12 stsp free(p);
5923 01757395 2019-07-12 stsp return err;
5924 01757395 2019-07-12 stsp }
5925 01757395 2019-07-12 stsp
5926 01757395 2019-07-12 stsp void
5927 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5928 01757395 2019-07-12 stsp {
5929 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5930 01757395 2019-07-12 stsp
5931 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5932 01757395 2019-07-12 stsp free((char *)pe->path);
5933 01757395 2019-07-12 stsp
5934 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5935 818c7501 2019-07-11 stsp }
5936 818c7501 2019-07-11 stsp
5937 0ebf8283 2019-07-24 stsp static const struct got_error *
5938 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5939 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5940 818c7501 2019-07-11 stsp {
5941 818c7501 2019-07-11 stsp const struct got_error *err;
5942 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5943 818c7501 2019-07-11 stsp
5944 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5945 818c7501 2019-07-11 stsp if (err) {
5946 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5947 818c7501 2019-07-11 stsp goto done;
5948 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5949 818c7501 2019-07-11 stsp if (err)
5950 818c7501 2019-07-11 stsp goto done;
5951 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5952 818c7501 2019-07-11 stsp if (err)
5953 818c7501 2019-07-11 stsp goto done;
5954 de05890f 2020-03-05 stsp } else if (is_rebase) {
5955 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5956 818c7501 2019-07-11 stsp int cmp;
5957 818c7501 2019-07-11 stsp
5958 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5959 818c7501 2019-07-11 stsp if (err)
5960 818c7501 2019-07-11 stsp goto done;
5961 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5962 818c7501 2019-07-11 stsp free(stored_id);
5963 818c7501 2019-07-11 stsp if (cmp != 0) {
5964 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5965 818c7501 2019-07-11 stsp goto done;
5966 818c7501 2019-07-11 stsp }
5967 818c7501 2019-07-11 stsp }
5968 0ebf8283 2019-07-24 stsp done:
5969 0ebf8283 2019-07-24 stsp if (commit_ref)
5970 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5971 0ebf8283 2019-07-24 stsp return err;
5972 0ebf8283 2019-07-24 stsp }
5973 0ebf8283 2019-07-24 stsp
5974 0ebf8283 2019-07-24 stsp static const struct got_error *
5975 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5976 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5977 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5978 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5979 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5980 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5981 0ebf8283 2019-07-24 stsp {
5982 0ebf8283 2019-07-24 stsp const struct got_error *err;
5983 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5984 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5985 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5986 818c7501 2019-07-11 stsp
5987 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5988 0ebf8283 2019-07-24 stsp
5989 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5990 0ebf8283 2019-07-24 stsp if (err)
5991 0ebf8283 2019-07-24 stsp return err;
5992 0ebf8283 2019-07-24 stsp
5993 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5994 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5995 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5996 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5997 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5998 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5999 818c7501 2019-07-11 stsp if (commit_ref)
6000 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6001 818c7501 2019-07-11 stsp return err;
6002 818c7501 2019-07-11 stsp }
6003 818c7501 2019-07-11 stsp
6004 818c7501 2019-07-11 stsp const struct got_error *
6005 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6006 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6007 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6008 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6009 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6010 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6011 818c7501 2019-07-11 stsp {
6012 0ebf8283 2019-07-24 stsp const struct got_error *err;
6013 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6014 0ebf8283 2019-07-24 stsp
6015 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6016 0ebf8283 2019-07-24 stsp if (err)
6017 0ebf8283 2019-07-24 stsp return err;
6018 0ebf8283 2019-07-24 stsp
6019 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6020 0ebf8283 2019-07-24 stsp if (err)
6021 0ebf8283 2019-07-24 stsp goto done;
6022 0ebf8283 2019-07-24 stsp
6023 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6024 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6025 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6026 0ebf8283 2019-07-24 stsp done:
6027 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6028 0ebf8283 2019-07-24 stsp return err;
6029 0ebf8283 2019-07-24 stsp }
6030 0ebf8283 2019-07-24 stsp
6031 0ebf8283 2019-07-24 stsp const struct got_error *
6032 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6033 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6034 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6035 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6036 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6037 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6038 0ebf8283 2019-07-24 stsp {
6039 0ebf8283 2019-07-24 stsp const struct got_error *err;
6040 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6041 0ebf8283 2019-07-24 stsp
6042 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6043 0ebf8283 2019-07-24 stsp if (err)
6044 0ebf8283 2019-07-24 stsp return err;
6045 0ebf8283 2019-07-24 stsp
6046 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6047 0ebf8283 2019-07-24 stsp if (err)
6048 0ebf8283 2019-07-24 stsp goto done;
6049 0ebf8283 2019-07-24 stsp
6050 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6051 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6052 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6053 0ebf8283 2019-07-24 stsp done:
6054 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6055 0ebf8283 2019-07-24 stsp return err;
6056 0ebf8283 2019-07-24 stsp }
6057 0ebf8283 2019-07-24 stsp
6058 0ebf8283 2019-07-24 stsp static const struct got_error *
6059 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6060 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6061 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6062 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6063 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6064 0ebf8283 2019-07-24 stsp {
6065 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6066 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6067 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6068 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6069 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6070 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6071 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6072 818c7501 2019-07-11 stsp
6073 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6074 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6075 a0e95631 2019-07-12 stsp
6076 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6077 818c7501 2019-07-11 stsp
6078 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6079 a0e95631 2019-07-12 stsp if (err)
6080 3e3a69f1 2019-07-25 stsp return err;
6081 a0e95631 2019-07-12 stsp
6082 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6083 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6084 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6085 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6086 01757395 2019-07-12 stsp /*
6087 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6088 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6089 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6090 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6091 01757395 2019-07-12 stsp */
6092 01757395 2019-07-12 stsp if (merged_paths) {
6093 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6094 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6095 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6096 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6097 f2a9dc41 2019-12-13 tracey 0);
6098 01757395 2019-07-12 stsp if (err)
6099 01757395 2019-07-12 stsp goto done;
6100 01757395 2019-07-12 stsp }
6101 01757395 2019-07-12 stsp } else {
6102 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6103 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6104 01757395 2019-07-12 stsp if (err)
6105 01757395 2019-07-12 stsp goto done;
6106 01757395 2019-07-12 stsp }
6107 a0e95631 2019-07-12 stsp
6108 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6109 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6110 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6111 a0e95631 2019-07-12 stsp if (err)
6112 a0e95631 2019-07-12 stsp goto done;
6113 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6114 a0e95631 2019-07-12 stsp goto done;
6115 ff0d2220 2019-07-11 stsp }
6116 818c7501 2019-07-11 stsp
6117 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6118 edd02c5e 2019-07-11 stsp if (err)
6119 edd02c5e 2019-07-11 stsp goto done;
6120 edd02c5e 2019-07-11 stsp
6121 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6122 818c7501 2019-07-11 stsp if (err)
6123 818c7501 2019-07-11 stsp goto done;
6124 818c7501 2019-07-11 stsp
6125 5943eee2 2019-08-13 stsp if (new_logmsg) {
6126 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6127 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6128 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6129 5943eee2 2019-08-13 stsp goto done;
6130 5943eee2 2019-08-13 stsp }
6131 5943eee2 2019-08-13 stsp } else {
6132 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6133 5943eee2 2019-08-13 stsp if (err)
6134 5943eee2 2019-08-13 stsp goto done;
6135 5943eee2 2019-08-13 stsp }
6136 0ebf8283 2019-07-24 stsp
6137 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6138 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6139 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6140 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6141 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6142 a0e95631 2019-07-12 stsp if (err)
6143 a0e95631 2019-07-12 stsp goto done;
6144 a0e95631 2019-07-12 stsp
6145 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6146 a0e95631 2019-07-12 stsp if (err)
6147 a0e95631 2019-07-12 stsp goto done;
6148 a0e95631 2019-07-12 stsp
6149 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6150 a0e95631 2019-07-12 stsp if (err)
6151 a0e95631 2019-07-12 stsp goto done;
6152 a0e95631 2019-07-12 stsp
6153 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6154 72fd46fa 2019-09-06 stsp fileindex, 0);
6155 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6156 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6157 a0e95631 2019-07-12 stsp err = sync_err;
6158 818c7501 2019-07-11 stsp done:
6159 a0e95631 2019-07-12 stsp free(fileindex_path);
6160 a0e95631 2019-07-12 stsp free(head_commit_id);
6161 a0e95631 2019-07-12 stsp if (head_ref)
6162 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6163 818c7501 2019-07-11 stsp if (err) {
6164 818c7501 2019-07-11 stsp free(*new_commit_id);
6165 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6166 818c7501 2019-07-11 stsp }
6167 818c7501 2019-07-11 stsp return err;
6168 818c7501 2019-07-11 stsp }
6169 818c7501 2019-07-11 stsp
6170 818c7501 2019-07-11 stsp const struct got_error *
6171 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6172 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6173 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6174 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6175 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6176 0ebf8283 2019-07-24 stsp {
6177 0ebf8283 2019-07-24 stsp const struct got_error *err;
6178 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6179 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6180 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6181 0ebf8283 2019-07-24 stsp
6182 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6183 0ebf8283 2019-07-24 stsp if (err)
6184 0ebf8283 2019-07-24 stsp return err;
6185 0ebf8283 2019-07-24 stsp
6186 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6187 0ebf8283 2019-07-24 stsp if (err)
6188 0ebf8283 2019-07-24 stsp goto done;
6189 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6190 0ebf8283 2019-07-24 stsp if (err)
6191 0ebf8283 2019-07-24 stsp goto done;
6192 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6193 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6194 0ebf8283 2019-07-24 stsp goto done;
6195 0ebf8283 2019-07-24 stsp }
6196 0ebf8283 2019-07-24 stsp
6197 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6198 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6199 0ebf8283 2019-07-24 stsp done:
6200 0ebf8283 2019-07-24 stsp if (commit_ref)
6201 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6202 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6203 0ebf8283 2019-07-24 stsp free(commit_id);
6204 0ebf8283 2019-07-24 stsp return err;
6205 0ebf8283 2019-07-24 stsp }
6206 0ebf8283 2019-07-24 stsp
6207 0ebf8283 2019-07-24 stsp const struct got_error *
6208 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6209 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6210 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6211 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6212 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6213 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6214 0ebf8283 2019-07-24 stsp {
6215 0ebf8283 2019-07-24 stsp const struct got_error *err;
6216 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6217 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6218 0ebf8283 2019-07-24 stsp
6219 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6220 0ebf8283 2019-07-24 stsp if (err)
6221 0ebf8283 2019-07-24 stsp return err;
6222 0ebf8283 2019-07-24 stsp
6223 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6224 0ebf8283 2019-07-24 stsp if (err)
6225 0ebf8283 2019-07-24 stsp goto done;
6226 0ebf8283 2019-07-24 stsp
6227 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6228 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6229 0ebf8283 2019-07-24 stsp done:
6230 0ebf8283 2019-07-24 stsp if (commit_ref)
6231 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6232 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6233 0ebf8283 2019-07-24 stsp return err;
6234 0ebf8283 2019-07-24 stsp }
6235 0ebf8283 2019-07-24 stsp
6236 0ebf8283 2019-07-24 stsp const struct got_error *
6237 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6238 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6239 818c7501 2019-07-11 stsp {
6240 3e3a69f1 2019-07-25 stsp if (fileindex)
6241 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6242 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6243 69844fba 2019-07-11 stsp }
6244 69844fba 2019-07-11 stsp
6245 69844fba 2019-07-11 stsp static const struct got_error *
6246 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6247 69844fba 2019-07-11 stsp {
6248 69844fba 2019-07-11 stsp const struct got_error *err;
6249 69844fba 2019-07-11 stsp struct got_reference *ref;
6250 69844fba 2019-07-11 stsp
6251 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6252 69844fba 2019-07-11 stsp if (err) {
6253 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6254 69844fba 2019-07-11 stsp return NULL;
6255 69844fba 2019-07-11 stsp return err;
6256 69844fba 2019-07-11 stsp }
6257 69844fba 2019-07-11 stsp
6258 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6259 69844fba 2019-07-11 stsp got_ref_close(ref);
6260 69844fba 2019-07-11 stsp return err;
6261 818c7501 2019-07-11 stsp }
6262 818c7501 2019-07-11 stsp
6263 69844fba 2019-07-11 stsp static const struct got_error *
6264 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6265 69844fba 2019-07-11 stsp {
6266 69844fba 2019-07-11 stsp const struct got_error *err;
6267 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6268 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6269 69844fba 2019-07-11 stsp
6270 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6271 69844fba 2019-07-11 stsp if (err)
6272 69844fba 2019-07-11 stsp goto done;
6273 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6274 69844fba 2019-07-11 stsp if (err)
6275 69844fba 2019-07-11 stsp goto done;
6276 69844fba 2019-07-11 stsp
6277 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6278 69844fba 2019-07-11 stsp if (err)
6279 69844fba 2019-07-11 stsp goto done;
6280 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6281 69844fba 2019-07-11 stsp if (err)
6282 69844fba 2019-07-11 stsp goto done;
6283 69844fba 2019-07-11 stsp
6284 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6285 69844fba 2019-07-11 stsp if (err)
6286 69844fba 2019-07-11 stsp goto done;
6287 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6288 69844fba 2019-07-11 stsp if (err)
6289 69844fba 2019-07-11 stsp goto done;
6290 69844fba 2019-07-11 stsp
6291 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6292 69844fba 2019-07-11 stsp if (err)
6293 69844fba 2019-07-11 stsp goto done;
6294 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6295 69844fba 2019-07-11 stsp if (err)
6296 69844fba 2019-07-11 stsp goto done;
6297 69844fba 2019-07-11 stsp
6298 69844fba 2019-07-11 stsp done:
6299 69844fba 2019-07-11 stsp free(tmp_branch_name);
6300 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6301 69844fba 2019-07-11 stsp free(branch_ref_name);
6302 69844fba 2019-07-11 stsp free(commit_ref_name);
6303 69844fba 2019-07-11 stsp return err;
6304 69844fba 2019-07-11 stsp }
6305 69844fba 2019-07-11 stsp
6306 818c7501 2019-07-11 stsp const struct got_error *
6307 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6308 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6309 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6310 818c7501 2019-07-11 stsp struct got_repository *repo)
6311 818c7501 2019-07-11 stsp {
6312 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6313 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6314 818c7501 2019-07-11 stsp
6315 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6316 818c7501 2019-07-11 stsp if (err)
6317 818c7501 2019-07-11 stsp return err;
6318 818c7501 2019-07-11 stsp
6319 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6320 818c7501 2019-07-11 stsp if (err)
6321 818c7501 2019-07-11 stsp goto done;
6322 818c7501 2019-07-11 stsp
6323 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6324 818c7501 2019-07-11 stsp if (err)
6325 818c7501 2019-07-11 stsp goto done;
6326 818c7501 2019-07-11 stsp
6327 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6328 818c7501 2019-07-11 stsp if (err)
6329 818c7501 2019-07-11 stsp goto done;
6330 818c7501 2019-07-11 stsp
6331 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6332 818c7501 2019-07-11 stsp done:
6333 3e3a69f1 2019-07-25 stsp if (fileindex)
6334 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6335 818c7501 2019-07-11 stsp free(new_head_commit_id);
6336 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6337 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6338 818c7501 2019-07-11 stsp err = unlockerr;
6339 818c7501 2019-07-11 stsp return err;
6340 818c7501 2019-07-11 stsp }
6341 818c7501 2019-07-11 stsp
6342 818c7501 2019-07-11 stsp const struct got_error *
6343 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6344 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6345 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6346 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6347 818c7501 2019-07-11 stsp {
6348 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6349 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6350 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6351 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6352 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6353 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6354 818c7501 2019-07-11 stsp
6355 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6356 818c7501 2019-07-11 stsp if (err)
6357 818c7501 2019-07-11 stsp return err;
6358 818c7501 2019-07-11 stsp
6359 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6360 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6361 818c7501 2019-07-11 stsp if (err)
6362 818c7501 2019-07-11 stsp goto done;
6363 818c7501 2019-07-11 stsp
6364 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6365 818c7501 2019-07-11 stsp if (err)
6366 818c7501 2019-07-11 stsp goto done;
6367 818c7501 2019-07-11 stsp
6368 818c7501 2019-07-11 stsp /*
6369 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6370 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6371 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6372 818c7501 2019-07-11 stsp */
6373 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6374 818c7501 2019-07-11 stsp if (err)
6375 818c7501 2019-07-11 stsp goto done;
6376 818c7501 2019-07-11 stsp
6377 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6378 818c7501 2019-07-11 stsp if (err)
6379 818c7501 2019-07-11 stsp goto done;
6380 818c7501 2019-07-11 stsp
6381 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6382 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6383 ca355955 2019-07-12 stsp if (err)
6384 ca355955 2019-07-12 stsp goto done;
6385 ca355955 2019-07-12 stsp
6386 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6387 ca355955 2019-07-12 stsp if (err)
6388 ca355955 2019-07-12 stsp goto done;
6389 ca355955 2019-07-12 stsp
6390 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6391 818c7501 2019-07-11 stsp if (err)
6392 818c7501 2019-07-11 stsp goto done;
6393 818c7501 2019-07-11 stsp
6394 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6395 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6396 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6397 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6398 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6399 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6400 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6401 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6402 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6403 55bd499d 2019-07-12 stsp if (err)
6404 1f1abb7e 2019-08-08 stsp goto sync;
6405 55bd499d 2019-07-12 stsp
6406 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6407 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6408 ca355955 2019-07-12 stsp sync:
6409 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6410 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6411 ca355955 2019-07-12 stsp err = sync_err;
6412 818c7501 2019-07-11 stsp done:
6413 818c7501 2019-07-11 stsp got_ref_close(resolved);
6414 a3a2faf2 2019-07-12 stsp free(tree_id);
6415 818c7501 2019-07-11 stsp free(commit_id);
6416 0ebf8283 2019-07-24 stsp if (fileindex)
6417 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6418 0ebf8283 2019-07-24 stsp free(fileindex_path);
6419 0ebf8283 2019-07-24 stsp
6420 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6421 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6422 0ebf8283 2019-07-24 stsp err = unlockerr;
6423 0ebf8283 2019-07-24 stsp return err;
6424 0ebf8283 2019-07-24 stsp }
6425 0ebf8283 2019-07-24 stsp
6426 0ebf8283 2019-07-24 stsp const struct got_error *
6427 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6428 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6429 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6430 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6431 0ebf8283 2019-07-24 stsp {
6432 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6433 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6434 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6435 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6436 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6437 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6438 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6439 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6440 0ebf8283 2019-07-24 stsp
6441 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6442 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6443 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6444 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6445 0ebf8283 2019-07-24 stsp
6446 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6447 0ebf8283 2019-07-24 stsp if (err)
6448 0ebf8283 2019-07-24 stsp return err;
6449 0ebf8283 2019-07-24 stsp
6450 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6451 0ebf8283 2019-07-24 stsp if (err)
6452 0ebf8283 2019-07-24 stsp goto done;
6453 0ebf8283 2019-07-24 stsp
6454 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6455 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6456 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6457 0ebf8283 2019-07-24 stsp &ok_arg);
6458 0ebf8283 2019-07-24 stsp if (err)
6459 0ebf8283 2019-07-24 stsp goto done;
6460 0ebf8283 2019-07-24 stsp
6461 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6462 0ebf8283 2019-07-24 stsp if (err)
6463 0ebf8283 2019-07-24 stsp goto done;
6464 0ebf8283 2019-07-24 stsp
6465 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6466 0ebf8283 2019-07-24 stsp if (err)
6467 0ebf8283 2019-07-24 stsp goto done;
6468 0ebf8283 2019-07-24 stsp
6469 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6470 0ebf8283 2019-07-24 stsp worktree);
6471 0ebf8283 2019-07-24 stsp if (err)
6472 0ebf8283 2019-07-24 stsp goto done;
6473 0ebf8283 2019-07-24 stsp
6474 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6475 0ebf8283 2019-07-24 stsp 0);
6476 0ebf8283 2019-07-24 stsp if (err)
6477 0ebf8283 2019-07-24 stsp goto done;
6478 0ebf8283 2019-07-24 stsp
6479 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6480 0ebf8283 2019-07-24 stsp if (err)
6481 0ebf8283 2019-07-24 stsp goto done;
6482 0ebf8283 2019-07-24 stsp
6483 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6484 0ebf8283 2019-07-24 stsp if (err)
6485 0ebf8283 2019-07-24 stsp goto done;
6486 0ebf8283 2019-07-24 stsp
6487 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6488 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6489 0ebf8283 2019-07-24 stsp if (err)
6490 0ebf8283 2019-07-24 stsp goto done;
6491 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6492 0ebf8283 2019-07-24 stsp if (err)
6493 0ebf8283 2019-07-24 stsp goto done;
6494 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6495 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6496 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6497 0ebf8283 2019-07-24 stsp goto done;
6498 0ebf8283 2019-07-24 stsp }
6499 0ebf8283 2019-07-24 stsp
6500 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6501 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6502 0ebf8283 2019-07-24 stsp if (err)
6503 0ebf8283 2019-07-24 stsp goto done;
6504 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6505 0ebf8283 2019-07-24 stsp if (err)
6506 0ebf8283 2019-07-24 stsp goto done;
6507 0ebf8283 2019-07-24 stsp
6508 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6509 0ebf8283 2019-07-24 stsp if (err)
6510 0ebf8283 2019-07-24 stsp goto done;
6511 0ebf8283 2019-07-24 stsp done:
6512 0ebf8283 2019-07-24 stsp free(fileindex_path);
6513 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6514 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6515 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6516 0ebf8283 2019-07-24 stsp if (wt_branch)
6517 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6518 0ebf8283 2019-07-24 stsp if (err) {
6519 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6520 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6521 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6522 0ebf8283 2019-07-24 stsp }
6523 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6524 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6525 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6526 0ebf8283 2019-07-24 stsp }
6527 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6528 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6529 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6530 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6531 3e3a69f1 2019-07-25 stsp }
6532 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6533 0ebf8283 2019-07-24 stsp }
6534 0ebf8283 2019-07-24 stsp return err;
6535 0ebf8283 2019-07-24 stsp }
6536 0ebf8283 2019-07-24 stsp
6537 0ebf8283 2019-07-24 stsp const struct got_error *
6538 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6539 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6540 0ebf8283 2019-07-24 stsp {
6541 3e3a69f1 2019-07-25 stsp if (fileindex)
6542 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6543 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6544 0ebf8283 2019-07-24 stsp }
6545 0ebf8283 2019-07-24 stsp
6546 0ebf8283 2019-07-24 stsp const struct got_error *
6547 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6548 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6549 0ebf8283 2019-07-24 stsp {
6550 0ebf8283 2019-07-24 stsp const struct got_error *err;
6551 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6552 0ebf8283 2019-07-24 stsp
6553 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6554 0ebf8283 2019-07-24 stsp if (err)
6555 0ebf8283 2019-07-24 stsp return err;
6556 0ebf8283 2019-07-24 stsp
6557 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6558 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6559 0ebf8283 2019-07-24 stsp return NULL;
6560 0ebf8283 2019-07-24 stsp }
6561 0ebf8283 2019-07-24 stsp
6562 0ebf8283 2019-07-24 stsp const struct got_error *
6563 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6564 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6565 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6566 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6567 0ebf8283 2019-07-24 stsp {
6568 0ebf8283 2019-07-24 stsp const struct got_error *err;
6569 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6570 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6571 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6572 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6573 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6574 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6575 0ebf8283 2019-07-24 stsp
6576 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6577 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6578 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6579 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6580 0ebf8283 2019-07-24 stsp
6581 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6582 3e3a69f1 2019-07-25 stsp if (err)
6583 3e3a69f1 2019-07-25 stsp return err;
6584 3e3a69f1 2019-07-25 stsp
6585 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6586 3e3a69f1 2019-07-25 stsp if (err)
6587 f032f1f7 2019-08-04 stsp goto done;
6588 f032f1f7 2019-08-04 stsp
6589 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6590 f032f1f7 2019-08-04 stsp &have_staged_files);
6591 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6592 f032f1f7 2019-08-04 stsp goto done;
6593 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6594 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6595 3e3a69f1 2019-07-25 stsp goto done;
6596 f032f1f7 2019-08-04 stsp }
6597 3e3a69f1 2019-07-25 stsp
6598 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6599 0ebf8283 2019-07-24 stsp if (err)
6600 f032f1f7 2019-08-04 stsp goto done;
6601 0ebf8283 2019-07-24 stsp
6602 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6603 0ebf8283 2019-07-24 stsp if (err)
6604 0ebf8283 2019-07-24 stsp goto done;
6605 0ebf8283 2019-07-24 stsp
6606 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6607 0ebf8283 2019-07-24 stsp if (err)
6608 0ebf8283 2019-07-24 stsp goto done;
6609 0ebf8283 2019-07-24 stsp
6610 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6611 0ebf8283 2019-07-24 stsp worktree);
6612 0ebf8283 2019-07-24 stsp if (err)
6613 0ebf8283 2019-07-24 stsp goto done;
6614 0ebf8283 2019-07-24 stsp
6615 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6616 0ebf8283 2019-07-24 stsp if (err)
6617 0ebf8283 2019-07-24 stsp goto done;
6618 0ebf8283 2019-07-24 stsp
6619 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6620 0ebf8283 2019-07-24 stsp if (err)
6621 0ebf8283 2019-07-24 stsp goto done;
6622 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6623 0ebf8283 2019-07-24 stsp if (err)
6624 0ebf8283 2019-07-24 stsp goto done;
6625 0ebf8283 2019-07-24 stsp
6626 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6627 0ebf8283 2019-07-24 stsp if (err)
6628 0ebf8283 2019-07-24 stsp goto done;
6629 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6630 0ebf8283 2019-07-24 stsp if (err)
6631 0ebf8283 2019-07-24 stsp goto done;
6632 0ebf8283 2019-07-24 stsp
6633 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6634 0ebf8283 2019-07-24 stsp if (err)
6635 0ebf8283 2019-07-24 stsp goto done;
6636 0ebf8283 2019-07-24 stsp done:
6637 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6638 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6639 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6640 0ebf8283 2019-07-24 stsp if (commit_ref)
6641 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6642 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6643 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6644 0ebf8283 2019-07-24 stsp if (err) {
6645 0ebf8283 2019-07-24 stsp free(*commit_id);
6646 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6647 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6648 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6649 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6650 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6651 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6652 0ebf8283 2019-07-24 stsp }
6653 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6654 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6655 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6656 3e3a69f1 2019-07-25 stsp }
6657 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6658 0ebf8283 2019-07-24 stsp }
6659 0ebf8283 2019-07-24 stsp return err;
6660 0ebf8283 2019-07-24 stsp }
6661 0ebf8283 2019-07-24 stsp
6662 0ebf8283 2019-07-24 stsp static const struct got_error *
6663 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6664 0ebf8283 2019-07-24 stsp {
6665 0ebf8283 2019-07-24 stsp const struct got_error *err;
6666 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6667 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6668 0ebf8283 2019-07-24 stsp
6669 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6670 0ebf8283 2019-07-24 stsp if (err)
6671 0ebf8283 2019-07-24 stsp goto done;
6672 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6673 0ebf8283 2019-07-24 stsp if (err)
6674 0ebf8283 2019-07-24 stsp goto done;
6675 0ebf8283 2019-07-24 stsp
6676 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6677 0ebf8283 2019-07-24 stsp worktree);
6678 0ebf8283 2019-07-24 stsp if (err)
6679 0ebf8283 2019-07-24 stsp goto done;
6680 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6681 0ebf8283 2019-07-24 stsp if (err)
6682 0ebf8283 2019-07-24 stsp goto done;
6683 0ebf8283 2019-07-24 stsp
6684 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6685 0ebf8283 2019-07-24 stsp if (err)
6686 0ebf8283 2019-07-24 stsp goto done;
6687 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6688 0ebf8283 2019-07-24 stsp if (err)
6689 0ebf8283 2019-07-24 stsp goto done;
6690 0ebf8283 2019-07-24 stsp
6691 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6692 0ebf8283 2019-07-24 stsp if (err)
6693 0ebf8283 2019-07-24 stsp goto done;
6694 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6695 0ebf8283 2019-07-24 stsp if (err)
6696 0ebf8283 2019-07-24 stsp goto done;
6697 0ebf8283 2019-07-24 stsp done:
6698 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6699 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6700 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6701 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6702 0ebf8283 2019-07-24 stsp return err;
6703 0ebf8283 2019-07-24 stsp }
6704 0ebf8283 2019-07-24 stsp
6705 0ebf8283 2019-07-24 stsp const struct got_error *
6706 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6707 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6708 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6709 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6710 0ebf8283 2019-07-24 stsp {
6711 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6712 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6713 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6714 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6715 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6716 0ebf8283 2019-07-24 stsp
6717 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6718 0ebf8283 2019-07-24 stsp if (err)
6719 0ebf8283 2019-07-24 stsp return err;
6720 0ebf8283 2019-07-24 stsp
6721 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6722 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6723 0ebf8283 2019-07-24 stsp if (err)
6724 0ebf8283 2019-07-24 stsp goto done;
6725 0ebf8283 2019-07-24 stsp
6726 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6727 0ebf8283 2019-07-24 stsp if (err)
6728 0ebf8283 2019-07-24 stsp goto done;
6729 0ebf8283 2019-07-24 stsp
6730 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6731 0ebf8283 2019-07-24 stsp if (err)
6732 0ebf8283 2019-07-24 stsp goto done;
6733 0ebf8283 2019-07-24 stsp
6734 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6735 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6736 0ebf8283 2019-07-24 stsp if (err)
6737 0ebf8283 2019-07-24 stsp goto done;
6738 0ebf8283 2019-07-24 stsp
6739 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6740 0ebf8283 2019-07-24 stsp if (err)
6741 0ebf8283 2019-07-24 stsp goto done;
6742 0ebf8283 2019-07-24 stsp
6743 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6744 0ebf8283 2019-07-24 stsp if (err)
6745 0ebf8283 2019-07-24 stsp goto done;
6746 0ebf8283 2019-07-24 stsp
6747 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6748 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6749 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6750 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6751 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6752 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6753 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6754 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6755 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6756 0ebf8283 2019-07-24 stsp if (err)
6757 1f1abb7e 2019-08-08 stsp goto sync;
6758 0ebf8283 2019-07-24 stsp
6759 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6760 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6761 0ebf8283 2019-07-24 stsp sync:
6762 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6763 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6764 0ebf8283 2019-07-24 stsp err = sync_err;
6765 0ebf8283 2019-07-24 stsp done:
6766 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6767 0ebf8283 2019-07-24 stsp free(tree_id);
6768 818c7501 2019-07-11 stsp free(fileindex_path);
6769 818c7501 2019-07-11 stsp
6770 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6771 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6772 818c7501 2019-07-11 stsp err = unlockerr;
6773 818c7501 2019-07-11 stsp return err;
6774 818c7501 2019-07-11 stsp }
6775 0ebf8283 2019-07-24 stsp
6776 0ebf8283 2019-07-24 stsp const struct got_error *
6777 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6778 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6779 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6780 0ebf8283 2019-07-24 stsp {
6781 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6782 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6783 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6784 0ebf8283 2019-07-24 stsp
6785 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6786 0ebf8283 2019-07-24 stsp if (err)
6787 0ebf8283 2019-07-24 stsp return err;
6788 0ebf8283 2019-07-24 stsp
6789 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6790 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6791 0ebf8283 2019-07-24 stsp if (err)
6792 0ebf8283 2019-07-24 stsp goto done;
6793 0ebf8283 2019-07-24 stsp
6794 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6795 0ebf8283 2019-07-24 stsp if (err)
6796 0ebf8283 2019-07-24 stsp goto done;
6797 0ebf8283 2019-07-24 stsp
6798 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6799 0ebf8283 2019-07-24 stsp if (err)
6800 0ebf8283 2019-07-24 stsp goto done;
6801 0ebf8283 2019-07-24 stsp
6802 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6803 0ebf8283 2019-07-24 stsp if (err)
6804 0ebf8283 2019-07-24 stsp goto done;
6805 0ebf8283 2019-07-24 stsp
6806 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6807 0ebf8283 2019-07-24 stsp done:
6808 3e3a69f1 2019-07-25 stsp if (fileindex)
6809 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6810 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6811 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6812 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6813 0ebf8283 2019-07-24 stsp err = unlockerr;
6814 0ebf8283 2019-07-24 stsp return err;
6815 0ebf8283 2019-07-24 stsp }
6816 0ebf8283 2019-07-24 stsp
6817 0ebf8283 2019-07-24 stsp const struct got_error *
6818 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6819 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6820 0ebf8283 2019-07-24 stsp {
6821 0ebf8283 2019-07-24 stsp const struct got_error *err;
6822 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6823 0ebf8283 2019-07-24 stsp
6824 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6825 0ebf8283 2019-07-24 stsp if (err)
6826 0ebf8283 2019-07-24 stsp return err;
6827 0ebf8283 2019-07-24 stsp
6828 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6829 0ebf8283 2019-07-24 stsp if (err)
6830 0ebf8283 2019-07-24 stsp goto done;
6831 0ebf8283 2019-07-24 stsp
6832 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6833 0ebf8283 2019-07-24 stsp done:
6834 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6835 2822a352 2019-10-15 stsp return err;
6836 2822a352 2019-10-15 stsp }
6837 2822a352 2019-10-15 stsp
6838 2822a352 2019-10-15 stsp const struct got_error *
6839 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6840 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6841 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6842 2822a352 2019-10-15 stsp struct got_repository *repo)
6843 2822a352 2019-10-15 stsp {
6844 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6845 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6846 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6847 2822a352 2019-10-15 stsp
6848 2822a352 2019-10-15 stsp *fileindex = NULL;
6849 2822a352 2019-10-15 stsp *branch_ref = NULL;
6850 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6851 2822a352 2019-10-15 stsp
6852 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6853 2822a352 2019-10-15 stsp if (err)
6854 2822a352 2019-10-15 stsp return err;
6855 2822a352 2019-10-15 stsp
6856 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6857 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6858 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6859 2822a352 2019-10-15 stsp "update -b or different branch name required");
6860 2822a352 2019-10-15 stsp goto done;
6861 2822a352 2019-10-15 stsp }
6862 2822a352 2019-10-15 stsp
6863 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6864 2822a352 2019-10-15 stsp if (err)
6865 2822a352 2019-10-15 stsp goto done;
6866 2822a352 2019-10-15 stsp
6867 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6868 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6869 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6870 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6871 2822a352 2019-10-15 stsp &ok_arg);
6872 2822a352 2019-10-15 stsp if (err)
6873 2822a352 2019-10-15 stsp goto done;
6874 2822a352 2019-10-15 stsp
6875 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6876 2822a352 2019-10-15 stsp if (err)
6877 2822a352 2019-10-15 stsp goto done;
6878 2822a352 2019-10-15 stsp
6879 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6880 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6881 2822a352 2019-10-15 stsp done:
6882 2822a352 2019-10-15 stsp if (err) {
6883 2822a352 2019-10-15 stsp if (*branch_ref) {
6884 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6885 2822a352 2019-10-15 stsp *branch_ref = NULL;
6886 2822a352 2019-10-15 stsp }
6887 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6888 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6889 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6890 2822a352 2019-10-15 stsp }
6891 2822a352 2019-10-15 stsp if (*fileindex) {
6892 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6893 2822a352 2019-10-15 stsp *fileindex = NULL;
6894 2822a352 2019-10-15 stsp }
6895 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6896 2822a352 2019-10-15 stsp }
6897 0cb83759 2019-08-03 stsp return err;
6898 0cb83759 2019-08-03 stsp }
6899 0cb83759 2019-08-03 stsp
6900 2822a352 2019-10-15 stsp const struct got_error *
6901 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6902 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6903 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6904 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6905 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6906 2822a352 2019-10-15 stsp {
6907 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6908 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6909 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6910 2822a352 2019-10-15 stsp
6911 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6912 2822a352 2019-10-15 stsp if (err)
6913 2822a352 2019-10-15 stsp goto done;
6914 2822a352 2019-10-15 stsp
6915 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6916 2822a352 2019-10-15 stsp if (err)
6917 2822a352 2019-10-15 stsp goto done;
6918 2822a352 2019-10-15 stsp
6919 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6920 2822a352 2019-10-15 stsp worktree->path_prefix);
6921 2822a352 2019-10-15 stsp if (err)
6922 2822a352 2019-10-15 stsp goto done;
6923 2822a352 2019-10-15 stsp
6924 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6925 2822a352 2019-10-15 stsp if (err)
6926 2822a352 2019-10-15 stsp goto done;
6927 2822a352 2019-10-15 stsp
6928 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6929 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6930 2822a352 2019-10-15 stsp if (err)
6931 2822a352 2019-10-15 stsp goto sync;
6932 2822a352 2019-10-15 stsp
6933 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6934 2822a352 2019-10-15 stsp if (err)
6935 2822a352 2019-10-15 stsp goto sync;
6936 2822a352 2019-10-15 stsp
6937 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6938 2822a352 2019-10-15 stsp sync:
6939 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6940 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6941 2822a352 2019-10-15 stsp err = sync_err;
6942 2822a352 2019-10-15 stsp
6943 2822a352 2019-10-15 stsp done:
6944 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6945 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6946 2822a352 2019-10-15 stsp err = unlockerr;
6947 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6948 2822a352 2019-10-15 stsp
6949 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6950 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6951 2822a352 2019-10-15 stsp err = unlockerr;
6952 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6953 2822a352 2019-10-15 stsp
6954 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6955 2822a352 2019-10-15 stsp free(fileindex_path);
6956 2822a352 2019-10-15 stsp free(tree_id);
6957 2822a352 2019-10-15 stsp
6958 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6959 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6960 2822a352 2019-10-15 stsp err = unlockerr;
6961 2822a352 2019-10-15 stsp return err;
6962 2822a352 2019-10-15 stsp }
6963 2822a352 2019-10-15 stsp
6964 2822a352 2019-10-15 stsp const struct got_error *
6965 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6966 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6967 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6968 2822a352 2019-10-15 stsp {
6969 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6970 8b692cd0 2019-10-21 stsp
6971 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6972 8b692cd0 2019-10-21 stsp
6973 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6974 8b692cd0 2019-10-21 stsp
6975 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6976 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6977 8b692cd0 2019-10-21 stsp err = unlockerr;
6978 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6979 8b692cd0 2019-10-21 stsp
6980 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6981 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6982 8b692cd0 2019-10-21 stsp err = unlockerr;
6983 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6984 8b692cd0 2019-10-21 stsp
6985 8b692cd0 2019-10-21 stsp return err;
6986 2822a352 2019-10-15 stsp }
6987 2822a352 2019-10-15 stsp
6988 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6989 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6990 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6991 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6992 2db2652d 2019-08-07 stsp struct got_repository *repo;
6993 2db2652d 2019-08-07 stsp int have_changes;
6994 2db2652d 2019-08-07 stsp };
6995 2db2652d 2019-08-07 stsp
6996 2db2652d 2019-08-07 stsp const struct got_error *
6997 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6998 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6999 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7000 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7001 735ef5ac 2019-08-03 stsp {
7002 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7003 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7004 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7005 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7006 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7007 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7008 8b13ce36 2019-08-08 stsp
7009 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7010 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7011 8b13ce36 2019-08-08 stsp return NULL;
7012 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7013 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7014 735ef5ac 2019-08-03 stsp
7015 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7016 735ef5ac 2019-08-03 stsp if (ie == NULL)
7017 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7018 735ef5ac 2019-08-03 stsp
7019 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7020 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7021 735ef5ac 2019-08-03 stsp relpath) == -1)
7022 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7023 735ef5ac 2019-08-03 stsp
7024 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7025 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7026 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7027 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7028 735ef5ac 2019-08-03 stsp }
7029 735ef5ac 2019-08-03 stsp
7030 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7031 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7032 3aa5969e 2019-08-06 stsp goto done;
7033 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7034 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7035 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7036 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7037 735ef5ac 2019-08-03 stsp goto done;
7038 3aa5969e 2019-08-06 stsp }
7039 735ef5ac 2019-08-03 stsp
7040 2db2652d 2019-08-07 stsp a->have_changes = 1;
7041 2db2652d 2019-08-07 stsp
7042 735ef5ac 2019-08-03 stsp p = in_repo_path;
7043 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7044 735ef5ac 2019-08-03 stsp p++;
7045 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7046 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7047 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7048 735ef5ac 2019-08-03 stsp done:
7049 735ef5ac 2019-08-03 stsp free(in_repo_path);
7050 dc424a06 2019-08-07 stsp return err;
7051 dc424a06 2019-08-07 stsp }
7052 dc424a06 2019-08-07 stsp
7053 2db2652d 2019-08-07 stsp struct stage_path_arg {
7054 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7055 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7056 2db2652d 2019-08-07 stsp struct got_repository *repo;
7057 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7058 2db2652d 2019-08-07 stsp void *status_arg;
7059 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7060 2db2652d 2019-08-07 stsp void *patch_arg;
7061 7b5dc508 2019-10-28 stsp int staged_something;
7062 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7063 2db2652d 2019-08-07 stsp };
7064 2db2652d 2019-08-07 stsp
7065 2db2652d 2019-08-07 stsp static const struct got_error *
7066 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7067 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7068 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7069 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7070 0cb83759 2019-08-03 stsp {
7071 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7072 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7073 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7074 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7075 0cb83759 2019-08-03 stsp uint32_t stage;
7076 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7077 0aeb8099 2020-07-23 stsp struct stat sb;
7078 8b13ce36 2019-08-08 stsp
7079 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7080 8b13ce36 2019-08-08 stsp return NULL;
7081 0cb83759 2019-08-03 stsp
7082 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7083 d3e7c587 2019-08-03 stsp if (ie == NULL)
7084 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7085 0cb83759 2019-08-03 stsp
7086 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7087 2db2652d 2019-08-07 stsp relpath)== -1)
7088 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7089 0cb83759 2019-08-03 stsp
7090 0cb83759 2019-08-03 stsp switch (status) {
7091 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7092 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7093 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7094 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7095 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7096 0aeb8099 2020-07-23 stsp break;
7097 0aeb8099 2020-07-23 stsp }
7098 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7099 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7100 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7101 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7102 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7103 dc424a06 2019-08-07 stsp if (err)
7104 dc424a06 2019-08-07 stsp break;
7105 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7106 dc424a06 2019-08-07 stsp break;
7107 dc424a06 2019-08-07 stsp } else {
7108 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7109 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7110 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7111 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7112 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7113 dc424a06 2019-08-07 stsp break;
7114 dc424a06 2019-08-07 stsp }
7115 dc424a06 2019-08-07 stsp }
7116 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7117 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7118 0cb83759 2019-08-03 stsp if (err)
7119 dc424a06 2019-08-07 stsp break;
7120 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7121 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7122 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7123 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7124 0cb83759 2019-08-03 stsp else
7125 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7126 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7127 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7128 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7129 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7130 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7131 35213c7c 2020-07-23 stsp ssize_t target_len;
7132 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7133 35213c7c 2020-07-23 stsp sizeof(target_path));
7134 35213c7c 2020-07-23 stsp if (target_len == -1) {
7135 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7136 35213c7c 2020-07-23 stsp ondisk_path);
7137 35213c7c 2020-07-23 stsp break;
7138 35213c7c 2020-07-23 stsp }
7139 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7140 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7141 35213c7c 2020-07-23 stsp a->worktree->root_path);
7142 35213c7c 2020-07-23 stsp if (err)
7143 35213c7c 2020-07-23 stsp break;
7144 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7145 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7146 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7147 35213c7c 2020-07-23 stsp break;
7148 35213c7c 2020-07-23 stsp }
7149 35213c7c 2020-07-23 stsp }
7150 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7151 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7152 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7153 35213c7c 2020-07-23 stsp else
7154 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7155 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7156 0aeb8099 2020-07-23 stsp } else {
7157 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7158 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7159 0aeb8099 2020-07-23 stsp }
7160 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7161 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7162 dc424a06 2019-08-07 stsp break;
7163 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7164 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7165 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7166 0cb83759 2019-08-03 stsp break;
7167 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7168 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7169 d3e7c587 2019-08-03 stsp break;
7170 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7171 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7172 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7173 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7174 dc424a06 2019-08-07 stsp if (err)
7175 dc424a06 2019-08-07 stsp break;
7176 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7177 88f33a19 2019-08-08 stsp break;
7178 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7179 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7180 dc424a06 2019-08-07 stsp break;
7181 88f33a19 2019-08-08 stsp }
7182 dc424a06 2019-08-07 stsp }
7183 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7184 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7185 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7186 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7187 dc424a06 2019-08-07 stsp break;
7188 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7189 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7190 12463d8b 2019-12-13 stsp de_name);
7191 0cb83759 2019-08-03 stsp break;
7192 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7193 d3e7c587 2019-08-03 stsp break;
7194 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7195 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7196 ebf48fd5 2019-08-03 stsp break;
7197 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7198 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7199 2a06fe5f 2019-08-24 stsp break;
7200 0cb83759 2019-08-03 stsp default:
7201 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7202 537ac44b 2019-08-03 stsp break;
7203 0cb83759 2019-08-03 stsp }
7204 dc424a06 2019-08-07 stsp
7205 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7206 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7207 dc424a06 2019-08-07 stsp free(path_content);
7208 2db2652d 2019-08-07 stsp free(ondisk_path);
7209 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7210 0ebf8283 2019-07-24 stsp return err;
7211 0ebf8283 2019-07-24 stsp }
7212 0cb83759 2019-08-03 stsp
7213 0cb83759 2019-08-03 stsp const struct got_error *
7214 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7215 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7216 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7217 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7218 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7219 0cb83759 2019-08-03 stsp {
7220 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7221 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7222 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7223 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7224 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7225 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7226 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7227 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7228 0cb83759 2019-08-03 stsp
7229 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7230 0cb83759 2019-08-03 stsp if (err)
7231 0cb83759 2019-08-03 stsp return err;
7232 0cb83759 2019-08-03 stsp
7233 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7234 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7235 735ef5ac 2019-08-03 stsp if (err)
7236 735ef5ac 2019-08-03 stsp goto done;
7237 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7238 735ef5ac 2019-08-03 stsp if (err)
7239 735ef5ac 2019-08-03 stsp goto done;
7240 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7241 0cb83759 2019-08-03 stsp if (err)
7242 0cb83759 2019-08-03 stsp goto done;
7243 0cb83759 2019-08-03 stsp
7244 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7245 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7246 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7247 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7248 2db2652d 2019-08-07 stsp oka.repo = repo;
7249 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7250 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7251 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7252 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7253 735ef5ac 2019-08-03 stsp if (err)
7254 735ef5ac 2019-08-03 stsp goto done;
7255 735ef5ac 2019-08-03 stsp }
7256 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7257 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7258 2db2652d 2019-08-07 stsp goto done;
7259 2db2652d 2019-08-07 stsp }
7260 735ef5ac 2019-08-03 stsp
7261 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7262 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7263 2db2652d 2019-08-07 stsp spa.repo = repo;
7264 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7265 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7266 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7267 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7268 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7269 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7270 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7271 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7272 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7273 42005733 2019-08-03 stsp if (err)
7274 2db2652d 2019-08-07 stsp goto done;
7275 7b5dc508 2019-10-28 stsp }
7276 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7277 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7278 7b5dc508 2019-10-28 stsp goto done;
7279 0cb83759 2019-08-03 stsp }
7280 0cb83759 2019-08-03 stsp
7281 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7282 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7283 0cb83759 2019-08-03 stsp err = sync_err;
7284 0cb83759 2019-08-03 stsp done:
7285 735ef5ac 2019-08-03 stsp if (head_ref)
7286 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7287 735ef5ac 2019-08-03 stsp free(head_commit_id);
7288 0cb83759 2019-08-03 stsp free(fileindex_path);
7289 0cb83759 2019-08-03 stsp if (fileindex)
7290 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7291 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7292 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7293 0cb83759 2019-08-03 stsp err = unlockerr;
7294 0cb83759 2019-08-03 stsp return err;
7295 0cb83759 2019-08-03 stsp }
7296 ad493afc 2019-08-03 stsp
7297 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7298 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7299 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7300 ad493afc 2019-08-03 stsp struct got_repository *repo;
7301 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7302 ad493afc 2019-08-03 stsp void *progress_arg;
7303 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7304 2e1f37b0 2019-08-08 stsp void *patch_arg;
7305 ad493afc 2019-08-03 stsp };
7306 2e1f37b0 2019-08-08 stsp
7307 2e1f37b0 2019-08-08 stsp static const struct got_error *
7308 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7309 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7310 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7311 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7312 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7313 2e1f37b0 2019-08-08 stsp {
7314 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7315 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7316 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7317 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7318 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7319 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7320 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7321 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7322 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7323 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7324 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7325 2e1f37b0 2019-08-08 stsp
7326 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7327 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7328 2e1f37b0 2019-08-08 stsp
7329 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7330 2e1f37b0 2019-08-08 stsp if (err)
7331 2e1f37b0 2019-08-08 stsp return err;
7332 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7333 2e1f37b0 2019-08-08 stsp if (err)
7334 2e1f37b0 2019-08-08 stsp goto done;
7335 2e1f37b0 2019-08-08 stsp
7336 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7337 2e1f37b0 2019-08-08 stsp if (err)
7338 2e1f37b0 2019-08-08 stsp goto done;
7339 2e1f37b0 2019-08-08 stsp
7340 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7341 2e1f37b0 2019-08-08 stsp if (err)
7342 2e1f37b0 2019-08-08 stsp goto done;
7343 2e1f37b0 2019-08-08 stsp
7344 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7345 2e1f37b0 2019-08-08 stsp if (err)
7346 2e1f37b0 2019-08-08 stsp goto done;
7347 2e1f37b0 2019-08-08 stsp
7348 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7349 2e1f37b0 2019-08-08 stsp if (err)
7350 2e1f37b0 2019-08-08 stsp goto done;
7351 ad493afc 2019-08-03 stsp
7352 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7353 2e1f37b0 2019-08-08 stsp if (err)
7354 2e1f37b0 2019-08-08 stsp goto done;
7355 2e1f37b0 2019-08-08 stsp
7356 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7357 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7358 2e1f37b0 2019-08-08 stsp goto done;
7359 2e1f37b0 2019-08-08 stsp }
7360 2e1f37b0 2019-08-08 stsp
7361 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7362 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7363 2e1f37b0 2019-08-08 stsp goto done;
7364 2e1f37b0 2019-08-08 stsp }
7365 2e1f37b0 2019-08-08 stsp
7366 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7367 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7368 2e1f37b0 2019-08-08 stsp if (err)
7369 2e1f37b0 2019-08-08 stsp goto done;
7370 2e1f37b0 2019-08-08 stsp
7371 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7372 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7373 2e1f37b0 2019-08-08 stsp if (err)
7374 2e1f37b0 2019-08-08 stsp goto done;
7375 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7376 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7377 2e1f37b0 2019-08-08 stsp if (err)
7378 2e1f37b0 2019-08-08 stsp goto done;
7379 2e1f37b0 2019-08-08 stsp
7380 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7381 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7382 2e1f37b0 2019-08-08 stsp goto done;
7383 2e1f37b0 2019-08-08 stsp }
7384 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7385 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7386 2e1f37b0 2019-08-08 stsp goto done;
7387 2e1f37b0 2019-08-08 stsp }
7388 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7389 2e1f37b0 2019-08-08 stsp int choice;
7390 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7391 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7392 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7393 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7394 2e1f37b0 2019-08-08 stsp if (err)
7395 2e1f37b0 2019-08-08 stsp goto done;
7396 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7397 2e1f37b0 2019-08-08 stsp have_content = 1;
7398 19e4b907 2019-08-08 stsp else
7399 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7400 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7401 2e1f37b0 2019-08-08 stsp break;
7402 2e1f37b0 2019-08-08 stsp }
7403 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7404 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7405 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7406 2e1f37b0 2019-08-08 stsp done:
7407 2e1f37b0 2019-08-08 stsp free(label1);
7408 2e1f37b0 2019-08-08 stsp if (blob)
7409 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7410 2e1f37b0 2019-08-08 stsp if (staged_blob)
7411 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7412 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7413 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7414 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7415 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7416 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7417 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7418 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7419 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7420 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7421 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7422 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7423 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7424 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7425 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7426 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7427 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7428 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7429 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7430 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7431 2e1f37b0 2019-08-08 stsp }
7432 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7433 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7434 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7435 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7436 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7437 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7438 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7439 2e1f37b0 2019-08-08 stsp }
7440 2e1f37b0 2019-08-08 stsp free(args);
7441 2e1f37b0 2019-08-08 stsp if (ds) {
7442 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7443 2e1f37b0 2019-08-08 stsp free(ds);
7444 2e1f37b0 2019-08-08 stsp }
7445 2e1f37b0 2019-08-08 stsp if (changes)
7446 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7447 2e1f37b0 2019-08-08 stsp free(path1);
7448 2e1f37b0 2019-08-08 stsp free(path2);
7449 fda8017d 2020-07-23 stsp return err;
7450 fda8017d 2020-07-23 stsp }
7451 fda8017d 2020-07-23 stsp
7452 fda8017d 2020-07-23 stsp static const struct got_error *
7453 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7454 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7455 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7456 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7457 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7458 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7459 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7460 fda8017d 2020-07-23 stsp {
7461 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7462 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7463 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7464 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7465 fda8017d 2020-07-23 stsp FILE *f = NULL;
7466 fda8017d 2020-07-23 stsp struct stat sb;
7467 fda8017d 2020-07-23 stsp
7468 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7469 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7470 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7471 fda8017d 2020-07-23 stsp if (err)
7472 fda8017d 2020-07-23 stsp return err;
7473 fda8017d 2020-07-23 stsp
7474 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7475 fda8017d 2020-07-23 stsp return NULL;
7476 fda8017d 2020-07-23 stsp
7477 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7478 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7479 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7480 fda8017d 2020-07-23 stsp if (err)
7481 fda8017d 2020-07-23 stsp goto done;
7482 fda8017d 2020-07-23 stsp }
7483 fda8017d 2020-07-23 stsp
7484 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7485 fda8017d 2020-07-23 stsp if (f == NULL) {
7486 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7487 fda8017d 2020-07-23 stsp path_unstaged_content);
7488 fda8017d 2020-07-23 stsp goto done;
7489 fda8017d 2020-07-23 stsp }
7490 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7491 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7492 fda8017d 2020-07-23 stsp goto done;
7493 fda8017d 2020-07-23 stsp }
7494 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7495 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7496 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7497 fda8017d 2020-07-23 stsp size_t r;
7498 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7499 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7500 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7501 fda8017d 2020-07-23 stsp goto done;
7502 fda8017d 2020-07-23 stsp }
7503 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7504 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7505 fda8017d 2020-07-23 stsp goto done;
7506 fda8017d 2020-07-23 stsp }
7507 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7508 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7509 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7510 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7511 fda8017d 2020-07-23 stsp progress_arg);
7512 fda8017d 2020-07-23 stsp } else {
7513 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7514 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7515 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7516 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7517 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7518 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7519 fda8017d 2020-07-23 stsp }
7520 fda8017d 2020-07-23 stsp if (err)
7521 fda8017d 2020-07-23 stsp goto done;
7522 fda8017d 2020-07-23 stsp
7523 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7524 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7525 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7526 fda8017d 2020-07-23 stsp } else
7527 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7528 fda8017d 2020-07-23 stsp done:
7529 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7530 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7531 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7532 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7533 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7534 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7535 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7536 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7537 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7538 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7539 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7540 2e1f37b0 2019-08-08 stsp return err;
7541 2e1f37b0 2019-08-08 stsp }
7542 2e1f37b0 2019-08-08 stsp
7543 ad493afc 2019-08-03 stsp static const struct got_error *
7544 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7545 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7546 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7547 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7548 ad493afc 2019-08-03 stsp {
7549 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7550 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7551 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7552 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7553 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7554 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7555 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7556 9bc94a15 2019-08-03 stsp struct stat sb;
7557 ad493afc 2019-08-03 stsp
7558 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7559 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7560 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7561 2e1f37b0 2019-08-08 stsp return NULL;
7562 2e1f37b0 2019-08-08 stsp
7563 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7564 ad493afc 2019-08-03 stsp if (ie == NULL)
7565 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7566 9bc94a15 2019-08-03 stsp
7567 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7568 9bc94a15 2019-08-03 stsp == -1)
7569 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7570 ad493afc 2019-08-03 stsp
7571 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7572 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7573 f69721c3 2019-10-21 stsp if (err)
7574 f69721c3 2019-10-21 stsp goto done;
7575 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7576 f69721c3 2019-10-21 stsp id_str) == -1) {
7577 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7578 f69721c3 2019-10-21 stsp goto done;
7579 f69721c3 2019-10-21 stsp }
7580 f69721c3 2019-10-21 stsp
7581 ad493afc 2019-08-03 stsp switch (staged_status) {
7582 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7583 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7584 ad493afc 2019-08-03 stsp blob_id, 8192);
7585 ad493afc 2019-08-03 stsp if (err)
7586 ad493afc 2019-08-03 stsp break;
7587 ad493afc 2019-08-03 stsp /* fall through */
7588 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7589 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7590 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7591 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7592 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7593 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7594 2e1f37b0 2019-08-08 stsp if (err)
7595 2e1f37b0 2019-08-08 stsp break;
7596 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7597 2e1f37b0 2019-08-08 stsp break;
7598 2e1f37b0 2019-08-08 stsp } else {
7599 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7600 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7601 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7602 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7603 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7604 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7605 2e1f37b0 2019-08-08 stsp }
7606 2e1f37b0 2019-08-08 stsp }
7607 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7608 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7609 ad493afc 2019-08-03 stsp if (err)
7610 ad493afc 2019-08-03 stsp break;
7611 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7612 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7613 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7614 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7615 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7616 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7617 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7618 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7619 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7620 ea7786be 2020-07-23 stsp break;
7621 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7622 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7623 36bf999c 2020-07-23 stsp char *staged_target;
7624 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7625 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7626 36bf999c 2020-07-23 stsp if (err)
7627 36bf999c 2020-07-23 stsp goto done;
7628 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7629 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7630 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7631 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7632 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7633 36bf999c 2020-07-23 stsp free(staged_target);
7634 dfe9fba0 2020-07-23 stsp } else {
7635 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7636 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7637 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7638 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7639 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7640 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7641 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7642 dfe9fba0 2020-07-23 stsp }
7643 ea7786be 2020-07-23 stsp break;
7644 ea7786be 2020-07-23 stsp default:
7645 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7646 ea7786be 2020-07-23 stsp break;
7647 ea7786be 2020-07-23 stsp }
7648 ad493afc 2019-08-03 stsp if (err == NULL)
7649 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7650 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7651 ad493afc 2019-08-03 stsp break;
7652 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7653 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7654 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7655 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7656 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7657 2e1f37b0 2019-08-08 stsp if (err)
7658 2e1f37b0 2019-08-08 stsp break;
7659 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7660 2e1f37b0 2019-08-08 stsp break;
7661 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7662 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7663 2e1f37b0 2019-08-08 stsp break;
7664 2e1f37b0 2019-08-08 stsp }
7665 2e1f37b0 2019-08-08 stsp }
7666 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7667 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7668 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7669 9bc94a15 2019-08-03 stsp if (err)
7670 9bc94a15 2019-08-03 stsp break;
7671 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7672 ad493afc 2019-08-03 stsp break;
7673 ad493afc 2019-08-03 stsp }
7674 f69721c3 2019-10-21 stsp done:
7675 ad493afc 2019-08-03 stsp free(ondisk_path);
7676 ad493afc 2019-08-03 stsp if (blob_base)
7677 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7678 ad493afc 2019-08-03 stsp if (blob_staged)
7679 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7680 f69721c3 2019-10-21 stsp free(id_str);
7681 f69721c3 2019-10-21 stsp free(label_orig);
7682 ad493afc 2019-08-03 stsp return err;
7683 ad493afc 2019-08-03 stsp }
7684 ad493afc 2019-08-03 stsp
7685 ad493afc 2019-08-03 stsp const struct got_error *
7686 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7687 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7688 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7689 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7690 ad493afc 2019-08-03 stsp struct got_repository *repo)
7691 ad493afc 2019-08-03 stsp {
7692 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7693 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7694 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7695 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7696 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7697 ad493afc 2019-08-03 stsp
7698 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7699 ad493afc 2019-08-03 stsp if (err)
7700 ad493afc 2019-08-03 stsp return err;
7701 ad493afc 2019-08-03 stsp
7702 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7703 ad493afc 2019-08-03 stsp if (err)
7704 ad493afc 2019-08-03 stsp goto done;
7705 ad493afc 2019-08-03 stsp
7706 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7707 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7708 ad493afc 2019-08-03 stsp upa.repo = repo;
7709 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7710 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7711 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7712 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7713 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7714 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7715 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7716 ad493afc 2019-08-03 stsp if (err)
7717 ad493afc 2019-08-03 stsp goto done;
7718 ad493afc 2019-08-03 stsp }
7719 ad493afc 2019-08-03 stsp
7720 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7721 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7722 ad493afc 2019-08-03 stsp err = sync_err;
7723 ad493afc 2019-08-03 stsp done:
7724 ad493afc 2019-08-03 stsp free(fileindex_path);
7725 ad493afc 2019-08-03 stsp if (fileindex)
7726 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7727 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7728 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7729 ad493afc 2019-08-03 stsp err = unlockerr;
7730 ad493afc 2019-08-03 stsp return err;
7731 ad493afc 2019-08-03 stsp }
7732 b2118c49 2020-07-28 stsp
7733 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7734 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7735 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7736 b2118c49 2020-07-28 stsp void *info_arg;
7737 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7738 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7739 b2118c49 2020-07-28 stsp void *cancel_arg;
7740 b2118c49 2020-07-28 stsp };
7741 b2118c49 2020-07-28 stsp
7742 b2118c49 2020-07-28 stsp static const struct got_error *
7743 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7744 b2118c49 2020-07-28 stsp {
7745 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7746 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7747 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7748 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7749 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7750 b2118c49 2020-07-28 stsp int stage;
7751 b2118c49 2020-07-28 stsp
7752 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7753 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7754 b2118c49 2020-07-28 stsp
7755 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7756 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7757 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7758 b2118c49 2020-07-28 stsp break;
7759 b2118c49 2020-07-28 stsp }
7760 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7761 b2118c49 2020-07-28 stsp return NULL;
7762 b2118c49 2020-07-28 stsp
7763 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7764 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7765 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7766 b2118c49 2020-07-28 stsp }
7767 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7768 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7769 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7770 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7771 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7772 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7773 b2118c49 2020-07-28 stsp }
7774 b2118c49 2020-07-28 stsp
7775 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7776 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7777 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7778 b2118c49 2020-07-28 stsp }
7779 b2118c49 2020-07-28 stsp
7780 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7781 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7782 b2118c49 2020-07-28 stsp }
7783 b2118c49 2020-07-28 stsp
7784 b2118c49 2020-07-28 stsp const struct got_error *
7785 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7786 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7787 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7788 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7789 b2118c49 2020-07-28 stsp
7790 b2118c49 2020-07-28 stsp {
7791 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7792 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7793 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7794 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7795 b2118c49 2020-07-28 stsp
7796 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7797 b2118c49 2020-07-28 stsp if (err)
7798 b2118c49 2020-07-28 stsp return err;
7799 b2118c49 2020-07-28 stsp
7800 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7801 b2118c49 2020-07-28 stsp if (err)
7802 b2118c49 2020-07-28 stsp goto done;
7803 b2118c49 2020-07-28 stsp
7804 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7805 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7806 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7807 b2118c49 2020-07-28 stsp arg.paths = paths;
7808 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7809 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7810 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7811 b2118c49 2020-07-28 stsp &arg);
7812 b2118c49 2020-07-28 stsp done:
7813 b2118c49 2020-07-28 stsp free(fileindex_path);
7814 b2118c49 2020-07-28 stsp if (fileindex)
7815 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7816 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7817 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7818 b2118c49 2020-07-28 stsp err = unlockerr;
7819 b2118c49 2020-07-28 stsp return err;
7820 b2118c49 2020-07-28 stsp }