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 aedea96d 2020-10-20 stsp char *worktree_path;
457 86c3caaf 2018-03-09 stsp
458 aedea96d 2020-10-20 stsp worktree_path = strdup(path);
459 aedea96d 2020-10-20 stsp if (worktree_path == NULL)
460 aedea96d 2020-10-20 stsp return got_error_from_errno("strdup");
461 aedea96d 2020-10-20 stsp
462 aedea96d 2020-10-20 stsp for (;;) {
463 aedea96d 2020-10-20 stsp char *parent_path;
464 aedea96d 2020-10-20 stsp
465 aedea96d 2020-10-20 stsp err = open_worktree(worktree, worktree_path);
466 aedea96d 2020-10-20 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
467 aedea96d 2020-10-20 stsp free(worktree_path);
468 247140b2 2019-02-05 stsp return err;
469 aedea96d 2020-10-20 stsp }
470 aedea96d 2020-10-20 stsp if (*worktree) {
471 aedea96d 2020-10-20 stsp free(worktree_path);
472 aedea96d 2020-10-20 stsp return NULL;
473 aedea96d 2020-10-20 stsp }
474 aedea96d 2020-10-20 stsp if (worktree_path[0] == '/' && worktree_path[1] == '\0')
475 aedea96d 2020-10-20 stsp break;
476 aedea96d 2020-10-20 stsp err = got_path_dirname(&parent_path, worktree_path);
477 aedea96d 2020-10-20 stsp if (err) {
478 aedea96d 2020-10-20 stsp if (err->code != GOT_ERR_BAD_PATH) {
479 aedea96d 2020-10-20 stsp free(worktree_path);
480 aedea96d 2020-10-20 stsp return err;
481 aedea96d 2020-10-20 stsp }
482 aedea96d 2020-10-20 stsp break;
483 aedea96d 2020-10-20 stsp }
484 aedea96d 2020-10-20 stsp free(worktree_path);
485 aedea96d 2020-10-20 stsp worktree_path = parent_path;
486 aedea96d 2020-10-20 stsp }
487 247140b2 2019-02-05 stsp
488 aedea96d 2020-10-20 stsp free(worktree_path);
489 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
490 247140b2 2019-02-05 stsp }
491 247140b2 2019-02-05 stsp
492 3a6ce05a 2019-02-11 stsp const struct got_error *
493 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
494 86c3caaf 2018-03-09 stsp {
495 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
496 cde76477 2018-03-11 stsp free(worktree->repo_path);
497 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
498 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
499 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
500 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
501 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
502 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
503 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
504 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
505 50b0790e 2020-09-11 stsp free(worktree->gotconfig_path);
506 50b0790e 2020-09-11 stsp got_gotconfig_free(worktree->gotconfig);
507 6d9d28c3 2018-03-11 stsp free(worktree);
508 3a6ce05a 2019-02-11 stsp return err;
509 86c3caaf 2018-03-09 stsp }
510 86c3caaf 2018-03-09 stsp
511 2fbdb5ae 2018-12-29 stsp const char *
512 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
513 c7f4312f 2019-02-05 stsp {
514 c7f4312f 2019-02-05 stsp return worktree->root_path;
515 c7f4312f 2019-02-05 stsp }
516 c7f4312f 2019-02-05 stsp
517 c7f4312f 2019-02-05 stsp const char *
518 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
519 86c3caaf 2018-03-09 stsp {
520 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
521 49520a32 2018-12-29 stsp }
522 49520a32 2018-12-29 stsp const char *
523 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
524 49520a32 2018-12-29 stsp {
525 f609be2e 2018-12-29 stsp return worktree->path_prefix;
526 e5dc7198 2018-12-29 stsp }
527 e5dc7198 2018-12-29 stsp
528 e5dc7198 2018-12-29 stsp const struct got_error *
529 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
530 e5dc7198 2018-12-29 stsp const char *path_prefix)
531 e5dc7198 2018-12-29 stsp {
532 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
533 e5dc7198 2018-12-29 stsp
534 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
535 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
536 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
537 e5dc7198 2018-12-29 stsp }
538 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
539 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
540 e5dc7198 2018-12-29 stsp free(absprefix);
541 e5dc7198 2018-12-29 stsp return NULL;
542 86c3caaf 2018-03-09 stsp }
543 86c3caaf 2018-03-09 stsp
544 bc70eb79 2019-05-09 stsp const char *
545 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
546 86c3caaf 2018-03-09 stsp {
547 36a38700 2019-05-10 stsp return worktree->head_ref_name;
548 024e9686 2019-05-14 stsp }
549 024e9686 2019-05-14 stsp
550 024e9686 2019-05-14 stsp const struct got_error *
551 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
552 024e9686 2019-05-14 stsp struct got_reference *head_ref)
553 024e9686 2019-05-14 stsp {
554 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
555 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
556 024e9686 2019-05-14 stsp
557 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
559 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
560 024e9686 2019-05-14 stsp path_got = NULL;
561 024e9686 2019-05-14 stsp goto done;
562 024e9686 2019-05-14 stsp }
563 024e9686 2019-05-14 stsp
564 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
565 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
566 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
567 024e9686 2019-05-14 stsp goto done;
568 024e9686 2019-05-14 stsp }
569 024e9686 2019-05-14 stsp
570 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
571 024e9686 2019-05-14 stsp if (err)
572 024e9686 2019-05-14 stsp goto done;
573 024e9686 2019-05-14 stsp
574 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
575 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
576 024e9686 2019-05-14 stsp done:
577 024e9686 2019-05-14 stsp free(path_got);
578 024e9686 2019-05-14 stsp if (err)
579 024e9686 2019-05-14 stsp free(head_ref_name);
580 024e9686 2019-05-14 stsp return err;
581 507dc3bb 2018-12-29 stsp }
582 507dc3bb 2018-12-29 stsp
583 b72f483a 2019-02-05 stsp struct got_object_id *
584 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
585 507dc3bb 2018-12-29 stsp {
586 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
587 86c3caaf 2018-03-09 stsp }
588 86c3caaf 2018-03-09 stsp
589 507dc3bb 2018-12-29 stsp const struct got_error *
590 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
591 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
592 507dc3bb 2018-12-29 stsp {
593 507dc3bb 2018-12-29 stsp const struct got_error *err;
594 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
595 507dc3bb 2018-12-29 stsp char *id_str = NULL;
596 507dc3bb 2018-12-29 stsp char *path_got = NULL;
597 507dc3bb 2018-12-29 stsp
598 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
599 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
600 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
601 507dc3bb 2018-12-29 stsp path_got = NULL;
602 507dc3bb 2018-12-29 stsp goto done;
603 507dc3bb 2018-12-29 stsp }
604 507dc3bb 2018-12-29 stsp
605 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
606 507dc3bb 2018-12-29 stsp if (err)
607 507dc3bb 2018-12-29 stsp return err;
608 507dc3bb 2018-12-29 stsp
609 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
610 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
611 507dc3bb 2018-12-29 stsp goto done;
612 507dc3bb 2018-12-29 stsp }
613 507dc3bb 2018-12-29 stsp
614 507dc3bb 2018-12-29 stsp /* Record our base commit. */
615 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
616 507dc3bb 2018-12-29 stsp if (err)
617 507dc3bb 2018-12-29 stsp goto done;
618 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
619 507dc3bb 2018-12-29 stsp if (err)
620 507dc3bb 2018-12-29 stsp goto done;
621 507dc3bb 2018-12-29 stsp
622 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
623 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
624 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
626 507dc3bb 2018-12-29 stsp goto done;
627 507dc3bb 2018-12-29 stsp }
628 507dc3bb 2018-12-29 stsp done:
629 507dc3bb 2018-12-29 stsp if (obj)
630 507dc3bb 2018-12-29 stsp got_object_close(obj);
631 507dc3bb 2018-12-29 stsp free(id_str);
632 507dc3bb 2018-12-29 stsp free(path_got);
633 507dc3bb 2018-12-29 stsp return err;
634 50b0790e 2020-09-11 stsp }
635 50b0790e 2020-09-11 stsp
636 50b0790e 2020-09-11 stsp const struct got_gotconfig *
637 50b0790e 2020-09-11 stsp got_worktree_get_gotconfig(struct got_worktree *worktree)
638 50b0790e 2020-09-11 stsp {
639 50b0790e 2020-09-11 stsp return worktree->gotconfig;
640 507dc3bb 2018-12-29 stsp }
641 507dc3bb 2018-12-29 stsp
642 9d31a1d8 2018-03-11 stsp static const struct got_error *
643 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
644 86c3caaf 2018-03-09 stsp {
645 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
646 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
647 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
648 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
649 86c3caaf 2018-03-09 stsp return NULL;
650 21908da4 2019-01-13 stsp }
651 21908da4 2019-01-13 stsp
652 21908da4 2019-01-13 stsp static const struct got_error *
653 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
654 4a1ddfc2 2019-01-12 stsp {
655 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
656 4a1ddfc2 2019-01-12 stsp char *abspath;
657 4a1ddfc2 2019-01-12 stsp
658 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
659 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
660 4a1ddfc2 2019-01-12 stsp
661 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
662 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
663 ddcd8544 2019-03-11 stsp struct stat sb;
664 ddcd8544 2019-03-11 stsp err = NULL;
665 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
666 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
667 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
668 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
669 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
670 ddcd8544 2019-03-11 stsp }
671 ddcd8544 2019-03-11 stsp }
672 4a1ddfc2 2019-01-12 stsp free(abspath);
673 68c76935 2019-02-19 stsp return err;
674 68c76935 2019-02-19 stsp }
675 68c76935 2019-02-19 stsp
676 68c76935 2019-02-19 stsp static const struct got_error *
677 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
678 68c76935 2019-02-19 stsp {
679 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
680 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
681 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
682 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
683 68c76935 2019-02-19 stsp
684 68c76935 2019-02-19 stsp *same = 1;
685 68c76935 2019-02-19 stsp
686 230a42bd 2019-05-11 jcs for (;;) {
687 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
688 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
690 68c76935 2019-02-19 stsp break;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
693 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
694 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
695 68c76935 2019-02-19 stsp break;
696 68c76935 2019-02-19 stsp }
697 68c76935 2019-02-19 stsp if (flen1 == 0) {
698 68c76935 2019-02-19 stsp if (flen2 != 0)
699 68c76935 2019-02-19 stsp *same = 0;
700 68c76935 2019-02-19 stsp break;
701 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
702 68c76935 2019-02-19 stsp if (flen1 != 0)
703 68c76935 2019-02-19 stsp *same = 0;
704 68c76935 2019-02-19 stsp break;
705 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
706 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
707 68c76935 2019-02-19 stsp *same = 0;
708 68c76935 2019-02-19 stsp break;
709 68c76935 2019-02-19 stsp }
710 68c76935 2019-02-19 stsp } else {
711 68c76935 2019-02-19 stsp *same = 0;
712 68c76935 2019-02-19 stsp break;
713 68c76935 2019-02-19 stsp }
714 68c76935 2019-02-19 stsp }
715 68c76935 2019-02-19 stsp
716 68c76935 2019-02-19 stsp return err;
717 68c76935 2019-02-19 stsp }
718 68c76935 2019-02-19 stsp
719 68c76935 2019-02-19 stsp static const struct got_error *
720 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
721 68c76935 2019-02-19 stsp {
722 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
723 68c76935 2019-02-19 stsp struct stat sb;
724 68c76935 2019-02-19 stsp size_t size1, size2;
725 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
726 68c76935 2019-02-19 stsp
727 68c76935 2019-02-19 stsp *same = 1;
728 68c76935 2019-02-19 stsp
729 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
730 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
731 68c76935 2019-02-19 stsp goto done;
732 68c76935 2019-02-19 stsp }
733 68c76935 2019-02-19 stsp size1 = sb.st_size;
734 68c76935 2019-02-19 stsp
735 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
736 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
737 68c76935 2019-02-19 stsp goto done;
738 68c76935 2019-02-19 stsp }
739 68c76935 2019-02-19 stsp size2 = sb.st_size;
740 68c76935 2019-02-19 stsp
741 68c76935 2019-02-19 stsp if (size1 != size2) {
742 68c76935 2019-02-19 stsp *same = 0;
743 68c76935 2019-02-19 stsp return NULL;
744 68c76935 2019-02-19 stsp }
745 68c76935 2019-02-19 stsp
746 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
747 68c76935 2019-02-19 stsp if (f1 == NULL)
748 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
749 68c76935 2019-02-19 stsp
750 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
751 68c76935 2019-02-19 stsp if (f2 == NULL) {
752 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
753 68c76935 2019-02-19 stsp goto done;
754 68c76935 2019-02-19 stsp }
755 68c76935 2019-02-19 stsp
756 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
757 68c76935 2019-02-19 stsp done:
758 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
759 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
760 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
761 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
762 68c76935 2019-02-19 stsp
763 6353ad76 2019-02-08 stsp return err;
764 6353ad76 2019-02-08 stsp }
765 6353ad76 2019-02-08 stsp
766 6353ad76 2019-02-08 stsp /*
767 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
768 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
769 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
770 6353ad76 2019-02-08 stsp */
771 6353ad76 2019-02-08 stsp static const struct got_error *
772 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
773 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
774 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
775 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
776 f69721c3 2019-10-21 stsp struct got_repository *repo,
777 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
778 6353ad76 2019-02-08 stsp {
779 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
780 6353ad76 2019-02-08 stsp int merged_fd = -1;
781 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
782 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
783 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
784 234035bc 2019-06-01 stsp int overlapcnt = 0;
785 3524bbf9 2020-10-20 stsp char *parent = NULL;
786 3b9f0f87 2020-07-23 stsp char *symlink_path = NULL;
787 3b9f0f87 2020-07-23 stsp FILE *symlinkf = NULL;
788 6353ad76 2019-02-08 stsp
789 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
790 234035bc 2019-06-01 stsp
791 3524bbf9 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
792 3524bbf9 2020-10-20 stsp if (err)
793 3524bbf9 2020-10-20 stsp return err;
794 af54ae4a 2019-02-19 stsp
795 3524bbf9 2020-10-20 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
796 3524bbf9 2020-10-20 stsp err = got_error_from_errno("asprintf");
797 3524bbf9 2020-10-20 stsp goto done;
798 3524bbf9 2020-10-20 stsp }
799 af54ae4a 2019-02-19 stsp
800 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
801 6353ad76 2019-02-08 stsp if (err)
802 6353ad76 2019-02-08 stsp goto done;
803 6353ad76 2019-02-08 stsp
804 af54ae4a 2019-02-19 stsp free(base_path);
805 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
806 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
807 af54ae4a 2019-02-19 stsp base_path = NULL;
808 af54ae4a 2019-02-19 stsp goto done;
809 af54ae4a 2019-02-19 stsp }
810 af54ae4a 2019-02-19 stsp
811 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
812 6353ad76 2019-02-08 stsp if (err)
813 6353ad76 2019-02-08 stsp goto done;
814 46b6ee73 2019-06-04 stsp if (blob_orig) {
815 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
816 46b6ee73 2019-06-04 stsp blob_orig);
817 1430b4e0 2019-03-27 stsp if (err)
818 1430b4e0 2019-03-27 stsp goto done;
819 1430b4e0 2019-03-27 stsp } else {
820 1430b4e0 2019-03-27 stsp /*
821 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
822 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
823 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
824 1430b4e0 2019-03-27 stsp */
825 1430b4e0 2019-03-27 stsp }
826 6353ad76 2019-02-08 stsp
827 3b9f0f87 2020-07-23 stsp /*
828 3b9f0f87 2020-07-23 stsp * In order the run a 3-way merge with a symlink we copy the symlink's
829 3b9f0f87 2020-07-23 stsp * target path into a temporary file and use that file with diff3.
830 3b9f0f87 2020-07-23 stsp */
831 3b9f0f87 2020-07-23 stsp if (S_ISLNK(st_mode)) {
832 3b9f0f87 2020-07-23 stsp char target_path[PATH_MAX];
833 3b9f0f87 2020-07-23 stsp ssize_t target_len;
834 3b9f0f87 2020-07-23 stsp size_t n;
835 3b9f0f87 2020-07-23 stsp
836 3b9f0f87 2020-07-23 stsp free(base_path);
837 3b9f0f87 2020-07-23 stsp if (asprintf(&base_path, "%s/got-symlink-merge",
838 3b9f0f87 2020-07-23 stsp parent) == -1) {
839 3b9f0f87 2020-07-23 stsp err = got_error_from_errno("asprintf");
840 3b9f0f87 2020-07-23 stsp base_path = NULL;
841 3b9f0f87 2020-07-23 stsp goto done;
842 3b9f0f87 2020-07-23 stsp }
843 3b9f0f87 2020-07-23 stsp err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
844 3b9f0f87 2020-07-23 stsp if (err)
845 3b9f0f87 2020-07-23 stsp goto done;
846 3b9f0f87 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
847 3b9f0f87 2020-07-23 stsp sizeof(target_path));
848 3b9f0f87 2020-07-23 stsp if (target_len == -1) {
849 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
850 3b9f0f87 2020-07-23 stsp goto done;
851 3b9f0f87 2020-07-23 stsp }
852 3b9f0f87 2020-07-23 stsp n = fwrite(target_path, 1, target_len, symlinkf);
853 3b9f0f87 2020-07-23 stsp if (n != target_len) {
854 3b9f0f87 2020-07-23 stsp err = got_ferror(symlinkf, GOT_ERR_IO);
855 3b9f0f87 2020-07-23 stsp goto done;
856 3b9f0f87 2020-07-23 stsp }
857 3b9f0f87 2020-07-23 stsp if (fflush(symlinkf) == EOF) {
858 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fflush", symlink_path);
859 3b9f0f87 2020-07-23 stsp goto done;
860 3b9f0f87 2020-07-23 stsp }
861 3b9f0f87 2020-07-23 stsp }
862 3b9f0f87 2020-07-23 stsp
863 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
864 3b9f0f87 2020-07-23 stsp blob_orig_path, symlink_path ? symlink_path : ondisk_path,
865 3b9f0f87 2020-07-23 stsp label_deriv, label_orig, NULL);
866 6353ad76 2019-02-08 stsp if (err)
867 6353ad76 2019-02-08 stsp goto done;
868 6353ad76 2019-02-08 stsp
869 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
870 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
871 1ee397ad 2019-07-12 stsp if (err)
872 1ee397ad 2019-07-12 stsp goto done;
873 6353ad76 2019-02-08 stsp
874 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
875 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
876 816dc654 2019-02-16 stsp goto done;
877 68c76935 2019-02-19 stsp }
878 68c76935 2019-02-19 stsp
879 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
880 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
881 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
882 68c76935 2019-02-19 stsp merged_path);
883 68c76935 2019-02-19 stsp if (err)
884 68c76935 2019-02-19 stsp goto done;
885 816dc654 2019-02-16 stsp }
886 6353ad76 2019-02-08 stsp
887 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
888 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
889 70a0c8ec 2019-02-20 stsp goto done;
890 70a0c8ec 2019-02-20 stsp }
891 70a0c8ec 2019-02-20 stsp
892 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
893 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
894 230a42bd 2019-05-11 jcs ondisk_path);
895 6353ad76 2019-02-08 stsp goto done;
896 6353ad76 2019-02-08 stsp }
897 6353ad76 2019-02-08 stsp done:
898 fdcb7daf 2019-12-15 stsp if (err) {
899 fdcb7daf 2019-12-15 stsp if (merged_path)
900 fdcb7daf 2019-12-15 stsp unlink(merged_path);
901 3b9f0f87 2020-07-23 stsp }
902 3b9f0f87 2020-07-23 stsp if (symlink_path) {
903 3b9f0f87 2020-07-23 stsp if (unlink(symlink_path) == -1 && err == NULL)
904 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("unlink", symlink_path);
905 fdcb7daf 2019-12-15 stsp }
906 3b9f0f87 2020-07-23 stsp if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
907 3b9f0f87 2020-07-23 stsp err = got_error_from_errno2("fclose", symlink_path);
908 3b9f0f87 2020-07-23 stsp free(symlink_path);
909 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
910 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
911 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
912 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
913 6353ad76 2019-02-08 stsp free(merged_path);
914 af54ae4a 2019-02-19 stsp free(base_path);
915 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
916 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
917 46b6ee73 2019-06-04 stsp free(blob_orig_path);
918 af57b12a 2020-07-23 stsp }
919 3524bbf9 2020-10-20 stsp free(parent);
920 af57b12a 2020-07-23 stsp return err;
921 af57b12a 2020-07-23 stsp }
922 af57b12a 2020-07-23 stsp
923 af57b12a 2020-07-23 stsp static const struct got_error *
924 af57b12a 2020-07-23 stsp update_symlink(const char *ondisk_path, const char *target_path,
925 af57b12a 2020-07-23 stsp size_t target_len)
926 af57b12a 2020-07-23 stsp {
927 af57b12a 2020-07-23 stsp /* This is not atomic but matches what 'ln -sf' does. */
928 af57b12a 2020-07-23 stsp if (unlink(ondisk_path) == -1)
929 af57b12a 2020-07-23 stsp return got_error_from_errno2("unlink", ondisk_path);
930 af57b12a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1)
931 af57b12a 2020-07-23 stsp return got_error_from_errno3("symlink", target_path,
932 af57b12a 2020-07-23 stsp ondisk_path);
933 af57b12a 2020-07-23 stsp return NULL;
934 af57b12a 2020-07-23 stsp }
935 af57b12a 2020-07-23 stsp
936 af57b12a 2020-07-23 stsp /*
937 11cc08c1 2020-07-23 stsp * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
938 11cc08c1 2020-07-23 stsp * in the work tree with a file that contains conflict markers and the
939 993e2a1b 2020-07-23 stsp * conflicting target paths of the original version, a "derived version"
940 993e2a1b 2020-07-23 stsp * of a symlink from an incoming change, and a local version of the symlink.
941 993e2a1b 2020-07-23 stsp *
942 993e2a1b 2020-07-23 stsp * The original versions's target path can be NULL if it is not available,
943 993e2a1b 2020-07-23 stsp * such as if both derived versions added a new symlink at the same path.
944 993e2a1b 2020-07-23 stsp *
945 993e2a1b 2020-07-23 stsp * The incoming derived symlink target is NULL in case the incoming change
946 993e2a1b 2020-07-23 stsp * has deleted this symlink.
947 11cc08c1 2020-07-23 stsp */
948 11cc08c1 2020-07-23 stsp static const struct got_error *
949 11cc08c1 2020-07-23 stsp install_symlink_conflict(const char *deriv_target,
950 11cc08c1 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, const char *orig_target,
951 11cc08c1 2020-07-23 stsp const char *label_orig, const char *local_target, const char *ondisk_path)
952 11cc08c1 2020-07-23 stsp {
953 11cc08c1 2020-07-23 stsp const struct got_error *err;
954 11cc08c1 2020-07-23 stsp char *id_str = NULL, *label_deriv = NULL, *path = NULL;
955 11cc08c1 2020-07-23 stsp FILE *f = NULL;
956 11cc08c1 2020-07-23 stsp
957 11cc08c1 2020-07-23 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
958 11cc08c1 2020-07-23 stsp if (err)
959 11cc08c1 2020-07-23 stsp return got_error_from_errno("asprintf");
960 11cc08c1 2020-07-23 stsp
961 11cc08c1 2020-07-23 stsp if (asprintf(&label_deriv, "%s: commit %s",
962 11cc08c1 2020-07-23 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
963 11cc08c1 2020-07-23 stsp err = got_error_from_errno("asprintf");
964 11cc08c1 2020-07-23 stsp goto done;
965 11cc08c1 2020-07-23 stsp }
966 11cc08c1 2020-07-23 stsp
967 11cc08c1 2020-07-23 stsp err = got_opentemp_named(&path, &f, "got-symlink-conflict");
968 11cc08c1 2020-07-23 stsp if (err)
969 11cc08c1 2020-07-23 stsp goto done;
970 11cc08c1 2020-07-23 stsp
971 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
972 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
973 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
974 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
975 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
976 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
977 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
978 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
979 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
980 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
981 11cc08c1 2020-07-23 stsp goto done;
982 11cc08c1 2020-07-23 stsp }
983 11cc08c1 2020-07-23 stsp
984 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
985 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
986 11cc08c1 2020-07-23 stsp goto done;
987 11cc08c1 2020-07-23 stsp }
988 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
989 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
990 11cc08c1 2020-07-23 stsp goto done;
991 11cc08c1 2020-07-23 stsp }
992 11cc08c1 2020-07-23 stsp if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
993 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("chmod", ondisk_path);
994 11cc08c1 2020-07-23 stsp goto done;
995 11cc08c1 2020-07-23 stsp }
996 11cc08c1 2020-07-23 stsp done:
997 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
998 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
999 11cc08c1 2020-07-23 stsp free(path);
1000 11cc08c1 2020-07-23 stsp free(id_str);
1001 11cc08c1 2020-07-23 stsp free(label_deriv);
1002 11cc08c1 2020-07-23 stsp return err;
1003 11cc08c1 2020-07-23 stsp }
1004 d219f183 2020-07-23 stsp
1005 d219f183 2020-07-23 stsp /* forward declaration */
1006 d219f183 2020-07-23 stsp static const struct got_error *
1007 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1008 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
1009 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
1010 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
1011 11cc08c1 2020-07-23 stsp
1012 11cc08c1 2020-07-23 stsp /*
1013 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1014 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1015 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1016 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1017 af57b12a 2020-07-23 stsp */
1018 af57b12a 2020-07-23 stsp static const struct got_error *
1019 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1020 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1021 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1022 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1023 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1024 af57b12a 2020-07-23 stsp {
1025 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1026 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1027 af57b12a 2020-07-23 stsp struct stat sb;
1028 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1029 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1030 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1031 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1032 af57b12a 2020-07-23 stsp
1033 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1034 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1035 af57b12a 2020-07-23 stsp
1036 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1037 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1038 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1039 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1040 af57b12a 2020-07-23 stsp ondisk_path);
1041 af57b12a 2020-07-23 stsp goto done;
1042 af57b12a 2020-07-23 stsp }
1043 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1044 af57b12a 2020-07-23 stsp
1045 526a746f 2020-07-23 stsp if (blob_orig) {
1046 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1047 526a746f 2020-07-23 stsp if (err)
1048 526a746f 2020-07-23 stsp goto done;
1049 526a746f 2020-07-23 stsp }
1050 af57b12a 2020-07-23 stsp
1051 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1052 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1053 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1054 11cc08c1 2020-07-23 stsp have_local_change = 1;
1055 11cc08c1 2020-07-23 stsp
1056 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1057 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1058 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1059 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1060 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1061 11cc08c1 2020-07-23 stsp
1062 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1063 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1064 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1065 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1066 11cc08c1 2020-07-23 stsp path);
1067 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1068 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1069 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1070 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1071 11cc08c1 2020-07-23 stsp path);
1072 11cc08c1 2020-07-23 stsp } else {
1073 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1074 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1075 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1076 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1077 11cc08c1 2020-07-23 stsp if (err)
1078 11cc08c1 2020-07-23 stsp goto done;
1079 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1080 11cc08c1 2020-07-23 stsp path);
1081 11cc08c1 2020-07-23 stsp }
1082 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1083 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1084 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1085 af57b12a 2020-07-23 stsp strlen(deriv_target));
1086 af57b12a 2020-07-23 stsp if (err)
1087 af57b12a 2020-07-23 stsp goto done;
1088 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1089 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1090 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1091 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1092 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1093 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1094 ea7786be 2020-07-23 stsp path);
1095 ea7786be 2020-07-23 stsp } else {
1096 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1097 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1098 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1099 ea7786be 2020-07-23 stsp if (err)
1100 ea7786be 2020-07-23 stsp goto done;
1101 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1102 ea7786be 2020-07-23 stsp path);
1103 ea7786be 2020-07-23 stsp }
1104 6353ad76 2019-02-08 stsp }
1105 11cc08c1 2020-07-23 stsp
1106 af57b12a 2020-07-23 stsp done:
1107 af57b12a 2020-07-23 stsp free(ancestor_target);
1108 14c901f1 2019-08-08 stsp return err;
1109 14c901f1 2019-08-08 stsp }
1110 14c901f1 2019-08-08 stsp
1111 14c901f1 2019-08-08 stsp /*
1112 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1113 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1114 14c901f1 2019-08-08 stsp * acts as the second derived version.
1115 14c901f1 2019-08-08 stsp */
1116 14c901f1 2019-08-08 stsp static const struct got_error *
1117 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1118 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1119 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1120 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1121 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1122 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1123 14c901f1 2019-08-08 stsp {
1124 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1125 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1126 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1127 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1128 14c901f1 2019-08-08 stsp
1129 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1130 14c901f1 2019-08-08 stsp
1131 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1132 ed6b5030 2020-10-20 stsp if (err)
1133 ed6b5030 2020-10-20 stsp return err;
1134 14c901f1 2019-08-08 stsp
1135 14c901f1 2019-08-08 stsp free(base_path);
1136 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1137 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1138 14c901f1 2019-08-08 stsp base_path = NULL;
1139 14c901f1 2019-08-08 stsp goto done;
1140 14c901f1 2019-08-08 stsp }
1141 14c901f1 2019-08-08 stsp
1142 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1143 14c901f1 2019-08-08 stsp if (err)
1144 14c901f1 2019-08-08 stsp goto done;
1145 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1146 14c901f1 2019-08-08 stsp blob_deriv);
1147 14c901f1 2019-08-08 stsp if (err)
1148 14c901f1 2019-08-08 stsp goto done;
1149 14c901f1 2019-08-08 stsp
1150 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1151 14c901f1 2019-08-08 stsp if (err)
1152 14c901f1 2019-08-08 stsp goto done;
1153 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1154 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1155 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1156 14c901f1 2019-08-08 stsp goto done;
1157 14c901f1 2019-08-08 stsp }
1158 14c901f1 2019-08-08 stsp
1159 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1160 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1161 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1162 14c901f1 2019-08-08 stsp done:
1163 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1164 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1165 14c901f1 2019-08-08 stsp free(base_path);
1166 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1167 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1168 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1169 14c901f1 2019-08-08 stsp }
1170 6353ad76 2019-02-08 stsp free(id_str);
1171 818c7501 2019-07-11 stsp free(label_deriv);
1172 ed6b5030 2020-10-20 stsp free(parent);
1173 4a1ddfc2 2019-01-12 stsp return err;
1174 4a1ddfc2 2019-01-12 stsp }
1175 4a1ddfc2 2019-01-12 stsp
1176 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1177 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1178 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1179 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1180 13d9040b 2019-03-27 stsp {
1181 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1182 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1183 13d9040b 2019-03-27 stsp
1184 65b05cec 2020-07-23 stsp *new_iep = NULL;
1185 65b05cec 2020-07-23 stsp
1186 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1187 054041d0 2020-06-24 stsp if (err)
1188 054041d0 2020-06-24 stsp return err;
1189 054041d0 2020-06-24 stsp
1190 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1191 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1192 054041d0 2020-06-24 stsp if (err)
1193 054041d0 2020-06-24 stsp goto done;
1194 054041d0 2020-06-24 stsp
1195 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1196 054041d0 2020-06-24 stsp done:
1197 054041d0 2020-06-24 stsp if (err)
1198 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1199 65b05cec 2020-07-23 stsp else
1200 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1201 13d9040b 2019-03-27 stsp return err;
1202 1ebedb77 2019-10-19 stsp }
1203 1ebedb77 2019-10-19 stsp
1204 1ebedb77 2019-10-19 stsp static mode_t
1205 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1206 1ebedb77 2019-10-19 stsp {
1207 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1208 1ebedb77 2019-10-19 stsp
1209 1ebedb77 2019-10-19 stsp if (executable) {
1210 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1211 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1212 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1213 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1214 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1215 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1216 1ebedb77 2019-10-19 stsp }
1217 1ebedb77 2019-10-19 stsp
1218 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1219 13d9040b 2019-03-27 stsp }
1220 13d9040b 2019-03-27 stsp
1221 8ba819a3 2020-07-23 stsp /* forward declaration */
1222 13d9040b 2019-03-27 stsp static const struct got_error *
1223 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1224 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1225 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1226 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1227 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1228 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1229 8ba819a3 2020-07-23 stsp
1230 5a1fbc73 2020-07-23 stsp /*
1231 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1232 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1233 5a1fbc73 2020-07-23 stsp */
1234 8ba819a3 2020-07-23 stsp static const struct got_error *
1235 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1236 5a1fbc73 2020-07-23 stsp size_t target_len)
1237 5a1fbc73 2020-07-23 stsp {
1238 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1239 5a1fbc73 2020-07-23 stsp ssize_t elen;
1240 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1241 5a1fbc73 2020-07-23 stsp int fd;
1242 5a1fbc73 2020-07-23 stsp
1243 5a1fbc73 2020-07-23 stsp /*
1244 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1245 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1246 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1247 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1248 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1249 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1250 5a1fbc73 2020-07-23 stsp */
1251 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1252 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1253 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1254 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1255 5a1fbc73 2020-07-23 stsp
1256 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1257 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1258 5a1fbc73 2020-07-23 stsp if (elen == -1)
1259 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1260 5a1fbc73 2020-07-23 stsp
1261 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1262 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1263 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1264 5a1fbc73 2020-07-23 stsp }
1265 5a1fbc73 2020-07-23 stsp
1266 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1267 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1268 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1269 5a1fbc73 2020-07-23 stsp return err;
1270 3c1ec782 2020-07-23 stsp }
1271 3c1ec782 2020-07-23 stsp
1272 3c1ec782 2020-07-23 stsp static const struct got_error *
1273 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1274 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1275 3c1ec782 2020-07-23 stsp {
1276 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1277 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1278 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1279 3c1ec782 2020-07-23 stsp
1280 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1281 3c1ec782 2020-07-23 stsp
1282 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1283 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1284 3c1ec782 2020-07-23 stsp return NULL;
1285 3c1ec782 2020-07-23 stsp }
1286 3c1ec782 2020-07-23 stsp
1287 3c1ec782 2020-07-23 stsp /*
1288 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1289 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1290 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1291 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1292 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1293 3c1ec782 2020-07-23 stsp */
1294 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1295 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1296 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1297 ce031e9e 2020-10-20 stsp if (err)
1298 ce031e9e 2020-10-20 stsp return err;
1299 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1300 ce031e9e 2020-10-20 stsp free(parent);
1301 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1302 ce031e9e 2020-10-20 stsp }
1303 ce031e9e 2020-10-20 stsp free(parent);
1304 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1305 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1306 3c1ec782 2020-07-23 stsp free(abspath);
1307 3c1ec782 2020-07-23 stsp return err;
1308 3c1ec782 2020-07-23 stsp }
1309 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1310 3c1ec782 2020-07-23 stsp free(abspath);
1311 3c1ec782 2020-07-23 stsp if (err)
1312 3c1ec782 2020-07-23 stsp return err;
1313 3c1ec782 2020-07-23 stsp } else {
1314 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1315 3c1ec782 2020-07-23 stsp if (err)
1316 3c1ec782 2020-07-23 stsp return err;
1317 3c1ec782 2020-07-23 stsp }
1318 3c1ec782 2020-07-23 stsp
1319 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1320 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1321 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1322 3c1ec782 2020-07-23 stsp return NULL;
1323 3c1ec782 2020-07-23 stsp }
1324 3c1ec782 2020-07-23 stsp
1325 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1326 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1327 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1328 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1329 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1330 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1331 3c1ec782 2020-07-23 stsp
1332 3c1ec782 2020-07-23 stsp free(path_got);
1333 3c1ec782 2020-07-23 stsp return NULL;
1334 5a1fbc73 2020-07-23 stsp }
1335 5a1fbc73 2020-07-23 stsp
1336 5a1fbc73 2020-07-23 stsp static const struct got_error *
1337 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1338 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1339 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1340 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1341 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1342 9d31a1d8 2018-03-11 stsp {
1343 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1344 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1345 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1346 906c123b 2020-07-23 stsp char *path_got = NULL;
1347 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1348 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1349 8ba819a3 2020-07-23 stsp
1350 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1351 2e1fa222 2020-07-23 stsp
1352 8ba819a3 2020-07-23 stsp /*
1353 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1354 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1355 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1356 8ba819a3 2020-07-23 stsp * in the blob object.
1357 8ba819a3 2020-07-23 stsp */
1358 8ba819a3 2020-07-23 stsp do {
1359 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1360 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1361 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1362 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1363 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1364 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1365 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1366 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1367 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1368 3b9f0f87 2020-07-23 stsp progress_arg);
1369 8ba819a3 2020-07-23 stsp }
1370 8ba819a3 2020-07-23 stsp if (len > 0) {
1371 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1372 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1373 8ba819a3 2020-07-23 stsp len - hdrlen);
1374 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1375 8ba819a3 2020-07-23 stsp hdrlen = 0;
1376 8ba819a3 2020-07-23 stsp }
1377 8ba819a3 2020-07-23 stsp } while (len != 0);
1378 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1379 8ba819a3 2020-07-23 stsp
1380 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1381 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1382 3c1ec782 2020-07-23 stsp if (err)
1383 3c1ec782 2020-07-23 stsp return err;
1384 8ba819a3 2020-07-23 stsp
1385 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1386 906c123b 2020-07-23 stsp /* install as a regular file */
1387 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1388 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1389 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1390 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1391 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1392 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1393 906c123b 2020-07-23 stsp goto done;
1394 906c123b 2020-07-23 stsp }
1395 906c123b 2020-07-23 stsp
1396 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1397 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1398 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1399 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1400 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1401 c90c8ce3 2020-07-23 stsp goto done;
1402 c90c8ce3 2020-07-23 stsp }
1403 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1404 5a1fbc73 2020-07-23 stsp target_path, target_len);
1405 5a1fbc73 2020-07-23 stsp if (err)
1406 f35fa46a 2020-07-23 stsp goto done;
1407 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1408 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1409 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1410 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1411 6e1eade5 2020-07-23 stsp path);
1412 f35fa46a 2020-07-23 stsp }
1413 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1414 f35fa46a 2020-07-23 stsp }
1415 f35fa46a 2020-07-23 stsp
1416 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1417 f4994adc 2020-10-20 stsp char *parent;
1418 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1419 f4994adc 2020-10-20 stsp if (err)
1420 f4994adc 2020-10-20 stsp goto done;
1421 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1422 f4994adc 2020-10-20 stsp free(parent);
1423 f35fa46a 2020-07-23 stsp if (err)
1424 f35fa46a 2020-07-23 stsp goto done;
1425 f35fa46a 2020-07-23 stsp /*
1426 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1427 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1428 f35fa46a 2020-07-23 stsp */
1429 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1430 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1431 f35fa46a 2020-07-23 stsp goto done;
1432 f35fa46a 2020-07-23 stsp }
1433 f35fa46a 2020-07-23 stsp }
1434 f35fa46a 2020-07-23 stsp
1435 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1436 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1437 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1438 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1439 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1440 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1441 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1442 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1443 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1444 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1445 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1446 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1447 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1448 8ba819a3 2020-07-23 stsp } else {
1449 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1450 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1451 8ba819a3 2020-07-23 stsp }
1452 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1453 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1454 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1455 8ba819a3 2020-07-23 stsp done:
1456 906c123b 2020-07-23 stsp free(path_got);
1457 8ba819a3 2020-07-23 stsp return err;
1458 8ba819a3 2020-07-23 stsp }
1459 8ba819a3 2020-07-23 stsp
1460 8ba819a3 2020-07-23 stsp static const struct got_error *
1461 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1462 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1463 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1464 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1465 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1466 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1467 8ba819a3 2020-07-23 stsp {
1468 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1469 507dc3bb 2018-12-29 stsp int fd = -1;
1470 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1471 507dc3bb 2018-12-29 stsp int update = 0;
1472 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1473 8ba819a3 2020-07-23 stsp
1474 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1475 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1476 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1477 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1478 f5375317 2020-10-20 stsp char *parent;
1479 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1480 f5375317 2020-10-20 stsp if (err)
1481 f5375317 2020-10-20 stsp return err;
1482 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1483 f5375317 2020-10-20 stsp free(parent);
1484 21908da4 2019-01-13 stsp if (err)
1485 21908da4 2019-01-13 stsp return err;
1486 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1487 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1488 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1489 21908da4 2019-01-13 stsp if (fd == -1)
1490 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1491 230a42bd 2019-05-11 jcs ondisk_path);
1492 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1493 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1494 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1495 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1496 3b9f0f87 2020-07-23 stsp goto done;
1497 3b9f0f87 2020-07-23 stsp }
1498 bd6aa359 2020-07-23 stsp if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1499 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1500 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1501 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1502 507dc3bb 2018-12-29 stsp goto done;
1503 d70b8e30 2018-12-27 stsp } else {
1504 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1505 507dc3bb 2018-12-29 stsp ondisk_path);
1506 507dc3bb 2018-12-29 stsp if (err)
1507 507dc3bb 2018-12-29 stsp goto done;
1508 507dc3bb 2018-12-29 stsp update = 1;
1509 9d31a1d8 2018-03-11 stsp }
1510 507dc3bb 2018-12-29 stsp } else
1511 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1512 9d31a1d8 2018-03-11 stsp }
1513 9d31a1d8 2018-03-11 stsp
1514 bd6aa359 2020-07-23 stsp if (progress_cb) {
1515 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1516 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1517 bd6aa359 2020-07-23 stsp path);
1518 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1519 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1520 bd6aa359 2020-07-23 stsp path);
1521 bd6aa359 2020-07-23 stsp else
1522 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1523 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1524 bd6aa359 2020-07-23 stsp if (err)
1525 bd6aa359 2020-07-23 stsp goto done;
1526 bd6aa359 2020-07-23 stsp }
1527 d7b62c98 2018-12-27 stsp
1528 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1529 9d31a1d8 2018-03-11 stsp do {
1530 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1531 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1532 9d31a1d8 2018-03-11 stsp if (err)
1533 9d31a1d8 2018-03-11 stsp break;
1534 9d31a1d8 2018-03-11 stsp if (len > 0) {
1535 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1536 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1537 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1538 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1539 b87c6f83 2018-12-24 stsp goto done;
1540 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1541 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1542 b87c6f83 2018-12-24 stsp goto done;
1543 9d31a1d8 2018-03-11 stsp }
1544 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1545 9d31a1d8 2018-03-11 stsp }
1546 9d31a1d8 2018-03-11 stsp } while (len != 0);
1547 9d31a1d8 2018-03-11 stsp
1548 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1549 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1550 816dc654 2019-02-16 stsp goto done;
1551 816dc654 2019-02-16 stsp }
1552 9d31a1d8 2018-03-11 stsp
1553 507dc3bb 2018-12-29 stsp if (update) {
1554 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1555 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1556 230a42bd 2019-05-11 jcs ondisk_path);
1557 2a57020b 2019-02-20 stsp unlink(tmppath);
1558 68ed9ba5 2019-02-10 stsp goto done;
1559 68ed9ba5 2019-02-10 stsp }
1560 68ed9ba5 2019-02-10 stsp }
1561 ba8a0d4d 2019-02-10 stsp
1562 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1563 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1564 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1565 1ebedb77 2019-10-19 stsp goto done;
1566 507dc3bb 2018-12-29 stsp }
1567 507dc3bb 2018-12-29 stsp
1568 9d31a1d8 2018-03-11 stsp done:
1569 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1570 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1571 507dc3bb 2018-12-29 stsp free(tmppath);
1572 6353ad76 2019-02-08 stsp return err;
1573 6353ad76 2019-02-08 stsp }
1574 6353ad76 2019-02-08 stsp
1575 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1576 6353ad76 2019-02-08 stsp static const struct got_error *
1577 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1578 7154f6ce 2019-03-27 stsp {
1579 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1580 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1581 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1582 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1583 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1584 7154f6ce 2019-03-27 stsp };
1585 7154f6ce 2019-03-27 stsp int i = 0;
1586 7154f6ce 2019-03-27 stsp char *line;
1587 7154f6ce 2019-03-27 stsp size_t len;
1588 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1589 7154f6ce 2019-03-27 stsp
1590 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1591 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1592 7154f6ce 2019-03-27 stsp if (line == NULL) {
1593 7154f6ce 2019-03-27 stsp if (feof(f))
1594 7154f6ce 2019-03-27 stsp break;
1595 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1596 7154f6ce 2019-03-27 stsp break;
1597 7154f6ce 2019-03-27 stsp }
1598 7154f6ce 2019-03-27 stsp
1599 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1600 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1601 19332e6d 2019-05-13 stsp == 0)
1602 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1603 7154f6ce 2019-03-27 stsp else
1604 7154f6ce 2019-03-27 stsp i++;
1605 7154f6ce 2019-03-27 stsp }
1606 7154f6ce 2019-03-27 stsp }
1607 7154f6ce 2019-03-27 stsp
1608 7154f6ce 2019-03-27 stsp return err;
1609 e2b1e152 2019-07-13 stsp }
1610 e2b1e152 2019-07-13 stsp
1611 e2b1e152 2019-07-13 stsp static int
1612 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1613 1ebedb77 2019-10-19 stsp {
1614 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1615 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1616 1ebedb77 2019-10-19 stsp }
1617 1ebedb77 2019-10-19 stsp
1618 1ebedb77 2019-10-19 stsp static int
1619 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1620 e2b1e152 2019-07-13 stsp {
1621 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1622 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1623 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1624 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1625 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1626 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1627 c363b2c1 2019-08-03 stsp }
1628 c363b2c1 2019-08-03 stsp
1629 c363b2c1 2019-08-03 stsp static unsigned char
1630 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1631 c363b2c1 2019-08-03 stsp {
1632 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1633 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1634 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1635 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1636 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1637 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1638 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1639 c363b2c1 2019-08-03 stsp default:
1640 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1641 a919d5c4 2020-07-23 stsp }
1642 a919d5c4 2020-07-23 stsp }
1643 a919d5c4 2020-07-23 stsp
1644 a919d5c4 2020-07-23 stsp static const struct got_error *
1645 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1646 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1647 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1648 a919d5c4 2020-07-23 stsp {
1649 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1650 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1651 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1652 a919d5c4 2020-07-23 stsp ssize_t elen;
1653 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1654 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1655 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1656 a919d5c4 2020-07-23 stsp
1657 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1658 a919d5c4 2020-07-23 stsp
1659 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1660 a919d5c4 2020-07-23 stsp do {
1661 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1662 a919d5c4 2020-07-23 stsp if (err)
1663 a919d5c4 2020-07-23 stsp return err;
1664 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1665 a919d5c4 2020-07-23 stsp /*
1666 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1667 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1668 a919d5c4 2020-07-23 stsp */
1669 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1670 a919d5c4 2020-07-23 stsp }
1671 a919d5c4 2020-07-23 stsp if (len > 0) {
1672 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1673 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1674 a919d5c4 2020-07-23 stsp len - hdrlen);
1675 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1676 a919d5c4 2020-07-23 stsp hdrlen = 0;
1677 a919d5c4 2020-07-23 stsp }
1678 a919d5c4 2020-07-23 stsp } while (len != 0);
1679 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1680 a919d5c4 2020-07-23 stsp
1681 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1682 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1683 a919d5c4 2020-07-23 stsp if (elen == -1)
1684 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1685 a919d5c4 2020-07-23 stsp } else {
1686 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1687 a919d5c4 2020-07-23 stsp if (elen == -1)
1688 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1689 c363b2c1 2019-08-03 stsp }
1690 a919d5c4 2020-07-23 stsp
1691 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1692 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1693 a919d5c4 2020-07-23 stsp
1694 a919d5c4 2020-07-23 stsp return NULL;
1695 7154f6ce 2019-03-27 stsp }
1696 7154f6ce 2019-03-27 stsp
1697 7154f6ce 2019-03-27 stsp static const struct got_error *
1698 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1699 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1700 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1701 6353ad76 2019-02-08 stsp {
1702 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1703 6353ad76 2019-02-08 stsp struct got_object_id id;
1704 6353ad76 2019-02-08 stsp size_t hdrlen;
1705 1338848f 2019-12-13 stsp int fd = -1;
1706 6353ad76 2019-02-08 stsp FILE *f = NULL;
1707 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1708 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1709 6353ad76 2019-02-08 stsp size_t flen, blen;
1710 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1711 6353ad76 2019-02-08 stsp
1712 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1713 6353ad76 2019-02-08 stsp
1714 7f91a133 2019-12-13 stsp /*
1715 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1716 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1717 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1718 7f91a133 2019-12-13 stsp */
1719 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1720 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1721 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1722 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1723 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1724 882ef1b9 2019-12-13 stsp else
1725 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1726 882ef1b9 2019-12-13 stsp goto done;
1727 882ef1b9 2019-12-13 stsp }
1728 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1729 3d35a492 2019-12-13 stsp goto done;
1730 3d35a492 2019-12-13 stsp }
1731 7f91a133 2019-12-13 stsp } else {
1732 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1733 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1734 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1735 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1736 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1737 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1738 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1739 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1740 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1741 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1742 3d35a492 2019-12-13 stsp else
1743 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1744 3d35a492 2019-12-13 stsp goto done;
1745 3d35a492 2019-12-13 stsp }
1746 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1747 1338848f 2019-12-13 stsp goto done;
1748 a378724f 2019-02-10 stsp }
1749 a378724f 2019-02-10 stsp }
1750 6353ad76 2019-02-08 stsp
1751 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1752 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1753 1338848f 2019-12-13 stsp goto done;
1754 3f148bc6 2019-07-27 stsp }
1755 efdd40df 2019-07-27 stsp
1756 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1757 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1758 1338848f 2019-12-13 stsp goto done;
1759 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1760 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1761 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1762 1338848f 2019-12-13 stsp goto done;
1763 d00136be 2019-03-26 stsp }
1764 b8f41171 2019-02-10 stsp
1765 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1766 f179e66d 2020-07-23 stsp goto done;
1767 f179e66d 2020-07-23 stsp
1768 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1769 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1770 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1771 1338848f 2019-12-13 stsp goto done;
1772 f179e66d 2020-07-23 stsp }
1773 6353ad76 2019-02-08 stsp
1774 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1775 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1776 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1777 c363b2c1 2019-08-03 stsp else
1778 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1779 c363b2c1 2019-08-03 stsp
1780 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1781 6353ad76 2019-02-08 stsp if (err)
1782 1338848f 2019-12-13 stsp goto done;
1783 6353ad76 2019-02-08 stsp
1784 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1785 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1786 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1787 a919d5c4 2020-07-23 stsp goto done;
1788 a919d5c4 2020-07-23 stsp }
1789 a919d5c4 2020-07-23 stsp
1790 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1791 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1792 ab0d4361 2019-12-13 stsp if (fd == -1) {
1793 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1794 ab0d4361 2019-12-13 stsp goto done;
1795 ab0d4361 2019-12-13 stsp }
1796 3d35a492 2019-12-13 stsp }
1797 3d35a492 2019-12-13 stsp
1798 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1799 6353ad76 2019-02-08 stsp if (f == NULL) {
1800 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1801 6353ad76 2019-02-08 stsp goto done;
1802 6353ad76 2019-02-08 stsp }
1803 1338848f 2019-12-13 stsp fd = -1;
1804 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1805 656b1f76 2019-05-11 jcs for (;;) {
1806 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1807 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1808 6353ad76 2019-02-08 stsp if (err)
1809 7154f6ce 2019-03-27 stsp goto done;
1810 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1811 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1812 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1813 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1814 7154f6ce 2019-03-27 stsp goto done;
1815 80c5c120 2019-02-19 stsp }
1816 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1817 6353ad76 2019-02-08 stsp if (flen != 0)
1818 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1819 6353ad76 2019-02-08 stsp break;
1820 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1821 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1822 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1823 6353ad76 2019-02-08 stsp break;
1824 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1825 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1826 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1827 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1828 6353ad76 2019-02-08 stsp break;
1829 6353ad76 2019-02-08 stsp }
1830 6353ad76 2019-02-08 stsp } else {
1831 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1832 6353ad76 2019-02-08 stsp break;
1833 6353ad76 2019-02-08 stsp }
1834 6353ad76 2019-02-08 stsp hdrlen = 0;
1835 6353ad76 2019-02-08 stsp }
1836 7154f6ce 2019-03-27 stsp
1837 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1838 7154f6ce 2019-03-27 stsp rewind(f);
1839 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1840 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1841 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1842 6353ad76 2019-02-08 stsp done:
1843 6353ad76 2019-02-08 stsp if (blob)
1844 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1845 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1846 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1847 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1848 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1849 9d31a1d8 2018-03-11 stsp return err;
1850 e2b1e152 2019-07-13 stsp }
1851 e2b1e152 2019-07-13 stsp
1852 e2b1e152 2019-07-13 stsp /*
1853 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1854 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1855 e2b1e152 2019-07-13 stsp */
1856 e2b1e152 2019-07-13 stsp static const struct got_error *
1857 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1858 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1859 e2b1e152 2019-07-13 stsp {
1860 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1861 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1862 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1863 e2b1e152 2019-07-13 stsp
1864 e2b1e152 2019-07-13 stsp return NULL;
1865 9d31a1d8 2018-03-11 stsp }
1866 9d31a1d8 2018-03-11 stsp
1867 9d31a1d8 2018-03-11 stsp static const struct got_error *
1868 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1869 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1870 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1871 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1872 0584f854 2019-04-06 stsp void *progress_arg)
1873 9d31a1d8 2018-03-11 stsp {
1874 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1875 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1876 6353ad76 2019-02-08 stsp char *ondisk_path;
1877 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1878 b8f41171 2019-02-10 stsp struct stat sb;
1879 9d31a1d8 2018-03-11 stsp
1880 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1881 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1882 6353ad76 2019-02-08 stsp
1883 abb4604f 2019-07-27 stsp if (ie) {
1884 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1885 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1886 a76c42e6 2019-08-03 stsp goto done;
1887 a76c42e6 2019-08-03 stsp }
1888 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1889 7f91a133 2019-12-13 stsp repo);
1890 abb4604f 2019-07-27 stsp if (err)
1891 abb4604f 2019-07-27 stsp goto done;
1892 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1893 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1894 c90c8ce3 2020-07-23 stsp } else {
1895 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1896 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1897 c90c8ce3 2020-07-23 stsp }
1898 6353ad76 2019-02-08 stsp
1899 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1900 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1901 b8f41171 2019-02-10 stsp goto done;
1902 b8f41171 2019-02-10 stsp }
1903 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1904 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1905 5036ab18 2020-04-18 stsp path);
1906 5036ab18 2020-04-18 stsp goto done;
1907 5036ab18 2020-04-18 stsp }
1908 b8f41171 2019-02-10 stsp
1909 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1910 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1911 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1912 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1913 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1914 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1915 e2b1e152 2019-07-13 stsp if (err)
1916 e2b1e152 2019-07-13 stsp goto done;
1917 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1918 b8f41171 2019-02-10 stsp path);
1919 6353ad76 2019-02-08 stsp goto done;
1920 a378724f 2019-02-10 stsp }
1921 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1922 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1923 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1924 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1925 b8f41171 2019-02-10 stsp goto done;
1926 e2b1e152 2019-07-13 stsp }
1927 9d31a1d8 2018-03-11 stsp }
1928 9d31a1d8 2018-03-11 stsp
1929 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1930 8da9e5f4 2019-01-12 stsp if (err)
1931 6353ad76 2019-02-08 stsp goto done;
1932 512f0d0e 2019-01-02 stsp
1933 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1934 234035bc 2019-06-01 stsp int update_timestamps;
1935 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1936 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1937 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1938 234035bc 2019-06-01 stsp struct got_object_id id2;
1939 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1940 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1941 234035bc 2019-06-01 stsp if (err)
1942 f69721c3 2019-10-21 stsp goto done;
1943 f69721c3 2019-10-21 stsp }
1944 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1945 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1946 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1947 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1948 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1949 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1950 f69721c3 2019-10-21 stsp goto done;
1951 f69721c3 2019-10-21 stsp }
1952 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1953 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1954 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1955 234035bc 2019-06-01 stsp goto done;
1956 f69721c3 2019-10-21 stsp }
1957 234035bc 2019-06-01 stsp }
1958 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1959 36bf999c 2020-07-23 stsp char *link_target;
1960 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1961 36bf999c 2020-07-23 stsp if (err)
1962 36bf999c 2020-07-23 stsp goto done;
1963 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1964 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1965 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1966 36bf999c 2020-07-23 stsp free(link_target);
1967 993e2a1b 2020-07-23 stsp } else {
1968 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1969 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1970 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1971 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1972 993e2a1b 2020-07-23 stsp }
1973 f69721c3 2019-10-21 stsp free(label_orig);
1974 234035bc 2019-06-01 stsp if (blob2)
1975 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1976 909d120e 2019-09-22 stsp if (err)
1977 909d120e 2019-09-22 stsp goto done;
1978 234035bc 2019-06-01 stsp /*
1979 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1980 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1981 234035bc 2019-06-01 stsp * unmodified files again.
1982 234035bc 2019-06-01 stsp */
1983 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1984 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1985 234035bc 2019-06-01 stsp update_timestamps);
1986 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1987 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1988 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1989 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1990 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1991 1ee397ad 2019-07-12 stsp if (err)
1992 1ee397ad 2019-07-12 stsp goto done;
1993 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1994 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1995 13d9040b 2019-03-27 stsp if (err)
1996 13d9040b 2019-03-27 stsp goto done;
1997 13d9040b 2019-03-27 stsp } else {
1998 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
1999 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2000 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2001 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2002 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2003 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2004 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2005 2e1fa222 2020-07-23 stsp } else {
2006 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2007 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2008 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2009 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2010 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2011 2e1fa222 2020-07-23 stsp }
2012 13d9040b 2019-03-27 stsp if (err)
2013 13d9040b 2019-03-27 stsp goto done;
2014 65b05cec 2020-07-23 stsp
2015 054041d0 2020-06-24 stsp if (ie) {
2016 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2017 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
2018 054041d0 2020-06-24 stsp } else {
2019 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2020 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
2021 054041d0 2020-06-24 stsp &blob->id);
2022 054041d0 2020-06-24 stsp }
2023 13d9040b 2019-03-27 stsp if (err)
2024 13d9040b 2019-03-27 stsp goto done;
2025 65b05cec 2020-07-23 stsp
2026 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2027 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2028 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2029 2e1fa222 2020-07-23 stsp }
2030 13d9040b 2019-03-27 stsp }
2031 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2032 6353ad76 2019-02-08 stsp done:
2033 6353ad76 2019-02-08 stsp free(ondisk_path);
2034 8da9e5f4 2019-01-12 stsp return err;
2035 90285c3b 2019-01-08 stsp }
2036 90285c3b 2019-01-08 stsp
2037 90285c3b 2019-01-08 stsp static const struct got_error *
2038 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2039 90285c3b 2019-01-08 stsp {
2040 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2041 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2042 90285c3b 2019-01-08 stsp
2043 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2044 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2045 90285c3b 2019-01-08 stsp
2046 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2047 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2048 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2049 c97665ae 2019-01-13 stsp } else {
2050 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
2051 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
2052 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
2053 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2054 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2055 230a42bd 2019-05-11 jcs parent);
2056 90285c3b 2019-01-08 stsp break;
2057 90285c3b 2019-01-08 stsp }
2058 90285c3b 2019-01-08 stsp parent = dirname(parent);
2059 90285c3b 2019-01-08 stsp }
2060 90285c3b 2019-01-08 stsp }
2061 90285c3b 2019-01-08 stsp free(ondisk_path);
2062 90285c3b 2019-01-08 stsp return err;
2063 512f0d0e 2019-01-02 stsp }
2064 512f0d0e 2019-01-02 stsp
2065 708d8e67 2019-03-27 stsp static const struct got_error *
2066 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2067 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2068 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2069 708d8e67 2019-03-27 stsp {
2070 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2071 708d8e67 2019-03-27 stsp unsigned char status;
2072 708d8e67 2019-03-27 stsp struct stat sb;
2073 708d8e67 2019-03-27 stsp char *ondisk_path;
2074 708d8e67 2019-03-27 stsp
2075 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2076 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2077 a76c42e6 2019-08-03 stsp
2078 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2079 708d8e67 2019-03-27 stsp == -1)
2080 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2081 708d8e67 2019-03-27 stsp
2082 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2083 708d8e67 2019-03-27 stsp if (err)
2084 5a58a424 2020-06-23 stsp goto done;
2085 708d8e67 2019-03-27 stsp
2086 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2087 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2088 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2089 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2090 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2091 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2092 993e2a1b 2020-07-23 stsp goto done;
2093 993e2a1b 2020-07-23 stsp }
2094 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2095 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2096 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2097 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2098 993e2a1b 2020-07-23 stsp if (err)
2099 993e2a1b 2020-07-23 stsp goto done;
2100 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2101 993e2a1b 2020-07-23 stsp ie->path);
2102 993e2a1b 2020-07-23 stsp goto done;
2103 993e2a1b 2020-07-23 stsp }
2104 993e2a1b 2020-07-23 stsp
2105 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2106 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2107 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2108 1ee397ad 2019-07-12 stsp if (err)
2109 5a58a424 2020-06-23 stsp goto done;
2110 fc6346c4 2019-03-27 stsp /*
2111 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2112 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2113 fc6346c4 2019-03-27 stsp */
2114 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2115 fc6346c4 2019-03-27 stsp 0);
2116 fc6346c4 2019-03-27 stsp } else {
2117 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2118 1ee397ad 2019-07-12 stsp if (err)
2119 5a58a424 2020-06-23 stsp goto done;
2120 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2121 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2122 fc6346c4 2019-03-27 stsp if (err)
2123 5a58a424 2020-06-23 stsp goto done;
2124 fc6346c4 2019-03-27 stsp }
2125 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2126 708d8e67 2019-03-27 stsp }
2127 5a58a424 2020-06-23 stsp done:
2128 5a58a424 2020-06-23 stsp free(ondisk_path);
2129 fc6346c4 2019-03-27 stsp return err;
2130 708d8e67 2019-03-27 stsp }
2131 708d8e67 2019-03-27 stsp
2132 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2133 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2134 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2135 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2136 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2137 8da9e5f4 2019-01-12 stsp void *progress_arg;
2138 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2139 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2140 8da9e5f4 2019-01-12 stsp };
2141 8da9e5f4 2019-01-12 stsp
2142 512f0d0e 2019-01-02 stsp static const struct got_error *
2143 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2144 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2145 512f0d0e 2019-01-02 stsp {
2146 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2147 0584f854 2019-04-06 stsp
2148 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2149 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2150 512f0d0e 2019-01-02 stsp
2151 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2152 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2153 8da9e5f4 2019-01-12 stsp }
2154 512f0d0e 2019-01-02 stsp
2155 8da9e5f4 2019-01-12 stsp static const struct got_error *
2156 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2157 8da9e5f4 2019-01-12 stsp {
2158 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2159 7a9df742 2019-01-08 stsp
2160 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2161 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2162 0584f854 2019-04-06 stsp
2163 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2164 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2165 9d31a1d8 2018-03-11 stsp }
2166 9d31a1d8 2018-03-11 stsp
2167 9d31a1d8 2018-03-11 stsp static const struct got_error *
2168 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2169 9d31a1d8 2018-03-11 stsp {
2170 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2171 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2172 8da9e5f4 2019-01-12 stsp char *path;
2173 9d31a1d8 2018-03-11 stsp
2174 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2175 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2176 0584f854 2019-04-06 stsp
2177 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2178 63c5ca5d 2019-08-24 stsp return NULL;
2179 63c5ca5d 2019-08-24 stsp
2180 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2181 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2182 8da9e5f4 2019-01-12 stsp == -1)
2183 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2184 9d31a1d8 2018-03-11 stsp
2185 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2186 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2187 8da9e5f4 2019-01-12 stsp else
2188 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2189 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2190 9d31a1d8 2018-03-11 stsp
2191 8da9e5f4 2019-01-12 stsp free(path);
2192 0cd1c46a 2019-03-11 stsp return err;
2193 0cd1c46a 2019-03-11 stsp }
2194 0cd1c46a 2019-03-11 stsp
2195 b2118c49 2020-07-28 stsp const struct got_error *
2196 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2197 b2118c49 2020-07-28 stsp {
2198 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2199 b2118c49 2020-07-28 stsp
2200 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2201 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2202 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2203 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2204 b2118c49 2020-07-28 stsp }
2205 b2118c49 2020-07-28 stsp
2206 b2118c49 2020-07-28 stsp return NULL;
2207 b2118c49 2020-07-28 stsp }
2208 b2118c49 2020-07-28 stsp
2209 818c7501 2019-07-11 stsp static const struct got_error *
2210 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2211 0cd1c46a 2019-03-11 stsp {
2212 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2213 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2214 0cd1c46a 2019-03-11 stsp
2215 517bab73 2019-03-11 stsp *refname = NULL;
2216 517bab73 2019-03-11 stsp
2217 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2218 b2118c49 2020-07-28 stsp if (err)
2219 b2118c49 2020-07-28 stsp return err;
2220 0cd1c46a 2019-03-11 stsp
2221 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2222 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2223 0647c563 2019-03-11 stsp *refname = NULL;
2224 0cd1c46a 2019-03-11 stsp }
2225 517bab73 2019-03-11 stsp free(uuidstr);
2226 517bab73 2019-03-11 stsp return err;
2227 517bab73 2019-03-11 stsp }
2228 517bab73 2019-03-11 stsp
2229 818c7501 2019-07-11 stsp const struct got_error *
2230 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2231 818c7501 2019-07-11 stsp {
2232 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2233 818c7501 2019-07-11 stsp }
2234 818c7501 2019-07-11 stsp
2235 818c7501 2019-07-11 stsp static const struct got_error *
2236 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2237 818c7501 2019-07-11 stsp {
2238 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2239 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2240 818c7501 2019-07-11 stsp }
2241 818c7501 2019-07-11 stsp
2242 818c7501 2019-07-11 stsp static const struct got_error *
2243 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2244 818c7501 2019-07-11 stsp {
2245 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2246 818c7501 2019-07-11 stsp }
2247 818c7501 2019-07-11 stsp
2248 818c7501 2019-07-11 stsp static const struct got_error *
2249 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2250 818c7501 2019-07-11 stsp {
2251 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2252 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2253 818c7501 2019-07-11 stsp }
2254 818c7501 2019-07-11 stsp
2255 818c7501 2019-07-11 stsp static const struct got_error *
2256 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2257 818c7501 2019-07-11 stsp {
2258 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2259 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2260 0ebf8283 2019-07-24 stsp }
2261 0ebf8283 2019-07-24 stsp
2262 0ebf8283 2019-07-24 stsp static const struct got_error *
2263 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2264 0ebf8283 2019-07-24 stsp {
2265 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2266 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2267 0ebf8283 2019-07-24 stsp }
2268 0ebf8283 2019-07-24 stsp
2269 0ebf8283 2019-07-24 stsp static const struct got_error *
2270 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2271 0ebf8283 2019-07-24 stsp {
2272 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2273 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2274 0ebf8283 2019-07-24 stsp }
2275 0ebf8283 2019-07-24 stsp
2276 0ebf8283 2019-07-24 stsp static const struct got_error *
2277 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2278 0ebf8283 2019-07-24 stsp {
2279 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2280 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2281 818c7501 2019-07-11 stsp }
2282 818c7501 2019-07-11 stsp
2283 0ebf8283 2019-07-24 stsp static const struct got_error *
2284 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2285 0ebf8283 2019-07-24 stsp {
2286 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2287 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2288 0ebf8283 2019-07-24 stsp }
2289 818c7501 2019-07-11 stsp
2290 0ebf8283 2019-07-24 stsp const struct got_error *
2291 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2292 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2293 0ebf8283 2019-07-24 stsp {
2294 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2295 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2296 0ebf8283 2019-07-24 stsp *path = NULL;
2297 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2298 0ebf8283 2019-07-24 stsp }
2299 0ebf8283 2019-07-24 stsp return NULL;
2300 0ebf8283 2019-07-24 stsp }
2301 0ebf8283 2019-07-24 stsp
2302 517bab73 2019-03-11 stsp /*
2303 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2304 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2305 517bab73 2019-03-11 stsp */
2306 517bab73 2019-03-11 stsp static const struct got_error *
2307 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2308 517bab73 2019-03-11 stsp {
2309 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2310 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2311 517bab73 2019-03-11 stsp char *refname;
2312 517bab73 2019-03-11 stsp
2313 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2314 517bab73 2019-03-11 stsp if (err)
2315 517bab73 2019-03-11 stsp return err;
2316 0cd1c46a 2019-03-11 stsp
2317 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2318 0cd1c46a 2019-03-11 stsp if (err)
2319 0cd1c46a 2019-03-11 stsp goto done;
2320 0cd1c46a 2019-03-11 stsp
2321 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2322 0cd1c46a 2019-03-11 stsp done:
2323 0cd1c46a 2019-03-11 stsp free(refname);
2324 0cd1c46a 2019-03-11 stsp if (ref)
2325 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2326 3e3a69f1 2019-07-25 stsp return err;
2327 3e3a69f1 2019-07-25 stsp }
2328 3e3a69f1 2019-07-25 stsp
2329 3e3a69f1 2019-07-25 stsp static const struct got_error *
2330 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2331 3e3a69f1 2019-07-25 stsp {
2332 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2333 3e3a69f1 2019-07-25 stsp
2334 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2335 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2336 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2337 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2338 3e3a69f1 2019-07-25 stsp }
2339 9d31a1d8 2018-03-11 stsp return err;
2340 9d31a1d8 2018-03-11 stsp }
2341 9d31a1d8 2018-03-11 stsp
2342 3e3a69f1 2019-07-25 stsp
2343 ebf99748 2019-05-09 stsp static const struct got_error *
2344 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2345 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2346 ebf99748 2019-05-09 stsp {
2347 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2348 ebf99748 2019-05-09 stsp FILE *index = NULL;
2349 0cd1c46a 2019-03-11 stsp
2350 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2351 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2352 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2353 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2354 ebf99748 2019-05-09 stsp
2355 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2356 3e3a69f1 2019-07-25 stsp if (err)
2357 ebf99748 2019-05-09 stsp goto done;
2358 ebf99748 2019-05-09 stsp
2359 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2360 ebf99748 2019-05-09 stsp if (index == NULL) {
2361 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2362 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2363 ebf99748 2019-05-09 stsp } else {
2364 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2365 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2366 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2367 ebf99748 2019-05-09 stsp }
2368 ebf99748 2019-05-09 stsp done:
2369 ebf99748 2019-05-09 stsp if (err) {
2370 ebf99748 2019-05-09 stsp free(*fileindex_path);
2371 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2372 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2373 ebf99748 2019-05-09 stsp *fileindex = NULL;
2374 ebf99748 2019-05-09 stsp }
2375 ebf99748 2019-05-09 stsp return err;
2376 ebf99748 2019-05-09 stsp }
2377 c932eeeb 2019-05-22 stsp
2378 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2379 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2380 c932eeeb 2019-05-22 stsp const char *path;
2381 c932eeeb 2019-05-22 stsp size_t path_len;
2382 c932eeeb 2019-05-22 stsp const char *entry_name;
2383 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2384 a484d721 2019-06-10 stsp void *progress_arg;
2385 c932eeeb 2019-05-22 stsp };
2386 ebf99748 2019-05-09 stsp
2387 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2388 c932eeeb 2019-05-22 stsp static const struct got_error *
2389 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2390 c932eeeb 2019-05-22 stsp {
2391 1ee397ad 2019-07-12 stsp const struct got_error *err;
2392 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2393 c932eeeb 2019-05-22 stsp
2394 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2395 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2396 c932eeeb 2019-05-22 stsp return NULL;
2397 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2398 102ff934 2019-06-11 stsp return NULL;
2399 102ff934 2019-06-11 stsp
2400 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2401 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2402 c932eeeb 2019-05-22 stsp return NULL;
2403 c932eeeb 2019-05-22 stsp
2404 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2405 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2406 edd02c5e 2019-07-11 stsp ie->path);
2407 1ee397ad 2019-07-12 stsp if (err)
2408 1ee397ad 2019-07-12 stsp return err;
2409 1ee397ad 2019-07-12 stsp }
2410 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2411 c932eeeb 2019-05-22 stsp return NULL;
2412 9c6338c4 2019-06-02 stsp }
2413 9c6338c4 2019-06-02 stsp
2414 9c6338c4 2019-06-02 stsp static const struct got_error *
2415 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2416 9c6338c4 2019-06-02 stsp {
2417 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2418 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2419 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2420 867630bb 2020-01-17 stsp struct timespec timeout;
2421 9c6338c4 2019-06-02 stsp
2422 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2423 9c6338c4 2019-06-02 stsp fileindex_path);
2424 9c6338c4 2019-06-02 stsp if (err)
2425 9c6338c4 2019-06-02 stsp goto done;
2426 9c6338c4 2019-06-02 stsp
2427 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2428 9c6338c4 2019-06-02 stsp if (err)
2429 9c6338c4 2019-06-02 stsp goto done;
2430 9c6338c4 2019-06-02 stsp
2431 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2432 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2433 9c6338c4 2019-06-02 stsp fileindex_path);
2434 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2435 9c6338c4 2019-06-02 stsp }
2436 867630bb 2020-01-17 stsp
2437 867630bb 2020-01-17 stsp /*
2438 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2439 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2440 867630bb 2020-01-17 stsp * was recorded in the file index.
2441 867630bb 2020-01-17 stsp */
2442 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2443 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2444 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2445 9c6338c4 2019-06-02 stsp done:
2446 9c6338c4 2019-06-02 stsp if (new_index)
2447 9c6338c4 2019-06-02 stsp fclose(new_index);
2448 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2449 9c6338c4 2019-06-02 stsp return err;
2450 c932eeeb 2019-05-22 stsp }
2451 6ced4176 2019-07-12 stsp
2452 6ced4176 2019-07-12 stsp static const struct got_error *
2453 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2454 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2455 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2456 6ced4176 2019-07-12 stsp {
2457 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2458 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2459 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2460 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2461 6ced4176 2019-07-12 stsp
2462 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2463 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2464 6ced4176 2019-07-12 stsp *tree_id = NULL;
2465 6ced4176 2019-07-12 stsp
2466 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2467 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2468 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2469 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2470 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2471 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2472 6ced4176 2019-07-12 stsp goto done;
2473 6ced4176 2019-07-12 stsp }
2474 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2475 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2476 6ced4176 2019-07-12 stsp if (err)
2477 6ced4176 2019-07-12 stsp goto done;
2478 6ced4176 2019-07-12 stsp return NULL;
2479 6ced4176 2019-07-12 stsp }
2480 6ced4176 2019-07-12 stsp
2481 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2482 6ced4176 2019-07-12 stsp
2483 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2484 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2485 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2486 6ced4176 2019-07-12 stsp goto done;
2487 6ced4176 2019-07-12 stsp }
2488 6ced4176 2019-07-12 stsp
2489 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2490 6ced4176 2019-07-12 stsp in_repo_path);
2491 6ced4176 2019-07-12 stsp if (err)
2492 6ced4176 2019-07-12 stsp goto done;
2493 6ced4176 2019-07-12 stsp
2494 6ced4176 2019-07-12 stsp free(in_repo_path);
2495 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2496 6ced4176 2019-07-12 stsp
2497 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2498 6ced4176 2019-07-12 stsp if (err)
2499 6ced4176 2019-07-12 stsp goto done;
2500 c932eeeb 2019-05-22 stsp
2501 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2502 6ced4176 2019-07-12 stsp /* Check out a single file. */
2503 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2504 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2505 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2506 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2507 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2508 6ced4176 2019-07-12 stsp goto done;
2509 6ced4176 2019-07-12 stsp }
2510 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2511 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2512 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2513 6ced4176 2019-07-12 stsp goto done;
2514 6ced4176 2019-07-12 stsp }
2515 6ced4176 2019-07-12 stsp } else {
2516 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2517 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2518 6ced4176 2019-07-12 stsp if (err)
2519 6ced4176 2019-07-12 stsp return err;
2520 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2521 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2522 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2523 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2524 6ced4176 2019-07-12 stsp goto done;
2525 6ced4176 2019-07-12 stsp }
2526 6ced4176 2019-07-12 stsp }
2527 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2528 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2529 6ced4176 2019-07-12 stsp } else {
2530 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2531 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2532 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2533 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2534 6ced4176 2019-07-12 stsp goto done;
2535 6ced4176 2019-07-12 stsp }
2536 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2537 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2538 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2539 6ced4176 2019-07-12 stsp goto done;
2540 6ced4176 2019-07-12 stsp }
2541 6ced4176 2019-07-12 stsp }
2542 6ced4176 2019-07-12 stsp done:
2543 6ced4176 2019-07-12 stsp free(id);
2544 6ced4176 2019-07-12 stsp free(in_repo_path);
2545 6ced4176 2019-07-12 stsp if (err) {
2546 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2547 6ced4176 2019-07-12 stsp free(*tree_relpath);
2548 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2549 6ced4176 2019-07-12 stsp free(*tree_id);
2550 6ced4176 2019-07-12 stsp *tree_id = NULL;
2551 6ced4176 2019-07-12 stsp }
2552 07ed472d 2019-07-12 stsp return err;
2553 07ed472d 2019-07-12 stsp }
2554 07ed472d 2019-07-12 stsp
2555 07ed472d 2019-07-12 stsp static const struct got_error *
2556 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2557 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2558 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2559 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2560 07ed472d 2019-07-12 stsp {
2561 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2562 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2563 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2564 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2565 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2566 07ed472d 2019-07-12 stsp
2567 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2568 7f47418f 2019-12-20 stsp if (err) {
2569 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2570 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2571 7f47418f 2019-12-20 stsp goto done;
2572 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2573 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2574 7f47418f 2019-12-20 stsp if (err)
2575 7f47418f 2019-12-20 stsp return err;
2576 7f47418f 2019-12-20 stsp }
2577 07ed472d 2019-07-12 stsp
2578 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2579 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2580 07ed472d 2019-07-12 stsp if (err)
2581 07ed472d 2019-07-12 stsp goto done;
2582 07ed472d 2019-07-12 stsp
2583 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2584 07ed472d 2019-07-12 stsp if (err)
2585 07ed472d 2019-07-12 stsp goto done;
2586 07ed472d 2019-07-12 stsp
2587 07ed472d 2019-07-12 stsp if (entry_name &&
2588 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2589 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2590 07ed472d 2019-07-12 stsp goto done;
2591 07ed472d 2019-07-12 stsp }
2592 07ed472d 2019-07-12 stsp
2593 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2594 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2595 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2596 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2597 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2598 07ed472d 2019-07-12 stsp arg.repo = repo;
2599 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2600 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2601 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2602 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2603 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2604 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2605 07ed472d 2019-07-12 stsp done:
2606 07ed472d 2019-07-12 stsp if (tree)
2607 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2608 07ed472d 2019-07-12 stsp if (commit)
2609 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2610 6ced4176 2019-07-12 stsp return err;
2611 6ced4176 2019-07-12 stsp }
2612 6ced4176 2019-07-12 stsp
2613 9d31a1d8 2018-03-11 stsp const struct got_error *
2614 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2615 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2616 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2617 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2618 9d31a1d8 2018-03-11 stsp {
2619 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2620 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2621 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2622 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2623 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2624 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2625 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2626 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2627 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2628 f2ea84fa 2019-07-27 stsp int entry_type;
2629 f2ea84fa 2019-07-27 stsp char *relpath;
2630 f2ea84fa 2019-07-27 stsp char *entry_name;
2631 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2632 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2633 9d31a1d8 2018-03-11 stsp
2634 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2635 f2ea84fa 2019-07-27 stsp
2636 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2637 9d31a1d8 2018-03-11 stsp if (err)
2638 9d31a1d8 2018-03-11 stsp return err;
2639 93a30277 2018-12-24 stsp
2640 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2641 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2642 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2643 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2644 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2645 f2ea84fa 2019-07-27 stsp goto done;
2646 f2ea84fa 2019-07-27 stsp }
2647 6ced4176 2019-07-12 stsp
2648 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2649 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2650 f2ea84fa 2019-07-27 stsp if (err) {
2651 f2ea84fa 2019-07-27 stsp free(tpd);
2652 6ced4176 2019-07-12 stsp goto done;
2653 6ced4176 2019-07-12 stsp }
2654 f2ea84fa 2019-07-27 stsp
2655 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2656 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2657 f2ea84fa 2019-07-27 stsp if (err) {
2658 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2659 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2660 f2ea84fa 2019-07-27 stsp free(tpd);
2661 f2ea84fa 2019-07-27 stsp goto done;
2662 f2ea84fa 2019-07-27 stsp }
2663 f2ea84fa 2019-07-27 stsp } else
2664 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2665 f2ea84fa 2019-07-27 stsp
2666 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2667 6ced4176 2019-07-12 stsp }
2668 6ced4176 2019-07-12 stsp
2669 51514078 2018-12-25 stsp /*
2670 51514078 2018-12-25 stsp * Read the file index.
2671 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2672 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2673 51514078 2018-12-25 stsp */
2674 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2675 8da9e5f4 2019-01-12 stsp if (err)
2676 c4cdcb68 2019-04-03 stsp goto done;
2677 c4cdcb68 2019-04-03 stsp
2678 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2679 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2680 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2681 f2ea84fa 2019-07-27 stsp
2682 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2683 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2684 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2685 f2ea84fa 2019-07-27 stsp if (err)
2686 f2ea84fa 2019-07-27 stsp break;
2687 f2ea84fa 2019-07-27 stsp
2688 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2689 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2690 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2691 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2692 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2693 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2694 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2695 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2696 f2ea84fa 2019-07-27 stsp if (err)
2697 f2ea84fa 2019-07-27 stsp break;
2698 f2ea84fa 2019-07-27 stsp
2699 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2700 711d9cd9 2019-07-12 stsp }
2701 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2702 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2703 af12c6b9 2019-06-04 stsp err = sync_err;
2704 9d31a1d8 2018-03-11 stsp done:
2705 9c6338c4 2019-06-02 stsp free(fileindex_path);
2706 edfa7d7f 2018-09-11 stsp if (tree)
2707 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2708 9d31a1d8 2018-03-11 stsp if (commit)
2709 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2710 5ade8233 2019-07-12 stsp if (fileindex)
2711 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2712 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2713 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2714 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2715 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2716 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2717 f2ea84fa 2019-07-27 stsp free(tpd);
2718 f2ea84fa 2019-07-27 stsp }
2719 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2720 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2721 9d31a1d8 2018-03-11 stsp err = unlockerr;
2722 f8d1f275 2019-02-04 stsp return err;
2723 f8d1f275 2019-02-04 stsp }
2724 f8d1f275 2019-02-04 stsp
2725 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2726 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2727 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2728 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2729 234035bc 2019-06-01 stsp void *progress_arg;
2730 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2731 234035bc 2019-06-01 stsp void *cancel_arg;
2732 f69721c3 2019-10-21 stsp const char *label_orig;
2733 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2734 234035bc 2019-06-01 stsp };
2735 234035bc 2019-06-01 stsp
2736 234035bc 2019-06-01 stsp static const struct got_error *
2737 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2738 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2739 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2740 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2741 234035bc 2019-06-01 stsp {
2742 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2743 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2744 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2745 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2746 234035bc 2019-06-01 stsp struct stat sb;
2747 234035bc 2019-06-01 stsp unsigned char status;
2748 234035bc 2019-06-01 stsp int local_changes_subsumed;
2749 234035bc 2019-06-01 stsp
2750 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2751 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2752 d6c87207 2019-08-02 stsp strlen(path2));
2753 1ee397ad 2019-07-12 stsp if (ie == NULL)
2754 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2755 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2756 234035bc 2019-06-01 stsp
2757 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2758 234035bc 2019-06-01 stsp path2) == -1)
2759 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2760 234035bc 2019-06-01 stsp
2761 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2762 7f91a133 2019-12-13 stsp repo);
2763 234035bc 2019-06-01 stsp if (err)
2764 234035bc 2019-06-01 stsp goto done;
2765 234035bc 2019-06-01 stsp
2766 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2767 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2768 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2769 234035bc 2019-06-01 stsp goto done;
2770 234035bc 2019-06-01 stsp }
2771 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2772 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2773 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2774 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2775 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2776 234035bc 2019-06-01 stsp goto done;
2777 234035bc 2019-06-01 stsp }
2778 234035bc 2019-06-01 stsp
2779 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2780 36bf999c 2020-07-23 stsp char *link_target2;
2781 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2782 36bf999c 2020-07-23 stsp if (err)
2783 36bf999c 2020-07-23 stsp goto done;
2784 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2785 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2786 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2787 36bf999c 2020-07-23 stsp free(link_target2);
2788 af57b12a 2020-07-23 stsp } else {
2789 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2790 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2791 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2792 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2793 af57b12a 2020-07-23 stsp }
2794 234035bc 2019-06-01 stsp } else if (blob1) {
2795 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2796 d6c87207 2019-08-02 stsp strlen(path1));
2797 1ee397ad 2019-07-12 stsp if (ie == NULL)
2798 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2799 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2800 2b92fad7 2019-06-02 stsp
2801 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2802 2b92fad7 2019-06-02 stsp path1) == -1)
2803 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2804 2b92fad7 2019-06-02 stsp
2805 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2806 7f91a133 2019-12-13 stsp repo);
2807 2b92fad7 2019-06-02 stsp if (err)
2808 2b92fad7 2019-06-02 stsp goto done;
2809 2b92fad7 2019-06-02 stsp
2810 2b92fad7 2019-06-02 stsp switch (status) {
2811 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2812 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2813 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2814 1ee397ad 2019-07-12 stsp if (err)
2815 1ee397ad 2019-07-12 stsp goto done;
2816 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2817 2b92fad7 2019-06-02 stsp if (err)
2818 2b92fad7 2019-06-02 stsp goto done;
2819 2b92fad7 2019-06-02 stsp if (ie)
2820 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2821 2b92fad7 2019-06-02 stsp break;
2822 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2823 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2824 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2825 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2826 1ee397ad 2019-07-12 stsp if (err)
2827 1ee397ad 2019-07-12 stsp goto done;
2828 2b92fad7 2019-06-02 stsp if (ie)
2829 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2830 2b92fad7 2019-06-02 stsp break;
2831 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2832 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2833 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2834 0a22ca1a 2020-09-23 stsp /*
2835 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2836 0a22ca1a 2020-09-23 stsp * exists in the repository.
2837 0a22ca1a 2020-09-23 stsp */
2838 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2839 0a22ca1a 2020-09-23 stsp if (err)
2840 0a22ca1a 2020-09-23 stsp goto done;
2841 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2842 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2843 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2844 0a22ca1a 2020-09-23 stsp if (err)
2845 0a22ca1a 2020-09-23 stsp goto done;
2846 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2847 0a22ca1a 2020-09-23 stsp path1);
2848 0a22ca1a 2020-09-23 stsp if (err)
2849 0a22ca1a 2020-09-23 stsp goto done;
2850 0a22ca1a 2020-09-23 stsp if (ie)
2851 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2852 0a22ca1a 2020-09-23 stsp ie);
2853 0a22ca1a 2020-09-23 stsp } else {
2854 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2855 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2856 0a22ca1a 2020-09-23 stsp }
2857 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2858 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2859 0a22ca1a 2020-09-23 stsp free(id);
2860 0a22ca1a 2020-09-23 stsp if (err)
2861 0a22ca1a 2020-09-23 stsp goto done;
2862 0a22ca1a 2020-09-23 stsp break;
2863 0a22ca1a 2020-09-23 stsp }
2864 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2865 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2866 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2867 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2868 1ee397ad 2019-07-12 stsp if (err)
2869 1ee397ad 2019-07-12 stsp goto done;
2870 2b92fad7 2019-06-02 stsp break;
2871 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2872 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2873 1ee397ad 2019-07-12 stsp if (err)
2874 1ee397ad 2019-07-12 stsp goto done;
2875 2b92fad7 2019-06-02 stsp break;
2876 2b92fad7 2019-06-02 stsp default:
2877 2b92fad7 2019-06-02 stsp break;
2878 2b92fad7 2019-06-02 stsp }
2879 234035bc 2019-06-01 stsp } else if (blob2) {
2880 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2881 234035bc 2019-06-01 stsp path2) == -1)
2882 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2883 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2884 d6c87207 2019-08-02 stsp strlen(path2));
2885 234035bc 2019-06-01 stsp if (ie) {
2886 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2887 7f91a133 2019-12-13 stsp -1, NULL, repo);
2888 234035bc 2019-06-01 stsp if (err)
2889 234035bc 2019-06-01 stsp goto done;
2890 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2891 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2892 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2893 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2894 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2895 1ee397ad 2019-07-12 stsp status, path2);
2896 234035bc 2019-06-01 stsp goto done;
2897 234035bc 2019-06-01 stsp }
2898 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2899 36bf999c 2020-07-23 stsp char *link_target2;
2900 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2901 36bf999c 2020-07-23 stsp blob2);
2902 36bf999c 2020-07-23 stsp if (err)
2903 36bf999c 2020-07-23 stsp goto done;
2904 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2905 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2906 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2907 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2908 36bf999c 2020-07-23 stsp free(link_target2);
2909 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2910 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2911 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2912 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2913 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2914 526a746f 2020-07-23 stsp a->progress_arg);
2915 dfe9fba0 2020-07-23 stsp } else {
2916 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2917 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2918 526a746f 2020-07-23 stsp }
2919 dfe9fba0 2020-07-23 stsp if (err)
2920 dfe9fba0 2020-07-23 stsp goto done;
2921 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2922 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2923 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2924 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2925 234035bc 2019-06-01 stsp if (err)
2926 234035bc 2019-06-01 stsp goto done;
2927 234035bc 2019-06-01 stsp }
2928 234035bc 2019-06-01 stsp } else {
2929 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2930 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2931 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2932 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2933 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2934 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2935 2e1fa222 2020-07-23 stsp } else {
2936 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2937 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2938 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2939 2e1fa222 2020-07-23 stsp }
2940 234035bc 2019-06-01 stsp if (err)
2941 234035bc 2019-06-01 stsp goto done;
2942 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2943 234035bc 2019-06-01 stsp if (err)
2944 234035bc 2019-06-01 stsp goto done;
2945 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2946 3969253a 2020-03-07 stsp NULL, NULL, 1);
2947 3969253a 2020-03-07 stsp if (err) {
2948 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2949 3969253a 2020-03-07 stsp goto done;
2950 3969253a 2020-03-07 stsp }
2951 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2952 2b92fad7 2019-06-02 stsp if (err) {
2953 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2954 2b92fad7 2019-06-02 stsp goto done;
2955 2b92fad7 2019-06-02 stsp }
2956 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2957 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2958 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2959 2e1fa222 2020-07-23 stsp }
2960 234035bc 2019-06-01 stsp }
2961 234035bc 2019-06-01 stsp }
2962 234035bc 2019-06-01 stsp done:
2963 234035bc 2019-06-01 stsp free(ondisk_path);
2964 234035bc 2019-06-01 stsp return err;
2965 234035bc 2019-06-01 stsp }
2966 234035bc 2019-06-01 stsp
2967 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2968 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2969 234035bc 2019-06-01 stsp struct got_repository *repo;
2970 234035bc 2019-06-01 stsp };
2971 234035bc 2019-06-01 stsp
2972 234035bc 2019-06-01 stsp static const struct got_error *
2973 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2974 234035bc 2019-06-01 stsp {
2975 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2976 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2977 234035bc 2019-06-01 stsp unsigned char status;
2978 234035bc 2019-06-01 stsp struct stat sb;
2979 234035bc 2019-06-01 stsp char *ondisk_path;
2980 234035bc 2019-06-01 stsp
2981 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2982 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2983 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2984 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2985 234035bc 2019-06-01 stsp
2986 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2987 234035bc 2019-06-01 stsp == -1)
2988 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2989 234035bc 2019-06-01 stsp
2990 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2991 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2992 234035bc 2019-06-01 stsp if (err)
2993 234035bc 2019-06-01 stsp return err;
2994 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2995 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2996 234035bc 2019-06-01 stsp
2997 234035bc 2019-06-01 stsp return NULL;
2998 234035bc 2019-06-01 stsp }
2999 234035bc 2019-06-01 stsp
3000 818c7501 2019-07-11 stsp static const struct got_error *
3001 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3002 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3003 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3004 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3005 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3006 234035bc 2019-06-01 stsp {
3007 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3008 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3009 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3010 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3011 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3012 234035bc 2019-06-01 stsp
3013 03415a1a 2019-06-02 stsp if (commit_id1) {
3014 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3015 03415a1a 2019-06-02 stsp worktree->path_prefix);
3016 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3017 03415a1a 2019-06-02 stsp goto done;
3018 69d57f3d 2020-07-31 stsp }
3019 69d57f3d 2020-07-31 stsp if (tree_id1) {
3020 69d57f3d 2020-07-31 stsp char *id_str;
3021 03415a1a 2019-06-02 stsp
3022 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3023 03415a1a 2019-06-02 stsp if (err)
3024 03415a1a 2019-06-02 stsp goto done;
3025 f69721c3 2019-10-21 stsp
3026 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3027 f69721c3 2019-10-21 stsp if (err)
3028 f69721c3 2019-10-21 stsp goto done;
3029 f69721c3 2019-10-21 stsp
3030 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3031 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3032 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3033 f69721c3 2019-10-21 stsp free(id_str);
3034 f69721c3 2019-10-21 stsp goto done;
3035 f69721c3 2019-10-21 stsp }
3036 f69721c3 2019-10-21 stsp free(id_str);
3037 03415a1a 2019-06-02 stsp }
3038 234035bc 2019-06-01 stsp
3039 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3040 234035bc 2019-06-01 stsp worktree->path_prefix);
3041 234035bc 2019-06-01 stsp if (err)
3042 234035bc 2019-06-01 stsp goto done;
3043 234035bc 2019-06-01 stsp
3044 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3045 234035bc 2019-06-01 stsp if (err)
3046 234035bc 2019-06-01 stsp goto done;
3047 234035bc 2019-06-01 stsp
3048 234035bc 2019-06-01 stsp arg.worktree = worktree;
3049 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3050 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3051 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3052 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3053 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3054 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3055 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3056 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3057 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3058 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3059 af12c6b9 2019-06-04 stsp err = sync_err;
3060 234035bc 2019-06-01 stsp done:
3061 234035bc 2019-06-01 stsp if (tree1)
3062 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3063 234035bc 2019-06-01 stsp if (tree2)
3064 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3065 f69721c3 2019-10-21 stsp free(label_orig);
3066 818c7501 2019-07-11 stsp return err;
3067 818c7501 2019-07-11 stsp }
3068 234035bc 2019-06-01 stsp
3069 818c7501 2019-07-11 stsp const struct got_error *
3070 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3071 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3072 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3073 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3074 818c7501 2019-07-11 stsp {
3075 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3076 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3077 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3078 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3079 818c7501 2019-07-11 stsp
3080 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3081 818c7501 2019-07-11 stsp if (err)
3082 818c7501 2019-07-11 stsp return err;
3083 818c7501 2019-07-11 stsp
3084 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3085 818c7501 2019-07-11 stsp if (err)
3086 818c7501 2019-07-11 stsp goto done;
3087 818c7501 2019-07-11 stsp
3088 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3089 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3090 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3091 818c7501 2019-07-11 stsp &mok_arg);
3092 818c7501 2019-07-11 stsp if (err)
3093 818c7501 2019-07-11 stsp goto done;
3094 818c7501 2019-07-11 stsp
3095 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3096 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3097 818c7501 2019-07-11 stsp done:
3098 818c7501 2019-07-11 stsp if (fileindex)
3099 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3100 818c7501 2019-07-11 stsp free(fileindex_path);
3101 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3102 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3103 234035bc 2019-06-01 stsp err = unlockerr;
3104 234035bc 2019-06-01 stsp return err;
3105 234035bc 2019-06-01 stsp }
3106 234035bc 2019-06-01 stsp
3107 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3108 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3109 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3110 927df6b7 2019-02-10 stsp const char *status_path;
3111 927df6b7 2019-02-10 stsp size_t status_path_len;
3112 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3113 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3114 f8d1f275 2019-02-04 stsp void *status_arg;
3115 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3116 f8d1f275 2019-02-04 stsp void *cancel_arg;
3117 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3118 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3119 f2a9dc41 2019-12-13 tracey int report_unchanged;
3120 3143d852 2020-06-25 stsp int no_ignores;
3121 f8d1f275 2019-02-04 stsp };
3122 88d0e355 2019-08-03 stsp
3123 f8d1f275 2019-02-04 stsp static const struct got_error *
3124 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3125 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3126 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3127 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3128 927df6b7 2019-02-10 stsp {
3129 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3130 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3131 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3132 927df6b7 2019-02-10 stsp struct stat sb;
3133 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3134 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3135 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3136 927df6b7 2019-02-10 stsp
3137 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3138 98eaaa12 2019-08-03 stsp if (err)
3139 98eaaa12 2019-08-03 stsp return err;
3140 98eaaa12 2019-08-03 stsp
3141 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3142 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3143 98eaaa12 2019-08-03 stsp return NULL;
3144 98eaaa12 2019-08-03 stsp
3145 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3146 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3147 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3148 98eaaa12 2019-08-03 stsp }
3149 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3150 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3151 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3152 927df6b7 2019-02-10 stsp }
3153 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3154 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3155 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3156 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3157 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3158 98eaaa12 2019-08-03 stsp }
3159 98eaaa12 2019-08-03 stsp
3160 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3161 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3162 927df6b7 2019-02-10 stsp }
3163 927df6b7 2019-02-10 stsp
3164 927df6b7 2019-02-10 stsp static const struct got_error *
3165 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3166 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3167 f8d1f275 2019-02-04 stsp {
3168 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3169 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3170 f8d1f275 2019-02-04 stsp char *abspath;
3171 f8d1f275 2019-02-04 stsp
3172 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3173 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3174 0584f854 2019-04-06 stsp
3175 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3176 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3177 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3178 927df6b7 2019-02-10 stsp return NULL;
3179 927df6b7 2019-02-10 stsp
3180 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3181 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3182 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3183 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3184 f8d1f275 2019-02-04 stsp } else {
3185 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3186 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3187 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3188 f8d1f275 2019-02-04 stsp }
3189 f8d1f275 2019-02-04 stsp
3190 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3191 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3192 f8d1f275 2019-02-04 stsp free(abspath);
3193 9d31a1d8 2018-03-11 stsp return err;
3194 9d31a1d8 2018-03-11 stsp }
3195 f8d1f275 2019-02-04 stsp
3196 f8d1f275 2019-02-04 stsp static const struct got_error *
3197 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3198 f8d1f275 2019-02-04 stsp {
3199 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3200 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3201 2ec1f75b 2019-03-26 stsp unsigned char status;
3202 927df6b7 2019-02-10 stsp
3203 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3204 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3205 0584f854 2019-04-06 stsp
3206 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3207 927df6b7 2019-02-10 stsp return NULL;
3208 927df6b7 2019-02-10 stsp
3209 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3210 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3211 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3212 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3213 2ec1f75b 2019-03-26 stsp else
3214 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3215 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3216 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3217 6841da00 2019-08-08 stsp }
3218 6841da00 2019-08-08 stsp
3219 6841da00 2019-08-08 stsp void
3220 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3221 6841da00 2019-08-08 stsp {
3222 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3223 6841da00 2019-08-08 stsp
3224 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3225 6841da00 2019-08-08 stsp free((char *)pe->path);
3226 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3227 6841da00 2019-08-08 stsp }
3228 6841da00 2019-08-08 stsp
3229 6841da00 2019-08-08 stsp void
3230 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3231 6841da00 2019-08-08 stsp {
3232 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3233 6841da00 2019-08-08 stsp
3234 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3235 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3236 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3237 6841da00 2019-08-08 stsp free((char *)pe->path);
3238 6841da00 2019-08-08 stsp }
3239 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3240 6841da00 2019-08-08 stsp }
3241 6841da00 2019-08-08 stsp
3242 6841da00 2019-08-08 stsp static const struct got_error *
3243 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3244 6841da00 2019-08-08 stsp {
3245 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3246 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3247 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3248 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3249 6841da00 2019-08-08 stsp size_t linesize = 0;
3250 6841da00 2019-08-08 stsp ssize_t linelen;
3251 6841da00 2019-08-08 stsp
3252 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3253 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3254 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3255 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3256 6841da00 2019-08-08 stsp
3257 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3258 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3259 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3260 bd8de430 2019-10-04 stsp
3261 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3262 bd8de430 2019-10-04 stsp if (line[0] == '#')
3263 bd8de430 2019-10-04 stsp continue;
3264 bd8de430 2019-10-04 stsp
3265 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3266 bd8de430 2019-10-04 stsp if (line[0] == '!')
3267 bd8de430 2019-10-04 stsp continue;
3268 bd8de430 2019-10-04 stsp
3269 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3270 6841da00 2019-08-08 stsp line) == -1) {
3271 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3272 6841da00 2019-08-08 stsp goto done;
3273 6841da00 2019-08-08 stsp }
3274 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3275 6841da00 2019-08-08 stsp if (err)
3276 6841da00 2019-08-08 stsp goto done;
3277 6841da00 2019-08-08 stsp }
3278 6841da00 2019-08-08 stsp if (ferror(f)) {
3279 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3280 6841da00 2019-08-08 stsp goto done;
3281 6841da00 2019-08-08 stsp }
3282 6841da00 2019-08-08 stsp
3283 6841da00 2019-08-08 stsp dirpath = strdup(path);
3284 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3285 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3286 6841da00 2019-08-08 stsp goto done;
3287 6841da00 2019-08-08 stsp }
3288 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3289 6841da00 2019-08-08 stsp done:
3290 6841da00 2019-08-08 stsp free(line);
3291 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3292 6841da00 2019-08-08 stsp free(dirpath);
3293 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3294 6841da00 2019-08-08 stsp }
3295 6841da00 2019-08-08 stsp return err;
3296 6841da00 2019-08-08 stsp }
3297 6841da00 2019-08-08 stsp
3298 6841da00 2019-08-08 stsp int
3299 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3300 6841da00 2019-08-08 stsp {
3301 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3302 bd8de430 2019-10-04 stsp
3303 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3304 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3305 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3306 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3307 bd8de430 2019-10-04 stsp
3308 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3309 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3310 6841da00 2019-08-08 stsp
3311 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3312 bd8de430 2019-10-04 stsp continue;
3313 bd8de430 2019-10-04 stsp pattern += 3;
3314 bd8de430 2019-10-04 stsp p = path;
3315 bd8de430 2019-10-04 stsp while (*p) {
3316 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3317 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3318 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3319 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3320 bd8de430 2019-10-04 stsp p++;
3321 bd8de430 2019-10-04 stsp while (*p == '/')
3322 bd8de430 2019-10-04 stsp p++;
3323 bd8de430 2019-10-04 stsp continue;
3324 bd8de430 2019-10-04 stsp }
3325 bd8de430 2019-10-04 stsp return 1;
3326 bd8de430 2019-10-04 stsp }
3327 bd8de430 2019-10-04 stsp }
3328 bd8de430 2019-10-04 stsp }
3329 bd8de430 2019-10-04 stsp
3330 6841da00 2019-08-08 stsp /*
3331 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3332 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3333 6841da00 2019-08-08 stsp * ignores backwards.
3334 6841da00 2019-08-08 stsp */
3335 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3336 6841da00 2019-08-08 stsp while (pe) {
3337 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3338 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3339 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3340 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3341 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3342 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3343 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3344 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3345 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3346 6841da00 2019-08-08 stsp continue;
3347 6841da00 2019-08-08 stsp return 1;
3348 6841da00 2019-08-08 stsp }
3349 6841da00 2019-08-08 stsp }
3350 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3351 6841da00 2019-08-08 stsp }
3352 6841da00 2019-08-08 stsp
3353 6841da00 2019-08-08 stsp return 0;
3354 6841da00 2019-08-08 stsp }
3355 6841da00 2019-08-08 stsp
3356 6841da00 2019-08-08 stsp static const struct got_error *
3357 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3358 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3359 6841da00 2019-08-08 stsp {
3360 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3361 6841da00 2019-08-08 stsp char *ignorespath;
3362 886cec17 2019-12-15 stsp int fd = -1;
3363 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3364 6841da00 2019-08-08 stsp
3365 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3366 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3367 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3368 6841da00 2019-08-08 stsp
3369 886cec17 2019-12-15 stsp if (dirfd != -1) {
3370 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3371 886cec17 2019-12-15 stsp if (fd == -1) {
3372 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3373 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3374 886cec17 2019-12-15 stsp ignorespath);
3375 886cec17 2019-12-15 stsp } else {
3376 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3377 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3378 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3379 886cec17 2019-12-15 stsp ignorespath);
3380 886cec17 2019-12-15 stsp else {
3381 886cec17 2019-12-15 stsp fd = -1;
3382 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3383 886cec17 2019-12-15 stsp }
3384 886cec17 2019-12-15 stsp }
3385 886cec17 2019-12-15 stsp } else {
3386 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3387 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3388 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3389 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3390 886cec17 2019-12-15 stsp ignorespath);
3391 886cec17 2019-12-15 stsp } else
3392 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3393 886cec17 2019-12-15 stsp }
3394 6841da00 2019-08-08 stsp
3395 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3396 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3397 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3398 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3399 6841da00 2019-08-08 stsp free(ignorespath);
3400 6841da00 2019-08-08 stsp return err;
3401 f8d1f275 2019-02-04 stsp }
3402 f8d1f275 2019-02-04 stsp
3403 f8d1f275 2019-02-04 stsp static const struct got_error *
3404 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3405 f8d1f275 2019-02-04 stsp {
3406 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3407 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3408 f8d1f275 2019-02-04 stsp char *path = NULL;
3409 f8d1f275 2019-02-04 stsp
3410 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3411 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3412 0584f854 2019-04-06 stsp
3413 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3414 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3415 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3416 f8d1f275 2019-02-04 stsp } else {
3417 f8d1f275 2019-02-04 stsp path = de->d_name;
3418 f8d1f275 2019-02-04 stsp }
3419 f8d1f275 2019-02-04 stsp
3420 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3421 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3422 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3423 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3424 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3425 f8d1f275 2019-02-04 stsp if (parent_path[0])
3426 f8d1f275 2019-02-04 stsp free(path);
3427 b72f483a 2019-02-05 stsp return err;
3428 f8d1f275 2019-02-04 stsp }
3429 f8d1f275 2019-02-04 stsp
3430 347d1d3e 2019-07-12 stsp static const struct got_error *
3431 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3432 3143d852 2020-06-25 stsp {
3433 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3434 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3435 3143d852 2020-06-25 stsp
3436 3143d852 2020-06-25 stsp if (a->no_ignores)
3437 3143d852 2020-06-25 stsp return NULL;
3438 3143d852 2020-06-25 stsp
3439 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3440 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3441 3143d852 2020-06-25 stsp if (err)
3442 3143d852 2020-06-25 stsp return err;
3443 3143d852 2020-06-25 stsp
3444 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3445 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3446 3143d852 2020-06-25 stsp
3447 3143d852 2020-06-25 stsp return err;
3448 3143d852 2020-06-25 stsp }
3449 3143d852 2020-06-25 stsp
3450 3143d852 2020-06-25 stsp static const struct got_error *
3451 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3452 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3453 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3454 abb4604f 2019-07-27 stsp {
3455 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3456 abb4604f 2019-07-27 stsp struct stat sb;
3457 abb4604f 2019-07-27 stsp
3458 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3459 abb4604f 2019-07-27 stsp if (ie)
3460 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3461 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3462 abb4604f 2019-07-27 stsp
3463 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3464 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3465 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3466 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3467 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3468 abb4604f 2019-07-27 stsp return NULL;
3469 abb4604f 2019-07-27 stsp }
3470 abb4604f 2019-07-27 stsp
3471 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3472 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3473 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3474 abb4604f 2019-07-27 stsp
3475 abb4604f 2019-07-27 stsp return NULL;
3476 3143d852 2020-06-25 stsp }
3477 3143d852 2020-06-25 stsp
3478 3143d852 2020-06-25 stsp static const struct got_error *
3479 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3480 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3481 3143d852 2020-06-25 stsp {
3482 3143d852 2020-06-25 stsp const struct got_error *err;
3483 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3484 3143d852 2020-06-25 stsp
3485 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3486 3143d852 2020-06-25 stsp ".cvsignore");
3487 3143d852 2020-06-25 stsp if (err)
3488 3143d852 2020-06-25 stsp return err;
3489 3143d852 2020-06-25 stsp
3490 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3491 3143d852 2020-06-25 stsp ".gitignore");
3492 3143d852 2020-06-25 stsp if (err)
3493 3143d852 2020-06-25 stsp return err;
3494 3143d852 2020-06-25 stsp
3495 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3496 3143d852 2020-06-25 stsp if (err) {
3497 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3498 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3499 3143d852 2020-06-25 stsp return err;
3500 3143d852 2020-06-25 stsp }
3501 3143d852 2020-06-25 stsp for (;;) {
3502 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3503 3143d852 2020-06-25 stsp ".cvsignore");
3504 3143d852 2020-06-25 stsp if (err)
3505 3143d852 2020-06-25 stsp break;
3506 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3507 3143d852 2020-06-25 stsp ".gitignore");
3508 3143d852 2020-06-25 stsp if (err)
3509 3143d852 2020-06-25 stsp break;
3510 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3511 3143d852 2020-06-25 stsp if (err) {
3512 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3513 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3514 3143d852 2020-06-25 stsp break;
3515 3143d852 2020-06-25 stsp }
3516 b737c85a 2020-06-26 stsp free(parent_path);
3517 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3518 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3519 3143d852 2020-06-25 stsp }
3520 3143d852 2020-06-25 stsp
3521 b737c85a 2020-06-26 stsp free(parent_path);
3522 b737c85a 2020-06-26 stsp free(next_parent_path);
3523 3143d852 2020-06-25 stsp return err;
3524 abb4604f 2019-07-27 stsp }
3525 abb4604f 2019-07-27 stsp
3526 abb4604f 2019-07-27 stsp static const struct got_error *
3527 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3528 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3529 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3530 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3531 f2a9dc41 2019-12-13 tracey int report_unchanged)
3532 f8d1f275 2019-02-04 stsp {
3533 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3534 6fc93f37 2019-12-13 stsp int fd = -1;
3535 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3536 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3537 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3538 3143d852 2020-06-25 stsp
3539 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3540 f8d1f275 2019-02-04 stsp
3541 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3542 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3543 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3544 8dc303cc 2019-07-27 stsp
3545 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3546 6fc93f37 2019-12-13 stsp if (fd == -1) {
3547 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3548 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3549 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3550 abb4604f 2019-07-27 stsp else
3551 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3552 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3553 f2a9dc41 2019-12-13 tracey report_unchanged);
3554 abb4604f 2019-07-27 stsp } else {
3555 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3556 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3557 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3558 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3559 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3560 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3561 abb4604f 2019-07-27 stsp arg.status_path = path;
3562 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3563 abb4604f 2019-07-27 stsp arg.repo = repo;
3564 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3565 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3566 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3567 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3568 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3569 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3570 022fae89 2019-12-06 tracey if (!no_ignores) {
3571 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3572 3143d852 2020-06-25 stsp worktree->root_path, path);
3573 3143d852 2020-06-25 stsp if (err)
3574 3143d852 2020-06-25 stsp goto done;
3575 022fae89 2019-12-06 tracey }
3576 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3577 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3578 927df6b7 2019-02-10 stsp }
3579 3143d852 2020-06-25 stsp done:
3580 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3581 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3582 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3583 927df6b7 2019-02-10 stsp free(ondisk_path);
3584 347d1d3e 2019-07-12 stsp return err;
3585 347d1d3e 2019-07-12 stsp }
3586 347d1d3e 2019-07-12 stsp
3587 347d1d3e 2019-07-12 stsp const struct got_error *
3588 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3589 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3590 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3591 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3592 347d1d3e 2019-07-12 stsp {
3593 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3594 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3595 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3596 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3597 347d1d3e 2019-07-12 stsp
3598 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3599 347d1d3e 2019-07-12 stsp if (err)
3600 347d1d3e 2019-07-12 stsp return err;
3601 347d1d3e 2019-07-12 stsp
3602 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3603 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3604 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3605 72ea6654 2019-07-27 stsp if (err)
3606 72ea6654 2019-07-27 stsp break;
3607 72ea6654 2019-07-27 stsp }
3608 f8d1f275 2019-02-04 stsp free(fileindex_path);
3609 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3610 f8d1f275 2019-02-04 stsp return err;
3611 f8d1f275 2019-02-04 stsp }
3612 6c7ab921 2019-03-18 stsp
3613 6c7ab921 2019-03-18 stsp const struct got_error *
3614 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3615 6c7ab921 2019-03-18 stsp const char *arg)
3616 6c7ab921 2019-03-18 stsp {
3617 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3618 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3619 6c7ab921 2019-03-18 stsp size_t len;
3620 00bb5ea0 2020-07-23 stsp struct stat sb;
3621 6c7ab921 2019-03-18 stsp
3622 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3623 6c7ab921 2019-03-18 stsp
3624 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3625 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3626 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3627 00bb5ea0 2020-07-23 stsp
3628 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3629 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3630 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3631 00bb5ea0 2020-07-23 stsp goto done;
3632 00bb5ea0 2020-07-23 stsp }
3633 00bb5ea0 2020-07-23 stsp }
3634 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3635 00bb5ea0 2020-07-23 stsp /*
3636 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3637 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3638 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3639 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3640 00bb5ea0 2020-07-23 stsp */
3641 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3642 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3643 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3644 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3645 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3646 00bb5ea0 2020-07-23 stsp goto done;
3647 00bb5ea0 2020-07-23 stsp }
3648 00bb5ea0 2020-07-23 stsp
3649 00bb5ea0 2020-07-23 stsp }
3650 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3651 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3652 00bb5ea0 2020-07-23 stsp if (err)
3653 d0710d08 2019-07-22 stsp goto done;
3654 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3655 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3656 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3657 00bb5ea0 2020-07-23 stsp goto done;
3658 00bb5ea0 2020-07-23 stsp }
3659 00bb5ea0 2020-07-23 stsp } else {
3660 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3661 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3662 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3663 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3664 00bb5ea0 2020-07-23 stsp goto done;
3665 00bb5ea0 2020-07-23 stsp }
3666 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3667 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3668 00bb5ea0 2020-07-23 stsp goto done;
3669 00bb5ea0 2020-07-23 stsp }
3670 d0710d08 2019-07-22 stsp }
3671 d0710d08 2019-07-22 stsp }
3672 6c7ab921 2019-03-18 stsp
3673 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3674 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3675 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3676 6c7ab921 2019-03-18 stsp goto done;
3677 6c7ab921 2019-03-18 stsp }
3678 6c7ab921 2019-03-18 stsp
3679 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3680 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3681 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3682 f6d88e1a 2019-05-29 stsp if (err)
3683 f6d88e1a 2019-05-29 stsp goto done;
3684 f6d88e1a 2019-05-29 stsp } else {
3685 f6d88e1a 2019-05-29 stsp path = strdup("");
3686 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3687 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3688 f6d88e1a 2019-05-29 stsp goto done;
3689 f6d88e1a 2019-05-29 stsp }
3690 6c7ab921 2019-03-18 stsp }
3691 6c7ab921 2019-03-18 stsp
3692 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3693 6c7ab921 2019-03-18 stsp len = strlen(path);
3694 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3695 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3696 6c7ab921 2019-03-18 stsp len--;
3697 6c7ab921 2019-03-18 stsp }
3698 6c7ab921 2019-03-18 stsp done:
3699 6c7ab921 2019-03-18 stsp free(resolved);
3700 d0710d08 2019-07-22 stsp free(cwd);
3701 6c7ab921 2019-03-18 stsp if (err == NULL)
3702 6c7ab921 2019-03-18 stsp *wt_path = path;
3703 6c7ab921 2019-03-18 stsp else
3704 6c7ab921 2019-03-18 stsp free(path);
3705 d00136be 2019-03-26 stsp return err;
3706 d00136be 2019-03-26 stsp }
3707 d00136be 2019-03-26 stsp
3708 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3709 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3710 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3711 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3712 4e68cba3 2019-11-23 stsp void *progress_arg;
3713 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3714 4e68cba3 2019-11-23 stsp };
3715 4e68cba3 2019-11-23 stsp
3716 4e68cba3 2019-11-23 stsp static const struct got_error *
3717 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3718 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3719 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3720 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3721 07f5b47a 2019-06-02 stsp {
3722 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3723 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3724 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3725 6d022e97 2019-08-04 stsp struct stat sb;
3726 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3727 07f5b47a 2019-06-02 stsp
3728 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3729 dbb83fbd 2019-12-12 stsp relpath) == -1)
3730 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3731 dbb83fbd 2019-12-12 stsp
3732 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3733 6d022e97 2019-08-04 stsp if (ie) {
3734 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3735 12463d8b 2019-12-13 stsp de_name, a->repo);
3736 6d022e97 2019-08-04 stsp if (err)
3737 dbb83fbd 2019-12-12 stsp goto done;
3738 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3739 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3740 dbb83fbd 2019-12-12 stsp goto done;
3741 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3742 dbb83fbd 2019-12-12 stsp if (err)
3743 dbb83fbd 2019-12-12 stsp goto done;
3744 6d022e97 2019-08-04 stsp }
3745 07f5b47a 2019-06-02 stsp
3746 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3747 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3748 dbb83fbd 2019-12-12 stsp goto done;
3749 4e68cba3 2019-11-23 stsp }
3750 07f5b47a 2019-06-02 stsp
3751 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3752 4e68cba3 2019-11-23 stsp if (err)
3753 4e68cba3 2019-11-23 stsp goto done;
3754 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3755 3969253a 2020-03-07 stsp if (err) {
3756 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3757 3969253a 2020-03-07 stsp goto done;
3758 3969253a 2020-03-07 stsp }
3759 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3760 07f5b47a 2019-06-02 stsp if (err) {
3761 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3762 4e68cba3 2019-11-23 stsp goto done;
3763 07f5b47a 2019-06-02 stsp }
3764 4e68cba3 2019-11-23 stsp done:
3765 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3766 dbb83fbd 2019-12-12 stsp if (err)
3767 dbb83fbd 2019-12-12 stsp return err;
3768 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3769 dbb83fbd 2019-12-12 stsp return NULL;
3770 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3771 07f5b47a 2019-06-02 stsp }
3772 07f5b47a 2019-06-02 stsp
3773 d00136be 2019-03-26 stsp const struct got_error *
3774 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3775 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3776 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3777 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3778 d00136be 2019-03-26 stsp {
3779 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3780 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3781 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3782 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3783 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3784 d00136be 2019-03-26 stsp
3785 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3786 d00136be 2019-03-26 stsp if (err)
3787 d00136be 2019-03-26 stsp return err;
3788 d00136be 2019-03-26 stsp
3789 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3790 d00136be 2019-03-26 stsp if (err)
3791 d00136be 2019-03-26 stsp goto done;
3792 d00136be 2019-03-26 stsp
3793 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3794 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3795 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3796 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3797 4e68cba3 2019-11-23 stsp saa.repo = repo;
3798 4e68cba3 2019-11-23 stsp
3799 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3800 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3801 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3802 1dd54920 2019-05-11 stsp if (err)
3803 af12c6b9 2019-06-04 stsp break;
3804 1dd54920 2019-05-11 stsp }
3805 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3806 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3807 af12c6b9 2019-06-04 stsp err = sync_err;
3808 d00136be 2019-03-26 stsp done:
3809 fb399478 2019-07-12 stsp free(fileindex_path);
3810 d00136be 2019-03-26 stsp if (fileindex)
3811 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3812 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3813 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3814 d00136be 2019-03-26 stsp err = unlockerr;
3815 6c7ab921 2019-03-18 stsp return err;
3816 6c7ab921 2019-03-18 stsp }
3817 17ed4618 2019-06-02 stsp
3818 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3819 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3820 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3821 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3822 f2a9dc41 2019-12-13 tracey void *progress_arg;
3823 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3824 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3825 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3826 766841c2 2020-08-13 stsp const char *status_codes;
3827 f2a9dc41 2019-12-13 tracey };
3828 f2a9dc41 2019-12-13 tracey
3829 f2a9dc41 2019-12-13 tracey static const struct got_error *
3830 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3831 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3832 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3833 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3834 17ed4618 2019-06-02 stsp {
3835 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3836 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3837 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3838 17ed4618 2019-06-02 stsp struct stat sb;
3839 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3840 17ed4618 2019-06-02 stsp
3841 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3842 17ed4618 2019-06-02 stsp if (ie == NULL)
3843 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3844 2ec1f75b 2019-03-26 stsp
3845 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3846 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3847 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3848 9acbc4fa 2019-08-03 stsp return NULL;
3849 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3850 9acbc4fa 2019-08-03 stsp }
3851 9acbc4fa 2019-08-03 stsp
3852 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3853 f2a9dc41 2019-12-13 tracey relpath) == -1)
3854 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3855 f2a9dc41 2019-12-13 tracey
3856 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3857 12463d8b 2019-12-13 stsp a->repo);
3858 17ed4618 2019-06-02 stsp if (err)
3859 f2a9dc41 2019-12-13 tracey goto done;
3860 17ed4618 2019-06-02 stsp
3861 766841c2 2020-08-13 stsp if (a->status_codes) {
3862 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3863 766841c2 2020-08-13 stsp int i;
3864 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3865 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3866 766841c2 2020-08-13 stsp break;
3867 766841c2 2020-08-13 stsp }
3868 766841c2 2020-08-13 stsp if (i == ncodes) {
3869 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3870 766841c2 2020-08-13 stsp free(ondisk_path);
3871 766841c2 2020-08-13 stsp return NULL;
3872 766841c2 2020-08-13 stsp }
3873 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3874 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3875 766841c2 2020-08-13 stsp static char msg[64];
3876 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3877 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3878 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3879 766841c2 2020-08-13 stsp goto done;
3880 766841c2 2020-08-13 stsp }
3881 766841c2 2020-08-13 stsp }
3882 766841c2 2020-08-13 stsp
3883 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3884 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3885 f2a9dc41 2019-12-13 tracey goto done;
3886 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3887 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3888 f2a9dc41 2019-12-13 tracey goto done;
3889 f2a9dc41 2019-12-13 tracey }
3890 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3891 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3892 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3893 f2a9dc41 2019-12-13 tracey goto done;
3894 f2a9dc41 2019-12-13 tracey }
3895 17ed4618 2019-06-02 stsp }
3896 17ed4618 2019-06-02 stsp
3897 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3898 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3899 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3900 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3901 12463d8b 2019-12-13 stsp ondisk_path);
3902 12463d8b 2019-12-13 stsp goto done;
3903 12463d8b 2019-12-13 stsp }
3904 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3905 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3906 12463d8b 2019-12-13 stsp goto done;
3907 15341bfd 2020-03-05 tracey }
3908 15341bfd 2020-03-05 tracey
3909 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3910 15341bfd 2020-03-05 tracey
3911 15341bfd 2020-03-05 tracey if (parent == NULL) {
3912 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3913 15341bfd 2020-03-05 tracey goto done;
3914 15341bfd 2020-03-05 tracey }
3915 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3916 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3917 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3918 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3919 15341bfd 2020-03-05 tracey parent);
3920 15341bfd 2020-03-05 tracey break;
3921 15341bfd 2020-03-05 tracey }
3922 15341bfd 2020-03-05 tracey parent = dirname(parent);
3923 15341bfd 2020-03-05 tracey if (parent == NULL) {
3924 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3925 15341bfd 2020-03-05 tracey goto done;
3926 15341bfd 2020-03-05 tracey }
3927 12463d8b 2019-12-13 stsp }
3928 f2a9dc41 2019-12-13 tracey }
3929 17ed4618 2019-06-02 stsp
3930 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3931 f2a9dc41 2019-12-13 tracey done:
3932 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3933 f2a9dc41 2019-12-13 tracey if (err)
3934 f2a9dc41 2019-12-13 tracey return err;
3935 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3936 f2a9dc41 2019-12-13 tracey return NULL;
3937 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3938 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3939 17ed4618 2019-06-02 stsp }
3940 17ed4618 2019-06-02 stsp
3941 2ec1f75b 2019-03-26 stsp const struct got_error *
3942 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3943 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3944 766841c2 2020-08-13 stsp const char *status_codes,
3945 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3946 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3947 2ec1f75b 2019-03-26 stsp {
3948 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3949 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3950 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3951 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3952 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3953 2ec1f75b 2019-03-26 stsp
3954 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3955 2ec1f75b 2019-03-26 stsp if (err)
3956 2ec1f75b 2019-03-26 stsp return err;
3957 2ec1f75b 2019-03-26 stsp
3958 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3959 2ec1f75b 2019-03-26 stsp if (err)
3960 2ec1f75b 2019-03-26 stsp goto done;
3961 f2a9dc41 2019-12-13 tracey
3962 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3963 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3964 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3965 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3966 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3967 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3968 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3969 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
3970 2ec1f75b 2019-03-26 stsp
3971 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3972 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3973 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3974 17ed4618 2019-06-02 stsp if (err)
3975 af12c6b9 2019-06-04 stsp break;
3976 2ec1f75b 2019-03-26 stsp }
3977 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3978 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3979 af12c6b9 2019-06-04 stsp err = sync_err;
3980 2ec1f75b 2019-03-26 stsp done:
3981 fb399478 2019-07-12 stsp free(fileindex_path);
3982 2ec1f75b 2019-03-26 stsp if (fileindex)
3983 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3984 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3985 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3986 2ec1f75b 2019-03-26 stsp err = unlockerr;
3987 33aa809d 2019-08-08 stsp return err;
3988 33aa809d 2019-08-08 stsp }
3989 33aa809d 2019-08-08 stsp
3990 33aa809d 2019-08-08 stsp static const struct got_error *
3991 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3992 33aa809d 2019-08-08 stsp {
3993 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3994 33aa809d 2019-08-08 stsp char *line = NULL;
3995 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3996 33aa809d 2019-08-08 stsp ssize_t linelen;
3997 33aa809d 2019-08-08 stsp
3998 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3999 33aa809d 2019-08-08 stsp if (linelen == -1) {
4000 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4001 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4002 33aa809d 2019-08-08 stsp goto done;
4003 33aa809d 2019-08-08 stsp }
4004 33aa809d 2019-08-08 stsp return NULL;
4005 33aa809d 2019-08-08 stsp }
4006 33aa809d 2019-08-08 stsp if (outfile) {
4007 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4008 33aa809d 2019-08-08 stsp if (n != linelen) {
4009 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4010 33aa809d 2019-08-08 stsp goto done;
4011 33aa809d 2019-08-08 stsp }
4012 33aa809d 2019-08-08 stsp }
4013 33aa809d 2019-08-08 stsp if (rejectfile) {
4014 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4015 33aa809d 2019-08-08 stsp if (n != linelen)
4016 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4017 33aa809d 2019-08-08 stsp }
4018 33aa809d 2019-08-08 stsp done:
4019 33aa809d 2019-08-08 stsp free(line);
4020 2ec1f75b 2019-03-26 stsp return err;
4021 2ec1f75b 2019-03-26 stsp }
4022 1f1abb7e 2019-08-08 stsp
4023 33aa809d 2019-08-08 stsp static const struct got_error *
4024 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4025 33aa809d 2019-08-08 stsp {
4026 33aa809d 2019-08-08 stsp char *line = NULL;
4027 33aa809d 2019-08-08 stsp size_t linesize = 0;
4028 33aa809d 2019-08-08 stsp ssize_t linelen;
4029 33aa809d 2019-08-08 stsp
4030 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4031 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4032 500467ff 2019-09-25 hiltjo if (ferror(f))
4033 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4034 500467ff 2019-09-25 hiltjo return NULL;
4035 500467ff 2019-09-25 hiltjo }
4036 33aa809d 2019-08-08 stsp free(line);
4037 33aa809d 2019-08-08 stsp return NULL;
4038 33aa809d 2019-08-08 stsp }
4039 33aa809d 2019-08-08 stsp
4040 33aa809d 2019-08-08 stsp static const struct got_error *
4041 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4042 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4043 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4044 33aa809d 2019-08-08 stsp {
4045 33aa809d 2019-08-08 stsp const struct got_error *err;
4046 33aa809d 2019-08-08 stsp
4047 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4048 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4049 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4050 33aa809d 2019-08-08 stsp if (err)
4051 33aa809d 2019-08-08 stsp return err;
4052 33aa809d 2019-08-08 stsp (*line_cur1)++;
4053 33aa809d 2019-08-08 stsp }
4054 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4055 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4056 33aa809d 2019-08-08 stsp if (rejectfile)
4057 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4058 33aa809d 2019-08-08 stsp else
4059 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4060 33aa809d 2019-08-08 stsp if (err)
4061 33aa809d 2019-08-08 stsp return err;
4062 33aa809d 2019-08-08 stsp (*line_cur2)++;
4063 33aa809d 2019-08-08 stsp }
4064 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4065 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4066 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4067 33aa809d 2019-08-08 stsp if (err)
4068 33aa809d 2019-08-08 stsp return err;
4069 33aa809d 2019-08-08 stsp (*line_cur2)++;
4070 33aa809d 2019-08-08 stsp }
4071 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4072 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4073 33aa809d 2019-08-08 stsp if (rejectfile)
4074 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4075 33aa809d 2019-08-08 stsp else
4076 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4077 33aa809d 2019-08-08 stsp if (err)
4078 33aa809d 2019-08-08 stsp return err;
4079 33aa809d 2019-08-08 stsp (*line_cur1)++;
4080 33aa809d 2019-08-08 stsp }
4081 f1e81a05 2019-08-10 stsp
4082 f1e81a05 2019-08-10 stsp return NULL;
4083 f1e81a05 2019-08-10 stsp }
4084 f1e81a05 2019-08-10 stsp
4085 f1e81a05 2019-08-10 stsp static const struct got_error *
4086 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4087 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4088 f1e81a05 2019-08-10 stsp {
4089 f1e81a05 2019-08-10 stsp const struct got_error *err;
4090 f1e81a05 2019-08-10 stsp
4091 f1e81a05 2019-08-10 stsp if (outfile) {
4092 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4093 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4094 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4095 f1e81a05 2019-08-10 stsp if (err)
4096 f1e81a05 2019-08-10 stsp return err;
4097 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4098 f1e81a05 2019-08-10 stsp }
4099 f1e81a05 2019-08-10 stsp }
4100 f1e81a05 2019-08-10 stsp if (rejectfile) {
4101 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4102 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4103 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4104 f1e81a05 2019-08-10 stsp if (err)
4105 f1e81a05 2019-08-10 stsp return err;
4106 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4107 f1e81a05 2019-08-10 stsp }
4108 33aa809d 2019-08-08 stsp }
4109 33aa809d 2019-08-08 stsp
4110 33aa809d 2019-08-08 stsp return NULL;
4111 33aa809d 2019-08-08 stsp }
4112 33aa809d 2019-08-08 stsp
4113 33aa809d 2019-08-08 stsp static const struct got_error *
4114 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4115 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4116 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4117 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
4118 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4119 33aa809d 2019-08-08 stsp {
4120 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4121 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4122 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4123 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4124 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4125 33aa809d 2019-08-08 stsp long pos1, pos2;
4126 33aa809d 2019-08-08 stsp FILE *hunkfile;
4127 33aa809d 2019-08-08 stsp
4128 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4129 33aa809d 2019-08-08 stsp
4130 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4131 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4132 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4133 33aa809d 2019-08-08 stsp
4134 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4135 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4136 33aa809d 2019-08-08 stsp
4137 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4138 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4139 33aa809d 2019-08-08 stsp
4140 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4141 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4142 33aa809d 2019-08-08 stsp goto done;
4143 33aa809d 2019-08-08 stsp }
4144 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4145 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4146 33aa809d 2019-08-08 stsp goto done;
4147 33aa809d 2019-08-08 stsp }
4148 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4149 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4150 33aa809d 2019-08-08 stsp goto done;
4151 33aa809d 2019-08-08 stsp }
4152 33aa809d 2019-08-08 stsp
4153 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4154 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4155 33aa809d 2019-08-08 stsp if (err)
4156 33aa809d 2019-08-08 stsp goto done;
4157 33aa809d 2019-08-08 stsp
4158 33aa809d 2019-08-08 stsp switch (*choice) {
4159 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4160 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4161 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4162 33aa809d 2019-08-08 stsp break;
4163 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4164 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4165 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4166 33aa809d 2019-08-08 stsp break;
4167 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4168 33aa809d 2019-08-08 stsp break;
4169 33aa809d 2019-08-08 stsp default:
4170 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4171 33aa809d 2019-08-08 stsp break;
4172 33aa809d 2019-08-08 stsp }
4173 33aa809d 2019-08-08 stsp done:
4174 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4175 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4176 33aa809d 2019-08-08 stsp return err;
4177 33aa809d 2019-08-08 stsp }
4178 33aa809d 2019-08-08 stsp
4179 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4180 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4181 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4182 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4183 1f1abb7e 2019-08-08 stsp void *progress_arg;
4184 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4185 33aa809d 2019-08-08 stsp void *patch_arg;
4186 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4187 1f1abb7e 2019-08-08 stsp };
4188 a129376b 2019-03-28 stsp
4189 e20a8b6f 2019-06-04 stsp static const struct got_error *
4190 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4191 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4192 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4193 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4194 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4195 33aa809d 2019-08-08 stsp {
4196 33aa809d 2019-08-08 stsp const struct got_error *err;
4197 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4198 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4199 1ebedb77 2019-10-19 stsp int fd2 = -1;
4200 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4201 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4202 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4203 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4204 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4205 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4206 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4207 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4208 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4209 33aa809d 2019-08-08 stsp int n = 0;
4210 33aa809d 2019-08-08 stsp
4211 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4212 33aa809d 2019-08-08 stsp
4213 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4214 33aa809d 2019-08-08 stsp if (err)
4215 33aa809d 2019-08-08 stsp return err;
4216 33aa809d 2019-08-08 stsp
4217 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4218 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4219 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4220 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4221 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4222 fa3cef63 2020-07-23 stsp goto done;
4223 fa3cef63 2020-07-23 stsp }
4224 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4225 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4226 fa3cef63 2020-07-23 stsp if (link_len == -1)
4227 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4228 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4229 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4230 12463d8b 2019-12-13 stsp }
4231 12463d8b 2019-12-13 stsp } else {
4232 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4233 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4234 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4235 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4236 fa3cef63 2020-07-23 stsp goto done;
4237 fa3cef63 2020-07-23 stsp }
4238 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4239 fa3cef63 2020-07-23 stsp sizeof(link_target));
4240 fa3cef63 2020-07-23 stsp if (link_len == -1)
4241 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4242 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4243 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4244 12463d8b 2019-12-13 stsp }
4245 1ebedb77 2019-10-19 stsp }
4246 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4247 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4248 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4249 fa3cef63 2020-07-23 stsp goto done;
4250 fa3cef63 2020-07-23 stsp }
4251 1ebedb77 2019-10-19 stsp
4252 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4253 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4254 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4255 fa3cef63 2020-07-23 stsp goto done;
4256 fa3cef63 2020-07-23 stsp }
4257 fa3cef63 2020-07-23 stsp fd2 = -1;
4258 fa3cef63 2020-07-23 stsp } else {
4259 fa3cef63 2020-07-23 stsp size_t n;
4260 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4261 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4262 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4263 fa3cef63 2020-07-23 stsp goto done;
4264 fa3cef63 2020-07-23 stsp }
4265 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4266 fa3cef63 2020-07-23 stsp if (n != link_len) {
4267 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4268 fa3cef63 2020-07-23 stsp goto done;
4269 fa3cef63 2020-07-23 stsp }
4270 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4271 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4272 fa3cef63 2020-07-23 stsp goto done;
4273 fa3cef63 2020-07-23 stsp }
4274 fa3cef63 2020-07-23 stsp rewind(f2);
4275 33aa809d 2019-08-08 stsp }
4276 33aa809d 2019-08-08 stsp
4277 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4278 33aa809d 2019-08-08 stsp if (err)
4279 33aa809d 2019-08-08 stsp goto done;
4280 33aa809d 2019-08-08 stsp
4281 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4282 33aa809d 2019-08-08 stsp if (err)
4283 33aa809d 2019-08-08 stsp goto done;
4284 33aa809d 2019-08-08 stsp
4285 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4286 33aa809d 2019-08-08 stsp if (err)
4287 33aa809d 2019-08-08 stsp goto done;
4288 33aa809d 2019-08-08 stsp
4289 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4290 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4291 33aa809d 2019-08-08 stsp goto done;
4292 33aa809d 2019-08-08 stsp }
4293 33aa809d 2019-08-08 stsp
4294 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4295 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4296 33aa809d 2019-08-08 stsp if (err)
4297 33aa809d 2019-08-08 stsp goto done;
4298 33aa809d 2019-08-08 stsp
4299 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4300 33aa809d 2019-08-08 stsp if (err)
4301 33aa809d 2019-08-08 stsp goto done;
4302 33aa809d 2019-08-08 stsp
4303 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4304 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4305 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4306 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4307 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4308 33aa809d 2019-08-08 stsp int choice;
4309 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4310 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4311 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4312 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4313 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4314 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4315 33aa809d 2019-08-08 stsp if (err)
4316 33aa809d 2019-08-08 stsp goto done;
4317 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4318 33aa809d 2019-08-08 stsp have_content = 1;
4319 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4320 33aa809d 2019-08-08 stsp break;
4321 33aa809d 2019-08-08 stsp }
4322 1ebedb77 2019-10-19 stsp if (have_content) {
4323 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4324 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4325 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4326 1ebedb77 2019-10-19 stsp if (err)
4327 1ebedb77 2019-10-19 stsp goto done;
4328 1ebedb77 2019-10-19 stsp
4329 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4330 fa3cef63 2020-07-23 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
4331 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("chmod", path2);
4332 fa3cef63 2020-07-23 stsp goto done;
4333 fa3cef63 2020-07-23 stsp }
4334 1ebedb77 2019-10-19 stsp }
4335 1ebedb77 2019-10-19 stsp }
4336 33aa809d 2019-08-08 stsp done:
4337 33aa809d 2019-08-08 stsp free(id_str);
4338 33aa809d 2019-08-08 stsp if (blob)
4339 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4340 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4341 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4342 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4343 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4344 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4345 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4346 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4347 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4348 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4349 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4350 33aa809d 2019-08-08 stsp if (err || !have_content) {
4351 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4352 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4353 33aa809d 2019-08-08 stsp free(*path_outfile);
4354 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4355 33aa809d 2019-08-08 stsp }
4356 33aa809d 2019-08-08 stsp free(args);
4357 33aa809d 2019-08-08 stsp if (ds) {
4358 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4359 33aa809d 2019-08-08 stsp free(ds);
4360 33aa809d 2019-08-08 stsp }
4361 33aa809d 2019-08-08 stsp if (changes)
4362 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4363 33aa809d 2019-08-08 stsp free(path1);
4364 33aa809d 2019-08-08 stsp return err;
4365 33aa809d 2019-08-08 stsp }
4366 33aa809d 2019-08-08 stsp
4367 33aa809d 2019-08-08 stsp static const struct got_error *
4368 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4369 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4370 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4371 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4372 a129376b 2019-03-28 stsp {
4373 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4374 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4375 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4376 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4377 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4378 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4379 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4380 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4381 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4382 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4383 a129376b 2019-03-28 stsp
4384 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4385 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4386 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4387 d3bcc3d1 2019-08-08 stsp return NULL;
4388 3d69ad8d 2019-08-17 semarie
4389 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4390 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4391 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4392 d3bcc3d1 2019-08-08 stsp
4393 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4394 65084dad 2019-08-08 stsp if (ie == NULL)
4395 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4396 a129376b 2019-03-28 stsp
4397 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4398 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4399 a129376b 2019-03-28 stsp if (err) {
4400 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4401 a129376b 2019-03-28 stsp goto done;
4402 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4403 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4404 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4405 e20a8b6f 2019-06-04 stsp goto done;
4406 e20a8b6f 2019-06-04 stsp }
4407 a129376b 2019-03-28 stsp }
4408 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4409 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4410 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4411 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4412 a129376b 2019-03-28 stsp goto done;
4413 a129376b 2019-03-28 stsp }
4414 a129376b 2019-03-28 stsp } else {
4415 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4416 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4417 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4418 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4419 a129376b 2019-03-28 stsp goto done;
4420 a129376b 2019-03-28 stsp }
4421 a129376b 2019-03-28 stsp } else {
4422 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4423 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4424 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4425 a129376b 2019-03-28 stsp goto done;
4426 a129376b 2019-03-28 stsp }
4427 a129376b 2019-03-28 stsp }
4428 a129376b 2019-03-28 stsp }
4429 a129376b 2019-03-28 stsp
4430 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4431 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4432 a9fa2909 2019-07-27 stsp if (err) {
4433 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4434 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4435 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4436 a9fa2909 2019-07-27 stsp goto done;
4437 a9fa2909 2019-07-27 stsp } else {
4438 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4439 a9fa2909 2019-07-27 stsp if (err)
4440 a9fa2909 2019-07-27 stsp goto done;
4441 a9fa2909 2019-07-27 stsp
4442 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4443 1233e6b6 2020-10-19 stsp if (err)
4444 a9fa2909 2019-07-27 stsp goto done;
4445 a9fa2909 2019-07-27 stsp
4446 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4447 1233e6b6 2020-10-19 stsp free(te_name);
4448 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4449 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4450 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4451 a9fa2909 2019-07-27 stsp goto done;
4452 a9fa2909 2019-07-27 stsp }
4453 a129376b 2019-03-28 stsp }
4454 a129376b 2019-03-28 stsp
4455 a129376b 2019-03-28 stsp switch (status) {
4456 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4457 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4458 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4459 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4460 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4461 33aa809d 2019-08-08 stsp if (err)
4462 33aa809d 2019-08-08 stsp goto done;
4463 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4464 33aa809d 2019-08-08 stsp break;
4465 33aa809d 2019-08-08 stsp }
4466 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4467 1f1abb7e 2019-08-08 stsp ie->path);
4468 1ee397ad 2019-07-12 stsp if (err)
4469 1ee397ad 2019-07-12 stsp goto done;
4470 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4471 a129376b 2019-03-28 stsp break;
4472 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4473 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4474 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4475 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4476 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4477 33aa809d 2019-08-08 stsp if (err)
4478 33aa809d 2019-08-08 stsp goto done;
4479 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4480 33aa809d 2019-08-08 stsp break;
4481 33aa809d 2019-08-08 stsp }
4482 33aa809d 2019-08-08 stsp /* fall through */
4483 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4484 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4485 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4486 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4487 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4488 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4489 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4490 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4491 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4492 24278f30 2019-08-03 stsp } else
4493 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4494 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4495 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4496 a129376b 2019-03-28 stsp if (err)
4497 65084dad 2019-08-08 stsp goto done;
4498 65084dad 2019-08-08 stsp
4499 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4500 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4501 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4502 a129376b 2019-03-28 stsp goto done;
4503 65084dad 2019-08-08 stsp }
4504 65084dad 2019-08-08 stsp
4505 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4506 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4507 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4508 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4509 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4510 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4511 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4512 33aa809d 2019-08-08 stsp break;
4513 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4514 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4515 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4516 369fd7e5 2020-07-23 stsp path_content);
4517 369fd7e5 2020-07-23 stsp break;
4518 369fd7e5 2020-07-23 stsp }
4519 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4520 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4521 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4522 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4523 369fd7e5 2020-07-23 stsp } else {
4524 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4525 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4526 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4527 369fd7e5 2020-07-23 stsp goto done;
4528 369fd7e5 2020-07-23 stsp }
4529 33aa809d 2019-08-08 stsp }
4530 33aa809d 2019-08-08 stsp } else {
4531 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4532 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4533 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4534 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4535 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4536 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4537 2e1fa222 2020-07-23 stsp } else {
4538 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4539 2e1fa222 2020-07-23 stsp ie->path,
4540 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4541 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4542 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4543 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4544 2e1fa222 2020-07-23 stsp }
4545 a129376b 2019-03-28 stsp if (err)
4546 a129376b 2019-03-28 stsp goto done;
4547 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4548 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4549 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4550 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4551 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4552 2e1fa222 2020-07-23 stsp if (err)
4553 2e1fa222 2020-07-23 stsp goto done;
4554 2e1fa222 2020-07-23 stsp }
4555 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4556 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4557 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4558 33aa809d 2019-08-08 stsp }
4559 a129376b 2019-03-28 stsp }
4560 a129376b 2019-03-28 stsp break;
4561 e20a8b6f 2019-06-04 stsp }
4562 a129376b 2019-03-28 stsp default:
4563 1f1abb7e 2019-08-08 stsp break;
4564 a129376b 2019-03-28 stsp }
4565 a129376b 2019-03-28 stsp done:
4566 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4567 33aa809d 2019-08-08 stsp free(path_content);
4568 e20a8b6f 2019-06-04 stsp free(parent_path);
4569 a129376b 2019-03-28 stsp free(tree_path);
4570 a129376b 2019-03-28 stsp if (blob)
4571 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4572 a129376b 2019-03-28 stsp if (tree)
4573 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4574 a129376b 2019-03-28 stsp free(tree_id);
4575 e20a8b6f 2019-06-04 stsp return err;
4576 e20a8b6f 2019-06-04 stsp }
4577 e20a8b6f 2019-06-04 stsp
4578 e20a8b6f 2019-06-04 stsp const struct got_error *
4579 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4580 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4581 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4582 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4583 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4584 e20a8b6f 2019-06-04 stsp {
4585 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4586 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4587 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4588 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4589 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4590 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4591 e20a8b6f 2019-06-04 stsp
4592 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4593 e20a8b6f 2019-06-04 stsp if (err)
4594 e20a8b6f 2019-06-04 stsp return err;
4595 e20a8b6f 2019-06-04 stsp
4596 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4597 e20a8b6f 2019-06-04 stsp if (err)
4598 e20a8b6f 2019-06-04 stsp goto done;
4599 e20a8b6f 2019-06-04 stsp
4600 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4601 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4602 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4603 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4604 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4605 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4606 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4607 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4608 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4609 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4610 e20a8b6f 2019-06-04 stsp if (err)
4611 af12c6b9 2019-06-04 stsp break;
4612 e20a8b6f 2019-06-04 stsp }
4613 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4614 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4615 e20a8b6f 2019-06-04 stsp err = sync_err;
4616 af12c6b9 2019-06-04 stsp done:
4617 fb399478 2019-07-12 stsp free(fileindex_path);
4618 a129376b 2019-03-28 stsp if (fileindex)
4619 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4620 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4621 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4622 a129376b 2019-03-28 stsp err = unlockerr;
4623 c4296144 2019-05-09 stsp return err;
4624 c4296144 2019-05-09 stsp }
4625 c4296144 2019-05-09 stsp
4626 cf066bf8 2019-05-09 stsp static void
4627 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4628 cf066bf8 2019-05-09 stsp {
4629 24519714 2019-05-09 stsp free(ct->path);
4630 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4631 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4632 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4633 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4634 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4635 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4636 cf066bf8 2019-05-09 stsp free(ct);
4637 cf066bf8 2019-05-09 stsp }
4638 24519714 2019-05-09 stsp
4639 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4640 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4641 24519714 2019-05-09 stsp struct got_repository *repo;
4642 24519714 2019-05-09 stsp struct got_worktree *worktree;
4643 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4644 5f8a88c6 2019-08-03 stsp int have_staged_files;
4645 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4646 24519714 2019-05-09 stsp };
4647 cf066bf8 2019-05-09 stsp
4648 c4296144 2019-05-09 stsp static const struct got_error *
4649 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4650 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4651 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4652 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4653 c4296144 2019-05-09 stsp {
4654 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4655 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4656 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4657 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4658 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4659 768aea60 2019-05-09 stsp struct stat sb;
4660 c4296144 2019-05-09 stsp
4661 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4662 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4663 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4664 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4665 5f8a88c6 2019-08-03 stsp return NULL;
4666 5f8a88c6 2019-08-03 stsp } else {
4667 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4668 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4669 c4296144 2019-05-09 stsp
4670 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4671 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4672 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4673 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4674 5f8a88c6 2019-08-03 stsp return NULL;
4675 5f8a88c6 2019-08-03 stsp }
4676 0b5cc0d6 2019-05-09 stsp
4677 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4678 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4679 036813ee 2019-05-09 stsp goto done;
4680 036813ee 2019-05-09 stsp }
4681 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4682 036813ee 2019-05-09 stsp parent_path = strdup("");
4683 69960a46 2019-05-09 stsp if (parent_path == NULL)
4684 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4685 69960a46 2019-05-09 stsp } else {
4686 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4687 69960a46 2019-05-09 stsp if (err)
4688 69960a46 2019-05-09 stsp return err;
4689 69960a46 2019-05-09 stsp }
4690 c4296144 2019-05-09 stsp
4691 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4692 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4693 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4694 c4296144 2019-05-09 stsp goto done;
4695 768aea60 2019-05-09 stsp }
4696 768aea60 2019-05-09 stsp
4697 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4698 768aea60 2019-05-09 stsp relpath) == -1) {
4699 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4700 768aea60 2019-05-09 stsp goto done;
4701 768aea60 2019-05-09 stsp }
4702 0aeb8099 2020-07-23 stsp
4703 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4704 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4705 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4706 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4707 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4708 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4709 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4710 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4711 0aeb8099 2020-07-23 stsp break;
4712 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4713 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4714 0aeb8099 2020-07-23 stsp break;
4715 0aeb8099 2020-07-23 stsp default:
4716 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4717 0aeb8099 2020-07-23 stsp goto done;
4718 0aeb8099 2020-07-23 stsp }
4719 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4720 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4721 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4722 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4723 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4724 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4725 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4726 12463d8b 2019-12-13 stsp ct->ondisk_path);
4727 12463d8b 2019-12-13 stsp goto done;
4728 12463d8b 2019-12-13 stsp }
4729 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4730 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4731 768aea60 2019-05-09 stsp goto done;
4732 768aea60 2019-05-09 stsp }
4733 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4734 c4296144 2019-05-09 stsp }
4735 c4296144 2019-05-09 stsp
4736 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4737 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4738 44d03001 2019-05-09 stsp relpath) == -1) {
4739 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4740 44d03001 2019-05-09 stsp goto done;
4741 44d03001 2019-05-09 stsp }
4742 44d03001 2019-05-09 stsp
4743 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4744 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4745 35213c7c 2020-07-23 stsp int is_bad_symlink;
4746 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4747 35213c7c 2020-07-23 stsp ssize_t target_len;
4748 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4749 35213c7c 2020-07-23 stsp sizeof(target_path));
4750 35213c7c 2020-07-23 stsp if (target_len == -1) {
4751 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4752 35213c7c 2020-07-23 stsp ct->ondisk_path);
4753 35213c7c 2020-07-23 stsp goto done;
4754 35213c7c 2020-07-23 stsp }
4755 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4756 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4757 35213c7c 2020-07-23 stsp if (err)
4758 35213c7c 2020-07-23 stsp goto done;
4759 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4760 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4761 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4762 35213c7c 2020-07-23 stsp goto done;
4763 35213c7c 2020-07-23 stsp }
4764 35213c7c 2020-07-23 stsp }
4765 35213c7c 2020-07-23 stsp
4766 35213c7c 2020-07-23 stsp
4767 cf066bf8 2019-05-09 stsp ct->status = status;
4768 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4769 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4770 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4771 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4772 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4773 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4774 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4775 b416585c 2019-05-13 stsp goto done;
4776 b416585c 2019-05-13 stsp }
4777 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4778 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4779 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4780 f0b75401 2019-08-03 stsp goto done;
4781 f0b75401 2019-08-03 stsp }
4782 f0b75401 2019-08-03 stsp }
4783 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4784 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4785 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4786 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4787 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4788 036813ee 2019-05-09 stsp goto done;
4789 036813ee 2019-05-09 stsp }
4790 ca2503ea 2019-05-09 stsp }
4791 24519714 2019-05-09 stsp ct->path = strdup(path);
4792 24519714 2019-05-09 stsp if (ct->path == NULL) {
4793 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4794 24519714 2019-05-09 stsp goto done;
4795 24519714 2019-05-09 stsp }
4796 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4797 c4296144 2019-05-09 stsp done:
4798 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4799 ed175427 2019-05-09 stsp free_commitable(ct);
4800 24519714 2019-05-09 stsp free(parent_path);
4801 036813ee 2019-05-09 stsp free(path);
4802 a129376b 2019-03-28 stsp return err;
4803 a129376b 2019-03-28 stsp }
4804 c4296144 2019-05-09 stsp
4805 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4806 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4807 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4808 036813ee 2019-05-09 stsp struct got_repository *);
4809 ed175427 2019-05-09 stsp
4810 ed175427 2019-05-09 stsp static const struct got_error *
4811 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4812 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4813 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4814 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4815 afa376bf 2019-05-09 stsp struct got_repository *repo)
4816 ed175427 2019-05-09 stsp {
4817 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4818 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4819 036813ee 2019-05-09 stsp char *subpath;
4820 ed175427 2019-05-09 stsp
4821 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4822 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4823 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4824 ed175427 2019-05-09 stsp
4825 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4826 ed175427 2019-05-09 stsp if (err)
4827 ed175427 2019-05-09 stsp return err;
4828 ed175427 2019-05-09 stsp
4829 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4830 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4831 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4832 036813ee 2019-05-09 stsp free(subpath);
4833 036813ee 2019-05-09 stsp return err;
4834 036813ee 2019-05-09 stsp }
4835 ed175427 2019-05-09 stsp
4836 036813ee 2019-05-09 stsp static const struct got_error *
4837 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4838 036813ee 2019-05-09 stsp {
4839 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4840 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4841 ed175427 2019-05-09 stsp
4842 036813ee 2019-05-09 stsp *match = 0;
4843 ed175427 2019-05-09 stsp
4844 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4845 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4846 0f63689d 2019-05-10 stsp return NULL;
4847 036813ee 2019-05-09 stsp }
4848 0b5cc0d6 2019-05-09 stsp
4849 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4850 0f63689d 2019-05-10 stsp if (err)
4851 0f63689d 2019-05-10 stsp return err;
4852 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4853 036813ee 2019-05-09 stsp free(ct_parent_path);
4854 036813ee 2019-05-09 stsp return err;
4855 036813ee 2019-05-09 stsp }
4856 0b5cc0d6 2019-05-09 stsp
4857 768aea60 2019-05-09 stsp static mode_t
4858 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4859 768aea60 2019-05-09 stsp {
4860 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4861 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4862 3d9a4ec4 2020-07-23 stsp
4863 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4864 768aea60 2019-05-09 stsp }
4865 768aea60 2019-05-09 stsp
4866 036813ee 2019-05-09 stsp static const struct got_error *
4867 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4868 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4869 036813ee 2019-05-09 stsp {
4870 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4871 ca2503ea 2019-05-09 stsp
4872 036813ee 2019-05-09 stsp *new_te = NULL;
4873 0b5cc0d6 2019-05-09 stsp
4874 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4875 036813ee 2019-05-09 stsp if (err)
4876 036813ee 2019-05-09 stsp goto done;
4877 0b5cc0d6 2019-05-09 stsp
4878 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4879 036813ee 2019-05-09 stsp
4880 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4881 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4882 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4883 5f8a88c6 2019-08-03 stsp else
4884 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4885 036813ee 2019-05-09 stsp done:
4886 036813ee 2019-05-09 stsp if (err && *new_te) {
4887 56e0773d 2019-11-28 stsp free(*new_te);
4888 036813ee 2019-05-09 stsp *new_te = NULL;
4889 036813ee 2019-05-09 stsp }
4890 036813ee 2019-05-09 stsp return err;
4891 036813ee 2019-05-09 stsp }
4892 036813ee 2019-05-09 stsp
4893 036813ee 2019-05-09 stsp static const struct got_error *
4894 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4895 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4896 036813ee 2019-05-09 stsp {
4897 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4898 102b254e 2020-10-19 stsp char *ct_name = NULL;
4899 036813ee 2019-05-09 stsp
4900 036813ee 2019-05-09 stsp *new_te = NULL;
4901 036813ee 2019-05-09 stsp
4902 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4903 036813ee 2019-05-09 stsp if (*new_te == NULL)
4904 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4905 036813ee 2019-05-09 stsp
4906 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
4907 102b254e 2020-10-19 stsp if (err)
4908 036813ee 2019-05-09 stsp goto done;
4909 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4910 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4911 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4912 036813ee 2019-05-09 stsp goto done;
4913 036813ee 2019-05-09 stsp }
4914 036813ee 2019-05-09 stsp
4915 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4916 036813ee 2019-05-09 stsp
4917 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4918 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4919 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4920 5f8a88c6 2019-08-03 stsp else
4921 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4922 036813ee 2019-05-09 stsp done:
4923 102b254e 2020-10-19 stsp free(ct_name);
4924 036813ee 2019-05-09 stsp if (err && *new_te) {
4925 56e0773d 2019-11-28 stsp free(*new_te);
4926 036813ee 2019-05-09 stsp *new_te = NULL;
4927 036813ee 2019-05-09 stsp }
4928 036813ee 2019-05-09 stsp return err;
4929 036813ee 2019-05-09 stsp }
4930 036813ee 2019-05-09 stsp
4931 036813ee 2019-05-09 stsp static const struct got_error *
4932 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4933 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4934 036813ee 2019-05-09 stsp {
4935 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4936 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4937 036813ee 2019-05-09 stsp
4938 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4939 036813ee 2019-05-09 stsp if (err)
4940 036813ee 2019-05-09 stsp return err;
4941 036813ee 2019-05-09 stsp if (new_pe == NULL)
4942 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4943 036813ee 2019-05-09 stsp return NULL;
4944 afa376bf 2019-05-09 stsp }
4945 afa376bf 2019-05-09 stsp
4946 afa376bf 2019-05-09 stsp static const struct got_error *
4947 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4948 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4949 afa376bf 2019-05-09 stsp {
4950 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4951 5f8a88c6 2019-08-03 stsp unsigned char status;
4952 5f8a88c6 2019-08-03 stsp
4953 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4954 afa376bf 2019-05-09 stsp ct_path++;
4955 5f8a88c6 2019-08-03 stsp
4956 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4957 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4958 5f8a88c6 2019-08-03 stsp else
4959 5f8a88c6 2019-08-03 stsp status = ct->status;
4960 5f8a88c6 2019-08-03 stsp
4961 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4962 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4963 036813ee 2019-05-09 stsp }
4964 036813ee 2019-05-09 stsp
4965 036813ee 2019-05-09 stsp static const struct got_error *
4966 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4967 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4968 44d03001 2019-05-09 stsp {
4969 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4970 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4971 44d03001 2019-05-09 stsp char *te_path;
4972 44d03001 2019-05-09 stsp
4973 44d03001 2019-05-09 stsp *modified = 0;
4974 44d03001 2019-05-09 stsp
4975 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4976 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4977 44d03001 2019-05-09 stsp te->name) == -1)
4978 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4979 44d03001 2019-05-09 stsp
4980 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4981 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4982 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4983 44d03001 2019-05-09 stsp strlen(te_path));
4984 44d03001 2019-05-09 stsp if (*modified)
4985 44d03001 2019-05-09 stsp break;
4986 44d03001 2019-05-09 stsp }
4987 44d03001 2019-05-09 stsp
4988 44d03001 2019-05-09 stsp free(te_path);
4989 44d03001 2019-05-09 stsp return err;
4990 44d03001 2019-05-09 stsp }
4991 44d03001 2019-05-09 stsp
4992 44d03001 2019-05-09 stsp static const struct got_error *
4993 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4994 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4995 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4996 036813ee 2019-05-09 stsp {
4997 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4998 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4999 036813ee 2019-05-09 stsp
5000 036813ee 2019-05-09 stsp *ctp = NULL;
5001 036813ee 2019-05-09 stsp
5002 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5003 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5004 036813ee 2019-05-09 stsp char *ct_name = NULL;
5005 036813ee 2019-05-09 stsp int path_matches;
5006 036813ee 2019-05-09 stsp
5007 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5008 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5009 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5010 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5011 5f8a88c6 2019-08-03 stsp continue;
5012 5f8a88c6 2019-08-03 stsp } else {
5013 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5014 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5015 5f8a88c6 2019-08-03 stsp continue;
5016 5f8a88c6 2019-08-03 stsp }
5017 036813ee 2019-05-09 stsp
5018 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5019 036813ee 2019-05-09 stsp continue;
5020 036813ee 2019-05-09 stsp
5021 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5022 036813ee 2019-05-09 stsp if (err)
5023 036813ee 2019-05-09 stsp return err;
5024 036813ee 2019-05-09 stsp if (!path_matches)
5025 036813ee 2019-05-09 stsp continue;
5026 036813ee 2019-05-09 stsp
5027 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5028 d34b633e 2020-10-19 stsp if (err)
5029 d34b633e 2020-10-19 stsp return err;
5030 d34b633e 2020-10-19 stsp
5031 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5032 d34b633e 2020-10-19 stsp free(ct_name);
5033 036813ee 2019-05-09 stsp continue;
5034 d34b633e 2020-10-19 stsp }
5035 d34b633e 2020-10-19 stsp free(ct_name);
5036 036813ee 2019-05-09 stsp
5037 036813ee 2019-05-09 stsp *ctp = ct;
5038 036813ee 2019-05-09 stsp break;
5039 036813ee 2019-05-09 stsp }
5040 2b496619 2019-07-10 stsp
5041 2b496619 2019-07-10 stsp return err;
5042 2b496619 2019-07-10 stsp }
5043 2b496619 2019-07-10 stsp
5044 2b496619 2019-07-10 stsp static const struct got_error *
5045 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5046 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5047 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5048 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5049 2b496619 2019-07-10 stsp struct got_repository *repo)
5050 2b496619 2019-07-10 stsp {
5051 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5052 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5053 2b496619 2019-07-10 stsp char *subtree_path;
5054 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5055 ba580f68 2020-03-22 stsp int nentries;
5056 2b496619 2019-07-10 stsp
5057 2b496619 2019-07-10 stsp *new_tep = NULL;
5058 2b496619 2019-07-10 stsp
5059 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5060 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5061 2b496619 2019-07-10 stsp child_path) == -1)
5062 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5063 036813ee 2019-05-09 stsp
5064 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5065 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5066 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5067 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5068 56e0773d 2019-11-28 stsp
5069 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5070 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5071 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5072 2b496619 2019-07-10 stsp goto done;
5073 2b496619 2019-07-10 stsp }
5074 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5075 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5076 2b496619 2019-07-10 stsp if (err) {
5077 56e0773d 2019-11-28 stsp free(new_te);
5078 2b496619 2019-07-10 stsp goto done;
5079 2b496619 2019-07-10 stsp }
5080 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5081 2b496619 2019-07-10 stsp done:
5082 56e0773d 2019-11-28 stsp free(id);
5083 2b496619 2019-07-10 stsp free(subtree_path);
5084 2b496619 2019-07-10 stsp if (err == NULL)
5085 2b496619 2019-07-10 stsp *new_tep = new_te;
5086 036813ee 2019-05-09 stsp return err;
5087 036813ee 2019-05-09 stsp }
5088 036813ee 2019-05-09 stsp
5089 036813ee 2019-05-09 stsp static const struct got_error *
5090 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5091 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5092 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5093 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5094 036813ee 2019-05-09 stsp struct got_repository *repo)
5095 036813ee 2019-05-09 stsp {
5096 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5097 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5098 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5099 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5100 036813ee 2019-05-09 stsp
5101 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5102 ba580f68 2020-03-22 stsp *nentries = 0;
5103 036813ee 2019-05-09 stsp
5104 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5105 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5106 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5107 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5108 036813ee 2019-05-09 stsp
5109 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5110 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5111 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5112 036813ee 2019-05-09 stsp continue;
5113 036813ee 2019-05-09 stsp
5114 f2b0a8b0 2020-07-31 stsp if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5115 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
5116 036813ee 2019-05-09 stsp continue;
5117 036813ee 2019-05-09 stsp
5118 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5119 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5120 036813ee 2019-05-09 stsp if (err)
5121 036813ee 2019-05-09 stsp goto done;
5122 036813ee 2019-05-09 stsp
5123 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5124 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5125 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5126 036813ee 2019-05-09 stsp if (err)
5127 036813ee 2019-05-09 stsp goto done;
5128 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5129 afa376bf 2019-05-09 stsp if (err)
5130 afa376bf 2019-05-09 stsp goto done;
5131 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5132 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5133 2b496619 2019-07-10 stsp if (err)
5134 2f51b5b3 2019-05-09 stsp goto done;
5135 ba580f68 2020-03-22 stsp (*nentries)++;
5136 2b496619 2019-07-10 stsp } else {
5137 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5138 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5139 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5140 2b496619 2019-07-10 stsp == NULL) {
5141 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5142 2b496619 2019-07-10 stsp child_path, path_base_tree,
5143 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5144 2b496619 2019-07-10 stsp repo);
5145 2b496619 2019-07-10 stsp if (err)
5146 2b496619 2019-07-10 stsp goto done;
5147 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5148 2b496619 2019-07-10 stsp if (err)
5149 2b496619 2019-07-10 stsp goto done;
5150 ba580f68 2020-03-22 stsp (*nentries)++;
5151 9ba0479c 2019-05-10 stsp }
5152 ed175427 2019-05-09 stsp }
5153 2f51b5b3 2019-05-09 stsp }
5154 2f51b5b3 2019-05-09 stsp
5155 2f51b5b3 2019-05-09 stsp if (base_tree) {
5156 56e0773d 2019-11-28 stsp int i, nbase_entries;
5157 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5158 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5159 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5160 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5161 63c5ca5d 2019-08-24 stsp
5162 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5163 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5164 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5165 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5166 63c5ca5d 2019-08-24 stsp if (err)
5167 63c5ca5d 2019-08-24 stsp goto done;
5168 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5169 63c5ca5d 2019-08-24 stsp if (err)
5170 63c5ca5d 2019-08-24 stsp goto done;
5171 ba580f68 2020-03-22 stsp (*nentries)++;
5172 63c5ca5d 2019-08-24 stsp continue;
5173 63c5ca5d 2019-08-24 stsp }
5174 2f51b5b3 2019-05-09 stsp
5175 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5176 44d03001 2019-05-09 stsp int modified;
5177 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5178 036813ee 2019-05-09 stsp if (err)
5179 036813ee 2019-05-09 stsp goto done;
5180 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5181 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5182 2f51b5b3 2019-05-09 stsp if (err)
5183 2f51b5b3 2019-05-09 stsp goto done;
5184 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5185 44d03001 2019-05-09 stsp if (modified) {
5186 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5187 ba580f68 2020-03-22 stsp int nsubentries;
5188 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5189 ba580f68 2020-03-22 stsp &nsubentries, te,
5190 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5191 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5192 44d03001 2019-05-09 stsp if (err)
5193 44d03001 2019-05-09 stsp goto done;
5194 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5195 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5196 ba580f68 2020-03-22 stsp free(new_id);
5197 ba580f68 2020-03-22 stsp continue;
5198 ba580f68 2020-03-22 stsp }
5199 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5200 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5201 56e0773d 2019-11-28 stsp free(new_id);
5202 44d03001 2019-05-09 stsp }
5203 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5204 036813ee 2019-05-09 stsp if (err)
5205 036813ee 2019-05-09 stsp goto done;
5206 ba580f68 2020-03-22 stsp (*nentries)++;
5207 2f51b5b3 2019-05-09 stsp continue;
5208 036813ee 2019-05-09 stsp }
5209 2f51b5b3 2019-05-09 stsp
5210 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5211 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5212 f66c734c 2019-09-22 stsp if (err)
5213 f66c734c 2019-09-22 stsp goto done;
5214 2f51b5b3 2019-05-09 stsp if (ct) {
5215 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5216 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5217 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5218 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5219 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5220 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5221 2f51b5b3 2019-05-09 stsp if (err)
5222 2f51b5b3 2019-05-09 stsp goto done;
5223 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5224 2f51b5b3 2019-05-09 stsp if (err)
5225 2f51b5b3 2019-05-09 stsp goto done;
5226 ba580f68 2020-03-22 stsp (*nentries)++;
5227 2f51b5b3 2019-05-09 stsp }
5228 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5229 b416585c 2019-05-13 stsp status_arg);
5230 afa376bf 2019-05-09 stsp if (err)
5231 afa376bf 2019-05-09 stsp goto done;
5232 2f51b5b3 2019-05-09 stsp } else {
5233 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5234 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5235 2f51b5b3 2019-05-09 stsp if (err)
5236 2f51b5b3 2019-05-09 stsp goto done;
5237 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5238 2f51b5b3 2019-05-09 stsp if (err)
5239 2f51b5b3 2019-05-09 stsp goto done;
5240 ba580f68 2020-03-22 stsp (*nentries)++;
5241 2f51b5b3 2019-05-09 stsp }
5242 ca2503ea 2019-05-09 stsp }
5243 ed175427 2019-05-09 stsp }
5244 0b5cc0d6 2019-05-09 stsp
5245 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5246 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5247 ed175427 2019-05-09 stsp done:
5248 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5249 2e1fa222 2020-07-23 stsp return err;
5250 2e1fa222 2020-07-23 stsp }
5251 2e1fa222 2020-07-23 stsp
5252 2e1fa222 2020-07-23 stsp static const struct got_error *
5253 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5254 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5255 72fd46fa 2019-09-06 stsp int have_staged_files)
5256 ebf99748 2019-05-09 stsp {
5257 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5258 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5259 ebf99748 2019-05-09 stsp
5260 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5261 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5262 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5263 ebf99748 2019-05-09 stsp
5264 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5265 ebf99748 2019-05-09 stsp if (ie) {
5266 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5267 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5268 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5269 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5270 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5271 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5272 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5273 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5274 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5275 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5276 72fd46fa 2019-09-06 stsp !have_staged_files);
5277 ebf99748 2019-05-09 stsp } else
5278 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5279 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5280 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5281 72fd46fa 2019-09-06 stsp !have_staged_files);
5282 ebf99748 2019-05-09 stsp } else {
5283 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5284 ebf99748 2019-05-09 stsp if (err)
5285 af12c6b9 2019-06-04 stsp break;
5286 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5287 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5288 3969253a 2020-03-07 stsp if (err) {
5289 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5290 3969253a 2020-03-07 stsp break;
5291 3969253a 2020-03-07 stsp }
5292 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5293 3969253a 2020-03-07 stsp if (err) {
5294 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5295 af12c6b9 2019-06-04 stsp break;
5296 3969253a 2020-03-07 stsp }
5297 ebf99748 2019-05-09 stsp }
5298 ebf99748 2019-05-09 stsp }
5299 d56d26ce 2019-05-10 stsp return err;
5300 d56d26ce 2019-05-10 stsp }
5301 735ef5ac 2019-08-03 stsp
5302 d56d26ce 2019-05-10 stsp
5303 d56d26ce 2019-05-10 stsp static const struct got_error *
5304 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5305 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5306 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5307 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5308 735ef5ac 2019-08-03 stsp int ood_errcode)
5309 d56d26ce 2019-05-10 stsp {
5310 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5311 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5312 1a36436d 2019-06-10 stsp
5313 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5314 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5315 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5316 1a36436d 2019-06-10 stsp return NULL;
5317 9bead371 2019-07-28 stsp /*
5318 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5319 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5320 9bead371 2019-07-28 stsp */
5321 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5322 735ef5ac 2019-08-03 stsp in_repo_path);
5323 9bead371 2019-07-28 stsp if (err) {
5324 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5325 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5326 1a36436d 2019-06-10 stsp goto done;
5327 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5328 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5329 1a36436d 2019-06-10 stsp } else {
5330 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5331 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5332 735ef5ac 2019-08-03 stsp in_repo_path);
5333 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5334 1a36436d 2019-06-10 stsp goto done;
5335 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5336 1a36436d 2019-06-10 stsp }
5337 1a36436d 2019-06-10 stsp done:
5338 1a36436d 2019-06-10 stsp free(id);
5339 1a36436d 2019-06-10 stsp return err;
5340 ebf99748 2019-05-09 stsp }
5341 ebf99748 2019-05-09 stsp
5342 c4296144 2019-05-09 stsp const struct got_error *
5343 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5344 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5345 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5346 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5347 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5348 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5349 afa376bf 2019-05-09 stsp struct got_repository *repo)
5350 c4296144 2019-05-09 stsp {
5351 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5352 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5353 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5354 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5355 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5356 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5357 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5358 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5359 ba580f68 2020-03-22 stsp int nentries;
5360 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5361 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5362 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5363 c4296144 2019-05-09 stsp
5364 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5365 c4296144 2019-05-09 stsp
5366 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5367 675c7539 2019-05-09 stsp
5368 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5369 588edf97 2019-05-10 stsp if (err)
5370 588edf97 2019-05-10 stsp goto done;
5371 de18fc63 2019-05-09 stsp
5372 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5373 588edf97 2019-05-10 stsp if (err)
5374 588edf97 2019-05-10 stsp goto done;
5375 588edf97 2019-05-10 stsp
5376 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5377 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5378 33ad4cbe 2019-05-12 jcs if (err)
5379 33ad4cbe 2019-05-12 jcs goto done;
5380 33ad4cbe 2019-05-12 jcs }
5381 c4296144 2019-05-09 stsp
5382 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5383 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5384 33ad4cbe 2019-05-12 jcs goto done;
5385 33ad4cbe 2019-05-12 jcs }
5386 33ad4cbe 2019-05-12 jcs
5387 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5388 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5389 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5390 cf066bf8 2019-05-09 stsp char *ondisk_path;
5391 cf066bf8 2019-05-09 stsp
5392 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5393 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5394 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5395 5f8a88c6 2019-08-03 stsp continue;
5396 5f8a88c6 2019-08-03 stsp
5397 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5398 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5399 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5400 cf066bf8 2019-05-09 stsp continue;
5401 cf066bf8 2019-05-09 stsp
5402 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5403 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5404 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5405 cf066bf8 2019-05-09 stsp goto done;
5406 cf066bf8 2019-05-09 stsp }
5407 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5408 2e1fa222 2020-07-23 stsp free(ondisk_path);
5409 2e1fa222 2020-07-23 stsp if (err)
5410 cf066bf8 2019-05-09 stsp goto done;
5411 cf066bf8 2019-05-09 stsp }
5412 036813ee 2019-05-09 stsp
5413 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5414 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5415 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5416 036813ee 2019-05-09 stsp if (err)
5417 036813ee 2019-05-09 stsp goto done;
5418 036813ee 2019-05-09 stsp
5419 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5420 de18fc63 2019-05-09 stsp if (err)
5421 de18fc63 2019-05-09 stsp goto done;
5422 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5423 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5424 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5425 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5426 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5427 33ad4cbe 2019-05-12 jcs free(logmsg);
5428 9d40349a 2019-05-09 stsp if (err)
5429 9d40349a 2019-05-09 stsp goto done;
5430 9d40349a 2019-05-09 stsp
5431 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5432 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5433 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5434 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5435 09f5bd90 2019-05-09 stsp goto done;
5436 09f5bd90 2019-05-09 stsp }
5437 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5438 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5439 09f5bd90 2019-05-09 stsp if (err)
5440 09f5bd90 2019-05-09 stsp goto done;
5441 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5442 ebf99748 2019-05-09 stsp if (err)
5443 ebf99748 2019-05-09 stsp goto done;
5444 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5445 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5446 09f5bd90 2019-05-09 stsp goto done;
5447 09f5bd90 2019-05-09 stsp }
5448 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5449 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5450 f2c16586 2019-05-09 stsp if (err)
5451 f2c16586 2019-05-09 stsp goto done;
5452 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5453 f2c16586 2019-05-09 stsp if (err)
5454 f2c16586 2019-05-09 stsp goto done;
5455 f2c16586 2019-05-09 stsp
5456 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5457 09f5bd90 2019-05-09 stsp if (err)
5458 09f5bd90 2019-05-09 stsp goto done;
5459 09f5bd90 2019-05-09 stsp
5460 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5461 ebf99748 2019-05-09 stsp if (err)
5462 ebf99748 2019-05-09 stsp goto done;
5463 c4296144 2019-05-09 stsp done:
5464 588edf97 2019-05-10 stsp if (head_tree)
5465 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5466 588edf97 2019-05-10 stsp if (head_commit)
5467 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5468 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5469 2f17228e 2019-05-12 stsp if (head_ref2) {
5470 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5471 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5472 2f17228e 2019-05-12 stsp err = unlockerr;
5473 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5474 39cd0ff6 2019-07-12 stsp }
5475 39cd0ff6 2019-07-12 stsp return err;
5476 39cd0ff6 2019-07-12 stsp }
5477 5c1e53bc 2019-07-28 stsp
5478 5c1e53bc 2019-07-28 stsp static const struct got_error *
5479 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5480 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5481 5c1e53bc 2019-07-28 stsp {
5482 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5483 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5484 39cd0ff6 2019-07-12 stsp
5485 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5486 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5487 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5488 5c1e53bc 2019-07-28 stsp
5489 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5490 5c1e53bc 2019-07-28 stsp ct_path++;
5491 5c1e53bc 2019-07-28 stsp
5492 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5493 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5494 5c1e53bc 2019-07-28 stsp break;
5495 5c1e53bc 2019-07-28 stsp }
5496 5c1e53bc 2019-07-28 stsp
5497 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5498 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5499 5c1e53bc 2019-07-28 stsp
5500 5c1e53bc 2019-07-28 stsp return NULL;
5501 5c1e53bc 2019-07-28 stsp }
5502 5c1e53bc 2019-07-28 stsp
5503 f0b75401 2019-08-03 stsp static const struct got_error *
5504 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5505 f0b75401 2019-08-03 stsp {
5506 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5507 f0b75401 2019-08-03 stsp
5508 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5509 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5510 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5511 f0b75401 2019-08-03 stsp }
5512 f0b75401 2019-08-03 stsp
5513 f0b75401 2019-08-03 stsp return NULL;
5514 f0b75401 2019-08-03 stsp }
5515 f0b75401 2019-08-03 stsp
5516 5f8a88c6 2019-08-03 stsp static const struct got_error *
5517 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5518 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5519 5f8a88c6 2019-08-03 stsp {
5520 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5521 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5522 5f8a88c6 2019-08-03 stsp
5523 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5524 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5525 5f8a88c6 2019-08-03 stsp continue;
5526 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5527 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5528 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5529 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5530 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5531 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5532 5f8a88c6 2019-08-03 stsp }
5533 5f8a88c6 2019-08-03 stsp
5534 5f8a88c6 2019-08-03 stsp return NULL;
5535 5f8a88c6 2019-08-03 stsp }
5536 5f8a88c6 2019-08-03 stsp
5537 39cd0ff6 2019-07-12 stsp const struct got_error *
5538 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5539 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5540 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5541 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5542 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5543 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5544 39cd0ff6 2019-07-12 stsp {
5545 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5546 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5547 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5548 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5549 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5550 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5551 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5552 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5553 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5554 39cd0ff6 2019-07-12 stsp
5555 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5556 39cd0ff6 2019-07-12 stsp
5557 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5558 39cd0ff6 2019-07-12 stsp
5559 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5560 39cd0ff6 2019-07-12 stsp if (err)
5561 39cd0ff6 2019-07-12 stsp goto done;
5562 39cd0ff6 2019-07-12 stsp
5563 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5564 39cd0ff6 2019-07-12 stsp if (err)
5565 39cd0ff6 2019-07-12 stsp goto done;
5566 39cd0ff6 2019-07-12 stsp
5567 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5568 39cd0ff6 2019-07-12 stsp if (err)
5569 39cd0ff6 2019-07-12 stsp goto done;
5570 39cd0ff6 2019-07-12 stsp
5571 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5572 39cd0ff6 2019-07-12 stsp if (err)
5573 39cd0ff6 2019-07-12 stsp goto done;
5574 39cd0ff6 2019-07-12 stsp
5575 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5576 5f8a88c6 2019-08-03 stsp &have_staged_files);
5577 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5578 5f8a88c6 2019-08-03 stsp goto done;
5579 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5580 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5581 5f8a88c6 2019-08-03 stsp if (err)
5582 5f8a88c6 2019-08-03 stsp goto done;
5583 5f8a88c6 2019-08-03 stsp }
5584 5f8a88c6 2019-08-03 stsp
5585 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5586 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5587 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5588 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5589 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5590 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5591 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5592 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5593 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5594 5c1e53bc 2019-07-28 stsp if (err)
5595 5c1e53bc 2019-07-28 stsp goto done;
5596 5c1e53bc 2019-07-28 stsp }
5597 39cd0ff6 2019-07-12 stsp
5598 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5599 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5600 39cd0ff6 2019-07-12 stsp goto done;
5601 5c1e53bc 2019-07-28 stsp }
5602 5c1e53bc 2019-07-28 stsp
5603 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5604 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5605 5c1e53bc 2019-07-28 stsp if (err)
5606 5c1e53bc 2019-07-28 stsp goto done;
5607 39cd0ff6 2019-07-12 stsp }
5608 f0b75401 2019-08-03 stsp
5609 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5610 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5611 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5612 f0b75401 2019-08-03 stsp
5613 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5614 f0b75401 2019-08-03 stsp ct_path++;
5615 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5616 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5617 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5618 b50cabdf 2019-07-12 stsp if (err)
5619 b50cabdf 2019-07-12 stsp goto done;
5620 f0b75401 2019-08-03 stsp
5621 b50cabdf 2019-07-12 stsp }
5622 b50cabdf 2019-07-12 stsp
5623 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5624 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5625 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5626 39cd0ff6 2019-07-12 stsp if (err)
5627 39cd0ff6 2019-07-12 stsp goto done;
5628 39cd0ff6 2019-07-12 stsp
5629 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5630 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5631 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5632 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5633 39cd0ff6 2019-07-12 stsp err = sync_err;
5634 39cd0ff6 2019-07-12 stsp done:
5635 39cd0ff6 2019-07-12 stsp if (fileindex)
5636 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5637 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5638 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5639 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5640 39cd0ff6 2019-07-12 stsp err = unlockerr;
5641 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5642 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5643 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5644 39cd0ff6 2019-07-12 stsp }
5645 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5646 c4296144 2019-05-09 stsp return err;
5647 8656d6c4 2019-05-20 stsp }
5648 8656d6c4 2019-05-20 stsp
5649 8656d6c4 2019-05-20 stsp const char *
5650 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5651 8656d6c4 2019-05-20 stsp {
5652 8656d6c4 2019-05-20 stsp return ct->path;
5653 8656d6c4 2019-05-20 stsp }
5654 8656d6c4 2019-05-20 stsp
5655 8656d6c4 2019-05-20 stsp unsigned int
5656 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5657 8656d6c4 2019-05-20 stsp {
5658 8656d6c4 2019-05-20 stsp return ct->status;
5659 818c7501 2019-07-11 stsp }
5660 818c7501 2019-07-11 stsp
5661 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5662 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5663 818c7501 2019-07-11 stsp struct got_repository *repo;
5664 818c7501 2019-07-11 stsp };
5665 818c7501 2019-07-11 stsp
5666 818c7501 2019-07-11 stsp static const struct got_error *
5667 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5668 818c7501 2019-07-11 stsp {
5669 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5670 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5671 818c7501 2019-07-11 stsp unsigned char status;
5672 818c7501 2019-07-11 stsp struct stat sb;
5673 818c7501 2019-07-11 stsp char *ondisk_path;
5674 818c7501 2019-07-11 stsp
5675 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5676 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5677 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5678 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5679 818c7501 2019-07-11 stsp
5680 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5681 818c7501 2019-07-11 stsp == -1)
5682 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5683 818c7501 2019-07-11 stsp
5684 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5685 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5686 818c7501 2019-07-11 stsp free(ondisk_path);
5687 818c7501 2019-07-11 stsp if (err)
5688 818c7501 2019-07-11 stsp return err;
5689 818c7501 2019-07-11 stsp
5690 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5691 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5692 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5693 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5694 818c7501 2019-07-11 stsp
5695 818c7501 2019-07-11 stsp return NULL;
5696 818c7501 2019-07-11 stsp }
5697 818c7501 2019-07-11 stsp
5698 818c7501 2019-07-11 stsp const struct got_error *
5699 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5700 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5701 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5702 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5703 818c7501 2019-07-11 stsp {
5704 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5705 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5706 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5707 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5708 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5709 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5710 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5711 818c7501 2019-07-11 stsp
5712 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5713 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5714 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5715 818c7501 2019-07-11 stsp
5716 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5717 818c7501 2019-07-11 stsp if (err)
5718 818c7501 2019-07-11 stsp return err;
5719 818c7501 2019-07-11 stsp
5720 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5721 818c7501 2019-07-11 stsp if (err)
5722 818c7501 2019-07-11 stsp goto done;
5723 818c7501 2019-07-11 stsp
5724 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5725 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5726 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5727 818c7501 2019-07-11 stsp &ok_arg);
5728 818c7501 2019-07-11 stsp if (err)
5729 818c7501 2019-07-11 stsp goto done;
5730 818c7501 2019-07-11 stsp
5731 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5732 818c7501 2019-07-11 stsp if (err)
5733 818c7501 2019-07-11 stsp goto done;
5734 818c7501 2019-07-11 stsp
5735 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5736 818c7501 2019-07-11 stsp if (err)
5737 818c7501 2019-07-11 stsp goto done;
5738 818c7501 2019-07-11 stsp
5739 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5740 818c7501 2019-07-11 stsp if (err)
5741 818c7501 2019-07-11 stsp goto done;
5742 818c7501 2019-07-11 stsp
5743 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5744 818c7501 2019-07-11 stsp 0);
5745 e51d7b55 2020-01-04 stsp if (err)
5746 e51d7b55 2020-01-04 stsp goto done;
5747 e51d7b55 2020-01-04 stsp
5748 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5749 818c7501 2019-07-11 stsp if (err)
5750 818c7501 2019-07-11 stsp goto done;
5751 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5752 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5753 e51d7b55 2020-01-04 stsp goto done;
5754 e51d7b55 2020-01-04 stsp }
5755 818c7501 2019-07-11 stsp
5756 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5757 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5758 818c7501 2019-07-11 stsp if (err)
5759 818c7501 2019-07-11 stsp goto done;
5760 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5761 818c7501 2019-07-11 stsp if (err)
5762 818c7501 2019-07-11 stsp goto done;
5763 818c7501 2019-07-11 stsp
5764 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5765 818c7501 2019-07-11 stsp
5766 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5767 818c7501 2019-07-11 stsp if (err)
5768 818c7501 2019-07-11 stsp goto done;
5769 818c7501 2019-07-11 stsp
5770 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5771 818c7501 2019-07-11 stsp if (err)
5772 818c7501 2019-07-11 stsp goto done;
5773 818c7501 2019-07-11 stsp
5774 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5775 818c7501 2019-07-11 stsp worktree->base_commit_id);
5776 818c7501 2019-07-11 stsp if (err)
5777 818c7501 2019-07-11 stsp goto done;
5778 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5779 818c7501 2019-07-11 stsp if (err)
5780 818c7501 2019-07-11 stsp goto done;
5781 818c7501 2019-07-11 stsp
5782 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5783 818c7501 2019-07-11 stsp if (err)
5784 818c7501 2019-07-11 stsp goto done;
5785 818c7501 2019-07-11 stsp done:
5786 818c7501 2019-07-11 stsp free(fileindex_path);
5787 818c7501 2019-07-11 stsp free(tmp_branch_name);
5788 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5789 818c7501 2019-07-11 stsp free(branch_ref_name);
5790 818c7501 2019-07-11 stsp if (branch_ref)
5791 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5792 818c7501 2019-07-11 stsp if (wt_branch)
5793 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5794 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5795 818c7501 2019-07-11 stsp if (err) {
5796 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5797 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5798 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5799 818c7501 2019-07-11 stsp }
5800 818c7501 2019-07-11 stsp if (*tmp_branch) {
5801 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5802 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5803 818c7501 2019-07-11 stsp }
5804 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5805 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5806 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5807 3e3a69f1 2019-07-25 stsp }
5808 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5809 818c7501 2019-07-11 stsp }
5810 818c7501 2019-07-11 stsp return err;
5811 818c7501 2019-07-11 stsp }
5812 818c7501 2019-07-11 stsp
5813 818c7501 2019-07-11 stsp const struct got_error *
5814 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5815 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5816 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5817 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5818 818c7501 2019-07-11 stsp {
5819 818c7501 2019-07-11 stsp const struct got_error *err;
5820 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5821 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5822 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5823 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5824 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5825 818c7501 2019-07-11 stsp
5826 818c7501 2019-07-11 stsp *commit_id = NULL;
5827 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5828 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5829 3e3a69f1 2019-07-25 stsp *branch = NULL;
5830 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5831 3e3a69f1 2019-07-25 stsp
5832 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5833 3e3a69f1 2019-07-25 stsp if (err)
5834 3e3a69f1 2019-07-25 stsp return err;
5835 818c7501 2019-07-11 stsp
5836 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5837 3e3a69f1 2019-07-25 stsp if (err)
5838 f032f1f7 2019-08-04 stsp goto done;
5839 f032f1f7 2019-08-04 stsp
5840 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5841 f032f1f7 2019-08-04 stsp &have_staged_files);
5842 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5843 f032f1f7 2019-08-04 stsp goto done;
5844 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5845 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5846 3e3a69f1 2019-07-25 stsp goto done;
5847 f032f1f7 2019-08-04 stsp }
5848 3e3a69f1 2019-07-25 stsp
5849 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5850 818c7501 2019-07-11 stsp if (err)
5851 f032f1f7 2019-08-04 stsp goto done;
5852 818c7501 2019-07-11 stsp
5853 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5854 818c7501 2019-07-11 stsp if (err)
5855 818c7501 2019-07-11 stsp goto done;
5856 818c7501 2019-07-11 stsp
5857 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5858 818c7501 2019-07-11 stsp if (err)
5859 818c7501 2019-07-11 stsp goto done;
5860 818c7501 2019-07-11 stsp
5861 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5862 818c7501 2019-07-11 stsp if (err)
5863 818c7501 2019-07-11 stsp goto done;
5864 818c7501 2019-07-11 stsp
5865 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5866 818c7501 2019-07-11 stsp if (err)
5867 818c7501 2019-07-11 stsp goto done;
5868 818c7501 2019-07-11 stsp
5869 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5870 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5871 818c7501 2019-07-11 stsp if (err)
5872 818c7501 2019-07-11 stsp goto done;
5873 818c7501 2019-07-11 stsp
5874 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5875 818c7501 2019-07-11 stsp if (err)
5876 818c7501 2019-07-11 stsp goto done;
5877 818c7501 2019-07-11 stsp
5878 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5879 818c7501 2019-07-11 stsp if (err)
5880 818c7501 2019-07-11 stsp goto done;
5881 818c7501 2019-07-11 stsp
5882 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5883 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5884 818c7501 2019-07-11 stsp if (err)
5885 818c7501 2019-07-11 stsp goto done;
5886 818c7501 2019-07-11 stsp
5887 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5888 818c7501 2019-07-11 stsp if (err)
5889 818c7501 2019-07-11 stsp goto done;
5890 818c7501 2019-07-11 stsp done:
5891 818c7501 2019-07-11 stsp free(commit_ref_name);
5892 818c7501 2019-07-11 stsp free(branch_ref_name);
5893 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5894 818c7501 2019-07-11 stsp if (commit_ref)
5895 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5896 818c7501 2019-07-11 stsp if (branch_ref)
5897 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5898 818c7501 2019-07-11 stsp if (err) {
5899 818c7501 2019-07-11 stsp free(*commit_id);
5900 818c7501 2019-07-11 stsp *commit_id = NULL;
5901 818c7501 2019-07-11 stsp if (*tmp_branch) {
5902 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5903 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5904 818c7501 2019-07-11 stsp }
5905 818c7501 2019-07-11 stsp if (*new_base_branch) {
5906 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5907 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5908 818c7501 2019-07-11 stsp }
5909 818c7501 2019-07-11 stsp if (*branch) {
5910 818c7501 2019-07-11 stsp got_ref_close(*branch);
5911 818c7501 2019-07-11 stsp *branch = NULL;
5912 818c7501 2019-07-11 stsp }
5913 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5914 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5915 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5916 3e3a69f1 2019-07-25 stsp }
5917 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5918 818c7501 2019-07-11 stsp }
5919 818c7501 2019-07-11 stsp return err;
5920 c4296144 2019-05-09 stsp }
5921 818c7501 2019-07-11 stsp
5922 818c7501 2019-07-11 stsp const struct got_error *
5923 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5924 818c7501 2019-07-11 stsp {
5925 818c7501 2019-07-11 stsp const struct got_error *err;
5926 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5927 818c7501 2019-07-11 stsp
5928 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5929 818c7501 2019-07-11 stsp if (err)
5930 818c7501 2019-07-11 stsp return err;
5931 818c7501 2019-07-11 stsp
5932 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5933 818c7501 2019-07-11 stsp free(tmp_branch_name);
5934 818c7501 2019-07-11 stsp return NULL;
5935 818c7501 2019-07-11 stsp }
5936 818c7501 2019-07-11 stsp
5937 818c7501 2019-07-11 stsp static const struct got_error *
5938 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5939 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5940 818c7501 2019-07-11 stsp {
5941 0ebf8283 2019-07-24 stsp *logmsg = arg;
5942 818c7501 2019-07-11 stsp return NULL;
5943 818c7501 2019-07-11 stsp }
5944 818c7501 2019-07-11 stsp
5945 818c7501 2019-07-11 stsp static const struct got_error *
5946 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5947 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5948 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5949 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5950 818c7501 2019-07-11 stsp {
5951 818c7501 2019-07-11 stsp return NULL;
5952 01757395 2019-07-12 stsp }
5953 01757395 2019-07-12 stsp
5954 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5955 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5956 01757395 2019-07-12 stsp void *progress_arg;
5957 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5958 01757395 2019-07-12 stsp };
5959 01757395 2019-07-12 stsp
5960 01757395 2019-07-12 stsp static const struct got_error *
5961 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5962 01757395 2019-07-12 stsp {
5963 01757395 2019-07-12 stsp const struct got_error *err;
5964 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5965 01757395 2019-07-12 stsp char *p;
5966 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5967 01757395 2019-07-12 stsp
5968 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5969 01757395 2019-07-12 stsp if (err)
5970 01757395 2019-07-12 stsp return err;
5971 01757395 2019-07-12 stsp
5972 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5973 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5974 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5975 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5976 01757395 2019-07-12 stsp return NULL;
5977 01757395 2019-07-12 stsp
5978 01757395 2019-07-12 stsp p = strdup(path);
5979 01757395 2019-07-12 stsp if (p == NULL)
5980 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5981 01757395 2019-07-12 stsp
5982 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5983 01757395 2019-07-12 stsp if (err || new == NULL)
5984 01757395 2019-07-12 stsp free(p);
5985 01757395 2019-07-12 stsp return err;
5986 01757395 2019-07-12 stsp }
5987 01757395 2019-07-12 stsp
5988 01757395 2019-07-12 stsp void
5989 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5990 01757395 2019-07-12 stsp {
5991 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5992 01757395 2019-07-12 stsp
5993 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5994 01757395 2019-07-12 stsp free((char *)pe->path);
5995 01757395 2019-07-12 stsp
5996 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5997 818c7501 2019-07-11 stsp }
5998 818c7501 2019-07-11 stsp
5999 0ebf8283 2019-07-24 stsp static const struct got_error *
6000 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6001 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6002 818c7501 2019-07-11 stsp {
6003 818c7501 2019-07-11 stsp const struct got_error *err;
6004 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6005 818c7501 2019-07-11 stsp
6006 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6007 818c7501 2019-07-11 stsp if (err) {
6008 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6009 818c7501 2019-07-11 stsp goto done;
6010 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6011 818c7501 2019-07-11 stsp if (err)
6012 818c7501 2019-07-11 stsp goto done;
6013 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6014 818c7501 2019-07-11 stsp if (err)
6015 818c7501 2019-07-11 stsp goto done;
6016 de05890f 2020-03-05 stsp } else if (is_rebase) {
6017 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6018 818c7501 2019-07-11 stsp int cmp;
6019 818c7501 2019-07-11 stsp
6020 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6021 818c7501 2019-07-11 stsp if (err)
6022 818c7501 2019-07-11 stsp goto done;
6023 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6024 818c7501 2019-07-11 stsp free(stored_id);
6025 818c7501 2019-07-11 stsp if (cmp != 0) {
6026 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6027 818c7501 2019-07-11 stsp goto done;
6028 818c7501 2019-07-11 stsp }
6029 818c7501 2019-07-11 stsp }
6030 0ebf8283 2019-07-24 stsp done:
6031 0ebf8283 2019-07-24 stsp if (commit_ref)
6032 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6033 0ebf8283 2019-07-24 stsp return err;
6034 0ebf8283 2019-07-24 stsp }
6035 0ebf8283 2019-07-24 stsp
6036 0ebf8283 2019-07-24 stsp static const struct got_error *
6037 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6038 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6039 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6040 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6041 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6042 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6043 0ebf8283 2019-07-24 stsp {
6044 0ebf8283 2019-07-24 stsp const struct got_error *err;
6045 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6046 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6047 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6048 818c7501 2019-07-11 stsp
6049 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6050 0ebf8283 2019-07-24 stsp
6051 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6052 0ebf8283 2019-07-24 stsp if (err)
6053 0ebf8283 2019-07-24 stsp return err;
6054 0ebf8283 2019-07-24 stsp
6055 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6056 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6057 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6058 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6059 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6060 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6061 818c7501 2019-07-11 stsp if (commit_ref)
6062 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6063 818c7501 2019-07-11 stsp return err;
6064 818c7501 2019-07-11 stsp }
6065 818c7501 2019-07-11 stsp
6066 818c7501 2019-07-11 stsp const struct got_error *
6067 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6068 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6069 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6070 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6071 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6072 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6073 818c7501 2019-07-11 stsp {
6074 0ebf8283 2019-07-24 stsp const struct got_error *err;
6075 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6076 0ebf8283 2019-07-24 stsp
6077 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6078 0ebf8283 2019-07-24 stsp if (err)
6079 0ebf8283 2019-07-24 stsp return err;
6080 0ebf8283 2019-07-24 stsp
6081 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6082 0ebf8283 2019-07-24 stsp if (err)
6083 0ebf8283 2019-07-24 stsp goto done;
6084 0ebf8283 2019-07-24 stsp
6085 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6086 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6087 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6088 0ebf8283 2019-07-24 stsp done:
6089 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6090 0ebf8283 2019-07-24 stsp return err;
6091 0ebf8283 2019-07-24 stsp }
6092 0ebf8283 2019-07-24 stsp
6093 0ebf8283 2019-07-24 stsp const struct got_error *
6094 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6095 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6096 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6097 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6098 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6099 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6100 0ebf8283 2019-07-24 stsp {
6101 0ebf8283 2019-07-24 stsp const struct got_error *err;
6102 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6103 0ebf8283 2019-07-24 stsp
6104 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6105 0ebf8283 2019-07-24 stsp if (err)
6106 0ebf8283 2019-07-24 stsp return err;
6107 0ebf8283 2019-07-24 stsp
6108 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6109 0ebf8283 2019-07-24 stsp if (err)
6110 0ebf8283 2019-07-24 stsp goto done;
6111 0ebf8283 2019-07-24 stsp
6112 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6113 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6114 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6115 0ebf8283 2019-07-24 stsp done:
6116 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6117 0ebf8283 2019-07-24 stsp return err;
6118 0ebf8283 2019-07-24 stsp }
6119 0ebf8283 2019-07-24 stsp
6120 0ebf8283 2019-07-24 stsp static const struct got_error *
6121 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6122 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6123 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6124 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6125 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6126 0ebf8283 2019-07-24 stsp {
6127 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6128 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6129 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6130 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6131 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6132 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6133 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6134 818c7501 2019-07-11 stsp
6135 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6136 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6137 a0e95631 2019-07-12 stsp
6138 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6139 818c7501 2019-07-11 stsp
6140 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6141 a0e95631 2019-07-12 stsp if (err)
6142 3e3a69f1 2019-07-25 stsp return err;
6143 a0e95631 2019-07-12 stsp
6144 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6145 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6146 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6147 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6148 01757395 2019-07-12 stsp /*
6149 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6150 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6151 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6152 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6153 01757395 2019-07-12 stsp */
6154 01757395 2019-07-12 stsp if (merged_paths) {
6155 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6156 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6157 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6158 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6159 f2a9dc41 2019-12-13 tracey 0);
6160 01757395 2019-07-12 stsp if (err)
6161 01757395 2019-07-12 stsp goto done;
6162 01757395 2019-07-12 stsp }
6163 01757395 2019-07-12 stsp } else {
6164 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6165 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6166 01757395 2019-07-12 stsp if (err)
6167 01757395 2019-07-12 stsp goto done;
6168 01757395 2019-07-12 stsp }
6169 a0e95631 2019-07-12 stsp
6170 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6171 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6172 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6173 a0e95631 2019-07-12 stsp if (err)
6174 a0e95631 2019-07-12 stsp goto done;
6175 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6176 a0e95631 2019-07-12 stsp goto done;
6177 ff0d2220 2019-07-11 stsp }
6178 818c7501 2019-07-11 stsp
6179 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6180 edd02c5e 2019-07-11 stsp if (err)
6181 edd02c5e 2019-07-11 stsp goto done;
6182 edd02c5e 2019-07-11 stsp
6183 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6184 818c7501 2019-07-11 stsp if (err)
6185 818c7501 2019-07-11 stsp goto done;
6186 818c7501 2019-07-11 stsp
6187 5943eee2 2019-08-13 stsp if (new_logmsg) {
6188 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6189 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6190 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6191 5943eee2 2019-08-13 stsp goto done;
6192 5943eee2 2019-08-13 stsp }
6193 5943eee2 2019-08-13 stsp } else {
6194 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6195 5943eee2 2019-08-13 stsp if (err)
6196 5943eee2 2019-08-13 stsp goto done;
6197 5943eee2 2019-08-13 stsp }
6198 0ebf8283 2019-07-24 stsp
6199 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6200 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6201 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6202 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6203 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6204 a0e95631 2019-07-12 stsp if (err)
6205 a0e95631 2019-07-12 stsp goto done;
6206 a0e95631 2019-07-12 stsp
6207 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6208 a0e95631 2019-07-12 stsp if (err)
6209 a0e95631 2019-07-12 stsp goto done;
6210 a0e95631 2019-07-12 stsp
6211 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6212 a0e95631 2019-07-12 stsp if (err)
6213 a0e95631 2019-07-12 stsp goto done;
6214 a0e95631 2019-07-12 stsp
6215 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6216 72fd46fa 2019-09-06 stsp fileindex, 0);
6217 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6218 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6219 a0e95631 2019-07-12 stsp err = sync_err;
6220 818c7501 2019-07-11 stsp done:
6221 a0e95631 2019-07-12 stsp free(fileindex_path);
6222 a0e95631 2019-07-12 stsp free(head_commit_id);
6223 a0e95631 2019-07-12 stsp if (head_ref)
6224 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6225 818c7501 2019-07-11 stsp if (err) {
6226 818c7501 2019-07-11 stsp free(*new_commit_id);
6227 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6228 818c7501 2019-07-11 stsp }
6229 818c7501 2019-07-11 stsp return err;
6230 818c7501 2019-07-11 stsp }
6231 818c7501 2019-07-11 stsp
6232 818c7501 2019-07-11 stsp const struct got_error *
6233 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6234 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6235 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6236 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6237 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6238 0ebf8283 2019-07-24 stsp {
6239 0ebf8283 2019-07-24 stsp const struct got_error *err;
6240 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6241 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6242 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6243 0ebf8283 2019-07-24 stsp
6244 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6245 0ebf8283 2019-07-24 stsp if (err)
6246 0ebf8283 2019-07-24 stsp return err;
6247 0ebf8283 2019-07-24 stsp
6248 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6249 0ebf8283 2019-07-24 stsp if (err)
6250 0ebf8283 2019-07-24 stsp goto done;
6251 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6252 0ebf8283 2019-07-24 stsp if (err)
6253 0ebf8283 2019-07-24 stsp goto done;
6254 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6255 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6256 0ebf8283 2019-07-24 stsp goto done;
6257 0ebf8283 2019-07-24 stsp }
6258 0ebf8283 2019-07-24 stsp
6259 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6260 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6261 0ebf8283 2019-07-24 stsp done:
6262 0ebf8283 2019-07-24 stsp if (commit_ref)
6263 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6264 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6265 0ebf8283 2019-07-24 stsp free(commit_id);
6266 0ebf8283 2019-07-24 stsp return err;
6267 0ebf8283 2019-07-24 stsp }
6268 0ebf8283 2019-07-24 stsp
6269 0ebf8283 2019-07-24 stsp const struct got_error *
6270 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6271 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6272 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6273 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6274 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6275 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6276 0ebf8283 2019-07-24 stsp {
6277 0ebf8283 2019-07-24 stsp const struct got_error *err;
6278 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6279 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6280 0ebf8283 2019-07-24 stsp
6281 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6282 0ebf8283 2019-07-24 stsp if (err)
6283 0ebf8283 2019-07-24 stsp return err;
6284 0ebf8283 2019-07-24 stsp
6285 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6286 0ebf8283 2019-07-24 stsp if (err)
6287 0ebf8283 2019-07-24 stsp goto done;
6288 0ebf8283 2019-07-24 stsp
6289 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6290 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6291 0ebf8283 2019-07-24 stsp done:
6292 0ebf8283 2019-07-24 stsp if (commit_ref)
6293 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6294 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6295 0ebf8283 2019-07-24 stsp return err;
6296 0ebf8283 2019-07-24 stsp }
6297 0ebf8283 2019-07-24 stsp
6298 0ebf8283 2019-07-24 stsp const struct got_error *
6299 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6300 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6301 818c7501 2019-07-11 stsp {
6302 3e3a69f1 2019-07-25 stsp if (fileindex)
6303 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6304 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6305 69844fba 2019-07-11 stsp }
6306 69844fba 2019-07-11 stsp
6307 69844fba 2019-07-11 stsp static const struct got_error *
6308 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6309 69844fba 2019-07-11 stsp {
6310 69844fba 2019-07-11 stsp const struct got_error *err;
6311 69844fba 2019-07-11 stsp struct got_reference *ref;
6312 69844fba 2019-07-11 stsp
6313 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6314 69844fba 2019-07-11 stsp if (err) {
6315 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6316 69844fba 2019-07-11 stsp return NULL;
6317 69844fba 2019-07-11 stsp return err;
6318 69844fba 2019-07-11 stsp }
6319 69844fba 2019-07-11 stsp
6320 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6321 69844fba 2019-07-11 stsp got_ref_close(ref);
6322 69844fba 2019-07-11 stsp return err;
6323 818c7501 2019-07-11 stsp }
6324 818c7501 2019-07-11 stsp
6325 69844fba 2019-07-11 stsp static const struct got_error *
6326 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6327 69844fba 2019-07-11 stsp {
6328 69844fba 2019-07-11 stsp const struct got_error *err;
6329 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6330 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6331 69844fba 2019-07-11 stsp
6332 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6333 69844fba 2019-07-11 stsp if (err)
6334 69844fba 2019-07-11 stsp goto done;
6335 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6336 69844fba 2019-07-11 stsp if (err)
6337 69844fba 2019-07-11 stsp goto done;
6338 69844fba 2019-07-11 stsp
6339 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6340 69844fba 2019-07-11 stsp if (err)
6341 69844fba 2019-07-11 stsp goto done;
6342 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6343 69844fba 2019-07-11 stsp if (err)
6344 69844fba 2019-07-11 stsp goto done;
6345 69844fba 2019-07-11 stsp
6346 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6347 69844fba 2019-07-11 stsp if (err)
6348 69844fba 2019-07-11 stsp goto done;
6349 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6350 69844fba 2019-07-11 stsp if (err)
6351 69844fba 2019-07-11 stsp goto done;
6352 69844fba 2019-07-11 stsp
6353 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6354 69844fba 2019-07-11 stsp if (err)
6355 69844fba 2019-07-11 stsp goto done;
6356 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6357 69844fba 2019-07-11 stsp if (err)
6358 69844fba 2019-07-11 stsp goto done;
6359 69844fba 2019-07-11 stsp
6360 69844fba 2019-07-11 stsp done:
6361 69844fba 2019-07-11 stsp free(tmp_branch_name);
6362 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6363 69844fba 2019-07-11 stsp free(branch_ref_name);
6364 69844fba 2019-07-11 stsp free(commit_ref_name);
6365 69844fba 2019-07-11 stsp return err;
6366 69844fba 2019-07-11 stsp }
6367 69844fba 2019-07-11 stsp
6368 818c7501 2019-07-11 stsp const struct got_error *
6369 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6370 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6371 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6372 818c7501 2019-07-11 stsp struct got_repository *repo)
6373 818c7501 2019-07-11 stsp {
6374 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6375 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6376 818c7501 2019-07-11 stsp
6377 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6378 818c7501 2019-07-11 stsp if (err)
6379 818c7501 2019-07-11 stsp return err;
6380 818c7501 2019-07-11 stsp
6381 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6382 818c7501 2019-07-11 stsp if (err)
6383 818c7501 2019-07-11 stsp goto done;
6384 818c7501 2019-07-11 stsp
6385 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6386 818c7501 2019-07-11 stsp if (err)
6387 818c7501 2019-07-11 stsp goto done;
6388 818c7501 2019-07-11 stsp
6389 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6390 818c7501 2019-07-11 stsp if (err)
6391 818c7501 2019-07-11 stsp goto done;
6392 818c7501 2019-07-11 stsp
6393 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6394 818c7501 2019-07-11 stsp done:
6395 3e3a69f1 2019-07-25 stsp if (fileindex)
6396 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6397 818c7501 2019-07-11 stsp free(new_head_commit_id);
6398 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6399 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6400 818c7501 2019-07-11 stsp err = unlockerr;
6401 818c7501 2019-07-11 stsp return err;
6402 818c7501 2019-07-11 stsp }
6403 818c7501 2019-07-11 stsp
6404 818c7501 2019-07-11 stsp const struct got_error *
6405 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6406 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6407 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6408 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6409 818c7501 2019-07-11 stsp {
6410 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6411 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6412 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6413 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6414 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6415 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6416 818c7501 2019-07-11 stsp
6417 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6418 818c7501 2019-07-11 stsp if (err)
6419 818c7501 2019-07-11 stsp return err;
6420 818c7501 2019-07-11 stsp
6421 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6422 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6423 818c7501 2019-07-11 stsp if (err)
6424 818c7501 2019-07-11 stsp goto done;
6425 818c7501 2019-07-11 stsp
6426 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6427 818c7501 2019-07-11 stsp if (err)
6428 818c7501 2019-07-11 stsp goto done;
6429 818c7501 2019-07-11 stsp
6430 818c7501 2019-07-11 stsp /*
6431 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6432 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6433 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6434 818c7501 2019-07-11 stsp */
6435 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6436 818c7501 2019-07-11 stsp if (err)
6437 818c7501 2019-07-11 stsp goto done;
6438 818c7501 2019-07-11 stsp
6439 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6440 818c7501 2019-07-11 stsp if (err)
6441 818c7501 2019-07-11 stsp goto done;
6442 818c7501 2019-07-11 stsp
6443 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6444 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6445 ca355955 2019-07-12 stsp if (err)
6446 ca355955 2019-07-12 stsp goto done;
6447 ca355955 2019-07-12 stsp
6448 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6449 ca355955 2019-07-12 stsp if (err)
6450 ca355955 2019-07-12 stsp goto done;
6451 ca355955 2019-07-12 stsp
6452 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6453 818c7501 2019-07-11 stsp if (err)
6454 818c7501 2019-07-11 stsp goto done;
6455 818c7501 2019-07-11 stsp
6456 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6457 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6458 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6459 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6460 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6461 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6462 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6463 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6464 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6465 55bd499d 2019-07-12 stsp if (err)
6466 1f1abb7e 2019-08-08 stsp goto sync;
6467 55bd499d 2019-07-12 stsp
6468 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6469 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6470 ca355955 2019-07-12 stsp sync:
6471 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6472 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6473 ca355955 2019-07-12 stsp err = sync_err;
6474 818c7501 2019-07-11 stsp done:
6475 818c7501 2019-07-11 stsp got_ref_close(resolved);
6476 a3a2faf2 2019-07-12 stsp free(tree_id);
6477 818c7501 2019-07-11 stsp free(commit_id);
6478 0ebf8283 2019-07-24 stsp if (fileindex)
6479 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6480 0ebf8283 2019-07-24 stsp free(fileindex_path);
6481 0ebf8283 2019-07-24 stsp
6482 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6483 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6484 0ebf8283 2019-07-24 stsp err = unlockerr;
6485 0ebf8283 2019-07-24 stsp return err;
6486 0ebf8283 2019-07-24 stsp }
6487 0ebf8283 2019-07-24 stsp
6488 0ebf8283 2019-07-24 stsp const struct got_error *
6489 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6490 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6491 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6492 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6493 0ebf8283 2019-07-24 stsp {
6494 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6495 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6496 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6497 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6498 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6499 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6500 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6501 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6502 0ebf8283 2019-07-24 stsp
6503 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6504 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6505 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6506 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6507 0ebf8283 2019-07-24 stsp
6508 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6509 0ebf8283 2019-07-24 stsp if (err)
6510 0ebf8283 2019-07-24 stsp return err;
6511 0ebf8283 2019-07-24 stsp
6512 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6513 0ebf8283 2019-07-24 stsp if (err)
6514 0ebf8283 2019-07-24 stsp goto done;
6515 0ebf8283 2019-07-24 stsp
6516 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6517 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6518 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6519 0ebf8283 2019-07-24 stsp &ok_arg);
6520 0ebf8283 2019-07-24 stsp if (err)
6521 0ebf8283 2019-07-24 stsp goto done;
6522 0ebf8283 2019-07-24 stsp
6523 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6524 0ebf8283 2019-07-24 stsp if (err)
6525 0ebf8283 2019-07-24 stsp goto done;
6526 0ebf8283 2019-07-24 stsp
6527 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6528 0ebf8283 2019-07-24 stsp if (err)
6529 0ebf8283 2019-07-24 stsp goto done;
6530 0ebf8283 2019-07-24 stsp
6531 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6532 0ebf8283 2019-07-24 stsp worktree);
6533 0ebf8283 2019-07-24 stsp if (err)
6534 0ebf8283 2019-07-24 stsp goto done;
6535 0ebf8283 2019-07-24 stsp
6536 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6537 0ebf8283 2019-07-24 stsp 0);
6538 0ebf8283 2019-07-24 stsp if (err)
6539 0ebf8283 2019-07-24 stsp goto done;
6540 0ebf8283 2019-07-24 stsp
6541 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6542 0ebf8283 2019-07-24 stsp if (err)
6543 0ebf8283 2019-07-24 stsp goto done;
6544 0ebf8283 2019-07-24 stsp
6545 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6546 0ebf8283 2019-07-24 stsp if (err)
6547 0ebf8283 2019-07-24 stsp goto done;
6548 0ebf8283 2019-07-24 stsp
6549 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6550 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6551 0ebf8283 2019-07-24 stsp if (err)
6552 0ebf8283 2019-07-24 stsp goto done;
6553 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6554 0ebf8283 2019-07-24 stsp if (err)
6555 0ebf8283 2019-07-24 stsp goto done;
6556 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6557 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6558 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6559 0ebf8283 2019-07-24 stsp goto done;
6560 0ebf8283 2019-07-24 stsp }
6561 0ebf8283 2019-07-24 stsp
6562 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6563 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6564 0ebf8283 2019-07-24 stsp if (err)
6565 0ebf8283 2019-07-24 stsp goto done;
6566 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6567 0ebf8283 2019-07-24 stsp if (err)
6568 0ebf8283 2019-07-24 stsp goto done;
6569 0ebf8283 2019-07-24 stsp
6570 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6571 0ebf8283 2019-07-24 stsp if (err)
6572 0ebf8283 2019-07-24 stsp goto done;
6573 0ebf8283 2019-07-24 stsp done:
6574 0ebf8283 2019-07-24 stsp free(fileindex_path);
6575 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6576 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6577 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6578 0ebf8283 2019-07-24 stsp if (wt_branch)
6579 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6580 0ebf8283 2019-07-24 stsp if (err) {
6581 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6582 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6583 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6584 0ebf8283 2019-07-24 stsp }
6585 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6586 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6587 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6588 0ebf8283 2019-07-24 stsp }
6589 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6590 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6591 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6592 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6593 3e3a69f1 2019-07-25 stsp }
6594 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6595 0ebf8283 2019-07-24 stsp }
6596 0ebf8283 2019-07-24 stsp return err;
6597 0ebf8283 2019-07-24 stsp }
6598 0ebf8283 2019-07-24 stsp
6599 0ebf8283 2019-07-24 stsp const struct got_error *
6600 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6601 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6602 0ebf8283 2019-07-24 stsp {
6603 3e3a69f1 2019-07-25 stsp if (fileindex)
6604 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6605 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6606 0ebf8283 2019-07-24 stsp }
6607 0ebf8283 2019-07-24 stsp
6608 0ebf8283 2019-07-24 stsp const struct got_error *
6609 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6610 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6611 0ebf8283 2019-07-24 stsp {
6612 0ebf8283 2019-07-24 stsp const struct got_error *err;
6613 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6614 0ebf8283 2019-07-24 stsp
6615 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6616 0ebf8283 2019-07-24 stsp if (err)
6617 0ebf8283 2019-07-24 stsp return err;
6618 0ebf8283 2019-07-24 stsp
6619 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6620 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6621 0ebf8283 2019-07-24 stsp return NULL;
6622 0ebf8283 2019-07-24 stsp }
6623 0ebf8283 2019-07-24 stsp
6624 0ebf8283 2019-07-24 stsp const struct got_error *
6625 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6626 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6627 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6628 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6629 0ebf8283 2019-07-24 stsp {
6630 0ebf8283 2019-07-24 stsp const struct got_error *err;
6631 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6632 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6633 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6634 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6635 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6636 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6637 0ebf8283 2019-07-24 stsp
6638 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6639 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6640 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6641 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6642 0ebf8283 2019-07-24 stsp
6643 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6644 3e3a69f1 2019-07-25 stsp if (err)
6645 3e3a69f1 2019-07-25 stsp return err;
6646 3e3a69f1 2019-07-25 stsp
6647 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6648 3e3a69f1 2019-07-25 stsp if (err)
6649 f032f1f7 2019-08-04 stsp goto done;
6650 f032f1f7 2019-08-04 stsp
6651 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6652 f032f1f7 2019-08-04 stsp &have_staged_files);
6653 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6654 f032f1f7 2019-08-04 stsp goto done;
6655 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6656 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6657 3e3a69f1 2019-07-25 stsp goto done;
6658 f032f1f7 2019-08-04 stsp }
6659 3e3a69f1 2019-07-25 stsp
6660 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6661 0ebf8283 2019-07-24 stsp if (err)
6662 f032f1f7 2019-08-04 stsp goto done;
6663 0ebf8283 2019-07-24 stsp
6664 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6665 0ebf8283 2019-07-24 stsp if (err)
6666 0ebf8283 2019-07-24 stsp goto done;
6667 0ebf8283 2019-07-24 stsp
6668 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6669 0ebf8283 2019-07-24 stsp if (err)
6670 0ebf8283 2019-07-24 stsp goto done;
6671 0ebf8283 2019-07-24 stsp
6672 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6673 0ebf8283 2019-07-24 stsp worktree);
6674 0ebf8283 2019-07-24 stsp if (err)
6675 0ebf8283 2019-07-24 stsp goto done;
6676 0ebf8283 2019-07-24 stsp
6677 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6678 0ebf8283 2019-07-24 stsp if (err)
6679 0ebf8283 2019-07-24 stsp goto done;
6680 0ebf8283 2019-07-24 stsp
6681 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6682 0ebf8283 2019-07-24 stsp if (err)
6683 0ebf8283 2019-07-24 stsp goto done;
6684 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6685 0ebf8283 2019-07-24 stsp if (err)
6686 0ebf8283 2019-07-24 stsp goto done;
6687 0ebf8283 2019-07-24 stsp
6688 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6689 0ebf8283 2019-07-24 stsp if (err)
6690 0ebf8283 2019-07-24 stsp goto done;
6691 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6692 0ebf8283 2019-07-24 stsp if (err)
6693 0ebf8283 2019-07-24 stsp goto done;
6694 0ebf8283 2019-07-24 stsp
6695 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6696 0ebf8283 2019-07-24 stsp if (err)
6697 0ebf8283 2019-07-24 stsp goto done;
6698 0ebf8283 2019-07-24 stsp done:
6699 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6700 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6701 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6702 0ebf8283 2019-07-24 stsp if (commit_ref)
6703 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6704 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6705 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6706 0ebf8283 2019-07-24 stsp if (err) {
6707 0ebf8283 2019-07-24 stsp free(*commit_id);
6708 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6709 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6710 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6711 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6712 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6713 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6714 0ebf8283 2019-07-24 stsp }
6715 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6716 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6717 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6718 3e3a69f1 2019-07-25 stsp }
6719 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6720 0ebf8283 2019-07-24 stsp }
6721 0ebf8283 2019-07-24 stsp return err;
6722 0ebf8283 2019-07-24 stsp }
6723 0ebf8283 2019-07-24 stsp
6724 0ebf8283 2019-07-24 stsp static const struct got_error *
6725 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6726 0ebf8283 2019-07-24 stsp {
6727 0ebf8283 2019-07-24 stsp const struct got_error *err;
6728 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6729 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6730 0ebf8283 2019-07-24 stsp
6731 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6732 0ebf8283 2019-07-24 stsp if (err)
6733 0ebf8283 2019-07-24 stsp goto done;
6734 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6735 0ebf8283 2019-07-24 stsp if (err)
6736 0ebf8283 2019-07-24 stsp goto done;
6737 0ebf8283 2019-07-24 stsp
6738 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6739 0ebf8283 2019-07-24 stsp worktree);
6740 0ebf8283 2019-07-24 stsp if (err)
6741 0ebf8283 2019-07-24 stsp goto done;
6742 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6743 0ebf8283 2019-07-24 stsp if (err)
6744 0ebf8283 2019-07-24 stsp goto done;
6745 0ebf8283 2019-07-24 stsp
6746 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6747 0ebf8283 2019-07-24 stsp if (err)
6748 0ebf8283 2019-07-24 stsp goto done;
6749 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6750 0ebf8283 2019-07-24 stsp if (err)
6751 0ebf8283 2019-07-24 stsp goto done;
6752 0ebf8283 2019-07-24 stsp
6753 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6754 0ebf8283 2019-07-24 stsp if (err)
6755 0ebf8283 2019-07-24 stsp goto done;
6756 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6757 0ebf8283 2019-07-24 stsp if (err)
6758 0ebf8283 2019-07-24 stsp goto done;
6759 0ebf8283 2019-07-24 stsp done:
6760 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6761 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6762 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6763 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6764 0ebf8283 2019-07-24 stsp return err;
6765 0ebf8283 2019-07-24 stsp }
6766 0ebf8283 2019-07-24 stsp
6767 0ebf8283 2019-07-24 stsp const struct got_error *
6768 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6769 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6770 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6771 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6772 0ebf8283 2019-07-24 stsp {
6773 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6774 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6775 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6776 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6777 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6778 0ebf8283 2019-07-24 stsp
6779 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6780 0ebf8283 2019-07-24 stsp if (err)
6781 0ebf8283 2019-07-24 stsp return err;
6782 0ebf8283 2019-07-24 stsp
6783 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6784 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6785 0ebf8283 2019-07-24 stsp if (err)
6786 0ebf8283 2019-07-24 stsp goto done;
6787 0ebf8283 2019-07-24 stsp
6788 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6789 0ebf8283 2019-07-24 stsp if (err)
6790 0ebf8283 2019-07-24 stsp goto done;
6791 0ebf8283 2019-07-24 stsp
6792 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6793 0ebf8283 2019-07-24 stsp if (err)
6794 0ebf8283 2019-07-24 stsp goto done;
6795 0ebf8283 2019-07-24 stsp
6796 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6797 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6798 0ebf8283 2019-07-24 stsp if (err)
6799 0ebf8283 2019-07-24 stsp goto done;
6800 0ebf8283 2019-07-24 stsp
6801 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6802 0ebf8283 2019-07-24 stsp if (err)
6803 0ebf8283 2019-07-24 stsp goto done;
6804 0ebf8283 2019-07-24 stsp
6805 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6806 0ebf8283 2019-07-24 stsp if (err)
6807 0ebf8283 2019-07-24 stsp goto done;
6808 0ebf8283 2019-07-24 stsp
6809 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6810 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6811 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6812 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6813 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6814 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6815 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6816 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6817 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6818 0ebf8283 2019-07-24 stsp if (err)
6819 1f1abb7e 2019-08-08 stsp goto sync;
6820 0ebf8283 2019-07-24 stsp
6821 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6822 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6823 0ebf8283 2019-07-24 stsp sync:
6824 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6825 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6826 0ebf8283 2019-07-24 stsp err = sync_err;
6827 0ebf8283 2019-07-24 stsp done:
6828 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6829 0ebf8283 2019-07-24 stsp free(tree_id);
6830 818c7501 2019-07-11 stsp free(fileindex_path);
6831 818c7501 2019-07-11 stsp
6832 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6833 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6834 818c7501 2019-07-11 stsp err = unlockerr;
6835 818c7501 2019-07-11 stsp return err;
6836 818c7501 2019-07-11 stsp }
6837 0ebf8283 2019-07-24 stsp
6838 0ebf8283 2019-07-24 stsp const struct got_error *
6839 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6840 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6841 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6842 0ebf8283 2019-07-24 stsp {
6843 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6844 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6845 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6846 0ebf8283 2019-07-24 stsp
6847 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6848 0ebf8283 2019-07-24 stsp if (err)
6849 0ebf8283 2019-07-24 stsp return err;
6850 0ebf8283 2019-07-24 stsp
6851 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6852 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6853 0ebf8283 2019-07-24 stsp if (err)
6854 0ebf8283 2019-07-24 stsp goto done;
6855 0ebf8283 2019-07-24 stsp
6856 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6857 0ebf8283 2019-07-24 stsp if (err)
6858 0ebf8283 2019-07-24 stsp goto done;
6859 0ebf8283 2019-07-24 stsp
6860 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6861 0ebf8283 2019-07-24 stsp if (err)
6862 0ebf8283 2019-07-24 stsp goto done;
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6865 0ebf8283 2019-07-24 stsp if (err)
6866 0ebf8283 2019-07-24 stsp goto done;
6867 0ebf8283 2019-07-24 stsp
6868 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6869 0ebf8283 2019-07-24 stsp done:
6870 3e3a69f1 2019-07-25 stsp if (fileindex)
6871 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6872 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6873 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6874 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6875 0ebf8283 2019-07-24 stsp err = unlockerr;
6876 0ebf8283 2019-07-24 stsp return err;
6877 0ebf8283 2019-07-24 stsp }
6878 0ebf8283 2019-07-24 stsp
6879 0ebf8283 2019-07-24 stsp const struct got_error *
6880 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6881 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6882 0ebf8283 2019-07-24 stsp {
6883 0ebf8283 2019-07-24 stsp const struct got_error *err;
6884 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6885 0ebf8283 2019-07-24 stsp
6886 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6887 0ebf8283 2019-07-24 stsp if (err)
6888 0ebf8283 2019-07-24 stsp return err;
6889 0ebf8283 2019-07-24 stsp
6890 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6891 0ebf8283 2019-07-24 stsp if (err)
6892 0ebf8283 2019-07-24 stsp goto done;
6893 0ebf8283 2019-07-24 stsp
6894 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6895 0ebf8283 2019-07-24 stsp done:
6896 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6897 2822a352 2019-10-15 stsp return err;
6898 2822a352 2019-10-15 stsp }
6899 2822a352 2019-10-15 stsp
6900 2822a352 2019-10-15 stsp const struct got_error *
6901 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6902 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6903 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6904 2822a352 2019-10-15 stsp struct got_repository *repo)
6905 2822a352 2019-10-15 stsp {
6906 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6907 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6908 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6909 2822a352 2019-10-15 stsp
6910 2822a352 2019-10-15 stsp *fileindex = NULL;
6911 2822a352 2019-10-15 stsp *branch_ref = NULL;
6912 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6913 2822a352 2019-10-15 stsp
6914 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6915 2822a352 2019-10-15 stsp if (err)
6916 2822a352 2019-10-15 stsp return err;
6917 2822a352 2019-10-15 stsp
6918 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6919 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6920 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6921 2822a352 2019-10-15 stsp "update -b or different branch name required");
6922 2822a352 2019-10-15 stsp goto done;
6923 2822a352 2019-10-15 stsp }
6924 2822a352 2019-10-15 stsp
6925 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6926 2822a352 2019-10-15 stsp if (err)
6927 2822a352 2019-10-15 stsp goto done;
6928 2822a352 2019-10-15 stsp
6929 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6930 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6931 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6932 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6933 2822a352 2019-10-15 stsp &ok_arg);
6934 2822a352 2019-10-15 stsp if (err)
6935 2822a352 2019-10-15 stsp goto done;
6936 2822a352 2019-10-15 stsp
6937 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6938 2822a352 2019-10-15 stsp if (err)
6939 2822a352 2019-10-15 stsp goto done;
6940 2822a352 2019-10-15 stsp
6941 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6942 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6943 2822a352 2019-10-15 stsp done:
6944 2822a352 2019-10-15 stsp if (err) {
6945 2822a352 2019-10-15 stsp if (*branch_ref) {
6946 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6947 2822a352 2019-10-15 stsp *branch_ref = NULL;
6948 2822a352 2019-10-15 stsp }
6949 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6950 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6951 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6952 2822a352 2019-10-15 stsp }
6953 2822a352 2019-10-15 stsp if (*fileindex) {
6954 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6955 2822a352 2019-10-15 stsp *fileindex = NULL;
6956 2822a352 2019-10-15 stsp }
6957 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6958 2822a352 2019-10-15 stsp }
6959 0cb83759 2019-08-03 stsp return err;
6960 0cb83759 2019-08-03 stsp }
6961 0cb83759 2019-08-03 stsp
6962 2822a352 2019-10-15 stsp const struct got_error *
6963 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6964 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6965 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6966 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6967 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6968 2822a352 2019-10-15 stsp {
6969 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6970 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6971 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6972 2822a352 2019-10-15 stsp
6973 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6974 2822a352 2019-10-15 stsp if (err)
6975 2822a352 2019-10-15 stsp goto done;
6976 2822a352 2019-10-15 stsp
6977 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6978 2822a352 2019-10-15 stsp if (err)
6979 2822a352 2019-10-15 stsp goto done;
6980 2822a352 2019-10-15 stsp
6981 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6982 2822a352 2019-10-15 stsp worktree->path_prefix);
6983 2822a352 2019-10-15 stsp if (err)
6984 2822a352 2019-10-15 stsp goto done;
6985 2822a352 2019-10-15 stsp
6986 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6987 2822a352 2019-10-15 stsp if (err)
6988 2822a352 2019-10-15 stsp goto done;
6989 2822a352 2019-10-15 stsp
6990 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6991 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6992 2822a352 2019-10-15 stsp if (err)
6993 2822a352 2019-10-15 stsp goto sync;
6994 2822a352 2019-10-15 stsp
6995 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6996 2822a352 2019-10-15 stsp if (err)
6997 2822a352 2019-10-15 stsp goto sync;
6998 2822a352 2019-10-15 stsp
6999 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7000 2822a352 2019-10-15 stsp sync:
7001 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7002 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7003 2822a352 2019-10-15 stsp err = sync_err;
7004 2822a352 2019-10-15 stsp
7005 2822a352 2019-10-15 stsp done:
7006 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7007 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7008 2822a352 2019-10-15 stsp err = unlockerr;
7009 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7010 2822a352 2019-10-15 stsp
7011 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7012 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7013 2822a352 2019-10-15 stsp err = unlockerr;
7014 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7015 2822a352 2019-10-15 stsp
7016 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7017 2822a352 2019-10-15 stsp free(fileindex_path);
7018 2822a352 2019-10-15 stsp free(tree_id);
7019 2822a352 2019-10-15 stsp
7020 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7021 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7022 2822a352 2019-10-15 stsp err = unlockerr;
7023 2822a352 2019-10-15 stsp return err;
7024 2822a352 2019-10-15 stsp }
7025 2822a352 2019-10-15 stsp
7026 2822a352 2019-10-15 stsp const struct got_error *
7027 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7028 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7029 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7030 2822a352 2019-10-15 stsp {
7031 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7032 8b692cd0 2019-10-21 stsp
7033 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7034 8b692cd0 2019-10-21 stsp
7035 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7036 8b692cd0 2019-10-21 stsp
7037 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7038 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7039 8b692cd0 2019-10-21 stsp err = unlockerr;
7040 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7041 8b692cd0 2019-10-21 stsp
7042 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7043 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7044 8b692cd0 2019-10-21 stsp err = unlockerr;
7045 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7046 8b692cd0 2019-10-21 stsp
7047 8b692cd0 2019-10-21 stsp return err;
7048 2822a352 2019-10-15 stsp }
7049 2822a352 2019-10-15 stsp
7050 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7051 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7052 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7053 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7054 2db2652d 2019-08-07 stsp struct got_repository *repo;
7055 2db2652d 2019-08-07 stsp int have_changes;
7056 2db2652d 2019-08-07 stsp };
7057 2db2652d 2019-08-07 stsp
7058 2db2652d 2019-08-07 stsp const struct got_error *
7059 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7060 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7061 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7062 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7063 735ef5ac 2019-08-03 stsp {
7064 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7065 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7066 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7067 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7068 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7069 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7070 8b13ce36 2019-08-08 stsp
7071 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7072 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7073 8b13ce36 2019-08-08 stsp return NULL;
7074 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7075 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7076 735ef5ac 2019-08-03 stsp
7077 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7078 735ef5ac 2019-08-03 stsp if (ie == NULL)
7079 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7080 735ef5ac 2019-08-03 stsp
7081 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7082 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7083 735ef5ac 2019-08-03 stsp relpath) == -1)
7084 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7085 735ef5ac 2019-08-03 stsp
7086 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7087 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7088 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7089 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7090 735ef5ac 2019-08-03 stsp }
7091 735ef5ac 2019-08-03 stsp
7092 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7093 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7094 3aa5969e 2019-08-06 stsp goto done;
7095 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7096 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7097 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7098 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7099 735ef5ac 2019-08-03 stsp goto done;
7100 3aa5969e 2019-08-06 stsp }
7101 735ef5ac 2019-08-03 stsp
7102 2db2652d 2019-08-07 stsp a->have_changes = 1;
7103 2db2652d 2019-08-07 stsp
7104 735ef5ac 2019-08-03 stsp p = in_repo_path;
7105 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7106 735ef5ac 2019-08-03 stsp p++;
7107 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7108 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7109 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7110 735ef5ac 2019-08-03 stsp done:
7111 735ef5ac 2019-08-03 stsp free(in_repo_path);
7112 dc424a06 2019-08-07 stsp return err;
7113 dc424a06 2019-08-07 stsp }
7114 dc424a06 2019-08-07 stsp
7115 2db2652d 2019-08-07 stsp struct stage_path_arg {
7116 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7117 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7118 2db2652d 2019-08-07 stsp struct got_repository *repo;
7119 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7120 2db2652d 2019-08-07 stsp void *status_arg;
7121 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7122 2db2652d 2019-08-07 stsp void *patch_arg;
7123 7b5dc508 2019-10-28 stsp int staged_something;
7124 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7125 2db2652d 2019-08-07 stsp };
7126 2db2652d 2019-08-07 stsp
7127 2db2652d 2019-08-07 stsp static const struct got_error *
7128 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7129 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7130 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7131 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7132 0cb83759 2019-08-03 stsp {
7133 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7134 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7135 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7136 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7137 0cb83759 2019-08-03 stsp uint32_t stage;
7138 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7139 0aeb8099 2020-07-23 stsp struct stat sb;
7140 8b13ce36 2019-08-08 stsp
7141 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7142 8b13ce36 2019-08-08 stsp return NULL;
7143 0cb83759 2019-08-03 stsp
7144 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7145 d3e7c587 2019-08-03 stsp if (ie == NULL)
7146 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7147 0cb83759 2019-08-03 stsp
7148 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7149 2db2652d 2019-08-07 stsp relpath)== -1)
7150 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7151 0cb83759 2019-08-03 stsp
7152 0cb83759 2019-08-03 stsp switch (status) {
7153 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7154 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7155 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7156 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7157 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7158 0aeb8099 2020-07-23 stsp break;
7159 0aeb8099 2020-07-23 stsp }
7160 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7161 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7162 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7163 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7164 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7165 dc424a06 2019-08-07 stsp if (err)
7166 dc424a06 2019-08-07 stsp break;
7167 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7168 dc424a06 2019-08-07 stsp break;
7169 dc424a06 2019-08-07 stsp } else {
7170 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7171 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7172 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7173 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7174 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7175 dc424a06 2019-08-07 stsp break;
7176 dc424a06 2019-08-07 stsp }
7177 dc424a06 2019-08-07 stsp }
7178 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7179 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7180 0cb83759 2019-08-03 stsp if (err)
7181 dc424a06 2019-08-07 stsp break;
7182 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7183 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7184 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7185 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7186 0cb83759 2019-08-03 stsp else
7187 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7188 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7189 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7190 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7191 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7192 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7193 35213c7c 2020-07-23 stsp ssize_t target_len;
7194 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7195 35213c7c 2020-07-23 stsp sizeof(target_path));
7196 35213c7c 2020-07-23 stsp if (target_len == -1) {
7197 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7198 35213c7c 2020-07-23 stsp ondisk_path);
7199 35213c7c 2020-07-23 stsp break;
7200 35213c7c 2020-07-23 stsp }
7201 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7202 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7203 35213c7c 2020-07-23 stsp a->worktree->root_path);
7204 35213c7c 2020-07-23 stsp if (err)
7205 35213c7c 2020-07-23 stsp break;
7206 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7207 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7208 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7209 35213c7c 2020-07-23 stsp break;
7210 35213c7c 2020-07-23 stsp }
7211 35213c7c 2020-07-23 stsp }
7212 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7213 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7214 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7215 35213c7c 2020-07-23 stsp else
7216 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7217 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7218 0aeb8099 2020-07-23 stsp } else {
7219 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7220 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7221 0aeb8099 2020-07-23 stsp }
7222 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7223 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7224 dc424a06 2019-08-07 stsp break;
7225 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7226 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7227 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7228 0cb83759 2019-08-03 stsp break;
7229 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7230 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7231 d3e7c587 2019-08-03 stsp break;
7232 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7233 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7234 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7235 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7236 dc424a06 2019-08-07 stsp if (err)
7237 dc424a06 2019-08-07 stsp break;
7238 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7239 88f33a19 2019-08-08 stsp break;
7240 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7241 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7242 dc424a06 2019-08-07 stsp break;
7243 88f33a19 2019-08-08 stsp }
7244 dc424a06 2019-08-07 stsp }
7245 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7246 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7247 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7248 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7249 dc424a06 2019-08-07 stsp break;
7250 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7251 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7252 12463d8b 2019-12-13 stsp de_name);
7253 0cb83759 2019-08-03 stsp break;
7254 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7255 d3e7c587 2019-08-03 stsp break;
7256 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7257 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7258 ebf48fd5 2019-08-03 stsp break;
7259 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7260 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7261 2a06fe5f 2019-08-24 stsp break;
7262 0cb83759 2019-08-03 stsp default:
7263 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7264 537ac44b 2019-08-03 stsp break;
7265 0cb83759 2019-08-03 stsp }
7266 dc424a06 2019-08-07 stsp
7267 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7268 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7269 dc424a06 2019-08-07 stsp free(path_content);
7270 2db2652d 2019-08-07 stsp free(ondisk_path);
7271 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7272 0ebf8283 2019-07-24 stsp return err;
7273 0ebf8283 2019-07-24 stsp }
7274 0cb83759 2019-08-03 stsp
7275 0cb83759 2019-08-03 stsp const struct got_error *
7276 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7277 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7278 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7279 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7280 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7281 0cb83759 2019-08-03 stsp {
7282 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7283 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7284 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7285 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7286 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7287 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7288 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7289 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7290 0cb83759 2019-08-03 stsp
7291 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7292 0cb83759 2019-08-03 stsp if (err)
7293 0cb83759 2019-08-03 stsp return err;
7294 0cb83759 2019-08-03 stsp
7295 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7296 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7297 735ef5ac 2019-08-03 stsp if (err)
7298 735ef5ac 2019-08-03 stsp goto done;
7299 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7300 735ef5ac 2019-08-03 stsp if (err)
7301 735ef5ac 2019-08-03 stsp goto done;
7302 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7303 0cb83759 2019-08-03 stsp if (err)
7304 0cb83759 2019-08-03 stsp goto done;
7305 0cb83759 2019-08-03 stsp
7306 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7307 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7308 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7309 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7310 2db2652d 2019-08-07 stsp oka.repo = repo;
7311 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7312 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7313 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7314 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7315 735ef5ac 2019-08-03 stsp if (err)
7316 735ef5ac 2019-08-03 stsp goto done;
7317 735ef5ac 2019-08-03 stsp }
7318 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7319 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7320 2db2652d 2019-08-07 stsp goto done;
7321 2db2652d 2019-08-07 stsp }
7322 735ef5ac 2019-08-03 stsp
7323 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7324 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7325 2db2652d 2019-08-07 stsp spa.repo = repo;
7326 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7327 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7328 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7329 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7330 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7331 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7332 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7333 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7334 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7335 42005733 2019-08-03 stsp if (err)
7336 2db2652d 2019-08-07 stsp goto done;
7337 7b5dc508 2019-10-28 stsp }
7338 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7339 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7340 7b5dc508 2019-10-28 stsp goto done;
7341 0cb83759 2019-08-03 stsp }
7342 0cb83759 2019-08-03 stsp
7343 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7344 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7345 0cb83759 2019-08-03 stsp err = sync_err;
7346 0cb83759 2019-08-03 stsp done:
7347 735ef5ac 2019-08-03 stsp if (head_ref)
7348 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7349 735ef5ac 2019-08-03 stsp free(head_commit_id);
7350 0cb83759 2019-08-03 stsp free(fileindex_path);
7351 0cb83759 2019-08-03 stsp if (fileindex)
7352 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7353 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7354 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7355 0cb83759 2019-08-03 stsp err = unlockerr;
7356 0cb83759 2019-08-03 stsp return err;
7357 0cb83759 2019-08-03 stsp }
7358 ad493afc 2019-08-03 stsp
7359 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7360 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7361 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7362 ad493afc 2019-08-03 stsp struct got_repository *repo;
7363 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7364 ad493afc 2019-08-03 stsp void *progress_arg;
7365 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7366 2e1f37b0 2019-08-08 stsp void *patch_arg;
7367 ad493afc 2019-08-03 stsp };
7368 2e1f37b0 2019-08-08 stsp
7369 2e1f37b0 2019-08-08 stsp static const struct got_error *
7370 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7371 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7372 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7373 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7374 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7375 2e1f37b0 2019-08-08 stsp {
7376 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7377 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7378 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7379 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7380 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7381 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7382 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7383 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7384 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7385 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7386 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7387 2e1f37b0 2019-08-08 stsp
7388 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7389 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7390 2e1f37b0 2019-08-08 stsp
7391 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7392 2e1f37b0 2019-08-08 stsp if (err)
7393 2e1f37b0 2019-08-08 stsp return err;
7394 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7395 2e1f37b0 2019-08-08 stsp if (err)
7396 2e1f37b0 2019-08-08 stsp goto done;
7397 2e1f37b0 2019-08-08 stsp
7398 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7399 2e1f37b0 2019-08-08 stsp if (err)
7400 2e1f37b0 2019-08-08 stsp goto done;
7401 2e1f37b0 2019-08-08 stsp
7402 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7403 2e1f37b0 2019-08-08 stsp if (err)
7404 2e1f37b0 2019-08-08 stsp goto done;
7405 2e1f37b0 2019-08-08 stsp
7406 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7407 2e1f37b0 2019-08-08 stsp if (err)
7408 2e1f37b0 2019-08-08 stsp goto done;
7409 2e1f37b0 2019-08-08 stsp
7410 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7411 2e1f37b0 2019-08-08 stsp if (err)
7412 2e1f37b0 2019-08-08 stsp goto done;
7413 ad493afc 2019-08-03 stsp
7414 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7415 2e1f37b0 2019-08-08 stsp if (err)
7416 2e1f37b0 2019-08-08 stsp goto done;
7417 2e1f37b0 2019-08-08 stsp
7418 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7419 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7420 2e1f37b0 2019-08-08 stsp goto done;
7421 2e1f37b0 2019-08-08 stsp }
7422 2e1f37b0 2019-08-08 stsp
7423 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7424 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7425 2e1f37b0 2019-08-08 stsp goto done;
7426 2e1f37b0 2019-08-08 stsp }
7427 2e1f37b0 2019-08-08 stsp
7428 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7429 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7430 2e1f37b0 2019-08-08 stsp if (err)
7431 2e1f37b0 2019-08-08 stsp goto done;
7432 2e1f37b0 2019-08-08 stsp
7433 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7434 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7435 2e1f37b0 2019-08-08 stsp if (err)
7436 2e1f37b0 2019-08-08 stsp goto done;
7437 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7438 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7439 2e1f37b0 2019-08-08 stsp if (err)
7440 2e1f37b0 2019-08-08 stsp goto done;
7441 2e1f37b0 2019-08-08 stsp
7442 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7443 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7444 2e1f37b0 2019-08-08 stsp goto done;
7445 2e1f37b0 2019-08-08 stsp }
7446 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7447 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7448 2e1f37b0 2019-08-08 stsp goto done;
7449 2e1f37b0 2019-08-08 stsp }
7450 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7451 2e1f37b0 2019-08-08 stsp int choice;
7452 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7453 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7454 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7455 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7456 2e1f37b0 2019-08-08 stsp if (err)
7457 2e1f37b0 2019-08-08 stsp goto done;
7458 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7459 2e1f37b0 2019-08-08 stsp have_content = 1;
7460 19e4b907 2019-08-08 stsp else
7461 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7462 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7463 2e1f37b0 2019-08-08 stsp break;
7464 2e1f37b0 2019-08-08 stsp }
7465 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7466 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7467 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7468 2e1f37b0 2019-08-08 stsp done:
7469 2e1f37b0 2019-08-08 stsp free(label1);
7470 2e1f37b0 2019-08-08 stsp if (blob)
7471 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7472 2e1f37b0 2019-08-08 stsp if (staged_blob)
7473 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7474 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7475 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7476 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7477 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7478 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7479 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7480 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7481 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7482 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7483 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7484 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7485 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7486 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7487 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7488 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7489 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7490 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7491 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7492 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7493 2e1f37b0 2019-08-08 stsp }
7494 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7495 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7496 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7497 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7498 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7499 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7500 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7501 2e1f37b0 2019-08-08 stsp }
7502 2e1f37b0 2019-08-08 stsp free(args);
7503 2e1f37b0 2019-08-08 stsp if (ds) {
7504 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7505 2e1f37b0 2019-08-08 stsp free(ds);
7506 2e1f37b0 2019-08-08 stsp }
7507 2e1f37b0 2019-08-08 stsp if (changes)
7508 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7509 2e1f37b0 2019-08-08 stsp free(path1);
7510 2e1f37b0 2019-08-08 stsp free(path2);
7511 fda8017d 2020-07-23 stsp return err;
7512 fda8017d 2020-07-23 stsp }
7513 fda8017d 2020-07-23 stsp
7514 fda8017d 2020-07-23 stsp static const struct got_error *
7515 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7516 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7517 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7518 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7519 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7520 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7521 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7522 fda8017d 2020-07-23 stsp {
7523 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7524 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7525 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7526 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7527 fda8017d 2020-07-23 stsp FILE *f = NULL;
7528 fda8017d 2020-07-23 stsp struct stat sb;
7529 fda8017d 2020-07-23 stsp
7530 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7531 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7532 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7533 fda8017d 2020-07-23 stsp if (err)
7534 fda8017d 2020-07-23 stsp return err;
7535 fda8017d 2020-07-23 stsp
7536 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7537 fda8017d 2020-07-23 stsp return NULL;
7538 fda8017d 2020-07-23 stsp
7539 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7540 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7541 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7542 fda8017d 2020-07-23 stsp if (err)
7543 fda8017d 2020-07-23 stsp goto done;
7544 fda8017d 2020-07-23 stsp }
7545 fda8017d 2020-07-23 stsp
7546 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7547 fda8017d 2020-07-23 stsp if (f == NULL) {
7548 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7549 fda8017d 2020-07-23 stsp path_unstaged_content);
7550 fda8017d 2020-07-23 stsp goto done;
7551 fda8017d 2020-07-23 stsp }
7552 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7553 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7554 fda8017d 2020-07-23 stsp goto done;
7555 fda8017d 2020-07-23 stsp }
7556 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7557 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7558 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7559 fda8017d 2020-07-23 stsp size_t r;
7560 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7561 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7562 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7563 fda8017d 2020-07-23 stsp goto done;
7564 fda8017d 2020-07-23 stsp }
7565 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7566 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7567 fda8017d 2020-07-23 stsp goto done;
7568 fda8017d 2020-07-23 stsp }
7569 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7570 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7571 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7572 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7573 fda8017d 2020-07-23 stsp progress_arg);
7574 fda8017d 2020-07-23 stsp } else {
7575 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7576 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7577 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7578 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7579 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7580 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7581 fda8017d 2020-07-23 stsp }
7582 fda8017d 2020-07-23 stsp if (err)
7583 fda8017d 2020-07-23 stsp goto done;
7584 fda8017d 2020-07-23 stsp
7585 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7586 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7587 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7588 fda8017d 2020-07-23 stsp } else
7589 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7590 fda8017d 2020-07-23 stsp done:
7591 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7592 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7593 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7594 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7595 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7596 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7597 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7598 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7599 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7600 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7601 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7602 2e1f37b0 2019-08-08 stsp return err;
7603 2e1f37b0 2019-08-08 stsp }
7604 2e1f37b0 2019-08-08 stsp
7605 ad493afc 2019-08-03 stsp static const struct got_error *
7606 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7607 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7608 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7609 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7610 ad493afc 2019-08-03 stsp {
7611 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7612 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7613 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7614 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7615 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7616 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7617 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7618 9bc94a15 2019-08-03 stsp struct stat sb;
7619 ad493afc 2019-08-03 stsp
7620 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7621 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7622 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7623 2e1f37b0 2019-08-08 stsp return NULL;
7624 2e1f37b0 2019-08-08 stsp
7625 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7626 ad493afc 2019-08-03 stsp if (ie == NULL)
7627 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7628 9bc94a15 2019-08-03 stsp
7629 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7630 9bc94a15 2019-08-03 stsp == -1)
7631 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7632 ad493afc 2019-08-03 stsp
7633 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7634 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7635 f69721c3 2019-10-21 stsp if (err)
7636 f69721c3 2019-10-21 stsp goto done;
7637 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7638 f69721c3 2019-10-21 stsp id_str) == -1) {
7639 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7640 f69721c3 2019-10-21 stsp goto done;
7641 f69721c3 2019-10-21 stsp }
7642 f69721c3 2019-10-21 stsp
7643 ad493afc 2019-08-03 stsp switch (staged_status) {
7644 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7645 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7646 ad493afc 2019-08-03 stsp blob_id, 8192);
7647 ad493afc 2019-08-03 stsp if (err)
7648 ad493afc 2019-08-03 stsp break;
7649 ad493afc 2019-08-03 stsp /* fall through */
7650 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7651 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7652 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7653 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7654 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7655 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7656 2e1f37b0 2019-08-08 stsp if (err)
7657 2e1f37b0 2019-08-08 stsp break;
7658 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7659 2e1f37b0 2019-08-08 stsp break;
7660 2e1f37b0 2019-08-08 stsp } else {
7661 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7662 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7663 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7664 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7665 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7666 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7667 2e1f37b0 2019-08-08 stsp }
7668 2e1f37b0 2019-08-08 stsp }
7669 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7670 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7671 ad493afc 2019-08-03 stsp if (err)
7672 ad493afc 2019-08-03 stsp break;
7673 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7674 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7675 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7676 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7677 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7678 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7679 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7680 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7681 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7682 ea7786be 2020-07-23 stsp break;
7683 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7684 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7685 36bf999c 2020-07-23 stsp char *staged_target;
7686 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7687 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7688 36bf999c 2020-07-23 stsp if (err)
7689 36bf999c 2020-07-23 stsp goto done;
7690 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7691 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7692 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7693 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7694 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7695 36bf999c 2020-07-23 stsp free(staged_target);
7696 dfe9fba0 2020-07-23 stsp } else {
7697 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7698 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7699 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7700 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7701 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7702 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7703 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7704 dfe9fba0 2020-07-23 stsp }
7705 ea7786be 2020-07-23 stsp break;
7706 ea7786be 2020-07-23 stsp default:
7707 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7708 ea7786be 2020-07-23 stsp break;
7709 ea7786be 2020-07-23 stsp }
7710 ad493afc 2019-08-03 stsp if (err == NULL)
7711 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7712 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7713 ad493afc 2019-08-03 stsp break;
7714 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7715 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7716 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7717 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7718 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7719 2e1f37b0 2019-08-08 stsp if (err)
7720 2e1f37b0 2019-08-08 stsp break;
7721 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7722 2e1f37b0 2019-08-08 stsp break;
7723 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7724 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7725 2e1f37b0 2019-08-08 stsp break;
7726 2e1f37b0 2019-08-08 stsp }
7727 2e1f37b0 2019-08-08 stsp }
7728 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7729 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7730 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7731 9bc94a15 2019-08-03 stsp if (err)
7732 9bc94a15 2019-08-03 stsp break;
7733 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7734 ad493afc 2019-08-03 stsp break;
7735 ad493afc 2019-08-03 stsp }
7736 f69721c3 2019-10-21 stsp done:
7737 ad493afc 2019-08-03 stsp free(ondisk_path);
7738 ad493afc 2019-08-03 stsp if (blob_base)
7739 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7740 ad493afc 2019-08-03 stsp if (blob_staged)
7741 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7742 f69721c3 2019-10-21 stsp free(id_str);
7743 f69721c3 2019-10-21 stsp free(label_orig);
7744 ad493afc 2019-08-03 stsp return err;
7745 ad493afc 2019-08-03 stsp }
7746 ad493afc 2019-08-03 stsp
7747 ad493afc 2019-08-03 stsp const struct got_error *
7748 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7749 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7750 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7751 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7752 ad493afc 2019-08-03 stsp struct got_repository *repo)
7753 ad493afc 2019-08-03 stsp {
7754 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7755 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7756 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7757 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7758 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7759 ad493afc 2019-08-03 stsp
7760 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7761 ad493afc 2019-08-03 stsp if (err)
7762 ad493afc 2019-08-03 stsp return err;
7763 ad493afc 2019-08-03 stsp
7764 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7765 ad493afc 2019-08-03 stsp if (err)
7766 ad493afc 2019-08-03 stsp goto done;
7767 ad493afc 2019-08-03 stsp
7768 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7769 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7770 ad493afc 2019-08-03 stsp upa.repo = repo;
7771 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7772 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7773 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7774 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7775 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7776 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7777 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7778 ad493afc 2019-08-03 stsp if (err)
7779 ad493afc 2019-08-03 stsp goto done;
7780 ad493afc 2019-08-03 stsp }
7781 ad493afc 2019-08-03 stsp
7782 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7783 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7784 ad493afc 2019-08-03 stsp err = sync_err;
7785 ad493afc 2019-08-03 stsp done:
7786 ad493afc 2019-08-03 stsp free(fileindex_path);
7787 ad493afc 2019-08-03 stsp if (fileindex)
7788 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7789 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7790 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7791 ad493afc 2019-08-03 stsp err = unlockerr;
7792 ad493afc 2019-08-03 stsp return err;
7793 ad493afc 2019-08-03 stsp }
7794 b2118c49 2020-07-28 stsp
7795 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7796 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7797 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7798 b2118c49 2020-07-28 stsp void *info_arg;
7799 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7800 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7801 b2118c49 2020-07-28 stsp void *cancel_arg;
7802 b2118c49 2020-07-28 stsp };
7803 b2118c49 2020-07-28 stsp
7804 b2118c49 2020-07-28 stsp static const struct got_error *
7805 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7806 b2118c49 2020-07-28 stsp {
7807 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7808 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7809 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7810 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7811 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7812 b2118c49 2020-07-28 stsp int stage;
7813 b2118c49 2020-07-28 stsp
7814 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7815 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7816 b2118c49 2020-07-28 stsp
7817 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7818 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7819 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7820 b2118c49 2020-07-28 stsp break;
7821 b2118c49 2020-07-28 stsp }
7822 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7823 b2118c49 2020-07-28 stsp return NULL;
7824 b2118c49 2020-07-28 stsp
7825 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7826 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7827 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7828 b2118c49 2020-07-28 stsp }
7829 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7830 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7831 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7832 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7833 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7834 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7835 b2118c49 2020-07-28 stsp }
7836 b2118c49 2020-07-28 stsp
7837 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7838 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7839 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7840 b2118c49 2020-07-28 stsp }
7841 b2118c49 2020-07-28 stsp
7842 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7843 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7844 b2118c49 2020-07-28 stsp }
7845 b2118c49 2020-07-28 stsp
7846 b2118c49 2020-07-28 stsp const struct got_error *
7847 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7848 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7849 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7850 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7851 b2118c49 2020-07-28 stsp
7852 b2118c49 2020-07-28 stsp {
7853 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7854 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7855 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7856 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7857 b2118c49 2020-07-28 stsp
7858 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7859 b2118c49 2020-07-28 stsp if (err)
7860 b2118c49 2020-07-28 stsp return err;
7861 b2118c49 2020-07-28 stsp
7862 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7863 b2118c49 2020-07-28 stsp if (err)
7864 b2118c49 2020-07-28 stsp goto done;
7865 b2118c49 2020-07-28 stsp
7866 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7867 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7868 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7869 b2118c49 2020-07-28 stsp arg.paths = paths;
7870 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7871 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7872 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7873 b2118c49 2020-07-28 stsp &arg);
7874 b2118c49 2020-07-28 stsp done:
7875 b2118c49 2020-07-28 stsp free(fileindex_path);
7876 b2118c49 2020-07-28 stsp if (fileindex)
7877 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7878 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7879 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7880 b2118c49 2020-07-28 stsp err = unlockerr;
7881 b2118c49 2020-07-28 stsp return err;
7882 b2118c49 2020-07-28 stsp }