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 int fd = -1;
67 99724ed4 2018-03-10 stsp
68 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
69 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
70 99724ed4 2018-03-10 stsp path = NULL;
71 99724ed4 2018-03-10 stsp goto done;
72 99724ed4 2018-03-10 stsp }
73 99724ed4 2018-03-10 stsp
74 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
75 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
76 99724ed4 2018-03-10 stsp if (fd == -1) {
77 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
78 99724ed4 2018-03-10 stsp goto done;
79 99724ed4 2018-03-10 stsp }
80 99724ed4 2018-03-10 stsp
81 99724ed4 2018-03-10 stsp if (content) {
82 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
83 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
84 638f9024 2019-05-13 stsp err = got_error_from_errno("dprintf");
85 99724ed4 2018-03-10 stsp goto done;
86 99724ed4 2018-03-10 stsp }
87 99724ed4 2018-03-10 stsp }
88 99724ed4 2018-03-10 stsp
89 99724ed4 2018-03-10 stsp done:
90 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
91 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
92 99724ed4 2018-03-10 stsp free(path);
93 507dc3bb 2018-12-29 stsp return err;
94 507dc3bb 2018-12-29 stsp }
95 507dc3bb 2018-12-29 stsp
96 507dc3bb 2018-12-29 stsp static const struct got_error *
97 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
98 507dc3bb 2018-12-29 stsp {
99 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
100 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
101 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
102 507dc3bb 2018-12-29 stsp char *path = NULL;
103 507dc3bb 2018-12-29 stsp
104 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
105 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
106 507dc3bb 2018-12-29 stsp path = NULL;
107 507dc3bb 2018-12-29 stsp goto done;
108 507dc3bb 2018-12-29 stsp }
109 507dc3bb 2018-12-29 stsp
110 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
111 507dc3bb 2018-12-29 stsp if (err)
112 507dc3bb 2018-12-29 stsp goto done;
113 507dc3bb 2018-12-29 stsp
114 507dc3bb 2018-12-29 stsp if (content) {
115 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
116 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
117 638f9024 2019-05-13 stsp err = got_error_from_errno2("fprintf", tmppath);
118 507dc3bb 2018-12-29 stsp goto done;
119 507dc3bb 2018-12-29 stsp }
120 507dc3bb 2018-12-29 stsp }
121 507dc3bb 2018-12-29 stsp
122 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
123 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, path);
124 2a57020b 2019-02-20 stsp unlink(tmppath);
125 507dc3bb 2018-12-29 stsp goto done;
126 507dc3bb 2018-12-29 stsp }
127 507dc3bb 2018-12-29 stsp
128 507dc3bb 2018-12-29 stsp done:
129 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
130 638f9024 2019-05-13 stsp err = got_error_from_errno2("fclose", tmppath);
131 230a42bd 2019-05-11 jcs free(tmppath);
132 99724ed4 2018-03-10 stsp return err;
133 99724ed4 2018-03-10 stsp }
134 99724ed4 2018-03-10 stsp
135 09fe317a 2018-03-11 stsp static const struct got_error *
136 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
137 09fe317a 2018-03-11 stsp {
138 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
139 09fe317a 2018-03-11 stsp char *path;
140 09fe317a 2018-03-11 stsp int fd = -1;
141 09fe317a 2018-03-11 stsp ssize_t n;
142 09fe317a 2018-03-11 stsp struct stat sb;
143 09fe317a 2018-03-11 stsp
144 09fe317a 2018-03-11 stsp *content = NULL;
145 09fe317a 2018-03-11 stsp
146 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
147 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
148 09fe317a 2018-03-11 stsp path = NULL;
149 09fe317a 2018-03-11 stsp goto done;
150 09fe317a 2018-03-11 stsp }
151 09fe317a 2018-03-11 stsp
152 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
153 09fe317a 2018-03-11 stsp if (fd == -1) {
154 f02eaa22 2019-03-11 stsp if (errno == ENOENT)
155 f02eaa22 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
156 f02eaa22 2019-03-11 stsp else
157 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", path);
158 ef99fdb1 2018-03-11 stsp goto done;
159 ef99fdb1 2018-03-11 stsp }
160 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
161 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
162 638f9024 2019-05-13 stsp : got_error_from_errno2("flock", path));
163 09fe317a 2018-03-11 stsp goto done;
164 09fe317a 2018-03-11 stsp }
165 09fe317a 2018-03-11 stsp
166 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
167 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", path);
168 d10c9b58 2019-02-19 stsp goto done;
169 d10c9b58 2019-02-19 stsp }
170 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
171 09fe317a 2018-03-11 stsp if (*content == NULL) {
172 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
173 09fe317a 2018-03-11 stsp goto done;
174 09fe317a 2018-03-11 stsp }
175 09fe317a 2018-03-11 stsp
176 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
177 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
178 638f9024 2019-05-13 stsp err = (n == -1 ? got_error_from_errno2("read", path) :
179 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
180 09fe317a 2018-03-11 stsp goto done;
181 09fe317a 2018-03-11 stsp }
182 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
183 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
184 09fe317a 2018-03-11 stsp goto done;
185 09fe317a 2018-03-11 stsp }
186 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
187 09fe317a 2018-03-11 stsp
188 09fe317a 2018-03-11 stsp done:
189 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
190 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path_got);
191 09fe317a 2018-03-11 stsp free(path);
192 09fe317a 2018-03-11 stsp if (err) {
193 09fe317a 2018-03-11 stsp free(*content);
194 09fe317a 2018-03-11 stsp *content = NULL;
195 09fe317a 2018-03-11 stsp }
196 09fe317a 2018-03-11 stsp return err;
197 09fe317a 2018-03-11 stsp }
198 09fe317a 2018-03-11 stsp
199 024e9686 2019-05-14 stsp static const struct got_error *
200 024e9686 2019-05-14 stsp write_head_ref(const char *path_got, struct got_reference *head_ref)
201 024e9686 2019-05-14 stsp {
202 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
203 024e9686 2019-05-14 stsp char *refstr = NULL;
204 024e9686 2019-05-14 stsp
205 024e9686 2019-05-14 stsp if (got_ref_is_symbolic(head_ref)) {
206 024e9686 2019-05-14 stsp refstr = got_ref_to_str(head_ref);
207 024e9686 2019-05-14 stsp if (refstr == NULL)
208 024e9686 2019-05-14 stsp return got_error_from_errno("got_ref_to_str");
209 024e9686 2019-05-14 stsp } else {
210 024e9686 2019-05-14 stsp refstr = strdup(got_ref_get_name(head_ref));
211 024e9686 2019-05-14 stsp if (refstr == NULL)
212 024e9686 2019-05-14 stsp return got_error_from_errno("strdup");
213 024e9686 2019-05-14 stsp }
214 024e9686 2019-05-14 stsp err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
215 024e9686 2019-05-14 stsp free(refstr);
216 024e9686 2019-05-14 stsp return err;
217 024e9686 2019-05-14 stsp }
218 024e9686 2019-05-14 stsp
219 86c3caaf 2018-03-09 stsp const struct got_error *
220 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
221 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
222 86c3caaf 2018-03-09 stsp {
223 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
224 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
225 ec22038e 2019-03-10 stsp uuid_t uuid;
226 ec22038e 2019-03-10 stsp uint32_t uuid_status;
227 65596e15 2018-12-24 stsp int obj_type;
228 7ac97322 2018-03-11 stsp char *path_got = NULL;
229 1451e70d 2018-03-10 stsp char *formatstr = NULL;
230 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
231 65596e15 2018-12-24 stsp char *basestr = NULL;
232 ec22038e 2019-03-10 stsp char *uuidstr = NULL;
233 65596e15 2018-12-24 stsp
234 0c48fee2 2019-03-11 stsp if (strcmp(path, got_repo_get_path(repo)) == 0) {
235 0c48fee2 2019-03-11 stsp err = got_error(GOT_ERR_WORKTREE_REPO);
236 0c48fee2 2019-03-11 stsp goto done;
237 0c48fee2 2019-03-11 stsp }
238 0c48fee2 2019-03-11 stsp
239 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
240 65596e15 2018-12-24 stsp if (err)
241 65596e15 2018-12-24 stsp return err;
242 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
243 65596e15 2018-12-24 stsp if (err)
244 65596e15 2018-12-24 stsp return err;
245 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
246 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
247 86c3caaf 2018-03-09 stsp
248 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
249 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
250 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
251 0bb8a95e 2018-03-12 stsp }
252 577ec78f 2018-03-11 stsp
253 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
254 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
255 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path);
256 86c3caaf 2018-03-09 stsp goto done;
257 86c3caaf 2018-03-09 stsp }
258 86c3caaf 2018-03-09 stsp
259 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
260 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
261 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
262 86c3caaf 2018-03-09 stsp goto done;
263 86c3caaf 2018-03-09 stsp }
264 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
265 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", path_got);
266 86c3caaf 2018-03-09 stsp goto done;
267 86c3caaf 2018-03-09 stsp }
268 86c3caaf 2018-03-09 stsp
269 056e7441 2018-03-11 stsp /* Create an empty lock file. */
270 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
271 056e7441 2018-03-11 stsp if (err)
272 056e7441 2018-03-11 stsp goto done;
273 056e7441 2018-03-11 stsp
274 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
275 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
276 99724ed4 2018-03-10 stsp if (err)
277 86c3caaf 2018-03-09 stsp goto done;
278 86c3caaf 2018-03-09 stsp
279 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
280 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
281 65596e15 2018-12-24 stsp if (err)
282 65596e15 2018-12-24 stsp goto done;
283 65596e15 2018-12-24 stsp
284 65596e15 2018-12-24 stsp /* Record our base commit. */
285 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
286 65596e15 2018-12-24 stsp if (err)
287 65596e15 2018-12-24 stsp goto done;
288 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
289 99724ed4 2018-03-10 stsp if (err)
290 86c3caaf 2018-03-09 stsp goto done;
291 86c3caaf 2018-03-09 stsp
292 1451e70d 2018-03-10 stsp /* Store path to repository. */
293 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
294 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
295 99724ed4 2018-03-10 stsp if (err)
296 86c3caaf 2018-03-09 stsp goto done;
297 86c3caaf 2018-03-09 stsp
298 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
299 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
300 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
301 ec22038e 2019-03-10 stsp if (err)
302 ec22038e 2019-03-10 stsp goto done;
303 ec22038e 2019-03-10 stsp
304 ec22038e 2019-03-10 stsp /* Generate UUID. */
305 ec22038e 2019-03-10 stsp uuid_create(&uuid, &uuid_status);
306 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
307 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
308 ec22038e 2019-03-10 stsp goto done;
309 ec22038e 2019-03-10 stsp }
310 ec22038e 2019-03-10 stsp uuid_to_string(&uuid, &uuidstr, &uuid_status);
311 ec22038e 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
312 ec22038e 2019-03-10 stsp err = got_error_uuid(uuid_status);
313 ec22038e 2019-03-10 stsp goto done;
314 ec22038e 2019-03-10 stsp }
315 ec22038e 2019-03-10 stsp err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
316 577ec78f 2018-03-11 stsp if (err)
317 577ec78f 2018-03-11 stsp goto done;
318 577ec78f 2018-03-11 stsp
319 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
320 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
321 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
322 1451e70d 2018-03-10 stsp goto done;
323 1451e70d 2018-03-10 stsp }
324 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
325 99724ed4 2018-03-10 stsp if (err)
326 1451e70d 2018-03-10 stsp goto done;
327 1451e70d 2018-03-10 stsp
328 86c3caaf 2018-03-09 stsp done:
329 65596e15 2018-12-24 stsp free(commit_id);
330 7ac97322 2018-03-11 stsp free(path_got);
331 1451e70d 2018-03-10 stsp free(formatstr);
332 0bb8a95e 2018-03-12 stsp free(absprefix);
333 65596e15 2018-12-24 stsp free(basestr);
334 ec22038e 2019-03-10 stsp free(uuidstr);
335 86c3caaf 2018-03-09 stsp return err;
336 86c3caaf 2018-03-09 stsp }
337 86c3caaf 2018-03-09 stsp
338 247140b2 2019-02-05 stsp static const struct got_error *
339 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
340 86c3caaf 2018-03-09 stsp {
341 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
342 7ac97322 2018-03-11 stsp char *path_got;
343 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
344 c442a90d 2019-03-10 stsp char *uuidstr = NULL;
345 056e7441 2018-03-11 stsp char *path_lock = NULL;
346 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
347 6d9d28c3 2018-03-11 stsp int version, fd = -1;
348 6d9d28c3 2018-03-11 stsp const char *errstr;
349 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
350 c442a90d 2019-03-10 stsp uint32_t uuid_status;
351 6d9d28c3 2018-03-11 stsp
352 6d9d28c3 2018-03-11 stsp *worktree = NULL;
353 6d9d28c3 2018-03-11 stsp
354 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
355 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
356 7ac97322 2018-03-11 stsp path_got = NULL;
357 6d9d28c3 2018-03-11 stsp goto done;
358 6d9d28c3 2018-03-11 stsp }
359 6d9d28c3 2018-03-11 stsp
360 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
361 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
362 056e7441 2018-03-11 stsp path_lock = NULL;
363 6d9d28c3 2018-03-11 stsp goto done;
364 6d9d28c3 2018-03-11 stsp }
365 6d9d28c3 2018-03-11 stsp
366 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
367 6d9d28c3 2018-03-11 stsp if (fd == -1) {
368 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
369 638f9024 2019-05-13 stsp : got_error_from_errno2("open", path_lock));
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp }
372 6d9d28c3 2018-03-11 stsp
373 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
374 6d9d28c3 2018-03-11 stsp if (err)
375 6d9d28c3 2018-03-11 stsp goto done;
376 6d9d28c3 2018-03-11 stsp
377 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
378 6d9d28c3 2018-03-11 stsp if (errstr) {
379 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
380 6d9d28c3 2018-03-11 stsp goto done;
381 6d9d28c3 2018-03-11 stsp }
382 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
383 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
384 6d9d28c3 2018-03-11 stsp goto done;
385 6d9d28c3 2018-03-11 stsp }
386 6d9d28c3 2018-03-11 stsp
387 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
388 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
389 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
390 6d9d28c3 2018-03-11 stsp goto done;
391 6d9d28c3 2018-03-11 stsp }
392 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
393 6d9d28c3 2018-03-11 stsp
394 0647c563 2019-03-11 stsp (*worktree)->root_path = strdup(path);
395 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
396 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
397 6d9d28c3 2018-03-11 stsp goto done;
398 6d9d28c3 2018-03-11 stsp }
399 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
400 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
401 6d9d28c3 2018-03-11 stsp if (err)
402 6d9d28c3 2018-03-11 stsp goto done;
403 eaccb85f 2018-12-25 stsp
404 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
405 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
406 93a30277 2018-12-24 stsp if (err)
407 93a30277 2018-12-24 stsp goto done;
408 93a30277 2018-12-24 stsp
409 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
410 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
411 c442a90d 2019-03-10 stsp if (err)
412 c442a90d 2019-03-10 stsp goto done;
413 c442a90d 2019-03-10 stsp
414 c442a90d 2019-03-10 stsp err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
415 eaccb85f 2018-12-25 stsp if (err)
416 c442a90d 2019-03-10 stsp goto done;
417 c442a90d 2019-03-10 stsp uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
418 c442a90d 2019-03-10 stsp if (uuid_status != uuid_s_ok) {
419 c442a90d 2019-03-10 stsp err = got_error_uuid(uuid_status);
420 eaccb85f 2018-12-25 stsp goto done;
421 c442a90d 2019-03-10 stsp }
422 eaccb85f 2018-12-25 stsp
423 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
424 eaccb85f 2018-12-25 stsp if (err)
425 eaccb85f 2018-12-25 stsp goto done;
426 eaccb85f 2018-12-25 stsp
427 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
428 eaccb85f 2018-12-25 stsp base_commit_id_str);
429 f5baf295 2018-03-11 stsp if (err)
430 6d9d28c3 2018-03-11 stsp goto done;
431 6d9d28c3 2018-03-11 stsp
432 36a38700 2019-05-10 stsp err = read_meta_file(&(*worktree)->head_ref_name, path_got,
433 36a38700 2019-05-10 stsp GOT_WORKTREE_HEAD_REF);
434 6d9d28c3 2018-03-11 stsp done:
435 eaccb85f 2018-12-25 stsp if (repo)
436 eaccb85f 2018-12-25 stsp got_repo_close(repo);
437 7ac97322 2018-03-11 stsp free(path_got);
438 056e7441 2018-03-11 stsp free(path_lock);
439 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
440 c442a90d 2019-03-10 stsp free(uuidstr);
441 bd165944 2019-03-10 stsp free(formatstr);
442 6d9d28c3 2018-03-11 stsp if (err) {
443 6d9d28c3 2018-03-11 stsp if (fd != -1)
444 6d9d28c3 2018-03-11 stsp close(fd);
445 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
446 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
447 6d9d28c3 2018-03-11 stsp *worktree = NULL;
448 6d9d28c3 2018-03-11 stsp } else
449 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
450 6d9d28c3 2018-03-11 stsp
451 6d9d28c3 2018-03-11 stsp return err;
452 86c3caaf 2018-03-09 stsp }
453 247140b2 2019-02-05 stsp
454 247140b2 2019-02-05 stsp const struct got_error *
455 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
456 247140b2 2019-02-05 stsp {
457 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
458 86c3caaf 2018-03-09 stsp
459 247140b2 2019-02-05 stsp do {
460 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
461 f02eaa22 2019-03-11 stsp if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
462 247140b2 2019-02-05 stsp return err;
463 247140b2 2019-02-05 stsp if (*worktree)
464 247140b2 2019-02-05 stsp return NULL;
465 247140b2 2019-02-05 stsp path = dirname(path);
466 d1542a27 2019-02-05 stsp if (path == NULL)
467 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
468 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
469 247140b2 2019-02-05 stsp
470 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
471 247140b2 2019-02-05 stsp }
472 247140b2 2019-02-05 stsp
473 3a6ce05a 2019-02-11 stsp const struct got_error *
474 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
475 86c3caaf 2018-03-09 stsp {
476 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
477 c88eb298 2018-03-11 stsp free(worktree->root_path);
478 cde76477 2018-03-11 stsp free(worktree->repo_path);
479 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
480 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
481 36a38700 2019-05-10 stsp free(worktree->head_ref_name);
482 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
483 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
484 638f9024 2019-05-13 stsp err = got_error_from_errno2("close",
485 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree));
486 6d9d28c3 2018-03-11 stsp free(worktree);
487 3a6ce05a 2019-02-11 stsp return err;
488 86c3caaf 2018-03-09 stsp }
489 86c3caaf 2018-03-09 stsp
490 2fbdb5ae 2018-12-29 stsp const char *
491 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
492 c7f4312f 2019-02-05 stsp {
493 c7f4312f 2019-02-05 stsp return worktree->root_path;
494 c7f4312f 2019-02-05 stsp }
495 c7f4312f 2019-02-05 stsp
496 c7f4312f 2019-02-05 stsp const char *
497 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
498 86c3caaf 2018-03-09 stsp {
499 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
500 49520a32 2018-12-29 stsp }
501 49520a32 2018-12-29 stsp
502 49520a32 2018-12-29 stsp const char *
503 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
504 49520a32 2018-12-29 stsp {
505 f609be2e 2018-12-29 stsp return worktree->path_prefix;
506 e5dc7198 2018-12-29 stsp }
507 e5dc7198 2018-12-29 stsp
508 e5dc7198 2018-12-29 stsp const struct got_error *
509 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
510 e5dc7198 2018-12-29 stsp const char *path_prefix)
511 e5dc7198 2018-12-29 stsp {
512 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
513 e5dc7198 2018-12-29 stsp
514 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
515 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
516 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
517 e5dc7198 2018-12-29 stsp }
518 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
519 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
520 e5dc7198 2018-12-29 stsp free(absprefix);
521 e5dc7198 2018-12-29 stsp return NULL;
522 86c3caaf 2018-03-09 stsp }
523 86c3caaf 2018-03-09 stsp
524 bc70eb79 2019-05-09 stsp const char *
525 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
526 86c3caaf 2018-03-09 stsp {
527 36a38700 2019-05-10 stsp return worktree->head_ref_name;
528 024e9686 2019-05-14 stsp }
529 024e9686 2019-05-14 stsp
530 024e9686 2019-05-14 stsp const struct got_error *
531 024e9686 2019-05-14 stsp got_worktree_set_head_ref(struct got_worktree *worktree,
532 024e9686 2019-05-14 stsp struct got_reference *head_ref)
533 024e9686 2019-05-14 stsp {
534 024e9686 2019-05-14 stsp const struct got_error *err = NULL;
535 024e9686 2019-05-14 stsp char *path_got = NULL, *head_ref_name = NULL;
536 024e9686 2019-05-14 stsp
537 024e9686 2019-05-14 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
538 024e9686 2019-05-14 stsp GOT_WORKTREE_GOT_DIR) == -1) {
539 024e9686 2019-05-14 stsp err = got_error_from_errno("asprintf");
540 024e9686 2019-05-14 stsp path_got = NULL;
541 024e9686 2019-05-14 stsp goto done;
542 024e9686 2019-05-14 stsp }
543 024e9686 2019-05-14 stsp
544 024e9686 2019-05-14 stsp head_ref_name = strdup(got_ref_get_name(head_ref));
545 024e9686 2019-05-14 stsp if (head_ref_name == NULL) {
546 024e9686 2019-05-14 stsp err = got_error_from_errno("strdup");
547 024e9686 2019-05-14 stsp goto done;
548 024e9686 2019-05-14 stsp }
549 024e9686 2019-05-14 stsp
550 024e9686 2019-05-14 stsp err = write_head_ref(path_got, head_ref);
551 024e9686 2019-05-14 stsp if (err)
552 024e9686 2019-05-14 stsp goto done;
553 024e9686 2019-05-14 stsp
554 024e9686 2019-05-14 stsp free(worktree->head_ref_name);
555 024e9686 2019-05-14 stsp worktree->head_ref_name = head_ref_name;
556 024e9686 2019-05-14 stsp done:
557 024e9686 2019-05-14 stsp free(path_got);
558 024e9686 2019-05-14 stsp if (err)
559 024e9686 2019-05-14 stsp free(head_ref_name);
560 024e9686 2019-05-14 stsp return err;
561 507dc3bb 2018-12-29 stsp }
562 507dc3bb 2018-12-29 stsp
563 b72f483a 2019-02-05 stsp struct got_object_id *
564 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
565 507dc3bb 2018-12-29 stsp {
566 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
567 86c3caaf 2018-03-09 stsp }
568 86c3caaf 2018-03-09 stsp
569 507dc3bb 2018-12-29 stsp const struct got_error *
570 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
571 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
572 507dc3bb 2018-12-29 stsp {
573 507dc3bb 2018-12-29 stsp const struct got_error *err;
574 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
575 507dc3bb 2018-12-29 stsp char *id_str = NULL;
576 507dc3bb 2018-12-29 stsp char *path_got = NULL;
577 507dc3bb 2018-12-29 stsp
578 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
579 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
580 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
581 507dc3bb 2018-12-29 stsp path_got = NULL;
582 507dc3bb 2018-12-29 stsp goto done;
583 507dc3bb 2018-12-29 stsp }
584 507dc3bb 2018-12-29 stsp
585 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
586 507dc3bb 2018-12-29 stsp if (err)
587 507dc3bb 2018-12-29 stsp return err;
588 507dc3bb 2018-12-29 stsp
589 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
590 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
591 507dc3bb 2018-12-29 stsp goto done;
592 507dc3bb 2018-12-29 stsp }
593 507dc3bb 2018-12-29 stsp
594 507dc3bb 2018-12-29 stsp /* Record our base commit. */
595 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
596 507dc3bb 2018-12-29 stsp if (err)
597 507dc3bb 2018-12-29 stsp goto done;
598 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
599 507dc3bb 2018-12-29 stsp if (err)
600 507dc3bb 2018-12-29 stsp goto done;
601 507dc3bb 2018-12-29 stsp
602 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
603 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
604 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
605 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
606 507dc3bb 2018-12-29 stsp goto done;
607 507dc3bb 2018-12-29 stsp }
608 507dc3bb 2018-12-29 stsp done:
609 507dc3bb 2018-12-29 stsp if (obj)
610 507dc3bb 2018-12-29 stsp got_object_close(obj);
611 507dc3bb 2018-12-29 stsp free(id_str);
612 507dc3bb 2018-12-29 stsp free(path_got);
613 507dc3bb 2018-12-29 stsp return err;
614 507dc3bb 2018-12-29 stsp }
615 507dc3bb 2018-12-29 stsp
616 9d31a1d8 2018-03-11 stsp static const struct got_error *
617 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
618 86c3caaf 2018-03-09 stsp {
619 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
620 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
621 638f9024 2019-05-13 stsp : got_error_from_errno2("flock",
622 230a42bd 2019-05-11 jcs got_worktree_get_root_path(worktree)));
623 86c3caaf 2018-03-09 stsp return NULL;
624 21908da4 2019-01-13 stsp }
625 21908da4 2019-01-13 stsp
626 21908da4 2019-01-13 stsp static const struct got_error *
627 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
628 4a1ddfc2 2019-01-12 stsp {
629 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
630 4a1ddfc2 2019-01-12 stsp char *abspath;
631 4a1ddfc2 2019-01-12 stsp
632 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
633 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
634 4a1ddfc2 2019-01-12 stsp
635 0cd1c46a 2019-03-11 stsp err = got_path_mkdir(abspath);
636 ddcd8544 2019-03-11 stsp if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
637 ddcd8544 2019-03-11 stsp struct stat sb;
638 ddcd8544 2019-03-11 stsp err = NULL;
639 ddcd8544 2019-03-11 stsp if (lstat(abspath, &sb) == -1) {
640 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
641 ddcd8544 2019-03-11 stsp } else if (!S_ISDIR(sb.st_mode)) {
642 ddcd8544 2019-03-11 stsp /* TODO directory is obstructed; do something */
643 ddcd8544 2019-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
644 ddcd8544 2019-03-11 stsp }
645 ddcd8544 2019-03-11 stsp }
646 4a1ddfc2 2019-01-12 stsp free(abspath);
647 68c76935 2019-02-19 stsp return err;
648 68c76935 2019-02-19 stsp }
649 68c76935 2019-02-19 stsp
650 68c76935 2019-02-19 stsp static const struct got_error *
651 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
652 68c76935 2019-02-19 stsp {
653 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
654 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
655 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
656 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
657 68c76935 2019-02-19 stsp
658 68c76935 2019-02-19 stsp *same = 1;
659 68c76935 2019-02-19 stsp
660 230a42bd 2019-05-11 jcs for (;;) {
661 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
662 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
663 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
664 68c76935 2019-02-19 stsp break;
665 68c76935 2019-02-19 stsp }
666 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
667 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
668 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
669 68c76935 2019-02-19 stsp break;
670 68c76935 2019-02-19 stsp }
671 68c76935 2019-02-19 stsp if (flen1 == 0) {
672 68c76935 2019-02-19 stsp if (flen2 != 0)
673 68c76935 2019-02-19 stsp *same = 0;
674 68c76935 2019-02-19 stsp break;
675 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
676 68c76935 2019-02-19 stsp if (flen1 != 0)
677 68c76935 2019-02-19 stsp *same = 0;
678 68c76935 2019-02-19 stsp break;
679 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
680 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
681 68c76935 2019-02-19 stsp *same = 0;
682 68c76935 2019-02-19 stsp break;
683 68c76935 2019-02-19 stsp }
684 68c76935 2019-02-19 stsp } else {
685 68c76935 2019-02-19 stsp *same = 0;
686 68c76935 2019-02-19 stsp break;
687 68c76935 2019-02-19 stsp }
688 68c76935 2019-02-19 stsp }
689 68c76935 2019-02-19 stsp
690 68c76935 2019-02-19 stsp return err;
691 68c76935 2019-02-19 stsp }
692 68c76935 2019-02-19 stsp
693 68c76935 2019-02-19 stsp static const struct got_error *
694 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
695 68c76935 2019-02-19 stsp {
696 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
697 68c76935 2019-02-19 stsp struct stat sb;
698 68c76935 2019-02-19 stsp size_t size1, size2;
699 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
700 68c76935 2019-02-19 stsp
701 68c76935 2019-02-19 stsp *same = 1;
702 68c76935 2019-02-19 stsp
703 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
704 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f1_path);
705 68c76935 2019-02-19 stsp goto done;
706 68c76935 2019-02-19 stsp }
707 68c76935 2019-02-19 stsp size1 = sb.st_size;
708 68c76935 2019-02-19 stsp
709 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
710 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", f2_path);
711 68c76935 2019-02-19 stsp goto done;
712 68c76935 2019-02-19 stsp }
713 68c76935 2019-02-19 stsp size2 = sb.st_size;
714 68c76935 2019-02-19 stsp
715 68c76935 2019-02-19 stsp if (size1 != size2) {
716 68c76935 2019-02-19 stsp *same = 0;
717 68c76935 2019-02-19 stsp return NULL;
718 68c76935 2019-02-19 stsp }
719 68c76935 2019-02-19 stsp
720 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
721 68c76935 2019-02-19 stsp if (f1 == NULL)
722 638f9024 2019-05-13 stsp return got_error_from_errno2("open", f1_path);
723 68c76935 2019-02-19 stsp
724 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
725 68c76935 2019-02-19 stsp if (f2 == NULL) {
726 638f9024 2019-05-13 stsp err = got_error_from_errno2("open", f2_path);
727 68c76935 2019-02-19 stsp goto done;
728 68c76935 2019-02-19 stsp }
729 68c76935 2019-02-19 stsp
730 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
731 68c76935 2019-02-19 stsp done:
732 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
733 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
734 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
735 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
736 68c76935 2019-02-19 stsp
737 6353ad76 2019-02-08 stsp return err;
738 6353ad76 2019-02-08 stsp }
739 6353ad76 2019-02-08 stsp
740 6353ad76 2019-02-08 stsp /*
741 46b6ee73 2019-06-04 stsp * Perform a 3-way merge where blob_orig acts as the common ancestor,
742 46b6ee73 2019-06-04 stsp * blob_deriv acts as the first derived version, and the file on disk
743 234035bc 2019-06-01 stsp * acts as the second derived version.
744 6353ad76 2019-02-08 stsp */
745 6353ad76 2019-02-08 stsp static const struct got_error *
746 234035bc 2019-06-01 stsp merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
747 46b6ee73 2019-06-04 stsp struct got_blob_object *blob_orig, const char *ondisk_path,
748 46b6ee73 2019-06-04 stsp const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
749 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
750 234035bc 2019-06-01 stsp void *progress_arg)
751 6353ad76 2019-02-08 stsp {
752 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
753 6353ad76 2019-02-08 stsp int merged_fd = -1;
754 46b6ee73 2019-06-04 stsp FILE *f_deriv = NULL, *f_orig = NULL;
755 46b6ee73 2019-06-04 stsp char *blob_deriv_path = NULL, *blob_orig_path = NULL;
756 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
757 6353ad76 2019-02-08 stsp char *id_str = NULL;
758 6353ad76 2019-02-08 stsp char *label1 = NULL;
759 234035bc 2019-06-01 stsp int overlapcnt = 0;
760 af54ae4a 2019-02-19 stsp char *parent;
761 6353ad76 2019-02-08 stsp
762 234035bc 2019-06-01 stsp *local_changes_subsumed = 0;
763 234035bc 2019-06-01 stsp
764 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
765 af54ae4a 2019-02-19 stsp if (parent == NULL)
766 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", ondisk_path);
767 af54ae4a 2019-02-19 stsp
768 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
769 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
770 af54ae4a 2019-02-19 stsp
771 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
772 6353ad76 2019-02-08 stsp if (err)
773 af54ae4a 2019-02-19 stsp goto done;
774 af54ae4a 2019-02-19 stsp
775 af54ae4a 2019-02-19 stsp free(base_path);
776 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
777 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
778 af54ae4a 2019-02-19 stsp base_path = NULL;
779 af54ae4a 2019-02-19 stsp goto done;
780 af54ae4a 2019-02-19 stsp }
781 af54ae4a 2019-02-19 stsp
782 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
783 6353ad76 2019-02-08 stsp if (err)
784 6353ad76 2019-02-08 stsp goto done;
785 46b6ee73 2019-06-04 stsp err = got_object_blob_dump_to_file(NULL, NULL, f_deriv, blob_deriv);
786 6353ad76 2019-02-08 stsp if (err)
787 6353ad76 2019-02-08 stsp goto done;
788 6353ad76 2019-02-08 stsp
789 af54ae4a 2019-02-19 stsp free(base_path);
790 46b6ee73 2019-06-04 stsp if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
791 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
792 af54ae4a 2019-02-19 stsp base_path = NULL;
793 af54ae4a 2019-02-19 stsp goto done;
794 af54ae4a 2019-02-19 stsp }
795 af54ae4a 2019-02-19 stsp
796 46b6ee73 2019-06-04 stsp err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
797 6353ad76 2019-02-08 stsp if (err)
798 6353ad76 2019-02-08 stsp goto done;
799 46b6ee73 2019-06-04 stsp if (blob_orig) {
800 46b6ee73 2019-06-04 stsp err = got_object_blob_dump_to_file(NULL, NULL, f_orig,
801 46b6ee73 2019-06-04 stsp blob_orig);
802 1430b4e0 2019-03-27 stsp if (err)
803 1430b4e0 2019-03-27 stsp goto done;
804 1430b4e0 2019-03-27 stsp } else {
805 1430b4e0 2019-03-27 stsp /*
806 1430b4e0 2019-03-27 stsp * If the file has no blob, this is an "add vs add" conflict,
807 1430b4e0 2019-03-27 stsp * and we simply use an empty ancestor file to make both files
808 1430b4e0 2019-03-27 stsp * appear in the merged result in their entirety.
809 1430b4e0 2019-03-27 stsp */
810 1430b4e0 2019-03-27 stsp }
811 6353ad76 2019-02-08 stsp
812 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
813 6353ad76 2019-02-08 stsp if (err)
814 6353ad76 2019-02-08 stsp goto done;
815 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
816 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
817 6353ad76 2019-02-08 stsp goto done;
818 6353ad76 2019-02-08 stsp }
819 6353ad76 2019-02-08 stsp
820 46b6ee73 2019-06-04 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
821 46b6ee73 2019-06-04 stsp blob_orig_path, ondisk_path, label1, path);
822 6353ad76 2019-02-08 stsp if (err)
823 6353ad76 2019-02-08 stsp goto done;
824 6353ad76 2019-02-08 stsp
825 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
826 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
827 6353ad76 2019-02-08 stsp
828 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
829 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
830 816dc654 2019-02-16 stsp goto done;
831 68c76935 2019-02-19 stsp }
832 68c76935 2019-02-19 stsp
833 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
834 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
835 46b6ee73 2019-06-04 stsp err = check_files_equal(local_changes_subsumed, blob_deriv_path,
836 68c76935 2019-02-19 stsp merged_path);
837 68c76935 2019-02-19 stsp if (err)
838 68c76935 2019-02-19 stsp goto done;
839 816dc654 2019-02-16 stsp }
840 6353ad76 2019-02-08 stsp
841 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
842 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", merged_path);
843 70a0c8ec 2019-02-20 stsp goto done;
844 70a0c8ec 2019-02-20 stsp }
845 70a0c8ec 2019-02-20 stsp
846 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
847 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", merged_path,
848 230a42bd 2019-05-11 jcs ondisk_path);
849 2a57020b 2019-02-20 stsp unlink(merged_path);
850 6353ad76 2019-02-08 stsp goto done;
851 6353ad76 2019-02-08 stsp }
852 6353ad76 2019-02-08 stsp
853 6353ad76 2019-02-08 stsp done:
854 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
855 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
856 46b6ee73 2019-06-04 stsp if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
857 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
858 46b6ee73 2019-06-04 stsp if (f_orig && fclose(f_orig) != 0 && err == NULL)
859 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
860 6353ad76 2019-02-08 stsp free(merged_path);
861 af54ae4a 2019-02-19 stsp free(base_path);
862 46b6ee73 2019-06-04 stsp if (blob_deriv_path) {
863 46b6ee73 2019-06-04 stsp unlink(blob_deriv_path);
864 46b6ee73 2019-06-04 stsp free(blob_deriv_path);
865 6353ad76 2019-02-08 stsp }
866 46b6ee73 2019-06-04 stsp if (blob_orig_path) {
867 46b6ee73 2019-06-04 stsp unlink(blob_orig_path);
868 46b6ee73 2019-06-04 stsp free(blob_orig_path);
869 6353ad76 2019-02-08 stsp }
870 6353ad76 2019-02-08 stsp free(id_str);
871 6353ad76 2019-02-08 stsp free(label1);
872 4a1ddfc2 2019-01-12 stsp return err;
873 4a1ddfc2 2019-01-12 stsp }
874 4a1ddfc2 2019-01-12 stsp
875 4a1ddfc2 2019-01-12 stsp static const struct got_error *
876 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
877 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
878 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
879 13d9040b 2019-03-27 stsp int update_timestamps)
880 13d9040b 2019-03-27 stsp {
881 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
882 13d9040b 2019-03-27 stsp
883 13d9040b 2019-03-27 stsp if (ie == NULL)
884 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
885 13d9040b 2019-03-27 stsp if (ie)
886 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
887 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
888 13d9040b 2019-03-27 stsp update_timestamps);
889 13d9040b 2019-03-27 stsp else {
890 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
891 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
892 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
893 13d9040b 2019-03-27 stsp if (!err)
894 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
895 13d9040b 2019-03-27 stsp }
896 13d9040b 2019-03-27 stsp return err;
897 13d9040b 2019-03-27 stsp }
898 13d9040b 2019-03-27 stsp
899 13d9040b 2019-03-27 stsp static const struct got_error *
900 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
901 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
902 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
903 a129376b 2019-03-28 stsp int reverting_versioned_file, struct got_repository *repo,
904 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
905 9d31a1d8 2018-03-11 stsp {
906 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
907 507dc3bb 2018-12-29 stsp int fd = -1;
908 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
909 507dc3bb 2018-12-29 stsp int update = 0;
910 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
911 9d31a1d8 2018-03-11 stsp
912 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
913 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
914 9d31a1d8 2018-03-11 stsp if (fd == -1) {
915 21908da4 2019-01-13 stsp if (errno == ENOENT) {
916 21908da4 2019-01-13 stsp char *parent = dirname(path);
917 21908da4 2019-01-13 stsp if (parent == NULL)
918 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
919 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
920 21908da4 2019-01-13 stsp if (err)
921 21908da4 2019-01-13 stsp return err;
922 21908da4 2019-01-13 stsp fd = open(ondisk_path,
923 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
924 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
925 21908da4 2019-01-13 stsp if (fd == -1)
926 638f9024 2019-05-13 stsp return got_error_from_errno2("open",
927 230a42bd 2019-05-11 jcs ondisk_path);
928 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
929 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
930 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
931 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
932 507dc3bb 2018-12-29 stsp goto done;
933 d70b8e30 2018-12-27 stsp } else {
934 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
935 507dc3bb 2018-12-29 stsp ondisk_path);
936 507dc3bb 2018-12-29 stsp if (err)
937 507dc3bb 2018-12-29 stsp goto done;
938 507dc3bb 2018-12-29 stsp update = 1;
939 9d31a1d8 2018-03-11 stsp }
940 507dc3bb 2018-12-29 stsp } else
941 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
942 9d31a1d8 2018-03-11 stsp }
943 9d31a1d8 2018-03-11 stsp
944 a378724f 2019-02-10 stsp if (restoring_missing_file)
945 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
946 a129376b 2019-03-28 stsp else if (reverting_versioned_file)
947 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
948 a378724f 2019-02-10 stsp else
949 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
950 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
951 d7b62c98 2018-12-27 stsp
952 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
953 9d31a1d8 2018-03-11 stsp do {
954 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
955 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
956 9d31a1d8 2018-03-11 stsp if (err)
957 9d31a1d8 2018-03-11 stsp break;
958 9d31a1d8 2018-03-11 stsp if (len > 0) {
959 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
960 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
961 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
962 638f9024 2019-05-13 stsp err = got_error_from_errno("write");
963 b87c6f83 2018-12-24 stsp goto done;
964 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
965 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
966 b87c6f83 2018-12-24 stsp goto done;
967 9d31a1d8 2018-03-11 stsp }
968 61d6eaa3 2018-12-24 stsp hdrlen = 0;
969 9d31a1d8 2018-03-11 stsp }
970 9d31a1d8 2018-03-11 stsp } while (len != 0);
971 9d31a1d8 2018-03-11 stsp
972 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
973 638f9024 2019-05-13 stsp err = got_error_from_errno("fsync");
974 816dc654 2019-02-16 stsp goto done;
975 816dc654 2019-02-16 stsp }
976 9d31a1d8 2018-03-11 stsp
977 507dc3bb 2018-12-29 stsp if (update) {
978 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
979 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath,
980 230a42bd 2019-05-11 jcs ondisk_path);
981 2a57020b 2019-02-20 stsp unlink(tmppath);
982 68ed9ba5 2019-02-10 stsp goto done;
983 68ed9ba5 2019-02-10 stsp }
984 68ed9ba5 2019-02-10 stsp }
985 ba8a0d4d 2019-02-10 stsp
986 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
987 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
988 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
989 507dc3bb 2018-12-29 stsp goto done;
990 507dc3bb 2018-12-29 stsp }
991 ba8a0d4d 2019-02-10 stsp } else {
992 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
993 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", ondisk_path);
994 68ed9ba5 2019-02-10 stsp goto done;
995 68ed9ba5 2019-02-10 stsp }
996 507dc3bb 2018-12-29 stsp }
997 507dc3bb 2018-12-29 stsp
998 9d31a1d8 2018-03-11 stsp done:
999 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
1000 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1001 507dc3bb 2018-12-29 stsp free(tmppath);
1002 6353ad76 2019-02-08 stsp return err;
1003 6353ad76 2019-02-08 stsp }
1004 6353ad76 2019-02-08 stsp
1005 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1006 6353ad76 2019-02-08 stsp static const struct got_error *
1007 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
1008 7154f6ce 2019-03-27 stsp {
1009 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
1010 7154f6ce 2019-03-27 stsp const char *markers[3] = {
1011 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
1012 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
1013 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
1014 7154f6ce 2019-03-27 stsp };
1015 7154f6ce 2019-03-27 stsp int i = 0;
1016 7154f6ce 2019-03-27 stsp char *line;
1017 7154f6ce 2019-03-27 stsp size_t len;
1018 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
1019 7154f6ce 2019-03-27 stsp
1020 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
1021 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
1022 7154f6ce 2019-03-27 stsp if (line == NULL) {
1023 7154f6ce 2019-03-27 stsp if (feof(f))
1024 7154f6ce 2019-03-27 stsp break;
1025 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
1026 7154f6ce 2019-03-27 stsp break;
1027 7154f6ce 2019-03-27 stsp }
1028 7154f6ce 2019-03-27 stsp
1029 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1030 19332e6d 2019-05-13 stsp if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1031 19332e6d 2019-05-13 stsp == 0)
1032 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
1033 7154f6ce 2019-03-27 stsp else
1034 7154f6ce 2019-03-27 stsp i++;
1035 7154f6ce 2019-03-27 stsp }
1036 7154f6ce 2019-03-27 stsp }
1037 7154f6ce 2019-03-27 stsp
1038 7154f6ce 2019-03-27 stsp return err;
1039 7154f6ce 2019-03-27 stsp }
1040 7154f6ce 2019-03-27 stsp
1041 7154f6ce 2019-03-27 stsp static const struct got_error *
1042 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1043 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1044 b8f41171 2019-02-10 stsp struct got_repository *repo)
1045 6353ad76 2019-02-08 stsp {
1046 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1047 6353ad76 2019-02-08 stsp struct got_object_id id;
1048 6353ad76 2019-02-08 stsp size_t hdrlen;
1049 6353ad76 2019-02-08 stsp FILE *f = NULL;
1050 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1051 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1052 6353ad76 2019-02-08 stsp size_t flen, blen;
1053 6353ad76 2019-02-08 stsp
1054 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1055 6353ad76 2019-02-08 stsp
1056 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1057 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1058 b8f41171 2019-02-10 stsp if (ie) {
1059 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1060 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1061 2ec1f75b 2019-03-26 stsp else
1062 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1063 b8f41171 2019-02-10 stsp sb->st_mode =
1064 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1065 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1066 b8f41171 2019-02-10 stsp } else
1067 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1068 a378724f 2019-02-10 stsp return NULL;
1069 a378724f 2019-02-10 stsp }
1070 638f9024 2019-05-13 stsp return got_error_from_errno2("lstat", abspath);
1071 a378724f 2019-02-10 stsp }
1072 6353ad76 2019-02-08 stsp
1073 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1074 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1075 6353ad76 2019-02-08 stsp return NULL;
1076 6353ad76 2019-02-08 stsp }
1077 6353ad76 2019-02-08 stsp
1078 b8f41171 2019-02-10 stsp if (ie == NULL)
1079 d00136be 2019-03-26 stsp return NULL;
1080 d00136be 2019-03-26 stsp
1081 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1082 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1083 2ec1f75b 2019-03-26 stsp return NULL;
1084 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1085 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1086 b8f41171 2019-02-10 stsp return NULL;
1087 d00136be 2019-03-26 stsp }
1088 b8f41171 2019-02-10 stsp
1089 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1090 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1091 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1092 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1093 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1094 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1095 6353ad76 2019-02-08 stsp return NULL;
1096 6353ad76 2019-02-08 stsp
1097 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1098 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1099 6353ad76 2019-02-08 stsp if (err)
1100 6353ad76 2019-02-08 stsp return err;
1101 6353ad76 2019-02-08 stsp
1102 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1103 6353ad76 2019-02-08 stsp if (f == NULL) {
1104 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1105 6353ad76 2019-02-08 stsp goto done;
1106 6353ad76 2019-02-08 stsp }
1107 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1108 656b1f76 2019-05-11 jcs for (;;) {
1109 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1110 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1111 6353ad76 2019-02-08 stsp if (err)
1112 7154f6ce 2019-03-27 stsp goto done;
1113 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1114 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1115 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1116 638f9024 2019-05-13 stsp err = got_error_from_errno("fread");
1117 7154f6ce 2019-03-27 stsp goto done;
1118 80c5c120 2019-02-19 stsp }
1119 6353ad76 2019-02-08 stsp if (blen == 0) {
1120 6353ad76 2019-02-08 stsp if (flen != 0)
1121 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1122 6353ad76 2019-02-08 stsp break;
1123 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1124 6353ad76 2019-02-08 stsp if (blen != 0)
1125 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1126 6353ad76 2019-02-08 stsp break;
1127 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1128 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1129 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1130 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1131 6353ad76 2019-02-08 stsp break;
1132 6353ad76 2019-02-08 stsp }
1133 6353ad76 2019-02-08 stsp } else {
1134 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1135 6353ad76 2019-02-08 stsp break;
1136 6353ad76 2019-02-08 stsp }
1137 6353ad76 2019-02-08 stsp hdrlen = 0;
1138 6353ad76 2019-02-08 stsp }
1139 7154f6ce 2019-03-27 stsp
1140 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1141 7154f6ce 2019-03-27 stsp rewind(f);
1142 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1143 7154f6ce 2019-03-27 stsp }
1144 6353ad76 2019-02-08 stsp done:
1145 6353ad76 2019-02-08 stsp if (blob)
1146 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1147 6353ad76 2019-02-08 stsp if (f)
1148 6353ad76 2019-02-08 stsp fclose(f);
1149 9d31a1d8 2018-03-11 stsp return err;
1150 9d31a1d8 2018-03-11 stsp }
1151 9d31a1d8 2018-03-11 stsp
1152 9d31a1d8 2018-03-11 stsp static const struct got_error *
1153 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1154 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1155 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1156 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1157 0584f854 2019-04-06 stsp void *progress_arg)
1158 9d31a1d8 2018-03-11 stsp {
1159 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1160 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1161 6353ad76 2019-02-08 stsp char *ondisk_path;
1162 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1163 b8f41171 2019-02-10 stsp struct stat sb;
1164 9d31a1d8 2018-03-11 stsp
1165 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1166 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1167 6353ad76 2019-02-08 stsp
1168 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1169 b8f41171 2019-02-10 stsp if (err)
1170 b8f41171 2019-02-10 stsp goto done;
1171 6353ad76 2019-02-08 stsp
1172 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1173 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1174 b8f41171 2019-02-10 stsp goto done;
1175 b8f41171 2019-02-10 stsp }
1176 b8f41171 2019-02-10 stsp
1177 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1178 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1179 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1180 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1181 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1182 b8f41171 2019-02-10 stsp path);
1183 6353ad76 2019-02-08 stsp goto done;
1184 a378724f 2019-02-10 stsp }
1185 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1186 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1187 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1188 b8f41171 2019-02-10 stsp goto done;
1189 9d31a1d8 2018-03-11 stsp }
1190 9d31a1d8 2018-03-11 stsp
1191 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1192 8da9e5f4 2019-01-12 stsp if (err)
1193 6353ad76 2019-02-08 stsp goto done;
1194 512f0d0e 2019-01-02 stsp
1195 234035bc 2019-06-01 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1196 234035bc 2019-06-01 stsp int update_timestamps;
1197 234035bc 2019-06-01 stsp struct got_blob_object *blob2 = NULL;
1198 234035bc 2019-06-01 stsp if (got_fileindex_entry_has_blob(ie)) {
1199 234035bc 2019-06-01 stsp struct got_object_id id2;
1200 234035bc 2019-06-01 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1201 234035bc 2019-06-01 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1202 234035bc 2019-06-01 stsp if (err)
1203 234035bc 2019-06-01 stsp goto done;
1204 234035bc 2019-06-01 stsp }
1205 234035bc 2019-06-01 stsp err = merge_blob(&update_timestamps, worktree, blob2,
1206 234035bc 2019-06-01 stsp ondisk_path, path, sb.st_mode, blob, repo,
1207 234035bc 2019-06-01 stsp progress_cb, progress_arg);
1208 234035bc 2019-06-01 stsp if (blob2)
1209 234035bc 2019-06-01 stsp got_object_blob_close(blob2);
1210 234035bc 2019-06-01 stsp /*
1211 234035bc 2019-06-01 stsp * Do not update timestamps of files with local changes.
1212 234035bc 2019-06-01 stsp * Otherwise, a future status walk would treat them as
1213 234035bc 2019-06-01 stsp * unmodified files again.
1214 234035bc 2019-06-01 stsp */
1215 234035bc 2019-06-01 stsp err = got_fileindex_entry_update(ie, ondisk_path,
1216 234035bc 2019-06-01 stsp blob->id.sha1, worktree->base_commit_id->sha1,
1217 234035bc 2019-06-01 stsp update_timestamps);
1218 234035bc 2019-06-01 stsp } else if (status == GOT_STATUS_DELETE) {
1219 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1220 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1221 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1222 13d9040b 2019-03-27 stsp if (err)
1223 13d9040b 2019-03-27 stsp goto done;
1224 13d9040b 2019-03-27 stsp } else {
1225 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1226 a129376b 2019-03-28 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1227 a129376b 2019-03-28 stsp repo, progress_cb, progress_arg);
1228 13d9040b 2019-03-27 stsp if (err)
1229 13d9040b 2019-03-27 stsp goto done;
1230 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1231 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1232 13d9040b 2019-03-27 stsp if (err)
1233 13d9040b 2019-03-27 stsp goto done;
1234 13d9040b 2019-03-27 stsp }
1235 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1236 6353ad76 2019-02-08 stsp done:
1237 6353ad76 2019-02-08 stsp free(ondisk_path);
1238 8da9e5f4 2019-01-12 stsp return err;
1239 90285c3b 2019-01-08 stsp }
1240 90285c3b 2019-01-08 stsp
1241 90285c3b 2019-01-08 stsp static const struct got_error *
1242 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1243 90285c3b 2019-01-08 stsp {
1244 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1245 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1246 90285c3b 2019-01-08 stsp
1247 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1248 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1249 90285c3b 2019-01-08 stsp
1250 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1251 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1252 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", ondisk_path);
1253 c97665ae 2019-01-13 stsp } else {
1254 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1255 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1256 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1257 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1258 638f9024 2019-05-13 stsp err = got_error_from_errno2("rmdir",
1259 230a42bd 2019-05-11 jcs parent);
1260 90285c3b 2019-01-08 stsp break;
1261 90285c3b 2019-01-08 stsp }
1262 90285c3b 2019-01-08 stsp parent = dirname(parent);
1263 90285c3b 2019-01-08 stsp }
1264 90285c3b 2019-01-08 stsp }
1265 90285c3b 2019-01-08 stsp free(ondisk_path);
1266 90285c3b 2019-01-08 stsp return err;
1267 512f0d0e 2019-01-02 stsp }
1268 512f0d0e 2019-01-02 stsp
1269 708d8e67 2019-03-27 stsp static const struct got_error *
1270 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1271 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie, struct got_repository *repo,
1272 0584f854 2019-04-06 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
1273 708d8e67 2019-03-27 stsp {
1274 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1275 708d8e67 2019-03-27 stsp unsigned char status;
1276 708d8e67 2019-03-27 stsp struct stat sb;
1277 708d8e67 2019-03-27 stsp char *ondisk_path;
1278 708d8e67 2019-03-27 stsp
1279 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1280 708d8e67 2019-03-27 stsp == -1)
1281 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1282 708d8e67 2019-03-27 stsp
1283 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1284 708d8e67 2019-03-27 stsp if (err)
1285 708d8e67 2019-03-27 stsp return err;
1286 708d8e67 2019-03-27 stsp
1287 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1288 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1289 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1290 fc6346c4 2019-03-27 stsp /*
1291 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1292 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1293 fc6346c4 2019-03-27 stsp */
1294 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1295 fc6346c4 2019-03-27 stsp 0);
1296 708d8e67 2019-03-27 stsp if (err)
1297 708d8e67 2019-03-27 stsp return err;
1298 fc6346c4 2019-03-27 stsp } else {
1299 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1300 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1301 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1302 fc6346c4 2019-03-27 stsp if (err)
1303 fc6346c4 2019-03-27 stsp return err;
1304 fc6346c4 2019-03-27 stsp }
1305 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1306 708d8e67 2019-03-27 stsp }
1307 708d8e67 2019-03-27 stsp
1308 fc6346c4 2019-03-27 stsp return err;
1309 708d8e67 2019-03-27 stsp }
1310 708d8e67 2019-03-27 stsp
1311 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1312 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1313 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1314 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1315 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1316 8da9e5f4 2019-01-12 stsp void *progress_arg;
1317 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1318 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1319 8da9e5f4 2019-01-12 stsp };
1320 8da9e5f4 2019-01-12 stsp
1321 512f0d0e 2019-01-02 stsp static const struct got_error *
1322 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1323 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1324 512f0d0e 2019-01-02 stsp {
1325 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1326 0584f854 2019-04-06 stsp
1327 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1328 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1329 512f0d0e 2019-01-02 stsp
1330 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1331 0584f854 2019-04-06 stsp ie->path, a->repo, a->progress_cb, a->progress_arg);
1332 8da9e5f4 2019-01-12 stsp }
1333 512f0d0e 2019-01-02 stsp
1334 8da9e5f4 2019-01-12 stsp static const struct got_error *
1335 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1336 8da9e5f4 2019-01-12 stsp {
1337 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1338 7a9df742 2019-01-08 stsp
1339 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1340 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1341 0584f854 2019-04-06 stsp
1342 234035bc 2019-06-01 stsp return delete_blob(a->worktree, a->fileindex, ie,
1343 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1344 9d31a1d8 2018-03-11 stsp }
1345 9d31a1d8 2018-03-11 stsp
1346 9d31a1d8 2018-03-11 stsp static const struct got_error *
1347 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1348 9d31a1d8 2018-03-11 stsp {
1349 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1350 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1351 8da9e5f4 2019-01-12 stsp char *path;
1352 9d31a1d8 2018-03-11 stsp
1353 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1354 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1355 0584f854 2019-04-06 stsp
1356 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1357 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1358 8da9e5f4 2019-01-12 stsp == -1)
1359 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1360 9d31a1d8 2018-03-11 stsp
1361 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1362 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1363 8da9e5f4 2019-01-12 stsp else
1364 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1365 0584f854 2019-04-06 stsp a->repo, a->progress_cb, a->progress_arg);
1366 9d31a1d8 2018-03-11 stsp
1367 8da9e5f4 2019-01-12 stsp free(path);
1368 0cd1c46a 2019-03-11 stsp return err;
1369 0cd1c46a 2019-03-11 stsp }
1370 0cd1c46a 2019-03-11 stsp
1371 517bab73 2019-03-11 stsp const struct got_error *
1372 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1373 0cd1c46a 2019-03-11 stsp {
1374 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1375 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1376 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1377 0cd1c46a 2019-03-11 stsp
1378 517bab73 2019-03-11 stsp *refname = NULL;
1379 517bab73 2019-03-11 stsp
1380 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1381 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1382 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1383 0cd1c46a 2019-03-11 stsp
1384 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1385 0647c563 2019-03-11 stsp == -1) {
1386 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1387 0647c563 2019-03-11 stsp *refname = NULL;
1388 0cd1c46a 2019-03-11 stsp }
1389 517bab73 2019-03-11 stsp free(uuidstr);
1390 517bab73 2019-03-11 stsp return err;
1391 517bab73 2019-03-11 stsp }
1392 517bab73 2019-03-11 stsp
1393 517bab73 2019-03-11 stsp /*
1394 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1395 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1396 517bab73 2019-03-11 stsp */
1397 517bab73 2019-03-11 stsp static const struct got_error *
1398 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1399 517bab73 2019-03-11 stsp {
1400 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1401 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1402 517bab73 2019-03-11 stsp char *refname;
1403 517bab73 2019-03-11 stsp
1404 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1405 517bab73 2019-03-11 stsp if (err)
1406 517bab73 2019-03-11 stsp return err;
1407 0cd1c46a 2019-03-11 stsp
1408 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1409 0cd1c46a 2019-03-11 stsp if (err)
1410 0cd1c46a 2019-03-11 stsp goto done;
1411 0cd1c46a 2019-03-11 stsp
1412 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1413 0cd1c46a 2019-03-11 stsp done:
1414 0cd1c46a 2019-03-11 stsp free(refname);
1415 0cd1c46a 2019-03-11 stsp if (ref)
1416 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1417 9d31a1d8 2018-03-11 stsp return err;
1418 9d31a1d8 2018-03-11 stsp }
1419 9d31a1d8 2018-03-11 stsp
1420 ebf99748 2019-05-09 stsp static const struct got_error *
1421 ebf99748 2019-05-09 stsp open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1422 ebf99748 2019-05-09 stsp struct got_worktree *worktree)
1423 ebf99748 2019-05-09 stsp {
1424 ebf99748 2019-05-09 stsp const struct got_error *err = NULL;
1425 ebf99748 2019-05-09 stsp FILE *index = NULL;
1426 0cd1c46a 2019-03-11 stsp
1427 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1428 ebf99748 2019-05-09 stsp *fileindex = got_fileindex_alloc();
1429 ebf99748 2019-05-09 stsp if (*fileindex == NULL)
1430 638f9024 2019-05-13 stsp return got_error_from_errno("got_fileindex_alloc");
1431 ebf99748 2019-05-09 stsp
1432 ebf99748 2019-05-09 stsp if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1433 ebf99748 2019-05-09 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1434 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1435 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1436 ebf99748 2019-05-09 stsp goto done;
1437 ebf99748 2019-05-09 stsp }
1438 ebf99748 2019-05-09 stsp
1439 ebf99748 2019-05-09 stsp index = fopen(*fileindex_path, "rb");
1440 ebf99748 2019-05-09 stsp if (index == NULL) {
1441 ebf99748 2019-05-09 stsp if (errno != ENOENT)
1442 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", *fileindex_path);
1443 ebf99748 2019-05-09 stsp } else {
1444 ebf99748 2019-05-09 stsp err = got_fileindex_read(*fileindex, index);
1445 ebf99748 2019-05-09 stsp if (fclose(index) != 0 && err == NULL)
1446 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1447 ebf99748 2019-05-09 stsp }
1448 ebf99748 2019-05-09 stsp done:
1449 ebf99748 2019-05-09 stsp if (err) {
1450 ebf99748 2019-05-09 stsp free(*fileindex_path);
1451 ebf99748 2019-05-09 stsp *fileindex_path = NULL;
1452 ebf99748 2019-05-09 stsp free(*fileindex);
1453 ebf99748 2019-05-09 stsp *fileindex = NULL;
1454 ebf99748 2019-05-09 stsp }
1455 ebf99748 2019-05-09 stsp return err;
1456 ebf99748 2019-05-09 stsp }
1457 c932eeeb 2019-05-22 stsp
1458 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg {
1459 c932eeeb 2019-05-22 stsp struct got_object_id *base_commit_id;
1460 c932eeeb 2019-05-22 stsp const char *path;
1461 c932eeeb 2019-05-22 stsp size_t path_len;
1462 c932eeeb 2019-05-22 stsp const char *entry_name;
1463 c932eeeb 2019-05-22 stsp };
1464 ebf99748 2019-05-09 stsp
1465 c932eeeb 2019-05-22 stsp /* Bump base commit ID of all files within an updated part of the work tree. */
1466 c932eeeb 2019-05-22 stsp static const struct got_error *
1467 c932eeeb 2019-05-22 stsp bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1468 c932eeeb 2019-05-22 stsp {
1469 c932eeeb 2019-05-22 stsp struct bump_base_commit_id_arg *a = arg;
1470 c932eeeb 2019-05-22 stsp
1471 c932eeeb 2019-05-22 stsp if (a->entry_name) {
1472 c932eeeb 2019-05-22 stsp if (strcmp(ie->path, a->path) != 0)
1473 c932eeeb 2019-05-22 stsp return NULL;
1474 c932eeeb 2019-05-22 stsp } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1475 c932eeeb 2019-05-22 stsp return NULL;
1476 c932eeeb 2019-05-22 stsp
1477 c932eeeb 2019-05-22 stsp memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1478 c932eeeb 2019-05-22 stsp return NULL;
1479 9c6338c4 2019-06-02 stsp }
1480 9c6338c4 2019-06-02 stsp
1481 9c6338c4 2019-06-02 stsp static const struct got_error *
1482 9c6338c4 2019-06-02 stsp sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1483 9c6338c4 2019-06-02 stsp {
1484 9c6338c4 2019-06-02 stsp const struct got_error *err = NULL;
1485 9c6338c4 2019-06-02 stsp char *new_fileindex_path = NULL;
1486 9c6338c4 2019-06-02 stsp FILE *new_index = NULL;
1487 9c6338c4 2019-06-02 stsp
1488 9c6338c4 2019-06-02 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1489 9c6338c4 2019-06-02 stsp fileindex_path);
1490 9c6338c4 2019-06-02 stsp if (err)
1491 9c6338c4 2019-06-02 stsp goto done;
1492 9c6338c4 2019-06-02 stsp
1493 9c6338c4 2019-06-02 stsp err = got_fileindex_write(fileindex, new_index);
1494 9c6338c4 2019-06-02 stsp if (err)
1495 9c6338c4 2019-06-02 stsp goto done;
1496 9c6338c4 2019-06-02 stsp
1497 9c6338c4 2019-06-02 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1498 9c6338c4 2019-06-02 stsp err = got_error_from_errno3("rename", new_fileindex_path,
1499 9c6338c4 2019-06-02 stsp fileindex_path);
1500 9c6338c4 2019-06-02 stsp unlink(new_fileindex_path);
1501 9c6338c4 2019-06-02 stsp }
1502 9c6338c4 2019-06-02 stsp done:
1503 9c6338c4 2019-06-02 stsp if (new_index)
1504 9c6338c4 2019-06-02 stsp fclose(new_index);
1505 9c6338c4 2019-06-02 stsp free(new_fileindex_path);
1506 9c6338c4 2019-06-02 stsp return err;
1507 c932eeeb 2019-05-22 stsp }
1508 c932eeeb 2019-05-22 stsp
1509 9d31a1d8 2018-03-11 stsp const struct got_error *
1510 c4cdcb68 2019-04-03 stsp got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1511 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1512 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1513 9d31a1d8 2018-03-11 stsp {
1514 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1515 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1516 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1517 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1518 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1519 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
1520 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1521 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1522 c4cdcb68 2019-04-03 stsp char *relpath = NULL, *entry_name = NULL;
1523 af12c6b9 2019-06-04 stsp struct bump_base_commit_id_arg bbc_arg;
1524 9d31a1d8 2018-03-11 stsp
1525 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1526 9d31a1d8 2018-03-11 stsp if (err)
1527 9d31a1d8 2018-03-11 stsp return err;
1528 93a30277 2018-12-24 stsp
1529 51514078 2018-12-25 stsp /*
1530 51514078 2018-12-25 stsp * Read the file index.
1531 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1532 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1533 51514078 2018-12-25 stsp */
1534 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1535 ebf99748 2019-05-09 stsp if (err)
1536 ebf99748 2019-05-09 stsp goto done;
1537 51514078 2018-12-25 stsp
1538 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1539 0cd1c46a 2019-03-11 stsp if (err)
1540 0cd1c46a 2019-03-11 stsp goto done;
1541 0cd1c46a 2019-03-11 stsp
1542 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1543 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1544 9d31a1d8 2018-03-11 stsp if (err)
1545 9d31a1d8 2018-03-11 stsp goto done;
1546 9d31a1d8 2018-03-11 stsp
1547 c4cdcb68 2019-04-03 stsp if (path[0]) {
1548 c4cdcb68 2019-04-03 stsp char *tree_path;
1549 c4cdcb68 2019-04-03 stsp int obj_type;
1550 c4cdcb68 2019-04-03 stsp relpath = strdup(path);
1551 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1552 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1553 c4cdcb68 2019-04-03 stsp goto done;
1554 c4cdcb68 2019-04-03 stsp }
1555 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1556 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1557 c4cdcb68 2019-04-03 stsp path) == -1) {
1558 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1559 c4cdcb68 2019-04-03 stsp goto done;
1560 c4cdcb68 2019-04-03 stsp }
1561 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1562 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1563 c4cdcb68 2019-04-03 stsp free(tree_path);
1564 c4cdcb68 2019-04-03 stsp if (err)
1565 c4cdcb68 2019-04-03 stsp goto done;
1566 c4cdcb68 2019-04-03 stsp err = got_object_get_type(&obj_type, repo, tree_id);
1567 c4cdcb68 2019-04-03 stsp if (err)
1568 c4cdcb68 2019-04-03 stsp goto done;
1569 c4cdcb68 2019-04-03 stsp if (obj_type == GOT_OBJ_TYPE_BLOB) {
1570 c4cdcb68 2019-04-03 stsp /* Split provided path into parent dir + entry name. */
1571 c4cdcb68 2019-04-03 stsp if (strchr(path, '/') == NULL) {
1572 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1573 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1574 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1575 c4cdcb68 2019-04-03 stsp goto done;
1576 c4cdcb68 2019-04-03 stsp }
1577 c4cdcb68 2019-04-03 stsp tree_path = strdup(worktree->path_prefix);
1578 c4cdcb68 2019-04-03 stsp if (tree_path == NULL) {
1579 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1580 c4cdcb68 2019-04-03 stsp goto done;
1581 c4cdcb68 2019-04-03 stsp }
1582 c4cdcb68 2019-04-03 stsp } else {
1583 c4cdcb68 2019-04-03 stsp err = got_path_dirname(&relpath, path);
1584 c4cdcb68 2019-04-03 stsp if (err)
1585 c4cdcb68 2019-04-03 stsp goto done;
1586 c4cdcb68 2019-04-03 stsp if (asprintf(&tree_path, "%s%s%s",
1587 c4cdcb68 2019-04-03 stsp worktree->path_prefix,
1588 c4cdcb68 2019-04-03 stsp got_path_is_root_dir(
1589 c4cdcb68 2019-04-03 stsp worktree->path_prefix) ? "" : "/",
1590 c4cdcb68 2019-04-03 stsp relpath) == -1) {
1591 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1592 c4cdcb68 2019-04-03 stsp goto done;
1593 c4cdcb68 2019-04-03 stsp }
1594 c4cdcb68 2019-04-03 stsp }
1595 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1596 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, tree_path);
1597 c4cdcb68 2019-04-03 stsp free(tree_path);
1598 c4cdcb68 2019-04-03 stsp if (err)
1599 c4cdcb68 2019-04-03 stsp goto done;
1600 c4cdcb68 2019-04-03 stsp entry_name = basename(path);
1601 c4cdcb68 2019-04-03 stsp if (entry_name == NULL) {
1602 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", path);
1603 c4cdcb68 2019-04-03 stsp goto done;
1604 c4cdcb68 2019-04-03 stsp }
1605 c4cdcb68 2019-04-03 stsp }
1606 c4cdcb68 2019-04-03 stsp } else {
1607 c4cdcb68 2019-04-03 stsp relpath = strdup("");
1608 c4cdcb68 2019-04-03 stsp if (relpath == NULL) {
1609 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1610 c4cdcb68 2019-04-03 stsp goto done;
1611 c4cdcb68 2019-04-03 stsp }
1612 c4cdcb68 2019-04-03 stsp err = got_object_id_by_path(&tree_id, repo,
1613 c4cdcb68 2019-04-03 stsp worktree->base_commit_id, worktree->path_prefix);
1614 c4cdcb68 2019-04-03 stsp if (err)
1615 c4cdcb68 2019-04-03 stsp goto done;
1616 c4cdcb68 2019-04-03 stsp }
1617 9d31a1d8 2018-03-11 stsp
1618 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1619 8da9e5f4 2019-01-12 stsp if (err)
1620 c4cdcb68 2019-04-03 stsp goto done;
1621 c4cdcb68 2019-04-03 stsp
1622 c4cdcb68 2019-04-03 stsp if (entry_name &&
1623 c4cdcb68 2019-04-03 stsp got_object_tree_find_entry(tree, entry_name) == NULL) {
1624 c4cdcb68 2019-04-03 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1625 8da9e5f4 2019-01-12 stsp goto done;
1626 c4cdcb68 2019-04-03 stsp }
1627 9d31a1d8 2018-03-11 stsp
1628 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1629 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1630 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1631 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1632 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1633 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1634 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1635 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1636 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1637 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1638 af12c6b9 2019-06-04 stsp err = got_fileindex_diff_tree(fileindex, tree, relpath,
1639 c4cdcb68 2019-04-03 stsp entry_name, repo, &diff_cb, &arg);
1640 af12c6b9 2019-06-04 stsp if (err)
1641 af12c6b9 2019-06-04 stsp goto sync;
1642 8da9e5f4 2019-01-12 stsp
1643 af12c6b9 2019-06-04 stsp bbc_arg.base_commit_id = worktree->base_commit_id;
1644 af12c6b9 2019-06-04 stsp bbc_arg.entry_name = entry_name;
1645 af12c6b9 2019-06-04 stsp bbc_arg.path = path;
1646 af12c6b9 2019-06-04 stsp bbc_arg.path_len = strlen(path);
1647 af12c6b9 2019-06-04 stsp err = got_fileindex_for_each_entry_safe(fileindex,
1648 af12c6b9 2019-06-04 stsp bump_base_commit_id, &bbc_arg);
1649 af12c6b9 2019-06-04 stsp sync:
1650 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1651 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1652 af12c6b9 2019-06-04 stsp err = sync_err;
1653 9d31a1d8 2018-03-11 stsp done:
1654 9c6338c4 2019-06-02 stsp free(fileindex_path);
1655 c4cdcb68 2019-04-03 stsp free(relpath);
1656 edfa7d7f 2018-09-11 stsp if (tree)
1657 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1658 9d31a1d8 2018-03-11 stsp if (commit)
1659 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1660 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1661 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1662 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1663 9d31a1d8 2018-03-11 stsp err = unlockerr;
1664 f8d1f275 2019-02-04 stsp return err;
1665 f8d1f275 2019-02-04 stsp }
1666 f8d1f275 2019-02-04 stsp
1667 234035bc 2019-06-01 stsp struct merge_file_cb_arg {
1668 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1669 234035bc 2019-06-01 stsp struct got_fileindex *fileindex;
1670 234035bc 2019-06-01 stsp got_worktree_checkout_cb progress_cb;
1671 234035bc 2019-06-01 stsp void *progress_arg;
1672 234035bc 2019-06-01 stsp got_worktree_cancel_cb cancel_cb;
1673 234035bc 2019-06-01 stsp void *cancel_arg;
1674 234035bc 2019-06-01 stsp };
1675 234035bc 2019-06-01 stsp
1676 234035bc 2019-06-01 stsp static const struct got_error *
1677 234035bc 2019-06-01 stsp merge_file_cb(void *arg, struct got_blob_object *blob1,
1678 234035bc 2019-06-01 stsp struct got_blob_object *blob2, struct got_object_id *id1,
1679 234035bc 2019-06-01 stsp struct got_object_id *id2, const char *path1, const char *path2,
1680 234035bc 2019-06-01 stsp struct got_repository *repo)
1681 234035bc 2019-06-01 stsp {
1682 234035bc 2019-06-01 stsp static const struct got_error *err = NULL;
1683 234035bc 2019-06-01 stsp struct merge_file_cb_arg *a = arg;
1684 234035bc 2019-06-01 stsp struct got_fileindex_entry *ie;
1685 234035bc 2019-06-01 stsp char *ondisk_path = NULL;
1686 234035bc 2019-06-01 stsp struct stat sb;
1687 234035bc 2019-06-01 stsp unsigned char status;
1688 234035bc 2019-06-01 stsp int local_changes_subsumed;
1689 234035bc 2019-06-01 stsp
1690 234035bc 2019-06-01 stsp if (blob1 && blob2) {
1691 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1692 234035bc 2019-06-01 stsp if (ie == NULL) {
1693 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1694 234035bc 2019-06-01 stsp path2);
1695 234035bc 2019-06-01 stsp return NULL;
1696 234035bc 2019-06-01 stsp }
1697 234035bc 2019-06-01 stsp
1698 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1699 234035bc 2019-06-01 stsp path2) == -1)
1700 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1701 234035bc 2019-06-01 stsp
1702 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1703 234035bc 2019-06-01 stsp if (err)
1704 234035bc 2019-06-01 stsp goto done;
1705 234035bc 2019-06-01 stsp
1706 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1707 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1708 234035bc 2019-06-01 stsp path2);
1709 234035bc 2019-06-01 stsp goto done;
1710 234035bc 2019-06-01 stsp }
1711 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1712 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1713 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1714 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1715 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status, path2);
1716 234035bc 2019-06-01 stsp goto done;
1717 234035bc 2019-06-01 stsp }
1718 234035bc 2019-06-01 stsp
1719 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1720 234035bc 2019-06-01 stsp ondisk_path, path2, sb.st_mode, blob2, repo,
1721 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1722 234035bc 2019-06-01 stsp } else if (blob1) {
1723 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path1);
1724 234035bc 2019-06-01 stsp if (ie == NULL) {
1725 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1726 234035bc 2019-06-01 stsp path2);
1727 234035bc 2019-06-01 stsp return NULL;
1728 234035bc 2019-06-01 stsp }
1729 2b92fad7 2019-06-02 stsp
1730 2b92fad7 2019-06-02 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1731 2b92fad7 2019-06-02 stsp path1) == -1)
1732 2b92fad7 2019-06-02 stsp return got_error_from_errno("asprintf");
1733 2b92fad7 2019-06-02 stsp
1734 2b92fad7 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1735 2b92fad7 2019-06-02 stsp if (err)
1736 2b92fad7 2019-06-02 stsp goto done;
1737 2b92fad7 2019-06-02 stsp
1738 2b92fad7 2019-06-02 stsp switch (status) {
1739 2b92fad7 2019-06-02 stsp case GOT_STATUS_NO_CHANGE:
1740 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1741 2b92fad7 2019-06-02 stsp path1);
1742 2b92fad7 2019-06-02 stsp err = remove_ondisk_file(a->worktree->root_path, path1);
1743 2b92fad7 2019-06-02 stsp if (err)
1744 2b92fad7 2019-06-02 stsp goto done;
1745 2b92fad7 2019-06-02 stsp if (ie)
1746 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1747 2b92fad7 2019-06-02 stsp break;
1748 2b92fad7 2019-06-02 stsp case GOT_STATUS_DELETE:
1749 2b92fad7 2019-06-02 stsp case GOT_STATUS_MISSING:
1750 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1751 2b92fad7 2019-06-02 stsp path1);
1752 2b92fad7 2019-06-02 stsp if (ie)
1753 2b92fad7 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1754 2b92fad7 2019-06-02 stsp break;
1755 2b92fad7 2019-06-02 stsp case GOT_STATUS_ADD:
1756 2b92fad7 2019-06-02 stsp case GOT_STATUS_MODIFY:
1757 2b92fad7 2019-06-02 stsp case GOT_STATUS_CONFLICT:
1758 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg,
1759 2b92fad7 2019-06-02 stsp GOT_STATUS_CANNOT_DELETE, path1);
1760 2b92fad7 2019-06-02 stsp break;
1761 2b92fad7 2019-06-02 stsp case GOT_STATUS_OBSTRUCTED:
1762 2b92fad7 2019-06-02 stsp (*a->progress_cb)(a->progress_arg, status, path1);
1763 2b92fad7 2019-06-02 stsp break;
1764 2b92fad7 2019-06-02 stsp default:
1765 2b92fad7 2019-06-02 stsp break;
1766 2b92fad7 2019-06-02 stsp }
1767 234035bc 2019-06-01 stsp } else if (blob2) {
1768 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1769 234035bc 2019-06-01 stsp path2) == -1)
1770 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1771 234035bc 2019-06-01 stsp ie = got_fileindex_entry_get(a->fileindex, path2);
1772 234035bc 2019-06-01 stsp if (ie) {
1773 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path,
1774 234035bc 2019-06-01 stsp repo);
1775 234035bc 2019-06-01 stsp if (err)
1776 234035bc 2019-06-01 stsp goto done;
1777 234035bc 2019-06-01 stsp if (status != GOT_STATUS_NO_CHANGE &&
1778 234035bc 2019-06-01 stsp status != GOT_STATUS_MODIFY &&
1779 234035bc 2019-06-01 stsp status != GOT_STATUS_CONFLICT &&
1780 234035bc 2019-06-01 stsp status != GOT_STATUS_ADD) {
1781 234035bc 2019-06-01 stsp (*a->progress_cb)(a->progress_arg, status,
1782 234035bc 2019-06-01 stsp path2);
1783 234035bc 2019-06-01 stsp goto done;
1784 234035bc 2019-06-01 stsp }
1785 234035bc 2019-06-01 stsp err = merge_blob(&local_changes_subsumed, a->worktree,
1786 234035bc 2019-06-01 stsp NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1787 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1788 234035bc 2019-06-01 stsp if (status == GOT_STATUS_DELETE) {
1789 234035bc 2019-06-01 stsp err = update_blob_fileindex_entry(a->worktree,
1790 234035bc 2019-06-01 stsp a->fileindex, ie, ondisk_path, ie->path,
1791 234035bc 2019-06-01 stsp blob2, 0);
1792 234035bc 2019-06-01 stsp if (err)
1793 234035bc 2019-06-01 stsp goto done;
1794 234035bc 2019-06-01 stsp }
1795 234035bc 2019-06-01 stsp } else {
1796 234035bc 2019-06-01 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
1797 234035bc 2019-06-01 stsp err = install_blob(a->worktree, ondisk_path, path2,
1798 234035bc 2019-06-01 stsp /* XXX get this from parent tree! */
1799 234035bc 2019-06-01 stsp GOT_DEFAULT_FILE_MODE,
1800 234035bc 2019-06-01 stsp sb.st_mode, blob2, 0, 0, repo,
1801 234035bc 2019-06-01 stsp a->progress_cb, a->progress_arg);
1802 234035bc 2019-06-01 stsp if (err)
1803 234035bc 2019-06-01 stsp goto done;
1804 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie,
1805 2b92fad7 2019-06-02 stsp ondisk_path, path2, NULL, NULL);
1806 234035bc 2019-06-01 stsp if (err)
1807 234035bc 2019-06-01 stsp goto done;
1808 2b92fad7 2019-06-02 stsp err = got_fileindex_entry_add(a->fileindex, ie);
1809 2b92fad7 2019-06-02 stsp if (err) {
1810 2b92fad7 2019-06-02 stsp got_fileindex_entry_free(ie);
1811 2b92fad7 2019-06-02 stsp goto done;
1812 2b92fad7 2019-06-02 stsp }
1813 234035bc 2019-06-01 stsp }
1814 234035bc 2019-06-01 stsp }
1815 234035bc 2019-06-01 stsp done:
1816 234035bc 2019-06-01 stsp free(ondisk_path);
1817 234035bc 2019-06-01 stsp return err;
1818 234035bc 2019-06-01 stsp }
1819 234035bc 2019-06-01 stsp
1820 234035bc 2019-06-01 stsp struct check_merge_ok_arg {
1821 234035bc 2019-06-01 stsp struct got_worktree *worktree;
1822 234035bc 2019-06-01 stsp struct got_repository *repo;
1823 234035bc 2019-06-01 stsp };
1824 234035bc 2019-06-01 stsp
1825 234035bc 2019-06-01 stsp static const struct got_error *
1826 234035bc 2019-06-01 stsp check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1827 234035bc 2019-06-01 stsp {
1828 234035bc 2019-06-01 stsp const struct got_error *err = NULL;
1829 234035bc 2019-06-01 stsp struct check_merge_ok_arg *a = arg;
1830 234035bc 2019-06-01 stsp unsigned char status;
1831 234035bc 2019-06-01 stsp struct stat sb;
1832 234035bc 2019-06-01 stsp char *ondisk_path;
1833 234035bc 2019-06-01 stsp
1834 234035bc 2019-06-01 stsp /* Reject merges into a work tree with mixed base commits. */
1835 234035bc 2019-06-01 stsp if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1836 234035bc 2019-06-01 stsp SHA1_DIGEST_LENGTH))
1837 234035bc 2019-06-01 stsp return got_error(GOT_ERR_MIXED_COMMITS);
1838 234035bc 2019-06-01 stsp
1839 234035bc 2019-06-01 stsp if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1840 234035bc 2019-06-01 stsp == -1)
1841 234035bc 2019-06-01 stsp return got_error_from_errno("asprintf");
1842 234035bc 2019-06-01 stsp
1843 234035bc 2019-06-01 stsp /* Reject merges into a work tree with conflicted files. */
1844 234035bc 2019-06-01 stsp err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1845 234035bc 2019-06-01 stsp if (err)
1846 234035bc 2019-06-01 stsp return err;
1847 234035bc 2019-06-01 stsp if (status == GOT_STATUS_CONFLICT)
1848 234035bc 2019-06-01 stsp return got_error(GOT_ERR_CONFLICTS);
1849 234035bc 2019-06-01 stsp
1850 234035bc 2019-06-01 stsp return NULL;
1851 234035bc 2019-06-01 stsp }
1852 234035bc 2019-06-01 stsp
1853 234035bc 2019-06-01 stsp const struct got_error *
1854 234035bc 2019-06-01 stsp got_worktree_merge_files(struct got_worktree *worktree,
1855 234035bc 2019-06-01 stsp struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1856 234035bc 2019-06-01 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1857 234035bc 2019-06-01 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1858 234035bc 2019-06-01 stsp {
1859 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
1860 234035bc 2019-06-01 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1861 234035bc 2019-06-01 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1862 234035bc 2019-06-01 stsp struct merge_file_cb_arg arg;
1863 234035bc 2019-06-01 stsp char *fileindex_path = NULL;
1864 234035bc 2019-06-01 stsp struct got_fileindex *fileindex = NULL;
1865 234035bc 2019-06-01 stsp struct check_merge_ok_arg mok_arg;
1866 234035bc 2019-06-01 stsp
1867 234035bc 2019-06-01 stsp err = lock_worktree(worktree, LOCK_EX);
1868 234035bc 2019-06-01 stsp if (err)
1869 234035bc 2019-06-01 stsp return err;
1870 234035bc 2019-06-01 stsp
1871 234035bc 2019-06-01 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
1872 234035bc 2019-06-01 stsp if (err)
1873 234035bc 2019-06-01 stsp goto done;
1874 234035bc 2019-06-01 stsp
1875 234035bc 2019-06-01 stsp mok_arg.worktree = worktree;
1876 234035bc 2019-06-01 stsp mok_arg.repo = repo;
1877 234035bc 2019-06-01 stsp err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1878 234035bc 2019-06-01 stsp &mok_arg);
1879 234035bc 2019-06-01 stsp if (err)
1880 234035bc 2019-06-01 stsp goto done;
1881 234035bc 2019-06-01 stsp
1882 03415a1a 2019-06-02 stsp if (commit_id1) {
1883 03415a1a 2019-06-02 stsp err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1884 03415a1a 2019-06-02 stsp worktree->path_prefix);
1885 03415a1a 2019-06-02 stsp if (err)
1886 03415a1a 2019-06-02 stsp goto done;
1887 03415a1a 2019-06-02 stsp
1888 03415a1a 2019-06-02 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1889 03415a1a 2019-06-02 stsp if (err)
1890 03415a1a 2019-06-02 stsp goto done;
1891 03415a1a 2019-06-02 stsp }
1892 234035bc 2019-06-01 stsp
1893 234035bc 2019-06-01 stsp err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1894 234035bc 2019-06-01 stsp worktree->path_prefix);
1895 234035bc 2019-06-01 stsp if (err)
1896 234035bc 2019-06-01 stsp goto done;
1897 234035bc 2019-06-01 stsp
1898 234035bc 2019-06-01 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1899 234035bc 2019-06-01 stsp if (err)
1900 234035bc 2019-06-01 stsp goto done;
1901 234035bc 2019-06-01 stsp
1902 234035bc 2019-06-01 stsp arg.worktree = worktree;
1903 234035bc 2019-06-01 stsp arg.fileindex = fileindex;
1904 234035bc 2019-06-01 stsp arg.progress_cb = progress_cb;
1905 234035bc 2019-06-01 stsp arg.progress_arg = progress_arg;
1906 234035bc 2019-06-01 stsp arg.cancel_cb = cancel_cb;
1907 234035bc 2019-06-01 stsp arg.cancel_arg = cancel_arg;
1908 234035bc 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1909 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
1910 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
1911 af12c6b9 2019-06-04 stsp err = sync_err;
1912 234035bc 2019-06-01 stsp done:
1913 234035bc 2019-06-01 stsp got_fileindex_free(fileindex);
1914 234035bc 2019-06-01 stsp if (tree1)
1915 234035bc 2019-06-01 stsp got_object_tree_close(tree1);
1916 234035bc 2019-06-01 stsp if (tree2)
1917 234035bc 2019-06-01 stsp got_object_tree_close(tree2);
1918 234035bc 2019-06-01 stsp
1919 234035bc 2019-06-01 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1920 234035bc 2019-06-01 stsp if (unlockerr && err == NULL)
1921 234035bc 2019-06-01 stsp err = unlockerr;
1922 234035bc 2019-06-01 stsp return err;
1923 234035bc 2019-06-01 stsp }
1924 234035bc 2019-06-01 stsp
1925 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1926 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1927 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1928 927df6b7 2019-02-10 stsp const char *status_path;
1929 927df6b7 2019-02-10 stsp size_t status_path_len;
1930 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1931 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1932 f8d1f275 2019-02-04 stsp void *status_arg;
1933 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1934 f8d1f275 2019-02-04 stsp void *cancel_arg;
1935 f8d1f275 2019-02-04 stsp };
1936 f8d1f275 2019-02-04 stsp
1937 f8d1f275 2019-02-04 stsp static const struct got_error *
1938 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1939 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1940 927df6b7 2019-02-10 stsp struct got_repository *repo)
1941 927df6b7 2019-02-10 stsp {
1942 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1943 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1944 927df6b7 2019-02-10 stsp struct stat sb;
1945 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1946 927df6b7 2019-02-10 stsp
1947 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1948 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1949 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1950 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1951 016a88dd 2019-05-13 stsp err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1952 016a88dd 2019-05-13 stsp &commit_id);
1953 927df6b7 2019-02-10 stsp }
1954 927df6b7 2019-02-10 stsp return err;
1955 927df6b7 2019-02-10 stsp }
1956 927df6b7 2019-02-10 stsp
1957 927df6b7 2019-02-10 stsp static const struct got_error *
1958 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1959 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1960 f8d1f275 2019-02-04 stsp {
1961 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1962 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1963 f8d1f275 2019-02-04 stsp char *abspath;
1964 f8d1f275 2019-02-04 stsp
1965 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1966 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1967 0584f854 2019-04-06 stsp
1968 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1969 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1970 927df6b7 2019-02-10 stsp return NULL;
1971 927df6b7 2019-02-10 stsp
1972 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1973 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1974 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1975 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1976 f8d1f275 2019-02-04 stsp } else {
1977 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1978 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1979 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
1980 f8d1f275 2019-02-04 stsp }
1981 f8d1f275 2019-02-04 stsp
1982 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1983 927df6b7 2019-02-10 stsp a->repo);
1984 f8d1f275 2019-02-04 stsp free(abspath);
1985 9d31a1d8 2018-03-11 stsp return err;
1986 9d31a1d8 2018-03-11 stsp }
1987 f8d1f275 2019-02-04 stsp
1988 f8d1f275 2019-02-04 stsp static const struct got_error *
1989 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1990 f8d1f275 2019-02-04 stsp {
1991 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1992 016a88dd 2019-05-13 stsp struct got_object_id blob_id, commit_id;
1993 2ec1f75b 2019-03-26 stsp unsigned char status;
1994 927df6b7 2019-02-10 stsp
1995 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1996 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
1997 0584f854 2019-04-06 stsp
1998 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1999 927df6b7 2019-02-10 stsp return NULL;
2000 927df6b7 2019-02-10 stsp
2001 016a88dd 2019-05-13 stsp memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2002 016a88dd 2019-05-13 stsp memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2003 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
2004 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
2005 2ec1f75b 2019-03-26 stsp else
2006 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
2007 016a88dd 2019-05-13 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2008 016a88dd 2019-05-13 stsp &commit_id);
2009 f8d1f275 2019-02-04 stsp }
2010 f8d1f275 2019-02-04 stsp
2011 f8d1f275 2019-02-04 stsp static const struct got_error *
2012 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
2013 f8d1f275 2019-02-04 stsp {
2014 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2015 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
2016 f8d1f275 2019-02-04 stsp char *path = NULL;
2017 f8d1f275 2019-02-04 stsp
2018 0584f854 2019-04-06 stsp if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2019 0584f854 2019-04-06 stsp return got_error(GOT_ERR_CANCELLED);
2020 0584f854 2019-04-06 stsp
2021 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
2022 2c201a36 2019-02-10 stsp return NULL;
2023 2c201a36 2019-02-10 stsp
2024 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
2025 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
2026 f8d1f275 2019-02-04 stsp return NULL;
2027 f8d1f275 2019-02-04 stsp
2028 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2029 927df6b7 2019-02-10 stsp return NULL;
2030 927df6b7 2019-02-10 stsp
2031 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
2032 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2033 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2034 f8d1f275 2019-02-04 stsp } else {
2035 f8d1f275 2019-02-04 stsp path = de->d_name;
2036 f8d1f275 2019-02-04 stsp }
2037 f8d1f275 2019-02-04 stsp
2038 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2039 016a88dd 2019-05-13 stsp NULL, NULL);
2040 f8d1f275 2019-02-04 stsp if (parent_path[0])
2041 f8d1f275 2019-02-04 stsp free(path);
2042 b72f483a 2019-02-05 stsp return err;
2043 f8d1f275 2019-02-04 stsp }
2044 f8d1f275 2019-02-04 stsp
2045 f8d1f275 2019-02-04 stsp const struct got_error *
2046 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
2047 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
2048 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2049 f8d1f275 2019-02-04 stsp {
2050 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
2051 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
2052 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
2053 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
2054 f8d1f275 2019-02-04 stsp FILE *index = NULL;
2055 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
2056 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
2057 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
2058 f8d1f275 2019-02-04 stsp
2059 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
2060 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
2061 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2062 f8d1f275 2019-02-04 stsp goto done;
2063 f8d1f275 2019-02-04 stsp }
2064 f8d1f275 2019-02-04 stsp
2065 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2066 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2067 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2068 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
2069 f8d1f275 2019-02-04 stsp goto done;
2070 f8d1f275 2019-02-04 stsp }
2071 f8d1f275 2019-02-04 stsp
2072 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
2073 f8d1f275 2019-02-04 stsp if (index == NULL) {
2074 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
2075 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2076 f8d1f275 2019-02-04 stsp goto done;
2077 f8d1f275 2019-02-04 stsp }
2078 f8d1f275 2019-02-04 stsp } else {
2079 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
2080 f8d1f275 2019-02-04 stsp fclose(index);
2081 f8d1f275 2019-02-04 stsp if (err)
2082 f8d1f275 2019-02-04 stsp goto done;
2083 f8d1f275 2019-02-04 stsp }
2084 f8d1f275 2019-02-04 stsp
2085 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
2086 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
2087 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2088 c513d110 2019-02-05 stsp goto done;
2089 c513d110 2019-02-05 stsp }
2090 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
2091 927df6b7 2019-02-10 stsp if (workdir == NULL) {
2092 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
2093 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
2094 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
2095 927df6b7 2019-02-10 stsp if (ie == NULL) {
2096 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
2097 927df6b7 2019-02-10 stsp goto done;
2098 927df6b7 2019-02-10 stsp }
2099 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
2100 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
2101 927df6b7 2019-02-10 stsp goto done;
2102 927df6b7 2019-02-10 stsp } else {
2103 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", ondisk_path);
2104 927df6b7 2019-02-10 stsp goto done;
2105 927df6b7 2019-02-10 stsp }
2106 927df6b7 2019-02-10 stsp }
2107 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
2108 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
2109 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
2110 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
2111 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
2112 927df6b7 2019-02-10 stsp arg.status_path = path;
2113 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
2114 f8d1f275 2019-02-04 stsp arg.repo = repo;
2115 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
2116 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
2117 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
2118 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
2119 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2120 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
2121 f8d1f275 2019-02-04 stsp done:
2122 f8d1f275 2019-02-04 stsp if (workdir)
2123 f8d1f275 2019-02-04 stsp closedir(workdir);
2124 927df6b7 2019-02-10 stsp free(ondisk_path);
2125 f8d1f275 2019-02-04 stsp free(fileindex_path);
2126 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
2127 f8d1f275 2019-02-04 stsp return err;
2128 f8d1f275 2019-02-04 stsp }
2129 6c7ab921 2019-03-18 stsp
2130 6c7ab921 2019-03-18 stsp const struct got_error *
2131 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2132 6c7ab921 2019-03-18 stsp const char *arg)
2133 6c7ab921 2019-03-18 stsp {
2134 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
2135 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
2136 6c7ab921 2019-03-18 stsp size_t len;
2137 6c7ab921 2019-03-18 stsp
2138 6c7ab921 2019-03-18 stsp *wt_path = NULL;
2139 6c7ab921 2019-03-18 stsp
2140 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
2141 6c7ab921 2019-03-18 stsp if (resolved == NULL)
2142 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", arg);
2143 6c7ab921 2019-03-18 stsp
2144 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
2145 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
2146 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
2147 6c7ab921 2019-03-18 stsp goto done;
2148 6c7ab921 2019-03-18 stsp }
2149 6c7ab921 2019-03-18 stsp
2150 f6d88e1a 2019-05-29 stsp if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2151 f6d88e1a 2019-05-29 stsp err = got_path_skip_common_ancestor(&path,
2152 f6d88e1a 2019-05-29 stsp got_worktree_get_root_path(worktree), resolved);
2153 f6d88e1a 2019-05-29 stsp if (err)
2154 f6d88e1a 2019-05-29 stsp goto done;
2155 f6d88e1a 2019-05-29 stsp } else {
2156 f6d88e1a 2019-05-29 stsp path = strdup("");
2157 f6d88e1a 2019-05-29 stsp if (path == NULL) {
2158 f6d88e1a 2019-05-29 stsp err = got_error_from_errno("strdup");
2159 f6d88e1a 2019-05-29 stsp goto done;
2160 f6d88e1a 2019-05-29 stsp }
2161 6c7ab921 2019-03-18 stsp }
2162 6c7ab921 2019-03-18 stsp
2163 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
2164 6c7ab921 2019-03-18 stsp len = strlen(path);
2165 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
2166 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
2167 6c7ab921 2019-03-18 stsp len--;
2168 6c7ab921 2019-03-18 stsp }
2169 6c7ab921 2019-03-18 stsp done:
2170 6c7ab921 2019-03-18 stsp free(resolved);
2171 6c7ab921 2019-03-18 stsp if (err == NULL)
2172 6c7ab921 2019-03-18 stsp *wt_path = path;
2173 6c7ab921 2019-03-18 stsp else
2174 6c7ab921 2019-03-18 stsp free(path);
2175 d00136be 2019-03-26 stsp return err;
2176 d00136be 2019-03-26 stsp }
2177 d00136be 2019-03-26 stsp
2178 07f5b47a 2019-06-02 stsp static const struct got_error *
2179 07f5b47a 2019-06-02 stsp schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2180 07f5b47a 2019-06-02 stsp const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2181 07f5b47a 2019-06-02 stsp struct got_repository *repo)
2182 07f5b47a 2019-06-02 stsp {
2183 07f5b47a 2019-06-02 stsp const struct got_error *err = NULL;
2184 07f5b47a 2019-06-02 stsp struct got_fileindex_entry *ie;
2185 07f5b47a 2019-06-02 stsp
2186 07f5b47a 2019-06-02 stsp /* Re-adding an existing entry is a no-op. */
2187 07f5b47a 2019-06-02 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2188 07f5b47a 2019-06-02 stsp return NULL;
2189 07f5b47a 2019-06-02 stsp
2190 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2191 07f5b47a 2019-06-02 stsp if (err)
2192 07f5b47a 2019-06-02 stsp return err;
2193 07f5b47a 2019-06-02 stsp
2194 07f5b47a 2019-06-02 stsp err = got_fileindex_entry_add(fileindex, ie);
2195 07f5b47a 2019-06-02 stsp if (err) {
2196 07f5b47a 2019-06-02 stsp got_fileindex_entry_free(ie);
2197 07f5b47a 2019-06-02 stsp return err;
2198 07f5b47a 2019-06-02 stsp }
2199 07f5b47a 2019-06-02 stsp
2200 07f5b47a 2019-06-02 stsp return report_file_status(ie, relpath, status_cb, status_arg, repo);
2201 07f5b47a 2019-06-02 stsp }
2202 07f5b47a 2019-06-02 stsp
2203 d00136be 2019-03-26 stsp const struct got_error *
2204 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
2205 1dd54920 2019-05-11 stsp struct got_pathlist_head *ondisk_paths,
2206 1dd54920 2019-05-11 stsp got_worktree_status_cb status_cb, void *status_arg,
2207 031a5338 2019-03-26 stsp struct got_repository *repo)
2208 d00136be 2019-03-26 stsp {
2209 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2210 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
2211 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2212 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2213 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2214 d00136be 2019-03-26 stsp
2215 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2216 d00136be 2019-03-26 stsp if (err)
2217 d00136be 2019-03-26 stsp return err;
2218 d00136be 2019-03-26 stsp
2219 d00136be 2019-03-26 stsp
2220 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
2221 d00136be 2019-03-26 stsp if (fileindex == NULL) {
2222 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2223 d00136be 2019-03-26 stsp goto done;
2224 d00136be 2019-03-26 stsp }
2225 d00136be 2019-03-26 stsp
2226 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2227 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2228 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2229 d00136be 2019-03-26 stsp fileindex_path = NULL;
2230 d00136be 2019-03-26 stsp goto done;
2231 d00136be 2019-03-26 stsp }
2232 d00136be 2019-03-26 stsp
2233 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2234 d00136be 2019-03-26 stsp if (index == NULL) {
2235 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2236 d00136be 2019-03-26 stsp goto done;
2237 d00136be 2019-03-26 stsp }
2238 d00136be 2019-03-26 stsp
2239 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2240 d00136be 2019-03-26 stsp if (err)
2241 d00136be 2019-03-26 stsp goto done;
2242 d00136be 2019-03-26 stsp
2243 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2244 1dd54920 2019-05-11 stsp char *relpath;
2245 1dd54920 2019-05-11 stsp err = got_path_skip_common_ancestor(&relpath,
2246 1dd54920 2019-05-11 stsp got_worktree_get_root_path(worktree), pe->path);
2247 1dd54920 2019-05-11 stsp if (err)
2248 af12c6b9 2019-06-04 stsp break;
2249 07f5b47a 2019-06-02 stsp err = schedule_addition(pe->path, fileindex, relpath,
2250 07f5b47a 2019-06-02 stsp status_cb, status_arg, repo);
2251 1dd54920 2019-05-11 stsp free(relpath);
2252 1dd54920 2019-05-11 stsp if (err)
2253 af12c6b9 2019-06-04 stsp break;
2254 1dd54920 2019-05-11 stsp }
2255 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2256 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2257 af12c6b9 2019-06-04 stsp err = sync_err;
2258 d00136be 2019-03-26 stsp done:
2259 d00136be 2019-03-26 stsp if (index) {
2260 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2261 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2262 d00136be 2019-03-26 stsp }
2263 d00136be 2019-03-26 stsp if (fileindex)
2264 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
2265 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2266 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
2267 d00136be 2019-03-26 stsp err = unlockerr;
2268 6c7ab921 2019-03-18 stsp return err;
2269 6c7ab921 2019-03-18 stsp }
2270 17ed4618 2019-06-02 stsp
2271 17ed4618 2019-06-02 stsp static const struct got_error *
2272 17ed4618 2019-06-02 stsp schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2273 17ed4618 2019-06-02 stsp const char *relpath, int delete_local_mods,
2274 17ed4618 2019-06-02 stsp got_worktree_status_cb status_cb, void *status_arg,
2275 17ed4618 2019-06-02 stsp struct got_repository *repo)
2276 17ed4618 2019-06-02 stsp {
2277 17ed4618 2019-06-02 stsp const struct got_error *err = NULL;
2278 17ed4618 2019-06-02 stsp struct got_fileindex_entry *ie = NULL;
2279 17ed4618 2019-06-02 stsp unsigned char status;
2280 17ed4618 2019-06-02 stsp struct stat sb;
2281 17ed4618 2019-06-02 stsp
2282 17ed4618 2019-06-02 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2283 17ed4618 2019-06-02 stsp if (ie == NULL)
2284 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_BAD_PATH);
2285 2ec1f75b 2019-03-26 stsp
2286 17ed4618 2019-06-02 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2287 17ed4618 2019-06-02 stsp if (err)
2288 17ed4618 2019-06-02 stsp return err;
2289 17ed4618 2019-06-02 stsp
2290 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_NO_CHANGE) {
2291 17ed4618 2019-06-02 stsp if (status == GOT_STATUS_DELETE)
2292 17ed4618 2019-06-02 stsp return got_error_set_errno(ENOENT, ondisk_path);
2293 17ed4618 2019-06-02 stsp if (status != GOT_STATUS_MODIFY)
2294 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_STATUS);
2295 17ed4618 2019-06-02 stsp if (!delete_local_mods)
2296 17ed4618 2019-06-02 stsp return got_error(GOT_ERR_FILE_MODIFIED);
2297 17ed4618 2019-06-02 stsp }
2298 17ed4618 2019-06-02 stsp
2299 17ed4618 2019-06-02 stsp if (unlink(ondisk_path) != 0)
2300 17ed4618 2019-06-02 stsp return got_error_from_errno2("unlink", ondisk_path);
2301 17ed4618 2019-06-02 stsp
2302 17ed4618 2019-06-02 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
2303 17ed4618 2019-06-02 stsp return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2304 17ed4618 2019-06-02 stsp }
2305 17ed4618 2019-06-02 stsp
2306 2ec1f75b 2019-03-26 stsp const struct got_error *
2307 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
2308 17ed4618 2019-06-02 stsp struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2309 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
2310 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
2311 2ec1f75b 2019-03-26 stsp {
2312 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
2313 17ed4618 2019-06-02 stsp char *fileindex_path = NULL;
2314 9c6338c4 2019-06-02 stsp FILE *index = NULL;
2315 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err, *unlockerr;
2316 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2317 2ec1f75b 2019-03-26 stsp
2318 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
2319 2ec1f75b 2019-03-26 stsp if (err)
2320 2ec1f75b 2019-03-26 stsp return err;
2321 2ec1f75b 2019-03-26 stsp
2322 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
2323 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
2324 638f9024 2019-05-13 stsp err = got_error_from_errno("got_fileindex_alloc");
2325 2ec1f75b 2019-03-26 stsp goto done;
2326 2ec1f75b 2019-03-26 stsp }
2327 2ec1f75b 2019-03-26 stsp
2328 2ec1f75b 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2329 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2330 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2331 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
2332 2ec1f75b 2019-03-26 stsp goto done;
2333 2ec1f75b 2019-03-26 stsp }
2334 2ec1f75b 2019-03-26 stsp
2335 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
2336 2ec1f75b 2019-03-26 stsp if (index == NULL) {
2337 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", fileindex_path);
2338 2ec1f75b 2019-03-26 stsp goto done;
2339 2ec1f75b 2019-03-26 stsp }
2340 2ec1f75b 2019-03-26 stsp
2341 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
2342 2ec1f75b 2019-03-26 stsp if (err)
2343 2ec1f75b 2019-03-26 stsp goto done;
2344 2ec1f75b 2019-03-26 stsp
2345 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2346 17ed4618 2019-06-02 stsp char *relpath;
2347 17ed4618 2019-06-02 stsp err = got_path_skip_common_ancestor(&relpath,
2348 17ed4618 2019-06-02 stsp got_worktree_get_root_path(worktree), pe->path);
2349 17ed4618 2019-06-02 stsp if (err)
2350 af12c6b9 2019-06-04 stsp break;
2351 17ed4618 2019-06-02 stsp err = schedule_for_deletion(pe->path, fileindex, relpath,
2352 17ed4618 2019-06-02 stsp delete_local_mods, status_cb, status_arg, repo);
2353 17ed4618 2019-06-02 stsp free(relpath);
2354 17ed4618 2019-06-02 stsp if (err)
2355 af12c6b9 2019-06-04 stsp break;
2356 2ec1f75b 2019-03-26 stsp }
2357 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2358 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
2359 af12c6b9 2019-06-04 stsp err = sync_err;
2360 2ec1f75b 2019-03-26 stsp done:
2361 2ec1f75b 2019-03-26 stsp if (index) {
2362 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
2363 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2364 2ec1f75b 2019-03-26 stsp }
2365 2ec1f75b 2019-03-26 stsp if (fileindex)
2366 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
2367 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2368 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
2369 2ec1f75b 2019-03-26 stsp err = unlockerr;
2370 2ec1f75b 2019-03-26 stsp return err;
2371 2ec1f75b 2019-03-26 stsp }
2372 a129376b 2019-03-28 stsp
2373 e20a8b6f 2019-06-04 stsp static const struct got_error *
2374 e20a8b6f 2019-06-04 stsp revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2375 a129376b 2019-03-28 stsp const char *ondisk_path,
2376 a129376b 2019-03-28 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2377 a129376b 2019-03-28 stsp struct got_repository *repo)
2378 a129376b 2019-03-28 stsp {
2379 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL;
2380 e20a8b6f 2019-06-04 stsp char *relpath = NULL, *parent_path = NULL;
2381 e20a8b6f 2019-06-04 stsp struct got_fileindex_entry *ie;
2382 a129376b 2019-03-28 stsp struct got_tree_object *tree = NULL;
2383 e20a8b6f 2019-06-04 stsp struct got_object_id *tree_id = NULL;
2384 a129376b 2019-03-28 stsp const struct got_tree_entry *te;
2385 e20a8b6f 2019-06-04 stsp char *tree_path = NULL, *te_name;
2386 a129376b 2019-03-28 stsp struct got_blob_object *blob = NULL;
2387 a129376b 2019-03-28 stsp unsigned char status;
2388 a129376b 2019-03-28 stsp struct stat sb;
2389 a129376b 2019-03-28 stsp
2390 a129376b 2019-03-28 stsp err = got_path_skip_common_ancestor(&relpath,
2391 a129376b 2019-03-28 stsp got_worktree_get_root_path(worktree), ondisk_path);
2392 a129376b 2019-03-28 stsp if (err)
2393 a129376b 2019-03-28 stsp goto done;
2394 a129376b 2019-03-28 stsp
2395 a129376b 2019-03-28 stsp ie = got_fileindex_entry_get(fileindex, relpath);
2396 a129376b 2019-03-28 stsp if (ie == NULL) {
2397 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_BAD_PATH);
2398 a129376b 2019-03-28 stsp goto done;
2399 a129376b 2019-03-28 stsp }
2400 a129376b 2019-03-28 stsp
2401 a129376b 2019-03-28 stsp /* Construct in-repository path of tree which contains this blob. */
2402 a129376b 2019-03-28 stsp err = got_path_dirname(&parent_path, ie->path);
2403 a129376b 2019-03-28 stsp if (err) {
2404 a129376b 2019-03-28 stsp if (err->code != GOT_ERR_BAD_PATH)
2405 a129376b 2019-03-28 stsp goto done;
2406 e20a8b6f 2019-06-04 stsp parent_path = strdup("/");
2407 e20a8b6f 2019-06-04 stsp if (parent_path == NULL) {
2408 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("strdup");
2409 e20a8b6f 2019-06-04 stsp goto done;
2410 e20a8b6f 2019-06-04 stsp }
2411 a129376b 2019-03-28 stsp }
2412 a129376b 2019-03-28 stsp if (got_path_is_root_dir(worktree->path_prefix)) {
2413 a129376b 2019-03-28 stsp tree_path = strdup(parent_path);
2414 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2415 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2416 a129376b 2019-03-28 stsp goto done;
2417 a129376b 2019-03-28 stsp }
2418 a129376b 2019-03-28 stsp } else {
2419 a129376b 2019-03-28 stsp if (got_path_is_root_dir(parent_path)) {
2420 a129376b 2019-03-28 stsp tree_path = strdup(worktree->path_prefix);
2421 a129376b 2019-03-28 stsp if (tree_path == NULL) {
2422 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2423 a129376b 2019-03-28 stsp goto done;
2424 a129376b 2019-03-28 stsp }
2425 a129376b 2019-03-28 stsp } else {
2426 a129376b 2019-03-28 stsp if (asprintf(&tree_path, "%s/%s",
2427 a129376b 2019-03-28 stsp worktree->path_prefix, parent_path) == -1) {
2428 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2429 a129376b 2019-03-28 stsp goto done;
2430 a129376b 2019-03-28 stsp }
2431 a129376b 2019-03-28 stsp }
2432 a129376b 2019-03-28 stsp }
2433 a129376b 2019-03-28 stsp
2434 a129376b 2019-03-28 stsp err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2435 a129376b 2019-03-28 stsp tree_path);
2436 a129376b 2019-03-28 stsp if (err)
2437 a129376b 2019-03-28 stsp goto done;
2438 a129376b 2019-03-28 stsp
2439 a129376b 2019-03-28 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2440 a129376b 2019-03-28 stsp if (err)
2441 a129376b 2019-03-28 stsp goto done;
2442 a129376b 2019-03-28 stsp
2443 a129376b 2019-03-28 stsp te_name = basename(ie->path);
2444 a129376b 2019-03-28 stsp if (te_name == NULL) {
2445 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ie->path);
2446 a129376b 2019-03-28 stsp goto done;
2447 a129376b 2019-03-28 stsp }
2448 a129376b 2019-03-28 stsp
2449 a129376b 2019-03-28 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2450 a129376b 2019-03-28 stsp if (err)
2451 a129376b 2019-03-28 stsp goto done;
2452 a129376b 2019-03-28 stsp
2453 a129376b 2019-03-28 stsp te = got_object_tree_find_entry(tree, te_name);
2454 a129376b 2019-03-28 stsp if (te == NULL && status != GOT_STATUS_ADD) {
2455 a129376b 2019-03-28 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
2456 a129376b 2019-03-28 stsp goto done;
2457 a129376b 2019-03-28 stsp }
2458 a129376b 2019-03-28 stsp
2459 a129376b 2019-03-28 stsp switch (status) {
2460 a129376b 2019-03-28 stsp case GOT_STATUS_ADD:
2461 a129376b 2019-03-28 stsp (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2462 a129376b 2019-03-28 stsp got_fileindex_entry_remove(fileindex, ie);
2463 a129376b 2019-03-28 stsp break;
2464 a129376b 2019-03-28 stsp case GOT_STATUS_DELETE:
2465 a129376b 2019-03-28 stsp case GOT_STATUS_MODIFY:
2466 a129376b 2019-03-28 stsp case GOT_STATUS_CONFLICT:
2467 e20a8b6f 2019-06-04 stsp case GOT_STATUS_MISSING: {
2468 e20a8b6f 2019-06-04 stsp struct got_object_id id;
2469 a129376b 2019-03-28 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2470 a129376b 2019-03-28 stsp err = got_object_open_as_blob(&blob, repo, &id, 8192);
2471 a129376b 2019-03-28 stsp if (err)
2472 a129376b 2019-03-28 stsp goto done;
2473 a129376b 2019-03-28 stsp err = install_blob(worktree, ondisk_path, ie->path,
2474 a129376b 2019-03-28 stsp te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2475 a129376b 2019-03-28 stsp progress_arg);
2476 a129376b 2019-03-28 stsp if (err)
2477 a129376b 2019-03-28 stsp goto done;
2478 a129376b 2019-03-28 stsp if (status == GOT_STATUS_DELETE) {
2479 a129376b 2019-03-28 stsp err = update_blob_fileindex_entry(worktree,
2480 a129376b 2019-03-28 stsp fileindex, ie, ondisk_path, ie->path, blob, 1);
2481 a129376b 2019-03-28 stsp if (err)
2482 a129376b 2019-03-28 stsp goto done;
2483 a129376b 2019-03-28 stsp }
2484 a129376b 2019-03-28 stsp break;
2485 e20a8b6f 2019-06-04 stsp }
2486 a129376b 2019-03-28 stsp default:
2487 a129376b 2019-03-28 stsp goto done;
2488 a129376b 2019-03-28 stsp }
2489 a129376b 2019-03-28 stsp done:
2490 a129376b 2019-03-28 stsp free(relpath);
2491 e20a8b6f 2019-06-04 stsp free(parent_path);
2492 a129376b 2019-03-28 stsp free(tree_path);
2493 a129376b 2019-03-28 stsp if (blob)
2494 a129376b 2019-03-28 stsp got_object_blob_close(blob);
2495 a129376b 2019-03-28 stsp if (tree)
2496 a129376b 2019-03-28 stsp got_object_tree_close(tree);
2497 a129376b 2019-03-28 stsp free(tree_id);
2498 e20a8b6f 2019-06-04 stsp return err;
2499 e20a8b6f 2019-06-04 stsp }
2500 e20a8b6f 2019-06-04 stsp
2501 e20a8b6f 2019-06-04 stsp const struct got_error *
2502 e20a8b6f 2019-06-04 stsp got_worktree_revert(struct got_worktree *worktree,
2503 e20a8b6f 2019-06-04 stsp struct got_pathlist_head *ondisk_paths,
2504 e20a8b6f 2019-06-04 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
2505 e20a8b6f 2019-06-04 stsp struct got_repository *repo)
2506 e20a8b6f 2019-06-04 stsp {
2507 e20a8b6f 2019-06-04 stsp struct got_fileindex *fileindex = NULL;
2508 e20a8b6f 2019-06-04 stsp char *fileindex_path = NULL;
2509 e20a8b6f 2019-06-04 stsp FILE *index = NULL;
2510 e20a8b6f 2019-06-04 stsp const struct got_error *err = NULL, *unlockerr = NULL;
2511 e20a8b6f 2019-06-04 stsp const struct got_error *sync_err = NULL;
2512 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2513 e20a8b6f 2019-06-04 stsp
2514 e20a8b6f 2019-06-04 stsp err = lock_worktree(worktree, LOCK_EX);
2515 e20a8b6f 2019-06-04 stsp if (err)
2516 e20a8b6f 2019-06-04 stsp return err;
2517 e20a8b6f 2019-06-04 stsp
2518 e20a8b6f 2019-06-04 stsp fileindex = got_fileindex_alloc();
2519 e20a8b6f 2019-06-04 stsp if (fileindex == NULL) {
2520 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("got_fileindex_alloc");
2521 e20a8b6f 2019-06-04 stsp goto done;
2522 e20a8b6f 2019-06-04 stsp }
2523 e20a8b6f 2019-06-04 stsp
2524 e20a8b6f 2019-06-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2525 e20a8b6f 2019-06-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2526 e20a8b6f 2019-06-04 stsp err = got_error_from_errno("asprintf");
2527 e20a8b6f 2019-06-04 stsp fileindex_path = NULL;
2528 e20a8b6f 2019-06-04 stsp goto done;
2529 e20a8b6f 2019-06-04 stsp }
2530 e20a8b6f 2019-06-04 stsp
2531 e20a8b6f 2019-06-04 stsp index = fopen(fileindex_path, "rb");
2532 e20a8b6f 2019-06-04 stsp if (index == NULL) {
2533 e20a8b6f 2019-06-04 stsp err = got_error_from_errno2("fopen", fileindex_path);
2534 e20a8b6f 2019-06-04 stsp goto done;
2535 e20a8b6f 2019-06-04 stsp }
2536 e20a8b6f 2019-06-04 stsp
2537 e20a8b6f 2019-06-04 stsp err = got_fileindex_read(fileindex, index);
2538 e20a8b6f 2019-06-04 stsp if (err)
2539 e20a8b6f 2019-06-04 stsp goto done;
2540 e20a8b6f 2019-06-04 stsp
2541 e20a8b6f 2019-06-04 stsp TAILQ_FOREACH(pe, ondisk_paths, entry) {
2542 e20a8b6f 2019-06-04 stsp err = revert_file(worktree, fileindex, pe->path,
2543 e20a8b6f 2019-06-04 stsp progress_cb, progress_arg, repo);
2544 e20a8b6f 2019-06-04 stsp if (err)
2545 af12c6b9 2019-06-04 stsp break;
2546 e20a8b6f 2019-06-04 stsp }
2547 e20a8b6f 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
2548 e20a8b6f 2019-06-04 stsp if (sync_err && err == NULL)
2549 e20a8b6f 2019-06-04 stsp err = sync_err;
2550 af12c6b9 2019-06-04 stsp done:
2551 a129376b 2019-03-28 stsp if (index) {
2552 a129376b 2019-03-28 stsp if (fclose(index) != 0 && err == NULL)
2553 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2554 a129376b 2019-03-28 stsp }
2555 a129376b 2019-03-28 stsp if (fileindex)
2556 a129376b 2019-03-28 stsp got_fileindex_free(fileindex);
2557 a129376b 2019-03-28 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
2558 a129376b 2019-03-28 stsp if (unlockerr && err == NULL)
2559 a129376b 2019-03-28 stsp err = unlockerr;
2560 c4296144 2019-05-09 stsp return err;
2561 c4296144 2019-05-09 stsp }
2562 c4296144 2019-05-09 stsp
2563 cf066bf8 2019-05-09 stsp static void
2564 33ad4cbe 2019-05-12 jcs free_commitable(struct got_commitable *ct)
2565 cf066bf8 2019-05-09 stsp {
2566 24519714 2019-05-09 stsp free(ct->path);
2567 44d03001 2019-05-09 stsp free(ct->in_repo_path);
2568 768aea60 2019-05-09 stsp free(ct->ondisk_path);
2569 e75eb4da 2019-05-10 stsp free(ct->blob_id);
2570 c4e12a88 2019-05-13 stsp free(ct->base_blob_id);
2571 b416585c 2019-05-13 stsp free(ct->base_commit_id);
2572 cf066bf8 2019-05-09 stsp free(ct);
2573 cf066bf8 2019-05-09 stsp }
2574 24519714 2019-05-09 stsp
2575 ed175427 2019-05-09 stsp struct collect_commitables_arg {
2576 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths;
2577 24519714 2019-05-09 stsp struct got_repository *repo;
2578 24519714 2019-05-09 stsp struct got_worktree *worktree;
2579 24519714 2019-05-09 stsp };
2580 cf066bf8 2019-05-09 stsp
2581 c4296144 2019-05-09 stsp static const struct got_error *
2582 036813ee 2019-05-09 stsp collect_commitables(void *arg, unsigned char status, const char *relpath,
2583 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2584 c4296144 2019-05-09 stsp {
2585 ed175427 2019-05-09 stsp struct collect_commitables_arg *a = arg;
2586 c4296144 2019-05-09 stsp const struct got_error *err = NULL;
2587 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2588 c4296144 2019-05-09 stsp struct got_pathlist_entry *new = NULL;
2589 036813ee 2019-05-09 stsp char *parent_path = NULL, *path = NULL;
2590 768aea60 2019-05-09 stsp struct stat sb;
2591 c4296144 2019-05-09 stsp
2592 c4296144 2019-05-09 stsp if (status == GOT_STATUS_CONFLICT)
2593 c4296144 2019-05-09 stsp return got_error(GOT_ERR_COMMIT_CONFLICT);
2594 c4296144 2019-05-09 stsp
2595 c4296144 2019-05-09 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2596 c4296144 2019-05-09 stsp status != GOT_STATUS_DELETE)
2597 c4296144 2019-05-09 stsp return NULL;
2598 0b5cc0d6 2019-05-09 stsp
2599 036813ee 2019-05-09 stsp if (asprintf(&path, "/%s", relpath) == -1) {
2600 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2601 036813ee 2019-05-09 stsp goto done;
2602 036813ee 2019-05-09 stsp }
2603 036813ee 2019-05-09 stsp if (strcmp(path, "/") == 0) {
2604 036813ee 2019-05-09 stsp parent_path = strdup("");
2605 69960a46 2019-05-09 stsp if (parent_path == NULL)
2606 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2607 69960a46 2019-05-09 stsp } else {
2608 69960a46 2019-05-09 stsp err = got_path_dirname(&parent_path, path);
2609 69960a46 2019-05-09 stsp if (err)
2610 69960a46 2019-05-09 stsp return err;
2611 69960a46 2019-05-09 stsp }
2612 c4296144 2019-05-09 stsp
2613 036813ee 2019-05-09 stsp ct = calloc(1, sizeof(*ct));
2614 cf066bf8 2019-05-09 stsp if (ct == NULL) {
2615 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
2616 c4296144 2019-05-09 stsp goto done;
2617 768aea60 2019-05-09 stsp }
2618 768aea60 2019-05-09 stsp
2619 768aea60 2019-05-09 stsp if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2620 768aea60 2019-05-09 stsp relpath) == -1) {
2621 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2622 768aea60 2019-05-09 stsp goto done;
2623 768aea60 2019-05-09 stsp }
2624 768aea60 2019-05-09 stsp if (status == GOT_STATUS_DELETE) {
2625 768aea60 2019-05-09 stsp sb.st_mode = GOT_DEFAULT_FILE_MODE;
2626 768aea60 2019-05-09 stsp } else {
2627 768aea60 2019-05-09 stsp if (lstat(ct->ondisk_path, &sb) != 0) {
2628 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", ct->ondisk_path);
2629 768aea60 2019-05-09 stsp goto done;
2630 768aea60 2019-05-09 stsp }
2631 768aea60 2019-05-09 stsp ct->mode = sb.st_mode;
2632 c4296144 2019-05-09 stsp }
2633 c4296144 2019-05-09 stsp
2634 44d03001 2019-05-09 stsp if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2635 44d03001 2019-05-09 stsp got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2636 44d03001 2019-05-09 stsp relpath) == -1) {
2637 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2638 44d03001 2019-05-09 stsp goto done;
2639 44d03001 2019-05-09 stsp }
2640 44d03001 2019-05-09 stsp
2641 cf066bf8 2019-05-09 stsp ct->status = status;
2642 e75eb4da 2019-05-10 stsp ct->blob_id = NULL; /* will be filled in when blob gets created */
2643 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD) {
2644 016a88dd 2019-05-13 stsp ct->base_blob_id = got_object_id_dup(blob_id);
2645 c4e12a88 2019-05-13 stsp if (ct->base_blob_id == NULL) {
2646 b416585c 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2647 b416585c 2019-05-13 stsp goto done;
2648 b416585c 2019-05-13 stsp }
2649 b416585c 2019-05-13 stsp ct->base_commit_id = got_object_id_dup(commit_id);
2650 b416585c 2019-05-13 stsp if (ct->base_commit_id == NULL) {
2651 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2652 036813ee 2019-05-09 stsp goto done;
2653 036813ee 2019-05-09 stsp }
2654 ca2503ea 2019-05-09 stsp }
2655 24519714 2019-05-09 stsp ct->path = strdup(path);
2656 24519714 2019-05-09 stsp if (ct->path == NULL) {
2657 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2658 24519714 2019-05-09 stsp goto done;
2659 24519714 2019-05-09 stsp }
2660 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2661 c4296144 2019-05-09 stsp done:
2662 c42269f6 2019-05-09 stsp if (ct && (err || new == NULL))
2663 ed175427 2019-05-09 stsp free_commitable(ct);
2664 24519714 2019-05-09 stsp free(parent_path);
2665 036813ee 2019-05-09 stsp free(path);
2666 a129376b 2019-03-28 stsp return err;
2667 a129376b 2019-03-28 stsp }
2668 c4296144 2019-05-09 stsp
2669 036813ee 2019-05-09 stsp static const struct got_error *write_tree(struct got_object_id **,
2670 036813ee 2019-05-09 stsp struct got_tree_object *, const char *, struct got_pathlist_head *,
2671 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2672 036813ee 2019-05-09 stsp struct got_repository *);
2673 ed175427 2019-05-09 stsp
2674 ed175427 2019-05-09 stsp static const struct got_error *
2675 036813ee 2019-05-09 stsp write_subtree(struct got_object_id **new_subtree_id,
2676 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *parent_path,
2677 afa376bf 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2678 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2679 afa376bf 2019-05-09 stsp struct got_repository *repo)
2680 ed175427 2019-05-09 stsp {
2681 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2682 036813ee 2019-05-09 stsp struct got_tree_object *subtree;
2683 036813ee 2019-05-09 stsp char *subpath;
2684 ed175427 2019-05-09 stsp
2685 036813ee 2019-05-09 stsp if (asprintf(&subpath, "%s%s%s", parent_path,
2686 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2687 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2688 ed175427 2019-05-09 stsp
2689 036813ee 2019-05-09 stsp err = got_object_open_as_tree(&subtree, repo, te->id);
2690 ed175427 2019-05-09 stsp if (err)
2691 ed175427 2019-05-09 stsp return err;
2692 ed175427 2019-05-09 stsp
2693 036813ee 2019-05-09 stsp err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2694 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
2695 036813ee 2019-05-09 stsp got_object_tree_close(subtree);
2696 036813ee 2019-05-09 stsp free(subpath);
2697 036813ee 2019-05-09 stsp return err;
2698 036813ee 2019-05-09 stsp }
2699 ed175427 2019-05-09 stsp
2700 036813ee 2019-05-09 stsp static const struct got_error *
2701 33ad4cbe 2019-05-12 jcs match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2702 036813ee 2019-05-09 stsp {
2703 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2704 036813ee 2019-05-09 stsp char *ct_parent_path = NULL;
2705 ed175427 2019-05-09 stsp
2706 036813ee 2019-05-09 stsp *match = 0;
2707 ed175427 2019-05-09 stsp
2708 036813ee 2019-05-09 stsp if (strchr(ct->path, '/') == NULL) {
2709 0f63689d 2019-05-10 stsp *match = got_path_is_root_dir(path);
2710 0f63689d 2019-05-10 stsp return NULL;
2711 036813ee 2019-05-09 stsp }
2712 0b5cc0d6 2019-05-09 stsp
2713 0f63689d 2019-05-10 stsp err = got_path_dirname(&ct_parent_path, ct->path);
2714 0f63689d 2019-05-10 stsp if (err)
2715 0f63689d 2019-05-10 stsp return err;
2716 036813ee 2019-05-09 stsp *match = (strcmp(path, ct_parent_path) == 0);
2717 036813ee 2019-05-09 stsp free(ct_parent_path);
2718 036813ee 2019-05-09 stsp return err;
2719 036813ee 2019-05-09 stsp }
2720 0b5cc0d6 2019-05-09 stsp
2721 768aea60 2019-05-09 stsp static mode_t
2722 33ad4cbe 2019-05-12 jcs get_ct_file_mode(struct got_commitable *ct)
2723 768aea60 2019-05-09 stsp {
2724 768aea60 2019-05-09 stsp return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2725 768aea60 2019-05-09 stsp }
2726 768aea60 2019-05-09 stsp
2727 036813ee 2019-05-09 stsp static const struct got_error *
2728 036813ee 2019-05-09 stsp alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2729 33ad4cbe 2019-05-12 jcs struct got_tree_entry *te, struct got_commitable *ct)
2730 036813ee 2019-05-09 stsp {
2731 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2732 ca2503ea 2019-05-09 stsp
2733 036813ee 2019-05-09 stsp *new_te = NULL;
2734 0b5cc0d6 2019-05-09 stsp
2735 036813ee 2019-05-09 stsp err = got_object_tree_entry_dup(new_te, te);
2736 036813ee 2019-05-09 stsp if (err)
2737 036813ee 2019-05-09 stsp goto done;
2738 0b5cc0d6 2019-05-09 stsp
2739 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2740 036813ee 2019-05-09 stsp
2741 036813ee 2019-05-09 stsp free((*new_te)->id);
2742 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2743 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2744 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2745 036813ee 2019-05-09 stsp goto done;
2746 036813ee 2019-05-09 stsp }
2747 036813ee 2019-05-09 stsp done:
2748 036813ee 2019-05-09 stsp if (err && *new_te) {
2749 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2750 036813ee 2019-05-09 stsp *new_te = NULL;
2751 036813ee 2019-05-09 stsp }
2752 036813ee 2019-05-09 stsp return err;
2753 036813ee 2019-05-09 stsp }
2754 036813ee 2019-05-09 stsp
2755 036813ee 2019-05-09 stsp static const struct got_error *
2756 036813ee 2019-05-09 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2757 33ad4cbe 2019-05-12 jcs struct got_commitable *ct)
2758 036813ee 2019-05-09 stsp {
2759 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2760 036813ee 2019-05-09 stsp char *ct_name;
2761 036813ee 2019-05-09 stsp
2762 036813ee 2019-05-09 stsp *new_te = NULL;
2763 036813ee 2019-05-09 stsp
2764 4229330b 2019-05-10 stsp *new_te = calloc(1, sizeof(**new_te));
2765 036813ee 2019-05-09 stsp if (*new_te == NULL)
2766 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2767 036813ee 2019-05-09 stsp
2768 036813ee 2019-05-09 stsp ct_name = basename(ct->path);
2769 036813ee 2019-05-09 stsp if (ct_name == NULL) {
2770 638f9024 2019-05-13 stsp err = got_error_from_errno2("basename", ct->path);
2771 036813ee 2019-05-09 stsp goto done;
2772 036813ee 2019-05-09 stsp }
2773 036813ee 2019-05-09 stsp (*new_te)->name = strdup(ct_name);
2774 036813ee 2019-05-09 stsp if ((*new_te)->name == NULL) {
2775 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2776 036813ee 2019-05-09 stsp goto done;
2777 036813ee 2019-05-09 stsp }
2778 036813ee 2019-05-09 stsp
2779 768aea60 2019-05-09 stsp (*new_te)->mode = get_ct_file_mode(ct);
2780 036813ee 2019-05-09 stsp
2781 e75eb4da 2019-05-10 stsp (*new_te)->id = got_object_id_dup(ct->blob_id);
2782 036813ee 2019-05-09 stsp if ((*new_te)->id == NULL) {
2783 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2784 036813ee 2019-05-09 stsp goto done;
2785 036813ee 2019-05-09 stsp }
2786 036813ee 2019-05-09 stsp done:
2787 036813ee 2019-05-09 stsp if (err && *new_te) {
2788 036813ee 2019-05-09 stsp got_object_tree_entry_close(*new_te);
2789 036813ee 2019-05-09 stsp *new_te = NULL;
2790 036813ee 2019-05-09 stsp }
2791 036813ee 2019-05-09 stsp return err;
2792 036813ee 2019-05-09 stsp }
2793 036813ee 2019-05-09 stsp
2794 036813ee 2019-05-09 stsp static const struct got_error *
2795 036813ee 2019-05-09 stsp insert_tree_entry(struct got_tree_entry *new_te,
2796 036813ee 2019-05-09 stsp struct got_pathlist_head *paths)
2797 036813ee 2019-05-09 stsp {
2798 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2799 036813ee 2019-05-09 stsp struct got_pathlist_entry *new_pe;
2800 036813ee 2019-05-09 stsp
2801 036813ee 2019-05-09 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2802 036813ee 2019-05-09 stsp if (err)
2803 036813ee 2019-05-09 stsp return err;
2804 036813ee 2019-05-09 stsp if (new_pe == NULL)
2805 036813ee 2019-05-09 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
2806 036813ee 2019-05-09 stsp return NULL;
2807 afa376bf 2019-05-09 stsp }
2808 afa376bf 2019-05-09 stsp
2809 afa376bf 2019-05-09 stsp static const struct got_error *
2810 33ad4cbe 2019-05-12 jcs report_ct_status(struct got_commitable *ct,
2811 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg)
2812 afa376bf 2019-05-09 stsp {
2813 afa376bf 2019-05-09 stsp const char *ct_path = ct->path;
2814 afa376bf 2019-05-09 stsp while (ct_path[0] == '/')
2815 afa376bf 2019-05-09 stsp ct_path++;
2816 016a88dd 2019-05-13 stsp return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2817 036813ee 2019-05-09 stsp }
2818 036813ee 2019-05-09 stsp
2819 036813ee 2019-05-09 stsp static const struct got_error *
2820 44d03001 2019-05-09 stsp match_modified_subtree(int *modified, struct got_tree_entry *te,
2821 44d03001 2019-05-09 stsp const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2822 44d03001 2019-05-09 stsp {
2823 44d03001 2019-05-09 stsp const struct got_error *err = NULL;
2824 44d03001 2019-05-09 stsp struct got_pathlist_entry *pe;
2825 44d03001 2019-05-09 stsp char *te_path;
2826 44d03001 2019-05-09 stsp
2827 44d03001 2019-05-09 stsp *modified = 0;
2828 44d03001 2019-05-09 stsp
2829 44d03001 2019-05-09 stsp if (asprintf(&te_path, "%s%s%s", base_tree_path,
2830 44d03001 2019-05-09 stsp got_path_is_root_dir(base_tree_path) ? "" : "/",
2831 44d03001 2019-05-09 stsp te->name) == -1)
2832 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2833 44d03001 2019-05-09 stsp
2834 44d03001 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2835 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2836 44d03001 2019-05-09 stsp *modified = got_path_is_child(ct->in_repo_path, te_path,
2837 44d03001 2019-05-09 stsp strlen(te_path));
2838 44d03001 2019-05-09 stsp if (*modified)
2839 44d03001 2019-05-09 stsp break;
2840 44d03001 2019-05-09 stsp }
2841 44d03001 2019-05-09 stsp
2842 44d03001 2019-05-09 stsp free(te_path);
2843 44d03001 2019-05-09 stsp return err;
2844 44d03001 2019-05-09 stsp }
2845 44d03001 2019-05-09 stsp
2846 44d03001 2019-05-09 stsp static const struct got_error *
2847 33ad4cbe 2019-05-12 jcs match_deleted_or_modified_ct(struct got_commitable **ctp,
2848 036813ee 2019-05-09 stsp struct got_tree_entry *te, const char *base_tree_path,
2849 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths)
2850 036813ee 2019-05-09 stsp {
2851 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2852 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2853 036813ee 2019-05-09 stsp
2854 036813ee 2019-05-09 stsp *ctp = NULL;
2855 036813ee 2019-05-09 stsp
2856 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2857 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2858 036813ee 2019-05-09 stsp char *ct_name = NULL;
2859 036813ee 2019-05-09 stsp int path_matches;
2860 036813ee 2019-05-09 stsp
2861 036813ee 2019-05-09 stsp if (ct->status != GOT_STATUS_MODIFY &&
2862 036813ee 2019-05-09 stsp ct->status != GOT_STATUS_DELETE)
2863 036813ee 2019-05-09 stsp continue;
2864 036813ee 2019-05-09 stsp
2865 c4e12a88 2019-05-13 stsp if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2866 036813ee 2019-05-09 stsp continue;
2867 036813ee 2019-05-09 stsp
2868 036813ee 2019-05-09 stsp err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2869 036813ee 2019-05-09 stsp if (err)
2870 036813ee 2019-05-09 stsp return err;
2871 036813ee 2019-05-09 stsp if (!path_matches)
2872 036813ee 2019-05-09 stsp continue;
2873 036813ee 2019-05-09 stsp
2874 036813ee 2019-05-09 stsp ct_name = basename(pe->path);
2875 036813ee 2019-05-09 stsp if (ct_name == NULL)
2876 638f9024 2019-05-13 stsp return got_error_from_errno2("basename", pe->path);
2877 036813ee 2019-05-09 stsp
2878 036813ee 2019-05-09 stsp if (strcmp(te->name, ct_name) != 0)
2879 036813ee 2019-05-09 stsp continue;
2880 036813ee 2019-05-09 stsp
2881 036813ee 2019-05-09 stsp *ctp = ct;
2882 036813ee 2019-05-09 stsp break;
2883 036813ee 2019-05-09 stsp }
2884 036813ee 2019-05-09 stsp
2885 036813ee 2019-05-09 stsp return err;
2886 036813ee 2019-05-09 stsp }
2887 036813ee 2019-05-09 stsp
2888 036813ee 2019-05-09 stsp static const struct got_error *
2889 036813ee 2019-05-09 stsp write_tree(struct got_object_id **new_tree_id,
2890 036813ee 2019-05-09 stsp struct got_tree_object *base_tree, const char *path_base_tree,
2891 036813ee 2019-05-09 stsp struct got_pathlist_head *commitable_paths,
2892 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
2893 036813ee 2019-05-09 stsp struct got_repository *repo)
2894 036813ee 2019-05-09 stsp {
2895 036813ee 2019-05-09 stsp const struct got_error *err = NULL;
2896 036813ee 2019-05-09 stsp const struct got_tree_entries *base_entries = NULL;
2897 036813ee 2019-05-09 stsp struct got_pathlist_head paths;
2898 036813ee 2019-05-09 stsp struct got_tree_entries new_tree_entries;
2899 2f51b5b3 2019-05-09 stsp struct got_tree_entry *te, *new_te = NULL;
2900 036813ee 2019-05-09 stsp struct got_pathlist_entry *pe;
2901 036813ee 2019-05-09 stsp
2902 036813ee 2019-05-09 stsp TAILQ_INIT(&paths);
2903 036813ee 2019-05-09 stsp new_tree_entries.nentries = 0;
2904 036813ee 2019-05-09 stsp SIMPLEQ_INIT(&new_tree_entries.head);
2905 036813ee 2019-05-09 stsp
2906 036813ee 2019-05-09 stsp /* Insert, and recurse into, newly added entries first. */
2907 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
2908 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2909 2f51b5b3 2019-05-09 stsp char *child_path = NULL, *slash;
2910 036813ee 2019-05-09 stsp
2911 a3df2849 2019-05-20 stsp if (ct->status != GOT_STATUS_ADD ||
2912 a3df2849 2019-05-20 stsp (ct->flags & GOT_COMMITABLE_ADDED))
2913 036813ee 2019-05-09 stsp continue;
2914 036813ee 2019-05-09 stsp
2915 036813ee 2019-05-09 stsp if (!got_path_is_child(pe->path, path_base_tree,
2916 2f51b5b3 2019-05-09 stsp strlen(path_base_tree)))
2917 036813ee 2019-05-09 stsp continue;
2918 036813ee 2019-05-09 stsp
2919 036813ee 2019-05-09 stsp err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2920 036813ee 2019-05-09 stsp pe->path);
2921 036813ee 2019-05-09 stsp if (err)
2922 036813ee 2019-05-09 stsp goto done;
2923 036813ee 2019-05-09 stsp
2924 2f51b5b3 2019-05-09 stsp slash = strchr(child_path, '/');
2925 2f51b5b3 2019-05-09 stsp if (slash == NULL) {
2926 036813ee 2019-05-09 stsp err = alloc_added_blob_tree_entry(&new_te, ct);
2927 036813ee 2019-05-09 stsp if (err)
2928 036813ee 2019-05-09 stsp goto done;
2929 afa376bf 2019-05-09 stsp err = report_ct_status(ct, status_cb, status_arg);
2930 afa376bf 2019-05-09 stsp if (err)
2931 afa376bf 2019-05-09 stsp goto done;
2932 a3df2849 2019-05-20 stsp ct->flags |= GOT_COMMITABLE_ADDED;
2933 036813ee 2019-05-09 stsp } else {
2934 2f51b5b3 2019-05-09 stsp char *subtree_path;
2935 036813ee 2019-05-09 stsp
2936 2f51b5b3 2019-05-09 stsp *slash = '\0'; /* trim trailing path components */
2937 baa7dcfa 2019-05-09 stsp if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2938 baa7dcfa 2019-05-09 stsp got_path_is_root_dir(path_base_tree) ? "" : "/",
2939 2f51b5b3 2019-05-09 stsp child_path) == -1) {
2940 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2941 2f51b5b3 2019-05-09 stsp goto done;
2942 baa7dcfa 2019-05-09 stsp }
2943 baa7dcfa 2019-05-09 stsp
2944 9ba0479c 2019-05-10 stsp new_te = calloc(1, sizeof(*new_te));
2945 9ba0479c 2019-05-10 stsp new_te->mode = S_IFDIR;
2946 9ba0479c 2019-05-10 stsp new_te->name = strdup(child_path);
2947 9ba0479c 2019-05-10 stsp if (new_te->name == NULL) {
2948 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2949 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2950 9ba0479c 2019-05-10 stsp new_te = NULL;
2951 9ba0479c 2019-05-10 stsp goto done;
2952 9ba0479c 2019-05-10 stsp }
2953 baa7dcfa 2019-05-09 stsp err = write_tree(&new_te->id, NULL, subtree_path,
2954 afa376bf 2019-05-09 stsp commitable_paths, status_cb, status_arg, repo);
2955 2f51b5b3 2019-05-09 stsp free(subtree_path);
2956 9ba0479c 2019-05-10 stsp if (err) {
2957 9ba0479c 2019-05-10 stsp got_object_tree_entry_close(new_te);
2958 9ba0479c 2019-05-10 stsp new_te = NULL;
2959 036813ee 2019-05-09 stsp goto done;
2960 9ba0479c 2019-05-10 stsp }
2961 ed175427 2019-05-09 stsp }
2962 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2963 2f51b5b3 2019-05-09 stsp if (err)
2964 2f51b5b3 2019-05-09 stsp goto done;
2965 2f51b5b3 2019-05-09 stsp }
2966 2f51b5b3 2019-05-09 stsp
2967 2f51b5b3 2019-05-09 stsp if (base_tree) {
2968 2f51b5b3 2019-05-09 stsp /* Handle modified and deleted entries. */
2969 2f51b5b3 2019-05-09 stsp base_entries = got_object_tree_get_entries(base_tree);
2970 2f51b5b3 2019-05-09 stsp SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2971 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = NULL;
2972 2f51b5b3 2019-05-09 stsp
2973 2f51b5b3 2019-05-09 stsp if (S_ISDIR(te->mode)) {
2974 44d03001 2019-05-09 stsp int modified;
2975 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
2976 036813ee 2019-05-09 stsp if (err)
2977 036813ee 2019-05-09 stsp goto done;
2978 44d03001 2019-05-09 stsp err = match_modified_subtree(&modified, te,
2979 44d03001 2019-05-09 stsp path_base_tree, commitable_paths);
2980 2f51b5b3 2019-05-09 stsp if (err)
2981 2f51b5b3 2019-05-09 stsp goto done;
2982 44d03001 2019-05-09 stsp /* Avoid recursion into unmodified subtrees. */
2983 44d03001 2019-05-09 stsp if (modified) {
2984 44d03001 2019-05-09 stsp free(new_te->id);
2985 44d03001 2019-05-09 stsp err = write_subtree(&new_te->id, te,
2986 44d03001 2019-05-09 stsp path_base_tree, commitable_paths,
2987 44d03001 2019-05-09 stsp status_cb, status_arg, repo);
2988 44d03001 2019-05-09 stsp if (err)
2989 44d03001 2019-05-09 stsp goto done;
2990 44d03001 2019-05-09 stsp }
2991 036813ee 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
2992 036813ee 2019-05-09 stsp if (err)
2993 036813ee 2019-05-09 stsp goto done;
2994 2f51b5b3 2019-05-09 stsp continue;
2995 036813ee 2019-05-09 stsp }
2996 2f51b5b3 2019-05-09 stsp
2997 2f51b5b3 2019-05-09 stsp err = match_deleted_or_modified_ct(&ct, te,
2998 2f51b5b3 2019-05-09 stsp path_base_tree, commitable_paths);
2999 2f51b5b3 2019-05-09 stsp if (ct) {
3000 2f51b5b3 2019-05-09 stsp /* NB: Deleted entries get dropped here. */
3001 2f51b5b3 2019-05-09 stsp if (ct->status == GOT_STATUS_MODIFY) {
3002 2f51b5b3 2019-05-09 stsp err = alloc_modified_blob_tree_entry(
3003 2f51b5b3 2019-05-09 stsp &new_te, te, ct);
3004 2f51b5b3 2019-05-09 stsp if (err)
3005 2f51b5b3 2019-05-09 stsp goto done;
3006 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3007 2f51b5b3 2019-05-09 stsp if (err)
3008 2f51b5b3 2019-05-09 stsp goto done;
3009 2f51b5b3 2019-05-09 stsp }
3010 b416585c 2019-05-13 stsp err = report_ct_status(ct, status_cb,
3011 b416585c 2019-05-13 stsp status_arg);
3012 afa376bf 2019-05-09 stsp if (err)
3013 afa376bf 2019-05-09 stsp goto done;
3014 2f51b5b3 2019-05-09 stsp } else {
3015 2f51b5b3 2019-05-09 stsp /* Entry is unchanged; just copy it. */
3016 2f51b5b3 2019-05-09 stsp err = got_object_tree_entry_dup(&new_te, te);
3017 2f51b5b3 2019-05-09 stsp if (err)
3018 2f51b5b3 2019-05-09 stsp goto done;
3019 2f51b5b3 2019-05-09 stsp err = insert_tree_entry(new_te, &paths);
3020 2f51b5b3 2019-05-09 stsp if (err)
3021 2f51b5b3 2019-05-09 stsp goto done;
3022 2f51b5b3 2019-05-09 stsp }
3023 ca2503ea 2019-05-09 stsp }
3024 ed175427 2019-05-09 stsp }
3025 0b5cc0d6 2019-05-09 stsp
3026 036813ee 2019-05-09 stsp /* Write new list of entries; deleted entries have been dropped. */
3027 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &paths, entry) {
3028 0b5cc0d6 2019-05-09 stsp struct got_tree_entry *te = pe->data;
3029 0b5cc0d6 2019-05-09 stsp new_tree_entries.nentries++;
3030 0b5cc0d6 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3031 0b5cc0d6 2019-05-09 stsp }
3032 036813ee 2019-05-09 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3033 ed175427 2019-05-09 stsp done:
3034 ca2503ea 2019-05-09 stsp got_object_tree_entries_close(&new_tree_entries);
3035 036813ee 2019-05-09 stsp got_pathlist_free(&paths);
3036 ed175427 2019-05-09 stsp return err;
3037 ed175427 2019-05-09 stsp }
3038 ed175427 2019-05-09 stsp
3039 ebf99748 2019-05-09 stsp static const struct got_error *
3040 ebf99748 2019-05-09 stsp update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3041 ebf99748 2019-05-09 stsp struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3042 ebf99748 2019-05-09 stsp {
3043 af12c6b9 2019-06-04 stsp const struct got_error *err = NULL, *sync_err;
3044 9c6338c4 2019-06-02 stsp char *fileindex_path = NULL;
3045 ebf99748 2019-05-09 stsp struct got_fileindex *fileindex = NULL;
3046 ebf99748 2019-05-09 stsp struct got_pathlist_entry *pe;
3047 ebf99748 2019-05-09 stsp
3048 ebf99748 2019-05-09 stsp err = open_fileindex(&fileindex, &fileindex_path, worktree);
3049 ebf99748 2019-05-09 stsp if (err)
3050 ebf99748 2019-05-09 stsp return err;
3051 ebf99748 2019-05-09 stsp
3052 ebf99748 2019-05-09 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
3053 ebf99748 2019-05-09 stsp struct got_fileindex_entry *ie;
3054 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3055 ebf99748 2019-05-09 stsp
3056 ebf99748 2019-05-09 stsp ie = got_fileindex_entry_get(fileindex, pe->path);
3057 ebf99748 2019-05-09 stsp if (ie) {
3058 ebf99748 2019-05-09 stsp if (ct->status == GOT_STATUS_DELETE) {
3059 ebf99748 2019-05-09 stsp got_fileindex_entry_remove(fileindex, ie);
3060 ebf99748 2019-05-09 stsp got_fileindex_entry_free(ie);
3061 ebf99748 2019-05-09 stsp } else
3062 ebf99748 2019-05-09 stsp err = got_fileindex_entry_update(ie,
3063 e75eb4da 2019-05-10 stsp ct->ondisk_path, ct->blob_id->sha1,
3064 ebf99748 2019-05-09 stsp new_base_commit_id->sha1, 1);
3065 ebf99748 2019-05-09 stsp } else {
3066 ebf99748 2019-05-09 stsp err = got_fileindex_entry_alloc(&ie,
3067 e75eb4da 2019-05-10 stsp ct->ondisk_path, pe->path, ct->blob_id->sha1,
3068 ebf99748 2019-05-09 stsp new_base_commit_id->sha1);
3069 ebf99748 2019-05-09 stsp if (err)
3070 af12c6b9 2019-06-04 stsp break;
3071 ebf99748 2019-05-09 stsp err = got_fileindex_entry_add(fileindex, ie);
3072 ebf99748 2019-05-09 stsp if (err)
3073 af12c6b9 2019-06-04 stsp break;
3074 ebf99748 2019-05-09 stsp }
3075 ebf99748 2019-05-09 stsp }
3076 af12c6b9 2019-06-04 stsp sync_err = sync_fileindex(fileindex, fileindex_path);
3077 af12c6b9 2019-06-04 stsp if (sync_err && err == NULL)
3078 af12c6b9 2019-06-04 stsp err = sync_err;
3079 ebf99748 2019-05-09 stsp free(fileindex_path);
3080 ebf99748 2019-05-09 stsp got_fileindex_free(fileindex);
3081 d56d26ce 2019-05-10 stsp return err;
3082 d56d26ce 2019-05-10 stsp }
3083 d56d26ce 2019-05-10 stsp
3084 d56d26ce 2019-05-10 stsp static const struct got_error *
3085 33ad4cbe 2019-05-12 jcs check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3086 d56d26ce 2019-05-10 stsp struct got_object_id *head_commit_id)
3087 d56d26ce 2019-05-10 stsp {
3088 d56d26ce 2019-05-10 stsp const struct got_error *err = NULL;
3089 d56d26ce 2019-05-10 stsp struct got_object_id *id_in_head;
3090 d56d26ce 2019-05-10 stsp
3091 1251a9e5 2019-05-10 stsp /*
3092 b416585c 2019-05-13 stsp * Require that modified/deleted files are based on the branch head.
3093 b416585c 2019-05-13 stsp * This requirement could be relaxed to force less update operations
3094 b416585c 2019-05-13 stsp * on users but, for now, we want to play it safe and see how it goes.
3095 b416585c 2019-05-13 stsp *
3096 b416585c 2019-05-13 stsp * If this check is relaxed, it must still ensure that no modifications
3097 b416585c 2019-05-13 stsp * were made to files *and their parents* in commits between the file's
3098 b416585c 2019-05-13 stsp * base commit and the branch head. Otherwise, tree conflicts will not
3099 b416585c 2019-05-13 stsp * be detected reliably.
3100 b416585c 2019-05-13 stsp */
3101 b416585c 2019-05-13 stsp if (ct->status != GOT_STATUS_ADD) {
3102 b416585c 2019-05-13 stsp if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
3103 b416585c 2019-05-13 stsp return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3104 b416585c 2019-05-13 stsp return NULL;
3105 d56d26ce 2019-05-10 stsp }
3106 d56d26ce 2019-05-10 stsp
3107 b416585c 2019-05-13 stsp /* Require that added files don't exist in the branch head. */
3108 b416585c 2019-05-13 stsp err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3109 b416585c 2019-05-13 stsp ct->in_repo_path);
3110 b416585c 2019-05-13 stsp if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3111 b416585c 2019-05-13 stsp return err;
3112 b416585c 2019-05-13 stsp if (id_in_head) {
3113 b416585c 2019-05-13 stsp free(id_in_head);
3114 b416585c 2019-05-13 stsp return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3115 b416585c 2019-05-13 stsp }
3116 d56d26ce 2019-05-10 stsp
3117 d56d26ce 2019-05-10 stsp free(id_in_head);
3118 b416585c 2019-05-13 stsp return NULL;
3119 ebf99748 2019-05-09 stsp }
3120 ebf99748 2019-05-09 stsp
3121 c4296144 2019-05-09 stsp const struct got_error *
3122 c4296144 2019-05-09 stsp got_worktree_commit(struct got_object_id **new_commit_id,
3123 c4296144 2019-05-09 stsp struct got_worktree *worktree, const char *ondisk_path,
3124 33ad4cbe 2019-05-12 jcs const char *author, const char *committer,
3125 33ad4cbe 2019-05-12 jcs got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3126 afa376bf 2019-05-09 stsp got_worktree_status_cb status_cb, void *status_arg,
3127 afa376bf 2019-05-09 stsp struct got_repository *repo)
3128 c4296144 2019-05-09 stsp {
3129 c4296144 2019-05-09 stsp const struct got_error *err = NULL, *unlockerr = NULL;
3130 ed175427 2019-05-09 stsp struct collect_commitables_arg cc_arg;
3131 036813ee 2019-05-09 stsp struct got_pathlist_head commitable_paths;
3132 c4296144 2019-05-09 stsp struct got_pathlist_entry *pe;
3133 bc70eb79 2019-05-09 stsp char *relpath = NULL;
3134 bc70eb79 2019-05-09 stsp const char *head_ref_name = NULL;
3135 36a38700 2019-05-10 stsp struct got_reference *head_ref = NULL;
3136 588edf97 2019-05-10 stsp struct got_commit_object *head_commit = NULL;
3137 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id = NULL;
3138 09f5bd90 2019-05-09 stsp struct got_reference *head_ref2 = NULL;
3139 09f5bd90 2019-05-09 stsp struct got_object_id *head_commit_id2 = NULL;
3140 588edf97 2019-05-10 stsp struct got_tree_object *head_tree = NULL;
3141 036813ee 2019-05-09 stsp struct got_object_id *new_tree_id = NULL;
3142 de18fc63 2019-05-09 stsp struct got_object_id_queue parent_ids;
3143 de18fc63 2019-05-09 stsp struct got_object_qid *pid = NULL;
3144 33ad4cbe 2019-05-12 jcs char *logmsg = NULL;
3145 c4296144 2019-05-09 stsp
3146 c4296144 2019-05-09 stsp *new_commit_id = NULL;
3147 c4296144 2019-05-09 stsp
3148 036813ee 2019-05-09 stsp TAILQ_INIT(&commitable_paths);
3149 de18fc63 2019-05-09 stsp SIMPLEQ_INIT(&parent_ids);
3150 c4296144 2019-05-09 stsp
3151 c4296144 2019-05-09 stsp if (ondisk_path) {
3152 c4296144 2019-05-09 stsp err = got_path_skip_common_ancestor(&relpath,
3153 c4296144 2019-05-09 stsp worktree->root_path, ondisk_path);
3154 c4296144 2019-05-09 stsp if (err)
3155 c4296144 2019-05-09 stsp return err;
3156 c4296144 2019-05-09 stsp }
3157 c4296144 2019-05-09 stsp
3158 c4296144 2019-05-09 stsp err = lock_worktree(worktree, LOCK_EX);
3159 c4296144 2019-05-09 stsp if (err)
3160 c4296144 2019-05-09 stsp goto done;
3161 675c7539 2019-05-09 stsp
3162 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3163 36a38700 2019-05-10 stsp if (err)
3164 36a38700 2019-05-10 stsp goto done;
3165 36a38700 2019-05-10 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
3166 09f5bd90 2019-05-09 stsp if (err)
3167 09f5bd90 2019-05-09 stsp goto done;
3168 09f5bd90 2019-05-09 stsp
3169 036813ee 2019-05-09 stsp cc_arg.commitable_paths = &commitable_paths;
3170 24519714 2019-05-09 stsp cc_arg.worktree = worktree;
3171 24519714 2019-05-09 stsp cc_arg.repo = repo;
3172 675c7539 2019-05-09 stsp err = got_worktree_status(worktree, relpath ? relpath : "",
3173 ed175427 2019-05-09 stsp repo, collect_commitables, &cc_arg, NULL, NULL);
3174 675c7539 2019-05-09 stsp if (err)
3175 675c7539 2019-05-09 stsp goto done;
3176 675c7539 2019-05-09 stsp
3177 33ad4cbe 2019-05-12 jcs if (TAILQ_EMPTY(&commitable_paths)) {
3178 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3179 33ad4cbe 2019-05-12 jcs goto done;
3180 33ad4cbe 2019-05-12 jcs }
3181 33ad4cbe 2019-05-12 jcs
3182 588edf97 2019-05-10 stsp err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3183 588edf97 2019-05-10 stsp if (err)
3184 588edf97 2019-05-10 stsp goto done;
3185 588edf97 2019-05-10 stsp
3186 819f385b 2019-05-10 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3187 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3188 d56d26ce 2019-05-10 stsp err = check_ct_out_of_date(ct, repo, head_commit_id);
3189 d56d26ce 2019-05-10 stsp if (err)
3190 819f385b 2019-05-10 stsp goto done;
3191 819f385b 2019-05-10 stsp }
3192 de18fc63 2019-05-09 stsp
3193 588edf97 2019-05-10 stsp err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3194 588edf97 2019-05-10 stsp if (err)
3195 588edf97 2019-05-10 stsp goto done;
3196 588edf97 2019-05-10 stsp
3197 33ad4cbe 2019-05-12 jcs if (commit_msg_cb != NULL) {
3198 33ad4cbe 2019-05-12 jcs err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3199 33ad4cbe 2019-05-12 jcs if (err)
3200 33ad4cbe 2019-05-12 jcs goto done;
3201 33ad4cbe 2019-05-12 jcs }
3202 c4296144 2019-05-09 stsp
3203 33ad4cbe 2019-05-12 jcs if (logmsg == NULL || strlen(logmsg) == 0) {
3204 33ad4cbe 2019-05-12 jcs err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3205 33ad4cbe 2019-05-12 jcs goto done;
3206 33ad4cbe 2019-05-12 jcs }
3207 33ad4cbe 2019-05-12 jcs
3208 cf066bf8 2019-05-09 stsp /* Create blobs from added and modified files and record their IDs. */
3209 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3210 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3211 cf066bf8 2019-05-09 stsp char *ondisk_path;
3212 cf066bf8 2019-05-09 stsp
3213 cf066bf8 2019-05-09 stsp if (ct->status != GOT_STATUS_ADD &&
3214 cf066bf8 2019-05-09 stsp ct->status != GOT_STATUS_MODIFY)
3215 cf066bf8 2019-05-09 stsp continue;
3216 cf066bf8 2019-05-09 stsp
3217 cf066bf8 2019-05-09 stsp if (asprintf(&ondisk_path, "%s/%s",
3218 cf066bf8 2019-05-09 stsp worktree->root_path, pe->path) == -1) {
3219 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3220 cf066bf8 2019-05-09 stsp goto done;
3221 cf066bf8 2019-05-09 stsp }
3222 e75eb4da 2019-05-10 stsp err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3223 a7055788 2019-05-09 stsp free(ondisk_path);
3224 cf066bf8 2019-05-09 stsp if (err)
3225 cf066bf8 2019-05-09 stsp goto done;
3226 cf066bf8 2019-05-09 stsp }
3227 036813ee 2019-05-09 stsp
3228 036813ee 2019-05-09 stsp /* Recursively write new tree objects. */
3229 588edf97 2019-05-10 stsp err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3230 afa376bf 2019-05-09 stsp status_cb, status_arg, repo);
3231 036813ee 2019-05-09 stsp if (err)
3232 036813ee 2019-05-09 stsp goto done;
3233 036813ee 2019-05-09 stsp
3234 de18fc63 2019-05-09 stsp err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3235 de18fc63 2019-05-09 stsp if (err)
3236 de18fc63 2019-05-09 stsp goto done;
3237 de18fc63 2019-05-09 stsp SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3238 de18fc63 2019-05-09 stsp err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3239 de18fc63 2019-05-09 stsp 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3240 de18fc63 2019-05-09 stsp got_object_qid_free(pid);
3241 33ad4cbe 2019-05-12 jcs if (logmsg != NULL)
3242 33ad4cbe 2019-05-12 jcs free(logmsg);
3243 9d40349a 2019-05-09 stsp if (err)
3244 9d40349a 2019-05-09 stsp goto done;
3245 9d40349a 2019-05-09 stsp
3246 09f5bd90 2019-05-09 stsp /* Check if a concurrent commit to our branch has occurred. */
3247 bc70eb79 2019-05-09 stsp head_ref_name = got_worktree_get_head_ref_name(worktree);
3248 bc70eb79 2019-05-09 stsp if (head_ref_name == NULL) {
3249 638f9024 2019-05-13 stsp err = got_error_from_errno("got_worktree_get_head_ref_name");
3250 09f5bd90 2019-05-09 stsp goto done;
3251 09f5bd90 2019-05-09 stsp }
3252 2f17228e 2019-05-12 stsp /* Lock the reference here to prevent concurrent modification. */
3253 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3254 09f5bd90 2019-05-09 stsp if (err)
3255 09f5bd90 2019-05-09 stsp goto done;
3256 09f5bd90 2019-05-09 stsp err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3257 ebf99748 2019-05-09 stsp if (err)
3258 ebf99748 2019-05-09 stsp goto done;
3259 09f5bd90 2019-05-09 stsp if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3260 09f5bd90 2019-05-09 stsp err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3261 09f5bd90 2019-05-09 stsp goto done;
3262 09f5bd90 2019-05-09 stsp }
3263 09f5bd90 2019-05-09 stsp /* Update branch head in repository. */
3264 2f17228e 2019-05-12 stsp err = got_ref_change_ref(head_ref2, *new_commit_id);
3265 f2c16586 2019-05-09 stsp if (err)
3266 f2c16586 2019-05-09 stsp goto done;
3267 2f17228e 2019-05-12 stsp err = got_ref_write(head_ref2, repo);
3268 f2c16586 2019-05-09 stsp if (err)
3269 f2c16586 2019-05-09 stsp goto done;
3270 f2c16586 2019-05-09 stsp
3271 09f5bd90 2019-05-09 stsp err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3272 09f5bd90 2019-05-09 stsp if (err)
3273 09f5bd90 2019-05-09 stsp goto done;
3274 09f5bd90 2019-05-09 stsp
3275 ebf99748 2019-05-09 stsp err = ref_base_commit(worktree, repo);
3276 ebf99748 2019-05-09 stsp if (err)
3277 ebf99748 2019-05-09 stsp goto done;
3278 ebf99748 2019-05-09 stsp
3279 ebf99748 2019-05-09 stsp err = update_fileindex_after_commit(&commitable_paths,
3280 ebf99748 2019-05-09 stsp *new_commit_id, worktree);
3281 ebf99748 2019-05-09 stsp if (err)
3282 ebf99748 2019-05-09 stsp goto done;
3283 c4296144 2019-05-09 stsp done:
3284 c4296144 2019-05-09 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
3285 c4296144 2019-05-09 stsp if (unlockerr && err == NULL)
3286 c4296144 2019-05-09 stsp err = unlockerr;
3287 036813ee 2019-05-09 stsp TAILQ_FOREACH(pe, &commitable_paths, entry) {
3288 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3289 ed175427 2019-05-09 stsp free_commitable(ct);
3290 cf066bf8 2019-05-09 stsp }
3291 036813ee 2019-05-09 stsp got_pathlist_free(&commitable_paths);
3292 588edf97 2019-05-10 stsp if (head_tree)
3293 588edf97 2019-05-10 stsp got_object_tree_close(head_tree);
3294 588edf97 2019-05-10 stsp if (head_commit)
3295 588edf97 2019-05-10 stsp got_object_commit_close(head_commit);
3296 c4296144 2019-05-09 stsp free(relpath);
3297 09f5bd90 2019-05-09 stsp free(head_commit_id);
3298 09f5bd90 2019-05-09 stsp free(head_commit_id2);
3299 36a38700 2019-05-10 stsp if (head_ref)
3300 36a38700 2019-05-10 stsp got_ref_close(head_ref);
3301 2f17228e 2019-05-12 stsp if (head_ref2) {
3302 2f17228e 2019-05-12 stsp unlockerr = got_ref_unlock(head_ref2);
3303 2f17228e 2019-05-12 stsp if (unlockerr && err == NULL)
3304 2f17228e 2019-05-12 stsp err = unlockerr;
3305 09f5bd90 2019-05-09 stsp got_ref_close(head_ref2);
3306 2f17228e 2019-05-12 stsp }
3307 c4296144 2019-05-09 stsp return err;
3308 8656d6c4 2019-05-20 stsp }
3309 8656d6c4 2019-05-20 stsp
3310 8656d6c4 2019-05-20 stsp const char *
3311 8656d6c4 2019-05-20 stsp got_commitable_get_path(struct got_commitable *ct)
3312 8656d6c4 2019-05-20 stsp {
3313 8656d6c4 2019-05-20 stsp return ct->path;
3314 8656d6c4 2019-05-20 stsp }
3315 8656d6c4 2019-05-20 stsp
3316 8656d6c4 2019-05-20 stsp unsigned int
3317 8656d6c4 2019-05-20 stsp got_commitable_get_status(struct got_commitable *ct)
3318 8656d6c4 2019-05-20 stsp {
3319 8656d6c4 2019-05-20 stsp return ct->status;
3320 c4296144 2019-05-09 stsp }