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