2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
21 86c3caaf 2018-03-09 stsp #include <string.h>
22 86c3caaf 2018-03-09 stsp #include <stdio.h>
23 86c3caaf 2018-03-09 stsp #include <stdlib.h>
24 86c3caaf 2018-03-09 stsp #include <fcntl.h>
25 86c3caaf 2018-03-09 stsp #include <errno.h>
26 86c3caaf 2018-03-09 stsp #include <unistd.h>
27 9d31a1d8 2018-03-11 stsp #include <sha1.h>
28 9d31a1d8 2018-03-11 stsp #include <zlib.h>
29 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
30 512f0d0e 2019-01-02 stsp #include <libgen.h>
32 86c3caaf 2018-03-09 stsp #include "got_error.h"
33 86c3caaf 2018-03-09 stsp #include "got_repository.h"
34 5261c201 2018-04-01 stsp #include "got_reference.h"
35 9d31a1d8 2018-03-11 stsp #include "got_object.h"
36 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
37 511a516b 2018-05-19 stsp #include "got_opentemp.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
43 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
48 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 99724ed4 2018-03-10 stsp static const struct got_error *
52 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
54 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
56 99724ed4 2018-03-10 stsp int fd = -1;
58 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
59 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
60 99724ed4 2018-03-10 stsp path = NULL;
64 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
65 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
66 99724ed4 2018-03-10 stsp if (fd == -1) {
67 99724ed4 2018-03-10 stsp err = got_error_from_errno();
71 99724ed4 2018-03-10 stsp if (content) {
72 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
73 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
74 99724ed4 2018-03-10 stsp err = got_error_from_errno();
80 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
81 99724ed4 2018-03-10 stsp err = got_error_from_errno();
86 507dc3bb 2018-12-29 stsp static const struct got_error *
87 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
89 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
90 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
91 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
92 507dc3bb 2018-12-29 stsp char *path = NULL;
94 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
95 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
96 507dc3bb 2018-12-29 stsp path = NULL;
100 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
104 507dc3bb 2018-12-29 stsp if (content) {
105 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
106 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
107 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
112 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
113 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
118 507dc3bb 2018-12-29 stsp free(tmppath);
119 507dc3bb 2018-12-29 stsp fclose(tmpfile);
120 99724ed4 2018-03-10 stsp return err;
123 09fe317a 2018-03-11 stsp static const struct got_error *
124 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
126 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
127 09fe317a 2018-03-11 stsp char *path;
128 09fe317a 2018-03-11 stsp int fd = -1;
130 09fe317a 2018-03-11 stsp struct stat sb;
132 09fe317a 2018-03-11 stsp *content = NULL;
134 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
135 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
136 09fe317a 2018-03-11 stsp path = NULL;
140 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
141 09fe317a 2018-03-11 stsp if (fd == -1) {
142 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
145 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 73a5ef67 2018-03-11 stsp : got_error_from_errno());
151 09fe317a 2018-03-11 stsp stat(path, &sb);
152 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
153 09fe317a 2018-03-11 stsp if (*content == NULL) {
154 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
158 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
159 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
160 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
161 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
164 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
165 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
168 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
171 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
172 09fe317a 2018-03-11 stsp err = got_error_from_errno();
173 09fe317a 2018-03-11 stsp free(path);
175 09fe317a 2018-03-11 stsp free(*content);
176 09fe317a 2018-03-11 stsp *content = NULL;
178 09fe317a 2018-03-11 stsp return err;
181 86c3caaf 2018-03-09 stsp const struct got_error *
182 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
183 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
185 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
186 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
187 65596e15 2018-12-24 stsp int obj_type;
188 7ac97322 2018-03-11 stsp char *path_got = NULL;
189 08d425ea 2018-12-24 stsp char *refstr = NULL;
190 cde76477 2018-03-11 stsp char *repo_path = NULL;
191 1451e70d 2018-03-10 stsp char *formatstr = NULL;
192 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
193 65596e15 2018-12-24 stsp char *basestr = NULL;
195 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
197 65596e15 2018-12-24 stsp return err;
198 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
200 65596e15 2018-12-24 stsp return err;
201 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
202 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
204 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
205 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
206 0a585a0d 2018-03-17 stsp return got_error_from_errno();
209 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
210 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
211 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
215 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
216 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
217 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
220 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
221 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
225 056e7441 2018-03-11 stsp /* Create an empty lock file. */
226 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
230 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
231 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
235 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
236 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
237 08d425ea 2018-12-24 stsp if (refstr == NULL) {
238 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
241 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
245 65596e15 2018-12-24 stsp /* Record our base commit. */
246 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
249 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
253 1451e70d 2018-03-10 stsp /* Store path to repository. */
254 cde76477 2018-03-11 stsp repo_path = got_repo_get_path(repo);
255 cde76477 2018-03-11 stsp if (repo_path == NULL) {
256 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
259 cde76477 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
263 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
264 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
265 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
269 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
270 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
271 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
274 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
279 65596e15 2018-12-24 stsp free(commit_id);
280 7ac97322 2018-03-11 stsp free(path_got);
281 1451e70d 2018-03-10 stsp free(formatstr);
282 08d425ea 2018-12-24 stsp free(refstr);
283 cde76477 2018-03-11 stsp free(repo_path);
284 0bb8a95e 2018-03-12 stsp free(absprefix);
285 65596e15 2018-12-24 stsp free(basestr);
286 86c3caaf 2018-03-09 stsp return err;
289 86c3caaf 2018-03-09 stsp const struct got_error *
290 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
292 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
293 7ac97322 2018-03-11 stsp char *path_got;
294 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
295 056e7441 2018-03-11 stsp char *path_lock = NULL;
296 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
297 271d2a38 2018-12-25 stsp char *head_ref_str = NULL;
298 6d9d28c3 2018-03-11 stsp int version, fd = -1;
299 6d9d28c3 2018-03-11 stsp const char *errstr;
300 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
302 6d9d28c3 2018-03-11 stsp *worktree = NULL;
304 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
305 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
306 7ac97322 2018-03-11 stsp path_got = NULL;
310 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
311 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
312 056e7441 2018-03-11 stsp path_lock = NULL;
316 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
317 6d9d28c3 2018-03-11 stsp if (fd == -1) {
318 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
319 73a5ef67 2018-03-11 stsp : got_error_from_errno());
323 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
327 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
328 6d9d28c3 2018-03-11 stsp if (errstr) {
329 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
332 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
333 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
337 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
338 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
339 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
342 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
344 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
345 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
346 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
349 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
350 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
354 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
355 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
359 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
360 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
364 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
368 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
369 eaccb85f 2018-12-25 stsp base_commit_id_str);
373 271d2a38 2018-12-25 stsp err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
377 271d2a38 2018-12-25 stsp err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
380 eaccb85f 2018-12-25 stsp got_repo_close(repo);
381 7ac97322 2018-03-11 stsp free(path_got);
382 056e7441 2018-03-11 stsp free(path_lock);
383 271d2a38 2018-12-25 stsp free(head_ref_str);
384 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
386 6d9d28c3 2018-03-11 stsp if (fd != -1)
388 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
389 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
390 6d9d28c3 2018-03-11 stsp *worktree = NULL;
392 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
394 6d9d28c3 2018-03-11 stsp return err;
398 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
400 c88eb298 2018-03-11 stsp free(worktree->root_path);
401 cde76477 2018-03-11 stsp free(worktree->repo_path);
402 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
403 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
404 271d2a38 2018-12-25 stsp if (worktree->head_ref)
405 271d2a38 2018-12-25 stsp got_ref_close(worktree->head_ref);
406 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
407 056e7441 2018-03-11 stsp close(worktree->lockfd);
408 6d9d28c3 2018-03-11 stsp free(worktree);
411 2fbdb5ae 2018-12-29 stsp const char *
412 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
414 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
417 49520a32 2018-12-29 stsp const char *
418 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
420 f609be2e 2018-12-29 stsp return worktree->path_prefix;
423 e5dc7198 2018-12-29 stsp const struct got_error *
424 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
425 e5dc7198 2018-12-29 stsp const char *path_prefix)
427 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
429 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
430 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
431 e5dc7198 2018-12-29 stsp return got_error_from_errno();
433 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
434 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
435 e5dc7198 2018-12-29 stsp free(absprefix);
436 e5dc7198 2018-12-29 stsp return NULL;
440 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
442 271d2a38 2018-12-25 stsp return got_ref_to_str(worktree->head_ref);
445 be7061eb 2018-12-30 stsp struct got_reference *
446 be7061eb 2018-12-30 stsp got_worktree_get_head_ref(struct got_worktree *worktree)
448 be7061eb 2018-12-30 stsp return got_ref_dup(worktree->head_ref);
451 507dc3bb 2018-12-29 stsp const struct got_object_id *
452 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
454 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
457 507dc3bb 2018-12-29 stsp const struct got_error *
458 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
459 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
461 507dc3bb 2018-12-29 stsp const struct got_error *err;
462 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
463 507dc3bb 2018-12-29 stsp char *id_str = NULL;
464 507dc3bb 2018-12-29 stsp char *path_got = NULL;
466 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
467 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
468 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
469 507dc3bb 2018-12-29 stsp path_got = NULL;
473 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
475 507dc3bb 2018-12-29 stsp return err;
477 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
478 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
482 507dc3bb 2018-12-29 stsp /* Record our base commit. */
483 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
486 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
490 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
491 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
492 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
493 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
498 507dc3bb 2018-12-29 stsp got_object_close(obj);
499 507dc3bb 2018-12-29 stsp free(id_str);
500 507dc3bb 2018-12-29 stsp free(path_got);
501 507dc3bb 2018-12-29 stsp return err;
504 9d31a1d8 2018-03-11 stsp static const struct got_error *
505 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
507 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
508 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
509 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
510 86c3caaf 2018-03-09 stsp return NULL;
513 9d31a1d8 2018-03-11 stsp static const char *
514 9d31a1d8 2018-03-11 stsp apply_path_prefix(struct got_worktree *worktree, const char *path)
516 9d31a1d8 2018-03-11 stsp const char *p = path;
517 9d31a1d8 2018-03-11 stsp p += strlen(worktree->path_prefix);
518 9d31a1d8 2018-03-11 stsp if (*p == '/')
523 9d31a1d8 2018-03-11 stsp static const struct got_error *
524 a207cf0a 2018-12-29 stsp blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
525 eac9755f 2018-12-29 stsp struct got_fileindex_entry *entry, const char *path,
526 eac9755f 2018-12-29 stsp struct got_blob_object *blob, struct got_repository *repo,
527 d7b62c98 2018-12-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
528 d7b62c98 2018-12-27 stsp const char *progress_path)
530 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
531 c34b20a2 2018-03-12 stsp char *ondisk_path;
532 507dc3bb 2018-12-29 stsp int fd = -1;
533 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
534 507dc3bb 2018-12-29 stsp int update = 0;
535 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
537 c34b20a2 2018-03-12 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
538 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
539 0a585a0d 2018-03-17 stsp return got_error_from_errno();
541 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
542 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
543 9d31a1d8 2018-03-11 stsp if (fd == -1) {
544 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
545 9d31a1d8 2018-03-11 stsp if (errno == EEXIST) {
546 9d31a1d8 2018-03-11 stsp struct stat sb;
547 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) == -1) {
548 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
550 9d31a1d8 2018-03-11 stsp } else if (!S_ISREG(sb.st_mode)) {
551 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
552 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
555 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
556 507dc3bb 2018-12-29 stsp ondisk_path);
559 507dc3bb 2018-12-29 stsp update = 1;
562 507dc3bb 2018-12-29 stsp return err;
565 507dc3bb 2018-12-29 stsp (*progress_cb)(progress_arg,
566 507dc3bb 2018-12-29 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
568 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
570 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
571 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
574 9d31a1d8 2018-03-11 stsp if (len > 0) {
575 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
576 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
577 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
578 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
580 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
581 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
584 61d6eaa3 2018-12-24 stsp hdrlen = 0;
586 9d31a1d8 2018-03-11 stsp } while (len != 0);
590 507dc3bb 2018-12-29 stsp if (update) {
591 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
592 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
598 51514078 2018-12-25 stsp err = got_fileindex_entry_update(entry, ondisk_path,
599 51514078 2018-12-25 stsp blob->id.sha1, worktree->base_commit_id->sha1);
601 51514078 2018-12-25 stsp err = got_fileindex_entry_alloc(&entry, ondisk_path,
602 51514078 2018-12-25 stsp apply_path_prefix(worktree, path), blob->id.sha1,
603 51514078 2018-12-25 stsp worktree->base_commit_id->sha1);
606 51514078 2018-12-25 stsp err = got_fileindex_entry_add(fileindex, entry);
611 507dc3bb 2018-12-29 stsp if (fd != -1)
613 c34b20a2 2018-03-12 stsp free(ondisk_path);
614 507dc3bb 2018-12-29 stsp free(tmppath);
615 9d31a1d8 2018-03-11 stsp return err;
618 9d31a1d8 2018-03-11 stsp static const struct got_error *
619 9d31a1d8 2018-03-11 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
621 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
622 9d31a1d8 2018-03-11 stsp char *abspath;
624 9d31a1d8 2018-03-11 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path,
625 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
626 0a585a0d 2018-03-17 stsp return got_error_from_errno();
628 9d31a1d8 2018-03-11 stsp /* XXX queue work rather than editing disk directly? */
629 9d31a1d8 2018-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
630 9d31a1d8 2018-03-11 stsp struct stat sb;
632 9d31a1d8 2018-03-11 stsp if (errno != EEXIST) {
633 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
637 9d31a1d8 2018-03-11 stsp if (lstat(abspath, &sb) == -1) {
638 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
642 9d31a1d8 2018-03-11 stsp if (!S_ISDIR(sb.st_mode)) {
643 9d31a1d8 2018-03-11 stsp /* TODO directory is obstructed; do something */
644 9d31a1d8 2018-03-11 stsp return got_error(GOT_ERR_FILE_OBSTRUCTED);
649 9d31a1d8 2018-03-11 stsp free(abspath);
650 9d31a1d8 2018-03-11 stsp return err;
653 9d31a1d8 2018-03-11 stsp static const struct got_error *
654 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *, struct got_fileindex *,
655 92a684f4 2018-03-12 stsp struct got_tree_object *, const char *, struct got_repository *,
656 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
657 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg);
659 9d31a1d8 2018-03-11 stsp static const struct got_error *
660 9d31a1d8 2018-03-11 stsp tree_checkout_entry(struct got_worktree *worktree,
661 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_entry *te,
662 92a684f4 2018-03-12 stsp const char *parent, struct got_repository *repo,
663 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
664 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
666 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
667 9d31a1d8 2018-03-11 stsp struct got_object *obj = NULL;
668 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
669 eac9755f 2018-12-29 stsp struct got_fileindex_entry *entry = NULL;
670 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
671 9d31a1d8 2018-03-11 stsp char *path = NULL;
672 f7b38925 2018-04-01 stsp char *progress_path = NULL;
673 9d31a1d8 2018-03-11 stsp size_t len;
675 9d31a1d8 2018-03-11 stsp if (parent[0] == '/' && parent[1] == '\0')
676 9d31a1d8 2018-03-11 stsp parent = "";
677 9d31a1d8 2018-03-11 stsp if (asprintf(&path, "%s/%s", parent, te->name) == -1)
678 0a585a0d 2018-03-17 stsp return got_error_from_errno();
680 9d31a1d8 2018-03-11 stsp /* Skip this entry if it is outside of our path prefix. */
681 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
682 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0) {
683 9d31a1d8 2018-03-11 stsp free(path);
684 9d31a1d8 2018-03-11 stsp return NULL;
687 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, te->id);
691 f7b38925 2018-04-01 stsp progress_path = path;
692 f7b38925 2018-04-01 stsp if (strncmp(progress_path, worktree->path_prefix, len) == 0)
693 f7b38925 2018-04-01 stsp progress_path += len;
695 15a94983 2018-12-23 stsp switch (obj->type) {
696 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_BLOB:
697 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) >= strlen(path))
699 eac9755f 2018-12-29 stsp entry = got_fileindex_entry_get(fileindex,
700 eac9755f 2018-12-29 stsp apply_path_prefix(worktree, path));
701 eac9755f 2018-12-29 stsp if (entry &&
702 eac9755f 2018-12-29 stsp memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
703 507dc3bb 2018-12-29 stsp SHA1_DIGEST_LENGTH) == 0) {
704 507dc3bb 2018-12-29 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
705 507dc3bb 2018-12-29 stsp progress_path);
708 507dc3bb 2018-12-29 stsp if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
709 eac9755f 2018-12-29 stsp SHA1_DIGEST_LENGTH) == 0)
711 9d31a1d8 2018-03-11 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
714 eac9755f 2018-12-29 stsp err = blob_checkout(worktree, fileindex, entry, path, blob,
715 eac9755f 2018-12-29 stsp repo, progress_cb, progress_arg, progress_path);
717 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_TREE:
718 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) < strlen(path)) {
719 9d31a1d8 2018-03-11 stsp err = add_dir_on_disk(worktree, path);
723 381be7cc 2018-12-29 stsp err = got_object_tree_open(&tree, repo, obj);
726 99437157 2018-11-11 stsp /* XXX infinite recursion possible */
727 92a684f4 2018-03-12 stsp err = tree_checkout(worktree, fileindex, tree, path, repo,
728 99437157 2018-11-11 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
736 9d31a1d8 2018-03-11 stsp got_object_blob_close(blob);
738 9d31a1d8 2018-03-11 stsp got_object_tree_close(tree);
740 9d31a1d8 2018-03-11 stsp got_object_close(obj);
741 9d31a1d8 2018-03-11 stsp free(path);
742 512f0d0e 2019-01-02 stsp return err;
745 512f0d0e 2019-01-02 stsp struct collect_missing_entry_args {
746 512f0d0e 2019-01-02 stsp struct got_fileindex *fileindex;
747 512f0d0e 2019-01-02 stsp const struct got_tree_entries *entries;
748 512f0d0e 2019-01-02 stsp struct got_fileindex missing_entries;
749 512f0d0e 2019-01-02 stsp const char *path_prefix;
752 512f0d0e 2019-01-02 stsp static const struct got_error *
753 512f0d0e 2019-01-02 stsp collect_missing_file(void *args, struct got_fileindex_entry *entry)
755 512f0d0e 2019-01-02 stsp struct collect_missing_entry_args *a = args;
756 512f0d0e 2019-01-02 stsp char *name;
757 512f0d0e 2019-01-02 stsp struct got_tree_entry *te;
758 512f0d0e 2019-01-02 stsp int found = 0;
760 512f0d0e 2019-01-02 stsp if (a->path_prefix[0] == '\0' && strchr(entry->path, '/') != NULL)
761 512f0d0e 2019-01-02 stsp return NULL;
762 512f0d0e 2019-01-02 stsp if (a->path_prefix[0] != '\0' &&
763 512f0d0e 2019-01-02 stsp strncmp(a->path_prefix, entry->path, strlen(a->path_prefix)) != 0)
764 512f0d0e 2019-01-02 stsp return NULL;
766 512f0d0e 2019-01-02 stsp name = basename(entry->path);
767 512f0d0e 2019-01-02 stsp if (name == NULL)
768 512f0d0e 2019-01-02 stsp return got_error_from_errno();
770 512f0d0e 2019-01-02 stsp SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
771 512f0d0e 2019-01-02 stsp if (strcmp(te->name, name) == 0) {
778 512f0d0e 2019-01-02 stsp return NULL;
780 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(a->fileindex, entry);
781 512f0d0e 2019-01-02 stsp return got_fileindex_entry_add(&a->missing_entries, entry);
784 512f0d0e 2019-01-02 stsp /* Remove files which exist in the file index but not in the tree. */
785 512f0d0e 2019-01-02 stsp static const struct got_error *
786 512f0d0e 2019-01-02 stsp remove_missing_files(struct got_worktree *worktree, const char *path,
787 512f0d0e 2019-01-02 stsp struct got_fileindex *fileindex, const struct got_tree_entries *entries,
788 512f0d0e 2019-01-02 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
789 512f0d0e 2019-01-02 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
791 512f0d0e 2019-01-02 stsp const struct got_error *err = NULL;
792 512f0d0e 2019-01-02 stsp struct collect_missing_entry_args a;
793 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry, *tmp;
795 512f0d0e 2019-01-02 stsp a.fileindex = fileindex;
796 512f0d0e 2019-01-02 stsp a.entries = entries;
797 512f0d0e 2019-01-02 stsp a.missing_entries.nentries = 0;
798 512f0d0e 2019-01-02 stsp a.path_prefix = path;
799 512f0d0e 2019-01-02 stsp while (a.path_prefix[0] == '/')
800 512f0d0e 2019-01-02 stsp a.path_prefix++;
801 512f0d0e 2019-01-02 stsp TAILQ_INIT(&a.missing_entries.entries);
802 512f0d0e 2019-01-02 stsp err = got_fileindex_for_each_entry(fileindex, collect_missing_file, &a);
804 512f0d0e 2019-01-02 stsp return err;
806 512f0d0e 2019-01-02 stsp TAILQ_FOREACH_SAFE(entry, &a.missing_entries.entries, entry, tmp) {
807 512f0d0e 2019-01-02 stsp char *ondisk_path = NULL;
809 512f0d0e 2019-01-02 stsp if (cancel_cb) {
810 512f0d0e 2019-01-02 stsp err = (*cancel_cb)(cancel_arg);
815 512f0d0e 2019-01-02 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, entry->path);
817 512f0d0e 2019-01-02 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
818 512f0d0e 2019-01-02 stsp entry->path) == -1) {
819 512f0d0e 2019-01-02 stsp err = got_error_from_errno();
823 512f0d0e 2019-01-02 stsp if (unlink(ondisk_path) == -1)
824 512f0d0e 2019-01-02 stsp err = got_error_from_errno();
825 512f0d0e 2019-01-02 stsp free(ondisk_path);
829 512f0d0e 2019-01-02 stsp TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
830 512f0d0e 2019-01-02 stsp got_fileindex_entry_free(entry);
834 512f0d0e 2019-01-02 stsp while (!TAILQ_EMPTY(&a.missing_entries.entries)) {
835 512f0d0e 2019-01-02 stsp entry = TAILQ_FIRST(&a.missing_entries.entries);
836 512f0d0e 2019-01-02 stsp TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
837 512f0d0e 2019-01-02 stsp got_fileindex_entry_free(entry);
841 9d31a1d8 2018-03-11 stsp return err;
844 9d31a1d8 2018-03-11 stsp static const struct got_error *
845 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *worktree,
846 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_object *tree,
847 92a684f4 2018-03-12 stsp const char *path, struct got_repository *repo,
848 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
849 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
851 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
852 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
853 9d31a1d8 2018-03-11 stsp struct got_tree_entry *te;
854 9d31a1d8 2018-03-11 stsp size_t len;
856 9d31a1d8 2018-03-11 stsp /* Skip this tree if it is outside of our path prefix. */
857 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
858 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0)
859 9d31a1d8 2018-03-11 stsp return NULL;
861 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
862 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
863 99437157 2018-11-11 stsp if (cancel_cb) {
864 99437157 2018-11-11 stsp err = (*cancel_cb)(cancel_arg);
866 512f0d0e 2019-01-02 stsp return err;
868 92a684f4 2018-03-12 stsp err = tree_checkout_entry(worktree, fileindex, te, path, repo,
869 99437157 2018-11-11 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
871 512f0d0e 2019-01-02 stsp return err;
874 512f0d0e 2019-01-02 stsp len = strlen(worktree->path_prefix);
875 512f0d0e 2019-01-02 stsp if (strncmp(worktree->path_prefix, path, len) == 0) {
876 512f0d0e 2019-01-02 stsp err = remove_missing_files(worktree, path, fileindex, entries,
877 512f0d0e 2019-01-02 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
879 512f0d0e 2019-01-02 stsp return err;
882 9d31a1d8 2018-03-11 stsp return err;
885 9d31a1d8 2018-03-11 stsp const struct got_error *
886 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
887 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
888 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
890 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
891 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
892 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
893 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
894 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
895 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
897 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
899 9d31a1d8 2018-03-11 stsp return err;
901 7426bbfd 2018-12-24 stsp fileindex = got_fileindex_alloc();
902 9d31a1d8 2018-03-11 stsp if (fileindex == NULL) {
903 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
907 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
908 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
909 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
910 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
915 51514078 2018-12-25 stsp * Read the file index.
916 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
917 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
919 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
920 51514078 2018-12-25 stsp if (index == NULL) {
921 51514078 2018-12-25 stsp err = got_error_from_errno();
924 51514078 2018-12-25 stsp err = got_fileindex_read(fileindex, index);
925 51514078 2018-12-25 stsp fclose(index);
929 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
930 b8bdcc21 2018-12-24 stsp fileindex_path);
934 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
935 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
939 93a30277 2018-12-24 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
943 a143fb78 2018-12-25 stsp checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
944 a143fb78 2018-12-25 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
946 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
947 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
951 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
952 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
956 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
957 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
961 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
962 9d31a1d8 2018-03-11 stsp if (commit)
963 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
964 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
965 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
966 b8bdcc21 2018-12-24 stsp if (new_index)
967 b8bdcc21 2018-12-24 stsp fclose(new_index);
968 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
969 9d31a1d8 2018-03-11 stsp free(fileindex_path);
970 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
971 a143fb78 2018-12-25 stsp if (checkout_err)
972 a143fb78 2018-12-25 stsp err = checkout_err;
973 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
974 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
975 9d31a1d8 2018-03-11 stsp err = unlockerr;
976 9d31a1d8 2018-03-11 stsp return err;