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 9d31a1d8 2018-03-11 stsp
59 9d31a1d8 2018-03-11 stsp #ifndef MIN
60 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 9d31a1d8 2018-03-11 stsp #endif
62 f69721c3 2019-10-21 stsp
63 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_MERGED "merged change"
64 f69721c3 2019-10-21 stsp #define GOT_MERGE_LABEL_BASE "3-way merge base"
65 86c3caaf 2018-03-09 stsp
66 99724ed4 2018-03-10 stsp static const struct got_error *
67 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
68 99724ed4 2018-03-10 stsp {
69 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
70 99724ed4 2018-03-10 stsp char *path;
71 99724ed4 2018-03-10 stsp
72 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
74 99724ed4 2018-03-10 stsp
75 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
76 99724ed4 2018-03-10 stsp free(path);
77 507dc3bb 2018-12-29 stsp return err;
78 507dc3bb 2018-12-29 stsp }
79 507dc3bb 2018-12-29 stsp
80 507dc3bb 2018-12-29 stsp static const struct got_error *
81 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
82 507dc3bb 2018-12-29 stsp {
83 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
84 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
85 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
86 507dc3bb 2018-12-29 stsp char *path = NULL;
87 507dc3bb 2018-12-29 stsp
88 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
90 507dc3bb 2018-12-29 stsp path = NULL;
91 507dc3bb 2018-12-29 stsp goto done;
92 507dc3bb 2018-12-29 stsp }
93 507dc3bb 2018-12-29 stsp
94 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
95 507dc3bb 2018-12-29 stsp if (err)
96 507dc3bb 2018-12-29 stsp goto done;
97 507dc3bb 2018-12-29 stsp
98 507dc3bb 2018-12-29 stsp if (content) {
99 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
100 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
102 507dc3bb 2018-12-29 stsp goto done;
103 507dc3bb 2018-12-29 stsp }
104 507dc3bb 2018-12-29 stsp }
105 507dc3bb 2018-12-29 stsp
106 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
107 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
108 2a57020b 2019-02-20 stsp unlink(tmppath);
109 507dc3bb 2018-12-29 stsp goto done;
110 507dc3bb 2018-12-29 stsp }
111 507dc3bb 2018-12-29 stsp
112 507dc3bb 2018-12-29 stsp done:
113 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
114 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
115 230a42bd 2019-05-11 jcs free(tmppath);
116 99724ed4 2018-03-10 stsp return err;
117 99724ed4 2018-03-10 stsp }
118 99724ed4 2018-03-10 stsp
119 09fe317a 2018-03-11 stsp static const struct got_error *
120 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
121 09fe317a 2018-03-11 stsp {
122 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
123 09fe317a 2018-03-11 stsp char *path;
124 09fe317a 2018-03-11 stsp int fd = -1;
125 09fe317a 2018-03-11 stsp ssize_t n;
126 09fe317a 2018-03-11 stsp struct stat sb;
127 09fe317a 2018-03-11 stsp
128 09fe317a 2018-03-11 stsp *content = NULL;
129 09fe317a 2018-03-11 stsp
130 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
132 09fe317a 2018-03-11 stsp path = NULL;
133 09fe317a 2018-03-11 stsp goto done;
134 09fe317a 2018-03-11 stsp }
135 09fe317a 2018-03-11 stsp
136 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
137 09fe317a 2018-03-11 stsp if (fd == -1) {
138 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
139 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
140 f02eaa22 2019-03-11 stsp else
141 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
142 ef99fdb1 2018-03-11 stsp goto done;
143 ef99fdb1 2018-03-11 stsp }
144 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
147 09fe317a 2018-03-11 stsp goto done;
148 09fe317a 2018-03-11 stsp }
149 09fe317a 2018-03-11 stsp
150 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
151 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
152 d10c9b58 2019-02-19 stsp goto done;
153 d10c9b58 2019-02-19 stsp }
154 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
155 09fe317a 2018-03-11 stsp if (*content == NULL) {
156 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
157 09fe317a 2018-03-11 stsp goto done;
158 09fe317a 2018-03-11 stsp }
159 09fe317a 2018-03-11 stsp
160 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
161 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
162 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
163 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
164 09fe317a 2018-03-11 stsp goto done;
165 09fe317a 2018-03-11 stsp }
166 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
167 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
168 09fe317a 2018-03-11 stsp goto done;
169 09fe317a 2018-03-11 stsp }
170 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
171 09fe317a 2018-03-11 stsp
172 09fe317a 2018-03-11 stsp done:
173 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
174 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
175 09fe317a 2018-03-11 stsp free(path);
176 09fe317a 2018-03-11 stsp if (err) {
177 09fe317a 2018-03-11 stsp free(*content);
178 09fe317a 2018-03-11 stsp *content = NULL;
179 09fe317a 2018-03-11 stsp }
180 09fe317a 2018-03-11 stsp return err;
181 09fe317a 2018-03-11 stsp }
182 09fe317a 2018-03-11 stsp
183 024e9686 2019-05-14 stsp static const struct got_error *
184 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
185 024e9686 2019-05-14 stsp {
186 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
187 024e9686 2019-05-14 stsp char *refstr = NULL;
188 024e9686 2019-05-14 stsp
189 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
190 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
191 024e9686 2019-05-14 stsp if (refstr == NULL)
192 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
193 024e9686 2019-05-14 stsp } else {
194 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
195 024e9686 2019-05-14 stsp if (refstr == NULL)
196 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
197 024e9686 2019-05-14 stsp }
198 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
199 024e9686 2019-05-14 stsp free(refstr);
200 024e9686 2019-05-14 stsp return err;
201 024e9686 2019-05-14 stsp }
202 024e9686 2019-05-14 stsp
203 86c3caaf 2018-03-09 stsp const struct got_error *
204 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
205 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
206 86c3caaf 2018-03-09 stsp {
207 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
208 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
209 ec22038e 2019-03-10 stsp uuid_t uuid;
210 ec22038e 2019-03-10 stsp uint32_t uuid_status;
211 65596e15 2018-12-24 stsp int obj_type;
212 7ac97322 2018-03-11 stsp char *path_got = NULL;
213 1451e70d 2018-03-10 stsp char *formatstr = NULL;
214 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
215 65596e15 2018-12-24 stsp char *basestr = NULL;
216 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
217 65596e15 2018-12-24 stsp
218 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
220 0c48fee2 2019-03-11 stsp goto done;
221 0c48fee2 2019-03-11 stsp }
222 0c48fee2 2019-03-11 stsp
223 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
224 65596e15 2018-12-24 stsp if (err)
225 65596e15 2018-12-24 stsp return err;
226 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
227 65596e15 2018-12-24 stsp if (err)
228 65596e15 2018-12-24 stsp return err;
229 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
231 86c3caaf 2018-03-09 stsp
232 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
233 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
234 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
235 0bb8a95e 2018-03-12 stsp }
236 577ec78f 2018-03-11 stsp
237 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
238 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
240 86c3caaf 2018-03-09 stsp goto done;
241 86c3caaf 2018-03-09 stsp }
242 86c3caaf 2018-03-09 stsp
243 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
244 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
246 86c3caaf 2018-03-09 stsp goto done;
247 86c3caaf 2018-03-09 stsp }
248 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
250 86c3caaf 2018-03-09 stsp goto done;
251 86c3caaf 2018-03-09 stsp }
252 86c3caaf 2018-03-09 stsp
253 056e7441 2018-03-11 stsp /* Create an empty lock file. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
255 056e7441 2018-03-11 stsp if (err)
256 056e7441 2018-03-11 stsp goto done;
257 056e7441 2018-03-11 stsp
258 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
259 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
260 99724ed4 2018-03-10 stsp if (err)
261 86c3caaf 2018-03-09 stsp goto done;
262 86c3caaf 2018-03-09 stsp
263 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
264 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
265 65596e15 2018-12-24 stsp if (err)
266 65596e15 2018-12-24 stsp goto done;
267 65596e15 2018-12-24 stsp
268 65596e15 2018-12-24 stsp /* Record our base commit. */
269 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
270 65596e15 2018-12-24 stsp if (err)
271 65596e15 2018-12-24 stsp goto done;
272 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 99724ed4 2018-03-10 stsp if (err)
274 86c3caaf 2018-03-09 stsp goto done;
275 86c3caaf 2018-03-09 stsp
276 1451e70d 2018-03-10 stsp /* Store path to repository. */
277 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
279 99724ed4 2018-03-10 stsp if (err)
280 86c3caaf 2018-03-09 stsp goto done;
281 86c3caaf 2018-03-09 stsp
282 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
283 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
285 ec22038e 2019-03-10 stsp if (err)
286 ec22038e 2019-03-10 stsp goto done;
287 ec22038e 2019-03-10 stsp
288 ec22038e 2019-03-10 stsp /* Generate UUID. */
289 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_create");
292 ec22038e 2019-03-10 stsp goto done;
293 ec22038e 2019-03-10 stsp }
294 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
296 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_to_string");
297 ec22038e 2019-03-10 stsp goto done;
298 ec22038e 2019-03-10 stsp }
299 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 577ec78f 2018-03-11 stsp if (err)
301 577ec78f 2018-03-11 stsp goto done;
302 577ec78f 2018-03-11 stsp
303 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
304 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
306 1451e70d 2018-03-10 stsp goto done;
307 1451e70d 2018-03-10 stsp }
308 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
309 99724ed4 2018-03-10 stsp if (err)
310 1451e70d 2018-03-10 stsp goto done;
311 1451e70d 2018-03-10 stsp
312 86c3caaf 2018-03-09 stsp done:
313 65596e15 2018-12-24 stsp free(commit_id);
314 7ac97322 2018-03-11 stsp free(path_got);
315 1451e70d 2018-03-10 stsp free(formatstr);
316 0bb8a95e 2018-03-12 stsp free(absprefix);
317 65596e15 2018-12-24 stsp free(basestr);
318 ec22038e 2019-03-10 stsp free(uuidstr);
319 86c3caaf 2018-03-09 stsp return err;
320 86c3caaf 2018-03-09 stsp }
321 86c3caaf 2018-03-09 stsp
322 247140b2 2019-02-05 stsp static const struct got_error *
323 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
324 86c3caaf 2018-03-09 stsp {
325 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
326 7ac97322 2018-03-11 stsp char *path_got;
327 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
328 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
329 056e7441 2018-03-11 stsp char *path_lock = NULL;
330 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
331 6d9d28c3 2018-03-11 stsp int version, fd = -1;
332 6d9d28c3 2018-03-11 stsp const char *errstr;
333 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
334 c442a90d 2019-03-10 stsp uint32_t uuid_status;
335 6d9d28c3 2018-03-11 stsp
336 6d9d28c3 2018-03-11 stsp *worktree = NULL;
337 6d9d28c3 2018-03-11 stsp
338 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
340 7ac97322 2018-03-11 stsp path_got = NULL;
341 6d9d28c3 2018-03-11 stsp goto done;
342 6d9d28c3 2018-03-11 stsp }
343 6d9d28c3 2018-03-11 stsp
344 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
346 056e7441 2018-03-11 stsp path_lock = NULL;
347 6d9d28c3 2018-03-11 stsp goto done;
348 6d9d28c3 2018-03-11 stsp }
349 6d9d28c3 2018-03-11 stsp
350 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 6d9d28c3 2018-03-11 stsp if (fd == -1) {
352 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
354 6d9d28c3 2018-03-11 stsp goto done;
355 6d9d28c3 2018-03-11 stsp }
356 6d9d28c3 2018-03-11 stsp
357 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 6d9d28c3 2018-03-11 stsp if (err)
359 6d9d28c3 2018-03-11 stsp goto done;
360 6d9d28c3 2018-03-11 stsp
361 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 6d9d28c3 2018-03-11 stsp if (errstr) {
363 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
364 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
365 6d9d28c3 2018-03-11 stsp goto done;
366 6d9d28c3 2018-03-11 stsp }
367 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
369 6d9d28c3 2018-03-11 stsp goto done;
370 6d9d28c3 2018-03-11 stsp }
371 6d9d28c3 2018-03-11 stsp
372 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
373 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
374 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
375 6d9d28c3 2018-03-11 stsp goto done;
376 6d9d28c3 2018-03-11 stsp }
377 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
378 6d9d28c3 2018-03-11 stsp
379 7d61d891 2020-07-23 stsp (*worktree)->root_path = realpath(path, NULL);
380 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
381 7d61d891 2020-07-23 stsp err = got_error_from_errno2("realpath", path);
382 6d9d28c3 2018-03-11 stsp goto done;
383 6d9d28c3 2018-03-11 stsp }
384 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
386 6d9d28c3 2018-03-11 stsp if (err)
387 6d9d28c3 2018-03-11 stsp goto done;
388 eaccb85f 2018-12-25 stsp
389 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
391 93a30277 2018-12-24 stsp if (err)
392 93a30277 2018-12-24 stsp goto done;
393 93a30277 2018-12-24 stsp
394 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
395 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
396 c442a90d 2019-03-10 stsp if (err)
397 c442a90d 2019-03-10 stsp goto done;
398 c442a90d 2019-03-10 stsp
399 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 eaccb85f 2018-12-25 stsp if (err)
401 c442a90d 2019-03-10 stsp goto done;
402 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
404 cc483380 2019-09-01 stsp err = got_error_uuid(uuid_status, "uuid_from_string");
405 eaccb85f 2018-12-25 stsp goto done;
406 c442a90d 2019-03-10 stsp }
407 eaccb85f 2018-12-25 stsp
408 c9956ddf 2019-09-08 stsp err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
409 eaccb85f 2018-12-25 stsp if (err)
410 eaccb85f 2018-12-25 stsp goto done;
411 eaccb85f 2018-12-25 stsp
412 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
413 eaccb85f 2018-12-25 stsp base_commit_id_str);
414 f5baf295 2018-03-11 stsp if (err)
415 6d9d28c3 2018-03-11 stsp goto done;
416 6d9d28c3 2018-03-11 stsp
417 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
419 6d9d28c3 2018-03-11 stsp done:
420 eaccb85f 2018-12-25 stsp if (repo)
421 eaccb85f 2018-12-25 stsp got_repo_close(repo);
422 7ac97322 2018-03-11 stsp free(path_got);
423 056e7441 2018-03-11 stsp free(path_lock);
424 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
425 c442a90d 2019-03-10 stsp free(uuidstr);
426 bd165944 2019-03-10 stsp free(formatstr);
427 6d9d28c3 2018-03-11 stsp if (err) {
428 6d9d28c3 2018-03-11 stsp if (fd != -1)
429 6d9d28c3 2018-03-11 stsp close(fd);
430 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
431 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
432 6d9d28c3 2018-03-11 stsp *worktree = NULL;
433 6d9d28c3 2018-03-11 stsp } else
434 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
435 6d9d28c3 2018-03-11 stsp
436 6d9d28c3 2018-03-11 stsp return err;
437 86c3caaf 2018-03-09 stsp }
438 247140b2 2019-02-05 stsp
439 247140b2 2019-02-05 stsp const struct got_error *
440 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
441 247140b2 2019-02-05 stsp {
442 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
443 86c3caaf 2018-03-09 stsp
444 247140b2 2019-02-05 stsp do {
445 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
446 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
447 247140b2 2019-02-05 stsp return err;
448 247140b2 2019-02-05 stsp if (*worktree)
449 247140b2 2019-02-05 stsp return NULL;
450 247140b2 2019-02-05 stsp path = dirname(path);
451 d1542a27 2019-02-05 stsp if (path == NULL)
452 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
453 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
454 247140b2 2019-02-05 stsp
455 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
456 247140b2 2019-02-05 stsp }
457 247140b2 2019-02-05 stsp
458 3a6ce05a 2019-02-11 stsp const struct got_error *
459 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
460 86c3caaf 2018-03-09 stsp {
461 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
462 cde76477 2018-03-11 stsp free(worktree->repo_path);
463 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
464 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
465 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
466 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
467 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
468 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
469 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
470 7f11502c 2019-08-28 hiltjo free(worktree->root_path);
471 6d9d28c3 2018-03-11 stsp free(worktree);
472 3a6ce05a 2019-02-11 stsp return err;
473 86c3caaf 2018-03-09 stsp }
474 86c3caaf 2018-03-09 stsp
475 2fbdb5ae 2018-12-29 stsp const char *
476 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
477 c7f4312f 2019-02-05 stsp {
478 c7f4312f 2019-02-05 stsp return worktree->root_path;
479 c7f4312f 2019-02-05 stsp }
480 c7f4312f 2019-02-05 stsp
481 c7f4312f 2019-02-05 stsp const char *
482 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
483 86c3caaf 2018-03-09 stsp {
484 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
485 49520a32 2018-12-29 stsp }
486 49520a32 2018-12-29 stsp
487 49520a32 2018-12-29 stsp const char *
488 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
489 49520a32 2018-12-29 stsp {
490 f609be2e 2018-12-29 stsp return worktree->path_prefix;
491 e5dc7198 2018-12-29 stsp }
492 e5dc7198 2018-12-29 stsp
493 e5dc7198 2018-12-29 stsp const struct got_error *
494 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 e5dc7198 2018-12-29 stsp const char *path_prefix)
496 e5dc7198 2018-12-29 stsp {
497 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
498 e5dc7198 2018-12-29 stsp
499 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
500 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
502 e5dc7198 2018-12-29 stsp }
503 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
504 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
505 e5dc7198 2018-12-29 stsp free(absprefix);
506 e5dc7198 2018-12-29 stsp return NULL;
507 86c3caaf 2018-03-09 stsp }
508 86c3caaf 2018-03-09 stsp
509 bc70eb79 2019-05-09 stsp const char *
510 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
511 86c3caaf 2018-03-09 stsp {
512 36a38700 2019-05-10 stsp return worktree->head_ref_name;
513 024e9686 2019-05-14 stsp }
514 024e9686 2019-05-14 stsp
515 024e9686 2019-05-14 stsp const struct got_error *
516 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
517 024e9686 2019-05-14 stsp struct got_reference *head_ref)
518 024e9686 2019-05-14 stsp {
519 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
520 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
521 024e9686 2019-05-14 stsp
522 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
524 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
525 024e9686 2019-05-14 stsp path_got = NULL;
526 024e9686 2019-05-14 stsp goto done;
527 024e9686 2019-05-14 stsp }
528 024e9686 2019-05-14 stsp
529 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
530 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
531 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
532 024e9686 2019-05-14 stsp goto done;
533 024e9686 2019-05-14 stsp }
534 024e9686 2019-05-14 stsp
535 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
536 024e9686 2019-05-14 stsp if (err)
537 024e9686 2019-05-14 stsp goto done;
538 024e9686 2019-05-14 stsp
539 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
540 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
541 024e9686 2019-05-14 stsp done:
542 024e9686 2019-05-14 stsp free(path_got);
543 024e9686 2019-05-14 stsp if (err)
544 024e9686 2019-05-14 stsp free(head_ref_name);
545 024e9686 2019-05-14 stsp return err;
546 507dc3bb 2018-12-29 stsp }
547 507dc3bb 2018-12-29 stsp
548 b72f483a 2019-02-05 stsp struct got_object_id *
549 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
550 507dc3bb 2018-12-29 stsp {
551 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
552 86c3caaf 2018-03-09 stsp }
553 86c3caaf 2018-03-09 stsp
554 507dc3bb 2018-12-29 stsp const struct got_error *
555 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
557 507dc3bb 2018-12-29 stsp {
558 507dc3bb 2018-12-29 stsp const struct got_error *err;
559 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
560 507dc3bb 2018-12-29 stsp char *id_str = NULL;
561 507dc3bb 2018-12-29 stsp char *path_got = NULL;
562 507dc3bb 2018-12-29 stsp
563 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
566 507dc3bb 2018-12-29 stsp path_got = NULL;
567 507dc3bb 2018-12-29 stsp goto done;
568 507dc3bb 2018-12-29 stsp }
569 507dc3bb 2018-12-29 stsp
570 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
571 507dc3bb 2018-12-29 stsp if (err)
572 507dc3bb 2018-12-29 stsp return err;
573 507dc3bb 2018-12-29 stsp
574 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
576 507dc3bb 2018-12-29 stsp goto done;
577 507dc3bb 2018-12-29 stsp }
578 507dc3bb 2018-12-29 stsp
579 507dc3bb 2018-12-29 stsp /* Record our base commit. */
580 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
581 507dc3bb 2018-12-29 stsp if (err)
582 507dc3bb 2018-12-29 stsp goto done;
583 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
584 507dc3bb 2018-12-29 stsp if (err)
585 507dc3bb 2018-12-29 stsp goto done;
586 507dc3bb 2018-12-29 stsp
587 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
588 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
589 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
590 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
591 507dc3bb 2018-12-29 stsp goto done;
592 507dc3bb 2018-12-29 stsp }
593 507dc3bb 2018-12-29 stsp done:
594 507dc3bb 2018-12-29 stsp if (obj)
595 507dc3bb 2018-12-29 stsp got_object_close(obj);
596 507dc3bb 2018-12-29 stsp free(id_str);
597 507dc3bb 2018-12-29 stsp free(path_got);
598 507dc3bb 2018-12-29 stsp return err;
599 507dc3bb 2018-12-29 stsp }
600 507dc3bb 2018-12-29 stsp
601 9d31a1d8 2018-03-11 stsp static const struct got_error *
602 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
603 86c3caaf 2018-03-09 stsp {
604 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
607 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
608 86c3caaf 2018-03-09 stsp return NULL;
609 21908da4 2019-01-13 stsp }
610 21908da4 2019-01-13 stsp
611 21908da4 2019-01-13 stsp static const struct got_error *
612 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
613 4a1ddfc2 2019-01-12 stsp {
614 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
615 4a1ddfc2 2019-01-12 stsp char *abspath;
616 4a1ddfc2 2019-01-12 stsp
617 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
619 4a1ddfc2 2019-01-12 stsp
620 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
621 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
622 ddcd8544 2019-03-11 stsp struct stat sb;
623 ddcd8544 2019-03-11 stsp err = NULL;
624 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
625 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
626 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
627 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
628 3665fce0 2020-07-13 stsp err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
629 ddcd8544 2019-03-11 stsp }
630 ddcd8544 2019-03-11 stsp }
631 4a1ddfc2 2019-01-12 stsp free(abspath);
632 68c76935 2019-02-19 stsp return err;
633 68c76935 2019-02-19 stsp }
634 68c76935 2019-02-19 stsp
635 68c76935 2019-02-19 stsp static const struct got_error *
636 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
637 68c76935 2019-02-19 stsp {
638 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
639 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
640 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
641 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
642 68c76935 2019-02-19 stsp
643 68c76935 2019-02-19 stsp *same = 1;
644 68c76935 2019-02-19 stsp
645 230a42bd 2019-05-11 jcs for (;;) {
646 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
649 68c76935 2019-02-19 stsp break;
650 68c76935 2019-02-19 stsp }
651 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
653 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
654 68c76935 2019-02-19 stsp break;
655 68c76935 2019-02-19 stsp }
656 68c76935 2019-02-19 stsp if (flen1 == 0) {
657 68c76935 2019-02-19 stsp if (flen2 != 0)
658 68c76935 2019-02-19 stsp *same = 0;
659 68c76935 2019-02-19 stsp break;
660 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
661 68c76935 2019-02-19 stsp if (flen1 != 0)
662 68c76935 2019-02-19 stsp *same = 0;
663 68c76935 2019-02-19 stsp break;
664 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
665 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
666 68c76935 2019-02-19 stsp *same = 0;
667 68c76935 2019-02-19 stsp break;
668 68c76935 2019-02-19 stsp }
669 68c76935 2019-02-19 stsp } else {
670 68c76935 2019-02-19 stsp *same = 0;
671 68c76935 2019-02-19 stsp break;
672 68c76935 2019-02-19 stsp }
673 68c76935 2019-02-19 stsp }
674 68c76935 2019-02-19 stsp
675 68c76935 2019-02-19 stsp return err;
676 68c76935 2019-02-19 stsp }
677 68c76935 2019-02-19 stsp
678 68c76935 2019-02-19 stsp static const struct got_error *
679 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
680 68c76935 2019-02-19 stsp {
681 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
682 68c76935 2019-02-19 stsp struct stat sb;
683 68c76935 2019-02-19 stsp size_t size1, size2;
684 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
685 68c76935 2019-02-19 stsp
686 68c76935 2019-02-19 stsp *same = 1;
687 68c76935 2019-02-19 stsp
688 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
690 68c76935 2019-02-19 stsp goto done;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp size1 = sb.st_size;
693 68c76935 2019-02-19 stsp
694 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
695 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
696 68c76935 2019-02-19 stsp goto done;
697 68c76935 2019-02-19 stsp }
698 68c76935 2019-02-19 stsp size2 = sb.st_size;
699 68c76935 2019-02-19 stsp
700 68c76935 2019-02-19 stsp if (size1 != size2) {
701 68c76935 2019-02-19 stsp *same = 0;
702 68c76935 2019-02-19 stsp return NULL;
703 68c76935 2019-02-19 stsp }
704 68c76935 2019-02-19 stsp
705 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
706 68c76935 2019-02-19 stsp if (f1 == NULL)
707 60522982 2019-12-15 stsp return got_error_from_errno2("fopen", f1_path);
708 68c76935 2019-02-19 stsp
709 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
710 68c76935 2019-02-19 stsp if (f2 == NULL) {
711 60522982 2019-12-15 stsp err = got_error_from_errno2("fopen", f2_path);
712 68c76935 2019-02-19 stsp goto done;
713 68c76935 2019-02-19 stsp }
714 68c76935 2019-02-19 stsp
715 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
716 68c76935 2019-02-19 stsp done:
717 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
718 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
719 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
720 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
721 68c76935 2019-02-19 stsp
722 6353ad76 2019-02-08 stsp return err;
723 6353ad76 2019-02-08 stsp }
724 6353ad76 2019-02-08 stsp
725 6353ad76 2019-02-08 stsp /*
726 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 14c901f1 2019-08-08 stsp * the file at deriv_path acts as the first derived version, and the
728 14c901f1 2019-08-08 stsp * file on disk acts as the second derived version.
729 6353ad76 2019-02-08 stsp */
730 6353ad76 2019-02-08 stsp static const struct got_error *
731 14c901f1 2019-08-08 stsp merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
733 14c901f1 2019-08-08 stsp const char *path, uint16_t st_mode, const char *deriv_path,
734 f69721c3 2019-10-21 stsp const char *label_orig, const char *label_deriv,
735 f69721c3 2019-10-21 stsp struct got_repository *repo,
736 14c901f1 2019-08-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
737 6353ad76 2019-02-08 stsp {
738 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
739 6353ad76 2019-02-08 stsp int merged_fd = -1;
740 14c901f1 2019-08-08 stsp FILE *f_orig = NULL;
741 14c901f1 2019-08-08 stsp char *blob_orig_path = NULL;
742 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
743 234035bc 2019-06-01 stsp int overlapcnt = 0;
744 af54ae4a 2019-02-19 stsp char *parent;
745 6353ad76 2019-02-08 stsp
746 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
747 234035bc 2019-06-01 stsp
748 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
749 af54ae4a 2019-02-19 stsp if (parent == NULL)
750 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
751 af54ae4a 2019-02-19 stsp
752 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
754 af54ae4a 2019-02-19 stsp
755 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
756 6353ad76 2019-02-08 stsp if (err)
757 6353ad76 2019-02-08 stsp goto done;
758 6353ad76 2019-02-08 stsp
759 af54ae4a 2019-02-19 stsp free(base_path);
760 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
762 af54ae4a 2019-02-19 stsp base_path = NULL;
763 af54ae4a 2019-02-19 stsp goto done;
764 af54ae4a 2019-02-19 stsp }
765 af54ae4a 2019-02-19 stsp
766 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
767 6353ad76 2019-02-08 stsp if (err)
768 6353ad76 2019-02-08 stsp goto done;
769 46b6ee73 2019-06-04 stsp if (blob_orig) {
770 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
771 46b6ee73 2019-06-04 stsp blob_orig);
772 1430b4e0 2019-03-27 stsp if (err)
773 1430b4e0 2019-03-27 stsp goto done;
774 1430b4e0 2019-03-27 stsp } else {
775 1430b4e0 2019-03-27 stsp /*
776 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
777 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
778 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
779 1430b4e0 2019-03-27 stsp */
780 1430b4e0 2019-03-27 stsp }
781 6353ad76 2019-02-08 stsp
782 14c901f1 2019-08-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 f69721c3 2019-10-21 stsp blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
784 6353ad76 2019-02-08 stsp if (err)
785 6353ad76 2019-02-08 stsp goto done;
786 6353ad76 2019-02-08 stsp
787 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
788 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
789 1ee397ad 2019-07-12 stsp if (err)
790 1ee397ad 2019-07-12 stsp goto done;
791 6353ad76 2019-02-08 stsp
792 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
793 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
794 816dc654 2019-02-16 stsp goto done;
795 68c76935 2019-02-19 stsp }
796 68c76935 2019-02-19 stsp
797 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
798 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
799 14c901f1 2019-08-08 stsp err = check_files_equal(local_changes_subsumed, deriv_path,
800 68c76935 2019-02-19 stsp merged_path);
801 68c76935 2019-02-19 stsp if (err)
802 68c76935 2019-02-19 stsp goto done;
803 816dc654 2019-02-16 stsp }
804 6353ad76 2019-02-08 stsp
805 2ad902c0 2019-12-15 stsp if (fchmod(merged_fd, st_mode) != 0) {
806 2ad902c0 2019-12-15 stsp err = got_error_from_errno2("fchmod", merged_path);
807 70a0c8ec 2019-02-20 stsp goto done;
808 70a0c8ec 2019-02-20 stsp }
809 70a0c8ec 2019-02-20 stsp
810 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
811 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
812 230a42bd 2019-05-11 jcs ondisk_path);
813 6353ad76 2019-02-08 stsp goto done;
814 6353ad76 2019-02-08 stsp }
815 6353ad76 2019-02-08 stsp done:
816 fdcb7daf 2019-12-15 stsp if (err) {
817 fdcb7daf 2019-12-15 stsp if (merged_path)
818 fdcb7daf 2019-12-15 stsp unlink(merged_path);
819 fdcb7daf 2019-12-15 stsp }
820 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
822 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
824 6353ad76 2019-02-08 stsp free(merged_path);
825 af54ae4a 2019-02-19 stsp free(base_path);
826 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
827 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
828 46b6ee73 2019-06-04 stsp free(blob_orig_path);
829 6353ad76 2019-02-08 stsp }
830 14c901f1 2019-08-08 stsp return err;
831 14c901f1 2019-08-08 stsp }
832 14c901f1 2019-08-08 stsp
833 14c901f1 2019-08-08 stsp /*
834 14c901f1 2019-08-08 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
835 14c901f1 2019-08-08 stsp * blob_deriv acts as the first derived version, and the file on disk
836 14c901f1 2019-08-08 stsp * acts as the second derived version.
837 14c901f1 2019-08-08 stsp */
838 14c901f1 2019-08-08 stsp static const struct got_error *
839 14c901f1 2019-08-08 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
840 14c901f1 2019-08-08 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
841 f69721c3 2019-10-21 stsp const char *path, uint16_t st_mode, const char *label_orig,
842 f69721c3 2019-10-21 stsp struct got_blob_object *blob_deriv,
843 f69721c3 2019-10-21 stsp struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
844 f69721c3 2019-10-21 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
845 14c901f1 2019-08-08 stsp {
846 14c901f1 2019-08-08 stsp const struct got_error *err = NULL;
847 14c901f1 2019-08-08 stsp FILE *f_deriv = NULL;
848 14c901f1 2019-08-08 stsp char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
849 14c901f1 2019-08-08 stsp char *label_deriv = NULL, *parent;
850 14c901f1 2019-08-08 stsp
851 14c901f1 2019-08-08 stsp *local_changes_subsumed = 0;
852 14c901f1 2019-08-08 stsp
853 14c901f1 2019-08-08 stsp parent = dirname(ondisk_path);
854 14c901f1 2019-08-08 stsp if (parent == NULL)
855 14c901f1 2019-08-08 stsp return got_error_from_errno2("dirname", ondisk_path);
856 14c901f1 2019-08-08 stsp
857 14c901f1 2019-08-08 stsp free(base_path);
858 14c901f1 2019-08-08 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
859 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
860 14c901f1 2019-08-08 stsp base_path = NULL;
861 14c901f1 2019-08-08 stsp goto done;
862 14c901f1 2019-08-08 stsp }
863 14c901f1 2019-08-08 stsp
864 14c901f1 2019-08-08 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
865 14c901f1 2019-08-08 stsp if (err)
866 14c901f1 2019-08-08 stsp goto done;
867 14c901f1 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
868 14c901f1 2019-08-08 stsp blob_deriv);
869 14c901f1 2019-08-08 stsp if (err)
870 14c901f1 2019-08-08 stsp goto done;
871 14c901f1 2019-08-08 stsp
872 14c901f1 2019-08-08 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
873 14c901f1 2019-08-08 stsp if (err)
874 14c901f1 2019-08-08 stsp goto done;
875 f69721c3 2019-10-21 stsp if (asprintf(&label_deriv, "%s: commit %s",
876 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_MERGED, id_str) == -1) {
877 14c901f1 2019-08-08 stsp err = got_error_from_errno("asprintf");
878 14c901f1 2019-08-08 stsp goto done;
879 14c901f1 2019-08-08 stsp }
880 14c901f1 2019-08-08 stsp
881 14c901f1 2019-08-08 stsp err = merge_file(local_changes_subsumed, worktree, blob_orig,
882 f69721c3 2019-10-21 stsp ondisk_path, path, st_mode, blob_deriv_path, label_orig,
883 f69721c3 2019-10-21 stsp label_deriv, repo, progress_cb, progress_arg);
884 14c901f1 2019-08-08 stsp done:
885 14c901f1 2019-08-08 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
886 14c901f1 2019-08-08 stsp err = got_error_from_errno("fclose");
887 14c901f1 2019-08-08 stsp free(base_path);
888 14c901f1 2019-08-08 stsp if (blob_deriv_path) {
889 14c901f1 2019-08-08 stsp unlink(blob_deriv_path);
890 14c901f1 2019-08-08 stsp free(blob_deriv_path);
891 14c901f1 2019-08-08 stsp }
892 6353ad76 2019-02-08 stsp free(id_str);
893 818c7501 2019-07-11 stsp free(label_deriv);
894 4a1ddfc2 2019-01-12 stsp return err;
895 4a1ddfc2 2019-01-12 stsp }
896 4a1ddfc2 2019-01-12 stsp
897 4a1ddfc2 2019-01-12 stsp static const struct got_error *
898 054041d0 2020-06-24 stsp create_fileindex_entry(struct got_fileindex *fileindex,
899 054041d0 2020-06-24 stsp struct got_object_id *base_commit_id, const char *ondisk_path,
900 054041d0 2020-06-24 stsp const char *path, struct got_object_id *blob_id)
901 13d9040b 2019-03-27 stsp {
902 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
903 054041d0 2020-06-24 stsp struct got_fileindex_entry *new_ie;
904 13d9040b 2019-03-27 stsp
905 054041d0 2020-06-24 stsp err = got_fileindex_entry_alloc(&new_ie, path);
906 054041d0 2020-06-24 stsp if (err)
907 054041d0 2020-06-24 stsp return err;
908 054041d0 2020-06-24 stsp
909 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(new_ie, ondisk_path,
910 054041d0 2020-06-24 stsp blob_id->sha1, base_commit_id->sha1, 1);
911 054041d0 2020-06-24 stsp if (err)
912 054041d0 2020-06-24 stsp goto done;
913 054041d0 2020-06-24 stsp
914 054041d0 2020-06-24 stsp err = got_fileindex_entry_add(fileindex, new_ie);
915 054041d0 2020-06-24 stsp done:
916 054041d0 2020-06-24 stsp if (err)
917 054041d0 2020-06-24 stsp got_fileindex_entry_free(new_ie);
918 13d9040b 2019-03-27 stsp return err;
919 1ebedb77 2019-10-19 stsp }
920 1ebedb77 2019-10-19 stsp
921 1ebedb77 2019-10-19 stsp static mode_t
922 1ebedb77 2019-10-19 stsp get_ondisk_perms(int executable, mode_t st_mode)
923 1ebedb77 2019-10-19 stsp {
924 1ebedb77 2019-10-19 stsp mode_t xbits = S_IXUSR;
925 1ebedb77 2019-10-19 stsp
926 1ebedb77 2019-10-19 stsp if (executable) {
927 1ebedb77 2019-10-19 stsp /* Map read bits to execute bits. */
928 1ebedb77 2019-10-19 stsp if (st_mode & S_IRGRP)
929 1ebedb77 2019-10-19 stsp xbits |= S_IXGRP;
930 1ebedb77 2019-10-19 stsp if (st_mode & S_IROTH)
931 1ebedb77 2019-10-19 stsp xbits |= S_IXOTH;
932 1ebedb77 2019-10-19 stsp return st_mode | xbits;
933 1ebedb77 2019-10-19 stsp }
934 1ebedb77 2019-10-19 stsp
935 1ebedb77 2019-10-19 stsp return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
936 13d9040b 2019-03-27 stsp }
937 13d9040b 2019-03-27 stsp
938 8ba819a3 2020-07-23 stsp /* forward declaration */
939 13d9040b 2019-03-27 stsp static const struct got_error *
940 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
941 bb51a5b4 2020-01-13 stsp const char *path, mode_t te_mode, mode_t st_mode,
942 4b55f459 2019-09-08 stsp struct got_blob_object *blob, int restoring_missing_file,
943 4b55f459 2019-09-08 stsp int reverting_versioned_file, struct got_repository *repo,
944 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
945 8ba819a3 2020-07-23 stsp
946 8ba819a3 2020-07-23 stsp static const struct got_error *
947 8ba819a3 2020-07-23 stsp install_symlink(struct got_worktree *worktree, const char *ondisk_path,
948 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
949 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
950 8ba819a3 2020-07-23 stsp int reverting_versioned_file, struct got_repository *repo,
951 4b55f459 2019-09-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
952 9d31a1d8 2018-03-11 stsp {
953 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
954 8ba819a3 2020-07-23 stsp char target_path[PATH_MAX];
955 8ba819a3 2020-07-23 stsp size_t len, target_len = 0;
956 8ba819a3 2020-07-23 stsp char *resolved_path = NULL, *abspath = NULL;
957 906c123b 2020-07-23 stsp char *path_got = NULL;
958 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
959 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
960 8ba819a3 2020-07-23 stsp
961 8ba819a3 2020-07-23 stsp /*
962 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
963 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
964 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
965 8ba819a3 2020-07-23 stsp * in the blob object.
966 8ba819a3 2020-07-23 stsp */
967 8ba819a3 2020-07-23 stsp do {
968 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
969 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
970 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
971 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
972 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
973 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
974 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
975 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
976 8ba819a3 2020-07-23 stsp }
977 8ba819a3 2020-07-23 stsp if (len > 0) {
978 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
979 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
980 8ba819a3 2020-07-23 stsp len - hdrlen);
981 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
982 8ba819a3 2020-07-23 stsp hdrlen = 0;
983 8ba819a3 2020-07-23 stsp }
984 8ba819a3 2020-07-23 stsp } while (len != 0);
985 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
986 8ba819a3 2020-07-23 stsp
987 8ba819a3 2020-07-23 stsp /*
988 8ba819a3 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
989 8ba819a3 2020-07-23 stsp * in which the blob object is being installed.
990 8ba819a3 2020-07-23 stsp */
991 8ba819a3 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
992 8ba819a3 2020-07-23 stsp char *parent = dirname(ondisk_path);
993 ef68ca6f 2020-07-23 stsp if (parent == NULL) {
994 ef68ca6f 2020-07-23 stsp err = got_error_from_errno2("dirname", ondisk_path);
995 ef68ca6f 2020-07-23 stsp goto done;
996 ef68ca6f 2020-07-23 stsp }
997 8ba819a3 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
998 8ba819a3 2020-07-23 stsp err = got_error_from_errno("asprintf");
999 8ba819a3 2020-07-23 stsp goto done;
1000 8ba819a3 2020-07-23 stsp }
1001 8ba819a3 2020-07-23 stsp }
1002 8ba819a3 2020-07-23 stsp
1003 8ba819a3 2020-07-23 stsp /*
1004 8ba819a3 2020-07-23 stsp * unveil(2) restricts our view of paths in the filesystem.
1005 8ba819a3 2020-07-23 stsp * ENOENT will occur if a link target path does not exist or
1006 8ba819a3 2020-07-23 stsp * if it points outside our unveiled path space.
1007 8ba819a3 2020-07-23 stsp */
1008 8ba819a3 2020-07-23 stsp resolved_path = realpath(abspath ? abspath : target_path, NULL);
1009 8ba819a3 2020-07-23 stsp if (resolved_path == NULL) {
1010 b15bc87b 2020-07-23 stsp if (errno != ENOENT) {
1011 b15bc87b 2020-07-23 stsp err = got_error_from_errno2("realpath", target_path);
1012 b15bc87b 2020-07-23 stsp goto done;
1013 b15bc87b 2020-07-23 stsp }
1014 8ba819a3 2020-07-23 stsp }
1015 8ba819a3 2020-07-23 stsp
1016 8ba819a3 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1017 0ab20ee9 2020-07-23 stsp if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1018 0ab20ee9 2020-07-23 stsp abspath : target_path), worktree->root_path,
1019 0ab20ee9 2020-07-23 stsp strlen(worktree->root_path))) {
1020 8ba819a3 2020-07-23 stsp /* install as a regular file */
1021 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1022 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1023 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1024 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1025 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1026 8ba819a3 2020-07-23 stsp goto done;
1027 8ba819a3 2020-07-23 stsp }
1028 8ba819a3 2020-07-23 stsp
1029 906c123b 2020-07-23 stsp /* Do not allow symlinks pointing into the .got directory. */
1030 906c123b 2020-07-23 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
1031 906c123b 2020-07-23 stsp GOT_WORKTREE_GOT_DIR) == -1) {
1032 906c123b 2020-07-23 stsp err = got_error_from_errno("asprintf");
1033 906c123b 2020-07-23 stsp goto done;
1034 906c123b 2020-07-23 stsp }
1035 906c123b 2020-07-23 stsp if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1036 906c123b 2020-07-23 stsp abspath : target_path), path_got, strlen(path_got))) {
1037 906c123b 2020-07-23 stsp /* install as a regular file */
1038 906c123b 2020-07-23 stsp got_object_blob_rewind(blob);
1039 906c123b 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1040 906c123b 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1041 906c123b 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1042 906c123b 2020-07-23 stsp repo, progress_cb, progress_arg);
1043 906c123b 2020-07-23 stsp goto done;
1044 906c123b 2020-07-23 stsp }
1045 906c123b 2020-07-23 stsp
1046 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1047 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1048 8ba819a3 2020-07-23 stsp struct stat sb;
1049 8ba819a3 2020-07-23 stsp ssize_t elen;
1050 8ba819a3 2020-07-23 stsp char etarget[PATH_MAX];
1051 8ba819a3 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
1052 8ba819a3 2020-07-23 stsp err = got_error_from_errno2("lstat",
1053 8ba819a3 2020-07-23 stsp ondisk_path);
1054 8ba819a3 2020-07-23 stsp goto done;
1055 8ba819a3 2020-07-23 stsp }
1056 8ba819a3 2020-07-23 stsp if (!S_ISLNK(sb.st_mode)) {
1057 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1058 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1059 8ba819a3 2020-07-23 stsp goto done;
1060 8ba819a3 2020-07-23 stsp }
1061 8ba819a3 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1062 8ba819a3 2020-07-23 stsp if (elen == -1) {
1063 8ba819a3 2020-07-23 stsp err = got_error_from_errno2("readlink",
1064 8ba819a3 2020-07-23 stsp ondisk_path);
1065 8ba819a3 2020-07-23 stsp goto done;
1066 8ba819a3 2020-07-23 stsp }
1067 8ba819a3 2020-07-23 stsp if (elen == target_len &&
1068 f35fa46a 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0) {
1069 f35fa46a 2020-07-23 stsp err = NULL; /* nothing to do */
1070 f35fa46a 2020-07-23 stsp goto done;
1071 f35fa46a 2020-07-23 stsp } else {
1072 f35fa46a 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
1073 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("unlink",
1074 f35fa46a 2020-07-23 stsp ondisk_path);
1075 f35fa46a 2020-07-23 stsp goto done;
1076 f35fa46a 2020-07-23 stsp }
1077 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1078 f35fa46a 2020-07-23 stsp err = got_error_from_errno3("symlink",
1079 f35fa46a 2020-07-23 stsp target_path, ondisk_path);
1080 f35fa46a 2020-07-23 stsp goto done;
1081 f35fa46a 2020-07-23 stsp }
1082 f35fa46a 2020-07-23 stsp
1083 f35fa46a 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1084 f35fa46a 2020-07-23 stsp GOT_STATUS_UPDATE, path);
1085 f35fa46a 2020-07-23 stsp goto done;
1086 f35fa46a 2020-07-23 stsp }
1087 f35fa46a 2020-07-23 stsp }
1088 f35fa46a 2020-07-23 stsp
1089 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1090 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1091 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1092 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1093 f35fa46a 2020-07-23 stsp ondisk_path);
1094 f35fa46a 2020-07-23 stsp goto done;
1095 f35fa46a 2020-07-23 stsp }
1096 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1097 f35fa46a 2020-07-23 stsp if (err)
1098 f35fa46a 2020-07-23 stsp goto done;
1099 f35fa46a 2020-07-23 stsp /*
1100 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1101 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1102 f35fa46a 2020-07-23 stsp */
1103 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1104 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1105 f35fa46a 2020-07-23 stsp goto done;
1106 f35fa46a 2020-07-23 stsp }
1107 f35fa46a 2020-07-23 stsp }
1108 f35fa46a 2020-07-23 stsp
1109 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1110 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1111 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1112 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1113 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1114 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1115 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1116 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1117 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1118 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1119 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1120 8ba819a3 2020-07-23 stsp } else {
1121 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1122 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1123 8ba819a3 2020-07-23 stsp }
1124 f35fa46a 2020-07-23 stsp } else
1125 f35fa46a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1126 8ba819a3 2020-07-23 stsp done:
1127 8ba819a3 2020-07-23 stsp free(resolved_path);
1128 8ba819a3 2020-07-23 stsp free(abspath);
1129 906c123b 2020-07-23 stsp free(path_got);
1130 8ba819a3 2020-07-23 stsp return err;
1131 8ba819a3 2020-07-23 stsp }
1132 8ba819a3 2020-07-23 stsp
1133 8ba819a3 2020-07-23 stsp static const struct got_error *
1134 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1135 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1136 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1137 8ba819a3 2020-07-23 stsp int reverting_versioned_file, struct got_repository *repo,
1138 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1139 8ba819a3 2020-07-23 stsp {
1140 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1141 507dc3bb 2018-12-29 stsp int fd = -1;
1142 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1143 507dc3bb 2018-12-29 stsp int update = 0;
1144 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1145 8ba819a3 2020-07-23 stsp
1146 8ba819a3 2020-07-23 stsp if (S_ISLNK(te_mode))
1147 8ba819a3 2020-07-23 stsp return install_symlink(worktree, ondisk_path, path, te_mode,
1148 8ba819a3 2020-07-23 stsp st_mode, blob, restoring_missing_file,
1149 8ba819a3 2020-07-23 stsp reverting_versioned_file, repo, progress_cb, progress_arg);
1150 9d31a1d8 2018-03-11 stsp
1151 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1152 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1153 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1154 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1155 21908da4 2019-01-13 stsp char *parent = dirname(path);
1156 21908da4 2019-01-13 stsp if (parent == NULL)
1157 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1158 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1159 21908da4 2019-01-13 stsp if (err)
1160 21908da4 2019-01-13 stsp return err;
1161 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1162 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1163 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1164 21908da4 2019-01-13 stsp if (fd == -1)
1165 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1166 230a42bd 2019-05-11 jcs ondisk_path);
1167 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1168 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
1169 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1170 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1171 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1172 507dc3bb 2018-12-29 stsp goto done;
1173 d70b8e30 2018-12-27 stsp } else {
1174 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1175 507dc3bb 2018-12-29 stsp ondisk_path);
1176 507dc3bb 2018-12-29 stsp if (err)
1177 507dc3bb 2018-12-29 stsp goto done;
1178 507dc3bb 2018-12-29 stsp update = 1;
1179 9d31a1d8 2018-03-11 stsp }
1180 507dc3bb 2018-12-29 stsp } else
1181 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1182 9d31a1d8 2018-03-11 stsp }
1183 9d31a1d8 2018-03-11 stsp
1184 a378724f 2019-02-10 stsp if (restoring_missing_file)
1185 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1186 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
1187 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1188 a378724f 2019-02-10 stsp else
1189 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
1190 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1191 1ee397ad 2019-07-12 stsp if (err)
1192 1ee397ad 2019-07-12 stsp goto done;
1193 d7b62c98 2018-12-27 stsp
1194 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1195 9d31a1d8 2018-03-11 stsp do {
1196 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1197 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1198 9d31a1d8 2018-03-11 stsp if (err)
1199 9d31a1d8 2018-03-11 stsp break;
1200 9d31a1d8 2018-03-11 stsp if (len > 0) {
1201 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1202 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1203 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1204 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1205 b87c6f83 2018-12-24 stsp goto done;
1206 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1207 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1208 b87c6f83 2018-12-24 stsp goto done;
1209 9d31a1d8 2018-03-11 stsp }
1210 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1211 9d31a1d8 2018-03-11 stsp }
1212 9d31a1d8 2018-03-11 stsp } while (len != 0);
1213 9d31a1d8 2018-03-11 stsp
1214 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1215 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1216 816dc654 2019-02-16 stsp goto done;
1217 816dc654 2019-02-16 stsp }
1218 9d31a1d8 2018-03-11 stsp
1219 507dc3bb 2018-12-29 stsp if (update) {
1220 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1221 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1222 230a42bd 2019-05-11 jcs ondisk_path);
1223 2a57020b 2019-02-20 stsp unlink(tmppath);
1224 68ed9ba5 2019-02-10 stsp goto done;
1225 68ed9ba5 2019-02-10 stsp }
1226 68ed9ba5 2019-02-10 stsp }
1227 ba8a0d4d 2019-02-10 stsp
1228 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1229 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1230 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1231 1ebedb77 2019-10-19 stsp goto done;
1232 507dc3bb 2018-12-29 stsp }
1233 507dc3bb 2018-12-29 stsp
1234 9d31a1d8 2018-03-11 stsp done:
1235 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1236 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1237 507dc3bb 2018-12-29 stsp free(tmppath);
1238 6353ad76 2019-02-08 stsp return err;
1239 6353ad76 2019-02-08 stsp }
1240 6353ad76 2019-02-08 stsp
1241 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1242 6353ad76 2019-02-08 stsp static const struct got_error *
1243 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1244 7154f6ce 2019-03-27 stsp {
1245 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1246 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1247 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1248 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1249 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1250 7154f6ce 2019-03-27 stsp };
1251 7154f6ce 2019-03-27 stsp int i = 0;
1252 7154f6ce 2019-03-27 stsp char *line;
1253 7154f6ce 2019-03-27 stsp size_t len;
1254 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1255 7154f6ce 2019-03-27 stsp
1256 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1257 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1258 7154f6ce 2019-03-27 stsp if (line == NULL) {
1259 7154f6ce 2019-03-27 stsp if (feof(f))
1260 7154f6ce 2019-03-27 stsp break;
1261 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1262 7154f6ce 2019-03-27 stsp break;
1263 7154f6ce 2019-03-27 stsp }
1264 7154f6ce 2019-03-27 stsp
1265 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1266 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1267 19332e6d 2019-05-13 stsp == 0)
1268 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1269 7154f6ce 2019-03-27 stsp else
1270 7154f6ce 2019-03-27 stsp i++;
1271 7154f6ce 2019-03-27 stsp }
1272 7154f6ce 2019-03-27 stsp }
1273 7154f6ce 2019-03-27 stsp
1274 7154f6ce 2019-03-27 stsp return err;
1275 e2b1e152 2019-07-13 stsp }
1276 e2b1e152 2019-07-13 stsp
1277 e2b1e152 2019-07-13 stsp static int
1278 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1279 1ebedb77 2019-10-19 stsp {
1280 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1281 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1282 1ebedb77 2019-10-19 stsp }
1283 1ebedb77 2019-10-19 stsp
1284 1ebedb77 2019-10-19 stsp static int
1285 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1286 e2b1e152 2019-07-13 stsp {
1287 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1288 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1289 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1290 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1291 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1292 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1293 c363b2c1 2019-08-03 stsp }
1294 c363b2c1 2019-08-03 stsp
1295 c363b2c1 2019-08-03 stsp static unsigned char
1296 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1297 c363b2c1 2019-08-03 stsp {
1298 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1299 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1300 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1301 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1302 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1303 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1304 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1305 c363b2c1 2019-08-03 stsp default:
1306 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1307 a919d5c4 2020-07-23 stsp }
1308 a919d5c4 2020-07-23 stsp }
1309 a919d5c4 2020-07-23 stsp
1310 a919d5c4 2020-07-23 stsp static const struct got_error *
1311 a919d5c4 2020-07-23 stsp get_symlink_status(unsigned char *status, struct stat *sb,
1312 a919d5c4 2020-07-23 stsp struct got_fileindex_entry *ie, const char *abspath,
1313 a919d5c4 2020-07-23 stsp int dirfd, const char *de_name, struct got_blob_object *blob)
1314 a919d5c4 2020-07-23 stsp {
1315 a919d5c4 2020-07-23 stsp const struct got_error *err = NULL;
1316 a919d5c4 2020-07-23 stsp char target_path[PATH_MAX];
1317 a919d5c4 2020-07-23 stsp char etarget[PATH_MAX];
1318 a919d5c4 2020-07-23 stsp ssize_t elen;
1319 a919d5c4 2020-07-23 stsp size_t len, target_len = 0;
1320 a919d5c4 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1321 a919d5c4 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
1322 a919d5c4 2020-07-23 stsp
1323 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_NO_CHANGE;
1324 a919d5c4 2020-07-23 stsp
1325 a919d5c4 2020-07-23 stsp /* Blob object content specifies the target path of the link. */
1326 a919d5c4 2020-07-23 stsp do {
1327 a919d5c4 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1328 a919d5c4 2020-07-23 stsp if (err)
1329 a919d5c4 2020-07-23 stsp return err;
1330 a919d5c4 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
1331 a919d5c4 2020-07-23 stsp /*
1332 a919d5c4 2020-07-23 stsp * Should not happen. The blob contents were OK
1333 a919d5c4 2020-07-23 stsp * when this symlink was installed.
1334 a919d5c4 2020-07-23 stsp */
1335 a919d5c4 2020-07-23 stsp return got_error(GOT_ERR_NO_SPACE);
1336 a919d5c4 2020-07-23 stsp }
1337 a919d5c4 2020-07-23 stsp if (len > 0) {
1338 a919d5c4 2020-07-23 stsp /* Skip blob object header first time around. */
1339 a919d5c4 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
1340 a919d5c4 2020-07-23 stsp len - hdrlen);
1341 a919d5c4 2020-07-23 stsp target_len += len - hdrlen;
1342 a919d5c4 2020-07-23 stsp hdrlen = 0;
1343 a919d5c4 2020-07-23 stsp }
1344 a919d5c4 2020-07-23 stsp } while (len != 0);
1345 a919d5c4 2020-07-23 stsp target_path[target_len] = '\0';
1346 a919d5c4 2020-07-23 stsp
1347 a919d5c4 2020-07-23 stsp if (dirfd != -1) {
1348 a919d5c4 2020-07-23 stsp elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1349 a919d5c4 2020-07-23 stsp if (elen == -1)
1350 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1351 a919d5c4 2020-07-23 stsp } else {
1352 a919d5c4 2020-07-23 stsp elen = readlink(abspath, etarget, sizeof(etarget));
1353 a919d5c4 2020-07-23 stsp if (elen == -1)
1354 a919d5c4 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
1355 c363b2c1 2019-08-03 stsp }
1356 a919d5c4 2020-07-23 stsp
1357 a919d5c4 2020-07-23 stsp if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1358 a919d5c4 2020-07-23 stsp *status = GOT_STATUS_MODIFY;
1359 a919d5c4 2020-07-23 stsp
1360 a919d5c4 2020-07-23 stsp return NULL;
1361 7154f6ce 2019-03-27 stsp }
1362 7154f6ce 2019-03-27 stsp
1363 7154f6ce 2019-03-27 stsp static const struct got_error *
1364 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1365 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1366 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1367 6353ad76 2019-02-08 stsp {
1368 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1369 6353ad76 2019-02-08 stsp struct got_object_id id;
1370 6353ad76 2019-02-08 stsp size_t hdrlen;
1371 1338848f 2019-12-13 stsp int fd = -1;
1372 6353ad76 2019-02-08 stsp FILE *f = NULL;
1373 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1374 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1375 6353ad76 2019-02-08 stsp size_t flen, blen;
1376 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1377 6353ad76 2019-02-08 stsp
1378 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1379 6353ad76 2019-02-08 stsp
1380 7f91a133 2019-12-13 stsp /*
1381 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1382 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1383 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1384 7f91a133 2019-12-13 stsp */
1385 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1386 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1387 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1388 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1389 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1390 882ef1b9 2019-12-13 stsp else
1391 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1392 882ef1b9 2019-12-13 stsp goto done;
1393 882ef1b9 2019-12-13 stsp }
1394 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1395 3d35a492 2019-12-13 stsp goto done;
1396 3d35a492 2019-12-13 stsp }
1397 7f91a133 2019-12-13 stsp } else {
1398 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1399 a919d5c4 2020-07-23 stsp if (fd == -1 && errno != ENOENT && errno != ELOOP)
1400 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1401 a919d5c4 2020-07-23 stsp else if (fd == -1 && errno == ELOOP) {
1402 a919d5c4 2020-07-23 stsp if (lstat(abspath, sb) == -1)
1403 a919d5c4 2020-07-23 stsp return got_error_from_errno2("lstat", abspath);
1404 a919d5c4 2020-07-23 stsp } else if (fd == -1 || fstat(fd, sb) == -1) {
1405 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1406 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1407 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1408 3d35a492 2019-12-13 stsp else
1409 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1410 3d35a492 2019-12-13 stsp goto done;
1411 3d35a492 2019-12-13 stsp }
1412 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1413 1338848f 2019-12-13 stsp goto done;
1414 a378724f 2019-02-10 stsp }
1415 a378724f 2019-02-10 stsp }
1416 6353ad76 2019-02-08 stsp
1417 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1418 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1419 1338848f 2019-12-13 stsp goto done;
1420 3f148bc6 2019-07-27 stsp }
1421 efdd40df 2019-07-27 stsp
1422 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1423 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1424 1338848f 2019-12-13 stsp goto done;
1425 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1426 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1427 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1428 1338848f 2019-12-13 stsp goto done;
1429 d00136be 2019-03-26 stsp }
1430 b8f41171 2019-02-10 stsp
1431 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1432 1338848f 2019-12-13 stsp goto done;
1433 6353ad76 2019-02-08 stsp
1434 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1435 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1436 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1437 c363b2c1 2019-08-03 stsp else
1438 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1439 c363b2c1 2019-08-03 stsp
1440 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1441 6353ad76 2019-02-08 stsp if (err)
1442 1338848f 2019-12-13 stsp goto done;
1443 6353ad76 2019-02-08 stsp
1444 a919d5c4 2020-07-23 stsp if (S_ISLNK(sb->st_mode)) {
1445 a919d5c4 2020-07-23 stsp /* Staging changes to symlinks is not yet(?) supported. */
1446 a919d5c4 2020-07-23 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
1447 a919d5c4 2020-07-23 stsp err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1448 a919d5c4 2020-07-23 stsp goto done;
1449 a919d5c4 2020-07-23 stsp }
1450 a919d5c4 2020-07-23 stsp err = get_symlink_status(status, sb, ie, abspath, dirfd,
1451 a919d5c4 2020-07-23 stsp de_name, blob);
1452 a919d5c4 2020-07-23 stsp goto done;
1453 a919d5c4 2020-07-23 stsp }
1454 a919d5c4 2020-07-23 stsp
1455 a919d5c4 2020-07-23 stsp
1456 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1457 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1458 ab0d4361 2019-12-13 stsp if (fd == -1) {
1459 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1460 ab0d4361 2019-12-13 stsp goto done;
1461 ab0d4361 2019-12-13 stsp }
1462 3d35a492 2019-12-13 stsp }
1463 3d35a492 2019-12-13 stsp
1464 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1465 6353ad76 2019-02-08 stsp if (f == NULL) {
1466 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1467 6353ad76 2019-02-08 stsp goto done;
1468 6353ad76 2019-02-08 stsp }
1469 1338848f 2019-12-13 stsp fd = -1;
1470 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1471 656b1f76 2019-05-11 jcs for (;;) {
1472 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1473 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1474 6353ad76 2019-02-08 stsp if (err)
1475 7154f6ce 2019-03-27 stsp goto done;
1476 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1477 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1478 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1479 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1480 7154f6ce 2019-03-27 stsp goto done;
1481 80c5c120 2019-02-19 stsp }
1482 6353ad76 2019-02-08 stsp if (blen == 0) {
1483 6353ad76 2019-02-08 stsp if (flen != 0)
1484 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1485 6353ad76 2019-02-08 stsp break;
1486 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1487 6353ad76 2019-02-08 stsp if (blen != 0)
1488 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1489 6353ad76 2019-02-08 stsp break;
1490 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1491 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1492 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1493 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1494 6353ad76 2019-02-08 stsp break;
1495 6353ad76 2019-02-08 stsp }
1496 6353ad76 2019-02-08 stsp } else {
1497 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1498 6353ad76 2019-02-08 stsp break;
1499 6353ad76 2019-02-08 stsp }
1500 6353ad76 2019-02-08 stsp hdrlen = 0;
1501 6353ad76 2019-02-08 stsp }
1502 7154f6ce 2019-03-27 stsp
1503 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1504 7154f6ce 2019-03-27 stsp rewind(f);
1505 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1506 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1507 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1508 6353ad76 2019-02-08 stsp done:
1509 6353ad76 2019-02-08 stsp if (blob)
1510 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1511 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1512 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1513 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1514 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1515 9d31a1d8 2018-03-11 stsp return err;
1516 e2b1e152 2019-07-13 stsp }
1517 e2b1e152 2019-07-13 stsp
1518 e2b1e152 2019-07-13 stsp /*
1519 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1520 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1521 e2b1e152 2019-07-13 stsp */
1522 e2b1e152 2019-07-13 stsp static const struct got_error *
1523 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1524 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1525 e2b1e152 2019-07-13 stsp {
1526 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1527 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1528 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1529 e2b1e152 2019-07-13 stsp
1530 e2b1e152 2019-07-13 stsp return NULL;
1531 9d31a1d8 2018-03-11 stsp }
1532 9d31a1d8 2018-03-11 stsp
1533 9d31a1d8 2018-03-11 stsp static const struct got_error *
1534 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1535 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1536 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1537 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1538 0584f854 2019-04-06 stsp void *progress_arg)
1539 9d31a1d8 2018-03-11 stsp {
1540 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1541 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1542 6353ad76 2019-02-08 stsp char *ondisk_path;
1543 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1544 b8f41171 2019-02-10 stsp struct stat sb;
1545 9d31a1d8 2018-03-11 stsp
1546 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1547 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1548 6353ad76 2019-02-08 stsp
1549 abb4604f 2019-07-27 stsp if (ie) {
1550 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1551 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1552 a76c42e6 2019-08-03 stsp goto done;
1553 a76c42e6 2019-08-03 stsp }
1554 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1555 7f91a133 2019-12-13 stsp repo);
1556 abb4604f 2019-07-27 stsp if (err)
1557 abb4604f 2019-07-27 stsp goto done;
1558 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1559 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1560 abb4604f 2019-07-27 stsp } else
1561 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1562 6353ad76 2019-02-08 stsp
1563 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1564 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1565 b8f41171 2019-02-10 stsp goto done;
1566 b8f41171 2019-02-10 stsp }
1567 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1568 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1569 5036ab18 2020-04-18 stsp path);
1570 5036ab18 2020-04-18 stsp goto done;
1571 5036ab18 2020-04-18 stsp }
1572 b8f41171 2019-02-10 stsp
1573 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1574 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1575 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1576 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1577 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1578 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1579 e2b1e152 2019-07-13 stsp if (err)
1580 e2b1e152 2019-07-13 stsp goto done;
1581 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1582 b8f41171 2019-02-10 stsp path);
1583 6353ad76 2019-02-08 stsp goto done;
1584 a378724f 2019-02-10 stsp }
1585 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1586 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1587 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1588 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1589 b8f41171 2019-02-10 stsp goto done;
1590 e2b1e152 2019-07-13 stsp }
1591 9d31a1d8 2018-03-11 stsp }
1592 9d31a1d8 2018-03-11 stsp
1593 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1594 8da9e5f4 2019-01-12 stsp if (err)
1595 6353ad76 2019-02-08 stsp goto done;
1596 512f0d0e 2019-01-02 stsp
1597 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1598 234035bc 2019-06-01 stsp int update_timestamps;
1599 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1600 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1601 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1602 234035bc 2019-06-01 stsp struct got_object_id id2;
1603 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1604 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1605 234035bc 2019-06-01 stsp if (err)
1606 f69721c3 2019-10-21 stsp goto done;
1607 f69721c3 2019-10-21 stsp }
1608 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1609 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1610 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1611 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1612 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1613 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1614 f69721c3 2019-10-21 stsp goto done;
1615 f69721c3 2019-10-21 stsp }
1616 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1617 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1618 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1619 234035bc 2019-06-01 stsp goto done;
1620 f69721c3 2019-10-21 stsp }
1621 234035bc 2019-06-01 stsp }
1622 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1623 f69721c3 2019-10-21 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1624 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1625 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1626 f69721c3 2019-10-21 stsp free(label_orig);
1627 234035bc 2019-06-01 stsp if (blob2)
1628 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1629 909d120e 2019-09-22 stsp if (err)
1630 909d120e 2019-09-22 stsp goto done;
1631 234035bc 2019-06-01 stsp /*
1632 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1633 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1634 234035bc 2019-06-01 stsp * unmodified files again.
1635 234035bc 2019-06-01 stsp */
1636 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1637 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1638 234035bc 2019-06-01 stsp update_timestamps);
1639 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1640 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1641 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1642 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1643 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1644 1ee397ad 2019-07-12 stsp if (err)
1645 1ee397ad 2019-07-12 stsp goto done;
1646 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1647 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1648 13d9040b 2019-03-27 stsp if (err)
1649 13d9040b 2019-03-27 stsp goto done;
1650 13d9040b 2019-03-27 stsp } else {
1651 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1652 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1653 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1654 13d9040b 2019-03-27 stsp if (err)
1655 13d9040b 2019-03-27 stsp goto done;
1656 054041d0 2020-06-24 stsp if (ie) {
1657 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1658 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1659 054041d0 2020-06-24 stsp } else {
1660 054041d0 2020-06-24 stsp err = create_fileindex_entry(fileindex,
1661 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1662 054041d0 2020-06-24 stsp &blob->id);
1663 054041d0 2020-06-24 stsp }
1664 13d9040b 2019-03-27 stsp if (err)
1665 13d9040b 2019-03-27 stsp goto done;
1666 13d9040b 2019-03-27 stsp }
1667 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1668 6353ad76 2019-02-08 stsp done:
1669 6353ad76 2019-02-08 stsp free(ondisk_path);
1670 8da9e5f4 2019-01-12 stsp return err;
1671 90285c3b 2019-01-08 stsp }
1672 90285c3b 2019-01-08 stsp
1673 90285c3b 2019-01-08 stsp static const struct got_error *
1674 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1675 90285c3b 2019-01-08 stsp {
1676 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1677 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1678 90285c3b 2019-01-08 stsp
1679 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1680 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1681 90285c3b 2019-01-08 stsp
1682 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1683 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1684 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1685 c97665ae 2019-01-13 stsp } else {
1686 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1687 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1688 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1689 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1690 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1691 230a42bd 2019-05-11 jcs parent);
1692 90285c3b 2019-01-08 stsp break;
1693 90285c3b 2019-01-08 stsp }
1694 90285c3b 2019-01-08 stsp parent = dirname(parent);
1695 90285c3b 2019-01-08 stsp }
1696 90285c3b 2019-01-08 stsp }
1697 90285c3b 2019-01-08 stsp free(ondisk_path);
1698 90285c3b 2019-01-08 stsp return err;
1699 512f0d0e 2019-01-02 stsp }
1700 512f0d0e 2019-01-02 stsp
1701 708d8e67 2019-03-27 stsp static const struct got_error *
1702 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1703 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1704 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1705 708d8e67 2019-03-27 stsp {
1706 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1707 708d8e67 2019-03-27 stsp unsigned char status;
1708 708d8e67 2019-03-27 stsp struct stat sb;
1709 708d8e67 2019-03-27 stsp char *ondisk_path;
1710 708d8e67 2019-03-27 stsp
1711 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1712 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1713 a76c42e6 2019-08-03 stsp
1714 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1715 708d8e67 2019-03-27 stsp == -1)
1716 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1717 708d8e67 2019-03-27 stsp
1718 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1719 708d8e67 2019-03-27 stsp if (err)
1720 5a58a424 2020-06-23 stsp goto done;
1721 708d8e67 2019-03-27 stsp
1722 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1723 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1724 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1725 1ee397ad 2019-07-12 stsp if (err)
1726 5a58a424 2020-06-23 stsp goto done;
1727 fc6346c4 2019-03-27 stsp /*
1728 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1729 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1730 fc6346c4 2019-03-27 stsp */
1731 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1732 fc6346c4 2019-03-27 stsp 0);
1733 fc6346c4 2019-03-27 stsp } else {
1734 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1735 1ee397ad 2019-07-12 stsp if (err)
1736 5a58a424 2020-06-23 stsp goto done;
1737 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1738 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1739 fc6346c4 2019-03-27 stsp if (err)
1740 5a58a424 2020-06-23 stsp goto done;
1741 fc6346c4 2019-03-27 stsp }
1742 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1743 708d8e67 2019-03-27 stsp }
1744 5a58a424 2020-06-23 stsp done:
1745 5a58a424 2020-06-23 stsp free(ondisk_path);
1746 fc6346c4 2019-03-27 stsp return err;
1747 708d8e67 2019-03-27 stsp }
1748 708d8e67 2019-03-27 stsp
1749 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1750 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1751 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1752 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1753 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1754 8da9e5f4 2019-01-12 stsp void *progress_arg;
1755 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
1756 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1757 8da9e5f4 2019-01-12 stsp };
1758 8da9e5f4 2019-01-12 stsp
1759 512f0d0e 2019-01-02 stsp static const struct got_error *
1760 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1761 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1762 512f0d0e 2019-01-02 stsp {
1763 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1764 0584f854 2019-04-06 stsp
1765 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1766 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1767 512f0d0e 2019-01-02 stsp
1768 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1769 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1770 8da9e5f4 2019-01-12 stsp }
1771 512f0d0e 2019-01-02 stsp
1772 8da9e5f4 2019-01-12 stsp static const struct got_error *
1773 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1774 8da9e5f4 2019-01-12 stsp {
1775 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1776 7a9df742 2019-01-08 stsp
1777 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1778 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1779 0584f854 2019-04-06 stsp
1780 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1781 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1782 9d31a1d8 2018-03-11 stsp }
1783 9d31a1d8 2018-03-11 stsp
1784 9d31a1d8 2018-03-11 stsp static const struct got_error *
1785 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1786 9d31a1d8 2018-03-11 stsp {
1787 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1788 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1789 8da9e5f4 2019-01-12 stsp char *path;
1790 9d31a1d8 2018-03-11 stsp
1791 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1792 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1793 0584f854 2019-04-06 stsp
1794 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
1795 63c5ca5d 2019-08-24 stsp return NULL;
1796 63c5ca5d 2019-08-24 stsp
1797 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1798 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1799 8da9e5f4 2019-01-12 stsp == -1)
1800 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1801 9d31a1d8 2018-03-11 stsp
1802 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1803 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1804 8da9e5f4 2019-01-12 stsp else
1805 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1806 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1807 9d31a1d8 2018-03-11 stsp
1808 8da9e5f4 2019-01-12 stsp free(path);
1809 0cd1c46a 2019-03-11 stsp return err;
1810 0cd1c46a 2019-03-11 stsp }
1811 0cd1c46a 2019-03-11 stsp
1812 818c7501 2019-07-11 stsp static const struct got_error *
1813 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1814 0cd1c46a 2019-03-11 stsp {
1815 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1816 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1817 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1818 0cd1c46a 2019-03-11 stsp
1819 517bab73 2019-03-11 stsp *refname = NULL;
1820 517bab73 2019-03-11 stsp
1821 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1822 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1823 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
1824 0cd1c46a 2019-03-11 stsp
1825 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1826 0647c563 2019-03-11 stsp == -1) {
1827 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1828 0647c563 2019-03-11 stsp *refname = NULL;
1829 0cd1c46a 2019-03-11 stsp }
1830 517bab73 2019-03-11 stsp free(uuidstr);
1831 517bab73 2019-03-11 stsp return err;
1832 517bab73 2019-03-11 stsp }
1833 517bab73 2019-03-11 stsp
1834 818c7501 2019-07-11 stsp const struct got_error *
1835 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1836 818c7501 2019-07-11 stsp {
1837 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1838 818c7501 2019-07-11 stsp }
1839 818c7501 2019-07-11 stsp
1840 818c7501 2019-07-11 stsp static const struct got_error *
1841 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1842 818c7501 2019-07-11 stsp {
1843 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1844 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1845 818c7501 2019-07-11 stsp }
1846 818c7501 2019-07-11 stsp
1847 818c7501 2019-07-11 stsp static const struct got_error *
1848 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1849 818c7501 2019-07-11 stsp {
1850 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1851 818c7501 2019-07-11 stsp }
1852 818c7501 2019-07-11 stsp
1853 818c7501 2019-07-11 stsp static const struct got_error *
1854 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1855 818c7501 2019-07-11 stsp {
1856 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1857 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1858 818c7501 2019-07-11 stsp }
1859 818c7501 2019-07-11 stsp
1860 818c7501 2019-07-11 stsp static const struct got_error *
1861 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1862 818c7501 2019-07-11 stsp {
1863 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1864 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1865 0ebf8283 2019-07-24 stsp }
1866 0ebf8283 2019-07-24 stsp
1867 0ebf8283 2019-07-24 stsp static const struct got_error *
1868 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1869 0ebf8283 2019-07-24 stsp {
1870 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1871 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1872 0ebf8283 2019-07-24 stsp }
1873 0ebf8283 2019-07-24 stsp
1874 0ebf8283 2019-07-24 stsp static const struct got_error *
1875 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1876 0ebf8283 2019-07-24 stsp {
1877 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1878 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1879 0ebf8283 2019-07-24 stsp }
1880 0ebf8283 2019-07-24 stsp
1881 0ebf8283 2019-07-24 stsp static const struct got_error *
1882 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1883 0ebf8283 2019-07-24 stsp {
1884 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1885 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1886 818c7501 2019-07-11 stsp }
1887 818c7501 2019-07-11 stsp
1888 0ebf8283 2019-07-24 stsp static const struct got_error *
1889 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1890 0ebf8283 2019-07-24 stsp {
1891 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1892 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1893 0ebf8283 2019-07-24 stsp }
1894 818c7501 2019-07-11 stsp
1895 0ebf8283 2019-07-24 stsp const struct got_error *
1896 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1897 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1898 0ebf8283 2019-07-24 stsp {
1899 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1900 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1901 0ebf8283 2019-07-24 stsp *path = NULL;
1902 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1903 0ebf8283 2019-07-24 stsp }
1904 0ebf8283 2019-07-24 stsp return NULL;
1905 0ebf8283 2019-07-24 stsp }
1906 0ebf8283 2019-07-24 stsp
1907 517bab73 2019-03-11 stsp /*
1908 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1909 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1910 517bab73 2019-03-11 stsp */
1911 517bab73 2019-03-11 stsp static const struct got_error *
1912 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1913 517bab73 2019-03-11 stsp {
1914 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1915 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1916 517bab73 2019-03-11 stsp char *refname;
1917 517bab73 2019-03-11 stsp
1918 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1919 517bab73 2019-03-11 stsp if (err)
1920 517bab73 2019-03-11 stsp return err;
1921 0cd1c46a 2019-03-11 stsp
1922 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1923 0cd1c46a 2019-03-11 stsp if (err)
1924 0cd1c46a 2019-03-11 stsp goto done;
1925 0cd1c46a 2019-03-11 stsp
1926 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1927 0cd1c46a 2019-03-11 stsp done:
1928 0cd1c46a 2019-03-11 stsp free(refname);
1929 0cd1c46a 2019-03-11 stsp if (ref)
1930 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1931 3e3a69f1 2019-07-25 stsp return err;
1932 3e3a69f1 2019-07-25 stsp }
1933 3e3a69f1 2019-07-25 stsp
1934 3e3a69f1 2019-07-25 stsp static const struct got_error *
1935 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1936 3e3a69f1 2019-07-25 stsp {
1937 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1938 3e3a69f1 2019-07-25 stsp
1939 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1940 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1941 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1942 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1943 3e3a69f1 2019-07-25 stsp }
1944 9d31a1d8 2018-03-11 stsp return err;
1945 9d31a1d8 2018-03-11 stsp }
1946 9d31a1d8 2018-03-11 stsp
1947 3e3a69f1 2019-07-25 stsp
1948 ebf99748 2019-05-09 stsp static const struct got_error *
1949 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1950 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
1951 ebf99748 2019-05-09 stsp {
1952 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1953 ebf99748 2019-05-09 stsp FILE *index = NULL;
1954 0cd1c46a 2019-03-11 stsp
1955 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1956 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1957 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1958 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1959 ebf99748 2019-05-09 stsp
1960 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1961 3e3a69f1 2019-07-25 stsp if (err)
1962 ebf99748 2019-05-09 stsp goto done;
1963 ebf99748 2019-05-09 stsp
1964 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1965 ebf99748 2019-05-09 stsp if (index == NULL) {
1966 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1967 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1968 ebf99748 2019-05-09 stsp } else {
1969 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1970 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1971 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1972 ebf99748 2019-05-09 stsp }
1973 ebf99748 2019-05-09 stsp done:
1974 ebf99748 2019-05-09 stsp if (err) {
1975 ebf99748 2019-05-09 stsp free(*fileindex_path);
1976 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1977 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1978 ebf99748 2019-05-09 stsp *fileindex = NULL;
1979 ebf99748 2019-05-09 stsp }
1980 ebf99748 2019-05-09 stsp return err;
1981 ebf99748 2019-05-09 stsp }
1982 c932eeeb 2019-05-22 stsp
1983 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1984 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1985 c932eeeb 2019-05-22 stsp const char *path;
1986 c932eeeb 2019-05-22 stsp size_t path_len;
1987 c932eeeb 2019-05-22 stsp const char *entry_name;
1988 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1989 a484d721 2019-06-10 stsp void *progress_arg;
1990 c932eeeb 2019-05-22 stsp };
1991 ebf99748 2019-05-09 stsp
1992 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1993 c932eeeb 2019-05-22 stsp static const struct got_error *
1994 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1995 c932eeeb 2019-05-22 stsp {
1996 1ee397ad 2019-07-12 stsp const struct got_error *err;
1997 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1998 c932eeeb 2019-05-22 stsp
1999 c932eeeb 2019-05-22 stsp if (a->entry_name) {
2000 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
2001 c932eeeb 2019-05-22 stsp return NULL;
2002 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2003 102ff934 2019-06-11 stsp return NULL;
2004 102ff934 2019-06-11 stsp
2005 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2006 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
2007 c932eeeb 2019-05-22 stsp return NULL;
2008 c932eeeb 2019-05-22 stsp
2009 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
2010 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2011 edd02c5e 2019-07-11 stsp ie->path);
2012 1ee397ad 2019-07-12 stsp if (err)
2013 1ee397ad 2019-07-12 stsp return err;
2014 1ee397ad 2019-07-12 stsp }
2015 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2016 c932eeeb 2019-05-22 stsp return NULL;
2017 9c6338c4 2019-06-02 stsp }
2018 9c6338c4 2019-06-02 stsp
2019 9c6338c4 2019-06-02 stsp static const struct got_error *
2020 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2021 9c6338c4 2019-06-02 stsp {
2022 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
2023 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
2024 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
2025 867630bb 2020-01-17 stsp struct timespec timeout;
2026 9c6338c4 2019-06-02 stsp
2027 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
2028 9c6338c4 2019-06-02 stsp fileindex_path);
2029 9c6338c4 2019-06-02 stsp if (err)
2030 9c6338c4 2019-06-02 stsp goto done;
2031 9c6338c4 2019-06-02 stsp
2032 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
2033 9c6338c4 2019-06-02 stsp if (err)
2034 9c6338c4 2019-06-02 stsp goto done;
2035 9c6338c4 2019-06-02 stsp
2036 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
2037 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
2038 9c6338c4 2019-06-02 stsp fileindex_path);
2039 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
2040 9c6338c4 2019-06-02 stsp }
2041 867630bb 2020-01-17 stsp
2042 867630bb 2020-01-17 stsp /*
2043 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
2044 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
2045 867630bb 2020-01-17 stsp * was recorded in the file index.
2046 867630bb 2020-01-17 stsp */
2047 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
2048 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
2049 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
2050 9c6338c4 2019-06-02 stsp done:
2051 9c6338c4 2019-06-02 stsp if (new_index)
2052 9c6338c4 2019-06-02 stsp fclose(new_index);
2053 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
2054 9c6338c4 2019-06-02 stsp return err;
2055 c932eeeb 2019-05-22 stsp }
2056 6ced4176 2019-07-12 stsp
2057 6ced4176 2019-07-12 stsp static const struct got_error *
2058 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2059 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
2060 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
2061 6ced4176 2019-07-12 stsp {
2062 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
2063 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
2064 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
2065 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2066 6ced4176 2019-07-12 stsp
2067 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2068 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2069 6ced4176 2019-07-12 stsp *tree_id = NULL;
2070 6ced4176 2019-07-12 stsp
2071 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
2072 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
2073 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
2074 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2075 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2076 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2077 6ced4176 2019-07-12 stsp goto done;
2078 6ced4176 2019-07-12 stsp }
2079 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2080 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
2081 6ced4176 2019-07-12 stsp if (err)
2082 6ced4176 2019-07-12 stsp goto done;
2083 6ced4176 2019-07-12 stsp return NULL;
2084 6ced4176 2019-07-12 stsp }
2085 6ced4176 2019-07-12 stsp
2086 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
2087 6ced4176 2019-07-12 stsp
2088 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2089 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
2090 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2091 6ced4176 2019-07-12 stsp goto done;
2092 6ced4176 2019-07-12 stsp }
2093 6ced4176 2019-07-12 stsp
2094 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2095 6ced4176 2019-07-12 stsp in_repo_path);
2096 6ced4176 2019-07-12 stsp if (err)
2097 6ced4176 2019-07-12 stsp goto done;
2098 6ced4176 2019-07-12 stsp
2099 6ced4176 2019-07-12 stsp free(in_repo_path);
2100 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2101 6ced4176 2019-07-12 stsp
2102 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2103 6ced4176 2019-07-12 stsp if (err)
2104 6ced4176 2019-07-12 stsp goto done;
2105 c932eeeb 2019-05-22 stsp
2106 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2107 6ced4176 2019-07-12 stsp /* Check out a single file. */
2108 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2109 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2110 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2111 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2112 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2113 6ced4176 2019-07-12 stsp goto done;
2114 6ced4176 2019-07-12 stsp }
2115 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2116 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2117 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2118 6ced4176 2019-07-12 stsp goto done;
2119 6ced4176 2019-07-12 stsp }
2120 6ced4176 2019-07-12 stsp } else {
2121 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2122 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2123 6ced4176 2019-07-12 stsp if (err)
2124 6ced4176 2019-07-12 stsp return err;
2125 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2126 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2127 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2128 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2129 6ced4176 2019-07-12 stsp goto done;
2130 6ced4176 2019-07-12 stsp }
2131 6ced4176 2019-07-12 stsp }
2132 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2133 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2134 6ced4176 2019-07-12 stsp } else {
2135 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2136 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2137 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2138 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2139 6ced4176 2019-07-12 stsp goto done;
2140 6ced4176 2019-07-12 stsp }
2141 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2142 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2143 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2144 6ced4176 2019-07-12 stsp goto done;
2145 6ced4176 2019-07-12 stsp }
2146 6ced4176 2019-07-12 stsp }
2147 6ced4176 2019-07-12 stsp done:
2148 6ced4176 2019-07-12 stsp free(id);
2149 6ced4176 2019-07-12 stsp free(in_repo_path);
2150 6ced4176 2019-07-12 stsp if (err) {
2151 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2152 6ced4176 2019-07-12 stsp free(*tree_relpath);
2153 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2154 6ced4176 2019-07-12 stsp free(*tree_id);
2155 6ced4176 2019-07-12 stsp *tree_id = NULL;
2156 6ced4176 2019-07-12 stsp }
2157 07ed472d 2019-07-12 stsp return err;
2158 07ed472d 2019-07-12 stsp }
2159 07ed472d 2019-07-12 stsp
2160 07ed472d 2019-07-12 stsp static const struct got_error *
2161 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2162 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2163 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2164 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2165 07ed472d 2019-07-12 stsp {
2166 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2167 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2168 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2169 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2170 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2171 07ed472d 2019-07-12 stsp
2172 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2173 7f47418f 2019-12-20 stsp if (err) {
2174 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2175 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2176 7f47418f 2019-12-20 stsp goto done;
2177 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2178 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2179 7f47418f 2019-12-20 stsp if (err)
2180 7f47418f 2019-12-20 stsp return err;
2181 7f47418f 2019-12-20 stsp }
2182 07ed472d 2019-07-12 stsp
2183 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2184 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2185 07ed472d 2019-07-12 stsp if (err)
2186 07ed472d 2019-07-12 stsp goto done;
2187 07ed472d 2019-07-12 stsp
2188 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2189 07ed472d 2019-07-12 stsp if (err)
2190 07ed472d 2019-07-12 stsp goto done;
2191 07ed472d 2019-07-12 stsp
2192 07ed472d 2019-07-12 stsp if (entry_name &&
2193 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2194 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2195 07ed472d 2019-07-12 stsp goto done;
2196 07ed472d 2019-07-12 stsp }
2197 07ed472d 2019-07-12 stsp
2198 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2199 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2200 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2201 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2202 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2203 07ed472d 2019-07-12 stsp arg.repo = repo;
2204 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2205 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2206 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2207 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2208 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2209 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2210 07ed472d 2019-07-12 stsp done:
2211 07ed472d 2019-07-12 stsp if (tree)
2212 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2213 07ed472d 2019-07-12 stsp if (commit)
2214 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2215 6ced4176 2019-07-12 stsp return err;
2216 6ced4176 2019-07-12 stsp }
2217 6ced4176 2019-07-12 stsp
2218 9d31a1d8 2018-03-11 stsp const struct got_error *
2219 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2220 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2221 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2222 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2223 9d31a1d8 2018-03-11 stsp {
2224 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2225 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2226 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2227 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2228 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2229 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2230 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2231 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2232 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2233 f2ea84fa 2019-07-27 stsp int entry_type;
2234 f2ea84fa 2019-07-27 stsp char *relpath;
2235 f2ea84fa 2019-07-27 stsp char *entry_name;
2236 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2237 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2238 9d31a1d8 2018-03-11 stsp
2239 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2240 f2ea84fa 2019-07-27 stsp
2241 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2242 9d31a1d8 2018-03-11 stsp if (err)
2243 9d31a1d8 2018-03-11 stsp return err;
2244 93a30277 2018-12-24 stsp
2245 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2246 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2247 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2248 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2249 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2250 f2ea84fa 2019-07-27 stsp goto done;
2251 f2ea84fa 2019-07-27 stsp }
2252 6ced4176 2019-07-12 stsp
2253 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2254 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2255 f2ea84fa 2019-07-27 stsp if (err) {
2256 f2ea84fa 2019-07-27 stsp free(tpd);
2257 6ced4176 2019-07-12 stsp goto done;
2258 6ced4176 2019-07-12 stsp }
2259 f2ea84fa 2019-07-27 stsp
2260 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2261 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2262 f2ea84fa 2019-07-27 stsp if (err) {
2263 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2264 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2265 f2ea84fa 2019-07-27 stsp free(tpd);
2266 f2ea84fa 2019-07-27 stsp goto done;
2267 f2ea84fa 2019-07-27 stsp }
2268 f2ea84fa 2019-07-27 stsp } else
2269 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2270 f2ea84fa 2019-07-27 stsp
2271 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2272 6ced4176 2019-07-12 stsp }
2273 6ced4176 2019-07-12 stsp
2274 51514078 2018-12-25 stsp /*
2275 51514078 2018-12-25 stsp * Read the file index.
2276 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2277 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2278 51514078 2018-12-25 stsp */
2279 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2280 8da9e5f4 2019-01-12 stsp if (err)
2281 c4cdcb68 2019-04-03 stsp goto done;
2282 c4cdcb68 2019-04-03 stsp
2283 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2284 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2285 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2286 f2ea84fa 2019-07-27 stsp
2287 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2288 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2289 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2290 f2ea84fa 2019-07-27 stsp if (err)
2291 f2ea84fa 2019-07-27 stsp break;
2292 f2ea84fa 2019-07-27 stsp
2293 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2294 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2295 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2296 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2297 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2298 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2299 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2300 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2301 f2ea84fa 2019-07-27 stsp if (err)
2302 f2ea84fa 2019-07-27 stsp break;
2303 f2ea84fa 2019-07-27 stsp
2304 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2305 711d9cd9 2019-07-12 stsp }
2306 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2307 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2308 af12c6b9 2019-06-04 stsp err = sync_err;
2309 9d31a1d8 2018-03-11 stsp done:
2310 9c6338c4 2019-06-02 stsp free(fileindex_path);
2311 edfa7d7f 2018-09-11 stsp if (tree)
2312 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2313 9d31a1d8 2018-03-11 stsp if (commit)
2314 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2315 5ade8233 2019-07-12 stsp if (fileindex)
2316 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2317 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2318 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2319 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2320 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2321 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2322 f2ea84fa 2019-07-27 stsp free(tpd);
2323 f2ea84fa 2019-07-27 stsp }
2324 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2325 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2326 9d31a1d8 2018-03-11 stsp err = unlockerr;
2327 f8d1f275 2019-02-04 stsp return err;
2328 f8d1f275 2019-02-04 stsp }
2329 f8d1f275 2019-02-04 stsp
2330 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2331 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2332 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2333 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2334 234035bc 2019-06-01 stsp void *progress_arg;
2335 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2336 234035bc 2019-06-01 stsp void *cancel_arg;
2337 f69721c3 2019-10-21 stsp const char *label_orig;
2338 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2339 234035bc 2019-06-01 stsp };
2340 234035bc 2019-06-01 stsp
2341 234035bc 2019-06-01 stsp static const struct got_error *
2342 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2343 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2344 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2345 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2346 234035bc 2019-06-01 stsp {
2347 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2348 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2349 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2350 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2351 234035bc 2019-06-01 stsp struct stat sb;
2352 234035bc 2019-06-01 stsp unsigned char status;
2353 234035bc 2019-06-01 stsp int local_changes_subsumed;
2354 234035bc 2019-06-01 stsp
2355 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2356 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2357 d6c87207 2019-08-02 stsp strlen(path2));
2358 1ee397ad 2019-07-12 stsp if (ie == NULL)
2359 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2360 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2361 234035bc 2019-06-01 stsp
2362 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2363 234035bc 2019-06-01 stsp path2) == -1)
2364 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2365 234035bc 2019-06-01 stsp
2366 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2367 7f91a133 2019-12-13 stsp repo);
2368 234035bc 2019-06-01 stsp if (err)
2369 234035bc 2019-06-01 stsp goto done;
2370 234035bc 2019-06-01 stsp
2371 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2372 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2373 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2374 234035bc 2019-06-01 stsp goto done;
2375 234035bc 2019-06-01 stsp }
2376 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2377 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2378 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2379 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2380 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2381 234035bc 2019-06-01 stsp goto done;
2382 234035bc 2019-06-01 stsp }
2383 234035bc 2019-06-01 stsp
2384 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2385 f69721c3 2019-10-21 stsp ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2386 f69721c3 2019-10-21 stsp a->commit_id2, repo, a->progress_cb, a->progress_arg);
2387 234035bc 2019-06-01 stsp } else if (blob1) {
2388 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2389 d6c87207 2019-08-02 stsp strlen(path1));
2390 1ee397ad 2019-07-12 stsp if (ie == NULL)
2391 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2392 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2393 2b92fad7 2019-06-02 stsp
2394 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2395 2b92fad7 2019-06-02 stsp path1) == -1)
2396 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2397 2b92fad7 2019-06-02 stsp
2398 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2399 7f91a133 2019-12-13 stsp repo);
2400 2b92fad7 2019-06-02 stsp if (err)
2401 2b92fad7 2019-06-02 stsp goto done;
2402 2b92fad7 2019-06-02 stsp
2403 2b92fad7 2019-06-02 stsp switch (status) {
2404 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2405 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2406 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2407 1ee397ad 2019-07-12 stsp if (err)
2408 1ee397ad 2019-07-12 stsp goto done;
2409 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2410 2b92fad7 2019-06-02 stsp if (err)
2411 2b92fad7 2019-06-02 stsp goto done;
2412 2b92fad7 2019-06-02 stsp if (ie)
2413 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2414 2b92fad7 2019-06-02 stsp break;
2415 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2416 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2417 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2418 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2419 1ee397ad 2019-07-12 stsp if (err)
2420 1ee397ad 2019-07-12 stsp goto done;
2421 2b92fad7 2019-06-02 stsp if (ie)
2422 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2423 2b92fad7 2019-06-02 stsp break;
2424 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2425 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2426 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2427 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2428 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2429 1ee397ad 2019-07-12 stsp if (err)
2430 1ee397ad 2019-07-12 stsp goto done;
2431 2b92fad7 2019-06-02 stsp break;
2432 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2433 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2434 1ee397ad 2019-07-12 stsp if (err)
2435 1ee397ad 2019-07-12 stsp goto done;
2436 2b92fad7 2019-06-02 stsp break;
2437 2b92fad7 2019-06-02 stsp default:
2438 2b92fad7 2019-06-02 stsp break;
2439 2b92fad7 2019-06-02 stsp }
2440 234035bc 2019-06-01 stsp } else if (blob2) {
2441 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2442 234035bc 2019-06-01 stsp path2) == -1)
2443 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2444 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2445 d6c87207 2019-08-02 stsp strlen(path2));
2446 234035bc 2019-06-01 stsp if (ie) {
2447 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2448 7f91a133 2019-12-13 stsp -1, NULL, repo);
2449 234035bc 2019-06-01 stsp if (err)
2450 234035bc 2019-06-01 stsp goto done;
2451 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2452 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2453 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2454 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2455 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2456 1ee397ad 2019-07-12 stsp status, path2);
2457 234035bc 2019-06-01 stsp goto done;
2458 234035bc 2019-06-01 stsp }
2459 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2460 f69721c3 2019-10-21 stsp NULL, ondisk_path, path2, sb.st_mode,
2461 f69721c3 2019-10-21 stsp a->label_orig, blob2, a->commit_id2, repo,
2462 f69721c3 2019-10-21 stsp a->progress_cb,
2463 f69721c3 2019-10-21 stsp a->progress_arg);
2464 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2465 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2466 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2467 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2468 234035bc 2019-06-01 stsp if (err)
2469 234035bc 2019-06-01 stsp goto done;
2470 234035bc 2019-06-01 stsp }
2471 234035bc 2019-06-01 stsp } else {
2472 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2473 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2474 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2475 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2476 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2477 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2478 234035bc 2019-06-01 stsp if (err)
2479 234035bc 2019-06-01 stsp goto done;
2480 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2481 234035bc 2019-06-01 stsp if (err)
2482 234035bc 2019-06-01 stsp goto done;
2483 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2484 3969253a 2020-03-07 stsp NULL, NULL, 1);
2485 3969253a 2020-03-07 stsp if (err) {
2486 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2487 3969253a 2020-03-07 stsp goto done;
2488 3969253a 2020-03-07 stsp }
2489 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2490 2b92fad7 2019-06-02 stsp if (err) {
2491 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2492 2b92fad7 2019-06-02 stsp goto done;
2493 2b92fad7 2019-06-02 stsp }
2494 234035bc 2019-06-01 stsp }
2495 234035bc 2019-06-01 stsp }
2496 234035bc 2019-06-01 stsp done:
2497 234035bc 2019-06-01 stsp free(ondisk_path);
2498 234035bc 2019-06-01 stsp return err;
2499 234035bc 2019-06-01 stsp }
2500 234035bc 2019-06-01 stsp
2501 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2502 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2503 234035bc 2019-06-01 stsp struct got_repository *repo;
2504 234035bc 2019-06-01 stsp };
2505 234035bc 2019-06-01 stsp
2506 234035bc 2019-06-01 stsp static const struct got_error *
2507 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2508 234035bc 2019-06-01 stsp {
2509 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2510 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2511 234035bc 2019-06-01 stsp unsigned char status;
2512 234035bc 2019-06-01 stsp struct stat sb;
2513 234035bc 2019-06-01 stsp char *ondisk_path;
2514 234035bc 2019-06-01 stsp
2515 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2516 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2517 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2518 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2519 234035bc 2019-06-01 stsp
2520 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2521 234035bc 2019-06-01 stsp == -1)
2522 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2523 234035bc 2019-06-01 stsp
2524 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2525 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2526 234035bc 2019-06-01 stsp if (err)
2527 234035bc 2019-06-01 stsp return err;
2528 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2529 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2530 234035bc 2019-06-01 stsp
2531 234035bc 2019-06-01 stsp return NULL;
2532 234035bc 2019-06-01 stsp }
2533 234035bc 2019-06-01 stsp
2534 818c7501 2019-07-11 stsp static const struct got_error *
2535 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2536 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2537 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2538 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2539 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2540 234035bc 2019-06-01 stsp {
2541 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2542 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2543 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2544 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2545 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2546 234035bc 2019-06-01 stsp
2547 03415a1a 2019-06-02 stsp if (commit_id1) {
2548 f69721c3 2019-10-21 stsp char *id_str;
2549 f69721c3 2019-10-21 stsp
2550 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2551 03415a1a 2019-06-02 stsp worktree->path_prefix);
2552 03415a1a 2019-06-02 stsp if (err)
2553 03415a1a 2019-06-02 stsp goto done;
2554 03415a1a 2019-06-02 stsp
2555 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2556 03415a1a 2019-06-02 stsp if (err)
2557 03415a1a 2019-06-02 stsp goto done;
2558 f69721c3 2019-10-21 stsp
2559 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2560 f69721c3 2019-10-21 stsp if (err)
2561 f69721c3 2019-10-21 stsp goto done;
2562 f69721c3 2019-10-21 stsp
2563 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2564 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2565 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2566 f69721c3 2019-10-21 stsp free(id_str);
2567 f69721c3 2019-10-21 stsp goto done;
2568 f69721c3 2019-10-21 stsp }
2569 f69721c3 2019-10-21 stsp free(id_str);
2570 03415a1a 2019-06-02 stsp }
2571 234035bc 2019-06-01 stsp
2572 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2573 234035bc 2019-06-01 stsp worktree->path_prefix);
2574 234035bc 2019-06-01 stsp if (err)
2575 234035bc 2019-06-01 stsp goto done;
2576 234035bc 2019-06-01 stsp
2577 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2578 234035bc 2019-06-01 stsp if (err)
2579 234035bc 2019-06-01 stsp goto done;
2580 234035bc 2019-06-01 stsp
2581 234035bc 2019-06-01 stsp arg.worktree = worktree;
2582 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2583 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2584 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2585 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2586 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2587 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2588 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2589 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2590 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2591 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2592 af12c6b9 2019-06-04 stsp err = sync_err;
2593 234035bc 2019-06-01 stsp done:
2594 234035bc 2019-06-01 stsp if (tree1)
2595 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2596 234035bc 2019-06-01 stsp if (tree2)
2597 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2598 f69721c3 2019-10-21 stsp free(label_orig);
2599 818c7501 2019-07-11 stsp return err;
2600 818c7501 2019-07-11 stsp }
2601 234035bc 2019-06-01 stsp
2602 818c7501 2019-07-11 stsp const struct got_error *
2603 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2604 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2605 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2606 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2607 818c7501 2019-07-11 stsp {
2608 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2609 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2610 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2611 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2612 818c7501 2019-07-11 stsp
2613 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2614 818c7501 2019-07-11 stsp if (err)
2615 818c7501 2019-07-11 stsp return err;
2616 818c7501 2019-07-11 stsp
2617 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2618 818c7501 2019-07-11 stsp if (err)
2619 818c7501 2019-07-11 stsp goto done;
2620 818c7501 2019-07-11 stsp
2621 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2622 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2623 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2624 818c7501 2019-07-11 stsp &mok_arg);
2625 818c7501 2019-07-11 stsp if (err)
2626 818c7501 2019-07-11 stsp goto done;
2627 818c7501 2019-07-11 stsp
2628 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2629 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2630 818c7501 2019-07-11 stsp done:
2631 818c7501 2019-07-11 stsp if (fileindex)
2632 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2633 818c7501 2019-07-11 stsp free(fileindex_path);
2634 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2635 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2636 234035bc 2019-06-01 stsp err = unlockerr;
2637 234035bc 2019-06-01 stsp return err;
2638 234035bc 2019-06-01 stsp }
2639 234035bc 2019-06-01 stsp
2640 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2641 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2642 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2643 927df6b7 2019-02-10 stsp const char *status_path;
2644 927df6b7 2019-02-10 stsp size_t status_path_len;
2645 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2646 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2647 f8d1f275 2019-02-04 stsp void *status_arg;
2648 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2649 f8d1f275 2019-02-04 stsp void *cancel_arg;
2650 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
2651 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
2652 f2a9dc41 2019-12-13 tracey int report_unchanged;
2653 3143d852 2020-06-25 stsp int no_ignores;
2654 f8d1f275 2019-02-04 stsp };
2655 88d0e355 2019-08-03 stsp
2656 f8d1f275 2019-02-04 stsp static const struct got_error *
2657 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2658 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
2659 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2660 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
2661 927df6b7 2019-02-10 stsp {
2662 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2663 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2664 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2665 927df6b7 2019-02-10 stsp struct stat sb;
2666 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2667 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2668 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2669 927df6b7 2019-02-10 stsp
2670 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2671 98eaaa12 2019-08-03 stsp if (err)
2672 98eaaa12 2019-08-03 stsp return err;
2673 98eaaa12 2019-08-03 stsp
2674 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2675 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2676 98eaaa12 2019-08-03 stsp return NULL;
2677 98eaaa12 2019-08-03 stsp
2678 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2679 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2680 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2681 98eaaa12 2019-08-03 stsp }
2682 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2683 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2684 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2685 927df6b7 2019-02-10 stsp }
2686 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2687 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2688 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2689 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2690 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2691 98eaaa12 2019-08-03 stsp }
2692 98eaaa12 2019-08-03 stsp
2693 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2694 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2695 927df6b7 2019-02-10 stsp }
2696 927df6b7 2019-02-10 stsp
2697 927df6b7 2019-02-10 stsp static const struct got_error *
2698 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2699 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
2700 f8d1f275 2019-02-04 stsp {
2701 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2702 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2703 f8d1f275 2019-02-04 stsp char *abspath;
2704 f8d1f275 2019-02-04 stsp
2705 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2706 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2707 0584f854 2019-04-06 stsp
2708 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2709 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2710 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2711 927df6b7 2019-02-10 stsp return NULL;
2712 927df6b7 2019-02-10 stsp
2713 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2714 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2715 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2716 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2717 f8d1f275 2019-02-04 stsp } else {
2718 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2719 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2720 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2721 f8d1f275 2019-02-04 stsp }
2722 f8d1f275 2019-02-04 stsp
2723 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
2724 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2725 f8d1f275 2019-02-04 stsp free(abspath);
2726 9d31a1d8 2018-03-11 stsp return err;
2727 9d31a1d8 2018-03-11 stsp }
2728 f8d1f275 2019-02-04 stsp
2729 f8d1f275 2019-02-04 stsp static const struct got_error *
2730 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2731 f8d1f275 2019-02-04 stsp {
2732 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2733 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2734 2ec1f75b 2019-03-26 stsp unsigned char status;
2735 927df6b7 2019-02-10 stsp
2736 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2737 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2738 0584f854 2019-04-06 stsp
2739 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2740 927df6b7 2019-02-10 stsp return NULL;
2741 927df6b7 2019-02-10 stsp
2742 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2743 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2744 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2745 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2746 2ec1f75b 2019-03-26 stsp else
2747 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2748 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2749 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2750 6841da00 2019-08-08 stsp }
2751 6841da00 2019-08-08 stsp
2752 6841da00 2019-08-08 stsp void
2753 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
2754 6841da00 2019-08-08 stsp {
2755 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2756 6841da00 2019-08-08 stsp
2757 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
2758 6841da00 2019-08-08 stsp free((char *)pe->path);
2759 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
2760 6841da00 2019-08-08 stsp }
2761 6841da00 2019-08-08 stsp
2762 6841da00 2019-08-08 stsp void
2763 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
2764 6841da00 2019-08-08 stsp {
2765 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2766 6841da00 2019-08-08 stsp
2767 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
2768 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2769 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2770 6841da00 2019-08-08 stsp free((char *)pe->path);
2771 6841da00 2019-08-08 stsp }
2772 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
2773 6841da00 2019-08-08 stsp }
2774 6841da00 2019-08-08 stsp
2775 6841da00 2019-08-08 stsp static const struct got_error *
2776 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2777 6841da00 2019-08-08 stsp {
2778 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2779 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
2780 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
2781 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
2782 6841da00 2019-08-08 stsp size_t linesize = 0;
2783 6841da00 2019-08-08 stsp ssize_t linelen;
2784 6841da00 2019-08-08 stsp
2785 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
2786 6841da00 2019-08-08 stsp if (ignorelist == NULL)
2787 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
2788 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
2789 6841da00 2019-08-08 stsp
2790 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
2791 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
2792 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
2793 bd8de430 2019-10-04 stsp
2794 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
2795 bd8de430 2019-10-04 stsp if (line[0] == '#')
2796 bd8de430 2019-10-04 stsp continue;
2797 bd8de430 2019-10-04 stsp
2798 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
2799 bd8de430 2019-10-04 stsp if (line[0] == '!')
2800 bd8de430 2019-10-04 stsp continue;
2801 bd8de430 2019-10-04 stsp
2802 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2803 6841da00 2019-08-08 stsp line) == -1) {
2804 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
2805 6841da00 2019-08-08 stsp goto done;
2806 6841da00 2019-08-08 stsp }
2807 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2808 6841da00 2019-08-08 stsp if (err)
2809 6841da00 2019-08-08 stsp goto done;
2810 6841da00 2019-08-08 stsp }
2811 6841da00 2019-08-08 stsp if (ferror(f)) {
2812 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
2813 6841da00 2019-08-08 stsp goto done;
2814 6841da00 2019-08-08 stsp }
2815 6841da00 2019-08-08 stsp
2816 6841da00 2019-08-08 stsp dirpath = strdup(path);
2817 6841da00 2019-08-08 stsp if (dirpath == NULL) {
2818 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
2819 6841da00 2019-08-08 stsp goto done;
2820 6841da00 2019-08-08 stsp }
2821 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2822 6841da00 2019-08-08 stsp done:
2823 6841da00 2019-08-08 stsp free(line);
2824 6841da00 2019-08-08 stsp if (err || pe == NULL) {
2825 6841da00 2019-08-08 stsp free(dirpath);
2826 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2827 6841da00 2019-08-08 stsp }
2828 6841da00 2019-08-08 stsp return err;
2829 6841da00 2019-08-08 stsp }
2830 6841da00 2019-08-08 stsp
2831 6841da00 2019-08-08 stsp int
2832 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
2833 6841da00 2019-08-08 stsp {
2834 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2835 bd8de430 2019-10-04 stsp
2836 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
2837 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
2838 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
2839 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
2840 bd8de430 2019-10-04 stsp
2841 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
2842 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
2843 6841da00 2019-08-08 stsp
2844 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
2845 bd8de430 2019-10-04 stsp continue;
2846 bd8de430 2019-10-04 stsp pattern += 3;
2847 bd8de430 2019-10-04 stsp p = path;
2848 bd8de430 2019-10-04 stsp while (*p) {
2849 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
2850 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
2851 bd8de430 2019-10-04 stsp /* Retry in next directory. */
2852 bd8de430 2019-10-04 stsp while (*p && *p != '/')
2853 bd8de430 2019-10-04 stsp p++;
2854 bd8de430 2019-10-04 stsp while (*p == '/')
2855 bd8de430 2019-10-04 stsp p++;
2856 bd8de430 2019-10-04 stsp continue;
2857 bd8de430 2019-10-04 stsp }
2858 bd8de430 2019-10-04 stsp return 1;
2859 bd8de430 2019-10-04 stsp }
2860 bd8de430 2019-10-04 stsp }
2861 bd8de430 2019-10-04 stsp }
2862 bd8de430 2019-10-04 stsp
2863 6841da00 2019-08-08 stsp /*
2864 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
2865 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
2866 6841da00 2019-08-08 stsp * ignores backwards.
2867 6841da00 2019-08-08 stsp */
2868 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
2869 6841da00 2019-08-08 stsp while (pe) {
2870 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
2871 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2872 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
2873 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
2874 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
2875 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
2876 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
2877 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
2878 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
2879 6841da00 2019-08-08 stsp continue;
2880 6841da00 2019-08-08 stsp return 1;
2881 6841da00 2019-08-08 stsp }
2882 6841da00 2019-08-08 stsp }
2883 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2884 6841da00 2019-08-08 stsp }
2885 6841da00 2019-08-08 stsp
2886 6841da00 2019-08-08 stsp return 0;
2887 6841da00 2019-08-08 stsp }
2888 6841da00 2019-08-08 stsp
2889 6841da00 2019-08-08 stsp static const struct got_error *
2890 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2891 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
2892 6841da00 2019-08-08 stsp {
2893 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2894 6841da00 2019-08-08 stsp char *ignorespath;
2895 886cec17 2019-12-15 stsp int fd = -1;
2896 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
2897 6841da00 2019-08-08 stsp
2898 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2899 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
2900 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
2901 6841da00 2019-08-08 stsp
2902 886cec17 2019-12-15 stsp if (dirfd != -1) {
2903 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2904 886cec17 2019-12-15 stsp if (fd == -1) {
2905 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2906 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
2907 886cec17 2019-12-15 stsp ignorespath);
2908 886cec17 2019-12-15 stsp } else {
2909 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
2910 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
2911 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
2912 886cec17 2019-12-15 stsp ignorespath);
2913 886cec17 2019-12-15 stsp else {
2914 886cec17 2019-12-15 stsp fd = -1;
2915 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
2916 886cec17 2019-12-15 stsp }
2917 886cec17 2019-12-15 stsp }
2918 886cec17 2019-12-15 stsp } else {
2919 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
2920 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
2921 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2922 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
2923 886cec17 2019-12-15 stsp ignorespath);
2924 886cec17 2019-12-15 stsp } else
2925 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
2926 886cec17 2019-12-15 stsp }
2927 6841da00 2019-08-08 stsp
2928 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2929 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
2930 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2931 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
2932 6841da00 2019-08-08 stsp free(ignorespath);
2933 6841da00 2019-08-08 stsp return err;
2934 f8d1f275 2019-02-04 stsp }
2935 f8d1f275 2019-02-04 stsp
2936 f8d1f275 2019-02-04 stsp static const struct got_error *
2937 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2938 f8d1f275 2019-02-04 stsp {
2939 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2940 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2941 f8d1f275 2019-02-04 stsp char *path = NULL;
2942 f8d1f275 2019-02-04 stsp
2943 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2944 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2945 0584f854 2019-04-06 stsp
2946 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2947 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2948 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2949 f8d1f275 2019-02-04 stsp } else {
2950 f8d1f275 2019-02-04 stsp path = de->d_name;
2951 f8d1f275 2019-02-04 stsp }
2952 f8d1f275 2019-02-04 stsp
2953 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
2954 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
2955 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
2956 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2957 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2958 f8d1f275 2019-02-04 stsp if (parent_path[0])
2959 f8d1f275 2019-02-04 stsp free(path);
2960 b72f483a 2019-02-05 stsp return err;
2961 f8d1f275 2019-02-04 stsp }
2962 f8d1f275 2019-02-04 stsp
2963 347d1d3e 2019-07-12 stsp static const struct got_error *
2964 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
2965 3143d852 2020-06-25 stsp {
2966 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
2967 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
2968 3143d852 2020-06-25 stsp
2969 3143d852 2020-06-25 stsp if (a->no_ignores)
2970 3143d852 2020-06-25 stsp return NULL;
2971 3143d852 2020-06-25 stsp
2972 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
2973 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
2974 3143d852 2020-06-25 stsp if (err)
2975 3143d852 2020-06-25 stsp return err;
2976 3143d852 2020-06-25 stsp
2977 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
2978 3143d852 2020-06-25 stsp dirfd, ".gitignore");
2979 3143d852 2020-06-25 stsp
2980 3143d852 2020-06-25 stsp return err;
2981 3143d852 2020-06-25 stsp }
2982 3143d852 2020-06-25 stsp
2983 3143d852 2020-06-25 stsp static const struct got_error *
2984 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2985 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2986 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
2987 abb4604f 2019-07-27 stsp {
2988 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2989 abb4604f 2019-07-27 stsp struct stat sb;
2990 abb4604f 2019-07-27 stsp
2991 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2992 abb4604f 2019-07-27 stsp if (ie)
2993 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
2994 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
2995 abb4604f 2019-07-27 stsp
2996 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2997 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2998 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2999 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3000 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3001 abb4604f 2019-07-27 stsp return NULL;
3002 abb4604f 2019-07-27 stsp }
3003 abb4604f 2019-07-27 stsp
3004 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3005 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3006 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3007 abb4604f 2019-07-27 stsp
3008 abb4604f 2019-07-27 stsp return NULL;
3009 3143d852 2020-06-25 stsp }
3010 3143d852 2020-06-25 stsp
3011 3143d852 2020-06-25 stsp static const struct got_error *
3012 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3013 3143d852 2020-06-25 stsp const char *root_path, const char *path)
3014 3143d852 2020-06-25 stsp {
3015 3143d852 2020-06-25 stsp const struct got_error *err;
3016 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
3017 3143d852 2020-06-25 stsp
3018 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3019 3143d852 2020-06-25 stsp ".cvsignore");
3020 3143d852 2020-06-25 stsp if (err)
3021 3143d852 2020-06-25 stsp return err;
3022 3143d852 2020-06-25 stsp
3023 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
3024 3143d852 2020-06-25 stsp ".gitignore");
3025 3143d852 2020-06-25 stsp if (err)
3026 3143d852 2020-06-25 stsp return err;
3027 3143d852 2020-06-25 stsp
3028 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
3029 3143d852 2020-06-25 stsp if (err) {
3030 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
3031 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
3032 3143d852 2020-06-25 stsp return err;
3033 3143d852 2020-06-25 stsp }
3034 3143d852 2020-06-25 stsp for (;;) {
3035 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3036 3143d852 2020-06-25 stsp ".cvsignore");
3037 3143d852 2020-06-25 stsp if (err)
3038 3143d852 2020-06-25 stsp break;
3039 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
3040 3143d852 2020-06-25 stsp ".gitignore");
3041 3143d852 2020-06-25 stsp if (err)
3042 3143d852 2020-06-25 stsp break;
3043 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
3044 3143d852 2020-06-25 stsp if (err) {
3045 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
3046 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
3047 3143d852 2020-06-25 stsp break;
3048 3143d852 2020-06-25 stsp }
3049 b737c85a 2020-06-26 stsp free(parent_path);
3050 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
3051 b737c85a 2020-06-26 stsp next_parent_path = NULL;
3052 3143d852 2020-06-25 stsp }
3053 3143d852 2020-06-25 stsp
3054 b737c85a 2020-06-26 stsp free(parent_path);
3055 b737c85a 2020-06-26 stsp free(next_parent_path);
3056 3143d852 2020-06-25 stsp return err;
3057 abb4604f 2019-07-27 stsp }
3058 abb4604f 2019-07-27 stsp
3059 abb4604f 2019-07-27 stsp static const struct got_error *
3060 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
3061 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
3062 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3063 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3064 f2a9dc41 2019-12-13 tracey int report_unchanged)
3065 f8d1f275 2019-02-04 stsp {
3066 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
3067 6fc93f37 2019-12-13 stsp int fd = -1;
3068 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
3069 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
3070 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
3071 3143d852 2020-06-25 stsp
3072 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
3073 f8d1f275 2019-02-04 stsp
3074 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
3075 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
3076 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
3077 8dc303cc 2019-07-27 stsp
3078 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3079 6fc93f37 2019-12-13 stsp if (fd == -1) {
3080 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3081 00bb5ea0 2020-07-23 stsp errno != ELOOP)
3082 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
3083 abb4604f 2019-07-27 stsp else
3084 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
3085 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
3086 f2a9dc41 2019-12-13 tracey report_unchanged);
3087 abb4604f 2019-07-27 stsp } else {
3088 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
3089 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
3090 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
3091 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
3092 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3093 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3094 abb4604f 2019-07-27 stsp arg.status_path = path;
3095 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3096 abb4604f 2019-07-27 stsp arg.repo = repo;
3097 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3098 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3099 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3100 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3101 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3102 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3103 022fae89 2019-12-06 tracey if (!no_ignores) {
3104 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3105 3143d852 2020-06-25 stsp worktree->root_path, path);
3106 3143d852 2020-06-25 stsp if (err)
3107 3143d852 2020-06-25 stsp goto done;
3108 022fae89 2019-12-06 tracey }
3109 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3110 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3111 927df6b7 2019-02-10 stsp }
3112 3143d852 2020-06-25 stsp done:
3113 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3114 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3115 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3116 927df6b7 2019-02-10 stsp free(ondisk_path);
3117 347d1d3e 2019-07-12 stsp return err;
3118 347d1d3e 2019-07-12 stsp }
3119 347d1d3e 2019-07-12 stsp
3120 347d1d3e 2019-07-12 stsp const struct got_error *
3121 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3122 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3123 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3124 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3125 347d1d3e 2019-07-12 stsp {
3126 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3127 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3128 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3129 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3130 347d1d3e 2019-07-12 stsp
3131 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3132 347d1d3e 2019-07-12 stsp if (err)
3133 347d1d3e 2019-07-12 stsp return err;
3134 347d1d3e 2019-07-12 stsp
3135 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3136 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3137 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3138 72ea6654 2019-07-27 stsp if (err)
3139 72ea6654 2019-07-27 stsp break;
3140 72ea6654 2019-07-27 stsp }
3141 f8d1f275 2019-02-04 stsp free(fileindex_path);
3142 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3143 f8d1f275 2019-02-04 stsp return err;
3144 f8d1f275 2019-02-04 stsp }
3145 6c7ab921 2019-03-18 stsp
3146 6c7ab921 2019-03-18 stsp const struct got_error *
3147 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3148 6c7ab921 2019-03-18 stsp const char *arg)
3149 6c7ab921 2019-03-18 stsp {
3150 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3151 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3152 6c7ab921 2019-03-18 stsp size_t len;
3153 00bb5ea0 2020-07-23 stsp struct stat sb;
3154 6c7ab921 2019-03-18 stsp
3155 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3156 6c7ab921 2019-03-18 stsp
3157 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3158 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3159 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3160 00bb5ea0 2020-07-23 stsp
3161 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3162 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3163 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3164 00bb5ea0 2020-07-23 stsp goto done;
3165 00bb5ea0 2020-07-23 stsp }
3166 00bb5ea0 2020-07-23 stsp }
3167 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3168 00bb5ea0 2020-07-23 stsp /*
3169 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3170 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3171 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3172 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3173 00bb5ea0 2020-07-23 stsp */
3174 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3175 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3176 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3177 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3178 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3179 00bb5ea0 2020-07-23 stsp goto done;
3180 00bb5ea0 2020-07-23 stsp }
3181 00bb5ea0 2020-07-23 stsp
3182 00bb5ea0 2020-07-23 stsp }
3183 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3184 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3185 00bb5ea0 2020-07-23 stsp if (err)
3186 d0710d08 2019-07-22 stsp goto done;
3187 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3188 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3189 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3190 00bb5ea0 2020-07-23 stsp goto done;
3191 00bb5ea0 2020-07-23 stsp }
3192 00bb5ea0 2020-07-23 stsp } else {
3193 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3194 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3195 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3196 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3197 00bb5ea0 2020-07-23 stsp goto done;
3198 00bb5ea0 2020-07-23 stsp }
3199 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3200 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3201 00bb5ea0 2020-07-23 stsp goto done;
3202 00bb5ea0 2020-07-23 stsp }
3203 d0710d08 2019-07-22 stsp }
3204 d0710d08 2019-07-22 stsp }
3205 6c7ab921 2019-03-18 stsp
3206 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3207 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3208 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3209 6c7ab921 2019-03-18 stsp goto done;
3210 6c7ab921 2019-03-18 stsp }
3211 6c7ab921 2019-03-18 stsp
3212 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3213 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3214 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3215 f6d88e1a 2019-05-29 stsp if (err)
3216 f6d88e1a 2019-05-29 stsp goto done;
3217 f6d88e1a 2019-05-29 stsp } else {
3218 f6d88e1a 2019-05-29 stsp path = strdup("");
3219 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3220 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3221 f6d88e1a 2019-05-29 stsp goto done;
3222 f6d88e1a 2019-05-29 stsp }
3223 6c7ab921 2019-03-18 stsp }
3224 6c7ab921 2019-03-18 stsp
3225 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3226 6c7ab921 2019-03-18 stsp len = strlen(path);
3227 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3228 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3229 6c7ab921 2019-03-18 stsp len--;
3230 6c7ab921 2019-03-18 stsp }
3231 6c7ab921 2019-03-18 stsp done:
3232 6c7ab921 2019-03-18 stsp free(resolved);
3233 d0710d08 2019-07-22 stsp free(cwd);
3234 6c7ab921 2019-03-18 stsp if (err == NULL)
3235 6c7ab921 2019-03-18 stsp *wt_path = path;
3236 6c7ab921 2019-03-18 stsp else
3237 6c7ab921 2019-03-18 stsp free(path);
3238 d00136be 2019-03-26 stsp return err;
3239 d00136be 2019-03-26 stsp }
3240 d00136be 2019-03-26 stsp
3241 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3242 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3243 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3244 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3245 4e68cba3 2019-11-23 stsp void *progress_arg;
3246 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3247 4e68cba3 2019-11-23 stsp };
3248 4e68cba3 2019-11-23 stsp
3249 4e68cba3 2019-11-23 stsp static const struct got_error *
3250 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3251 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3252 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3253 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3254 07f5b47a 2019-06-02 stsp {
3255 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3256 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3257 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3258 6d022e97 2019-08-04 stsp struct stat sb;
3259 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3260 07f5b47a 2019-06-02 stsp
3261 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3262 dbb83fbd 2019-12-12 stsp relpath) == -1)
3263 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3264 dbb83fbd 2019-12-12 stsp
3265 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3266 6d022e97 2019-08-04 stsp if (ie) {
3267 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3268 12463d8b 2019-12-13 stsp de_name, a->repo);
3269 6d022e97 2019-08-04 stsp if (err)
3270 dbb83fbd 2019-12-12 stsp goto done;
3271 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3272 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3273 dbb83fbd 2019-12-12 stsp goto done;
3274 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3275 dbb83fbd 2019-12-12 stsp if (err)
3276 dbb83fbd 2019-12-12 stsp goto done;
3277 6d022e97 2019-08-04 stsp }
3278 07f5b47a 2019-06-02 stsp
3279 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3280 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3281 dbb83fbd 2019-12-12 stsp goto done;
3282 4e68cba3 2019-11-23 stsp }
3283 07f5b47a 2019-06-02 stsp
3284 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3285 4e68cba3 2019-11-23 stsp if (err)
3286 4e68cba3 2019-11-23 stsp goto done;
3287 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3288 3969253a 2020-03-07 stsp if (err) {
3289 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3290 3969253a 2020-03-07 stsp goto done;
3291 3969253a 2020-03-07 stsp }
3292 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3293 07f5b47a 2019-06-02 stsp if (err) {
3294 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3295 4e68cba3 2019-11-23 stsp goto done;
3296 07f5b47a 2019-06-02 stsp }
3297 4e68cba3 2019-11-23 stsp done:
3298 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3299 dbb83fbd 2019-12-12 stsp if (err)
3300 dbb83fbd 2019-12-12 stsp return err;
3301 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3302 dbb83fbd 2019-12-12 stsp return NULL;
3303 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3304 07f5b47a 2019-06-02 stsp }
3305 07f5b47a 2019-06-02 stsp
3306 d00136be 2019-03-26 stsp const struct got_error *
3307 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3308 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3309 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3310 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3311 d00136be 2019-03-26 stsp {
3312 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3313 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3314 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3315 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3316 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3317 d00136be 2019-03-26 stsp
3318 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3319 d00136be 2019-03-26 stsp if (err)
3320 d00136be 2019-03-26 stsp return err;
3321 d00136be 2019-03-26 stsp
3322 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3323 d00136be 2019-03-26 stsp if (err)
3324 d00136be 2019-03-26 stsp goto done;
3325 d00136be 2019-03-26 stsp
3326 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3327 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3328 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3329 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3330 4e68cba3 2019-11-23 stsp saa.repo = repo;
3331 4e68cba3 2019-11-23 stsp
3332 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3333 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3334 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3335 1dd54920 2019-05-11 stsp if (err)
3336 af12c6b9 2019-06-04 stsp break;
3337 1dd54920 2019-05-11 stsp }
3338 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3339 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3340 af12c6b9 2019-06-04 stsp err = sync_err;
3341 d00136be 2019-03-26 stsp done:
3342 fb399478 2019-07-12 stsp free(fileindex_path);
3343 d00136be 2019-03-26 stsp if (fileindex)
3344 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3345 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3346 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3347 d00136be 2019-03-26 stsp err = unlockerr;
3348 6c7ab921 2019-03-18 stsp return err;
3349 6c7ab921 2019-03-18 stsp }
3350 17ed4618 2019-06-02 stsp
3351 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3352 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3353 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3354 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3355 f2a9dc41 2019-12-13 tracey void *progress_arg;
3356 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3357 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3358 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3359 f2a9dc41 2019-12-13 tracey };
3360 f2a9dc41 2019-12-13 tracey
3361 f2a9dc41 2019-12-13 tracey static const struct got_error *
3362 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3363 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3364 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3365 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3366 17ed4618 2019-06-02 stsp {
3367 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3368 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3369 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3370 17ed4618 2019-06-02 stsp struct stat sb;
3371 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3372 17ed4618 2019-06-02 stsp
3373 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3374 17ed4618 2019-06-02 stsp if (ie == NULL)
3375 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3376 2ec1f75b 2019-03-26 stsp
3377 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3378 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3379 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3380 9acbc4fa 2019-08-03 stsp return NULL;
3381 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3382 9acbc4fa 2019-08-03 stsp }
3383 9acbc4fa 2019-08-03 stsp
3384 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3385 f2a9dc41 2019-12-13 tracey relpath) == -1)
3386 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3387 f2a9dc41 2019-12-13 tracey
3388 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3389 12463d8b 2019-12-13 stsp a->repo);
3390 17ed4618 2019-06-02 stsp if (err)
3391 f2a9dc41 2019-12-13 tracey goto done;
3392 17ed4618 2019-06-02 stsp
3393 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3394 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3395 f2a9dc41 2019-12-13 tracey goto done;
3396 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3397 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3398 f2a9dc41 2019-12-13 tracey goto done;
3399 f2a9dc41 2019-12-13 tracey }
3400 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3401 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3402 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3403 f2a9dc41 2019-12-13 tracey goto done;
3404 f2a9dc41 2019-12-13 tracey }
3405 17ed4618 2019-06-02 stsp }
3406 17ed4618 2019-06-02 stsp
3407 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3408 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3409 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3410 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3411 12463d8b 2019-12-13 stsp ondisk_path);
3412 12463d8b 2019-12-13 stsp goto done;
3413 12463d8b 2019-12-13 stsp }
3414 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3415 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3416 12463d8b 2019-12-13 stsp goto done;
3417 15341bfd 2020-03-05 tracey }
3418 15341bfd 2020-03-05 tracey
3419 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3420 15341bfd 2020-03-05 tracey
3421 15341bfd 2020-03-05 tracey if (parent == NULL) {
3422 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3423 15341bfd 2020-03-05 tracey goto done;
3424 15341bfd 2020-03-05 tracey }
3425 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3426 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3427 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3428 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3429 15341bfd 2020-03-05 tracey parent);
3430 15341bfd 2020-03-05 tracey break;
3431 15341bfd 2020-03-05 tracey }
3432 15341bfd 2020-03-05 tracey parent = dirname(parent);
3433 15341bfd 2020-03-05 tracey if (parent == NULL) {
3434 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3435 15341bfd 2020-03-05 tracey goto done;
3436 15341bfd 2020-03-05 tracey }
3437 12463d8b 2019-12-13 stsp }
3438 f2a9dc41 2019-12-13 tracey }
3439 17ed4618 2019-06-02 stsp
3440 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3441 f2a9dc41 2019-12-13 tracey done:
3442 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3443 f2a9dc41 2019-12-13 tracey if (err)
3444 f2a9dc41 2019-12-13 tracey return err;
3445 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3446 f2a9dc41 2019-12-13 tracey return NULL;
3447 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3448 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3449 17ed4618 2019-06-02 stsp }
3450 17ed4618 2019-06-02 stsp
3451 2ec1f75b 2019-03-26 stsp const struct got_error *
3452 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3453 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3454 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3455 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3456 2ec1f75b 2019-03-26 stsp {
3457 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3458 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3459 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3460 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3461 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3462 2ec1f75b 2019-03-26 stsp
3463 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3464 2ec1f75b 2019-03-26 stsp if (err)
3465 2ec1f75b 2019-03-26 stsp return err;
3466 2ec1f75b 2019-03-26 stsp
3467 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3468 2ec1f75b 2019-03-26 stsp if (err)
3469 2ec1f75b 2019-03-26 stsp goto done;
3470 f2a9dc41 2019-12-13 tracey
3471 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3472 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3473 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3474 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3475 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3476 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3477 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3478 2ec1f75b 2019-03-26 stsp
3479 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3480 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3481 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3482 17ed4618 2019-06-02 stsp if (err)
3483 af12c6b9 2019-06-04 stsp break;
3484 2ec1f75b 2019-03-26 stsp }
3485 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3486 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3487 af12c6b9 2019-06-04 stsp err = sync_err;
3488 2ec1f75b 2019-03-26 stsp done:
3489 fb399478 2019-07-12 stsp free(fileindex_path);
3490 2ec1f75b 2019-03-26 stsp if (fileindex)
3491 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3492 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3493 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3494 2ec1f75b 2019-03-26 stsp err = unlockerr;
3495 33aa809d 2019-08-08 stsp return err;
3496 33aa809d 2019-08-08 stsp }
3497 33aa809d 2019-08-08 stsp
3498 33aa809d 2019-08-08 stsp static const struct got_error *
3499 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3500 33aa809d 2019-08-08 stsp {
3501 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3502 33aa809d 2019-08-08 stsp char *line = NULL;
3503 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3504 33aa809d 2019-08-08 stsp ssize_t linelen;
3505 33aa809d 2019-08-08 stsp
3506 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3507 33aa809d 2019-08-08 stsp if (linelen == -1) {
3508 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3509 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3510 33aa809d 2019-08-08 stsp goto done;
3511 33aa809d 2019-08-08 stsp }
3512 33aa809d 2019-08-08 stsp return NULL;
3513 33aa809d 2019-08-08 stsp }
3514 33aa809d 2019-08-08 stsp if (outfile) {
3515 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3516 33aa809d 2019-08-08 stsp if (n != linelen) {
3517 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3518 33aa809d 2019-08-08 stsp goto done;
3519 33aa809d 2019-08-08 stsp }
3520 33aa809d 2019-08-08 stsp }
3521 33aa809d 2019-08-08 stsp if (rejectfile) {
3522 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3523 33aa809d 2019-08-08 stsp if (n != linelen)
3524 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3525 33aa809d 2019-08-08 stsp }
3526 33aa809d 2019-08-08 stsp done:
3527 33aa809d 2019-08-08 stsp free(line);
3528 2ec1f75b 2019-03-26 stsp return err;
3529 2ec1f75b 2019-03-26 stsp }
3530 1f1abb7e 2019-08-08 stsp
3531 33aa809d 2019-08-08 stsp static const struct got_error *
3532 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3533 33aa809d 2019-08-08 stsp {
3534 33aa809d 2019-08-08 stsp char *line = NULL;
3535 33aa809d 2019-08-08 stsp size_t linesize = 0;
3536 33aa809d 2019-08-08 stsp ssize_t linelen;
3537 33aa809d 2019-08-08 stsp
3538 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3539 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3540 500467ff 2019-09-25 hiltjo if (ferror(f))
3541 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3542 500467ff 2019-09-25 hiltjo return NULL;
3543 500467ff 2019-09-25 hiltjo }
3544 33aa809d 2019-08-08 stsp free(line);
3545 33aa809d 2019-08-08 stsp return NULL;
3546 33aa809d 2019-08-08 stsp }
3547 33aa809d 2019-08-08 stsp
3548 33aa809d 2019-08-08 stsp static const struct got_error *
3549 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3550 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3551 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3552 33aa809d 2019-08-08 stsp {
3553 33aa809d 2019-08-08 stsp const struct got_error *err;
3554 33aa809d 2019-08-08 stsp
3555 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3556 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3557 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3558 33aa809d 2019-08-08 stsp if (err)
3559 33aa809d 2019-08-08 stsp return err;
3560 33aa809d 2019-08-08 stsp (*line_cur1)++;
3561 33aa809d 2019-08-08 stsp }
3562 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3563 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3564 33aa809d 2019-08-08 stsp if (rejectfile)
3565 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3566 33aa809d 2019-08-08 stsp else
3567 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3568 33aa809d 2019-08-08 stsp if (err)
3569 33aa809d 2019-08-08 stsp return err;
3570 33aa809d 2019-08-08 stsp (*line_cur2)++;
3571 33aa809d 2019-08-08 stsp }
3572 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3573 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3574 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3575 33aa809d 2019-08-08 stsp if (err)
3576 33aa809d 2019-08-08 stsp return err;
3577 33aa809d 2019-08-08 stsp (*line_cur2)++;
3578 33aa809d 2019-08-08 stsp }
3579 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3580 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3581 33aa809d 2019-08-08 stsp if (rejectfile)
3582 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3583 33aa809d 2019-08-08 stsp else
3584 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3585 33aa809d 2019-08-08 stsp if (err)
3586 33aa809d 2019-08-08 stsp return err;
3587 33aa809d 2019-08-08 stsp (*line_cur1)++;
3588 33aa809d 2019-08-08 stsp }
3589 f1e81a05 2019-08-10 stsp
3590 f1e81a05 2019-08-10 stsp return NULL;
3591 f1e81a05 2019-08-10 stsp }
3592 f1e81a05 2019-08-10 stsp
3593 f1e81a05 2019-08-10 stsp static const struct got_error *
3594 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3595 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3596 f1e81a05 2019-08-10 stsp {
3597 f1e81a05 2019-08-10 stsp const struct got_error *err;
3598 f1e81a05 2019-08-10 stsp
3599 f1e81a05 2019-08-10 stsp if (outfile) {
3600 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3601 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3602 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3603 f1e81a05 2019-08-10 stsp if (err)
3604 f1e81a05 2019-08-10 stsp return err;
3605 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3606 f1e81a05 2019-08-10 stsp }
3607 f1e81a05 2019-08-10 stsp }
3608 f1e81a05 2019-08-10 stsp if (rejectfile) {
3609 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3610 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3611 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3612 f1e81a05 2019-08-10 stsp if (err)
3613 f1e81a05 2019-08-10 stsp return err;
3614 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3615 f1e81a05 2019-08-10 stsp }
3616 33aa809d 2019-08-08 stsp }
3617 33aa809d 2019-08-08 stsp
3618 33aa809d 2019-08-08 stsp return NULL;
3619 33aa809d 2019-08-08 stsp }
3620 33aa809d 2019-08-08 stsp
3621 33aa809d 2019-08-08 stsp static const struct got_error *
3622 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3623 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3624 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3625 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3626 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3627 33aa809d 2019-08-08 stsp {
3628 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3629 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3630 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3631 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3632 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3633 33aa809d 2019-08-08 stsp long pos1, pos2;
3634 33aa809d 2019-08-08 stsp FILE *hunkfile;
3635 33aa809d 2019-08-08 stsp
3636 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3637 33aa809d 2019-08-08 stsp
3638 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
3639 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
3640 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
3641 33aa809d 2019-08-08 stsp
3642 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
3643 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
3644 33aa809d 2019-08-08 stsp
3645 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
3646 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3647 33aa809d 2019-08-08 stsp
3648 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
3649 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3650 33aa809d 2019-08-08 stsp goto done;
3651 33aa809d 2019-08-08 stsp }
3652 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
3653 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3654 33aa809d 2019-08-08 stsp goto done;
3655 33aa809d 2019-08-08 stsp }
3656 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3657 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
3658 33aa809d 2019-08-08 stsp goto done;
3659 33aa809d 2019-08-08 stsp }
3660 33aa809d 2019-08-08 stsp
3661 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3662 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
3663 33aa809d 2019-08-08 stsp if (err)
3664 33aa809d 2019-08-08 stsp goto done;
3665 33aa809d 2019-08-08 stsp
3666 33aa809d 2019-08-08 stsp switch (*choice) {
3667 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
3668 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3669 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
3670 33aa809d 2019-08-08 stsp break;
3671 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
3672 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3673 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
3674 33aa809d 2019-08-08 stsp break;
3675 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
3676 33aa809d 2019-08-08 stsp break;
3677 33aa809d 2019-08-08 stsp default:
3678 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
3679 33aa809d 2019-08-08 stsp break;
3680 33aa809d 2019-08-08 stsp }
3681 33aa809d 2019-08-08 stsp done:
3682 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3683 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
3684 33aa809d 2019-08-08 stsp return err;
3685 33aa809d 2019-08-08 stsp }
3686 33aa809d 2019-08-08 stsp
3687 1f1abb7e 2019-08-08 stsp struct revert_file_args {
3688 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
3689 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
3690 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
3691 1f1abb7e 2019-08-08 stsp void *progress_arg;
3692 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
3693 33aa809d 2019-08-08 stsp void *patch_arg;
3694 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
3695 1f1abb7e 2019-08-08 stsp };
3696 a129376b 2019-03-28 stsp
3697 e20a8b6f 2019-06-04 stsp static const struct got_error *
3698 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
3699 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
3700 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
3701 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
3702 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3703 33aa809d 2019-08-08 stsp {
3704 33aa809d 2019-08-08 stsp const struct got_error *err;
3705 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
3706 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3707 1ebedb77 2019-10-19 stsp int fd2 = -1;
3708 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
3709 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
3710 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
3711 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
3712 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
3713 33aa809d 2019-08-08 stsp struct got_diff_change *change;
3714 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3715 33aa809d 2019-08-08 stsp int n = 0;
3716 33aa809d 2019-08-08 stsp
3717 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3718 33aa809d 2019-08-08 stsp
3719 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
3720 33aa809d 2019-08-08 stsp if (err)
3721 33aa809d 2019-08-08 stsp return err;
3722 33aa809d 2019-08-08 stsp
3723 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
3724 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3725 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3726 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
3727 12463d8b 2019-12-13 stsp goto done;
3728 12463d8b 2019-12-13 stsp }
3729 12463d8b 2019-12-13 stsp } else {
3730 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3731 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3732 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
3733 12463d8b 2019-12-13 stsp goto done;
3734 12463d8b 2019-12-13 stsp }
3735 1ebedb77 2019-10-19 stsp }
3736 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
3737 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
3738 1ebedb77 2019-10-19 stsp goto done;
3739 1ebedb77 2019-10-19 stsp }
3740 1ebedb77 2019-10-19 stsp
3741 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
3742 33aa809d 2019-08-08 stsp if (f2 == NULL) {
3743 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
3744 33aa809d 2019-08-08 stsp goto done;
3745 33aa809d 2019-08-08 stsp }
3746 1ebedb77 2019-10-19 stsp fd2 = -1;
3747 33aa809d 2019-08-08 stsp
3748 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3749 33aa809d 2019-08-08 stsp if (err)
3750 33aa809d 2019-08-08 stsp goto done;
3751 33aa809d 2019-08-08 stsp
3752 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3753 33aa809d 2019-08-08 stsp if (err)
3754 33aa809d 2019-08-08 stsp goto done;
3755 33aa809d 2019-08-08 stsp
3756 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3757 33aa809d 2019-08-08 stsp if (err)
3758 33aa809d 2019-08-08 stsp goto done;
3759 33aa809d 2019-08-08 stsp
3760 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
3761 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
3762 33aa809d 2019-08-08 stsp goto done;
3763 33aa809d 2019-08-08 stsp }
3764 33aa809d 2019-08-08 stsp
3765 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
3766 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3767 33aa809d 2019-08-08 stsp if (err)
3768 33aa809d 2019-08-08 stsp goto done;
3769 33aa809d 2019-08-08 stsp
3770 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3771 33aa809d 2019-08-08 stsp if (err)
3772 33aa809d 2019-08-08 stsp goto done;
3773 33aa809d 2019-08-08 stsp
3774 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
3775 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
3776 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
3777 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
3778 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3779 33aa809d 2019-08-08 stsp int choice;
3780 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
3781 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
3782 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
3783 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
3784 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
3785 e635744c 2019-08-08 stsp patch_cb, patch_arg);
3786 33aa809d 2019-08-08 stsp if (err)
3787 33aa809d 2019-08-08 stsp goto done;
3788 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
3789 33aa809d 2019-08-08 stsp have_content = 1;
3790 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
3791 33aa809d 2019-08-08 stsp break;
3792 33aa809d 2019-08-08 stsp }
3793 1ebedb77 2019-10-19 stsp if (have_content) {
3794 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3795 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
3796 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
3797 1ebedb77 2019-10-19 stsp if (err)
3798 1ebedb77 2019-10-19 stsp goto done;
3799 1ebedb77 2019-10-19 stsp
3800 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
3801 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
3802 1ebedb77 2019-10-19 stsp goto done;
3803 1ebedb77 2019-10-19 stsp }
3804 1ebedb77 2019-10-19 stsp }
3805 33aa809d 2019-08-08 stsp done:
3806 33aa809d 2019-08-08 stsp free(id_str);
3807 33aa809d 2019-08-08 stsp if (blob)
3808 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
3809 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3810 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
3811 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3812 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
3813 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3814 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
3815 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
3816 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
3817 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
3818 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
3819 33aa809d 2019-08-08 stsp if (err || !have_content) {
3820 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3821 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
3822 33aa809d 2019-08-08 stsp free(*path_outfile);
3823 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3824 33aa809d 2019-08-08 stsp }
3825 33aa809d 2019-08-08 stsp free(args);
3826 33aa809d 2019-08-08 stsp if (ds) {
3827 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
3828 33aa809d 2019-08-08 stsp free(ds);
3829 33aa809d 2019-08-08 stsp }
3830 33aa809d 2019-08-08 stsp if (changes)
3831 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
3832 33aa809d 2019-08-08 stsp free(path1);
3833 33aa809d 2019-08-08 stsp return err;
3834 33aa809d 2019-08-08 stsp }
3835 33aa809d 2019-08-08 stsp
3836 33aa809d 2019-08-08 stsp static const struct got_error *
3837 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
3838 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
3839 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3840 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3841 a129376b 2019-03-28 stsp {
3842 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
3843 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
3844 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
3845 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
3846 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
3847 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
3848 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
3849 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
3850 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
3851 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
3852 a129376b 2019-03-28 stsp
3853 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
3854 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
3855 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
3856 d3bcc3d1 2019-08-08 stsp return NULL;
3857 3d69ad8d 2019-08-17 semarie
3858 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
3859 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
3860 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
3861 d3bcc3d1 2019-08-08 stsp
3862 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3863 65084dad 2019-08-08 stsp if (ie == NULL)
3864 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3865 a129376b 2019-03-28 stsp
3866 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
3867 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
3868 a129376b 2019-03-28 stsp if (err) {
3869 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
3870 a129376b 2019-03-28 stsp goto done;
3871 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
3872 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
3873 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
3874 e20a8b6f 2019-06-04 stsp goto done;
3875 e20a8b6f 2019-06-04 stsp }
3876 a129376b 2019-03-28 stsp }
3877 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
3878 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
3879 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3880 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3881 a129376b 2019-03-28 stsp goto done;
3882 a129376b 2019-03-28 stsp }
3883 a129376b 2019-03-28 stsp } else {
3884 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
3885 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
3886 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3887 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3888 a129376b 2019-03-28 stsp goto done;
3889 a129376b 2019-03-28 stsp }
3890 a129376b 2019-03-28 stsp } else {
3891 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
3892 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
3893 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3894 a129376b 2019-03-28 stsp goto done;
3895 a129376b 2019-03-28 stsp }
3896 a129376b 2019-03-28 stsp }
3897 a129376b 2019-03-28 stsp }
3898 a129376b 2019-03-28 stsp
3899 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
3900 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
3901 a9fa2909 2019-07-27 stsp if (err) {
3902 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3903 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
3904 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
3905 a9fa2909 2019-07-27 stsp goto done;
3906 a9fa2909 2019-07-27 stsp } else {
3907 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
3908 a9fa2909 2019-07-27 stsp if (err)
3909 a9fa2909 2019-07-27 stsp goto done;
3910 a9fa2909 2019-07-27 stsp
3911 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
3912 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
3913 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
3914 a9fa2909 2019-07-27 stsp goto done;
3915 a9fa2909 2019-07-27 stsp }
3916 a9fa2909 2019-07-27 stsp
3917 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
3918 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
3919 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
3920 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
3921 a9fa2909 2019-07-27 stsp goto done;
3922 a9fa2909 2019-07-27 stsp }
3923 a129376b 2019-03-28 stsp }
3924 a129376b 2019-03-28 stsp
3925 a129376b 2019-03-28 stsp switch (status) {
3926 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
3927 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3928 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3929 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3930 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3931 33aa809d 2019-08-08 stsp if (err)
3932 33aa809d 2019-08-08 stsp goto done;
3933 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3934 33aa809d 2019-08-08 stsp break;
3935 33aa809d 2019-08-08 stsp }
3936 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3937 1f1abb7e 2019-08-08 stsp ie->path);
3938 1ee397ad 2019-07-12 stsp if (err)
3939 1ee397ad 2019-07-12 stsp goto done;
3940 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
3941 a129376b 2019-03-28 stsp break;
3942 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
3943 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3944 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3945 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3946 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3947 33aa809d 2019-08-08 stsp if (err)
3948 33aa809d 2019-08-08 stsp goto done;
3949 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3950 33aa809d 2019-08-08 stsp break;
3951 33aa809d 2019-08-08 stsp }
3952 33aa809d 2019-08-08 stsp /* fall through */
3953 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
3954 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
3955 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
3956 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
3957 e20a8b6f 2019-06-04 stsp struct got_object_id id;
3958 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3959 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3960 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
3961 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3962 24278f30 2019-08-03 stsp } else
3963 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
3964 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3965 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3966 a129376b 2019-03-28 stsp if (err)
3967 65084dad 2019-08-08 stsp goto done;
3968 65084dad 2019-08-08 stsp
3969 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3970 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
3971 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
3972 a129376b 2019-03-28 stsp goto done;
3973 65084dad 2019-08-08 stsp }
3974 65084dad 2019-08-08 stsp
3975 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3976 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
3977 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
3978 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
3979 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
3980 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
3981 33aa809d 2019-08-08 stsp break;
3982 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
3983 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
3984 33aa809d 2019-08-08 stsp path_content, ondisk_path);
3985 33aa809d 2019-08-08 stsp goto done;
3986 33aa809d 2019-08-08 stsp }
3987 33aa809d 2019-08-08 stsp } else {
3988 33aa809d 2019-08-08 stsp err = install_blob(a->worktree, ondisk_path, ie->path,
3989 33aa809d 2019-08-08 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
3990 33aa809d 2019-08-08 stsp got_fileindex_perms_to_st(ie), blob, 0, 1,
3991 33aa809d 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
3992 a129376b 2019-03-28 stsp if (err)
3993 a129376b 2019-03-28 stsp goto done;
3994 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
3995 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
3996 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3997 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
3998 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
3999 33aa809d 2019-08-08 stsp if (err)
4000 33aa809d 2019-08-08 stsp goto done;
4001 33aa809d 2019-08-08 stsp }
4002 a129376b 2019-03-28 stsp }
4003 a129376b 2019-03-28 stsp break;
4004 e20a8b6f 2019-06-04 stsp }
4005 a129376b 2019-03-28 stsp default:
4006 1f1abb7e 2019-08-08 stsp break;
4007 a129376b 2019-03-28 stsp }
4008 a129376b 2019-03-28 stsp done:
4009 1f1abb7e 2019-08-08 stsp free(ondisk_path);
4010 33aa809d 2019-08-08 stsp free(path_content);
4011 e20a8b6f 2019-06-04 stsp free(parent_path);
4012 a129376b 2019-03-28 stsp free(tree_path);
4013 a129376b 2019-03-28 stsp if (blob)
4014 a129376b 2019-03-28 stsp got_object_blob_close(blob);
4015 a129376b 2019-03-28 stsp if (tree)
4016 a129376b 2019-03-28 stsp got_object_tree_close(tree);
4017 a129376b 2019-03-28 stsp free(tree_id);
4018 e20a8b6f 2019-06-04 stsp return err;
4019 e20a8b6f 2019-06-04 stsp }
4020 e20a8b6f 2019-06-04 stsp
4021 e20a8b6f 2019-06-04 stsp const struct got_error *
4022 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
4023 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
4024 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4025 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
4026 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
4027 e20a8b6f 2019-06-04 stsp {
4028 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
4029 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
4030 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4031 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
4032 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
4033 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
4034 e20a8b6f 2019-06-04 stsp
4035 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
4036 e20a8b6f 2019-06-04 stsp if (err)
4037 e20a8b6f 2019-06-04 stsp return err;
4038 e20a8b6f 2019-06-04 stsp
4039 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4040 e20a8b6f 2019-06-04 stsp if (err)
4041 e20a8b6f 2019-06-04 stsp goto done;
4042 e20a8b6f 2019-06-04 stsp
4043 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
4044 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
4045 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
4046 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
4047 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
4048 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
4049 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
4050 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
4051 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4052 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
4053 e20a8b6f 2019-06-04 stsp if (err)
4054 af12c6b9 2019-06-04 stsp break;
4055 e20a8b6f 2019-06-04 stsp }
4056 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4057 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
4058 e20a8b6f 2019-06-04 stsp err = sync_err;
4059 af12c6b9 2019-06-04 stsp done:
4060 fb399478 2019-07-12 stsp free(fileindex_path);
4061 a129376b 2019-03-28 stsp if (fileindex)
4062 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
4063 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4064 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
4065 a129376b 2019-03-28 stsp err = unlockerr;
4066 c4296144 2019-05-09 stsp return err;
4067 c4296144 2019-05-09 stsp }
4068 c4296144 2019-05-09 stsp
4069 cf066bf8 2019-05-09 stsp static void
4070 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
4071 cf066bf8 2019-05-09 stsp {
4072 24519714 2019-05-09 stsp free(ct->path);
4073 44d03001 2019-05-09 stsp free(ct->in_repo_path);
4074 768aea60 2019-05-09 stsp free(ct->ondisk_path);
4075 e75eb4da 2019-05-10 stsp free(ct->blob_id);
4076 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
4077 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
4078 b416585c 2019-05-13 stsp free(ct->base_commit_id);
4079 cf066bf8 2019-05-09 stsp free(ct);
4080 cf066bf8 2019-05-09 stsp }
4081 24519714 2019-05-09 stsp
4082 ed175427 2019-05-09 stsp struct collect_commitables_arg {
4083 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
4084 24519714 2019-05-09 stsp struct got_repository *repo;
4085 24519714 2019-05-09 stsp struct got_worktree *worktree;
4086 5f8a88c6 2019-08-03 stsp int have_staged_files;
4087 24519714 2019-05-09 stsp };
4088 cf066bf8 2019-05-09 stsp
4089 c4296144 2019-05-09 stsp static const struct got_error *
4090 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
4091 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
4092 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4093 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4094 c4296144 2019-05-09 stsp {
4095 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4096 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4097 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4098 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4099 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4100 768aea60 2019-05-09 stsp struct stat sb;
4101 c4296144 2019-05-09 stsp
4102 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4103 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4104 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4105 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4106 5f8a88c6 2019-08-03 stsp return NULL;
4107 5f8a88c6 2019-08-03 stsp } else {
4108 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4109 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4110 c4296144 2019-05-09 stsp
4111 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4112 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4113 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4114 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4115 5f8a88c6 2019-08-03 stsp return NULL;
4116 5f8a88c6 2019-08-03 stsp }
4117 0b5cc0d6 2019-05-09 stsp
4118 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4119 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4120 036813ee 2019-05-09 stsp goto done;
4121 036813ee 2019-05-09 stsp }
4122 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4123 036813ee 2019-05-09 stsp parent_path = strdup("");
4124 69960a46 2019-05-09 stsp if (parent_path == NULL)
4125 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4126 69960a46 2019-05-09 stsp } else {
4127 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4128 69960a46 2019-05-09 stsp if (err)
4129 69960a46 2019-05-09 stsp return err;
4130 69960a46 2019-05-09 stsp }
4131 c4296144 2019-05-09 stsp
4132 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4133 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4134 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4135 c4296144 2019-05-09 stsp goto done;
4136 768aea60 2019-05-09 stsp }
4137 768aea60 2019-05-09 stsp
4138 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4139 768aea60 2019-05-09 stsp relpath) == -1) {
4140 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4141 768aea60 2019-05-09 stsp goto done;
4142 768aea60 2019-05-09 stsp }
4143 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4144 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
4145 768aea60 2019-05-09 stsp } else {
4146 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4147 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4148 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4149 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4150 12463d8b 2019-12-13 stsp ct->ondisk_path);
4151 12463d8b 2019-12-13 stsp goto done;
4152 12463d8b 2019-12-13 stsp }
4153 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4154 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4155 768aea60 2019-05-09 stsp goto done;
4156 768aea60 2019-05-09 stsp }
4157 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4158 c4296144 2019-05-09 stsp }
4159 c4296144 2019-05-09 stsp
4160 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4161 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4162 44d03001 2019-05-09 stsp relpath) == -1) {
4163 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4164 44d03001 2019-05-09 stsp goto done;
4165 44d03001 2019-05-09 stsp }
4166 44d03001 2019-05-09 stsp
4167 cf066bf8 2019-05-09 stsp ct->status = status;
4168 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4169 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4170 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4171 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4172 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4173 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4174 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4175 b416585c 2019-05-13 stsp goto done;
4176 b416585c 2019-05-13 stsp }
4177 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4178 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4179 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4180 f0b75401 2019-08-03 stsp goto done;
4181 f0b75401 2019-08-03 stsp }
4182 f0b75401 2019-08-03 stsp }
4183 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4184 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4185 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4186 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4187 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4188 036813ee 2019-05-09 stsp goto done;
4189 036813ee 2019-05-09 stsp }
4190 ca2503ea 2019-05-09 stsp }
4191 24519714 2019-05-09 stsp ct->path = strdup(path);
4192 24519714 2019-05-09 stsp if (ct->path == NULL) {
4193 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4194 24519714 2019-05-09 stsp goto done;
4195 24519714 2019-05-09 stsp }
4196 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4197 c4296144 2019-05-09 stsp done:
4198 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4199 ed175427 2019-05-09 stsp free_commitable(ct);
4200 24519714 2019-05-09 stsp free(parent_path);
4201 036813ee 2019-05-09 stsp free(path);
4202 a129376b 2019-03-28 stsp return err;
4203 a129376b 2019-03-28 stsp }
4204 c4296144 2019-05-09 stsp
4205 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4206 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4207 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4208 036813ee 2019-05-09 stsp struct got_repository *);
4209 ed175427 2019-05-09 stsp
4210 ed175427 2019-05-09 stsp static const struct got_error *
4211 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4212 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4213 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4214 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4215 afa376bf 2019-05-09 stsp struct got_repository *repo)
4216 ed175427 2019-05-09 stsp {
4217 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4218 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4219 036813ee 2019-05-09 stsp char *subpath;
4220 ed175427 2019-05-09 stsp
4221 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4222 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4223 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4224 ed175427 2019-05-09 stsp
4225 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4226 ed175427 2019-05-09 stsp if (err)
4227 ed175427 2019-05-09 stsp return err;
4228 ed175427 2019-05-09 stsp
4229 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4230 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4231 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4232 036813ee 2019-05-09 stsp free(subpath);
4233 036813ee 2019-05-09 stsp return err;
4234 036813ee 2019-05-09 stsp }
4235 ed175427 2019-05-09 stsp
4236 036813ee 2019-05-09 stsp static const struct got_error *
4237 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4238 036813ee 2019-05-09 stsp {
4239 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4240 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4241 ed175427 2019-05-09 stsp
4242 036813ee 2019-05-09 stsp *match = 0;
4243 ed175427 2019-05-09 stsp
4244 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4245 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4246 0f63689d 2019-05-10 stsp return NULL;
4247 036813ee 2019-05-09 stsp }
4248 0b5cc0d6 2019-05-09 stsp
4249 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4250 0f63689d 2019-05-10 stsp if (err)
4251 0f63689d 2019-05-10 stsp return err;
4252 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4253 036813ee 2019-05-09 stsp free(ct_parent_path);
4254 036813ee 2019-05-09 stsp return err;
4255 036813ee 2019-05-09 stsp }
4256 0b5cc0d6 2019-05-09 stsp
4257 768aea60 2019-05-09 stsp static mode_t
4258 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4259 768aea60 2019-05-09 stsp {
4260 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4261 768aea60 2019-05-09 stsp }
4262 768aea60 2019-05-09 stsp
4263 036813ee 2019-05-09 stsp static const struct got_error *
4264 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4265 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4266 036813ee 2019-05-09 stsp {
4267 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4268 ca2503ea 2019-05-09 stsp
4269 036813ee 2019-05-09 stsp *new_te = NULL;
4270 0b5cc0d6 2019-05-09 stsp
4271 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4272 036813ee 2019-05-09 stsp if (err)
4273 036813ee 2019-05-09 stsp goto done;
4274 0b5cc0d6 2019-05-09 stsp
4275 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4276 036813ee 2019-05-09 stsp
4277 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4278 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4279 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4280 5f8a88c6 2019-08-03 stsp else
4281 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4282 036813ee 2019-05-09 stsp done:
4283 036813ee 2019-05-09 stsp if (err && *new_te) {
4284 56e0773d 2019-11-28 stsp free(*new_te);
4285 036813ee 2019-05-09 stsp *new_te = NULL;
4286 036813ee 2019-05-09 stsp }
4287 036813ee 2019-05-09 stsp return err;
4288 036813ee 2019-05-09 stsp }
4289 036813ee 2019-05-09 stsp
4290 036813ee 2019-05-09 stsp static const struct got_error *
4291 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4292 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4293 036813ee 2019-05-09 stsp {
4294 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4295 036813ee 2019-05-09 stsp char *ct_name;
4296 036813ee 2019-05-09 stsp
4297 036813ee 2019-05-09 stsp *new_te = NULL;
4298 036813ee 2019-05-09 stsp
4299 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4300 036813ee 2019-05-09 stsp if (*new_te == NULL)
4301 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4302 036813ee 2019-05-09 stsp
4303 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4304 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4305 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4306 036813ee 2019-05-09 stsp goto done;
4307 036813ee 2019-05-09 stsp }
4308 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4309 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4310 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4311 036813ee 2019-05-09 stsp goto done;
4312 036813ee 2019-05-09 stsp }
4313 036813ee 2019-05-09 stsp
4314 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4315 036813ee 2019-05-09 stsp
4316 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4317 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4318 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4319 5f8a88c6 2019-08-03 stsp else
4320 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4321 036813ee 2019-05-09 stsp done:
4322 036813ee 2019-05-09 stsp if (err && *new_te) {
4323 56e0773d 2019-11-28 stsp free(*new_te);
4324 036813ee 2019-05-09 stsp *new_te = NULL;
4325 036813ee 2019-05-09 stsp }
4326 036813ee 2019-05-09 stsp return err;
4327 036813ee 2019-05-09 stsp }
4328 036813ee 2019-05-09 stsp
4329 036813ee 2019-05-09 stsp static const struct got_error *
4330 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4331 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4332 036813ee 2019-05-09 stsp {
4333 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4334 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4335 036813ee 2019-05-09 stsp
4336 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4337 036813ee 2019-05-09 stsp if (err)
4338 036813ee 2019-05-09 stsp return err;
4339 036813ee 2019-05-09 stsp if (new_pe == NULL)
4340 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4341 036813ee 2019-05-09 stsp return NULL;
4342 afa376bf 2019-05-09 stsp }
4343 afa376bf 2019-05-09 stsp
4344 afa376bf 2019-05-09 stsp static const struct got_error *
4345 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4346 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4347 afa376bf 2019-05-09 stsp {
4348 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4349 5f8a88c6 2019-08-03 stsp unsigned char status;
4350 5f8a88c6 2019-08-03 stsp
4351 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4352 afa376bf 2019-05-09 stsp ct_path++;
4353 5f8a88c6 2019-08-03 stsp
4354 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4355 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4356 5f8a88c6 2019-08-03 stsp else
4357 5f8a88c6 2019-08-03 stsp status = ct->status;
4358 5f8a88c6 2019-08-03 stsp
4359 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4360 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4361 036813ee 2019-05-09 stsp }
4362 036813ee 2019-05-09 stsp
4363 036813ee 2019-05-09 stsp static const struct got_error *
4364 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4365 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4366 44d03001 2019-05-09 stsp {
4367 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4368 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4369 44d03001 2019-05-09 stsp char *te_path;
4370 44d03001 2019-05-09 stsp
4371 44d03001 2019-05-09 stsp *modified = 0;
4372 44d03001 2019-05-09 stsp
4373 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4374 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4375 44d03001 2019-05-09 stsp te->name) == -1)
4376 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4377 44d03001 2019-05-09 stsp
4378 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4379 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4380 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4381 44d03001 2019-05-09 stsp strlen(te_path));
4382 44d03001 2019-05-09 stsp if (*modified)
4383 44d03001 2019-05-09 stsp break;
4384 44d03001 2019-05-09 stsp }
4385 44d03001 2019-05-09 stsp
4386 44d03001 2019-05-09 stsp free(te_path);
4387 44d03001 2019-05-09 stsp return err;
4388 44d03001 2019-05-09 stsp }
4389 44d03001 2019-05-09 stsp
4390 44d03001 2019-05-09 stsp static const struct got_error *
4391 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4392 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4393 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4394 036813ee 2019-05-09 stsp {
4395 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4396 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4397 036813ee 2019-05-09 stsp
4398 036813ee 2019-05-09 stsp *ctp = NULL;
4399 036813ee 2019-05-09 stsp
4400 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4401 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4402 036813ee 2019-05-09 stsp char *ct_name = NULL;
4403 036813ee 2019-05-09 stsp int path_matches;
4404 036813ee 2019-05-09 stsp
4405 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4406 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4407 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4408 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4409 5f8a88c6 2019-08-03 stsp continue;
4410 5f8a88c6 2019-08-03 stsp } else {
4411 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4412 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4413 5f8a88c6 2019-08-03 stsp continue;
4414 5f8a88c6 2019-08-03 stsp }
4415 036813ee 2019-05-09 stsp
4416 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4417 036813ee 2019-05-09 stsp continue;
4418 036813ee 2019-05-09 stsp
4419 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4420 036813ee 2019-05-09 stsp if (err)
4421 036813ee 2019-05-09 stsp return err;
4422 036813ee 2019-05-09 stsp if (!path_matches)
4423 036813ee 2019-05-09 stsp continue;
4424 036813ee 2019-05-09 stsp
4425 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4426 036813ee 2019-05-09 stsp if (ct_name == NULL)
4427 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4428 036813ee 2019-05-09 stsp
4429 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4430 036813ee 2019-05-09 stsp continue;
4431 036813ee 2019-05-09 stsp
4432 036813ee 2019-05-09 stsp *ctp = ct;
4433 036813ee 2019-05-09 stsp break;
4434 036813ee 2019-05-09 stsp }
4435 2b496619 2019-07-10 stsp
4436 2b496619 2019-07-10 stsp return err;
4437 2b496619 2019-07-10 stsp }
4438 2b496619 2019-07-10 stsp
4439 2b496619 2019-07-10 stsp static const struct got_error *
4440 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4441 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4442 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4443 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4444 2b496619 2019-07-10 stsp struct got_repository *repo)
4445 2b496619 2019-07-10 stsp {
4446 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4447 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4448 2b496619 2019-07-10 stsp char *subtree_path;
4449 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4450 ba580f68 2020-03-22 stsp int nentries;
4451 2b496619 2019-07-10 stsp
4452 2b496619 2019-07-10 stsp *new_tep = NULL;
4453 2b496619 2019-07-10 stsp
4454 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4455 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4456 2b496619 2019-07-10 stsp child_path) == -1)
4457 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4458 036813ee 2019-05-09 stsp
4459 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4460 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4461 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4462 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4463 56e0773d 2019-11-28 stsp
4464 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4465 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4466 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4467 2b496619 2019-07-10 stsp goto done;
4468 2b496619 2019-07-10 stsp }
4469 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4470 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4471 2b496619 2019-07-10 stsp if (err) {
4472 56e0773d 2019-11-28 stsp free(new_te);
4473 2b496619 2019-07-10 stsp goto done;
4474 2b496619 2019-07-10 stsp }
4475 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4476 2b496619 2019-07-10 stsp done:
4477 56e0773d 2019-11-28 stsp free(id);
4478 2b496619 2019-07-10 stsp free(subtree_path);
4479 2b496619 2019-07-10 stsp if (err == NULL)
4480 2b496619 2019-07-10 stsp *new_tep = new_te;
4481 036813ee 2019-05-09 stsp return err;
4482 036813ee 2019-05-09 stsp }
4483 036813ee 2019-05-09 stsp
4484 036813ee 2019-05-09 stsp static const struct got_error *
4485 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4486 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4487 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4488 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4489 036813ee 2019-05-09 stsp struct got_repository *repo)
4490 036813ee 2019-05-09 stsp {
4491 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4492 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4493 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4494 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4495 036813ee 2019-05-09 stsp
4496 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4497 ba580f68 2020-03-22 stsp *nentries = 0;
4498 036813ee 2019-05-09 stsp
4499 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4500 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4501 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4502 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4503 036813ee 2019-05-09 stsp
4504 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4505 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4506 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4507 036813ee 2019-05-09 stsp continue;
4508 036813ee 2019-05-09 stsp
4509 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4510 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4511 036813ee 2019-05-09 stsp continue;
4512 036813ee 2019-05-09 stsp
4513 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4514 036813ee 2019-05-09 stsp pe->path);
4515 036813ee 2019-05-09 stsp if (err)
4516 036813ee 2019-05-09 stsp goto done;
4517 036813ee 2019-05-09 stsp
4518 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4519 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4520 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4521 036813ee 2019-05-09 stsp if (err)
4522 036813ee 2019-05-09 stsp goto done;
4523 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4524 afa376bf 2019-05-09 stsp if (err)
4525 afa376bf 2019-05-09 stsp goto done;
4526 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4527 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4528 2b496619 2019-07-10 stsp if (err)
4529 2f51b5b3 2019-05-09 stsp goto done;
4530 ba580f68 2020-03-22 stsp (*nentries)++;
4531 2b496619 2019-07-10 stsp } else {
4532 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4533 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4534 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4535 2b496619 2019-07-10 stsp == NULL) {
4536 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4537 2b496619 2019-07-10 stsp child_path, path_base_tree,
4538 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4539 2b496619 2019-07-10 stsp repo);
4540 2b496619 2019-07-10 stsp if (err)
4541 2b496619 2019-07-10 stsp goto done;
4542 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4543 2b496619 2019-07-10 stsp if (err)
4544 2b496619 2019-07-10 stsp goto done;
4545 ba580f68 2020-03-22 stsp (*nentries)++;
4546 9ba0479c 2019-05-10 stsp }
4547 ed175427 2019-05-09 stsp }
4548 2f51b5b3 2019-05-09 stsp }
4549 2f51b5b3 2019-05-09 stsp
4550 2f51b5b3 2019-05-09 stsp if (base_tree) {
4551 56e0773d 2019-11-28 stsp int i, nbase_entries;
4552 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4553 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4554 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4555 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4556 63c5ca5d 2019-08-24 stsp
4557 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4558 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4559 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4560 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4561 63c5ca5d 2019-08-24 stsp if (err)
4562 63c5ca5d 2019-08-24 stsp goto done;
4563 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4564 63c5ca5d 2019-08-24 stsp if (err)
4565 63c5ca5d 2019-08-24 stsp goto done;
4566 ba580f68 2020-03-22 stsp (*nentries)++;
4567 63c5ca5d 2019-08-24 stsp continue;
4568 63c5ca5d 2019-08-24 stsp }
4569 2f51b5b3 2019-05-09 stsp
4570 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4571 44d03001 2019-05-09 stsp int modified;
4572 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4573 036813ee 2019-05-09 stsp if (err)
4574 036813ee 2019-05-09 stsp goto done;
4575 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4576 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4577 2f51b5b3 2019-05-09 stsp if (err)
4578 2f51b5b3 2019-05-09 stsp goto done;
4579 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4580 44d03001 2019-05-09 stsp if (modified) {
4581 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4582 ba580f68 2020-03-22 stsp int nsubentries;
4583 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
4584 ba580f68 2020-03-22 stsp &nsubentries, te,
4585 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4586 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4587 44d03001 2019-05-09 stsp if (err)
4588 44d03001 2019-05-09 stsp goto done;
4589 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
4590 ba580f68 2020-03-22 stsp /* All entries were deleted. */
4591 ba580f68 2020-03-22 stsp free(new_id);
4592 ba580f68 2020-03-22 stsp continue;
4593 ba580f68 2020-03-22 stsp }
4594 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4595 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4596 56e0773d 2019-11-28 stsp free(new_id);
4597 44d03001 2019-05-09 stsp }
4598 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4599 036813ee 2019-05-09 stsp if (err)
4600 036813ee 2019-05-09 stsp goto done;
4601 ba580f68 2020-03-22 stsp (*nentries)++;
4602 2f51b5b3 2019-05-09 stsp continue;
4603 036813ee 2019-05-09 stsp }
4604 2f51b5b3 2019-05-09 stsp
4605 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
4606 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
4607 f66c734c 2019-09-22 stsp if (err)
4608 f66c734c 2019-09-22 stsp goto done;
4609 2f51b5b3 2019-05-09 stsp if (ct) {
4610 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
4611 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
4612 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
4613 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4614 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
4615 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
4616 2f51b5b3 2019-05-09 stsp if (err)
4617 2f51b5b3 2019-05-09 stsp goto done;
4618 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4619 2f51b5b3 2019-05-09 stsp if (err)
4620 2f51b5b3 2019-05-09 stsp goto done;
4621 ba580f68 2020-03-22 stsp (*nentries)++;
4622 2f51b5b3 2019-05-09 stsp }
4623 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
4624 b416585c 2019-05-13 stsp status_arg);
4625 afa376bf 2019-05-09 stsp if (err)
4626 afa376bf 2019-05-09 stsp goto done;
4627 2f51b5b3 2019-05-09 stsp } else {
4628 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
4629 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4630 2f51b5b3 2019-05-09 stsp if (err)
4631 2f51b5b3 2019-05-09 stsp goto done;
4632 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4633 2f51b5b3 2019-05-09 stsp if (err)
4634 2f51b5b3 2019-05-09 stsp goto done;
4635 ba580f68 2020-03-22 stsp (*nentries)++;
4636 2f51b5b3 2019-05-09 stsp }
4637 ca2503ea 2019-05-09 stsp }
4638 ed175427 2019-05-09 stsp }
4639 0b5cc0d6 2019-05-09 stsp
4640 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
4641 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4642 ed175427 2019-05-09 stsp done:
4643 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
4644 ed175427 2019-05-09 stsp return err;
4645 ed175427 2019-05-09 stsp }
4646 ed175427 2019-05-09 stsp
4647 ebf99748 2019-05-09 stsp static const struct got_error *
4648 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4649 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4650 72fd46fa 2019-09-06 stsp int have_staged_files)
4651 ebf99748 2019-05-09 stsp {
4652 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
4653 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
4654 ebf99748 2019-05-09 stsp
4655 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4656 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
4657 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4658 ebf99748 2019-05-09 stsp
4659 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4660 ebf99748 2019-05-09 stsp if (ie) {
4661 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
4662 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
4663 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
4664 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
4665 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4666 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
4667 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
4668 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
4669 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
4670 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4671 72fd46fa 2019-09-06 stsp !have_staged_files);
4672 ebf99748 2019-05-09 stsp } else
4673 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
4674 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
4675 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4676 72fd46fa 2019-09-06 stsp !have_staged_files);
4677 ebf99748 2019-05-09 stsp } else {
4678 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
4679 ebf99748 2019-05-09 stsp if (err)
4680 af12c6b9 2019-06-04 stsp break;
4681 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
4682 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4683 3969253a 2020-03-07 stsp if (err) {
4684 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4685 3969253a 2020-03-07 stsp break;
4686 3969253a 2020-03-07 stsp }
4687 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
4688 3969253a 2020-03-07 stsp if (err) {
4689 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4690 af12c6b9 2019-06-04 stsp break;
4691 3969253a 2020-03-07 stsp }
4692 ebf99748 2019-05-09 stsp }
4693 ebf99748 2019-05-09 stsp }
4694 d56d26ce 2019-05-10 stsp return err;
4695 d56d26ce 2019-05-10 stsp }
4696 735ef5ac 2019-08-03 stsp
4697 d56d26ce 2019-05-10 stsp
4698 d56d26ce 2019-05-10 stsp static const struct got_error *
4699 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
4700 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
4701 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
4702 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
4703 735ef5ac 2019-08-03 stsp int ood_errcode)
4704 d56d26ce 2019-05-10 stsp {
4705 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
4706 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
4707 1a36436d 2019-06-10 stsp
4708 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4709 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
4710 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4711 1a36436d 2019-06-10 stsp return NULL;
4712 9bead371 2019-07-28 stsp /*
4713 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
4714 9bead371 2019-07-28 stsp * on matches file content in the branch head.
4715 9bead371 2019-07-28 stsp */
4716 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4717 735ef5ac 2019-08-03 stsp in_repo_path);
4718 9bead371 2019-07-28 stsp if (err) {
4719 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
4720 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
4721 1a36436d 2019-06-10 stsp goto done;
4722 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
4723 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
4724 1a36436d 2019-06-10 stsp } else {
4725 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
4726 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4727 735ef5ac 2019-08-03 stsp in_repo_path);
4728 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4729 1a36436d 2019-06-10 stsp goto done;
4730 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
4731 1a36436d 2019-06-10 stsp }
4732 1a36436d 2019-06-10 stsp done:
4733 1a36436d 2019-06-10 stsp free(id);
4734 1a36436d 2019-06-10 stsp return err;
4735 ebf99748 2019-05-09 stsp }
4736 ebf99748 2019-05-09 stsp
4737 c4296144 2019-05-09 stsp const struct got_error *
4738 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
4739 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
4740 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
4741 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
4742 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4743 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4744 afa376bf 2019-05-09 stsp struct got_repository *repo)
4745 c4296144 2019-05-09 stsp {
4746 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4747 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
4748 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
4749 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
4750 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
4751 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
4752 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
4753 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
4754 ba580f68 2020-03-22 stsp int nentries;
4755 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
4756 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
4757 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
4758 c4296144 2019-05-09 stsp
4759 c4296144 2019-05-09 stsp *new_commit_id = NULL;
4760 c4296144 2019-05-09 stsp
4761 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
4762 675c7539 2019-05-09 stsp
4763 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4764 588edf97 2019-05-10 stsp if (err)
4765 588edf97 2019-05-10 stsp goto done;
4766 de18fc63 2019-05-09 stsp
4767 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4768 588edf97 2019-05-10 stsp if (err)
4769 588edf97 2019-05-10 stsp goto done;
4770 588edf97 2019-05-10 stsp
4771 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
4772 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4773 33ad4cbe 2019-05-12 jcs if (err)
4774 33ad4cbe 2019-05-12 jcs goto done;
4775 33ad4cbe 2019-05-12 jcs }
4776 c4296144 2019-05-09 stsp
4777 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
4778 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4779 33ad4cbe 2019-05-12 jcs goto done;
4780 33ad4cbe 2019-05-12 jcs }
4781 33ad4cbe 2019-05-12 jcs
4782 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
4783 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4784 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4785 cf066bf8 2019-05-09 stsp char *ondisk_path;
4786 cf066bf8 2019-05-09 stsp
4787 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
4788 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4789 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
4790 5f8a88c6 2019-08-03 stsp continue;
4791 5f8a88c6 2019-08-03 stsp
4792 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
4793 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
4794 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
4795 cf066bf8 2019-05-09 stsp continue;
4796 cf066bf8 2019-05-09 stsp
4797 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
4798 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
4799 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4800 cf066bf8 2019-05-09 stsp goto done;
4801 cf066bf8 2019-05-09 stsp }
4802 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4803 a7055788 2019-05-09 stsp free(ondisk_path);
4804 cf066bf8 2019-05-09 stsp if (err)
4805 cf066bf8 2019-05-09 stsp goto done;
4806 cf066bf8 2019-05-09 stsp }
4807 036813ee 2019-05-09 stsp
4808 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
4809 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4810 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4811 036813ee 2019-05-09 stsp if (err)
4812 036813ee 2019-05-09 stsp goto done;
4813 036813ee 2019-05-09 stsp
4814 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4815 de18fc63 2019-05-09 stsp if (err)
4816 de18fc63 2019-05-09 stsp goto done;
4817 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4818 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4819 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4820 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
4821 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
4822 33ad4cbe 2019-05-12 jcs free(logmsg);
4823 9d40349a 2019-05-09 stsp if (err)
4824 9d40349a 2019-05-09 stsp goto done;
4825 9d40349a 2019-05-09 stsp
4826 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
4827 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
4828 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
4829 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
4830 09f5bd90 2019-05-09 stsp goto done;
4831 09f5bd90 2019-05-09 stsp }
4832 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
4833 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4834 09f5bd90 2019-05-09 stsp if (err)
4835 09f5bd90 2019-05-09 stsp goto done;
4836 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4837 ebf99748 2019-05-09 stsp if (err)
4838 ebf99748 2019-05-09 stsp goto done;
4839 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4840 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4841 09f5bd90 2019-05-09 stsp goto done;
4842 09f5bd90 2019-05-09 stsp }
4843 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
4844 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
4845 f2c16586 2019-05-09 stsp if (err)
4846 f2c16586 2019-05-09 stsp goto done;
4847 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
4848 f2c16586 2019-05-09 stsp if (err)
4849 f2c16586 2019-05-09 stsp goto done;
4850 f2c16586 2019-05-09 stsp
4851 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4852 09f5bd90 2019-05-09 stsp if (err)
4853 09f5bd90 2019-05-09 stsp goto done;
4854 09f5bd90 2019-05-09 stsp
4855 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
4856 ebf99748 2019-05-09 stsp if (err)
4857 ebf99748 2019-05-09 stsp goto done;
4858 c4296144 2019-05-09 stsp done:
4859 588edf97 2019-05-10 stsp if (head_tree)
4860 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
4861 588edf97 2019-05-10 stsp if (head_commit)
4862 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
4863 09f5bd90 2019-05-09 stsp free(head_commit_id2);
4864 2f17228e 2019-05-12 stsp if (head_ref2) {
4865 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
4866 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
4867 2f17228e 2019-05-12 stsp err = unlockerr;
4868 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
4869 39cd0ff6 2019-07-12 stsp }
4870 39cd0ff6 2019-07-12 stsp return err;
4871 39cd0ff6 2019-07-12 stsp }
4872 5c1e53bc 2019-07-28 stsp
4873 5c1e53bc 2019-07-28 stsp static const struct got_error *
4874 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
4875 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
4876 5c1e53bc 2019-07-28 stsp {
4877 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
4878 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
4879 39cd0ff6 2019-07-12 stsp
4880 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
4881 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
4882 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
4883 5c1e53bc 2019-07-28 stsp
4884 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
4885 5c1e53bc 2019-07-28 stsp ct_path++;
4886 5c1e53bc 2019-07-28 stsp
4887 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
4888 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
4889 5c1e53bc 2019-07-28 stsp break;
4890 5c1e53bc 2019-07-28 stsp }
4891 5c1e53bc 2019-07-28 stsp
4892 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
4893 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
4894 5c1e53bc 2019-07-28 stsp
4895 5c1e53bc 2019-07-28 stsp return NULL;
4896 5c1e53bc 2019-07-28 stsp }
4897 5c1e53bc 2019-07-28 stsp
4898 f0b75401 2019-08-03 stsp static const struct got_error *
4899 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
4900 f0b75401 2019-08-03 stsp {
4901 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
4902 f0b75401 2019-08-03 stsp
4903 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4904 f0b75401 2019-08-03 stsp *have_staged_files = 1;
4905 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
4906 f0b75401 2019-08-03 stsp }
4907 f0b75401 2019-08-03 stsp
4908 f0b75401 2019-08-03 stsp return NULL;
4909 f0b75401 2019-08-03 stsp }
4910 f0b75401 2019-08-03 stsp
4911 5f8a88c6 2019-08-03 stsp static const struct got_error *
4912 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
4913 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
4914 5f8a88c6 2019-08-03 stsp {
4915 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
4916 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
4917 5f8a88c6 2019-08-03 stsp
4918 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
4919 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
4920 5f8a88c6 2019-08-03 stsp continue;
4921 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4922 5f8a88c6 2019-08-03 stsp if (ie == NULL)
4923 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4924 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4925 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
4926 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
4927 5f8a88c6 2019-08-03 stsp }
4928 5f8a88c6 2019-08-03 stsp
4929 5f8a88c6 2019-08-03 stsp return NULL;
4930 5f8a88c6 2019-08-03 stsp }
4931 5f8a88c6 2019-08-03 stsp
4932 39cd0ff6 2019-07-12 stsp const struct got_error *
4933 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
4934 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
4935 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
4936 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4937 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4938 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
4939 39cd0ff6 2019-07-12 stsp {
4940 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4941 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4942 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
4943 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4944 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4945 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
4946 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
4947 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4948 f0b75401 2019-08-03 stsp int have_staged_files = 0;
4949 39cd0ff6 2019-07-12 stsp
4950 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
4951 39cd0ff6 2019-07-12 stsp
4952 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4953 39cd0ff6 2019-07-12 stsp
4954 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
4955 39cd0ff6 2019-07-12 stsp if (err)
4956 39cd0ff6 2019-07-12 stsp goto done;
4957 39cd0ff6 2019-07-12 stsp
4958 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4959 39cd0ff6 2019-07-12 stsp if (err)
4960 39cd0ff6 2019-07-12 stsp goto done;
4961 39cd0ff6 2019-07-12 stsp
4962 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4963 39cd0ff6 2019-07-12 stsp if (err)
4964 39cd0ff6 2019-07-12 stsp goto done;
4965 39cd0ff6 2019-07-12 stsp
4966 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4967 39cd0ff6 2019-07-12 stsp if (err)
4968 39cd0ff6 2019-07-12 stsp goto done;
4969 39cd0ff6 2019-07-12 stsp
4970 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4971 5f8a88c6 2019-08-03 stsp &have_staged_files);
4972 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
4973 5f8a88c6 2019-08-03 stsp goto done;
4974 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
4975 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
4976 5f8a88c6 2019-08-03 stsp if (err)
4977 5f8a88c6 2019-08-03 stsp goto done;
4978 5f8a88c6 2019-08-03 stsp }
4979 5f8a88c6 2019-08-03 stsp
4980 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4981 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
4982 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
4983 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
4984 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4985 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4986 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4987 5c1e53bc 2019-07-28 stsp if (err)
4988 5c1e53bc 2019-07-28 stsp goto done;
4989 5c1e53bc 2019-07-28 stsp }
4990 39cd0ff6 2019-07-12 stsp
4991 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4992 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4993 39cd0ff6 2019-07-12 stsp goto done;
4994 5c1e53bc 2019-07-28 stsp }
4995 5c1e53bc 2019-07-28 stsp
4996 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4997 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
4998 5c1e53bc 2019-07-28 stsp if (err)
4999 5c1e53bc 2019-07-28 stsp goto done;
5000 39cd0ff6 2019-07-12 stsp }
5001 f0b75401 2019-08-03 stsp
5002 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5003 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
5004 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
5005 f0b75401 2019-08-03 stsp
5006 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
5007 f0b75401 2019-08-03 stsp ct_path++;
5008 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
5009 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5010 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5011 b50cabdf 2019-07-12 stsp if (err)
5012 b50cabdf 2019-07-12 stsp goto done;
5013 f0b75401 2019-08-03 stsp
5014 b50cabdf 2019-07-12 stsp }
5015 b50cabdf 2019-07-12 stsp
5016 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
5017 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
5018 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5019 39cd0ff6 2019-07-12 stsp if (err)
5020 39cd0ff6 2019-07-12 stsp goto done;
5021 39cd0ff6 2019-07-12 stsp
5022 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5023 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
5024 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5025 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
5026 39cd0ff6 2019-07-12 stsp err = sync_err;
5027 39cd0ff6 2019-07-12 stsp done:
5028 39cd0ff6 2019-07-12 stsp if (fileindex)
5029 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
5030 39cd0ff6 2019-07-12 stsp free(fileindex_path);
5031 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5032 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
5033 39cd0ff6 2019-07-12 stsp err = unlockerr;
5034 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
5035 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
5036 39cd0ff6 2019-07-12 stsp free_commitable(ct);
5037 39cd0ff6 2019-07-12 stsp }
5038 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
5039 c4296144 2019-05-09 stsp return err;
5040 8656d6c4 2019-05-20 stsp }
5041 8656d6c4 2019-05-20 stsp
5042 8656d6c4 2019-05-20 stsp const char *
5043 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
5044 8656d6c4 2019-05-20 stsp {
5045 8656d6c4 2019-05-20 stsp return ct->path;
5046 8656d6c4 2019-05-20 stsp }
5047 8656d6c4 2019-05-20 stsp
5048 8656d6c4 2019-05-20 stsp unsigned int
5049 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
5050 8656d6c4 2019-05-20 stsp {
5051 8656d6c4 2019-05-20 stsp return ct->status;
5052 818c7501 2019-07-11 stsp }
5053 818c7501 2019-07-11 stsp
5054 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
5055 818c7501 2019-07-11 stsp struct got_worktree *worktree;
5056 818c7501 2019-07-11 stsp struct got_repository *repo;
5057 818c7501 2019-07-11 stsp };
5058 818c7501 2019-07-11 stsp
5059 818c7501 2019-07-11 stsp static const struct got_error *
5060 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5061 818c7501 2019-07-11 stsp {
5062 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5063 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
5064 818c7501 2019-07-11 stsp unsigned char status;
5065 818c7501 2019-07-11 stsp struct stat sb;
5066 818c7501 2019-07-11 stsp char *ondisk_path;
5067 818c7501 2019-07-11 stsp
5068 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
5069 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5070 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
5071 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
5072 818c7501 2019-07-11 stsp
5073 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5074 818c7501 2019-07-11 stsp == -1)
5075 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
5076 818c7501 2019-07-11 stsp
5077 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
5078 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5079 818c7501 2019-07-11 stsp free(ondisk_path);
5080 818c7501 2019-07-11 stsp if (err)
5081 818c7501 2019-07-11 stsp return err;
5082 818c7501 2019-07-11 stsp
5083 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
5084 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
5085 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5086 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5087 818c7501 2019-07-11 stsp
5088 818c7501 2019-07-11 stsp return NULL;
5089 818c7501 2019-07-11 stsp }
5090 818c7501 2019-07-11 stsp
5091 818c7501 2019-07-11 stsp const struct got_error *
5092 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5093 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5094 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5095 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5096 818c7501 2019-07-11 stsp {
5097 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5098 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5099 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5100 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5101 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5102 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5103 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5104 818c7501 2019-07-11 stsp
5105 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5106 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5107 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5108 818c7501 2019-07-11 stsp
5109 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5110 818c7501 2019-07-11 stsp if (err)
5111 818c7501 2019-07-11 stsp return err;
5112 818c7501 2019-07-11 stsp
5113 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5114 818c7501 2019-07-11 stsp if (err)
5115 818c7501 2019-07-11 stsp goto done;
5116 818c7501 2019-07-11 stsp
5117 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5118 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5119 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5120 818c7501 2019-07-11 stsp &ok_arg);
5121 818c7501 2019-07-11 stsp if (err)
5122 818c7501 2019-07-11 stsp goto done;
5123 818c7501 2019-07-11 stsp
5124 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5125 818c7501 2019-07-11 stsp if (err)
5126 818c7501 2019-07-11 stsp goto done;
5127 818c7501 2019-07-11 stsp
5128 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5129 818c7501 2019-07-11 stsp if (err)
5130 818c7501 2019-07-11 stsp goto done;
5131 818c7501 2019-07-11 stsp
5132 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5133 818c7501 2019-07-11 stsp if (err)
5134 818c7501 2019-07-11 stsp goto done;
5135 818c7501 2019-07-11 stsp
5136 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5137 818c7501 2019-07-11 stsp 0);
5138 e51d7b55 2020-01-04 stsp if (err)
5139 e51d7b55 2020-01-04 stsp goto done;
5140 e51d7b55 2020-01-04 stsp
5141 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5142 818c7501 2019-07-11 stsp if (err)
5143 818c7501 2019-07-11 stsp goto done;
5144 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5145 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5146 e51d7b55 2020-01-04 stsp goto done;
5147 e51d7b55 2020-01-04 stsp }
5148 818c7501 2019-07-11 stsp
5149 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5150 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5151 818c7501 2019-07-11 stsp if (err)
5152 818c7501 2019-07-11 stsp goto done;
5153 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5154 818c7501 2019-07-11 stsp if (err)
5155 818c7501 2019-07-11 stsp goto done;
5156 818c7501 2019-07-11 stsp
5157 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5158 818c7501 2019-07-11 stsp
5159 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5160 818c7501 2019-07-11 stsp if (err)
5161 818c7501 2019-07-11 stsp goto done;
5162 818c7501 2019-07-11 stsp
5163 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5164 818c7501 2019-07-11 stsp if (err)
5165 818c7501 2019-07-11 stsp goto done;
5166 818c7501 2019-07-11 stsp
5167 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5168 818c7501 2019-07-11 stsp worktree->base_commit_id);
5169 818c7501 2019-07-11 stsp if (err)
5170 818c7501 2019-07-11 stsp goto done;
5171 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5172 818c7501 2019-07-11 stsp if (err)
5173 818c7501 2019-07-11 stsp goto done;
5174 818c7501 2019-07-11 stsp
5175 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5176 818c7501 2019-07-11 stsp if (err)
5177 818c7501 2019-07-11 stsp goto done;
5178 818c7501 2019-07-11 stsp done:
5179 818c7501 2019-07-11 stsp free(fileindex_path);
5180 818c7501 2019-07-11 stsp free(tmp_branch_name);
5181 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5182 818c7501 2019-07-11 stsp free(branch_ref_name);
5183 818c7501 2019-07-11 stsp if (branch_ref)
5184 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5185 818c7501 2019-07-11 stsp if (wt_branch)
5186 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5187 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5188 818c7501 2019-07-11 stsp if (err) {
5189 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5190 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5191 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5192 818c7501 2019-07-11 stsp }
5193 818c7501 2019-07-11 stsp if (*tmp_branch) {
5194 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5195 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5196 818c7501 2019-07-11 stsp }
5197 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5198 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5199 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5200 3e3a69f1 2019-07-25 stsp }
5201 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5202 818c7501 2019-07-11 stsp }
5203 818c7501 2019-07-11 stsp return err;
5204 818c7501 2019-07-11 stsp }
5205 818c7501 2019-07-11 stsp
5206 818c7501 2019-07-11 stsp const struct got_error *
5207 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5208 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5209 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5210 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5211 818c7501 2019-07-11 stsp {
5212 818c7501 2019-07-11 stsp const struct got_error *err;
5213 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5214 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5215 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5216 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5217 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5218 818c7501 2019-07-11 stsp
5219 818c7501 2019-07-11 stsp *commit_id = NULL;
5220 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5221 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5222 3e3a69f1 2019-07-25 stsp *branch = NULL;
5223 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5224 3e3a69f1 2019-07-25 stsp
5225 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5226 3e3a69f1 2019-07-25 stsp if (err)
5227 3e3a69f1 2019-07-25 stsp return err;
5228 818c7501 2019-07-11 stsp
5229 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5230 3e3a69f1 2019-07-25 stsp if (err)
5231 f032f1f7 2019-08-04 stsp goto done;
5232 f032f1f7 2019-08-04 stsp
5233 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5234 f032f1f7 2019-08-04 stsp &have_staged_files);
5235 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5236 f032f1f7 2019-08-04 stsp goto done;
5237 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5238 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5239 3e3a69f1 2019-07-25 stsp goto done;
5240 f032f1f7 2019-08-04 stsp }
5241 3e3a69f1 2019-07-25 stsp
5242 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5243 818c7501 2019-07-11 stsp if (err)
5244 f032f1f7 2019-08-04 stsp goto done;
5245 818c7501 2019-07-11 stsp
5246 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5247 818c7501 2019-07-11 stsp if (err)
5248 818c7501 2019-07-11 stsp goto done;
5249 818c7501 2019-07-11 stsp
5250 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5251 818c7501 2019-07-11 stsp if (err)
5252 818c7501 2019-07-11 stsp goto done;
5253 818c7501 2019-07-11 stsp
5254 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5255 818c7501 2019-07-11 stsp if (err)
5256 818c7501 2019-07-11 stsp goto done;
5257 818c7501 2019-07-11 stsp
5258 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5259 818c7501 2019-07-11 stsp if (err)
5260 818c7501 2019-07-11 stsp goto done;
5261 818c7501 2019-07-11 stsp
5262 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5263 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5264 818c7501 2019-07-11 stsp if (err)
5265 818c7501 2019-07-11 stsp goto done;
5266 818c7501 2019-07-11 stsp
5267 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5268 818c7501 2019-07-11 stsp if (err)
5269 818c7501 2019-07-11 stsp goto done;
5270 818c7501 2019-07-11 stsp
5271 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5272 818c7501 2019-07-11 stsp if (err)
5273 818c7501 2019-07-11 stsp goto done;
5274 818c7501 2019-07-11 stsp
5275 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5276 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5277 818c7501 2019-07-11 stsp if (err)
5278 818c7501 2019-07-11 stsp goto done;
5279 818c7501 2019-07-11 stsp
5280 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5281 818c7501 2019-07-11 stsp if (err)
5282 818c7501 2019-07-11 stsp goto done;
5283 818c7501 2019-07-11 stsp done:
5284 818c7501 2019-07-11 stsp free(commit_ref_name);
5285 818c7501 2019-07-11 stsp free(branch_ref_name);
5286 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5287 818c7501 2019-07-11 stsp if (commit_ref)
5288 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5289 818c7501 2019-07-11 stsp if (branch_ref)
5290 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5291 818c7501 2019-07-11 stsp if (err) {
5292 818c7501 2019-07-11 stsp free(*commit_id);
5293 818c7501 2019-07-11 stsp *commit_id = NULL;
5294 818c7501 2019-07-11 stsp if (*tmp_branch) {
5295 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5296 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5297 818c7501 2019-07-11 stsp }
5298 818c7501 2019-07-11 stsp if (*new_base_branch) {
5299 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5300 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5301 818c7501 2019-07-11 stsp }
5302 818c7501 2019-07-11 stsp if (*branch) {
5303 818c7501 2019-07-11 stsp got_ref_close(*branch);
5304 818c7501 2019-07-11 stsp *branch = NULL;
5305 818c7501 2019-07-11 stsp }
5306 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5307 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5308 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5309 3e3a69f1 2019-07-25 stsp }
5310 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5311 818c7501 2019-07-11 stsp }
5312 818c7501 2019-07-11 stsp return err;
5313 c4296144 2019-05-09 stsp }
5314 818c7501 2019-07-11 stsp
5315 818c7501 2019-07-11 stsp const struct got_error *
5316 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5317 818c7501 2019-07-11 stsp {
5318 818c7501 2019-07-11 stsp const struct got_error *err;
5319 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5320 818c7501 2019-07-11 stsp
5321 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5322 818c7501 2019-07-11 stsp if (err)
5323 818c7501 2019-07-11 stsp return err;
5324 818c7501 2019-07-11 stsp
5325 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5326 818c7501 2019-07-11 stsp free(tmp_branch_name);
5327 818c7501 2019-07-11 stsp return NULL;
5328 818c7501 2019-07-11 stsp }
5329 818c7501 2019-07-11 stsp
5330 818c7501 2019-07-11 stsp static const struct got_error *
5331 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5332 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5333 818c7501 2019-07-11 stsp {
5334 0ebf8283 2019-07-24 stsp *logmsg = arg;
5335 818c7501 2019-07-11 stsp return NULL;
5336 818c7501 2019-07-11 stsp }
5337 818c7501 2019-07-11 stsp
5338 818c7501 2019-07-11 stsp static const struct got_error *
5339 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5340 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5341 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5342 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5343 818c7501 2019-07-11 stsp {
5344 818c7501 2019-07-11 stsp return NULL;
5345 01757395 2019-07-12 stsp }
5346 01757395 2019-07-12 stsp
5347 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5348 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5349 01757395 2019-07-12 stsp void *progress_arg;
5350 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5351 01757395 2019-07-12 stsp };
5352 01757395 2019-07-12 stsp
5353 01757395 2019-07-12 stsp static const struct got_error *
5354 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5355 01757395 2019-07-12 stsp {
5356 01757395 2019-07-12 stsp const struct got_error *err;
5357 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5358 01757395 2019-07-12 stsp char *p;
5359 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5360 01757395 2019-07-12 stsp
5361 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5362 01757395 2019-07-12 stsp if (err)
5363 01757395 2019-07-12 stsp return err;
5364 01757395 2019-07-12 stsp
5365 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5366 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5367 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5368 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5369 01757395 2019-07-12 stsp return NULL;
5370 01757395 2019-07-12 stsp
5371 01757395 2019-07-12 stsp p = strdup(path);
5372 01757395 2019-07-12 stsp if (p == NULL)
5373 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5374 01757395 2019-07-12 stsp
5375 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5376 01757395 2019-07-12 stsp if (err || new == NULL)
5377 01757395 2019-07-12 stsp free(p);
5378 01757395 2019-07-12 stsp return err;
5379 01757395 2019-07-12 stsp }
5380 01757395 2019-07-12 stsp
5381 01757395 2019-07-12 stsp void
5382 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5383 01757395 2019-07-12 stsp {
5384 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5385 01757395 2019-07-12 stsp
5386 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5387 01757395 2019-07-12 stsp free((char *)pe->path);
5388 01757395 2019-07-12 stsp
5389 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5390 818c7501 2019-07-11 stsp }
5391 818c7501 2019-07-11 stsp
5392 0ebf8283 2019-07-24 stsp static const struct got_error *
5393 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5394 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5395 818c7501 2019-07-11 stsp {
5396 818c7501 2019-07-11 stsp const struct got_error *err;
5397 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5398 818c7501 2019-07-11 stsp
5399 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5400 818c7501 2019-07-11 stsp if (err) {
5401 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5402 818c7501 2019-07-11 stsp goto done;
5403 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5404 818c7501 2019-07-11 stsp if (err)
5405 818c7501 2019-07-11 stsp goto done;
5406 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5407 818c7501 2019-07-11 stsp if (err)
5408 818c7501 2019-07-11 stsp goto done;
5409 de05890f 2020-03-05 stsp } else if (is_rebase) {
5410 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5411 818c7501 2019-07-11 stsp int cmp;
5412 818c7501 2019-07-11 stsp
5413 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5414 818c7501 2019-07-11 stsp if (err)
5415 818c7501 2019-07-11 stsp goto done;
5416 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5417 818c7501 2019-07-11 stsp free(stored_id);
5418 818c7501 2019-07-11 stsp if (cmp != 0) {
5419 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5420 818c7501 2019-07-11 stsp goto done;
5421 818c7501 2019-07-11 stsp }
5422 818c7501 2019-07-11 stsp }
5423 0ebf8283 2019-07-24 stsp done:
5424 0ebf8283 2019-07-24 stsp if (commit_ref)
5425 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5426 0ebf8283 2019-07-24 stsp return err;
5427 0ebf8283 2019-07-24 stsp }
5428 0ebf8283 2019-07-24 stsp
5429 0ebf8283 2019-07-24 stsp static const struct got_error *
5430 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5431 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5432 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5433 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5434 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5435 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5436 0ebf8283 2019-07-24 stsp {
5437 0ebf8283 2019-07-24 stsp const struct got_error *err;
5438 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5439 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5440 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5441 818c7501 2019-07-11 stsp
5442 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5443 0ebf8283 2019-07-24 stsp
5444 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5445 0ebf8283 2019-07-24 stsp if (err)
5446 0ebf8283 2019-07-24 stsp return err;
5447 0ebf8283 2019-07-24 stsp
5448 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5449 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5450 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5451 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5452 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5453 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5454 818c7501 2019-07-11 stsp if (commit_ref)
5455 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5456 818c7501 2019-07-11 stsp return err;
5457 818c7501 2019-07-11 stsp }
5458 818c7501 2019-07-11 stsp
5459 818c7501 2019-07-11 stsp const struct got_error *
5460 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5461 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5462 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5463 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5464 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5465 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5466 818c7501 2019-07-11 stsp {
5467 0ebf8283 2019-07-24 stsp const struct got_error *err;
5468 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5469 0ebf8283 2019-07-24 stsp
5470 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5471 0ebf8283 2019-07-24 stsp if (err)
5472 0ebf8283 2019-07-24 stsp return err;
5473 0ebf8283 2019-07-24 stsp
5474 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5475 0ebf8283 2019-07-24 stsp if (err)
5476 0ebf8283 2019-07-24 stsp goto done;
5477 0ebf8283 2019-07-24 stsp
5478 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5479 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5480 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5481 0ebf8283 2019-07-24 stsp done:
5482 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5483 0ebf8283 2019-07-24 stsp return err;
5484 0ebf8283 2019-07-24 stsp }
5485 0ebf8283 2019-07-24 stsp
5486 0ebf8283 2019-07-24 stsp const struct got_error *
5487 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5488 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5489 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5490 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5491 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5492 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5493 0ebf8283 2019-07-24 stsp {
5494 0ebf8283 2019-07-24 stsp const struct got_error *err;
5495 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5496 0ebf8283 2019-07-24 stsp
5497 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5498 0ebf8283 2019-07-24 stsp if (err)
5499 0ebf8283 2019-07-24 stsp return err;
5500 0ebf8283 2019-07-24 stsp
5501 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5502 0ebf8283 2019-07-24 stsp if (err)
5503 0ebf8283 2019-07-24 stsp goto done;
5504 0ebf8283 2019-07-24 stsp
5505 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5506 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5507 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5508 0ebf8283 2019-07-24 stsp done:
5509 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5510 0ebf8283 2019-07-24 stsp return err;
5511 0ebf8283 2019-07-24 stsp }
5512 0ebf8283 2019-07-24 stsp
5513 0ebf8283 2019-07-24 stsp static const struct got_error *
5514 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
5515 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5516 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5517 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5518 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
5519 0ebf8283 2019-07-24 stsp {
5520 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
5521 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5522 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5523 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5524 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
5525 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5526 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
5527 818c7501 2019-07-11 stsp
5528 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5529 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
5530 a0e95631 2019-07-12 stsp
5531 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5532 818c7501 2019-07-11 stsp
5533 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5534 a0e95631 2019-07-12 stsp if (err)
5535 3e3a69f1 2019-07-25 stsp return err;
5536 a0e95631 2019-07-12 stsp
5537 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5538 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
5539 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
5540 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
5541 01757395 2019-07-12 stsp /*
5542 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
5543 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
5544 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
5545 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
5546 01757395 2019-07-12 stsp */
5547 01757395 2019-07-12 stsp if (merged_paths) {
5548 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5549 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
5550 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
5551 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5552 f2a9dc41 2019-12-13 tracey 0);
5553 01757395 2019-07-12 stsp if (err)
5554 01757395 2019-07-12 stsp goto done;
5555 01757395 2019-07-12 stsp }
5556 01757395 2019-07-12 stsp } else {
5557 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5558 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5559 01757395 2019-07-12 stsp if (err)
5560 01757395 2019-07-12 stsp goto done;
5561 01757395 2019-07-12 stsp }
5562 a0e95631 2019-07-12 stsp
5563 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5564 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
5565 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5566 a0e95631 2019-07-12 stsp if (err)
5567 a0e95631 2019-07-12 stsp goto done;
5568 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5569 a0e95631 2019-07-12 stsp goto done;
5570 ff0d2220 2019-07-11 stsp }
5571 818c7501 2019-07-11 stsp
5572 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5573 edd02c5e 2019-07-11 stsp if (err)
5574 edd02c5e 2019-07-11 stsp goto done;
5575 edd02c5e 2019-07-11 stsp
5576 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5577 818c7501 2019-07-11 stsp if (err)
5578 818c7501 2019-07-11 stsp goto done;
5579 818c7501 2019-07-11 stsp
5580 5943eee2 2019-08-13 stsp if (new_logmsg) {
5581 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
5582 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
5583 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5584 5943eee2 2019-08-13 stsp goto done;
5585 5943eee2 2019-08-13 stsp }
5586 5943eee2 2019-08-13 stsp } else {
5587 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5588 5943eee2 2019-08-13 stsp if (err)
5589 5943eee2 2019-08-13 stsp goto done;
5590 5943eee2 2019-08-13 stsp }
5591 0ebf8283 2019-07-24 stsp
5592 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
5593 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5594 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
5595 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
5596 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5597 a0e95631 2019-07-12 stsp if (err)
5598 a0e95631 2019-07-12 stsp goto done;
5599 a0e95631 2019-07-12 stsp
5600 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
5601 a0e95631 2019-07-12 stsp if (err)
5602 a0e95631 2019-07-12 stsp goto done;
5603 a0e95631 2019-07-12 stsp
5604 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5605 a0e95631 2019-07-12 stsp if (err)
5606 a0e95631 2019-07-12 stsp goto done;
5607 a0e95631 2019-07-12 stsp
5608 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5609 72fd46fa 2019-09-06 stsp fileindex, 0);
5610 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5611 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
5612 a0e95631 2019-07-12 stsp err = sync_err;
5613 818c7501 2019-07-11 stsp done:
5614 a0e95631 2019-07-12 stsp free(fileindex_path);
5615 a0e95631 2019-07-12 stsp free(head_commit_id);
5616 a0e95631 2019-07-12 stsp if (head_ref)
5617 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
5618 818c7501 2019-07-11 stsp if (err) {
5619 818c7501 2019-07-11 stsp free(*new_commit_id);
5620 818c7501 2019-07-11 stsp *new_commit_id = NULL;
5621 818c7501 2019-07-11 stsp }
5622 818c7501 2019-07-11 stsp return err;
5623 818c7501 2019-07-11 stsp }
5624 818c7501 2019-07-11 stsp
5625 818c7501 2019-07-11 stsp const struct got_error *
5626 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5627 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5628 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5629 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5630 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
5631 0ebf8283 2019-07-24 stsp {
5632 0ebf8283 2019-07-24 stsp const struct got_error *err;
5633 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5634 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5635 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5636 0ebf8283 2019-07-24 stsp
5637 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5638 0ebf8283 2019-07-24 stsp if (err)
5639 0ebf8283 2019-07-24 stsp return err;
5640 0ebf8283 2019-07-24 stsp
5641 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5642 0ebf8283 2019-07-24 stsp if (err)
5643 0ebf8283 2019-07-24 stsp goto done;
5644 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
5645 0ebf8283 2019-07-24 stsp if (err)
5646 0ebf8283 2019-07-24 stsp goto done;
5647 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5648 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5649 0ebf8283 2019-07-24 stsp goto done;
5650 0ebf8283 2019-07-24 stsp }
5651 0ebf8283 2019-07-24 stsp
5652 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5653 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5654 0ebf8283 2019-07-24 stsp done:
5655 0ebf8283 2019-07-24 stsp if (commit_ref)
5656 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5657 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5658 0ebf8283 2019-07-24 stsp free(commit_id);
5659 0ebf8283 2019-07-24 stsp return err;
5660 0ebf8283 2019-07-24 stsp }
5661 0ebf8283 2019-07-24 stsp
5662 0ebf8283 2019-07-24 stsp const struct got_error *
5663 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5664 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5665 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5666 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5667 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
5668 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5669 0ebf8283 2019-07-24 stsp {
5670 0ebf8283 2019-07-24 stsp const struct got_error *err;
5671 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5672 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5673 0ebf8283 2019-07-24 stsp
5674 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5675 0ebf8283 2019-07-24 stsp if (err)
5676 0ebf8283 2019-07-24 stsp return err;
5677 0ebf8283 2019-07-24 stsp
5678 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5679 0ebf8283 2019-07-24 stsp if (err)
5680 0ebf8283 2019-07-24 stsp goto done;
5681 0ebf8283 2019-07-24 stsp
5682 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5683 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5684 0ebf8283 2019-07-24 stsp done:
5685 0ebf8283 2019-07-24 stsp if (commit_ref)
5686 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5687 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5688 0ebf8283 2019-07-24 stsp return err;
5689 0ebf8283 2019-07-24 stsp }
5690 0ebf8283 2019-07-24 stsp
5691 0ebf8283 2019-07-24 stsp const struct got_error *
5692 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
5693 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5694 818c7501 2019-07-11 stsp {
5695 3e3a69f1 2019-07-25 stsp if (fileindex)
5696 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5697 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
5698 69844fba 2019-07-11 stsp }
5699 69844fba 2019-07-11 stsp
5700 69844fba 2019-07-11 stsp static const struct got_error *
5701 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
5702 69844fba 2019-07-11 stsp {
5703 69844fba 2019-07-11 stsp const struct got_error *err;
5704 69844fba 2019-07-11 stsp struct got_reference *ref;
5705 69844fba 2019-07-11 stsp
5706 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
5707 69844fba 2019-07-11 stsp if (err) {
5708 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
5709 69844fba 2019-07-11 stsp return NULL;
5710 69844fba 2019-07-11 stsp return err;
5711 69844fba 2019-07-11 stsp }
5712 69844fba 2019-07-11 stsp
5713 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
5714 69844fba 2019-07-11 stsp got_ref_close(ref);
5715 69844fba 2019-07-11 stsp return err;
5716 818c7501 2019-07-11 stsp }
5717 818c7501 2019-07-11 stsp
5718 69844fba 2019-07-11 stsp static const struct got_error *
5719 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5720 69844fba 2019-07-11 stsp {
5721 69844fba 2019-07-11 stsp const struct got_error *err;
5722 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5723 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
5724 69844fba 2019-07-11 stsp
5725 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5726 69844fba 2019-07-11 stsp if (err)
5727 69844fba 2019-07-11 stsp goto done;
5728 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
5729 69844fba 2019-07-11 stsp if (err)
5730 69844fba 2019-07-11 stsp goto done;
5731 69844fba 2019-07-11 stsp
5732 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5733 69844fba 2019-07-11 stsp if (err)
5734 69844fba 2019-07-11 stsp goto done;
5735 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
5736 69844fba 2019-07-11 stsp if (err)
5737 69844fba 2019-07-11 stsp goto done;
5738 69844fba 2019-07-11 stsp
5739 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5740 69844fba 2019-07-11 stsp if (err)
5741 69844fba 2019-07-11 stsp goto done;
5742 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
5743 69844fba 2019-07-11 stsp if (err)
5744 69844fba 2019-07-11 stsp goto done;
5745 69844fba 2019-07-11 stsp
5746 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5747 69844fba 2019-07-11 stsp if (err)
5748 69844fba 2019-07-11 stsp goto done;
5749 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
5750 69844fba 2019-07-11 stsp if (err)
5751 69844fba 2019-07-11 stsp goto done;
5752 69844fba 2019-07-11 stsp
5753 69844fba 2019-07-11 stsp done:
5754 69844fba 2019-07-11 stsp free(tmp_branch_name);
5755 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
5756 69844fba 2019-07-11 stsp free(branch_ref_name);
5757 69844fba 2019-07-11 stsp free(commit_ref_name);
5758 69844fba 2019-07-11 stsp return err;
5759 69844fba 2019-07-11 stsp }
5760 69844fba 2019-07-11 stsp
5761 818c7501 2019-07-11 stsp const struct got_error *
5762 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
5763 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5764 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5765 818c7501 2019-07-11 stsp struct got_repository *repo)
5766 818c7501 2019-07-11 stsp {
5767 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
5768 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
5769 818c7501 2019-07-11 stsp
5770 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5771 818c7501 2019-07-11 stsp if (err)
5772 818c7501 2019-07-11 stsp return err;
5773 818c7501 2019-07-11 stsp
5774 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5775 818c7501 2019-07-11 stsp if (err)
5776 818c7501 2019-07-11 stsp goto done;
5777 818c7501 2019-07-11 stsp
5778 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
5779 818c7501 2019-07-11 stsp if (err)
5780 818c7501 2019-07-11 stsp goto done;
5781 818c7501 2019-07-11 stsp
5782 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
5783 818c7501 2019-07-11 stsp if (err)
5784 818c7501 2019-07-11 stsp goto done;
5785 818c7501 2019-07-11 stsp
5786 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
5787 818c7501 2019-07-11 stsp done:
5788 3e3a69f1 2019-07-25 stsp if (fileindex)
5789 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5790 818c7501 2019-07-11 stsp free(new_head_commit_id);
5791 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5792 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5793 818c7501 2019-07-11 stsp err = unlockerr;
5794 818c7501 2019-07-11 stsp return err;
5795 818c7501 2019-07-11 stsp }
5796 818c7501 2019-07-11 stsp
5797 818c7501 2019-07-11 stsp const struct got_error *
5798 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
5799 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5800 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
5801 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
5802 818c7501 2019-07-11 stsp {
5803 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
5804 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
5805 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
5806 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5807 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5808 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
5809 818c7501 2019-07-11 stsp
5810 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5811 818c7501 2019-07-11 stsp if (err)
5812 818c7501 2019-07-11 stsp return err;
5813 818c7501 2019-07-11 stsp
5814 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
5815 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
5816 818c7501 2019-07-11 stsp if (err)
5817 818c7501 2019-07-11 stsp goto done;
5818 818c7501 2019-07-11 stsp
5819 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
5820 818c7501 2019-07-11 stsp if (err)
5821 818c7501 2019-07-11 stsp goto done;
5822 818c7501 2019-07-11 stsp
5823 818c7501 2019-07-11 stsp /*
5824 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
5825 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
5826 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
5827 818c7501 2019-07-11 stsp */
5828 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
5829 818c7501 2019-07-11 stsp if (err)
5830 818c7501 2019-07-11 stsp goto done;
5831 818c7501 2019-07-11 stsp
5832 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5833 818c7501 2019-07-11 stsp if (err)
5834 818c7501 2019-07-11 stsp goto done;
5835 818c7501 2019-07-11 stsp
5836 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
5837 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
5838 ca355955 2019-07-12 stsp if (err)
5839 ca355955 2019-07-12 stsp goto done;
5840 ca355955 2019-07-12 stsp
5841 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
5842 ca355955 2019-07-12 stsp if (err)
5843 ca355955 2019-07-12 stsp goto done;
5844 ca355955 2019-07-12 stsp
5845 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5846 818c7501 2019-07-11 stsp if (err)
5847 818c7501 2019-07-11 stsp goto done;
5848 818c7501 2019-07-11 stsp
5849 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5850 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5851 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5852 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5853 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
5854 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
5855 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5856 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5857 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
5858 55bd499d 2019-07-12 stsp if (err)
5859 1f1abb7e 2019-08-08 stsp goto sync;
5860 55bd499d 2019-07-12 stsp
5861 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5862 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
5863 ca355955 2019-07-12 stsp sync:
5864 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5865 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
5866 ca355955 2019-07-12 stsp err = sync_err;
5867 818c7501 2019-07-11 stsp done:
5868 818c7501 2019-07-11 stsp got_ref_close(resolved);
5869 a3a2faf2 2019-07-12 stsp free(tree_id);
5870 818c7501 2019-07-11 stsp free(commit_id);
5871 0ebf8283 2019-07-24 stsp if (fileindex)
5872 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
5873 0ebf8283 2019-07-24 stsp free(fileindex_path);
5874 0ebf8283 2019-07-24 stsp
5875 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5876 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5877 0ebf8283 2019-07-24 stsp err = unlockerr;
5878 0ebf8283 2019-07-24 stsp return err;
5879 0ebf8283 2019-07-24 stsp }
5880 0ebf8283 2019-07-24 stsp
5881 0ebf8283 2019-07-24 stsp const struct got_error *
5882 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5883 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5884 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
5885 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5886 0ebf8283 2019-07-24 stsp {
5887 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5888 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5889 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
5890 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
5891 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5892 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
5893 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
5894 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5895 0ebf8283 2019-07-24 stsp
5896 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5897 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5898 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5899 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5900 0ebf8283 2019-07-24 stsp
5901 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5902 0ebf8283 2019-07-24 stsp if (err)
5903 0ebf8283 2019-07-24 stsp return err;
5904 0ebf8283 2019-07-24 stsp
5905 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5906 0ebf8283 2019-07-24 stsp if (err)
5907 0ebf8283 2019-07-24 stsp goto done;
5908 0ebf8283 2019-07-24 stsp
5909 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
5910 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
5911 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5912 0ebf8283 2019-07-24 stsp &ok_arg);
5913 0ebf8283 2019-07-24 stsp if (err)
5914 0ebf8283 2019-07-24 stsp goto done;
5915 0ebf8283 2019-07-24 stsp
5916 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5917 0ebf8283 2019-07-24 stsp if (err)
5918 0ebf8283 2019-07-24 stsp goto done;
5919 0ebf8283 2019-07-24 stsp
5920 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5921 0ebf8283 2019-07-24 stsp if (err)
5922 0ebf8283 2019-07-24 stsp goto done;
5923 0ebf8283 2019-07-24 stsp
5924 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5925 0ebf8283 2019-07-24 stsp worktree);
5926 0ebf8283 2019-07-24 stsp if (err)
5927 0ebf8283 2019-07-24 stsp goto done;
5928 0ebf8283 2019-07-24 stsp
5929 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5930 0ebf8283 2019-07-24 stsp 0);
5931 0ebf8283 2019-07-24 stsp if (err)
5932 0ebf8283 2019-07-24 stsp goto done;
5933 0ebf8283 2019-07-24 stsp
5934 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5935 0ebf8283 2019-07-24 stsp if (err)
5936 0ebf8283 2019-07-24 stsp goto done;
5937 0ebf8283 2019-07-24 stsp
5938 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
5939 0ebf8283 2019-07-24 stsp if (err)
5940 0ebf8283 2019-07-24 stsp goto done;
5941 0ebf8283 2019-07-24 stsp
5942 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5943 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5944 0ebf8283 2019-07-24 stsp if (err)
5945 0ebf8283 2019-07-24 stsp goto done;
5946 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
5947 0ebf8283 2019-07-24 stsp if (err)
5948 0ebf8283 2019-07-24 stsp goto done;
5949 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5950 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
5951 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
5952 0ebf8283 2019-07-24 stsp goto done;
5953 0ebf8283 2019-07-24 stsp }
5954 0ebf8283 2019-07-24 stsp
5955 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5956 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5957 0ebf8283 2019-07-24 stsp if (err)
5958 0ebf8283 2019-07-24 stsp goto done;
5959 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
5960 0ebf8283 2019-07-24 stsp if (err)
5961 0ebf8283 2019-07-24 stsp goto done;
5962 0ebf8283 2019-07-24 stsp
5963 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5964 0ebf8283 2019-07-24 stsp if (err)
5965 0ebf8283 2019-07-24 stsp goto done;
5966 0ebf8283 2019-07-24 stsp done:
5967 0ebf8283 2019-07-24 stsp free(fileindex_path);
5968 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5969 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5970 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5971 0ebf8283 2019-07-24 stsp if (wt_branch)
5972 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
5973 0ebf8283 2019-07-24 stsp if (err) {
5974 0ebf8283 2019-07-24 stsp if (*branch_ref) {
5975 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
5976 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5977 0ebf8283 2019-07-24 stsp }
5978 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5979 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5980 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5981 0ebf8283 2019-07-24 stsp }
5982 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5983 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5984 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5985 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5986 3e3a69f1 2019-07-25 stsp }
5987 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
5988 0ebf8283 2019-07-24 stsp }
5989 0ebf8283 2019-07-24 stsp return err;
5990 0ebf8283 2019-07-24 stsp }
5991 0ebf8283 2019-07-24 stsp
5992 0ebf8283 2019-07-24 stsp const struct got_error *
5993 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
5994 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5995 0ebf8283 2019-07-24 stsp {
5996 3e3a69f1 2019-07-25 stsp if (fileindex)
5997 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5998 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
5999 0ebf8283 2019-07-24 stsp }
6000 0ebf8283 2019-07-24 stsp
6001 0ebf8283 2019-07-24 stsp const struct got_error *
6002 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
6003 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
6004 0ebf8283 2019-07-24 stsp {
6005 0ebf8283 2019-07-24 stsp const struct got_error *err;
6006 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
6007 0ebf8283 2019-07-24 stsp
6008 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6009 0ebf8283 2019-07-24 stsp if (err)
6010 0ebf8283 2019-07-24 stsp return err;
6011 0ebf8283 2019-07-24 stsp
6012 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6013 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6014 0ebf8283 2019-07-24 stsp return NULL;
6015 0ebf8283 2019-07-24 stsp }
6016 0ebf8283 2019-07-24 stsp
6017 0ebf8283 2019-07-24 stsp const struct got_error *
6018 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
6019 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
6020 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6021 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6022 0ebf8283 2019-07-24 stsp {
6023 0ebf8283 2019-07-24 stsp const struct got_error *err;
6024 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6025 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6026 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
6027 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
6028 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
6029 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
6030 0ebf8283 2019-07-24 stsp
6031 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6032 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6033 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6034 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6035 0ebf8283 2019-07-24 stsp
6036 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
6037 3e3a69f1 2019-07-25 stsp if (err)
6038 3e3a69f1 2019-07-25 stsp return err;
6039 3e3a69f1 2019-07-25 stsp
6040 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6041 3e3a69f1 2019-07-25 stsp if (err)
6042 f032f1f7 2019-08-04 stsp goto done;
6043 f032f1f7 2019-08-04 stsp
6044 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6045 f032f1f7 2019-08-04 stsp &have_staged_files);
6046 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
6047 f032f1f7 2019-08-04 stsp goto done;
6048 f032f1f7 2019-08-04 stsp if (have_staged_files) {
6049 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
6050 3e3a69f1 2019-07-25 stsp goto done;
6051 f032f1f7 2019-08-04 stsp }
6052 3e3a69f1 2019-07-25 stsp
6053 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6054 0ebf8283 2019-07-24 stsp if (err)
6055 f032f1f7 2019-08-04 stsp goto done;
6056 0ebf8283 2019-07-24 stsp
6057 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6058 0ebf8283 2019-07-24 stsp if (err)
6059 0ebf8283 2019-07-24 stsp goto done;
6060 0ebf8283 2019-07-24 stsp
6061 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6062 0ebf8283 2019-07-24 stsp if (err)
6063 0ebf8283 2019-07-24 stsp goto done;
6064 0ebf8283 2019-07-24 stsp
6065 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6066 0ebf8283 2019-07-24 stsp worktree);
6067 0ebf8283 2019-07-24 stsp if (err)
6068 0ebf8283 2019-07-24 stsp goto done;
6069 0ebf8283 2019-07-24 stsp
6070 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6071 0ebf8283 2019-07-24 stsp if (err)
6072 0ebf8283 2019-07-24 stsp goto done;
6073 0ebf8283 2019-07-24 stsp
6074 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6075 0ebf8283 2019-07-24 stsp if (err)
6076 0ebf8283 2019-07-24 stsp goto done;
6077 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
6078 0ebf8283 2019-07-24 stsp if (err)
6079 0ebf8283 2019-07-24 stsp goto done;
6080 0ebf8283 2019-07-24 stsp
6081 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6082 0ebf8283 2019-07-24 stsp if (err)
6083 0ebf8283 2019-07-24 stsp goto done;
6084 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6085 0ebf8283 2019-07-24 stsp if (err)
6086 0ebf8283 2019-07-24 stsp goto done;
6087 0ebf8283 2019-07-24 stsp
6088 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6089 0ebf8283 2019-07-24 stsp if (err)
6090 0ebf8283 2019-07-24 stsp goto done;
6091 0ebf8283 2019-07-24 stsp done:
6092 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6093 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6094 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6095 0ebf8283 2019-07-24 stsp if (commit_ref)
6096 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6097 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6098 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6099 0ebf8283 2019-07-24 stsp if (err) {
6100 0ebf8283 2019-07-24 stsp free(*commit_id);
6101 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6102 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6103 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6104 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6105 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6106 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6107 0ebf8283 2019-07-24 stsp }
6108 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6109 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6110 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6111 3e3a69f1 2019-07-25 stsp }
6112 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6113 0ebf8283 2019-07-24 stsp }
6114 0ebf8283 2019-07-24 stsp return err;
6115 0ebf8283 2019-07-24 stsp }
6116 0ebf8283 2019-07-24 stsp
6117 0ebf8283 2019-07-24 stsp static const struct got_error *
6118 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6119 0ebf8283 2019-07-24 stsp {
6120 0ebf8283 2019-07-24 stsp const struct got_error *err;
6121 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6122 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6123 0ebf8283 2019-07-24 stsp
6124 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6125 0ebf8283 2019-07-24 stsp if (err)
6126 0ebf8283 2019-07-24 stsp goto done;
6127 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6128 0ebf8283 2019-07-24 stsp if (err)
6129 0ebf8283 2019-07-24 stsp goto done;
6130 0ebf8283 2019-07-24 stsp
6131 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6132 0ebf8283 2019-07-24 stsp worktree);
6133 0ebf8283 2019-07-24 stsp if (err)
6134 0ebf8283 2019-07-24 stsp goto done;
6135 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6136 0ebf8283 2019-07-24 stsp if (err)
6137 0ebf8283 2019-07-24 stsp goto done;
6138 0ebf8283 2019-07-24 stsp
6139 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6140 0ebf8283 2019-07-24 stsp if (err)
6141 0ebf8283 2019-07-24 stsp goto done;
6142 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6143 0ebf8283 2019-07-24 stsp if (err)
6144 0ebf8283 2019-07-24 stsp goto done;
6145 0ebf8283 2019-07-24 stsp
6146 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6147 0ebf8283 2019-07-24 stsp if (err)
6148 0ebf8283 2019-07-24 stsp goto done;
6149 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6150 0ebf8283 2019-07-24 stsp if (err)
6151 0ebf8283 2019-07-24 stsp goto done;
6152 0ebf8283 2019-07-24 stsp done:
6153 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6154 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6155 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6156 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6157 0ebf8283 2019-07-24 stsp return err;
6158 0ebf8283 2019-07-24 stsp }
6159 0ebf8283 2019-07-24 stsp
6160 0ebf8283 2019-07-24 stsp const struct got_error *
6161 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6162 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6163 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6164 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6165 0ebf8283 2019-07-24 stsp {
6166 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6167 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6168 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6169 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6170 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6171 0ebf8283 2019-07-24 stsp
6172 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6173 0ebf8283 2019-07-24 stsp if (err)
6174 0ebf8283 2019-07-24 stsp return err;
6175 0ebf8283 2019-07-24 stsp
6176 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6177 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
6178 0ebf8283 2019-07-24 stsp if (err)
6179 0ebf8283 2019-07-24 stsp goto done;
6180 0ebf8283 2019-07-24 stsp
6181 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6182 0ebf8283 2019-07-24 stsp if (err)
6183 0ebf8283 2019-07-24 stsp goto done;
6184 0ebf8283 2019-07-24 stsp
6185 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6186 0ebf8283 2019-07-24 stsp if (err)
6187 0ebf8283 2019-07-24 stsp goto done;
6188 0ebf8283 2019-07-24 stsp
6189 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6190 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6191 0ebf8283 2019-07-24 stsp if (err)
6192 0ebf8283 2019-07-24 stsp goto done;
6193 0ebf8283 2019-07-24 stsp
6194 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6195 0ebf8283 2019-07-24 stsp if (err)
6196 0ebf8283 2019-07-24 stsp goto done;
6197 0ebf8283 2019-07-24 stsp
6198 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6199 0ebf8283 2019-07-24 stsp if (err)
6200 0ebf8283 2019-07-24 stsp goto done;
6201 0ebf8283 2019-07-24 stsp
6202 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6203 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6204 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6205 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6206 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6207 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6208 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6209 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6210 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6211 0ebf8283 2019-07-24 stsp if (err)
6212 1f1abb7e 2019-08-08 stsp goto sync;
6213 0ebf8283 2019-07-24 stsp
6214 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6215 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6216 0ebf8283 2019-07-24 stsp sync:
6217 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6218 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6219 0ebf8283 2019-07-24 stsp err = sync_err;
6220 0ebf8283 2019-07-24 stsp done:
6221 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6222 0ebf8283 2019-07-24 stsp free(tree_id);
6223 818c7501 2019-07-11 stsp free(fileindex_path);
6224 818c7501 2019-07-11 stsp
6225 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6226 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6227 818c7501 2019-07-11 stsp err = unlockerr;
6228 818c7501 2019-07-11 stsp return err;
6229 818c7501 2019-07-11 stsp }
6230 0ebf8283 2019-07-24 stsp
6231 0ebf8283 2019-07-24 stsp const struct got_error *
6232 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6233 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6234 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6235 0ebf8283 2019-07-24 stsp {
6236 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6237 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6238 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6239 0ebf8283 2019-07-24 stsp
6240 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6241 0ebf8283 2019-07-24 stsp if (err)
6242 0ebf8283 2019-07-24 stsp return err;
6243 0ebf8283 2019-07-24 stsp
6244 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6245 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6246 0ebf8283 2019-07-24 stsp if (err)
6247 0ebf8283 2019-07-24 stsp goto done;
6248 0ebf8283 2019-07-24 stsp
6249 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6250 0ebf8283 2019-07-24 stsp if (err)
6251 0ebf8283 2019-07-24 stsp goto done;
6252 0ebf8283 2019-07-24 stsp
6253 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6254 0ebf8283 2019-07-24 stsp if (err)
6255 0ebf8283 2019-07-24 stsp goto done;
6256 0ebf8283 2019-07-24 stsp
6257 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6258 0ebf8283 2019-07-24 stsp if (err)
6259 0ebf8283 2019-07-24 stsp goto done;
6260 0ebf8283 2019-07-24 stsp
6261 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6262 0ebf8283 2019-07-24 stsp done:
6263 3e3a69f1 2019-07-25 stsp if (fileindex)
6264 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6265 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6266 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6267 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6268 0ebf8283 2019-07-24 stsp err = unlockerr;
6269 0ebf8283 2019-07-24 stsp return err;
6270 0ebf8283 2019-07-24 stsp }
6271 0ebf8283 2019-07-24 stsp
6272 0ebf8283 2019-07-24 stsp const struct got_error *
6273 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6274 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6275 0ebf8283 2019-07-24 stsp {
6276 0ebf8283 2019-07-24 stsp const struct got_error *err;
6277 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6278 0ebf8283 2019-07-24 stsp
6279 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6280 0ebf8283 2019-07-24 stsp if (err)
6281 0ebf8283 2019-07-24 stsp return err;
6282 0ebf8283 2019-07-24 stsp
6283 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6284 0ebf8283 2019-07-24 stsp if (err)
6285 0ebf8283 2019-07-24 stsp goto done;
6286 0ebf8283 2019-07-24 stsp
6287 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6288 0ebf8283 2019-07-24 stsp done:
6289 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6290 2822a352 2019-10-15 stsp return err;
6291 2822a352 2019-10-15 stsp }
6292 2822a352 2019-10-15 stsp
6293 2822a352 2019-10-15 stsp const struct got_error *
6294 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6295 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6296 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6297 2822a352 2019-10-15 stsp struct got_repository *repo)
6298 2822a352 2019-10-15 stsp {
6299 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6300 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6301 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6302 2822a352 2019-10-15 stsp
6303 2822a352 2019-10-15 stsp *fileindex = NULL;
6304 2822a352 2019-10-15 stsp *branch_ref = NULL;
6305 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6306 2822a352 2019-10-15 stsp
6307 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6308 2822a352 2019-10-15 stsp if (err)
6309 2822a352 2019-10-15 stsp return err;
6310 2822a352 2019-10-15 stsp
6311 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6312 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6313 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6314 2822a352 2019-10-15 stsp "update -b or different branch name required");
6315 2822a352 2019-10-15 stsp goto done;
6316 2822a352 2019-10-15 stsp }
6317 2822a352 2019-10-15 stsp
6318 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6319 2822a352 2019-10-15 stsp if (err)
6320 2822a352 2019-10-15 stsp goto done;
6321 2822a352 2019-10-15 stsp
6322 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6323 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6324 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6325 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6326 2822a352 2019-10-15 stsp &ok_arg);
6327 2822a352 2019-10-15 stsp if (err)
6328 2822a352 2019-10-15 stsp goto done;
6329 2822a352 2019-10-15 stsp
6330 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6331 2822a352 2019-10-15 stsp if (err)
6332 2822a352 2019-10-15 stsp goto done;
6333 2822a352 2019-10-15 stsp
6334 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6335 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6336 2822a352 2019-10-15 stsp done:
6337 2822a352 2019-10-15 stsp if (err) {
6338 2822a352 2019-10-15 stsp if (*branch_ref) {
6339 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6340 2822a352 2019-10-15 stsp *branch_ref = NULL;
6341 2822a352 2019-10-15 stsp }
6342 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6343 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6344 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6345 2822a352 2019-10-15 stsp }
6346 2822a352 2019-10-15 stsp if (*fileindex) {
6347 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6348 2822a352 2019-10-15 stsp *fileindex = NULL;
6349 2822a352 2019-10-15 stsp }
6350 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6351 2822a352 2019-10-15 stsp }
6352 0cb83759 2019-08-03 stsp return err;
6353 0cb83759 2019-08-03 stsp }
6354 0cb83759 2019-08-03 stsp
6355 2822a352 2019-10-15 stsp const struct got_error *
6356 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6357 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6358 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6359 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6360 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6361 2822a352 2019-10-15 stsp {
6362 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6363 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6364 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6365 2822a352 2019-10-15 stsp
6366 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6367 2822a352 2019-10-15 stsp if (err)
6368 2822a352 2019-10-15 stsp goto done;
6369 2822a352 2019-10-15 stsp
6370 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6371 2822a352 2019-10-15 stsp if (err)
6372 2822a352 2019-10-15 stsp goto done;
6373 2822a352 2019-10-15 stsp
6374 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6375 2822a352 2019-10-15 stsp worktree->path_prefix);
6376 2822a352 2019-10-15 stsp if (err)
6377 2822a352 2019-10-15 stsp goto done;
6378 2822a352 2019-10-15 stsp
6379 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6380 2822a352 2019-10-15 stsp if (err)
6381 2822a352 2019-10-15 stsp goto done;
6382 2822a352 2019-10-15 stsp
6383 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6384 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6385 2822a352 2019-10-15 stsp if (err)
6386 2822a352 2019-10-15 stsp goto sync;
6387 2822a352 2019-10-15 stsp
6388 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6389 2822a352 2019-10-15 stsp if (err)
6390 2822a352 2019-10-15 stsp goto sync;
6391 2822a352 2019-10-15 stsp
6392 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6393 2822a352 2019-10-15 stsp sync:
6394 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6395 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6396 2822a352 2019-10-15 stsp err = sync_err;
6397 2822a352 2019-10-15 stsp
6398 2822a352 2019-10-15 stsp done:
6399 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6400 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6401 2822a352 2019-10-15 stsp err = unlockerr;
6402 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6403 2822a352 2019-10-15 stsp
6404 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6405 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6406 2822a352 2019-10-15 stsp err = unlockerr;
6407 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6408 2822a352 2019-10-15 stsp
6409 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6410 2822a352 2019-10-15 stsp free(fileindex_path);
6411 2822a352 2019-10-15 stsp free(tree_id);
6412 2822a352 2019-10-15 stsp
6413 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6414 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6415 2822a352 2019-10-15 stsp err = unlockerr;
6416 2822a352 2019-10-15 stsp return err;
6417 2822a352 2019-10-15 stsp }
6418 2822a352 2019-10-15 stsp
6419 2822a352 2019-10-15 stsp const struct got_error *
6420 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6421 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6422 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6423 2822a352 2019-10-15 stsp {
6424 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6425 8b692cd0 2019-10-21 stsp
6426 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6427 8b692cd0 2019-10-21 stsp
6428 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6429 8b692cd0 2019-10-21 stsp
6430 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6431 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6432 8b692cd0 2019-10-21 stsp err = unlockerr;
6433 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6434 8b692cd0 2019-10-21 stsp
6435 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6436 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6437 8b692cd0 2019-10-21 stsp err = unlockerr;
6438 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6439 8b692cd0 2019-10-21 stsp
6440 8b692cd0 2019-10-21 stsp return err;
6441 2822a352 2019-10-15 stsp }
6442 2822a352 2019-10-15 stsp
6443 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6444 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6445 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6446 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6447 2db2652d 2019-08-07 stsp struct got_repository *repo;
6448 2db2652d 2019-08-07 stsp int have_changes;
6449 2db2652d 2019-08-07 stsp };
6450 2db2652d 2019-08-07 stsp
6451 2db2652d 2019-08-07 stsp const struct got_error *
6452 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6453 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6454 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6455 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6456 735ef5ac 2019-08-03 stsp {
6457 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6458 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6459 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6460 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6461 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6462 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6463 8b13ce36 2019-08-08 stsp
6464 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6465 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6466 8b13ce36 2019-08-08 stsp return NULL;
6467 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6468 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6469 735ef5ac 2019-08-03 stsp
6470 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6471 735ef5ac 2019-08-03 stsp if (ie == NULL)
6472 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6473 735ef5ac 2019-08-03 stsp
6474 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6475 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6476 735ef5ac 2019-08-03 stsp relpath) == -1)
6477 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6478 735ef5ac 2019-08-03 stsp
6479 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6480 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6481 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6482 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6483 735ef5ac 2019-08-03 stsp }
6484 735ef5ac 2019-08-03 stsp
6485 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6486 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6487 3aa5969e 2019-08-06 stsp goto done;
6488 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6489 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6490 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6491 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6492 735ef5ac 2019-08-03 stsp goto done;
6493 3aa5969e 2019-08-06 stsp }
6494 735ef5ac 2019-08-03 stsp
6495 2db2652d 2019-08-07 stsp a->have_changes = 1;
6496 2db2652d 2019-08-07 stsp
6497 735ef5ac 2019-08-03 stsp p = in_repo_path;
6498 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6499 735ef5ac 2019-08-03 stsp p++;
6500 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6501 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6502 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6503 735ef5ac 2019-08-03 stsp done:
6504 735ef5ac 2019-08-03 stsp free(in_repo_path);
6505 dc424a06 2019-08-07 stsp return err;
6506 dc424a06 2019-08-07 stsp }
6507 dc424a06 2019-08-07 stsp
6508 2db2652d 2019-08-07 stsp struct stage_path_arg {
6509 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6510 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6511 2db2652d 2019-08-07 stsp struct got_repository *repo;
6512 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
6513 2db2652d 2019-08-07 stsp void *status_arg;
6514 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
6515 2db2652d 2019-08-07 stsp void *patch_arg;
6516 7b5dc508 2019-10-28 stsp int staged_something;
6517 2db2652d 2019-08-07 stsp };
6518 2db2652d 2019-08-07 stsp
6519 2db2652d 2019-08-07 stsp static const struct got_error *
6520 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
6521 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6522 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6523 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6524 0cb83759 2019-08-03 stsp {
6525 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
6526 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
6527 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
6528 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
6529 0cb83759 2019-08-03 stsp uint32_t stage;
6530 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
6531 8b13ce36 2019-08-08 stsp
6532 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
6533 8b13ce36 2019-08-08 stsp return NULL;
6534 0cb83759 2019-08-03 stsp
6535 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6536 d3e7c587 2019-08-03 stsp if (ie == NULL)
6537 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6538 0cb83759 2019-08-03 stsp
6539 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6540 2db2652d 2019-08-07 stsp relpath)== -1)
6541 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
6542 0cb83759 2019-08-03 stsp
6543 0cb83759 2019-08-03 stsp switch (status) {
6544 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
6545 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
6546 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6547 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
6548 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6549 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6550 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
6551 dc424a06 2019-08-07 stsp if (err)
6552 dc424a06 2019-08-07 stsp break;
6553 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
6554 dc424a06 2019-08-07 stsp break;
6555 dc424a06 2019-08-07 stsp } else {
6556 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
6557 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
6558 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
6559 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
6560 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
6561 dc424a06 2019-08-07 stsp break;
6562 dc424a06 2019-08-07 stsp }
6563 dc424a06 2019-08-07 stsp }
6564 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
6565 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
6566 0cb83759 2019-08-03 stsp if (err)
6567 dc424a06 2019-08-07 stsp break;
6568 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6569 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6570 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6571 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
6572 0cb83759 2019-08-03 stsp else
6573 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
6574 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6575 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6576 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6577 dc424a06 2019-08-07 stsp break;
6578 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6579 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
6580 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
6581 0cb83759 2019-08-03 stsp break;
6582 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
6583 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
6584 d3e7c587 2019-08-03 stsp break;
6585 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6586 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6587 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
6588 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
6589 dc424a06 2019-08-07 stsp if (err)
6590 dc424a06 2019-08-07 stsp break;
6591 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6592 88f33a19 2019-08-08 stsp break;
6593 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6594 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6595 dc424a06 2019-08-07 stsp break;
6596 88f33a19 2019-08-08 stsp }
6597 dc424a06 2019-08-07 stsp }
6598 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
6599 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6600 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6601 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6602 dc424a06 2019-08-07 stsp break;
6603 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6604 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6605 12463d8b 2019-12-13 stsp de_name);
6606 0cb83759 2019-08-03 stsp break;
6607 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
6608 d3e7c587 2019-08-03 stsp break;
6609 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
6610 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6611 ebf48fd5 2019-08-03 stsp break;
6612 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
6613 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
6614 2a06fe5f 2019-08-24 stsp break;
6615 0cb83759 2019-08-03 stsp default:
6616 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6617 537ac44b 2019-08-03 stsp break;
6618 0cb83759 2019-08-03 stsp }
6619 dc424a06 2019-08-07 stsp
6620 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
6621 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
6622 dc424a06 2019-08-07 stsp free(path_content);
6623 2db2652d 2019-08-07 stsp free(ondisk_path);
6624 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
6625 0ebf8283 2019-07-24 stsp return err;
6626 0ebf8283 2019-07-24 stsp }
6627 0cb83759 2019-08-03 stsp
6628 0cb83759 2019-08-03 stsp const struct got_error *
6629 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
6630 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
6631 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
6632 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6633 1e71573e 2019-08-03 stsp struct got_repository *repo)
6634 0cb83759 2019-08-03 stsp {
6635 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6636 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
6637 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6638 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
6639 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
6640 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
6641 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
6642 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
6643 0cb83759 2019-08-03 stsp
6644 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6645 0cb83759 2019-08-03 stsp if (err)
6646 0cb83759 2019-08-03 stsp return err;
6647 0cb83759 2019-08-03 stsp
6648 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
6649 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
6650 735ef5ac 2019-08-03 stsp if (err)
6651 735ef5ac 2019-08-03 stsp goto done;
6652 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6653 735ef5ac 2019-08-03 stsp if (err)
6654 735ef5ac 2019-08-03 stsp goto done;
6655 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6656 0cb83759 2019-08-03 stsp if (err)
6657 0cb83759 2019-08-03 stsp goto done;
6658 0cb83759 2019-08-03 stsp
6659 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
6660 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
6661 2db2652d 2019-08-07 stsp oka.worktree = worktree;
6662 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
6663 2db2652d 2019-08-07 stsp oka.repo = repo;
6664 2db2652d 2019-08-07 stsp oka.have_changes = 0;
6665 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6666 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6667 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
6668 735ef5ac 2019-08-03 stsp if (err)
6669 735ef5ac 2019-08-03 stsp goto done;
6670 735ef5ac 2019-08-03 stsp }
6671 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
6672 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6673 2db2652d 2019-08-07 stsp goto done;
6674 2db2652d 2019-08-07 stsp }
6675 735ef5ac 2019-08-03 stsp
6676 2db2652d 2019-08-07 stsp spa.worktree = worktree;
6677 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
6678 2db2652d 2019-08-07 stsp spa.repo = repo;
6679 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
6680 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
6681 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
6682 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
6683 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
6684 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6685 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6686 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
6687 42005733 2019-08-03 stsp if (err)
6688 2db2652d 2019-08-07 stsp goto done;
6689 7b5dc508 2019-10-28 stsp }
6690 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
6691 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6692 7b5dc508 2019-10-28 stsp goto done;
6693 0cb83759 2019-08-03 stsp }
6694 0cb83759 2019-08-03 stsp
6695 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6696 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
6697 0cb83759 2019-08-03 stsp err = sync_err;
6698 0cb83759 2019-08-03 stsp done:
6699 735ef5ac 2019-08-03 stsp if (head_ref)
6700 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
6701 735ef5ac 2019-08-03 stsp free(head_commit_id);
6702 0cb83759 2019-08-03 stsp free(fileindex_path);
6703 0cb83759 2019-08-03 stsp if (fileindex)
6704 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
6705 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6706 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
6707 0cb83759 2019-08-03 stsp err = unlockerr;
6708 0cb83759 2019-08-03 stsp return err;
6709 0cb83759 2019-08-03 stsp }
6710 ad493afc 2019-08-03 stsp
6711 ad493afc 2019-08-03 stsp struct unstage_path_arg {
6712 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
6713 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
6714 ad493afc 2019-08-03 stsp struct got_repository *repo;
6715 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
6716 ad493afc 2019-08-03 stsp void *progress_arg;
6717 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
6718 2e1f37b0 2019-08-08 stsp void *patch_arg;
6719 ad493afc 2019-08-03 stsp };
6720 2e1f37b0 2019-08-08 stsp
6721 2e1f37b0 2019-08-08 stsp static const struct got_error *
6722 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
6723 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
6724 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
6725 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
6726 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
6727 2e1f37b0 2019-08-08 stsp {
6728 2e1f37b0 2019-08-08 stsp const struct got_error *err;
6729 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
6730 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6731 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6732 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
6733 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
6734 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
6735 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
6736 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
6737 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6738 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
6739 2e1f37b0 2019-08-08 stsp
6740 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
6741 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
6742 2e1f37b0 2019-08-08 stsp
6743 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
6744 2e1f37b0 2019-08-08 stsp if (err)
6745 2e1f37b0 2019-08-08 stsp return err;
6746 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6747 2e1f37b0 2019-08-08 stsp if (err)
6748 2e1f37b0 2019-08-08 stsp goto done;
6749 2e1f37b0 2019-08-08 stsp
6750 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6751 2e1f37b0 2019-08-08 stsp if (err)
6752 2e1f37b0 2019-08-08 stsp goto done;
6753 2e1f37b0 2019-08-08 stsp
6754 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6755 2e1f37b0 2019-08-08 stsp if (err)
6756 2e1f37b0 2019-08-08 stsp goto done;
6757 2e1f37b0 2019-08-08 stsp
6758 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6759 2e1f37b0 2019-08-08 stsp if (err)
6760 2e1f37b0 2019-08-08 stsp goto done;
6761 2e1f37b0 2019-08-08 stsp
6762 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6763 2e1f37b0 2019-08-08 stsp if (err)
6764 2e1f37b0 2019-08-08 stsp goto done;
6765 ad493afc 2019-08-03 stsp
6766 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6767 2e1f37b0 2019-08-08 stsp if (err)
6768 2e1f37b0 2019-08-08 stsp goto done;
6769 2e1f37b0 2019-08-08 stsp
6770 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
6771 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
6772 2e1f37b0 2019-08-08 stsp goto done;
6773 2e1f37b0 2019-08-08 stsp }
6774 2e1f37b0 2019-08-08 stsp
6775 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
6776 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
6777 2e1f37b0 2019-08-08 stsp goto done;
6778 2e1f37b0 2019-08-08 stsp }
6779 2e1f37b0 2019-08-08 stsp
6780 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
6781 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6782 2e1f37b0 2019-08-08 stsp if (err)
6783 2e1f37b0 2019-08-08 stsp goto done;
6784 2e1f37b0 2019-08-08 stsp
6785 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
6786 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
6787 2e1f37b0 2019-08-08 stsp if (err)
6788 2e1f37b0 2019-08-08 stsp goto done;
6789 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
6790 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
6791 2e1f37b0 2019-08-08 stsp if (err)
6792 2e1f37b0 2019-08-08 stsp goto done;
6793 2e1f37b0 2019-08-08 stsp
6794 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
6795 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
6796 2e1f37b0 2019-08-08 stsp goto done;
6797 2e1f37b0 2019-08-08 stsp }
6798 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
6799 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
6800 2e1f37b0 2019-08-08 stsp goto done;
6801 2e1f37b0 2019-08-08 stsp }
6802 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6803 2e1f37b0 2019-08-08 stsp int choice;
6804 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
6805 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
6806 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
6807 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
6808 2e1f37b0 2019-08-08 stsp if (err)
6809 2e1f37b0 2019-08-08 stsp goto done;
6810 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
6811 2e1f37b0 2019-08-08 stsp have_content = 1;
6812 19e4b907 2019-08-08 stsp else
6813 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
6814 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
6815 2e1f37b0 2019-08-08 stsp break;
6816 2e1f37b0 2019-08-08 stsp }
6817 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
6818 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6819 f1e81a05 2019-08-10 stsp outfile, rejectfile);
6820 2e1f37b0 2019-08-08 stsp done:
6821 2e1f37b0 2019-08-08 stsp free(label1);
6822 2e1f37b0 2019-08-08 stsp if (blob)
6823 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
6824 2e1f37b0 2019-08-08 stsp if (staged_blob)
6825 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
6826 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
6827 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
6828 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
6829 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
6830 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
6831 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
6832 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6833 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
6834 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
6835 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
6836 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
6837 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
6838 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
6839 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
6840 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
6841 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
6842 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
6843 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
6844 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
6845 2e1f37b0 2019-08-08 stsp }
6846 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
6847 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
6848 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
6849 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
6850 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
6851 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
6852 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
6853 2e1f37b0 2019-08-08 stsp }
6854 2e1f37b0 2019-08-08 stsp free(args);
6855 2e1f37b0 2019-08-08 stsp if (ds) {
6856 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
6857 2e1f37b0 2019-08-08 stsp free(ds);
6858 2e1f37b0 2019-08-08 stsp }
6859 2e1f37b0 2019-08-08 stsp if (changes)
6860 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
6861 2e1f37b0 2019-08-08 stsp free(path1);
6862 2e1f37b0 2019-08-08 stsp free(path2);
6863 2e1f37b0 2019-08-08 stsp return err;
6864 2e1f37b0 2019-08-08 stsp }
6865 2e1f37b0 2019-08-08 stsp
6866 ad493afc 2019-08-03 stsp static const struct got_error *
6867 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
6868 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
6869 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6870 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6871 ad493afc 2019-08-03 stsp {
6872 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
6873 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
6874 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
6875 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6876 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
6877 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
6878 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
6879 ad493afc 2019-08-03 stsp int local_changes_subsumed;
6880 9bc94a15 2019-08-03 stsp struct stat sb;
6881 ad493afc 2019-08-03 stsp
6882 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
6883 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
6884 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
6885 2e1f37b0 2019-08-08 stsp return NULL;
6886 2e1f37b0 2019-08-08 stsp
6887 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6888 ad493afc 2019-08-03 stsp if (ie == NULL)
6889 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6890 9bc94a15 2019-08-03 stsp
6891 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6892 9bc94a15 2019-08-03 stsp == -1)
6893 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
6894 ad493afc 2019-08-03 stsp
6895 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
6896 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
6897 f69721c3 2019-10-21 stsp if (err)
6898 f69721c3 2019-10-21 stsp goto done;
6899 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6900 f69721c3 2019-10-21 stsp id_str) == -1) {
6901 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
6902 f69721c3 2019-10-21 stsp goto done;
6903 f69721c3 2019-10-21 stsp }
6904 f69721c3 2019-10-21 stsp
6905 ad493afc 2019-08-03 stsp switch (staged_status) {
6906 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
6907 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
6908 ad493afc 2019-08-03 stsp blob_id, 8192);
6909 ad493afc 2019-08-03 stsp if (err)
6910 ad493afc 2019-08-03 stsp break;
6911 ad493afc 2019-08-03 stsp /* fall through */
6912 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
6913 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
6914 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
6915 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
6916 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6917 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
6918 2e1f37b0 2019-08-08 stsp if (err)
6919 2e1f37b0 2019-08-08 stsp break;
6920 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
6921 2e1f37b0 2019-08-08 stsp break;
6922 2e1f37b0 2019-08-08 stsp } else {
6923 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
6924 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
6925 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
6926 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
6927 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
6928 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
6929 2e1f37b0 2019-08-08 stsp break;
6930 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
6931 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
6932 2e1f37b0 2019-08-08 stsp &staged_blob_id,
6933 2e1f37b0 2019-08-08 stsp path_new_staged_content,
6934 2e1f37b0 2019-08-08 stsp a->repo);
6935 2e1f37b0 2019-08-08 stsp if (err)
6936 2e1f37b0 2019-08-08 stsp break;
6937 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
6938 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
6939 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
6940 2e1f37b0 2019-08-08 stsp }
6941 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
6942 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
6943 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
6944 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
6945 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
6946 f69721c3 2019-10-21 stsp a->progress_arg);
6947 2e1f37b0 2019-08-08 stsp if (err == NULL &&
6948 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
6949 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
6950 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
6951 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
6952 2e1f37b0 2019-08-08 stsp }
6953 2e1f37b0 2019-08-08 stsp }
6954 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
6955 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
6956 ad493afc 2019-08-03 stsp if (err)
6957 ad493afc 2019-08-03 stsp break;
6958 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
6959 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
6960 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6961 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
6962 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
6963 ad493afc 2019-08-03 stsp if (err == NULL)
6964 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6965 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6966 ad493afc 2019-08-03 stsp break;
6967 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
6968 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
6969 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
6970 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6971 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
6972 2e1f37b0 2019-08-08 stsp if (err)
6973 2e1f37b0 2019-08-08 stsp break;
6974 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6975 2e1f37b0 2019-08-08 stsp break;
6976 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6977 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6978 2e1f37b0 2019-08-08 stsp break;
6979 2e1f37b0 2019-08-08 stsp }
6980 2e1f37b0 2019-08-08 stsp }
6981 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6982 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
6983 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
6984 9bc94a15 2019-08-03 stsp if (err)
6985 9bc94a15 2019-08-03 stsp break;
6986 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
6987 ad493afc 2019-08-03 stsp break;
6988 ad493afc 2019-08-03 stsp }
6989 f69721c3 2019-10-21 stsp done:
6990 ad493afc 2019-08-03 stsp free(ondisk_path);
6991 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
6992 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
6993 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
6994 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
6995 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
6996 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
6997 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
6998 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
6999 ad493afc 2019-08-03 stsp if (blob_base)
7000 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
7001 ad493afc 2019-08-03 stsp if (blob_staged)
7002 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
7003 f69721c3 2019-10-21 stsp free(id_str);
7004 f69721c3 2019-10-21 stsp free(label_orig);
7005 ad493afc 2019-08-03 stsp return err;
7006 ad493afc 2019-08-03 stsp }
7007 ad493afc 2019-08-03 stsp
7008 ad493afc 2019-08-03 stsp const struct got_error *
7009 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
7010 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
7011 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
7012 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
7013 ad493afc 2019-08-03 stsp struct got_repository *repo)
7014 ad493afc 2019-08-03 stsp {
7015 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
7016 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7017 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
7018 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
7019 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
7020 ad493afc 2019-08-03 stsp
7021 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
7022 ad493afc 2019-08-03 stsp if (err)
7023 ad493afc 2019-08-03 stsp return err;
7024 ad493afc 2019-08-03 stsp
7025 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
7026 ad493afc 2019-08-03 stsp if (err)
7027 ad493afc 2019-08-03 stsp goto done;
7028 ad493afc 2019-08-03 stsp
7029 ad493afc 2019-08-03 stsp upa.worktree = worktree;
7030 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
7031 ad493afc 2019-08-03 stsp upa.repo = repo;
7032 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
7033 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
7034 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
7035 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
7036 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
7037 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
7038 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
7039 ad493afc 2019-08-03 stsp if (err)
7040 ad493afc 2019-08-03 stsp goto done;
7041 ad493afc 2019-08-03 stsp }
7042 ad493afc 2019-08-03 stsp
7043 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
7044 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
7045 ad493afc 2019-08-03 stsp err = sync_err;
7046 ad493afc 2019-08-03 stsp done:
7047 ad493afc 2019-08-03 stsp free(fileindex_path);
7048 ad493afc 2019-08-03 stsp if (fileindex)
7049 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
7050 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
7051 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
7052 ad493afc 2019-08-03 stsp err = unlockerr;
7053 ad493afc 2019-08-03 stsp return err;
7054 ad493afc 2019-08-03 stsp }