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 3818e3c4 2020-11-01 naddy goto done;
970 3818e3c4 2020-11-01 naddy
971 3818e3c4 2020-11-01 naddy if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
972 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path);
973 11cc08c1 2020-07-23 stsp goto done;
974 3818e3c4 2020-11-01 naddy }
975 11cc08c1 2020-07-23 stsp
976 283102fc 2020-07-23 stsp if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
977 993e2a1b 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
978 993e2a1b 2020-07-23 stsp deriv_target ? deriv_target : "(symlink was deleted)",
979 11cc08c1 2020-07-23 stsp orig_target ? label_orig : "",
980 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
981 11cc08c1 2020-07-23 stsp orig_target ? orig_target : "",
982 11cc08c1 2020-07-23 stsp orig_target ? "\n" : "",
983 11cc08c1 2020-07-23 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
984 11cc08c1 2020-07-23 stsp local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
985 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fprintf", path);
986 11cc08c1 2020-07-23 stsp goto done;
987 11cc08c1 2020-07-23 stsp }
988 11cc08c1 2020-07-23 stsp
989 11cc08c1 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
990 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("unlink", ondisk_path);
991 11cc08c1 2020-07-23 stsp goto done;
992 11cc08c1 2020-07-23 stsp }
993 11cc08c1 2020-07-23 stsp if (rename(path, ondisk_path) == -1) {
994 11cc08c1 2020-07-23 stsp err = got_error_from_errno3("rename", path, ondisk_path);
995 11cc08c1 2020-07-23 stsp goto done;
996 11cc08c1 2020-07-23 stsp }
997 11cc08c1 2020-07-23 stsp done:
998 11cc08c1 2020-07-23 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
999 11cc08c1 2020-07-23 stsp err = got_error_from_errno2("fclose", path);
1000 11cc08c1 2020-07-23 stsp free(path);
1001 11cc08c1 2020-07-23 stsp free(id_str);
1002 11cc08c1 2020-07-23 stsp free(label_deriv);
1003 11cc08c1 2020-07-23 stsp return err;
1004 11cc08c1 2020-07-23 stsp }
1005 d219f183 2020-07-23 stsp
1006 d219f183 2020-07-23 stsp /* forward declaration */
1007 d219f183 2020-07-23 stsp static const struct got_error *
1008 d219f183 2020-07-23 stsp merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1009 d219f183 2020-07-23 stsp const char *, const char *, uint16_t, const char *,
1010 d219f183 2020-07-23 stsp struct got_blob_object *, struct got_object_id *,
1011 d219f183 2020-07-23 stsp struct got_repository *, got_worktree_checkout_cb, void *);
1012 11cc08c1 2020-07-23 stsp
1013 11cc08c1 2020-07-23 stsp /*
1014 af57b12a 2020-07-23 stsp * Merge a symlink into the work tree, where blob_orig acts as the common
1015 36bf999c 2020-07-23 stsp * ancestor, deriv_target is the link target of the first derived version,
1016 36bf999c 2020-07-23 stsp * and the symlink on disk acts as the second derived version.
1017 af57b12a 2020-07-23 stsp * Assume that contents of both blobs represent symlinks.
1018 af57b12a 2020-07-23 stsp */
1019 af57b12a 2020-07-23 stsp static const struct got_error *
1020 af57b12a 2020-07-23 stsp merge_symlink(struct got_worktree *worktree,
1021 af57b12a 2020-07-23 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1022 36bf999c 2020-07-23 stsp const char *path, const char *label_orig, const char *deriv_target,
1023 af57b12a 2020-07-23 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1024 af57b12a 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1025 af57b12a 2020-07-23 stsp {
1026 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1027 36bf999c 2020-07-23 stsp char *ancestor_target = NULL;
1028 af57b12a 2020-07-23 stsp struct stat sb;
1029 11cc08c1 2020-07-23 stsp ssize_t ondisk_len, deriv_len;
1030 af57b12a 2020-07-23 stsp char ondisk_target[PATH_MAX];
1031 11cc08c1 2020-07-23 stsp int have_local_change = 0;
1032 11cc08c1 2020-07-23 stsp int have_incoming_change = 0;
1033 af57b12a 2020-07-23 stsp
1034 af57b12a 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1)
1035 af57b12a 2020-07-23 stsp return got_error_from_errno2("lstat", ondisk_path);
1036 af57b12a 2020-07-23 stsp
1037 af57b12a 2020-07-23 stsp ondisk_len = readlink(ondisk_path, ondisk_target,
1038 af57b12a 2020-07-23 stsp sizeof(ondisk_target));
1039 af57b12a 2020-07-23 stsp if (ondisk_len == -1) {
1040 af57b12a 2020-07-23 stsp err = got_error_from_errno2("readlink",
1041 af57b12a 2020-07-23 stsp ondisk_path);
1042 af57b12a 2020-07-23 stsp goto done;
1043 af57b12a 2020-07-23 stsp }
1044 56d815a9 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
1045 af57b12a 2020-07-23 stsp
1046 526a746f 2020-07-23 stsp if (blob_orig) {
1047 526a746f 2020-07-23 stsp err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1048 526a746f 2020-07-23 stsp if (err)
1049 526a746f 2020-07-23 stsp goto done;
1050 526a746f 2020-07-23 stsp }
1051 af57b12a 2020-07-23 stsp
1052 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1053 11cc08c1 2020-07-23 stsp (ondisk_len != strlen(ancestor_target) ||
1054 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1055 11cc08c1 2020-07-23 stsp have_local_change = 1;
1056 11cc08c1 2020-07-23 stsp
1057 11cc08c1 2020-07-23 stsp deriv_len = strlen(deriv_target);
1058 11cc08c1 2020-07-23 stsp if (ancestor_target == NULL ||
1059 11cc08c1 2020-07-23 stsp (deriv_len != strlen(ancestor_target) ||
1060 11cc08c1 2020-07-23 stsp memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1061 11cc08c1 2020-07-23 stsp have_incoming_change = 1;
1062 11cc08c1 2020-07-23 stsp
1063 11cc08c1 2020-07-23 stsp if (!have_local_change && !have_incoming_change) {
1064 11cc08c1 2020-07-23 stsp if (ancestor_target) {
1065 11cc08c1 2020-07-23 stsp /* Both sides made the same change. */
1066 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1067 11cc08c1 2020-07-23 stsp path);
1068 11cc08c1 2020-07-23 stsp } else if (deriv_len == ondisk_len &&
1069 11cc08c1 2020-07-23 stsp memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1070 11cc08c1 2020-07-23 stsp /* Both sides added the same symlink. */
1071 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1072 11cc08c1 2020-07-23 stsp path);
1073 11cc08c1 2020-07-23 stsp } else {
1074 11cc08c1 2020-07-23 stsp /* Both sides added symlinks which don't match. */
1075 11cc08c1 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1076 11cc08c1 2020-07-23 stsp deriv_base_commit_id, ancestor_target,
1077 11cc08c1 2020-07-23 stsp label_orig, ondisk_target, ondisk_path);
1078 11cc08c1 2020-07-23 stsp if (err)
1079 11cc08c1 2020-07-23 stsp goto done;
1080 11cc08c1 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1081 11cc08c1 2020-07-23 stsp path);
1082 11cc08c1 2020-07-23 stsp }
1083 11cc08c1 2020-07-23 stsp } else if (!have_local_change && have_incoming_change) {
1084 af57b12a 2020-07-23 stsp /* Apply the incoming change. */
1085 af57b12a 2020-07-23 stsp err = update_symlink(ondisk_path, deriv_target,
1086 af57b12a 2020-07-23 stsp strlen(deriv_target));
1087 af57b12a 2020-07-23 stsp if (err)
1088 af57b12a 2020-07-23 stsp goto done;
1089 af57b12a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1090 11cc08c1 2020-07-23 stsp } else if (have_local_change && have_incoming_change) {
1091 ea7786be 2020-07-23 stsp if (deriv_len == ondisk_len &&
1092 ea7786be 2020-07-23 stsp memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1093 ea7786be 2020-07-23 stsp /* Both sides made the same change. */
1094 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1095 ea7786be 2020-07-23 stsp path);
1096 ea7786be 2020-07-23 stsp } else {
1097 ea7786be 2020-07-23 stsp err = install_symlink_conflict(deriv_target,
1098 ea7786be 2020-07-23 stsp deriv_base_commit_id, ancestor_target, label_orig,
1099 ea7786be 2020-07-23 stsp ondisk_target, ondisk_path);
1100 ea7786be 2020-07-23 stsp if (err)
1101 ea7786be 2020-07-23 stsp goto done;
1102 ea7786be 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1103 ea7786be 2020-07-23 stsp path);
1104 ea7786be 2020-07-23 stsp }
1105 6353ad76 2019-02-08 stsp }
1106 11cc08c1 2020-07-23 stsp
1107 af57b12a 2020-07-23 stsp done:
1108 af57b12a 2020-07-23 stsp free(ancestor_target);
1109 14c901f1 2019-08-08 stsp return err;
1110 14c901f1 2019-08-08 stsp }
1111 14c901f1 2019-08-08 stsp
1112 14c901f1 2019-08-08 stsp /*
1113 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
1114 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
1115 14c901f1 2019-08-08 stsp * acts as the second derived version.
1116 14c901f1 2019-08-08 stsp */
1117 14c901f1 2019-08-08 stsp static const struct got_error *
1118 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1119 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
1120 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
1121 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
1122 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1123 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1124 14c901f1 2019-08-08 stsp {
1125 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
1126 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
1127 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1128 ed6b5030 2020-10-20 stsp char *label_deriv = NULL, *parent = NULL;
1129 14c901f1 2019-08-08 stsp
1130 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
1131 14c901f1 2019-08-08 stsp
1132 ed6b5030 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1133 ed6b5030 2020-10-20 stsp if (err)
1134 ed6b5030 2020-10-20 stsp return err;
1135 14c901f1 2019-08-08 stsp
1136 14c901f1 2019-08-08 stsp free(base_path);
1137 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1138 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1139 14c901f1 2019-08-08 stsp base_path = NULL;
1140 14c901f1 2019-08-08 stsp goto done;
1141 14c901f1 2019-08-08 stsp }
1142 14c901f1 2019-08-08 stsp
1143 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1144 14c901f1 2019-08-08 stsp if (err)
1145 14c901f1 2019-08-08 stsp goto done;
1146 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1147 14c901f1 2019-08-08 stsp blob_deriv);
1148 14c901f1 2019-08-08 stsp if (err)
1149 14c901f1 2019-08-08 stsp goto done;
1150 14c901f1 2019-08-08 stsp
1151 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
1152 14c901f1 2019-08-08 stsp if (err)
1153 14c901f1 2019-08-08 stsp goto done;
1154 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
1155 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1156 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
1157 14c901f1 2019-08-08 stsp goto done;
1158 14c901f1 2019-08-08 stsp }
1159 14c901f1 2019-08-08 stsp
1160 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
1161 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1162 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
1163 14c901f1 2019-08-08 stsp done:
1164 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1165 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
1166 14c901f1 2019-08-08 stsp free(base_path);
1167 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
1168 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
1169 14c901f1 2019-08-08 stsp free(blob_deriv_path);
1170 14c901f1 2019-08-08 stsp }
1171 6353ad76 2019-02-08 stsp free(id_str);
1172 818c7501 2019-07-11 stsp free(label_deriv);
1173 ed6b5030 2020-10-20 stsp free(parent);
1174 4a1ddfc2 2019-01-12 stsp return err;
1175 4a1ddfc2 2019-01-12 stsp }
1176 4a1ddfc2 2019-01-12 stsp
1177 4a1ddfc2 2019-01-12 stsp static const struct got_error *
1178 65b05cec 2020-07-23 stsp create_fileindex_entry(struct got_fileindex_entry **new_iep,
1179 65b05cec 2020-07-23 stsp struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1180 65b05cec 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_object_id *blob_id)
1181 13d9040b 2019-03-27 stsp {
1182 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
1183 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
1184 13d9040b 2019-03-27 stsp
1185 65b05cec 2020-07-23 stsp *new_iep = NULL;
1186 65b05cec 2020-07-23 stsp
1187 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
1188 054041d0 2020-06-24 stsp if (err)
1189 054041d0 2020-06-24 stsp return err;
1190 054041d0 2020-06-24 stsp
1191 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
1192 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
1193 054041d0 2020-06-24 stsp if (err)
1194 054041d0 2020-06-24 stsp goto done;
1195 054041d0 2020-06-24 stsp
1196 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
1197 054041d0 2020-06-24 stsp done:
1198 054041d0 2020-06-24 stsp if (err)
1199 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
1200 65b05cec 2020-07-23 stsp else
1201 65b05cec 2020-07-23 stsp *new_iep = new_ie;
1202 13d9040b 2019-03-27 stsp return err;
1203 1ebedb77 2019-10-19 stsp }
1204 1ebedb77 2019-10-19 stsp
1205 1ebedb77 2019-10-19 stsp static mode_t
1206 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
1207 1ebedb77 2019-10-19 stsp {
1208 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
1209 1ebedb77 2019-10-19 stsp
1210 1ebedb77 2019-10-19 stsp if (executable) {
1211 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
1212 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
1213 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
1214 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
1215 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
1216 1ebedb77 2019-10-19 stsp return st_mode | xbits;
1217 1ebedb77 2019-10-19 stsp }
1218 1ebedb77 2019-10-19 stsp
1219 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1220 13d9040b 2019-03-27 stsp }
1221 13d9040b 2019-03-27 stsp
1222 8ba819a3 2020-07-23 stsp /* forward declaration */
1223 13d9040b 2019-03-27 stsp static const struct got_error *
1224 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1225 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
1226 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
1227 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1228 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1229 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
1230 8ba819a3 2020-07-23 stsp
1231 5a1fbc73 2020-07-23 stsp /*
1232 5a1fbc73 2020-07-23 stsp * This function assumes that the provided symlink target points at a
1233 5a1fbc73 2020-07-23 stsp * safe location in the work tree!
1234 5a1fbc73 2020-07-23 stsp */
1235 8ba819a3 2020-07-23 stsp static const struct got_error *
1236 5a1fbc73 2020-07-23 stsp replace_existing_symlink(const char *ondisk_path, const char *target_path,
1237 5a1fbc73 2020-07-23 stsp size_t target_len)
1238 5a1fbc73 2020-07-23 stsp {
1239 5a1fbc73 2020-07-23 stsp const struct got_error *err = NULL;
1240 5a1fbc73 2020-07-23 stsp ssize_t elen;
1241 5a1fbc73 2020-07-23 stsp char etarget[PATH_MAX];
1242 5a1fbc73 2020-07-23 stsp int fd;
1243 5a1fbc73 2020-07-23 stsp
1244 5a1fbc73 2020-07-23 stsp /*
1245 5a1fbc73 2020-07-23 stsp * "Bad" symlinks (those pointing outside the work tree or into the
1246 5a1fbc73 2020-07-23 stsp * .got directory) are installed in the work tree as a regular file
1247 5a1fbc73 2020-07-23 stsp * which contains the bad symlink target path.
1248 5a1fbc73 2020-07-23 stsp * The new symlink target has already been checked for safety by our
1249 5a1fbc73 2020-07-23 stsp * caller. If we can successfully open a regular file then we simply
1250 5a1fbc73 2020-07-23 stsp * replace this file with a symlink below.
1251 5a1fbc73 2020-07-23 stsp */
1252 5a1fbc73 2020-07-23 stsp fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1253 5a1fbc73 2020-07-23 stsp if (fd == -1) {
1254 5a1fbc73 2020-07-23 stsp if (errno != ELOOP)
1255 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("open", ondisk_path);
1256 5a1fbc73 2020-07-23 stsp
1257 5a1fbc73 2020-07-23 stsp /* We are updating an existing on-disk symlink. */
1258 5a1fbc73 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1259 5a1fbc73 2020-07-23 stsp if (elen == -1)
1260 5a1fbc73 2020-07-23 stsp return got_error_from_errno2("readlink", ondisk_path);
1261 5a1fbc73 2020-07-23 stsp
1262 5a1fbc73 2020-07-23 stsp if (elen == target_len &&
1263 5a1fbc73 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0)
1264 5a1fbc73 2020-07-23 stsp return NULL; /* nothing to do */
1265 5a1fbc73 2020-07-23 stsp }
1266 5a1fbc73 2020-07-23 stsp
1267 5a1fbc73 2020-07-23 stsp err = update_symlink(ondisk_path, target_path, target_len);
1268 5a1fbc73 2020-07-23 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1269 5a1fbc73 2020-07-23 stsp err = got_error_from_errno2("close", ondisk_path);
1270 5a1fbc73 2020-07-23 stsp return err;
1271 3c1ec782 2020-07-23 stsp }
1272 3c1ec782 2020-07-23 stsp
1273 3c1ec782 2020-07-23 stsp static const struct got_error *
1274 3c1ec782 2020-07-23 stsp is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1275 3c1ec782 2020-07-23 stsp size_t target_len, const char *ondisk_path, const char *wtroot_path)
1276 3c1ec782 2020-07-23 stsp {
1277 3c1ec782 2020-07-23 stsp const struct got_error *err = NULL;
1278 3c1ec782 2020-07-23 stsp char canonpath[PATH_MAX];
1279 3c1ec782 2020-07-23 stsp char *path_got = NULL;
1280 3c1ec782 2020-07-23 stsp
1281 3c1ec782 2020-07-23 stsp *is_bad_symlink = 0;
1282 3c1ec782 2020-07-23 stsp
1283 3c1ec782 2020-07-23 stsp if (target_len >= sizeof(canonpath)) {
1284 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1285 3c1ec782 2020-07-23 stsp return NULL;
1286 3c1ec782 2020-07-23 stsp }
1287 3c1ec782 2020-07-23 stsp
1288 3c1ec782 2020-07-23 stsp /*
1289 3c1ec782 2020-07-23 stsp * We do not use realpath(3) to resolve the symlink's target
1290 3c1ec782 2020-07-23 stsp * path because we don't want to resolve symlinks recursively.
1291 3c1ec782 2020-07-23 stsp * Instead we make the path absolute and then canonicalize it.
1292 3c1ec782 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
1293 3c1ec782 2020-07-23 stsp * in which the blob object is being installed.
1294 3c1ec782 2020-07-23 stsp */
1295 3c1ec782 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
1296 ce031e9e 2020-10-20 stsp char *abspath, *parent;
1297 ce031e9e 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1298 ce031e9e 2020-10-20 stsp if (err)
1299 ce031e9e 2020-10-20 stsp return err;
1300 ce031e9e 2020-10-20 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1301 ce031e9e 2020-10-20 stsp free(parent);
1302 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1303 ce031e9e 2020-10-20 stsp }
1304 ce031e9e 2020-10-20 stsp free(parent);
1305 3c1ec782 2020-07-23 stsp if (strlen(abspath) >= sizeof(canonpath)) {
1306 3c1ec782 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1307 3c1ec782 2020-07-23 stsp free(abspath);
1308 3c1ec782 2020-07-23 stsp return err;
1309 3c1ec782 2020-07-23 stsp }
1310 3c1ec782 2020-07-23 stsp err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1311 3c1ec782 2020-07-23 stsp free(abspath);
1312 3c1ec782 2020-07-23 stsp if (err)
1313 3c1ec782 2020-07-23 stsp return err;
1314 3c1ec782 2020-07-23 stsp } else {
1315 3c1ec782 2020-07-23 stsp err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1316 3c1ec782 2020-07-23 stsp if (err)
1317 3c1ec782 2020-07-23 stsp return err;
1318 3c1ec782 2020-07-23 stsp }
1319 3c1ec782 2020-07-23 stsp
1320 3c1ec782 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1321 3c1ec782 2020-07-23 stsp if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1322 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1323 3c1ec782 2020-07-23 stsp return NULL;
1324 3c1ec782 2020-07-23 stsp }
1325 3c1ec782 2020-07-23 stsp
1326 3c1ec782 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1327 3c1ec782 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", wtroot_path,
1328 3c1ec782 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1)
1329 3c1ec782 2020-07-23 stsp return got_error_from_errno("asprintf");
1330 3c1ec782 2020-07-23 stsp if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1331 3c1ec782 2020-07-23 stsp *is_bad_symlink = 1;
1332 3c1ec782 2020-07-23 stsp
1333 3c1ec782 2020-07-23 stsp free(path_got);
1334 3c1ec782 2020-07-23 stsp return NULL;
1335 5a1fbc73 2020-07-23 stsp }
1336 5a1fbc73 2020-07-23 stsp
1337 5a1fbc73 2020-07-23 stsp static const struct got_error *
1338 2e1fa222 2020-07-23 stsp install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1339 2e1fa222 2020-07-23 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
1340 2e1fa222 2020-07-23 stsp int restoring_missing_file, int reverting_versioned_file,
1341 c90c8ce3 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1342 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1343 9d31a1d8 2018-03-11 stsp {
1344 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1345 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
1346 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
1347 906c123b 2020-07-23 stsp char *path_got = NULL;
1348 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1349 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1350 8ba819a3 2020-07-23 stsp
1351 2e1fa222 2020-07-23 stsp *is_bad_symlink = 0;
1352 2e1fa222 2020-07-23 stsp
1353 8ba819a3 2020-07-23 stsp /*
1354 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
1355 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
1356 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
1357 8ba819a3 2020-07-23 stsp * in the blob object.
1358 8ba819a3 2020-07-23 stsp */
1359 8ba819a3 2020-07-23 stsp do {
1360 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1361 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1362 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
1363 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1364 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1365 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
1366 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1367 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1368 3b9f0f87 2020-07-23 stsp 1, path_is_unversioned, repo, progress_cb,
1369 3b9f0f87 2020-07-23 stsp progress_arg);
1370 8ba819a3 2020-07-23 stsp }
1371 8ba819a3 2020-07-23 stsp if (len > 0) {
1372 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
1373 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1374 8ba819a3 2020-07-23 stsp len - hdrlen);
1375 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
1376 8ba819a3 2020-07-23 stsp hdrlen = 0;
1377 8ba819a3 2020-07-23 stsp }
1378 8ba819a3 2020-07-23 stsp } while (len != 0);
1379 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
1380 8ba819a3 2020-07-23 stsp
1381 3c1ec782 2020-07-23 stsp err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1382 3c1ec782 2020-07-23 stsp ondisk_path, worktree->root_path);
1383 3c1ec782 2020-07-23 stsp if (err)
1384 3c1ec782 2020-07-23 stsp return err;
1385 8ba819a3 2020-07-23 stsp
1386 3c1ec782 2020-07-23 stsp if (*is_bad_symlink) {
1387 906c123b 2020-07-23 stsp /* install as a regular file */
1388 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1389 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1390 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1391 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1392 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1393 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo, progress_cb, progress_arg);
1394 906c123b 2020-07-23 stsp goto done;
1395 906c123b 2020-07-23 stsp }
1396 906c123b 2020-07-23 stsp
1397 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1398 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1399 c90c8ce3 2020-07-23 stsp if (path_is_unversioned) {
1400 c90c8ce3 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1401 c90c8ce3 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1402 c90c8ce3 2020-07-23 stsp goto done;
1403 c90c8ce3 2020-07-23 stsp }
1404 5a1fbc73 2020-07-23 stsp err = replace_existing_symlink(ondisk_path,
1405 5a1fbc73 2020-07-23 stsp target_path, target_len);
1406 5a1fbc73 2020-07-23 stsp if (err)
1407 f35fa46a 2020-07-23 stsp goto done;
1408 5a1fbc73 2020-07-23 stsp if (progress_cb) {
1409 5a1fbc73 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1410 6e1eade5 2020-07-23 stsp reverting_versioned_file ?
1411 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1412 6e1eade5 2020-07-23 stsp path);
1413 f35fa46a 2020-07-23 stsp }
1414 5a1fbc73 2020-07-23 stsp goto done; /* Nothing else to do. */
1415 f35fa46a 2020-07-23 stsp }
1416 f35fa46a 2020-07-23 stsp
1417 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1418 f4994adc 2020-10-20 stsp char *parent;
1419 f4994adc 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
1420 f4994adc 2020-10-20 stsp if (err)
1421 f4994adc 2020-10-20 stsp goto done;
1422 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1423 f4994adc 2020-10-20 stsp free(parent);
1424 f35fa46a 2020-07-23 stsp if (err)
1425 f35fa46a 2020-07-23 stsp goto done;
1426 f35fa46a 2020-07-23 stsp /*
1427 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1428 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1429 f35fa46a 2020-07-23 stsp */
1430 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1431 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1432 f35fa46a 2020-07-23 stsp goto done;
1433 f35fa46a 2020-07-23 stsp }
1434 f35fa46a 2020-07-23 stsp }
1435 f35fa46a 2020-07-23 stsp
1436 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1437 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1438 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1439 2e1fa222 2020-07-23 stsp *is_bad_symlink = 1;
1440 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1441 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1442 b88d214a 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1443 bd6aa359 2020-07-23 stsp restoring_missing_file, reverting_versioned_file, 1,
1444 3b9f0f87 2020-07-23 stsp path_is_unversioned, repo,
1445 3b9f0f87 2020-07-23 stsp progress_cb, progress_arg);
1446 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1447 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1448 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1449 8ba819a3 2020-07-23 stsp } else {
1450 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1451 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1452 8ba819a3 2020-07-23 stsp }
1453 bd6aa359 2020-07-23 stsp } else if (progress_cb)
1454 6e1eade5 2020-07-23 stsp err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1455 6e1eade5 2020-07-23 stsp GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1456 8ba819a3 2020-07-23 stsp done:
1457 906c123b 2020-07-23 stsp free(path_got);
1458 8ba819a3 2020-07-23 stsp return err;
1459 8ba819a3 2020-07-23 stsp }
1460 8ba819a3 2020-07-23 stsp
1461 8ba819a3 2020-07-23 stsp static const struct got_error *
1462 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1463 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1464 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1465 bd6aa359 2020-07-23 stsp int reverting_versioned_file, int installing_bad_symlink,
1466 3b9f0f87 2020-07-23 stsp int path_is_unversioned, struct got_repository *repo,
1467 3b9f0f87 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1468 8ba819a3 2020-07-23 stsp {
1469 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1470 507dc3bb 2018-12-29 stsp int fd = -1;
1471 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1472 507dc3bb 2018-12-29 stsp int update = 0;
1473 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1474 8ba819a3 2020-07-23 stsp
1475 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1476 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1477 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1478 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1479 f5375317 2020-10-20 stsp char *parent;
1480 f5375317 2020-10-20 stsp err = got_path_dirname(&parent, path);
1481 f5375317 2020-10-20 stsp if (err)
1482 f5375317 2020-10-20 stsp return err;
1483 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1484 f5375317 2020-10-20 stsp free(parent);
1485 21908da4 2019-01-13 stsp if (err)
1486 21908da4 2019-01-13 stsp return err;
1487 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1488 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1489 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1490 21908da4 2019-01-13 stsp if (fd == -1)
1491 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1492 230a42bd 2019-05-11 jcs ondisk_path);
1493 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1494 3b9f0f87 2020-07-23 stsp if (path_is_unversioned) {
1495 3b9f0f87 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1496 3b9f0f87 2020-07-23 stsp GOT_STATUS_UNVERSIONED, path);
1497 3b9f0f87 2020-07-23 stsp goto done;
1498 3b9f0f87 2020-07-23 stsp }
1499 f6d8c0ac 2020-11-09 stsp if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1500 f6d8c0ac 2020-11-09 stsp !S_ISREG(st_mode) && !installing_bad_symlink) {
1501 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1502 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1503 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1504 507dc3bb 2018-12-29 stsp goto done;
1505 d70b8e30 2018-12-27 stsp } else {
1506 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1507 507dc3bb 2018-12-29 stsp ondisk_path);
1508 507dc3bb 2018-12-29 stsp if (err)
1509 507dc3bb 2018-12-29 stsp goto done;
1510 507dc3bb 2018-12-29 stsp update = 1;
1511 9d31a1d8 2018-03-11 stsp }
1512 507dc3bb 2018-12-29 stsp } else
1513 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1514 9d31a1d8 2018-03-11 stsp }
1515 9d31a1d8 2018-03-11 stsp
1516 3818e3c4 2020-11-01 naddy if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1517 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod",
1518 3818e3c4 2020-11-01 naddy update ? tmppath : ondisk_path);
1519 3818e3c4 2020-11-01 naddy goto done;
1520 3818e3c4 2020-11-01 naddy }
1521 3818e3c4 2020-11-01 naddy
1522 bd6aa359 2020-07-23 stsp if (progress_cb) {
1523 bd6aa359 2020-07-23 stsp if (restoring_missing_file)
1524 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1525 bd6aa359 2020-07-23 stsp path);
1526 bd6aa359 2020-07-23 stsp else if (reverting_versioned_file)
1527 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1528 bd6aa359 2020-07-23 stsp path);
1529 bd6aa359 2020-07-23 stsp else
1530 bd6aa359 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1531 bd6aa359 2020-07-23 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1532 bd6aa359 2020-07-23 stsp if (err)
1533 bd6aa359 2020-07-23 stsp goto done;
1534 bd6aa359 2020-07-23 stsp }
1535 d7b62c98 2018-12-27 stsp
1536 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1537 9d31a1d8 2018-03-11 stsp do {
1538 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1539 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1540 9d31a1d8 2018-03-11 stsp if (err)
1541 9d31a1d8 2018-03-11 stsp break;
1542 9d31a1d8 2018-03-11 stsp if (len > 0) {
1543 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1544 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1545 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1546 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1547 b87c6f83 2018-12-24 stsp goto done;
1548 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1549 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1550 b87c6f83 2018-12-24 stsp goto done;
1551 9d31a1d8 2018-03-11 stsp }
1552 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1553 9d31a1d8 2018-03-11 stsp }
1554 9d31a1d8 2018-03-11 stsp } while (len != 0);
1555 9d31a1d8 2018-03-11 stsp
1556 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1557 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1558 816dc654 2019-02-16 stsp goto done;
1559 816dc654 2019-02-16 stsp }
1560 9d31a1d8 2018-03-11 stsp
1561 507dc3bb 2018-12-29 stsp if (update) {
1562 f6d8c0ac 2020-11-09 stsp if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1563 f6d8c0ac 2020-11-09 stsp err = got_error_from_errno2("unlink", ondisk_path);
1564 f6d8c0ac 2020-11-09 stsp goto done;
1565 f6d8c0ac 2020-11-09 stsp }
1566 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1567 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1568 230a42bd 2019-05-11 jcs ondisk_path);
1569 68ed9ba5 2019-02-10 stsp goto done;
1570 68ed9ba5 2019-02-10 stsp }
1571 63df146d 2020-11-09 stsp free(tmppath);
1572 63df146d 2020-11-09 stsp tmppath = NULL;
1573 507dc3bb 2018-12-29 stsp }
1574 507dc3bb 2018-12-29 stsp
1575 9d31a1d8 2018-03-11 stsp done:
1576 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1577 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1578 63df146d 2020-11-09 stsp if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1579 63df146d 2020-11-09 stsp err = got_error_from_errno2("unlink", tmppath);
1580 507dc3bb 2018-12-29 stsp free(tmppath);
1581 6353ad76 2019-02-08 stsp return err;
1582 6353ad76 2019-02-08 stsp }
1583 6353ad76 2019-02-08 stsp
1584 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1585 6353ad76 2019-02-08 stsp static const struct got_error *
1586 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1587 7154f6ce 2019-03-27 stsp {
1588 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1589 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1590 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1591 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1592 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1593 7154f6ce 2019-03-27 stsp };
1594 7154f6ce 2019-03-27 stsp int i = 0;
1595 7154f6ce 2019-03-27 stsp char *line;
1596 7154f6ce 2019-03-27 stsp size_t len;
1597 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1598 7154f6ce 2019-03-27 stsp
1599 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1600 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1601 7154f6ce 2019-03-27 stsp if (line == NULL) {
1602 7154f6ce 2019-03-27 stsp if (feof(f))
1603 7154f6ce 2019-03-27 stsp break;
1604 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1605 7154f6ce 2019-03-27 stsp break;
1606 7154f6ce 2019-03-27 stsp }
1607 7154f6ce 2019-03-27 stsp
1608 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1609 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1610 19332e6d 2019-05-13 stsp == 0)
1611 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1612 7154f6ce 2019-03-27 stsp else
1613 7154f6ce 2019-03-27 stsp i++;
1614 7154f6ce 2019-03-27 stsp }
1615 7154f6ce 2019-03-27 stsp }
1616 7154f6ce 2019-03-27 stsp
1617 7154f6ce 2019-03-27 stsp return err;
1618 e2b1e152 2019-07-13 stsp }
1619 e2b1e152 2019-07-13 stsp
1620 e2b1e152 2019-07-13 stsp static int
1621 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1622 1ebedb77 2019-10-19 stsp {
1623 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1624 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1625 1ebedb77 2019-10-19 stsp }
1626 1ebedb77 2019-10-19 stsp
1627 1ebedb77 2019-10-19 stsp static int
1628 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1629 e2b1e152 2019-07-13 stsp {
1630 0823ffc2 2020-09-10 naddy return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1631 0823ffc2 2020-09-10 naddy ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1632 0823ffc2 2020-09-10 naddy ie->mtime_sec == sb->st_mtim.tv_sec &&
1633 0823ffc2 2020-09-10 naddy ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1634 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1635 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1636 c363b2c1 2019-08-03 stsp }
1637 c363b2c1 2019-08-03 stsp
1638 c363b2c1 2019-08-03 stsp static unsigned char
1639 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1640 c363b2c1 2019-08-03 stsp {
1641 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1642 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1643 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1644 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1645 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1646 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1647 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1648 c363b2c1 2019-08-03 stsp default:
1649 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1650 a919d5c4 2020-07-23 stsp }
1651 a919d5c4 2020-07-23 stsp }
1652 a919d5c4 2020-07-23 stsp
1653 a919d5c4 2020-07-23 stsp static const struct got_error *
1654 f9eec9d5 2020-07-23 stsp get_symlink_modification_status(unsigned char *status,
1655 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1656 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1657 a919d5c4 2020-07-23 stsp {
1658 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1659 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1660 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1661 a919d5c4 2020-07-23 stsp ssize_t elen;
1662 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1663 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1664 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1665 a919d5c4 2020-07-23 stsp
1666 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1667 a919d5c4 2020-07-23 stsp
1668 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1669 a919d5c4 2020-07-23 stsp do {
1670 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1671 a919d5c4 2020-07-23 stsp if (err)
1672 a919d5c4 2020-07-23 stsp return err;
1673 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1674 a919d5c4 2020-07-23 stsp /*
1675 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1676 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1677 a919d5c4 2020-07-23 stsp */
1678 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1679 a919d5c4 2020-07-23 stsp }
1680 a919d5c4 2020-07-23 stsp if (len > 0) {
1681 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1682 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1683 a919d5c4 2020-07-23 stsp len - hdrlen);
1684 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1685 a919d5c4 2020-07-23 stsp hdrlen = 0;
1686 a919d5c4 2020-07-23 stsp }
1687 a919d5c4 2020-07-23 stsp } while (len != 0);
1688 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1689 a919d5c4 2020-07-23 stsp
1690 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1691 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1692 a919d5c4 2020-07-23 stsp if (elen == -1)
1693 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1694 a919d5c4 2020-07-23 stsp } else {
1695 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1696 a919d5c4 2020-07-23 stsp if (elen == -1)
1697 b448fd00 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
1698 c363b2c1 2019-08-03 stsp }
1699 a919d5c4 2020-07-23 stsp
1700 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1701 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1702 a919d5c4 2020-07-23 stsp
1703 a919d5c4 2020-07-23 stsp return NULL;
1704 7154f6ce 2019-03-27 stsp }
1705 7154f6ce 2019-03-27 stsp
1706 7154f6ce 2019-03-27 stsp static const struct got_error *
1707 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1708 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1709 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1710 6353ad76 2019-02-08 stsp {
1711 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1712 6353ad76 2019-02-08 stsp struct got_object_id id;
1713 6353ad76 2019-02-08 stsp size_t hdrlen;
1714 1338848f 2019-12-13 stsp int fd = -1;
1715 6353ad76 2019-02-08 stsp FILE *f = NULL;
1716 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1717 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1718 6353ad76 2019-02-08 stsp size_t flen, blen;
1719 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1720 6353ad76 2019-02-08 stsp
1721 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1722 6353ad76 2019-02-08 stsp
1723 7f91a133 2019-12-13 stsp /*
1724 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1725 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1726 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1727 7f91a133 2019-12-13 stsp */
1728 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1729 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1730 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1731 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1732 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1733 882ef1b9 2019-12-13 stsp else
1734 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1735 882ef1b9 2019-12-13 stsp goto done;
1736 882ef1b9 2019-12-13 stsp }
1737 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1738 3d35a492 2019-12-13 stsp goto done;
1739 3d35a492 2019-12-13 stsp }
1740 7f91a133 2019-12-13 stsp } else {
1741 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1742 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1743 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1744 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1745 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1746 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1747 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1748 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1749 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1750 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1751 3d35a492 2019-12-13 stsp else
1752 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1753 3d35a492 2019-12-13 stsp goto done;
1754 3d35a492 2019-12-13 stsp }
1755 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1756 1338848f 2019-12-13 stsp goto done;
1757 a378724f 2019-02-10 stsp }
1758 a378724f 2019-02-10 stsp }
1759 6353ad76 2019-02-08 stsp
1760 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1761 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1762 1338848f 2019-12-13 stsp goto done;
1763 3f148bc6 2019-07-27 stsp }
1764 efdd40df 2019-07-27 stsp
1765 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1766 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1767 1338848f 2019-12-13 stsp goto done;
1768 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1769 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1770 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1771 1338848f 2019-12-13 stsp goto done;
1772 d00136be 2019-03-26 stsp }
1773 b8f41171 2019-02-10 stsp
1774 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1775 f179e66d 2020-07-23 stsp goto done;
1776 f179e66d 2020-07-23 stsp
1777 f179e66d 2020-07-23 stsp if (S_ISLNK(sb->st_mode) &&
1778 f179e66d 2020-07-23 stsp got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1779 f179e66d 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1780 1338848f 2019-12-13 stsp goto done;
1781 f179e66d 2020-07-23 stsp }
1782 6353ad76 2019-02-08 stsp
1783 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1784 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1785 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1786 c363b2c1 2019-08-03 stsp else
1787 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1788 c363b2c1 2019-08-03 stsp
1789 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1790 6353ad76 2019-02-08 stsp if (err)
1791 1338848f 2019-12-13 stsp goto done;
1792 6353ad76 2019-02-08 stsp
1793 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1794 f9eec9d5 2020-07-23 stsp err = get_symlink_modification_status(status, ie,
1795 f9eec9d5 2020-07-23 stsp abspath, dirfd, de_name, blob);
1796 a919d5c4 2020-07-23 stsp goto done;
1797 a919d5c4 2020-07-23 stsp }
1798 a919d5c4 2020-07-23 stsp
1799 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1800 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1801 ab0d4361 2019-12-13 stsp if (fd == -1) {
1802 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1803 ab0d4361 2019-12-13 stsp goto done;
1804 ab0d4361 2019-12-13 stsp }
1805 3d35a492 2019-12-13 stsp }
1806 3d35a492 2019-12-13 stsp
1807 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1808 6353ad76 2019-02-08 stsp if (f == NULL) {
1809 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1810 6353ad76 2019-02-08 stsp goto done;
1811 6353ad76 2019-02-08 stsp }
1812 1338848f 2019-12-13 stsp fd = -1;
1813 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1814 656b1f76 2019-05-11 jcs for (;;) {
1815 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1816 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1817 6353ad76 2019-02-08 stsp if (err)
1818 7154f6ce 2019-03-27 stsp goto done;
1819 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1820 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1821 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1822 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1823 7154f6ce 2019-03-27 stsp goto done;
1824 80c5c120 2019-02-19 stsp }
1825 4a26d3f8 2020-10-07 stsp if (blen - hdrlen == 0) {
1826 6353ad76 2019-02-08 stsp if (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 } else if (flen == 0) {
1830 4a26d3f8 2020-10-07 stsp if (blen - hdrlen != 0)
1831 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1832 6353ad76 2019-02-08 stsp break;
1833 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1834 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1835 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1836 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1837 6353ad76 2019-02-08 stsp break;
1838 6353ad76 2019-02-08 stsp }
1839 6353ad76 2019-02-08 stsp } else {
1840 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1841 6353ad76 2019-02-08 stsp break;
1842 6353ad76 2019-02-08 stsp }
1843 6353ad76 2019-02-08 stsp hdrlen = 0;
1844 6353ad76 2019-02-08 stsp }
1845 7154f6ce 2019-03-27 stsp
1846 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1847 7154f6ce 2019-03-27 stsp rewind(f);
1848 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1849 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1850 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1851 6353ad76 2019-02-08 stsp done:
1852 6353ad76 2019-02-08 stsp if (blob)
1853 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1854 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1855 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1856 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1857 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1858 9d31a1d8 2018-03-11 stsp return err;
1859 e2b1e152 2019-07-13 stsp }
1860 e2b1e152 2019-07-13 stsp
1861 e2b1e152 2019-07-13 stsp /*
1862 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1863 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1864 e2b1e152 2019-07-13 stsp */
1865 e2b1e152 2019-07-13 stsp static const struct got_error *
1866 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1867 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1868 e2b1e152 2019-07-13 stsp {
1869 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1870 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1871 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1872 e2b1e152 2019-07-13 stsp
1873 e2b1e152 2019-07-13 stsp return NULL;
1874 9d31a1d8 2018-03-11 stsp }
1875 9d31a1d8 2018-03-11 stsp
1876 9d31a1d8 2018-03-11 stsp static const struct got_error *
1877 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1878 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1879 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1880 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1881 0584f854 2019-04-06 stsp void *progress_arg)
1882 9d31a1d8 2018-03-11 stsp {
1883 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1884 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1885 6353ad76 2019-02-08 stsp char *ondisk_path;
1886 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1887 b8f41171 2019-02-10 stsp struct stat sb;
1888 9d31a1d8 2018-03-11 stsp
1889 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1890 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1891 6353ad76 2019-02-08 stsp
1892 abb4604f 2019-07-27 stsp if (ie) {
1893 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1894 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1895 a76c42e6 2019-08-03 stsp goto done;
1896 a76c42e6 2019-08-03 stsp }
1897 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1898 7f91a133 2019-12-13 stsp repo);
1899 abb4604f 2019-07-27 stsp if (err)
1900 abb4604f 2019-07-27 stsp goto done;
1901 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1902 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1903 c90c8ce3 2020-07-23 stsp } else {
1904 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1905 c90c8ce3 2020-07-23 stsp status = GOT_STATUS_UNVERSIONED;
1906 c90c8ce3 2020-07-23 stsp }
1907 6353ad76 2019-02-08 stsp
1908 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1909 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1910 b8f41171 2019-02-10 stsp goto done;
1911 b8f41171 2019-02-10 stsp }
1912 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1913 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1914 5036ab18 2020-04-18 stsp path);
1915 5036ab18 2020-04-18 stsp goto done;
1916 5036ab18 2020-04-18 stsp }
1917 b8f41171 2019-02-10 stsp
1918 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1919 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1920 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1921 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1922 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1923 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1924 e2b1e152 2019-07-13 stsp if (err)
1925 e2b1e152 2019-07-13 stsp goto done;
1926 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1927 b8f41171 2019-02-10 stsp path);
1928 6353ad76 2019-02-08 stsp goto done;
1929 a378724f 2019-02-10 stsp }
1930 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1931 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1932 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1933 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1934 b8f41171 2019-02-10 stsp goto done;
1935 e2b1e152 2019-07-13 stsp }
1936 9d31a1d8 2018-03-11 stsp }
1937 9d31a1d8 2018-03-11 stsp
1938 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1939 8da9e5f4 2019-01-12 stsp if (err)
1940 6353ad76 2019-02-08 stsp goto done;
1941 512f0d0e 2019-01-02 stsp
1942 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1943 234035bc 2019-06-01 stsp int update_timestamps;
1944 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1945 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1946 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1947 234035bc 2019-06-01 stsp struct got_object_id id2;
1948 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1949 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1950 234035bc 2019-06-01 stsp if (err)
1951 f69721c3 2019-10-21 stsp goto done;
1952 f69721c3 2019-10-21 stsp }
1953 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1954 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1955 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1956 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1957 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1958 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1959 f69721c3 2019-10-21 stsp goto done;
1960 f69721c3 2019-10-21 stsp }
1961 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1962 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1963 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1964 234035bc 2019-06-01 stsp goto done;
1965 f69721c3 2019-10-21 stsp }
1966 234035bc 2019-06-01 stsp }
1967 dfe9fba0 2020-07-23 stsp if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1968 36bf999c 2020-07-23 stsp char *link_target;
1969 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target, blob);
1970 36bf999c 2020-07-23 stsp if (err)
1971 36bf999c 2020-07-23 stsp goto done;
1972 36bf999c 2020-07-23 stsp err = merge_symlink(worktree, blob2, ondisk_path, path,
1973 36bf999c 2020-07-23 stsp label_orig, link_target, worktree->base_commit_id,
1974 36bf999c 2020-07-23 stsp repo, progress_cb, progress_arg);
1975 36bf999c 2020-07-23 stsp free(link_target);
1976 993e2a1b 2020-07-23 stsp } else {
1977 993e2a1b 2020-07-23 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1978 993e2a1b 2020-07-23 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1979 993e2a1b 2020-07-23 stsp worktree->base_commit_id, repo,
1980 993e2a1b 2020-07-23 stsp progress_cb, progress_arg);
1981 993e2a1b 2020-07-23 stsp }
1982 f69721c3 2019-10-21 stsp free(label_orig);
1983 234035bc 2019-06-01 stsp if (blob2)
1984 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1985 909d120e 2019-09-22 stsp if (err)
1986 909d120e 2019-09-22 stsp goto done;
1987 234035bc 2019-06-01 stsp /*
1988 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1989 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1990 234035bc 2019-06-01 stsp * unmodified files again.
1991 234035bc 2019-06-01 stsp */
1992 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1993 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1994 234035bc 2019-06-01 stsp update_timestamps);
1995 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1996 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1997 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1998 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1999 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2000 1ee397ad 2019-07-12 stsp if (err)
2001 1ee397ad 2019-07-12 stsp goto done;
2002 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2003 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
2004 13d9040b 2019-03-27 stsp if (err)
2005 13d9040b 2019-03-27 stsp goto done;
2006 13d9040b 2019-03-27 stsp } else {
2007 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2008 2e1fa222 2020-07-23 stsp if (S_ISLNK(te->mode)) {
2009 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink, worktree,
2010 2e1fa222 2020-07-23 stsp ondisk_path, path, blob,
2011 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_MISSING, 0,
2012 c90c8ce3 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2013 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2014 2e1fa222 2020-07-23 stsp } else {
2015 2e1fa222 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
2016 2e1fa222 2020-07-23 stsp te->mode, sb.st_mode, blob,
2017 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_MISSING, 0, 0,
2018 3b9f0f87 2020-07-23 stsp status == GOT_STATUS_UNVERSIONED, repo,
2019 2e1fa222 2020-07-23 stsp progress_cb, progress_arg);
2020 2e1fa222 2020-07-23 stsp }
2021 13d9040b 2019-03-27 stsp if (err)
2022 13d9040b 2019-03-27 stsp goto done;
2023 65b05cec 2020-07-23 stsp
2024 054041d0 2020-06-24 stsp if (ie) {
2025 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2026 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
2027 054041d0 2020-06-24 stsp } else {
2028 65b05cec 2020-07-23 stsp err = create_fileindex_entry(&ie, fileindex,
2029 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
2030 054041d0 2020-06-24 stsp &blob->id);
2031 054041d0 2020-06-24 stsp }
2032 13d9040b 2019-03-27 stsp if (err)
2033 13d9040b 2019-03-27 stsp goto done;
2034 65b05cec 2020-07-23 stsp
2035 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2036 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2037 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2038 2e1fa222 2020-07-23 stsp }
2039 13d9040b 2019-03-27 stsp }
2040 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
2041 6353ad76 2019-02-08 stsp done:
2042 6353ad76 2019-02-08 stsp free(ondisk_path);
2043 8da9e5f4 2019-01-12 stsp return err;
2044 90285c3b 2019-01-08 stsp }
2045 90285c3b 2019-01-08 stsp
2046 90285c3b 2019-01-08 stsp static const struct got_error *
2047 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
2048 90285c3b 2019-01-08 stsp {
2049 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
2050 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
2051 90285c3b 2019-01-08 stsp
2052 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2053 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2054 90285c3b 2019-01-08 stsp
2055 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
2056 c97665ae 2019-01-13 stsp if (errno != ENOENT)
2057 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
2058 c97665ae 2019-01-13 stsp } else {
2059 fddefe3b 2020-10-20 stsp size_t root_len = strlen(root_path);
2060 fddefe3b 2020-10-20 stsp do {
2061 fddefe3b 2020-10-20 stsp char *parent;
2062 fddefe3b 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
2063 fddefe3b 2020-10-20 stsp if (err)
2064 2513f20a 2020-10-20 stsp break;
2065 fddefe3b 2020-10-20 stsp free(ondisk_path);
2066 fddefe3b 2020-10-20 stsp ondisk_path = parent;
2067 fddefe3b 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
2068 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
2069 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
2070 fddefe3b 2020-10-20 stsp ondisk_path);
2071 90285c3b 2019-01-08 stsp break;
2072 90285c3b 2019-01-08 stsp }
2073 fddefe3b 2020-10-20 stsp } while (got_path_cmp(ondisk_path, root_path,
2074 fddefe3b 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
2075 90285c3b 2019-01-08 stsp }
2076 90285c3b 2019-01-08 stsp free(ondisk_path);
2077 90285c3b 2019-01-08 stsp return err;
2078 512f0d0e 2019-01-02 stsp }
2079 512f0d0e 2019-01-02 stsp
2080 708d8e67 2019-03-27 stsp static const struct got_error *
2081 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2082 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
2083 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
2084 708d8e67 2019-03-27 stsp {
2085 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
2086 708d8e67 2019-03-27 stsp unsigned char status;
2087 708d8e67 2019-03-27 stsp struct stat sb;
2088 708d8e67 2019-03-27 stsp char *ondisk_path;
2089 708d8e67 2019-03-27 stsp
2090 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2091 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2092 a76c42e6 2019-08-03 stsp
2093 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2094 708d8e67 2019-03-27 stsp == -1)
2095 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2096 708d8e67 2019-03-27 stsp
2097 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2098 708d8e67 2019-03-27 stsp if (err)
2099 5a58a424 2020-06-23 stsp goto done;
2100 708d8e67 2019-03-27 stsp
2101 993e2a1b 2020-07-23 stsp if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2102 993e2a1b 2020-07-23 stsp char ondisk_target[PATH_MAX];
2103 993e2a1b 2020-07-23 stsp ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2104 993e2a1b 2020-07-23 stsp sizeof(ondisk_target));
2105 993e2a1b 2020-07-23 stsp if (ondisk_len == -1) {
2106 993e2a1b 2020-07-23 stsp err = got_error_from_errno2("readlink", ondisk_path);
2107 993e2a1b 2020-07-23 stsp goto done;
2108 993e2a1b 2020-07-23 stsp }
2109 993e2a1b 2020-07-23 stsp ondisk_target[ondisk_len] = '\0';
2110 993e2a1b 2020-07-23 stsp err = install_symlink_conflict(NULL, worktree->base_commit_id,
2111 993e2a1b 2020-07-23 stsp NULL, NULL, /* XXX pass common ancestor info? */
2112 993e2a1b 2020-07-23 stsp ondisk_target, ondisk_path);
2113 993e2a1b 2020-07-23 stsp if (err)
2114 993e2a1b 2020-07-23 stsp goto done;
2115 993e2a1b 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2116 993e2a1b 2020-07-23 stsp ie->path);
2117 993e2a1b 2020-07-23 stsp goto done;
2118 993e2a1b 2020-07-23 stsp }
2119 993e2a1b 2020-07-23 stsp
2120 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2121 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
2122 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2123 1ee397ad 2019-07-12 stsp if (err)
2124 5a58a424 2020-06-23 stsp goto done;
2125 fc6346c4 2019-03-27 stsp /*
2126 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
2127 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
2128 fc6346c4 2019-03-27 stsp */
2129 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
2130 fc6346c4 2019-03-27 stsp 0);
2131 fc6346c4 2019-03-27 stsp } else {
2132 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2133 1ee397ad 2019-07-12 stsp if (err)
2134 5a58a424 2020-06-23 stsp goto done;
2135 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
2136 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
2137 fc6346c4 2019-03-27 stsp if (err)
2138 5a58a424 2020-06-23 stsp goto done;
2139 fc6346c4 2019-03-27 stsp }
2140 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
2141 708d8e67 2019-03-27 stsp }
2142 5a58a424 2020-06-23 stsp done:
2143 5a58a424 2020-06-23 stsp free(ondisk_path);
2144 fc6346c4 2019-03-27 stsp return err;
2145 708d8e67 2019-03-27 stsp }
2146 708d8e67 2019-03-27 stsp
2147 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
2148 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
2149 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
2150 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
2151 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
2152 8da9e5f4 2019-01-12 stsp void *progress_arg;
2153 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2154 8da9e5f4 2019-01-12 stsp void *cancel_arg;
2155 8da9e5f4 2019-01-12 stsp };
2156 8da9e5f4 2019-01-12 stsp
2157 512f0d0e 2019-01-02 stsp static const struct got_error *
2158 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
2159 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
2160 512f0d0e 2019-01-02 stsp {
2161 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2162 0584f854 2019-04-06 stsp
2163 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2164 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2165 512f0d0e 2019-01-02 stsp
2166 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
2167 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
2168 8da9e5f4 2019-01-12 stsp }
2169 512f0d0e 2019-01-02 stsp
2170 8da9e5f4 2019-01-12 stsp static const struct got_error *
2171 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2172 8da9e5f4 2019-01-12 stsp {
2173 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2174 7a9df742 2019-01-08 stsp
2175 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2176 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2177 0584f854 2019-04-06 stsp
2178 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
2179 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2180 9d31a1d8 2018-03-11 stsp }
2181 9d31a1d8 2018-03-11 stsp
2182 9d31a1d8 2018-03-11 stsp static const struct got_error *
2183 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2184 9d31a1d8 2018-03-11 stsp {
2185 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
2186 8da9e5f4 2019-01-12 stsp const struct got_error *err;
2187 8da9e5f4 2019-01-12 stsp char *path;
2188 9d31a1d8 2018-03-11 stsp
2189 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2190 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2191 0584f854 2019-04-06 stsp
2192 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2193 63c5ca5d 2019-08-24 stsp return NULL;
2194 63c5ca5d 2019-08-24 stsp
2195 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
2196 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
2197 8da9e5f4 2019-01-12 stsp == -1)
2198 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2199 9d31a1d8 2018-03-11 stsp
2200 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
2201 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
2202 8da9e5f4 2019-01-12 stsp else
2203 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2204 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
2205 9d31a1d8 2018-03-11 stsp
2206 8da9e5f4 2019-01-12 stsp free(path);
2207 0cd1c46a 2019-03-11 stsp return err;
2208 0cd1c46a 2019-03-11 stsp }
2209 0cd1c46a 2019-03-11 stsp
2210 b2118c49 2020-07-28 stsp const struct got_error *
2211 b2118c49 2020-07-28 stsp got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2212 b2118c49 2020-07-28 stsp {
2213 b2118c49 2020-07-28 stsp uint32_t uuid_status;
2214 b2118c49 2020-07-28 stsp
2215 b2118c49 2020-07-28 stsp uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2216 b2118c49 2020-07-28 stsp if (uuid_status != uuid_s_ok) {
2217 b2118c49 2020-07-28 stsp *uuidstr = NULL;
2218 b2118c49 2020-07-28 stsp return got_error_uuid(uuid_status, "uuid_to_string");
2219 b2118c49 2020-07-28 stsp }
2220 b2118c49 2020-07-28 stsp
2221 b2118c49 2020-07-28 stsp return NULL;
2222 b2118c49 2020-07-28 stsp }
2223 b2118c49 2020-07-28 stsp
2224 818c7501 2019-07-11 stsp static const struct got_error *
2225 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2226 0cd1c46a 2019-03-11 stsp {
2227 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
2228 0647c563 2019-03-11 stsp char *uuidstr = NULL;
2229 0cd1c46a 2019-03-11 stsp
2230 517bab73 2019-03-11 stsp *refname = NULL;
2231 517bab73 2019-03-11 stsp
2232 b2118c49 2020-07-28 stsp err = got_worktree_get_uuid(&uuidstr, worktree);
2233 b2118c49 2020-07-28 stsp if (err)
2234 b2118c49 2020-07-28 stsp return err;
2235 0cd1c46a 2019-03-11 stsp
2236 b2118c49 2020-07-28 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2237 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2238 0647c563 2019-03-11 stsp *refname = NULL;
2239 0cd1c46a 2019-03-11 stsp }
2240 517bab73 2019-03-11 stsp free(uuidstr);
2241 517bab73 2019-03-11 stsp return err;
2242 517bab73 2019-03-11 stsp }
2243 517bab73 2019-03-11 stsp
2244 818c7501 2019-07-11 stsp const struct got_error *
2245 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2246 818c7501 2019-07-11 stsp {
2247 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2248 818c7501 2019-07-11 stsp }
2249 818c7501 2019-07-11 stsp
2250 818c7501 2019-07-11 stsp static const struct got_error *
2251 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2252 818c7501 2019-07-11 stsp {
2253 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2254 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2255 818c7501 2019-07-11 stsp }
2256 818c7501 2019-07-11 stsp
2257 818c7501 2019-07-11 stsp static const struct got_error *
2258 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2259 818c7501 2019-07-11 stsp {
2260 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2261 818c7501 2019-07-11 stsp }
2262 818c7501 2019-07-11 stsp
2263 818c7501 2019-07-11 stsp static const struct got_error *
2264 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2265 818c7501 2019-07-11 stsp {
2266 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2267 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2268 818c7501 2019-07-11 stsp }
2269 818c7501 2019-07-11 stsp
2270 818c7501 2019-07-11 stsp static const struct got_error *
2271 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2272 818c7501 2019-07-11 stsp {
2273 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
2274 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2275 0ebf8283 2019-07-24 stsp }
2276 0ebf8283 2019-07-24 stsp
2277 0ebf8283 2019-07-24 stsp static const struct got_error *
2278 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2279 0ebf8283 2019-07-24 stsp {
2280 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2281 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2282 0ebf8283 2019-07-24 stsp }
2283 0ebf8283 2019-07-24 stsp
2284 0ebf8283 2019-07-24 stsp static const struct got_error *
2285 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2286 0ebf8283 2019-07-24 stsp {
2287 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2288 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2289 0ebf8283 2019-07-24 stsp }
2290 0ebf8283 2019-07-24 stsp
2291 0ebf8283 2019-07-24 stsp static const struct got_error *
2292 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2293 0ebf8283 2019-07-24 stsp {
2294 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2295 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2296 818c7501 2019-07-11 stsp }
2297 818c7501 2019-07-11 stsp
2298 0ebf8283 2019-07-24 stsp static const struct got_error *
2299 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2300 0ebf8283 2019-07-24 stsp {
2301 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
2302 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2303 0ebf8283 2019-07-24 stsp }
2304 818c7501 2019-07-11 stsp
2305 0ebf8283 2019-07-24 stsp const struct got_error *
2306 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
2307 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
2308 0ebf8283 2019-07-24 stsp {
2309 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
2310 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2311 0ebf8283 2019-07-24 stsp *path = NULL;
2312 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
2313 0ebf8283 2019-07-24 stsp }
2314 0ebf8283 2019-07-24 stsp return NULL;
2315 0ebf8283 2019-07-24 stsp }
2316 0ebf8283 2019-07-24 stsp
2317 517bab73 2019-03-11 stsp /*
2318 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
2319 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
2320 517bab73 2019-03-11 stsp */
2321 517bab73 2019-03-11 stsp static const struct got_error *
2322 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2323 517bab73 2019-03-11 stsp {
2324 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
2325 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
2326 517bab73 2019-03-11 stsp char *refname;
2327 517bab73 2019-03-11 stsp
2328 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
2329 517bab73 2019-03-11 stsp if (err)
2330 517bab73 2019-03-11 stsp return err;
2331 0cd1c46a 2019-03-11 stsp
2332 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2333 0cd1c46a 2019-03-11 stsp if (err)
2334 0cd1c46a 2019-03-11 stsp goto done;
2335 0cd1c46a 2019-03-11 stsp
2336 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
2337 0cd1c46a 2019-03-11 stsp done:
2338 0cd1c46a 2019-03-11 stsp free(refname);
2339 0cd1c46a 2019-03-11 stsp if (ref)
2340 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
2341 3e3a69f1 2019-07-25 stsp return err;
2342 3e3a69f1 2019-07-25 stsp }
2343 3e3a69f1 2019-07-25 stsp
2344 3e3a69f1 2019-07-25 stsp static const struct got_error *
2345 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2346 3e3a69f1 2019-07-25 stsp {
2347 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
2348 3e3a69f1 2019-07-25 stsp
2349 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2350 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2351 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
2352 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
2353 3e3a69f1 2019-07-25 stsp }
2354 9d31a1d8 2018-03-11 stsp return err;
2355 9d31a1d8 2018-03-11 stsp }
2356 9d31a1d8 2018-03-11 stsp
2357 3e3a69f1 2019-07-25 stsp
2358 ebf99748 2019-05-09 stsp static const struct got_error *
2359 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2360 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
2361 ebf99748 2019-05-09 stsp {
2362 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
2363 ebf99748 2019-05-09 stsp FILE *index = NULL;
2364 0cd1c46a 2019-03-11 stsp
2365 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2366 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
2367 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
2368 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
2369 ebf99748 2019-05-09 stsp
2370 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
2371 3e3a69f1 2019-07-25 stsp if (err)
2372 ebf99748 2019-05-09 stsp goto done;
2373 ebf99748 2019-05-09 stsp
2374 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
2375 ebf99748 2019-05-09 stsp if (index == NULL) {
2376 ebf99748 2019-05-09 stsp if (errno != ENOENT)
2377 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
2378 ebf99748 2019-05-09 stsp } else {
2379 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
2380 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
2381 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2382 ebf99748 2019-05-09 stsp }
2383 ebf99748 2019-05-09 stsp done:
2384 ebf99748 2019-05-09 stsp if (err) {
2385 ebf99748 2019-05-09 stsp free(*fileindex_path);
2386 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
2387 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
2388 ebf99748 2019-05-09 stsp *fileindex = NULL;
2389 ebf99748 2019-05-09 stsp }
2390 ebf99748 2019-05-09 stsp return err;
2391 ebf99748 2019-05-09 stsp }
2392 c932eeeb 2019-05-22 stsp
2393 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
2394 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
2395 c932eeeb 2019-05-22 stsp const char *path;
2396 c932eeeb 2019-05-22 stsp size_t path_len;
2397 c932eeeb 2019-05-22 stsp const char *entry_name;
2398 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
2399 a484d721 2019-06-10 stsp void *progress_arg;
2400 c932eeeb 2019-05-22 stsp };
2401 ebf99748 2019-05-09 stsp
2402 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
2403 c932eeeb 2019-05-22 stsp static const struct got_error *
2404 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2405 c932eeeb 2019-05-22 stsp {
2406 1ee397ad 2019-07-12 stsp const struct got_error *err;
2407 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
2408 c932eeeb 2019-05-22 stsp
2409 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2410 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2411 c932eeeb 2019-05-22 stsp return NULL;
2412 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2413 102ff934 2019-06-11 stsp return NULL;
2414 102ff934 2019-06-11 stsp
2415 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2416 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2417 c932eeeb 2019-05-22 stsp return NULL;
2418 c932eeeb 2019-05-22 stsp
2419 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2420 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2421 edd02c5e 2019-07-11 stsp ie->path);
2422 1ee397ad 2019-07-12 stsp if (err)
2423 1ee397ad 2019-07-12 stsp return err;
2424 1ee397ad 2019-07-12 stsp }
2425 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2426 c932eeeb 2019-05-22 stsp return NULL;
2427 9c6338c4 2019-06-02 stsp }
2428 9c6338c4 2019-06-02 stsp
2429 9c6338c4 2019-06-02 stsp static const struct got_error *
2430 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2431 9c6338c4 2019-06-02 stsp {
2432 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2433 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2434 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2435 867630bb 2020-01-17 stsp struct timespec timeout;
2436 9c6338c4 2019-06-02 stsp
2437 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2438 9c6338c4 2019-06-02 stsp fileindex_path);
2439 9c6338c4 2019-06-02 stsp if (err)
2440 9c6338c4 2019-06-02 stsp goto done;
2441 9c6338c4 2019-06-02 stsp
2442 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2443 9c6338c4 2019-06-02 stsp if (err)
2444 9c6338c4 2019-06-02 stsp goto done;
2445 9c6338c4 2019-06-02 stsp
2446 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2447 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2448 9c6338c4 2019-06-02 stsp fileindex_path);
2449 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2450 9c6338c4 2019-06-02 stsp }
2451 867630bb 2020-01-17 stsp
2452 867630bb 2020-01-17 stsp /*
2453 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2454 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2455 867630bb 2020-01-17 stsp * was recorded in the file index.
2456 867630bb 2020-01-17 stsp */
2457 62d463ca 2020-10-20 naddy timeout.tv_sec = 0;
2458 62d463ca 2020-10-20 naddy timeout.tv_nsec = 1;
2459 62d463ca 2020-10-20 naddy nanosleep(&timeout, NULL);
2460 9c6338c4 2019-06-02 stsp done:
2461 9c6338c4 2019-06-02 stsp if (new_index)
2462 9c6338c4 2019-06-02 stsp fclose(new_index);
2463 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2464 9c6338c4 2019-06-02 stsp return err;
2465 c932eeeb 2019-05-22 stsp }
2466 6ced4176 2019-07-12 stsp
2467 6ced4176 2019-07-12 stsp static const struct got_error *
2468 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2469 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2470 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2471 6ced4176 2019-07-12 stsp {
2472 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2473 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2474 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2475 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2476 6ced4176 2019-07-12 stsp
2477 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2478 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2479 6ced4176 2019-07-12 stsp *tree_id = NULL;
2480 6ced4176 2019-07-12 stsp
2481 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2482 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2483 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2484 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2485 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2486 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2487 6ced4176 2019-07-12 stsp goto done;
2488 6ced4176 2019-07-12 stsp }
2489 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2490 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2491 6ced4176 2019-07-12 stsp if (err)
2492 6ced4176 2019-07-12 stsp goto done;
2493 6ced4176 2019-07-12 stsp return NULL;
2494 6ced4176 2019-07-12 stsp }
2495 6ced4176 2019-07-12 stsp
2496 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2497 6ced4176 2019-07-12 stsp
2498 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2499 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2500 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2501 6ced4176 2019-07-12 stsp goto done;
2502 6ced4176 2019-07-12 stsp }
2503 6ced4176 2019-07-12 stsp
2504 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2505 6ced4176 2019-07-12 stsp in_repo_path);
2506 6ced4176 2019-07-12 stsp if (err)
2507 6ced4176 2019-07-12 stsp goto done;
2508 6ced4176 2019-07-12 stsp
2509 6ced4176 2019-07-12 stsp free(in_repo_path);
2510 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2511 6ced4176 2019-07-12 stsp
2512 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2513 6ced4176 2019-07-12 stsp if (err)
2514 6ced4176 2019-07-12 stsp goto done;
2515 c932eeeb 2019-05-22 stsp
2516 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2517 6ced4176 2019-07-12 stsp /* Check out a single file. */
2518 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2519 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2520 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2521 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2522 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2523 6ced4176 2019-07-12 stsp goto done;
2524 6ced4176 2019-07-12 stsp }
2525 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2526 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2527 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2528 6ced4176 2019-07-12 stsp goto done;
2529 6ced4176 2019-07-12 stsp }
2530 6ced4176 2019-07-12 stsp } else {
2531 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2532 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2533 6ced4176 2019-07-12 stsp if (err)
2534 6ced4176 2019-07-12 stsp return err;
2535 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2536 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2537 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2538 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
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 err = got_object_id_by_path(tree_id, repo,
2543 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2544 6ced4176 2019-07-12 stsp } else {
2545 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2546 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2547 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2548 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2549 6ced4176 2019-07-12 stsp goto done;
2550 6ced4176 2019-07-12 stsp }
2551 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2552 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2553 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2554 6ced4176 2019-07-12 stsp goto done;
2555 6ced4176 2019-07-12 stsp }
2556 6ced4176 2019-07-12 stsp }
2557 6ced4176 2019-07-12 stsp done:
2558 6ced4176 2019-07-12 stsp free(id);
2559 6ced4176 2019-07-12 stsp free(in_repo_path);
2560 6ced4176 2019-07-12 stsp if (err) {
2561 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2562 6ced4176 2019-07-12 stsp free(*tree_relpath);
2563 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2564 6ced4176 2019-07-12 stsp free(*tree_id);
2565 6ced4176 2019-07-12 stsp *tree_id = NULL;
2566 6ced4176 2019-07-12 stsp }
2567 07ed472d 2019-07-12 stsp return err;
2568 07ed472d 2019-07-12 stsp }
2569 07ed472d 2019-07-12 stsp
2570 07ed472d 2019-07-12 stsp static const struct got_error *
2571 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2572 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2573 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2574 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2575 07ed472d 2019-07-12 stsp {
2576 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2577 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2578 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2579 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2580 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2581 07ed472d 2019-07-12 stsp
2582 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2583 7f47418f 2019-12-20 stsp if (err) {
2584 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2585 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2586 7f47418f 2019-12-20 stsp goto done;
2587 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2588 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2589 7f47418f 2019-12-20 stsp if (err)
2590 7f47418f 2019-12-20 stsp return err;
2591 7f47418f 2019-12-20 stsp }
2592 07ed472d 2019-07-12 stsp
2593 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2594 62d463ca 2020-10-20 naddy worktree->base_commit_id);
2595 07ed472d 2019-07-12 stsp if (err)
2596 07ed472d 2019-07-12 stsp goto done;
2597 07ed472d 2019-07-12 stsp
2598 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2599 07ed472d 2019-07-12 stsp if (err)
2600 07ed472d 2019-07-12 stsp goto done;
2601 07ed472d 2019-07-12 stsp
2602 07ed472d 2019-07-12 stsp if (entry_name &&
2603 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2604 b66cd6f3 2020-07-31 stsp err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2605 07ed472d 2019-07-12 stsp goto done;
2606 07ed472d 2019-07-12 stsp }
2607 07ed472d 2019-07-12 stsp
2608 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2609 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2610 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2611 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2612 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2613 07ed472d 2019-07-12 stsp arg.repo = repo;
2614 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2615 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2616 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2617 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2618 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2619 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2620 07ed472d 2019-07-12 stsp done:
2621 07ed472d 2019-07-12 stsp if (tree)
2622 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2623 07ed472d 2019-07-12 stsp if (commit)
2624 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2625 6ced4176 2019-07-12 stsp return err;
2626 6ced4176 2019-07-12 stsp }
2627 6ced4176 2019-07-12 stsp
2628 9d31a1d8 2018-03-11 stsp const struct got_error *
2629 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2630 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2631 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2632 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2633 9d31a1d8 2018-03-11 stsp {
2634 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2635 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2636 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2637 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2638 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2639 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2640 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2641 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2642 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2643 f2ea84fa 2019-07-27 stsp int entry_type;
2644 f2ea84fa 2019-07-27 stsp char *relpath;
2645 f2ea84fa 2019-07-27 stsp char *entry_name;
2646 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2647 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2648 9d31a1d8 2018-03-11 stsp
2649 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2650 f2ea84fa 2019-07-27 stsp
2651 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2652 9d31a1d8 2018-03-11 stsp if (err)
2653 9d31a1d8 2018-03-11 stsp return err;
2654 93a30277 2018-12-24 stsp
2655 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2656 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2657 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2658 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2659 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2660 f2ea84fa 2019-07-27 stsp goto done;
2661 f2ea84fa 2019-07-27 stsp }
2662 6ced4176 2019-07-12 stsp
2663 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2664 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2665 f2ea84fa 2019-07-27 stsp if (err) {
2666 f2ea84fa 2019-07-27 stsp free(tpd);
2667 6ced4176 2019-07-12 stsp goto done;
2668 6ced4176 2019-07-12 stsp }
2669 f2ea84fa 2019-07-27 stsp
2670 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2671 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2672 f2ea84fa 2019-07-27 stsp if (err) {
2673 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2674 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2675 f2ea84fa 2019-07-27 stsp free(tpd);
2676 f2ea84fa 2019-07-27 stsp goto done;
2677 f2ea84fa 2019-07-27 stsp }
2678 f2ea84fa 2019-07-27 stsp } else
2679 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2680 f2ea84fa 2019-07-27 stsp
2681 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2682 6ced4176 2019-07-12 stsp }
2683 6ced4176 2019-07-12 stsp
2684 51514078 2018-12-25 stsp /*
2685 51514078 2018-12-25 stsp * Read the file index.
2686 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2687 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2688 51514078 2018-12-25 stsp */
2689 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2690 8da9e5f4 2019-01-12 stsp if (err)
2691 c4cdcb68 2019-04-03 stsp goto done;
2692 c4cdcb68 2019-04-03 stsp
2693 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2694 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2695 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2696 f2ea84fa 2019-07-27 stsp
2697 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2698 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2699 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2700 f2ea84fa 2019-07-27 stsp if (err)
2701 f2ea84fa 2019-07-27 stsp break;
2702 f2ea84fa 2019-07-27 stsp
2703 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2704 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2705 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2706 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2707 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2708 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2709 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2710 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2711 f2ea84fa 2019-07-27 stsp if (err)
2712 f2ea84fa 2019-07-27 stsp break;
2713 f2ea84fa 2019-07-27 stsp
2714 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2715 711d9cd9 2019-07-12 stsp }
2716 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2717 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2718 af12c6b9 2019-06-04 stsp err = sync_err;
2719 9d31a1d8 2018-03-11 stsp done:
2720 9c6338c4 2019-06-02 stsp free(fileindex_path);
2721 edfa7d7f 2018-09-11 stsp if (tree)
2722 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2723 9d31a1d8 2018-03-11 stsp if (commit)
2724 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2725 5ade8233 2019-07-12 stsp if (fileindex)
2726 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2727 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2728 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2729 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2730 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2731 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2732 f2ea84fa 2019-07-27 stsp free(tpd);
2733 f2ea84fa 2019-07-27 stsp }
2734 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2735 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2736 9d31a1d8 2018-03-11 stsp err = unlockerr;
2737 f8d1f275 2019-02-04 stsp return err;
2738 f8d1f275 2019-02-04 stsp }
2739 f8d1f275 2019-02-04 stsp
2740 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2741 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2742 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2743 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2744 234035bc 2019-06-01 stsp void *progress_arg;
2745 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2746 234035bc 2019-06-01 stsp void *cancel_arg;
2747 f69721c3 2019-10-21 stsp const char *label_orig;
2748 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2749 234035bc 2019-06-01 stsp };
2750 234035bc 2019-06-01 stsp
2751 234035bc 2019-06-01 stsp static const struct got_error *
2752 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2753 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2754 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2755 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2756 234035bc 2019-06-01 stsp {
2757 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2758 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2759 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2760 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2761 234035bc 2019-06-01 stsp struct stat sb;
2762 234035bc 2019-06-01 stsp unsigned char status;
2763 234035bc 2019-06-01 stsp int local_changes_subsumed;
2764 234035bc 2019-06-01 stsp
2765 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2766 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2767 d6c87207 2019-08-02 stsp strlen(path2));
2768 1ee397ad 2019-07-12 stsp if (ie == NULL)
2769 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2770 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2771 234035bc 2019-06-01 stsp
2772 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2773 234035bc 2019-06-01 stsp path2) == -1)
2774 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2775 234035bc 2019-06-01 stsp
2776 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2777 7f91a133 2019-12-13 stsp repo);
2778 234035bc 2019-06-01 stsp if (err)
2779 234035bc 2019-06-01 stsp goto done;
2780 234035bc 2019-06-01 stsp
2781 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2782 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2783 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2784 234035bc 2019-06-01 stsp goto done;
2785 234035bc 2019-06-01 stsp }
2786 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2787 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2788 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2789 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2790 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2791 234035bc 2019-06-01 stsp goto done;
2792 234035bc 2019-06-01 stsp }
2793 234035bc 2019-06-01 stsp
2794 af57b12a 2020-07-23 stsp if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2795 36bf999c 2020-07-23 stsp char *link_target2;
2796 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2, blob2);
2797 36bf999c 2020-07-23 stsp if (err)
2798 36bf999c 2020-07-23 stsp goto done;
2799 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob1, ondisk_path,
2800 36bf999c 2020-07-23 stsp path2, a->label_orig, link_target2, a->commit_id2,
2801 36bf999c 2020-07-23 stsp repo, a->progress_cb, a->progress_arg);
2802 36bf999c 2020-07-23 stsp free(link_target2);
2803 af57b12a 2020-07-23 stsp } else {
2804 af57b12a 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2805 af57b12a 2020-07-23 stsp blob1, ondisk_path, path2, sb.st_mode,
2806 af57b12a 2020-07-23 stsp a->label_orig, blob2, a->commit_id2, repo,
2807 af57b12a 2020-07-23 stsp a->progress_cb, a->progress_arg);
2808 af57b12a 2020-07-23 stsp }
2809 234035bc 2019-06-01 stsp } else if (blob1) {
2810 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2811 d6c87207 2019-08-02 stsp strlen(path1));
2812 1ee397ad 2019-07-12 stsp if (ie == NULL)
2813 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2814 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2815 2b92fad7 2019-06-02 stsp
2816 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2817 2b92fad7 2019-06-02 stsp path1) == -1)
2818 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2819 2b92fad7 2019-06-02 stsp
2820 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2821 7f91a133 2019-12-13 stsp repo);
2822 2b92fad7 2019-06-02 stsp if (err)
2823 2b92fad7 2019-06-02 stsp goto done;
2824 2b92fad7 2019-06-02 stsp
2825 2b92fad7 2019-06-02 stsp switch (status) {
2826 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2827 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2828 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2829 1ee397ad 2019-07-12 stsp if (err)
2830 1ee397ad 2019-07-12 stsp goto done;
2831 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2832 2b92fad7 2019-06-02 stsp if (err)
2833 2b92fad7 2019-06-02 stsp goto done;
2834 2b92fad7 2019-06-02 stsp if (ie)
2835 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2836 2b92fad7 2019-06-02 stsp break;
2837 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2838 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2839 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2840 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2841 1ee397ad 2019-07-12 stsp if (err)
2842 1ee397ad 2019-07-12 stsp goto done;
2843 2b92fad7 2019-06-02 stsp if (ie)
2844 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2845 2b92fad7 2019-06-02 stsp break;
2846 0a22ca1a 2020-09-23 stsp case GOT_STATUS_ADD: {
2847 0a22ca1a 2020-09-23 stsp struct got_object_id *id;
2848 0a22ca1a 2020-09-23 stsp FILE *blob1_f;
2849 0a22ca1a 2020-09-23 stsp /*
2850 0a22ca1a 2020-09-23 stsp * Delete the added file only if its content already
2851 0a22ca1a 2020-09-23 stsp * exists in the repository.
2852 0a22ca1a 2020-09-23 stsp */
2853 0a22ca1a 2020-09-23 stsp err = got_object_blob_file_create(&id, &blob1_f, path1);
2854 0a22ca1a 2020-09-23 stsp if (err)
2855 0a22ca1a 2020-09-23 stsp goto done;
2856 0a22ca1a 2020-09-23 stsp if (got_object_id_cmp(id, id1) == 0) {
2857 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2858 0a22ca1a 2020-09-23 stsp GOT_STATUS_DELETE, path1);
2859 0a22ca1a 2020-09-23 stsp if (err)
2860 0a22ca1a 2020-09-23 stsp goto done;
2861 0a22ca1a 2020-09-23 stsp err = remove_ondisk_file(a->worktree->root_path,
2862 0a22ca1a 2020-09-23 stsp path1);
2863 0a22ca1a 2020-09-23 stsp if (err)
2864 0a22ca1a 2020-09-23 stsp goto done;
2865 0a22ca1a 2020-09-23 stsp if (ie)
2866 0a22ca1a 2020-09-23 stsp got_fileindex_entry_remove(a->fileindex,
2867 0a22ca1a 2020-09-23 stsp ie);
2868 0a22ca1a 2020-09-23 stsp } else {
2869 0a22ca1a 2020-09-23 stsp err = (*a->progress_cb)(a->progress_arg,
2870 0a22ca1a 2020-09-23 stsp GOT_STATUS_CANNOT_DELETE, path1);
2871 0a22ca1a 2020-09-23 stsp }
2872 0a22ca1a 2020-09-23 stsp if (fclose(blob1_f) == EOF && err == NULL)
2873 0a22ca1a 2020-09-23 stsp err = got_error_from_errno("fclose");
2874 0a22ca1a 2020-09-23 stsp free(id);
2875 0a22ca1a 2020-09-23 stsp if (err)
2876 0a22ca1a 2020-09-23 stsp goto done;
2877 0a22ca1a 2020-09-23 stsp break;
2878 0a22ca1a 2020-09-23 stsp }
2879 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2880 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2881 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2882 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2883 1ee397ad 2019-07-12 stsp if (err)
2884 1ee397ad 2019-07-12 stsp goto done;
2885 2b92fad7 2019-06-02 stsp break;
2886 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2887 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2888 1ee397ad 2019-07-12 stsp if (err)
2889 1ee397ad 2019-07-12 stsp goto done;
2890 2b92fad7 2019-06-02 stsp break;
2891 2b92fad7 2019-06-02 stsp default:
2892 2b92fad7 2019-06-02 stsp break;
2893 2b92fad7 2019-06-02 stsp }
2894 234035bc 2019-06-01 stsp } else if (blob2) {
2895 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2896 234035bc 2019-06-01 stsp path2) == -1)
2897 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2898 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2899 d6c87207 2019-08-02 stsp strlen(path2));
2900 234035bc 2019-06-01 stsp if (ie) {
2901 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2902 7f91a133 2019-12-13 stsp -1, NULL, repo);
2903 234035bc 2019-06-01 stsp if (err)
2904 234035bc 2019-06-01 stsp goto done;
2905 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2906 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2907 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2908 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2909 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2910 1ee397ad 2019-07-12 stsp status, path2);
2911 234035bc 2019-06-01 stsp goto done;
2912 234035bc 2019-06-01 stsp }
2913 dfe9fba0 2020-07-23 stsp if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2914 36bf999c 2020-07-23 stsp char *link_target2;
2915 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(&link_target2,
2916 36bf999c 2020-07-23 stsp blob2);
2917 36bf999c 2020-07-23 stsp if (err)
2918 36bf999c 2020-07-23 stsp goto done;
2919 526a746f 2020-07-23 stsp err = merge_symlink(a->worktree, NULL,
2920 dfe9fba0 2020-07-23 stsp ondisk_path, path2, a->label_orig,
2921 36bf999c 2020-07-23 stsp link_target2, a->commit_id2, repo,
2922 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
2923 36bf999c 2020-07-23 stsp free(link_target2);
2924 dfe9fba0 2020-07-23 stsp } else if (S_ISREG(sb.st_mode)) {
2925 526a746f 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
2926 526a746f 2020-07-23 stsp a->worktree, NULL, ondisk_path, path2,
2927 526a746f 2020-07-23 stsp sb.st_mode, a->label_orig, blob2,
2928 526a746f 2020-07-23 stsp a->commit_id2, repo, a->progress_cb,
2929 526a746f 2020-07-23 stsp a->progress_arg);
2930 dfe9fba0 2020-07-23 stsp } else {
2931 dfe9fba0 2020-07-23 stsp err = got_error_path(ondisk_path,
2932 dfe9fba0 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
2933 526a746f 2020-07-23 stsp }
2934 dfe9fba0 2020-07-23 stsp if (err)
2935 dfe9fba0 2020-07-23 stsp goto done;
2936 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2937 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2938 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2939 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2940 234035bc 2019-06-01 stsp if (err)
2941 234035bc 2019-06-01 stsp goto done;
2942 234035bc 2019-06-01 stsp }
2943 234035bc 2019-06-01 stsp } else {
2944 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
2945 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2946 2e1fa222 2020-07-23 stsp if (S_ISLNK(mode2)) {
2947 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
2948 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, path2, blob2, 0,
2949 c90c8ce3 2020-07-23 stsp 0, 1, repo, a->progress_cb, a->progress_arg);
2950 2e1fa222 2020-07-23 stsp } else {
2951 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path, path2,
2952 3b9f0f87 2020-07-23 stsp mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2953 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
2954 2e1fa222 2020-07-23 stsp }
2955 234035bc 2019-06-01 stsp if (err)
2956 234035bc 2019-06-01 stsp goto done;
2957 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2958 234035bc 2019-06-01 stsp if (err)
2959 234035bc 2019-06-01 stsp goto done;
2960 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2961 3969253a 2020-03-07 stsp NULL, NULL, 1);
2962 3969253a 2020-03-07 stsp if (err) {
2963 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2964 3969253a 2020-03-07 stsp goto done;
2965 3969253a 2020-03-07 stsp }
2966 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2967 2b92fad7 2019-06-02 stsp if (err) {
2968 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2969 2b92fad7 2019-06-02 stsp goto done;
2970 2b92fad7 2019-06-02 stsp }
2971 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
2972 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
2973 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
2974 2e1fa222 2020-07-23 stsp }
2975 234035bc 2019-06-01 stsp }
2976 234035bc 2019-06-01 stsp }
2977 234035bc 2019-06-01 stsp done:
2978 234035bc 2019-06-01 stsp free(ondisk_path);
2979 234035bc 2019-06-01 stsp return err;
2980 234035bc 2019-06-01 stsp }
2981 234035bc 2019-06-01 stsp
2982 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2983 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2984 234035bc 2019-06-01 stsp struct got_repository *repo;
2985 234035bc 2019-06-01 stsp };
2986 234035bc 2019-06-01 stsp
2987 234035bc 2019-06-01 stsp static const struct got_error *
2988 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2989 234035bc 2019-06-01 stsp {
2990 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2991 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2992 234035bc 2019-06-01 stsp unsigned char status;
2993 234035bc 2019-06-01 stsp struct stat sb;
2994 234035bc 2019-06-01 stsp char *ondisk_path;
2995 234035bc 2019-06-01 stsp
2996 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2997 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2998 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2999 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3000 234035bc 2019-06-01 stsp
3001 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3002 234035bc 2019-06-01 stsp == -1)
3003 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
3004 234035bc 2019-06-01 stsp
3005 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
3006 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3007 234035bc 2019-06-01 stsp if (err)
3008 234035bc 2019-06-01 stsp return err;
3009 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
3010 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
3011 234035bc 2019-06-01 stsp
3012 234035bc 2019-06-01 stsp return NULL;
3013 234035bc 2019-06-01 stsp }
3014 234035bc 2019-06-01 stsp
3015 818c7501 2019-07-11 stsp static const struct got_error *
3016 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3017 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
3018 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
3019 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3020 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3021 234035bc 2019-06-01 stsp {
3022 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
3023 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
3026 f69721c3 2019-10-21 stsp char *label_orig = NULL;
3027 234035bc 2019-06-01 stsp
3028 03415a1a 2019-06-02 stsp if (commit_id1) {
3029 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3030 03415a1a 2019-06-02 stsp worktree->path_prefix);
3031 69d57f3d 2020-07-31 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3032 03415a1a 2019-06-02 stsp goto done;
3033 69d57f3d 2020-07-31 stsp }
3034 69d57f3d 2020-07-31 stsp if (tree_id1) {
3035 69d57f3d 2020-07-31 stsp char *id_str;
3036 03415a1a 2019-06-02 stsp
3037 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3038 03415a1a 2019-06-02 stsp if (err)
3039 03415a1a 2019-06-02 stsp goto done;
3040 f69721c3 2019-10-21 stsp
3041 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
3042 f69721c3 2019-10-21 stsp if (err)
3043 f69721c3 2019-10-21 stsp goto done;
3044 f69721c3 2019-10-21 stsp
3045 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
3046 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
3047 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
3048 f69721c3 2019-10-21 stsp free(id_str);
3049 f69721c3 2019-10-21 stsp goto done;
3050 f69721c3 2019-10-21 stsp }
3051 f69721c3 2019-10-21 stsp free(id_str);
3052 03415a1a 2019-06-02 stsp }
3053 234035bc 2019-06-01 stsp
3054 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3055 234035bc 2019-06-01 stsp worktree->path_prefix);
3056 234035bc 2019-06-01 stsp if (err)
3057 234035bc 2019-06-01 stsp goto done;
3058 234035bc 2019-06-01 stsp
3059 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3060 234035bc 2019-06-01 stsp if (err)
3061 234035bc 2019-06-01 stsp goto done;
3062 234035bc 2019-06-01 stsp
3063 234035bc 2019-06-01 stsp arg.worktree = worktree;
3064 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
3065 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
3066 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
3067 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
3068 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
3069 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
3070 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
3071 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3072 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3073 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3074 af12c6b9 2019-06-04 stsp err = sync_err;
3075 234035bc 2019-06-01 stsp done:
3076 234035bc 2019-06-01 stsp if (tree1)
3077 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
3078 234035bc 2019-06-01 stsp if (tree2)
3079 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
3080 f69721c3 2019-10-21 stsp free(label_orig);
3081 818c7501 2019-07-11 stsp return err;
3082 818c7501 2019-07-11 stsp }
3083 234035bc 2019-06-01 stsp
3084 818c7501 2019-07-11 stsp const struct got_error *
3085 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
3086 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3087 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3088 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3089 818c7501 2019-07-11 stsp {
3090 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
3091 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3092 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
3093 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
3094 818c7501 2019-07-11 stsp
3095 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3096 818c7501 2019-07-11 stsp if (err)
3097 818c7501 2019-07-11 stsp return err;
3098 818c7501 2019-07-11 stsp
3099 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3100 818c7501 2019-07-11 stsp if (err)
3101 818c7501 2019-07-11 stsp goto done;
3102 818c7501 2019-07-11 stsp
3103 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
3104 818c7501 2019-07-11 stsp mok_arg.repo = repo;
3105 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3106 818c7501 2019-07-11 stsp &mok_arg);
3107 818c7501 2019-07-11 stsp if (err)
3108 818c7501 2019-07-11 stsp goto done;
3109 818c7501 2019-07-11 stsp
3110 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3111 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3112 818c7501 2019-07-11 stsp done:
3113 818c7501 2019-07-11 stsp if (fileindex)
3114 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
3115 818c7501 2019-07-11 stsp free(fileindex_path);
3116 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3117 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
3118 234035bc 2019-06-01 stsp err = unlockerr;
3119 234035bc 2019-06-01 stsp return err;
3120 234035bc 2019-06-01 stsp }
3121 234035bc 2019-06-01 stsp
3122 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
3123 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
3124 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
3125 927df6b7 2019-02-10 stsp const char *status_path;
3126 927df6b7 2019-02-10 stsp size_t status_path_len;
3127 f8d1f275 2019-02-04 stsp struct got_repository *repo;
3128 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
3129 f8d1f275 2019-02-04 stsp void *status_arg;
3130 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
3131 f8d1f275 2019-02-04 stsp void *cancel_arg;
3132 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
3133 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
3134 f2a9dc41 2019-12-13 tracey int report_unchanged;
3135 3143d852 2020-06-25 stsp int no_ignores;
3136 f8d1f275 2019-02-04 stsp };
3137 88d0e355 2019-08-03 stsp
3138 f8d1f275 2019-02-04 stsp static const struct got_error *
3139 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3140 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
3141 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3142 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
3143 927df6b7 2019-02-10 stsp {
3144 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
3145 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
3146 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
3147 927df6b7 2019-02-10 stsp struct stat sb;
3148 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
3149 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3150 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
3151 927df6b7 2019-02-10 stsp
3152 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3153 98eaaa12 2019-08-03 stsp if (err)
3154 98eaaa12 2019-08-03 stsp return err;
3155 98eaaa12 2019-08-03 stsp
3156 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
3157 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3158 98eaaa12 2019-08-03 stsp return NULL;
3159 98eaaa12 2019-08-03 stsp
3160 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
3161 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3162 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
3163 98eaaa12 2019-08-03 stsp }
3164 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
3165 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3166 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
3167 927df6b7 2019-02-10 stsp }
3168 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3169 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3170 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3171 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3172 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
3173 98eaaa12 2019-08-03 stsp }
3174 98eaaa12 2019-08-03 stsp
3175 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
3176 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3177 927df6b7 2019-02-10 stsp }
3178 927df6b7 2019-02-10 stsp
3179 927df6b7 2019-02-10 stsp static const struct got_error *
3180 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
3181 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
3182 f8d1f275 2019-02-04 stsp {
3183 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3184 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3185 f8d1f275 2019-02-04 stsp char *abspath;
3186 f8d1f275 2019-02-04 stsp
3187 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3188 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3189 0584f854 2019-04-06 stsp
3190 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
3191 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
3192 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3193 927df6b7 2019-02-10 stsp return NULL;
3194 927df6b7 2019-02-10 stsp
3195 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3196 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3197 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
3198 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3199 f8d1f275 2019-02-04 stsp } else {
3200 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3201 f8d1f275 2019-02-04 stsp de->d_name) == -1)
3202 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3203 f8d1f275 2019-02-04 stsp }
3204 f8d1f275 2019-02-04 stsp
3205 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
3206 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3207 f8d1f275 2019-02-04 stsp free(abspath);
3208 9d31a1d8 2018-03-11 stsp return err;
3209 9d31a1d8 2018-03-11 stsp }
3210 f8d1f275 2019-02-04 stsp
3211 f8d1f275 2019-02-04 stsp static const struct got_error *
3212 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3213 f8d1f275 2019-02-04 stsp {
3214 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3215 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
3216 2ec1f75b 2019-03-26 stsp unsigned char status;
3217 927df6b7 2019-02-10 stsp
3218 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3219 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3220 0584f854 2019-04-06 stsp
3221 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3222 927df6b7 2019-02-10 stsp return NULL;
3223 927df6b7 2019-02-10 stsp
3224 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3225 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3226 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
3227 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
3228 2ec1f75b 2019-03-26 stsp else
3229 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
3230 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3231 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3232 6841da00 2019-08-08 stsp }
3233 6841da00 2019-08-08 stsp
3234 6841da00 2019-08-08 stsp void
3235 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
3236 6841da00 2019-08-08 stsp {
3237 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3238 6841da00 2019-08-08 stsp
3239 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
3240 6841da00 2019-08-08 stsp free((char *)pe->path);
3241 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
3242 6841da00 2019-08-08 stsp }
3243 6841da00 2019-08-08 stsp
3244 6841da00 2019-08-08 stsp void
3245 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
3246 6841da00 2019-08-08 stsp {
3247 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3248 6841da00 2019-08-08 stsp
3249 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
3250 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3251 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3252 6841da00 2019-08-08 stsp free((char *)pe->path);
3253 6841da00 2019-08-08 stsp }
3254 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
3255 6841da00 2019-08-08 stsp }
3256 6841da00 2019-08-08 stsp
3257 6841da00 2019-08-08 stsp static const struct got_error *
3258 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3259 6841da00 2019-08-08 stsp {
3260 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3261 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
3262 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
3263 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
3264 6841da00 2019-08-08 stsp size_t linesize = 0;
3265 6841da00 2019-08-08 stsp ssize_t linelen;
3266 6841da00 2019-08-08 stsp
3267 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
3268 6841da00 2019-08-08 stsp if (ignorelist == NULL)
3269 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
3270 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
3271 6841da00 2019-08-08 stsp
3272 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
3273 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
3274 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
3275 bd8de430 2019-10-04 stsp
3276 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
3277 bd8de430 2019-10-04 stsp if (line[0] == '#')
3278 bd8de430 2019-10-04 stsp continue;
3279 bd8de430 2019-10-04 stsp
3280 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
3281 bd8de430 2019-10-04 stsp if (line[0] == '!')
3282 bd8de430 2019-10-04 stsp continue;
3283 bd8de430 2019-10-04 stsp
3284 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3285 6841da00 2019-08-08 stsp line) == -1) {
3286 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
3287 6841da00 2019-08-08 stsp goto done;
3288 6841da00 2019-08-08 stsp }
3289 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3290 6841da00 2019-08-08 stsp if (err)
3291 6841da00 2019-08-08 stsp goto done;
3292 6841da00 2019-08-08 stsp }
3293 6841da00 2019-08-08 stsp if (ferror(f)) {
3294 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
3295 6841da00 2019-08-08 stsp goto done;
3296 6841da00 2019-08-08 stsp }
3297 6841da00 2019-08-08 stsp
3298 6841da00 2019-08-08 stsp dirpath = strdup(path);
3299 6841da00 2019-08-08 stsp if (dirpath == NULL) {
3300 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
3301 6841da00 2019-08-08 stsp goto done;
3302 6841da00 2019-08-08 stsp }
3303 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3304 6841da00 2019-08-08 stsp done:
3305 6841da00 2019-08-08 stsp free(line);
3306 6841da00 2019-08-08 stsp if (err || pe == NULL) {
3307 6841da00 2019-08-08 stsp free(dirpath);
3308 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
3309 6841da00 2019-08-08 stsp }
3310 6841da00 2019-08-08 stsp return err;
3311 6841da00 2019-08-08 stsp }
3312 6841da00 2019-08-08 stsp
3313 6841da00 2019-08-08 stsp int
3314 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
3315 6841da00 2019-08-08 stsp {
3316 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
3317 bd8de430 2019-10-04 stsp
3318 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
3319 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
3320 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
3321 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
3322 bd8de430 2019-10-04 stsp
3323 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3324 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
3325 6841da00 2019-08-08 stsp
3326 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
3327 bd8de430 2019-10-04 stsp continue;
3328 bd8de430 2019-10-04 stsp pattern += 3;
3329 bd8de430 2019-10-04 stsp p = path;
3330 bd8de430 2019-10-04 stsp while (*p) {
3331 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
3332 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
3333 bd8de430 2019-10-04 stsp /* Retry in next directory. */
3334 bd8de430 2019-10-04 stsp while (*p && *p != '/')
3335 bd8de430 2019-10-04 stsp p++;
3336 bd8de430 2019-10-04 stsp while (*p == '/')
3337 bd8de430 2019-10-04 stsp p++;
3338 bd8de430 2019-10-04 stsp continue;
3339 bd8de430 2019-10-04 stsp }
3340 bd8de430 2019-10-04 stsp return 1;
3341 bd8de430 2019-10-04 stsp }
3342 bd8de430 2019-10-04 stsp }
3343 bd8de430 2019-10-04 stsp }
3344 bd8de430 2019-10-04 stsp
3345 6841da00 2019-08-08 stsp /*
3346 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
3347 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
3348 6841da00 2019-08-08 stsp * ignores backwards.
3349 6841da00 2019-08-08 stsp */
3350 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
3351 6841da00 2019-08-08 stsp while (pe) {
3352 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
3353 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
3354 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
3355 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
3356 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
3357 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
3358 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
3359 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
3360 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
3361 6841da00 2019-08-08 stsp continue;
3362 6841da00 2019-08-08 stsp return 1;
3363 6841da00 2019-08-08 stsp }
3364 6841da00 2019-08-08 stsp }
3365 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3366 6841da00 2019-08-08 stsp }
3367 6841da00 2019-08-08 stsp
3368 6841da00 2019-08-08 stsp return 0;
3369 6841da00 2019-08-08 stsp }
3370 6841da00 2019-08-08 stsp
3371 6841da00 2019-08-08 stsp static const struct got_error *
3372 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3373 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
3374 6841da00 2019-08-08 stsp {
3375 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
3376 6841da00 2019-08-08 stsp char *ignorespath;
3377 886cec17 2019-12-15 stsp int fd = -1;
3378 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
3379 6841da00 2019-08-08 stsp
3380 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3381 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
3382 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
3383 6841da00 2019-08-08 stsp
3384 886cec17 2019-12-15 stsp if (dirfd != -1) {
3385 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3386 886cec17 2019-12-15 stsp if (fd == -1) {
3387 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3388 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
3389 886cec17 2019-12-15 stsp ignorespath);
3390 886cec17 2019-12-15 stsp } else {
3391 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
3392 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
3393 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
3394 886cec17 2019-12-15 stsp ignorespath);
3395 886cec17 2019-12-15 stsp else {
3396 886cec17 2019-12-15 stsp fd = -1;
3397 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3398 886cec17 2019-12-15 stsp }
3399 886cec17 2019-12-15 stsp }
3400 886cec17 2019-12-15 stsp } else {
3401 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
3402 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
3403 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
3404 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
3405 886cec17 2019-12-15 stsp ignorespath);
3406 886cec17 2019-12-15 stsp } else
3407 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
3408 886cec17 2019-12-15 stsp }
3409 6841da00 2019-08-08 stsp
3410 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3411 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
3412 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3413 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
3414 6841da00 2019-08-08 stsp free(ignorespath);
3415 6841da00 2019-08-08 stsp return err;
3416 f8d1f275 2019-02-04 stsp }
3417 f8d1f275 2019-02-04 stsp
3418 f8d1f275 2019-02-04 stsp static const struct got_error *
3419 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3420 f8d1f275 2019-02-04 stsp {
3421 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3422 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
3423 f8d1f275 2019-02-04 stsp char *path = NULL;
3424 f8d1f275 2019-02-04 stsp
3425 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3426 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
3427 0584f854 2019-04-06 stsp
3428 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
3429 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3430 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3431 f8d1f275 2019-02-04 stsp } else {
3432 f8d1f275 2019-02-04 stsp path = de->d_name;
3433 f8d1f275 2019-02-04 stsp }
3434 f8d1f275 2019-02-04 stsp
3435 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
3436 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
3437 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
3438 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3439 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3440 f8d1f275 2019-02-04 stsp if (parent_path[0])
3441 f8d1f275 2019-02-04 stsp free(path);
3442 b72f483a 2019-02-05 stsp return err;
3443 f8d1f275 2019-02-04 stsp }
3444 f8d1f275 2019-02-04 stsp
3445 347d1d3e 2019-07-12 stsp static const struct got_error *
3446 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
3447 3143d852 2020-06-25 stsp {
3448 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
3449 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
3450 3143d852 2020-06-25 stsp
3451 3143d852 2020-06-25 stsp if (a->no_ignores)
3452 3143d852 2020-06-25 stsp return NULL;
3453 3143d852 2020-06-25 stsp
3454 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
3455 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
3456 3143d852 2020-06-25 stsp if (err)
3457 3143d852 2020-06-25 stsp return err;
3458 3143d852 2020-06-25 stsp
3459 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
3460 3143d852 2020-06-25 stsp dirfd, ".gitignore");
3461 3143d852 2020-06-25 stsp
3462 3143d852 2020-06-25 stsp return err;
3463 3143d852 2020-06-25 stsp }
3464 3143d852 2020-06-25 stsp
3465 3143d852 2020-06-25 stsp static const struct got_error *
3466 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
3467 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3468 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
3469 abb4604f 2019-07-27 stsp {
3470 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
3471 abb4604f 2019-07-27 stsp struct stat sb;
3472 abb4604f 2019-07-27 stsp
3473 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3474 abb4604f 2019-07-27 stsp if (ie)
3475 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
3476 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
3477 abb4604f 2019-07-27 stsp
3478 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
3479 abb4604f 2019-07-27 stsp if (errno != ENOENT)
3480 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
3481 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3482 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3483 abb4604f 2019-07-27 stsp return NULL;
3484 abb4604f 2019-07-27 stsp }
3485 abb4604f 2019-07-27 stsp
3486 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3487 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3488 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3489 abb4604f 2019-07-27 stsp
3490 abb4604f 2019-07-27 stsp return NULL;
3491 3143d852 2020-06-25 stsp }
3492 3143d852 2020-06-25 stsp
3493 3143d852 2020-06-25 stsp static const struct got_error *
3494 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3495 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3496 3143d852 2020-06-25 stsp {
3497 3143d852 2020-06-25 stsp const struct got_error *err;
3498 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3499 3143d852 2020-06-25 stsp
3500 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3501 3143d852 2020-06-25 stsp ".cvsignore");
3502 3143d852 2020-06-25 stsp if (err)
3503 3143d852 2020-06-25 stsp return err;
3504 3143d852 2020-06-25 stsp
3505 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3506 3143d852 2020-06-25 stsp ".gitignore");
3507 3143d852 2020-06-25 stsp if (err)
3508 3143d852 2020-06-25 stsp return err;
3509 3143d852 2020-06-25 stsp
3510 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3511 3143d852 2020-06-25 stsp if (err) {
3512 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3513 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3514 3143d852 2020-06-25 stsp return err;
3515 3143d852 2020-06-25 stsp }
3516 3143d852 2020-06-25 stsp for (;;) {
3517 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3518 3143d852 2020-06-25 stsp ".cvsignore");
3519 3143d852 2020-06-25 stsp if (err)
3520 3143d852 2020-06-25 stsp break;
3521 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3522 3143d852 2020-06-25 stsp ".gitignore");
3523 3143d852 2020-06-25 stsp if (err)
3524 3143d852 2020-06-25 stsp break;
3525 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3526 3143d852 2020-06-25 stsp if (err) {
3527 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3528 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3529 3143d852 2020-06-25 stsp break;
3530 3143d852 2020-06-25 stsp }
3531 b737c85a 2020-06-26 stsp free(parent_path);
3532 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3533 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3534 3143d852 2020-06-25 stsp }
3535 3143d852 2020-06-25 stsp
3536 b737c85a 2020-06-26 stsp free(parent_path);
3537 b737c85a 2020-06-26 stsp free(next_parent_path);
3538 3143d852 2020-06-25 stsp return err;
3539 abb4604f 2019-07-27 stsp }
3540 abb4604f 2019-07-27 stsp
3541 abb4604f 2019-07-27 stsp static const struct got_error *
3542 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3543 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3544 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3545 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3546 f2a9dc41 2019-12-13 tracey int report_unchanged)
3547 f8d1f275 2019-02-04 stsp {
3548 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3549 6fc93f37 2019-12-13 stsp int fd = -1;
3550 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3551 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3552 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3553 3143d852 2020-06-25 stsp
3554 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3555 f8d1f275 2019-02-04 stsp
3556 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3557 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3558 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3559 8dc303cc 2019-07-27 stsp
3560 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3561 6fc93f37 2019-12-13 stsp if (fd == -1) {
3562 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3563 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3564 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3565 abb4604f 2019-07-27 stsp else
3566 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3567 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3568 f2a9dc41 2019-12-13 tracey report_unchanged);
3569 abb4604f 2019-07-27 stsp } else {
3570 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3571 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3572 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3573 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3574 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3575 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3576 abb4604f 2019-07-27 stsp arg.status_path = path;
3577 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3578 abb4604f 2019-07-27 stsp arg.repo = repo;
3579 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3580 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3581 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3582 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3583 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3584 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3585 022fae89 2019-12-06 tracey if (!no_ignores) {
3586 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3587 3143d852 2020-06-25 stsp worktree->root_path, path);
3588 3143d852 2020-06-25 stsp if (err)
3589 3143d852 2020-06-25 stsp goto done;
3590 022fae89 2019-12-06 tracey }
3591 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3592 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3593 927df6b7 2019-02-10 stsp }
3594 3143d852 2020-06-25 stsp done:
3595 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3596 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3597 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3598 927df6b7 2019-02-10 stsp free(ondisk_path);
3599 347d1d3e 2019-07-12 stsp return err;
3600 347d1d3e 2019-07-12 stsp }
3601 347d1d3e 2019-07-12 stsp
3602 347d1d3e 2019-07-12 stsp const struct got_error *
3603 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3604 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3605 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3606 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3607 347d1d3e 2019-07-12 stsp {
3608 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3609 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3610 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3611 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3612 347d1d3e 2019-07-12 stsp
3613 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3614 347d1d3e 2019-07-12 stsp if (err)
3615 347d1d3e 2019-07-12 stsp return err;
3616 347d1d3e 2019-07-12 stsp
3617 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3618 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3619 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3620 72ea6654 2019-07-27 stsp if (err)
3621 72ea6654 2019-07-27 stsp break;
3622 72ea6654 2019-07-27 stsp }
3623 f8d1f275 2019-02-04 stsp free(fileindex_path);
3624 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3625 f8d1f275 2019-02-04 stsp return err;
3626 f8d1f275 2019-02-04 stsp }
3627 6c7ab921 2019-03-18 stsp
3628 6c7ab921 2019-03-18 stsp const struct got_error *
3629 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3630 6c7ab921 2019-03-18 stsp const char *arg)
3631 6c7ab921 2019-03-18 stsp {
3632 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3633 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3634 6c7ab921 2019-03-18 stsp size_t len;
3635 00bb5ea0 2020-07-23 stsp struct stat sb;
3636 01740607 2020-11-04 stsp char *abspath = NULL;
3637 01740607 2020-11-04 stsp char canonpath[PATH_MAX];
3638 6c7ab921 2019-03-18 stsp
3639 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3640 6c7ab921 2019-03-18 stsp
3641 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3642 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3643 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3644 00bb5ea0 2020-07-23 stsp
3645 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3646 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3647 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3648 00bb5ea0 2020-07-23 stsp goto done;
3649 00bb5ea0 2020-07-23 stsp }
3650 727173c3 2020-11-06 stsp sb.st_mode = 0;
3651 00bb5ea0 2020-07-23 stsp }
3652 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3653 00bb5ea0 2020-07-23 stsp /*
3654 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3655 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3656 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3657 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3658 00bb5ea0 2020-07-23 stsp */
3659 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3660 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3661 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3662 00bb5ea0 2020-07-23 stsp goto done;
3663 00bb5ea0 2020-07-23 stsp }
3664 00bb5ea0 2020-07-23 stsp
3665 00bb5ea0 2020-07-23 stsp }
3666 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3667 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3668 00bb5ea0 2020-07-23 stsp if (err)
3669 d0710d08 2019-07-22 stsp goto done;
3670 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3671 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3672 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3673 00bb5ea0 2020-07-23 stsp goto done;
3674 00bb5ea0 2020-07-23 stsp }
3675 00bb5ea0 2020-07-23 stsp } else {
3676 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3677 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3678 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3679 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3680 00bb5ea0 2020-07-23 stsp goto done;
3681 00bb5ea0 2020-07-23 stsp }
3682 01740607 2020-11-04 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3683 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3684 01740607 2020-11-04 stsp goto done;
3685 01740607 2020-11-04 stsp }
3686 01740607 2020-11-04 stsp err = got_canonpath(abspath, canonpath,
3687 01740607 2020-11-04 stsp sizeof(canonpath));
3688 01740607 2020-11-04 stsp if (err)
3689 00bb5ea0 2020-07-23 stsp goto done;
3690 01740607 2020-11-04 stsp resolved = strdup(canonpath);
3691 01740607 2020-11-04 stsp if (resolved == NULL) {
3692 01740607 2020-11-04 stsp err = got_error_from_errno("strdup");
3693 01740607 2020-11-04 stsp goto done;
3694 00bb5ea0 2020-07-23 stsp }
3695 d0710d08 2019-07-22 stsp }
3696 d0710d08 2019-07-22 stsp }
3697 6c7ab921 2019-03-18 stsp
3698 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3699 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3700 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3701 6c7ab921 2019-03-18 stsp goto done;
3702 6c7ab921 2019-03-18 stsp }
3703 6c7ab921 2019-03-18 stsp
3704 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3705 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3706 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3707 f6d88e1a 2019-05-29 stsp if (err)
3708 f6d88e1a 2019-05-29 stsp goto done;
3709 f6d88e1a 2019-05-29 stsp } else {
3710 f6d88e1a 2019-05-29 stsp path = strdup("");
3711 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3712 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3713 f6d88e1a 2019-05-29 stsp goto done;
3714 f6d88e1a 2019-05-29 stsp }
3715 6c7ab921 2019-03-18 stsp }
3716 6c7ab921 2019-03-18 stsp
3717 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3718 6c7ab921 2019-03-18 stsp len = strlen(path);
3719 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3720 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3721 6c7ab921 2019-03-18 stsp len--;
3722 6c7ab921 2019-03-18 stsp }
3723 6c7ab921 2019-03-18 stsp done:
3724 01740607 2020-11-04 stsp free(abspath);
3725 6c7ab921 2019-03-18 stsp free(resolved);
3726 d0710d08 2019-07-22 stsp free(cwd);
3727 6c7ab921 2019-03-18 stsp if (err == NULL)
3728 6c7ab921 2019-03-18 stsp *wt_path = path;
3729 6c7ab921 2019-03-18 stsp else
3730 6c7ab921 2019-03-18 stsp free(path);
3731 d00136be 2019-03-26 stsp return err;
3732 d00136be 2019-03-26 stsp }
3733 d00136be 2019-03-26 stsp
3734 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3735 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3736 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3737 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3738 4e68cba3 2019-11-23 stsp void *progress_arg;
3739 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3740 4e68cba3 2019-11-23 stsp };
3741 4e68cba3 2019-11-23 stsp
3742 4e68cba3 2019-11-23 stsp static const struct got_error *
3743 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3744 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3745 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3746 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3747 07f5b47a 2019-06-02 stsp {
3748 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3749 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3750 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3751 6d022e97 2019-08-04 stsp struct stat sb;
3752 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3753 07f5b47a 2019-06-02 stsp
3754 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3755 dbb83fbd 2019-12-12 stsp relpath) == -1)
3756 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3757 dbb83fbd 2019-12-12 stsp
3758 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3759 6d022e97 2019-08-04 stsp if (ie) {
3760 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3761 12463d8b 2019-12-13 stsp de_name, a->repo);
3762 6d022e97 2019-08-04 stsp if (err)
3763 dbb83fbd 2019-12-12 stsp goto done;
3764 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3765 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3766 dbb83fbd 2019-12-12 stsp goto done;
3767 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3768 dbb83fbd 2019-12-12 stsp if (err)
3769 dbb83fbd 2019-12-12 stsp goto done;
3770 6d022e97 2019-08-04 stsp }
3771 07f5b47a 2019-06-02 stsp
3772 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3773 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3774 dbb83fbd 2019-12-12 stsp goto done;
3775 4e68cba3 2019-11-23 stsp }
3776 07f5b47a 2019-06-02 stsp
3777 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3778 4e68cba3 2019-11-23 stsp if (err)
3779 4e68cba3 2019-11-23 stsp goto done;
3780 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3781 3969253a 2020-03-07 stsp if (err) {
3782 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3783 3969253a 2020-03-07 stsp goto done;
3784 3969253a 2020-03-07 stsp }
3785 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3786 07f5b47a 2019-06-02 stsp if (err) {
3787 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3788 4e68cba3 2019-11-23 stsp goto done;
3789 07f5b47a 2019-06-02 stsp }
3790 4e68cba3 2019-11-23 stsp done:
3791 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3792 dbb83fbd 2019-12-12 stsp if (err)
3793 dbb83fbd 2019-12-12 stsp return err;
3794 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3795 dbb83fbd 2019-12-12 stsp return NULL;
3796 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3797 07f5b47a 2019-06-02 stsp }
3798 07f5b47a 2019-06-02 stsp
3799 d00136be 2019-03-26 stsp const struct got_error *
3800 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3801 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3802 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3803 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3804 d00136be 2019-03-26 stsp {
3805 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3806 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3807 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3808 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3809 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3810 d00136be 2019-03-26 stsp
3811 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3812 d00136be 2019-03-26 stsp if (err)
3813 d00136be 2019-03-26 stsp return err;
3814 d00136be 2019-03-26 stsp
3815 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3816 d00136be 2019-03-26 stsp if (err)
3817 d00136be 2019-03-26 stsp goto done;
3818 d00136be 2019-03-26 stsp
3819 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3820 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3821 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3822 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3823 4e68cba3 2019-11-23 stsp saa.repo = repo;
3824 4e68cba3 2019-11-23 stsp
3825 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3826 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3827 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3828 1dd54920 2019-05-11 stsp if (err)
3829 af12c6b9 2019-06-04 stsp break;
3830 1dd54920 2019-05-11 stsp }
3831 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3832 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3833 af12c6b9 2019-06-04 stsp err = sync_err;
3834 d00136be 2019-03-26 stsp done:
3835 fb399478 2019-07-12 stsp free(fileindex_path);
3836 d00136be 2019-03-26 stsp if (fileindex)
3837 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3838 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3839 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3840 d00136be 2019-03-26 stsp err = unlockerr;
3841 6c7ab921 2019-03-18 stsp return err;
3842 6c7ab921 2019-03-18 stsp }
3843 17ed4618 2019-06-02 stsp
3844 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3845 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3846 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3847 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3848 f2a9dc41 2019-12-13 tracey void *progress_arg;
3849 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3850 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3851 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3852 766841c2 2020-08-13 stsp const char *status_codes;
3853 f2a9dc41 2019-12-13 tracey };
3854 f2a9dc41 2019-12-13 tracey
3855 f2a9dc41 2019-12-13 tracey static const struct got_error *
3856 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3857 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3858 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3859 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3860 17ed4618 2019-06-02 stsp {
3861 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3862 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3863 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3864 17ed4618 2019-06-02 stsp struct stat sb;
3865 20a2ad1c 2020-10-20 stsp char *ondisk_path;
3866 17ed4618 2019-06-02 stsp
3867 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3868 17ed4618 2019-06-02 stsp if (ie == NULL)
3869 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3870 2ec1f75b 2019-03-26 stsp
3871 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3872 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3873 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3874 9acbc4fa 2019-08-03 stsp return NULL;
3875 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3876 9acbc4fa 2019-08-03 stsp }
3877 9acbc4fa 2019-08-03 stsp
3878 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3879 f2a9dc41 2019-12-13 tracey relpath) == -1)
3880 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3881 f2a9dc41 2019-12-13 tracey
3882 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3883 12463d8b 2019-12-13 stsp a->repo);
3884 17ed4618 2019-06-02 stsp if (err)
3885 f2a9dc41 2019-12-13 tracey goto done;
3886 17ed4618 2019-06-02 stsp
3887 766841c2 2020-08-13 stsp if (a->status_codes) {
3888 766841c2 2020-08-13 stsp size_t ncodes = strlen(a->status_codes);
3889 766841c2 2020-08-13 stsp int i;
3890 766841c2 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
3891 766841c2 2020-08-13 stsp if (status == a->status_codes[i])
3892 766841c2 2020-08-13 stsp break;
3893 766841c2 2020-08-13 stsp }
3894 766841c2 2020-08-13 stsp if (i == ncodes) {
3895 766841c2 2020-08-13 stsp /* Do not delete files in non-matching status. */
3896 766841c2 2020-08-13 stsp free(ondisk_path);
3897 766841c2 2020-08-13 stsp return NULL;
3898 766841c2 2020-08-13 stsp }
3899 766841c2 2020-08-13 stsp if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3900 766841c2 2020-08-13 stsp a->status_codes[i] != GOT_STATUS_MISSING) {
3901 766841c2 2020-08-13 stsp static char msg[64];
3902 766841c2 2020-08-13 stsp snprintf(msg, sizeof(msg),
3903 766841c2 2020-08-13 stsp "invalid status code '%c'", a->status_codes[i]);
3904 766841c2 2020-08-13 stsp err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3905 766841c2 2020-08-13 stsp goto done;
3906 766841c2 2020-08-13 stsp }
3907 766841c2 2020-08-13 stsp }
3908 766841c2 2020-08-13 stsp
3909 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3910 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3911 f2a9dc41 2019-12-13 tracey goto done;
3912 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3913 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3914 f2a9dc41 2019-12-13 tracey goto done;
3915 f2a9dc41 2019-12-13 tracey }
3916 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3917 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3918 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3919 f2a9dc41 2019-12-13 tracey goto done;
3920 f2a9dc41 2019-12-13 tracey }
3921 17ed4618 2019-06-02 stsp }
3922 17ed4618 2019-06-02 stsp
3923 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3924 20a2ad1c 2020-10-20 stsp size_t root_len;
3925 20a2ad1c 2020-10-20 stsp
3926 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3927 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3928 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3929 12463d8b 2019-12-13 stsp ondisk_path);
3930 12463d8b 2019-12-13 stsp goto done;
3931 12463d8b 2019-12-13 stsp }
3932 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3933 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3934 12463d8b 2019-12-13 stsp goto done;
3935 15341bfd 2020-03-05 tracey }
3936 15341bfd 2020-03-05 tracey
3937 20a2ad1c 2020-10-20 stsp root_len = strlen(a->worktree->root_path);
3938 20a2ad1c 2020-10-20 stsp do {
3939 20a2ad1c 2020-10-20 stsp char *parent;
3940 20a2ad1c 2020-10-20 stsp err = got_path_dirname(&parent, ondisk_path);
3941 20a2ad1c 2020-10-20 stsp if (err)
3942 2513f20a 2020-10-20 stsp goto done;
3943 20a2ad1c 2020-10-20 stsp free(ondisk_path);
3944 20a2ad1c 2020-10-20 stsp ondisk_path = parent;
3945 20a2ad1c 2020-10-20 stsp if (rmdir(ondisk_path) == -1) {
3946 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3947 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3948 20a2ad1c 2020-10-20 stsp ondisk_path);
3949 15341bfd 2020-03-05 tracey break;
3950 15341bfd 2020-03-05 tracey }
3951 20a2ad1c 2020-10-20 stsp } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3952 20a2ad1c 2020-10-20 stsp strlen(ondisk_path), root_len) != 0);
3953 f2a9dc41 2019-12-13 tracey }
3954 17ed4618 2019-06-02 stsp
3955 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3956 f2a9dc41 2019-12-13 tracey done:
3957 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3958 f2a9dc41 2019-12-13 tracey if (err)
3959 f2a9dc41 2019-12-13 tracey return err;
3960 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3961 f2a9dc41 2019-12-13 tracey return NULL;
3962 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3963 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3964 17ed4618 2019-06-02 stsp }
3965 17ed4618 2019-06-02 stsp
3966 2ec1f75b 2019-03-26 stsp const struct got_error *
3967 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3968 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3969 766841c2 2020-08-13 stsp const char *status_codes,
3970 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3971 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3972 2ec1f75b 2019-03-26 stsp {
3973 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3974 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3975 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3976 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3977 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3978 2ec1f75b 2019-03-26 stsp
3979 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3980 2ec1f75b 2019-03-26 stsp if (err)
3981 2ec1f75b 2019-03-26 stsp return err;
3982 2ec1f75b 2019-03-26 stsp
3983 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3984 2ec1f75b 2019-03-26 stsp if (err)
3985 2ec1f75b 2019-03-26 stsp goto done;
3986 f2a9dc41 2019-12-13 tracey
3987 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3988 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3989 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3990 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3991 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3992 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3993 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3994 766841c2 2020-08-13 stsp sda.status_codes = status_codes;
3995 2ec1f75b 2019-03-26 stsp
3996 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3997 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3998 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3999 17ed4618 2019-06-02 stsp if (err)
4000 af12c6b9 2019-06-04 stsp break;
4001 2ec1f75b 2019-03-26 stsp }
4002 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4003 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
4004 af12c6b9 2019-06-04 stsp err = sync_err;
4005 2ec1f75b 2019-03-26 stsp done:
4006 fb399478 2019-07-12 stsp free(fileindex_path);
4007 2ec1f75b 2019-03-26 stsp if (fileindex)
4008 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
4009 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4010 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
4011 2ec1f75b 2019-03-26 stsp err = unlockerr;
4012 33aa809d 2019-08-08 stsp return err;
4013 33aa809d 2019-08-08 stsp }
4014 33aa809d 2019-08-08 stsp
4015 33aa809d 2019-08-08 stsp static const struct got_error *
4016 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4017 33aa809d 2019-08-08 stsp {
4018 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4019 33aa809d 2019-08-08 stsp char *line = NULL;
4020 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
4021 33aa809d 2019-08-08 stsp ssize_t linelen;
4022 33aa809d 2019-08-08 stsp
4023 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
4024 33aa809d 2019-08-08 stsp if (linelen == -1) {
4025 33aa809d 2019-08-08 stsp if (ferror(infile)) {
4026 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
4027 33aa809d 2019-08-08 stsp goto done;
4028 33aa809d 2019-08-08 stsp }
4029 33aa809d 2019-08-08 stsp return NULL;
4030 33aa809d 2019-08-08 stsp }
4031 33aa809d 2019-08-08 stsp if (outfile) {
4032 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
4033 33aa809d 2019-08-08 stsp if (n != linelen) {
4034 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4035 33aa809d 2019-08-08 stsp goto done;
4036 33aa809d 2019-08-08 stsp }
4037 33aa809d 2019-08-08 stsp }
4038 33aa809d 2019-08-08 stsp if (rejectfile) {
4039 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
4040 33aa809d 2019-08-08 stsp if (n != linelen)
4041 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
4042 33aa809d 2019-08-08 stsp }
4043 33aa809d 2019-08-08 stsp done:
4044 33aa809d 2019-08-08 stsp free(line);
4045 2ec1f75b 2019-03-26 stsp return err;
4046 2ec1f75b 2019-03-26 stsp }
4047 1f1abb7e 2019-08-08 stsp
4048 33aa809d 2019-08-08 stsp static const struct got_error *
4049 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
4050 33aa809d 2019-08-08 stsp {
4051 33aa809d 2019-08-08 stsp char *line = NULL;
4052 33aa809d 2019-08-08 stsp size_t linesize = 0;
4053 33aa809d 2019-08-08 stsp ssize_t linelen;
4054 33aa809d 2019-08-08 stsp
4055 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
4056 500467ff 2019-09-25 hiltjo if (linelen == -1) {
4057 500467ff 2019-09-25 hiltjo if (ferror(f))
4058 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
4059 500467ff 2019-09-25 hiltjo return NULL;
4060 500467ff 2019-09-25 hiltjo }
4061 33aa809d 2019-08-08 stsp free(line);
4062 33aa809d 2019-08-08 stsp return NULL;
4063 33aa809d 2019-08-08 stsp }
4064 33aa809d 2019-08-08 stsp
4065 33aa809d 2019-08-08 stsp static const struct got_error *
4066 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4067 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
4068 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
4069 33aa809d 2019-08-08 stsp {
4070 33aa809d 2019-08-08 stsp const struct got_error *err;
4071 33aa809d 2019-08-08 stsp
4072 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
4073 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
4074 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
4075 33aa809d 2019-08-08 stsp if (err)
4076 33aa809d 2019-08-08 stsp return err;
4077 33aa809d 2019-08-08 stsp (*line_cur1)++;
4078 33aa809d 2019-08-08 stsp }
4079 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
4080 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
4081 33aa809d 2019-08-08 stsp if (rejectfile)
4082 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
4083 33aa809d 2019-08-08 stsp else
4084 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
4085 33aa809d 2019-08-08 stsp if (err)
4086 33aa809d 2019-08-08 stsp return err;
4087 33aa809d 2019-08-08 stsp (*line_cur2)++;
4088 33aa809d 2019-08-08 stsp }
4089 33aa809d 2019-08-08 stsp /* Copy patched lines. */
4090 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
4091 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
4092 33aa809d 2019-08-08 stsp if (err)
4093 33aa809d 2019-08-08 stsp return err;
4094 33aa809d 2019-08-08 stsp (*line_cur2)++;
4095 33aa809d 2019-08-08 stsp }
4096 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
4097 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
4098 33aa809d 2019-08-08 stsp if (rejectfile)
4099 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
4100 33aa809d 2019-08-08 stsp else
4101 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
4102 33aa809d 2019-08-08 stsp if (err)
4103 33aa809d 2019-08-08 stsp return err;
4104 33aa809d 2019-08-08 stsp (*line_cur1)++;
4105 33aa809d 2019-08-08 stsp }
4106 f1e81a05 2019-08-10 stsp
4107 f1e81a05 2019-08-10 stsp return NULL;
4108 f1e81a05 2019-08-10 stsp }
4109 f1e81a05 2019-08-10 stsp
4110 f1e81a05 2019-08-10 stsp static const struct got_error *
4111 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4112 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
4113 f1e81a05 2019-08-10 stsp {
4114 f1e81a05 2019-08-10 stsp const struct got_error *err;
4115 f1e81a05 2019-08-10 stsp
4116 f1e81a05 2019-08-10 stsp if (outfile) {
4117 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
4118 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
4119 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
4120 f1e81a05 2019-08-10 stsp if (err)
4121 f1e81a05 2019-08-10 stsp return err;
4122 f1e81a05 2019-08-10 stsp (*line_cur1)++;
4123 f1e81a05 2019-08-10 stsp }
4124 f1e81a05 2019-08-10 stsp }
4125 f1e81a05 2019-08-10 stsp if (rejectfile) {
4126 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
4127 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
4128 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
4129 f1e81a05 2019-08-10 stsp if (err)
4130 f1e81a05 2019-08-10 stsp return err;
4131 f1e81a05 2019-08-10 stsp (*line_cur2)++;
4132 f1e81a05 2019-08-10 stsp }
4133 33aa809d 2019-08-08 stsp }
4134 33aa809d 2019-08-08 stsp
4135 33aa809d 2019-08-08 stsp return NULL;
4136 33aa809d 2019-08-08 stsp }
4137 33aa809d 2019-08-08 stsp
4138 33aa809d 2019-08-08 stsp static const struct got_error *
4139 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
4140 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
4141 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
4142 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
4143 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4144 33aa809d 2019-08-08 stsp {
4145 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4146 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
4147 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
4148 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
4149 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
4150 33aa809d 2019-08-08 stsp long pos1, pos2;
4151 33aa809d 2019-08-08 stsp FILE *hunkfile;
4152 33aa809d 2019-08-08 stsp
4153 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4154 33aa809d 2019-08-08 stsp
4155 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
4156 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
4157 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
4158 33aa809d 2019-08-08 stsp
4159 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
4160 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
4161 33aa809d 2019-08-08 stsp
4162 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
4163 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
4164 33aa809d 2019-08-08 stsp
4165 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
4166 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4167 33aa809d 2019-08-08 stsp goto done;
4168 33aa809d 2019-08-08 stsp }
4169 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
4170 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
4171 33aa809d 2019-08-08 stsp goto done;
4172 33aa809d 2019-08-08 stsp }
4173 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4174 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
4175 33aa809d 2019-08-08 stsp goto done;
4176 33aa809d 2019-08-08 stsp }
4177 33aa809d 2019-08-08 stsp
4178 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4179 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
4180 33aa809d 2019-08-08 stsp if (err)
4181 33aa809d 2019-08-08 stsp goto done;
4182 33aa809d 2019-08-08 stsp
4183 33aa809d 2019-08-08 stsp switch (*choice) {
4184 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
4185 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4186 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
4187 33aa809d 2019-08-08 stsp break;
4188 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
4189 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4190 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
4191 33aa809d 2019-08-08 stsp break;
4192 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
4193 33aa809d 2019-08-08 stsp break;
4194 33aa809d 2019-08-08 stsp default:
4195 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
4196 33aa809d 2019-08-08 stsp break;
4197 33aa809d 2019-08-08 stsp }
4198 33aa809d 2019-08-08 stsp done:
4199 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4200 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
4201 33aa809d 2019-08-08 stsp return err;
4202 33aa809d 2019-08-08 stsp }
4203 33aa809d 2019-08-08 stsp
4204 1f1abb7e 2019-08-08 stsp struct revert_file_args {
4205 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
4206 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
4207 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
4208 1f1abb7e 2019-08-08 stsp void *progress_arg;
4209 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
4210 33aa809d 2019-08-08 stsp void *patch_arg;
4211 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
4212 1f1abb7e 2019-08-08 stsp };
4213 a129376b 2019-03-28 stsp
4214 e20a8b6f 2019-06-04 stsp static const struct got_error *
4215 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
4216 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
4217 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
4218 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
4219 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
4220 33aa809d 2019-08-08 stsp {
4221 33aa809d 2019-08-08 stsp const struct got_error *err;
4222 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
4223 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4224 1ebedb77 2019-10-19 stsp int fd2 = -1;
4225 fa3cef63 2020-07-23 stsp char link_target[PATH_MAX];
4226 fa3cef63 2020-07-23 stsp ssize_t link_len = 0;
4227 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
4228 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
4229 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
4230 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
4231 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
4232 33aa809d 2019-08-08 stsp struct got_diff_change *change;
4233 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
4234 33aa809d 2019-08-08 stsp int n = 0;
4235 33aa809d 2019-08-08 stsp
4236 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4237 33aa809d 2019-08-08 stsp
4238 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
4239 33aa809d 2019-08-08 stsp if (err)
4240 33aa809d 2019-08-08 stsp return err;
4241 33aa809d 2019-08-08 stsp
4242 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
4243 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4244 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4245 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4246 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("openat", path2);
4247 fa3cef63 2020-07-23 stsp goto done;
4248 fa3cef63 2020-07-23 stsp }
4249 fa3cef63 2020-07-23 stsp link_len = readlinkat(dirfd2, de_name2,
4250 fa3cef63 2020-07-23 stsp link_target, sizeof(link_target));
4251 fa3cef63 2020-07-23 stsp if (link_len == -1)
4252 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlinkat", path2);
4253 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4254 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4255 12463d8b 2019-12-13 stsp }
4256 12463d8b 2019-12-13 stsp } else {
4257 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4258 12463d8b 2019-12-13 stsp if (fd2 == -1) {
4259 fa3cef63 2020-07-23 stsp if (errno != ELOOP) {
4260 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("open", path2);
4261 fa3cef63 2020-07-23 stsp goto done;
4262 fa3cef63 2020-07-23 stsp }
4263 fa3cef63 2020-07-23 stsp link_len = readlink(path2, link_target,
4264 fa3cef63 2020-07-23 stsp sizeof(link_target));
4265 fa3cef63 2020-07-23 stsp if (link_len == -1)
4266 fa3cef63 2020-07-23 stsp return got_error_from_errno2("readlink", path2);
4267 fa3cef63 2020-07-23 stsp sb2.st_mode = S_IFLNK;
4268 fa3cef63 2020-07-23 stsp sb2.st_size = link_len;
4269 12463d8b 2019-12-13 stsp }
4270 1ebedb77 2019-10-19 stsp }
4271 fa3cef63 2020-07-23 stsp if (fd2 != -1) {
4272 fa3cef63 2020-07-23 stsp if (fstat(fd2, &sb2) == -1) {
4273 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fstat", path2);
4274 fa3cef63 2020-07-23 stsp goto done;
4275 fa3cef63 2020-07-23 stsp }
4276 1ebedb77 2019-10-19 stsp
4277 fa3cef63 2020-07-23 stsp f2 = fdopen(fd2, "r");
4278 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4279 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("fdopen", path2);
4280 fa3cef63 2020-07-23 stsp goto done;
4281 fa3cef63 2020-07-23 stsp }
4282 fa3cef63 2020-07-23 stsp fd2 = -1;
4283 fa3cef63 2020-07-23 stsp } else {
4284 fa3cef63 2020-07-23 stsp size_t n;
4285 fa3cef63 2020-07-23 stsp f2 = got_opentemp();
4286 fa3cef63 2020-07-23 stsp if (f2 == NULL) {
4287 fa3cef63 2020-07-23 stsp err = got_error_from_errno2("got_opentemp", path2);
4288 fa3cef63 2020-07-23 stsp goto done;
4289 fa3cef63 2020-07-23 stsp }
4290 fa3cef63 2020-07-23 stsp n = fwrite(link_target, 1, link_len, f2);
4291 fa3cef63 2020-07-23 stsp if (n != link_len) {
4292 fa3cef63 2020-07-23 stsp err = got_ferror(f2, GOT_ERR_IO);
4293 fa3cef63 2020-07-23 stsp goto done;
4294 fa3cef63 2020-07-23 stsp }
4295 fa3cef63 2020-07-23 stsp if (fflush(f2) == EOF) {
4296 fa3cef63 2020-07-23 stsp err = got_error_from_errno("fflush");
4297 fa3cef63 2020-07-23 stsp goto done;
4298 fa3cef63 2020-07-23 stsp }
4299 fa3cef63 2020-07-23 stsp rewind(f2);
4300 33aa809d 2019-08-08 stsp }
4301 33aa809d 2019-08-08 stsp
4302 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4303 33aa809d 2019-08-08 stsp if (err)
4304 33aa809d 2019-08-08 stsp goto done;
4305 33aa809d 2019-08-08 stsp
4306 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4307 33aa809d 2019-08-08 stsp if (err)
4308 33aa809d 2019-08-08 stsp goto done;
4309 33aa809d 2019-08-08 stsp
4310 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4311 33aa809d 2019-08-08 stsp if (err)
4312 33aa809d 2019-08-08 stsp goto done;
4313 33aa809d 2019-08-08 stsp
4314 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
4315 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
4316 33aa809d 2019-08-08 stsp goto done;
4317 33aa809d 2019-08-08 stsp }
4318 33aa809d 2019-08-08 stsp
4319 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
4320 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
4321 33aa809d 2019-08-08 stsp if (err)
4322 33aa809d 2019-08-08 stsp goto done;
4323 33aa809d 2019-08-08 stsp
4324 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4325 33aa809d 2019-08-08 stsp if (err)
4326 33aa809d 2019-08-08 stsp goto done;
4327 33aa809d 2019-08-08 stsp
4328 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
4329 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
4330 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
4331 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
4332 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4333 33aa809d 2019-08-08 stsp int choice;
4334 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
4335 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
4336 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
4337 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
4338 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
4339 e635744c 2019-08-08 stsp patch_cb, patch_arg);
4340 33aa809d 2019-08-08 stsp if (err)
4341 33aa809d 2019-08-08 stsp goto done;
4342 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
4343 33aa809d 2019-08-08 stsp have_content = 1;
4344 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
4345 33aa809d 2019-08-08 stsp break;
4346 33aa809d 2019-08-08 stsp }
4347 1ebedb77 2019-10-19 stsp if (have_content) {
4348 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4349 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
4350 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
4351 1ebedb77 2019-10-19 stsp if (err)
4352 1ebedb77 2019-10-19 stsp goto done;
4353 1ebedb77 2019-10-19 stsp
4354 fa3cef63 2020-07-23 stsp if (!S_ISLNK(sb2.st_mode)) {
4355 3818e3c4 2020-11-01 naddy if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4356 3818e3c4 2020-11-01 naddy err = got_error_from_errno2("fchmod", path2);
4357 fa3cef63 2020-07-23 stsp goto done;
4358 fa3cef63 2020-07-23 stsp }
4359 1ebedb77 2019-10-19 stsp }
4360 1ebedb77 2019-10-19 stsp }
4361 33aa809d 2019-08-08 stsp done:
4362 33aa809d 2019-08-08 stsp free(id_str);
4363 33aa809d 2019-08-08 stsp if (blob)
4364 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
4365 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
4366 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
4367 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4368 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
4369 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4370 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
4371 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
4372 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
4373 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
4374 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
4375 33aa809d 2019-08-08 stsp if (err || !have_content) {
4376 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4377 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
4378 33aa809d 2019-08-08 stsp free(*path_outfile);
4379 33aa809d 2019-08-08 stsp *path_outfile = NULL;
4380 33aa809d 2019-08-08 stsp }
4381 33aa809d 2019-08-08 stsp free(args);
4382 33aa809d 2019-08-08 stsp if (ds) {
4383 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
4384 33aa809d 2019-08-08 stsp free(ds);
4385 33aa809d 2019-08-08 stsp }
4386 33aa809d 2019-08-08 stsp if (changes)
4387 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
4388 33aa809d 2019-08-08 stsp free(path1);
4389 33aa809d 2019-08-08 stsp return err;
4390 33aa809d 2019-08-08 stsp }
4391 33aa809d 2019-08-08 stsp
4392 33aa809d 2019-08-08 stsp static const struct got_error *
4393 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
4394 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
4395 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4396 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4397 a129376b 2019-03-28 stsp {
4398 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
4399 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
4400 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
4401 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
4402 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
4403 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
4404 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
4405 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
4406 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
4407 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
4408 a129376b 2019-03-28 stsp
4409 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
4410 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
4411 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
4412 d3bcc3d1 2019-08-08 stsp return NULL;
4413 3d69ad8d 2019-08-17 semarie
4414 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
4415 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
4416 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
4417 d3bcc3d1 2019-08-08 stsp
4418 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4419 65084dad 2019-08-08 stsp if (ie == NULL)
4420 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
4421 a129376b 2019-03-28 stsp
4422 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
4423 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
4424 a129376b 2019-03-28 stsp if (err) {
4425 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
4426 a129376b 2019-03-28 stsp goto done;
4427 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
4428 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
4429 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
4430 e20a8b6f 2019-06-04 stsp goto done;
4431 e20a8b6f 2019-06-04 stsp }
4432 a129376b 2019-03-28 stsp }
4433 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
4434 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
4435 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4436 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4437 a129376b 2019-03-28 stsp goto done;
4438 a129376b 2019-03-28 stsp }
4439 a129376b 2019-03-28 stsp } else {
4440 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
4441 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
4442 a129376b 2019-03-28 stsp if (tree_path == NULL) {
4443 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4444 a129376b 2019-03-28 stsp goto done;
4445 a129376b 2019-03-28 stsp }
4446 a129376b 2019-03-28 stsp } else {
4447 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
4448 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
4449 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4450 a129376b 2019-03-28 stsp goto done;
4451 a129376b 2019-03-28 stsp }
4452 a129376b 2019-03-28 stsp }
4453 a129376b 2019-03-28 stsp }
4454 a129376b 2019-03-28 stsp
4455 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
4456 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
4457 a9fa2909 2019-07-27 stsp if (err) {
4458 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4459 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
4460 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
4461 a9fa2909 2019-07-27 stsp goto done;
4462 a9fa2909 2019-07-27 stsp } else {
4463 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
4464 a9fa2909 2019-07-27 stsp if (err)
4465 a9fa2909 2019-07-27 stsp goto done;
4466 a9fa2909 2019-07-27 stsp
4467 1233e6b6 2020-10-19 stsp err = got_path_basename(&te_name, ie->path);
4468 1233e6b6 2020-10-19 stsp if (err)
4469 a9fa2909 2019-07-27 stsp goto done;
4470 a9fa2909 2019-07-27 stsp
4471 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
4472 1233e6b6 2020-10-19 stsp free(te_name);
4473 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
4474 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
4475 b66cd6f3 2020-07-31 stsp err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4476 a9fa2909 2019-07-27 stsp goto done;
4477 a9fa2909 2019-07-27 stsp }
4478 a129376b 2019-03-28 stsp }
4479 a129376b 2019-03-28 stsp
4480 a129376b 2019-03-28 stsp switch (status) {
4481 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
4482 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4483 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4484 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4485 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4486 33aa809d 2019-08-08 stsp if (err)
4487 33aa809d 2019-08-08 stsp goto done;
4488 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4489 33aa809d 2019-08-08 stsp break;
4490 33aa809d 2019-08-08 stsp }
4491 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4492 1f1abb7e 2019-08-08 stsp ie->path);
4493 1ee397ad 2019-07-12 stsp if (err)
4494 1ee397ad 2019-07-12 stsp goto done;
4495 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
4496 a129376b 2019-03-28 stsp break;
4497 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
4498 33aa809d 2019-08-08 stsp if (a->patch_cb) {
4499 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
4500 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
4501 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
4502 33aa809d 2019-08-08 stsp if (err)
4503 33aa809d 2019-08-08 stsp goto done;
4504 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
4505 33aa809d 2019-08-08 stsp break;
4506 33aa809d 2019-08-08 stsp }
4507 33aa809d 2019-08-08 stsp /* fall through */
4508 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
4509 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
4510 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
4511 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
4512 e20a8b6f 2019-06-04 stsp struct got_object_id id;
4513 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4514 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
4515 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
4516 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4517 24278f30 2019-08-03 stsp } else
4518 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
4519 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
4520 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4521 a129376b 2019-03-28 stsp if (err)
4522 65084dad 2019-08-08 stsp goto done;
4523 65084dad 2019-08-08 stsp
4524 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4525 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
4526 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
4527 a129376b 2019-03-28 stsp goto done;
4528 65084dad 2019-08-08 stsp }
4529 65084dad 2019-08-08 stsp
4530 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4531 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
4532 369fd7e5 2020-07-23 stsp int is_bad_symlink = 0;
4533 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
4534 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
4535 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
4536 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
4537 33aa809d 2019-08-08 stsp break;
4538 369fd7e5 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4539 369fd7e5 2020-07-23 stsp if (unlink(path_content) == -1) {
4540 369fd7e5 2020-07-23 stsp err = got_error_from_errno2("unlink",
4541 369fd7e5 2020-07-23 stsp path_content);
4542 369fd7e5 2020-07-23 stsp break;
4543 369fd7e5 2020-07-23 stsp }
4544 369fd7e5 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4545 369fd7e5 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4546 369fd7e5 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4547 369fd7e5 2020-07-23 stsp a->progress_cb, a->progress_arg);
4548 369fd7e5 2020-07-23 stsp } else {
4549 369fd7e5 2020-07-23 stsp if (rename(path_content, ondisk_path) == -1) {
4550 369fd7e5 2020-07-23 stsp err = got_error_from_errno3("rename",
4551 369fd7e5 2020-07-23 stsp path_content, ondisk_path);
4552 369fd7e5 2020-07-23 stsp goto done;
4553 369fd7e5 2020-07-23 stsp }
4554 33aa809d 2019-08-08 stsp }
4555 33aa809d 2019-08-08 stsp } else {
4556 2e1fa222 2020-07-23 stsp int is_bad_symlink = 0;
4557 2e1fa222 2020-07-23 stsp if (te && S_ISLNK(te->mode)) {
4558 2e1fa222 2020-07-23 stsp err = install_symlink(&is_bad_symlink,
4559 2e1fa222 2020-07-23 stsp a->worktree, ondisk_path, ie->path,
4560 c90c8ce3 2020-07-23 stsp blob, 0, 1, 0, a->repo,
4561 2e1fa222 2020-07-23 stsp a->progress_cb, a->progress_arg);
4562 2e1fa222 2020-07-23 stsp } else {
4563 2e1fa222 2020-07-23 stsp err = install_blob(a->worktree, ondisk_path,
4564 2e1fa222 2020-07-23 stsp ie->path,
4565 2e1fa222 2020-07-23 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
4566 3b9f0f87 2020-07-23 stsp got_fileindex_perms_to_st(ie), blob,
4567 3b9f0f87 2020-07-23 stsp 0, 1, 0, 0, a->repo,
4568 3b9f0f87 2020-07-23 stsp a->progress_cb, a->progress_arg);
4569 2e1fa222 2020-07-23 stsp }
4570 a129376b 2019-03-28 stsp if (err)
4571 a129376b 2019-03-28 stsp goto done;
4572 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
4573 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
4574 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
4575 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
4576 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
4577 2e1fa222 2020-07-23 stsp if (err)
4578 2e1fa222 2020-07-23 stsp goto done;
4579 2e1fa222 2020-07-23 stsp }
4580 2e1fa222 2020-07-23 stsp if (is_bad_symlink) {
4581 6131ab45 2020-07-23 stsp got_fileindex_entry_filetype_set(ie,
4582 2e1fa222 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
4583 33aa809d 2019-08-08 stsp }
4584 a129376b 2019-03-28 stsp }
4585 a129376b 2019-03-28 stsp break;
4586 e20a8b6f 2019-06-04 stsp }
4587 a129376b 2019-03-28 stsp default:
4588 1f1abb7e 2019-08-08 stsp break;
4589 a129376b 2019-03-28 stsp }
4590 a129376b 2019-03-28 stsp done:
4591 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4592 33aa809d 2019-08-08 stsp free(path_content);
4593 e20a8b6f 2019-06-04 stsp free(parent_path);
4594 a129376b 2019-03-28 stsp free(tree_path);
4595 a129376b 2019-03-28 stsp if (blob)
4596 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4597 a129376b 2019-03-28 stsp if (tree)
4598 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4599 a129376b 2019-03-28 stsp free(tree_id);
4600 e20a8b6f 2019-06-04 stsp return err;
4601 e20a8b6f 2019-06-04 stsp }
4602 e20a8b6f 2019-06-04 stsp
4603 e20a8b6f 2019-06-04 stsp const struct got_error *
4604 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4605 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4606 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4607 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4608 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4609 e20a8b6f 2019-06-04 stsp {
4610 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4611 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4612 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4613 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4614 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4615 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4616 e20a8b6f 2019-06-04 stsp
4617 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4618 e20a8b6f 2019-06-04 stsp if (err)
4619 e20a8b6f 2019-06-04 stsp return err;
4620 e20a8b6f 2019-06-04 stsp
4621 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4622 e20a8b6f 2019-06-04 stsp if (err)
4623 e20a8b6f 2019-06-04 stsp goto done;
4624 e20a8b6f 2019-06-04 stsp
4625 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4626 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4627 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4628 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4629 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4630 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4631 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4632 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4633 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4634 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4635 e20a8b6f 2019-06-04 stsp if (err)
4636 af12c6b9 2019-06-04 stsp break;
4637 e20a8b6f 2019-06-04 stsp }
4638 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4639 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4640 e20a8b6f 2019-06-04 stsp err = sync_err;
4641 af12c6b9 2019-06-04 stsp done:
4642 fb399478 2019-07-12 stsp free(fileindex_path);
4643 a129376b 2019-03-28 stsp if (fileindex)
4644 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4645 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4646 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4647 a129376b 2019-03-28 stsp err = unlockerr;
4648 c4296144 2019-05-09 stsp return err;
4649 c4296144 2019-05-09 stsp }
4650 c4296144 2019-05-09 stsp
4651 cf066bf8 2019-05-09 stsp static void
4652 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4653 cf066bf8 2019-05-09 stsp {
4654 24519714 2019-05-09 stsp free(ct->path);
4655 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4656 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4657 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4658 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4659 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4660 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4661 cf066bf8 2019-05-09 stsp free(ct);
4662 cf066bf8 2019-05-09 stsp }
4663 24519714 2019-05-09 stsp
4664 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4665 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4666 24519714 2019-05-09 stsp struct got_repository *repo;
4667 24519714 2019-05-09 stsp struct got_worktree *worktree;
4668 0aeb8099 2020-07-23 stsp struct got_fileindex *fileindex;
4669 5f8a88c6 2019-08-03 stsp int have_staged_files;
4670 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
4671 24519714 2019-05-09 stsp };
4672 cf066bf8 2019-05-09 stsp
4673 c4296144 2019-05-09 stsp static const struct got_error *
4674 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4675 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4676 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4677 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4678 c4296144 2019-05-09 stsp {
4679 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4680 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4681 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4682 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4683 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4684 768aea60 2019-05-09 stsp struct stat sb;
4685 c4296144 2019-05-09 stsp
4686 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4687 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4688 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4689 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4690 5f8a88c6 2019-08-03 stsp return NULL;
4691 5f8a88c6 2019-08-03 stsp } else {
4692 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4693 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4694 c4296144 2019-05-09 stsp
4695 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4696 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4697 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4698 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4699 5f8a88c6 2019-08-03 stsp return NULL;
4700 5f8a88c6 2019-08-03 stsp }
4701 0b5cc0d6 2019-05-09 stsp
4702 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4703 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4704 036813ee 2019-05-09 stsp goto done;
4705 036813ee 2019-05-09 stsp }
4706 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4707 036813ee 2019-05-09 stsp parent_path = strdup("");
4708 69960a46 2019-05-09 stsp if (parent_path == NULL)
4709 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4710 69960a46 2019-05-09 stsp } else {
4711 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4712 69960a46 2019-05-09 stsp if (err)
4713 69960a46 2019-05-09 stsp return err;
4714 69960a46 2019-05-09 stsp }
4715 c4296144 2019-05-09 stsp
4716 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4717 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4718 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4719 c4296144 2019-05-09 stsp goto done;
4720 768aea60 2019-05-09 stsp }
4721 768aea60 2019-05-09 stsp
4722 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4723 768aea60 2019-05-09 stsp relpath) == -1) {
4724 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4725 768aea60 2019-05-09 stsp goto done;
4726 768aea60 2019-05-09 stsp }
4727 0aeb8099 2020-07-23 stsp
4728 0aeb8099 2020-07-23 stsp if (staged_status == GOT_STATUS_ADD ||
4729 0aeb8099 2020-07-23 stsp staged_status == GOT_STATUS_MODIFY) {
4730 0aeb8099 2020-07-23 stsp struct got_fileindex_entry *ie;
4731 0aeb8099 2020-07-23 stsp ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4732 0aeb8099 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
4733 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
4734 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
4735 0aeb8099 2020-07-23 stsp ct->mode = S_IFREG;
4736 0aeb8099 2020-07-23 stsp break;
4737 0aeb8099 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
4738 0aeb8099 2020-07-23 stsp ct->mode = S_IFLNK;
4739 0aeb8099 2020-07-23 stsp break;
4740 0aeb8099 2020-07-23 stsp default:
4741 0aeb8099 2020-07-23 stsp err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4742 0aeb8099 2020-07-23 stsp goto done;
4743 0aeb8099 2020-07-23 stsp }
4744 0aeb8099 2020-07-23 stsp ct->mode |= got_fileindex_entry_perms_get(ie);
4745 0aeb8099 2020-07-23 stsp } else if (status != GOT_STATUS_DELETE &&
4746 0aeb8099 2020-07-23 stsp staged_status != GOT_STATUS_DELETE) {
4747 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4748 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4749 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4750 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4751 12463d8b 2019-12-13 stsp ct->ondisk_path);
4752 12463d8b 2019-12-13 stsp goto done;
4753 12463d8b 2019-12-13 stsp }
4754 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4755 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4756 768aea60 2019-05-09 stsp goto done;
4757 768aea60 2019-05-09 stsp }
4758 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4759 c4296144 2019-05-09 stsp }
4760 c4296144 2019-05-09 stsp
4761 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4762 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4763 44d03001 2019-05-09 stsp relpath) == -1) {
4764 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4765 44d03001 2019-05-09 stsp goto done;
4766 44d03001 2019-05-09 stsp }
4767 44d03001 2019-05-09 stsp
4768 35213c7c 2020-07-23 stsp if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4769 35213c7c 2020-07-23 stsp status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4770 35213c7c 2020-07-23 stsp int is_bad_symlink;
4771 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
4772 35213c7c 2020-07-23 stsp ssize_t target_len;
4773 35213c7c 2020-07-23 stsp target_len = readlink(ct->ondisk_path, target_path,
4774 35213c7c 2020-07-23 stsp sizeof(target_path));
4775 35213c7c 2020-07-23 stsp if (target_len == -1) {
4776 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
4777 35213c7c 2020-07-23 stsp ct->ondisk_path);
4778 35213c7c 2020-07-23 stsp goto done;
4779 35213c7c 2020-07-23 stsp }
4780 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink, target_path,
4781 35213c7c 2020-07-23 stsp target_len, ct->ondisk_path, a->worktree->root_path);
4782 35213c7c 2020-07-23 stsp if (err)
4783 35213c7c 2020-07-23 stsp goto done;
4784 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
4785 35213c7c 2020-07-23 stsp err = got_error_path(ct->ondisk_path,
4786 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
4787 35213c7c 2020-07-23 stsp goto done;
4788 35213c7c 2020-07-23 stsp }
4789 35213c7c 2020-07-23 stsp }
4790 35213c7c 2020-07-23 stsp
4791 35213c7c 2020-07-23 stsp
4792 cf066bf8 2019-05-09 stsp ct->status = status;
4793 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4794 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4795 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4796 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4797 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4798 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4799 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4800 b416585c 2019-05-13 stsp goto done;
4801 b416585c 2019-05-13 stsp }
4802 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4803 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4804 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4805 f0b75401 2019-08-03 stsp goto done;
4806 f0b75401 2019-08-03 stsp }
4807 f0b75401 2019-08-03 stsp }
4808 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4809 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4810 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4811 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4812 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4813 036813ee 2019-05-09 stsp goto done;
4814 036813ee 2019-05-09 stsp }
4815 ca2503ea 2019-05-09 stsp }
4816 24519714 2019-05-09 stsp ct->path = strdup(path);
4817 24519714 2019-05-09 stsp if (ct->path == NULL) {
4818 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4819 24519714 2019-05-09 stsp goto done;
4820 24519714 2019-05-09 stsp }
4821 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4822 c4296144 2019-05-09 stsp done:
4823 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4824 ed175427 2019-05-09 stsp free_commitable(ct);
4825 24519714 2019-05-09 stsp free(parent_path);
4826 036813ee 2019-05-09 stsp free(path);
4827 a129376b 2019-03-28 stsp return err;
4828 a129376b 2019-03-28 stsp }
4829 c4296144 2019-05-09 stsp
4830 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4831 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4832 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4833 036813ee 2019-05-09 stsp struct got_repository *);
4834 ed175427 2019-05-09 stsp
4835 ed175427 2019-05-09 stsp static const struct got_error *
4836 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4837 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4838 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4839 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4840 afa376bf 2019-05-09 stsp struct got_repository *repo)
4841 ed175427 2019-05-09 stsp {
4842 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4843 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4844 036813ee 2019-05-09 stsp char *subpath;
4845 ed175427 2019-05-09 stsp
4846 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4847 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4848 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4849 ed175427 2019-05-09 stsp
4850 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4851 ed175427 2019-05-09 stsp if (err)
4852 ed175427 2019-05-09 stsp return err;
4853 ed175427 2019-05-09 stsp
4854 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4855 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4856 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4857 036813ee 2019-05-09 stsp free(subpath);
4858 036813ee 2019-05-09 stsp return err;
4859 036813ee 2019-05-09 stsp }
4860 ed175427 2019-05-09 stsp
4861 036813ee 2019-05-09 stsp static const struct got_error *
4862 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4863 036813ee 2019-05-09 stsp {
4864 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4865 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4866 ed175427 2019-05-09 stsp
4867 036813ee 2019-05-09 stsp *match = 0;
4868 ed175427 2019-05-09 stsp
4869 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4870 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4871 0f63689d 2019-05-10 stsp return NULL;
4872 036813ee 2019-05-09 stsp }
4873 0b5cc0d6 2019-05-09 stsp
4874 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4875 0f63689d 2019-05-10 stsp if (err)
4876 0f63689d 2019-05-10 stsp return err;
4877 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4878 036813ee 2019-05-09 stsp free(ct_parent_path);
4879 036813ee 2019-05-09 stsp return err;
4880 036813ee 2019-05-09 stsp }
4881 0b5cc0d6 2019-05-09 stsp
4882 768aea60 2019-05-09 stsp static mode_t
4883 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4884 768aea60 2019-05-09 stsp {
4885 3d9a4ec4 2020-07-23 stsp if (S_ISLNK(ct->mode))
4886 3d9a4ec4 2020-07-23 stsp return S_IFLNK;
4887 3d9a4ec4 2020-07-23 stsp
4888 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4889 768aea60 2019-05-09 stsp }
4890 768aea60 2019-05-09 stsp
4891 036813ee 2019-05-09 stsp static const struct got_error *
4892 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4893 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4894 036813ee 2019-05-09 stsp {
4895 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4896 ca2503ea 2019-05-09 stsp
4897 036813ee 2019-05-09 stsp *new_te = NULL;
4898 0b5cc0d6 2019-05-09 stsp
4899 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4900 036813ee 2019-05-09 stsp if (err)
4901 036813ee 2019-05-09 stsp goto done;
4902 0b5cc0d6 2019-05-09 stsp
4903 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4904 036813ee 2019-05-09 stsp
4905 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4906 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4907 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4908 5f8a88c6 2019-08-03 stsp else
4909 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4910 036813ee 2019-05-09 stsp done:
4911 036813ee 2019-05-09 stsp if (err && *new_te) {
4912 56e0773d 2019-11-28 stsp free(*new_te);
4913 036813ee 2019-05-09 stsp *new_te = NULL;
4914 036813ee 2019-05-09 stsp }
4915 036813ee 2019-05-09 stsp return err;
4916 036813ee 2019-05-09 stsp }
4917 036813ee 2019-05-09 stsp
4918 036813ee 2019-05-09 stsp static const struct got_error *
4919 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4920 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4921 036813ee 2019-05-09 stsp {
4922 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4923 102b254e 2020-10-19 stsp char *ct_name = NULL;
4924 036813ee 2019-05-09 stsp
4925 036813ee 2019-05-09 stsp *new_te = NULL;
4926 036813ee 2019-05-09 stsp
4927 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4928 036813ee 2019-05-09 stsp if (*new_te == NULL)
4929 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4930 036813ee 2019-05-09 stsp
4931 102b254e 2020-10-19 stsp err = got_path_basename(&ct_name, ct->path);
4932 102b254e 2020-10-19 stsp if (err)
4933 036813ee 2019-05-09 stsp goto done;
4934 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4935 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4936 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4937 036813ee 2019-05-09 stsp goto done;
4938 036813ee 2019-05-09 stsp }
4939 036813ee 2019-05-09 stsp
4940 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4941 036813ee 2019-05-09 stsp
4942 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4943 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4944 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4945 5f8a88c6 2019-08-03 stsp else
4946 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4947 036813ee 2019-05-09 stsp done:
4948 102b254e 2020-10-19 stsp free(ct_name);
4949 036813ee 2019-05-09 stsp if (err && *new_te) {
4950 56e0773d 2019-11-28 stsp free(*new_te);
4951 036813ee 2019-05-09 stsp *new_te = NULL;
4952 036813ee 2019-05-09 stsp }
4953 036813ee 2019-05-09 stsp return err;
4954 036813ee 2019-05-09 stsp }
4955 036813ee 2019-05-09 stsp
4956 036813ee 2019-05-09 stsp static const struct got_error *
4957 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4958 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4959 036813ee 2019-05-09 stsp {
4960 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4961 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4962 036813ee 2019-05-09 stsp
4963 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4964 036813ee 2019-05-09 stsp if (err)
4965 036813ee 2019-05-09 stsp return err;
4966 036813ee 2019-05-09 stsp if (new_pe == NULL)
4967 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4968 036813ee 2019-05-09 stsp return NULL;
4969 afa376bf 2019-05-09 stsp }
4970 afa376bf 2019-05-09 stsp
4971 afa376bf 2019-05-09 stsp static const struct got_error *
4972 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4973 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4974 afa376bf 2019-05-09 stsp {
4975 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4976 5f8a88c6 2019-08-03 stsp unsigned char status;
4977 5f8a88c6 2019-08-03 stsp
4978 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4979 afa376bf 2019-05-09 stsp ct_path++;
4980 5f8a88c6 2019-08-03 stsp
4981 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4982 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4983 5f8a88c6 2019-08-03 stsp else
4984 5f8a88c6 2019-08-03 stsp status = ct->status;
4985 5f8a88c6 2019-08-03 stsp
4986 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4987 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4988 036813ee 2019-05-09 stsp }
4989 036813ee 2019-05-09 stsp
4990 036813ee 2019-05-09 stsp static const struct got_error *
4991 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4992 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4993 44d03001 2019-05-09 stsp {
4994 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4995 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4996 44d03001 2019-05-09 stsp char *te_path;
4997 44d03001 2019-05-09 stsp
4998 44d03001 2019-05-09 stsp *modified = 0;
4999 44d03001 2019-05-09 stsp
5000 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
5001 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
5002 44d03001 2019-05-09 stsp te->name) == -1)
5003 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5004 44d03001 2019-05-09 stsp
5005 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5006 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5007 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
5008 44d03001 2019-05-09 stsp strlen(te_path));
5009 62d463ca 2020-10-20 naddy if (*modified)
5010 44d03001 2019-05-09 stsp break;
5011 44d03001 2019-05-09 stsp }
5012 44d03001 2019-05-09 stsp
5013 44d03001 2019-05-09 stsp free(te_path);
5014 44d03001 2019-05-09 stsp return err;
5015 44d03001 2019-05-09 stsp }
5016 44d03001 2019-05-09 stsp
5017 44d03001 2019-05-09 stsp static const struct got_error *
5018 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
5019 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
5020 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
5021 036813ee 2019-05-09 stsp {
5022 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5023 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5024 036813ee 2019-05-09 stsp
5025 036813ee 2019-05-09 stsp *ctp = NULL;
5026 036813ee 2019-05-09 stsp
5027 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5028 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5029 036813ee 2019-05-09 stsp char *ct_name = NULL;
5030 036813ee 2019-05-09 stsp int path_matches;
5031 036813ee 2019-05-09 stsp
5032 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5033 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
5034 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
5035 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
5036 5f8a88c6 2019-08-03 stsp continue;
5037 5f8a88c6 2019-08-03 stsp } else {
5038 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
5039 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
5040 5f8a88c6 2019-08-03 stsp continue;
5041 5f8a88c6 2019-08-03 stsp }
5042 036813ee 2019-05-09 stsp
5043 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5044 036813ee 2019-05-09 stsp continue;
5045 036813ee 2019-05-09 stsp
5046 62d463ca 2020-10-20 naddy err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5047 62d463ca 2020-10-20 naddy if (err)
5048 036813ee 2019-05-09 stsp return err;
5049 036813ee 2019-05-09 stsp if (!path_matches)
5050 036813ee 2019-05-09 stsp continue;
5051 036813ee 2019-05-09 stsp
5052 d34b633e 2020-10-19 stsp err = got_path_basename(&ct_name, pe->path);
5053 d34b633e 2020-10-19 stsp if (err)
5054 d34b633e 2020-10-19 stsp return err;
5055 d34b633e 2020-10-19 stsp
5056 d34b633e 2020-10-19 stsp if (strcmp(te->name, ct_name) != 0) {
5057 d34b633e 2020-10-19 stsp free(ct_name);
5058 036813ee 2019-05-09 stsp continue;
5059 d34b633e 2020-10-19 stsp }
5060 d34b633e 2020-10-19 stsp free(ct_name);
5061 036813ee 2019-05-09 stsp
5062 036813ee 2019-05-09 stsp *ctp = ct;
5063 036813ee 2019-05-09 stsp break;
5064 036813ee 2019-05-09 stsp }
5065 2b496619 2019-07-10 stsp
5066 2b496619 2019-07-10 stsp return err;
5067 2b496619 2019-07-10 stsp }
5068 2b496619 2019-07-10 stsp
5069 2b496619 2019-07-10 stsp static const struct got_error *
5070 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5071 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
5072 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
5073 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
5074 2b496619 2019-07-10 stsp struct got_repository *repo)
5075 2b496619 2019-07-10 stsp {
5076 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
5077 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
5078 2b496619 2019-07-10 stsp char *subtree_path;
5079 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
5080 ba580f68 2020-03-22 stsp int nentries;
5081 2b496619 2019-07-10 stsp
5082 2b496619 2019-07-10 stsp *new_tep = NULL;
5083 2b496619 2019-07-10 stsp
5084 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5085 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
5086 2b496619 2019-07-10 stsp child_path) == -1)
5087 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
5088 036813ee 2019-05-09 stsp
5089 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
5090 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
5091 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
5092 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
5093 56e0773d 2019-11-28 stsp
5094 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5095 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
5096 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
5097 2b496619 2019-07-10 stsp goto done;
5098 2b496619 2019-07-10 stsp }
5099 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
5100 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
5101 2b496619 2019-07-10 stsp if (err) {
5102 56e0773d 2019-11-28 stsp free(new_te);
5103 2b496619 2019-07-10 stsp goto done;
5104 2b496619 2019-07-10 stsp }
5105 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
5106 2b496619 2019-07-10 stsp done:
5107 56e0773d 2019-11-28 stsp free(id);
5108 2b496619 2019-07-10 stsp free(subtree_path);
5109 2b496619 2019-07-10 stsp if (err == NULL)
5110 2b496619 2019-07-10 stsp *new_tep = new_te;
5111 036813ee 2019-05-09 stsp return err;
5112 036813ee 2019-05-09 stsp }
5113 036813ee 2019-05-09 stsp
5114 036813ee 2019-05-09 stsp static const struct got_error *
5115 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
5116 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
5117 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
5118 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5119 036813ee 2019-05-09 stsp struct got_repository *repo)
5120 036813ee 2019-05-09 stsp {
5121 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
5122 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
5123 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
5124 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
5125 036813ee 2019-05-09 stsp
5126 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
5127 ba580f68 2020-03-22 stsp *nentries = 0;
5128 036813ee 2019-05-09 stsp
5129 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
5130 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5131 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5132 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
5133 036813ee 2019-05-09 stsp
5134 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
5135 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
5136 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
5137 036813ee 2019-05-09 stsp continue;
5138 036813ee 2019-05-09 stsp
5139 62d463ca 2020-10-20 naddy if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5140 62d463ca 2020-10-20 naddy strlen(path_base_tree)))
5141 036813ee 2019-05-09 stsp continue;
5142 036813ee 2019-05-09 stsp
5143 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5144 f2b0a8b0 2020-07-31 stsp ct->in_repo_path);
5145 036813ee 2019-05-09 stsp if (err)
5146 036813ee 2019-05-09 stsp goto done;
5147 036813ee 2019-05-09 stsp
5148 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
5149 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
5150 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
5151 036813ee 2019-05-09 stsp if (err)
5152 036813ee 2019-05-09 stsp goto done;
5153 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
5154 afa376bf 2019-05-09 stsp if (err)
5155 afa376bf 2019-05-09 stsp goto done;
5156 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
5157 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5158 2b496619 2019-07-10 stsp if (err)
5159 2f51b5b3 2019-05-09 stsp goto done;
5160 ba580f68 2020-03-22 stsp (*nentries)++;
5161 2b496619 2019-07-10 stsp } else {
5162 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
5163 2b496619 2019-07-10 stsp if (base_tree == NULL ||
5164 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
5165 2b496619 2019-07-10 stsp == NULL) {
5166 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
5167 2b496619 2019-07-10 stsp child_path, path_base_tree,
5168 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
5169 2b496619 2019-07-10 stsp repo);
5170 2b496619 2019-07-10 stsp if (err)
5171 2b496619 2019-07-10 stsp goto done;
5172 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
5173 2b496619 2019-07-10 stsp if (err)
5174 2b496619 2019-07-10 stsp goto done;
5175 ba580f68 2020-03-22 stsp (*nentries)++;
5176 9ba0479c 2019-05-10 stsp }
5177 ed175427 2019-05-09 stsp }
5178 2f51b5b3 2019-05-09 stsp }
5179 2f51b5b3 2019-05-09 stsp
5180 2f51b5b3 2019-05-09 stsp if (base_tree) {
5181 56e0773d 2019-11-28 stsp int i, nbase_entries;
5182 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
5183 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
5184 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
5185 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
5186 63c5ca5d 2019-08-24 stsp
5187 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
5188 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
5189 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
5190 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
5191 63c5ca5d 2019-08-24 stsp if (err)
5192 63c5ca5d 2019-08-24 stsp goto done;
5193 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
5194 63c5ca5d 2019-08-24 stsp if (err)
5195 63c5ca5d 2019-08-24 stsp goto done;
5196 ba580f68 2020-03-22 stsp (*nentries)++;
5197 63c5ca5d 2019-08-24 stsp continue;
5198 63c5ca5d 2019-08-24 stsp }
5199 2f51b5b3 2019-05-09 stsp
5200 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
5201 44d03001 2019-05-09 stsp int modified;
5202 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5203 036813ee 2019-05-09 stsp if (err)
5204 036813ee 2019-05-09 stsp goto done;
5205 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
5206 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
5207 2f51b5b3 2019-05-09 stsp if (err)
5208 2f51b5b3 2019-05-09 stsp goto done;
5209 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
5210 44d03001 2019-05-09 stsp if (modified) {
5211 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
5212 ba580f68 2020-03-22 stsp int nsubentries;
5213 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
5214 ba580f68 2020-03-22 stsp &nsubentries, te,
5215 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
5216 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
5217 44d03001 2019-05-09 stsp if (err)
5218 44d03001 2019-05-09 stsp goto done;
5219 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
5220 ba580f68 2020-03-22 stsp /* All entries were deleted. */
5221 ba580f68 2020-03-22 stsp free(new_id);
5222 ba580f68 2020-03-22 stsp continue;
5223 ba580f68 2020-03-22 stsp }
5224 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
5225 56e0773d 2019-11-28 stsp sizeof(new_te->id));
5226 56e0773d 2019-11-28 stsp free(new_id);
5227 44d03001 2019-05-09 stsp }
5228 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5229 036813ee 2019-05-09 stsp if (err)
5230 036813ee 2019-05-09 stsp goto done;
5231 ba580f68 2020-03-22 stsp (*nentries)++;
5232 2f51b5b3 2019-05-09 stsp continue;
5233 036813ee 2019-05-09 stsp }
5234 2f51b5b3 2019-05-09 stsp
5235 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
5236 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
5237 f66c734c 2019-09-22 stsp if (err)
5238 f66c734c 2019-09-22 stsp goto done;
5239 2f51b5b3 2019-05-09 stsp if (ct) {
5240 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
5241 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
5242 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
5243 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5244 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
5245 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
5246 2f51b5b3 2019-05-09 stsp if (err)
5247 2f51b5b3 2019-05-09 stsp goto done;
5248 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5249 2f51b5b3 2019-05-09 stsp if (err)
5250 2f51b5b3 2019-05-09 stsp goto done;
5251 ba580f68 2020-03-22 stsp (*nentries)++;
5252 2f51b5b3 2019-05-09 stsp }
5253 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
5254 b416585c 2019-05-13 stsp status_arg);
5255 afa376bf 2019-05-09 stsp if (err)
5256 afa376bf 2019-05-09 stsp goto done;
5257 2f51b5b3 2019-05-09 stsp } else {
5258 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
5259 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
5260 2f51b5b3 2019-05-09 stsp if (err)
5261 2f51b5b3 2019-05-09 stsp goto done;
5262 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
5263 2f51b5b3 2019-05-09 stsp if (err)
5264 2f51b5b3 2019-05-09 stsp goto done;
5265 ba580f68 2020-03-22 stsp (*nentries)++;
5266 2f51b5b3 2019-05-09 stsp }
5267 ca2503ea 2019-05-09 stsp }
5268 ed175427 2019-05-09 stsp }
5269 0b5cc0d6 2019-05-09 stsp
5270 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
5271 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5272 ed175427 2019-05-09 stsp done:
5273 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
5274 2e1fa222 2020-07-23 stsp return err;
5275 2e1fa222 2020-07-23 stsp }
5276 2e1fa222 2020-07-23 stsp
5277 2e1fa222 2020-07-23 stsp static const struct got_error *
5278 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
5279 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
5280 72fd46fa 2019-09-06 stsp int have_staged_files)
5281 ebf99748 2019-05-09 stsp {
5282 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
5283 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
5284 ebf99748 2019-05-09 stsp
5285 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5286 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
5287 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5288 ebf99748 2019-05-09 stsp
5289 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5290 ebf99748 2019-05-09 stsp if (ie) {
5291 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
5292 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
5293 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
5294 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
5295 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
5296 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
5297 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
5298 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
5299 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
5300 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
5301 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5302 72fd46fa 2019-09-06 stsp !have_staged_files);
5303 ebf99748 2019-05-09 stsp } else
5304 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
5305 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
5306 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
5307 72fd46fa 2019-09-06 stsp !have_staged_files);
5308 ebf99748 2019-05-09 stsp } else {
5309 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
5310 ebf99748 2019-05-09 stsp if (err)
5311 af12c6b9 2019-06-04 stsp break;
5312 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
5313 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
5314 3969253a 2020-03-07 stsp if (err) {
5315 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5316 3969253a 2020-03-07 stsp break;
5317 3969253a 2020-03-07 stsp }
5318 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
5319 3969253a 2020-03-07 stsp if (err) {
5320 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
5321 af12c6b9 2019-06-04 stsp break;
5322 3969253a 2020-03-07 stsp }
5323 ebf99748 2019-05-09 stsp }
5324 ebf99748 2019-05-09 stsp }
5325 d56d26ce 2019-05-10 stsp return err;
5326 d56d26ce 2019-05-10 stsp }
5327 735ef5ac 2019-08-03 stsp
5328 d56d26ce 2019-05-10 stsp
5329 d56d26ce 2019-05-10 stsp static const struct got_error *
5330 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
5331 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
5332 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
5333 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
5334 735ef5ac 2019-08-03 stsp int ood_errcode)
5335 d56d26ce 2019-05-10 stsp {
5336 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
5337 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
5338 1a36436d 2019-06-10 stsp
5339 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5340 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
5341 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5342 1a36436d 2019-06-10 stsp return NULL;
5343 9bead371 2019-07-28 stsp /*
5344 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
5345 9bead371 2019-07-28 stsp * on matches file content in the branch head.
5346 9bead371 2019-07-28 stsp */
5347 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5348 735ef5ac 2019-08-03 stsp in_repo_path);
5349 9bead371 2019-07-28 stsp if (err) {
5350 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
5351 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
5352 1a36436d 2019-06-10 stsp goto done;
5353 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
5354 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
5355 1a36436d 2019-06-10 stsp } else {
5356 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
5357 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
5358 735ef5ac 2019-08-03 stsp in_repo_path);
5359 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5360 1a36436d 2019-06-10 stsp goto done;
5361 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
5362 1a36436d 2019-06-10 stsp }
5363 1a36436d 2019-06-10 stsp done:
5364 1a36436d 2019-06-10 stsp free(id);
5365 1a36436d 2019-06-10 stsp return err;
5366 ebf99748 2019-05-09 stsp }
5367 ebf99748 2019-05-09 stsp
5368 c4296144 2019-05-09 stsp const struct got_error *
5369 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
5370 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
5371 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
5372 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
5373 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5374 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
5375 afa376bf 2019-05-09 stsp struct got_repository *repo)
5376 c4296144 2019-05-09 stsp {
5377 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
5378 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
5379 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
5380 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
5381 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
5382 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
5383 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
5384 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
5385 ba580f68 2020-03-22 stsp int nentries;
5386 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
5387 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
5388 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
5389 c4296144 2019-05-09 stsp
5390 c4296144 2019-05-09 stsp *new_commit_id = NULL;
5391 c4296144 2019-05-09 stsp
5392 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
5393 675c7539 2019-05-09 stsp
5394 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5395 588edf97 2019-05-10 stsp if (err)
5396 588edf97 2019-05-10 stsp goto done;
5397 de18fc63 2019-05-09 stsp
5398 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5399 588edf97 2019-05-10 stsp if (err)
5400 588edf97 2019-05-10 stsp goto done;
5401 588edf97 2019-05-10 stsp
5402 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
5403 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5404 33ad4cbe 2019-05-12 jcs if (err)
5405 33ad4cbe 2019-05-12 jcs goto done;
5406 33ad4cbe 2019-05-12 jcs }
5407 c4296144 2019-05-09 stsp
5408 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
5409 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5410 33ad4cbe 2019-05-12 jcs goto done;
5411 33ad4cbe 2019-05-12 jcs }
5412 33ad4cbe 2019-05-12 jcs
5413 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
5414 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
5415 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5416 cf066bf8 2019-05-09 stsp char *ondisk_path;
5417 cf066bf8 2019-05-09 stsp
5418 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
5419 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
5420 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
5421 5f8a88c6 2019-08-03 stsp continue;
5422 5f8a88c6 2019-08-03 stsp
5423 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
5424 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
5425 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
5426 cf066bf8 2019-05-09 stsp continue;
5427 cf066bf8 2019-05-09 stsp
5428 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
5429 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
5430 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5431 cf066bf8 2019-05-09 stsp goto done;
5432 cf066bf8 2019-05-09 stsp }
5433 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5434 2e1fa222 2020-07-23 stsp free(ondisk_path);
5435 2e1fa222 2020-07-23 stsp if (err)
5436 cf066bf8 2019-05-09 stsp goto done;
5437 cf066bf8 2019-05-09 stsp }
5438 036813ee 2019-05-09 stsp
5439 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
5440 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5441 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
5442 036813ee 2019-05-09 stsp if (err)
5443 036813ee 2019-05-09 stsp goto done;
5444 036813ee 2019-05-09 stsp
5445 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5446 de18fc63 2019-05-09 stsp if (err)
5447 de18fc63 2019-05-09 stsp goto done;
5448 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5449 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5450 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5451 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
5452 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
5453 33ad4cbe 2019-05-12 jcs free(logmsg);
5454 9d40349a 2019-05-09 stsp if (err)
5455 9d40349a 2019-05-09 stsp goto done;
5456 9d40349a 2019-05-09 stsp
5457 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
5458 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
5459 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
5460 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
5461 09f5bd90 2019-05-09 stsp goto done;
5462 09f5bd90 2019-05-09 stsp }
5463 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
5464 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5465 09f5bd90 2019-05-09 stsp if (err)
5466 09f5bd90 2019-05-09 stsp goto done;
5467 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5468 ebf99748 2019-05-09 stsp if (err)
5469 ebf99748 2019-05-09 stsp goto done;
5470 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5471 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5472 09f5bd90 2019-05-09 stsp goto done;
5473 09f5bd90 2019-05-09 stsp }
5474 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
5475 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
5476 f2c16586 2019-05-09 stsp if (err)
5477 f2c16586 2019-05-09 stsp goto done;
5478 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
5479 f2c16586 2019-05-09 stsp if (err)
5480 f2c16586 2019-05-09 stsp goto done;
5481 f2c16586 2019-05-09 stsp
5482 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5483 09f5bd90 2019-05-09 stsp if (err)
5484 09f5bd90 2019-05-09 stsp goto done;
5485 09f5bd90 2019-05-09 stsp
5486 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
5487 ebf99748 2019-05-09 stsp if (err)
5488 ebf99748 2019-05-09 stsp goto done;
5489 c4296144 2019-05-09 stsp done:
5490 588edf97 2019-05-10 stsp if (head_tree)
5491 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
5492 588edf97 2019-05-10 stsp if (head_commit)
5493 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
5494 09f5bd90 2019-05-09 stsp free(head_commit_id2);
5495 2f17228e 2019-05-12 stsp if (head_ref2) {
5496 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
5497 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
5498 2f17228e 2019-05-12 stsp err = unlockerr;
5499 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
5500 39cd0ff6 2019-07-12 stsp }
5501 39cd0ff6 2019-07-12 stsp return err;
5502 39cd0ff6 2019-07-12 stsp }
5503 5c1e53bc 2019-07-28 stsp
5504 5c1e53bc 2019-07-28 stsp static const struct got_error *
5505 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
5506 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
5507 5c1e53bc 2019-07-28 stsp {
5508 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
5509 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
5510 39cd0ff6 2019-07-12 stsp
5511 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
5512 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
5513 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
5514 5c1e53bc 2019-07-28 stsp
5515 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
5516 5c1e53bc 2019-07-28 stsp ct_path++;
5517 5c1e53bc 2019-07-28 stsp
5518 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
5519 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
5520 5c1e53bc 2019-07-28 stsp break;
5521 5c1e53bc 2019-07-28 stsp }
5522 5c1e53bc 2019-07-28 stsp
5523 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
5524 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
5525 5c1e53bc 2019-07-28 stsp
5526 5c1e53bc 2019-07-28 stsp return NULL;
5527 5c1e53bc 2019-07-28 stsp }
5528 5c1e53bc 2019-07-28 stsp
5529 f0b75401 2019-08-03 stsp static const struct got_error *
5530 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
5531 f0b75401 2019-08-03 stsp {
5532 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
5533 f0b75401 2019-08-03 stsp
5534 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5535 f0b75401 2019-08-03 stsp *have_staged_files = 1;
5536 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
5537 f0b75401 2019-08-03 stsp }
5538 f0b75401 2019-08-03 stsp
5539 f0b75401 2019-08-03 stsp return NULL;
5540 f0b75401 2019-08-03 stsp }
5541 f0b75401 2019-08-03 stsp
5542 5f8a88c6 2019-08-03 stsp static const struct got_error *
5543 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
5544 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
5545 5f8a88c6 2019-08-03 stsp {
5546 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
5547 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
5548 5f8a88c6 2019-08-03 stsp
5549 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
5550 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
5551 5f8a88c6 2019-08-03 stsp continue;
5552 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5553 5f8a88c6 2019-08-03 stsp if (ie == NULL)
5554 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5555 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5556 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
5557 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
5558 5f8a88c6 2019-08-03 stsp }
5559 5f8a88c6 2019-08-03 stsp
5560 5f8a88c6 2019-08-03 stsp return NULL;
5561 5f8a88c6 2019-08-03 stsp }
5562 5f8a88c6 2019-08-03 stsp
5563 39cd0ff6 2019-07-12 stsp const struct got_error *
5564 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
5565 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
5566 35213c7c 2020-07-23 stsp const char *author, const char *committer, int allow_bad_symlinks,
5567 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5568 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
5569 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
5570 39cd0ff6 2019-07-12 stsp {
5571 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5572 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
5573 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
5574 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5575 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5576 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
5577 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
5578 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5579 f0b75401 2019-08-03 stsp int have_staged_files = 0;
5580 39cd0ff6 2019-07-12 stsp
5581 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
5582 39cd0ff6 2019-07-12 stsp
5583 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5584 39cd0ff6 2019-07-12 stsp
5585 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
5586 39cd0ff6 2019-07-12 stsp if (err)
5587 39cd0ff6 2019-07-12 stsp goto done;
5588 39cd0ff6 2019-07-12 stsp
5589 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5590 39cd0ff6 2019-07-12 stsp if (err)
5591 39cd0ff6 2019-07-12 stsp goto done;
5592 39cd0ff6 2019-07-12 stsp
5593 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5594 39cd0ff6 2019-07-12 stsp if (err)
5595 39cd0ff6 2019-07-12 stsp goto done;
5596 39cd0ff6 2019-07-12 stsp
5597 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
5598 39cd0ff6 2019-07-12 stsp if (err)
5599 39cd0ff6 2019-07-12 stsp goto done;
5600 39cd0ff6 2019-07-12 stsp
5601 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5602 5f8a88c6 2019-08-03 stsp &have_staged_files);
5603 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
5604 5f8a88c6 2019-08-03 stsp goto done;
5605 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
5606 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
5607 5f8a88c6 2019-08-03 stsp if (err)
5608 5f8a88c6 2019-08-03 stsp goto done;
5609 5f8a88c6 2019-08-03 stsp }
5610 5f8a88c6 2019-08-03 stsp
5611 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5612 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
5613 0aeb8099 2020-07-23 stsp cc_arg.fileindex = fileindex;
5614 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
5615 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
5616 35213c7c 2020-07-23 stsp cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5617 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5618 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
5619 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5620 5c1e53bc 2019-07-28 stsp if (err)
5621 5c1e53bc 2019-07-28 stsp goto done;
5622 5c1e53bc 2019-07-28 stsp }
5623 39cd0ff6 2019-07-12 stsp
5624 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5625 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5626 39cd0ff6 2019-07-12 stsp goto done;
5627 5c1e53bc 2019-07-28 stsp }
5628 5c1e53bc 2019-07-28 stsp
5629 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
5630 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
5631 5c1e53bc 2019-07-28 stsp if (err)
5632 5c1e53bc 2019-07-28 stsp goto done;
5633 39cd0ff6 2019-07-12 stsp }
5634 f0b75401 2019-08-03 stsp
5635 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5636 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5637 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5638 f0b75401 2019-08-03 stsp
5639 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5640 f0b75401 2019-08-03 stsp ct_path++;
5641 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5642 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5643 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5644 b50cabdf 2019-07-12 stsp if (err)
5645 b50cabdf 2019-07-12 stsp goto done;
5646 f0b75401 2019-08-03 stsp
5647 b50cabdf 2019-07-12 stsp }
5648 b50cabdf 2019-07-12 stsp
5649 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5650 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5651 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5652 39cd0ff6 2019-07-12 stsp if (err)
5653 39cd0ff6 2019-07-12 stsp goto done;
5654 39cd0ff6 2019-07-12 stsp
5655 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5656 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5657 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5658 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5659 39cd0ff6 2019-07-12 stsp err = sync_err;
5660 39cd0ff6 2019-07-12 stsp done:
5661 39cd0ff6 2019-07-12 stsp if (fileindex)
5662 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5663 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5664 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5665 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5666 39cd0ff6 2019-07-12 stsp err = unlockerr;
5667 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5668 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5669 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5670 39cd0ff6 2019-07-12 stsp }
5671 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5672 c4296144 2019-05-09 stsp return err;
5673 8656d6c4 2019-05-20 stsp }
5674 8656d6c4 2019-05-20 stsp
5675 8656d6c4 2019-05-20 stsp const char *
5676 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5677 8656d6c4 2019-05-20 stsp {
5678 8656d6c4 2019-05-20 stsp return ct->path;
5679 8656d6c4 2019-05-20 stsp }
5680 8656d6c4 2019-05-20 stsp
5681 8656d6c4 2019-05-20 stsp unsigned int
5682 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5683 8656d6c4 2019-05-20 stsp {
5684 8656d6c4 2019-05-20 stsp return ct->status;
5685 818c7501 2019-07-11 stsp }
5686 818c7501 2019-07-11 stsp
5687 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5688 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5689 818c7501 2019-07-11 stsp struct got_repository *repo;
5690 818c7501 2019-07-11 stsp };
5691 818c7501 2019-07-11 stsp
5692 818c7501 2019-07-11 stsp static const struct got_error *
5693 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5694 818c7501 2019-07-11 stsp {
5695 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5696 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5697 818c7501 2019-07-11 stsp unsigned char status;
5698 818c7501 2019-07-11 stsp struct stat sb;
5699 818c7501 2019-07-11 stsp char *ondisk_path;
5700 818c7501 2019-07-11 stsp
5701 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5702 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5703 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5704 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5705 818c7501 2019-07-11 stsp
5706 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5707 818c7501 2019-07-11 stsp == -1)
5708 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5709 818c7501 2019-07-11 stsp
5710 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5711 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5712 818c7501 2019-07-11 stsp free(ondisk_path);
5713 818c7501 2019-07-11 stsp if (err)
5714 818c7501 2019-07-11 stsp return err;
5715 818c7501 2019-07-11 stsp
5716 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5717 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5718 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5719 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5720 818c7501 2019-07-11 stsp
5721 818c7501 2019-07-11 stsp return NULL;
5722 818c7501 2019-07-11 stsp }
5723 818c7501 2019-07-11 stsp
5724 818c7501 2019-07-11 stsp const struct got_error *
5725 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5726 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5727 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5728 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5729 818c7501 2019-07-11 stsp {
5730 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5731 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5732 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5733 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5734 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5735 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5736 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5737 818c7501 2019-07-11 stsp
5738 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5739 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5740 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5741 818c7501 2019-07-11 stsp
5742 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5743 818c7501 2019-07-11 stsp if (err)
5744 818c7501 2019-07-11 stsp return err;
5745 818c7501 2019-07-11 stsp
5746 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5747 818c7501 2019-07-11 stsp if (err)
5748 818c7501 2019-07-11 stsp goto done;
5749 818c7501 2019-07-11 stsp
5750 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5751 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5752 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5753 818c7501 2019-07-11 stsp &ok_arg);
5754 818c7501 2019-07-11 stsp if (err)
5755 818c7501 2019-07-11 stsp goto done;
5756 818c7501 2019-07-11 stsp
5757 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5758 818c7501 2019-07-11 stsp if (err)
5759 818c7501 2019-07-11 stsp goto done;
5760 818c7501 2019-07-11 stsp
5761 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5762 818c7501 2019-07-11 stsp if (err)
5763 818c7501 2019-07-11 stsp goto done;
5764 818c7501 2019-07-11 stsp
5765 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5766 818c7501 2019-07-11 stsp if (err)
5767 818c7501 2019-07-11 stsp goto done;
5768 818c7501 2019-07-11 stsp
5769 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5770 818c7501 2019-07-11 stsp 0);
5771 e51d7b55 2020-01-04 stsp if (err)
5772 e51d7b55 2020-01-04 stsp goto done;
5773 e51d7b55 2020-01-04 stsp
5774 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5775 818c7501 2019-07-11 stsp if (err)
5776 818c7501 2019-07-11 stsp goto done;
5777 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5778 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5779 e51d7b55 2020-01-04 stsp goto done;
5780 e51d7b55 2020-01-04 stsp }
5781 818c7501 2019-07-11 stsp
5782 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5783 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5784 818c7501 2019-07-11 stsp if (err)
5785 818c7501 2019-07-11 stsp goto done;
5786 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5787 818c7501 2019-07-11 stsp if (err)
5788 818c7501 2019-07-11 stsp goto done;
5789 818c7501 2019-07-11 stsp
5790 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5791 818c7501 2019-07-11 stsp
5792 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5793 818c7501 2019-07-11 stsp if (err)
5794 818c7501 2019-07-11 stsp goto done;
5795 818c7501 2019-07-11 stsp
5796 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5797 818c7501 2019-07-11 stsp if (err)
5798 818c7501 2019-07-11 stsp goto done;
5799 818c7501 2019-07-11 stsp
5800 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5801 818c7501 2019-07-11 stsp worktree->base_commit_id);
5802 818c7501 2019-07-11 stsp if (err)
5803 818c7501 2019-07-11 stsp goto done;
5804 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5805 818c7501 2019-07-11 stsp if (err)
5806 818c7501 2019-07-11 stsp goto done;
5807 818c7501 2019-07-11 stsp
5808 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5809 818c7501 2019-07-11 stsp if (err)
5810 818c7501 2019-07-11 stsp goto done;
5811 818c7501 2019-07-11 stsp done:
5812 818c7501 2019-07-11 stsp free(fileindex_path);
5813 818c7501 2019-07-11 stsp free(tmp_branch_name);
5814 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5815 818c7501 2019-07-11 stsp free(branch_ref_name);
5816 818c7501 2019-07-11 stsp if (branch_ref)
5817 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5818 818c7501 2019-07-11 stsp if (wt_branch)
5819 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5820 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5821 818c7501 2019-07-11 stsp if (err) {
5822 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5823 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5824 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5825 818c7501 2019-07-11 stsp }
5826 818c7501 2019-07-11 stsp if (*tmp_branch) {
5827 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5828 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5829 818c7501 2019-07-11 stsp }
5830 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5831 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5832 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5833 3e3a69f1 2019-07-25 stsp }
5834 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5835 818c7501 2019-07-11 stsp }
5836 818c7501 2019-07-11 stsp return err;
5837 818c7501 2019-07-11 stsp }
5838 818c7501 2019-07-11 stsp
5839 818c7501 2019-07-11 stsp const struct got_error *
5840 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5841 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5842 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5843 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5844 818c7501 2019-07-11 stsp {
5845 818c7501 2019-07-11 stsp const struct got_error *err;
5846 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5847 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5848 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5849 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5850 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5851 818c7501 2019-07-11 stsp
5852 818c7501 2019-07-11 stsp *commit_id = NULL;
5853 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5854 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5855 3e3a69f1 2019-07-25 stsp *branch = NULL;
5856 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5857 3e3a69f1 2019-07-25 stsp
5858 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5859 3e3a69f1 2019-07-25 stsp if (err)
5860 3e3a69f1 2019-07-25 stsp return err;
5861 818c7501 2019-07-11 stsp
5862 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5863 3e3a69f1 2019-07-25 stsp if (err)
5864 f032f1f7 2019-08-04 stsp goto done;
5865 f032f1f7 2019-08-04 stsp
5866 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5867 f032f1f7 2019-08-04 stsp &have_staged_files);
5868 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5869 f032f1f7 2019-08-04 stsp goto done;
5870 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5871 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5872 3e3a69f1 2019-07-25 stsp goto done;
5873 f032f1f7 2019-08-04 stsp }
5874 3e3a69f1 2019-07-25 stsp
5875 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5876 818c7501 2019-07-11 stsp if (err)
5877 f032f1f7 2019-08-04 stsp goto done;
5878 818c7501 2019-07-11 stsp
5879 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5880 818c7501 2019-07-11 stsp if (err)
5881 818c7501 2019-07-11 stsp goto done;
5882 818c7501 2019-07-11 stsp
5883 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
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 = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5888 818c7501 2019-07-11 stsp if (err)
5889 818c7501 2019-07-11 stsp goto done;
5890 818c7501 2019-07-11 stsp
5891 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5892 818c7501 2019-07-11 stsp if (err)
5893 818c7501 2019-07-11 stsp goto done;
5894 818c7501 2019-07-11 stsp
5895 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5896 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5897 818c7501 2019-07-11 stsp if (err)
5898 818c7501 2019-07-11 stsp goto done;
5899 818c7501 2019-07-11 stsp
5900 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5901 818c7501 2019-07-11 stsp if (err)
5902 818c7501 2019-07-11 stsp goto done;
5903 818c7501 2019-07-11 stsp
5904 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5905 818c7501 2019-07-11 stsp if (err)
5906 818c7501 2019-07-11 stsp goto done;
5907 818c7501 2019-07-11 stsp
5908 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5909 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5910 818c7501 2019-07-11 stsp if (err)
5911 818c7501 2019-07-11 stsp goto done;
5912 818c7501 2019-07-11 stsp
5913 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5914 818c7501 2019-07-11 stsp if (err)
5915 818c7501 2019-07-11 stsp goto done;
5916 818c7501 2019-07-11 stsp done:
5917 818c7501 2019-07-11 stsp free(commit_ref_name);
5918 818c7501 2019-07-11 stsp free(branch_ref_name);
5919 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5920 818c7501 2019-07-11 stsp if (commit_ref)
5921 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5922 818c7501 2019-07-11 stsp if (branch_ref)
5923 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5924 818c7501 2019-07-11 stsp if (err) {
5925 818c7501 2019-07-11 stsp free(*commit_id);
5926 818c7501 2019-07-11 stsp *commit_id = NULL;
5927 818c7501 2019-07-11 stsp if (*tmp_branch) {
5928 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5929 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5930 818c7501 2019-07-11 stsp }
5931 818c7501 2019-07-11 stsp if (*new_base_branch) {
5932 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5933 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5934 818c7501 2019-07-11 stsp }
5935 818c7501 2019-07-11 stsp if (*branch) {
5936 818c7501 2019-07-11 stsp got_ref_close(*branch);
5937 818c7501 2019-07-11 stsp *branch = NULL;
5938 818c7501 2019-07-11 stsp }
5939 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5940 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5941 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5942 3e3a69f1 2019-07-25 stsp }
5943 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5944 818c7501 2019-07-11 stsp }
5945 818c7501 2019-07-11 stsp return err;
5946 c4296144 2019-05-09 stsp }
5947 818c7501 2019-07-11 stsp
5948 818c7501 2019-07-11 stsp const struct got_error *
5949 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5950 818c7501 2019-07-11 stsp {
5951 818c7501 2019-07-11 stsp const struct got_error *err;
5952 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5953 818c7501 2019-07-11 stsp
5954 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5955 818c7501 2019-07-11 stsp if (err)
5956 818c7501 2019-07-11 stsp return err;
5957 818c7501 2019-07-11 stsp
5958 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5959 818c7501 2019-07-11 stsp free(tmp_branch_name);
5960 818c7501 2019-07-11 stsp return NULL;
5961 818c7501 2019-07-11 stsp }
5962 818c7501 2019-07-11 stsp
5963 818c7501 2019-07-11 stsp static const struct got_error *
5964 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5965 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5966 818c7501 2019-07-11 stsp {
5967 0ebf8283 2019-07-24 stsp *logmsg = arg;
5968 818c7501 2019-07-11 stsp return NULL;
5969 818c7501 2019-07-11 stsp }
5970 818c7501 2019-07-11 stsp
5971 818c7501 2019-07-11 stsp static const struct got_error *
5972 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5973 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5974 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5975 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5976 818c7501 2019-07-11 stsp {
5977 818c7501 2019-07-11 stsp return NULL;
5978 01757395 2019-07-12 stsp }
5979 01757395 2019-07-12 stsp
5980 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5981 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5982 01757395 2019-07-12 stsp void *progress_arg;
5983 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5984 01757395 2019-07-12 stsp };
5985 01757395 2019-07-12 stsp
5986 01757395 2019-07-12 stsp static const struct got_error *
5987 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5988 01757395 2019-07-12 stsp {
5989 01757395 2019-07-12 stsp const struct got_error *err;
5990 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5991 01757395 2019-07-12 stsp char *p;
5992 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5993 01757395 2019-07-12 stsp
5994 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5995 01757395 2019-07-12 stsp if (err)
5996 01757395 2019-07-12 stsp return err;
5997 01757395 2019-07-12 stsp
5998 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5999 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
6000 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
6001 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
6002 01757395 2019-07-12 stsp return NULL;
6003 01757395 2019-07-12 stsp
6004 01757395 2019-07-12 stsp p = strdup(path);
6005 01757395 2019-07-12 stsp if (p == NULL)
6006 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
6007 01757395 2019-07-12 stsp
6008 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6009 01757395 2019-07-12 stsp if (err || new == NULL)
6010 01757395 2019-07-12 stsp free(p);
6011 01757395 2019-07-12 stsp return err;
6012 01757395 2019-07-12 stsp }
6013 01757395 2019-07-12 stsp
6014 01757395 2019-07-12 stsp void
6015 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6016 01757395 2019-07-12 stsp {
6017 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6018 01757395 2019-07-12 stsp
6019 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
6020 01757395 2019-07-12 stsp free((char *)pe->path);
6021 01757395 2019-07-12 stsp
6022 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
6023 818c7501 2019-07-11 stsp }
6024 818c7501 2019-07-11 stsp
6025 0ebf8283 2019-07-24 stsp static const struct got_error *
6026 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6027 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
6028 818c7501 2019-07-11 stsp {
6029 818c7501 2019-07-11 stsp const struct got_error *err;
6030 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
6031 818c7501 2019-07-11 stsp
6032 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6033 818c7501 2019-07-11 stsp if (err) {
6034 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
6035 818c7501 2019-07-11 stsp goto done;
6036 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6037 818c7501 2019-07-11 stsp if (err)
6038 818c7501 2019-07-11 stsp goto done;
6039 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
6040 818c7501 2019-07-11 stsp if (err)
6041 818c7501 2019-07-11 stsp goto done;
6042 de05890f 2020-03-05 stsp } else if (is_rebase) {
6043 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
6044 818c7501 2019-07-11 stsp int cmp;
6045 818c7501 2019-07-11 stsp
6046 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
6047 818c7501 2019-07-11 stsp if (err)
6048 818c7501 2019-07-11 stsp goto done;
6049 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
6050 818c7501 2019-07-11 stsp free(stored_id);
6051 818c7501 2019-07-11 stsp if (cmp != 0) {
6052 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6053 818c7501 2019-07-11 stsp goto done;
6054 818c7501 2019-07-11 stsp }
6055 818c7501 2019-07-11 stsp }
6056 0ebf8283 2019-07-24 stsp done:
6057 0ebf8283 2019-07-24 stsp if (commit_ref)
6058 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6059 0ebf8283 2019-07-24 stsp return err;
6060 0ebf8283 2019-07-24 stsp }
6061 0ebf8283 2019-07-24 stsp
6062 0ebf8283 2019-07-24 stsp static const struct got_error *
6063 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
6064 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
6065 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6066 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
6067 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6068 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6069 0ebf8283 2019-07-24 stsp {
6070 0ebf8283 2019-07-24 stsp const struct got_error *err;
6071 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6072 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
6073 3e3a69f1 2019-07-25 stsp char *fileindex_path;
6074 818c7501 2019-07-11 stsp
6075 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6076 0ebf8283 2019-07-24 stsp
6077 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6078 0ebf8283 2019-07-24 stsp if (err)
6079 0ebf8283 2019-07-24 stsp return err;
6080 0ebf8283 2019-07-24 stsp
6081 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
6082 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
6083 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
6084 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
6085 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
6086 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
6087 818c7501 2019-07-11 stsp if (commit_ref)
6088 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
6089 818c7501 2019-07-11 stsp return err;
6090 818c7501 2019-07-11 stsp }
6091 818c7501 2019-07-11 stsp
6092 818c7501 2019-07-11 stsp const struct got_error *
6093 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6094 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6095 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6096 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6097 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6098 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6099 818c7501 2019-07-11 stsp {
6100 0ebf8283 2019-07-24 stsp const struct got_error *err;
6101 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6102 0ebf8283 2019-07-24 stsp
6103 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6104 0ebf8283 2019-07-24 stsp if (err)
6105 0ebf8283 2019-07-24 stsp return err;
6106 0ebf8283 2019-07-24 stsp
6107 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6108 0ebf8283 2019-07-24 stsp if (err)
6109 0ebf8283 2019-07-24 stsp goto done;
6110 0ebf8283 2019-07-24 stsp
6111 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6112 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6113 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6114 0ebf8283 2019-07-24 stsp done:
6115 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6116 0ebf8283 2019-07-24 stsp return err;
6117 0ebf8283 2019-07-24 stsp }
6118 0ebf8283 2019-07-24 stsp
6119 0ebf8283 2019-07-24 stsp const struct got_error *
6120 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6121 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6122 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6123 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
6124 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6125 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6126 0ebf8283 2019-07-24 stsp {
6127 0ebf8283 2019-07-24 stsp const struct got_error *err;
6128 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6129 0ebf8283 2019-07-24 stsp
6130 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6131 0ebf8283 2019-07-24 stsp if (err)
6132 0ebf8283 2019-07-24 stsp return err;
6133 0ebf8283 2019-07-24 stsp
6134 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6135 0ebf8283 2019-07-24 stsp if (err)
6136 0ebf8283 2019-07-24 stsp goto done;
6137 0ebf8283 2019-07-24 stsp
6138 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6139 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
6140 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
6141 0ebf8283 2019-07-24 stsp done:
6142 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6143 0ebf8283 2019-07-24 stsp return err;
6144 0ebf8283 2019-07-24 stsp }
6145 0ebf8283 2019-07-24 stsp
6146 0ebf8283 2019-07-24 stsp static const struct got_error *
6147 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
6148 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6149 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6150 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6151 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
6152 0ebf8283 2019-07-24 stsp {
6153 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
6154 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
6155 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
6156 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6157 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
6158 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
6159 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
6160 818c7501 2019-07-11 stsp
6161 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
6162 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
6163 a0e95631 2019-07-12 stsp
6164 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
6165 818c7501 2019-07-11 stsp
6166 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6167 a0e95631 2019-07-12 stsp if (err)
6168 3e3a69f1 2019-07-25 stsp return err;
6169 a0e95631 2019-07-12 stsp
6170 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
6171 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
6172 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
6173 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
6174 01757395 2019-07-12 stsp /*
6175 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
6176 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
6177 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
6178 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
6179 01757395 2019-07-12 stsp */
6180 01757395 2019-07-12 stsp if (merged_paths) {
6181 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
6182 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
6183 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
6184 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6185 f2a9dc41 2019-12-13 tracey 0);
6186 01757395 2019-07-12 stsp if (err)
6187 01757395 2019-07-12 stsp goto done;
6188 01757395 2019-07-12 stsp }
6189 01757395 2019-07-12 stsp } else {
6190 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6191 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6192 01757395 2019-07-12 stsp if (err)
6193 01757395 2019-07-12 stsp goto done;
6194 01757395 2019-07-12 stsp }
6195 a0e95631 2019-07-12 stsp
6196 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
6197 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
6198 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6199 a0e95631 2019-07-12 stsp if (err)
6200 a0e95631 2019-07-12 stsp goto done;
6201 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6202 a0e95631 2019-07-12 stsp goto done;
6203 ff0d2220 2019-07-11 stsp }
6204 818c7501 2019-07-11 stsp
6205 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6206 edd02c5e 2019-07-11 stsp if (err)
6207 edd02c5e 2019-07-11 stsp goto done;
6208 edd02c5e 2019-07-11 stsp
6209 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6210 818c7501 2019-07-11 stsp if (err)
6211 818c7501 2019-07-11 stsp goto done;
6212 818c7501 2019-07-11 stsp
6213 5943eee2 2019-08-13 stsp if (new_logmsg) {
6214 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
6215 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
6216 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6217 5943eee2 2019-08-13 stsp goto done;
6218 5943eee2 2019-08-13 stsp }
6219 5943eee2 2019-08-13 stsp } else {
6220 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6221 5943eee2 2019-08-13 stsp if (err)
6222 5943eee2 2019-08-13 stsp goto done;
6223 5943eee2 2019-08-13 stsp }
6224 0ebf8283 2019-07-24 stsp
6225 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
6226 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6227 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
6228 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
6229 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6230 a0e95631 2019-07-12 stsp if (err)
6231 a0e95631 2019-07-12 stsp goto done;
6232 a0e95631 2019-07-12 stsp
6233 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
6234 a0e95631 2019-07-12 stsp if (err)
6235 a0e95631 2019-07-12 stsp goto done;
6236 a0e95631 2019-07-12 stsp
6237 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
6238 a0e95631 2019-07-12 stsp if (err)
6239 a0e95631 2019-07-12 stsp goto done;
6240 a0e95631 2019-07-12 stsp
6241 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
6242 72fd46fa 2019-09-06 stsp fileindex, 0);
6243 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6244 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
6245 a0e95631 2019-07-12 stsp err = sync_err;
6246 818c7501 2019-07-11 stsp done:
6247 a0e95631 2019-07-12 stsp free(fileindex_path);
6248 a0e95631 2019-07-12 stsp free(head_commit_id);
6249 a0e95631 2019-07-12 stsp if (head_ref)
6250 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
6251 818c7501 2019-07-11 stsp if (err) {
6252 818c7501 2019-07-11 stsp free(*new_commit_id);
6253 818c7501 2019-07-11 stsp *new_commit_id = NULL;
6254 818c7501 2019-07-11 stsp }
6255 818c7501 2019-07-11 stsp return err;
6256 818c7501 2019-07-11 stsp }
6257 818c7501 2019-07-11 stsp
6258 818c7501 2019-07-11 stsp const struct got_error *
6259 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6260 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6261 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6262 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6263 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
6264 0ebf8283 2019-07-24 stsp {
6265 0ebf8283 2019-07-24 stsp const struct got_error *err;
6266 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6267 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6268 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6269 0ebf8283 2019-07-24 stsp
6270 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6271 0ebf8283 2019-07-24 stsp if (err)
6272 0ebf8283 2019-07-24 stsp return err;
6273 0ebf8283 2019-07-24 stsp
6274 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6275 0ebf8283 2019-07-24 stsp if (err)
6276 0ebf8283 2019-07-24 stsp goto done;
6277 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
6278 0ebf8283 2019-07-24 stsp if (err)
6279 0ebf8283 2019-07-24 stsp goto done;
6280 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6281 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
6282 0ebf8283 2019-07-24 stsp goto done;
6283 0ebf8283 2019-07-24 stsp }
6284 0ebf8283 2019-07-24 stsp
6285 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6286 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6287 0ebf8283 2019-07-24 stsp done:
6288 0ebf8283 2019-07-24 stsp if (commit_ref)
6289 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6290 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6291 0ebf8283 2019-07-24 stsp free(commit_id);
6292 0ebf8283 2019-07-24 stsp return err;
6293 0ebf8283 2019-07-24 stsp }
6294 0ebf8283 2019-07-24 stsp
6295 0ebf8283 2019-07-24 stsp const struct got_error *
6296 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6297 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6298 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6299 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
6300 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
6301 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6302 0ebf8283 2019-07-24 stsp {
6303 0ebf8283 2019-07-24 stsp const struct got_error *err;
6304 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6305 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6306 0ebf8283 2019-07-24 stsp
6307 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6308 0ebf8283 2019-07-24 stsp if (err)
6309 0ebf8283 2019-07-24 stsp return err;
6310 0ebf8283 2019-07-24 stsp
6311 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6312 0ebf8283 2019-07-24 stsp if (err)
6313 0ebf8283 2019-07-24 stsp goto done;
6314 0ebf8283 2019-07-24 stsp
6315 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6316 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6317 0ebf8283 2019-07-24 stsp done:
6318 0ebf8283 2019-07-24 stsp if (commit_ref)
6319 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6320 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6321 0ebf8283 2019-07-24 stsp return err;
6322 0ebf8283 2019-07-24 stsp }
6323 0ebf8283 2019-07-24 stsp
6324 0ebf8283 2019-07-24 stsp const struct got_error *
6325 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
6326 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6327 818c7501 2019-07-11 stsp {
6328 3e3a69f1 2019-07-25 stsp if (fileindex)
6329 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6330 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
6331 69844fba 2019-07-11 stsp }
6332 69844fba 2019-07-11 stsp
6333 69844fba 2019-07-11 stsp static const struct got_error *
6334 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
6335 69844fba 2019-07-11 stsp {
6336 69844fba 2019-07-11 stsp const struct got_error *err;
6337 69844fba 2019-07-11 stsp struct got_reference *ref;
6338 69844fba 2019-07-11 stsp
6339 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
6340 69844fba 2019-07-11 stsp if (err) {
6341 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
6342 69844fba 2019-07-11 stsp return NULL;
6343 69844fba 2019-07-11 stsp return err;
6344 69844fba 2019-07-11 stsp }
6345 69844fba 2019-07-11 stsp
6346 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
6347 69844fba 2019-07-11 stsp got_ref_close(ref);
6348 69844fba 2019-07-11 stsp return err;
6349 818c7501 2019-07-11 stsp }
6350 818c7501 2019-07-11 stsp
6351 69844fba 2019-07-11 stsp static const struct got_error *
6352 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6353 69844fba 2019-07-11 stsp {
6354 69844fba 2019-07-11 stsp const struct got_error *err;
6355 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6356 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6357 69844fba 2019-07-11 stsp
6358 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6359 69844fba 2019-07-11 stsp if (err)
6360 69844fba 2019-07-11 stsp goto done;
6361 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
6362 69844fba 2019-07-11 stsp if (err)
6363 69844fba 2019-07-11 stsp goto done;
6364 69844fba 2019-07-11 stsp
6365 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6366 69844fba 2019-07-11 stsp if (err)
6367 69844fba 2019-07-11 stsp goto done;
6368 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
6369 69844fba 2019-07-11 stsp if (err)
6370 69844fba 2019-07-11 stsp goto done;
6371 69844fba 2019-07-11 stsp
6372 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6373 69844fba 2019-07-11 stsp if (err)
6374 69844fba 2019-07-11 stsp goto done;
6375 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
6376 69844fba 2019-07-11 stsp if (err)
6377 69844fba 2019-07-11 stsp goto done;
6378 69844fba 2019-07-11 stsp
6379 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6380 69844fba 2019-07-11 stsp if (err)
6381 69844fba 2019-07-11 stsp goto done;
6382 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
6383 69844fba 2019-07-11 stsp if (err)
6384 69844fba 2019-07-11 stsp goto done;
6385 69844fba 2019-07-11 stsp
6386 69844fba 2019-07-11 stsp done:
6387 69844fba 2019-07-11 stsp free(tmp_branch_name);
6388 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
6389 69844fba 2019-07-11 stsp free(branch_ref_name);
6390 69844fba 2019-07-11 stsp free(commit_ref_name);
6391 69844fba 2019-07-11 stsp return err;
6392 69844fba 2019-07-11 stsp }
6393 69844fba 2019-07-11 stsp
6394 818c7501 2019-07-11 stsp const struct got_error *
6395 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
6396 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6397 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6398 818c7501 2019-07-11 stsp struct got_repository *repo)
6399 818c7501 2019-07-11 stsp {
6400 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
6401 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
6402 818c7501 2019-07-11 stsp
6403 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6404 818c7501 2019-07-11 stsp if (err)
6405 818c7501 2019-07-11 stsp return err;
6406 818c7501 2019-07-11 stsp
6407 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6408 818c7501 2019-07-11 stsp if (err)
6409 818c7501 2019-07-11 stsp goto done;
6410 818c7501 2019-07-11 stsp
6411 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
6412 818c7501 2019-07-11 stsp if (err)
6413 818c7501 2019-07-11 stsp goto done;
6414 818c7501 2019-07-11 stsp
6415 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
6416 818c7501 2019-07-11 stsp if (err)
6417 818c7501 2019-07-11 stsp goto done;
6418 818c7501 2019-07-11 stsp
6419 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
6420 818c7501 2019-07-11 stsp done:
6421 3e3a69f1 2019-07-25 stsp if (fileindex)
6422 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6423 818c7501 2019-07-11 stsp free(new_head_commit_id);
6424 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6425 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6426 818c7501 2019-07-11 stsp err = unlockerr;
6427 818c7501 2019-07-11 stsp return err;
6428 818c7501 2019-07-11 stsp }
6429 818c7501 2019-07-11 stsp
6430 818c7501 2019-07-11 stsp const struct got_error *
6431 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
6432 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6433 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
6434 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6435 818c7501 2019-07-11 stsp {
6436 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
6437 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
6438 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
6439 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
6440 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6441 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
6442 818c7501 2019-07-11 stsp
6443 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
6444 818c7501 2019-07-11 stsp if (err)
6445 818c7501 2019-07-11 stsp return err;
6446 818c7501 2019-07-11 stsp
6447 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
6448 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
6449 818c7501 2019-07-11 stsp if (err)
6450 818c7501 2019-07-11 stsp goto done;
6451 818c7501 2019-07-11 stsp
6452 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
6453 818c7501 2019-07-11 stsp if (err)
6454 818c7501 2019-07-11 stsp goto done;
6455 818c7501 2019-07-11 stsp
6456 818c7501 2019-07-11 stsp /*
6457 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
6458 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
6459 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
6460 818c7501 2019-07-11 stsp */
6461 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
6462 818c7501 2019-07-11 stsp if (err)
6463 818c7501 2019-07-11 stsp goto done;
6464 818c7501 2019-07-11 stsp
6465 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6466 818c7501 2019-07-11 stsp if (err)
6467 818c7501 2019-07-11 stsp goto done;
6468 818c7501 2019-07-11 stsp
6469 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
6470 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
6471 ca355955 2019-07-12 stsp if (err)
6472 ca355955 2019-07-12 stsp goto done;
6473 ca355955 2019-07-12 stsp
6474 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
6475 ca355955 2019-07-12 stsp if (err)
6476 ca355955 2019-07-12 stsp goto done;
6477 ca355955 2019-07-12 stsp
6478 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6479 818c7501 2019-07-11 stsp if (err)
6480 818c7501 2019-07-11 stsp goto done;
6481 818c7501 2019-07-11 stsp
6482 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6483 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6484 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6485 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6486 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6487 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6488 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6489 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
6490 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6491 55bd499d 2019-07-12 stsp if (err)
6492 1f1abb7e 2019-08-08 stsp goto sync;
6493 55bd499d 2019-07-12 stsp
6494 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6495 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
6496 ca355955 2019-07-12 stsp sync:
6497 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6498 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
6499 ca355955 2019-07-12 stsp err = sync_err;
6500 818c7501 2019-07-11 stsp done:
6501 818c7501 2019-07-11 stsp got_ref_close(resolved);
6502 a3a2faf2 2019-07-12 stsp free(tree_id);
6503 818c7501 2019-07-11 stsp free(commit_id);
6504 0ebf8283 2019-07-24 stsp if (fileindex)
6505 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
6506 0ebf8283 2019-07-24 stsp free(fileindex_path);
6507 0ebf8283 2019-07-24 stsp
6508 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6509 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6510 0ebf8283 2019-07-24 stsp err = unlockerr;
6511 0ebf8283 2019-07-24 stsp return err;
6512 0ebf8283 2019-07-24 stsp }
6513 0ebf8283 2019-07-24 stsp
6514 0ebf8283 2019-07-24 stsp const struct got_error *
6515 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6516 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6517 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
6518 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6519 0ebf8283 2019-07-24 stsp {
6520 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6521 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6522 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
6523 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
6524 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6525 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
6526 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
6527 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6528 0ebf8283 2019-07-24 stsp
6529 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6530 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6531 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6532 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6533 0ebf8283 2019-07-24 stsp
6534 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6535 0ebf8283 2019-07-24 stsp if (err)
6536 0ebf8283 2019-07-24 stsp return err;
6537 0ebf8283 2019-07-24 stsp
6538 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6539 0ebf8283 2019-07-24 stsp if (err)
6540 0ebf8283 2019-07-24 stsp goto done;
6541 0ebf8283 2019-07-24 stsp
6542 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
6543 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
6544 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6545 0ebf8283 2019-07-24 stsp &ok_arg);
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 = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6550 0ebf8283 2019-07-24 stsp if (err)
6551 0ebf8283 2019-07-24 stsp goto done;
6552 0ebf8283 2019-07-24 stsp
6553 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6554 0ebf8283 2019-07-24 stsp if (err)
6555 0ebf8283 2019-07-24 stsp goto done;
6556 0ebf8283 2019-07-24 stsp
6557 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6558 0ebf8283 2019-07-24 stsp worktree);
6559 0ebf8283 2019-07-24 stsp if (err)
6560 0ebf8283 2019-07-24 stsp goto done;
6561 0ebf8283 2019-07-24 stsp
6562 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6563 0ebf8283 2019-07-24 stsp 0);
6564 0ebf8283 2019-07-24 stsp if (err)
6565 0ebf8283 2019-07-24 stsp goto done;
6566 0ebf8283 2019-07-24 stsp
6567 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6568 0ebf8283 2019-07-24 stsp if (err)
6569 0ebf8283 2019-07-24 stsp goto done;
6570 0ebf8283 2019-07-24 stsp
6571 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
6572 0ebf8283 2019-07-24 stsp if (err)
6573 0ebf8283 2019-07-24 stsp goto done;
6574 0ebf8283 2019-07-24 stsp
6575 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6576 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6577 0ebf8283 2019-07-24 stsp if (err)
6578 0ebf8283 2019-07-24 stsp goto done;
6579 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
6580 0ebf8283 2019-07-24 stsp if (err)
6581 0ebf8283 2019-07-24 stsp goto done;
6582 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6583 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
6584 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
6585 0ebf8283 2019-07-24 stsp goto done;
6586 0ebf8283 2019-07-24 stsp }
6587 0ebf8283 2019-07-24 stsp
6588 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
6589 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
6590 0ebf8283 2019-07-24 stsp if (err)
6591 0ebf8283 2019-07-24 stsp goto done;
6592 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
6593 0ebf8283 2019-07-24 stsp if (err)
6594 0ebf8283 2019-07-24 stsp goto done;
6595 0ebf8283 2019-07-24 stsp
6596 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
6597 0ebf8283 2019-07-24 stsp if (err)
6598 0ebf8283 2019-07-24 stsp goto done;
6599 0ebf8283 2019-07-24 stsp done:
6600 0ebf8283 2019-07-24 stsp free(fileindex_path);
6601 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6602 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6603 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6604 0ebf8283 2019-07-24 stsp if (wt_branch)
6605 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
6606 0ebf8283 2019-07-24 stsp if (err) {
6607 0ebf8283 2019-07-24 stsp if (*branch_ref) {
6608 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
6609 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
6610 0ebf8283 2019-07-24 stsp }
6611 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6612 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6613 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6614 0ebf8283 2019-07-24 stsp }
6615 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6616 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6617 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6618 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6619 3e3a69f1 2019-07-25 stsp }
6620 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
6621 0ebf8283 2019-07-24 stsp }
6622 0ebf8283 2019-07-24 stsp return err;
6623 0ebf8283 2019-07-24 stsp }
6624 0ebf8283 2019-07-24 stsp
6625 0ebf8283 2019-07-24 stsp const struct got_error *
6626 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
6627 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
6628 0ebf8283 2019-07-24 stsp {
6629 3e3a69f1 2019-07-25 stsp if (fileindex)
6630 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6631 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
6632 0ebf8283 2019-07-24 stsp }
6633 0ebf8283 2019-07-24 stsp
6634 0ebf8283 2019-07-24 stsp const struct got_error *
6635 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6636 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6637 0ebf8283 2019-07-24 stsp {
6638 0ebf8283 2019-07-24 stsp const struct got_error *err;
6639 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6640 0ebf8283 2019-07-24 stsp
6641 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6642 0ebf8283 2019-07-24 stsp if (err)
6643 0ebf8283 2019-07-24 stsp return err;
6644 0ebf8283 2019-07-24 stsp
6645 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6646 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6647 0ebf8283 2019-07-24 stsp return NULL;
6648 0ebf8283 2019-07-24 stsp }
6649 0ebf8283 2019-07-24 stsp
6650 0ebf8283 2019-07-24 stsp const struct got_error *
6651 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6652 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6653 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6654 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6655 0ebf8283 2019-07-24 stsp {
6656 0ebf8283 2019-07-24 stsp const struct got_error *err;
6657 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6658 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6659 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6660 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6661 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6662 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6663 0ebf8283 2019-07-24 stsp
6664 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6665 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6666 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6667 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6668 0ebf8283 2019-07-24 stsp
6669 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6670 3e3a69f1 2019-07-25 stsp if (err)
6671 3e3a69f1 2019-07-25 stsp return err;
6672 3e3a69f1 2019-07-25 stsp
6673 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6674 3e3a69f1 2019-07-25 stsp if (err)
6675 f032f1f7 2019-08-04 stsp goto done;
6676 f032f1f7 2019-08-04 stsp
6677 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6678 f032f1f7 2019-08-04 stsp &have_staged_files);
6679 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6680 f032f1f7 2019-08-04 stsp goto done;
6681 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6682 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6683 3e3a69f1 2019-07-25 stsp goto done;
6684 f032f1f7 2019-08-04 stsp }
6685 3e3a69f1 2019-07-25 stsp
6686 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6687 0ebf8283 2019-07-24 stsp if (err)
6688 f032f1f7 2019-08-04 stsp goto done;
6689 0ebf8283 2019-07-24 stsp
6690 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6691 0ebf8283 2019-07-24 stsp if (err)
6692 0ebf8283 2019-07-24 stsp goto done;
6693 0ebf8283 2019-07-24 stsp
6694 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6695 0ebf8283 2019-07-24 stsp if (err)
6696 0ebf8283 2019-07-24 stsp goto done;
6697 0ebf8283 2019-07-24 stsp
6698 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6699 0ebf8283 2019-07-24 stsp worktree);
6700 0ebf8283 2019-07-24 stsp if (err)
6701 0ebf8283 2019-07-24 stsp goto done;
6702 0ebf8283 2019-07-24 stsp
6703 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6704 0ebf8283 2019-07-24 stsp if (err)
6705 0ebf8283 2019-07-24 stsp goto done;
6706 0ebf8283 2019-07-24 stsp
6707 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6708 0ebf8283 2019-07-24 stsp if (err)
6709 0ebf8283 2019-07-24 stsp goto done;
6710 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6711 0ebf8283 2019-07-24 stsp if (err)
6712 0ebf8283 2019-07-24 stsp goto done;
6713 0ebf8283 2019-07-24 stsp
6714 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6715 0ebf8283 2019-07-24 stsp if (err)
6716 0ebf8283 2019-07-24 stsp goto done;
6717 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6718 0ebf8283 2019-07-24 stsp if (err)
6719 0ebf8283 2019-07-24 stsp goto done;
6720 0ebf8283 2019-07-24 stsp
6721 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6722 0ebf8283 2019-07-24 stsp if (err)
6723 0ebf8283 2019-07-24 stsp goto done;
6724 0ebf8283 2019-07-24 stsp done:
6725 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6726 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6727 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6728 0ebf8283 2019-07-24 stsp if (commit_ref)
6729 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6730 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6731 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6732 0ebf8283 2019-07-24 stsp if (err) {
6733 0ebf8283 2019-07-24 stsp free(*commit_id);
6734 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6735 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6736 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6737 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6738 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6739 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6740 0ebf8283 2019-07-24 stsp }
6741 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6742 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6743 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6744 3e3a69f1 2019-07-25 stsp }
6745 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6746 0ebf8283 2019-07-24 stsp }
6747 0ebf8283 2019-07-24 stsp return err;
6748 0ebf8283 2019-07-24 stsp }
6749 0ebf8283 2019-07-24 stsp
6750 0ebf8283 2019-07-24 stsp static const struct got_error *
6751 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6752 0ebf8283 2019-07-24 stsp {
6753 0ebf8283 2019-07-24 stsp const struct got_error *err;
6754 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6755 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6756 0ebf8283 2019-07-24 stsp
6757 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6758 0ebf8283 2019-07-24 stsp if (err)
6759 0ebf8283 2019-07-24 stsp goto done;
6760 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6761 0ebf8283 2019-07-24 stsp if (err)
6762 0ebf8283 2019-07-24 stsp goto done;
6763 0ebf8283 2019-07-24 stsp
6764 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6765 0ebf8283 2019-07-24 stsp worktree);
6766 0ebf8283 2019-07-24 stsp if (err)
6767 0ebf8283 2019-07-24 stsp goto done;
6768 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6769 0ebf8283 2019-07-24 stsp if (err)
6770 0ebf8283 2019-07-24 stsp goto done;
6771 0ebf8283 2019-07-24 stsp
6772 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6773 0ebf8283 2019-07-24 stsp if (err)
6774 0ebf8283 2019-07-24 stsp goto done;
6775 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6776 0ebf8283 2019-07-24 stsp if (err)
6777 0ebf8283 2019-07-24 stsp goto done;
6778 0ebf8283 2019-07-24 stsp
6779 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6780 0ebf8283 2019-07-24 stsp if (err)
6781 0ebf8283 2019-07-24 stsp goto done;
6782 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6783 0ebf8283 2019-07-24 stsp if (err)
6784 0ebf8283 2019-07-24 stsp goto done;
6785 0ebf8283 2019-07-24 stsp done:
6786 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6787 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6788 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6789 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6790 0ebf8283 2019-07-24 stsp return err;
6791 0ebf8283 2019-07-24 stsp }
6792 0ebf8283 2019-07-24 stsp
6793 0ebf8283 2019-07-24 stsp const struct got_error *
6794 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6795 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6796 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6797 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6798 0ebf8283 2019-07-24 stsp {
6799 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6800 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6801 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6802 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6803 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6804 0ebf8283 2019-07-24 stsp
6805 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6806 0ebf8283 2019-07-24 stsp if (err)
6807 0ebf8283 2019-07-24 stsp return err;
6808 0ebf8283 2019-07-24 stsp
6809 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6810 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6811 0ebf8283 2019-07-24 stsp if (err)
6812 0ebf8283 2019-07-24 stsp goto done;
6813 0ebf8283 2019-07-24 stsp
6814 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6815 0ebf8283 2019-07-24 stsp if (err)
6816 0ebf8283 2019-07-24 stsp goto done;
6817 0ebf8283 2019-07-24 stsp
6818 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6819 0ebf8283 2019-07-24 stsp if (err)
6820 0ebf8283 2019-07-24 stsp goto done;
6821 0ebf8283 2019-07-24 stsp
6822 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6823 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6824 0ebf8283 2019-07-24 stsp if (err)
6825 0ebf8283 2019-07-24 stsp goto done;
6826 0ebf8283 2019-07-24 stsp
6827 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6828 0ebf8283 2019-07-24 stsp if (err)
6829 0ebf8283 2019-07-24 stsp goto done;
6830 0ebf8283 2019-07-24 stsp
6831 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6832 0ebf8283 2019-07-24 stsp if (err)
6833 0ebf8283 2019-07-24 stsp goto done;
6834 0ebf8283 2019-07-24 stsp
6835 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6836 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6837 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6838 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6839 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6840 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6841 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6842 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6843 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6844 0ebf8283 2019-07-24 stsp if (err)
6845 1f1abb7e 2019-08-08 stsp goto sync;
6846 0ebf8283 2019-07-24 stsp
6847 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6848 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6849 0ebf8283 2019-07-24 stsp sync:
6850 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6851 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6852 0ebf8283 2019-07-24 stsp err = sync_err;
6853 0ebf8283 2019-07-24 stsp done:
6854 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6855 0ebf8283 2019-07-24 stsp free(tree_id);
6856 818c7501 2019-07-11 stsp free(fileindex_path);
6857 818c7501 2019-07-11 stsp
6858 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6859 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6860 818c7501 2019-07-11 stsp err = unlockerr;
6861 818c7501 2019-07-11 stsp return err;
6862 818c7501 2019-07-11 stsp }
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp const struct got_error *
6865 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6866 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6867 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6868 0ebf8283 2019-07-24 stsp {
6869 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6870 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6871 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6872 0ebf8283 2019-07-24 stsp
6873 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6874 0ebf8283 2019-07-24 stsp if (err)
6875 0ebf8283 2019-07-24 stsp return err;
6876 0ebf8283 2019-07-24 stsp
6877 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6878 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6879 0ebf8283 2019-07-24 stsp if (err)
6880 0ebf8283 2019-07-24 stsp goto done;
6881 0ebf8283 2019-07-24 stsp
6882 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6883 0ebf8283 2019-07-24 stsp if (err)
6884 0ebf8283 2019-07-24 stsp goto done;
6885 0ebf8283 2019-07-24 stsp
6886 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6887 0ebf8283 2019-07-24 stsp if (err)
6888 0ebf8283 2019-07-24 stsp goto done;
6889 0ebf8283 2019-07-24 stsp
6890 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
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_histedit_refs(worktree, repo);
6895 0ebf8283 2019-07-24 stsp done:
6896 3e3a69f1 2019-07-25 stsp if (fileindex)
6897 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6898 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6899 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6900 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6901 0ebf8283 2019-07-24 stsp err = unlockerr;
6902 0ebf8283 2019-07-24 stsp return err;
6903 0ebf8283 2019-07-24 stsp }
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp const struct got_error *
6906 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6907 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6908 0ebf8283 2019-07-24 stsp {
6909 0ebf8283 2019-07-24 stsp const struct got_error *err;
6910 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6911 0ebf8283 2019-07-24 stsp
6912 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6913 0ebf8283 2019-07-24 stsp if (err)
6914 0ebf8283 2019-07-24 stsp return err;
6915 0ebf8283 2019-07-24 stsp
6916 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6917 0ebf8283 2019-07-24 stsp if (err)
6918 0ebf8283 2019-07-24 stsp goto done;
6919 0ebf8283 2019-07-24 stsp
6920 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6921 0ebf8283 2019-07-24 stsp done:
6922 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6923 2822a352 2019-10-15 stsp return err;
6924 2822a352 2019-10-15 stsp }
6925 2822a352 2019-10-15 stsp
6926 2822a352 2019-10-15 stsp const struct got_error *
6927 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6928 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6929 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6930 2822a352 2019-10-15 stsp struct got_repository *repo)
6931 2822a352 2019-10-15 stsp {
6932 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6933 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6934 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6935 2822a352 2019-10-15 stsp
6936 2822a352 2019-10-15 stsp *fileindex = NULL;
6937 2822a352 2019-10-15 stsp *branch_ref = NULL;
6938 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6939 2822a352 2019-10-15 stsp
6940 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6941 2822a352 2019-10-15 stsp if (err)
6942 2822a352 2019-10-15 stsp return err;
6943 2822a352 2019-10-15 stsp
6944 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6945 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6946 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6947 2822a352 2019-10-15 stsp "update -b or different branch name required");
6948 2822a352 2019-10-15 stsp goto done;
6949 2822a352 2019-10-15 stsp }
6950 2822a352 2019-10-15 stsp
6951 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6952 2822a352 2019-10-15 stsp if (err)
6953 2822a352 2019-10-15 stsp goto done;
6954 2822a352 2019-10-15 stsp
6955 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6956 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6957 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6958 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6959 2822a352 2019-10-15 stsp &ok_arg);
6960 2822a352 2019-10-15 stsp if (err)
6961 2822a352 2019-10-15 stsp goto done;
6962 2822a352 2019-10-15 stsp
6963 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6964 2822a352 2019-10-15 stsp if (err)
6965 2822a352 2019-10-15 stsp goto done;
6966 2822a352 2019-10-15 stsp
6967 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6968 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6969 2822a352 2019-10-15 stsp done:
6970 2822a352 2019-10-15 stsp if (err) {
6971 2822a352 2019-10-15 stsp if (*branch_ref) {
6972 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6973 2822a352 2019-10-15 stsp *branch_ref = NULL;
6974 2822a352 2019-10-15 stsp }
6975 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6976 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6977 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6978 2822a352 2019-10-15 stsp }
6979 2822a352 2019-10-15 stsp if (*fileindex) {
6980 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6981 2822a352 2019-10-15 stsp *fileindex = NULL;
6982 2822a352 2019-10-15 stsp }
6983 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6984 2822a352 2019-10-15 stsp }
6985 0cb83759 2019-08-03 stsp return err;
6986 0cb83759 2019-08-03 stsp }
6987 0cb83759 2019-08-03 stsp
6988 2822a352 2019-10-15 stsp const struct got_error *
6989 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6990 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6991 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6992 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6993 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6994 2822a352 2019-10-15 stsp {
6995 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6996 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6997 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6998 2822a352 2019-10-15 stsp
6999 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
7000 2822a352 2019-10-15 stsp if (err)
7001 2822a352 2019-10-15 stsp goto done;
7002 2822a352 2019-10-15 stsp
7003 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
7004 2822a352 2019-10-15 stsp if (err)
7005 2822a352 2019-10-15 stsp goto done;
7006 2822a352 2019-10-15 stsp
7007 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
7008 2822a352 2019-10-15 stsp worktree->path_prefix);
7009 2822a352 2019-10-15 stsp if (err)
7010 2822a352 2019-10-15 stsp goto done;
7011 2822a352 2019-10-15 stsp
7012 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7013 2822a352 2019-10-15 stsp if (err)
7014 2822a352 2019-10-15 stsp goto done;
7015 2822a352 2019-10-15 stsp
7016 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7017 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
7018 2822a352 2019-10-15 stsp if (err)
7019 2822a352 2019-10-15 stsp goto sync;
7020 2822a352 2019-10-15 stsp
7021 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
7022 2822a352 2019-10-15 stsp if (err)
7023 2822a352 2019-10-15 stsp goto sync;
7024 2822a352 2019-10-15 stsp
7025 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
7026 2822a352 2019-10-15 stsp sync:
7027 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7028 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
7029 2822a352 2019-10-15 stsp err = sync_err;
7030 2822a352 2019-10-15 stsp
7031 2822a352 2019-10-15 stsp done:
7032 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
7033 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7034 2822a352 2019-10-15 stsp err = unlockerr;
7035 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7036 2822a352 2019-10-15 stsp
7037 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
7038 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7039 2822a352 2019-10-15 stsp err = unlockerr;
7040 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7041 2822a352 2019-10-15 stsp
7042 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
7043 2822a352 2019-10-15 stsp free(fileindex_path);
7044 2822a352 2019-10-15 stsp free(tree_id);
7045 2822a352 2019-10-15 stsp
7046 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7047 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
7048 2822a352 2019-10-15 stsp err = unlockerr;
7049 2822a352 2019-10-15 stsp return err;
7050 2822a352 2019-10-15 stsp }
7051 2822a352 2019-10-15 stsp
7052 2822a352 2019-10-15 stsp const struct got_error *
7053 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
7054 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
7055 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7056 2822a352 2019-10-15 stsp {
7057 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
7058 8b692cd0 2019-10-21 stsp
7059 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
7060 8b692cd0 2019-10-21 stsp
7061 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
7062 8b692cd0 2019-10-21 stsp
7063 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
7064 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7065 8b692cd0 2019-10-21 stsp err = unlockerr;
7066 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
7067 8b692cd0 2019-10-21 stsp
7068 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
7069 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
7070 8b692cd0 2019-10-21 stsp err = unlockerr;
7071 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
7072 8b692cd0 2019-10-21 stsp
7073 8b692cd0 2019-10-21 stsp return err;
7074 2822a352 2019-10-15 stsp }
7075 2822a352 2019-10-15 stsp
7076 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
7077 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
7078 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7079 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7080 2db2652d 2019-08-07 stsp struct got_repository *repo;
7081 2db2652d 2019-08-07 stsp int have_changes;
7082 2db2652d 2019-08-07 stsp };
7083 2db2652d 2019-08-07 stsp
7084 2db2652d 2019-08-07 stsp const struct got_error *
7085 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
7086 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7087 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7088 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7089 735ef5ac 2019-08-03 stsp {
7090 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
7091 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
7092 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
7093 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
7094 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
7095 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
7096 8b13ce36 2019-08-08 stsp
7097 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
7098 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
7099 8b13ce36 2019-08-08 stsp return NULL;
7100 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
7101 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
7102 735ef5ac 2019-08-03 stsp
7103 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7104 735ef5ac 2019-08-03 stsp if (ie == NULL)
7105 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7106 735ef5ac 2019-08-03 stsp
7107 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7108 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7109 735ef5ac 2019-08-03 stsp relpath) == -1)
7110 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
7111 735ef5ac 2019-08-03 stsp
7112 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
7113 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
7114 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7115 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
7116 735ef5ac 2019-08-03 stsp }
7117 735ef5ac 2019-08-03 stsp
7118 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
7119 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7120 3aa5969e 2019-08-06 stsp goto done;
7121 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
7122 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
7123 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
7124 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7125 735ef5ac 2019-08-03 stsp goto done;
7126 3aa5969e 2019-08-06 stsp }
7127 735ef5ac 2019-08-03 stsp
7128 2db2652d 2019-08-07 stsp a->have_changes = 1;
7129 2db2652d 2019-08-07 stsp
7130 735ef5ac 2019-08-03 stsp p = in_repo_path;
7131 735ef5ac 2019-08-03 stsp while (p[0] == '/')
7132 735ef5ac 2019-08-03 stsp p++;
7133 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
7134 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
7135 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
7136 735ef5ac 2019-08-03 stsp done:
7137 735ef5ac 2019-08-03 stsp free(in_repo_path);
7138 dc424a06 2019-08-07 stsp return err;
7139 dc424a06 2019-08-07 stsp }
7140 dc424a06 2019-08-07 stsp
7141 2db2652d 2019-08-07 stsp struct stage_path_arg {
7142 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
7143 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
7144 2db2652d 2019-08-07 stsp struct got_repository *repo;
7145 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
7146 2db2652d 2019-08-07 stsp void *status_arg;
7147 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
7148 2db2652d 2019-08-07 stsp void *patch_arg;
7149 7b5dc508 2019-10-28 stsp int staged_something;
7150 35213c7c 2020-07-23 stsp int allow_bad_symlinks;
7151 2db2652d 2019-08-07 stsp };
7152 2db2652d 2019-08-07 stsp
7153 2db2652d 2019-08-07 stsp static const struct got_error *
7154 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
7155 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
7156 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7157 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7158 0cb83759 2019-08-03 stsp {
7159 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
7160 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
7161 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
7162 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
7163 0cb83759 2019-08-03 stsp uint32_t stage;
7164 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
7165 0aeb8099 2020-07-23 stsp struct stat sb;
7166 8b13ce36 2019-08-08 stsp
7167 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
7168 8b13ce36 2019-08-08 stsp return NULL;
7169 0cb83759 2019-08-03 stsp
7170 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7171 d3e7c587 2019-08-03 stsp if (ie == NULL)
7172 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7173 0cb83759 2019-08-03 stsp
7174 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7175 2db2652d 2019-08-07 stsp relpath)== -1)
7176 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
7177 0cb83759 2019-08-03 stsp
7178 0cb83759 2019-08-03 stsp switch (status) {
7179 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
7180 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
7181 0aeb8099 2020-07-23 stsp /* XXX could sb.st_mode be passed in by our caller? */
7182 0aeb8099 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
7183 0aeb8099 2020-07-23 stsp err = got_error_from_errno2("lstat", ondisk_path);
7184 0aeb8099 2020-07-23 stsp break;
7185 0aeb8099 2020-07-23 stsp }
7186 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7187 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
7188 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7189 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7190 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
7191 dc424a06 2019-08-07 stsp if (err)
7192 dc424a06 2019-08-07 stsp break;
7193 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
7194 dc424a06 2019-08-07 stsp break;
7195 dc424a06 2019-08-07 stsp } else {
7196 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
7197 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
7198 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
7199 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
7200 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
7201 dc424a06 2019-08-07 stsp break;
7202 dc424a06 2019-08-07 stsp }
7203 dc424a06 2019-08-07 stsp }
7204 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
7205 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
7206 0cb83759 2019-08-03 stsp if (err)
7207 dc424a06 2019-08-07 stsp break;
7208 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7209 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
7210 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7211 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
7212 0cb83759 2019-08-03 stsp else
7213 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
7214 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7215 0aeb8099 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
7216 35213c7c 2020-07-23 stsp int is_bad_symlink = 0;
7217 35213c7c 2020-07-23 stsp if (!a->allow_bad_symlinks) {
7218 35213c7c 2020-07-23 stsp char target_path[PATH_MAX];
7219 35213c7c 2020-07-23 stsp ssize_t target_len;
7220 35213c7c 2020-07-23 stsp target_len = readlink(ondisk_path, target_path,
7221 35213c7c 2020-07-23 stsp sizeof(target_path));
7222 35213c7c 2020-07-23 stsp if (target_len == -1) {
7223 35213c7c 2020-07-23 stsp err = got_error_from_errno2("readlink",
7224 35213c7c 2020-07-23 stsp ondisk_path);
7225 35213c7c 2020-07-23 stsp break;
7226 35213c7c 2020-07-23 stsp }
7227 35213c7c 2020-07-23 stsp err = is_bad_symlink_target(&is_bad_symlink,
7228 35213c7c 2020-07-23 stsp target_path, target_len, ondisk_path,
7229 35213c7c 2020-07-23 stsp a->worktree->root_path);
7230 35213c7c 2020-07-23 stsp if (err)
7231 35213c7c 2020-07-23 stsp break;
7232 35213c7c 2020-07-23 stsp if (is_bad_symlink) {
7233 35213c7c 2020-07-23 stsp err = got_error_path(ondisk_path,
7234 35213c7c 2020-07-23 stsp GOT_ERR_BAD_SYMLINK);
7235 35213c7c 2020-07-23 stsp break;
7236 35213c7c 2020-07-23 stsp }
7237 35213c7c 2020-07-23 stsp }
7238 35213c7c 2020-07-23 stsp if (is_bad_symlink)
7239 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7240 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_BAD_SYMLINK);
7241 35213c7c 2020-07-23 stsp else
7242 35213c7c 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7243 35213c7c 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK);
7244 0aeb8099 2020-07-23 stsp } else {
7245 0aeb8099 2020-07-23 stsp got_fileindex_entry_staged_filetype_set(ie,
7246 0aeb8099 2020-07-23 stsp GOT_FILEIDX_MODE_REGULAR_FILE);
7247 0aeb8099 2020-07-23 stsp }
7248 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7249 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7250 dc424a06 2019-08-07 stsp break;
7251 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7252 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
7253 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
7254 0cb83759 2019-08-03 stsp break;
7255 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
7256 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
7257 d3e7c587 2019-08-03 stsp break;
7258 2db2652d 2019-08-07 stsp if (a->patch_cb) {
7259 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
7260 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
7261 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
7262 dc424a06 2019-08-07 stsp if (err)
7263 dc424a06 2019-08-07 stsp break;
7264 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7265 88f33a19 2019-08-08 stsp break;
7266 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7267 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7268 dc424a06 2019-08-07 stsp break;
7269 88f33a19 2019-08-08 stsp }
7270 dc424a06 2019-08-07 stsp }
7271 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
7272 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
7273 7b5dc508 2019-10-28 stsp a->staged_something = 1;
7274 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
7275 dc424a06 2019-08-07 stsp break;
7276 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7277 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7278 12463d8b 2019-12-13 stsp de_name);
7279 0cb83759 2019-08-03 stsp break;
7280 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
7281 d3e7c587 2019-08-03 stsp break;
7282 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
7283 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7284 ebf48fd5 2019-08-03 stsp break;
7285 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
7286 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
7287 2a06fe5f 2019-08-24 stsp break;
7288 0cb83759 2019-08-03 stsp default:
7289 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7290 537ac44b 2019-08-03 stsp break;
7291 0cb83759 2019-08-03 stsp }
7292 dc424a06 2019-08-07 stsp
7293 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
7294 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
7295 dc424a06 2019-08-07 stsp free(path_content);
7296 2db2652d 2019-08-07 stsp free(ondisk_path);
7297 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
7298 0ebf8283 2019-07-24 stsp return err;
7299 0ebf8283 2019-07-24 stsp }
7300 0cb83759 2019-08-03 stsp
7301 0cb83759 2019-08-03 stsp const struct got_error *
7302 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
7303 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
7304 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
7305 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7306 35213c7c 2020-07-23 stsp int allow_bad_symlinks, struct got_repository *repo)
7307 0cb83759 2019-08-03 stsp {
7308 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7309 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
7310 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7311 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
7312 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
7313 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
7314 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
7315 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
7316 0cb83759 2019-08-03 stsp
7317 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7318 0cb83759 2019-08-03 stsp if (err)
7319 0cb83759 2019-08-03 stsp return err;
7320 0cb83759 2019-08-03 stsp
7321 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
7322 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
7323 735ef5ac 2019-08-03 stsp if (err)
7324 735ef5ac 2019-08-03 stsp goto done;
7325 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
7326 735ef5ac 2019-08-03 stsp if (err)
7327 735ef5ac 2019-08-03 stsp goto done;
7328 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7329 0cb83759 2019-08-03 stsp if (err)
7330 0cb83759 2019-08-03 stsp goto done;
7331 0cb83759 2019-08-03 stsp
7332 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
7333 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
7334 2db2652d 2019-08-07 stsp oka.worktree = worktree;
7335 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
7336 2db2652d 2019-08-07 stsp oka.repo = repo;
7337 2db2652d 2019-08-07 stsp oka.have_changes = 0;
7338 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7339 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7340 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
7341 735ef5ac 2019-08-03 stsp if (err)
7342 735ef5ac 2019-08-03 stsp goto done;
7343 735ef5ac 2019-08-03 stsp }
7344 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
7345 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7346 2db2652d 2019-08-07 stsp goto done;
7347 2db2652d 2019-08-07 stsp }
7348 735ef5ac 2019-08-03 stsp
7349 2db2652d 2019-08-07 stsp spa.worktree = worktree;
7350 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
7351 2db2652d 2019-08-07 stsp spa.repo = repo;
7352 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
7353 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
7354 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
7355 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
7356 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
7357 35213c7c 2020-07-23 stsp spa.allow_bad_symlinks = allow_bad_symlinks;
7358 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7359 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7360 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
7361 42005733 2019-08-03 stsp if (err)
7362 2db2652d 2019-08-07 stsp goto done;
7363 7b5dc508 2019-10-28 stsp }
7364 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
7365 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7366 7b5dc508 2019-10-28 stsp goto done;
7367 0cb83759 2019-08-03 stsp }
7368 0cb83759 2019-08-03 stsp
7369 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7370 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
7371 0cb83759 2019-08-03 stsp err = sync_err;
7372 0cb83759 2019-08-03 stsp done:
7373 735ef5ac 2019-08-03 stsp if (head_ref)
7374 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
7375 735ef5ac 2019-08-03 stsp free(head_commit_id);
7376 0cb83759 2019-08-03 stsp free(fileindex_path);
7377 0cb83759 2019-08-03 stsp if (fileindex)
7378 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
7379 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7380 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
7381 0cb83759 2019-08-03 stsp err = unlockerr;
7382 0cb83759 2019-08-03 stsp return err;
7383 0cb83759 2019-08-03 stsp }
7384 ad493afc 2019-08-03 stsp
7385 ad493afc 2019-08-03 stsp struct unstage_path_arg {
7386 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
7387 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
7388 ad493afc 2019-08-03 stsp struct got_repository *repo;
7389 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
7390 ad493afc 2019-08-03 stsp void *progress_arg;
7391 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
7392 2e1f37b0 2019-08-08 stsp void *patch_arg;
7393 ad493afc 2019-08-03 stsp };
7394 2e1f37b0 2019-08-08 stsp
7395 2e1f37b0 2019-08-08 stsp static const struct got_error *
7396 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
7397 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
7398 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
7399 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
7400 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
7401 2e1f37b0 2019-08-08 stsp {
7402 2e1f37b0 2019-08-08 stsp const struct got_error *err;
7403 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
7404 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7405 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7406 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
7407 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
7408 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
7409 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
7410 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
7411 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
7412 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
7413 2e1f37b0 2019-08-08 stsp
7414 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7415 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7416 2e1f37b0 2019-08-08 stsp
7417 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
7418 2e1f37b0 2019-08-08 stsp if (err)
7419 2e1f37b0 2019-08-08 stsp return err;
7420 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7421 2e1f37b0 2019-08-08 stsp if (err)
7422 2e1f37b0 2019-08-08 stsp goto done;
7423 2e1f37b0 2019-08-08 stsp
7424 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7425 2e1f37b0 2019-08-08 stsp if (err)
7426 2e1f37b0 2019-08-08 stsp goto done;
7427 2e1f37b0 2019-08-08 stsp
7428 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7429 2e1f37b0 2019-08-08 stsp if (err)
7430 2e1f37b0 2019-08-08 stsp goto done;
7431 2e1f37b0 2019-08-08 stsp
7432 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7433 2e1f37b0 2019-08-08 stsp if (err)
7434 2e1f37b0 2019-08-08 stsp goto done;
7435 2e1f37b0 2019-08-08 stsp
7436 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7437 2e1f37b0 2019-08-08 stsp if (err)
7438 2e1f37b0 2019-08-08 stsp goto done;
7439 ad493afc 2019-08-03 stsp
7440 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7441 2e1f37b0 2019-08-08 stsp if (err)
7442 2e1f37b0 2019-08-08 stsp goto done;
7443 2e1f37b0 2019-08-08 stsp
7444 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
7445 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
7446 2e1f37b0 2019-08-08 stsp goto done;
7447 2e1f37b0 2019-08-08 stsp }
7448 2e1f37b0 2019-08-08 stsp
7449 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
7450 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
7451 2e1f37b0 2019-08-08 stsp goto done;
7452 2e1f37b0 2019-08-08 stsp }
7453 2e1f37b0 2019-08-08 stsp
7454 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
7455 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7456 2e1f37b0 2019-08-08 stsp if (err)
7457 2e1f37b0 2019-08-08 stsp goto done;
7458 2e1f37b0 2019-08-08 stsp
7459 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
7460 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
7461 2e1f37b0 2019-08-08 stsp if (err)
7462 2e1f37b0 2019-08-08 stsp goto done;
7463 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
7464 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
7465 2e1f37b0 2019-08-08 stsp if (err)
7466 2e1f37b0 2019-08-08 stsp goto done;
7467 2e1f37b0 2019-08-08 stsp
7468 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
7469 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
7470 2e1f37b0 2019-08-08 stsp goto done;
7471 2e1f37b0 2019-08-08 stsp }
7472 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
7473 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
7474 2e1f37b0 2019-08-08 stsp goto done;
7475 2e1f37b0 2019-08-08 stsp }
7476 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7477 2e1f37b0 2019-08-08 stsp int choice;
7478 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
7479 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
7480 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
7481 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
7482 2e1f37b0 2019-08-08 stsp if (err)
7483 2e1f37b0 2019-08-08 stsp goto done;
7484 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
7485 2e1f37b0 2019-08-08 stsp have_content = 1;
7486 19e4b907 2019-08-08 stsp else
7487 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
7488 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
7489 2e1f37b0 2019-08-08 stsp break;
7490 2e1f37b0 2019-08-08 stsp }
7491 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
7492 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7493 f1e81a05 2019-08-10 stsp outfile, rejectfile);
7494 2e1f37b0 2019-08-08 stsp done:
7495 2e1f37b0 2019-08-08 stsp free(label1);
7496 2e1f37b0 2019-08-08 stsp if (blob)
7497 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
7498 2e1f37b0 2019-08-08 stsp if (staged_blob)
7499 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
7500 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
7501 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
7502 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
7503 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
7504 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
7505 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
7506 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7507 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
7508 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
7509 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
7510 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
7511 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
7512 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
7513 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
7514 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
7515 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7516 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
7517 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
7518 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
7519 2e1f37b0 2019-08-08 stsp }
7520 fda8017d 2020-07-23 stsp if (err || !have_content || !have_rejected_content) {
7521 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
7522 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
7523 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
7524 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
7525 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
7526 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
7527 2e1f37b0 2019-08-08 stsp }
7528 2e1f37b0 2019-08-08 stsp free(args);
7529 2e1f37b0 2019-08-08 stsp if (ds) {
7530 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
7531 2e1f37b0 2019-08-08 stsp free(ds);
7532 2e1f37b0 2019-08-08 stsp }
7533 2e1f37b0 2019-08-08 stsp if (changes)
7534 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
7535 2e1f37b0 2019-08-08 stsp free(path1);
7536 2e1f37b0 2019-08-08 stsp free(path2);
7537 fda8017d 2020-07-23 stsp return err;
7538 fda8017d 2020-07-23 stsp }
7539 fda8017d 2020-07-23 stsp
7540 fda8017d 2020-07-23 stsp static const struct got_error *
7541 fda8017d 2020-07-23 stsp unstage_hunks(struct got_object_id *staged_blob_id,
7542 fda8017d 2020-07-23 stsp struct got_blob_object *blob_base,
7543 fda8017d 2020-07-23 stsp struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7544 fda8017d 2020-07-23 stsp const char *ondisk_path, const char *label_orig,
7545 fda8017d 2020-07-23 stsp struct got_worktree *worktree, struct got_repository *repo,
7546 fda8017d 2020-07-23 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7547 fda8017d 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
7548 fda8017d 2020-07-23 stsp {
7549 fda8017d 2020-07-23 stsp const struct got_error *err = NULL;
7550 fda8017d 2020-07-23 stsp char *path_unstaged_content = NULL;
7551 fda8017d 2020-07-23 stsp char *path_new_staged_content = NULL;
7552 fda8017d 2020-07-23 stsp struct got_object_id *new_staged_blob_id = NULL;
7553 fda8017d 2020-07-23 stsp FILE *f = NULL;
7554 fda8017d 2020-07-23 stsp struct stat sb;
7555 fda8017d 2020-07-23 stsp
7556 fda8017d 2020-07-23 stsp err = create_unstaged_content(&path_unstaged_content,
7557 fda8017d 2020-07-23 stsp &path_new_staged_content, blob_id, staged_blob_id,
7558 fda8017d 2020-07-23 stsp ie->path, repo, patch_cb, patch_arg);
7559 fda8017d 2020-07-23 stsp if (err)
7560 fda8017d 2020-07-23 stsp return err;
7561 fda8017d 2020-07-23 stsp
7562 fda8017d 2020-07-23 stsp if (path_unstaged_content == NULL)
7563 fda8017d 2020-07-23 stsp return NULL;
7564 fda8017d 2020-07-23 stsp
7565 fda8017d 2020-07-23 stsp if (path_new_staged_content) {
7566 fda8017d 2020-07-23 stsp err = got_object_blob_create(&new_staged_blob_id,
7567 fda8017d 2020-07-23 stsp path_new_staged_content, repo);
7568 fda8017d 2020-07-23 stsp if (err)
7569 fda8017d 2020-07-23 stsp goto done;
7570 fda8017d 2020-07-23 stsp }
7571 fda8017d 2020-07-23 stsp
7572 fda8017d 2020-07-23 stsp f = fopen(path_unstaged_content, "r");
7573 fda8017d 2020-07-23 stsp if (f == NULL) {
7574 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fopen",
7575 fda8017d 2020-07-23 stsp path_unstaged_content);
7576 fda8017d 2020-07-23 stsp goto done;
7577 fda8017d 2020-07-23 stsp }
7578 fda8017d 2020-07-23 stsp if (fstat(fileno(f), &sb) == -1) {
7579 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fstat", path_unstaged_content);
7580 fda8017d 2020-07-23 stsp goto done;
7581 fda8017d 2020-07-23 stsp }
7582 fda8017d 2020-07-23 stsp if (got_fileindex_entry_staged_filetype_get(ie) ==
7583 fda8017d 2020-07-23 stsp GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7584 fda8017d 2020-07-23 stsp char link_target[PATH_MAX];
7585 fda8017d 2020-07-23 stsp size_t r;
7586 fda8017d 2020-07-23 stsp r = fread(link_target, 1, sizeof(link_target), f);
7587 fda8017d 2020-07-23 stsp if (r == 0 && ferror(f)) {
7588 fda8017d 2020-07-23 stsp err = got_error_from_errno("fread");
7589 fda8017d 2020-07-23 stsp goto done;
7590 fda8017d 2020-07-23 stsp }
7591 fda8017d 2020-07-23 stsp if (r >= sizeof(link_target)) { /* should not happen */
7592 fda8017d 2020-07-23 stsp err = got_error(GOT_ERR_NO_SPACE);
7593 fda8017d 2020-07-23 stsp goto done;
7594 fda8017d 2020-07-23 stsp }
7595 fda8017d 2020-07-23 stsp link_target[r] = '\0';
7596 fda8017d 2020-07-23 stsp err = merge_symlink(worktree, blob_base,
7597 fda8017d 2020-07-23 stsp ondisk_path, ie->path, label_orig, link_target,
7598 fda8017d 2020-07-23 stsp worktree->base_commit_id, repo, progress_cb,
7599 fda8017d 2020-07-23 stsp progress_arg);
7600 fda8017d 2020-07-23 stsp } else {
7601 fda8017d 2020-07-23 stsp int local_changes_subsumed;
7602 fda8017d 2020-07-23 stsp err = merge_file(&local_changes_subsumed, worktree,
7603 fda8017d 2020-07-23 stsp blob_base, ondisk_path, ie->path,
7604 fda8017d 2020-07-23 stsp got_fileindex_perms_to_st(ie),
7605 fda8017d 2020-07-23 stsp path_unstaged_content, label_orig, "unstaged",
7606 fda8017d 2020-07-23 stsp repo, progress_cb, progress_arg);
7607 fda8017d 2020-07-23 stsp }
7608 fda8017d 2020-07-23 stsp if (err)
7609 fda8017d 2020-07-23 stsp goto done;
7610 fda8017d 2020-07-23 stsp
7611 fda8017d 2020-07-23 stsp if (new_staged_blob_id) {
7612 fda8017d 2020-07-23 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7613 fda8017d 2020-07-23 stsp SHA1_DIGEST_LENGTH);
7614 2ac8aa02 2020-11-09 stsp } else {
7615 fda8017d 2020-07-23 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7616 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7617 2ac8aa02 2020-11-09 stsp }
7618 fda8017d 2020-07-23 stsp done:
7619 fda8017d 2020-07-23 stsp free(new_staged_blob_id);
7620 fda8017d 2020-07-23 stsp if (path_unstaged_content &&
7621 fda8017d 2020-07-23 stsp unlink(path_unstaged_content) == -1 && err == NULL)
7622 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
7623 fda8017d 2020-07-23 stsp if (path_new_staged_content &&
7624 fda8017d 2020-07-23 stsp unlink(path_new_staged_content) == -1 && err == NULL)
7625 fda8017d 2020-07-23 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
7626 fda8017d 2020-07-23 stsp if (f && fclose(f) != 0 && err == NULL)
7627 fda8017d 2020-07-23 stsp err = got_error_from_errno2("fclose", path_unstaged_content);
7628 fda8017d 2020-07-23 stsp free(path_unstaged_content);
7629 fda8017d 2020-07-23 stsp free(path_new_staged_content);
7630 2e1f37b0 2019-08-08 stsp return err;
7631 2e1f37b0 2019-08-08 stsp }
7632 2e1f37b0 2019-08-08 stsp
7633 ad493afc 2019-08-03 stsp static const struct got_error *
7634 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
7635 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
7636 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7637 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7638 ad493afc 2019-08-03 stsp {
7639 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
7640 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
7641 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
7642 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7643 fda8017d 2020-07-23 stsp char *ondisk_path = NULL;
7644 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
7645 ad493afc 2019-08-03 stsp int local_changes_subsumed;
7646 9bc94a15 2019-08-03 stsp struct stat sb;
7647 ad493afc 2019-08-03 stsp
7648 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
7649 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
7650 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
7651 2e1f37b0 2019-08-08 stsp return NULL;
7652 2e1f37b0 2019-08-08 stsp
7653 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7654 ad493afc 2019-08-03 stsp if (ie == NULL)
7655 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7656 9bc94a15 2019-08-03 stsp
7657 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7658 9bc94a15 2019-08-03 stsp == -1)
7659 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
7660 ad493afc 2019-08-03 stsp
7661 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
7662 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
7663 f69721c3 2019-10-21 stsp if (err)
7664 f69721c3 2019-10-21 stsp goto done;
7665 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7666 f69721c3 2019-10-21 stsp id_str) == -1) {
7667 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
7668 f69721c3 2019-10-21 stsp goto done;
7669 f69721c3 2019-10-21 stsp }
7670 f69721c3 2019-10-21 stsp
7671 ad493afc 2019-08-03 stsp switch (staged_status) {
7672 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
7673 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
7674 ad493afc 2019-08-03 stsp blob_id, 8192);
7675 ad493afc 2019-08-03 stsp if (err)
7676 ad493afc 2019-08-03 stsp break;
7677 ad493afc 2019-08-03 stsp /* fall through */
7678 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
7679 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7680 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
7681 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7682 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7683 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7684 2e1f37b0 2019-08-08 stsp if (err)
7685 2e1f37b0 2019-08-08 stsp break;
7686 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
7687 2e1f37b0 2019-08-08 stsp break;
7688 2e1f37b0 2019-08-08 stsp } else {
7689 fda8017d 2020-07-23 stsp err = unstage_hunks(staged_blob_id,
7690 fda8017d 2020-07-23 stsp blob_base, blob_id, ie, ondisk_path,
7691 fda8017d 2020-07-23 stsp label_orig, a->worktree, a->repo,
7692 fda8017d 2020-07-23 stsp a->patch_cb, a->patch_arg,
7693 fda8017d 2020-07-23 stsp a->progress_cb, a->progress_arg);
7694 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
7695 2e1f37b0 2019-08-08 stsp }
7696 2e1f37b0 2019-08-08 stsp }
7697 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
7698 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
7699 ad493afc 2019-08-03 stsp if (err)
7700 ad493afc 2019-08-03 stsp break;
7701 ea7786be 2020-07-23 stsp switch (got_fileindex_entry_staged_filetype_get(ie)) {
7702 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_BAD_SYMLINK:
7703 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_REGULAR_FILE:
7704 ea7786be 2020-07-23 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
7705 ea7786be 2020-07-23 stsp blob_base, ondisk_path, relpath,
7706 ea7786be 2020-07-23 stsp got_fileindex_perms_to_st(ie), label_orig,
7707 ea7786be 2020-07-23 stsp blob_staged, commit_id ? commit_id :
7708 ea7786be 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7709 ea7786be 2020-07-23 stsp a->progress_cb, a->progress_arg);
7710 ea7786be 2020-07-23 stsp break;
7711 ea7786be 2020-07-23 stsp case GOT_FILEIDX_MODE_SYMLINK:
7712 dfe9fba0 2020-07-23 stsp if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7713 36bf999c 2020-07-23 stsp char *staged_target;
7714 36bf999c 2020-07-23 stsp err = got_object_blob_read_to_str(
7715 36bf999c 2020-07-23 stsp &staged_target, blob_staged);
7716 36bf999c 2020-07-23 stsp if (err)
7717 36bf999c 2020-07-23 stsp goto done;
7718 dfe9fba0 2020-07-23 stsp err = merge_symlink(a->worktree, blob_base,
7719 dfe9fba0 2020-07-23 stsp ondisk_path, relpath, label_orig,
7720 36bf999c 2020-07-23 stsp staged_target, commit_id ? commit_id :
7721 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id,
7722 dfe9fba0 2020-07-23 stsp a->repo, a->progress_cb, a->progress_arg);
7723 36bf999c 2020-07-23 stsp free(staged_target);
7724 dfe9fba0 2020-07-23 stsp } else {
7725 dfe9fba0 2020-07-23 stsp err = merge_blob(&local_changes_subsumed,
7726 dfe9fba0 2020-07-23 stsp a->worktree, blob_base, ondisk_path,
7727 dfe9fba0 2020-07-23 stsp relpath, got_fileindex_perms_to_st(ie),
7728 dfe9fba0 2020-07-23 stsp label_orig, blob_staged,
7729 dfe9fba0 2020-07-23 stsp commit_id ? commit_id :
7730 dfe9fba0 2020-07-23 stsp a->worktree->base_commit_id, a->repo,
7731 dfe9fba0 2020-07-23 stsp a->progress_cb, a->progress_arg);
7732 dfe9fba0 2020-07-23 stsp }
7733 ea7786be 2020-07-23 stsp break;
7734 ea7786be 2020-07-23 stsp default:
7735 ea7786be 2020-07-23 stsp err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7736 ea7786be 2020-07-23 stsp break;
7737 ea7786be 2020-07-23 stsp }
7738 2ac8aa02 2020-11-09 stsp if (err == NULL) {
7739 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
7740 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
7741 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7742 2ac8aa02 2020-11-09 stsp }
7743 ad493afc 2019-08-03 stsp break;
7744 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
7745 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
7746 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
7747 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
7748 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
7749 2e1f37b0 2019-08-08 stsp if (err)
7750 2e1f37b0 2019-08-08 stsp break;
7751 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
7752 2e1f37b0 2019-08-08 stsp break;
7753 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
7754 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
7755 2e1f37b0 2019-08-08 stsp break;
7756 2e1f37b0 2019-08-08 stsp }
7757 2e1f37b0 2019-08-08 stsp }
7758 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7759 2ac8aa02 2020-11-09 stsp got_fileindex_entry_staged_filetype_set(ie, 0);
7760 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
7761 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
7762 9bc94a15 2019-08-03 stsp if (err)
7763 9bc94a15 2019-08-03 stsp break;
7764 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
7765 ad493afc 2019-08-03 stsp break;
7766 ad493afc 2019-08-03 stsp }
7767 f69721c3 2019-10-21 stsp done:
7768 ad493afc 2019-08-03 stsp free(ondisk_path);
7769 ad493afc 2019-08-03 stsp if (blob_base)
7770 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7771 ad493afc 2019-08-03 stsp if (blob_staged)
7772 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7773 f69721c3 2019-10-21 stsp free(id_str);
7774 f69721c3 2019-10-21 stsp free(label_orig);
7775 ad493afc 2019-08-03 stsp return err;
7776 ad493afc 2019-08-03 stsp }
7777 ad493afc 2019-08-03 stsp
7778 ad493afc 2019-08-03 stsp const struct got_error *
7779 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7780 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7781 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7782 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7783 ad493afc 2019-08-03 stsp struct got_repository *repo)
7784 ad493afc 2019-08-03 stsp {
7785 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7786 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7787 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7788 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7789 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7790 ad493afc 2019-08-03 stsp
7791 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7792 ad493afc 2019-08-03 stsp if (err)
7793 ad493afc 2019-08-03 stsp return err;
7794 ad493afc 2019-08-03 stsp
7795 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7796 ad493afc 2019-08-03 stsp if (err)
7797 ad493afc 2019-08-03 stsp goto done;
7798 ad493afc 2019-08-03 stsp
7799 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7800 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7801 ad493afc 2019-08-03 stsp upa.repo = repo;
7802 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7803 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7804 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7805 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7806 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7807 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7808 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7809 ad493afc 2019-08-03 stsp if (err)
7810 ad493afc 2019-08-03 stsp goto done;
7811 ad493afc 2019-08-03 stsp }
7812 ad493afc 2019-08-03 stsp
7813 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7814 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7815 ad493afc 2019-08-03 stsp err = sync_err;
7816 ad493afc 2019-08-03 stsp done:
7817 ad493afc 2019-08-03 stsp free(fileindex_path);
7818 ad493afc 2019-08-03 stsp if (fileindex)
7819 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7820 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7821 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7822 ad493afc 2019-08-03 stsp err = unlockerr;
7823 ad493afc 2019-08-03 stsp return err;
7824 ad493afc 2019-08-03 stsp }
7825 b2118c49 2020-07-28 stsp
7826 b2118c49 2020-07-28 stsp struct report_file_info_arg {
7827 b2118c49 2020-07-28 stsp struct got_worktree *worktree;
7828 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb;
7829 b2118c49 2020-07-28 stsp void *info_arg;
7830 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths;
7831 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb;
7832 b2118c49 2020-07-28 stsp void *cancel_arg;
7833 b2118c49 2020-07-28 stsp };
7834 b2118c49 2020-07-28 stsp
7835 b2118c49 2020-07-28 stsp static const struct got_error *
7836 b2118c49 2020-07-28 stsp report_file_info(void *arg, struct got_fileindex_entry *ie)
7837 b2118c49 2020-07-28 stsp {
7838 b2118c49 2020-07-28 stsp struct report_file_info_arg *a = arg;
7839 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
7840 b2118c49 2020-07-28 stsp struct got_object_id blob_id, staged_blob_id, commit_id;
7841 b2118c49 2020-07-28 stsp struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7842 b2118c49 2020-07-28 stsp struct got_object_id *commit_idp = NULL;
7843 b2118c49 2020-07-28 stsp int stage;
7844 b2118c49 2020-07-28 stsp
7845 b2118c49 2020-07-28 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7846 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_CANCELLED);
7847 b2118c49 2020-07-28 stsp
7848 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, a->paths, entry) {
7849 b2118c49 2020-07-28 stsp if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7850 b2118c49 2020-07-28 stsp got_path_is_child(ie->path, pe->path, pe->path_len))
7851 b2118c49 2020-07-28 stsp break;
7852 b2118c49 2020-07-28 stsp }
7853 b2118c49 2020-07-28 stsp if (pe == NULL) /* not found */
7854 b2118c49 2020-07-28 stsp return NULL;
7855 b2118c49 2020-07-28 stsp
7856 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_blob(ie)) {
7857 b2118c49 2020-07-28 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7858 b2118c49 2020-07-28 stsp blob_idp = &blob_id;
7859 b2118c49 2020-07-28 stsp }
7860 b2118c49 2020-07-28 stsp stage = got_fileindex_entry_stage_get(ie);
7861 b2118c49 2020-07-28 stsp if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7862 b2118c49 2020-07-28 stsp stage == GOT_FILEIDX_STAGE_ADD) {
7863 b2118c49 2020-07-28 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7864 b2118c49 2020-07-28 stsp SHA1_DIGEST_LENGTH);
7865 b2118c49 2020-07-28 stsp staged_blob_idp = &staged_blob_id;
7866 b2118c49 2020-07-28 stsp }
7867 b2118c49 2020-07-28 stsp
7868 b2118c49 2020-07-28 stsp if (got_fileindex_entry_has_commit(ie)) {
7869 b2118c49 2020-07-28 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7870 b2118c49 2020-07-28 stsp commit_idp = &commit_id;
7871 b2118c49 2020-07-28 stsp }
7872 b2118c49 2020-07-28 stsp
7873 b2118c49 2020-07-28 stsp return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7874 b2118c49 2020-07-28 stsp (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7875 b2118c49 2020-07-28 stsp }
7876 b2118c49 2020-07-28 stsp
7877 b2118c49 2020-07-28 stsp const struct got_error *
7878 b2118c49 2020-07-28 stsp got_worktree_path_info(struct got_worktree *worktree,
7879 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths,
7880 b2118c49 2020-07-28 stsp got_worktree_path_info_cb info_cb, void *info_arg,
7881 b2118c49 2020-07-28 stsp got_cancel_cb cancel_cb, void *cancel_arg)
7882 b2118c49 2020-07-28 stsp
7883 b2118c49 2020-07-28 stsp {
7884 b2118c49 2020-07-28 stsp const struct got_error *err = NULL, *unlockerr;
7885 b2118c49 2020-07-28 stsp struct got_fileindex *fileindex = NULL;
7886 b2118c49 2020-07-28 stsp char *fileindex_path = NULL;
7887 b2118c49 2020-07-28 stsp struct report_file_info_arg arg;
7888 b2118c49 2020-07-28 stsp
7889 b2118c49 2020-07-28 stsp err = lock_worktree(worktree, LOCK_SH);
7890 b2118c49 2020-07-28 stsp if (err)
7891 b2118c49 2020-07-28 stsp return err;
7892 b2118c49 2020-07-28 stsp
7893 b2118c49 2020-07-28 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7894 b2118c49 2020-07-28 stsp if (err)
7895 b2118c49 2020-07-28 stsp goto done;
7896 b2118c49 2020-07-28 stsp
7897 b2118c49 2020-07-28 stsp arg.worktree = worktree;
7898 b2118c49 2020-07-28 stsp arg.info_cb = info_cb;
7899 b2118c49 2020-07-28 stsp arg.info_arg = info_arg;
7900 b2118c49 2020-07-28 stsp arg.paths = paths;
7901 b2118c49 2020-07-28 stsp arg.cancel_cb = cancel_cb;
7902 b2118c49 2020-07-28 stsp arg.cancel_arg = cancel_arg;
7903 b2118c49 2020-07-28 stsp err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7904 b2118c49 2020-07-28 stsp &arg);
7905 b2118c49 2020-07-28 stsp done:
7906 b2118c49 2020-07-28 stsp free(fileindex_path);
7907 b2118c49 2020-07-28 stsp if (fileindex)
7908 b2118c49 2020-07-28 stsp got_fileindex_free(fileindex);
7909 b2118c49 2020-07-28 stsp unlockerr = lock_worktree(worktree, LOCK_UN);
7910 b2118c49 2020-07-28 stsp if (unlockerr && err == NULL)
7911 b2118c49 2020-07-28 stsp err = unlockerr;
7912 b2118c49 2020-07-28 stsp return err;
7913 b2118c49 2020-07-28 stsp }