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