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 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
380 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
381 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
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 8ba819a3 2020-07-23 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
958 8ba819a3 2020-07-23 stsp size_t hdrlen = got_object_blob_get_hdrlen(blob);
959 8ba819a3 2020-07-23 stsp
960 8ba819a3 2020-07-23 stsp /*
961 8ba819a3 2020-07-23 stsp * Blob object content specifies the target path of the link.
962 8ba819a3 2020-07-23 stsp * If a symbolic link cannot be installed we instead create
963 8ba819a3 2020-07-23 stsp * a regular file which contains the link target path stored
964 8ba819a3 2020-07-23 stsp * in the blob object.
965 8ba819a3 2020-07-23 stsp */
966 8ba819a3 2020-07-23 stsp do {
967 8ba819a3 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
968 8ba819a3 2020-07-23 stsp if (len + target_len >= sizeof(target_path)) {
969 8ba819a3 2020-07-23 stsp /* Path too long; install as a regular file. */
970 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
971 8ba819a3 2020-07-23 stsp return install_blob(worktree, ondisk_path, path,
972 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
973 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
974 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
975 8ba819a3 2020-07-23 stsp }
976 8ba819a3 2020-07-23 stsp if (len > 0) {
977 8ba819a3 2020-07-23 stsp /* Skip blob object header first time around. */
978 8ba819a3 2020-07-23 stsp memcpy(target_path + target_len, buf + hdrlen,
979 8ba819a3 2020-07-23 stsp len - hdrlen);
980 8ba819a3 2020-07-23 stsp target_len += len - hdrlen;
981 8ba819a3 2020-07-23 stsp hdrlen = 0;
982 8ba819a3 2020-07-23 stsp }
983 8ba819a3 2020-07-23 stsp } while (len != 0);
984 8ba819a3 2020-07-23 stsp target_path[target_len] = '\0';
985 8ba819a3 2020-07-23 stsp
986 8ba819a3 2020-07-23 stsp /*
987 8ba819a3 2020-07-23 stsp * Relative symlink target lookup should begin at the directory
988 8ba819a3 2020-07-23 stsp * in which the blob object is being installed.
989 8ba819a3 2020-07-23 stsp */
990 8ba819a3 2020-07-23 stsp if (!got_path_is_absolute(target_path)) {
991 8ba819a3 2020-07-23 stsp char *parent = dirname(ondisk_path);
992 8ba819a3 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
993 8ba819a3 2020-07-23 stsp err = got_error_from_errno("asprintf");
994 8ba819a3 2020-07-23 stsp goto done;
995 8ba819a3 2020-07-23 stsp }
996 8ba819a3 2020-07-23 stsp }
997 8ba819a3 2020-07-23 stsp
998 8ba819a3 2020-07-23 stsp /*
999 8ba819a3 2020-07-23 stsp * unveil(2) restricts our view of paths in the filesystem.
1000 8ba819a3 2020-07-23 stsp * ENOENT will occur if a link target path does not exist or
1001 8ba819a3 2020-07-23 stsp * if it points outside our unveiled path space.
1002 8ba819a3 2020-07-23 stsp */
1003 8ba819a3 2020-07-23 stsp resolved_path = realpath(abspath ? abspath : target_path, NULL);
1004 8ba819a3 2020-07-23 stsp if (resolved_path == NULL) {
1005 8ba819a3 2020-07-23 stsp if (errno != ENOENT)
1006 8ba819a3 2020-07-23 stsp return got_error_from_errno2("realpath", target_path);
1007 8ba819a3 2020-07-23 stsp }
1008 8ba819a3 2020-07-23 stsp
1009 8ba819a3 2020-07-23 stsp /* Only allow symlinks pointing at paths within the work tree. */
1010 0ab20ee9 2020-07-23 stsp if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1011 0ab20ee9 2020-07-23 stsp abspath : target_path), worktree->root_path,
1012 0ab20ee9 2020-07-23 stsp strlen(worktree->root_path))) {
1013 8ba819a3 2020-07-23 stsp /* install as a regular file */
1014 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1015 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1016 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1017 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1018 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1019 8ba819a3 2020-07-23 stsp goto done;
1020 8ba819a3 2020-07-23 stsp }
1021 8ba819a3 2020-07-23 stsp
1022 8ba819a3 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1023 8ba819a3 2020-07-23 stsp if (errno == EEXIST) {
1024 8ba819a3 2020-07-23 stsp struct stat sb;
1025 8ba819a3 2020-07-23 stsp ssize_t elen;
1026 8ba819a3 2020-07-23 stsp char etarget[PATH_MAX];
1027 8ba819a3 2020-07-23 stsp if (lstat(ondisk_path, &sb) == -1) {
1028 8ba819a3 2020-07-23 stsp err = got_error_from_errno2("lstat",
1029 8ba819a3 2020-07-23 stsp ondisk_path);
1030 8ba819a3 2020-07-23 stsp goto done;
1031 8ba819a3 2020-07-23 stsp }
1032 8ba819a3 2020-07-23 stsp if (!S_ISLNK(sb.st_mode)) {
1033 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1034 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1035 8ba819a3 2020-07-23 stsp goto done;
1036 8ba819a3 2020-07-23 stsp }
1037 8ba819a3 2020-07-23 stsp elen = readlink(ondisk_path, etarget, sizeof(etarget));
1038 8ba819a3 2020-07-23 stsp if (elen == -1) {
1039 8ba819a3 2020-07-23 stsp err = got_error_from_errno2("readlink",
1040 8ba819a3 2020-07-23 stsp ondisk_path);
1041 8ba819a3 2020-07-23 stsp goto done;
1042 8ba819a3 2020-07-23 stsp }
1043 8ba819a3 2020-07-23 stsp if (elen == target_len &&
1044 f35fa46a 2020-07-23 stsp memcmp(etarget, target_path, target_len) == 0) {
1045 f35fa46a 2020-07-23 stsp err = NULL; /* nothing to do */
1046 f35fa46a 2020-07-23 stsp goto done;
1047 f35fa46a 2020-07-23 stsp } else {
1048 f35fa46a 2020-07-23 stsp if (unlink(ondisk_path) == -1) {
1049 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("unlink",
1050 f35fa46a 2020-07-23 stsp ondisk_path);
1051 f35fa46a 2020-07-23 stsp goto done;
1052 f35fa46a 2020-07-23 stsp }
1053 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) == -1) {
1054 f35fa46a 2020-07-23 stsp err = got_error_from_errno3("symlink",
1055 f35fa46a 2020-07-23 stsp target_path, ondisk_path);
1056 f35fa46a 2020-07-23 stsp goto done;
1057 f35fa46a 2020-07-23 stsp }
1058 f35fa46a 2020-07-23 stsp
1059 f35fa46a 2020-07-23 stsp err = (*progress_cb)(progress_arg,
1060 f35fa46a 2020-07-23 stsp GOT_STATUS_UPDATE, path);
1061 f35fa46a 2020-07-23 stsp goto done;
1062 f35fa46a 2020-07-23 stsp }
1063 f35fa46a 2020-07-23 stsp }
1064 f35fa46a 2020-07-23 stsp
1065 f35fa46a 2020-07-23 stsp if (errno == ENOENT) {
1066 f35fa46a 2020-07-23 stsp char *parent = dirname(ondisk_path);
1067 f35fa46a 2020-07-23 stsp if (parent == NULL) {
1068 f35fa46a 2020-07-23 stsp err = got_error_from_errno2("dirname",
1069 f35fa46a 2020-07-23 stsp ondisk_path);
1070 f35fa46a 2020-07-23 stsp goto done;
1071 f35fa46a 2020-07-23 stsp }
1072 f35fa46a 2020-07-23 stsp err = add_dir_on_disk(worktree, parent);
1073 f35fa46a 2020-07-23 stsp if (err)
1074 f35fa46a 2020-07-23 stsp goto done;
1075 f35fa46a 2020-07-23 stsp /*
1076 f35fa46a 2020-07-23 stsp * Retry, and fall through to error handling
1077 f35fa46a 2020-07-23 stsp * below if this second attempt fails.
1078 f35fa46a 2020-07-23 stsp */
1079 f35fa46a 2020-07-23 stsp if (symlink(target_path, ondisk_path) != -1) {
1080 f35fa46a 2020-07-23 stsp err = NULL; /* success */
1081 f35fa46a 2020-07-23 stsp goto done;
1082 f35fa46a 2020-07-23 stsp }
1083 f35fa46a 2020-07-23 stsp }
1084 f35fa46a 2020-07-23 stsp
1085 f35fa46a 2020-07-23 stsp /* Handle errors from first or second creation attempt. */
1086 f35fa46a 2020-07-23 stsp if (errno == ENAMETOOLONG) {
1087 8ba819a3 2020-07-23 stsp /* bad target path; install as a regular file */
1088 8ba819a3 2020-07-23 stsp got_object_blob_rewind(blob);
1089 8ba819a3 2020-07-23 stsp err = install_blob(worktree, ondisk_path, path,
1090 8ba819a3 2020-07-23 stsp GOT_DEFAULT_FILE_MODE, st_mode, blob,
1091 8ba819a3 2020-07-23 stsp restoring_missing_file, reverting_versioned_file,
1092 8ba819a3 2020-07-23 stsp repo, progress_cb, progress_arg);
1093 8ba819a3 2020-07-23 stsp } else if (errno == ENOTDIR) {
1094 8ba819a3 2020-07-23 stsp err = got_error_path(ondisk_path,
1095 8ba819a3 2020-07-23 stsp GOT_ERR_FILE_OBSTRUCTED);
1096 8ba819a3 2020-07-23 stsp } else {
1097 8ba819a3 2020-07-23 stsp err = got_error_from_errno3("symlink",
1098 8ba819a3 2020-07-23 stsp target_path, ondisk_path);
1099 8ba819a3 2020-07-23 stsp }
1100 f35fa46a 2020-07-23 stsp } else
1101 f35fa46a 2020-07-23 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1102 8ba819a3 2020-07-23 stsp done:
1103 8ba819a3 2020-07-23 stsp free(resolved_path);
1104 8ba819a3 2020-07-23 stsp free(abspath);
1105 8ba819a3 2020-07-23 stsp return err;
1106 8ba819a3 2020-07-23 stsp }
1107 8ba819a3 2020-07-23 stsp
1108 8ba819a3 2020-07-23 stsp static const struct got_error *
1109 8ba819a3 2020-07-23 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
1110 8ba819a3 2020-07-23 stsp const char *path, mode_t te_mode, mode_t st_mode,
1111 8ba819a3 2020-07-23 stsp struct got_blob_object *blob, int restoring_missing_file,
1112 8ba819a3 2020-07-23 stsp int reverting_versioned_file, struct got_repository *repo,
1113 8ba819a3 2020-07-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1114 8ba819a3 2020-07-23 stsp {
1115 8ba819a3 2020-07-23 stsp const struct got_error *err = NULL;
1116 507dc3bb 2018-12-29 stsp int fd = -1;
1117 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
1118 507dc3bb 2018-12-29 stsp int update = 0;
1119 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
1120 8ba819a3 2020-07-23 stsp
1121 8ba819a3 2020-07-23 stsp if (S_ISLNK(te_mode))
1122 8ba819a3 2020-07-23 stsp return install_symlink(worktree, ondisk_path, path, te_mode,
1123 8ba819a3 2020-07-23 stsp st_mode, blob, restoring_missing_file,
1124 8ba819a3 2020-07-23 stsp reverting_versioned_file, repo, progress_cb, progress_arg);
1125 9d31a1d8 2018-03-11 stsp
1126 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1127 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
1128 9d31a1d8 2018-03-11 stsp if (fd == -1) {
1129 21908da4 2019-01-13 stsp if (errno == ENOENT) {
1130 21908da4 2019-01-13 stsp char *parent = dirname(path);
1131 21908da4 2019-01-13 stsp if (parent == NULL)
1132 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
1133 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
1134 21908da4 2019-01-13 stsp if (err)
1135 21908da4 2019-01-13 stsp return err;
1136 21908da4 2019-01-13 stsp fd = open(ondisk_path,
1137 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1138 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
1139 21908da4 2019-01-13 stsp if (fd == -1)
1140 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
1141 230a42bd 2019-05-11 jcs ondisk_path);
1142 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
1143 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
1144 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
1145 3665fce0 2020-07-13 stsp err = got_error_path(ondisk_path,
1146 3665fce0 2020-07-13 stsp GOT_ERR_FILE_OBSTRUCTED);
1147 507dc3bb 2018-12-29 stsp goto done;
1148 d70b8e30 2018-12-27 stsp } else {
1149 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
1150 507dc3bb 2018-12-29 stsp ondisk_path);
1151 507dc3bb 2018-12-29 stsp if (err)
1152 507dc3bb 2018-12-29 stsp goto done;
1153 507dc3bb 2018-12-29 stsp update = 1;
1154 9d31a1d8 2018-03-11 stsp }
1155 507dc3bb 2018-12-29 stsp } else
1156 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
1157 9d31a1d8 2018-03-11 stsp }
1158 9d31a1d8 2018-03-11 stsp
1159 a378724f 2019-02-10 stsp if (restoring_missing_file)
1160 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
1161 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
1162 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
1163 a378724f 2019-02-10 stsp else
1164 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
1165 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1166 1ee397ad 2019-07-12 stsp if (err)
1167 1ee397ad 2019-07-12 stsp goto done;
1168 d7b62c98 2018-12-27 stsp
1169 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1170 9d31a1d8 2018-03-11 stsp do {
1171 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
1172 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
1173 9d31a1d8 2018-03-11 stsp if (err)
1174 9d31a1d8 2018-03-11 stsp break;
1175 9d31a1d8 2018-03-11 stsp if (len > 0) {
1176 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
1177 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1178 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
1179 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
1180 b87c6f83 2018-12-24 stsp goto done;
1181 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
1182 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
1183 b87c6f83 2018-12-24 stsp goto done;
1184 9d31a1d8 2018-03-11 stsp }
1185 61d6eaa3 2018-12-24 stsp hdrlen = 0;
1186 9d31a1d8 2018-03-11 stsp }
1187 9d31a1d8 2018-03-11 stsp } while (len != 0);
1188 9d31a1d8 2018-03-11 stsp
1189 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
1190 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
1191 816dc654 2019-02-16 stsp goto done;
1192 816dc654 2019-02-16 stsp }
1193 9d31a1d8 2018-03-11 stsp
1194 507dc3bb 2018-12-29 stsp if (update) {
1195 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
1196 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
1197 230a42bd 2019-05-11 jcs ondisk_path);
1198 2a57020b 2019-02-20 stsp unlink(tmppath);
1199 68ed9ba5 2019-02-10 stsp goto done;
1200 68ed9ba5 2019-02-10 stsp }
1201 68ed9ba5 2019-02-10 stsp }
1202 ba8a0d4d 2019-02-10 stsp
1203 1ebedb77 2019-10-19 stsp if (chmod(ondisk_path,
1204 1ebedb77 2019-10-19 stsp get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1205 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", ondisk_path);
1206 1ebedb77 2019-10-19 stsp goto done;
1207 507dc3bb 2018-12-29 stsp }
1208 507dc3bb 2018-12-29 stsp
1209 9d31a1d8 2018-03-11 stsp done:
1210 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1211 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1212 507dc3bb 2018-12-29 stsp free(tmppath);
1213 6353ad76 2019-02-08 stsp return err;
1214 6353ad76 2019-02-08 stsp }
1215 6353ad76 2019-02-08 stsp
1216 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1217 6353ad76 2019-02-08 stsp static const struct got_error *
1218 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1219 7154f6ce 2019-03-27 stsp {
1220 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1221 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1222 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1223 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1224 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1225 7154f6ce 2019-03-27 stsp };
1226 7154f6ce 2019-03-27 stsp int i = 0;
1227 7154f6ce 2019-03-27 stsp char *line;
1228 7154f6ce 2019-03-27 stsp size_t len;
1229 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1230 7154f6ce 2019-03-27 stsp
1231 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1232 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1233 7154f6ce 2019-03-27 stsp if (line == NULL) {
1234 7154f6ce 2019-03-27 stsp if (feof(f))
1235 7154f6ce 2019-03-27 stsp break;
1236 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1237 7154f6ce 2019-03-27 stsp break;
1238 7154f6ce 2019-03-27 stsp }
1239 7154f6ce 2019-03-27 stsp
1240 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1241 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1242 19332e6d 2019-05-13 stsp == 0)
1243 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1244 7154f6ce 2019-03-27 stsp else
1245 7154f6ce 2019-03-27 stsp i++;
1246 7154f6ce 2019-03-27 stsp }
1247 7154f6ce 2019-03-27 stsp }
1248 7154f6ce 2019-03-27 stsp
1249 7154f6ce 2019-03-27 stsp return err;
1250 e2b1e152 2019-07-13 stsp }
1251 e2b1e152 2019-07-13 stsp
1252 e2b1e152 2019-07-13 stsp static int
1253 1ebedb77 2019-10-19 stsp xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1254 1ebedb77 2019-10-19 stsp {
1255 1ebedb77 2019-10-19 stsp mode_t ie_mode = got_fileindex_perms_to_st(ie);
1256 1ebedb77 2019-10-19 stsp return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1257 1ebedb77 2019-10-19 stsp }
1258 1ebedb77 2019-10-19 stsp
1259 1ebedb77 2019-10-19 stsp static int
1260 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1261 e2b1e152 2019-07-13 stsp {
1262 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1263 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1264 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1265 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1266 1ebedb77 2019-10-19 stsp ie->size == (sb->st_size & 0xffffffff) &&
1267 1ebedb77 2019-10-19 stsp !xbit_differs(ie, sb->st_mode));
1268 c363b2c1 2019-08-03 stsp }
1269 c363b2c1 2019-08-03 stsp
1270 c363b2c1 2019-08-03 stsp static unsigned char
1271 c363b2c1 2019-08-03 stsp get_staged_status(struct got_fileindex_entry *ie)
1272 c363b2c1 2019-08-03 stsp {
1273 c363b2c1 2019-08-03 stsp switch (got_fileindex_entry_stage_get(ie)) {
1274 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_ADD:
1275 c363b2c1 2019-08-03 stsp return GOT_STATUS_ADD;
1276 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_DELETE:
1277 c363b2c1 2019-08-03 stsp return GOT_STATUS_DELETE;
1278 c363b2c1 2019-08-03 stsp case GOT_FILEIDX_STAGE_MODIFY:
1279 c363b2c1 2019-08-03 stsp return GOT_STATUS_MODIFY;
1280 c363b2c1 2019-08-03 stsp default:
1281 c363b2c1 2019-08-03 stsp return GOT_STATUS_NO_CHANGE;
1282 c363b2c1 2019-08-03 stsp }
1283 7154f6ce 2019-03-27 stsp }
1284 7154f6ce 2019-03-27 stsp
1285 7154f6ce 2019-03-27 stsp static const struct got_error *
1286 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1287 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1288 7f91a133 2019-12-13 stsp int dirfd, const char *de_name, struct got_repository *repo)
1289 6353ad76 2019-02-08 stsp {
1290 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1291 6353ad76 2019-02-08 stsp struct got_object_id id;
1292 6353ad76 2019-02-08 stsp size_t hdrlen;
1293 1338848f 2019-12-13 stsp int fd = -1;
1294 6353ad76 2019-02-08 stsp FILE *f = NULL;
1295 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1296 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1297 6353ad76 2019-02-08 stsp size_t flen, blen;
1298 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
1299 6353ad76 2019-02-08 stsp
1300 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1301 6353ad76 2019-02-08 stsp
1302 7f91a133 2019-12-13 stsp /*
1303 7f91a133 2019-12-13 stsp * Whenever the caller provides a directory descriptor and a
1304 7f91a133 2019-12-13 stsp * directory entry name for the file, use them! This prevents
1305 7f91a133 2019-12-13 stsp * race conditions if filesystem paths change beneath our feet.
1306 7f91a133 2019-12-13 stsp */
1307 7f91a133 2019-12-13 stsp if (dirfd != -1) {
1308 3d35a492 2019-12-13 stsp if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1309 882ef1b9 2019-12-13 stsp if (errno == ENOENT) {
1310 882ef1b9 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1311 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1312 882ef1b9 2019-12-13 stsp else
1313 882ef1b9 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1314 882ef1b9 2019-12-13 stsp goto done;
1315 882ef1b9 2019-12-13 stsp }
1316 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstatat", abspath);
1317 3d35a492 2019-12-13 stsp goto done;
1318 3d35a492 2019-12-13 stsp }
1319 7f91a133 2019-12-13 stsp } else {
1320 7f91a133 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1321 7f91a133 2019-12-13 stsp if (fd == -1 && errno != ENOENT)
1322 7f91a133 2019-12-13 stsp return got_error_from_errno2("open", abspath);
1323 3d35a492 2019-12-13 stsp if (fd == -1 || fstat(fd, sb) == -1) {
1324 3d35a492 2019-12-13 stsp if (errno == ENOENT) {
1325 3d35a492 2019-12-13 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1326 3d35a492 2019-12-13 stsp *status = GOT_STATUS_MISSING;
1327 3d35a492 2019-12-13 stsp else
1328 3d35a492 2019-12-13 stsp *status = GOT_STATUS_DELETE;
1329 3d35a492 2019-12-13 stsp goto done;
1330 3d35a492 2019-12-13 stsp }
1331 3d35a492 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
1332 1338848f 2019-12-13 stsp goto done;
1333 a378724f 2019-02-10 stsp }
1334 a378724f 2019-02-10 stsp }
1335 6353ad76 2019-02-08 stsp
1336 00bb5ea0 2020-07-23 stsp if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1337 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1338 1338848f 2019-12-13 stsp goto done;
1339 3f148bc6 2019-07-27 stsp }
1340 efdd40df 2019-07-27 stsp
1341 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1342 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1343 1338848f 2019-12-13 stsp goto done;
1344 244725f2 2019-08-03 stsp } else if (!got_fileindex_entry_has_blob(ie) &&
1345 244725f2 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
1346 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1347 1338848f 2019-12-13 stsp goto done;
1348 d00136be 2019-03-26 stsp }
1349 b8f41171 2019-02-10 stsp
1350 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1351 1338848f 2019-12-13 stsp goto done;
1352 6353ad76 2019-02-08 stsp
1353 c363b2c1 2019-08-03 stsp if (staged_status == GOT_STATUS_MODIFY ||
1354 c363b2c1 2019-08-03 stsp staged_status == GOT_STATUS_ADD)
1355 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1356 c363b2c1 2019-08-03 stsp else
1357 c363b2c1 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1358 c363b2c1 2019-08-03 stsp
1359 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1360 6353ad76 2019-02-08 stsp if (err)
1361 1338848f 2019-12-13 stsp goto done;
1362 6353ad76 2019-02-08 stsp
1363 3d35a492 2019-12-13 stsp if (dirfd != -1) {
1364 3d35a492 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1365 ab0d4361 2019-12-13 stsp if (fd == -1) {
1366 ab0d4361 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
1367 ab0d4361 2019-12-13 stsp goto done;
1368 ab0d4361 2019-12-13 stsp }
1369 3d35a492 2019-12-13 stsp }
1370 3d35a492 2019-12-13 stsp
1371 1338848f 2019-12-13 stsp f = fdopen(fd, "r");
1372 6353ad76 2019-02-08 stsp if (f == NULL) {
1373 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", abspath);
1374 6353ad76 2019-02-08 stsp goto done;
1375 6353ad76 2019-02-08 stsp }
1376 1338848f 2019-12-13 stsp fd = -1;
1377 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1378 656b1f76 2019-05-11 jcs for (;;) {
1379 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1380 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1381 6353ad76 2019-02-08 stsp if (err)
1382 7154f6ce 2019-03-27 stsp goto done;
1383 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1384 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1385 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1386 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1387 7154f6ce 2019-03-27 stsp goto done;
1388 80c5c120 2019-02-19 stsp }
1389 6353ad76 2019-02-08 stsp if (blen == 0) {
1390 6353ad76 2019-02-08 stsp if (flen != 0)
1391 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1392 6353ad76 2019-02-08 stsp break;
1393 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1394 6353ad76 2019-02-08 stsp if (blen != 0)
1395 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1396 6353ad76 2019-02-08 stsp break;
1397 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1398 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1399 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1400 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1401 6353ad76 2019-02-08 stsp break;
1402 6353ad76 2019-02-08 stsp }
1403 6353ad76 2019-02-08 stsp } else {
1404 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1405 6353ad76 2019-02-08 stsp break;
1406 6353ad76 2019-02-08 stsp }
1407 6353ad76 2019-02-08 stsp hdrlen = 0;
1408 6353ad76 2019-02-08 stsp }
1409 7154f6ce 2019-03-27 stsp
1410 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1411 7154f6ce 2019-03-27 stsp rewind(f);
1412 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1413 1ebedb77 2019-10-19 stsp } else if (xbit_differs(ie, sb->st_mode))
1414 1ebedb77 2019-10-19 stsp *status = GOT_STATUS_MODE_CHANGE;
1415 6353ad76 2019-02-08 stsp done:
1416 6353ad76 2019-02-08 stsp if (blob)
1417 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1418 43ff8261 2019-12-13 stsp if (f != NULL && fclose(f) == EOF && err == NULL)
1419 f4d199c9 2019-12-13 stsp err = got_error_from_errno2("fclose", abspath);
1420 1338848f 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
1421 1338848f 2019-12-13 stsp err = got_error_from_errno2("close", abspath);
1422 9d31a1d8 2018-03-11 stsp return err;
1423 e2b1e152 2019-07-13 stsp }
1424 e2b1e152 2019-07-13 stsp
1425 e2b1e152 2019-07-13 stsp /*
1426 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1427 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1428 e2b1e152 2019-07-13 stsp */
1429 e2b1e152 2019-07-13 stsp static const struct got_error *
1430 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1431 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1432 e2b1e152 2019-07-13 stsp {
1433 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1434 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1435 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1436 e2b1e152 2019-07-13 stsp
1437 e2b1e152 2019-07-13 stsp return NULL;
1438 9d31a1d8 2018-03-11 stsp }
1439 9d31a1d8 2018-03-11 stsp
1440 9d31a1d8 2018-03-11 stsp static const struct got_error *
1441 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1442 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1443 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1444 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1445 0584f854 2019-04-06 stsp void *progress_arg)
1446 9d31a1d8 2018-03-11 stsp {
1447 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1448 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1449 6353ad76 2019-02-08 stsp char *ondisk_path;
1450 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1451 b8f41171 2019-02-10 stsp struct stat sb;
1452 9d31a1d8 2018-03-11 stsp
1453 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1454 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1455 6353ad76 2019-02-08 stsp
1456 abb4604f 2019-07-27 stsp if (ie) {
1457 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1458 a76c42e6 2019-08-03 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1459 a76c42e6 2019-08-03 stsp goto done;
1460 a76c42e6 2019-08-03 stsp }
1461 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1462 7f91a133 2019-12-13 stsp repo);
1463 abb4604f 2019-07-27 stsp if (err)
1464 abb4604f 2019-07-27 stsp goto done;
1465 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1466 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1467 abb4604f 2019-07-27 stsp } else
1468 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1469 6353ad76 2019-02-08 stsp
1470 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1471 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1472 b8f41171 2019-02-10 stsp goto done;
1473 b8f41171 2019-02-10 stsp }
1474 5036ab18 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT) {
1475 5036ab18 2020-04-18 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1476 5036ab18 2020-04-18 stsp path);
1477 5036ab18 2020-04-18 stsp goto done;
1478 5036ab18 2020-04-18 stsp }
1479 b8f41171 2019-02-10 stsp
1480 523b8417 2019-10-19 stsp if (ie && status != GOT_STATUS_MISSING &&
1481 523b8417 2019-10-19 stsp (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1482 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1483 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1484 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1485 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1486 e2b1e152 2019-07-13 stsp if (err)
1487 e2b1e152 2019-07-13 stsp goto done;
1488 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1489 b8f41171 2019-02-10 stsp path);
1490 6353ad76 2019-02-08 stsp goto done;
1491 a378724f 2019-02-10 stsp }
1492 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1493 56e0773d 2019-11-28 stsp memcmp(ie->blob_sha1, te->id.sha1,
1494 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1495 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1496 b8f41171 2019-02-10 stsp goto done;
1497 e2b1e152 2019-07-13 stsp }
1498 9d31a1d8 2018-03-11 stsp }
1499 9d31a1d8 2018-03-11 stsp
1500 56e0773d 2019-11-28 stsp err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1501 8da9e5f4 2019-01-12 stsp if (err)
1502 6353ad76 2019-02-08 stsp goto done;
1503 512f0d0e 2019-01-02 stsp
1504 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1505 234035bc 2019-06-01 stsp int update_timestamps;
1506 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1507 f69721c3 2019-10-21 stsp char *label_orig = NULL;
1508 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1509 234035bc 2019-06-01 stsp struct got_object_id id2;
1510 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1511 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1512 234035bc 2019-06-01 stsp if (err)
1513 f69721c3 2019-10-21 stsp goto done;
1514 f69721c3 2019-10-21 stsp }
1515 f69721c3 2019-10-21 stsp if (got_fileindex_entry_has_commit(ie)) {
1516 f69721c3 2019-10-21 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
1517 f69721c3 2019-10-21 stsp if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1518 f69721c3 2019-10-21 stsp sizeof(id_str)) == NULL) {
1519 6dd1ece6 2019-11-10 stsp err = got_error_path(id_str,
1520 6dd1ece6 2019-11-10 stsp GOT_ERR_BAD_OBJ_ID_STR);
1521 f69721c3 2019-10-21 stsp goto done;
1522 f69721c3 2019-10-21 stsp }
1523 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
1524 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
1525 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
1526 234035bc 2019-06-01 stsp goto done;
1527 f69721c3 2019-10-21 stsp }
1528 234035bc 2019-06-01 stsp }
1529 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1530 f69721c3 2019-10-21 stsp ondisk_path, path, sb.st_mode, label_orig, blob,
1531 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1532 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1533 f69721c3 2019-10-21 stsp free(label_orig);
1534 234035bc 2019-06-01 stsp if (blob2)
1535 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1536 909d120e 2019-09-22 stsp if (err)
1537 909d120e 2019-09-22 stsp goto done;
1538 234035bc 2019-06-01 stsp /*
1539 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1540 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1541 234035bc 2019-06-01 stsp * unmodified files again.
1542 234035bc 2019-06-01 stsp */
1543 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1544 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1545 234035bc 2019-06-01 stsp update_timestamps);
1546 1ebedb77 2019-10-19 stsp } else if (status == GOT_STATUS_MODE_CHANGE) {
1547 1ebedb77 2019-10-19 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1548 1ebedb77 2019-10-19 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1549 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1550 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1551 1ee397ad 2019-07-12 stsp if (err)
1552 1ee397ad 2019-07-12 stsp goto done;
1553 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1554 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 0);
1555 13d9040b 2019-03-27 stsp if (err)
1556 13d9040b 2019-03-27 stsp goto done;
1557 13d9040b 2019-03-27 stsp } else {
1558 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1559 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1560 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1561 13d9040b 2019-03-27 stsp if (err)
1562 13d9040b 2019-03-27 stsp goto done;
1563 054041d0 2020-06-24 stsp if (ie) {
1564 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1565 054041d0 2020-06-24 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
1566 054041d0 2020-06-24 stsp } else {
1567 054041d0 2020-06-24 stsp err = create_fileindex_entry(fileindex,
1568 054041d0 2020-06-24 stsp worktree->base_commit_id, ondisk_path, path,
1569 054041d0 2020-06-24 stsp &blob->id);
1570 054041d0 2020-06-24 stsp }
1571 13d9040b 2019-03-27 stsp if (err)
1572 13d9040b 2019-03-27 stsp goto done;
1573 13d9040b 2019-03-27 stsp }
1574 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1575 6353ad76 2019-02-08 stsp done:
1576 6353ad76 2019-02-08 stsp free(ondisk_path);
1577 8da9e5f4 2019-01-12 stsp return err;
1578 90285c3b 2019-01-08 stsp }
1579 90285c3b 2019-01-08 stsp
1580 90285c3b 2019-01-08 stsp static const struct got_error *
1581 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1582 90285c3b 2019-01-08 stsp {
1583 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1584 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1585 90285c3b 2019-01-08 stsp
1586 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1587 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1588 90285c3b 2019-01-08 stsp
1589 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1590 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1591 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1592 c97665ae 2019-01-13 stsp } else {
1593 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1594 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1595 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1596 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1597 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1598 230a42bd 2019-05-11 jcs parent);
1599 90285c3b 2019-01-08 stsp break;
1600 90285c3b 2019-01-08 stsp }
1601 90285c3b 2019-01-08 stsp parent = dirname(parent);
1602 90285c3b 2019-01-08 stsp }
1603 90285c3b 2019-01-08 stsp }
1604 90285c3b 2019-01-08 stsp free(ondisk_path);
1605 90285c3b 2019-01-08 stsp return err;
1606 512f0d0e 2019-01-02 stsp }
1607 512f0d0e 2019-01-02 stsp
1608 708d8e67 2019-03-27 stsp static const struct got_error *
1609 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1610 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1611 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1612 708d8e67 2019-03-27 stsp {
1613 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1614 708d8e67 2019-03-27 stsp unsigned char status;
1615 708d8e67 2019-03-27 stsp struct stat sb;
1616 708d8e67 2019-03-27 stsp char *ondisk_path;
1617 708d8e67 2019-03-27 stsp
1618 a76c42e6 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1619 a76c42e6 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1620 a76c42e6 2019-08-03 stsp
1621 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1622 708d8e67 2019-03-27 stsp == -1)
1623 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1624 708d8e67 2019-03-27 stsp
1625 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1626 708d8e67 2019-03-27 stsp if (err)
1627 5a58a424 2020-06-23 stsp goto done;
1628 708d8e67 2019-03-27 stsp
1629 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1630 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1631 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1632 1ee397ad 2019-07-12 stsp if (err)
1633 5a58a424 2020-06-23 stsp goto done;
1634 fc6346c4 2019-03-27 stsp /*
1635 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1636 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1637 fc6346c4 2019-03-27 stsp */
1638 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1639 fc6346c4 2019-03-27 stsp 0);
1640 fc6346c4 2019-03-27 stsp } else {
1641 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1642 1ee397ad 2019-07-12 stsp if (err)
1643 5a58a424 2020-06-23 stsp goto done;
1644 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1645 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1646 fc6346c4 2019-03-27 stsp if (err)
1647 5a58a424 2020-06-23 stsp goto done;
1648 fc6346c4 2019-03-27 stsp }
1649 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1650 708d8e67 2019-03-27 stsp }
1651 5a58a424 2020-06-23 stsp done:
1652 5a58a424 2020-06-23 stsp free(ondisk_path);
1653 fc6346c4 2019-03-27 stsp return err;
1654 708d8e67 2019-03-27 stsp }
1655 708d8e67 2019-03-27 stsp
1656 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1657 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1658 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1659 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1660 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1661 8da9e5f4 2019-01-12 stsp void *progress_arg;
1662 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
1663 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1664 8da9e5f4 2019-01-12 stsp };
1665 8da9e5f4 2019-01-12 stsp
1666 512f0d0e 2019-01-02 stsp static const struct got_error *
1667 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1668 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1669 512f0d0e 2019-01-02 stsp {
1670 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1671 0584f854 2019-04-06 stsp
1672 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1673 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1674 512f0d0e 2019-01-02 stsp
1675 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1676 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1677 8da9e5f4 2019-01-12 stsp }
1678 512f0d0e 2019-01-02 stsp
1679 8da9e5f4 2019-01-12 stsp static const struct got_error *
1680 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1681 8da9e5f4 2019-01-12 stsp {
1682 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1683 7a9df742 2019-01-08 stsp
1684 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1685 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1686 0584f854 2019-04-06 stsp
1687 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1688 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1689 9d31a1d8 2018-03-11 stsp }
1690 9d31a1d8 2018-03-11 stsp
1691 9d31a1d8 2018-03-11 stsp static const struct got_error *
1692 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1693 9d31a1d8 2018-03-11 stsp {
1694 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1695 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1696 8da9e5f4 2019-01-12 stsp char *path;
1697 9d31a1d8 2018-03-11 stsp
1698 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1699 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1700 0584f854 2019-04-06 stsp
1701 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
1702 63c5ca5d 2019-08-24 stsp return NULL;
1703 63c5ca5d 2019-08-24 stsp
1704 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1705 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1706 8da9e5f4 2019-01-12 stsp == -1)
1707 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1708 9d31a1d8 2018-03-11 stsp
1709 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1710 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1711 8da9e5f4 2019-01-12 stsp else
1712 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1713 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1714 9d31a1d8 2018-03-11 stsp
1715 8da9e5f4 2019-01-12 stsp free(path);
1716 0cd1c46a 2019-03-11 stsp return err;
1717 0cd1c46a 2019-03-11 stsp }
1718 0cd1c46a 2019-03-11 stsp
1719 818c7501 2019-07-11 stsp static const struct got_error *
1720 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1721 0cd1c46a 2019-03-11 stsp {
1722 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1723 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1724 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1725 0cd1c46a 2019-03-11 stsp
1726 517bab73 2019-03-11 stsp *refname = NULL;
1727 517bab73 2019-03-11 stsp
1728 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1729 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1730 cc483380 2019-09-01 stsp return got_error_uuid(uuid_status, "uuid_to_string");
1731 0cd1c46a 2019-03-11 stsp
1732 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1733 0647c563 2019-03-11 stsp == -1) {
1734 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1735 0647c563 2019-03-11 stsp *refname = NULL;
1736 0cd1c46a 2019-03-11 stsp }
1737 517bab73 2019-03-11 stsp free(uuidstr);
1738 517bab73 2019-03-11 stsp return err;
1739 517bab73 2019-03-11 stsp }
1740 517bab73 2019-03-11 stsp
1741 818c7501 2019-07-11 stsp const struct got_error *
1742 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1743 818c7501 2019-07-11 stsp {
1744 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1745 818c7501 2019-07-11 stsp }
1746 818c7501 2019-07-11 stsp
1747 818c7501 2019-07-11 stsp static const struct got_error *
1748 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1749 818c7501 2019-07-11 stsp {
1750 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1751 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1752 818c7501 2019-07-11 stsp }
1753 818c7501 2019-07-11 stsp
1754 818c7501 2019-07-11 stsp static const struct got_error *
1755 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1756 818c7501 2019-07-11 stsp {
1757 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1758 818c7501 2019-07-11 stsp }
1759 818c7501 2019-07-11 stsp
1760 818c7501 2019-07-11 stsp static const struct got_error *
1761 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1762 818c7501 2019-07-11 stsp {
1763 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1764 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1765 818c7501 2019-07-11 stsp }
1766 818c7501 2019-07-11 stsp
1767 818c7501 2019-07-11 stsp static const struct got_error *
1768 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1769 818c7501 2019-07-11 stsp {
1770 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1771 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1772 0ebf8283 2019-07-24 stsp }
1773 0ebf8283 2019-07-24 stsp
1774 0ebf8283 2019-07-24 stsp static const struct got_error *
1775 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1776 0ebf8283 2019-07-24 stsp {
1777 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1778 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1779 0ebf8283 2019-07-24 stsp }
1780 0ebf8283 2019-07-24 stsp
1781 0ebf8283 2019-07-24 stsp static const struct got_error *
1782 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1783 0ebf8283 2019-07-24 stsp {
1784 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1785 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1786 0ebf8283 2019-07-24 stsp }
1787 0ebf8283 2019-07-24 stsp
1788 0ebf8283 2019-07-24 stsp static const struct got_error *
1789 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1790 0ebf8283 2019-07-24 stsp {
1791 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1792 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1793 818c7501 2019-07-11 stsp }
1794 818c7501 2019-07-11 stsp
1795 0ebf8283 2019-07-24 stsp static const struct got_error *
1796 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1797 0ebf8283 2019-07-24 stsp {
1798 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1799 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1800 0ebf8283 2019-07-24 stsp }
1801 818c7501 2019-07-11 stsp
1802 0ebf8283 2019-07-24 stsp const struct got_error *
1803 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1804 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1805 0ebf8283 2019-07-24 stsp {
1806 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1807 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1808 0ebf8283 2019-07-24 stsp *path = NULL;
1809 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1810 0ebf8283 2019-07-24 stsp }
1811 0ebf8283 2019-07-24 stsp return NULL;
1812 0ebf8283 2019-07-24 stsp }
1813 0ebf8283 2019-07-24 stsp
1814 517bab73 2019-03-11 stsp /*
1815 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1816 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1817 517bab73 2019-03-11 stsp */
1818 517bab73 2019-03-11 stsp static const struct got_error *
1819 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1820 517bab73 2019-03-11 stsp {
1821 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1822 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1823 517bab73 2019-03-11 stsp char *refname;
1824 517bab73 2019-03-11 stsp
1825 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1826 517bab73 2019-03-11 stsp if (err)
1827 517bab73 2019-03-11 stsp return err;
1828 0cd1c46a 2019-03-11 stsp
1829 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1830 0cd1c46a 2019-03-11 stsp if (err)
1831 0cd1c46a 2019-03-11 stsp goto done;
1832 0cd1c46a 2019-03-11 stsp
1833 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1834 0cd1c46a 2019-03-11 stsp done:
1835 0cd1c46a 2019-03-11 stsp free(refname);
1836 0cd1c46a 2019-03-11 stsp if (ref)
1837 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1838 3e3a69f1 2019-07-25 stsp return err;
1839 3e3a69f1 2019-07-25 stsp }
1840 3e3a69f1 2019-07-25 stsp
1841 3e3a69f1 2019-07-25 stsp static const struct got_error *
1842 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1843 3e3a69f1 2019-07-25 stsp {
1844 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1845 3e3a69f1 2019-07-25 stsp
1846 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1847 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1848 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1849 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1850 3e3a69f1 2019-07-25 stsp }
1851 9d31a1d8 2018-03-11 stsp return err;
1852 9d31a1d8 2018-03-11 stsp }
1853 9d31a1d8 2018-03-11 stsp
1854 3e3a69f1 2019-07-25 stsp
1855 ebf99748 2019-05-09 stsp static const struct got_error *
1856 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1857 4b55f459 2019-09-08 stsp struct got_worktree *worktree)
1858 ebf99748 2019-05-09 stsp {
1859 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1860 ebf99748 2019-05-09 stsp FILE *index = NULL;
1861 0cd1c46a 2019-03-11 stsp
1862 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1863 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1864 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1865 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1866 ebf99748 2019-05-09 stsp
1867 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1868 3e3a69f1 2019-07-25 stsp if (err)
1869 ebf99748 2019-05-09 stsp goto done;
1870 ebf99748 2019-05-09 stsp
1871 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1872 ebf99748 2019-05-09 stsp if (index == NULL) {
1873 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1874 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1875 ebf99748 2019-05-09 stsp } else {
1876 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1877 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1878 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1879 ebf99748 2019-05-09 stsp }
1880 ebf99748 2019-05-09 stsp done:
1881 ebf99748 2019-05-09 stsp if (err) {
1882 ebf99748 2019-05-09 stsp free(*fileindex_path);
1883 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1884 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1885 ebf99748 2019-05-09 stsp *fileindex = NULL;
1886 ebf99748 2019-05-09 stsp }
1887 ebf99748 2019-05-09 stsp return err;
1888 ebf99748 2019-05-09 stsp }
1889 c932eeeb 2019-05-22 stsp
1890 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1891 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1892 c932eeeb 2019-05-22 stsp const char *path;
1893 c932eeeb 2019-05-22 stsp size_t path_len;
1894 c932eeeb 2019-05-22 stsp const char *entry_name;
1895 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1896 a484d721 2019-06-10 stsp void *progress_arg;
1897 c932eeeb 2019-05-22 stsp };
1898 ebf99748 2019-05-09 stsp
1899 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1900 c932eeeb 2019-05-22 stsp static const struct got_error *
1901 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1902 c932eeeb 2019-05-22 stsp {
1903 1ee397ad 2019-07-12 stsp const struct got_error *err;
1904 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1905 c932eeeb 2019-05-22 stsp
1906 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1907 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1908 c932eeeb 2019-05-22 stsp return NULL;
1909 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1910 102ff934 2019-06-11 stsp return NULL;
1911 102ff934 2019-06-11 stsp
1912 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1913 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1914 c932eeeb 2019-05-22 stsp return NULL;
1915 c932eeeb 2019-05-22 stsp
1916 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1917 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1918 edd02c5e 2019-07-11 stsp ie->path);
1919 1ee397ad 2019-07-12 stsp if (err)
1920 1ee397ad 2019-07-12 stsp return err;
1921 1ee397ad 2019-07-12 stsp }
1922 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1923 c932eeeb 2019-05-22 stsp return NULL;
1924 9c6338c4 2019-06-02 stsp }
1925 9c6338c4 2019-06-02 stsp
1926 9c6338c4 2019-06-02 stsp static const struct got_error *
1927 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1928 9c6338c4 2019-06-02 stsp {
1929 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1930 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1931 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1932 867630bb 2020-01-17 stsp struct timespec timeout;
1933 9c6338c4 2019-06-02 stsp
1934 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1935 9c6338c4 2019-06-02 stsp fileindex_path);
1936 9c6338c4 2019-06-02 stsp if (err)
1937 9c6338c4 2019-06-02 stsp goto done;
1938 9c6338c4 2019-06-02 stsp
1939 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1940 9c6338c4 2019-06-02 stsp if (err)
1941 9c6338c4 2019-06-02 stsp goto done;
1942 9c6338c4 2019-06-02 stsp
1943 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1944 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1945 9c6338c4 2019-06-02 stsp fileindex_path);
1946 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1947 9c6338c4 2019-06-02 stsp }
1948 867630bb 2020-01-17 stsp
1949 867630bb 2020-01-17 stsp /*
1950 867630bb 2020-01-17 stsp * Sleep for a short amount of time to ensure that files modified after
1951 867630bb 2020-01-17 stsp * this program exits have a different time stamp from the one which
1952 867630bb 2020-01-17 stsp * was recorded in the file index.
1953 867630bb 2020-01-17 stsp */
1954 867630bb 2020-01-17 stsp timeout.tv_sec = 0;
1955 867630bb 2020-01-17 stsp timeout.tv_nsec = 1;
1956 867630bb 2020-01-17 stsp nanosleep(&timeout, NULL);
1957 9c6338c4 2019-06-02 stsp done:
1958 9c6338c4 2019-06-02 stsp if (new_index)
1959 9c6338c4 2019-06-02 stsp fclose(new_index);
1960 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1961 9c6338c4 2019-06-02 stsp return err;
1962 c932eeeb 2019-05-22 stsp }
1963 6ced4176 2019-07-12 stsp
1964 6ced4176 2019-07-12 stsp static const struct got_error *
1965 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1966 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1967 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1968 6ced4176 2019-07-12 stsp {
1969 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1970 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1971 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1972 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1973 6ced4176 2019-07-12 stsp
1974 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1975 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1976 6ced4176 2019-07-12 stsp *tree_id = NULL;
1977 6ced4176 2019-07-12 stsp
1978 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1979 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1980 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1981 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1982 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1983 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1984 6ced4176 2019-07-12 stsp goto done;
1985 6ced4176 2019-07-12 stsp }
1986 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1987 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1988 6ced4176 2019-07-12 stsp if (err)
1989 6ced4176 2019-07-12 stsp goto done;
1990 6ced4176 2019-07-12 stsp return NULL;
1991 6ced4176 2019-07-12 stsp }
1992 6ced4176 2019-07-12 stsp
1993 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1994 6ced4176 2019-07-12 stsp
1995 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1996 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1997 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1998 6ced4176 2019-07-12 stsp goto done;
1999 6ced4176 2019-07-12 stsp }
2000 6ced4176 2019-07-12 stsp
2001 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2002 6ced4176 2019-07-12 stsp in_repo_path);
2003 6ced4176 2019-07-12 stsp if (err)
2004 6ced4176 2019-07-12 stsp goto done;
2005 6ced4176 2019-07-12 stsp
2006 6ced4176 2019-07-12 stsp free(in_repo_path);
2007 6ced4176 2019-07-12 stsp in_repo_path = NULL;
2008 6ced4176 2019-07-12 stsp
2009 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
2010 6ced4176 2019-07-12 stsp if (err)
2011 6ced4176 2019-07-12 stsp goto done;
2012 c932eeeb 2019-05-22 stsp
2013 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2014 6ced4176 2019-07-12 stsp /* Check out a single file. */
2015 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
2016 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
2017 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
2018 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
2019 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2020 6ced4176 2019-07-12 stsp goto done;
2021 6ced4176 2019-07-12 stsp }
2022 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
2023 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2024 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2025 6ced4176 2019-07-12 stsp goto done;
2026 6ced4176 2019-07-12 stsp }
2027 6ced4176 2019-07-12 stsp } else {
2028 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
2029 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
2030 6ced4176 2019-07-12 stsp if (err)
2031 6ced4176 2019-07-12 stsp return err;
2032 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
2033 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
2034 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
2035 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
2036 6ced4176 2019-07-12 stsp goto done;
2037 6ced4176 2019-07-12 stsp }
2038 6ced4176 2019-07-12 stsp }
2039 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
2040 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
2041 6ced4176 2019-07-12 stsp } else {
2042 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
2043 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
2044 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
2045 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
2046 6ced4176 2019-07-12 stsp goto done;
2047 6ced4176 2019-07-12 stsp }
2048 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
2049 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
2050 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
2051 6ced4176 2019-07-12 stsp goto done;
2052 6ced4176 2019-07-12 stsp }
2053 6ced4176 2019-07-12 stsp }
2054 6ced4176 2019-07-12 stsp done:
2055 6ced4176 2019-07-12 stsp free(id);
2056 6ced4176 2019-07-12 stsp free(in_repo_path);
2057 6ced4176 2019-07-12 stsp if (err) {
2058 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
2059 6ced4176 2019-07-12 stsp free(*tree_relpath);
2060 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
2061 6ced4176 2019-07-12 stsp free(*tree_id);
2062 6ced4176 2019-07-12 stsp *tree_id = NULL;
2063 6ced4176 2019-07-12 stsp }
2064 07ed472d 2019-07-12 stsp return err;
2065 07ed472d 2019-07-12 stsp }
2066 07ed472d 2019-07-12 stsp
2067 07ed472d 2019-07-12 stsp static const struct got_error *
2068 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2069 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2070 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2071 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2072 07ed472d 2019-07-12 stsp {
2073 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
2074 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
2075 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
2076 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
2077 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
2078 07ed472d 2019-07-12 stsp
2079 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
2080 7f47418f 2019-12-20 stsp if (err) {
2081 6201aef3 2020-02-02 stsp if (!(err->code == GOT_ERR_ERRNO &&
2082 6201aef3 2020-02-02 stsp (errno == EACCES || errno == EROFS)))
2083 7f47418f 2019-12-20 stsp goto done;
2084 7f47418f 2019-12-20 stsp err = (*progress_cb)(progress_arg,
2085 7f47418f 2019-12-20 stsp GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2086 7f47418f 2019-12-20 stsp if (err)
2087 7f47418f 2019-12-20 stsp return err;
2088 7f47418f 2019-12-20 stsp }
2089 07ed472d 2019-07-12 stsp
2090 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
2091 07ed472d 2019-07-12 stsp worktree->base_commit_id);
2092 07ed472d 2019-07-12 stsp if (err)
2093 07ed472d 2019-07-12 stsp goto done;
2094 07ed472d 2019-07-12 stsp
2095 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2096 07ed472d 2019-07-12 stsp if (err)
2097 07ed472d 2019-07-12 stsp goto done;
2098 07ed472d 2019-07-12 stsp
2099 07ed472d 2019-07-12 stsp if (entry_name &&
2100 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
2101 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2102 07ed472d 2019-07-12 stsp goto done;
2103 07ed472d 2019-07-12 stsp }
2104 07ed472d 2019-07-12 stsp
2105 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
2106 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
2107 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
2108 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
2109 07ed472d 2019-07-12 stsp arg.worktree = worktree;
2110 07ed472d 2019-07-12 stsp arg.repo = repo;
2111 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
2112 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
2113 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
2114 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
2115 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
2116 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
2117 07ed472d 2019-07-12 stsp done:
2118 07ed472d 2019-07-12 stsp if (tree)
2119 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
2120 07ed472d 2019-07-12 stsp if (commit)
2121 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
2122 6ced4176 2019-07-12 stsp return err;
2123 6ced4176 2019-07-12 stsp }
2124 6ced4176 2019-07-12 stsp
2125 9d31a1d8 2018-03-11 stsp const struct got_error *
2126 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
2127 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2128 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2129 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2130 9d31a1d8 2018-03-11 stsp {
2131 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2132 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
2133 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
2134 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
2135 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2136 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2137 f2ea84fa 2019-07-27 stsp struct tree_path_data {
2138 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
2139 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
2140 f2ea84fa 2019-07-27 stsp int entry_type;
2141 f2ea84fa 2019-07-27 stsp char *relpath;
2142 f2ea84fa 2019-07-27 stsp char *entry_name;
2143 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
2144 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2145 9d31a1d8 2018-03-11 stsp
2146 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
2147 f2ea84fa 2019-07-27 stsp
2148 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
2149 9d31a1d8 2018-03-11 stsp if (err)
2150 9d31a1d8 2018-03-11 stsp return err;
2151 93a30277 2018-12-24 stsp
2152 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
2153 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2154 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
2155 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
2156 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
2157 f2ea84fa 2019-07-27 stsp goto done;
2158 f2ea84fa 2019-07-27 stsp }
2159 6ced4176 2019-07-12 stsp
2160 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
2161 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2162 f2ea84fa 2019-07-27 stsp if (err) {
2163 f2ea84fa 2019-07-27 stsp free(tpd);
2164 6ced4176 2019-07-12 stsp goto done;
2165 6ced4176 2019-07-12 stsp }
2166 f2ea84fa 2019-07-27 stsp
2167 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2168 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
2169 f2ea84fa 2019-07-27 stsp if (err) {
2170 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2171 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2172 f2ea84fa 2019-07-27 stsp free(tpd);
2173 f2ea84fa 2019-07-27 stsp goto done;
2174 f2ea84fa 2019-07-27 stsp }
2175 f2ea84fa 2019-07-27 stsp } else
2176 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
2177 f2ea84fa 2019-07-27 stsp
2178 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2179 6ced4176 2019-07-12 stsp }
2180 6ced4176 2019-07-12 stsp
2181 51514078 2018-12-25 stsp /*
2182 51514078 2018-12-25 stsp * Read the file index.
2183 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
2184 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
2185 51514078 2018-12-25 stsp */
2186 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2187 8da9e5f4 2019-01-12 stsp if (err)
2188 c4cdcb68 2019-04-03 stsp goto done;
2189 c4cdcb68 2019-04-03 stsp
2190 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2191 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2192 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
2193 f2ea84fa 2019-07-27 stsp
2194 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
2195 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
2196 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
2197 f2ea84fa 2019-07-27 stsp if (err)
2198 f2ea84fa 2019-07-27 stsp break;
2199 f2ea84fa 2019-07-27 stsp
2200 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
2201 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
2202 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
2203 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
2204 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
2205 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
2206 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
2207 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
2208 f2ea84fa 2019-07-27 stsp if (err)
2209 f2ea84fa 2019-07-27 stsp break;
2210 f2ea84fa 2019-07-27 stsp
2211 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
2212 711d9cd9 2019-07-12 stsp }
2213 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2214 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2215 af12c6b9 2019-06-04 stsp err = sync_err;
2216 9d31a1d8 2018-03-11 stsp done:
2217 9c6338c4 2019-06-02 stsp free(fileindex_path);
2218 edfa7d7f 2018-09-11 stsp if (tree)
2219 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
2220 9d31a1d8 2018-03-11 stsp if (commit)
2221 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
2222 5ade8233 2019-07-12 stsp if (fileindex)
2223 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
2224 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
2225 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
2226 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2227 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
2228 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
2229 f2ea84fa 2019-07-27 stsp free(tpd);
2230 f2ea84fa 2019-07-27 stsp }
2231 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2232 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
2233 9d31a1d8 2018-03-11 stsp err = unlockerr;
2234 f8d1f275 2019-02-04 stsp return err;
2235 f8d1f275 2019-02-04 stsp }
2236 f8d1f275 2019-02-04 stsp
2237 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
2238 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2239 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
2240 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
2241 234035bc 2019-06-01 stsp void *progress_arg;
2242 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2243 234035bc 2019-06-01 stsp void *cancel_arg;
2244 f69721c3 2019-10-21 stsp const char *label_orig;
2245 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
2246 234035bc 2019-06-01 stsp };
2247 234035bc 2019-06-01 stsp
2248 234035bc 2019-06-01 stsp static const struct got_error *
2249 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
2250 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
2251 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
2252 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
2253 234035bc 2019-06-01 stsp {
2254 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
2255 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
2256 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
2257 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
2258 234035bc 2019-06-01 stsp struct stat sb;
2259 234035bc 2019-06-01 stsp unsigned char status;
2260 234035bc 2019-06-01 stsp int local_changes_subsumed;
2261 234035bc 2019-06-01 stsp
2262 234035bc 2019-06-01 stsp if (blob1 && blob2) {
2263 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2264 d6c87207 2019-08-02 stsp strlen(path2));
2265 1ee397ad 2019-07-12 stsp if (ie == NULL)
2266 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2267 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
2268 234035bc 2019-06-01 stsp
2269 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2270 234035bc 2019-06-01 stsp path2) == -1)
2271 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2272 234035bc 2019-06-01 stsp
2273 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2274 7f91a133 2019-12-13 stsp repo);
2275 234035bc 2019-06-01 stsp if (err)
2276 234035bc 2019-06-01 stsp goto done;
2277 234035bc 2019-06-01 stsp
2278 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2279 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2280 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
2281 234035bc 2019-06-01 stsp goto done;
2282 234035bc 2019-06-01 stsp }
2283 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2284 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2285 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2286 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2287 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
2288 234035bc 2019-06-01 stsp goto done;
2289 234035bc 2019-06-01 stsp }
2290 234035bc 2019-06-01 stsp
2291 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
2292 f69721c3 2019-10-21 stsp ondisk_path, path2, sb.st_mode, a->label_orig, blob2,
2293 f69721c3 2019-10-21 stsp a->commit_id2, repo, a->progress_cb, a->progress_arg);
2294 234035bc 2019-06-01 stsp } else if (blob1) {
2295 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
2296 d6c87207 2019-08-02 stsp strlen(path1));
2297 1ee397ad 2019-07-12 stsp if (ie == NULL)
2298 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
2299 3c24af98 2020-02-07 tracey GOT_STATUS_MISSING, path1);
2300 2b92fad7 2019-06-02 stsp
2301 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2302 2b92fad7 2019-06-02 stsp path1) == -1)
2303 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
2304 2b92fad7 2019-06-02 stsp
2305 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2306 7f91a133 2019-12-13 stsp repo);
2307 2b92fad7 2019-06-02 stsp if (err)
2308 2b92fad7 2019-06-02 stsp goto done;
2309 2b92fad7 2019-06-02 stsp
2310 2b92fad7 2019-06-02 stsp switch (status) {
2311 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
2312 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2313 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2314 1ee397ad 2019-07-12 stsp if (err)
2315 1ee397ad 2019-07-12 stsp goto done;
2316 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
2317 2b92fad7 2019-06-02 stsp if (err)
2318 2b92fad7 2019-06-02 stsp goto done;
2319 2b92fad7 2019-06-02 stsp if (ie)
2320 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2321 2b92fad7 2019-06-02 stsp break;
2322 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
2323 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
2324 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2325 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
2326 1ee397ad 2019-07-12 stsp if (err)
2327 1ee397ad 2019-07-12 stsp goto done;
2328 2b92fad7 2019-06-02 stsp if (ie)
2329 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2330 2b92fad7 2019-06-02 stsp break;
2331 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
2332 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
2333 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
2334 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2335 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
2336 1ee397ad 2019-07-12 stsp if (err)
2337 1ee397ad 2019-07-12 stsp goto done;
2338 2b92fad7 2019-06-02 stsp break;
2339 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
2340 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
2341 1ee397ad 2019-07-12 stsp if (err)
2342 1ee397ad 2019-07-12 stsp goto done;
2343 2b92fad7 2019-06-02 stsp break;
2344 2b92fad7 2019-06-02 stsp default:
2345 2b92fad7 2019-06-02 stsp break;
2346 2b92fad7 2019-06-02 stsp }
2347 234035bc 2019-06-01 stsp } else if (blob2) {
2348 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2349 234035bc 2019-06-01 stsp path2) == -1)
2350 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2351 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
2352 d6c87207 2019-08-02 stsp strlen(path2));
2353 234035bc 2019-06-01 stsp if (ie) {
2354 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2355 7f91a133 2019-12-13 stsp -1, NULL, repo);
2356 234035bc 2019-06-01 stsp if (err)
2357 234035bc 2019-06-01 stsp goto done;
2358 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2359 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2360 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2361 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2362 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2363 1ee397ad 2019-07-12 stsp status, path2);
2364 234035bc 2019-06-01 stsp goto done;
2365 234035bc 2019-06-01 stsp }
2366 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2367 f69721c3 2019-10-21 stsp NULL, ondisk_path, path2, sb.st_mode,
2368 f69721c3 2019-10-21 stsp a->label_orig, blob2, a->commit_id2, repo,
2369 f69721c3 2019-10-21 stsp a->progress_cb,
2370 f69721c3 2019-10-21 stsp a->progress_arg);
2371 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2372 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
2373 054041d0 2020-06-24 stsp ondisk_path, blob2->id.sha1,
2374 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 0);
2375 234035bc 2019-06-01 stsp if (err)
2376 234035bc 2019-06-01 stsp goto done;
2377 234035bc 2019-06-01 stsp }
2378 234035bc 2019-06-01 stsp } else {
2379 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2380 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2381 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2382 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2383 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2384 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2385 234035bc 2019-06-01 stsp if (err)
2386 234035bc 2019-06-01 stsp goto done;
2387 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, path2);
2388 234035bc 2019-06-01 stsp if (err)
2389 234035bc 2019-06-01 stsp goto done;
2390 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path,
2391 3969253a 2020-03-07 stsp NULL, NULL, 1);
2392 3969253a 2020-03-07 stsp if (err) {
2393 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
2394 3969253a 2020-03-07 stsp goto done;
2395 3969253a 2020-03-07 stsp }
2396 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2397 2b92fad7 2019-06-02 stsp if (err) {
2398 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2399 2b92fad7 2019-06-02 stsp goto done;
2400 2b92fad7 2019-06-02 stsp }
2401 234035bc 2019-06-01 stsp }
2402 234035bc 2019-06-01 stsp }
2403 234035bc 2019-06-01 stsp done:
2404 234035bc 2019-06-01 stsp free(ondisk_path);
2405 234035bc 2019-06-01 stsp return err;
2406 234035bc 2019-06-01 stsp }
2407 234035bc 2019-06-01 stsp
2408 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2409 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2410 234035bc 2019-06-01 stsp struct got_repository *repo;
2411 234035bc 2019-06-01 stsp };
2412 234035bc 2019-06-01 stsp
2413 234035bc 2019-06-01 stsp static const struct got_error *
2414 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2415 234035bc 2019-06-01 stsp {
2416 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2417 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2418 234035bc 2019-06-01 stsp unsigned char status;
2419 234035bc 2019-06-01 stsp struct stat sb;
2420 234035bc 2019-06-01 stsp char *ondisk_path;
2421 234035bc 2019-06-01 stsp
2422 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2423 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2424 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2425 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2426 234035bc 2019-06-01 stsp
2427 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2428 234035bc 2019-06-01 stsp == -1)
2429 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2430 234035bc 2019-06-01 stsp
2431 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2432 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2433 234035bc 2019-06-01 stsp if (err)
2434 234035bc 2019-06-01 stsp return err;
2435 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2436 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2437 234035bc 2019-06-01 stsp
2438 234035bc 2019-06-01 stsp return NULL;
2439 234035bc 2019-06-01 stsp }
2440 234035bc 2019-06-01 stsp
2441 818c7501 2019-07-11 stsp static const struct got_error *
2442 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2443 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2444 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2445 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2446 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2447 234035bc 2019-06-01 stsp {
2448 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2449 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2450 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2451 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2452 f69721c3 2019-10-21 stsp char *label_orig = NULL;
2453 234035bc 2019-06-01 stsp
2454 03415a1a 2019-06-02 stsp if (commit_id1) {
2455 f69721c3 2019-10-21 stsp char *id_str;
2456 f69721c3 2019-10-21 stsp
2457 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2458 03415a1a 2019-06-02 stsp worktree->path_prefix);
2459 03415a1a 2019-06-02 stsp if (err)
2460 03415a1a 2019-06-02 stsp goto done;
2461 03415a1a 2019-06-02 stsp
2462 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2463 03415a1a 2019-06-02 stsp if (err)
2464 03415a1a 2019-06-02 stsp goto done;
2465 f69721c3 2019-10-21 stsp
2466 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str, commit_id1);
2467 f69721c3 2019-10-21 stsp if (err)
2468 f69721c3 2019-10-21 stsp goto done;
2469 f69721c3 2019-10-21 stsp
2470 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s",
2471 f69721c3 2019-10-21 stsp GOT_MERGE_LABEL_BASE, id_str) == -1) {
2472 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
2473 f69721c3 2019-10-21 stsp free(id_str);
2474 f69721c3 2019-10-21 stsp goto done;
2475 f69721c3 2019-10-21 stsp }
2476 f69721c3 2019-10-21 stsp free(id_str);
2477 03415a1a 2019-06-02 stsp }
2478 234035bc 2019-06-01 stsp
2479 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2480 234035bc 2019-06-01 stsp worktree->path_prefix);
2481 234035bc 2019-06-01 stsp if (err)
2482 234035bc 2019-06-01 stsp goto done;
2483 234035bc 2019-06-01 stsp
2484 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2485 234035bc 2019-06-01 stsp if (err)
2486 234035bc 2019-06-01 stsp goto done;
2487 234035bc 2019-06-01 stsp
2488 234035bc 2019-06-01 stsp arg.worktree = worktree;
2489 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2490 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2491 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2492 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2493 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2494 f69721c3 2019-10-21 stsp arg.label_orig = label_orig;
2495 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2496 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2497 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2498 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2499 af12c6b9 2019-06-04 stsp err = sync_err;
2500 234035bc 2019-06-01 stsp done:
2501 234035bc 2019-06-01 stsp if (tree1)
2502 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2503 234035bc 2019-06-01 stsp if (tree2)
2504 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2505 f69721c3 2019-10-21 stsp free(label_orig);
2506 818c7501 2019-07-11 stsp return err;
2507 818c7501 2019-07-11 stsp }
2508 234035bc 2019-06-01 stsp
2509 818c7501 2019-07-11 stsp const struct got_error *
2510 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2511 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2512 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2513 e6209546 2019-08-22 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2514 818c7501 2019-07-11 stsp {
2515 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2516 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2517 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2518 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2519 818c7501 2019-07-11 stsp
2520 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2521 818c7501 2019-07-11 stsp if (err)
2522 818c7501 2019-07-11 stsp return err;
2523 818c7501 2019-07-11 stsp
2524 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2525 818c7501 2019-07-11 stsp if (err)
2526 818c7501 2019-07-11 stsp goto done;
2527 818c7501 2019-07-11 stsp
2528 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2529 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2530 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2531 818c7501 2019-07-11 stsp &mok_arg);
2532 818c7501 2019-07-11 stsp if (err)
2533 818c7501 2019-07-11 stsp goto done;
2534 818c7501 2019-07-11 stsp
2535 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2536 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2537 818c7501 2019-07-11 stsp done:
2538 818c7501 2019-07-11 stsp if (fileindex)
2539 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2540 818c7501 2019-07-11 stsp free(fileindex_path);
2541 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2542 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2543 234035bc 2019-06-01 stsp err = unlockerr;
2544 234035bc 2019-06-01 stsp return err;
2545 234035bc 2019-06-01 stsp }
2546 234035bc 2019-06-01 stsp
2547 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2548 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2549 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2550 927df6b7 2019-02-10 stsp const char *status_path;
2551 927df6b7 2019-02-10 stsp size_t status_path_len;
2552 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2553 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2554 f8d1f275 2019-02-04 stsp void *status_arg;
2555 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb;
2556 f8d1f275 2019-02-04 stsp void *cancel_arg;
2557 6841da00 2019-08-08 stsp /* A pathlist containing per-directory pathlists of ignore patterns. */
2558 6841da00 2019-08-08 stsp struct got_pathlist_head ignores;
2559 f2a9dc41 2019-12-13 tracey int report_unchanged;
2560 3143d852 2020-06-25 stsp int no_ignores;
2561 f8d1f275 2019-02-04 stsp };
2562 88d0e355 2019-08-03 stsp
2563 f8d1f275 2019-02-04 stsp static const struct got_error *
2564 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2565 7f91a133 2019-12-13 stsp int dirfd, const char *de_name,
2566 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2567 f2a9dc41 2019-12-13 tracey struct got_repository *repo, int report_unchanged)
2568 927df6b7 2019-02-10 stsp {
2569 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2570 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2571 c363b2c1 2019-08-03 stsp unsigned char staged_status = get_staged_status(ie);
2572 927df6b7 2019-02-10 stsp struct stat sb;
2573 537ac44b 2019-08-03 stsp struct got_object_id blob_id, commit_id, staged_blob_id;
2574 98eaaa12 2019-08-03 stsp struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2575 98eaaa12 2019-08-03 stsp struct got_object_id *staged_blob_idp = NULL;
2576 927df6b7 2019-02-10 stsp
2577 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2578 98eaaa12 2019-08-03 stsp if (err)
2579 98eaaa12 2019-08-03 stsp return err;
2580 98eaaa12 2019-08-03 stsp
2581 98eaaa12 2019-08-03 stsp if (status == GOT_STATUS_NO_CHANGE &&
2582 f2a9dc41 2019-12-13 tracey staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2583 98eaaa12 2019-08-03 stsp return NULL;
2584 98eaaa12 2019-08-03 stsp
2585 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_blob(ie)) {
2586 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2587 98eaaa12 2019-08-03 stsp blob_idp = &blob_id;
2588 98eaaa12 2019-08-03 stsp }
2589 98eaaa12 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
2590 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2591 98eaaa12 2019-08-03 stsp commit_idp = &commit_id;
2592 927df6b7 2019-02-10 stsp }
2593 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2594 98eaaa12 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
2595 98eaaa12 2019-08-03 stsp memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2596 98eaaa12 2019-08-03 stsp SHA1_DIGEST_LENGTH);
2597 98eaaa12 2019-08-03 stsp staged_blob_idp = &staged_blob_id;
2598 98eaaa12 2019-08-03 stsp }
2599 98eaaa12 2019-08-03 stsp
2600 98eaaa12 2019-08-03 stsp return (*status_cb)(status_arg, status, staged_status,
2601 12463d8b 2019-12-13 stsp ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2602 927df6b7 2019-02-10 stsp }
2603 927df6b7 2019-02-10 stsp
2604 927df6b7 2019-02-10 stsp static const struct got_error *
2605 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2606 7f91a133 2019-12-13 stsp struct dirent *de, const char *parent_path, int dirfd)
2607 f8d1f275 2019-02-04 stsp {
2608 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2609 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2610 f8d1f275 2019-02-04 stsp char *abspath;
2611 f8d1f275 2019-02-04 stsp
2612 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2613 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2614 0584f854 2019-04-06 stsp
2615 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2616 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2617 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2618 927df6b7 2019-02-10 stsp return NULL;
2619 927df6b7 2019-02-10 stsp
2620 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2621 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2622 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2623 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2624 f8d1f275 2019-02-04 stsp } else {
2625 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2626 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2627 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2628 f8d1f275 2019-02-04 stsp }
2629 f8d1f275 2019-02-04 stsp
2630 7f91a133 2019-12-13 stsp err = report_file_status(ie, abspath, dirfd, de->d_name,
2631 7f91a133 2019-12-13 stsp a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2632 f8d1f275 2019-02-04 stsp free(abspath);
2633 9d31a1d8 2018-03-11 stsp return err;
2634 9d31a1d8 2018-03-11 stsp }
2635 f8d1f275 2019-02-04 stsp
2636 f8d1f275 2019-02-04 stsp static const struct got_error *
2637 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2638 f8d1f275 2019-02-04 stsp {
2639 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2640 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2641 2ec1f75b 2019-03-26 stsp unsigned char status;
2642 927df6b7 2019-02-10 stsp
2643 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2644 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2645 0584f854 2019-04-06 stsp
2646 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2647 927df6b7 2019-02-10 stsp return NULL;
2648 927df6b7 2019-02-10 stsp
2649 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2650 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2651 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2652 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2653 2ec1f75b 2019-03-26 stsp else
2654 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2655 88d0e355 2019-08-03 stsp return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2656 12463d8b 2019-12-13 stsp ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2657 6841da00 2019-08-08 stsp }
2658 6841da00 2019-08-08 stsp
2659 6841da00 2019-08-08 stsp void
2660 6841da00 2019-08-08 stsp free_ignorelist(struct got_pathlist_head *ignorelist)
2661 6841da00 2019-08-08 stsp {
2662 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2663 6841da00 2019-08-08 stsp
2664 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignorelist, entry)
2665 6841da00 2019-08-08 stsp free((char *)pe->path);
2666 6841da00 2019-08-08 stsp got_pathlist_free(ignorelist);
2667 6841da00 2019-08-08 stsp }
2668 6841da00 2019-08-08 stsp
2669 6841da00 2019-08-08 stsp void
2670 6841da00 2019-08-08 stsp free_ignores(struct got_pathlist_head *ignores)
2671 6841da00 2019-08-08 stsp {
2672 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2673 6841da00 2019-08-08 stsp
2674 6841da00 2019-08-08 stsp TAILQ_FOREACH(pe, ignores, entry) {
2675 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2676 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2677 6841da00 2019-08-08 stsp free((char *)pe->path);
2678 6841da00 2019-08-08 stsp }
2679 6841da00 2019-08-08 stsp got_pathlist_free(ignores);
2680 6841da00 2019-08-08 stsp }
2681 6841da00 2019-08-08 stsp
2682 6841da00 2019-08-08 stsp static const struct got_error *
2683 6841da00 2019-08-08 stsp read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2684 6841da00 2019-08-08 stsp {
2685 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2686 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe = NULL;
2687 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist;
2688 a0de39f3 2019-08-09 stsp char *line = NULL, *pattern, *dirpath = NULL;
2689 6841da00 2019-08-08 stsp size_t linesize = 0;
2690 6841da00 2019-08-08 stsp ssize_t linelen;
2691 6841da00 2019-08-08 stsp
2692 6841da00 2019-08-08 stsp ignorelist = calloc(1, sizeof(*ignorelist));
2693 6841da00 2019-08-08 stsp if (ignorelist == NULL)
2694 6841da00 2019-08-08 stsp return got_error_from_errno("calloc");
2695 6841da00 2019-08-08 stsp TAILQ_INIT(ignorelist);
2696 6841da00 2019-08-08 stsp
2697 6841da00 2019-08-08 stsp while ((linelen = getline(&line, &linesize, f)) != -1) {
2698 6841da00 2019-08-08 stsp if (linelen > 0 && line[linelen - 1] == '\n')
2699 6841da00 2019-08-08 stsp line[linelen - 1] = '\0';
2700 bd8de430 2019-10-04 stsp
2701 bd8de430 2019-10-04 stsp /* Git's ignores may contain comments. */
2702 bd8de430 2019-10-04 stsp if (line[0] == '#')
2703 bd8de430 2019-10-04 stsp continue;
2704 bd8de430 2019-10-04 stsp
2705 bd8de430 2019-10-04 stsp /* Git's negated patterns are not (yet?) supported. */
2706 bd8de430 2019-10-04 stsp if (line[0] == '!')
2707 bd8de430 2019-10-04 stsp continue;
2708 bd8de430 2019-10-04 stsp
2709 6841da00 2019-08-08 stsp if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
2710 6841da00 2019-08-08 stsp line) == -1) {
2711 6841da00 2019-08-08 stsp err = got_error_from_errno("asprintf");
2712 6841da00 2019-08-08 stsp goto done;
2713 6841da00 2019-08-08 stsp }
2714 6841da00 2019-08-08 stsp err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
2715 6841da00 2019-08-08 stsp if (err)
2716 6841da00 2019-08-08 stsp goto done;
2717 6841da00 2019-08-08 stsp }
2718 6841da00 2019-08-08 stsp if (ferror(f)) {
2719 6841da00 2019-08-08 stsp err = got_error_from_errno("getline");
2720 6841da00 2019-08-08 stsp goto done;
2721 6841da00 2019-08-08 stsp }
2722 6841da00 2019-08-08 stsp
2723 6841da00 2019-08-08 stsp dirpath = strdup(path);
2724 6841da00 2019-08-08 stsp if (dirpath == NULL) {
2725 6841da00 2019-08-08 stsp err = got_error_from_errno("strdup");
2726 6841da00 2019-08-08 stsp goto done;
2727 6841da00 2019-08-08 stsp }
2728 6841da00 2019-08-08 stsp err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
2729 6841da00 2019-08-08 stsp done:
2730 6841da00 2019-08-08 stsp free(line);
2731 6841da00 2019-08-08 stsp if (err || pe == NULL) {
2732 6841da00 2019-08-08 stsp free(dirpath);
2733 6841da00 2019-08-08 stsp free_ignorelist(ignorelist);
2734 6841da00 2019-08-08 stsp }
2735 6841da00 2019-08-08 stsp return err;
2736 6841da00 2019-08-08 stsp }
2737 6841da00 2019-08-08 stsp
2738 6841da00 2019-08-08 stsp int
2739 6841da00 2019-08-08 stsp match_ignores(struct got_pathlist_head *ignores, const char *path)
2740 6841da00 2019-08-08 stsp {
2741 6841da00 2019-08-08 stsp struct got_pathlist_entry *pe;
2742 bd8de430 2019-10-04 stsp
2743 bd8de430 2019-10-04 stsp /* Handle patterns which match in all directories. */
2744 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pe, ignores, entry) {
2745 bd8de430 2019-10-04 stsp struct got_pathlist_head *ignorelist = pe->data;
2746 bd8de430 2019-10-04 stsp struct got_pathlist_entry *pi;
2747 bd8de430 2019-10-04 stsp
2748 bd8de430 2019-10-04 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
2749 bd8de430 2019-10-04 stsp const char *p, *pattern = pi->path;
2750 6841da00 2019-08-08 stsp
2751 bd8de430 2019-10-04 stsp if (strncmp(pattern, "**/", 3) != 0)
2752 bd8de430 2019-10-04 stsp continue;
2753 bd8de430 2019-10-04 stsp pattern += 3;
2754 bd8de430 2019-10-04 stsp p = path;
2755 bd8de430 2019-10-04 stsp while (*p) {
2756 bd8de430 2019-10-04 stsp if (fnmatch(pattern, p,
2757 bd8de430 2019-10-04 stsp FNM_PATHNAME | FNM_LEADING_DIR)) {
2758 bd8de430 2019-10-04 stsp /* Retry in next directory. */
2759 bd8de430 2019-10-04 stsp while (*p && *p != '/')
2760 bd8de430 2019-10-04 stsp p++;
2761 bd8de430 2019-10-04 stsp while (*p == '/')
2762 bd8de430 2019-10-04 stsp p++;
2763 bd8de430 2019-10-04 stsp continue;
2764 bd8de430 2019-10-04 stsp }
2765 bd8de430 2019-10-04 stsp return 1;
2766 bd8de430 2019-10-04 stsp }
2767 bd8de430 2019-10-04 stsp }
2768 bd8de430 2019-10-04 stsp }
2769 bd8de430 2019-10-04 stsp
2770 6841da00 2019-08-08 stsp /*
2771 6841da00 2019-08-08 stsp * The ignores pathlist contains ignore lists from children before
2772 6841da00 2019-08-08 stsp * parents, so we can find the most specific ignorelist by walking
2773 6841da00 2019-08-08 stsp * ignores backwards.
2774 6841da00 2019-08-08 stsp */
2775 6841da00 2019-08-08 stsp pe = TAILQ_LAST(ignores, got_pathlist_head);
2776 6841da00 2019-08-08 stsp while (pe) {
2777 6841da00 2019-08-08 stsp if (got_path_is_child(path, pe->path, pe->path_len)) {
2778 6841da00 2019-08-08 stsp struct got_pathlist_head *ignorelist = pe->data;
2779 6841da00 2019-08-08 stsp struct got_pathlist_entry *pi;
2780 6841da00 2019-08-08 stsp TAILQ_FOREACH(pi, ignorelist, entry) {
2781 bd8de430 2019-10-04 stsp const char *pattern = pi->path;
2782 bd8de430 2019-10-04 stsp int flags = FNM_LEADING_DIR;
2783 bd8de430 2019-10-04 stsp if (strstr(pattern, "/**/") == NULL)
2784 bd8de430 2019-10-04 stsp flags |= FNM_PATHNAME;
2785 bd8de430 2019-10-04 stsp if (fnmatch(pattern, path, flags))
2786 6841da00 2019-08-08 stsp continue;
2787 6841da00 2019-08-08 stsp return 1;
2788 6841da00 2019-08-08 stsp }
2789 6841da00 2019-08-08 stsp }
2790 6841da00 2019-08-08 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
2791 6841da00 2019-08-08 stsp }
2792 6841da00 2019-08-08 stsp
2793 6841da00 2019-08-08 stsp return 0;
2794 6841da00 2019-08-08 stsp }
2795 6841da00 2019-08-08 stsp
2796 6841da00 2019-08-08 stsp static const struct got_error *
2797 b80270a7 2019-08-08 stsp add_ignores(struct got_pathlist_head *ignores, const char *root_path,
2798 886cec17 2019-12-15 stsp const char *path, int dirfd, const char *ignores_filename)
2799 6841da00 2019-08-08 stsp {
2800 6841da00 2019-08-08 stsp const struct got_error *err = NULL;
2801 6841da00 2019-08-08 stsp char *ignorespath;
2802 886cec17 2019-12-15 stsp int fd = -1;
2803 6841da00 2019-08-08 stsp FILE *ignoresfile = NULL;
2804 6841da00 2019-08-08 stsp
2805 bd8de430 2019-10-04 stsp if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
2806 bd8de430 2019-10-04 stsp path[0] ? "/" : "", ignores_filename) == -1)
2807 6841da00 2019-08-08 stsp return got_error_from_errno("asprintf");
2808 6841da00 2019-08-08 stsp
2809 886cec17 2019-12-15 stsp if (dirfd != -1) {
2810 886cec17 2019-12-15 stsp fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
2811 886cec17 2019-12-15 stsp if (fd == -1) {
2812 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2813 886cec17 2019-12-15 stsp err = got_error_from_errno2("openat",
2814 886cec17 2019-12-15 stsp ignorespath);
2815 886cec17 2019-12-15 stsp } else {
2816 886cec17 2019-12-15 stsp ignoresfile = fdopen(fd, "r");
2817 886cec17 2019-12-15 stsp if (ignoresfile == NULL)
2818 886cec17 2019-12-15 stsp err = got_error_from_errno2("fdopen",
2819 886cec17 2019-12-15 stsp ignorespath);
2820 886cec17 2019-12-15 stsp else {
2821 886cec17 2019-12-15 stsp fd = -1;
2822 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
2823 886cec17 2019-12-15 stsp }
2824 886cec17 2019-12-15 stsp }
2825 886cec17 2019-12-15 stsp } else {
2826 886cec17 2019-12-15 stsp ignoresfile = fopen(ignorespath, "r");
2827 886cec17 2019-12-15 stsp if (ignoresfile == NULL) {
2828 886cec17 2019-12-15 stsp if (errno != ENOENT && errno != EACCES)
2829 886cec17 2019-12-15 stsp err = got_error_from_errno2("fopen",
2830 886cec17 2019-12-15 stsp ignorespath);
2831 886cec17 2019-12-15 stsp } else
2832 886cec17 2019-12-15 stsp err = read_ignores(ignores, path, ignoresfile);
2833 886cec17 2019-12-15 stsp }
2834 6841da00 2019-08-08 stsp
2835 6841da00 2019-08-08 stsp if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
2836 4e68cba3 2019-11-23 stsp err = got_error_from_errno2("fclose", path);
2837 886cec17 2019-12-15 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2838 886cec17 2019-12-15 stsp err = got_error_from_errno2("close", path);
2839 6841da00 2019-08-08 stsp free(ignorespath);
2840 6841da00 2019-08-08 stsp return err;
2841 f8d1f275 2019-02-04 stsp }
2842 f8d1f275 2019-02-04 stsp
2843 f8d1f275 2019-02-04 stsp static const struct got_error *
2844 7f91a133 2019-12-13 stsp status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
2845 f8d1f275 2019-02-04 stsp {
2846 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2847 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2848 f8d1f275 2019-02-04 stsp char *path = NULL;
2849 f8d1f275 2019-02-04 stsp
2850 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2851 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2852 0584f854 2019-04-06 stsp
2853 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2854 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2855 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2856 f8d1f275 2019-02-04 stsp } else {
2857 f8d1f275 2019-02-04 stsp path = de->d_name;
2858 f8d1f275 2019-02-04 stsp }
2859 f8d1f275 2019-02-04 stsp
2860 3143d852 2020-06-25 stsp if (de->d_type != DT_DIR &&
2861 3143d852 2020-06-25 stsp got_path_is_child(path, a->status_path, a->status_path_len)
2862 6841da00 2019-08-08 stsp && !match_ignores(&a->ignores, path))
2863 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2864 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2865 f8d1f275 2019-02-04 stsp if (parent_path[0])
2866 f8d1f275 2019-02-04 stsp free(path);
2867 b72f483a 2019-02-05 stsp return err;
2868 f8d1f275 2019-02-04 stsp }
2869 f8d1f275 2019-02-04 stsp
2870 347d1d3e 2019-07-12 stsp static const struct got_error *
2871 3143d852 2020-06-25 stsp status_traverse(void *arg, const char *path, int dirfd)
2872 3143d852 2020-06-25 stsp {
2873 3143d852 2020-06-25 stsp const struct got_error *err = NULL;
2874 3143d852 2020-06-25 stsp struct diff_dir_cb_arg *a = arg;
2875 3143d852 2020-06-25 stsp
2876 3143d852 2020-06-25 stsp if (a->no_ignores)
2877 3143d852 2020-06-25 stsp return NULL;
2878 3143d852 2020-06-25 stsp
2879 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path,
2880 3143d852 2020-06-25 stsp path, dirfd, ".cvsignore");
2881 3143d852 2020-06-25 stsp if (err)
2882 3143d852 2020-06-25 stsp return err;
2883 3143d852 2020-06-25 stsp
2884 3143d852 2020-06-25 stsp err = add_ignores(&a->ignores, a->worktree->root_path, path,
2885 3143d852 2020-06-25 stsp dirfd, ".gitignore");
2886 3143d852 2020-06-25 stsp
2887 3143d852 2020-06-25 stsp return err;
2888 3143d852 2020-06-25 stsp }
2889 3143d852 2020-06-25 stsp
2890 3143d852 2020-06-25 stsp static const struct got_error *
2891 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2892 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2893 f2a9dc41 2019-12-13 tracey void *status_arg, struct got_repository *repo, int report_unchanged)
2894 abb4604f 2019-07-27 stsp {
2895 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2896 abb4604f 2019-07-27 stsp struct stat sb;
2897 abb4604f 2019-07-27 stsp
2898 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2899 abb4604f 2019-07-27 stsp if (ie)
2900 7f91a133 2019-12-13 stsp return report_file_status(ie, ondisk_path, -1, NULL,
2901 7f91a133 2019-12-13 stsp status_cb, status_arg, repo, report_unchanged);
2902 abb4604f 2019-07-27 stsp
2903 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2904 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2905 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2906 2a06fe5f 2019-08-24 stsp return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
2907 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2908 abb4604f 2019-07-27 stsp return NULL;
2909 abb4604f 2019-07-27 stsp }
2910 abb4604f 2019-07-27 stsp
2911 00bb5ea0 2020-07-23 stsp if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
2912 88d0e355 2019-08-03 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2913 12463d8b 2019-12-13 stsp GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
2914 abb4604f 2019-07-27 stsp
2915 abb4604f 2019-07-27 stsp return NULL;
2916 3143d852 2020-06-25 stsp }
2917 3143d852 2020-06-25 stsp
2918 3143d852 2020-06-25 stsp static const struct got_error *
2919 3143d852 2020-06-25 stsp add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
2920 3143d852 2020-06-25 stsp const char *root_path, const char *path)
2921 3143d852 2020-06-25 stsp {
2922 3143d852 2020-06-25 stsp const struct got_error *err;
2923 b737c85a 2020-06-26 stsp char *parent_path, *next_parent_path = NULL;
2924 3143d852 2020-06-25 stsp
2925 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
2926 3143d852 2020-06-25 stsp ".cvsignore");
2927 3143d852 2020-06-25 stsp if (err)
2928 3143d852 2020-06-25 stsp return err;
2929 3143d852 2020-06-25 stsp
2930 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, "", -1,
2931 3143d852 2020-06-25 stsp ".gitignore");
2932 3143d852 2020-06-25 stsp if (err)
2933 3143d852 2020-06-25 stsp return err;
2934 3143d852 2020-06-25 stsp
2935 3143d852 2020-06-25 stsp err = got_path_dirname(&parent_path, path);
2936 3143d852 2020-06-25 stsp if (err) {
2937 3143d852 2020-06-25 stsp if (err->code == GOT_ERR_BAD_PATH)
2938 3143d852 2020-06-25 stsp return NULL; /* cannot traverse parent */
2939 3143d852 2020-06-25 stsp return err;
2940 3143d852 2020-06-25 stsp }
2941 3143d852 2020-06-25 stsp for (;;) {
2942 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
2943 3143d852 2020-06-25 stsp ".cvsignore");
2944 3143d852 2020-06-25 stsp if (err)
2945 3143d852 2020-06-25 stsp break;
2946 3143d852 2020-06-25 stsp err = add_ignores(ignores, root_path, parent_path, -1,
2947 3143d852 2020-06-25 stsp ".gitignore");
2948 3143d852 2020-06-25 stsp if (err)
2949 3143d852 2020-06-25 stsp break;
2950 3143d852 2020-06-25 stsp err = got_path_dirname(&next_parent_path, parent_path);
2951 3143d852 2020-06-25 stsp if (err) {
2952 b737c85a 2020-06-26 stsp if (err->code == GOT_ERR_BAD_PATH)
2953 b737c85a 2020-06-26 stsp err = NULL; /* traversed everything */
2954 3143d852 2020-06-25 stsp break;
2955 3143d852 2020-06-25 stsp }
2956 b737c85a 2020-06-26 stsp free(parent_path);
2957 b737c85a 2020-06-26 stsp parent_path = next_parent_path;
2958 b737c85a 2020-06-26 stsp next_parent_path = NULL;
2959 3143d852 2020-06-25 stsp }
2960 3143d852 2020-06-25 stsp
2961 b737c85a 2020-06-26 stsp free(parent_path);
2962 b737c85a 2020-06-26 stsp free(next_parent_path);
2963 3143d852 2020-06-25 stsp return err;
2964 abb4604f 2019-07-27 stsp }
2965 abb4604f 2019-07-27 stsp
2966 abb4604f 2019-07-27 stsp static const struct got_error *
2967 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2968 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2969 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2970 f2a9dc41 2019-12-13 tracey got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
2971 f2a9dc41 2019-12-13 tracey int report_unchanged)
2972 f8d1f275 2019-02-04 stsp {
2973 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2974 6fc93f37 2019-12-13 stsp int fd = -1;
2975 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2976 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2977 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2978 3143d852 2020-06-25 stsp
2979 3143d852 2020-06-25 stsp TAILQ_INIT(&arg.ignores);
2980 f8d1f275 2019-02-04 stsp
2981 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2982 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
2983 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
2984 8dc303cc 2019-07-27 stsp
2985 6fc93f37 2019-12-13 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
2986 6fc93f37 2019-12-13 stsp if (fd == -1) {
2987 00bb5ea0 2020-07-23 stsp if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
2988 00bb5ea0 2020-07-23 stsp errno != ELOOP)
2989 6fc93f37 2019-12-13 stsp err = got_error_from_errno2("open", ondisk_path);
2990 abb4604f 2019-07-27 stsp else
2991 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2992 f2a9dc41 2019-12-13 tracey fileindex, status_cb, status_arg, repo,
2993 f2a9dc41 2019-12-13 tracey report_unchanged);
2994 abb4604f 2019-07-27 stsp } else {
2995 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2996 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2997 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2998 3143d852 2020-06-25 stsp fdiff_cb.diff_traverse = status_traverse;
2999 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
3000 abb4604f 2019-07-27 stsp arg.worktree = worktree;
3001 abb4604f 2019-07-27 stsp arg.status_path = path;
3002 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
3003 abb4604f 2019-07-27 stsp arg.repo = repo;
3004 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
3005 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
3006 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
3007 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
3008 f2a9dc41 2019-12-13 tracey arg.report_unchanged = report_unchanged;
3009 3143d852 2020-06-25 stsp arg.no_ignores = no_ignores;
3010 022fae89 2019-12-06 tracey if (!no_ignores) {
3011 3143d852 2020-06-25 stsp err = add_ignores_from_parent_paths(&arg.ignores,
3012 3143d852 2020-06-25 stsp worktree->root_path, path);
3013 3143d852 2020-06-25 stsp if (err)
3014 3143d852 2020-06-25 stsp goto done;
3015 022fae89 2019-12-06 tracey }
3016 3143d852 2020-06-25 stsp err = got_fileindex_diff_dir(fileindex, fd,
3017 3143d852 2020-06-25 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
3018 927df6b7 2019-02-10 stsp }
3019 3143d852 2020-06-25 stsp done:
3020 3143d852 2020-06-25 stsp free_ignores(&arg.ignores);
3021 6fc93f37 2019-12-13 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
3022 6fc93f37 2019-12-13 stsp err = got_error_from_errno("close");
3023 927df6b7 2019-02-10 stsp free(ondisk_path);
3024 347d1d3e 2019-07-12 stsp return err;
3025 347d1d3e 2019-07-12 stsp }
3026 347d1d3e 2019-07-12 stsp
3027 347d1d3e 2019-07-12 stsp const struct got_error *
3028 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
3029 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
3030 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
3031 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
3032 347d1d3e 2019-07-12 stsp {
3033 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
3034 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
3035 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3036 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
3037 347d1d3e 2019-07-12 stsp
3038 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3039 347d1d3e 2019-07-12 stsp if (err)
3040 347d1d3e 2019-07-12 stsp return err;
3041 347d1d3e 2019-07-12 stsp
3042 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
3043 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3044 f2a9dc41 2019-12-13 tracey status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3045 72ea6654 2019-07-27 stsp if (err)
3046 72ea6654 2019-07-27 stsp break;
3047 72ea6654 2019-07-27 stsp }
3048 f8d1f275 2019-02-04 stsp free(fileindex_path);
3049 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
3050 f8d1f275 2019-02-04 stsp return err;
3051 f8d1f275 2019-02-04 stsp }
3052 6c7ab921 2019-03-18 stsp
3053 6c7ab921 2019-03-18 stsp const struct got_error *
3054 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3055 6c7ab921 2019-03-18 stsp const char *arg)
3056 6c7ab921 2019-03-18 stsp {
3057 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
3058 00bb5ea0 2020-07-23 stsp char *resolved = NULL, *cwd = NULL, *path = NULL;
3059 6c7ab921 2019-03-18 stsp size_t len;
3060 00bb5ea0 2020-07-23 stsp struct stat sb;
3061 6c7ab921 2019-03-18 stsp
3062 6c7ab921 2019-03-18 stsp *wt_path = NULL;
3063 6c7ab921 2019-03-18 stsp
3064 00bb5ea0 2020-07-23 stsp cwd = getcwd(NULL, 0);
3065 00bb5ea0 2020-07-23 stsp if (cwd == NULL)
3066 00bb5ea0 2020-07-23 stsp return got_error_from_errno("getcwd");
3067 00bb5ea0 2020-07-23 stsp
3068 00bb5ea0 2020-07-23 stsp if (lstat(arg, &sb) == -1) {
3069 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3070 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("lstat", arg);
3071 00bb5ea0 2020-07-23 stsp goto done;
3072 00bb5ea0 2020-07-23 stsp }
3073 00bb5ea0 2020-07-23 stsp }
3074 00bb5ea0 2020-07-23 stsp if (S_ISLNK(sb.st_mode)) {
3075 00bb5ea0 2020-07-23 stsp /*
3076 00bb5ea0 2020-07-23 stsp * We cannot use realpath(3) with symlinks since we want to
3077 00bb5ea0 2020-07-23 stsp * operate on the symlink itself.
3078 00bb5ea0 2020-07-23 stsp * But we can make the path absolute, assuming it is relative
3079 00bb5ea0 2020-07-23 stsp * to the current working directory, and then canonicalize it.
3080 00bb5ea0 2020-07-23 stsp */
3081 00bb5ea0 2020-07-23 stsp char *abspath = NULL;
3082 00bb5ea0 2020-07-23 stsp char canonpath[PATH_MAX];
3083 00bb5ea0 2020-07-23 stsp if (!got_path_is_absolute(arg)) {
3084 00bb5ea0 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3085 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3086 00bb5ea0 2020-07-23 stsp goto done;
3087 00bb5ea0 2020-07-23 stsp }
3088 00bb5ea0 2020-07-23 stsp
3089 00bb5ea0 2020-07-23 stsp }
3090 00bb5ea0 2020-07-23 stsp err = got_canonpath(abspath ? abspath : arg, canonpath,
3091 00bb5ea0 2020-07-23 stsp sizeof(canonpath));
3092 00bb5ea0 2020-07-23 stsp if (err)
3093 d0710d08 2019-07-22 stsp goto done;
3094 00bb5ea0 2020-07-23 stsp resolved = strdup(canonpath);
3095 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3096 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("strdup");
3097 00bb5ea0 2020-07-23 stsp goto done;
3098 00bb5ea0 2020-07-23 stsp }
3099 00bb5ea0 2020-07-23 stsp } else {
3100 00bb5ea0 2020-07-23 stsp resolved = realpath(arg, NULL);
3101 00bb5ea0 2020-07-23 stsp if (resolved == NULL) {
3102 00bb5ea0 2020-07-23 stsp if (errno != ENOENT) {
3103 00bb5ea0 2020-07-23 stsp err = got_error_from_errno2("realpath", arg);
3104 00bb5ea0 2020-07-23 stsp goto done;
3105 00bb5ea0 2020-07-23 stsp }
3106 00bb5ea0 2020-07-23 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3107 00bb5ea0 2020-07-23 stsp err = got_error_from_errno("asprintf");
3108 00bb5ea0 2020-07-23 stsp goto done;
3109 00bb5ea0 2020-07-23 stsp }
3110 d0710d08 2019-07-22 stsp }
3111 d0710d08 2019-07-22 stsp }
3112 6c7ab921 2019-03-18 stsp
3113 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
3114 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
3115 63f810e6 2020-02-29 stsp err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3116 6c7ab921 2019-03-18 stsp goto done;
3117 6c7ab921 2019-03-18 stsp }
3118 6c7ab921 2019-03-18 stsp
3119 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3120 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
3121 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
3122 f6d88e1a 2019-05-29 stsp if (err)
3123 f6d88e1a 2019-05-29 stsp goto done;
3124 f6d88e1a 2019-05-29 stsp } else {
3125 f6d88e1a 2019-05-29 stsp path = strdup("");
3126 f6d88e1a 2019-05-29 stsp if (path == NULL) {
3127 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
3128 f6d88e1a 2019-05-29 stsp goto done;
3129 f6d88e1a 2019-05-29 stsp }
3130 6c7ab921 2019-03-18 stsp }
3131 6c7ab921 2019-03-18 stsp
3132 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
3133 6c7ab921 2019-03-18 stsp len = strlen(path);
3134 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
3135 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
3136 6c7ab921 2019-03-18 stsp len--;
3137 6c7ab921 2019-03-18 stsp }
3138 6c7ab921 2019-03-18 stsp done:
3139 6c7ab921 2019-03-18 stsp free(resolved);
3140 d0710d08 2019-07-22 stsp free(cwd);
3141 6c7ab921 2019-03-18 stsp if (err == NULL)
3142 6c7ab921 2019-03-18 stsp *wt_path = path;
3143 6c7ab921 2019-03-18 stsp else
3144 6c7ab921 2019-03-18 stsp free(path);
3145 d00136be 2019-03-26 stsp return err;
3146 d00136be 2019-03-26 stsp }
3147 d00136be 2019-03-26 stsp
3148 4e68cba3 2019-11-23 stsp struct schedule_addition_args {
3149 4e68cba3 2019-11-23 stsp struct got_worktree *worktree;
3150 4e68cba3 2019-11-23 stsp struct got_fileindex *fileindex;
3151 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb;
3152 4e68cba3 2019-11-23 stsp void *progress_arg;
3153 4e68cba3 2019-11-23 stsp struct got_repository *repo;
3154 4e68cba3 2019-11-23 stsp };
3155 4e68cba3 2019-11-23 stsp
3156 4e68cba3 2019-11-23 stsp static const struct got_error *
3157 4e68cba3 2019-11-23 stsp schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3158 4e68cba3 2019-11-23 stsp const char *relpath, struct got_object_id *blob_id,
3159 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3160 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3161 07f5b47a 2019-06-02 stsp {
3162 4e68cba3 2019-11-23 stsp struct schedule_addition_args *a = arg;
3163 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
3164 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
3165 6d022e97 2019-08-04 stsp struct stat sb;
3166 dbb83fbd 2019-12-12 stsp char *ondisk_path;
3167 07f5b47a 2019-06-02 stsp
3168 dbb83fbd 2019-12-12 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3169 dbb83fbd 2019-12-12 stsp relpath) == -1)
3170 dbb83fbd 2019-12-12 stsp return got_error_from_errno("asprintf");
3171 dbb83fbd 2019-12-12 stsp
3172 4e68cba3 2019-11-23 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3173 6d022e97 2019-08-04 stsp if (ie) {
3174 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3175 12463d8b 2019-12-13 stsp de_name, a->repo);
3176 6d022e97 2019-08-04 stsp if (err)
3177 dbb83fbd 2019-12-12 stsp goto done;
3178 6d022e97 2019-08-04 stsp /* Re-adding an existing entry is a no-op. */
3179 6d022e97 2019-08-04 stsp if (status == GOT_STATUS_ADD)
3180 dbb83fbd 2019-12-12 stsp goto done;
3181 dbb83fbd 2019-12-12 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3182 dbb83fbd 2019-12-12 stsp if (err)
3183 dbb83fbd 2019-12-12 stsp goto done;
3184 6d022e97 2019-08-04 stsp }
3185 07f5b47a 2019-06-02 stsp
3186 dbb83fbd 2019-12-12 stsp if (status != GOT_STATUS_UNVERSIONED) {
3187 dbb83fbd 2019-12-12 stsp err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3188 dbb83fbd 2019-12-12 stsp goto done;
3189 4e68cba3 2019-11-23 stsp }
3190 07f5b47a 2019-06-02 stsp
3191 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, relpath);
3192 4e68cba3 2019-11-23 stsp if (err)
3193 4e68cba3 2019-11-23 stsp goto done;
3194 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3195 3969253a 2020-03-07 stsp if (err) {
3196 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
3197 3969253a 2020-03-07 stsp goto done;
3198 3969253a 2020-03-07 stsp }
3199 4e68cba3 2019-11-23 stsp err = got_fileindex_entry_add(a->fileindex, ie);
3200 07f5b47a 2019-06-02 stsp if (err) {
3201 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
3202 4e68cba3 2019-11-23 stsp goto done;
3203 07f5b47a 2019-06-02 stsp }
3204 4e68cba3 2019-11-23 stsp done:
3205 dbb83fbd 2019-12-12 stsp free(ondisk_path);
3206 dbb83fbd 2019-12-12 stsp if (err)
3207 dbb83fbd 2019-12-12 stsp return err;
3208 dbb83fbd 2019-12-12 stsp if (status == GOT_STATUS_ADD)
3209 dbb83fbd 2019-12-12 stsp return NULL;
3210 dbb83fbd 2019-12-12 stsp return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3211 07f5b47a 2019-06-02 stsp }
3212 07f5b47a 2019-06-02 stsp
3213 d00136be 2019-03-26 stsp const struct got_error *
3214 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
3215 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths,
3216 4e68cba3 2019-11-23 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3217 022fae89 2019-12-06 tracey struct got_repository *repo, int no_ignores)
3218 d00136be 2019-03-26 stsp {
3219 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3220 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3221 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3222 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3223 4e68cba3 2019-11-23 stsp struct schedule_addition_args saa;
3224 d00136be 2019-03-26 stsp
3225 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3226 d00136be 2019-03-26 stsp if (err)
3227 d00136be 2019-03-26 stsp return err;
3228 d00136be 2019-03-26 stsp
3229 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3230 d00136be 2019-03-26 stsp if (err)
3231 d00136be 2019-03-26 stsp goto done;
3232 d00136be 2019-03-26 stsp
3233 4e68cba3 2019-11-23 stsp saa.worktree = worktree;
3234 4e68cba3 2019-11-23 stsp saa.fileindex = fileindex;
3235 4e68cba3 2019-11-23 stsp saa.progress_cb = progress_cb;
3236 4e68cba3 2019-11-23 stsp saa.progress_arg = progress_arg;
3237 4e68cba3 2019-11-23 stsp saa.repo = repo;
3238 4e68cba3 2019-11-23 stsp
3239 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3240 4e68cba3 2019-11-23 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3241 f2a9dc41 2019-12-13 tracey schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3242 1dd54920 2019-05-11 stsp if (err)
3243 af12c6b9 2019-06-04 stsp break;
3244 1dd54920 2019-05-11 stsp }
3245 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3246 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3247 af12c6b9 2019-06-04 stsp err = sync_err;
3248 d00136be 2019-03-26 stsp done:
3249 fb399478 2019-07-12 stsp free(fileindex_path);
3250 d00136be 2019-03-26 stsp if (fileindex)
3251 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
3252 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3253 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
3254 d00136be 2019-03-26 stsp err = unlockerr;
3255 6c7ab921 2019-03-18 stsp return err;
3256 6c7ab921 2019-03-18 stsp }
3257 17ed4618 2019-06-02 stsp
3258 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args {
3259 f2a9dc41 2019-12-13 tracey struct got_worktree *worktree;
3260 f2a9dc41 2019-12-13 tracey struct got_fileindex *fileindex;
3261 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb;
3262 f2a9dc41 2019-12-13 tracey void *progress_arg;
3263 f2a9dc41 2019-12-13 tracey struct got_repository *repo;
3264 f2a9dc41 2019-12-13 tracey int delete_local_mods;
3265 70e3e7f5 2019-12-13 tracey int keep_on_disk;
3266 f2a9dc41 2019-12-13 tracey };
3267 f2a9dc41 2019-12-13 tracey
3268 f2a9dc41 2019-12-13 tracey static const struct got_error *
3269 f2a9dc41 2019-12-13 tracey schedule_for_deletion(void *arg, unsigned char status,
3270 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *relpath,
3271 f2a9dc41 2019-12-13 tracey struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3272 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
3273 17ed4618 2019-06-02 stsp {
3274 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args *a = arg;
3275 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
3276 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
3277 17ed4618 2019-06-02 stsp struct stat sb;
3278 15341bfd 2020-03-05 tracey char *ondisk_path, *parent = NULL;
3279 17ed4618 2019-06-02 stsp
3280 f2a9dc41 2019-12-13 tracey ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3281 17ed4618 2019-06-02 stsp if (ie == NULL)
3282 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3283 2ec1f75b 2019-03-26 stsp
3284 9acbc4fa 2019-08-03 stsp staged_status = get_staged_status(ie);
3285 9acbc4fa 2019-08-03 stsp if (staged_status != GOT_STATUS_NO_CHANGE) {
3286 9acbc4fa 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3287 9acbc4fa 2019-08-03 stsp return NULL;
3288 9acbc4fa 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3289 9acbc4fa 2019-08-03 stsp }
3290 9acbc4fa 2019-08-03 stsp
3291 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3292 f2a9dc41 2019-12-13 tracey relpath) == -1)
3293 f2a9dc41 2019-12-13 tracey return got_error_from_errno("asprintf");
3294 f2a9dc41 2019-12-13 tracey
3295 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3296 12463d8b 2019-12-13 stsp a->repo);
3297 17ed4618 2019-06-02 stsp if (err)
3298 f2a9dc41 2019-12-13 tracey goto done;
3299 17ed4618 2019-06-02 stsp
3300 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
3301 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
3302 f2a9dc41 2019-12-13 tracey goto done;
3303 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3304 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3305 f2a9dc41 2019-12-13 tracey goto done;
3306 f2a9dc41 2019-12-13 tracey }
3307 f2a9dc41 2019-12-13 tracey if (status != GOT_STATUS_MODIFY &&
3308 f2a9dc41 2019-12-13 tracey status != GOT_STATUS_MISSING) {
3309 f2a9dc41 2019-12-13 tracey err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3310 f2a9dc41 2019-12-13 tracey goto done;
3311 f2a9dc41 2019-12-13 tracey }
3312 17ed4618 2019-06-02 stsp }
3313 17ed4618 2019-06-02 stsp
3314 70e3e7f5 2019-12-13 tracey if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3315 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3316 12463d8b 2019-12-13 stsp if (unlinkat(dirfd, de_name, 0) != 0) {
3317 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlinkat",
3318 12463d8b 2019-12-13 stsp ondisk_path);
3319 12463d8b 2019-12-13 stsp goto done;
3320 12463d8b 2019-12-13 stsp }
3321 12463d8b 2019-12-13 stsp } else if (unlink(ondisk_path) != 0) {
3322 12463d8b 2019-12-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
3323 12463d8b 2019-12-13 stsp goto done;
3324 15341bfd 2020-03-05 tracey }
3325 15341bfd 2020-03-05 tracey
3326 15341bfd 2020-03-05 tracey parent = dirname(ondisk_path);
3327 15341bfd 2020-03-05 tracey
3328 15341bfd 2020-03-05 tracey if (parent == NULL) {
3329 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", ondisk_path);
3330 15341bfd 2020-03-05 tracey goto done;
3331 15341bfd 2020-03-05 tracey }
3332 15341bfd 2020-03-05 tracey while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3333 15341bfd 2020-03-05 tracey if (rmdir(parent) == -1) {
3334 15341bfd 2020-03-05 tracey if (errno != ENOTEMPTY)
3335 15341bfd 2020-03-05 tracey err = got_error_from_errno2("rmdir",
3336 15341bfd 2020-03-05 tracey parent);
3337 15341bfd 2020-03-05 tracey break;
3338 15341bfd 2020-03-05 tracey }
3339 15341bfd 2020-03-05 tracey parent = dirname(parent);
3340 15341bfd 2020-03-05 tracey if (parent == NULL) {
3341 15341bfd 2020-03-05 tracey err = got_error_from_errno2("dirname", parent);
3342 15341bfd 2020-03-05 tracey goto done;
3343 15341bfd 2020-03-05 tracey }
3344 12463d8b 2019-12-13 stsp }
3345 f2a9dc41 2019-12-13 tracey }
3346 17ed4618 2019-06-02 stsp
3347 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
3348 f2a9dc41 2019-12-13 tracey done:
3349 f2a9dc41 2019-12-13 tracey free(ondisk_path);
3350 f2a9dc41 2019-12-13 tracey if (err)
3351 f2a9dc41 2019-12-13 tracey return err;
3352 f2a9dc41 2019-12-13 tracey if (status == GOT_STATUS_DELETE)
3353 f2a9dc41 2019-12-13 tracey return NULL;
3354 f2a9dc41 2019-12-13 tracey return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3355 f2a9dc41 2019-12-13 tracey staged_status, relpath);
3356 17ed4618 2019-06-02 stsp }
3357 17ed4618 2019-06-02 stsp
3358 2ec1f75b 2019-03-26 stsp const struct got_error *
3359 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
3360 6d022e97 2019-08-04 stsp struct got_pathlist_head *paths, int delete_local_mods,
3361 f2a9dc41 2019-12-13 tracey got_worktree_delete_cb progress_cb, void *progress_arg,
3362 70e3e7f5 2019-12-13 tracey struct got_repository *repo, int keep_on_disk)
3363 2ec1f75b 2019-03-26 stsp {
3364 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
3365 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
3366 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
3367 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3368 f2a9dc41 2019-12-13 tracey struct schedule_deletion_args sda;
3369 2ec1f75b 2019-03-26 stsp
3370 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
3371 2ec1f75b 2019-03-26 stsp if (err)
3372 2ec1f75b 2019-03-26 stsp return err;
3373 2ec1f75b 2019-03-26 stsp
3374 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3375 2ec1f75b 2019-03-26 stsp if (err)
3376 2ec1f75b 2019-03-26 stsp goto done;
3377 f2a9dc41 2019-12-13 tracey
3378 f2a9dc41 2019-12-13 tracey sda.worktree = worktree;
3379 f2a9dc41 2019-12-13 tracey sda.fileindex = fileindex;
3380 f2a9dc41 2019-12-13 tracey sda.progress_cb = progress_cb;
3381 f2a9dc41 2019-12-13 tracey sda.progress_arg = progress_arg;
3382 f2a9dc41 2019-12-13 tracey sda.repo = repo;
3383 f2a9dc41 2019-12-13 tracey sda.delete_local_mods = delete_local_mods;
3384 70e3e7f5 2019-12-13 tracey sda.keep_on_disk = keep_on_disk;
3385 2ec1f75b 2019-03-26 stsp
3386 6d022e97 2019-08-04 stsp TAILQ_FOREACH(pe, paths, entry) {
3387 f2a9dc41 2019-12-13 tracey err = worktree_status(worktree, pe->path, fileindex, repo,
3388 f2a9dc41 2019-12-13 tracey schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3389 17ed4618 2019-06-02 stsp if (err)
3390 af12c6b9 2019-06-04 stsp break;
3391 2ec1f75b 2019-03-26 stsp }
3392 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3393 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3394 af12c6b9 2019-06-04 stsp err = sync_err;
3395 2ec1f75b 2019-03-26 stsp done:
3396 fb399478 2019-07-12 stsp free(fileindex_path);
3397 2ec1f75b 2019-03-26 stsp if (fileindex)
3398 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
3399 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3400 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
3401 2ec1f75b 2019-03-26 stsp err = unlockerr;
3402 33aa809d 2019-08-08 stsp return err;
3403 33aa809d 2019-08-08 stsp }
3404 33aa809d 2019-08-08 stsp
3405 33aa809d 2019-08-08 stsp static const struct got_error *
3406 33aa809d 2019-08-08 stsp copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3407 33aa809d 2019-08-08 stsp {
3408 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3409 33aa809d 2019-08-08 stsp char *line = NULL;
3410 33aa809d 2019-08-08 stsp size_t linesize = 0, n;
3411 33aa809d 2019-08-08 stsp ssize_t linelen;
3412 33aa809d 2019-08-08 stsp
3413 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, infile);
3414 33aa809d 2019-08-08 stsp if (linelen == -1) {
3415 33aa809d 2019-08-08 stsp if (ferror(infile)) {
3416 33aa809d 2019-08-08 stsp err = got_error_from_errno("getline");
3417 33aa809d 2019-08-08 stsp goto done;
3418 33aa809d 2019-08-08 stsp }
3419 33aa809d 2019-08-08 stsp return NULL;
3420 33aa809d 2019-08-08 stsp }
3421 33aa809d 2019-08-08 stsp if (outfile) {
3422 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, outfile);
3423 33aa809d 2019-08-08 stsp if (n != linelen) {
3424 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3425 33aa809d 2019-08-08 stsp goto done;
3426 33aa809d 2019-08-08 stsp }
3427 33aa809d 2019-08-08 stsp }
3428 33aa809d 2019-08-08 stsp if (rejectfile) {
3429 33aa809d 2019-08-08 stsp n = fwrite(line, 1, linelen, rejectfile);
3430 33aa809d 2019-08-08 stsp if (n != linelen)
3431 33aa809d 2019-08-08 stsp err = got_ferror(outfile, GOT_ERR_IO);
3432 33aa809d 2019-08-08 stsp }
3433 33aa809d 2019-08-08 stsp done:
3434 33aa809d 2019-08-08 stsp free(line);
3435 2ec1f75b 2019-03-26 stsp return err;
3436 2ec1f75b 2019-03-26 stsp }
3437 1f1abb7e 2019-08-08 stsp
3438 33aa809d 2019-08-08 stsp static const struct got_error *
3439 33aa809d 2019-08-08 stsp skip_one_line(FILE *f)
3440 33aa809d 2019-08-08 stsp {
3441 33aa809d 2019-08-08 stsp char *line = NULL;
3442 33aa809d 2019-08-08 stsp size_t linesize = 0;
3443 33aa809d 2019-08-08 stsp ssize_t linelen;
3444 33aa809d 2019-08-08 stsp
3445 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, f);
3446 500467ff 2019-09-25 hiltjo if (linelen == -1) {
3447 500467ff 2019-09-25 hiltjo if (ferror(f))
3448 500467ff 2019-09-25 hiltjo return got_error_from_errno("getline");
3449 500467ff 2019-09-25 hiltjo return NULL;
3450 500467ff 2019-09-25 hiltjo }
3451 33aa809d 2019-08-08 stsp free(line);
3452 33aa809d 2019-08-08 stsp return NULL;
3453 33aa809d 2019-08-08 stsp }
3454 33aa809d 2019-08-08 stsp
3455 33aa809d 2019-08-08 stsp static const struct got_error *
3456 33aa809d 2019-08-08 stsp copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3457 33aa809d 2019-08-08 stsp int start_old, int end_old, int start_new, int end_new,
3458 33aa809d 2019-08-08 stsp FILE *outfile, FILE *rejectfile)
3459 33aa809d 2019-08-08 stsp {
3460 33aa809d 2019-08-08 stsp const struct got_error *err;
3461 33aa809d 2019-08-08 stsp
3462 33aa809d 2019-08-08 stsp /* Copy old file's lines leading up to patch. */
3463 33aa809d 2019-08-08 stsp while (!feof(f1) && *line_cur1 < start_old) {
3464 33aa809d 2019-08-08 stsp err = copy_one_line(f1, outfile, NULL);
3465 33aa809d 2019-08-08 stsp if (err)
3466 33aa809d 2019-08-08 stsp return err;
3467 33aa809d 2019-08-08 stsp (*line_cur1)++;
3468 33aa809d 2019-08-08 stsp }
3469 33aa809d 2019-08-08 stsp /* Skip new file's lines leading up to patch. */
3470 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 < start_new) {
3471 33aa809d 2019-08-08 stsp if (rejectfile)
3472 33aa809d 2019-08-08 stsp err = copy_one_line(f2, NULL, rejectfile);
3473 33aa809d 2019-08-08 stsp else
3474 33aa809d 2019-08-08 stsp err = skip_one_line(f2);
3475 33aa809d 2019-08-08 stsp if (err)
3476 33aa809d 2019-08-08 stsp return err;
3477 33aa809d 2019-08-08 stsp (*line_cur2)++;
3478 33aa809d 2019-08-08 stsp }
3479 33aa809d 2019-08-08 stsp /* Copy patched lines. */
3480 33aa809d 2019-08-08 stsp while (!feof(f2) && *line_cur2 <= end_new) {
3481 33aa809d 2019-08-08 stsp err = copy_one_line(f2, outfile, NULL);
3482 33aa809d 2019-08-08 stsp if (err)
3483 33aa809d 2019-08-08 stsp return err;
3484 33aa809d 2019-08-08 stsp (*line_cur2)++;
3485 33aa809d 2019-08-08 stsp }
3486 33aa809d 2019-08-08 stsp /* Skip over old file's replaced lines. */
3487 f1e81a05 2019-08-10 stsp while (!feof(f1) && *line_cur1 <= end_old) {
3488 33aa809d 2019-08-08 stsp if (rejectfile)
3489 33aa809d 2019-08-08 stsp err = copy_one_line(f1, NULL, rejectfile);
3490 33aa809d 2019-08-08 stsp else
3491 33aa809d 2019-08-08 stsp err = skip_one_line(f1);
3492 33aa809d 2019-08-08 stsp if (err)
3493 33aa809d 2019-08-08 stsp return err;
3494 33aa809d 2019-08-08 stsp (*line_cur1)++;
3495 33aa809d 2019-08-08 stsp }
3496 f1e81a05 2019-08-10 stsp
3497 f1e81a05 2019-08-10 stsp return NULL;
3498 f1e81a05 2019-08-10 stsp }
3499 f1e81a05 2019-08-10 stsp
3500 f1e81a05 2019-08-10 stsp static const struct got_error *
3501 f1e81a05 2019-08-10 stsp copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3502 f1e81a05 2019-08-10 stsp FILE *outfile, FILE *rejectfile)
3503 f1e81a05 2019-08-10 stsp {
3504 f1e81a05 2019-08-10 stsp const struct got_error *err;
3505 f1e81a05 2019-08-10 stsp
3506 f1e81a05 2019-08-10 stsp if (outfile) {
3507 f1e81a05 2019-08-10 stsp /* Copy old file's lines until EOF. */
3508 f1e81a05 2019-08-10 stsp while (!feof(f1)) {
3509 f1e81a05 2019-08-10 stsp err = copy_one_line(f1, outfile, NULL);
3510 f1e81a05 2019-08-10 stsp if (err)
3511 f1e81a05 2019-08-10 stsp return err;
3512 f1e81a05 2019-08-10 stsp (*line_cur1)++;
3513 f1e81a05 2019-08-10 stsp }
3514 f1e81a05 2019-08-10 stsp }
3515 f1e81a05 2019-08-10 stsp if (rejectfile) {
3516 f1e81a05 2019-08-10 stsp /* Copy new file's lines until EOF. */
3517 f1e81a05 2019-08-10 stsp while (!feof(f2)) {
3518 f1e81a05 2019-08-10 stsp err = copy_one_line(f2, NULL, rejectfile);
3519 f1e81a05 2019-08-10 stsp if (err)
3520 f1e81a05 2019-08-10 stsp return err;
3521 f1e81a05 2019-08-10 stsp (*line_cur2)++;
3522 f1e81a05 2019-08-10 stsp }
3523 33aa809d 2019-08-08 stsp }
3524 33aa809d 2019-08-08 stsp
3525 33aa809d 2019-08-08 stsp return NULL;
3526 33aa809d 2019-08-08 stsp }
3527 33aa809d 2019-08-08 stsp
3528 33aa809d 2019-08-08 stsp static const struct got_error *
3529 33aa809d 2019-08-08 stsp apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3530 33aa809d 2019-08-08 stsp int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3531 33aa809d 2019-08-08 stsp int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3532 33aa809d 2019-08-08 stsp int *line_cur2, FILE *outfile, FILE *rejectfile,
3533 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3534 33aa809d 2019-08-08 stsp {
3535 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3536 33aa809d 2019-08-08 stsp int start_old = change->cv.a;
3537 33aa809d 2019-08-08 stsp int end_old = change->cv.b;
3538 33aa809d 2019-08-08 stsp int start_new = change->cv.c;
3539 33aa809d 2019-08-08 stsp int end_new = change->cv.d;
3540 33aa809d 2019-08-08 stsp long pos1, pos2;
3541 33aa809d 2019-08-08 stsp FILE *hunkfile;
3542 33aa809d 2019-08-08 stsp
3543 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3544 33aa809d 2019-08-08 stsp
3545 33aa809d 2019-08-08 stsp hunkfile = got_opentemp();
3546 33aa809d 2019-08-08 stsp if (hunkfile == NULL)
3547 33aa809d 2019-08-08 stsp return got_error_from_errno("got_opentemp");
3548 33aa809d 2019-08-08 stsp
3549 33aa809d 2019-08-08 stsp pos1 = ftell(f1);
3550 33aa809d 2019-08-08 stsp pos2 = ftell(f2);
3551 33aa809d 2019-08-08 stsp
3552 33aa809d 2019-08-08 stsp /* XXX TODO needs error checking */
3553 33aa809d 2019-08-08 stsp got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3554 33aa809d 2019-08-08 stsp
3555 33aa809d 2019-08-08 stsp if (fseek(f1, pos1, SEEK_SET) == -1) {
3556 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3557 33aa809d 2019-08-08 stsp goto done;
3558 33aa809d 2019-08-08 stsp }
3559 33aa809d 2019-08-08 stsp if (fseek(f2, pos2, SEEK_SET) == -1) {
3560 33aa809d 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
3561 33aa809d 2019-08-08 stsp goto done;
3562 33aa809d 2019-08-08 stsp }
3563 33aa809d 2019-08-08 stsp if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3564 33aa809d 2019-08-08 stsp err = got_ferror(hunkfile, GOT_ERR_IO);
3565 33aa809d 2019-08-08 stsp goto done;
3566 33aa809d 2019-08-08 stsp }
3567 33aa809d 2019-08-08 stsp
3568 33aa809d 2019-08-08 stsp err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3569 33aa809d 2019-08-08 stsp hunkfile, n, nchanges);
3570 33aa809d 2019-08-08 stsp if (err)
3571 33aa809d 2019-08-08 stsp goto done;
3572 33aa809d 2019-08-08 stsp
3573 33aa809d 2019-08-08 stsp switch (*choice) {
3574 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_YES:
3575 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3576 33aa809d 2019-08-08 stsp end_old, start_new, end_new, outfile, rejectfile);
3577 33aa809d 2019-08-08 stsp break;
3578 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_NO:
3579 33aa809d 2019-08-08 stsp err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3580 33aa809d 2019-08-08 stsp end_old, start_new, end_new, rejectfile, outfile);
3581 33aa809d 2019-08-08 stsp break;
3582 33aa809d 2019-08-08 stsp case GOT_PATCH_CHOICE_QUIT:
3583 33aa809d 2019-08-08 stsp break;
3584 33aa809d 2019-08-08 stsp default:
3585 33aa809d 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
3586 33aa809d 2019-08-08 stsp break;
3587 33aa809d 2019-08-08 stsp }
3588 33aa809d 2019-08-08 stsp done:
3589 33aa809d 2019-08-08 stsp if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3590 33aa809d 2019-08-08 stsp err = got_error_from_errno("fclose");
3591 33aa809d 2019-08-08 stsp return err;
3592 33aa809d 2019-08-08 stsp }
3593 33aa809d 2019-08-08 stsp
3594 1f1abb7e 2019-08-08 stsp struct revert_file_args {
3595 1f1abb7e 2019-08-08 stsp struct got_worktree *worktree;
3596 1f1abb7e 2019-08-08 stsp struct got_fileindex *fileindex;
3597 1f1abb7e 2019-08-08 stsp got_worktree_checkout_cb progress_cb;
3598 1f1abb7e 2019-08-08 stsp void *progress_arg;
3599 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb;
3600 33aa809d 2019-08-08 stsp void *patch_arg;
3601 1f1abb7e 2019-08-08 stsp struct got_repository *repo;
3602 1f1abb7e 2019-08-08 stsp };
3603 a129376b 2019-03-28 stsp
3604 e20a8b6f 2019-06-04 stsp static const struct got_error *
3605 e635744c 2019-08-08 stsp create_patched_content(char **path_outfile, int reverse_patch,
3606 e635744c 2019-08-08 stsp struct got_object_id *blob_id, const char *path2,
3607 12463d8b 2019-12-13 stsp int dirfd2, const char *de_name2,
3608 e635744c 2019-08-08 stsp const char *relpath, struct got_repository *repo,
3609 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
3610 33aa809d 2019-08-08 stsp {
3611 33aa809d 2019-08-08 stsp const struct got_error *err;
3612 33aa809d 2019-08-08 stsp struct got_blob_object *blob = NULL;
3613 33aa809d 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3614 1ebedb77 2019-10-19 stsp int fd2 = -1;
3615 33aa809d 2019-08-08 stsp char *path1 = NULL, *id_str = NULL;
3616 33aa809d 2019-08-08 stsp struct stat sb1, sb2;
3617 33aa809d 2019-08-08 stsp struct got_diff_changes *changes = NULL;
3618 33aa809d 2019-08-08 stsp struct got_diff_state *ds = NULL;
3619 33aa809d 2019-08-08 stsp struct got_diff_args *args = NULL;
3620 33aa809d 2019-08-08 stsp struct got_diff_change *change;
3621 33aa809d 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3622 33aa809d 2019-08-08 stsp int n = 0;
3623 33aa809d 2019-08-08 stsp
3624 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3625 33aa809d 2019-08-08 stsp
3626 33aa809d 2019-08-08 stsp err = got_object_id_str(&id_str, blob_id);
3627 33aa809d 2019-08-08 stsp if (err)
3628 33aa809d 2019-08-08 stsp return err;
3629 33aa809d 2019-08-08 stsp
3630 12463d8b 2019-12-13 stsp if (dirfd2 != -1) {
3631 12463d8b 2019-12-13 stsp fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3632 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3633 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", path2);
3634 12463d8b 2019-12-13 stsp goto done;
3635 12463d8b 2019-12-13 stsp }
3636 12463d8b 2019-12-13 stsp } else {
3637 12463d8b 2019-12-13 stsp fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3638 12463d8b 2019-12-13 stsp if (fd2 == -1) {
3639 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", path2);
3640 12463d8b 2019-12-13 stsp goto done;
3641 12463d8b 2019-12-13 stsp }
3642 1ebedb77 2019-10-19 stsp }
3643 1ebedb77 2019-10-19 stsp if (fstat(fd2, &sb2) == -1) {
3644 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("fstat", path2);
3645 1ebedb77 2019-10-19 stsp goto done;
3646 1ebedb77 2019-10-19 stsp }
3647 1ebedb77 2019-10-19 stsp
3648 1ebedb77 2019-10-19 stsp f2 = fdopen(fd2, "r");
3649 33aa809d 2019-08-08 stsp if (f2 == NULL) {
3650 60522982 2019-12-15 stsp err = got_error_from_errno2("fdopen", path2);
3651 33aa809d 2019-08-08 stsp goto done;
3652 33aa809d 2019-08-08 stsp }
3653 1ebedb77 2019-10-19 stsp fd2 = -1;
3654 33aa809d 2019-08-08 stsp
3655 33aa809d 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3656 33aa809d 2019-08-08 stsp if (err)
3657 33aa809d 2019-08-08 stsp goto done;
3658 33aa809d 2019-08-08 stsp
3659 e635744c 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3660 33aa809d 2019-08-08 stsp if (err)
3661 33aa809d 2019-08-08 stsp goto done;
3662 33aa809d 2019-08-08 stsp
3663 33aa809d 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3664 33aa809d 2019-08-08 stsp if (err)
3665 33aa809d 2019-08-08 stsp goto done;
3666 33aa809d 2019-08-08 stsp
3667 33aa809d 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
3668 33aa809d 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
3669 33aa809d 2019-08-08 stsp goto done;
3670 33aa809d 2019-08-08 stsp }
3671 33aa809d 2019-08-08 stsp
3672 33aa809d 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
3673 33aa809d 2019-08-08 stsp f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3674 33aa809d 2019-08-08 stsp if (err)
3675 33aa809d 2019-08-08 stsp goto done;
3676 33aa809d 2019-08-08 stsp
3677 e635744c 2019-08-08 stsp err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3678 33aa809d 2019-08-08 stsp if (err)
3679 33aa809d 2019-08-08 stsp goto done;
3680 33aa809d 2019-08-08 stsp
3681 33aa809d 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1)
3682 33aa809d 2019-08-08 stsp return got_ferror(f1, GOT_ERR_IO);
3683 33aa809d 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1)
3684 33aa809d 2019-08-08 stsp return got_ferror(f2, GOT_ERR_IO);
3685 33aa809d 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
3686 33aa809d 2019-08-08 stsp int choice;
3687 33aa809d 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
3688 33aa809d 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
3689 33aa809d 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
3690 e635744c 2019-08-08 stsp reverse_patch ? NULL : outfile,
3691 e635744c 2019-08-08 stsp reverse_patch ? outfile : NULL,
3692 e635744c 2019-08-08 stsp patch_cb, patch_arg);
3693 33aa809d 2019-08-08 stsp if (err)
3694 33aa809d 2019-08-08 stsp goto done;
3695 33aa809d 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
3696 33aa809d 2019-08-08 stsp have_content = 1;
3697 33aa809d 2019-08-08 stsp else if (choice == GOT_PATCH_CHOICE_QUIT)
3698 33aa809d 2019-08-08 stsp break;
3699 33aa809d 2019-08-08 stsp }
3700 1ebedb77 2019-10-19 stsp if (have_content) {
3701 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
3702 f1e81a05 2019-08-10 stsp reverse_patch ? NULL : outfile,
3703 f1e81a05 2019-08-10 stsp reverse_patch ? outfile : NULL);
3704 1ebedb77 2019-10-19 stsp if (err)
3705 1ebedb77 2019-10-19 stsp goto done;
3706 1ebedb77 2019-10-19 stsp
3707 1ebedb77 2019-10-19 stsp if (chmod(*path_outfile, sb2.st_mode) == -1) {
3708 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("chmod", path2);
3709 1ebedb77 2019-10-19 stsp goto done;
3710 1ebedb77 2019-10-19 stsp }
3711 1ebedb77 2019-10-19 stsp }
3712 33aa809d 2019-08-08 stsp done:
3713 33aa809d 2019-08-08 stsp free(id_str);
3714 33aa809d 2019-08-08 stsp if (blob)
3715 33aa809d 2019-08-08 stsp got_object_blob_close(blob);
3716 33aa809d 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
3717 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
3718 33aa809d 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3719 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
3720 1ebedb77 2019-10-19 stsp if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3721 1ebedb77 2019-10-19 stsp err = got_error_from_errno2("close", path2);
3722 33aa809d 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
3723 33aa809d 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_outfile);
3724 33aa809d 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
3725 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
3726 33aa809d 2019-08-08 stsp if (err || !have_content) {
3727 33aa809d 2019-08-08 stsp if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
3728 33aa809d 2019-08-08 stsp err = got_error_from_errno2("unlink", *path_outfile);
3729 33aa809d 2019-08-08 stsp free(*path_outfile);
3730 33aa809d 2019-08-08 stsp *path_outfile = NULL;
3731 33aa809d 2019-08-08 stsp }
3732 33aa809d 2019-08-08 stsp free(args);
3733 33aa809d 2019-08-08 stsp if (ds) {
3734 33aa809d 2019-08-08 stsp got_diff_state_free(ds);
3735 33aa809d 2019-08-08 stsp free(ds);
3736 33aa809d 2019-08-08 stsp }
3737 33aa809d 2019-08-08 stsp if (changes)
3738 33aa809d 2019-08-08 stsp got_diff_free_changes(changes);
3739 33aa809d 2019-08-08 stsp free(path1);
3740 33aa809d 2019-08-08 stsp return err;
3741 33aa809d 2019-08-08 stsp }
3742 33aa809d 2019-08-08 stsp
3743 33aa809d 2019-08-08 stsp static const struct got_error *
3744 1f1abb7e 2019-08-08 stsp revert_file(void *arg, unsigned char status, unsigned char staged_status,
3745 1f1abb7e 2019-08-08 stsp const char *relpath, struct got_object_id *blob_id,
3746 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3747 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3748 a129376b 2019-03-28 stsp {
3749 1f1abb7e 2019-08-08 stsp struct revert_file_args *a = arg;
3750 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
3751 1f1abb7e 2019-08-08 stsp char *parent_path = NULL;
3752 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
3753 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
3754 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
3755 24278f30 2019-08-03 stsp const struct got_tree_entry *te = NULL;
3756 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
3757 33aa809d 2019-08-08 stsp char *ondisk_path = NULL, *path_content = NULL;
3758 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
3759 a129376b 2019-03-28 stsp
3760 d3bcc3d1 2019-08-08 stsp /* Reverting a staged deletion is a no-op. */
3761 d3bcc3d1 2019-08-08 stsp if (status == GOT_STATUS_DELETE &&
3762 d3bcc3d1 2019-08-08 stsp staged_status != GOT_STATUS_NO_CHANGE)
3763 d3bcc3d1 2019-08-08 stsp return NULL;
3764 3d69ad8d 2019-08-17 semarie
3765 3d69ad8d 2019-08-17 semarie if (status == GOT_STATUS_UNVERSIONED)
3766 3d69ad8d 2019-08-17 semarie return (*a->progress_cb)(a->progress_arg,
3767 3d69ad8d 2019-08-17 semarie GOT_STATUS_UNVERSIONED, relpath);
3768 d3bcc3d1 2019-08-08 stsp
3769 1f1abb7e 2019-08-08 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3770 65084dad 2019-08-08 stsp if (ie == NULL)
3771 63f810e6 2020-02-29 stsp return got_error_path(relpath, GOT_ERR_BAD_PATH);
3772 a129376b 2019-03-28 stsp
3773 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
3774 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
3775 a129376b 2019-03-28 stsp if (err) {
3776 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
3777 a129376b 2019-03-28 stsp goto done;
3778 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
3779 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
3780 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
3781 e20a8b6f 2019-06-04 stsp goto done;
3782 e20a8b6f 2019-06-04 stsp }
3783 a129376b 2019-03-28 stsp }
3784 1f1abb7e 2019-08-08 stsp if (got_path_is_root_dir(a->worktree->path_prefix)) {
3785 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
3786 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3787 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3788 a129376b 2019-03-28 stsp goto done;
3789 a129376b 2019-03-28 stsp }
3790 a129376b 2019-03-28 stsp } else {
3791 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
3792 1f1abb7e 2019-08-08 stsp tree_path = strdup(a->worktree->path_prefix);
3793 a129376b 2019-03-28 stsp if (tree_path == NULL) {
3794 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
3795 a129376b 2019-03-28 stsp goto done;
3796 a129376b 2019-03-28 stsp }
3797 a129376b 2019-03-28 stsp } else {
3798 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
3799 1f1abb7e 2019-08-08 stsp a->worktree->path_prefix, parent_path) == -1) {
3800 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3801 a129376b 2019-03-28 stsp goto done;
3802 a129376b 2019-03-28 stsp }
3803 a129376b 2019-03-28 stsp }
3804 a129376b 2019-03-28 stsp }
3805 a129376b 2019-03-28 stsp
3806 1f1abb7e 2019-08-08 stsp err = got_object_id_by_path(&tree_id, a->repo,
3807 1f1abb7e 2019-08-08 stsp a->worktree->base_commit_id, tree_path);
3808 a9fa2909 2019-07-27 stsp if (err) {
3809 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
3810 24278f30 2019-08-03 stsp (status == GOT_STATUS_ADD ||
3811 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_ADD)))
3812 a9fa2909 2019-07-27 stsp goto done;
3813 a9fa2909 2019-07-27 stsp } else {
3814 1f1abb7e 2019-08-08 stsp err = got_object_open_as_tree(&tree, a->repo, tree_id);
3815 a9fa2909 2019-07-27 stsp if (err)
3816 a9fa2909 2019-07-27 stsp goto done;
3817 a9fa2909 2019-07-27 stsp
3818 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
3819 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
3820 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
3821 a9fa2909 2019-07-27 stsp goto done;
3822 a9fa2909 2019-07-27 stsp }
3823 a9fa2909 2019-07-27 stsp
3824 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
3825 24278f30 2019-08-03 stsp if (te == NULL && status != GOT_STATUS_ADD &&
3826 24278f30 2019-08-03 stsp staged_status != GOT_STATUS_ADD) {
3827 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
3828 a9fa2909 2019-07-27 stsp goto done;
3829 a9fa2909 2019-07-27 stsp }
3830 a129376b 2019-03-28 stsp }
3831 a129376b 2019-03-28 stsp
3832 a129376b 2019-03-28 stsp switch (status) {
3833 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
3834 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3835 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3836 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3837 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3838 33aa809d 2019-08-08 stsp if (err)
3839 33aa809d 2019-08-08 stsp goto done;
3840 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3841 33aa809d 2019-08-08 stsp break;
3842 33aa809d 2019-08-08 stsp }
3843 1f1abb7e 2019-08-08 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
3844 1f1abb7e 2019-08-08 stsp ie->path);
3845 1ee397ad 2019-07-12 stsp if (err)
3846 1ee397ad 2019-07-12 stsp goto done;
3847 1f1abb7e 2019-08-08 stsp got_fileindex_entry_remove(a->fileindex, ie);
3848 a129376b 2019-03-28 stsp break;
3849 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
3850 33aa809d 2019-08-08 stsp if (a->patch_cb) {
3851 33aa809d 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
3852 33aa809d 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
3853 33aa809d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
3854 33aa809d 2019-08-08 stsp if (err)
3855 33aa809d 2019-08-08 stsp goto done;
3856 33aa809d 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
3857 33aa809d 2019-08-08 stsp break;
3858 33aa809d 2019-08-08 stsp }
3859 33aa809d 2019-08-08 stsp /* fall through */
3860 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
3861 1ebedb77 2019-10-19 stsp case GOT_STATUS_MODE_CHANGE:
3862 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
3863 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
3864 e20a8b6f 2019-06-04 stsp struct got_object_id id;
3865 24278f30 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3866 24278f30 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY) {
3867 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->staged_blob_sha1,
3868 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3869 24278f30 2019-08-03 stsp } else
3870 24278f30 2019-08-03 stsp memcpy(id.sha1, ie->blob_sha1,
3871 24278f30 2019-08-03 stsp SHA1_DIGEST_LENGTH);
3872 1f1abb7e 2019-08-08 stsp err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
3873 a129376b 2019-03-28 stsp if (err)
3874 65084dad 2019-08-08 stsp goto done;
3875 65084dad 2019-08-08 stsp
3876 65084dad 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3877 65084dad 2019-08-08 stsp got_worktree_get_root_path(a->worktree), relpath) == -1) {
3878 65084dad 2019-08-08 stsp err = got_error_from_errno("asprintf");
3879 a129376b 2019-03-28 stsp goto done;
3880 65084dad 2019-08-08 stsp }
3881 65084dad 2019-08-08 stsp
3882 33aa809d 2019-08-08 stsp if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
3883 33aa809d 2019-08-08 stsp status == GOT_STATUS_CONFLICT)) {
3884 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 1, &id,
3885 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path, a->repo,
3886 33aa809d 2019-08-08 stsp a->patch_cb, a->patch_arg);
3887 33aa809d 2019-08-08 stsp if (err || path_content == NULL)
3888 33aa809d 2019-08-08 stsp break;
3889 33aa809d 2019-08-08 stsp if (rename(path_content, ondisk_path) == -1) {
3890 33aa809d 2019-08-08 stsp err = got_error_from_errno3("rename",
3891 33aa809d 2019-08-08 stsp path_content, ondisk_path);
3892 33aa809d 2019-08-08 stsp goto done;
3893 33aa809d 2019-08-08 stsp }
3894 33aa809d 2019-08-08 stsp } else {
3895 33aa809d 2019-08-08 stsp err = install_blob(a->worktree, ondisk_path, ie->path,
3896 33aa809d 2019-08-08 stsp te ? te->mode : GOT_DEFAULT_FILE_MODE,
3897 33aa809d 2019-08-08 stsp got_fileindex_perms_to_st(ie), blob, 0, 1,
3898 33aa809d 2019-08-08 stsp a->repo, a->progress_cb, a->progress_arg);
3899 a129376b 2019-03-28 stsp if (err)
3900 a129376b 2019-03-28 stsp goto done;
3901 1ebedb77 2019-10-19 stsp if (status == GOT_STATUS_DELETE ||
3902 1ebedb77 2019-10-19 stsp status == GOT_STATUS_MODE_CHANGE) {
3903 054041d0 2020-06-24 stsp err = got_fileindex_entry_update(ie,
3904 054041d0 2020-06-24 stsp ondisk_path, blob->id.sha1,
3905 054041d0 2020-06-24 stsp a->worktree->base_commit_id->sha1, 1);
3906 33aa809d 2019-08-08 stsp if (err)
3907 33aa809d 2019-08-08 stsp goto done;
3908 33aa809d 2019-08-08 stsp }
3909 a129376b 2019-03-28 stsp }
3910 a129376b 2019-03-28 stsp break;
3911 e20a8b6f 2019-06-04 stsp }
3912 a129376b 2019-03-28 stsp default:
3913 1f1abb7e 2019-08-08 stsp break;
3914 a129376b 2019-03-28 stsp }
3915 a129376b 2019-03-28 stsp done:
3916 1f1abb7e 2019-08-08 stsp free(ondisk_path);
3917 33aa809d 2019-08-08 stsp free(path_content);
3918 e20a8b6f 2019-06-04 stsp free(parent_path);
3919 a129376b 2019-03-28 stsp free(tree_path);
3920 a129376b 2019-03-28 stsp if (blob)
3921 a129376b 2019-03-28 stsp got_object_blob_close(blob);
3922 a129376b 2019-03-28 stsp if (tree)
3923 a129376b 2019-03-28 stsp got_object_tree_close(tree);
3924 a129376b 2019-03-28 stsp free(tree_id);
3925 e20a8b6f 2019-06-04 stsp return err;
3926 e20a8b6f 2019-06-04 stsp }
3927 e20a8b6f 2019-06-04 stsp
3928 e20a8b6f 2019-06-04 stsp const struct got_error *
3929 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
3930 2163d960 2019-08-08 stsp struct got_pathlist_head *paths,
3931 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3932 33aa809d 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
3933 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
3934 e20a8b6f 2019-06-04 stsp {
3935 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
3936 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
3937 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3938 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
3939 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3940 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
3941 e20a8b6f 2019-06-04 stsp
3942 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
3943 e20a8b6f 2019-06-04 stsp if (err)
3944 e20a8b6f 2019-06-04 stsp return err;
3945 e20a8b6f 2019-06-04 stsp
3946 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3947 e20a8b6f 2019-06-04 stsp if (err)
3948 e20a8b6f 2019-06-04 stsp goto done;
3949 e20a8b6f 2019-06-04 stsp
3950 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
3951 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
3952 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
3953 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
3954 33aa809d 2019-08-08 stsp rfa.patch_cb = patch_cb;
3955 33aa809d 2019-08-08 stsp rfa.patch_arg = patch_arg;
3956 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
3957 2163d960 2019-08-08 stsp TAILQ_FOREACH(pe, paths, entry) {
3958 1f1abb7e 2019-08-08 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3959 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
3960 e20a8b6f 2019-06-04 stsp if (err)
3961 af12c6b9 2019-06-04 stsp break;
3962 e20a8b6f 2019-06-04 stsp }
3963 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3964 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
3965 e20a8b6f 2019-06-04 stsp err = sync_err;
3966 af12c6b9 2019-06-04 stsp done:
3967 fb399478 2019-07-12 stsp free(fileindex_path);
3968 a129376b 2019-03-28 stsp if (fileindex)
3969 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
3970 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3971 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
3972 a129376b 2019-03-28 stsp err = unlockerr;
3973 c4296144 2019-05-09 stsp return err;
3974 c4296144 2019-05-09 stsp }
3975 c4296144 2019-05-09 stsp
3976 cf066bf8 2019-05-09 stsp static void
3977 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
3978 cf066bf8 2019-05-09 stsp {
3979 24519714 2019-05-09 stsp free(ct->path);
3980 44d03001 2019-05-09 stsp free(ct->in_repo_path);
3981 768aea60 2019-05-09 stsp free(ct->ondisk_path);
3982 e75eb4da 2019-05-10 stsp free(ct->blob_id);
3983 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
3984 f0b75401 2019-08-03 stsp free(ct->staged_blob_id);
3985 b416585c 2019-05-13 stsp free(ct->base_commit_id);
3986 cf066bf8 2019-05-09 stsp free(ct);
3987 cf066bf8 2019-05-09 stsp }
3988 24519714 2019-05-09 stsp
3989 ed175427 2019-05-09 stsp struct collect_commitables_arg {
3990 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
3991 24519714 2019-05-09 stsp struct got_repository *repo;
3992 24519714 2019-05-09 stsp struct got_worktree *worktree;
3993 5f8a88c6 2019-08-03 stsp int have_staged_files;
3994 24519714 2019-05-09 stsp };
3995 cf066bf8 2019-05-09 stsp
3996 c4296144 2019-05-09 stsp static const struct got_error *
3997 88d0e355 2019-08-03 stsp collect_commitables(void *arg, unsigned char status,
3998 88d0e355 2019-08-03 stsp unsigned char staged_status, const char *relpath,
3999 537ac44b 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4000 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
4001 c4296144 2019-05-09 stsp {
4002 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
4003 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
4004 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4005 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
4006 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
4007 768aea60 2019-05-09 stsp struct stat sb;
4008 c4296144 2019-05-09 stsp
4009 5f8a88c6 2019-08-03 stsp if (a->have_staged_files) {
4010 5f8a88c6 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4011 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4012 5f8a88c6 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4013 5f8a88c6 2019-08-03 stsp return NULL;
4014 5f8a88c6 2019-08-03 stsp } else {
4015 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_CONFLICT)
4016 5f8a88c6 2019-08-03 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
4017 c4296144 2019-05-09 stsp
4018 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4019 1ebedb77 2019-10-19 stsp status != GOT_STATUS_MODE_CHANGE &&
4020 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_ADD &&
4021 5f8a88c6 2019-08-03 stsp status != GOT_STATUS_DELETE)
4022 5f8a88c6 2019-08-03 stsp return NULL;
4023 5f8a88c6 2019-08-03 stsp }
4024 0b5cc0d6 2019-05-09 stsp
4025 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
4026 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4027 036813ee 2019-05-09 stsp goto done;
4028 036813ee 2019-05-09 stsp }
4029 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
4030 036813ee 2019-05-09 stsp parent_path = strdup("");
4031 69960a46 2019-05-09 stsp if (parent_path == NULL)
4032 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
4033 69960a46 2019-05-09 stsp } else {
4034 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
4035 69960a46 2019-05-09 stsp if (err)
4036 69960a46 2019-05-09 stsp return err;
4037 69960a46 2019-05-09 stsp }
4038 c4296144 2019-05-09 stsp
4039 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
4040 cf066bf8 2019-05-09 stsp if (ct == NULL) {
4041 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4042 c4296144 2019-05-09 stsp goto done;
4043 768aea60 2019-05-09 stsp }
4044 768aea60 2019-05-09 stsp
4045 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4046 768aea60 2019-05-09 stsp relpath) == -1) {
4047 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4048 768aea60 2019-05-09 stsp goto done;
4049 768aea60 2019-05-09 stsp }
4050 5f8a88c6 2019-08-03 stsp if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4051 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
4052 768aea60 2019-05-09 stsp } else {
4053 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4054 12463d8b 2019-12-13 stsp if (fstatat(dirfd, de_name, &sb,
4055 12463d8b 2019-12-13 stsp AT_SYMLINK_NOFOLLOW) == -1) {
4056 82223ffc 2019-12-13 stsp err = got_error_from_errno2("fstatat",
4057 12463d8b 2019-12-13 stsp ct->ondisk_path);
4058 12463d8b 2019-12-13 stsp goto done;
4059 12463d8b 2019-12-13 stsp }
4060 12463d8b 2019-12-13 stsp } else if (lstat(ct->ondisk_path, &sb) == -1) {
4061 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
4062 768aea60 2019-05-09 stsp goto done;
4063 768aea60 2019-05-09 stsp }
4064 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
4065 c4296144 2019-05-09 stsp }
4066 c4296144 2019-05-09 stsp
4067 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4068 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4069 44d03001 2019-05-09 stsp relpath) == -1) {
4070 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4071 44d03001 2019-05-09 stsp goto done;
4072 44d03001 2019-05-09 stsp }
4073 44d03001 2019-05-09 stsp
4074 cf066bf8 2019-05-09 stsp ct->status = status;
4075 5f8a88c6 2019-08-03 stsp ct->staged_status = staged_status;
4076 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
4077 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_ADD &&
4078 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) {
4079 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
4080 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
4081 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4082 b416585c 2019-05-13 stsp goto done;
4083 b416585c 2019-05-13 stsp }
4084 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
4085 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
4086 f0b75401 2019-08-03 stsp err = got_error_from_errno("got_object_id_dup");
4087 f0b75401 2019-08-03 stsp goto done;
4088 f0b75401 2019-08-03 stsp }
4089 f0b75401 2019-08-03 stsp }
4090 f0b75401 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4091 f0b75401 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4092 f0b75401 2019-08-03 stsp ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4093 f0b75401 2019-08-03 stsp if (ct->staged_blob_id == NULL) {
4094 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4095 036813ee 2019-05-09 stsp goto done;
4096 036813ee 2019-05-09 stsp }
4097 ca2503ea 2019-05-09 stsp }
4098 24519714 2019-05-09 stsp ct->path = strdup(path);
4099 24519714 2019-05-09 stsp if (ct->path == NULL) {
4100 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
4101 24519714 2019-05-09 stsp goto done;
4102 24519714 2019-05-09 stsp }
4103 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4104 c4296144 2019-05-09 stsp done:
4105 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
4106 ed175427 2019-05-09 stsp free_commitable(ct);
4107 24519714 2019-05-09 stsp free(parent_path);
4108 036813ee 2019-05-09 stsp free(path);
4109 a129376b 2019-03-28 stsp return err;
4110 a129376b 2019-03-28 stsp }
4111 c4296144 2019-05-09 stsp
4112 ba580f68 2020-03-22 stsp static const struct got_error *write_tree(struct got_object_id **, int *,
4113 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
4114 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4115 036813ee 2019-05-09 stsp struct got_repository *);
4116 ed175427 2019-05-09 stsp
4117 ed175427 2019-05-09 stsp static const struct got_error *
4118 ba580f68 2020-03-22 stsp write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4119 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
4120 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4121 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4122 afa376bf 2019-05-09 stsp struct got_repository *repo)
4123 ed175427 2019-05-09 stsp {
4124 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
4125 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
4126 036813ee 2019-05-09 stsp char *subpath;
4127 ed175427 2019-05-09 stsp
4128 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
4129 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4130 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4131 ed175427 2019-05-09 stsp
4132 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, repo, &te->id);
4133 ed175427 2019-05-09 stsp if (err)
4134 ed175427 2019-05-09 stsp return err;
4135 ed175427 2019-05-09 stsp
4136 ba580f68 2020-03-22 stsp err = write_tree(new_subtree_id, nentries, subtree, subpath,
4137 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4138 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
4139 036813ee 2019-05-09 stsp free(subpath);
4140 036813ee 2019-05-09 stsp return err;
4141 036813ee 2019-05-09 stsp }
4142 ed175427 2019-05-09 stsp
4143 036813ee 2019-05-09 stsp static const struct got_error *
4144 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4145 036813ee 2019-05-09 stsp {
4146 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4147 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
4148 ed175427 2019-05-09 stsp
4149 036813ee 2019-05-09 stsp *match = 0;
4150 ed175427 2019-05-09 stsp
4151 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
4152 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
4153 0f63689d 2019-05-10 stsp return NULL;
4154 036813ee 2019-05-09 stsp }
4155 0b5cc0d6 2019-05-09 stsp
4156 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4157 0f63689d 2019-05-10 stsp if (err)
4158 0f63689d 2019-05-10 stsp return err;
4159 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
4160 036813ee 2019-05-09 stsp free(ct_parent_path);
4161 036813ee 2019-05-09 stsp return err;
4162 036813ee 2019-05-09 stsp }
4163 0b5cc0d6 2019-05-09 stsp
4164 768aea60 2019-05-09 stsp static mode_t
4165 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
4166 768aea60 2019-05-09 stsp {
4167 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4168 768aea60 2019-05-09 stsp }
4169 768aea60 2019-05-09 stsp
4170 036813ee 2019-05-09 stsp static const struct got_error *
4171 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4172 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
4173 036813ee 2019-05-09 stsp {
4174 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4175 ca2503ea 2019-05-09 stsp
4176 036813ee 2019-05-09 stsp *new_te = NULL;
4177 0b5cc0d6 2019-05-09 stsp
4178 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
4179 036813ee 2019-05-09 stsp if (err)
4180 036813ee 2019-05-09 stsp goto done;
4181 0b5cc0d6 2019-05-09 stsp
4182 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4183 036813ee 2019-05-09 stsp
4184 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_MODIFY)
4185 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4186 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4187 5f8a88c6 2019-08-03 stsp else
4188 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4189 036813ee 2019-05-09 stsp done:
4190 036813ee 2019-05-09 stsp if (err && *new_te) {
4191 56e0773d 2019-11-28 stsp free(*new_te);
4192 036813ee 2019-05-09 stsp *new_te = NULL;
4193 036813ee 2019-05-09 stsp }
4194 036813ee 2019-05-09 stsp return err;
4195 036813ee 2019-05-09 stsp }
4196 036813ee 2019-05-09 stsp
4197 036813ee 2019-05-09 stsp static const struct got_error *
4198 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4199 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
4200 036813ee 2019-05-09 stsp {
4201 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4202 036813ee 2019-05-09 stsp char *ct_name;
4203 036813ee 2019-05-09 stsp
4204 036813ee 2019-05-09 stsp *new_te = NULL;
4205 036813ee 2019-05-09 stsp
4206 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
4207 036813ee 2019-05-09 stsp if (*new_te == NULL)
4208 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4209 036813ee 2019-05-09 stsp
4210 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
4211 036813ee 2019-05-09 stsp if (ct_name == NULL) {
4212 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
4213 036813ee 2019-05-09 stsp goto done;
4214 036813ee 2019-05-09 stsp }
4215 56e0773d 2019-11-28 stsp if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4216 56e0773d 2019-11-28 stsp sizeof((*new_te)->name)) {
4217 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4218 036813ee 2019-05-09 stsp goto done;
4219 036813ee 2019-05-09 stsp }
4220 036813ee 2019-05-09 stsp
4221 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
4222 036813ee 2019-05-09 stsp
4223 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD)
4224 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->staged_blob_id,
4225 56e0773d 2019-11-28 stsp sizeof((*new_te)->id));
4226 5f8a88c6 2019-08-03 stsp else
4227 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4228 036813ee 2019-05-09 stsp done:
4229 036813ee 2019-05-09 stsp if (err && *new_te) {
4230 56e0773d 2019-11-28 stsp free(*new_te);
4231 036813ee 2019-05-09 stsp *new_te = NULL;
4232 036813ee 2019-05-09 stsp }
4233 036813ee 2019-05-09 stsp return err;
4234 036813ee 2019-05-09 stsp }
4235 036813ee 2019-05-09 stsp
4236 036813ee 2019-05-09 stsp static const struct got_error *
4237 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
4238 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
4239 036813ee 2019-05-09 stsp {
4240 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4241 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
4242 036813ee 2019-05-09 stsp
4243 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4244 036813ee 2019-05-09 stsp if (err)
4245 036813ee 2019-05-09 stsp return err;
4246 036813ee 2019-05-09 stsp if (new_pe == NULL)
4247 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
4248 036813ee 2019-05-09 stsp return NULL;
4249 afa376bf 2019-05-09 stsp }
4250 afa376bf 2019-05-09 stsp
4251 afa376bf 2019-05-09 stsp static const struct got_error *
4252 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
4253 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
4254 afa376bf 2019-05-09 stsp {
4255 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
4256 5f8a88c6 2019-08-03 stsp unsigned char status;
4257 5f8a88c6 2019-08-03 stsp
4258 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
4259 afa376bf 2019-05-09 stsp ct_path++;
4260 5f8a88c6 2019-08-03 stsp
4261 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4262 5f8a88c6 2019-08-03 stsp status = ct->staged_status;
4263 5f8a88c6 2019-08-03 stsp else
4264 5f8a88c6 2019-08-03 stsp status = ct->status;
4265 5f8a88c6 2019-08-03 stsp
4266 5f8a88c6 2019-08-03 stsp return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4267 12463d8b 2019-12-13 stsp ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4268 036813ee 2019-05-09 stsp }
4269 036813ee 2019-05-09 stsp
4270 036813ee 2019-05-09 stsp static const struct got_error *
4271 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
4272 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4273 44d03001 2019-05-09 stsp {
4274 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
4275 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
4276 44d03001 2019-05-09 stsp char *te_path;
4277 44d03001 2019-05-09 stsp
4278 44d03001 2019-05-09 stsp *modified = 0;
4279 44d03001 2019-05-09 stsp
4280 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
4281 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
4282 44d03001 2019-05-09 stsp te->name) == -1)
4283 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4284 44d03001 2019-05-09 stsp
4285 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4286 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4287 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
4288 44d03001 2019-05-09 stsp strlen(te_path));
4289 44d03001 2019-05-09 stsp if (*modified)
4290 44d03001 2019-05-09 stsp break;
4291 44d03001 2019-05-09 stsp }
4292 44d03001 2019-05-09 stsp
4293 44d03001 2019-05-09 stsp free(te_path);
4294 44d03001 2019-05-09 stsp return err;
4295 44d03001 2019-05-09 stsp }
4296 44d03001 2019-05-09 stsp
4297 44d03001 2019-05-09 stsp static const struct got_error *
4298 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
4299 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
4300 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
4301 036813ee 2019-05-09 stsp {
4302 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4303 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4304 036813ee 2019-05-09 stsp
4305 036813ee 2019-05-09 stsp *ctp = NULL;
4306 036813ee 2019-05-09 stsp
4307 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4308 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4309 036813ee 2019-05-09 stsp char *ct_name = NULL;
4310 036813ee 2019-05-09 stsp int path_matches;
4311 036813ee 2019-05-09 stsp
4312 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4313 5f8a88c6 2019-08-03 stsp if (ct->status != GOT_STATUS_MODIFY &&
4314 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE &&
4315 5f8a88c6 2019-08-03 stsp ct->status != GOT_STATUS_DELETE)
4316 5f8a88c6 2019-08-03 stsp continue;
4317 5f8a88c6 2019-08-03 stsp } else {
4318 5f8a88c6 2019-08-03 stsp if (ct->staged_status != GOT_STATUS_MODIFY &&
4319 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_DELETE)
4320 5f8a88c6 2019-08-03 stsp continue;
4321 5f8a88c6 2019-08-03 stsp }
4322 036813ee 2019-05-09 stsp
4323 56e0773d 2019-11-28 stsp if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4324 036813ee 2019-05-09 stsp continue;
4325 036813ee 2019-05-09 stsp
4326 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4327 036813ee 2019-05-09 stsp if (err)
4328 036813ee 2019-05-09 stsp return err;
4329 036813ee 2019-05-09 stsp if (!path_matches)
4330 036813ee 2019-05-09 stsp continue;
4331 036813ee 2019-05-09 stsp
4332 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
4333 036813ee 2019-05-09 stsp if (ct_name == NULL)
4334 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
4335 036813ee 2019-05-09 stsp
4336 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
4337 036813ee 2019-05-09 stsp continue;
4338 036813ee 2019-05-09 stsp
4339 036813ee 2019-05-09 stsp *ctp = ct;
4340 036813ee 2019-05-09 stsp break;
4341 036813ee 2019-05-09 stsp }
4342 2b496619 2019-07-10 stsp
4343 2b496619 2019-07-10 stsp return err;
4344 2b496619 2019-07-10 stsp }
4345 2b496619 2019-07-10 stsp
4346 2b496619 2019-07-10 stsp static const struct got_error *
4347 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4348 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
4349 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
4350 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
4351 2b496619 2019-07-10 stsp struct got_repository *repo)
4352 2b496619 2019-07-10 stsp {
4353 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
4354 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
4355 2b496619 2019-07-10 stsp char *subtree_path;
4356 56e0773d 2019-11-28 stsp struct got_object_id *id = NULL;
4357 ba580f68 2020-03-22 stsp int nentries;
4358 2b496619 2019-07-10 stsp
4359 2b496619 2019-07-10 stsp *new_tep = NULL;
4360 2b496619 2019-07-10 stsp
4361 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4362 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
4363 2b496619 2019-07-10 stsp child_path) == -1)
4364 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
4365 036813ee 2019-05-09 stsp
4366 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
4367 d6fca0ba 2019-09-15 hiltjo if (new_te == NULL)
4368 d6fca0ba 2019-09-15 hiltjo return got_error_from_errno("calloc");
4369 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
4370 56e0773d 2019-11-28 stsp
4371 56e0773d 2019-11-28 stsp if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4372 56e0773d 2019-11-28 stsp sizeof(new_te->name)) {
4373 56e0773d 2019-11-28 stsp err = got_error(GOT_ERR_NO_SPACE);
4374 2b496619 2019-07-10 stsp goto done;
4375 2b496619 2019-07-10 stsp }
4376 ba580f68 2020-03-22 stsp err = write_tree(&id, &nentries, NULL, subtree_path,
4377 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
4378 2b496619 2019-07-10 stsp if (err) {
4379 56e0773d 2019-11-28 stsp free(new_te);
4380 2b496619 2019-07-10 stsp goto done;
4381 2b496619 2019-07-10 stsp }
4382 56e0773d 2019-11-28 stsp memcpy(&new_te->id, id, sizeof(new_te->id));
4383 2b496619 2019-07-10 stsp done:
4384 56e0773d 2019-11-28 stsp free(id);
4385 2b496619 2019-07-10 stsp free(subtree_path);
4386 2b496619 2019-07-10 stsp if (err == NULL)
4387 2b496619 2019-07-10 stsp *new_tep = new_te;
4388 036813ee 2019-05-09 stsp return err;
4389 036813ee 2019-05-09 stsp }
4390 036813ee 2019-05-09 stsp
4391 036813ee 2019-05-09 stsp static const struct got_error *
4392 ba580f68 2020-03-22 stsp write_tree(struct got_object_id **new_tree_id, int *nentries,
4393 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
4394 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
4395 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4396 036813ee 2019-05-09 stsp struct got_repository *repo)
4397 036813ee 2019-05-09 stsp {
4398 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
4399 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
4400 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
4401 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
4402 036813ee 2019-05-09 stsp
4403 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
4404 ba580f68 2020-03-22 stsp *nentries = 0;
4405 036813ee 2019-05-09 stsp
4406 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
4407 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4408 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4409 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
4410 036813ee 2019-05-09 stsp
4411 5f8a88c6 2019-08-03 stsp if ((ct->status != GOT_STATUS_ADD &&
4412 5f8a88c6 2019-08-03 stsp ct->staged_status != GOT_STATUS_ADD) ||
4413 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
4414 036813ee 2019-05-09 stsp continue;
4415 036813ee 2019-05-09 stsp
4416 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
4417 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
4418 036813ee 2019-05-09 stsp continue;
4419 036813ee 2019-05-09 stsp
4420 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4421 036813ee 2019-05-09 stsp pe->path);
4422 036813ee 2019-05-09 stsp if (err)
4423 036813ee 2019-05-09 stsp goto done;
4424 036813ee 2019-05-09 stsp
4425 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
4426 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
4427 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
4428 036813ee 2019-05-09 stsp if (err)
4429 036813ee 2019-05-09 stsp goto done;
4430 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
4431 afa376bf 2019-05-09 stsp if (err)
4432 afa376bf 2019-05-09 stsp goto done;
4433 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
4434 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4435 2b496619 2019-07-10 stsp if (err)
4436 2f51b5b3 2019-05-09 stsp goto done;
4437 ba580f68 2020-03-22 stsp (*nentries)++;
4438 2b496619 2019-07-10 stsp } else {
4439 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
4440 2b496619 2019-07-10 stsp if (base_tree == NULL ||
4441 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
4442 2b496619 2019-07-10 stsp == NULL) {
4443 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
4444 2b496619 2019-07-10 stsp child_path, path_base_tree,
4445 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
4446 2b496619 2019-07-10 stsp repo);
4447 2b496619 2019-07-10 stsp if (err)
4448 2b496619 2019-07-10 stsp goto done;
4449 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
4450 2b496619 2019-07-10 stsp if (err)
4451 2b496619 2019-07-10 stsp goto done;
4452 ba580f68 2020-03-22 stsp (*nentries)++;
4453 9ba0479c 2019-05-10 stsp }
4454 ed175427 2019-05-09 stsp }
4455 2f51b5b3 2019-05-09 stsp }
4456 2f51b5b3 2019-05-09 stsp
4457 2f51b5b3 2019-05-09 stsp if (base_tree) {
4458 56e0773d 2019-11-28 stsp int i, nbase_entries;
4459 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
4460 56e0773d 2019-11-28 stsp nbase_entries = got_object_tree_get_nentries(base_tree);
4461 56e0773d 2019-11-28 stsp for (i = 0; i < nbase_entries; i++) {
4462 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
4463 63c5ca5d 2019-08-24 stsp
4464 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(base_tree, i);
4465 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te)) {
4466 63c5ca5d 2019-08-24 stsp /* Entry is a submodule; just copy it. */
4467 63c5ca5d 2019-08-24 stsp err = got_object_tree_entry_dup(&new_te, te);
4468 63c5ca5d 2019-08-24 stsp if (err)
4469 63c5ca5d 2019-08-24 stsp goto done;
4470 63c5ca5d 2019-08-24 stsp err = insert_tree_entry(new_te, &paths);
4471 63c5ca5d 2019-08-24 stsp if (err)
4472 63c5ca5d 2019-08-24 stsp goto done;
4473 ba580f68 2020-03-22 stsp (*nentries)++;
4474 63c5ca5d 2019-08-24 stsp continue;
4475 63c5ca5d 2019-08-24 stsp }
4476 2f51b5b3 2019-05-09 stsp
4477 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
4478 44d03001 2019-05-09 stsp int modified;
4479 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4480 036813ee 2019-05-09 stsp if (err)
4481 036813ee 2019-05-09 stsp goto done;
4482 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
4483 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
4484 2f51b5b3 2019-05-09 stsp if (err)
4485 2f51b5b3 2019-05-09 stsp goto done;
4486 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
4487 44d03001 2019-05-09 stsp if (modified) {
4488 56e0773d 2019-11-28 stsp struct got_object_id *new_id;
4489 ba580f68 2020-03-22 stsp int nsubentries;
4490 ba580f68 2020-03-22 stsp err = write_subtree(&new_id,
4491 ba580f68 2020-03-22 stsp &nsubentries, te,
4492 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
4493 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
4494 44d03001 2019-05-09 stsp if (err)
4495 44d03001 2019-05-09 stsp goto done;
4496 ba580f68 2020-03-22 stsp if (nsubentries == 0) {
4497 ba580f68 2020-03-22 stsp /* All entries were deleted. */
4498 ba580f68 2020-03-22 stsp free(new_id);
4499 ba580f68 2020-03-22 stsp continue;
4500 ba580f68 2020-03-22 stsp }
4501 56e0773d 2019-11-28 stsp memcpy(&new_te->id, new_id,
4502 56e0773d 2019-11-28 stsp sizeof(new_te->id));
4503 56e0773d 2019-11-28 stsp free(new_id);
4504 44d03001 2019-05-09 stsp }
4505 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4506 036813ee 2019-05-09 stsp if (err)
4507 036813ee 2019-05-09 stsp goto done;
4508 ba580f68 2020-03-22 stsp (*nentries)++;
4509 2f51b5b3 2019-05-09 stsp continue;
4510 036813ee 2019-05-09 stsp }
4511 2f51b5b3 2019-05-09 stsp
4512 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
4513 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
4514 f66c734c 2019-09-22 stsp if (err)
4515 f66c734c 2019-09-22 stsp goto done;
4516 2f51b5b3 2019-05-09 stsp if (ct) {
4517 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
4518 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_MODIFY ||
4519 1ebedb77 2019-10-19 stsp ct->status == GOT_STATUS_MODE_CHANGE ||
4520 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4521 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
4522 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
4523 2f51b5b3 2019-05-09 stsp if (err)
4524 2f51b5b3 2019-05-09 stsp goto done;
4525 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4526 2f51b5b3 2019-05-09 stsp if (err)
4527 2f51b5b3 2019-05-09 stsp goto done;
4528 ba580f68 2020-03-22 stsp (*nentries)++;
4529 2f51b5b3 2019-05-09 stsp }
4530 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
4531 b416585c 2019-05-13 stsp status_arg);
4532 afa376bf 2019-05-09 stsp if (err)
4533 afa376bf 2019-05-09 stsp goto done;
4534 2f51b5b3 2019-05-09 stsp } else {
4535 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
4536 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
4537 2f51b5b3 2019-05-09 stsp if (err)
4538 2f51b5b3 2019-05-09 stsp goto done;
4539 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
4540 2f51b5b3 2019-05-09 stsp if (err)
4541 2f51b5b3 2019-05-09 stsp goto done;
4542 ba580f68 2020-03-22 stsp (*nentries)++;
4543 2f51b5b3 2019-05-09 stsp }
4544 ca2503ea 2019-05-09 stsp }
4545 ed175427 2019-05-09 stsp }
4546 0b5cc0d6 2019-05-09 stsp
4547 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
4548 ba580f68 2020-03-22 stsp err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4549 ed175427 2019-05-09 stsp done:
4550 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
4551 ed175427 2019-05-09 stsp return err;
4552 ed175427 2019-05-09 stsp }
4553 ed175427 2019-05-09 stsp
4554 ebf99748 2019-05-09 stsp static const struct got_error *
4555 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4556 72fd46fa 2019-09-06 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4557 72fd46fa 2019-09-06 stsp int have_staged_files)
4558 ebf99748 2019-05-09 stsp {
4559 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
4560 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
4561 ebf99748 2019-05-09 stsp
4562 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4563 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
4564 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4565 ebf99748 2019-05-09 stsp
4566 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4567 ebf99748 2019-05-09 stsp if (ie) {
4568 5f8a88c6 2019-08-03 stsp if (ct->status == GOT_STATUS_DELETE ||
4569 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_DELETE) {
4570 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
4571 5f8a88c6 2019-08-03 stsp } else if (ct->staged_status == GOT_STATUS_ADD ||
4572 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY) {
4573 5f8a88c6 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
4574 5f8a88c6 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
4575 5f8a88c6 2019-08-03 stsp err = got_fileindex_entry_update(ie,
4576 5f8a88c6 2019-08-03 stsp ct->ondisk_path, ct->staged_blob_id->sha1,
4577 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4578 72fd46fa 2019-09-06 stsp !have_staged_files);
4579 ebf99748 2019-05-09 stsp } else
4580 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
4581 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
4582 72fd46fa 2019-09-06 stsp new_base_commit_id->sha1,
4583 72fd46fa 2019-09-06 stsp !have_staged_files);
4584 ebf99748 2019-05-09 stsp } else {
4585 3969253a 2020-03-07 stsp err = got_fileindex_entry_alloc(&ie, pe->path);
4586 ebf99748 2019-05-09 stsp if (err)
4587 af12c6b9 2019-06-04 stsp break;
4588 3969253a 2020-03-07 stsp err = got_fileindex_entry_update(ie, ct->ondisk_path,
4589 3969253a 2020-03-07 stsp ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4590 3969253a 2020-03-07 stsp if (err) {
4591 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4592 3969253a 2020-03-07 stsp break;
4593 3969253a 2020-03-07 stsp }
4594 3969253a 2020-03-07 stsp err = got_fileindex_entry_add(fileindex, ie);
4595 3969253a 2020-03-07 stsp if (err) {
4596 3969253a 2020-03-07 stsp got_fileindex_entry_free(ie);
4597 af12c6b9 2019-06-04 stsp break;
4598 3969253a 2020-03-07 stsp }
4599 ebf99748 2019-05-09 stsp }
4600 ebf99748 2019-05-09 stsp }
4601 d56d26ce 2019-05-10 stsp return err;
4602 d56d26ce 2019-05-10 stsp }
4603 735ef5ac 2019-08-03 stsp
4604 d56d26ce 2019-05-10 stsp
4605 d56d26ce 2019-05-10 stsp static const struct got_error *
4606 735ef5ac 2019-08-03 stsp check_out_of_date(const char *in_repo_path, unsigned char status,
4607 5f8a88c6 2019-08-03 stsp unsigned char staged_status, struct got_object_id *base_blob_id,
4608 5f8a88c6 2019-08-03 stsp struct got_object_id *base_commit_id,
4609 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id, struct got_repository *repo,
4610 735ef5ac 2019-08-03 stsp int ood_errcode)
4611 d56d26ce 2019-05-10 stsp {
4612 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
4613 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
4614 1a36436d 2019-06-10 stsp
4615 5f8a88c6 2019-08-03 stsp if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4616 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
4617 735ef5ac 2019-08-03 stsp if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4618 1a36436d 2019-06-10 stsp return NULL;
4619 9bead371 2019-07-28 stsp /*
4620 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
4621 9bead371 2019-07-28 stsp * on matches file content in the branch head.
4622 9bead371 2019-07-28 stsp */
4623 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4624 735ef5ac 2019-08-03 stsp in_repo_path);
4625 9bead371 2019-07-28 stsp if (err) {
4626 aa9f8247 2019-08-05 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
4627 aa9f8247 2019-08-05 stsp err = got_error(ood_errcode);
4628 1a36436d 2019-06-10 stsp goto done;
4629 735ef5ac 2019-08-03 stsp } else if (got_object_id_cmp(id, base_blob_id) != 0)
4630 735ef5ac 2019-08-03 stsp err = got_error(ood_errcode);
4631 1a36436d 2019-06-10 stsp } else {
4632 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
4633 735ef5ac 2019-08-03 stsp err = got_object_id_by_path(&id, repo, head_commit_id,
4634 735ef5ac 2019-08-03 stsp in_repo_path);
4635 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4636 1a36436d 2019-06-10 stsp goto done;
4637 735ef5ac 2019-08-03 stsp err = id ? got_error(ood_errcode) : NULL;
4638 1a36436d 2019-06-10 stsp }
4639 1a36436d 2019-06-10 stsp done:
4640 1a36436d 2019-06-10 stsp free(id);
4641 1a36436d 2019-06-10 stsp return err;
4642 ebf99748 2019-05-09 stsp }
4643 ebf99748 2019-05-09 stsp
4644 c4296144 2019-05-09 stsp const struct got_error *
4645 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
4646 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
4647 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
4648 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
4649 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4650 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
4651 afa376bf 2019-05-09 stsp struct got_repository *repo)
4652 c4296144 2019-05-09 stsp {
4653 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
4654 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
4655 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
4656 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
4657 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
4658 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
4659 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
4660 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
4661 ba580f68 2020-03-22 stsp int nentries;
4662 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
4663 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
4664 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
4665 c4296144 2019-05-09 stsp
4666 c4296144 2019-05-09 stsp *new_commit_id = NULL;
4667 c4296144 2019-05-09 stsp
4668 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
4669 675c7539 2019-05-09 stsp
4670 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4671 588edf97 2019-05-10 stsp if (err)
4672 588edf97 2019-05-10 stsp goto done;
4673 de18fc63 2019-05-09 stsp
4674 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4675 588edf97 2019-05-10 stsp if (err)
4676 588edf97 2019-05-10 stsp goto done;
4677 588edf97 2019-05-10 stsp
4678 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
4679 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
4680 33ad4cbe 2019-05-12 jcs if (err)
4681 33ad4cbe 2019-05-12 jcs goto done;
4682 33ad4cbe 2019-05-12 jcs }
4683 c4296144 2019-05-09 stsp
4684 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
4685 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
4686 33ad4cbe 2019-05-12 jcs goto done;
4687 33ad4cbe 2019-05-12 jcs }
4688 33ad4cbe 2019-05-12 jcs
4689 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
4690 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
4691 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4692 cf066bf8 2019-05-09 stsp char *ondisk_path;
4693 cf066bf8 2019-05-09 stsp
4694 5f8a88c6 2019-08-03 stsp /* Blobs for staged files already exist. */
4695 5f8a88c6 2019-08-03 stsp if (ct->staged_status == GOT_STATUS_ADD ||
4696 5f8a88c6 2019-08-03 stsp ct->staged_status == GOT_STATUS_MODIFY)
4697 5f8a88c6 2019-08-03 stsp continue;
4698 5f8a88c6 2019-08-03 stsp
4699 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
4700 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODIFY &&
4701 1ebedb77 2019-10-19 stsp ct->status != GOT_STATUS_MODE_CHANGE)
4702 cf066bf8 2019-05-09 stsp continue;
4703 cf066bf8 2019-05-09 stsp
4704 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
4705 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
4706 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4707 cf066bf8 2019-05-09 stsp goto done;
4708 cf066bf8 2019-05-09 stsp }
4709 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
4710 a7055788 2019-05-09 stsp free(ondisk_path);
4711 cf066bf8 2019-05-09 stsp if (err)
4712 cf066bf8 2019-05-09 stsp goto done;
4713 cf066bf8 2019-05-09 stsp }
4714 036813ee 2019-05-09 stsp
4715 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
4716 ba580f68 2020-03-22 stsp err = write_tree(&new_tree_id, &nentries, head_tree, "/",
4717 ba580f68 2020-03-22 stsp commitable_paths, status_cb, status_arg, repo);
4718 036813ee 2019-05-09 stsp if (err)
4719 036813ee 2019-05-09 stsp goto done;
4720 036813ee 2019-05-09 stsp
4721 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
4722 de18fc63 2019-05-09 stsp if (err)
4723 de18fc63 2019-05-09 stsp goto done;
4724 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
4725 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
4726 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
4727 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
4728 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
4729 33ad4cbe 2019-05-12 jcs free(logmsg);
4730 9d40349a 2019-05-09 stsp if (err)
4731 9d40349a 2019-05-09 stsp goto done;
4732 9d40349a 2019-05-09 stsp
4733 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
4734 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
4735 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
4736 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
4737 09f5bd90 2019-05-09 stsp goto done;
4738 09f5bd90 2019-05-09 stsp }
4739 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
4740 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
4741 09f5bd90 2019-05-09 stsp if (err)
4742 09f5bd90 2019-05-09 stsp goto done;
4743 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
4744 ebf99748 2019-05-09 stsp if (err)
4745 ebf99748 2019-05-09 stsp goto done;
4746 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
4747 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
4748 09f5bd90 2019-05-09 stsp goto done;
4749 09f5bd90 2019-05-09 stsp }
4750 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
4751 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
4752 f2c16586 2019-05-09 stsp if (err)
4753 f2c16586 2019-05-09 stsp goto done;
4754 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
4755 f2c16586 2019-05-09 stsp if (err)
4756 f2c16586 2019-05-09 stsp goto done;
4757 f2c16586 2019-05-09 stsp
4758 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
4759 09f5bd90 2019-05-09 stsp if (err)
4760 09f5bd90 2019-05-09 stsp goto done;
4761 09f5bd90 2019-05-09 stsp
4762 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
4763 ebf99748 2019-05-09 stsp if (err)
4764 ebf99748 2019-05-09 stsp goto done;
4765 c4296144 2019-05-09 stsp done:
4766 588edf97 2019-05-10 stsp if (head_tree)
4767 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
4768 588edf97 2019-05-10 stsp if (head_commit)
4769 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
4770 09f5bd90 2019-05-09 stsp free(head_commit_id2);
4771 2f17228e 2019-05-12 stsp if (head_ref2) {
4772 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
4773 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
4774 2f17228e 2019-05-12 stsp err = unlockerr;
4775 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
4776 39cd0ff6 2019-07-12 stsp }
4777 39cd0ff6 2019-07-12 stsp return err;
4778 39cd0ff6 2019-07-12 stsp }
4779 5c1e53bc 2019-07-28 stsp
4780 5c1e53bc 2019-07-28 stsp static const struct got_error *
4781 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
4782 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
4783 5c1e53bc 2019-07-28 stsp {
4784 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
4785 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
4786 39cd0ff6 2019-07-12 stsp
4787 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
4788 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
4789 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
4790 5c1e53bc 2019-07-28 stsp
4791 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
4792 5c1e53bc 2019-07-28 stsp ct_path++;
4793 5c1e53bc 2019-07-28 stsp
4794 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
4795 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
4796 5c1e53bc 2019-07-28 stsp break;
4797 5c1e53bc 2019-07-28 stsp }
4798 5c1e53bc 2019-07-28 stsp
4799 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
4800 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
4801 5c1e53bc 2019-07-28 stsp
4802 5c1e53bc 2019-07-28 stsp return NULL;
4803 5c1e53bc 2019-07-28 stsp }
4804 5c1e53bc 2019-07-28 stsp
4805 f0b75401 2019-08-03 stsp static const struct got_error *
4806 f0b75401 2019-08-03 stsp check_staged_file(void *arg, struct got_fileindex_entry *ie)
4807 f0b75401 2019-08-03 stsp {
4808 f0b75401 2019-08-03 stsp int *have_staged_files = arg;
4809 f0b75401 2019-08-03 stsp
4810 f0b75401 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
4811 f0b75401 2019-08-03 stsp *have_staged_files = 1;
4812 f0b75401 2019-08-03 stsp return got_error(GOT_ERR_CANCELLED);
4813 f0b75401 2019-08-03 stsp }
4814 f0b75401 2019-08-03 stsp
4815 f0b75401 2019-08-03 stsp return NULL;
4816 f0b75401 2019-08-03 stsp }
4817 f0b75401 2019-08-03 stsp
4818 5f8a88c6 2019-08-03 stsp static const struct got_error *
4819 5f8a88c6 2019-08-03 stsp check_non_staged_files(struct got_fileindex *fileindex,
4820 5f8a88c6 2019-08-03 stsp struct got_pathlist_head *paths)
4821 5f8a88c6 2019-08-03 stsp {
4822 5f8a88c6 2019-08-03 stsp struct got_pathlist_entry *pe;
4823 5f8a88c6 2019-08-03 stsp struct got_fileindex_entry *ie;
4824 5f8a88c6 2019-08-03 stsp
4825 5f8a88c6 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
4826 5f8a88c6 2019-08-03 stsp if (pe->path[0] == '\0')
4827 5f8a88c6 2019-08-03 stsp continue;
4828 5f8a88c6 2019-08-03 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4829 5f8a88c6 2019-08-03 stsp if (ie == NULL)
4830 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path, GOT_ERR_BAD_PATH);
4831 5f8a88c6 2019-08-03 stsp if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
4832 5f8a88c6 2019-08-03 stsp return got_error_path(pe->path,
4833 5f8a88c6 2019-08-03 stsp GOT_ERR_FILE_NOT_STAGED);
4834 5f8a88c6 2019-08-03 stsp }
4835 5f8a88c6 2019-08-03 stsp
4836 5f8a88c6 2019-08-03 stsp return NULL;
4837 5f8a88c6 2019-08-03 stsp }
4838 5f8a88c6 2019-08-03 stsp
4839 39cd0ff6 2019-07-12 stsp const struct got_error *
4840 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
4841 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
4842 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
4843 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4844 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
4845 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
4846 39cd0ff6 2019-07-12 stsp {
4847 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
4848 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
4849 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
4850 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4851 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4852 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
4853 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
4854 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4855 f0b75401 2019-08-03 stsp int have_staged_files = 0;
4856 39cd0ff6 2019-07-12 stsp
4857 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
4858 39cd0ff6 2019-07-12 stsp
4859 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4860 39cd0ff6 2019-07-12 stsp
4861 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
4862 39cd0ff6 2019-07-12 stsp if (err)
4863 39cd0ff6 2019-07-12 stsp goto done;
4864 39cd0ff6 2019-07-12 stsp
4865 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4866 39cd0ff6 2019-07-12 stsp if (err)
4867 39cd0ff6 2019-07-12 stsp goto done;
4868 39cd0ff6 2019-07-12 stsp
4869 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4870 39cd0ff6 2019-07-12 stsp if (err)
4871 39cd0ff6 2019-07-12 stsp goto done;
4872 39cd0ff6 2019-07-12 stsp
4873 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
4874 39cd0ff6 2019-07-12 stsp if (err)
4875 39cd0ff6 2019-07-12 stsp goto done;
4876 39cd0ff6 2019-07-12 stsp
4877 5f8a88c6 2019-08-03 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
4878 5f8a88c6 2019-08-03 stsp &have_staged_files);
4879 5f8a88c6 2019-08-03 stsp if (err && err->code != GOT_ERR_CANCELLED)
4880 5f8a88c6 2019-08-03 stsp goto done;
4881 5f8a88c6 2019-08-03 stsp if (have_staged_files) {
4882 5f8a88c6 2019-08-03 stsp err = check_non_staged_files(fileindex, paths);
4883 5f8a88c6 2019-08-03 stsp if (err)
4884 5f8a88c6 2019-08-03 stsp goto done;
4885 5f8a88c6 2019-08-03 stsp }
4886 5f8a88c6 2019-08-03 stsp
4887 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4888 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
4889 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
4890 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = have_staged_files;
4891 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4892 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
4893 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
4894 5c1e53bc 2019-07-28 stsp if (err)
4895 5c1e53bc 2019-07-28 stsp goto done;
4896 5c1e53bc 2019-07-28 stsp }
4897 39cd0ff6 2019-07-12 stsp
4898 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4899 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4900 39cd0ff6 2019-07-12 stsp goto done;
4901 5c1e53bc 2019-07-28 stsp }
4902 5c1e53bc 2019-07-28 stsp
4903 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
4904 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
4905 5c1e53bc 2019-07-28 stsp if (err)
4906 5c1e53bc 2019-07-28 stsp goto done;
4907 39cd0ff6 2019-07-12 stsp }
4908 f0b75401 2019-08-03 stsp
4909 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4910 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
4911 f0b75401 2019-08-03 stsp const char *ct_path = ct->in_repo_path;
4912 f0b75401 2019-08-03 stsp
4913 f0b75401 2019-08-03 stsp while (ct_path[0] == '/')
4914 f0b75401 2019-08-03 stsp ct_path++;
4915 f0b75401 2019-08-03 stsp err = check_out_of_date(ct_path, ct->status,
4916 5f8a88c6 2019-08-03 stsp ct->staged_status, ct->base_blob_id, ct->base_commit_id,
4917 5f8a88c6 2019-08-03 stsp head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
4918 b50cabdf 2019-07-12 stsp if (err)
4919 b50cabdf 2019-07-12 stsp goto done;
4920 f0b75401 2019-08-03 stsp
4921 b50cabdf 2019-07-12 stsp }
4922 b50cabdf 2019-07-12 stsp
4923 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
4924 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
4925 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
4926 39cd0ff6 2019-07-12 stsp if (err)
4927 39cd0ff6 2019-07-12 stsp goto done;
4928 39cd0ff6 2019-07-12 stsp
4929 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4930 72fd46fa 2019-09-06 stsp fileindex, have_staged_files);
4931 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4932 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
4933 39cd0ff6 2019-07-12 stsp err = sync_err;
4934 39cd0ff6 2019-07-12 stsp done:
4935 39cd0ff6 2019-07-12 stsp if (fileindex)
4936 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
4937 39cd0ff6 2019-07-12 stsp free(fileindex_path);
4938 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4939 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
4940 39cd0ff6 2019-07-12 stsp err = unlockerr;
4941 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
4942 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
4943 39cd0ff6 2019-07-12 stsp free_commitable(ct);
4944 39cd0ff6 2019-07-12 stsp }
4945 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
4946 c4296144 2019-05-09 stsp return err;
4947 8656d6c4 2019-05-20 stsp }
4948 8656d6c4 2019-05-20 stsp
4949 8656d6c4 2019-05-20 stsp const char *
4950 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
4951 8656d6c4 2019-05-20 stsp {
4952 8656d6c4 2019-05-20 stsp return ct->path;
4953 8656d6c4 2019-05-20 stsp }
4954 8656d6c4 2019-05-20 stsp
4955 8656d6c4 2019-05-20 stsp unsigned int
4956 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
4957 8656d6c4 2019-05-20 stsp {
4958 8656d6c4 2019-05-20 stsp return ct->status;
4959 818c7501 2019-07-11 stsp }
4960 818c7501 2019-07-11 stsp
4961 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
4962 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4963 818c7501 2019-07-11 stsp struct got_repository *repo;
4964 818c7501 2019-07-11 stsp };
4965 818c7501 2019-07-11 stsp
4966 818c7501 2019-07-11 stsp static const struct got_error *
4967 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
4968 818c7501 2019-07-11 stsp {
4969 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4970 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
4971 818c7501 2019-07-11 stsp unsigned char status;
4972 818c7501 2019-07-11 stsp struct stat sb;
4973 818c7501 2019-07-11 stsp char *ondisk_path;
4974 818c7501 2019-07-11 stsp
4975 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
4976 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
4977 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
4978 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
4979 818c7501 2019-07-11 stsp
4980 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
4981 818c7501 2019-07-11 stsp == -1)
4982 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4983 818c7501 2019-07-11 stsp
4984 b9622844 2019-08-03 stsp /* Reject rebase of a work tree with modified or staged files. */
4985 7f91a133 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
4986 818c7501 2019-07-11 stsp free(ondisk_path);
4987 818c7501 2019-07-11 stsp if (err)
4988 818c7501 2019-07-11 stsp return err;
4989 818c7501 2019-07-11 stsp
4990 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
4991 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
4992 b9622844 2019-08-03 stsp if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
4993 b9622844 2019-08-03 stsp return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
4994 818c7501 2019-07-11 stsp
4995 818c7501 2019-07-11 stsp return NULL;
4996 818c7501 2019-07-11 stsp }
4997 818c7501 2019-07-11 stsp
4998 818c7501 2019-07-11 stsp const struct got_error *
4999 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5000 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5001 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
5002 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5003 818c7501 2019-07-11 stsp {
5004 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
5005 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5006 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
5007 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5008 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
5009 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5010 e51d7b55 2020-01-04 stsp struct got_object_id *wt_branch_tip = NULL;
5011 818c7501 2019-07-11 stsp
5012 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5013 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5014 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5015 818c7501 2019-07-11 stsp
5016 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5017 818c7501 2019-07-11 stsp if (err)
5018 818c7501 2019-07-11 stsp return err;
5019 818c7501 2019-07-11 stsp
5020 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5021 818c7501 2019-07-11 stsp if (err)
5022 818c7501 2019-07-11 stsp goto done;
5023 818c7501 2019-07-11 stsp
5024 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
5025 818c7501 2019-07-11 stsp ok_arg.repo = repo;
5026 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5027 818c7501 2019-07-11 stsp &ok_arg);
5028 818c7501 2019-07-11 stsp if (err)
5029 818c7501 2019-07-11 stsp goto done;
5030 818c7501 2019-07-11 stsp
5031 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5032 818c7501 2019-07-11 stsp if (err)
5033 818c7501 2019-07-11 stsp goto done;
5034 818c7501 2019-07-11 stsp
5035 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5036 818c7501 2019-07-11 stsp if (err)
5037 818c7501 2019-07-11 stsp goto done;
5038 818c7501 2019-07-11 stsp
5039 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5040 818c7501 2019-07-11 stsp if (err)
5041 818c7501 2019-07-11 stsp goto done;
5042 818c7501 2019-07-11 stsp
5043 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5044 818c7501 2019-07-11 stsp 0);
5045 e51d7b55 2020-01-04 stsp if (err)
5046 e51d7b55 2020-01-04 stsp goto done;
5047 e51d7b55 2020-01-04 stsp
5048 e51d7b55 2020-01-04 stsp err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5049 818c7501 2019-07-11 stsp if (err)
5050 818c7501 2019-07-11 stsp goto done;
5051 e51d7b55 2020-01-04 stsp if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5052 e51d7b55 2020-01-04 stsp err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5053 e51d7b55 2020-01-04 stsp goto done;
5054 e51d7b55 2020-01-04 stsp }
5055 818c7501 2019-07-11 stsp
5056 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
5057 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
5058 818c7501 2019-07-11 stsp if (err)
5059 818c7501 2019-07-11 stsp goto done;
5060 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
5061 818c7501 2019-07-11 stsp if (err)
5062 818c7501 2019-07-11 stsp goto done;
5063 818c7501 2019-07-11 stsp
5064 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
5065 818c7501 2019-07-11 stsp
5066 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5067 818c7501 2019-07-11 stsp if (err)
5068 818c7501 2019-07-11 stsp goto done;
5069 818c7501 2019-07-11 stsp
5070 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
5071 818c7501 2019-07-11 stsp if (err)
5072 818c7501 2019-07-11 stsp goto done;
5073 818c7501 2019-07-11 stsp
5074 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5075 818c7501 2019-07-11 stsp worktree->base_commit_id);
5076 818c7501 2019-07-11 stsp if (err)
5077 818c7501 2019-07-11 stsp goto done;
5078 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
5079 818c7501 2019-07-11 stsp if (err)
5080 818c7501 2019-07-11 stsp goto done;
5081 818c7501 2019-07-11 stsp
5082 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5083 818c7501 2019-07-11 stsp if (err)
5084 818c7501 2019-07-11 stsp goto done;
5085 818c7501 2019-07-11 stsp done:
5086 818c7501 2019-07-11 stsp free(fileindex_path);
5087 818c7501 2019-07-11 stsp free(tmp_branch_name);
5088 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
5089 818c7501 2019-07-11 stsp free(branch_ref_name);
5090 818c7501 2019-07-11 stsp if (branch_ref)
5091 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5092 818c7501 2019-07-11 stsp if (wt_branch)
5093 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
5094 e51d7b55 2020-01-04 stsp free(wt_branch_tip);
5095 818c7501 2019-07-11 stsp if (err) {
5096 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
5097 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
5098 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
5099 818c7501 2019-07-11 stsp }
5100 818c7501 2019-07-11 stsp if (*tmp_branch) {
5101 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5102 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5103 818c7501 2019-07-11 stsp }
5104 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5105 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5106 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5107 3e3a69f1 2019-07-25 stsp }
5108 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
5109 818c7501 2019-07-11 stsp }
5110 818c7501 2019-07-11 stsp return err;
5111 818c7501 2019-07-11 stsp }
5112 818c7501 2019-07-11 stsp
5113 818c7501 2019-07-11 stsp const struct got_error *
5114 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
5115 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5116 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
5117 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
5118 818c7501 2019-07-11 stsp {
5119 818c7501 2019-07-11 stsp const struct got_error *err;
5120 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5121 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5122 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5123 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5124 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5125 818c7501 2019-07-11 stsp
5126 818c7501 2019-07-11 stsp *commit_id = NULL;
5127 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
5128 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
5129 3e3a69f1 2019-07-25 stsp *branch = NULL;
5130 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5131 3e3a69f1 2019-07-25 stsp
5132 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5133 3e3a69f1 2019-07-25 stsp if (err)
5134 3e3a69f1 2019-07-25 stsp return err;
5135 818c7501 2019-07-11 stsp
5136 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5137 3e3a69f1 2019-07-25 stsp if (err)
5138 f032f1f7 2019-08-04 stsp goto done;
5139 f032f1f7 2019-08-04 stsp
5140 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5141 f032f1f7 2019-08-04 stsp &have_staged_files);
5142 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5143 f032f1f7 2019-08-04 stsp goto done;
5144 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5145 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5146 3e3a69f1 2019-07-25 stsp goto done;
5147 f032f1f7 2019-08-04 stsp }
5148 3e3a69f1 2019-07-25 stsp
5149 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5150 818c7501 2019-07-11 stsp if (err)
5151 f032f1f7 2019-08-04 stsp goto done;
5152 818c7501 2019-07-11 stsp
5153 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
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 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5158 818c7501 2019-07-11 stsp if (err)
5159 818c7501 2019-07-11 stsp goto done;
5160 818c7501 2019-07-11 stsp
5161 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5162 818c7501 2019-07-11 stsp if (err)
5163 818c7501 2019-07-11 stsp goto done;
5164 818c7501 2019-07-11 stsp
5165 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5166 818c7501 2019-07-11 stsp if (err)
5167 818c7501 2019-07-11 stsp goto done;
5168 818c7501 2019-07-11 stsp
5169 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
5170 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
5171 818c7501 2019-07-11 stsp if (err)
5172 818c7501 2019-07-11 stsp goto done;
5173 818c7501 2019-07-11 stsp
5174 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5175 818c7501 2019-07-11 stsp if (err)
5176 818c7501 2019-07-11 stsp goto done;
5177 818c7501 2019-07-11 stsp
5178 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5179 818c7501 2019-07-11 stsp if (err)
5180 818c7501 2019-07-11 stsp goto done;
5181 818c7501 2019-07-11 stsp
5182 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
5183 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
5184 818c7501 2019-07-11 stsp if (err)
5185 818c7501 2019-07-11 stsp goto done;
5186 818c7501 2019-07-11 stsp
5187 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5188 818c7501 2019-07-11 stsp if (err)
5189 818c7501 2019-07-11 stsp goto done;
5190 818c7501 2019-07-11 stsp done:
5191 818c7501 2019-07-11 stsp free(commit_ref_name);
5192 818c7501 2019-07-11 stsp free(branch_ref_name);
5193 3e3a69f1 2019-07-25 stsp free(fileindex_path);
5194 818c7501 2019-07-11 stsp if (commit_ref)
5195 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5196 818c7501 2019-07-11 stsp if (branch_ref)
5197 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
5198 818c7501 2019-07-11 stsp if (err) {
5199 818c7501 2019-07-11 stsp free(*commit_id);
5200 818c7501 2019-07-11 stsp *commit_id = NULL;
5201 818c7501 2019-07-11 stsp if (*tmp_branch) {
5202 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
5203 818c7501 2019-07-11 stsp *tmp_branch = NULL;
5204 818c7501 2019-07-11 stsp }
5205 818c7501 2019-07-11 stsp if (*new_base_branch) {
5206 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
5207 818c7501 2019-07-11 stsp *new_base_branch = NULL;
5208 818c7501 2019-07-11 stsp }
5209 818c7501 2019-07-11 stsp if (*branch) {
5210 818c7501 2019-07-11 stsp got_ref_close(*branch);
5211 818c7501 2019-07-11 stsp *branch = NULL;
5212 818c7501 2019-07-11 stsp }
5213 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5214 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5215 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5216 3e3a69f1 2019-07-25 stsp }
5217 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
5218 818c7501 2019-07-11 stsp }
5219 818c7501 2019-07-11 stsp return err;
5220 c4296144 2019-05-09 stsp }
5221 818c7501 2019-07-11 stsp
5222 818c7501 2019-07-11 stsp const struct got_error *
5223 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5224 818c7501 2019-07-11 stsp {
5225 818c7501 2019-07-11 stsp const struct got_error *err;
5226 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
5227 818c7501 2019-07-11 stsp
5228 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5229 818c7501 2019-07-11 stsp if (err)
5230 818c7501 2019-07-11 stsp return err;
5231 818c7501 2019-07-11 stsp
5232 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5233 818c7501 2019-07-11 stsp free(tmp_branch_name);
5234 818c7501 2019-07-11 stsp return NULL;
5235 818c7501 2019-07-11 stsp }
5236 818c7501 2019-07-11 stsp
5237 818c7501 2019-07-11 stsp static const struct got_error *
5238 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5239 818c7501 2019-07-11 stsp char **logmsg, void *arg)
5240 818c7501 2019-07-11 stsp {
5241 0ebf8283 2019-07-24 stsp *logmsg = arg;
5242 818c7501 2019-07-11 stsp return NULL;
5243 818c7501 2019-07-11 stsp }
5244 818c7501 2019-07-11 stsp
5245 818c7501 2019-07-11 stsp static const struct got_error *
5246 88d0e355 2019-08-03 stsp rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5247 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5248 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5249 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5250 818c7501 2019-07-11 stsp {
5251 818c7501 2019-07-11 stsp return NULL;
5252 01757395 2019-07-12 stsp }
5253 01757395 2019-07-12 stsp
5254 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
5255 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
5256 01757395 2019-07-12 stsp void *progress_arg;
5257 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
5258 01757395 2019-07-12 stsp };
5259 01757395 2019-07-12 stsp
5260 01757395 2019-07-12 stsp static const struct got_error *
5261 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
5262 01757395 2019-07-12 stsp {
5263 01757395 2019-07-12 stsp const struct got_error *err;
5264 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
5265 01757395 2019-07-12 stsp char *p;
5266 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
5267 01757395 2019-07-12 stsp
5268 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
5269 01757395 2019-07-12 stsp if (err)
5270 01757395 2019-07-12 stsp return err;
5271 01757395 2019-07-12 stsp
5272 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
5273 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
5274 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
5275 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
5276 01757395 2019-07-12 stsp return NULL;
5277 01757395 2019-07-12 stsp
5278 01757395 2019-07-12 stsp p = strdup(path);
5279 01757395 2019-07-12 stsp if (p == NULL)
5280 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
5281 01757395 2019-07-12 stsp
5282 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5283 01757395 2019-07-12 stsp if (err || new == NULL)
5284 01757395 2019-07-12 stsp free(p);
5285 01757395 2019-07-12 stsp return err;
5286 01757395 2019-07-12 stsp }
5287 01757395 2019-07-12 stsp
5288 01757395 2019-07-12 stsp void
5289 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5290 01757395 2019-07-12 stsp {
5291 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5292 01757395 2019-07-12 stsp
5293 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
5294 01757395 2019-07-12 stsp free((char *)pe->path);
5295 01757395 2019-07-12 stsp
5296 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
5297 818c7501 2019-07-11 stsp }
5298 818c7501 2019-07-11 stsp
5299 0ebf8283 2019-07-24 stsp static const struct got_error *
5300 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5301 de05890f 2020-03-05 stsp int is_rebase, struct got_repository *repo)
5302 818c7501 2019-07-11 stsp {
5303 818c7501 2019-07-11 stsp const struct got_error *err;
5304 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
5305 818c7501 2019-07-11 stsp
5306 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5307 818c7501 2019-07-11 stsp if (err) {
5308 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
5309 818c7501 2019-07-11 stsp goto done;
5310 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5311 818c7501 2019-07-11 stsp if (err)
5312 818c7501 2019-07-11 stsp goto done;
5313 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
5314 818c7501 2019-07-11 stsp if (err)
5315 818c7501 2019-07-11 stsp goto done;
5316 de05890f 2020-03-05 stsp } else if (is_rebase) {
5317 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
5318 818c7501 2019-07-11 stsp int cmp;
5319 818c7501 2019-07-11 stsp
5320 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
5321 818c7501 2019-07-11 stsp if (err)
5322 818c7501 2019-07-11 stsp goto done;
5323 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
5324 818c7501 2019-07-11 stsp free(stored_id);
5325 818c7501 2019-07-11 stsp if (cmp != 0) {
5326 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5327 818c7501 2019-07-11 stsp goto done;
5328 818c7501 2019-07-11 stsp }
5329 818c7501 2019-07-11 stsp }
5330 0ebf8283 2019-07-24 stsp done:
5331 0ebf8283 2019-07-24 stsp if (commit_ref)
5332 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5333 0ebf8283 2019-07-24 stsp return err;
5334 0ebf8283 2019-07-24 stsp }
5335 0ebf8283 2019-07-24 stsp
5336 0ebf8283 2019-07-24 stsp static const struct got_error *
5337 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
5338 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
5339 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5340 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
5341 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5342 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5343 0ebf8283 2019-07-24 stsp {
5344 0ebf8283 2019-07-24 stsp const struct got_error *err;
5345 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5346 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
5347 3e3a69f1 2019-07-25 stsp char *fileindex_path;
5348 818c7501 2019-07-11 stsp
5349 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5350 0ebf8283 2019-07-24 stsp
5351 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5352 0ebf8283 2019-07-24 stsp if (err)
5353 0ebf8283 2019-07-24 stsp return err;
5354 0ebf8283 2019-07-24 stsp
5355 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
5356 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
5357 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
5358 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
5359 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
5360 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
5361 818c7501 2019-07-11 stsp if (commit_ref)
5362 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
5363 818c7501 2019-07-11 stsp return err;
5364 818c7501 2019-07-11 stsp }
5365 818c7501 2019-07-11 stsp
5366 818c7501 2019-07-11 stsp const struct got_error *
5367 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5368 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5369 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5370 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5371 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5372 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5373 818c7501 2019-07-11 stsp {
5374 0ebf8283 2019-07-24 stsp const struct got_error *err;
5375 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5376 0ebf8283 2019-07-24 stsp
5377 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5378 0ebf8283 2019-07-24 stsp if (err)
5379 0ebf8283 2019-07-24 stsp return err;
5380 0ebf8283 2019-07-24 stsp
5381 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5382 0ebf8283 2019-07-24 stsp if (err)
5383 0ebf8283 2019-07-24 stsp goto done;
5384 0ebf8283 2019-07-24 stsp
5385 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5386 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5387 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5388 0ebf8283 2019-07-24 stsp done:
5389 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5390 0ebf8283 2019-07-24 stsp return err;
5391 0ebf8283 2019-07-24 stsp }
5392 0ebf8283 2019-07-24 stsp
5393 0ebf8283 2019-07-24 stsp const struct got_error *
5394 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5395 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5396 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5397 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
5398 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
5399 e6209546 2019-08-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
5400 0ebf8283 2019-07-24 stsp {
5401 0ebf8283 2019-07-24 stsp const struct got_error *err;
5402 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5403 0ebf8283 2019-07-24 stsp
5404 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5405 0ebf8283 2019-07-24 stsp if (err)
5406 0ebf8283 2019-07-24 stsp return err;
5407 0ebf8283 2019-07-24 stsp
5408 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5409 0ebf8283 2019-07-24 stsp if (err)
5410 0ebf8283 2019-07-24 stsp goto done;
5411 0ebf8283 2019-07-24 stsp
5412 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5413 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
5414 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
5415 0ebf8283 2019-07-24 stsp done:
5416 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5417 0ebf8283 2019-07-24 stsp return err;
5418 0ebf8283 2019-07-24 stsp }
5419 0ebf8283 2019-07-24 stsp
5420 0ebf8283 2019-07-24 stsp static const struct got_error *
5421 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
5422 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5423 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5424 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5425 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
5426 0ebf8283 2019-07-24 stsp {
5427 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
5428 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
5429 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
5430 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5431 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
5432 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
5433 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
5434 818c7501 2019-07-11 stsp
5435 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
5436 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
5437 a0e95631 2019-07-12 stsp
5438 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
5439 818c7501 2019-07-11 stsp
5440 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5441 a0e95631 2019-07-12 stsp if (err)
5442 3e3a69f1 2019-07-25 stsp return err;
5443 a0e95631 2019-07-12 stsp
5444 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
5445 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
5446 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
5447 5f8a88c6 2019-08-03 stsp cc_arg.have_staged_files = 0;
5448 01757395 2019-07-12 stsp /*
5449 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
5450 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
5451 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
5452 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
5453 01757395 2019-07-12 stsp */
5454 01757395 2019-07-12 stsp if (merged_paths) {
5455 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
5456 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
5457 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
5458 f2a9dc41 2019-12-13 tracey repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5459 f2a9dc41 2019-12-13 tracey 0);
5460 01757395 2019-07-12 stsp if (err)
5461 01757395 2019-07-12 stsp goto done;
5462 01757395 2019-07-12 stsp }
5463 01757395 2019-07-12 stsp } else {
5464 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5465 f2a9dc41 2019-12-13 tracey collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5466 01757395 2019-07-12 stsp if (err)
5467 01757395 2019-07-12 stsp goto done;
5468 01757395 2019-07-12 stsp }
5469 a0e95631 2019-07-12 stsp
5470 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
5471 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
5472 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5473 a0e95631 2019-07-12 stsp if (err)
5474 a0e95631 2019-07-12 stsp goto done;
5475 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5476 a0e95631 2019-07-12 stsp goto done;
5477 ff0d2220 2019-07-11 stsp }
5478 818c7501 2019-07-11 stsp
5479 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5480 edd02c5e 2019-07-11 stsp if (err)
5481 edd02c5e 2019-07-11 stsp goto done;
5482 edd02c5e 2019-07-11 stsp
5483 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
5484 818c7501 2019-07-11 stsp if (err)
5485 818c7501 2019-07-11 stsp goto done;
5486 818c7501 2019-07-11 stsp
5487 5943eee2 2019-08-13 stsp if (new_logmsg) {
5488 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
5489 5943eee2 2019-08-13 stsp if (logmsg == NULL) {
5490 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5491 5943eee2 2019-08-13 stsp goto done;
5492 5943eee2 2019-08-13 stsp }
5493 5943eee2 2019-08-13 stsp } else {
5494 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5495 5943eee2 2019-08-13 stsp if (err)
5496 5943eee2 2019-08-13 stsp goto done;
5497 5943eee2 2019-08-13 stsp }
5498 0ebf8283 2019-07-24 stsp
5499 5943eee2 2019-08-13 stsp /* NB: commit_worktree will call free(logmsg) */
5500 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5501 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
5502 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
5503 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5504 a0e95631 2019-07-12 stsp if (err)
5505 a0e95631 2019-07-12 stsp goto done;
5506 a0e95631 2019-07-12 stsp
5507 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
5508 a0e95631 2019-07-12 stsp if (err)
5509 a0e95631 2019-07-12 stsp goto done;
5510 a0e95631 2019-07-12 stsp
5511 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
5512 a0e95631 2019-07-12 stsp if (err)
5513 a0e95631 2019-07-12 stsp goto done;
5514 a0e95631 2019-07-12 stsp
5515 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5516 72fd46fa 2019-09-06 stsp fileindex, 0);
5517 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5518 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
5519 a0e95631 2019-07-12 stsp err = sync_err;
5520 818c7501 2019-07-11 stsp done:
5521 a0e95631 2019-07-12 stsp free(fileindex_path);
5522 a0e95631 2019-07-12 stsp free(head_commit_id);
5523 a0e95631 2019-07-12 stsp if (head_ref)
5524 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
5525 818c7501 2019-07-11 stsp if (err) {
5526 818c7501 2019-07-11 stsp free(*new_commit_id);
5527 818c7501 2019-07-11 stsp *new_commit_id = NULL;
5528 818c7501 2019-07-11 stsp }
5529 818c7501 2019-07-11 stsp return err;
5530 818c7501 2019-07-11 stsp }
5531 818c7501 2019-07-11 stsp
5532 818c7501 2019-07-11 stsp const struct got_error *
5533 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5534 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5535 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5536 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5537 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
5538 0ebf8283 2019-07-24 stsp {
5539 0ebf8283 2019-07-24 stsp const struct got_error *err;
5540 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5541 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5542 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5543 0ebf8283 2019-07-24 stsp
5544 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5545 0ebf8283 2019-07-24 stsp if (err)
5546 0ebf8283 2019-07-24 stsp return err;
5547 0ebf8283 2019-07-24 stsp
5548 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5549 0ebf8283 2019-07-24 stsp if (err)
5550 0ebf8283 2019-07-24 stsp goto done;
5551 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
5552 0ebf8283 2019-07-24 stsp if (err)
5553 0ebf8283 2019-07-24 stsp goto done;
5554 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5555 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
5556 0ebf8283 2019-07-24 stsp goto done;
5557 0ebf8283 2019-07-24 stsp }
5558 0ebf8283 2019-07-24 stsp
5559 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5560 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5561 0ebf8283 2019-07-24 stsp done:
5562 0ebf8283 2019-07-24 stsp if (commit_ref)
5563 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5564 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5565 0ebf8283 2019-07-24 stsp free(commit_id);
5566 0ebf8283 2019-07-24 stsp return err;
5567 0ebf8283 2019-07-24 stsp }
5568 0ebf8283 2019-07-24 stsp
5569 0ebf8283 2019-07-24 stsp const struct got_error *
5570 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5571 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5572 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5573 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
5574 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
5575 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5576 0ebf8283 2019-07-24 stsp {
5577 0ebf8283 2019-07-24 stsp const struct got_error *err;
5578 0ebf8283 2019-07-24 stsp char *commit_ref_name;
5579 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5580 0ebf8283 2019-07-24 stsp
5581 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5582 0ebf8283 2019-07-24 stsp if (err)
5583 0ebf8283 2019-07-24 stsp return err;
5584 0ebf8283 2019-07-24 stsp
5585 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5586 0ebf8283 2019-07-24 stsp if (err)
5587 0ebf8283 2019-07-24 stsp goto done;
5588 0ebf8283 2019-07-24 stsp
5589 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5590 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5591 0ebf8283 2019-07-24 stsp done:
5592 0ebf8283 2019-07-24 stsp if (commit_ref)
5593 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
5594 0ebf8283 2019-07-24 stsp free(commit_ref_name);
5595 0ebf8283 2019-07-24 stsp return err;
5596 0ebf8283 2019-07-24 stsp }
5597 0ebf8283 2019-07-24 stsp
5598 0ebf8283 2019-07-24 stsp const struct got_error *
5599 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
5600 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5601 818c7501 2019-07-11 stsp {
5602 3e3a69f1 2019-07-25 stsp if (fileindex)
5603 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5604 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
5605 69844fba 2019-07-11 stsp }
5606 69844fba 2019-07-11 stsp
5607 69844fba 2019-07-11 stsp static const struct got_error *
5608 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
5609 69844fba 2019-07-11 stsp {
5610 69844fba 2019-07-11 stsp const struct got_error *err;
5611 69844fba 2019-07-11 stsp struct got_reference *ref;
5612 69844fba 2019-07-11 stsp
5613 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
5614 69844fba 2019-07-11 stsp if (err) {
5615 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
5616 69844fba 2019-07-11 stsp return NULL;
5617 69844fba 2019-07-11 stsp return err;
5618 69844fba 2019-07-11 stsp }
5619 69844fba 2019-07-11 stsp
5620 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
5621 69844fba 2019-07-11 stsp got_ref_close(ref);
5622 69844fba 2019-07-11 stsp return err;
5623 818c7501 2019-07-11 stsp }
5624 818c7501 2019-07-11 stsp
5625 69844fba 2019-07-11 stsp static const struct got_error *
5626 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5627 69844fba 2019-07-11 stsp {
5628 69844fba 2019-07-11 stsp const struct got_error *err;
5629 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5630 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
5631 69844fba 2019-07-11 stsp
5632 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5633 69844fba 2019-07-11 stsp if (err)
5634 69844fba 2019-07-11 stsp goto done;
5635 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
5636 69844fba 2019-07-11 stsp if (err)
5637 69844fba 2019-07-11 stsp goto done;
5638 69844fba 2019-07-11 stsp
5639 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5640 69844fba 2019-07-11 stsp if (err)
5641 69844fba 2019-07-11 stsp goto done;
5642 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
5643 69844fba 2019-07-11 stsp if (err)
5644 69844fba 2019-07-11 stsp goto done;
5645 69844fba 2019-07-11 stsp
5646 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5647 69844fba 2019-07-11 stsp if (err)
5648 69844fba 2019-07-11 stsp goto done;
5649 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
5650 69844fba 2019-07-11 stsp if (err)
5651 69844fba 2019-07-11 stsp goto done;
5652 69844fba 2019-07-11 stsp
5653 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5654 69844fba 2019-07-11 stsp if (err)
5655 69844fba 2019-07-11 stsp goto done;
5656 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
5657 69844fba 2019-07-11 stsp if (err)
5658 69844fba 2019-07-11 stsp goto done;
5659 69844fba 2019-07-11 stsp
5660 69844fba 2019-07-11 stsp done:
5661 69844fba 2019-07-11 stsp free(tmp_branch_name);
5662 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
5663 69844fba 2019-07-11 stsp free(branch_ref_name);
5664 69844fba 2019-07-11 stsp free(commit_ref_name);
5665 69844fba 2019-07-11 stsp return err;
5666 69844fba 2019-07-11 stsp }
5667 69844fba 2019-07-11 stsp
5668 818c7501 2019-07-11 stsp const struct got_error *
5669 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
5670 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
5671 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
5672 818c7501 2019-07-11 stsp struct got_repository *repo)
5673 818c7501 2019-07-11 stsp {
5674 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
5675 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
5676 818c7501 2019-07-11 stsp
5677 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
5678 818c7501 2019-07-11 stsp if (err)
5679 818c7501 2019-07-11 stsp return err;
5680 818c7501 2019-07-11 stsp
5681 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
5682 818c7501 2019-07-11 stsp if (err)
5683 818c7501 2019-07-11 stsp goto done;
5684 818c7501 2019-07-11 stsp
5685 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
5686 818c7501 2019-07-11 stsp if (err)
5687 818c7501 2019-07-11 stsp goto done;
5688 818c7501 2019-07-11 stsp
5689 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
5690 818c7501 2019-07-11 stsp if (err)
5691 818c7501 2019-07-11 stsp goto done;
5692 818c7501 2019-07-11 stsp
5693 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
5694 818c7501 2019-07-11 stsp done:
5695 3e3a69f1 2019-07-25 stsp if (fileindex)
5696 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5697 818c7501 2019-07-11 stsp free(new_head_commit_id);
5698 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5699 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
5700 818c7501 2019-07-11 stsp err = unlockerr;
5701 818c7501 2019-07-11 stsp return err;
5702 818c7501 2019-07-11 stsp }
5703 818c7501 2019-07-11 stsp
5704 818c7501 2019-07-11 stsp const struct got_error *
5705 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
5706 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
5707 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
5708 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
5709 818c7501 2019-07-11 stsp {
5710 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
5711 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
5712 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
5713 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
5714 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
5715 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
5716 818c7501 2019-07-11 stsp
5717 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
5718 818c7501 2019-07-11 stsp if (err)
5719 818c7501 2019-07-11 stsp return err;
5720 818c7501 2019-07-11 stsp
5721 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
5722 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
5723 818c7501 2019-07-11 stsp if (err)
5724 818c7501 2019-07-11 stsp goto done;
5725 818c7501 2019-07-11 stsp
5726 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
5727 818c7501 2019-07-11 stsp if (err)
5728 818c7501 2019-07-11 stsp goto done;
5729 818c7501 2019-07-11 stsp
5730 818c7501 2019-07-11 stsp /*
5731 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
5732 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
5733 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
5734 818c7501 2019-07-11 stsp */
5735 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
5736 818c7501 2019-07-11 stsp if (err)
5737 818c7501 2019-07-11 stsp goto done;
5738 818c7501 2019-07-11 stsp
5739 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
5740 818c7501 2019-07-11 stsp if (err)
5741 818c7501 2019-07-11 stsp goto done;
5742 818c7501 2019-07-11 stsp
5743 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
5744 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
5745 ca355955 2019-07-12 stsp if (err)
5746 ca355955 2019-07-12 stsp goto done;
5747 ca355955 2019-07-12 stsp
5748 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
5749 ca355955 2019-07-12 stsp if (err)
5750 ca355955 2019-07-12 stsp goto done;
5751 ca355955 2019-07-12 stsp
5752 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
5753 818c7501 2019-07-11 stsp if (err)
5754 818c7501 2019-07-11 stsp goto done;
5755 818c7501 2019-07-11 stsp
5756 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
5757 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
5758 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
5759 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
5760 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
5761 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
5762 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
5763 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
5764 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
5765 55bd499d 2019-07-12 stsp if (err)
5766 1f1abb7e 2019-08-08 stsp goto sync;
5767 55bd499d 2019-07-12 stsp
5768 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
5769 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
5770 ca355955 2019-07-12 stsp sync:
5771 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
5772 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
5773 ca355955 2019-07-12 stsp err = sync_err;
5774 818c7501 2019-07-11 stsp done:
5775 818c7501 2019-07-11 stsp got_ref_close(resolved);
5776 a3a2faf2 2019-07-12 stsp free(tree_id);
5777 818c7501 2019-07-11 stsp free(commit_id);
5778 0ebf8283 2019-07-24 stsp if (fileindex)
5779 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
5780 0ebf8283 2019-07-24 stsp free(fileindex_path);
5781 0ebf8283 2019-07-24 stsp
5782 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
5783 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
5784 0ebf8283 2019-07-24 stsp err = unlockerr;
5785 0ebf8283 2019-07-24 stsp return err;
5786 0ebf8283 2019-07-24 stsp }
5787 0ebf8283 2019-07-24 stsp
5788 0ebf8283 2019-07-24 stsp const struct got_error *
5789 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
5790 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
5791 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
5792 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5793 0ebf8283 2019-07-24 stsp {
5794 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5795 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5796 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
5797 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
5798 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
5799 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
5800 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
5801 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5802 0ebf8283 2019-07-24 stsp
5803 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5804 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5805 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5806 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5807 0ebf8283 2019-07-24 stsp
5808 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
5809 0ebf8283 2019-07-24 stsp if (err)
5810 0ebf8283 2019-07-24 stsp return err;
5811 0ebf8283 2019-07-24 stsp
5812 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5813 0ebf8283 2019-07-24 stsp if (err)
5814 0ebf8283 2019-07-24 stsp goto done;
5815 0ebf8283 2019-07-24 stsp
5816 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
5817 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
5818 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5819 0ebf8283 2019-07-24 stsp &ok_arg);
5820 0ebf8283 2019-07-24 stsp if (err)
5821 0ebf8283 2019-07-24 stsp goto done;
5822 0ebf8283 2019-07-24 stsp
5823 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5824 0ebf8283 2019-07-24 stsp if (err)
5825 0ebf8283 2019-07-24 stsp goto done;
5826 0ebf8283 2019-07-24 stsp
5827 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5828 0ebf8283 2019-07-24 stsp if (err)
5829 0ebf8283 2019-07-24 stsp goto done;
5830 0ebf8283 2019-07-24 stsp
5831 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5832 0ebf8283 2019-07-24 stsp worktree);
5833 0ebf8283 2019-07-24 stsp if (err)
5834 0ebf8283 2019-07-24 stsp goto done;
5835 0ebf8283 2019-07-24 stsp
5836 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5837 0ebf8283 2019-07-24 stsp 0);
5838 0ebf8283 2019-07-24 stsp if (err)
5839 0ebf8283 2019-07-24 stsp goto done;
5840 0ebf8283 2019-07-24 stsp
5841 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
5842 0ebf8283 2019-07-24 stsp if (err)
5843 0ebf8283 2019-07-24 stsp goto done;
5844 0ebf8283 2019-07-24 stsp
5845 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
5846 0ebf8283 2019-07-24 stsp if (err)
5847 0ebf8283 2019-07-24 stsp goto done;
5848 0ebf8283 2019-07-24 stsp
5849 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
5850 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5851 0ebf8283 2019-07-24 stsp if (err)
5852 0ebf8283 2019-07-24 stsp goto done;
5853 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
5854 0ebf8283 2019-07-24 stsp if (err)
5855 0ebf8283 2019-07-24 stsp goto done;
5856 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
5857 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
5858 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
5859 0ebf8283 2019-07-24 stsp goto done;
5860 0ebf8283 2019-07-24 stsp }
5861 0ebf8283 2019-07-24 stsp
5862 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
5863 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
5864 0ebf8283 2019-07-24 stsp if (err)
5865 0ebf8283 2019-07-24 stsp goto done;
5866 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
5867 0ebf8283 2019-07-24 stsp if (err)
5868 0ebf8283 2019-07-24 stsp goto done;
5869 0ebf8283 2019-07-24 stsp
5870 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
5871 0ebf8283 2019-07-24 stsp if (err)
5872 0ebf8283 2019-07-24 stsp goto done;
5873 0ebf8283 2019-07-24 stsp done:
5874 0ebf8283 2019-07-24 stsp free(fileindex_path);
5875 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5876 0ebf8283 2019-07-24 stsp free(branch_ref_name);
5877 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
5878 0ebf8283 2019-07-24 stsp if (wt_branch)
5879 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
5880 0ebf8283 2019-07-24 stsp if (err) {
5881 0ebf8283 2019-07-24 stsp if (*branch_ref) {
5882 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
5883 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
5884 0ebf8283 2019-07-24 stsp }
5885 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
5886 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
5887 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5888 0ebf8283 2019-07-24 stsp }
5889 0ebf8283 2019-07-24 stsp free(*base_commit_id);
5890 3e3a69f1 2019-07-25 stsp if (*fileindex) {
5891 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
5892 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5893 3e3a69f1 2019-07-25 stsp }
5894 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
5895 0ebf8283 2019-07-24 stsp }
5896 0ebf8283 2019-07-24 stsp return err;
5897 0ebf8283 2019-07-24 stsp }
5898 0ebf8283 2019-07-24 stsp
5899 0ebf8283 2019-07-24 stsp const struct got_error *
5900 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
5901 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
5902 0ebf8283 2019-07-24 stsp {
5903 3e3a69f1 2019-07-25 stsp if (fileindex)
5904 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
5905 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
5906 0ebf8283 2019-07-24 stsp }
5907 0ebf8283 2019-07-24 stsp
5908 0ebf8283 2019-07-24 stsp const struct got_error *
5909 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
5910 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
5911 0ebf8283 2019-07-24 stsp {
5912 0ebf8283 2019-07-24 stsp const struct got_error *err;
5913 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
5914 0ebf8283 2019-07-24 stsp
5915 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5916 0ebf8283 2019-07-24 stsp if (err)
5917 0ebf8283 2019-07-24 stsp return err;
5918 0ebf8283 2019-07-24 stsp
5919 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5920 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
5921 0ebf8283 2019-07-24 stsp return NULL;
5922 0ebf8283 2019-07-24 stsp }
5923 0ebf8283 2019-07-24 stsp
5924 0ebf8283 2019-07-24 stsp const struct got_error *
5925 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
5926 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
5927 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
5928 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5929 0ebf8283 2019-07-24 stsp {
5930 0ebf8283 2019-07-24 stsp const struct got_error *err;
5931 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
5932 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5933 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
5934 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
5935 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
5936 f032f1f7 2019-08-04 stsp int have_staged_files = 0;
5937 0ebf8283 2019-07-24 stsp
5938 0ebf8283 2019-07-24 stsp *commit_id = NULL;
5939 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
5940 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
5941 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
5942 0ebf8283 2019-07-24 stsp
5943 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
5944 3e3a69f1 2019-07-25 stsp if (err)
5945 3e3a69f1 2019-07-25 stsp return err;
5946 3e3a69f1 2019-07-25 stsp
5947 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
5948 3e3a69f1 2019-07-25 stsp if (err)
5949 f032f1f7 2019-08-04 stsp goto done;
5950 f032f1f7 2019-08-04 stsp
5951 f032f1f7 2019-08-04 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5952 f032f1f7 2019-08-04 stsp &have_staged_files);
5953 f032f1f7 2019-08-04 stsp if (err && err->code != GOT_ERR_CANCELLED)
5954 f032f1f7 2019-08-04 stsp goto done;
5955 f032f1f7 2019-08-04 stsp if (have_staged_files) {
5956 f032f1f7 2019-08-04 stsp err = got_error(GOT_ERR_STAGED_PATHS);
5957 3e3a69f1 2019-07-25 stsp goto done;
5958 f032f1f7 2019-08-04 stsp }
5959 3e3a69f1 2019-07-25 stsp
5960 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
5961 0ebf8283 2019-07-24 stsp if (err)
5962 f032f1f7 2019-08-04 stsp goto done;
5963 0ebf8283 2019-07-24 stsp
5964 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
5965 0ebf8283 2019-07-24 stsp if (err)
5966 0ebf8283 2019-07-24 stsp goto done;
5967 0ebf8283 2019-07-24 stsp
5968 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5969 0ebf8283 2019-07-24 stsp if (err)
5970 0ebf8283 2019-07-24 stsp goto done;
5971 0ebf8283 2019-07-24 stsp
5972 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
5973 0ebf8283 2019-07-24 stsp worktree);
5974 0ebf8283 2019-07-24 stsp if (err)
5975 0ebf8283 2019-07-24 stsp goto done;
5976 0ebf8283 2019-07-24 stsp
5977 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
5978 0ebf8283 2019-07-24 stsp if (err)
5979 0ebf8283 2019-07-24 stsp goto done;
5980 0ebf8283 2019-07-24 stsp
5981 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5982 0ebf8283 2019-07-24 stsp if (err)
5983 0ebf8283 2019-07-24 stsp goto done;
5984 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
5985 0ebf8283 2019-07-24 stsp if (err)
5986 0ebf8283 2019-07-24 stsp goto done;
5987 0ebf8283 2019-07-24 stsp
5988 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
5989 0ebf8283 2019-07-24 stsp if (err)
5990 0ebf8283 2019-07-24 stsp goto done;
5991 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
5992 0ebf8283 2019-07-24 stsp if (err)
5993 0ebf8283 2019-07-24 stsp goto done;
5994 0ebf8283 2019-07-24 stsp
5995 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5996 0ebf8283 2019-07-24 stsp if (err)
5997 0ebf8283 2019-07-24 stsp goto done;
5998 0ebf8283 2019-07-24 stsp done:
5999 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6000 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6001 3e3a69f1 2019-07-25 stsp free(fileindex_path);
6002 0ebf8283 2019-07-24 stsp if (commit_ref)
6003 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
6004 0ebf8283 2019-07-24 stsp if (base_commit_ref)
6005 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
6006 0ebf8283 2019-07-24 stsp if (err) {
6007 0ebf8283 2019-07-24 stsp free(*commit_id);
6008 0ebf8283 2019-07-24 stsp *commit_id = NULL;
6009 0ebf8283 2019-07-24 stsp free(*base_commit_id);
6010 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
6011 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
6012 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
6013 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
6014 0ebf8283 2019-07-24 stsp }
6015 3e3a69f1 2019-07-25 stsp if (*fileindex) {
6016 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
6017 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
6018 3e3a69f1 2019-07-25 stsp }
6019 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
6020 0ebf8283 2019-07-24 stsp }
6021 0ebf8283 2019-07-24 stsp return err;
6022 0ebf8283 2019-07-24 stsp }
6023 0ebf8283 2019-07-24 stsp
6024 0ebf8283 2019-07-24 stsp static const struct got_error *
6025 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6026 0ebf8283 2019-07-24 stsp {
6027 0ebf8283 2019-07-24 stsp const struct got_error *err;
6028 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6029 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
6030 0ebf8283 2019-07-24 stsp
6031 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6032 0ebf8283 2019-07-24 stsp if (err)
6033 0ebf8283 2019-07-24 stsp goto done;
6034 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
6035 0ebf8283 2019-07-24 stsp if (err)
6036 0ebf8283 2019-07-24 stsp goto done;
6037 0ebf8283 2019-07-24 stsp
6038 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6039 0ebf8283 2019-07-24 stsp worktree);
6040 0ebf8283 2019-07-24 stsp if (err)
6041 0ebf8283 2019-07-24 stsp goto done;
6042 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
6043 0ebf8283 2019-07-24 stsp if (err)
6044 0ebf8283 2019-07-24 stsp goto done;
6045 0ebf8283 2019-07-24 stsp
6046 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6047 0ebf8283 2019-07-24 stsp if (err)
6048 0ebf8283 2019-07-24 stsp goto done;
6049 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
6050 0ebf8283 2019-07-24 stsp if (err)
6051 0ebf8283 2019-07-24 stsp goto done;
6052 0ebf8283 2019-07-24 stsp
6053 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6054 0ebf8283 2019-07-24 stsp if (err)
6055 0ebf8283 2019-07-24 stsp goto done;
6056 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
6057 0ebf8283 2019-07-24 stsp if (err)
6058 0ebf8283 2019-07-24 stsp goto done;
6059 0ebf8283 2019-07-24 stsp done:
6060 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
6061 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
6062 0ebf8283 2019-07-24 stsp free(branch_ref_name);
6063 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6064 0ebf8283 2019-07-24 stsp return err;
6065 0ebf8283 2019-07-24 stsp }
6066 0ebf8283 2019-07-24 stsp
6067 0ebf8283 2019-07-24 stsp const struct got_error *
6068 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
6069 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6070 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
6071 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
6072 0ebf8283 2019-07-24 stsp {
6073 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
6074 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6075 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
6076 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
6077 1f1abb7e 2019-08-08 stsp struct revert_file_args rfa;
6078 0ebf8283 2019-07-24 stsp
6079 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
6080 0ebf8283 2019-07-24 stsp if (err)
6081 0ebf8283 2019-07-24 stsp return err;
6082 0ebf8283 2019-07-24 stsp
6083 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6084 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
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_worktree_set_head_ref(worktree, resolved);
6089 0ebf8283 2019-07-24 stsp if (err)
6090 0ebf8283 2019-07-24 stsp goto done;
6091 0ebf8283 2019-07-24 stsp
6092 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6093 0ebf8283 2019-07-24 stsp if (err)
6094 0ebf8283 2019-07-24 stsp goto done;
6095 0ebf8283 2019-07-24 stsp
6096 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6097 0ebf8283 2019-07-24 stsp worktree->path_prefix);
6098 0ebf8283 2019-07-24 stsp if (err)
6099 0ebf8283 2019-07-24 stsp goto done;
6100 0ebf8283 2019-07-24 stsp
6101 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6102 0ebf8283 2019-07-24 stsp if (err)
6103 0ebf8283 2019-07-24 stsp goto done;
6104 0ebf8283 2019-07-24 stsp
6105 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
6106 0ebf8283 2019-07-24 stsp if (err)
6107 0ebf8283 2019-07-24 stsp goto done;
6108 0ebf8283 2019-07-24 stsp
6109 1f1abb7e 2019-08-08 stsp rfa.worktree = worktree;
6110 1f1abb7e 2019-08-08 stsp rfa.fileindex = fileindex;
6111 1f1abb7e 2019-08-08 stsp rfa.progress_cb = progress_cb;
6112 1f1abb7e 2019-08-08 stsp rfa.progress_arg = progress_arg;
6113 33aa809d 2019-08-08 stsp rfa.patch_cb = NULL;
6114 33aa809d 2019-08-08 stsp rfa.patch_arg = NULL;
6115 1f1abb7e 2019-08-08 stsp rfa.repo = repo;
6116 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
6117 f2a9dc41 2019-12-13 tracey revert_file, &rfa, NULL, NULL, 0, 0);
6118 0ebf8283 2019-07-24 stsp if (err)
6119 1f1abb7e 2019-08-08 stsp goto sync;
6120 0ebf8283 2019-07-24 stsp
6121 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6122 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
6123 0ebf8283 2019-07-24 stsp sync:
6124 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6125 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
6126 0ebf8283 2019-07-24 stsp err = sync_err;
6127 0ebf8283 2019-07-24 stsp done:
6128 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
6129 0ebf8283 2019-07-24 stsp free(tree_id);
6130 818c7501 2019-07-11 stsp free(fileindex_path);
6131 818c7501 2019-07-11 stsp
6132 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6133 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
6134 818c7501 2019-07-11 stsp err = unlockerr;
6135 818c7501 2019-07-11 stsp return err;
6136 818c7501 2019-07-11 stsp }
6137 0ebf8283 2019-07-24 stsp
6138 0ebf8283 2019-07-24 stsp const struct got_error *
6139 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
6140 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6141 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
6142 0ebf8283 2019-07-24 stsp {
6143 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
6144 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
6145 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
6146 0ebf8283 2019-07-24 stsp
6147 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6148 0ebf8283 2019-07-24 stsp if (err)
6149 0ebf8283 2019-07-24 stsp return err;
6150 0ebf8283 2019-07-24 stsp
6151 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
6152 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
6153 0ebf8283 2019-07-24 stsp if (err)
6154 0ebf8283 2019-07-24 stsp goto done;
6155 0ebf8283 2019-07-24 stsp
6156 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
6157 0ebf8283 2019-07-24 stsp if (err)
6158 0ebf8283 2019-07-24 stsp goto done;
6159 0ebf8283 2019-07-24 stsp
6160 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
6161 0ebf8283 2019-07-24 stsp if (err)
6162 0ebf8283 2019-07-24 stsp goto done;
6163 0ebf8283 2019-07-24 stsp
6164 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
6165 0ebf8283 2019-07-24 stsp if (err)
6166 0ebf8283 2019-07-24 stsp goto done;
6167 0ebf8283 2019-07-24 stsp
6168 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
6169 0ebf8283 2019-07-24 stsp done:
6170 3e3a69f1 2019-07-25 stsp if (fileindex)
6171 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
6172 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
6173 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6174 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
6175 0ebf8283 2019-07-24 stsp err = unlockerr;
6176 0ebf8283 2019-07-24 stsp return err;
6177 0ebf8283 2019-07-24 stsp }
6178 0ebf8283 2019-07-24 stsp
6179 0ebf8283 2019-07-24 stsp const struct got_error *
6180 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6181 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6182 0ebf8283 2019-07-24 stsp {
6183 0ebf8283 2019-07-24 stsp const struct got_error *err;
6184 0ebf8283 2019-07-24 stsp char *commit_ref_name;
6185 0ebf8283 2019-07-24 stsp
6186 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6187 0ebf8283 2019-07-24 stsp if (err)
6188 0ebf8283 2019-07-24 stsp return err;
6189 0ebf8283 2019-07-24 stsp
6190 de05890f 2020-03-05 stsp err = store_commit_id(commit_ref_name, commit_id, 0, repo);
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_ref(commit_ref_name, repo);
6195 0ebf8283 2019-07-24 stsp done:
6196 0ebf8283 2019-07-24 stsp free(commit_ref_name);
6197 2822a352 2019-10-15 stsp return err;
6198 2822a352 2019-10-15 stsp }
6199 2822a352 2019-10-15 stsp
6200 2822a352 2019-10-15 stsp const struct got_error *
6201 2822a352 2019-10-15 stsp got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6202 2822a352 2019-10-15 stsp struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6203 2822a352 2019-10-15 stsp struct got_worktree *worktree, const char *refname,
6204 2822a352 2019-10-15 stsp struct got_repository *repo)
6205 2822a352 2019-10-15 stsp {
6206 2822a352 2019-10-15 stsp const struct got_error *err = NULL;
6207 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6208 2822a352 2019-10-15 stsp struct check_rebase_ok_arg ok_arg;
6209 2822a352 2019-10-15 stsp
6210 2822a352 2019-10-15 stsp *fileindex = NULL;
6211 2822a352 2019-10-15 stsp *branch_ref = NULL;
6212 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6213 2822a352 2019-10-15 stsp
6214 2822a352 2019-10-15 stsp err = lock_worktree(worktree, LOCK_EX);
6215 2822a352 2019-10-15 stsp if (err)
6216 2822a352 2019-10-15 stsp return err;
6217 2822a352 2019-10-15 stsp
6218 2822a352 2019-10-15 stsp if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6219 2822a352 2019-10-15 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6220 2822a352 2019-10-15 stsp "cannot integrate a branch into itself; "
6221 2822a352 2019-10-15 stsp "update -b or different branch name required");
6222 2822a352 2019-10-15 stsp goto done;
6223 2822a352 2019-10-15 stsp }
6224 2822a352 2019-10-15 stsp
6225 2822a352 2019-10-15 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
6226 2822a352 2019-10-15 stsp if (err)
6227 2822a352 2019-10-15 stsp goto done;
6228 2822a352 2019-10-15 stsp
6229 2822a352 2019-10-15 stsp /* Preconditions are the same as for rebase. */
6230 2822a352 2019-10-15 stsp ok_arg.worktree = worktree;
6231 2822a352 2019-10-15 stsp ok_arg.repo = repo;
6232 2822a352 2019-10-15 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6233 2822a352 2019-10-15 stsp &ok_arg);
6234 2822a352 2019-10-15 stsp if (err)
6235 2822a352 2019-10-15 stsp goto done;
6236 2822a352 2019-10-15 stsp
6237 2822a352 2019-10-15 stsp err = got_ref_open(branch_ref, repo, refname, 1);
6238 2822a352 2019-10-15 stsp if (err)
6239 2822a352 2019-10-15 stsp goto done;
6240 2822a352 2019-10-15 stsp
6241 2822a352 2019-10-15 stsp err = got_ref_open(base_branch_ref, repo,
6242 2822a352 2019-10-15 stsp got_worktree_get_head_ref_name(worktree), 1);
6243 2822a352 2019-10-15 stsp done:
6244 2822a352 2019-10-15 stsp if (err) {
6245 2822a352 2019-10-15 stsp if (*branch_ref) {
6246 2822a352 2019-10-15 stsp got_ref_close(*branch_ref);
6247 2822a352 2019-10-15 stsp *branch_ref = NULL;
6248 2822a352 2019-10-15 stsp }
6249 2822a352 2019-10-15 stsp if (*base_branch_ref) {
6250 2822a352 2019-10-15 stsp got_ref_close(*base_branch_ref);
6251 2822a352 2019-10-15 stsp *base_branch_ref = NULL;
6252 2822a352 2019-10-15 stsp }
6253 2822a352 2019-10-15 stsp if (*fileindex) {
6254 2822a352 2019-10-15 stsp got_fileindex_free(*fileindex);
6255 2822a352 2019-10-15 stsp *fileindex = NULL;
6256 2822a352 2019-10-15 stsp }
6257 2822a352 2019-10-15 stsp lock_worktree(worktree, LOCK_SH);
6258 2822a352 2019-10-15 stsp }
6259 0cb83759 2019-08-03 stsp return err;
6260 0cb83759 2019-08-03 stsp }
6261 0cb83759 2019-08-03 stsp
6262 2822a352 2019-10-15 stsp const struct got_error *
6263 2822a352 2019-10-15 stsp got_worktree_integrate_continue(struct got_worktree *worktree,
6264 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6265 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6266 2822a352 2019-10-15 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6267 2822a352 2019-10-15 stsp got_cancel_cb cancel_cb, void *cancel_arg)
6268 2822a352 2019-10-15 stsp {
6269 2822a352 2019-10-15 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6270 2822a352 2019-10-15 stsp char *fileindex_path = NULL;
6271 2822a352 2019-10-15 stsp struct got_object_id *tree_id = NULL, *commit_id = NULL;
6272 2822a352 2019-10-15 stsp
6273 2822a352 2019-10-15 stsp err = get_fileindex_path(&fileindex_path, worktree);
6274 2822a352 2019-10-15 stsp if (err)
6275 2822a352 2019-10-15 stsp goto done;
6276 2822a352 2019-10-15 stsp
6277 2822a352 2019-10-15 stsp err = got_ref_resolve(&commit_id, repo, branch_ref);
6278 2822a352 2019-10-15 stsp if (err)
6279 2822a352 2019-10-15 stsp goto done;
6280 2822a352 2019-10-15 stsp
6281 2822a352 2019-10-15 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
6282 2822a352 2019-10-15 stsp worktree->path_prefix);
6283 2822a352 2019-10-15 stsp if (err)
6284 2822a352 2019-10-15 stsp goto done;
6285 2822a352 2019-10-15 stsp
6286 2822a352 2019-10-15 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6287 2822a352 2019-10-15 stsp if (err)
6288 2822a352 2019-10-15 stsp goto done;
6289 2822a352 2019-10-15 stsp
6290 2822a352 2019-10-15 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6291 2822a352 2019-10-15 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
6292 2822a352 2019-10-15 stsp if (err)
6293 2822a352 2019-10-15 stsp goto sync;
6294 2822a352 2019-10-15 stsp
6295 2822a352 2019-10-15 stsp err = got_ref_change_ref(base_branch_ref, commit_id);
6296 2822a352 2019-10-15 stsp if (err)
6297 2822a352 2019-10-15 stsp goto sync;
6298 2822a352 2019-10-15 stsp
6299 2822a352 2019-10-15 stsp err = got_ref_write(base_branch_ref, repo);
6300 2822a352 2019-10-15 stsp sync:
6301 2822a352 2019-10-15 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6302 2822a352 2019-10-15 stsp if (sync_err && err == NULL)
6303 2822a352 2019-10-15 stsp err = sync_err;
6304 2822a352 2019-10-15 stsp
6305 2822a352 2019-10-15 stsp done:
6306 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(branch_ref);
6307 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6308 2822a352 2019-10-15 stsp err = unlockerr;
6309 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6310 2822a352 2019-10-15 stsp
6311 2822a352 2019-10-15 stsp unlockerr = got_ref_unlock(base_branch_ref);
6312 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6313 2822a352 2019-10-15 stsp err = unlockerr;
6314 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6315 2822a352 2019-10-15 stsp
6316 2822a352 2019-10-15 stsp got_fileindex_free(fileindex);
6317 2822a352 2019-10-15 stsp free(fileindex_path);
6318 2822a352 2019-10-15 stsp free(tree_id);
6319 2822a352 2019-10-15 stsp
6320 2822a352 2019-10-15 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6321 2822a352 2019-10-15 stsp if (unlockerr && err == NULL)
6322 2822a352 2019-10-15 stsp err = unlockerr;
6323 2822a352 2019-10-15 stsp return err;
6324 2822a352 2019-10-15 stsp }
6325 2822a352 2019-10-15 stsp
6326 2822a352 2019-10-15 stsp const struct got_error *
6327 2822a352 2019-10-15 stsp got_worktree_integrate_abort(struct got_worktree *worktree,
6328 2822a352 2019-10-15 stsp struct got_fileindex *fileindex, struct got_repository *repo,
6329 2822a352 2019-10-15 stsp struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6330 2822a352 2019-10-15 stsp {
6331 8b692cd0 2019-10-21 stsp const struct got_error *err = NULL, *unlockerr = NULL;
6332 8b692cd0 2019-10-21 stsp
6333 8b692cd0 2019-10-21 stsp got_fileindex_free(fileindex);
6334 8b692cd0 2019-10-21 stsp
6335 8b692cd0 2019-10-21 stsp err = lock_worktree(worktree, LOCK_SH);
6336 8b692cd0 2019-10-21 stsp
6337 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(branch_ref);
6338 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6339 8b692cd0 2019-10-21 stsp err = unlockerr;
6340 2822a352 2019-10-15 stsp got_ref_close(branch_ref);
6341 8b692cd0 2019-10-21 stsp
6342 8b692cd0 2019-10-21 stsp unlockerr = got_ref_unlock(base_branch_ref);
6343 8b692cd0 2019-10-21 stsp if (unlockerr && err == NULL)
6344 8b692cd0 2019-10-21 stsp err = unlockerr;
6345 2822a352 2019-10-15 stsp got_ref_close(base_branch_ref);
6346 8b692cd0 2019-10-21 stsp
6347 8b692cd0 2019-10-21 stsp return err;
6348 2822a352 2019-10-15 stsp }
6349 2822a352 2019-10-15 stsp
6350 2db2652d 2019-08-07 stsp struct check_stage_ok_arg {
6351 2db2652d 2019-08-07 stsp struct got_object_id *head_commit_id;
6352 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6353 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6354 2db2652d 2019-08-07 stsp struct got_repository *repo;
6355 2db2652d 2019-08-07 stsp int have_changes;
6356 2db2652d 2019-08-07 stsp };
6357 2db2652d 2019-08-07 stsp
6358 2db2652d 2019-08-07 stsp const struct got_error *
6359 2db2652d 2019-08-07 stsp check_stage_ok(void *arg, unsigned char status,
6360 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6361 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6362 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6363 735ef5ac 2019-08-03 stsp {
6364 2db2652d 2019-08-07 stsp struct check_stage_ok_arg *a = arg;
6365 735ef5ac 2019-08-03 stsp const struct got_error *err = NULL;
6366 735ef5ac 2019-08-03 stsp struct got_fileindex_entry *ie;
6367 2db2652d 2019-08-07 stsp struct got_object_id base_commit_id;
6368 2db2652d 2019-08-07 stsp struct got_object_id *base_commit_idp = NULL;
6369 735ef5ac 2019-08-03 stsp char *in_repo_path = NULL, *p;
6370 8b13ce36 2019-08-08 stsp
6371 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_UNVERSIONED ||
6372 7b5dc508 2019-10-28 stsp status == GOT_STATUS_NO_CHANGE)
6373 8b13ce36 2019-08-08 stsp return NULL;
6374 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6375 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, relpath);
6376 735ef5ac 2019-08-03 stsp
6377 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6378 735ef5ac 2019-08-03 stsp if (ie == NULL)
6379 735ef5ac 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6380 735ef5ac 2019-08-03 stsp
6381 2db2652d 2019-08-07 stsp if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6382 2db2652d 2019-08-07 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6383 735ef5ac 2019-08-03 stsp relpath) == -1)
6384 735ef5ac 2019-08-03 stsp return got_error_from_errno("asprintf");
6385 735ef5ac 2019-08-03 stsp
6386 735ef5ac 2019-08-03 stsp if (got_fileindex_entry_has_commit(ie)) {
6387 735ef5ac 2019-08-03 stsp memcpy(base_commit_id.sha1, ie->commit_sha1,
6388 735ef5ac 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6389 735ef5ac 2019-08-03 stsp base_commit_idp = &base_commit_id;
6390 735ef5ac 2019-08-03 stsp }
6391 735ef5ac 2019-08-03 stsp
6392 7b5dc508 2019-10-28 stsp if (status == GOT_STATUS_CONFLICT) {
6393 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6394 3aa5969e 2019-08-06 stsp goto done;
6395 3aa5969e 2019-08-06 stsp } else if (status != GOT_STATUS_ADD &&
6396 3aa5969e 2019-08-06 stsp status != GOT_STATUS_MODIFY &&
6397 3aa5969e 2019-08-06 stsp status != GOT_STATUS_DELETE) {
6398 3aa5969e 2019-08-06 stsp err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6399 735ef5ac 2019-08-03 stsp goto done;
6400 3aa5969e 2019-08-06 stsp }
6401 735ef5ac 2019-08-03 stsp
6402 2db2652d 2019-08-07 stsp a->have_changes = 1;
6403 2db2652d 2019-08-07 stsp
6404 735ef5ac 2019-08-03 stsp p = in_repo_path;
6405 735ef5ac 2019-08-03 stsp while (p[0] == '/')
6406 735ef5ac 2019-08-03 stsp p++;
6407 2db2652d 2019-08-07 stsp err = check_out_of_date(p, status, staged_status,
6408 2db2652d 2019-08-07 stsp blob_id, base_commit_idp, a->head_commit_id, a->repo,
6409 5f8a88c6 2019-08-03 stsp GOT_ERR_STAGE_OUT_OF_DATE);
6410 735ef5ac 2019-08-03 stsp done:
6411 735ef5ac 2019-08-03 stsp free(in_repo_path);
6412 dc424a06 2019-08-07 stsp return err;
6413 dc424a06 2019-08-07 stsp }
6414 dc424a06 2019-08-07 stsp
6415 2db2652d 2019-08-07 stsp struct stage_path_arg {
6416 2db2652d 2019-08-07 stsp struct got_worktree *worktree;
6417 2db2652d 2019-08-07 stsp struct got_fileindex *fileindex;
6418 2db2652d 2019-08-07 stsp struct got_repository *repo;
6419 2db2652d 2019-08-07 stsp got_worktree_status_cb status_cb;
6420 2db2652d 2019-08-07 stsp void *status_arg;
6421 2db2652d 2019-08-07 stsp got_worktree_patch_cb patch_cb;
6422 2db2652d 2019-08-07 stsp void *patch_arg;
6423 7b5dc508 2019-10-28 stsp int staged_something;
6424 2db2652d 2019-08-07 stsp };
6425 2db2652d 2019-08-07 stsp
6426 2db2652d 2019-08-07 stsp static const struct got_error *
6427 2db2652d 2019-08-07 stsp stage_path(void *arg, unsigned char status,
6428 2db2652d 2019-08-07 stsp unsigned char staged_status, const char *relpath,
6429 2db2652d 2019-08-07 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6430 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6431 0cb83759 2019-08-03 stsp {
6432 2db2652d 2019-08-07 stsp struct stage_path_arg *a = arg;
6433 0cb83759 2019-08-03 stsp const struct got_error *err = NULL;
6434 0cb83759 2019-08-03 stsp struct got_fileindex_entry *ie;
6435 2db2652d 2019-08-07 stsp char *ondisk_path = NULL, *path_content = NULL;
6436 0cb83759 2019-08-03 stsp uint32_t stage;
6437 af5a81b2 2019-08-08 stsp struct got_object_id *new_staged_blob_id = NULL;
6438 8b13ce36 2019-08-08 stsp
6439 8b13ce36 2019-08-08 stsp if (status == GOT_STATUS_UNVERSIONED)
6440 8b13ce36 2019-08-08 stsp return NULL;
6441 0cb83759 2019-08-03 stsp
6442 2db2652d 2019-08-07 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6443 d3e7c587 2019-08-03 stsp if (ie == NULL)
6444 d3e7c587 2019-08-03 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6445 0cb83759 2019-08-03 stsp
6446 2db2652d 2019-08-07 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6447 2db2652d 2019-08-07 stsp relpath)== -1)
6448 2db2652d 2019-08-07 stsp return got_error_from_errno("asprintf");
6449 0cb83759 2019-08-03 stsp
6450 0cb83759 2019-08-03 stsp switch (status) {
6451 0cb83759 2019-08-03 stsp case GOT_STATUS_ADD:
6452 0cb83759 2019-08-03 stsp case GOT_STATUS_MODIFY:
6453 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6454 dc424a06 2019-08-07 stsp if (status == GOT_STATUS_ADD) {
6455 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6456 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6457 a7c9878d 2019-08-08 stsp status, ie->path, NULL, 1, 1);
6458 dc424a06 2019-08-07 stsp if (err)
6459 dc424a06 2019-08-07 stsp break;
6460 dc424a06 2019-08-07 stsp if (choice != GOT_PATCH_CHOICE_YES)
6461 dc424a06 2019-08-07 stsp break;
6462 dc424a06 2019-08-07 stsp } else {
6463 e635744c 2019-08-08 stsp err = create_patched_content(&path_content, 0,
6464 af5a81b2 2019-08-08 stsp staged_blob_id ? staged_blob_id : blob_id,
6465 12463d8b 2019-12-13 stsp ondisk_path, dirfd, de_name, ie->path,
6466 12463d8b 2019-12-13 stsp a->repo, a->patch_cb, a->patch_arg);
6467 dc424a06 2019-08-07 stsp if (err || path_content == NULL)
6468 dc424a06 2019-08-07 stsp break;
6469 dc424a06 2019-08-07 stsp }
6470 dc424a06 2019-08-07 stsp }
6471 af5a81b2 2019-08-08 stsp err = got_object_blob_create(&new_staged_blob_id,
6472 2db2652d 2019-08-07 stsp path_content ? path_content : ondisk_path, a->repo);
6473 0cb83759 2019-08-03 stsp if (err)
6474 dc424a06 2019-08-07 stsp break;
6475 af5a81b2 2019-08-08 stsp memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6476 0cb83759 2019-08-03 stsp SHA1_DIGEST_LENGTH);
6477 d3e7c587 2019-08-03 stsp if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6478 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_ADD;
6479 0cb83759 2019-08-03 stsp else
6480 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_MODIFY;
6481 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6482 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6483 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6484 dc424a06 2019-08-07 stsp break;
6485 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6486 2db2652d 2019-08-07 stsp get_staged_status(ie), relpath, blob_id,
6487 12463d8b 2019-12-13 stsp new_staged_blob_id, NULL, dirfd, de_name);
6488 0cb83759 2019-08-03 stsp break;
6489 0cb83759 2019-08-03 stsp case GOT_STATUS_DELETE:
6490 d3e7c587 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
6491 d3e7c587 2019-08-03 stsp break;
6492 2db2652d 2019-08-07 stsp if (a->patch_cb) {
6493 dc424a06 2019-08-07 stsp int choice = GOT_PATCH_CHOICE_NONE;
6494 2db2652d 2019-08-07 stsp err = (*a->patch_cb)(&choice, a->patch_arg, status,
6495 a7c9878d 2019-08-08 stsp ie->path, NULL, 1, 1);
6496 dc424a06 2019-08-07 stsp if (err)
6497 dc424a06 2019-08-07 stsp break;
6498 88f33a19 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6499 88f33a19 2019-08-08 stsp break;
6500 88f33a19 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6501 88f33a19 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6502 dc424a06 2019-08-07 stsp break;
6503 88f33a19 2019-08-08 stsp }
6504 dc424a06 2019-08-07 stsp }
6505 0cb83759 2019-08-03 stsp stage = GOT_FILEIDX_STAGE_DELETE;
6506 537ac44b 2019-08-03 stsp got_fileindex_entry_stage_set(ie, stage);
6507 7b5dc508 2019-10-28 stsp a->staged_something = 1;
6508 2db2652d 2019-08-07 stsp if (a->status_cb == NULL)
6509 dc424a06 2019-08-07 stsp break;
6510 2db2652d 2019-08-07 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6511 12463d8b 2019-12-13 stsp get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6512 12463d8b 2019-12-13 stsp de_name);
6513 0cb83759 2019-08-03 stsp break;
6514 d3e7c587 2019-08-03 stsp case GOT_STATUS_NO_CHANGE:
6515 d3e7c587 2019-08-03 stsp break;
6516 ebf48fd5 2019-08-03 stsp case GOT_STATUS_CONFLICT:
6517 ebf48fd5 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6518 ebf48fd5 2019-08-03 stsp break;
6519 2a06fe5f 2019-08-24 stsp case GOT_STATUS_NONEXISTENT:
6520 2a06fe5f 2019-08-24 stsp err = got_error_set_errno(ENOENT, relpath);
6521 2a06fe5f 2019-08-24 stsp break;
6522 0cb83759 2019-08-03 stsp default:
6523 42005733 2019-08-03 stsp err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6524 537ac44b 2019-08-03 stsp break;
6525 0cb83759 2019-08-03 stsp }
6526 dc424a06 2019-08-07 stsp
6527 dc424a06 2019-08-07 stsp if (path_content && unlink(path_content) == -1 && err == NULL)
6528 dc424a06 2019-08-07 stsp err = got_error_from_errno2("unlink", path_content);
6529 dc424a06 2019-08-07 stsp free(path_content);
6530 2db2652d 2019-08-07 stsp free(ondisk_path);
6531 af5a81b2 2019-08-08 stsp free(new_staged_blob_id);
6532 0ebf8283 2019-07-24 stsp return err;
6533 0ebf8283 2019-07-24 stsp }
6534 0cb83759 2019-08-03 stsp
6535 0cb83759 2019-08-03 stsp const struct got_error *
6536 1e71573e 2019-08-03 stsp got_worktree_stage(struct got_worktree *worktree,
6537 1e71573e 2019-08-03 stsp struct got_pathlist_head *paths,
6538 0cb83759 2019-08-03 stsp got_worktree_status_cb status_cb, void *status_arg,
6539 dc424a06 2019-08-07 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6540 1e71573e 2019-08-03 stsp struct got_repository *repo)
6541 0cb83759 2019-08-03 stsp {
6542 0cb83759 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6543 0cb83759 2019-08-03 stsp struct got_pathlist_entry *pe;
6544 0cb83759 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6545 0cb83759 2019-08-03 stsp char *fileindex_path = NULL;
6546 735ef5ac 2019-08-03 stsp struct got_reference *head_ref = NULL;
6547 735ef5ac 2019-08-03 stsp struct got_object_id *head_commit_id = NULL;
6548 2db2652d 2019-08-07 stsp struct check_stage_ok_arg oka;
6549 2db2652d 2019-08-07 stsp struct stage_path_arg spa;
6550 0cb83759 2019-08-03 stsp
6551 0cb83759 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6552 0cb83759 2019-08-03 stsp if (err)
6553 0cb83759 2019-08-03 stsp return err;
6554 0cb83759 2019-08-03 stsp
6555 735ef5ac 2019-08-03 stsp err = got_ref_open(&head_ref, repo,
6556 735ef5ac 2019-08-03 stsp got_worktree_get_head_ref_name(worktree), 0);
6557 735ef5ac 2019-08-03 stsp if (err)
6558 735ef5ac 2019-08-03 stsp goto done;
6559 735ef5ac 2019-08-03 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
6560 735ef5ac 2019-08-03 stsp if (err)
6561 735ef5ac 2019-08-03 stsp goto done;
6562 0cb83759 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6563 0cb83759 2019-08-03 stsp if (err)
6564 0cb83759 2019-08-03 stsp goto done;
6565 0cb83759 2019-08-03 stsp
6566 3aa5969e 2019-08-06 stsp /* Check pre-conditions before staging anything. */
6567 2db2652d 2019-08-07 stsp oka.head_commit_id = head_commit_id;
6568 2db2652d 2019-08-07 stsp oka.worktree = worktree;
6569 2db2652d 2019-08-07 stsp oka.fileindex = fileindex;
6570 2db2652d 2019-08-07 stsp oka.repo = repo;
6571 2db2652d 2019-08-07 stsp oka.have_changes = 0;
6572 0cb83759 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6573 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6574 f2a9dc41 2019-12-13 tracey check_stage_ok, &oka, NULL, NULL, 0, 0);
6575 735ef5ac 2019-08-03 stsp if (err)
6576 735ef5ac 2019-08-03 stsp goto done;
6577 735ef5ac 2019-08-03 stsp }
6578 2db2652d 2019-08-07 stsp if (!oka.have_changes) {
6579 2db2652d 2019-08-07 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6580 2db2652d 2019-08-07 stsp goto done;
6581 2db2652d 2019-08-07 stsp }
6582 735ef5ac 2019-08-03 stsp
6583 2db2652d 2019-08-07 stsp spa.worktree = worktree;
6584 2db2652d 2019-08-07 stsp spa.fileindex = fileindex;
6585 2db2652d 2019-08-07 stsp spa.repo = repo;
6586 2db2652d 2019-08-07 stsp spa.patch_cb = patch_cb;
6587 2db2652d 2019-08-07 stsp spa.patch_arg = patch_arg;
6588 2db2652d 2019-08-07 stsp spa.status_cb = status_cb;
6589 2db2652d 2019-08-07 stsp spa.status_arg = status_arg;
6590 7b5dc508 2019-10-28 stsp spa.staged_something = 0;
6591 735ef5ac 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6592 2db2652d 2019-08-07 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6593 f2a9dc41 2019-12-13 tracey stage_path, &spa, NULL, NULL, 0, 0);
6594 42005733 2019-08-03 stsp if (err)
6595 2db2652d 2019-08-07 stsp goto done;
6596 7b5dc508 2019-10-28 stsp }
6597 7b5dc508 2019-10-28 stsp if (!spa.staged_something) {
6598 7b5dc508 2019-10-28 stsp err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6599 7b5dc508 2019-10-28 stsp goto done;
6600 0cb83759 2019-08-03 stsp }
6601 0cb83759 2019-08-03 stsp
6602 0cb83759 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6603 0cb83759 2019-08-03 stsp if (sync_err && err == NULL)
6604 0cb83759 2019-08-03 stsp err = sync_err;
6605 0cb83759 2019-08-03 stsp done:
6606 735ef5ac 2019-08-03 stsp if (head_ref)
6607 735ef5ac 2019-08-03 stsp got_ref_close(head_ref);
6608 735ef5ac 2019-08-03 stsp free(head_commit_id);
6609 0cb83759 2019-08-03 stsp free(fileindex_path);
6610 0cb83759 2019-08-03 stsp if (fileindex)
6611 0cb83759 2019-08-03 stsp got_fileindex_free(fileindex);
6612 0cb83759 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6613 0cb83759 2019-08-03 stsp if (unlockerr && err == NULL)
6614 0cb83759 2019-08-03 stsp err = unlockerr;
6615 0cb83759 2019-08-03 stsp return err;
6616 0cb83759 2019-08-03 stsp }
6617 ad493afc 2019-08-03 stsp
6618 ad493afc 2019-08-03 stsp struct unstage_path_arg {
6619 ad493afc 2019-08-03 stsp struct got_worktree *worktree;
6620 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex;
6621 ad493afc 2019-08-03 stsp struct got_repository *repo;
6622 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb;
6623 ad493afc 2019-08-03 stsp void *progress_arg;
6624 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb;
6625 2e1f37b0 2019-08-08 stsp void *patch_arg;
6626 ad493afc 2019-08-03 stsp };
6627 2e1f37b0 2019-08-08 stsp
6628 2e1f37b0 2019-08-08 stsp static const struct got_error *
6629 2e1f37b0 2019-08-08 stsp create_unstaged_content(char **path_unstaged_content,
6630 2e1f37b0 2019-08-08 stsp char **path_new_staged_content, struct got_object_id *blob_id,
6631 2e1f37b0 2019-08-08 stsp struct got_object_id *staged_blob_id, const char *relpath,
6632 2e1f37b0 2019-08-08 stsp struct got_repository *repo,
6633 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg)
6634 2e1f37b0 2019-08-08 stsp {
6635 2e1f37b0 2019-08-08 stsp const struct got_error *err;
6636 2e1f37b0 2019-08-08 stsp struct got_blob_object *blob = NULL, *staged_blob = NULL;
6637 2e1f37b0 2019-08-08 stsp FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6638 2e1f37b0 2019-08-08 stsp char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6639 2e1f37b0 2019-08-08 stsp struct stat sb1, sb2;
6640 2e1f37b0 2019-08-08 stsp struct got_diff_changes *changes = NULL;
6641 2e1f37b0 2019-08-08 stsp struct got_diff_state *ds = NULL;
6642 2e1f37b0 2019-08-08 stsp struct got_diff_args *args = NULL;
6643 2e1f37b0 2019-08-08 stsp struct got_diff_change *change;
6644 2e1f37b0 2019-08-08 stsp int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6645 2e1f37b0 2019-08-08 stsp int have_content = 0, have_rejected_content = 0;
6646 2e1f37b0 2019-08-08 stsp
6647 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
6648 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
6649 2e1f37b0 2019-08-08 stsp
6650 2e1f37b0 2019-08-08 stsp err = got_object_id_str(&label1, blob_id);
6651 2e1f37b0 2019-08-08 stsp if (err)
6652 2e1f37b0 2019-08-08 stsp return err;
6653 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6654 2e1f37b0 2019-08-08 stsp if (err)
6655 2e1f37b0 2019-08-08 stsp goto done;
6656 2e1f37b0 2019-08-08 stsp
6657 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
6658 2e1f37b0 2019-08-08 stsp if (err)
6659 2e1f37b0 2019-08-08 stsp goto done;
6660 2e1f37b0 2019-08-08 stsp
6661 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
6662 2e1f37b0 2019-08-08 stsp if (err)
6663 2e1f37b0 2019-08-08 stsp goto done;
6664 2e1f37b0 2019-08-08 stsp
6665 2e1f37b0 2019-08-08 stsp err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
6666 2e1f37b0 2019-08-08 stsp if (err)
6667 2e1f37b0 2019-08-08 stsp goto done;
6668 2e1f37b0 2019-08-08 stsp
6669 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
6670 2e1f37b0 2019-08-08 stsp if (err)
6671 2e1f37b0 2019-08-08 stsp goto done;
6672 ad493afc 2019-08-03 stsp
6673 2e1f37b0 2019-08-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
6674 2e1f37b0 2019-08-08 stsp if (err)
6675 2e1f37b0 2019-08-08 stsp goto done;
6676 2e1f37b0 2019-08-08 stsp
6677 2e1f37b0 2019-08-08 stsp if (stat(path1, &sb1) == -1) {
6678 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path1);
6679 2e1f37b0 2019-08-08 stsp goto done;
6680 2e1f37b0 2019-08-08 stsp }
6681 2e1f37b0 2019-08-08 stsp
6682 2e1f37b0 2019-08-08 stsp if (stat(path2, &sb2) == -1) {
6683 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("stat", path2);
6684 2e1f37b0 2019-08-08 stsp goto done;
6685 2e1f37b0 2019-08-08 stsp }
6686 2e1f37b0 2019-08-08 stsp
6687 2e1f37b0 2019-08-08 stsp err = got_diff_files(&changes, &ds, &args, &diff_flags,
6688 2e1f37b0 2019-08-08 stsp f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
6689 2e1f37b0 2019-08-08 stsp if (err)
6690 2e1f37b0 2019-08-08 stsp goto done;
6691 2e1f37b0 2019-08-08 stsp
6692 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_unstaged_content, &outfile,
6693 2e1f37b0 2019-08-08 stsp "got-unstaged-content");
6694 2e1f37b0 2019-08-08 stsp if (err)
6695 2e1f37b0 2019-08-08 stsp goto done;
6696 2e1f37b0 2019-08-08 stsp err = got_opentemp_named(path_new_staged_content, &rejectfile,
6697 2e1f37b0 2019-08-08 stsp "got-new-staged-content");
6698 2e1f37b0 2019-08-08 stsp if (err)
6699 2e1f37b0 2019-08-08 stsp goto done;
6700 2e1f37b0 2019-08-08 stsp
6701 2e1f37b0 2019-08-08 stsp if (fseek(f1, 0L, SEEK_SET) == -1) {
6702 2e1f37b0 2019-08-08 stsp err = got_ferror(f1, GOT_ERR_IO);
6703 2e1f37b0 2019-08-08 stsp goto done;
6704 2e1f37b0 2019-08-08 stsp }
6705 2e1f37b0 2019-08-08 stsp if (fseek(f2, 0L, SEEK_SET) == -1) {
6706 2e1f37b0 2019-08-08 stsp err = got_ferror(f2, GOT_ERR_IO);
6707 2e1f37b0 2019-08-08 stsp goto done;
6708 2e1f37b0 2019-08-08 stsp }
6709 2e1f37b0 2019-08-08 stsp SIMPLEQ_FOREACH(change, &changes->entries, entry) {
6710 2e1f37b0 2019-08-08 stsp int choice;
6711 2e1f37b0 2019-08-08 stsp err = apply_or_reject_change(&choice, change, ++n,
6712 2e1f37b0 2019-08-08 stsp changes->nchanges, ds, args, diff_flags, relpath,
6713 2e1f37b0 2019-08-08 stsp f1, f2, &line_cur1, &line_cur2,
6714 2e1f37b0 2019-08-08 stsp outfile, rejectfile, patch_cb, patch_arg);
6715 2e1f37b0 2019-08-08 stsp if (err)
6716 2e1f37b0 2019-08-08 stsp goto done;
6717 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_YES)
6718 2e1f37b0 2019-08-08 stsp have_content = 1;
6719 19e4b907 2019-08-08 stsp else
6720 2e1f37b0 2019-08-08 stsp have_rejected_content = 1;
6721 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_QUIT)
6722 2e1f37b0 2019-08-08 stsp break;
6723 2e1f37b0 2019-08-08 stsp }
6724 f1e81a05 2019-08-10 stsp if (have_content || have_rejected_content)
6725 f1e81a05 2019-08-10 stsp err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
6726 f1e81a05 2019-08-10 stsp outfile, rejectfile);
6727 2e1f37b0 2019-08-08 stsp done:
6728 2e1f37b0 2019-08-08 stsp free(label1);
6729 2e1f37b0 2019-08-08 stsp if (blob)
6730 2e1f37b0 2019-08-08 stsp got_object_blob_close(blob);
6731 2e1f37b0 2019-08-08 stsp if (staged_blob)
6732 2e1f37b0 2019-08-08 stsp got_object_blob_close(staged_blob);
6733 2e1f37b0 2019-08-08 stsp if (f1 && fclose(f1) == EOF && err == NULL)
6734 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path1);
6735 2e1f37b0 2019-08-08 stsp if (f2 && fclose(f2) == EOF && err == NULL)
6736 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", path2);
6737 2e1f37b0 2019-08-08 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
6738 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_unstaged_content);
6739 2e1f37b0 2019-08-08 stsp if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
6740 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("fclose", *path_new_staged_content);
6741 2e1f37b0 2019-08-08 stsp if (path1 && unlink(path1) == -1 && err == NULL)
6742 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path1);
6743 2e1f37b0 2019-08-08 stsp if (path2 && unlink(path2) == -1 && err == NULL)
6744 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path2);
6745 2e1f37b0 2019-08-08 stsp if (err || !have_content) {
6746 2e1f37b0 2019-08-08 stsp if (*path_unstaged_content &&
6747 2e1f37b0 2019-08-08 stsp unlink(*path_unstaged_content) == -1 && err == NULL)
6748 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
6749 2e1f37b0 2019-08-08 stsp *path_unstaged_content);
6750 2e1f37b0 2019-08-08 stsp free(*path_unstaged_content);
6751 2e1f37b0 2019-08-08 stsp *path_unstaged_content = NULL;
6752 2e1f37b0 2019-08-08 stsp }
6753 2e1f37b0 2019-08-08 stsp if (err || !have_rejected_content) {
6754 2e1f37b0 2019-08-08 stsp if (*path_new_staged_content &&
6755 2e1f37b0 2019-08-08 stsp unlink(*path_new_staged_content) == -1 && err == NULL)
6756 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink",
6757 2e1f37b0 2019-08-08 stsp *path_new_staged_content);
6758 2e1f37b0 2019-08-08 stsp free(*path_new_staged_content);
6759 2e1f37b0 2019-08-08 stsp *path_new_staged_content = NULL;
6760 2e1f37b0 2019-08-08 stsp }
6761 2e1f37b0 2019-08-08 stsp free(args);
6762 2e1f37b0 2019-08-08 stsp if (ds) {
6763 2e1f37b0 2019-08-08 stsp got_diff_state_free(ds);
6764 2e1f37b0 2019-08-08 stsp free(ds);
6765 2e1f37b0 2019-08-08 stsp }
6766 2e1f37b0 2019-08-08 stsp if (changes)
6767 2e1f37b0 2019-08-08 stsp got_diff_free_changes(changes);
6768 2e1f37b0 2019-08-08 stsp free(path1);
6769 2e1f37b0 2019-08-08 stsp free(path2);
6770 2e1f37b0 2019-08-08 stsp return err;
6771 2e1f37b0 2019-08-08 stsp }
6772 2e1f37b0 2019-08-08 stsp
6773 ad493afc 2019-08-03 stsp static const struct got_error *
6774 ad493afc 2019-08-03 stsp unstage_path(void *arg, unsigned char status,
6775 ad493afc 2019-08-03 stsp unsigned char staged_status, const char *relpath,
6776 ad493afc 2019-08-03 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6777 12463d8b 2019-12-13 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6778 ad493afc 2019-08-03 stsp {
6779 ad493afc 2019-08-03 stsp const struct got_error *err = NULL;
6780 ad493afc 2019-08-03 stsp struct unstage_path_arg *a = arg;
6781 ad493afc 2019-08-03 stsp struct got_fileindex_entry *ie;
6782 ad493afc 2019-08-03 stsp struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
6783 2e1f37b0 2019-08-08 stsp char *ondisk_path = NULL, *path_unstaged_content = NULL;
6784 2e1f37b0 2019-08-08 stsp char *path_new_staged_content = NULL;
6785 f69721c3 2019-10-21 stsp char *id_str = NULL, *label_orig = NULL;
6786 ad493afc 2019-08-03 stsp int local_changes_subsumed;
6787 9bc94a15 2019-08-03 stsp struct stat sb;
6788 ad493afc 2019-08-03 stsp
6789 2e1f37b0 2019-08-08 stsp if (staged_status != GOT_STATUS_ADD &&
6790 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_MODIFY &&
6791 2e1f37b0 2019-08-08 stsp staged_status != GOT_STATUS_DELETE)
6792 2e1f37b0 2019-08-08 stsp return NULL;
6793 2e1f37b0 2019-08-08 stsp
6794 ad493afc 2019-08-03 stsp ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6795 ad493afc 2019-08-03 stsp if (ie == NULL)
6796 8b13ce36 2019-08-08 stsp return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6797 9bc94a15 2019-08-03 stsp
6798 9bc94a15 2019-08-03 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
6799 9bc94a15 2019-08-03 stsp == -1)
6800 9bc94a15 2019-08-03 stsp return got_error_from_errno("asprintf");
6801 ad493afc 2019-08-03 stsp
6802 f69721c3 2019-10-21 stsp err = got_object_id_str(&id_str,
6803 f69721c3 2019-10-21 stsp commit_id ? commit_id : a->worktree->base_commit_id);
6804 f69721c3 2019-10-21 stsp if (err)
6805 f69721c3 2019-10-21 stsp goto done;
6806 f69721c3 2019-10-21 stsp if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
6807 f69721c3 2019-10-21 stsp id_str) == -1) {
6808 f69721c3 2019-10-21 stsp err = got_error_from_errno("asprintf");
6809 f69721c3 2019-10-21 stsp goto done;
6810 f69721c3 2019-10-21 stsp }
6811 f69721c3 2019-10-21 stsp
6812 ad493afc 2019-08-03 stsp switch (staged_status) {
6813 ad493afc 2019-08-03 stsp case GOT_STATUS_MODIFY:
6814 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_base, a->repo,
6815 ad493afc 2019-08-03 stsp blob_id, 8192);
6816 ad493afc 2019-08-03 stsp if (err)
6817 ad493afc 2019-08-03 stsp break;
6818 ad493afc 2019-08-03 stsp /* fall through */
6819 ad493afc 2019-08-03 stsp case GOT_STATUS_ADD:
6820 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
6821 2e1f37b0 2019-08-08 stsp if (staged_status == GOT_STATUS_ADD) {
6822 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
6823 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6824 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
6825 2e1f37b0 2019-08-08 stsp if (err)
6826 2e1f37b0 2019-08-08 stsp break;
6827 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES)
6828 2e1f37b0 2019-08-08 stsp break;
6829 2e1f37b0 2019-08-08 stsp } else {
6830 2e1f37b0 2019-08-08 stsp err = create_unstaged_content(
6831 2e1f37b0 2019-08-08 stsp &path_unstaged_content,
6832 2e1f37b0 2019-08-08 stsp &path_new_staged_content, blob_id,
6833 2e1f37b0 2019-08-08 stsp staged_blob_id, ie->path, a->repo,
6834 2e1f37b0 2019-08-08 stsp a->patch_cb, a->patch_arg);
6835 2e1f37b0 2019-08-08 stsp if (err || path_unstaged_content == NULL)
6836 2e1f37b0 2019-08-08 stsp break;
6837 2e1f37b0 2019-08-08 stsp if (path_new_staged_content) {
6838 2e1f37b0 2019-08-08 stsp err = got_object_blob_create(
6839 2e1f37b0 2019-08-08 stsp &staged_blob_id,
6840 2e1f37b0 2019-08-08 stsp path_new_staged_content,
6841 2e1f37b0 2019-08-08 stsp a->repo);
6842 2e1f37b0 2019-08-08 stsp if (err)
6843 2e1f37b0 2019-08-08 stsp break;
6844 2e1f37b0 2019-08-08 stsp memcpy(ie->staged_blob_sha1,
6845 2e1f37b0 2019-08-08 stsp staged_blob_id->sha1,
6846 2e1f37b0 2019-08-08 stsp SHA1_DIGEST_LENGTH);
6847 2e1f37b0 2019-08-08 stsp }
6848 2e1f37b0 2019-08-08 stsp err = merge_file(&local_changes_subsumed,
6849 2e1f37b0 2019-08-08 stsp a->worktree, blob_base, ondisk_path,
6850 2e1f37b0 2019-08-08 stsp relpath, got_fileindex_perms_to_st(ie),
6851 f69721c3 2019-10-21 stsp path_unstaged_content, label_orig,
6852 f69721c3 2019-10-21 stsp "unstaged", a->repo, a->progress_cb,
6853 f69721c3 2019-10-21 stsp a->progress_arg);
6854 2e1f37b0 2019-08-08 stsp if (err == NULL &&
6855 2e1f37b0 2019-08-08 stsp path_new_staged_content == NULL)
6856 2e1f37b0 2019-08-08 stsp got_fileindex_entry_stage_set(ie,
6857 2e1f37b0 2019-08-08 stsp GOT_FILEIDX_STAGE_NONE);
6858 2e1f37b0 2019-08-08 stsp break; /* Done with this file. */
6859 2e1f37b0 2019-08-08 stsp }
6860 2e1f37b0 2019-08-08 stsp }
6861 ad493afc 2019-08-03 stsp err = got_object_open_as_blob(&blob_staged, a->repo,
6862 ad493afc 2019-08-03 stsp staged_blob_id, 8192);
6863 ad493afc 2019-08-03 stsp if (err)
6864 ad493afc 2019-08-03 stsp break;
6865 ad493afc 2019-08-03 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
6866 ad493afc 2019-08-03 stsp blob_base, ondisk_path, relpath,
6867 f69721c3 2019-10-21 stsp got_fileindex_perms_to_st(ie), label_orig, blob_staged,
6868 ad493afc 2019-08-03 stsp commit_id ? commit_id : a->worktree->base_commit_id,
6869 ad493afc 2019-08-03 stsp a->repo, a->progress_cb, a->progress_arg);
6870 ad493afc 2019-08-03 stsp if (err == NULL)
6871 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie,
6872 ad493afc 2019-08-03 stsp GOT_FILEIDX_STAGE_NONE);
6873 ad493afc 2019-08-03 stsp break;
6874 ad493afc 2019-08-03 stsp case GOT_STATUS_DELETE:
6875 2e1f37b0 2019-08-08 stsp if (a->patch_cb) {
6876 2e1f37b0 2019-08-08 stsp int choice = GOT_PATCH_CHOICE_NONE;
6877 2e1f37b0 2019-08-08 stsp err = (*a->patch_cb)(&choice, a->patch_arg,
6878 2e1f37b0 2019-08-08 stsp staged_status, ie->path, NULL, 1, 1);
6879 2e1f37b0 2019-08-08 stsp if (err)
6880 2e1f37b0 2019-08-08 stsp break;
6881 2e1f37b0 2019-08-08 stsp if (choice == GOT_PATCH_CHOICE_NO)
6882 2e1f37b0 2019-08-08 stsp break;
6883 2e1f37b0 2019-08-08 stsp if (choice != GOT_PATCH_CHOICE_YES) {
6884 2e1f37b0 2019-08-08 stsp err = got_error(GOT_ERR_PATCH_CHOICE);
6885 2e1f37b0 2019-08-08 stsp break;
6886 2e1f37b0 2019-08-08 stsp }
6887 2e1f37b0 2019-08-08 stsp }
6888 ad493afc 2019-08-03 stsp got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
6889 12463d8b 2019-12-13 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
6890 12463d8b 2019-12-13 stsp dirfd, de_name, a->repo);
6891 9bc94a15 2019-08-03 stsp if (err)
6892 9bc94a15 2019-08-03 stsp break;
6893 9bc94a15 2019-08-03 stsp err = (*a->progress_cb)(a->progress_arg, status, relpath);
6894 ad493afc 2019-08-03 stsp break;
6895 ad493afc 2019-08-03 stsp }
6896 f69721c3 2019-10-21 stsp done:
6897 ad493afc 2019-08-03 stsp free(ondisk_path);
6898 2e1f37b0 2019-08-08 stsp if (path_unstaged_content &&
6899 2e1f37b0 2019-08-08 stsp unlink(path_unstaged_content) == -1 && err == NULL)
6900 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_unstaged_content);
6901 2e1f37b0 2019-08-08 stsp if (path_new_staged_content &&
6902 2e1f37b0 2019-08-08 stsp unlink(path_new_staged_content) == -1 && err == NULL)
6903 2e1f37b0 2019-08-08 stsp err = got_error_from_errno2("unlink", path_new_staged_content);
6904 2e1f37b0 2019-08-08 stsp free(path_unstaged_content);
6905 2e1f37b0 2019-08-08 stsp free(path_new_staged_content);
6906 ad493afc 2019-08-03 stsp if (blob_base)
6907 ad493afc 2019-08-03 stsp got_object_blob_close(blob_base);
6908 ad493afc 2019-08-03 stsp if (blob_staged)
6909 ad493afc 2019-08-03 stsp got_object_blob_close(blob_staged);
6910 f69721c3 2019-10-21 stsp free(id_str);
6911 f69721c3 2019-10-21 stsp free(label_orig);
6912 ad493afc 2019-08-03 stsp return err;
6913 ad493afc 2019-08-03 stsp }
6914 ad493afc 2019-08-03 stsp
6915 ad493afc 2019-08-03 stsp const struct got_error *
6916 ad493afc 2019-08-03 stsp got_worktree_unstage(struct got_worktree *worktree,
6917 ad493afc 2019-08-03 stsp struct got_pathlist_head *paths,
6918 ad493afc 2019-08-03 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
6919 2e1f37b0 2019-08-08 stsp got_worktree_patch_cb patch_cb, void *patch_arg,
6920 ad493afc 2019-08-03 stsp struct got_repository *repo)
6921 ad493afc 2019-08-03 stsp {
6922 ad493afc 2019-08-03 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
6923 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6924 ad493afc 2019-08-03 stsp struct got_fileindex *fileindex = NULL;
6925 ad493afc 2019-08-03 stsp char *fileindex_path = NULL;
6926 ad493afc 2019-08-03 stsp struct unstage_path_arg upa;
6927 ad493afc 2019-08-03 stsp
6928 ad493afc 2019-08-03 stsp err = lock_worktree(worktree, LOCK_EX);
6929 ad493afc 2019-08-03 stsp if (err)
6930 ad493afc 2019-08-03 stsp return err;
6931 ad493afc 2019-08-03 stsp
6932 ad493afc 2019-08-03 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
6933 ad493afc 2019-08-03 stsp if (err)
6934 ad493afc 2019-08-03 stsp goto done;
6935 ad493afc 2019-08-03 stsp
6936 ad493afc 2019-08-03 stsp upa.worktree = worktree;
6937 ad493afc 2019-08-03 stsp upa.fileindex = fileindex;
6938 ad493afc 2019-08-03 stsp upa.repo = repo;
6939 ad493afc 2019-08-03 stsp upa.progress_cb = progress_cb;
6940 ad493afc 2019-08-03 stsp upa.progress_arg = progress_arg;
6941 2e1f37b0 2019-08-08 stsp upa.patch_cb = patch_cb;
6942 2e1f37b0 2019-08-08 stsp upa.patch_arg = patch_arg;
6943 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, paths, entry) {
6944 ad493afc 2019-08-03 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
6945 f2a9dc41 2019-12-13 tracey unstage_path, &upa, NULL, NULL, 0, 0);
6946 ad493afc 2019-08-03 stsp if (err)
6947 ad493afc 2019-08-03 stsp goto done;
6948 ad493afc 2019-08-03 stsp }
6949 ad493afc 2019-08-03 stsp
6950 ad493afc 2019-08-03 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
6951 ad493afc 2019-08-03 stsp if (sync_err && err == NULL)
6952 ad493afc 2019-08-03 stsp err = sync_err;
6953 ad493afc 2019-08-03 stsp done:
6954 ad493afc 2019-08-03 stsp free(fileindex_path);
6955 ad493afc 2019-08-03 stsp if (fileindex)
6956 ad493afc 2019-08-03 stsp got_fileindex_free(fileindex);
6957 ad493afc 2019-08-03 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
6958 ad493afc 2019-08-03 stsp if (unlockerr && err == NULL)
6959 ad493afc 2019-08-03 stsp err = unlockerr;
6960 ad493afc 2019-08-03 stsp return err;
6961 ad493afc 2019-08-03 stsp }