Blame


1 86c3caaf 2018-03-09 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
21 86c3caaf 2018-03-09 stsp
22 d1f6d47b 2019-02-04 stsp #include <dirent.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 86c3caaf 2018-03-09 stsp #include <fcntl.h>
28 86c3caaf 2018-03-09 stsp #include <errno.h>
29 86c3caaf 2018-03-09 stsp #include <unistd.h>
30 9d31a1d8 2018-03-11 stsp #include <sha1.h>
31 9d31a1d8 2018-03-11 stsp #include <zlib.h>
32 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
33 512f0d0e 2019-01-02 stsp #include <libgen.h>
34 ec22038e 2019-03-10 stsp #include <uuid.h>
35 7154f6ce 2019-03-27 stsp #include <util.h>
36 86c3caaf 2018-03-09 stsp
37 86c3caaf 2018-03-09 stsp #include "got_error.h"
38 86c3caaf 2018-03-09 stsp #include "got_repository.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 9d31a1d8 2018-03-11 stsp #include "got_object.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 234035bc 2019-06-01 stsp #include "got_diff.h"
45 86c3caaf 2018-03-09 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
49 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
50 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
51 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
52 ed175427 2019-05-09 stsp #include "got_lib_object_parse.h"
53 cf066bf8 2019-05-09 stsp #include "got_lib_object_create.h"
54 24519714 2019-05-09 stsp #include "got_lib_object_idset.h"
55 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
56 9d31a1d8 2018-03-11 stsp
57 9d31a1d8 2018-03-11 stsp #ifndef MIN
58 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 9d31a1d8 2018-03-11 stsp #endif
60 86c3caaf 2018-03-09 stsp
61 99724ed4 2018-03-10 stsp static const struct got_error *
62 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
63 99724ed4 2018-03-10 stsp {
64 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
65 99724ed4 2018-03-10 stsp char *path;
66 99724ed4 2018-03-10 stsp
67 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
69 99724ed4 2018-03-10 stsp
70 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, content);
71 99724ed4 2018-03-10 stsp free(path);
72 507dc3bb 2018-12-29 stsp return err;
73 507dc3bb 2018-12-29 stsp }
74 507dc3bb 2018-12-29 stsp
75 507dc3bb 2018-12-29 stsp static const struct got_error *
76 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
77 507dc3bb 2018-12-29 stsp {
78 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
79 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
80 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
81 507dc3bb 2018-12-29 stsp char *path = NULL;
82 507dc3bb 2018-12-29 stsp
83 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
85 507dc3bb 2018-12-29 stsp path = NULL;
86 507dc3bb 2018-12-29 stsp goto done;
87 507dc3bb 2018-12-29 stsp }
88 507dc3bb 2018-12-29 stsp
89 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
90 507dc3bb 2018-12-29 stsp if (err)
91 507dc3bb 2018-12-29 stsp goto done;
92 507dc3bb 2018-12-29 stsp
93 507dc3bb 2018-12-29 stsp if (content) {
94 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
95 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
96 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
97 507dc3bb 2018-12-29 stsp goto done;
98 507dc3bb 2018-12-29 stsp }
99 507dc3bb 2018-12-29 stsp }
100 507dc3bb 2018-12-29 stsp
101 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
102 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
103 2a57020b 2019-02-20 stsp unlink(tmppath);
104 507dc3bb 2018-12-29 stsp goto done;
105 507dc3bb 2018-12-29 stsp }
106 507dc3bb 2018-12-29 stsp
107 507dc3bb 2018-12-29 stsp done:
108 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
109 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
110 230a42bd 2019-05-11 jcs free(tmppath);
111 99724ed4 2018-03-10 stsp return err;
112 99724ed4 2018-03-10 stsp }
113 99724ed4 2018-03-10 stsp
114 09fe317a 2018-03-11 stsp static const struct got_error *
115 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
116 09fe317a 2018-03-11 stsp {
117 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
118 09fe317a 2018-03-11 stsp char *path;
119 09fe317a 2018-03-11 stsp int fd = -1;
120 09fe317a 2018-03-11 stsp ssize_t n;
121 09fe317a 2018-03-11 stsp struct stat sb;
122 09fe317a 2018-03-11 stsp
123 09fe317a 2018-03-11 stsp *content = NULL;
124 09fe317a 2018-03-11 stsp
125 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
127 09fe317a 2018-03-11 stsp path = NULL;
128 09fe317a 2018-03-11 stsp goto done;
129 09fe317a 2018-03-11 stsp }
130 09fe317a 2018-03-11 stsp
131 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
132 09fe317a 2018-03-11 stsp if (fd == -1) {
133 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
134 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
135 f02eaa22 2019-03-11 stsp else
136 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
137 ef99fdb1 2018-03-11 stsp goto done;
138 ef99fdb1 2018-03-11 stsp }
139 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
142 09fe317a 2018-03-11 stsp goto done;
143 09fe317a 2018-03-11 stsp }
144 09fe317a 2018-03-11 stsp
145 e4b9a50c 2019-07-27 stsp if (fstat(fd, &sb) != 0) {
146 e4b9a50c 2019-07-27 stsp err = got_error_from_errno2("fstat", path);
147 d10c9b58 2019-02-19 stsp goto done;
148 d10c9b58 2019-02-19 stsp }
149 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
150 09fe317a 2018-03-11 stsp if (*content == NULL) {
151 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
152 09fe317a 2018-03-11 stsp goto done;
153 09fe317a 2018-03-11 stsp }
154 09fe317a 2018-03-11 stsp
155 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
156 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
157 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
158 a2e6d162 2019-07-27 stsp got_error_path(path, GOT_ERR_WORKTREE_META));
159 09fe317a 2018-03-11 stsp goto done;
160 09fe317a 2018-03-11 stsp }
161 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
162 a2e6d162 2019-07-27 stsp err = got_error_path(path, GOT_ERR_WORKTREE_META);
163 09fe317a 2018-03-11 stsp goto done;
164 09fe317a 2018-03-11 stsp }
165 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
166 09fe317a 2018-03-11 stsp
167 09fe317a 2018-03-11 stsp done:
168 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
169 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
170 09fe317a 2018-03-11 stsp free(path);
171 09fe317a 2018-03-11 stsp if (err) {
172 09fe317a 2018-03-11 stsp free(*content);
173 09fe317a 2018-03-11 stsp *content = NULL;
174 09fe317a 2018-03-11 stsp }
175 09fe317a 2018-03-11 stsp return err;
176 09fe317a 2018-03-11 stsp }
177 09fe317a 2018-03-11 stsp
178 024e9686 2019-05-14 stsp static const struct got_error *
179 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
180 024e9686 2019-05-14 stsp {
181 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
182 024e9686 2019-05-14 stsp char *refstr = NULL;
183 024e9686 2019-05-14 stsp
184 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
185 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
186 024e9686 2019-05-14 stsp if (refstr == NULL)
187 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
188 024e9686 2019-05-14 stsp } else {
189 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
190 024e9686 2019-05-14 stsp if (refstr == NULL)
191 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
192 024e9686 2019-05-14 stsp }
193 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 024e9686 2019-05-14 stsp free(refstr);
195 024e9686 2019-05-14 stsp return err;
196 024e9686 2019-05-14 stsp }
197 024e9686 2019-05-14 stsp
198 86c3caaf 2018-03-09 stsp const struct got_error *
199 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
200 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
201 86c3caaf 2018-03-09 stsp {
202 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
203 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
204 ec22038e 2019-03-10 stsp uuid_t uuid;
205 ec22038e 2019-03-10 stsp uint32_t uuid_status;
206 65596e15 2018-12-24 stsp int obj_type;
207 7ac97322 2018-03-11 stsp char *path_got = NULL;
208 1451e70d 2018-03-10 stsp char *formatstr = NULL;
209 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
210 65596e15 2018-12-24 stsp char *basestr = NULL;
211 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
212 65596e15 2018-12-24 stsp
213 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
215 0c48fee2 2019-03-11 stsp goto done;
216 0c48fee2 2019-03-11 stsp }
217 0c48fee2 2019-03-11 stsp
218 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
219 65596e15 2018-12-24 stsp if (err)
220 65596e15 2018-12-24 stsp return err;
221 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
222 65596e15 2018-12-24 stsp if (err)
223 65596e15 2018-12-24 stsp return err;
224 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
226 86c3caaf 2018-03-09 stsp
227 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
228 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
229 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
230 0bb8a95e 2018-03-12 stsp }
231 577ec78f 2018-03-11 stsp
232 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
233 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
235 86c3caaf 2018-03-09 stsp goto done;
236 86c3caaf 2018-03-09 stsp }
237 86c3caaf 2018-03-09 stsp
238 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
239 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp }
243 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
245 86c3caaf 2018-03-09 stsp goto done;
246 86c3caaf 2018-03-09 stsp }
247 86c3caaf 2018-03-09 stsp
248 056e7441 2018-03-11 stsp /* Create an empty lock file. */
249 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
250 056e7441 2018-03-11 stsp if (err)
251 056e7441 2018-03-11 stsp goto done;
252 056e7441 2018-03-11 stsp
253 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
254 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
255 99724ed4 2018-03-10 stsp if (err)
256 86c3caaf 2018-03-09 stsp goto done;
257 86c3caaf 2018-03-09 stsp
258 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
259 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
260 65596e15 2018-12-24 stsp if (err)
261 65596e15 2018-12-24 stsp goto done;
262 65596e15 2018-12-24 stsp
263 65596e15 2018-12-24 stsp /* Record our base commit. */
264 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
265 65596e15 2018-12-24 stsp if (err)
266 65596e15 2018-12-24 stsp goto done;
267 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
268 99724ed4 2018-03-10 stsp if (err)
269 86c3caaf 2018-03-09 stsp goto done;
270 86c3caaf 2018-03-09 stsp
271 1451e70d 2018-03-10 stsp /* Store path to repository. */
272 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
274 99724ed4 2018-03-10 stsp if (err)
275 86c3caaf 2018-03-09 stsp goto done;
276 86c3caaf 2018-03-09 stsp
277 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
278 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
280 ec22038e 2019-03-10 stsp if (err)
281 ec22038e 2019-03-10 stsp goto done;
282 ec22038e 2019-03-10 stsp
283 ec22038e 2019-03-10 stsp /* Generate UUID. */
284 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
285 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
286 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
287 ec22038e 2019-03-10 stsp goto done;
288 ec22038e 2019-03-10 stsp }
289 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
291 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
292 ec22038e 2019-03-10 stsp goto done;
293 ec22038e 2019-03-10 stsp }
294 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
295 577ec78f 2018-03-11 stsp if (err)
296 577ec78f 2018-03-11 stsp goto done;
297 577ec78f 2018-03-11 stsp
298 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
299 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
301 1451e70d 2018-03-10 stsp goto done;
302 1451e70d 2018-03-10 stsp }
303 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
304 99724ed4 2018-03-10 stsp if (err)
305 1451e70d 2018-03-10 stsp goto done;
306 1451e70d 2018-03-10 stsp
307 86c3caaf 2018-03-09 stsp done:
308 65596e15 2018-12-24 stsp free(commit_id);
309 7ac97322 2018-03-11 stsp free(path_got);
310 1451e70d 2018-03-10 stsp free(formatstr);
311 0bb8a95e 2018-03-12 stsp free(absprefix);
312 65596e15 2018-12-24 stsp free(basestr);
313 ec22038e 2019-03-10 stsp free(uuidstr);
314 86c3caaf 2018-03-09 stsp return err;
315 86c3caaf 2018-03-09 stsp }
316 86c3caaf 2018-03-09 stsp
317 247140b2 2019-02-05 stsp static const struct got_error *
318 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
319 86c3caaf 2018-03-09 stsp {
320 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
321 7ac97322 2018-03-11 stsp char *path_got;
322 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
323 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
324 056e7441 2018-03-11 stsp char *path_lock = NULL;
325 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
326 6d9d28c3 2018-03-11 stsp int version, fd = -1;
327 6d9d28c3 2018-03-11 stsp const char *errstr;
328 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
329 c442a90d 2019-03-10 stsp uint32_t uuid_status;
330 6d9d28c3 2018-03-11 stsp
331 6d9d28c3 2018-03-11 stsp *worktree = NULL;
332 6d9d28c3 2018-03-11 stsp
333 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
335 7ac97322 2018-03-11 stsp path_got = NULL;
336 6d9d28c3 2018-03-11 stsp goto done;
337 6d9d28c3 2018-03-11 stsp }
338 6d9d28c3 2018-03-11 stsp
339 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
341 056e7441 2018-03-11 stsp path_lock = NULL;
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 6d9d28c3 2018-03-11 stsp
345 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 6d9d28c3 2018-03-11 stsp if (fd == -1) {
347 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
349 6d9d28c3 2018-03-11 stsp goto done;
350 6d9d28c3 2018-03-11 stsp }
351 6d9d28c3 2018-03-11 stsp
352 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
353 6d9d28c3 2018-03-11 stsp if (err)
354 6d9d28c3 2018-03-11 stsp goto done;
355 6d9d28c3 2018-03-11 stsp
356 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 6d9d28c3 2018-03-11 stsp if (errstr) {
358 a2e6d162 2019-07-27 stsp err = got_error_msg(GOT_ERR_WORKTREE_META,
359 a2e6d162 2019-07-27 stsp "could not parse work tree format version number");
360 6d9d28c3 2018-03-11 stsp goto done;
361 6d9d28c3 2018-03-11 stsp }
362 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
364 6d9d28c3 2018-03-11 stsp goto done;
365 6d9d28c3 2018-03-11 stsp }
366 6d9d28c3 2018-03-11 stsp
367 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
368 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
369 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
373 6d9d28c3 2018-03-11 stsp
374 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
375 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
376 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
377 6d9d28c3 2018-03-11 stsp goto done;
378 6d9d28c3 2018-03-11 stsp }
379 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
380 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
381 6d9d28c3 2018-03-11 stsp if (err)
382 6d9d28c3 2018-03-11 stsp goto done;
383 eaccb85f 2018-12-25 stsp
384 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
386 93a30277 2018-12-24 stsp if (err)
387 93a30277 2018-12-24 stsp goto done;
388 93a30277 2018-12-24 stsp
389 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
390 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
391 c442a90d 2019-03-10 stsp if (err)
392 c442a90d 2019-03-10 stsp goto done;
393 c442a90d 2019-03-10 stsp
394 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
395 eaccb85f 2018-12-25 stsp if (err)
396 c442a90d 2019-03-10 stsp goto done;
397 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
399 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
400 eaccb85f 2018-12-25 stsp goto done;
401 c442a90d 2019-03-10 stsp }
402 eaccb85f 2018-12-25 stsp
403 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
404 eaccb85f 2018-12-25 stsp if (err)
405 eaccb85f 2018-12-25 stsp goto done;
406 eaccb85f 2018-12-25 stsp
407 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
408 eaccb85f 2018-12-25 stsp base_commit_id_str);
409 f5baf295 2018-03-11 stsp if (err)
410 6d9d28c3 2018-03-11 stsp goto done;
411 6d9d28c3 2018-03-11 stsp
412 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
414 6d9d28c3 2018-03-11 stsp done:
415 eaccb85f 2018-12-25 stsp if (repo)
416 eaccb85f 2018-12-25 stsp got_repo_close(repo);
417 7ac97322 2018-03-11 stsp free(path_got);
418 056e7441 2018-03-11 stsp free(path_lock);
419 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
420 c442a90d 2019-03-10 stsp free(uuidstr);
421 bd165944 2019-03-10 stsp free(formatstr);
422 6d9d28c3 2018-03-11 stsp if (err) {
423 6d9d28c3 2018-03-11 stsp if (fd != -1)
424 6d9d28c3 2018-03-11 stsp close(fd);
425 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
426 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
427 6d9d28c3 2018-03-11 stsp *worktree = NULL;
428 6d9d28c3 2018-03-11 stsp } else
429 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
430 6d9d28c3 2018-03-11 stsp
431 6d9d28c3 2018-03-11 stsp return err;
432 86c3caaf 2018-03-09 stsp }
433 247140b2 2019-02-05 stsp
434 247140b2 2019-02-05 stsp const struct got_error *
435 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
436 247140b2 2019-02-05 stsp {
437 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
438 86c3caaf 2018-03-09 stsp
439 247140b2 2019-02-05 stsp do {
440 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
441 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
442 247140b2 2019-02-05 stsp return err;
443 247140b2 2019-02-05 stsp if (*worktree)
444 247140b2 2019-02-05 stsp return NULL;
445 247140b2 2019-02-05 stsp path = dirname(path);
446 d1542a27 2019-02-05 stsp if (path == NULL)
447 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
448 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 247140b2 2019-02-05 stsp
450 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
451 247140b2 2019-02-05 stsp }
452 247140b2 2019-02-05 stsp
453 3a6ce05a 2019-02-11 stsp const struct got_error *
454 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
455 86c3caaf 2018-03-09 stsp {
456 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
457 c88eb298 2018-03-11 stsp free(worktree->root_path);
458 cde76477 2018-03-11 stsp free(worktree->repo_path);
459 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
460 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
461 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
462 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
463 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
464 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
465 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
466 6d9d28c3 2018-03-11 stsp free(worktree);
467 3a6ce05a 2019-02-11 stsp return err;
468 86c3caaf 2018-03-09 stsp }
469 86c3caaf 2018-03-09 stsp
470 2fbdb5ae 2018-12-29 stsp const char *
471 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
472 c7f4312f 2019-02-05 stsp {
473 c7f4312f 2019-02-05 stsp return worktree->root_path;
474 c7f4312f 2019-02-05 stsp }
475 c7f4312f 2019-02-05 stsp
476 c7f4312f 2019-02-05 stsp const char *
477 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
478 86c3caaf 2018-03-09 stsp {
479 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
480 49520a32 2018-12-29 stsp }
481 49520a32 2018-12-29 stsp
482 49520a32 2018-12-29 stsp const char *
483 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
484 49520a32 2018-12-29 stsp {
485 f609be2e 2018-12-29 stsp return worktree->path_prefix;
486 e5dc7198 2018-12-29 stsp }
487 e5dc7198 2018-12-29 stsp
488 e5dc7198 2018-12-29 stsp const struct got_error *
489 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 e5dc7198 2018-12-29 stsp const char *path_prefix)
491 e5dc7198 2018-12-29 stsp {
492 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
493 e5dc7198 2018-12-29 stsp
494 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
495 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
497 e5dc7198 2018-12-29 stsp }
498 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
499 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
500 e5dc7198 2018-12-29 stsp free(absprefix);
501 e5dc7198 2018-12-29 stsp return NULL;
502 86c3caaf 2018-03-09 stsp }
503 86c3caaf 2018-03-09 stsp
504 bc70eb79 2019-05-09 stsp const char *
505 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 86c3caaf 2018-03-09 stsp {
507 36a38700 2019-05-10 stsp return worktree->head_ref_name;
508 024e9686 2019-05-14 stsp }
509 024e9686 2019-05-14 stsp
510 024e9686 2019-05-14 stsp const struct got_error *
511 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
512 024e9686 2019-05-14 stsp struct got_reference *head_ref)
513 024e9686 2019-05-14 stsp {
514 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
515 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
516 024e9686 2019-05-14 stsp
517 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
519 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
520 024e9686 2019-05-14 stsp path_got = NULL;
521 024e9686 2019-05-14 stsp goto done;
522 024e9686 2019-05-14 stsp }
523 024e9686 2019-05-14 stsp
524 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
525 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
526 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
527 024e9686 2019-05-14 stsp goto done;
528 024e9686 2019-05-14 stsp }
529 024e9686 2019-05-14 stsp
530 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
531 024e9686 2019-05-14 stsp if (err)
532 024e9686 2019-05-14 stsp goto done;
533 024e9686 2019-05-14 stsp
534 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
535 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
536 024e9686 2019-05-14 stsp done:
537 024e9686 2019-05-14 stsp free(path_got);
538 024e9686 2019-05-14 stsp if (err)
539 024e9686 2019-05-14 stsp free(head_ref_name);
540 024e9686 2019-05-14 stsp return err;
541 507dc3bb 2018-12-29 stsp }
542 507dc3bb 2018-12-29 stsp
543 b72f483a 2019-02-05 stsp struct got_object_id *
544 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 507dc3bb 2018-12-29 stsp {
546 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
547 86c3caaf 2018-03-09 stsp }
548 86c3caaf 2018-03-09 stsp
549 507dc3bb 2018-12-29 stsp const struct got_error *
550 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
552 507dc3bb 2018-12-29 stsp {
553 507dc3bb 2018-12-29 stsp const struct got_error *err;
554 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
555 507dc3bb 2018-12-29 stsp char *id_str = NULL;
556 507dc3bb 2018-12-29 stsp char *path_got = NULL;
557 507dc3bb 2018-12-29 stsp
558 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
560 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
561 507dc3bb 2018-12-29 stsp path_got = NULL;
562 507dc3bb 2018-12-29 stsp goto done;
563 507dc3bb 2018-12-29 stsp }
564 507dc3bb 2018-12-29 stsp
565 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
566 507dc3bb 2018-12-29 stsp if (err)
567 507dc3bb 2018-12-29 stsp return err;
568 507dc3bb 2018-12-29 stsp
569 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
571 507dc3bb 2018-12-29 stsp goto done;
572 507dc3bb 2018-12-29 stsp }
573 507dc3bb 2018-12-29 stsp
574 507dc3bb 2018-12-29 stsp /* Record our base commit. */
575 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
576 507dc3bb 2018-12-29 stsp if (err)
577 507dc3bb 2018-12-29 stsp goto done;
578 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
579 507dc3bb 2018-12-29 stsp if (err)
580 507dc3bb 2018-12-29 stsp goto done;
581 507dc3bb 2018-12-29 stsp
582 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
583 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
584 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
585 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
586 507dc3bb 2018-12-29 stsp goto done;
587 507dc3bb 2018-12-29 stsp }
588 507dc3bb 2018-12-29 stsp done:
589 507dc3bb 2018-12-29 stsp if (obj)
590 507dc3bb 2018-12-29 stsp got_object_close(obj);
591 507dc3bb 2018-12-29 stsp free(id_str);
592 507dc3bb 2018-12-29 stsp free(path_got);
593 507dc3bb 2018-12-29 stsp return err;
594 507dc3bb 2018-12-29 stsp }
595 507dc3bb 2018-12-29 stsp
596 9d31a1d8 2018-03-11 stsp static const struct got_error *
597 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
598 86c3caaf 2018-03-09 stsp {
599 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
602 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
603 86c3caaf 2018-03-09 stsp return NULL;
604 21908da4 2019-01-13 stsp }
605 21908da4 2019-01-13 stsp
606 21908da4 2019-01-13 stsp static const struct got_error *
607 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 4a1ddfc2 2019-01-12 stsp {
609 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
610 4a1ddfc2 2019-01-12 stsp char *abspath;
611 4a1ddfc2 2019-01-12 stsp
612 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
614 4a1ddfc2 2019-01-12 stsp
615 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
616 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
617 ddcd8544 2019-03-11 stsp struct stat sb;
618 ddcd8544 2019-03-11 stsp err = NULL;
619 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
620 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
621 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
622 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
623 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
624 ddcd8544 2019-03-11 stsp }
625 ddcd8544 2019-03-11 stsp }
626 4a1ddfc2 2019-01-12 stsp free(abspath);
627 68c76935 2019-02-19 stsp return err;
628 68c76935 2019-02-19 stsp }
629 68c76935 2019-02-19 stsp
630 68c76935 2019-02-19 stsp static const struct got_error *
631 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 68c76935 2019-02-19 stsp {
633 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
634 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
635 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
636 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
637 68c76935 2019-02-19 stsp
638 68c76935 2019-02-19 stsp *same = 1;
639 68c76935 2019-02-19 stsp
640 230a42bd 2019-05-11 jcs for (;;) {
641 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
643 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
644 68c76935 2019-02-19 stsp break;
645 68c76935 2019-02-19 stsp }
646 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
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 if (flen1 == 0) {
652 68c76935 2019-02-19 stsp if (flen2 != 0)
653 68c76935 2019-02-19 stsp *same = 0;
654 68c76935 2019-02-19 stsp break;
655 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
656 68c76935 2019-02-19 stsp if (flen1 != 0)
657 68c76935 2019-02-19 stsp *same = 0;
658 68c76935 2019-02-19 stsp break;
659 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
660 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
661 68c76935 2019-02-19 stsp *same = 0;
662 68c76935 2019-02-19 stsp break;
663 68c76935 2019-02-19 stsp }
664 68c76935 2019-02-19 stsp } else {
665 68c76935 2019-02-19 stsp *same = 0;
666 68c76935 2019-02-19 stsp break;
667 68c76935 2019-02-19 stsp }
668 68c76935 2019-02-19 stsp }
669 68c76935 2019-02-19 stsp
670 68c76935 2019-02-19 stsp return err;
671 68c76935 2019-02-19 stsp }
672 68c76935 2019-02-19 stsp
673 68c76935 2019-02-19 stsp static const struct got_error *
674 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 68c76935 2019-02-19 stsp {
676 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
677 68c76935 2019-02-19 stsp struct stat sb;
678 68c76935 2019-02-19 stsp size_t size1, size2;
679 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
680 68c76935 2019-02-19 stsp
681 68c76935 2019-02-19 stsp *same = 1;
682 68c76935 2019-02-19 stsp
683 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
684 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
685 68c76935 2019-02-19 stsp goto done;
686 68c76935 2019-02-19 stsp }
687 68c76935 2019-02-19 stsp size1 = sb.st_size;
688 68c76935 2019-02-19 stsp
689 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
691 68c76935 2019-02-19 stsp goto done;
692 68c76935 2019-02-19 stsp }
693 68c76935 2019-02-19 stsp size2 = sb.st_size;
694 68c76935 2019-02-19 stsp
695 68c76935 2019-02-19 stsp if (size1 != size2) {
696 68c76935 2019-02-19 stsp *same = 0;
697 68c76935 2019-02-19 stsp return NULL;
698 68c76935 2019-02-19 stsp }
699 68c76935 2019-02-19 stsp
700 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
701 68c76935 2019-02-19 stsp if (f1 == NULL)
702 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
703 68c76935 2019-02-19 stsp
704 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
705 68c76935 2019-02-19 stsp if (f2 == NULL) {
706 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
707 68c76935 2019-02-19 stsp goto done;
708 68c76935 2019-02-19 stsp }
709 68c76935 2019-02-19 stsp
710 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
711 68c76935 2019-02-19 stsp done:
712 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
713 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
714 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
715 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
716 68c76935 2019-02-19 stsp
717 6353ad76 2019-02-08 stsp return err;
718 6353ad76 2019-02-08 stsp }
719 6353ad76 2019-02-08 stsp
720 6353ad76 2019-02-08 stsp /*
721 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 46b6ee73 2019-06-04 stsp * blob_deriv acts as the first derived version, and the file on disk
723 234035bc 2019-06-01 stsp * acts as the second derived version.
724 6353ad76 2019-02-08 stsp */
725 6353ad76 2019-02-08 stsp static const struct got_error *
726 234035bc 2019-06-01 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
727 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
728 46b6ee73 2019-06-04 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
729 818c7501 2019-07-11 stsp struct got_object_id *deriv_base_commit_id,
730 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
731 234035bc 2019-06-01 stsp void *progress_arg)
732 6353ad76 2019-02-08 stsp {
733 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
734 6353ad76 2019-02-08 stsp int merged_fd = -1;
735 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
736 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
737 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
738 6353ad76 2019-02-08 stsp char *id_str = NULL;
739 818c7501 2019-07-11 stsp char *label_deriv = NULL;
740 234035bc 2019-06-01 stsp int overlapcnt = 0;
741 af54ae4a 2019-02-19 stsp char *parent;
742 6353ad76 2019-02-08 stsp
743 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
744 234035bc 2019-06-01 stsp
745 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
746 af54ae4a 2019-02-19 stsp if (parent == NULL)
747 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
748 af54ae4a 2019-02-19 stsp
749 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
750 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
751 af54ae4a 2019-02-19 stsp
752 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
753 6353ad76 2019-02-08 stsp if (err)
754 af54ae4a 2019-02-19 stsp goto done;
755 af54ae4a 2019-02-19 stsp
756 af54ae4a 2019-02-19 stsp free(base_path);
757 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
758 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
759 af54ae4a 2019-02-19 stsp base_path = NULL;
760 af54ae4a 2019-02-19 stsp goto done;
761 af54ae4a 2019-02-19 stsp }
762 af54ae4a 2019-02-19 stsp
763 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
764 6353ad76 2019-02-08 stsp if (err)
765 6353ad76 2019-02-08 stsp goto done;
766 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
767 6c4c42e0 2019-06-24 stsp blob_deriv);
768 6353ad76 2019-02-08 stsp if (err)
769 6353ad76 2019-02-08 stsp goto done;
770 6353ad76 2019-02-08 stsp
771 af54ae4a 2019-02-19 stsp free(base_path);
772 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
773 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
774 af54ae4a 2019-02-19 stsp base_path = NULL;
775 af54ae4a 2019-02-19 stsp goto done;
776 af54ae4a 2019-02-19 stsp }
777 af54ae4a 2019-02-19 stsp
778 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
779 6353ad76 2019-02-08 stsp if (err)
780 6353ad76 2019-02-08 stsp goto done;
781 46b6ee73 2019-06-04 stsp if (blob_orig) {
782 6c4c42e0 2019-06-24 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
783 46b6ee73 2019-06-04 stsp blob_orig);
784 1430b4e0 2019-03-27 stsp if (err)
785 1430b4e0 2019-03-27 stsp goto done;
786 1430b4e0 2019-03-27 stsp } else {
787 1430b4e0 2019-03-27 stsp /*
788 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
789 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
790 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
791 1430b4e0 2019-03-27 stsp */
792 1430b4e0 2019-03-27 stsp }
793 6353ad76 2019-02-08 stsp
794 818c7501 2019-07-11 stsp err = got_object_id_str(&id_str, deriv_base_commit_id);
795 6353ad76 2019-02-08 stsp if (err)
796 6353ad76 2019-02-08 stsp goto done;
797 818c7501 2019-07-11 stsp if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
798 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
799 6353ad76 2019-02-08 stsp goto done;
800 6353ad76 2019-02-08 stsp }
801 6353ad76 2019-02-08 stsp
802 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
803 818c7501 2019-07-11 stsp blob_orig_path, ondisk_path, label_deriv, path);
804 6353ad76 2019-02-08 stsp if (err)
805 6353ad76 2019-02-08 stsp goto done;
806 6353ad76 2019-02-08 stsp
807 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
808 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 1ee397ad 2019-07-12 stsp if (err)
810 1ee397ad 2019-07-12 stsp goto done;
811 6353ad76 2019-02-08 stsp
812 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
813 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
814 816dc654 2019-02-16 stsp goto done;
815 68c76935 2019-02-19 stsp }
816 68c76935 2019-02-19 stsp
817 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
818 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
819 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
820 68c76935 2019-02-19 stsp merged_path);
821 68c76935 2019-02-19 stsp if (err)
822 68c76935 2019-02-19 stsp goto done;
823 816dc654 2019-02-16 stsp }
824 6353ad76 2019-02-08 stsp
825 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
826 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
827 70a0c8ec 2019-02-20 stsp goto done;
828 70a0c8ec 2019-02-20 stsp }
829 70a0c8ec 2019-02-20 stsp
830 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
831 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
832 230a42bd 2019-05-11 jcs ondisk_path);
833 2a57020b 2019-02-20 stsp unlink(merged_path);
834 6353ad76 2019-02-08 stsp goto done;
835 6353ad76 2019-02-08 stsp }
836 6353ad76 2019-02-08 stsp
837 6353ad76 2019-02-08 stsp done:
838 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
839 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
840 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
841 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
842 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
843 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
844 6353ad76 2019-02-08 stsp free(merged_path);
845 af54ae4a 2019-02-19 stsp free(base_path);
846 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
847 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
848 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
849 6353ad76 2019-02-08 stsp }
850 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
851 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
852 46b6ee73 2019-06-04 stsp free(blob_orig_path);
853 6353ad76 2019-02-08 stsp }
854 6353ad76 2019-02-08 stsp free(id_str);
855 818c7501 2019-07-11 stsp free(label_deriv);
856 4a1ddfc2 2019-01-12 stsp return err;
857 4a1ddfc2 2019-01-12 stsp }
858 4a1ddfc2 2019-01-12 stsp
859 4a1ddfc2 2019-01-12 stsp static const struct got_error *
860 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
861 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
862 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
863 13d9040b 2019-03-27 stsp int update_timestamps)
864 13d9040b 2019-03-27 stsp {
865 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
866 13d9040b 2019-03-27 stsp
867 13d9040b 2019-03-27 stsp if (ie == NULL)
868 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
869 13d9040b 2019-03-27 stsp if (ie)
870 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
871 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
872 13d9040b 2019-03-27 stsp update_timestamps);
873 13d9040b 2019-03-27 stsp else {
874 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
875 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
876 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
877 13d9040b 2019-03-27 stsp if (!err)
878 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
879 13d9040b 2019-03-27 stsp }
880 13d9040b 2019-03-27 stsp return err;
881 13d9040b 2019-03-27 stsp }
882 13d9040b 2019-03-27 stsp
883 13d9040b 2019-03-27 stsp static const struct got_error *
884 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
885 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
886 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
887 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
888 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
889 9d31a1d8 2018-03-11 stsp {
890 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
891 507dc3bb 2018-12-29 stsp int fd = -1;
892 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
893 507dc3bb 2018-12-29 stsp int update = 0;
894 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
895 9d31a1d8 2018-03-11 stsp
896 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
897 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
898 9d31a1d8 2018-03-11 stsp if (fd == -1) {
899 21908da4 2019-01-13 stsp if (errno == ENOENT) {
900 21908da4 2019-01-13 stsp char *parent = dirname(path);
901 21908da4 2019-01-13 stsp if (parent == NULL)
902 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
903 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
904 21908da4 2019-01-13 stsp if (err)
905 21908da4 2019-01-13 stsp return err;
906 21908da4 2019-01-13 stsp fd = open(ondisk_path,
907 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
908 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
909 21908da4 2019-01-13 stsp if (fd == -1)
910 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
911 230a42bd 2019-05-11 jcs ondisk_path);
912 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
913 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
914 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
915 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
916 507dc3bb 2018-12-29 stsp goto done;
917 d70b8e30 2018-12-27 stsp } else {
918 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
919 507dc3bb 2018-12-29 stsp ondisk_path);
920 507dc3bb 2018-12-29 stsp if (err)
921 507dc3bb 2018-12-29 stsp goto done;
922 507dc3bb 2018-12-29 stsp update = 1;
923 9d31a1d8 2018-03-11 stsp }
924 507dc3bb 2018-12-29 stsp } else
925 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
926 9d31a1d8 2018-03-11 stsp }
927 9d31a1d8 2018-03-11 stsp
928 a378724f 2019-02-10 stsp if (restoring_missing_file)
929 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
930 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
931 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
932 a378724f 2019-02-10 stsp else
933 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg,
934 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
935 1ee397ad 2019-07-12 stsp if (err)
936 1ee397ad 2019-07-12 stsp goto done;
937 d7b62c98 2018-12-27 stsp
938 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
939 9d31a1d8 2018-03-11 stsp do {
940 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
941 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
942 9d31a1d8 2018-03-11 stsp if (err)
943 9d31a1d8 2018-03-11 stsp break;
944 9d31a1d8 2018-03-11 stsp if (len > 0) {
945 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
946 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
947 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
948 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
949 b87c6f83 2018-12-24 stsp goto done;
950 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
951 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
952 b87c6f83 2018-12-24 stsp goto done;
953 9d31a1d8 2018-03-11 stsp }
954 61d6eaa3 2018-12-24 stsp hdrlen = 0;
955 9d31a1d8 2018-03-11 stsp }
956 9d31a1d8 2018-03-11 stsp } while (len != 0);
957 9d31a1d8 2018-03-11 stsp
958 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
959 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
960 816dc654 2019-02-16 stsp goto done;
961 816dc654 2019-02-16 stsp }
962 9d31a1d8 2018-03-11 stsp
963 507dc3bb 2018-12-29 stsp if (update) {
964 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
965 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
966 230a42bd 2019-05-11 jcs ondisk_path);
967 2a57020b 2019-02-20 stsp unlink(tmppath);
968 68ed9ba5 2019-02-10 stsp goto done;
969 68ed9ba5 2019-02-10 stsp }
970 68ed9ba5 2019-02-10 stsp }
971 ba8a0d4d 2019-02-10 stsp
972 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
973 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
974 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
975 507dc3bb 2018-12-29 stsp goto done;
976 507dc3bb 2018-12-29 stsp }
977 ba8a0d4d 2019-02-10 stsp } else {
978 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
979 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
980 68ed9ba5 2019-02-10 stsp goto done;
981 68ed9ba5 2019-02-10 stsp }
982 507dc3bb 2018-12-29 stsp }
983 507dc3bb 2018-12-29 stsp
984 9d31a1d8 2018-03-11 stsp done:
985 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
986 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
987 507dc3bb 2018-12-29 stsp free(tmppath);
988 6353ad76 2019-02-08 stsp return err;
989 6353ad76 2019-02-08 stsp }
990 6353ad76 2019-02-08 stsp
991 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
992 6353ad76 2019-02-08 stsp static const struct got_error *
993 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
994 7154f6ce 2019-03-27 stsp {
995 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
996 7154f6ce 2019-03-27 stsp const char *markers[3] = {
997 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
998 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
999 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1000 7154f6ce 2019-03-27 stsp };
1001 7154f6ce 2019-03-27 stsp int i = 0;
1002 7154f6ce 2019-03-27 stsp char *line;
1003 7154f6ce 2019-03-27 stsp size_t len;
1004 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1005 7154f6ce 2019-03-27 stsp
1006 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1007 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1008 7154f6ce 2019-03-27 stsp if (line == NULL) {
1009 7154f6ce 2019-03-27 stsp if (feof(f))
1010 7154f6ce 2019-03-27 stsp break;
1011 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1012 7154f6ce 2019-03-27 stsp break;
1013 7154f6ce 2019-03-27 stsp }
1014 7154f6ce 2019-03-27 stsp
1015 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1016 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1017 19332e6d 2019-05-13 stsp == 0)
1018 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1019 7154f6ce 2019-03-27 stsp else
1020 7154f6ce 2019-03-27 stsp i++;
1021 7154f6ce 2019-03-27 stsp }
1022 7154f6ce 2019-03-27 stsp }
1023 7154f6ce 2019-03-27 stsp
1024 7154f6ce 2019-03-27 stsp return err;
1025 e2b1e152 2019-07-13 stsp }
1026 e2b1e152 2019-07-13 stsp
1027 e2b1e152 2019-07-13 stsp static int
1028 e2b1e152 2019-07-13 stsp stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1029 e2b1e152 2019-07-13 stsp {
1030 e2b1e152 2019-07-13 stsp return !(ie->ctime_sec == sb->st_ctime &&
1031 e2b1e152 2019-07-13 stsp ie->ctime_nsec == sb->st_ctimensec &&
1032 e2b1e152 2019-07-13 stsp ie->mtime_sec == sb->st_mtime &&
1033 e2b1e152 2019-07-13 stsp ie->mtime_nsec == sb->st_mtimensec &&
1034 e2b1e152 2019-07-13 stsp ie->size == (sb->st_size & 0xffffffff));
1035 7154f6ce 2019-03-27 stsp }
1036 7154f6ce 2019-03-27 stsp
1037 7154f6ce 2019-03-27 stsp static const struct got_error *
1038 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1039 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1040 b8f41171 2019-02-10 stsp struct got_repository *repo)
1041 6353ad76 2019-02-08 stsp {
1042 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1043 6353ad76 2019-02-08 stsp struct got_object_id id;
1044 6353ad76 2019-02-08 stsp size_t hdrlen;
1045 6353ad76 2019-02-08 stsp FILE *f = NULL;
1046 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1047 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1048 6353ad76 2019-02-08 stsp size_t flen, blen;
1049 6353ad76 2019-02-08 stsp
1050 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1051 6353ad76 2019-02-08 stsp
1052 339c298e 2019-07-27 stsp if (lstat(abspath, sb) == -1) {
1053 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1054 abb4604f 2019-07-27 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1055 abb4604f 2019-07-27 stsp *status = GOT_STATUS_MISSING;
1056 abb4604f 2019-07-27 stsp else
1057 abb4604f 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1058 a378724f 2019-02-10 stsp return NULL;
1059 a378724f 2019-02-10 stsp }
1060 339c298e 2019-07-27 stsp return got_error_from_errno2("lstat", abspath);
1061 a378724f 2019-02-10 stsp }
1062 6353ad76 2019-02-08 stsp
1063 339c298e 2019-07-27 stsp if (!S_ISREG(sb->st_mode)) {
1064 339c298e 2019-07-27 stsp *status = GOT_STATUS_OBSTRUCTED;
1065 339c298e 2019-07-27 stsp return NULL;
1066 3f148bc6 2019-07-27 stsp }
1067 efdd40df 2019-07-27 stsp
1068 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1069 339c298e 2019-07-27 stsp *status = GOT_STATUS_DELETE;
1070 339c298e 2019-07-27 stsp return NULL;
1071 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1072 339c298e 2019-07-27 stsp *status = GOT_STATUS_ADD;
1073 339c298e 2019-07-27 stsp return NULL;
1074 d00136be 2019-03-26 stsp }
1075 b8f41171 2019-02-10 stsp
1076 e2b1e152 2019-07-13 stsp if (!stat_info_differs(ie, sb))
1077 339c298e 2019-07-27 stsp return NULL;
1078 6353ad76 2019-02-08 stsp
1079 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1080 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1081 6353ad76 2019-02-08 stsp if (err)
1082 339c298e 2019-07-27 stsp return err;
1083 6353ad76 2019-02-08 stsp
1084 339c298e 2019-07-27 stsp f = fopen(abspath, "r");
1085 6353ad76 2019-02-08 stsp if (f == NULL) {
1086 339c298e 2019-07-27 stsp err = got_error_from_errno2("fopen", abspath);
1087 6353ad76 2019-02-08 stsp goto done;
1088 6353ad76 2019-02-08 stsp }
1089 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1090 656b1f76 2019-05-11 jcs for (;;) {
1091 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1092 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1093 6353ad76 2019-02-08 stsp if (err)
1094 7154f6ce 2019-03-27 stsp goto done;
1095 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1096 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1097 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1098 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1099 7154f6ce 2019-03-27 stsp goto done;
1100 80c5c120 2019-02-19 stsp }
1101 6353ad76 2019-02-08 stsp if (blen == 0) {
1102 6353ad76 2019-02-08 stsp if (flen != 0)
1103 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1104 6353ad76 2019-02-08 stsp break;
1105 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1106 6353ad76 2019-02-08 stsp if (blen != 0)
1107 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1108 6353ad76 2019-02-08 stsp break;
1109 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1110 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1111 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1112 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1113 6353ad76 2019-02-08 stsp break;
1114 6353ad76 2019-02-08 stsp }
1115 6353ad76 2019-02-08 stsp } else {
1116 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1117 6353ad76 2019-02-08 stsp break;
1118 6353ad76 2019-02-08 stsp }
1119 6353ad76 2019-02-08 stsp hdrlen = 0;
1120 6353ad76 2019-02-08 stsp }
1121 7154f6ce 2019-03-27 stsp
1122 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1123 7154f6ce 2019-03-27 stsp rewind(f);
1124 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1125 7154f6ce 2019-03-27 stsp }
1126 6353ad76 2019-02-08 stsp done:
1127 6353ad76 2019-02-08 stsp if (blob)
1128 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1129 6353ad76 2019-02-08 stsp if (f)
1130 6353ad76 2019-02-08 stsp fclose(f);
1131 9d31a1d8 2018-03-11 stsp return err;
1132 e2b1e152 2019-07-13 stsp }
1133 e2b1e152 2019-07-13 stsp
1134 e2b1e152 2019-07-13 stsp /*
1135 e2b1e152 2019-07-13 stsp * Update timestamps in the file index if a file is unmodified and
1136 e2b1e152 2019-07-13 stsp * we had to run a full content comparison to find out.
1137 e2b1e152 2019-07-13 stsp */
1138 e2b1e152 2019-07-13 stsp static const struct got_error *
1139 e2b1e152 2019-07-13 stsp sync_timestamps(char *ondisk_path, unsigned char status,
1140 e2b1e152 2019-07-13 stsp struct got_fileindex_entry *ie, struct stat *sb)
1141 e2b1e152 2019-07-13 stsp {
1142 e2b1e152 2019-07-13 stsp if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1143 e2b1e152 2019-07-13 stsp return got_fileindex_entry_update(ie, ondisk_path,
1144 e2b1e152 2019-07-13 stsp ie->blob_sha1, ie->commit_sha1, 1);
1145 e2b1e152 2019-07-13 stsp
1146 e2b1e152 2019-07-13 stsp return NULL;
1147 9d31a1d8 2018-03-11 stsp }
1148 9d31a1d8 2018-03-11 stsp
1149 9d31a1d8 2018-03-11 stsp static const struct got_error *
1150 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1151 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1152 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1153 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1154 0584f854 2019-04-06 stsp void *progress_arg)
1155 9d31a1d8 2018-03-11 stsp {
1156 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1157 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1158 6353ad76 2019-02-08 stsp char *ondisk_path;
1159 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1160 b8f41171 2019-02-10 stsp struct stat sb;
1161 9d31a1d8 2018-03-11 stsp
1162 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1163 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1164 6353ad76 2019-02-08 stsp
1165 abb4604f 2019-07-27 stsp if (ie) {
1166 abb4604f 2019-07-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1167 abb4604f 2019-07-27 stsp if (err)
1168 abb4604f 2019-07-27 stsp goto done;
1169 abb4604f 2019-07-27 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1170 abb4604f 2019-07-27 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
1171 abb4604f 2019-07-27 stsp } else
1172 abb4604f 2019-07-27 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1173 6353ad76 2019-02-08 stsp
1174 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1175 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, status, path);
1176 b8f41171 2019-02-10 stsp goto done;
1177 b8f41171 2019-02-10 stsp }
1178 b8f41171 2019-02-10 stsp
1179 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1180 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1181 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1182 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1183 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1184 e2b1e152 2019-07-13 stsp if (err)
1185 e2b1e152 2019-07-13 stsp goto done;
1186 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1187 b8f41171 2019-02-10 stsp path);
1188 6353ad76 2019-02-08 stsp goto done;
1189 a378724f 2019-02-10 stsp }
1190 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1191 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1192 e2b1e152 2019-07-13 stsp SHA1_DIGEST_LENGTH) == 0) {
1193 e2b1e152 2019-07-13 stsp err = sync_timestamps(ondisk_path, status, ie, &sb);
1194 b8f41171 2019-02-10 stsp goto done;
1195 e2b1e152 2019-07-13 stsp }
1196 9d31a1d8 2018-03-11 stsp }
1197 9d31a1d8 2018-03-11 stsp
1198 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1199 8da9e5f4 2019-01-12 stsp if (err)
1200 6353ad76 2019-02-08 stsp goto done;
1201 512f0d0e 2019-01-02 stsp
1202 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1203 234035bc 2019-06-01 stsp int update_timestamps;
1204 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1205 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1206 234035bc 2019-06-01 stsp struct got_object_id id2;
1207 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1208 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1209 234035bc 2019-06-01 stsp if (err)
1210 234035bc 2019-06-01 stsp goto done;
1211 234035bc 2019-06-01 stsp }
1212 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1213 818c7501 2019-07-11 stsp ondisk_path, path, sb.st_mode, blob,
1214 818c7501 2019-07-11 stsp worktree->base_commit_id, repo,
1215 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1216 234035bc 2019-06-01 stsp if (blob2)
1217 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1218 234035bc 2019-06-01 stsp /*
1219 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1220 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1221 234035bc 2019-06-01 stsp * unmodified files again.
1222 234035bc 2019-06-01 stsp */
1223 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1224 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1225 234035bc 2019-06-01 stsp update_timestamps);
1226 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1227 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1228 1ee397ad 2019-07-12 stsp if (err)
1229 1ee397ad 2019-07-12 stsp goto done;
1230 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1231 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1232 13d9040b 2019-03-27 stsp if (err)
1233 13d9040b 2019-03-27 stsp goto done;
1234 13d9040b 2019-03-27 stsp } else {
1235 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1236 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1237 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1238 13d9040b 2019-03-27 stsp if (err)
1239 13d9040b 2019-03-27 stsp goto done;
1240 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1241 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1242 13d9040b 2019-03-27 stsp if (err)
1243 13d9040b 2019-03-27 stsp goto done;
1244 13d9040b 2019-03-27 stsp }
1245 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1246 6353ad76 2019-02-08 stsp done:
1247 6353ad76 2019-02-08 stsp free(ondisk_path);
1248 8da9e5f4 2019-01-12 stsp return err;
1249 90285c3b 2019-01-08 stsp }
1250 90285c3b 2019-01-08 stsp
1251 90285c3b 2019-01-08 stsp static const struct got_error *
1252 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1253 90285c3b 2019-01-08 stsp {
1254 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1255 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1256 90285c3b 2019-01-08 stsp
1257 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1258 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1259 90285c3b 2019-01-08 stsp
1260 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1261 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1262 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1263 c97665ae 2019-01-13 stsp } else {
1264 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1265 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1266 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1267 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1268 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1269 230a42bd 2019-05-11 jcs parent);
1270 90285c3b 2019-01-08 stsp break;
1271 90285c3b 2019-01-08 stsp }
1272 90285c3b 2019-01-08 stsp parent = dirname(parent);
1273 90285c3b 2019-01-08 stsp }
1274 90285c3b 2019-01-08 stsp }
1275 90285c3b 2019-01-08 stsp free(ondisk_path);
1276 90285c3b 2019-01-08 stsp return err;
1277 512f0d0e 2019-01-02 stsp }
1278 512f0d0e 2019-01-02 stsp
1279 708d8e67 2019-03-27 stsp static const struct got_error *
1280 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1281 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1282 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1283 708d8e67 2019-03-27 stsp {
1284 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1285 708d8e67 2019-03-27 stsp unsigned char status;
1286 708d8e67 2019-03-27 stsp struct stat sb;
1287 708d8e67 2019-03-27 stsp char *ondisk_path;
1288 708d8e67 2019-03-27 stsp
1289 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1290 708d8e67 2019-03-27 stsp == -1)
1291 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1292 708d8e67 2019-03-27 stsp
1293 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1294 708d8e67 2019-03-27 stsp if (err)
1295 708d8e67 2019-03-27 stsp return err;
1296 708d8e67 2019-03-27 stsp
1297 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1298 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1299 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1300 1ee397ad 2019-07-12 stsp if (err)
1301 1ee397ad 2019-07-12 stsp return err;
1302 fc6346c4 2019-03-27 stsp /*
1303 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1304 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1305 fc6346c4 2019-03-27 stsp */
1306 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1307 fc6346c4 2019-03-27 stsp 0);
1308 708d8e67 2019-03-27 stsp if (err)
1309 708d8e67 2019-03-27 stsp return err;
1310 fc6346c4 2019-03-27 stsp } else {
1311 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1312 1ee397ad 2019-07-12 stsp if (err)
1313 1ee397ad 2019-07-12 stsp return err;
1314 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1315 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1316 fc6346c4 2019-03-27 stsp if (err)
1317 fc6346c4 2019-03-27 stsp return err;
1318 fc6346c4 2019-03-27 stsp }
1319 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1320 708d8e67 2019-03-27 stsp }
1321 708d8e67 2019-03-27 stsp
1322 fc6346c4 2019-03-27 stsp return err;
1323 708d8e67 2019-03-27 stsp }
1324 708d8e67 2019-03-27 stsp
1325 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1326 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1327 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1328 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1329 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1330 8da9e5f4 2019-01-12 stsp void *progress_arg;
1331 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1332 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1333 8da9e5f4 2019-01-12 stsp };
1334 8da9e5f4 2019-01-12 stsp
1335 512f0d0e 2019-01-02 stsp static const struct got_error *
1336 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1337 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1338 512f0d0e 2019-01-02 stsp {
1339 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1340 0584f854 2019-04-06 stsp
1341 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1342 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1343 512f0d0e 2019-01-02 stsp
1344 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1345 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1346 8da9e5f4 2019-01-12 stsp }
1347 512f0d0e 2019-01-02 stsp
1348 8da9e5f4 2019-01-12 stsp static const struct got_error *
1349 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1350 8da9e5f4 2019-01-12 stsp {
1351 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1352 7a9df742 2019-01-08 stsp
1353 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1354 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1355 0584f854 2019-04-06 stsp
1356 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1357 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1358 9d31a1d8 2018-03-11 stsp }
1359 9d31a1d8 2018-03-11 stsp
1360 9d31a1d8 2018-03-11 stsp static const struct got_error *
1361 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1362 9d31a1d8 2018-03-11 stsp {
1363 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1364 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1365 8da9e5f4 2019-01-12 stsp char *path;
1366 9d31a1d8 2018-03-11 stsp
1367 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1368 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1369 0584f854 2019-04-06 stsp
1370 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1371 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1372 8da9e5f4 2019-01-12 stsp == -1)
1373 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1374 9d31a1d8 2018-03-11 stsp
1375 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1376 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1377 8da9e5f4 2019-01-12 stsp else
1378 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1379 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1380 9d31a1d8 2018-03-11 stsp
1381 8da9e5f4 2019-01-12 stsp free(path);
1382 0cd1c46a 2019-03-11 stsp return err;
1383 0cd1c46a 2019-03-11 stsp }
1384 0cd1c46a 2019-03-11 stsp
1385 818c7501 2019-07-11 stsp static const struct got_error *
1386 818c7501 2019-07-11 stsp get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1387 0cd1c46a 2019-03-11 stsp {
1388 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1389 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1390 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1391 0cd1c46a 2019-03-11 stsp
1392 517bab73 2019-03-11 stsp *refname = NULL;
1393 517bab73 2019-03-11 stsp
1394 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1395 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1396 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1397 0cd1c46a 2019-03-11 stsp
1398 818c7501 2019-07-11 stsp if (asprintf(refname, "%s-%s", prefix, uuidstr)
1399 0647c563 2019-03-11 stsp == -1) {
1400 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1401 0647c563 2019-03-11 stsp *refname = NULL;
1402 0cd1c46a 2019-03-11 stsp }
1403 517bab73 2019-03-11 stsp free(uuidstr);
1404 517bab73 2019-03-11 stsp return err;
1405 517bab73 2019-03-11 stsp }
1406 517bab73 2019-03-11 stsp
1407 818c7501 2019-07-11 stsp const struct got_error *
1408 818c7501 2019-07-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1409 818c7501 2019-07-11 stsp {
1410 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1411 818c7501 2019-07-11 stsp }
1412 818c7501 2019-07-11 stsp
1413 818c7501 2019-07-11 stsp static const struct got_error *
1414 818c7501 2019-07-11 stsp get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1415 818c7501 2019-07-11 stsp {
1416 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1417 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1418 818c7501 2019-07-11 stsp }
1419 818c7501 2019-07-11 stsp
1420 818c7501 2019-07-11 stsp static const struct got_error *
1421 818c7501 2019-07-11 stsp get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1422 818c7501 2019-07-11 stsp {
1423 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1424 818c7501 2019-07-11 stsp }
1425 818c7501 2019-07-11 stsp
1426 818c7501 2019-07-11 stsp static const struct got_error *
1427 818c7501 2019-07-11 stsp get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1428 818c7501 2019-07-11 stsp {
1429 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1430 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1431 818c7501 2019-07-11 stsp }
1432 818c7501 2019-07-11 stsp
1433 818c7501 2019-07-11 stsp static const struct got_error *
1434 818c7501 2019-07-11 stsp get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1435 818c7501 2019-07-11 stsp {
1436 818c7501 2019-07-11 stsp return get_ref_name(refname, worktree,
1437 818c7501 2019-07-11 stsp GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1438 0ebf8283 2019-07-24 stsp }
1439 0ebf8283 2019-07-24 stsp
1440 0ebf8283 2019-07-24 stsp static const struct got_error *
1441 0ebf8283 2019-07-24 stsp get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1442 0ebf8283 2019-07-24 stsp {
1443 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1444 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1445 0ebf8283 2019-07-24 stsp }
1446 0ebf8283 2019-07-24 stsp
1447 0ebf8283 2019-07-24 stsp static const struct got_error *
1448 0ebf8283 2019-07-24 stsp get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1449 0ebf8283 2019-07-24 stsp {
1450 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1451 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1452 0ebf8283 2019-07-24 stsp }
1453 0ebf8283 2019-07-24 stsp
1454 0ebf8283 2019-07-24 stsp static const struct got_error *
1455 0ebf8283 2019-07-24 stsp get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1456 0ebf8283 2019-07-24 stsp {
1457 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1458 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1459 818c7501 2019-07-11 stsp }
1460 818c7501 2019-07-11 stsp
1461 0ebf8283 2019-07-24 stsp static const struct got_error *
1462 0ebf8283 2019-07-24 stsp get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1463 0ebf8283 2019-07-24 stsp {
1464 0ebf8283 2019-07-24 stsp return get_ref_name(refname, worktree,
1465 0ebf8283 2019-07-24 stsp GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1466 0ebf8283 2019-07-24 stsp }
1467 818c7501 2019-07-11 stsp
1468 0ebf8283 2019-07-24 stsp const struct got_error *
1469 c3022ba5 2019-07-27 stsp got_worktree_get_histedit_script_path(char **path,
1470 c3022ba5 2019-07-27 stsp struct got_worktree *worktree)
1471 0ebf8283 2019-07-24 stsp {
1472 0ebf8283 2019-07-24 stsp if (asprintf(path, "%s/%s/%s", worktree->root_path,
1473 c3022ba5 2019-07-27 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1474 0ebf8283 2019-07-24 stsp *path = NULL;
1475 0ebf8283 2019-07-24 stsp return got_error_from_errno("asprintf");
1476 0ebf8283 2019-07-24 stsp }
1477 0ebf8283 2019-07-24 stsp return NULL;
1478 0ebf8283 2019-07-24 stsp }
1479 0ebf8283 2019-07-24 stsp
1480 517bab73 2019-03-11 stsp /*
1481 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1482 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1483 517bab73 2019-03-11 stsp */
1484 517bab73 2019-03-11 stsp static const struct got_error *
1485 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1486 517bab73 2019-03-11 stsp {
1487 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1488 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1489 517bab73 2019-03-11 stsp char *refname;
1490 517bab73 2019-03-11 stsp
1491 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1492 517bab73 2019-03-11 stsp if (err)
1493 517bab73 2019-03-11 stsp return err;
1494 0cd1c46a 2019-03-11 stsp
1495 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1496 0cd1c46a 2019-03-11 stsp if (err)
1497 0cd1c46a 2019-03-11 stsp goto done;
1498 0cd1c46a 2019-03-11 stsp
1499 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1500 0cd1c46a 2019-03-11 stsp done:
1501 0cd1c46a 2019-03-11 stsp free(refname);
1502 0cd1c46a 2019-03-11 stsp if (ref)
1503 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1504 3e3a69f1 2019-07-25 stsp return err;
1505 3e3a69f1 2019-07-25 stsp }
1506 3e3a69f1 2019-07-25 stsp
1507 3e3a69f1 2019-07-25 stsp static const struct got_error *
1508 3e3a69f1 2019-07-25 stsp get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1509 3e3a69f1 2019-07-25 stsp {
1510 3e3a69f1 2019-07-25 stsp const struct got_error *err = NULL;
1511 3e3a69f1 2019-07-25 stsp
1512 3e3a69f1 2019-07-25 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1513 3e3a69f1 2019-07-25 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1514 3e3a69f1 2019-07-25 stsp err = got_error_from_errno("asprintf");
1515 3e3a69f1 2019-07-25 stsp *fileindex_path = NULL;
1516 3e3a69f1 2019-07-25 stsp }
1517 9d31a1d8 2018-03-11 stsp return err;
1518 9d31a1d8 2018-03-11 stsp }
1519 9d31a1d8 2018-03-11 stsp
1520 3e3a69f1 2019-07-25 stsp
1521 ebf99748 2019-05-09 stsp static const struct got_error *
1522 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1523 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1524 ebf99748 2019-05-09 stsp {
1525 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1526 ebf99748 2019-05-09 stsp FILE *index = NULL;
1527 0cd1c46a 2019-03-11 stsp
1528 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1529 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1530 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1531 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1532 ebf99748 2019-05-09 stsp
1533 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(fileindex_path, worktree);
1534 3e3a69f1 2019-07-25 stsp if (err)
1535 ebf99748 2019-05-09 stsp goto done;
1536 ebf99748 2019-05-09 stsp
1537 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1538 ebf99748 2019-05-09 stsp if (index == NULL) {
1539 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1540 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1541 ebf99748 2019-05-09 stsp } else {
1542 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1543 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1544 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1545 ebf99748 2019-05-09 stsp }
1546 ebf99748 2019-05-09 stsp done:
1547 ebf99748 2019-05-09 stsp if (err) {
1548 ebf99748 2019-05-09 stsp free(*fileindex_path);
1549 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1550 950a4a90 2019-07-10 stsp got_fileindex_free(*fileindex);
1551 ebf99748 2019-05-09 stsp *fileindex = NULL;
1552 ebf99748 2019-05-09 stsp }
1553 ebf99748 2019-05-09 stsp return err;
1554 ebf99748 2019-05-09 stsp }
1555 c932eeeb 2019-05-22 stsp
1556 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1557 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1558 c932eeeb 2019-05-22 stsp const char *path;
1559 c932eeeb 2019-05-22 stsp size_t path_len;
1560 c932eeeb 2019-05-22 stsp const char *entry_name;
1561 a484d721 2019-06-10 stsp got_worktree_checkout_cb progress_cb;
1562 a484d721 2019-06-10 stsp void *progress_arg;
1563 c932eeeb 2019-05-22 stsp };
1564 ebf99748 2019-05-09 stsp
1565 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1566 c932eeeb 2019-05-22 stsp static const struct got_error *
1567 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1568 c932eeeb 2019-05-22 stsp {
1569 1ee397ad 2019-07-12 stsp const struct got_error *err;
1570 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1571 c932eeeb 2019-05-22 stsp
1572 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1573 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1574 c932eeeb 2019-05-22 stsp return NULL;
1575 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1576 102ff934 2019-06-11 stsp return NULL;
1577 102ff934 2019-06-11 stsp
1578 102ff934 2019-06-11 stsp if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1579 102ff934 2019-06-11 stsp SHA1_DIGEST_LENGTH) == 0)
1580 c932eeeb 2019-05-22 stsp return NULL;
1581 c932eeeb 2019-05-22 stsp
1582 1ee397ad 2019-07-12 stsp if (a->progress_cb) {
1583 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1584 edd02c5e 2019-07-11 stsp ie->path);
1585 1ee397ad 2019-07-12 stsp if (err)
1586 1ee397ad 2019-07-12 stsp return err;
1587 1ee397ad 2019-07-12 stsp }
1588 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1589 c932eeeb 2019-05-22 stsp return NULL;
1590 9c6338c4 2019-06-02 stsp }
1591 9c6338c4 2019-06-02 stsp
1592 9c6338c4 2019-06-02 stsp static const struct got_error *
1593 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1594 9c6338c4 2019-06-02 stsp {
1595 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1596 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1597 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1598 9c6338c4 2019-06-02 stsp
1599 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1600 9c6338c4 2019-06-02 stsp fileindex_path);
1601 9c6338c4 2019-06-02 stsp if (err)
1602 9c6338c4 2019-06-02 stsp goto done;
1603 9c6338c4 2019-06-02 stsp
1604 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1605 9c6338c4 2019-06-02 stsp if (err)
1606 9c6338c4 2019-06-02 stsp goto done;
1607 9c6338c4 2019-06-02 stsp
1608 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1609 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1610 9c6338c4 2019-06-02 stsp fileindex_path);
1611 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1612 9c6338c4 2019-06-02 stsp }
1613 9c6338c4 2019-06-02 stsp done:
1614 9c6338c4 2019-06-02 stsp if (new_index)
1615 9c6338c4 2019-06-02 stsp fclose(new_index);
1616 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1617 9c6338c4 2019-06-02 stsp return err;
1618 c932eeeb 2019-05-22 stsp }
1619 6ced4176 2019-07-12 stsp
1620 6ced4176 2019-07-12 stsp static const struct got_error *
1621 6ced4176 2019-07-12 stsp find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1622 6ced4176 2019-07-12 stsp struct got_object_id **tree_id, const char *wt_relpath,
1623 6ced4176 2019-07-12 stsp struct got_worktree *worktree, struct got_repository *repo)
1624 6ced4176 2019-07-12 stsp {
1625 6ced4176 2019-07-12 stsp const struct got_error *err = NULL;
1626 6ced4176 2019-07-12 stsp struct got_object_id *id = NULL;
1627 6ced4176 2019-07-12 stsp char *in_repo_path = NULL;
1628 6ced4176 2019-07-12 stsp int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1629 6ced4176 2019-07-12 stsp
1630 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1631 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1632 6ced4176 2019-07-12 stsp *tree_id = NULL;
1633 6ced4176 2019-07-12 stsp
1634 6ced4176 2019-07-12 stsp if (wt_relpath[0] == '\0') {
1635 6ced4176 2019-07-12 stsp /* Check out all files within the work tree. */
1636 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_TREE;
1637 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1638 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1639 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1640 6ced4176 2019-07-12 stsp goto done;
1641 6ced4176 2019-07-12 stsp }
1642 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1643 6ced4176 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
1644 6ced4176 2019-07-12 stsp if (err)
1645 6ced4176 2019-07-12 stsp goto done;
1646 6ced4176 2019-07-12 stsp return NULL;
1647 6ced4176 2019-07-12 stsp }
1648 6ced4176 2019-07-12 stsp
1649 6ced4176 2019-07-12 stsp /* Check out a subset of files in the work tree. */
1650 6ced4176 2019-07-12 stsp
1651 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1652 6ced4176 2019-07-12 stsp is_root_wt ? "" : "/", wt_relpath) == -1) {
1653 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1654 6ced4176 2019-07-12 stsp goto done;
1655 6ced4176 2019-07-12 stsp }
1656 6ced4176 2019-07-12 stsp
1657 6ced4176 2019-07-12 stsp err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1658 6ced4176 2019-07-12 stsp in_repo_path);
1659 6ced4176 2019-07-12 stsp if (err)
1660 6ced4176 2019-07-12 stsp goto done;
1661 6ced4176 2019-07-12 stsp
1662 6ced4176 2019-07-12 stsp free(in_repo_path);
1663 6ced4176 2019-07-12 stsp in_repo_path = NULL;
1664 6ced4176 2019-07-12 stsp
1665 6ced4176 2019-07-12 stsp err = got_object_get_type(entry_type, repo, id);
1666 6ced4176 2019-07-12 stsp if (err)
1667 6ced4176 2019-07-12 stsp goto done;
1668 c932eeeb 2019-05-22 stsp
1669 6ced4176 2019-07-12 stsp if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1670 6ced4176 2019-07-12 stsp /* Check out a single file. */
1671 6ced4176 2019-07-12 stsp if (strchr(wt_relpath, '/') == NULL) {
1672 6ced4176 2019-07-12 stsp /* Check out a single file in work tree's root dir. */
1673 6ced4176 2019-07-12 stsp in_repo_path = strdup(worktree->path_prefix);
1674 6ced4176 2019-07-12 stsp if (in_repo_path == NULL) {
1675 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1676 6ced4176 2019-07-12 stsp goto done;
1677 6ced4176 2019-07-12 stsp }
1678 6ced4176 2019-07-12 stsp *tree_relpath = strdup("");
1679 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1680 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1681 6ced4176 2019-07-12 stsp goto done;
1682 6ced4176 2019-07-12 stsp }
1683 6ced4176 2019-07-12 stsp } else {
1684 6ced4176 2019-07-12 stsp /* Check out a single file in a subdirectory. */
1685 6ced4176 2019-07-12 stsp err = got_path_dirname(tree_relpath, wt_relpath);
1686 6ced4176 2019-07-12 stsp if (err)
1687 6ced4176 2019-07-12 stsp return err;
1688 6ced4176 2019-07-12 stsp if (asprintf(&in_repo_path, "%s%s%s",
1689 6ced4176 2019-07-12 stsp worktree->path_prefix, is_root_wt ? "" : "/",
1690 6ced4176 2019-07-12 stsp *tree_relpath) == -1) {
1691 6ced4176 2019-07-12 stsp err = got_error_from_errno("asprintf");
1692 6ced4176 2019-07-12 stsp goto done;
1693 6ced4176 2019-07-12 stsp }
1694 6ced4176 2019-07-12 stsp }
1695 6ced4176 2019-07-12 stsp err = got_object_id_by_path(tree_id, repo,
1696 6ced4176 2019-07-12 stsp worktree->base_commit_id, in_repo_path);
1697 6ced4176 2019-07-12 stsp } else {
1698 6ced4176 2019-07-12 stsp /* Check out all files within a subdirectory. */
1699 6ced4176 2019-07-12 stsp *tree_id = got_object_id_dup(id);
1700 6ced4176 2019-07-12 stsp if (*tree_id == NULL) {
1701 6ced4176 2019-07-12 stsp err = got_error_from_errno("got_object_id_dup");
1702 6ced4176 2019-07-12 stsp goto done;
1703 6ced4176 2019-07-12 stsp }
1704 6ced4176 2019-07-12 stsp *tree_relpath = strdup(wt_relpath);
1705 6ced4176 2019-07-12 stsp if (*tree_relpath == NULL) {
1706 6ced4176 2019-07-12 stsp err = got_error_from_errno("strdup");
1707 6ced4176 2019-07-12 stsp goto done;
1708 6ced4176 2019-07-12 stsp }
1709 6ced4176 2019-07-12 stsp }
1710 6ced4176 2019-07-12 stsp done:
1711 6ced4176 2019-07-12 stsp free(id);
1712 6ced4176 2019-07-12 stsp free(in_repo_path);
1713 6ced4176 2019-07-12 stsp if (err) {
1714 6ced4176 2019-07-12 stsp *entry_type = GOT_OBJ_TYPE_ANY;
1715 6ced4176 2019-07-12 stsp free(*tree_relpath);
1716 6ced4176 2019-07-12 stsp *tree_relpath = NULL;
1717 6ced4176 2019-07-12 stsp free(*tree_id);
1718 6ced4176 2019-07-12 stsp *tree_id = NULL;
1719 6ced4176 2019-07-12 stsp }
1720 07ed472d 2019-07-12 stsp return err;
1721 07ed472d 2019-07-12 stsp }
1722 07ed472d 2019-07-12 stsp
1723 07ed472d 2019-07-12 stsp static const struct got_error *
1724 07ed472d 2019-07-12 stsp checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1725 07ed472d 2019-07-12 stsp const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1726 07ed472d 2019-07-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1727 07ed472d 2019-07-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1728 07ed472d 2019-07-12 stsp {
1729 07ed472d 2019-07-12 stsp const struct got_error *err = NULL;
1730 07ed472d 2019-07-12 stsp struct got_commit_object *commit = NULL;
1731 07ed472d 2019-07-12 stsp struct got_tree_object *tree = NULL;
1732 07ed472d 2019-07-12 stsp struct got_fileindex_diff_tree_cb diff_cb;
1733 07ed472d 2019-07-12 stsp struct diff_cb_arg arg;
1734 07ed472d 2019-07-12 stsp
1735 07ed472d 2019-07-12 stsp err = ref_base_commit(worktree, repo);
1736 07ed472d 2019-07-12 stsp if (err)
1737 07ed472d 2019-07-12 stsp goto done;
1738 07ed472d 2019-07-12 stsp
1739 07ed472d 2019-07-12 stsp err = got_object_open_as_commit(&commit, repo,
1740 07ed472d 2019-07-12 stsp worktree->base_commit_id);
1741 07ed472d 2019-07-12 stsp if (err)
1742 07ed472d 2019-07-12 stsp goto done;
1743 07ed472d 2019-07-12 stsp
1744 07ed472d 2019-07-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1745 07ed472d 2019-07-12 stsp if (err)
1746 07ed472d 2019-07-12 stsp goto done;
1747 07ed472d 2019-07-12 stsp
1748 07ed472d 2019-07-12 stsp if (entry_name &&
1749 07ed472d 2019-07-12 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1750 07ed472d 2019-07-12 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1751 07ed472d 2019-07-12 stsp goto done;
1752 07ed472d 2019-07-12 stsp }
1753 07ed472d 2019-07-12 stsp
1754 07ed472d 2019-07-12 stsp diff_cb.diff_old_new = diff_old_new;
1755 07ed472d 2019-07-12 stsp diff_cb.diff_old = diff_old;
1756 07ed472d 2019-07-12 stsp diff_cb.diff_new = diff_new;
1757 07ed472d 2019-07-12 stsp arg.fileindex = fileindex;
1758 07ed472d 2019-07-12 stsp arg.worktree = worktree;
1759 07ed472d 2019-07-12 stsp arg.repo = repo;
1760 07ed472d 2019-07-12 stsp arg.progress_cb = progress_cb;
1761 07ed472d 2019-07-12 stsp arg.progress_arg = progress_arg;
1762 07ed472d 2019-07-12 stsp arg.cancel_cb = cancel_cb;
1763 07ed472d 2019-07-12 stsp arg.cancel_arg = cancel_arg;
1764 07ed472d 2019-07-12 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1765 07ed472d 2019-07-12 stsp entry_name, repo, &diff_cb, &arg);
1766 07ed472d 2019-07-12 stsp done:
1767 07ed472d 2019-07-12 stsp if (tree)
1768 07ed472d 2019-07-12 stsp got_object_tree_close(tree);
1769 07ed472d 2019-07-12 stsp if (commit)
1770 07ed472d 2019-07-12 stsp got_object_commit_close(commit);
1771 6ced4176 2019-07-12 stsp return err;
1772 6ced4176 2019-07-12 stsp }
1773 6ced4176 2019-07-12 stsp
1774 9d31a1d8 2018-03-11 stsp const struct got_error *
1775 f2ea84fa 2019-07-27 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1776 f2ea84fa 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
1777 f2ea84fa 2019-07-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1778 f2ea84fa 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1779 9d31a1d8 2018-03-11 stsp {
1780 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1781 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1782 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1783 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1784 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1785 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1786 f2ea84fa 2019-07-27 stsp struct tree_path_data {
1787 f2ea84fa 2019-07-27 stsp SIMPLEQ_ENTRY(tree_path_data) entry;
1788 f2ea84fa 2019-07-27 stsp struct got_object_id *tree_id;
1789 f2ea84fa 2019-07-27 stsp int entry_type;
1790 f2ea84fa 2019-07-27 stsp char *relpath;
1791 f2ea84fa 2019-07-27 stsp char *entry_name;
1792 f2ea84fa 2019-07-27 stsp } *tpd = NULL;
1793 f2ea84fa 2019-07-27 stsp SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1794 9d31a1d8 2018-03-11 stsp
1795 f2ea84fa 2019-07-27 stsp SIMPLEQ_INIT(&tree_paths);
1796 f2ea84fa 2019-07-27 stsp
1797 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1798 9d31a1d8 2018-03-11 stsp if (err)
1799 9d31a1d8 2018-03-11 stsp return err;
1800 93a30277 2018-12-24 stsp
1801 f2ea84fa 2019-07-27 stsp /* Map all specified paths to in-repository trees. */
1802 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1803 f2ea84fa 2019-07-27 stsp tpd = malloc(sizeof(*tpd));
1804 f2ea84fa 2019-07-27 stsp if (tpd == NULL) {
1805 f2ea84fa 2019-07-27 stsp err = got_error_from_errno("malloc");
1806 f2ea84fa 2019-07-27 stsp goto done;
1807 f2ea84fa 2019-07-27 stsp }
1808 6ced4176 2019-07-12 stsp
1809 f2ea84fa 2019-07-27 stsp err = find_tree_entry_for_checkout(&tpd->entry_type,
1810 f2ea84fa 2019-07-27 stsp &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1811 f2ea84fa 2019-07-27 stsp if (err) {
1812 f2ea84fa 2019-07-27 stsp free(tpd);
1813 6ced4176 2019-07-12 stsp goto done;
1814 6ced4176 2019-07-12 stsp }
1815 f2ea84fa 2019-07-27 stsp
1816 f2ea84fa 2019-07-27 stsp if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1817 f2ea84fa 2019-07-27 stsp err = got_path_basename(&tpd->entry_name, pe->path);
1818 f2ea84fa 2019-07-27 stsp if (err) {
1819 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1820 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1821 f2ea84fa 2019-07-27 stsp free(tpd);
1822 f2ea84fa 2019-07-27 stsp goto done;
1823 f2ea84fa 2019-07-27 stsp }
1824 f2ea84fa 2019-07-27 stsp } else
1825 f2ea84fa 2019-07-27 stsp tpd->entry_name = NULL;
1826 f2ea84fa 2019-07-27 stsp
1827 f2ea84fa 2019-07-27 stsp SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1828 6ced4176 2019-07-12 stsp }
1829 6ced4176 2019-07-12 stsp
1830 51514078 2018-12-25 stsp /*
1831 51514078 2018-12-25 stsp * Read the file index.
1832 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1833 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1834 51514078 2018-12-25 stsp */
1835 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1836 8da9e5f4 2019-01-12 stsp if (err)
1837 c4cdcb68 2019-04-03 stsp goto done;
1838 c4cdcb68 2019-04-03 stsp
1839 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1840 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
1841 711d9cd9 2019-07-12 stsp struct bump_base_commit_id_arg bbc_arg;
1842 f2ea84fa 2019-07-27 stsp
1843 f2ea84fa 2019-07-27 stsp err = checkout_files(worktree, fileindex, tpd->relpath,
1844 f2ea84fa 2019-07-27 stsp tpd->tree_id, tpd->entry_name, repo,
1845 f2ea84fa 2019-07-27 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1846 f2ea84fa 2019-07-27 stsp if (err)
1847 f2ea84fa 2019-07-27 stsp break;
1848 f2ea84fa 2019-07-27 stsp
1849 711d9cd9 2019-07-12 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1850 f2ea84fa 2019-07-27 stsp bbc_arg.entry_name = tpd->entry_name;
1851 f2ea84fa 2019-07-27 stsp bbc_arg.path = pe->path;
1852 f2b16ada 2019-08-02 stsp bbc_arg.path_len = pe->path_len;
1853 711d9cd9 2019-07-12 stsp bbc_arg.progress_cb = progress_cb;
1854 711d9cd9 2019-07-12 stsp bbc_arg.progress_arg = progress_arg;
1855 711d9cd9 2019-07-12 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1856 711d9cd9 2019-07-12 stsp bump_base_commit_id, &bbc_arg);
1857 f2ea84fa 2019-07-27 stsp if (err)
1858 f2ea84fa 2019-07-27 stsp break;
1859 f2ea84fa 2019-07-27 stsp
1860 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_NEXT(tpd, entry);
1861 711d9cd9 2019-07-12 stsp }
1862 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1863 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1864 af12c6b9 2019-06-04 stsp err = sync_err;
1865 9d31a1d8 2018-03-11 stsp done:
1866 9c6338c4 2019-06-02 stsp free(fileindex_path);
1867 edfa7d7f 2018-09-11 stsp if (tree)
1868 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1869 9d31a1d8 2018-03-11 stsp if (commit)
1870 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1871 5ade8233 2019-07-12 stsp if (fileindex)
1872 5ade8233 2019-07-12 stsp got_fileindex_free(fileindex);
1873 f2ea84fa 2019-07-27 stsp while (!SIMPLEQ_EMPTY(&tree_paths)) {
1874 f2ea84fa 2019-07-27 stsp tpd = SIMPLEQ_FIRST(&tree_paths);
1875 f2ea84fa 2019-07-27 stsp SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1876 f2ea84fa 2019-07-27 stsp free(tpd->relpath);
1877 f2ea84fa 2019-07-27 stsp free(tpd->tree_id);
1878 f2ea84fa 2019-07-27 stsp free(tpd);
1879 f2ea84fa 2019-07-27 stsp }
1880 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1881 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1882 9d31a1d8 2018-03-11 stsp err = unlockerr;
1883 f8d1f275 2019-02-04 stsp return err;
1884 f8d1f275 2019-02-04 stsp }
1885 f8d1f275 2019-02-04 stsp
1886 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1887 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1888 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1889 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1890 234035bc 2019-06-01 stsp void *progress_arg;
1891 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1892 234035bc 2019-06-01 stsp void *cancel_arg;
1893 818c7501 2019-07-11 stsp struct got_object_id *commit_id2;
1894 234035bc 2019-06-01 stsp };
1895 234035bc 2019-06-01 stsp
1896 234035bc 2019-06-01 stsp static const struct got_error *
1897 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1898 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1899 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1900 234035bc 2019-06-01 stsp struct got_repository *repo)
1901 234035bc 2019-06-01 stsp {
1902 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1903 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1904 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1905 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1906 234035bc 2019-06-01 stsp struct stat sb;
1907 234035bc 2019-06-01 stsp unsigned char status;
1908 234035bc 2019-06-01 stsp int local_changes_subsumed;
1909 234035bc 2019-06-01 stsp
1910 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1911 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
1912 d6c87207 2019-08-02 stsp strlen(path2));
1913 1ee397ad 2019-07-12 stsp if (ie == NULL)
1914 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1915 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1916 234035bc 2019-06-01 stsp
1917 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1918 234035bc 2019-06-01 stsp path2) == -1)
1919 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1920 234035bc 2019-06-01 stsp
1921 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1922 234035bc 2019-06-01 stsp if (err)
1923 234035bc 2019-06-01 stsp goto done;
1924 234035bc 2019-06-01 stsp
1925 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1926 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1927 1ee397ad 2019-07-12 stsp GOT_STATUS_MERGE, path2);
1928 234035bc 2019-06-01 stsp goto done;
1929 234035bc 2019-06-01 stsp }
1930 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1931 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1932 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1933 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1934 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path2);
1935 234035bc 2019-06-01 stsp goto done;
1936 234035bc 2019-06-01 stsp }
1937 234035bc 2019-06-01 stsp
1938 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1939 818c7501 2019-07-11 stsp ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1940 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1941 234035bc 2019-06-01 stsp } else if (blob1) {
1942 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path1,
1943 d6c87207 2019-08-02 stsp strlen(path1));
1944 1ee397ad 2019-07-12 stsp if (ie == NULL)
1945 1ee397ad 2019-07-12 stsp return (*a->progress_cb)(a->progress_arg,
1946 1ee397ad 2019-07-12 stsp GOT_STATUS_MISSING, path2);
1947 2b92fad7 2019-06-02 stsp
1948 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1949 2b92fad7 2019-06-02 stsp path1) == -1)
1950 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1951 2b92fad7 2019-06-02 stsp
1952 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1953 2b92fad7 2019-06-02 stsp if (err)
1954 2b92fad7 2019-06-02 stsp goto done;
1955 2b92fad7 2019-06-02 stsp
1956 2b92fad7 2019-06-02 stsp switch (status) {
1957 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1958 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1959 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
1960 1ee397ad 2019-07-12 stsp if (err)
1961 1ee397ad 2019-07-12 stsp goto done;
1962 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1963 2b92fad7 2019-06-02 stsp if (err)
1964 2b92fad7 2019-06-02 stsp goto done;
1965 2b92fad7 2019-06-02 stsp if (ie)
1966 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1967 2b92fad7 2019-06-02 stsp break;
1968 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1969 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1970 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1971 1ee397ad 2019-07-12 stsp GOT_STATUS_DELETE, path1);
1972 1ee397ad 2019-07-12 stsp if (err)
1973 1ee397ad 2019-07-12 stsp goto done;
1974 2b92fad7 2019-06-02 stsp if (ie)
1975 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1976 2b92fad7 2019-06-02 stsp break;
1977 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
1978 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
1979 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
1980 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
1981 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
1982 1ee397ad 2019-07-12 stsp if (err)
1983 1ee397ad 2019-07-12 stsp goto done;
1984 2b92fad7 2019-06-02 stsp break;
1985 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
1986 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path1);
1987 1ee397ad 2019-07-12 stsp if (err)
1988 1ee397ad 2019-07-12 stsp goto done;
1989 2b92fad7 2019-06-02 stsp break;
1990 2b92fad7 2019-06-02 stsp default:
1991 2b92fad7 2019-06-02 stsp break;
1992 2b92fad7 2019-06-02 stsp }
1993 234035bc 2019-06-01 stsp } else if (blob2) {
1994 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1995 234035bc 2019-06-01 stsp path2) == -1)
1996 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1997 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(a->fileindex, path2,
1998 d6c87207 2019-08-02 stsp strlen(path2));
1999 234035bc 2019-06-01 stsp if (ie) {
2000 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
2001 234035bc 2019-06-01 stsp repo);
2002 234035bc 2019-06-01 stsp if (err)
2003 234035bc 2019-06-01 stsp goto done;
2004 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
2005 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
2006 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
2007 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
2008 1ee397ad 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg,
2009 1ee397ad 2019-07-12 stsp status, path2);
2010 234035bc 2019-06-01 stsp goto done;
2011 234035bc 2019-06-01 stsp }
2012 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
2013 818c7501 2019-07-11 stsp NULL, ondisk_path, path2, sb.st_mode, blob2,
2014 818c7501 2019-07-11 stsp a->commit_id2, repo,
2015 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2016 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
2017 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
2018 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
2019 234035bc 2019-06-01 stsp blob2, 0);
2020 234035bc 2019-06-01 stsp if (err)
2021 234035bc 2019-06-01 stsp goto done;
2022 234035bc 2019-06-01 stsp }
2023 234035bc 2019-06-01 stsp } else {
2024 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2025 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
2026 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
2027 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
2028 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
2029 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
2030 234035bc 2019-06-01 stsp if (err)
2031 234035bc 2019-06-01 stsp goto done;
2032 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
2033 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
2034 234035bc 2019-06-01 stsp if (err)
2035 234035bc 2019-06-01 stsp goto done;
2036 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
2037 2b92fad7 2019-06-02 stsp if (err) {
2038 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
2039 2b92fad7 2019-06-02 stsp goto done;
2040 2b92fad7 2019-06-02 stsp }
2041 234035bc 2019-06-01 stsp }
2042 234035bc 2019-06-01 stsp }
2043 234035bc 2019-06-01 stsp done:
2044 234035bc 2019-06-01 stsp free(ondisk_path);
2045 234035bc 2019-06-01 stsp return err;
2046 234035bc 2019-06-01 stsp }
2047 234035bc 2019-06-01 stsp
2048 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
2049 234035bc 2019-06-01 stsp struct got_worktree *worktree;
2050 234035bc 2019-06-01 stsp struct got_repository *repo;
2051 234035bc 2019-06-01 stsp };
2052 234035bc 2019-06-01 stsp
2053 234035bc 2019-06-01 stsp static const struct got_error *
2054 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2055 234035bc 2019-06-01 stsp {
2056 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
2057 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
2058 234035bc 2019-06-01 stsp unsigned char status;
2059 234035bc 2019-06-01 stsp struct stat sb;
2060 234035bc 2019-06-01 stsp char *ondisk_path;
2061 234035bc 2019-06-01 stsp
2062 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
2063 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2064 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
2065 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
2066 234035bc 2019-06-01 stsp
2067 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2068 234035bc 2019-06-01 stsp == -1)
2069 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
2070 234035bc 2019-06-01 stsp
2071 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
2072 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2073 234035bc 2019-06-01 stsp if (err)
2074 234035bc 2019-06-01 stsp return err;
2075 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
2076 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
2077 234035bc 2019-06-01 stsp
2078 234035bc 2019-06-01 stsp return NULL;
2079 234035bc 2019-06-01 stsp }
2080 234035bc 2019-06-01 stsp
2081 818c7501 2019-07-11 stsp static const struct got_error *
2082 818c7501 2019-07-11 stsp merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 818c7501 2019-07-11 stsp const char *fileindex_path, struct got_object_id *commit_id1,
2084 818c7501 2019-07-11 stsp struct got_object_id *commit_id2, struct got_repository *repo,
2085 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2086 818c7501 2019-07-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2087 234035bc 2019-06-01 stsp {
2088 818c7501 2019-07-11 stsp const struct got_error *err = NULL, *sync_err;
2089 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2090 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2091 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
2092 234035bc 2019-06-01 stsp
2093 03415a1a 2019-06-02 stsp if (commit_id1) {
2094 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2095 03415a1a 2019-06-02 stsp worktree->path_prefix);
2096 03415a1a 2019-06-02 stsp if (err)
2097 03415a1a 2019-06-02 stsp goto done;
2098 03415a1a 2019-06-02 stsp
2099 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2100 03415a1a 2019-06-02 stsp if (err)
2101 03415a1a 2019-06-02 stsp goto done;
2102 03415a1a 2019-06-02 stsp }
2103 234035bc 2019-06-01 stsp
2104 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2105 234035bc 2019-06-01 stsp worktree->path_prefix);
2106 234035bc 2019-06-01 stsp if (err)
2107 234035bc 2019-06-01 stsp goto done;
2108 234035bc 2019-06-01 stsp
2109 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2110 234035bc 2019-06-01 stsp if (err)
2111 234035bc 2019-06-01 stsp goto done;
2112 234035bc 2019-06-01 stsp
2113 234035bc 2019-06-01 stsp arg.worktree = worktree;
2114 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
2115 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
2116 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
2117 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
2118 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
2119 818c7501 2019-07-11 stsp arg.commit_id2 = commit_id2;
2120 31b4484f 2019-07-27 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2121 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2122 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2123 af12c6b9 2019-06-04 stsp err = sync_err;
2124 234035bc 2019-06-01 stsp done:
2125 234035bc 2019-06-01 stsp if (tree1)
2126 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
2127 234035bc 2019-06-01 stsp if (tree2)
2128 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
2129 818c7501 2019-07-11 stsp return err;
2130 818c7501 2019-07-11 stsp }
2131 234035bc 2019-06-01 stsp
2132 818c7501 2019-07-11 stsp const struct got_error *
2133 818c7501 2019-07-11 stsp got_worktree_merge_files(struct got_worktree *worktree,
2134 818c7501 2019-07-11 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2135 818c7501 2019-07-11 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2136 818c7501 2019-07-11 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2137 818c7501 2019-07-11 stsp {
2138 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
2139 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
2140 818c7501 2019-07-11 stsp struct got_fileindex *fileindex = NULL;
2141 818c7501 2019-07-11 stsp struct check_merge_ok_arg mok_arg;
2142 818c7501 2019-07-11 stsp
2143 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
2144 818c7501 2019-07-11 stsp if (err)
2145 818c7501 2019-07-11 stsp return err;
2146 818c7501 2019-07-11 stsp
2147 818c7501 2019-07-11 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2148 818c7501 2019-07-11 stsp if (err)
2149 818c7501 2019-07-11 stsp goto done;
2150 818c7501 2019-07-11 stsp
2151 818c7501 2019-07-11 stsp mok_arg.worktree = worktree;
2152 818c7501 2019-07-11 stsp mok_arg.repo = repo;
2153 818c7501 2019-07-11 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2154 818c7501 2019-07-11 stsp &mok_arg);
2155 818c7501 2019-07-11 stsp if (err)
2156 818c7501 2019-07-11 stsp goto done;
2157 818c7501 2019-07-11 stsp
2158 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2159 818c7501 2019-07-11 stsp commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2160 818c7501 2019-07-11 stsp done:
2161 818c7501 2019-07-11 stsp if (fileindex)
2162 818c7501 2019-07-11 stsp got_fileindex_free(fileindex);
2163 818c7501 2019-07-11 stsp free(fileindex_path);
2164 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2165 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
2166 234035bc 2019-06-01 stsp err = unlockerr;
2167 234035bc 2019-06-01 stsp return err;
2168 234035bc 2019-06-01 stsp }
2169 234035bc 2019-06-01 stsp
2170 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
2171 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
2172 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
2173 927df6b7 2019-02-10 stsp const char *status_path;
2174 927df6b7 2019-02-10 stsp size_t status_path_len;
2175 f8d1f275 2019-02-04 stsp struct got_repository *repo;
2176 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
2177 f8d1f275 2019-02-04 stsp void *status_arg;
2178 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
2179 f8d1f275 2019-02-04 stsp void *cancel_arg;
2180 f8d1f275 2019-02-04 stsp };
2181 f8d1f275 2019-02-04 stsp
2182 f8d1f275 2019-02-04 stsp static const struct got_error *
2183 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2184 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
2185 927df6b7 2019-02-10 stsp struct got_repository *repo)
2186 927df6b7 2019-02-10 stsp {
2187 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
2188 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
2189 927df6b7 2019-02-10 stsp struct stat sb;
2190 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2191 927df6b7 2019-02-10 stsp
2192 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
2193 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2194 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2195 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2196 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2197 016a88dd 2019-05-13 stsp &commit_id);
2198 927df6b7 2019-02-10 stsp }
2199 927df6b7 2019-02-10 stsp return err;
2200 927df6b7 2019-02-10 stsp }
2201 927df6b7 2019-02-10 stsp
2202 927df6b7 2019-02-10 stsp static const struct got_error *
2203 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
2204 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
2205 f8d1f275 2019-02-04 stsp {
2206 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2207 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2208 f8d1f275 2019-02-04 stsp char *abspath;
2209 f8d1f275 2019-02-04 stsp
2210 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2212 0584f854 2019-04-06 stsp
2213 d572f586 2019-08-02 stsp if (got_path_cmp(parent_path, a->status_path,
2214 d572f586 2019-08-02 stsp strlen(parent_path), a->status_path_len) != 0 &&
2215 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2216 927df6b7 2019-02-10 stsp return NULL;
2217 927df6b7 2019-02-10 stsp
2218 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2219 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2220 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
2221 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2222 f8d1f275 2019-02-04 stsp } else {
2223 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2224 f8d1f275 2019-02-04 stsp de->d_name) == -1)
2225 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2226 f8d1f275 2019-02-04 stsp }
2227 f8d1f275 2019-02-04 stsp
2228 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2229 927df6b7 2019-02-10 stsp a->repo);
2230 f8d1f275 2019-02-04 stsp free(abspath);
2231 9d31a1d8 2018-03-11 stsp return err;
2232 9d31a1d8 2018-03-11 stsp }
2233 f8d1f275 2019-02-04 stsp
2234 f8d1f275 2019-02-04 stsp static const struct got_error *
2235 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2236 f8d1f275 2019-02-04 stsp {
2237 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2238 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
2239 2ec1f75b 2019-03-26 stsp unsigned char status;
2240 927df6b7 2019-02-10 stsp
2241 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2243 0584f854 2019-04-06 stsp
2244 c577a9ce 2019-07-27 stsp if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2245 927df6b7 2019-02-10 stsp return NULL;
2246 927df6b7 2019-02-10 stsp
2247 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2248 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2249 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2250 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2251 2ec1f75b 2019-03-26 stsp else
2252 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2253 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2254 016a88dd 2019-05-13 stsp &commit_id);
2255 f8d1f275 2019-02-04 stsp }
2256 f8d1f275 2019-02-04 stsp
2257 f8d1f275 2019-02-04 stsp static const struct got_error *
2258 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2259 f8d1f275 2019-02-04 stsp {
2260 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2261 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2262 f8d1f275 2019-02-04 stsp char *path = NULL;
2263 f8d1f275 2019-02-04 stsp
2264 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2266 0584f854 2019-04-06 stsp
2267 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2268 2c201a36 2019-02-10 stsp return NULL;
2269 2c201a36 2019-02-10 stsp
2270 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2271 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2272 927df6b7 2019-02-10 stsp return NULL;
2273 927df6b7 2019-02-10 stsp
2274 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2275 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2276 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2277 f8d1f275 2019-02-04 stsp } else {
2278 f8d1f275 2019-02-04 stsp path = de->d_name;
2279 f8d1f275 2019-02-04 stsp }
2280 f8d1f275 2019-02-04 stsp
2281 c577a9ce 2019-07-27 stsp if (got_path_is_child(path, a->status_path, a->status_path_len))
2282 c577a9ce 2019-07-27 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2283 c577a9ce 2019-07-27 stsp path, NULL, NULL);
2284 f8d1f275 2019-02-04 stsp if (parent_path[0])
2285 f8d1f275 2019-02-04 stsp free(path);
2286 b72f483a 2019-02-05 stsp return err;
2287 f8d1f275 2019-02-04 stsp }
2288 f8d1f275 2019-02-04 stsp
2289 347d1d3e 2019-07-12 stsp static const struct got_error *
2290 abb4604f 2019-07-27 stsp report_single_file_status(const char *path, const char *ondisk_path,
2291 abb4604f 2019-07-27 stsp struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2292 abb4604f 2019-07-27 stsp void *status_arg, struct got_repository *repo)
2293 abb4604f 2019-07-27 stsp {
2294 abb4604f 2019-07-27 stsp struct got_fileindex_entry *ie;
2295 abb4604f 2019-07-27 stsp struct stat sb;
2296 abb4604f 2019-07-27 stsp
2297 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2298 abb4604f 2019-07-27 stsp if (ie)
2299 abb4604f 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb,
2300 abb4604f 2019-07-27 stsp status_arg, repo);
2301 abb4604f 2019-07-27 stsp
2302 abb4604f 2019-07-27 stsp if (lstat(ondisk_path, &sb) == -1) {
2303 abb4604f 2019-07-27 stsp if (errno != ENOENT)
2304 abb4604f 2019-07-27 stsp return got_error_from_errno2("lstat", ondisk_path);
2305 abb4604f 2019-07-27 stsp return NULL;
2306 abb4604f 2019-07-27 stsp }
2307 abb4604f 2019-07-27 stsp
2308 abb4604f 2019-07-27 stsp if (S_ISREG(sb.st_mode))
2309 abb4604f 2019-07-27 stsp return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED, path,
2310 abb4604f 2019-07-27 stsp NULL, NULL);
2311 abb4604f 2019-07-27 stsp
2312 abb4604f 2019-07-27 stsp return NULL;
2313 abb4604f 2019-07-27 stsp }
2314 abb4604f 2019-07-27 stsp
2315 abb4604f 2019-07-27 stsp static const struct got_error *
2316 347d1d3e 2019-07-12 stsp worktree_status(struct got_worktree *worktree, const char *path,
2317 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex, struct got_repository *repo,
2318 347d1d3e 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
2319 347d1d3e 2019-07-12 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2320 f8d1f275 2019-02-04 stsp {
2321 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2322 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2323 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2324 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2325 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2326 f8d1f275 2019-02-04 stsp
2327 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2328 8dc303cc 2019-07-27 stsp worktree->root_path, path[0] ? "/" : "", path) == -1)
2329 8dc303cc 2019-07-27 stsp return got_error_from_errno("asprintf");
2330 8dc303cc 2019-07-27 stsp
2331 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2332 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2333 abb4604f 2019-07-27 stsp if (errno != ENOTDIR && errno != ENOENT)
2334 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2335 abb4604f 2019-07-27 stsp else
2336 abb4604f 2019-07-27 stsp err = report_single_file_status(path, ondisk_path,
2337 abb4604f 2019-07-27 stsp fileindex, status_cb, status_arg, repo);
2338 abb4604f 2019-07-27 stsp } else {
2339 abb4604f 2019-07-27 stsp fdiff_cb.diff_old_new = status_old_new;
2340 abb4604f 2019-07-27 stsp fdiff_cb.diff_old = status_old;
2341 abb4604f 2019-07-27 stsp fdiff_cb.diff_new = status_new;
2342 abb4604f 2019-07-27 stsp arg.fileindex = fileindex;
2343 abb4604f 2019-07-27 stsp arg.worktree = worktree;
2344 abb4604f 2019-07-27 stsp arg.status_path = path;
2345 abb4604f 2019-07-27 stsp arg.status_path_len = strlen(path);
2346 abb4604f 2019-07-27 stsp arg.repo = repo;
2347 abb4604f 2019-07-27 stsp arg.status_cb = status_cb;
2348 abb4604f 2019-07-27 stsp arg.status_arg = status_arg;
2349 abb4604f 2019-07-27 stsp arg.cancel_cb = cancel_cb;
2350 abb4604f 2019-07-27 stsp arg.cancel_arg = cancel_arg;
2351 abb4604f 2019-07-27 stsp err = got_fileindex_diff_dir(fileindex, workdir,
2352 abb4604f 2019-07-27 stsp worktree->root_path, path, repo, &fdiff_cb, &arg);
2353 927df6b7 2019-02-10 stsp }
2354 8dc303cc 2019-07-27 stsp
2355 f8d1f275 2019-02-04 stsp if (workdir)
2356 f8d1f275 2019-02-04 stsp closedir(workdir);
2357 927df6b7 2019-02-10 stsp free(ondisk_path);
2358 347d1d3e 2019-07-12 stsp return err;
2359 347d1d3e 2019-07-12 stsp }
2360 347d1d3e 2019-07-12 stsp
2361 347d1d3e 2019-07-12 stsp const struct got_error *
2362 72ea6654 2019-07-27 stsp got_worktree_status(struct got_worktree *worktree,
2363 72ea6654 2019-07-27 stsp struct got_pathlist_head *paths, struct got_repository *repo,
2364 72ea6654 2019-07-27 stsp got_worktree_status_cb status_cb, void *status_arg,
2365 72ea6654 2019-07-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2366 347d1d3e 2019-07-12 stsp {
2367 347d1d3e 2019-07-12 stsp const struct got_error *err = NULL;
2368 347d1d3e 2019-07-12 stsp char *fileindex_path = NULL;
2369 347d1d3e 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
2370 72ea6654 2019-07-27 stsp struct got_pathlist_entry *pe;
2371 347d1d3e 2019-07-12 stsp
2372 347d1d3e 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2373 347d1d3e 2019-07-12 stsp if (err)
2374 347d1d3e 2019-07-12 stsp return err;
2375 347d1d3e 2019-07-12 stsp
2376 72ea6654 2019-07-27 stsp TAILQ_FOREACH(pe, paths, entry) {
2377 72ea6654 2019-07-27 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
2378 72ea6654 2019-07-27 stsp status_cb, status_arg, cancel_cb, cancel_arg);
2379 72ea6654 2019-07-27 stsp if (err)
2380 72ea6654 2019-07-27 stsp break;
2381 72ea6654 2019-07-27 stsp }
2382 f8d1f275 2019-02-04 stsp free(fileindex_path);
2383 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2384 f8d1f275 2019-02-04 stsp return err;
2385 f8d1f275 2019-02-04 stsp }
2386 6c7ab921 2019-03-18 stsp
2387 6c7ab921 2019-03-18 stsp const struct got_error *
2388 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2389 6c7ab921 2019-03-18 stsp const char *arg)
2390 6c7ab921 2019-03-18 stsp {
2391 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2392 d0710d08 2019-07-22 stsp char *resolved, *cwd = NULL, *path = NULL;
2393 6c7ab921 2019-03-18 stsp size_t len;
2394 6c7ab921 2019-03-18 stsp
2395 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2396 6c7ab921 2019-03-18 stsp
2397 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2398 d0710d08 2019-07-22 stsp if (resolved == NULL) {
2399 d0710d08 2019-07-22 stsp if (errno != ENOENT)
2400 d0710d08 2019-07-22 stsp return got_error_from_errno2("realpath", arg);
2401 d0710d08 2019-07-22 stsp cwd = getcwd(NULL, 0);
2402 d0710d08 2019-07-22 stsp if (cwd == NULL)
2403 d0710d08 2019-07-22 stsp return got_error_from_errno("getcwd");
2404 d0710d08 2019-07-22 stsp if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2405 d0710d08 2019-07-22 stsp err = got_error_from_errno("asprintf");
2406 d0710d08 2019-07-22 stsp goto done;
2407 d0710d08 2019-07-22 stsp }
2408 d0710d08 2019-07-22 stsp }
2409 6c7ab921 2019-03-18 stsp
2410 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2411 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2412 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2413 6c7ab921 2019-03-18 stsp goto done;
2414 6c7ab921 2019-03-18 stsp }
2415 6c7ab921 2019-03-18 stsp
2416 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2417 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2418 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2419 f6d88e1a 2019-05-29 stsp if (err)
2420 f6d88e1a 2019-05-29 stsp goto done;
2421 f6d88e1a 2019-05-29 stsp } else {
2422 f6d88e1a 2019-05-29 stsp path = strdup("");
2423 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2424 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2425 f6d88e1a 2019-05-29 stsp goto done;
2426 f6d88e1a 2019-05-29 stsp }
2427 6c7ab921 2019-03-18 stsp }
2428 6c7ab921 2019-03-18 stsp
2429 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2430 6c7ab921 2019-03-18 stsp len = strlen(path);
2431 3f17dee4 2019-07-27 stsp while (len > 0 && path[len - 1] == '/') {
2432 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2433 6c7ab921 2019-03-18 stsp len--;
2434 6c7ab921 2019-03-18 stsp }
2435 6c7ab921 2019-03-18 stsp done:
2436 6c7ab921 2019-03-18 stsp free(resolved);
2437 d0710d08 2019-07-22 stsp free(cwd);
2438 6c7ab921 2019-03-18 stsp if (err == NULL)
2439 6c7ab921 2019-03-18 stsp *wt_path = path;
2440 6c7ab921 2019-03-18 stsp else
2441 6c7ab921 2019-03-18 stsp free(path);
2442 d00136be 2019-03-26 stsp return err;
2443 d00136be 2019-03-26 stsp }
2444 d00136be 2019-03-26 stsp
2445 07f5b47a 2019-06-02 stsp static const struct got_error *
2446 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2447 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2448 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2449 07f5b47a 2019-06-02 stsp {
2450 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2451 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2452 07f5b47a 2019-06-02 stsp
2453 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2454 d6c87207 2019-08-02 stsp if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2455 07f5b47a 2019-06-02 stsp return NULL;
2456 07f5b47a 2019-06-02 stsp
2457 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2458 07f5b47a 2019-06-02 stsp if (err)
2459 07f5b47a 2019-06-02 stsp return err;
2460 07f5b47a 2019-06-02 stsp
2461 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2462 07f5b47a 2019-06-02 stsp if (err) {
2463 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2464 07f5b47a 2019-06-02 stsp return err;
2465 07f5b47a 2019-06-02 stsp }
2466 07f5b47a 2019-06-02 stsp
2467 c02b99b6 2019-07-27 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2468 07f5b47a 2019-06-02 stsp }
2469 07f5b47a 2019-06-02 stsp
2470 d00136be 2019-03-26 stsp const struct got_error *
2471 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2472 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2473 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2474 031a5338 2019-03-26 stsp struct got_repository *repo)
2475 d00136be 2019-03-26 stsp {
2476 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2477 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2478 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2479 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2480 d00136be 2019-03-26 stsp
2481 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2482 d00136be 2019-03-26 stsp if (err)
2483 d00136be 2019-03-26 stsp return err;
2484 d00136be 2019-03-26 stsp
2485 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2486 d00136be 2019-03-26 stsp if (err)
2487 d00136be 2019-03-26 stsp goto done;
2488 d00136be 2019-03-26 stsp
2489 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2490 1dd54920 2019-05-11 stsp char *relpath;
2491 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2492 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2493 1dd54920 2019-05-11 stsp if (err)
2494 af12c6b9 2019-06-04 stsp break;
2495 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2496 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2497 1dd54920 2019-05-11 stsp free(relpath);
2498 1dd54920 2019-05-11 stsp if (err)
2499 af12c6b9 2019-06-04 stsp break;
2500 1dd54920 2019-05-11 stsp }
2501 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2502 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2503 af12c6b9 2019-06-04 stsp err = sync_err;
2504 d00136be 2019-03-26 stsp done:
2505 fb399478 2019-07-12 stsp free(fileindex_path);
2506 d00136be 2019-03-26 stsp if (fileindex)
2507 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2508 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2509 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2510 d00136be 2019-03-26 stsp err = unlockerr;
2511 6c7ab921 2019-03-18 stsp return err;
2512 6c7ab921 2019-03-18 stsp }
2513 17ed4618 2019-06-02 stsp
2514 17ed4618 2019-06-02 stsp static const struct got_error *
2515 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2516 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2517 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2518 17ed4618 2019-06-02 stsp struct got_repository *repo)
2519 17ed4618 2019-06-02 stsp {
2520 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2521 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2522 17ed4618 2019-06-02 stsp unsigned char status;
2523 17ed4618 2019-06-02 stsp struct stat sb;
2524 17ed4618 2019-06-02 stsp
2525 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2526 17ed4618 2019-06-02 stsp if (ie == NULL)
2527 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2528 2ec1f75b 2019-03-26 stsp
2529 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2530 17ed4618 2019-06-02 stsp if (err)
2531 17ed4618 2019-06-02 stsp return err;
2532 17ed4618 2019-06-02 stsp
2533 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2534 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2535 de426ea6 2019-08-03 stsp return NULL;
2536 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2537 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2538 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2539 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2540 17ed4618 2019-06-02 stsp }
2541 17ed4618 2019-06-02 stsp
2542 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2543 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2544 17ed4618 2019-06-02 stsp
2545 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2546 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2547 17ed4618 2019-06-02 stsp }
2548 17ed4618 2019-06-02 stsp
2549 2ec1f75b 2019-03-26 stsp const struct got_error *
2550 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2551 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2552 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2553 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2554 2ec1f75b 2019-03-26 stsp {
2555 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2556 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2557 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2558 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2559 2ec1f75b 2019-03-26 stsp
2560 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2561 2ec1f75b 2019-03-26 stsp if (err)
2562 2ec1f75b 2019-03-26 stsp return err;
2563 2ec1f75b 2019-03-26 stsp
2564 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2565 2ec1f75b 2019-03-26 stsp if (err)
2566 2ec1f75b 2019-03-26 stsp goto done;
2567 2ec1f75b 2019-03-26 stsp
2568 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2569 17ed4618 2019-06-02 stsp char *relpath;
2570 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2571 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2572 17ed4618 2019-06-02 stsp if (err)
2573 af12c6b9 2019-06-04 stsp break;
2574 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2575 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2576 17ed4618 2019-06-02 stsp free(relpath);
2577 17ed4618 2019-06-02 stsp if (err)
2578 af12c6b9 2019-06-04 stsp break;
2579 2ec1f75b 2019-03-26 stsp }
2580 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2581 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2582 af12c6b9 2019-06-04 stsp err = sync_err;
2583 2ec1f75b 2019-03-26 stsp done:
2584 fb399478 2019-07-12 stsp free(fileindex_path);
2585 2ec1f75b 2019-03-26 stsp if (fileindex)
2586 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2587 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2588 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2589 2ec1f75b 2019-03-26 stsp err = unlockerr;
2590 2ec1f75b 2019-03-26 stsp return err;
2591 2ec1f75b 2019-03-26 stsp }
2592 a129376b 2019-03-28 stsp
2593 e20a8b6f 2019-06-04 stsp static const struct got_error *
2594 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2595 a129376b 2019-03-28 stsp const char *ondisk_path,
2596 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2597 a129376b 2019-03-28 stsp struct got_repository *repo)
2598 a129376b 2019-03-28 stsp {
2599 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2600 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2601 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2602 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2603 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2604 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2605 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2606 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2607 a129376b 2019-03-28 stsp unsigned char status;
2608 a129376b 2019-03-28 stsp struct stat sb;
2609 a129376b 2019-03-28 stsp
2610 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2611 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2612 a129376b 2019-03-28 stsp if (err)
2613 a129376b 2019-03-28 stsp goto done;
2614 a129376b 2019-03-28 stsp
2615 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2616 a129376b 2019-03-28 stsp if (ie == NULL) {
2617 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2618 a129376b 2019-03-28 stsp goto done;
2619 a129376b 2019-03-28 stsp }
2620 a129376b 2019-03-28 stsp
2621 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2622 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2623 a129376b 2019-03-28 stsp if (err) {
2624 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2625 a129376b 2019-03-28 stsp goto done;
2626 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2627 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2628 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2629 e20a8b6f 2019-06-04 stsp goto done;
2630 e20a8b6f 2019-06-04 stsp }
2631 a129376b 2019-03-28 stsp }
2632 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2633 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2634 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2635 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2636 a129376b 2019-03-28 stsp goto done;
2637 a129376b 2019-03-28 stsp }
2638 a129376b 2019-03-28 stsp } else {
2639 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2640 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2641 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2642 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2643 a129376b 2019-03-28 stsp goto done;
2644 a129376b 2019-03-28 stsp }
2645 a129376b 2019-03-28 stsp } else {
2646 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2647 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2648 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2649 a129376b 2019-03-28 stsp goto done;
2650 a129376b 2019-03-28 stsp }
2651 a129376b 2019-03-28 stsp }
2652 a129376b 2019-03-28 stsp }
2653 a129376b 2019-03-28 stsp
2654 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2655 a129376b 2019-03-28 stsp if (err)
2656 a129376b 2019-03-28 stsp goto done;
2657 7fa0d182 2019-08-03 stsp if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2658 7fa0d182 2019-08-03 stsp sb.st_mode = got_fileindex_perms_to_st(ie);
2659 a129376b 2019-03-28 stsp
2660 a9fa2909 2019-07-27 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2661 a9fa2909 2019-07-27 stsp tree_path);
2662 a9fa2909 2019-07-27 stsp if (err) {
2663 a9fa2909 2019-07-27 stsp if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2664 a9fa2909 2019-07-27 stsp status == GOT_STATUS_ADD))
2665 a9fa2909 2019-07-27 stsp goto done;
2666 a9fa2909 2019-07-27 stsp } else {
2667 a9fa2909 2019-07-27 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2668 a9fa2909 2019-07-27 stsp if (err)
2669 a9fa2909 2019-07-27 stsp goto done;
2670 a9fa2909 2019-07-27 stsp
2671 a9fa2909 2019-07-27 stsp te_name = basename(ie->path);
2672 a9fa2909 2019-07-27 stsp if (te_name == NULL) {
2673 a9fa2909 2019-07-27 stsp err = got_error_from_errno2("basename", ie->path);
2674 a9fa2909 2019-07-27 stsp goto done;
2675 a9fa2909 2019-07-27 stsp }
2676 a9fa2909 2019-07-27 stsp
2677 a9fa2909 2019-07-27 stsp te = got_object_tree_find_entry(tree, te_name);
2678 a9fa2909 2019-07-27 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2679 a9fa2909 2019-07-27 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2680 a9fa2909 2019-07-27 stsp goto done;
2681 a9fa2909 2019-07-27 stsp }
2682 a129376b 2019-03-28 stsp }
2683 a129376b 2019-03-28 stsp
2684 a129376b 2019-03-28 stsp switch (status) {
2685 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2686 1ee397ad 2019-07-12 stsp err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2687 1ee397ad 2019-07-12 stsp if (err)
2688 1ee397ad 2019-07-12 stsp goto done;
2689 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2690 a129376b 2019-03-28 stsp break;
2691 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2692 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2693 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2694 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2695 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2696 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2697 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2698 a129376b 2019-03-28 stsp if (err)
2699 a129376b 2019-03-28 stsp goto done;
2700 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2701 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2702 a129376b 2019-03-28 stsp progress_arg);
2703 a129376b 2019-03-28 stsp if (err)
2704 a129376b 2019-03-28 stsp goto done;
2705 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2706 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2707 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2708 a129376b 2019-03-28 stsp if (err)
2709 a129376b 2019-03-28 stsp goto done;
2710 a129376b 2019-03-28 stsp }
2711 a129376b 2019-03-28 stsp break;
2712 e20a8b6f 2019-06-04 stsp }
2713 a129376b 2019-03-28 stsp default:
2714 a129376b 2019-03-28 stsp goto done;
2715 a129376b 2019-03-28 stsp }
2716 a129376b 2019-03-28 stsp done:
2717 a129376b 2019-03-28 stsp free(relpath);
2718 e20a8b6f 2019-06-04 stsp free(parent_path);
2719 a129376b 2019-03-28 stsp free(tree_path);
2720 a129376b 2019-03-28 stsp if (blob)
2721 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2722 a129376b 2019-03-28 stsp if (tree)
2723 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2724 a129376b 2019-03-28 stsp free(tree_id);
2725 e20a8b6f 2019-06-04 stsp return err;
2726 e20a8b6f 2019-06-04 stsp }
2727 e20a8b6f 2019-06-04 stsp
2728 e20a8b6f 2019-06-04 stsp const struct got_error *
2729 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2730 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2731 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2732 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2733 e20a8b6f 2019-06-04 stsp {
2734 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2735 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2736 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2737 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2738 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2739 e20a8b6f 2019-06-04 stsp
2740 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2741 e20a8b6f 2019-06-04 stsp if (err)
2742 e20a8b6f 2019-06-04 stsp return err;
2743 e20a8b6f 2019-06-04 stsp
2744 3605a814 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
2745 e20a8b6f 2019-06-04 stsp if (err)
2746 e20a8b6f 2019-06-04 stsp goto done;
2747 e20a8b6f 2019-06-04 stsp
2748 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2749 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2750 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2751 e20a8b6f 2019-06-04 stsp if (err)
2752 af12c6b9 2019-06-04 stsp break;
2753 e20a8b6f 2019-06-04 stsp }
2754 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2755 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2756 e20a8b6f 2019-06-04 stsp err = sync_err;
2757 af12c6b9 2019-06-04 stsp done:
2758 fb399478 2019-07-12 stsp free(fileindex_path);
2759 a129376b 2019-03-28 stsp if (fileindex)
2760 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2761 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2762 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2763 a129376b 2019-03-28 stsp err = unlockerr;
2764 c4296144 2019-05-09 stsp return err;
2765 c4296144 2019-05-09 stsp }
2766 c4296144 2019-05-09 stsp
2767 cf066bf8 2019-05-09 stsp static void
2768 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2769 cf066bf8 2019-05-09 stsp {
2770 24519714 2019-05-09 stsp free(ct->path);
2771 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2772 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2773 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2774 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2775 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2776 cf066bf8 2019-05-09 stsp free(ct);
2777 cf066bf8 2019-05-09 stsp }
2778 24519714 2019-05-09 stsp
2779 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2780 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2781 24519714 2019-05-09 stsp struct got_repository *repo;
2782 24519714 2019-05-09 stsp struct got_worktree *worktree;
2783 24519714 2019-05-09 stsp };
2784 cf066bf8 2019-05-09 stsp
2785 c4296144 2019-05-09 stsp static const struct got_error *
2786 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2787 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2788 c4296144 2019-05-09 stsp {
2789 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2790 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2791 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2792 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2793 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2794 768aea60 2019-05-09 stsp struct stat sb;
2795 c4296144 2019-05-09 stsp
2796 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2797 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2798 c4296144 2019-05-09 stsp
2799 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2800 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2801 c4296144 2019-05-09 stsp return NULL;
2802 0b5cc0d6 2019-05-09 stsp
2803 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2804 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2805 036813ee 2019-05-09 stsp goto done;
2806 036813ee 2019-05-09 stsp }
2807 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2808 036813ee 2019-05-09 stsp parent_path = strdup("");
2809 69960a46 2019-05-09 stsp if (parent_path == NULL)
2810 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2811 69960a46 2019-05-09 stsp } else {
2812 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2813 69960a46 2019-05-09 stsp if (err)
2814 69960a46 2019-05-09 stsp return err;
2815 69960a46 2019-05-09 stsp }
2816 c4296144 2019-05-09 stsp
2817 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2818 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2819 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2820 c4296144 2019-05-09 stsp goto done;
2821 768aea60 2019-05-09 stsp }
2822 768aea60 2019-05-09 stsp
2823 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2824 768aea60 2019-05-09 stsp relpath) == -1) {
2825 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2826 768aea60 2019-05-09 stsp goto done;
2827 768aea60 2019-05-09 stsp }
2828 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2829 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2830 768aea60 2019-05-09 stsp } else {
2831 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2832 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2833 768aea60 2019-05-09 stsp goto done;
2834 768aea60 2019-05-09 stsp }
2835 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2836 c4296144 2019-05-09 stsp }
2837 c4296144 2019-05-09 stsp
2838 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2839 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2840 44d03001 2019-05-09 stsp relpath) == -1) {
2841 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2842 44d03001 2019-05-09 stsp goto done;
2843 44d03001 2019-05-09 stsp }
2844 44d03001 2019-05-09 stsp
2845 cf066bf8 2019-05-09 stsp ct->status = status;
2846 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2847 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2848 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2849 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2850 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2851 b416585c 2019-05-13 stsp goto done;
2852 b416585c 2019-05-13 stsp }
2853 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2854 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2855 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2856 036813ee 2019-05-09 stsp goto done;
2857 036813ee 2019-05-09 stsp }
2858 ca2503ea 2019-05-09 stsp }
2859 24519714 2019-05-09 stsp ct->path = strdup(path);
2860 24519714 2019-05-09 stsp if (ct->path == NULL) {
2861 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2862 24519714 2019-05-09 stsp goto done;
2863 24519714 2019-05-09 stsp }
2864 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2865 c4296144 2019-05-09 stsp done:
2866 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2867 ed175427 2019-05-09 stsp free_commitable(ct);
2868 24519714 2019-05-09 stsp free(parent_path);
2869 036813ee 2019-05-09 stsp free(path);
2870 a129376b 2019-03-28 stsp return err;
2871 a129376b 2019-03-28 stsp }
2872 c4296144 2019-05-09 stsp
2873 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2874 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2875 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2876 036813ee 2019-05-09 stsp struct got_repository *);
2877 ed175427 2019-05-09 stsp
2878 ed175427 2019-05-09 stsp static const struct got_error *
2879 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2880 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2881 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2882 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2883 afa376bf 2019-05-09 stsp struct got_repository *repo)
2884 ed175427 2019-05-09 stsp {
2885 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2886 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2887 036813ee 2019-05-09 stsp char *subpath;
2888 ed175427 2019-05-09 stsp
2889 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2890 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2891 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2892 ed175427 2019-05-09 stsp
2893 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2894 ed175427 2019-05-09 stsp if (err)
2895 ed175427 2019-05-09 stsp return err;
2896 ed175427 2019-05-09 stsp
2897 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2898 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2899 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2900 036813ee 2019-05-09 stsp free(subpath);
2901 036813ee 2019-05-09 stsp return err;
2902 036813ee 2019-05-09 stsp }
2903 ed175427 2019-05-09 stsp
2904 036813ee 2019-05-09 stsp static const struct got_error *
2905 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2906 036813ee 2019-05-09 stsp {
2907 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2908 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2909 ed175427 2019-05-09 stsp
2910 036813ee 2019-05-09 stsp *match = 0;
2911 ed175427 2019-05-09 stsp
2912 e0233cea 2019-07-25 stsp if (strchr(ct->in_repo_path, '/') == NULL) {
2913 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2914 0f63689d 2019-05-10 stsp return NULL;
2915 036813ee 2019-05-09 stsp }
2916 0b5cc0d6 2019-05-09 stsp
2917 e0233cea 2019-07-25 stsp err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2918 0f63689d 2019-05-10 stsp if (err)
2919 0f63689d 2019-05-10 stsp return err;
2920 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2921 036813ee 2019-05-09 stsp free(ct_parent_path);
2922 036813ee 2019-05-09 stsp return err;
2923 036813ee 2019-05-09 stsp }
2924 0b5cc0d6 2019-05-09 stsp
2925 768aea60 2019-05-09 stsp static mode_t
2926 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2927 768aea60 2019-05-09 stsp {
2928 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2929 768aea60 2019-05-09 stsp }
2930 768aea60 2019-05-09 stsp
2931 036813ee 2019-05-09 stsp static const struct got_error *
2932 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2933 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2934 036813ee 2019-05-09 stsp {
2935 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2936 ca2503ea 2019-05-09 stsp
2937 036813ee 2019-05-09 stsp *new_te = NULL;
2938 0b5cc0d6 2019-05-09 stsp
2939 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2940 036813ee 2019-05-09 stsp if (err)
2941 036813ee 2019-05-09 stsp goto done;
2942 0b5cc0d6 2019-05-09 stsp
2943 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2944 036813ee 2019-05-09 stsp
2945 036813ee 2019-05-09 stsp free((*new_te)->id);
2946 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2947 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2948 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2949 036813ee 2019-05-09 stsp goto done;
2950 036813ee 2019-05-09 stsp }
2951 036813ee 2019-05-09 stsp done:
2952 036813ee 2019-05-09 stsp if (err && *new_te) {
2953 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2954 036813ee 2019-05-09 stsp *new_te = NULL;
2955 036813ee 2019-05-09 stsp }
2956 036813ee 2019-05-09 stsp return err;
2957 036813ee 2019-05-09 stsp }
2958 036813ee 2019-05-09 stsp
2959 036813ee 2019-05-09 stsp static const struct got_error *
2960 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2961 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2962 036813ee 2019-05-09 stsp {
2963 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2964 036813ee 2019-05-09 stsp char *ct_name;
2965 036813ee 2019-05-09 stsp
2966 036813ee 2019-05-09 stsp *new_te = NULL;
2967 036813ee 2019-05-09 stsp
2968 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2969 036813ee 2019-05-09 stsp if (*new_te == NULL)
2970 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2971 036813ee 2019-05-09 stsp
2972 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2973 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2974 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2975 036813ee 2019-05-09 stsp goto done;
2976 036813ee 2019-05-09 stsp }
2977 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2978 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2979 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2980 036813ee 2019-05-09 stsp goto done;
2981 036813ee 2019-05-09 stsp }
2982 036813ee 2019-05-09 stsp
2983 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2984 036813ee 2019-05-09 stsp
2985 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2986 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2987 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2988 036813ee 2019-05-09 stsp goto done;
2989 036813ee 2019-05-09 stsp }
2990 036813ee 2019-05-09 stsp done:
2991 036813ee 2019-05-09 stsp if (err && *new_te) {
2992 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2993 036813ee 2019-05-09 stsp *new_te = NULL;
2994 036813ee 2019-05-09 stsp }
2995 036813ee 2019-05-09 stsp return err;
2996 036813ee 2019-05-09 stsp }
2997 036813ee 2019-05-09 stsp
2998 036813ee 2019-05-09 stsp static const struct got_error *
2999 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
3000 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
3001 036813ee 2019-05-09 stsp {
3002 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3003 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
3004 036813ee 2019-05-09 stsp
3005 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3006 036813ee 2019-05-09 stsp if (err)
3007 036813ee 2019-05-09 stsp return err;
3008 036813ee 2019-05-09 stsp if (new_pe == NULL)
3009 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
3010 036813ee 2019-05-09 stsp return NULL;
3011 afa376bf 2019-05-09 stsp }
3012 afa376bf 2019-05-09 stsp
3013 afa376bf 2019-05-09 stsp static const struct got_error *
3014 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
3015 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
3016 afa376bf 2019-05-09 stsp {
3017 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
3018 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
3019 afa376bf 2019-05-09 stsp ct_path++;
3020 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
3021 036813ee 2019-05-09 stsp }
3022 036813ee 2019-05-09 stsp
3023 036813ee 2019-05-09 stsp static const struct got_error *
3024 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
3025 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3026 44d03001 2019-05-09 stsp {
3027 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
3028 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
3029 44d03001 2019-05-09 stsp char *te_path;
3030 44d03001 2019-05-09 stsp
3031 44d03001 2019-05-09 stsp *modified = 0;
3032 44d03001 2019-05-09 stsp
3033 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
3034 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
3035 44d03001 2019-05-09 stsp te->name) == -1)
3036 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3037 44d03001 2019-05-09 stsp
3038 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3039 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3040 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
3041 44d03001 2019-05-09 stsp strlen(te_path));
3042 44d03001 2019-05-09 stsp if (*modified)
3043 44d03001 2019-05-09 stsp break;
3044 44d03001 2019-05-09 stsp }
3045 44d03001 2019-05-09 stsp
3046 44d03001 2019-05-09 stsp free(te_path);
3047 44d03001 2019-05-09 stsp return err;
3048 44d03001 2019-05-09 stsp }
3049 44d03001 2019-05-09 stsp
3050 44d03001 2019-05-09 stsp static const struct got_error *
3051 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
3052 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
3053 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
3054 036813ee 2019-05-09 stsp {
3055 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3056 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3057 036813ee 2019-05-09 stsp
3058 036813ee 2019-05-09 stsp *ctp = NULL;
3059 036813ee 2019-05-09 stsp
3060 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3061 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3062 036813ee 2019-05-09 stsp char *ct_name = NULL;
3063 036813ee 2019-05-09 stsp int path_matches;
3064 036813ee 2019-05-09 stsp
3065 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
3066 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
3067 036813ee 2019-05-09 stsp continue;
3068 036813ee 2019-05-09 stsp
3069 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3070 036813ee 2019-05-09 stsp continue;
3071 036813ee 2019-05-09 stsp
3072 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3073 036813ee 2019-05-09 stsp if (err)
3074 036813ee 2019-05-09 stsp return err;
3075 036813ee 2019-05-09 stsp if (!path_matches)
3076 036813ee 2019-05-09 stsp continue;
3077 036813ee 2019-05-09 stsp
3078 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
3079 036813ee 2019-05-09 stsp if (ct_name == NULL)
3080 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
3081 036813ee 2019-05-09 stsp
3082 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
3083 036813ee 2019-05-09 stsp continue;
3084 036813ee 2019-05-09 stsp
3085 036813ee 2019-05-09 stsp *ctp = ct;
3086 036813ee 2019-05-09 stsp break;
3087 036813ee 2019-05-09 stsp }
3088 2b496619 2019-07-10 stsp
3089 2b496619 2019-07-10 stsp return err;
3090 2b496619 2019-07-10 stsp }
3091 2b496619 2019-07-10 stsp
3092 2b496619 2019-07-10 stsp static const struct got_error *
3093 2b496619 2019-07-10 stsp make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3094 2b496619 2019-07-10 stsp const char *child_path, const char *path_base_tree,
3095 2b496619 2019-07-10 stsp struct got_pathlist_head *commitable_paths,
3096 2b496619 2019-07-10 stsp got_worktree_status_cb status_cb, void *status_arg,
3097 2b496619 2019-07-10 stsp struct got_repository *repo)
3098 2b496619 2019-07-10 stsp {
3099 2b496619 2019-07-10 stsp const struct got_error *err = NULL;
3100 2b496619 2019-07-10 stsp struct got_tree_entry *new_te;
3101 2b496619 2019-07-10 stsp char *subtree_path;
3102 2b496619 2019-07-10 stsp
3103 2b496619 2019-07-10 stsp *new_tep = NULL;
3104 2b496619 2019-07-10 stsp
3105 2b496619 2019-07-10 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3106 2b496619 2019-07-10 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
3107 2b496619 2019-07-10 stsp child_path) == -1)
3108 2b496619 2019-07-10 stsp return got_error_from_errno("asprintf");
3109 036813ee 2019-05-09 stsp
3110 2b496619 2019-07-10 stsp new_te = calloc(1, sizeof(*new_te));
3111 2b496619 2019-07-10 stsp new_te->mode = S_IFDIR;
3112 2b496619 2019-07-10 stsp new_te->name = strdup(child_path);
3113 2b496619 2019-07-10 stsp if (new_te->name == NULL) {
3114 2b496619 2019-07-10 stsp err = got_error_from_errno("strdup");
3115 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3116 2b496619 2019-07-10 stsp goto done;
3117 2b496619 2019-07-10 stsp }
3118 2b496619 2019-07-10 stsp err = write_tree(&new_te->id, NULL, subtree_path,
3119 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg, repo);
3120 2b496619 2019-07-10 stsp if (err) {
3121 2b496619 2019-07-10 stsp got_object_tree_entry_close(new_te);
3122 2b496619 2019-07-10 stsp goto done;
3123 2b496619 2019-07-10 stsp }
3124 2b496619 2019-07-10 stsp done:
3125 2b496619 2019-07-10 stsp free(subtree_path);
3126 2b496619 2019-07-10 stsp if (err == NULL)
3127 2b496619 2019-07-10 stsp *new_tep = new_te;
3128 036813ee 2019-05-09 stsp return err;
3129 036813ee 2019-05-09 stsp }
3130 036813ee 2019-05-09 stsp
3131 036813ee 2019-05-09 stsp static const struct got_error *
3132 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
3133 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
3134 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
3135 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3136 036813ee 2019-05-09 stsp struct got_repository *repo)
3137 036813ee 2019-05-09 stsp {
3138 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
3139 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
3140 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
3141 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
3142 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
3143 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
3144 036813ee 2019-05-09 stsp
3145 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
3146 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
3147 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
3148 036813ee 2019-05-09 stsp
3149 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
3150 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3151 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3152 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
3153 036813ee 2019-05-09 stsp
3154 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
3155 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
3156 036813ee 2019-05-09 stsp continue;
3157 036813ee 2019-05-09 stsp
3158 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
3159 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
3160 036813ee 2019-05-09 stsp continue;
3161 036813ee 2019-05-09 stsp
3162 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3163 036813ee 2019-05-09 stsp pe->path);
3164 036813ee 2019-05-09 stsp if (err)
3165 036813ee 2019-05-09 stsp goto done;
3166 036813ee 2019-05-09 stsp
3167 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
3168 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
3169 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
3170 036813ee 2019-05-09 stsp if (err)
3171 036813ee 2019-05-09 stsp goto done;
3172 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
3173 afa376bf 2019-05-09 stsp if (err)
3174 afa376bf 2019-05-09 stsp goto done;
3175 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
3176 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3177 2b496619 2019-07-10 stsp if (err)
3178 2f51b5b3 2019-05-09 stsp goto done;
3179 2b496619 2019-07-10 stsp } else {
3180 2b496619 2019-07-10 stsp *slash = '\0'; /* trim trailing path components */
3181 2b496619 2019-07-10 stsp if (base_tree == NULL ||
3182 2b496619 2019-07-10 stsp got_object_tree_find_entry(base_tree, child_path)
3183 2b496619 2019-07-10 stsp == NULL) {
3184 2b496619 2019-07-10 stsp err = make_subtree_for_added_blob(&new_te,
3185 2b496619 2019-07-10 stsp child_path, path_base_tree,
3186 2b496619 2019-07-10 stsp commitable_paths, status_cb, status_arg,
3187 2b496619 2019-07-10 stsp repo);
3188 2b496619 2019-07-10 stsp if (err)
3189 2b496619 2019-07-10 stsp goto done;
3190 2b496619 2019-07-10 stsp err = insert_tree_entry(new_te, &paths);
3191 2b496619 2019-07-10 stsp if (err)
3192 2b496619 2019-07-10 stsp goto done;
3193 9ba0479c 2019-05-10 stsp }
3194 ed175427 2019-05-09 stsp }
3195 2f51b5b3 2019-05-09 stsp }
3196 2f51b5b3 2019-05-09 stsp
3197 2f51b5b3 2019-05-09 stsp if (base_tree) {
3198 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
3199 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
3200 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3201 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
3202 2f51b5b3 2019-05-09 stsp
3203 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
3204 44d03001 2019-05-09 stsp int modified;
3205 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3206 036813ee 2019-05-09 stsp if (err)
3207 036813ee 2019-05-09 stsp goto done;
3208 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
3209 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
3210 2f51b5b3 2019-05-09 stsp if (err)
3211 2f51b5b3 2019-05-09 stsp goto done;
3212 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
3213 44d03001 2019-05-09 stsp if (modified) {
3214 44d03001 2019-05-09 stsp free(new_te->id);
3215 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
3216 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
3217 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
3218 44d03001 2019-05-09 stsp if (err)
3219 44d03001 2019-05-09 stsp goto done;
3220 44d03001 2019-05-09 stsp }
3221 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3222 036813ee 2019-05-09 stsp if (err)
3223 036813ee 2019-05-09 stsp goto done;
3224 2f51b5b3 2019-05-09 stsp continue;
3225 036813ee 2019-05-09 stsp }
3226 2f51b5b3 2019-05-09 stsp
3227 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
3228 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
3229 2f51b5b3 2019-05-09 stsp if (ct) {
3230 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3231 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
3232 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3233 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3234 2f51b5b3 2019-05-09 stsp if (err)
3235 2f51b5b3 2019-05-09 stsp goto done;
3236 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3237 2f51b5b3 2019-05-09 stsp if (err)
3238 2f51b5b3 2019-05-09 stsp goto done;
3239 2f51b5b3 2019-05-09 stsp }
3240 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3241 b416585c 2019-05-13 stsp status_arg);
3242 afa376bf 2019-05-09 stsp if (err)
3243 afa376bf 2019-05-09 stsp goto done;
3244 2f51b5b3 2019-05-09 stsp } else {
3245 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3246 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3247 2f51b5b3 2019-05-09 stsp if (err)
3248 2f51b5b3 2019-05-09 stsp goto done;
3249 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3250 2f51b5b3 2019-05-09 stsp if (err)
3251 2f51b5b3 2019-05-09 stsp goto done;
3252 2f51b5b3 2019-05-09 stsp }
3253 ca2503ea 2019-05-09 stsp }
3254 ed175427 2019-05-09 stsp }
3255 0b5cc0d6 2019-05-09 stsp
3256 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3257 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3258 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3259 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3260 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3261 0b5cc0d6 2019-05-09 stsp }
3262 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3263 ed175427 2019-05-09 stsp done:
3264 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3265 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3266 ed175427 2019-05-09 stsp return err;
3267 ed175427 2019-05-09 stsp }
3268 ed175427 2019-05-09 stsp
3269 ebf99748 2019-05-09 stsp static const struct got_error *
3270 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3271 93ec1b5f 2019-07-12 stsp struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3272 ebf99748 2019-05-09 stsp {
3273 8ec7bf54 2019-07-12 stsp const struct got_error *err = NULL;
3274 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3275 ebf99748 2019-05-09 stsp
3276 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3277 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3278 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3279 ebf99748 2019-05-09 stsp
3280 d6c87207 2019-08-02 stsp ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3281 ebf99748 2019-05-09 stsp if (ie) {
3282 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3283 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3284 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3285 ebf99748 2019-05-09 stsp } else
3286 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3287 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3288 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3289 ebf99748 2019-05-09 stsp } else {
3290 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3291 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3292 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3293 ebf99748 2019-05-09 stsp if (err)
3294 af12c6b9 2019-06-04 stsp break;
3295 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3296 ebf99748 2019-05-09 stsp if (err)
3297 af12c6b9 2019-06-04 stsp break;
3298 ebf99748 2019-05-09 stsp }
3299 ebf99748 2019-05-09 stsp }
3300 d56d26ce 2019-05-10 stsp return err;
3301 d56d26ce 2019-05-10 stsp }
3302 d56d26ce 2019-05-10 stsp
3303 d56d26ce 2019-05-10 stsp static const struct got_error *
3304 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3305 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3306 d56d26ce 2019-05-10 stsp {
3307 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3308 9bead371 2019-07-28 stsp struct got_object_id *id = NULL;
3309 1a36436d 2019-06-10 stsp struct got_commit_object *commit = NULL;
3310 1a36436d 2019-06-10 stsp const char *ct_path = ct->in_repo_path;
3311 1a36436d 2019-06-10 stsp
3312 1a36436d 2019-06-10 stsp while (ct_path[0] == '/')
3313 1a36436d 2019-06-10 stsp ct_path++;
3314 d56d26ce 2019-05-10 stsp
3315 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3316 1a36436d 2019-06-10 stsp /* Trivial case: base commit == head commit */
3317 1a36436d 2019-06-10 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3318 1a36436d 2019-06-10 stsp return NULL;
3319 9bead371 2019-07-28 stsp /*
3320 9bead371 2019-07-28 stsp * Ensure file content which local changes were based
3321 9bead371 2019-07-28 stsp * on matches file content in the branch head.
3322 9bead371 2019-07-28 stsp */
3323 9bead371 2019-07-28 stsp err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3324 9bead371 2019-07-28 stsp if (err) {
3325 9bead371 2019-07-28 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY)
3326 9bead371 2019-07-28 stsp goto done;
3327 9bead371 2019-07-28 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3328 1a36436d 2019-06-10 stsp goto done;
3329 9bead371 2019-07-28 stsp } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3330 9bead371 2019-07-28 stsp err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3331 1a36436d 2019-06-10 stsp } else {
3332 1a36436d 2019-06-10 stsp /* Require that added files don't exist in the branch head. */
3333 9bead371 2019-07-28 stsp err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3334 1a36436d 2019-06-10 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3335 1a36436d 2019-06-10 stsp goto done;
3336 9bead371 2019-07-28 stsp err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3337 1a36436d 2019-06-10 stsp }
3338 1a36436d 2019-06-10 stsp done:
3339 1a36436d 2019-06-10 stsp if (commit)
3340 1a36436d 2019-06-10 stsp got_object_commit_close(commit);
3341 1a36436d 2019-06-10 stsp free(id);
3342 1a36436d 2019-06-10 stsp return err;
3343 ebf99748 2019-05-09 stsp }
3344 ebf99748 2019-05-09 stsp
3345 c4296144 2019-05-09 stsp const struct got_error *
3346 39cd0ff6 2019-07-12 stsp commit_worktree(struct got_object_id **new_commit_id,
3347 39cd0ff6 2019-07-12 stsp struct got_pathlist_head *commitable_paths,
3348 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id, struct got_worktree *worktree,
3349 5c1e53bc 2019-07-28 stsp const char *author, const char *committer,
3350 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3351 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3352 afa376bf 2019-05-09 stsp struct got_repository *repo)
3353 c4296144 2019-05-09 stsp {
3354 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3355 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3356 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3357 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3358 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3359 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3360 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3361 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3362 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3363 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3364 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3365 c4296144 2019-05-09 stsp
3366 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3367 c4296144 2019-05-09 stsp
3368 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3369 675c7539 2019-05-09 stsp
3370 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3371 588edf97 2019-05-10 stsp if (err)
3372 588edf97 2019-05-10 stsp goto done;
3373 de18fc63 2019-05-09 stsp
3374 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3375 588edf97 2019-05-10 stsp if (err)
3376 588edf97 2019-05-10 stsp goto done;
3377 588edf97 2019-05-10 stsp
3378 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3379 39cd0ff6 2019-07-12 stsp err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3380 33ad4cbe 2019-05-12 jcs if (err)
3381 33ad4cbe 2019-05-12 jcs goto done;
3382 33ad4cbe 2019-05-12 jcs }
3383 c4296144 2019-05-09 stsp
3384 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3385 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3386 33ad4cbe 2019-05-12 jcs goto done;
3387 33ad4cbe 2019-05-12 jcs }
3388 33ad4cbe 2019-05-12 jcs
3389 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3390 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3391 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3392 cf066bf8 2019-05-09 stsp char *ondisk_path;
3393 cf066bf8 2019-05-09 stsp
3394 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3395 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3396 cf066bf8 2019-05-09 stsp continue;
3397 cf066bf8 2019-05-09 stsp
3398 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3399 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3400 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3401 cf066bf8 2019-05-09 stsp goto done;
3402 cf066bf8 2019-05-09 stsp }
3403 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3404 a7055788 2019-05-09 stsp free(ondisk_path);
3405 cf066bf8 2019-05-09 stsp if (err)
3406 cf066bf8 2019-05-09 stsp goto done;
3407 cf066bf8 2019-05-09 stsp }
3408 036813ee 2019-05-09 stsp
3409 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3410 39cd0ff6 2019-07-12 stsp err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3411 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3412 036813ee 2019-05-09 stsp if (err)
3413 036813ee 2019-05-09 stsp goto done;
3414 036813ee 2019-05-09 stsp
3415 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3416 de18fc63 2019-05-09 stsp if (err)
3417 de18fc63 2019-05-09 stsp goto done;
3418 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3419 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3420 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3421 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3422 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3423 33ad4cbe 2019-05-12 jcs free(logmsg);
3424 9d40349a 2019-05-09 stsp if (err)
3425 9d40349a 2019-05-09 stsp goto done;
3426 9d40349a 2019-05-09 stsp
3427 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3428 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3429 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3430 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3431 09f5bd90 2019-05-09 stsp goto done;
3432 09f5bd90 2019-05-09 stsp }
3433 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3434 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3435 09f5bd90 2019-05-09 stsp if (err)
3436 09f5bd90 2019-05-09 stsp goto done;
3437 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3438 ebf99748 2019-05-09 stsp if (err)
3439 ebf99748 2019-05-09 stsp goto done;
3440 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3441 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3442 09f5bd90 2019-05-09 stsp goto done;
3443 09f5bd90 2019-05-09 stsp }
3444 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3445 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3446 f2c16586 2019-05-09 stsp if (err)
3447 f2c16586 2019-05-09 stsp goto done;
3448 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3449 f2c16586 2019-05-09 stsp if (err)
3450 f2c16586 2019-05-09 stsp goto done;
3451 f2c16586 2019-05-09 stsp
3452 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3453 09f5bd90 2019-05-09 stsp if (err)
3454 09f5bd90 2019-05-09 stsp goto done;
3455 09f5bd90 2019-05-09 stsp
3456 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3457 ebf99748 2019-05-09 stsp if (err)
3458 ebf99748 2019-05-09 stsp goto done;
3459 c4296144 2019-05-09 stsp done:
3460 588edf97 2019-05-10 stsp if (head_tree)
3461 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3462 588edf97 2019-05-10 stsp if (head_commit)
3463 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3464 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3465 2f17228e 2019-05-12 stsp if (head_ref2) {
3466 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3467 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3468 2f17228e 2019-05-12 stsp err = unlockerr;
3469 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3470 39cd0ff6 2019-07-12 stsp }
3471 39cd0ff6 2019-07-12 stsp return err;
3472 39cd0ff6 2019-07-12 stsp }
3473 5c1e53bc 2019-07-28 stsp
3474 5c1e53bc 2019-07-28 stsp static const struct got_error *
3475 5c1e53bc 2019-07-28 stsp check_path_is_commitable(const char *path,
3476 5c1e53bc 2019-07-28 stsp struct got_pathlist_head *commitable_paths)
3477 5c1e53bc 2019-07-28 stsp {
3478 5c1e53bc 2019-07-28 stsp struct got_pathlist_entry *cpe = NULL;
3479 5c1e53bc 2019-07-28 stsp size_t path_len = strlen(path);
3480 39cd0ff6 2019-07-12 stsp
3481 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(cpe, commitable_paths, entry) {
3482 5c1e53bc 2019-07-28 stsp struct got_commitable *ct = cpe->data;
3483 5c1e53bc 2019-07-28 stsp const char *ct_path = ct->path;
3484 5c1e53bc 2019-07-28 stsp
3485 5c1e53bc 2019-07-28 stsp while (ct_path[0] == '/')
3486 5c1e53bc 2019-07-28 stsp ct_path++;
3487 5c1e53bc 2019-07-28 stsp
3488 5c1e53bc 2019-07-28 stsp if (strcmp(path, ct_path) == 0 ||
3489 5c1e53bc 2019-07-28 stsp got_path_is_child(ct_path, path, path_len))
3490 5c1e53bc 2019-07-28 stsp break;
3491 5c1e53bc 2019-07-28 stsp }
3492 5c1e53bc 2019-07-28 stsp
3493 5c1e53bc 2019-07-28 stsp if (cpe == NULL)
3494 5c1e53bc 2019-07-28 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
3495 5c1e53bc 2019-07-28 stsp
3496 5c1e53bc 2019-07-28 stsp return NULL;
3497 5c1e53bc 2019-07-28 stsp }
3498 5c1e53bc 2019-07-28 stsp
3499 39cd0ff6 2019-07-12 stsp const struct got_error *
3500 39cd0ff6 2019-07-12 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3501 5c1e53bc 2019-07-28 stsp struct got_worktree *worktree, struct got_pathlist_head *paths,
3502 39cd0ff6 2019-07-12 stsp const char *author, const char *committer,
3503 39cd0ff6 2019-07-12 stsp got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3504 39cd0ff6 2019-07-12 stsp got_worktree_status_cb status_cb, void *status_arg,
3505 39cd0ff6 2019-07-12 stsp struct got_repository *repo)
3506 39cd0ff6 2019-07-12 stsp {
3507 39cd0ff6 2019-07-12 stsp const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3508 39cd0ff6 2019-07-12 stsp struct got_fileindex *fileindex = NULL;
3509 5c1e53bc 2019-07-28 stsp char *fileindex_path = NULL;
3510 39cd0ff6 2019-07-12 stsp struct got_pathlist_head commitable_paths;
3511 39cd0ff6 2019-07-12 stsp struct collect_commitables_arg cc_arg;
3512 39cd0ff6 2019-07-12 stsp struct got_pathlist_entry *pe;
3513 39cd0ff6 2019-07-12 stsp struct got_reference *head_ref = NULL;
3514 39cd0ff6 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
3515 39cd0ff6 2019-07-12 stsp
3516 39cd0ff6 2019-07-12 stsp *new_commit_id = NULL;
3517 39cd0ff6 2019-07-12 stsp
3518 39cd0ff6 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
3519 39cd0ff6 2019-07-12 stsp
3520 39cd0ff6 2019-07-12 stsp err = lock_worktree(worktree, LOCK_EX);
3521 39cd0ff6 2019-07-12 stsp if (err)
3522 39cd0ff6 2019-07-12 stsp goto done;
3523 39cd0ff6 2019-07-12 stsp
3524 39cd0ff6 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3525 39cd0ff6 2019-07-12 stsp if (err)
3526 39cd0ff6 2019-07-12 stsp goto done;
3527 39cd0ff6 2019-07-12 stsp
3528 39cd0ff6 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3529 39cd0ff6 2019-07-12 stsp if (err)
3530 39cd0ff6 2019-07-12 stsp goto done;
3531 39cd0ff6 2019-07-12 stsp
3532 39cd0ff6 2019-07-12 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3533 39cd0ff6 2019-07-12 stsp if (err)
3534 39cd0ff6 2019-07-12 stsp goto done;
3535 39cd0ff6 2019-07-12 stsp
3536 39cd0ff6 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
3537 39cd0ff6 2019-07-12 stsp cc_arg.worktree = worktree;
3538 39cd0ff6 2019-07-12 stsp cc_arg.repo = repo;
3539 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
3540 5c1e53bc 2019-07-28 stsp err = worktree_status(worktree, pe->path, fileindex, repo,
3541 5c1e53bc 2019-07-28 stsp collect_commitables, &cc_arg, NULL, NULL);
3542 5c1e53bc 2019-07-28 stsp if (err)
3543 5c1e53bc 2019-07-28 stsp goto done;
3544 5c1e53bc 2019-07-28 stsp }
3545 39cd0ff6 2019-07-12 stsp
3546 39cd0ff6 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
3547 39cd0ff6 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3548 39cd0ff6 2019-07-12 stsp goto done;
3549 5c1e53bc 2019-07-28 stsp }
3550 5c1e53bc 2019-07-28 stsp
3551 5c1e53bc 2019-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
3552 5c1e53bc 2019-07-28 stsp err = check_path_is_commitable(pe->path, &commitable_paths);
3553 5c1e53bc 2019-07-28 stsp if (err)
3554 5c1e53bc 2019-07-28 stsp goto done;
3555 39cd0ff6 2019-07-12 stsp }
3556 39cd0ff6 2019-07-12 stsp
3557 b50cabdf 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3558 b50cabdf 2019-07-12 stsp struct got_commitable *ct = pe->data;
3559 b50cabdf 2019-07-12 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3560 b50cabdf 2019-07-12 stsp if (err)
3561 b50cabdf 2019-07-12 stsp goto done;
3562 b50cabdf 2019-07-12 stsp }
3563 b50cabdf 2019-07-12 stsp
3564 39cd0ff6 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths,
3565 5c1e53bc 2019-07-28 stsp head_commit_id, worktree, author, committer,
3566 39cd0ff6 2019-07-12 stsp commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3567 39cd0ff6 2019-07-12 stsp if (err)
3568 39cd0ff6 2019-07-12 stsp goto done;
3569 39cd0ff6 2019-07-12 stsp
3570 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3571 93ec1b5f 2019-07-12 stsp fileindex);
3572 39cd0ff6 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3573 39cd0ff6 2019-07-12 stsp if (sync_err && err == NULL)
3574 39cd0ff6 2019-07-12 stsp err = sync_err;
3575 39cd0ff6 2019-07-12 stsp done:
3576 39cd0ff6 2019-07-12 stsp if (fileindex)
3577 39cd0ff6 2019-07-12 stsp got_fileindex_free(fileindex);
3578 39cd0ff6 2019-07-12 stsp free(fileindex_path);
3579 39cd0ff6 2019-07-12 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3580 39cd0ff6 2019-07-12 stsp if (unlockerr && err == NULL)
3581 39cd0ff6 2019-07-12 stsp err = unlockerr;
3582 39cd0ff6 2019-07-12 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3583 39cd0ff6 2019-07-12 stsp struct got_commitable *ct = pe->data;
3584 39cd0ff6 2019-07-12 stsp free_commitable(ct);
3585 39cd0ff6 2019-07-12 stsp }
3586 39cd0ff6 2019-07-12 stsp got_pathlist_free(&commitable_paths);
3587 c4296144 2019-05-09 stsp return err;
3588 8656d6c4 2019-05-20 stsp }
3589 8656d6c4 2019-05-20 stsp
3590 8656d6c4 2019-05-20 stsp const char *
3591 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3592 8656d6c4 2019-05-20 stsp {
3593 8656d6c4 2019-05-20 stsp return ct->path;
3594 8656d6c4 2019-05-20 stsp }
3595 8656d6c4 2019-05-20 stsp
3596 8656d6c4 2019-05-20 stsp unsigned int
3597 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3598 8656d6c4 2019-05-20 stsp {
3599 8656d6c4 2019-05-20 stsp return ct->status;
3600 818c7501 2019-07-11 stsp }
3601 818c7501 2019-07-11 stsp
3602 818c7501 2019-07-11 stsp struct check_rebase_ok_arg {
3603 818c7501 2019-07-11 stsp struct got_worktree *worktree;
3604 818c7501 2019-07-11 stsp struct got_repository *repo;
3605 818c7501 2019-07-11 stsp };
3606 818c7501 2019-07-11 stsp
3607 818c7501 2019-07-11 stsp static const struct got_error *
3608 818c7501 2019-07-11 stsp check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3609 818c7501 2019-07-11 stsp {
3610 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3611 818c7501 2019-07-11 stsp struct check_rebase_ok_arg *a = arg;
3612 818c7501 2019-07-11 stsp unsigned char status;
3613 818c7501 2019-07-11 stsp struct stat sb;
3614 818c7501 2019-07-11 stsp char *ondisk_path;
3615 818c7501 2019-07-11 stsp
3616 6a263307 2019-07-27 stsp /* Reject rebase of a work tree with mixed base commits. */
3617 6a263307 2019-07-27 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3618 6a263307 2019-07-27 stsp SHA1_DIGEST_LENGTH))
3619 6a263307 2019-07-27 stsp return got_error(GOT_ERR_MIXED_COMMITS);
3620 818c7501 2019-07-11 stsp
3621 818c7501 2019-07-11 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3622 818c7501 2019-07-11 stsp == -1)
3623 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
3624 818c7501 2019-07-11 stsp
3625 818c7501 2019-07-11 stsp /* Reject rebase of a work tree with modified or conflicted files. */
3626 818c7501 2019-07-11 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3627 818c7501 2019-07-11 stsp free(ondisk_path);
3628 818c7501 2019-07-11 stsp if (err)
3629 818c7501 2019-07-11 stsp return err;
3630 818c7501 2019-07-11 stsp
3631 6a263307 2019-07-27 stsp if (status != GOT_STATUS_NO_CHANGE)
3632 818c7501 2019-07-11 stsp return got_error(GOT_ERR_MODIFIED);
3633 818c7501 2019-07-11 stsp
3634 818c7501 2019-07-11 stsp return NULL;
3635 818c7501 2019-07-11 stsp }
3636 818c7501 2019-07-11 stsp
3637 818c7501 2019-07-11 stsp const struct got_error *
3638 818c7501 2019-07-11 stsp got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3639 3e3a69f1 2019-07-25 stsp struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3640 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_reference *branch,
3641 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
3642 818c7501 2019-07-11 stsp {
3643 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
3644 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3645 818c7501 2019-07-11 stsp char *branch_ref_name = NULL;
3646 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
3647 818c7501 2019-07-11 stsp struct check_rebase_ok_arg ok_arg;
3648 818c7501 2019-07-11 stsp struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3649 818c7501 2019-07-11 stsp
3650 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3651 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3652 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3653 818c7501 2019-07-11 stsp
3654 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
3655 818c7501 2019-07-11 stsp if (err)
3656 818c7501 2019-07-11 stsp return err;
3657 818c7501 2019-07-11 stsp
3658 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3659 818c7501 2019-07-11 stsp if (err)
3660 818c7501 2019-07-11 stsp goto done;
3661 818c7501 2019-07-11 stsp
3662 818c7501 2019-07-11 stsp ok_arg.worktree = worktree;
3663 818c7501 2019-07-11 stsp ok_arg.repo = repo;
3664 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3665 818c7501 2019-07-11 stsp &ok_arg);
3666 818c7501 2019-07-11 stsp if (err)
3667 818c7501 2019-07-11 stsp goto done;
3668 818c7501 2019-07-11 stsp
3669 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3670 818c7501 2019-07-11 stsp if (err)
3671 818c7501 2019-07-11 stsp goto done;
3672 818c7501 2019-07-11 stsp
3673 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3674 818c7501 2019-07-11 stsp if (err)
3675 818c7501 2019-07-11 stsp goto done;
3676 818c7501 2019-07-11 stsp
3677 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3678 818c7501 2019-07-11 stsp if (err)
3679 818c7501 2019-07-11 stsp goto done;
3680 818c7501 2019-07-11 stsp
3681 818c7501 2019-07-11 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3682 818c7501 2019-07-11 stsp 0);
3683 818c7501 2019-07-11 stsp if (err)
3684 818c7501 2019-07-11 stsp goto done;
3685 818c7501 2019-07-11 stsp
3686 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(new_base_branch_ref,
3687 818c7501 2019-07-11 stsp new_base_branch_ref_name, wt_branch);
3688 818c7501 2019-07-11 stsp if (err)
3689 818c7501 2019-07-11 stsp goto done;
3690 818c7501 2019-07-11 stsp err = got_ref_write(*new_base_branch_ref, repo);
3691 818c7501 2019-07-11 stsp if (err)
3692 818c7501 2019-07-11 stsp goto done;
3693 818c7501 2019-07-11 stsp
3694 818c7501 2019-07-11 stsp /* TODO Lock original branch's ref while rebasing? */
3695 818c7501 2019-07-11 stsp
3696 818c7501 2019-07-11 stsp err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3697 818c7501 2019-07-11 stsp if (err)
3698 818c7501 2019-07-11 stsp goto done;
3699 818c7501 2019-07-11 stsp
3700 818c7501 2019-07-11 stsp err = got_ref_write(branch_ref, repo);
3701 818c7501 2019-07-11 stsp if (err)
3702 818c7501 2019-07-11 stsp goto done;
3703 818c7501 2019-07-11 stsp
3704 818c7501 2019-07-11 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
3705 818c7501 2019-07-11 stsp worktree->base_commit_id);
3706 818c7501 2019-07-11 stsp if (err)
3707 818c7501 2019-07-11 stsp goto done;
3708 818c7501 2019-07-11 stsp err = got_ref_write(*tmp_branch, repo);
3709 818c7501 2019-07-11 stsp if (err)
3710 818c7501 2019-07-11 stsp goto done;
3711 818c7501 2019-07-11 stsp
3712 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
3713 818c7501 2019-07-11 stsp if (err)
3714 818c7501 2019-07-11 stsp goto done;
3715 818c7501 2019-07-11 stsp done:
3716 818c7501 2019-07-11 stsp free(fileindex_path);
3717 818c7501 2019-07-11 stsp free(tmp_branch_name);
3718 818c7501 2019-07-11 stsp free(new_base_branch_ref_name);
3719 818c7501 2019-07-11 stsp free(branch_ref_name);
3720 818c7501 2019-07-11 stsp if (branch_ref)
3721 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3722 818c7501 2019-07-11 stsp if (wt_branch)
3723 818c7501 2019-07-11 stsp got_ref_close(wt_branch);
3724 818c7501 2019-07-11 stsp if (err) {
3725 818c7501 2019-07-11 stsp if (*new_base_branch_ref) {
3726 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch_ref);
3727 818c7501 2019-07-11 stsp *new_base_branch_ref = NULL;
3728 818c7501 2019-07-11 stsp }
3729 818c7501 2019-07-11 stsp if (*tmp_branch) {
3730 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3731 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3732 818c7501 2019-07-11 stsp }
3733 3e3a69f1 2019-07-25 stsp if (*fileindex) {
3734 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
3735 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3736 3e3a69f1 2019-07-25 stsp }
3737 818c7501 2019-07-11 stsp lock_worktree(worktree, LOCK_SH);
3738 818c7501 2019-07-11 stsp }
3739 818c7501 2019-07-11 stsp return err;
3740 818c7501 2019-07-11 stsp }
3741 818c7501 2019-07-11 stsp
3742 818c7501 2019-07-11 stsp const struct got_error *
3743 818c7501 2019-07-11 stsp got_worktree_rebase_continue(struct got_object_id **commit_id,
3744 818c7501 2019-07-11 stsp struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3745 3e3a69f1 2019-07-25 stsp struct got_reference **branch, struct got_fileindex **fileindex,
3746 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_repository *repo)
3747 818c7501 2019-07-11 stsp {
3748 818c7501 2019-07-11 stsp const struct got_error *err;
3749 818c7501 2019-07-11 stsp char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3750 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3751 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3752 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
3753 818c7501 2019-07-11 stsp
3754 818c7501 2019-07-11 stsp *commit_id = NULL;
3755 3e3a69f1 2019-07-25 stsp *new_base_branch = NULL;
3756 3e3a69f1 2019-07-25 stsp *tmp_branch = NULL;
3757 3e3a69f1 2019-07-25 stsp *branch = NULL;
3758 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3759 3e3a69f1 2019-07-25 stsp
3760 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
3761 3e3a69f1 2019-07-25 stsp if (err)
3762 3e3a69f1 2019-07-25 stsp return err;
3763 818c7501 2019-07-11 stsp
3764 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
3765 3e3a69f1 2019-07-25 stsp if (err)
3766 3e3a69f1 2019-07-25 stsp goto done;
3767 3e3a69f1 2019-07-25 stsp
3768 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3769 818c7501 2019-07-11 stsp if (err)
3770 818c7501 2019-07-11 stsp return err;
3771 818c7501 2019-07-11 stsp
3772 818c7501 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3773 818c7501 2019-07-11 stsp if (err)
3774 818c7501 2019-07-11 stsp goto done;
3775 818c7501 2019-07-11 stsp
3776 818c7501 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3777 818c7501 2019-07-11 stsp if (err)
3778 818c7501 2019-07-11 stsp goto done;
3779 818c7501 2019-07-11 stsp
3780 818c7501 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3781 818c7501 2019-07-11 stsp if (err)
3782 818c7501 2019-07-11 stsp goto done;
3783 818c7501 2019-07-11 stsp
3784 818c7501 2019-07-11 stsp err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3785 818c7501 2019-07-11 stsp if (err)
3786 818c7501 2019-07-11 stsp goto done;
3787 818c7501 2019-07-11 stsp
3788 818c7501 2019-07-11 stsp err = got_ref_open(branch, repo,
3789 818c7501 2019-07-11 stsp got_ref_get_symref_target(branch_ref), 0);
3790 818c7501 2019-07-11 stsp if (err)
3791 818c7501 2019-07-11 stsp goto done;
3792 818c7501 2019-07-11 stsp
3793 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3794 818c7501 2019-07-11 stsp if (err)
3795 818c7501 2019-07-11 stsp goto done;
3796 818c7501 2019-07-11 stsp
3797 818c7501 2019-07-11 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
3798 818c7501 2019-07-11 stsp if (err)
3799 818c7501 2019-07-11 stsp goto done;
3800 818c7501 2019-07-11 stsp
3801 818c7501 2019-07-11 stsp err = got_ref_open(new_base_branch, repo,
3802 818c7501 2019-07-11 stsp new_base_branch_ref_name, 0);
3803 818c7501 2019-07-11 stsp if (err)
3804 818c7501 2019-07-11 stsp goto done;
3805 818c7501 2019-07-11 stsp
3806 818c7501 2019-07-11 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3807 818c7501 2019-07-11 stsp if (err)
3808 818c7501 2019-07-11 stsp goto done;
3809 818c7501 2019-07-11 stsp done:
3810 818c7501 2019-07-11 stsp free(commit_ref_name);
3811 818c7501 2019-07-11 stsp free(branch_ref_name);
3812 3e3a69f1 2019-07-25 stsp free(fileindex_path);
3813 818c7501 2019-07-11 stsp if (commit_ref)
3814 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3815 818c7501 2019-07-11 stsp if (branch_ref)
3816 818c7501 2019-07-11 stsp got_ref_close(branch_ref);
3817 818c7501 2019-07-11 stsp if (err) {
3818 818c7501 2019-07-11 stsp free(*commit_id);
3819 818c7501 2019-07-11 stsp *commit_id = NULL;
3820 818c7501 2019-07-11 stsp if (*tmp_branch) {
3821 818c7501 2019-07-11 stsp got_ref_close(*tmp_branch);
3822 818c7501 2019-07-11 stsp *tmp_branch = NULL;
3823 818c7501 2019-07-11 stsp }
3824 818c7501 2019-07-11 stsp if (*new_base_branch) {
3825 818c7501 2019-07-11 stsp got_ref_close(*new_base_branch);
3826 818c7501 2019-07-11 stsp *new_base_branch = NULL;
3827 818c7501 2019-07-11 stsp }
3828 818c7501 2019-07-11 stsp if (*branch) {
3829 818c7501 2019-07-11 stsp got_ref_close(*branch);
3830 818c7501 2019-07-11 stsp *branch = NULL;
3831 818c7501 2019-07-11 stsp }
3832 3e3a69f1 2019-07-25 stsp if (*fileindex) {
3833 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
3834 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
3835 3e3a69f1 2019-07-25 stsp }
3836 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_SH);
3837 818c7501 2019-07-11 stsp }
3838 818c7501 2019-07-11 stsp return err;
3839 c4296144 2019-05-09 stsp }
3840 818c7501 2019-07-11 stsp
3841 818c7501 2019-07-11 stsp const struct got_error *
3842 818c7501 2019-07-11 stsp got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3843 818c7501 2019-07-11 stsp {
3844 818c7501 2019-07-11 stsp const struct got_error *err;
3845 818c7501 2019-07-11 stsp char *tmp_branch_name = NULL;
3846 818c7501 2019-07-11 stsp
3847 818c7501 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3848 818c7501 2019-07-11 stsp if (err)
3849 818c7501 2019-07-11 stsp return err;
3850 818c7501 2019-07-11 stsp
3851 818c7501 2019-07-11 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3852 818c7501 2019-07-11 stsp free(tmp_branch_name);
3853 818c7501 2019-07-11 stsp return NULL;
3854 818c7501 2019-07-11 stsp }
3855 818c7501 2019-07-11 stsp
3856 818c7501 2019-07-11 stsp static const struct got_error *
3857 818c7501 2019-07-11 stsp collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3858 818c7501 2019-07-11 stsp char **logmsg, void *arg)
3859 818c7501 2019-07-11 stsp {
3860 0ebf8283 2019-07-24 stsp *logmsg = arg;
3861 818c7501 2019-07-11 stsp return NULL;
3862 818c7501 2019-07-11 stsp }
3863 818c7501 2019-07-11 stsp
3864 818c7501 2019-07-11 stsp static const struct got_error *
3865 818c7501 2019-07-11 stsp rebase_status(void *arg, unsigned char status, const char *path,
3866 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
3867 818c7501 2019-07-11 stsp {
3868 818c7501 2019-07-11 stsp return NULL;
3869 01757395 2019-07-12 stsp }
3870 01757395 2019-07-12 stsp
3871 01757395 2019-07-12 stsp struct collect_merged_paths_arg {
3872 01757395 2019-07-12 stsp got_worktree_checkout_cb progress_cb;
3873 01757395 2019-07-12 stsp void *progress_arg;
3874 01757395 2019-07-12 stsp struct got_pathlist_head *merged_paths;
3875 01757395 2019-07-12 stsp };
3876 01757395 2019-07-12 stsp
3877 01757395 2019-07-12 stsp static const struct got_error *
3878 01757395 2019-07-12 stsp collect_merged_paths(void *arg, unsigned char status, const char *path)
3879 01757395 2019-07-12 stsp {
3880 01757395 2019-07-12 stsp const struct got_error *err;
3881 01757395 2019-07-12 stsp struct collect_merged_paths_arg *a = arg;
3882 01757395 2019-07-12 stsp char *p;
3883 01757395 2019-07-12 stsp struct got_pathlist_entry *new;
3884 01757395 2019-07-12 stsp
3885 01757395 2019-07-12 stsp err = (*a->progress_cb)(a->progress_arg, status, path);
3886 01757395 2019-07-12 stsp if (err)
3887 01757395 2019-07-12 stsp return err;
3888 01757395 2019-07-12 stsp
3889 01757395 2019-07-12 stsp if (status != GOT_STATUS_MERGE &&
3890 01757395 2019-07-12 stsp status != GOT_STATUS_ADD &&
3891 01757395 2019-07-12 stsp status != GOT_STATUS_DELETE &&
3892 01757395 2019-07-12 stsp status != GOT_STATUS_CONFLICT)
3893 01757395 2019-07-12 stsp return NULL;
3894 01757395 2019-07-12 stsp
3895 01757395 2019-07-12 stsp p = strdup(path);
3896 01757395 2019-07-12 stsp if (p == NULL)
3897 01757395 2019-07-12 stsp return got_error_from_errno("strdup");
3898 01757395 2019-07-12 stsp
3899 01757395 2019-07-12 stsp err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3900 01757395 2019-07-12 stsp if (err || new == NULL)
3901 01757395 2019-07-12 stsp free(p);
3902 01757395 2019-07-12 stsp return err;
3903 01757395 2019-07-12 stsp }
3904 01757395 2019-07-12 stsp
3905 01757395 2019-07-12 stsp void
3906 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3907 01757395 2019-07-12 stsp {
3908 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
3909 01757395 2019-07-12 stsp
3910 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry)
3911 01757395 2019-07-12 stsp free((char *)pe->path);
3912 01757395 2019-07-12 stsp
3913 01757395 2019-07-12 stsp got_pathlist_free(merged_paths);
3914 818c7501 2019-07-11 stsp }
3915 818c7501 2019-07-11 stsp
3916 0ebf8283 2019-07-24 stsp static const struct got_error *
3917 0ebf8283 2019-07-24 stsp store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3918 0ebf8283 2019-07-24 stsp struct got_repository *repo)
3919 818c7501 2019-07-11 stsp {
3920 818c7501 2019-07-11 stsp const struct got_error *err;
3921 818c7501 2019-07-11 stsp struct got_reference *commit_ref = NULL;
3922 818c7501 2019-07-11 stsp
3923 818c7501 2019-07-11 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3924 818c7501 2019-07-11 stsp if (err) {
3925 818c7501 2019-07-11 stsp if (err->code != GOT_ERR_NOT_REF)
3926 818c7501 2019-07-11 stsp goto done;
3927 818c7501 2019-07-11 stsp err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3928 818c7501 2019-07-11 stsp if (err)
3929 818c7501 2019-07-11 stsp goto done;
3930 818c7501 2019-07-11 stsp err = got_ref_write(commit_ref, repo);
3931 818c7501 2019-07-11 stsp if (err)
3932 818c7501 2019-07-11 stsp goto done;
3933 818c7501 2019-07-11 stsp } else {
3934 818c7501 2019-07-11 stsp struct got_object_id *stored_id;
3935 818c7501 2019-07-11 stsp int cmp;
3936 818c7501 2019-07-11 stsp
3937 818c7501 2019-07-11 stsp err = got_ref_resolve(&stored_id, repo, commit_ref);
3938 818c7501 2019-07-11 stsp if (err)
3939 818c7501 2019-07-11 stsp goto done;
3940 818c7501 2019-07-11 stsp cmp = got_object_id_cmp(commit_id, stored_id);
3941 818c7501 2019-07-11 stsp free(stored_id);
3942 818c7501 2019-07-11 stsp if (cmp != 0) {
3943 818c7501 2019-07-11 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
3944 818c7501 2019-07-11 stsp goto done;
3945 818c7501 2019-07-11 stsp }
3946 818c7501 2019-07-11 stsp }
3947 0ebf8283 2019-07-24 stsp done:
3948 0ebf8283 2019-07-24 stsp if (commit_ref)
3949 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
3950 0ebf8283 2019-07-24 stsp return err;
3951 0ebf8283 2019-07-24 stsp }
3952 0ebf8283 2019-07-24 stsp
3953 0ebf8283 2019-07-24 stsp static const struct got_error *
3954 0ebf8283 2019-07-24 stsp rebase_merge_files(struct got_pathlist_head *merged_paths,
3955 0ebf8283 2019-07-24 stsp const char *commit_ref_name, struct got_worktree *worktree,
3956 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3957 3e3a69f1 2019-07-25 stsp struct got_object_id *commit_id, struct got_repository *repo,
3958 3e3a69f1 2019-07-25 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3959 3e3a69f1 2019-07-25 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3960 0ebf8283 2019-07-24 stsp {
3961 0ebf8283 2019-07-24 stsp const struct got_error *err;
3962 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
3963 0ebf8283 2019-07-24 stsp struct collect_merged_paths_arg cmp_arg;
3964 3e3a69f1 2019-07-25 stsp char *fileindex_path;
3965 818c7501 2019-07-11 stsp
3966 0ebf8283 2019-07-24 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
3967 0ebf8283 2019-07-24 stsp
3968 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
3969 0ebf8283 2019-07-24 stsp if (err)
3970 0ebf8283 2019-07-24 stsp return err;
3971 0ebf8283 2019-07-24 stsp
3972 01757395 2019-07-12 stsp cmp_arg.progress_cb = progress_cb;
3973 01757395 2019-07-12 stsp cmp_arg.progress_arg = progress_arg;
3974 01757395 2019-07-12 stsp cmp_arg.merged_paths = merged_paths;
3975 818c7501 2019-07-11 stsp err = merge_files(worktree, fileindex, fileindex_path,
3976 01757395 2019-07-12 stsp parent_commit_id, commit_id, repo, collect_merged_paths,
3977 01757395 2019-07-12 stsp &cmp_arg, cancel_cb, cancel_arg);
3978 818c7501 2019-07-11 stsp if (commit_ref)
3979 818c7501 2019-07-11 stsp got_ref_close(commit_ref);
3980 818c7501 2019-07-11 stsp return err;
3981 818c7501 2019-07-11 stsp }
3982 818c7501 2019-07-11 stsp
3983 818c7501 2019-07-11 stsp const struct got_error *
3984 0ebf8283 2019-07-24 stsp got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3985 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
3986 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3987 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
3988 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
3989 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3990 818c7501 2019-07-11 stsp {
3991 0ebf8283 2019-07-24 stsp const struct got_error *err;
3992 0ebf8283 2019-07-24 stsp char *commit_ref_name;
3993 0ebf8283 2019-07-24 stsp
3994 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3995 0ebf8283 2019-07-24 stsp if (err)
3996 0ebf8283 2019-07-24 stsp return err;
3997 0ebf8283 2019-07-24 stsp
3998 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
3999 0ebf8283 2019-07-24 stsp if (err)
4000 0ebf8283 2019-07-24 stsp goto done;
4001 0ebf8283 2019-07-24 stsp
4002 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4003 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4004 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4005 0ebf8283 2019-07-24 stsp done:
4006 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4007 0ebf8283 2019-07-24 stsp return err;
4008 0ebf8283 2019-07-24 stsp }
4009 0ebf8283 2019-07-24 stsp
4010 0ebf8283 2019-07-24 stsp const struct got_error *
4011 0ebf8283 2019-07-24 stsp got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4012 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4013 3e3a69f1 2019-07-25 stsp struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4014 3e3a69f1 2019-07-25 stsp struct got_repository *repo,
4015 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
4016 0ebf8283 2019-07-24 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4017 0ebf8283 2019-07-24 stsp {
4018 0ebf8283 2019-07-24 stsp const struct got_error *err;
4019 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4020 0ebf8283 2019-07-24 stsp
4021 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4022 0ebf8283 2019-07-24 stsp if (err)
4023 0ebf8283 2019-07-24 stsp return err;
4024 0ebf8283 2019-07-24 stsp
4025 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4026 0ebf8283 2019-07-24 stsp if (err)
4027 0ebf8283 2019-07-24 stsp goto done;
4028 0ebf8283 2019-07-24 stsp
4029 0ebf8283 2019-07-24 stsp err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4030 3e3a69f1 2019-07-25 stsp fileindex, parent_commit_id, commit_id, repo, progress_cb,
4031 3e3a69f1 2019-07-25 stsp progress_arg, cancel_cb, cancel_arg);
4032 0ebf8283 2019-07-24 stsp done:
4033 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4034 0ebf8283 2019-07-24 stsp return err;
4035 0ebf8283 2019-07-24 stsp }
4036 0ebf8283 2019-07-24 stsp
4037 0ebf8283 2019-07-24 stsp static const struct got_error *
4038 0ebf8283 2019-07-24 stsp rebase_commit(struct got_object_id **new_commit_id,
4039 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4040 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4041 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4042 3e3a69f1 2019-07-25 stsp const char *new_logmsg, struct got_repository *repo)
4043 0ebf8283 2019-07-24 stsp {
4044 a0e95631 2019-07-12 stsp const struct got_error *err, *sync_err;
4045 a0e95631 2019-07-12 stsp struct got_pathlist_head commitable_paths;
4046 a0e95631 2019-07-12 stsp struct collect_commitables_arg cc_arg;
4047 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4048 a0e95631 2019-07-12 stsp struct got_reference *head_ref = NULL;
4049 a0e95631 2019-07-12 stsp struct got_object_id *head_commit_id = NULL;
4050 0ebf8283 2019-07-24 stsp char *logmsg = NULL;
4051 818c7501 2019-07-11 stsp
4052 a0e95631 2019-07-12 stsp TAILQ_INIT(&commitable_paths);
4053 eb3df2c4 2019-07-12 stsp *new_commit_id = NULL;
4054 a0e95631 2019-07-12 stsp
4055 877a927c 2019-07-12 stsp /* Work tree is locked/unlocked during rebase preparation/teardown. */
4056 818c7501 2019-07-11 stsp
4057 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4058 a0e95631 2019-07-12 stsp if (err)
4059 3e3a69f1 2019-07-25 stsp return err;
4060 a0e95631 2019-07-12 stsp
4061 a0e95631 2019-07-12 stsp cc_arg.commitable_paths = &commitable_paths;
4062 a0e95631 2019-07-12 stsp cc_arg.worktree = worktree;
4063 a0e95631 2019-07-12 stsp cc_arg.repo = repo;
4064 01757395 2019-07-12 stsp /*
4065 01757395 2019-07-12 stsp * If possible get the status of individual files directly to
4066 01757395 2019-07-12 stsp * avoid crawling the entire work tree once per rebased commit.
4067 01757395 2019-07-12 stsp * TODO: Ideally, merged_paths would contain a list of commitables
4068 01757395 2019-07-12 stsp * we could use so we could skip worktree_status() entirely.
4069 01757395 2019-07-12 stsp */
4070 01757395 2019-07-12 stsp if (merged_paths) {
4071 01757395 2019-07-12 stsp struct got_pathlist_entry *pe;
4072 8f8646e5 2019-07-25 stsp if (TAILQ_EMPTY(merged_paths)) {
4073 8f8646e5 2019-07-25 stsp err = got_error(GOT_ERR_NO_MERGED_PATHS);
4074 8f8646e5 2019-07-25 stsp goto done;
4075 8f8646e5 2019-07-25 stsp }
4076 01757395 2019-07-12 stsp TAILQ_FOREACH(pe, merged_paths, entry) {
4077 01757395 2019-07-12 stsp err = worktree_status(worktree, pe->path, fileindex,
4078 01757395 2019-07-12 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
4079 01757395 2019-07-12 stsp if (err)
4080 01757395 2019-07-12 stsp goto done;
4081 01757395 2019-07-12 stsp }
4082 01757395 2019-07-12 stsp } else {
4083 01757395 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4084 01757395 2019-07-12 stsp collect_commitables, &cc_arg, NULL, NULL);
4085 01757395 2019-07-12 stsp if (err)
4086 01757395 2019-07-12 stsp goto done;
4087 01757395 2019-07-12 stsp }
4088 a0e95631 2019-07-12 stsp
4089 a0e95631 2019-07-12 stsp if (TAILQ_EMPTY(&commitable_paths)) {
4090 a0e95631 2019-07-12 stsp /* No-op change; commit will be elided. */
4091 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4092 a0e95631 2019-07-12 stsp if (err)
4093 a0e95631 2019-07-12 stsp goto done;
4094 a0e95631 2019-07-12 stsp err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4095 a0e95631 2019-07-12 stsp goto done;
4096 ff0d2220 2019-07-11 stsp }
4097 818c7501 2019-07-11 stsp
4098 a0e95631 2019-07-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4099 edd02c5e 2019-07-11 stsp if (err)
4100 edd02c5e 2019-07-11 stsp goto done;
4101 edd02c5e 2019-07-11 stsp
4102 a0e95631 2019-07-12 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
4103 818c7501 2019-07-11 stsp if (err)
4104 818c7501 2019-07-11 stsp goto done;
4105 818c7501 2019-07-11 stsp
4106 0ebf8283 2019-07-24 stsp if (new_logmsg)
4107 0ebf8283 2019-07-24 stsp logmsg = strdup(new_logmsg);
4108 0ebf8283 2019-07-24 stsp else
4109 0ebf8283 2019-07-24 stsp logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4110 0ebf8283 2019-07-24 stsp if (logmsg == NULL)
4111 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
4112 0ebf8283 2019-07-24 stsp
4113 a0e95631 2019-07-12 stsp err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4114 5c1e53bc 2019-07-28 stsp worktree, got_object_commit_get_author(orig_commit),
4115 a0e95631 2019-07-12 stsp got_object_commit_get_committer(orig_commit),
4116 0ebf8283 2019-07-24 stsp collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4117 a0e95631 2019-07-12 stsp if (err)
4118 a0e95631 2019-07-12 stsp goto done;
4119 a0e95631 2019-07-12 stsp
4120 818c7501 2019-07-11 stsp err = got_ref_change_ref(tmp_branch, *new_commit_id);
4121 a0e95631 2019-07-12 stsp if (err)
4122 a0e95631 2019-07-12 stsp goto done;
4123 a0e95631 2019-07-12 stsp
4124 a0e95631 2019-07-12 stsp err = got_ref_delete(commit_ref, repo);
4125 a0e95631 2019-07-12 stsp if (err)
4126 a0e95631 2019-07-12 stsp goto done;
4127 a0e95631 2019-07-12 stsp
4128 93ec1b5f 2019-07-12 stsp err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4129 93ec1b5f 2019-07-12 stsp fileindex);
4130 a0e95631 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4131 a0e95631 2019-07-12 stsp if (sync_err && err == NULL)
4132 a0e95631 2019-07-12 stsp err = sync_err;
4133 818c7501 2019-07-11 stsp done:
4134 a0e95631 2019-07-12 stsp free(fileindex_path);
4135 a0e95631 2019-07-12 stsp free(head_commit_id);
4136 a0e95631 2019-07-12 stsp if (head_ref)
4137 a0e95631 2019-07-12 stsp got_ref_close(head_ref);
4138 818c7501 2019-07-11 stsp if (err) {
4139 818c7501 2019-07-11 stsp free(*new_commit_id);
4140 818c7501 2019-07-11 stsp *new_commit_id = NULL;
4141 818c7501 2019-07-11 stsp }
4142 818c7501 2019-07-11 stsp return err;
4143 818c7501 2019-07-11 stsp }
4144 818c7501 2019-07-11 stsp
4145 818c7501 2019-07-11 stsp const struct got_error *
4146 0ebf8283 2019-07-24 stsp got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4147 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4148 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4149 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4150 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, struct got_repository *repo)
4151 0ebf8283 2019-07-24 stsp {
4152 0ebf8283 2019-07-24 stsp const struct got_error *err;
4153 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4154 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4155 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4156 0ebf8283 2019-07-24 stsp
4157 0ebf8283 2019-07-24 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4158 0ebf8283 2019-07-24 stsp if (err)
4159 0ebf8283 2019-07-24 stsp return err;
4160 0ebf8283 2019-07-24 stsp
4161 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4162 0ebf8283 2019-07-24 stsp if (err)
4163 0ebf8283 2019-07-24 stsp goto done;
4164 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4165 0ebf8283 2019-07-24 stsp if (err)
4166 0ebf8283 2019-07-24 stsp goto done;
4167 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4168 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_REBASE_COMMITID);
4169 0ebf8283 2019-07-24 stsp goto done;
4170 0ebf8283 2019-07-24 stsp }
4171 0ebf8283 2019-07-24 stsp
4172 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4173 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4174 0ebf8283 2019-07-24 stsp done:
4175 0ebf8283 2019-07-24 stsp if (commit_ref)
4176 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4177 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4178 0ebf8283 2019-07-24 stsp free(commit_id);
4179 0ebf8283 2019-07-24 stsp return err;
4180 0ebf8283 2019-07-24 stsp }
4181 0ebf8283 2019-07-24 stsp
4182 0ebf8283 2019-07-24 stsp const struct got_error *
4183 0ebf8283 2019-07-24 stsp got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4184 0ebf8283 2019-07-24 stsp struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4185 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4186 3e3a69f1 2019-07-25 stsp struct got_commit_object *orig_commit,
4187 0ebf8283 2019-07-24 stsp struct got_object_id *orig_commit_id, const char *new_logmsg,
4188 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4189 0ebf8283 2019-07-24 stsp {
4190 0ebf8283 2019-07-24 stsp const struct got_error *err;
4191 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4192 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4193 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4194 0ebf8283 2019-07-24 stsp
4195 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4196 0ebf8283 2019-07-24 stsp if (err)
4197 0ebf8283 2019-07-24 stsp return err;
4198 0ebf8283 2019-07-24 stsp
4199 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4200 0ebf8283 2019-07-24 stsp if (err)
4201 0ebf8283 2019-07-24 stsp goto done;
4202 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&commit_id, repo, commit_ref);
4203 0ebf8283 2019-07-24 stsp if (err)
4204 0ebf8283 2019-07-24 stsp goto done;
4205 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4206 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4207 0ebf8283 2019-07-24 stsp goto done;
4208 0ebf8283 2019-07-24 stsp }
4209 0ebf8283 2019-07-24 stsp
4210 0ebf8283 2019-07-24 stsp err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4211 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4212 0ebf8283 2019-07-24 stsp done:
4213 0ebf8283 2019-07-24 stsp if (commit_ref)
4214 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4215 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4216 0ebf8283 2019-07-24 stsp free(commit_id);
4217 0ebf8283 2019-07-24 stsp return err;
4218 0ebf8283 2019-07-24 stsp }
4219 0ebf8283 2019-07-24 stsp
4220 0ebf8283 2019-07-24 stsp const struct got_error *
4221 3e3a69f1 2019-07-25 stsp got_worktree_rebase_postpone(struct got_worktree *worktree,
4222 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4223 818c7501 2019-07-11 stsp {
4224 3e3a69f1 2019-07-25 stsp if (fileindex)
4225 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4226 818c7501 2019-07-11 stsp return lock_worktree(worktree, LOCK_SH);
4227 69844fba 2019-07-11 stsp }
4228 69844fba 2019-07-11 stsp
4229 69844fba 2019-07-11 stsp static const struct got_error *
4230 69844fba 2019-07-11 stsp delete_ref(const char *name, struct got_repository *repo)
4231 69844fba 2019-07-11 stsp {
4232 69844fba 2019-07-11 stsp const struct got_error *err;
4233 69844fba 2019-07-11 stsp struct got_reference *ref;
4234 69844fba 2019-07-11 stsp
4235 69844fba 2019-07-11 stsp err = got_ref_open(&ref, repo, name, 0);
4236 69844fba 2019-07-11 stsp if (err) {
4237 69844fba 2019-07-11 stsp if (err->code == GOT_ERR_NOT_REF)
4238 69844fba 2019-07-11 stsp return NULL;
4239 69844fba 2019-07-11 stsp return err;
4240 69844fba 2019-07-11 stsp }
4241 69844fba 2019-07-11 stsp
4242 69844fba 2019-07-11 stsp err = got_ref_delete(ref, repo);
4243 69844fba 2019-07-11 stsp got_ref_close(ref);
4244 69844fba 2019-07-11 stsp return err;
4245 818c7501 2019-07-11 stsp }
4246 818c7501 2019-07-11 stsp
4247 69844fba 2019-07-11 stsp static const struct got_error *
4248 69844fba 2019-07-11 stsp delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4249 69844fba 2019-07-11 stsp {
4250 69844fba 2019-07-11 stsp const struct got_error *err;
4251 69844fba 2019-07-11 stsp char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4252 69844fba 2019-07-11 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4253 69844fba 2019-07-11 stsp
4254 69844fba 2019-07-11 stsp err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4255 69844fba 2019-07-11 stsp if (err)
4256 69844fba 2019-07-11 stsp goto done;
4257 69844fba 2019-07-11 stsp err = delete_ref(tmp_branch_name, repo);
4258 69844fba 2019-07-11 stsp if (err)
4259 69844fba 2019-07-11 stsp goto done;
4260 69844fba 2019-07-11 stsp
4261 69844fba 2019-07-11 stsp err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4262 69844fba 2019-07-11 stsp if (err)
4263 69844fba 2019-07-11 stsp goto done;
4264 69844fba 2019-07-11 stsp err = delete_ref(new_base_branch_ref_name, repo);
4265 69844fba 2019-07-11 stsp if (err)
4266 69844fba 2019-07-11 stsp goto done;
4267 69844fba 2019-07-11 stsp
4268 69844fba 2019-07-11 stsp err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4269 69844fba 2019-07-11 stsp if (err)
4270 69844fba 2019-07-11 stsp goto done;
4271 69844fba 2019-07-11 stsp err = delete_ref(branch_ref_name, repo);
4272 69844fba 2019-07-11 stsp if (err)
4273 69844fba 2019-07-11 stsp goto done;
4274 69844fba 2019-07-11 stsp
4275 69844fba 2019-07-11 stsp err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4276 69844fba 2019-07-11 stsp if (err)
4277 69844fba 2019-07-11 stsp goto done;
4278 69844fba 2019-07-11 stsp err = delete_ref(commit_ref_name, repo);
4279 69844fba 2019-07-11 stsp if (err)
4280 69844fba 2019-07-11 stsp goto done;
4281 69844fba 2019-07-11 stsp
4282 69844fba 2019-07-11 stsp done:
4283 69844fba 2019-07-11 stsp free(tmp_branch_name);
4284 69844fba 2019-07-11 stsp free(new_base_branch_ref_name);
4285 69844fba 2019-07-11 stsp free(branch_ref_name);
4286 69844fba 2019-07-11 stsp free(commit_ref_name);
4287 69844fba 2019-07-11 stsp return err;
4288 69844fba 2019-07-11 stsp }
4289 69844fba 2019-07-11 stsp
4290 818c7501 2019-07-11 stsp const struct got_error *
4291 818c7501 2019-07-11 stsp got_worktree_rebase_complete(struct got_worktree *worktree,
4292 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4293 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4294 818c7501 2019-07-11 stsp struct got_repository *repo)
4295 818c7501 2019-07-11 stsp {
4296 818c7501 2019-07-11 stsp const struct got_error *err, *unlockerr;
4297 818c7501 2019-07-11 stsp struct got_object_id *new_head_commit_id = NULL;
4298 818c7501 2019-07-11 stsp
4299 818c7501 2019-07-11 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4300 818c7501 2019-07-11 stsp if (err)
4301 818c7501 2019-07-11 stsp return err;
4302 818c7501 2019-07-11 stsp
4303 818c7501 2019-07-11 stsp err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4304 818c7501 2019-07-11 stsp if (err)
4305 818c7501 2019-07-11 stsp goto done;
4306 818c7501 2019-07-11 stsp
4307 818c7501 2019-07-11 stsp err = got_ref_write(rebased_branch, repo);
4308 818c7501 2019-07-11 stsp if (err)
4309 818c7501 2019-07-11 stsp goto done;
4310 818c7501 2019-07-11 stsp
4311 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, rebased_branch);
4312 818c7501 2019-07-11 stsp if (err)
4313 818c7501 2019-07-11 stsp goto done;
4314 818c7501 2019-07-11 stsp
4315 69844fba 2019-07-11 stsp err = delete_rebase_refs(worktree, repo);
4316 818c7501 2019-07-11 stsp done:
4317 3e3a69f1 2019-07-25 stsp if (fileindex)
4318 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4319 818c7501 2019-07-11 stsp free(new_head_commit_id);
4320 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4321 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4322 818c7501 2019-07-11 stsp err = unlockerr;
4323 818c7501 2019-07-11 stsp return err;
4324 818c7501 2019-07-11 stsp }
4325 818c7501 2019-07-11 stsp
4326 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg {
4327 818c7501 2019-07-11 stsp struct got_pathlist_head *revertible_paths;
4328 818c7501 2019-07-11 stsp struct got_worktree *worktree;
4329 818c7501 2019-07-11 stsp };
4330 818c7501 2019-07-11 stsp
4331 818c7501 2019-07-11 stsp static const struct got_error *
4332 818c7501 2019-07-11 stsp collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4333 818c7501 2019-07-11 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
4334 818c7501 2019-07-11 stsp {
4335 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg *a = arg;
4336 818c7501 2019-07-11 stsp const struct got_error *err = NULL;
4337 818c7501 2019-07-11 stsp struct got_pathlist_entry *new = NULL;
4338 818c7501 2019-07-11 stsp char *path = NULL;
4339 818c7501 2019-07-11 stsp
4340 818c7501 2019-07-11 stsp if (status != GOT_STATUS_ADD &&
4341 818c7501 2019-07-11 stsp status != GOT_STATUS_DELETE &&
4342 818c7501 2019-07-11 stsp status != GOT_STATUS_MODIFY &&
4343 818c7501 2019-07-11 stsp status != GOT_STATUS_CONFLICT &&
4344 818c7501 2019-07-11 stsp status != GOT_STATUS_MISSING)
4345 818c7501 2019-07-11 stsp return NULL;
4346 818c7501 2019-07-11 stsp
4347 818c7501 2019-07-11 stsp if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4348 818c7501 2019-07-11 stsp return got_error_from_errno("asprintf");
4349 818c7501 2019-07-11 stsp
4350 818c7501 2019-07-11 stsp err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4351 818c7501 2019-07-11 stsp if (err || new == NULL)
4352 818c7501 2019-07-11 stsp free(path);
4353 818c7501 2019-07-11 stsp return err;
4354 818c7501 2019-07-11 stsp }
4355 818c7501 2019-07-11 stsp
4356 818c7501 2019-07-11 stsp const struct got_error *
4357 818c7501 2019-07-11 stsp got_worktree_rebase_abort(struct got_worktree *worktree,
4358 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4359 3e3a69f1 2019-07-25 stsp struct got_reference *new_base_branch,
4360 818c7501 2019-07-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4361 818c7501 2019-07-11 stsp {
4362 ca355955 2019-07-12 stsp const struct got_error *err, *unlockerr, *sync_err;
4363 818c7501 2019-07-11 stsp struct got_reference *resolved = NULL;
4364 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL;
4365 818c7501 2019-07-11 stsp char *fileindex_path = NULL;
4366 818c7501 2019-07-11 stsp struct got_pathlist_head revertible_paths;
4367 818c7501 2019-07-11 stsp struct got_pathlist_entry *pe;
4368 818c7501 2019-07-11 stsp struct collect_revertible_paths_arg crp_arg;
4369 a3a2faf2 2019-07-12 stsp struct got_object_id *tree_id = NULL;
4370 818c7501 2019-07-11 stsp
4371 818c7501 2019-07-11 stsp TAILQ_INIT(&revertible_paths);
4372 818c7501 2019-07-11 stsp
4373 818c7501 2019-07-11 stsp err = lock_worktree(worktree, LOCK_EX);
4374 818c7501 2019-07-11 stsp if (err)
4375 818c7501 2019-07-11 stsp return err;
4376 818c7501 2019-07-11 stsp
4377 818c7501 2019-07-11 stsp err = got_ref_open(&resolved, repo,
4378 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch), 0);
4379 818c7501 2019-07-11 stsp if (err)
4380 818c7501 2019-07-11 stsp goto done;
4381 818c7501 2019-07-11 stsp
4382 818c7501 2019-07-11 stsp err = got_worktree_set_head_ref(worktree, resolved);
4383 818c7501 2019-07-11 stsp if (err)
4384 818c7501 2019-07-11 stsp goto done;
4385 818c7501 2019-07-11 stsp
4386 818c7501 2019-07-11 stsp /*
4387 818c7501 2019-07-11 stsp * XXX commits to the base branch could have happened while
4388 818c7501 2019-07-11 stsp * we were busy rebasing; should we store the original commit ID
4389 818c7501 2019-07-11 stsp * when rebase begins and read it back here?
4390 818c7501 2019-07-11 stsp */
4391 818c7501 2019-07-11 stsp err = got_ref_resolve(&commit_id, repo, resolved);
4392 818c7501 2019-07-11 stsp if (err)
4393 818c7501 2019-07-11 stsp goto done;
4394 818c7501 2019-07-11 stsp
4395 818c7501 2019-07-11 stsp err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4396 818c7501 2019-07-11 stsp if (err)
4397 818c7501 2019-07-11 stsp goto done;
4398 818c7501 2019-07-11 stsp
4399 ca355955 2019-07-12 stsp err = got_object_id_by_path(&tree_id, repo,
4400 ca355955 2019-07-12 stsp worktree->base_commit_id, worktree->path_prefix);
4401 ca355955 2019-07-12 stsp if (err)
4402 ca355955 2019-07-12 stsp goto done;
4403 ca355955 2019-07-12 stsp
4404 ca355955 2019-07-12 stsp err = delete_rebase_refs(worktree, repo);
4405 ca355955 2019-07-12 stsp if (err)
4406 ca355955 2019-07-12 stsp goto done;
4407 ca355955 2019-07-12 stsp
4408 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4409 818c7501 2019-07-11 stsp if (err)
4410 818c7501 2019-07-11 stsp goto done;
4411 818c7501 2019-07-11 stsp
4412 347d1d3e 2019-07-12 stsp crp_arg.revertible_paths = &revertible_paths;
4413 347d1d3e 2019-07-12 stsp crp_arg.worktree = worktree;
4414 347d1d3e 2019-07-12 stsp err = worktree_status(worktree, "", fileindex, repo,
4415 347d1d3e 2019-07-12 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4416 55bd499d 2019-07-12 stsp if (err)
4417 55bd499d 2019-07-12 stsp goto done;
4418 55bd499d 2019-07-12 stsp
4419 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4420 818c7501 2019-07-11 stsp err = revert_file(worktree, fileindex, pe->path,
4421 818c7501 2019-07-11 stsp progress_cb, progress_arg, repo);
4422 818c7501 2019-07-11 stsp if (err)
4423 ca355955 2019-07-12 stsp goto sync;
4424 818c7501 2019-07-11 stsp }
4425 69844fba 2019-07-11 stsp
4426 267fb255 2019-07-12 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4427 267fb255 2019-07-12 stsp repo, progress_cb, progress_arg, NULL, NULL);
4428 ca355955 2019-07-12 stsp sync:
4429 ca355955 2019-07-12 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4430 ca355955 2019-07-12 stsp if (sync_err && err == NULL)
4431 ca355955 2019-07-12 stsp err = sync_err;
4432 818c7501 2019-07-11 stsp done:
4433 818c7501 2019-07-11 stsp got_ref_close(resolved);
4434 a3a2faf2 2019-07-12 stsp free(tree_id);
4435 818c7501 2019-07-11 stsp free(commit_id);
4436 0ebf8283 2019-07-24 stsp if (fileindex)
4437 0ebf8283 2019-07-24 stsp got_fileindex_free(fileindex);
4438 0ebf8283 2019-07-24 stsp free(fileindex_path);
4439 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
4440 0ebf8283 2019-07-24 stsp free((char *)pe->path);
4441 0ebf8283 2019-07-24 stsp got_pathlist_free(&revertible_paths);
4442 0ebf8283 2019-07-24 stsp
4443 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4444 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
4445 0ebf8283 2019-07-24 stsp err = unlockerr;
4446 0ebf8283 2019-07-24 stsp return err;
4447 0ebf8283 2019-07-24 stsp }
4448 0ebf8283 2019-07-24 stsp
4449 0ebf8283 2019-07-24 stsp const struct got_error *
4450 0ebf8283 2019-07-24 stsp got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4451 0ebf8283 2019-07-24 stsp struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4452 3e3a69f1 2019-07-25 stsp struct got_fileindex **fileindex, struct got_worktree *worktree,
4453 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4454 0ebf8283 2019-07-24 stsp {
4455 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4456 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4457 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL;
4458 0ebf8283 2019-07-24 stsp char *base_commit_ref_name = NULL;
4459 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4460 0ebf8283 2019-07-24 stsp struct check_rebase_ok_arg ok_arg;
4461 0ebf8283 2019-07-24 stsp struct got_reference *wt_branch = NULL;
4462 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4463 0ebf8283 2019-07-24 stsp
4464 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4465 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4466 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4467 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4468 0ebf8283 2019-07-24 stsp
4469 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4470 0ebf8283 2019-07-24 stsp if (err)
4471 0ebf8283 2019-07-24 stsp return err;
4472 0ebf8283 2019-07-24 stsp
4473 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4474 0ebf8283 2019-07-24 stsp if (err)
4475 0ebf8283 2019-07-24 stsp goto done;
4476 0ebf8283 2019-07-24 stsp
4477 0ebf8283 2019-07-24 stsp ok_arg.worktree = worktree;
4478 0ebf8283 2019-07-24 stsp ok_arg.repo = repo;
4479 3e3a69f1 2019-07-25 stsp err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4480 0ebf8283 2019-07-24 stsp &ok_arg);
4481 0ebf8283 2019-07-24 stsp if (err)
4482 0ebf8283 2019-07-24 stsp goto done;
4483 0ebf8283 2019-07-24 stsp
4484 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4485 0ebf8283 2019-07-24 stsp if (err)
4486 0ebf8283 2019-07-24 stsp goto done;
4487 0ebf8283 2019-07-24 stsp
4488 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4489 0ebf8283 2019-07-24 stsp if (err)
4490 0ebf8283 2019-07-24 stsp goto done;
4491 0ebf8283 2019-07-24 stsp
4492 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4493 0ebf8283 2019-07-24 stsp worktree);
4494 0ebf8283 2019-07-24 stsp if (err)
4495 0ebf8283 2019-07-24 stsp goto done;
4496 0ebf8283 2019-07-24 stsp
4497 0ebf8283 2019-07-24 stsp err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4498 0ebf8283 2019-07-24 stsp 0);
4499 0ebf8283 2019-07-24 stsp if (err)
4500 0ebf8283 2019-07-24 stsp goto done;
4501 0ebf8283 2019-07-24 stsp
4502 0ebf8283 2019-07-24 stsp err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4503 0ebf8283 2019-07-24 stsp if (err)
4504 0ebf8283 2019-07-24 stsp goto done;
4505 0ebf8283 2019-07-24 stsp
4506 0ebf8283 2019-07-24 stsp err = got_ref_write(*branch_ref, repo);
4507 0ebf8283 2019-07-24 stsp if (err)
4508 0ebf8283 2019-07-24 stsp goto done;
4509 0ebf8283 2019-07-24 stsp
4510 0ebf8283 2019-07-24 stsp err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4511 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4512 0ebf8283 2019-07-24 stsp if (err)
4513 0ebf8283 2019-07-24 stsp goto done;
4514 0ebf8283 2019-07-24 stsp err = got_ref_write(base_commit_ref, repo);
4515 0ebf8283 2019-07-24 stsp if (err)
4516 0ebf8283 2019-07-24 stsp goto done;
4517 0ebf8283 2019-07-24 stsp *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4518 0ebf8283 2019-07-24 stsp if (*base_commit_id == NULL) {
4519 0ebf8283 2019-07-24 stsp err = got_error_from_errno("got_object_id_dup");
4520 0ebf8283 2019-07-24 stsp goto done;
4521 0ebf8283 2019-07-24 stsp }
4522 0ebf8283 2019-07-24 stsp
4523 0ebf8283 2019-07-24 stsp err = got_ref_alloc(tmp_branch, tmp_branch_name,
4524 0ebf8283 2019-07-24 stsp worktree->base_commit_id);
4525 0ebf8283 2019-07-24 stsp if (err)
4526 0ebf8283 2019-07-24 stsp goto done;
4527 0ebf8283 2019-07-24 stsp err = got_ref_write(*tmp_branch, repo);
4528 0ebf8283 2019-07-24 stsp if (err)
4529 0ebf8283 2019-07-24 stsp goto done;
4530 0ebf8283 2019-07-24 stsp
4531 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, *tmp_branch);
4532 0ebf8283 2019-07-24 stsp if (err)
4533 0ebf8283 2019-07-24 stsp goto done;
4534 0ebf8283 2019-07-24 stsp done:
4535 0ebf8283 2019-07-24 stsp free(fileindex_path);
4536 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4537 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4538 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4539 0ebf8283 2019-07-24 stsp if (wt_branch)
4540 0ebf8283 2019-07-24 stsp got_ref_close(wt_branch);
4541 0ebf8283 2019-07-24 stsp if (err) {
4542 0ebf8283 2019-07-24 stsp if (*branch_ref) {
4543 0ebf8283 2019-07-24 stsp got_ref_close(*branch_ref);
4544 0ebf8283 2019-07-24 stsp *branch_ref = NULL;
4545 0ebf8283 2019-07-24 stsp }
4546 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4547 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4548 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4549 0ebf8283 2019-07-24 stsp }
4550 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4551 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4552 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4553 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4554 3e3a69f1 2019-07-25 stsp }
4555 0ebf8283 2019-07-24 stsp lock_worktree(worktree, LOCK_SH);
4556 0ebf8283 2019-07-24 stsp }
4557 0ebf8283 2019-07-24 stsp return err;
4558 0ebf8283 2019-07-24 stsp }
4559 0ebf8283 2019-07-24 stsp
4560 0ebf8283 2019-07-24 stsp const struct got_error *
4561 3e3a69f1 2019-07-25 stsp got_worktree_histedit_postpone(struct got_worktree *worktree,
4562 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex)
4563 0ebf8283 2019-07-24 stsp {
4564 3e3a69f1 2019-07-25 stsp if (fileindex)
4565 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4566 0ebf8283 2019-07-24 stsp return lock_worktree(worktree, LOCK_SH);
4567 0ebf8283 2019-07-24 stsp }
4568 0ebf8283 2019-07-24 stsp
4569 0ebf8283 2019-07-24 stsp const struct got_error *
4570 0ebf8283 2019-07-24 stsp got_worktree_histedit_in_progress(int *in_progress,
4571 0ebf8283 2019-07-24 stsp struct got_worktree *worktree)
4572 0ebf8283 2019-07-24 stsp {
4573 0ebf8283 2019-07-24 stsp const struct got_error *err;
4574 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL;
4575 0ebf8283 2019-07-24 stsp
4576 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4577 0ebf8283 2019-07-24 stsp if (err)
4578 0ebf8283 2019-07-24 stsp return err;
4579 0ebf8283 2019-07-24 stsp
4580 0ebf8283 2019-07-24 stsp *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4581 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4582 0ebf8283 2019-07-24 stsp return NULL;
4583 0ebf8283 2019-07-24 stsp }
4584 0ebf8283 2019-07-24 stsp
4585 0ebf8283 2019-07-24 stsp const struct got_error *
4586 0ebf8283 2019-07-24 stsp got_worktree_histedit_continue(struct got_object_id **commit_id,
4587 0ebf8283 2019-07-24 stsp struct got_reference **tmp_branch, struct got_reference **branch_ref,
4588 3e3a69f1 2019-07-25 stsp struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4589 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4590 0ebf8283 2019-07-24 stsp {
4591 0ebf8283 2019-07-24 stsp const struct got_error *err;
4592 0ebf8283 2019-07-24 stsp char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4593 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4594 0ebf8283 2019-07-24 stsp struct got_reference *commit_ref = NULL;
4595 0ebf8283 2019-07-24 stsp struct got_reference *base_commit_ref = NULL;
4596 3e3a69f1 2019-07-25 stsp char *fileindex_path = NULL;
4597 0ebf8283 2019-07-24 stsp
4598 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4599 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4600 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4601 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4602 0ebf8283 2019-07-24 stsp
4603 3e3a69f1 2019-07-25 stsp err = lock_worktree(worktree, LOCK_EX);
4604 3e3a69f1 2019-07-25 stsp if (err)
4605 3e3a69f1 2019-07-25 stsp return err;
4606 3e3a69f1 2019-07-25 stsp
4607 3e3a69f1 2019-07-25 stsp err = open_fileindex(fileindex, &fileindex_path, worktree);
4608 3e3a69f1 2019-07-25 stsp if (err)
4609 3e3a69f1 2019-07-25 stsp goto done;
4610 3e3a69f1 2019-07-25 stsp
4611 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4612 0ebf8283 2019-07-24 stsp if (err)
4613 0ebf8283 2019-07-24 stsp return err;
4614 0ebf8283 2019-07-24 stsp
4615 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4616 0ebf8283 2019-07-24 stsp if (err)
4617 0ebf8283 2019-07-24 stsp goto done;
4618 0ebf8283 2019-07-24 stsp
4619 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4620 0ebf8283 2019-07-24 stsp if (err)
4621 0ebf8283 2019-07-24 stsp goto done;
4622 0ebf8283 2019-07-24 stsp
4623 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4624 0ebf8283 2019-07-24 stsp worktree);
4625 0ebf8283 2019-07-24 stsp if (err)
4626 0ebf8283 2019-07-24 stsp goto done;
4627 0ebf8283 2019-07-24 stsp
4628 0ebf8283 2019-07-24 stsp err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4629 0ebf8283 2019-07-24 stsp if (err)
4630 0ebf8283 2019-07-24 stsp goto done;
4631 0ebf8283 2019-07-24 stsp
4632 0ebf8283 2019-07-24 stsp err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4633 0ebf8283 2019-07-24 stsp if (err)
4634 0ebf8283 2019-07-24 stsp goto done;
4635 0ebf8283 2019-07-24 stsp err = got_ref_resolve(commit_id, repo, commit_ref);
4636 0ebf8283 2019-07-24 stsp if (err)
4637 0ebf8283 2019-07-24 stsp goto done;
4638 0ebf8283 2019-07-24 stsp
4639 0ebf8283 2019-07-24 stsp err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4640 0ebf8283 2019-07-24 stsp if (err)
4641 0ebf8283 2019-07-24 stsp goto done;
4642 0ebf8283 2019-07-24 stsp err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4643 0ebf8283 2019-07-24 stsp if (err)
4644 0ebf8283 2019-07-24 stsp goto done;
4645 0ebf8283 2019-07-24 stsp
4646 0ebf8283 2019-07-24 stsp err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4647 0ebf8283 2019-07-24 stsp if (err)
4648 0ebf8283 2019-07-24 stsp goto done;
4649 0ebf8283 2019-07-24 stsp done:
4650 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4651 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4652 3e3a69f1 2019-07-25 stsp free(fileindex_path);
4653 0ebf8283 2019-07-24 stsp if (commit_ref)
4654 0ebf8283 2019-07-24 stsp got_ref_close(commit_ref);
4655 0ebf8283 2019-07-24 stsp if (base_commit_ref)
4656 0ebf8283 2019-07-24 stsp got_ref_close(base_commit_ref);
4657 0ebf8283 2019-07-24 stsp if (err) {
4658 0ebf8283 2019-07-24 stsp free(*commit_id);
4659 0ebf8283 2019-07-24 stsp *commit_id = NULL;
4660 0ebf8283 2019-07-24 stsp free(*base_commit_id);
4661 0ebf8283 2019-07-24 stsp *base_commit_id = NULL;
4662 0ebf8283 2019-07-24 stsp if (*tmp_branch) {
4663 0ebf8283 2019-07-24 stsp got_ref_close(*tmp_branch);
4664 0ebf8283 2019-07-24 stsp *tmp_branch = NULL;
4665 0ebf8283 2019-07-24 stsp }
4666 3e3a69f1 2019-07-25 stsp if (*fileindex) {
4667 3e3a69f1 2019-07-25 stsp got_fileindex_free(*fileindex);
4668 3e3a69f1 2019-07-25 stsp *fileindex = NULL;
4669 3e3a69f1 2019-07-25 stsp }
4670 3e3a69f1 2019-07-25 stsp lock_worktree(worktree, LOCK_EX);
4671 0ebf8283 2019-07-24 stsp }
4672 0ebf8283 2019-07-24 stsp return err;
4673 0ebf8283 2019-07-24 stsp }
4674 0ebf8283 2019-07-24 stsp
4675 0ebf8283 2019-07-24 stsp static const struct got_error *
4676 0ebf8283 2019-07-24 stsp delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4677 0ebf8283 2019-07-24 stsp {
4678 0ebf8283 2019-07-24 stsp const struct got_error *err;
4679 0ebf8283 2019-07-24 stsp char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4680 0ebf8283 2019-07-24 stsp char *branch_ref_name = NULL, *commit_ref_name = NULL;
4681 0ebf8283 2019-07-24 stsp
4682 0ebf8283 2019-07-24 stsp err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4683 0ebf8283 2019-07-24 stsp if (err)
4684 0ebf8283 2019-07-24 stsp goto done;
4685 0ebf8283 2019-07-24 stsp err = delete_ref(tmp_branch_name, repo);
4686 0ebf8283 2019-07-24 stsp if (err)
4687 0ebf8283 2019-07-24 stsp goto done;
4688 0ebf8283 2019-07-24 stsp
4689 0ebf8283 2019-07-24 stsp err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4690 0ebf8283 2019-07-24 stsp worktree);
4691 0ebf8283 2019-07-24 stsp if (err)
4692 0ebf8283 2019-07-24 stsp goto done;
4693 0ebf8283 2019-07-24 stsp err = delete_ref(base_commit_ref_name, repo);
4694 0ebf8283 2019-07-24 stsp if (err)
4695 0ebf8283 2019-07-24 stsp goto done;
4696 0ebf8283 2019-07-24 stsp
4697 0ebf8283 2019-07-24 stsp err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4698 0ebf8283 2019-07-24 stsp if (err)
4699 0ebf8283 2019-07-24 stsp goto done;
4700 0ebf8283 2019-07-24 stsp err = delete_ref(branch_ref_name, repo);
4701 0ebf8283 2019-07-24 stsp if (err)
4702 0ebf8283 2019-07-24 stsp goto done;
4703 0ebf8283 2019-07-24 stsp
4704 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4705 0ebf8283 2019-07-24 stsp if (err)
4706 0ebf8283 2019-07-24 stsp goto done;
4707 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
4708 0ebf8283 2019-07-24 stsp if (err)
4709 0ebf8283 2019-07-24 stsp goto done;
4710 0ebf8283 2019-07-24 stsp done:
4711 0ebf8283 2019-07-24 stsp free(tmp_branch_name);
4712 0ebf8283 2019-07-24 stsp free(base_commit_ref_name);
4713 0ebf8283 2019-07-24 stsp free(branch_ref_name);
4714 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4715 0ebf8283 2019-07-24 stsp return err;
4716 0ebf8283 2019-07-24 stsp }
4717 0ebf8283 2019-07-24 stsp
4718 0ebf8283 2019-07-24 stsp const struct got_error *
4719 0ebf8283 2019-07-24 stsp got_worktree_histedit_abort(struct got_worktree *worktree,
4720 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_repository *repo,
4721 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_object_id *base_commit_id,
4722 0ebf8283 2019-07-24 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
4723 0ebf8283 2019-07-24 stsp {
4724 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr, *sync_err;
4725 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
4726 0ebf8283 2019-07-24 stsp char *fileindex_path = NULL;
4727 0ebf8283 2019-07-24 stsp struct got_pathlist_head revertible_paths;
4728 0ebf8283 2019-07-24 stsp struct got_pathlist_entry *pe;
4729 0ebf8283 2019-07-24 stsp struct collect_revertible_paths_arg crp_arg;
4730 0ebf8283 2019-07-24 stsp struct got_object_id *tree_id = NULL;
4731 0ebf8283 2019-07-24 stsp
4732 0ebf8283 2019-07-24 stsp TAILQ_INIT(&revertible_paths);
4733 0ebf8283 2019-07-24 stsp
4734 0ebf8283 2019-07-24 stsp err = lock_worktree(worktree, LOCK_EX);
4735 0ebf8283 2019-07-24 stsp if (err)
4736 0ebf8283 2019-07-24 stsp return err;
4737 0ebf8283 2019-07-24 stsp
4738 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
4739 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch), 0);
4740 0ebf8283 2019-07-24 stsp if (err)
4741 0ebf8283 2019-07-24 stsp goto done;
4742 0ebf8283 2019-07-24 stsp
4743 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
4744 0ebf8283 2019-07-24 stsp if (err)
4745 0ebf8283 2019-07-24 stsp goto done;
4746 0ebf8283 2019-07-24 stsp
4747 0ebf8283 2019-07-24 stsp err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4748 0ebf8283 2019-07-24 stsp if (err)
4749 0ebf8283 2019-07-24 stsp goto done;
4750 0ebf8283 2019-07-24 stsp
4751 0ebf8283 2019-07-24 stsp err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4752 0ebf8283 2019-07-24 stsp worktree->path_prefix);
4753 0ebf8283 2019-07-24 stsp if (err)
4754 0ebf8283 2019-07-24 stsp goto done;
4755 0ebf8283 2019-07-24 stsp
4756 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
4757 0ebf8283 2019-07-24 stsp if (err)
4758 0ebf8283 2019-07-24 stsp goto done;
4759 0ebf8283 2019-07-24 stsp
4760 3e3a69f1 2019-07-25 stsp err = get_fileindex_path(&fileindex_path, worktree);
4761 0ebf8283 2019-07-24 stsp if (err)
4762 0ebf8283 2019-07-24 stsp goto done;
4763 0ebf8283 2019-07-24 stsp
4764 0ebf8283 2019-07-24 stsp crp_arg.revertible_paths = &revertible_paths;
4765 0ebf8283 2019-07-24 stsp crp_arg.worktree = worktree;
4766 0ebf8283 2019-07-24 stsp err = worktree_status(worktree, "", fileindex, repo,
4767 0ebf8283 2019-07-24 stsp collect_revertible_paths, &crp_arg, NULL, NULL);
4768 0ebf8283 2019-07-24 stsp if (err)
4769 0ebf8283 2019-07-24 stsp goto done;
4770 0ebf8283 2019-07-24 stsp
4771 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(pe, &revertible_paths, entry) {
4772 0ebf8283 2019-07-24 stsp err = revert_file(worktree, fileindex, pe->path,
4773 0ebf8283 2019-07-24 stsp progress_cb, progress_arg, repo);
4774 0ebf8283 2019-07-24 stsp if (err)
4775 0ebf8283 2019-07-24 stsp goto sync;
4776 0ebf8283 2019-07-24 stsp }
4777 0ebf8283 2019-07-24 stsp
4778 0ebf8283 2019-07-24 stsp err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4779 0ebf8283 2019-07-24 stsp repo, progress_cb, progress_arg, NULL, NULL);
4780 0ebf8283 2019-07-24 stsp sync:
4781 0ebf8283 2019-07-24 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
4782 0ebf8283 2019-07-24 stsp if (sync_err && err == NULL)
4783 0ebf8283 2019-07-24 stsp err = sync_err;
4784 0ebf8283 2019-07-24 stsp done:
4785 0ebf8283 2019-07-24 stsp got_ref_close(resolved);
4786 0ebf8283 2019-07-24 stsp free(tree_id);
4787 818c7501 2019-07-11 stsp free(fileindex_path);
4788 818c7501 2019-07-11 stsp TAILQ_FOREACH(pe, &revertible_paths, entry)
4789 818c7501 2019-07-11 stsp free((char *)pe->path);
4790 818c7501 2019-07-11 stsp got_pathlist_free(&revertible_paths);
4791 818c7501 2019-07-11 stsp
4792 818c7501 2019-07-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4793 818c7501 2019-07-11 stsp if (unlockerr && err == NULL)
4794 818c7501 2019-07-11 stsp err = unlockerr;
4795 818c7501 2019-07-11 stsp return err;
4796 818c7501 2019-07-11 stsp }
4797 0ebf8283 2019-07-24 stsp
4798 0ebf8283 2019-07-24 stsp const struct got_error *
4799 0ebf8283 2019-07-24 stsp got_worktree_histedit_complete(struct got_worktree *worktree,
4800 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4801 3e3a69f1 2019-07-25 stsp struct got_reference *edited_branch, struct got_repository *repo)
4802 0ebf8283 2019-07-24 stsp {
4803 0ebf8283 2019-07-24 stsp const struct got_error *err, *unlockerr;
4804 0ebf8283 2019-07-24 stsp struct got_object_id *new_head_commit_id = NULL;
4805 0ebf8283 2019-07-24 stsp struct got_reference *resolved = NULL;
4806 0ebf8283 2019-07-24 stsp
4807 0ebf8283 2019-07-24 stsp err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4808 0ebf8283 2019-07-24 stsp if (err)
4809 0ebf8283 2019-07-24 stsp return err;
4810 0ebf8283 2019-07-24 stsp
4811 0ebf8283 2019-07-24 stsp err = got_ref_open(&resolved, repo,
4812 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(edited_branch), 0);
4813 0ebf8283 2019-07-24 stsp if (err)
4814 0ebf8283 2019-07-24 stsp goto done;
4815 0ebf8283 2019-07-24 stsp
4816 0ebf8283 2019-07-24 stsp err = got_ref_change_ref(resolved, new_head_commit_id);
4817 0ebf8283 2019-07-24 stsp if (err)
4818 0ebf8283 2019-07-24 stsp goto done;
4819 0ebf8283 2019-07-24 stsp
4820 0ebf8283 2019-07-24 stsp err = got_ref_write(resolved, repo);
4821 0ebf8283 2019-07-24 stsp if (err)
4822 0ebf8283 2019-07-24 stsp goto done;
4823 0ebf8283 2019-07-24 stsp
4824 0ebf8283 2019-07-24 stsp err = got_worktree_set_head_ref(worktree, resolved);
4825 0ebf8283 2019-07-24 stsp if (err)
4826 0ebf8283 2019-07-24 stsp goto done;
4827 0ebf8283 2019-07-24 stsp
4828 0ebf8283 2019-07-24 stsp err = delete_histedit_refs(worktree, repo);
4829 0ebf8283 2019-07-24 stsp done:
4830 3e3a69f1 2019-07-25 stsp if (fileindex)
4831 3e3a69f1 2019-07-25 stsp got_fileindex_free(fileindex);
4832 0ebf8283 2019-07-24 stsp free(new_head_commit_id);
4833 0ebf8283 2019-07-24 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
4834 0ebf8283 2019-07-24 stsp if (unlockerr && err == NULL)
4835 0ebf8283 2019-07-24 stsp err = unlockerr;
4836 0ebf8283 2019-07-24 stsp return err;
4837 0ebf8283 2019-07-24 stsp }
4838 0ebf8283 2019-07-24 stsp
4839 0ebf8283 2019-07-24 stsp const struct got_error *
4840 0ebf8283 2019-07-24 stsp got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4841 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4842 0ebf8283 2019-07-24 stsp {
4843 0ebf8283 2019-07-24 stsp const struct got_error *err;
4844 0ebf8283 2019-07-24 stsp char *commit_ref_name;
4845 0ebf8283 2019-07-24 stsp
4846 0ebf8283 2019-07-24 stsp err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4847 0ebf8283 2019-07-24 stsp if (err)
4848 0ebf8283 2019-07-24 stsp return err;
4849 0ebf8283 2019-07-24 stsp
4850 0ebf8283 2019-07-24 stsp err = store_commit_id(commit_ref_name, commit_id, repo);
4851 0ebf8283 2019-07-24 stsp if (err)
4852 0ebf8283 2019-07-24 stsp goto done;
4853 0ebf8283 2019-07-24 stsp
4854 0ebf8283 2019-07-24 stsp err = delete_ref(commit_ref_name, repo);
4855 0ebf8283 2019-07-24 stsp done:
4856 0ebf8283 2019-07-24 stsp free(commit_ref_name);
4857 0ebf8283 2019-07-24 stsp return err;
4858 0ebf8283 2019-07-24 stsp }