2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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>
20 133d2798 2019-01-08 stsp #include <sys/tree.h>
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>
35 86c3caaf 2018-03-09 stsp #include "got_error.h"
36 86c3caaf 2018-03-09 stsp #include "got_repository.h"
37 5261c201 2018-04-01 stsp #include "got_reference.h"
38 9d31a1d8 2018-03-11 stsp #include "got_object.h"
39 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
46 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
52 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 99724ed4 2018-03-10 stsp static const struct got_error *
56 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
58 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
60 99724ed4 2018-03-10 stsp int fd = -1;
62 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
63 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
64 99724ed4 2018-03-10 stsp path = NULL;
68 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
69 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
70 99724ed4 2018-03-10 stsp if (fd == -1) {
71 99724ed4 2018-03-10 stsp err = got_error_from_errno();
75 99724ed4 2018-03-10 stsp if (content) {
76 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
77 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
78 99724ed4 2018-03-10 stsp err = got_error_from_errno();
84 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
85 99724ed4 2018-03-10 stsp err = got_error_from_errno();
90 507dc3bb 2018-12-29 stsp static const struct got_error *
91 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
93 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
94 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
95 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
96 507dc3bb 2018-12-29 stsp char *path = NULL;
98 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
99 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
100 507dc3bb 2018-12-29 stsp path = NULL;
104 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
108 507dc3bb 2018-12-29 stsp if (content) {
109 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
110 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
111 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
116 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
117 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
122 507dc3bb 2018-12-29 stsp free(tmppath);
123 507dc3bb 2018-12-29 stsp fclose(tmpfile);
124 99724ed4 2018-03-10 stsp return err;
127 09fe317a 2018-03-11 stsp static const struct got_error *
128 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
130 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
131 09fe317a 2018-03-11 stsp char *path;
132 09fe317a 2018-03-11 stsp int fd = -1;
134 09fe317a 2018-03-11 stsp struct stat sb;
136 09fe317a 2018-03-11 stsp *content = NULL;
138 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
139 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
140 09fe317a 2018-03-11 stsp path = NULL;
144 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
145 09fe317a 2018-03-11 stsp if (fd == -1) {
146 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
149 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
150 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
151 73a5ef67 2018-03-11 stsp : got_error_from_errno());
155 09fe317a 2018-03-11 stsp stat(path, &sb);
156 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
157 09fe317a 2018-03-11 stsp if (*content == NULL) {
158 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
162 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
163 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
164 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
165 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
168 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
169 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
172 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
175 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
176 09fe317a 2018-03-11 stsp err = got_error_from_errno();
177 09fe317a 2018-03-11 stsp free(path);
179 09fe317a 2018-03-11 stsp free(*content);
180 09fe317a 2018-03-11 stsp *content = NULL;
182 09fe317a 2018-03-11 stsp return err;
185 86c3caaf 2018-03-09 stsp const struct got_error *
186 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
187 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
189 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
190 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
191 65596e15 2018-12-24 stsp int obj_type;
192 7ac97322 2018-03-11 stsp char *path_got = NULL;
193 08d425ea 2018-12-24 stsp char *refstr = NULL;
194 1451e70d 2018-03-10 stsp char *formatstr = NULL;
195 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
196 65596e15 2018-12-24 stsp char *basestr = NULL;
198 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
200 65596e15 2018-12-24 stsp return err;
201 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
203 65596e15 2018-12-24 stsp return err;
204 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
205 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
207 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
208 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
209 0a585a0d 2018-03-17 stsp return got_error_from_errno();
212 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
213 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
214 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
218 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
219 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
220 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
223 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
224 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
228 056e7441 2018-03-11 stsp /* Create an empty lock file. */
229 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
233 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
234 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
238 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
239 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
240 08d425ea 2018-12-24 stsp if (refstr == NULL) {
241 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
244 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
248 65596e15 2018-12-24 stsp /* Record our base commit. */
249 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
252 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
256 1451e70d 2018-03-10 stsp /* Store path to repository. */
257 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
258 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
262 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
263 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
264 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
268 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
269 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
270 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
273 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
278 65596e15 2018-12-24 stsp free(commit_id);
279 7ac97322 2018-03-11 stsp free(path_got);
280 1451e70d 2018-03-10 stsp free(formatstr);
281 08d425ea 2018-12-24 stsp free(refstr);
282 0bb8a95e 2018-03-12 stsp free(absprefix);
283 65596e15 2018-12-24 stsp free(basestr);
284 86c3caaf 2018-03-09 stsp return err;
287 247140b2 2019-02-05 stsp static const struct got_error *
288 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
290 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
291 7ac97322 2018-03-11 stsp char *path_got;
292 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
293 056e7441 2018-03-11 stsp char *path_lock = NULL;
294 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
295 271d2a38 2018-12-25 stsp char *head_ref_str = NULL;
296 6d9d28c3 2018-03-11 stsp int version, fd = -1;
297 6d9d28c3 2018-03-11 stsp const char *errstr;
298 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
300 6d9d28c3 2018-03-11 stsp *worktree = NULL;
302 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
303 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
304 7ac97322 2018-03-11 stsp path_got = NULL;
308 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
309 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
310 056e7441 2018-03-11 stsp path_lock = NULL;
314 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
315 6d9d28c3 2018-03-11 stsp if (fd == -1) {
316 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
317 73a5ef67 2018-03-11 stsp : got_error_from_errno());
321 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
325 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
326 6d9d28c3 2018-03-11 stsp if (errstr) {
327 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
330 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
331 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
335 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
336 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
337 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
340 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
342 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
343 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
344 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
347 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
348 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
352 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
353 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
357 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
358 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
362 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
366 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
367 eaccb85f 2018-12-25 stsp base_commit_id_str);
371 271d2a38 2018-12-25 stsp err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
375 271d2a38 2018-12-25 stsp err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
378 eaccb85f 2018-12-25 stsp got_repo_close(repo);
379 7ac97322 2018-03-11 stsp free(path_got);
380 056e7441 2018-03-11 stsp free(path_lock);
381 271d2a38 2018-12-25 stsp free(head_ref_str);
382 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
384 6d9d28c3 2018-03-11 stsp if (fd != -1)
386 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
387 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
388 6d9d28c3 2018-03-11 stsp *worktree = NULL;
390 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
392 6d9d28c3 2018-03-11 stsp return err;
395 247140b2 2019-02-05 stsp const struct got_error *
396 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
398 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
401 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
402 247140b2 2019-02-05 stsp if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
403 247140b2 2019-02-05 stsp return err;
404 247140b2 2019-02-05 stsp if (*worktree)
405 247140b2 2019-02-05 stsp return NULL;
406 247140b2 2019-02-05 stsp path = dirname(path);
407 d1542a27 2019-02-05 stsp if (path == NULL)
408 d1542a27 2019-02-05 stsp return got_error_from_errno();
409 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
411 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
415 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
417 c88eb298 2018-03-11 stsp free(worktree->root_path);
418 cde76477 2018-03-11 stsp free(worktree->repo_path);
419 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
420 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
421 271d2a38 2018-12-25 stsp if (worktree->head_ref)
422 271d2a38 2018-12-25 stsp got_ref_close(worktree->head_ref);
423 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
424 056e7441 2018-03-11 stsp close(worktree->lockfd);
425 6d9d28c3 2018-03-11 stsp free(worktree);
428 2fbdb5ae 2018-12-29 stsp const char *
429 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
431 c7f4312f 2019-02-05 stsp return worktree->root_path;
434 c7f4312f 2019-02-05 stsp const char *
435 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
437 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
440 49520a32 2018-12-29 stsp const char *
441 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
443 f609be2e 2018-12-29 stsp return worktree->path_prefix;
446 e5dc7198 2018-12-29 stsp const struct got_error *
447 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
448 e5dc7198 2018-12-29 stsp const char *path_prefix)
450 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
452 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
453 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
454 e5dc7198 2018-12-29 stsp return got_error_from_errno();
456 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
457 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
458 e5dc7198 2018-12-29 stsp free(absprefix);
459 e5dc7198 2018-12-29 stsp return NULL;
463 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
465 271d2a38 2018-12-25 stsp return got_ref_to_str(worktree->head_ref);
468 be7061eb 2018-12-30 stsp struct got_reference *
469 be7061eb 2018-12-30 stsp got_worktree_get_head_ref(struct got_worktree *worktree)
471 be7061eb 2018-12-30 stsp return got_ref_dup(worktree->head_ref);
474 b72f483a 2019-02-05 stsp struct got_object_id *
475 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
477 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
480 507dc3bb 2018-12-29 stsp const struct got_error *
481 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
482 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
484 507dc3bb 2018-12-29 stsp const struct got_error *err;
485 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
486 507dc3bb 2018-12-29 stsp char *id_str = NULL;
487 507dc3bb 2018-12-29 stsp char *path_got = NULL;
489 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
490 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
491 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
492 507dc3bb 2018-12-29 stsp path_got = NULL;
496 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
498 507dc3bb 2018-12-29 stsp return err;
500 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
501 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
505 507dc3bb 2018-12-29 stsp /* Record our base commit. */
506 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
509 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
513 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
514 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
515 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
516 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
521 507dc3bb 2018-12-29 stsp got_object_close(obj);
522 507dc3bb 2018-12-29 stsp free(id_str);
523 507dc3bb 2018-12-29 stsp free(path_got);
524 507dc3bb 2018-12-29 stsp return err;
527 9d31a1d8 2018-03-11 stsp static const struct got_error *
528 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
530 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
531 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
532 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
533 86c3caaf 2018-03-09 stsp return NULL;
536 9d31a1d8 2018-03-11 stsp static const struct got_error *
537 21908da4 2019-01-13 stsp make_parent_dirs(const char *abspath)
539 21908da4 2019-01-13 stsp const struct got_error *err = NULL;
541 21908da4 2019-01-13 stsp char *parent = dirname(abspath);
542 21908da4 2019-01-13 stsp if (parent == NULL)
543 21908da4 2019-01-13 stsp return NULL;
545 21908da4 2019-01-13 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
546 21908da4 2019-01-13 stsp if (errno == ENOENT) {
547 21908da4 2019-01-13 stsp err = make_parent_dirs(parent);
549 21908da4 2019-01-13 stsp return err;
550 c65fcef2 2019-01-13 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
551 c65fcef2 2019-01-13 stsp return got_error_from_errno();
553 c65fcef2 2019-01-13 stsp err = got_error_from_errno();
556 c65fcef2 2019-01-13 stsp return err;
559 21908da4 2019-01-13 stsp static const struct got_error *
560 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
562 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
563 4a1ddfc2 2019-01-12 stsp char *abspath;
565 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
566 4a1ddfc2 2019-01-12 stsp return got_error_from_errno();
568 4a1ddfc2 2019-01-12 stsp /* XXX queue work rather than editing disk directly? */
569 4a1ddfc2 2019-01-12 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
570 4a1ddfc2 2019-01-12 stsp struct stat sb;
572 21908da4 2019-01-13 stsp if (errno == EEXIST) {
573 21908da4 2019-01-13 stsp if (lstat(abspath, &sb) == -1) {
574 21908da4 2019-01-13 stsp err = got_error_from_errno();
578 21908da4 2019-01-13 stsp if (!S_ISDIR(sb.st_mode)) {
579 21908da4 2019-01-13 stsp /* TODO directory is obstructed; do something */
580 c65fcef2 2019-01-13 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
584 21908da4 2019-01-13 stsp return NULL;
585 c65fcef2 2019-01-13 stsp } else if (errno == ENOENT) {
586 21908da4 2019-01-13 stsp err = make_parent_dirs(abspath);
589 c65fcef2 2019-01-13 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
590 c65fcef2 2019-01-13 stsp err = got_error_from_errno();
592 4a1ddfc2 2019-01-12 stsp err = got_error_from_errno();
596 4a1ddfc2 2019-01-12 stsp free(abspath);
597 6353ad76 2019-02-08 stsp return err;
601 6353ad76 2019-02-08 stsp * Perform a 3-way merge where the file's version in the file index (blob2)
602 6353ad76 2019-02-08 stsp * acts as the common ancestor, the incoming blob (blob1) acts as the first
603 6353ad76 2019-02-08 stsp * derived version, and the file on disk acts as the second derived version.
605 6353ad76 2019-02-08 stsp static const struct got_error *
606 6353ad76 2019-02-08 stsp merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
607 6353ad76 2019-02-08 stsp struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
608 68ed9ba5 2019-02-10 stsp uint16_t mode, struct got_blob_object *blob1, struct got_repository *repo,
609 6353ad76 2019-02-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
611 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
612 6353ad76 2019-02-08 stsp int merged_fd = -1;
613 6353ad76 2019-02-08 stsp struct got_blob_object *blob2 = NULL;
614 6353ad76 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL;
615 6353ad76 2019-02-08 stsp char *blob1_path = NULL, *blob2_path = NULL;
616 6353ad76 2019-02-08 stsp char *merged_path = NULL;
617 6353ad76 2019-02-08 stsp struct got_object_id id2;
618 6353ad76 2019-02-08 stsp char *id_str = NULL;
619 6353ad76 2019-02-08 stsp char *label1 = NULL;
620 6353ad76 2019-02-08 stsp int overlapcnt = 0;
622 6353ad76 2019-02-08 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, "/tmp/got-merged");
624 6353ad76 2019-02-08 stsp return err;
625 6353ad76 2019-02-08 stsp err = got_opentemp_named(&blob1_path, &f1, "/tmp/got-merge-blob1");
628 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
632 6353ad76 2019-02-08 stsp err = got_opentemp_named(&blob2_path, &f2, "/tmp/got-merge-blob2");
636 6353ad76 2019-02-08 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
637 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
640 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
644 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
647 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
648 6353ad76 2019-02-08 stsp err = got_error_from_errno();
652 6353ad76 2019-02-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
653 6353ad76 2019-02-08 stsp blob2_path, ondisk_path, label1, path);
657 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
658 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
661 6353ad76 2019-02-08 stsp fsync(merged_fd);
663 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
664 6353ad76 2019-02-08 stsp err = got_error_from_errno();
669 02c07007 2019-02-10 stsp * Do not update timestamps of already modified files. Otherwise,
670 02c07007 2019-02-10 stsp * the status walk would treat them as unmodified files again.
672 6353ad76 2019-02-08 stsp err = got_fileindex_entry_update(ie, ondisk_path,
673 02c07007 2019-02-10 stsp blob1->id.sha1, worktree->base_commit_id->sha1, 0);
675 6353ad76 2019-02-08 stsp if (merged_fd != -1)
676 6353ad76 2019-02-08 stsp close(merged_fd);
678 6353ad76 2019-02-08 stsp fclose(f1);
680 6353ad76 2019-02-08 stsp fclose(f2);
682 6353ad76 2019-02-08 stsp got_object_blob_close(blob2);
683 6353ad76 2019-02-08 stsp free(merged_path);
684 6353ad76 2019-02-08 stsp if (blob1_path) {
685 6353ad76 2019-02-08 stsp unlink(blob1_path);
686 6353ad76 2019-02-08 stsp free(blob1_path);
688 6353ad76 2019-02-08 stsp if (blob2_path) {
689 6353ad76 2019-02-08 stsp unlink(blob2_path);
690 6353ad76 2019-02-08 stsp free(blob2_path);
692 6353ad76 2019-02-08 stsp free(id_str);
693 6353ad76 2019-02-08 stsp free(label1);
694 4a1ddfc2 2019-01-12 stsp return err;
697 4a1ddfc2 2019-01-12 stsp static const struct got_error *
698 8da9e5f4 2019-01-12 stsp install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
699 6353ad76 2019-02-08 stsp struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
700 a378724f 2019-02-10 stsp uint16_t mode, struct got_blob_object *blob, int restoring_missing_file,
701 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
702 8da9e5f4 2019-01-12 stsp void *progress_arg)
704 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
705 507dc3bb 2018-12-29 stsp int fd = -1;
706 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
707 507dc3bb 2018-12-29 stsp int update = 0;
708 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
709 68ed9ba5 2019-02-10 stsp struct stat sb;
711 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
712 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
713 9d31a1d8 2018-03-11 stsp if (fd == -1) {
714 21908da4 2019-01-13 stsp if (errno == ENOENT) {
715 21908da4 2019-01-13 stsp char *parent = dirname(path);
716 21908da4 2019-01-13 stsp if (parent == NULL)
717 21908da4 2019-01-13 stsp return got_error_from_errno();
718 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
720 21908da4 2019-01-13 stsp return err;
721 21908da4 2019-01-13 stsp fd = open(ondisk_path,
722 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
723 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
724 21908da4 2019-01-13 stsp if (fd == -1)
725 21908da4 2019-01-13 stsp return got_error_from_errno();
726 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
727 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) == -1) {
728 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
730 9d31a1d8 2018-03-11 stsp } else if (!S_ISREG(sb.st_mode)) {
731 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
732 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
735 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
736 507dc3bb 2018-12-29 stsp ondisk_path);
739 507dc3bb 2018-12-29 stsp update = 1;
742 7e7c1e4c 2019-01-12 stsp return got_error_from_errno();
745 a378724f 2019-02-10 stsp if (restoring_missing_file)
746 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
748 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
749 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
751 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
753 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
754 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
757 9d31a1d8 2018-03-11 stsp if (len > 0) {
758 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
759 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
760 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
761 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
763 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
764 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
767 61d6eaa3 2018-12-24 stsp hdrlen = 0;
769 9d31a1d8 2018-03-11 stsp } while (len != 0);
773 507dc3bb 2018-12-29 stsp if (update) {
774 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
775 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
779 ba8a0d4d 2019-02-10 stsp /* In case of an update stat buf has been loaded above. */
780 ba8a0d4d 2019-02-10 stsp if (lstat(ondisk_path, &sb) == -1) {
781 ba8a0d4d 2019-02-10 stsp err = got_error_from_errno();
786 ba8a0d4d 2019-02-10 stsp if (mode & S_IXUSR) {
787 ba8a0d4d 2019-02-10 stsp if (chmod(ondisk_path, sb.st_mode | S_IXUSR) == -1) {
788 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
792 ba8a0d4d 2019-02-10 stsp if (chmod(ondisk_path, sb.st_mode & ~S_IXUSR) == -1) {
793 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
798 5f9fef37 2019-01-13 stsp if (entry == NULL)
799 5f9fef37 2019-01-13 stsp entry = got_fileindex_entry_get(fileindex, path);
801 51514078 2018-12-25 stsp err = got_fileindex_entry_update(entry, ondisk_path,
802 02c07007 2019-02-10 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
804 51514078 2018-12-25 stsp err = got_fileindex_entry_alloc(&entry, ondisk_path,
805 8da9e5f4 2019-01-12 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
808 51514078 2018-12-25 stsp err = got_fileindex_entry_add(fileindex, entry);
811 507dc3bb 2018-12-29 stsp if (fd != -1)
813 507dc3bb 2018-12-29 stsp free(tmppath);
814 6353ad76 2019-02-08 stsp return err;
817 6353ad76 2019-02-08 stsp static const struct got_error *
818 6353ad76 2019-02-08 stsp get_file_status(unsigned char *status, struct got_fileindex_entry *ie,
819 6353ad76 2019-02-08 stsp const char *abspath, struct got_repository *repo)
821 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
822 6353ad76 2019-02-08 stsp struct got_object_id id;
823 6353ad76 2019-02-08 stsp size_t hdrlen;
824 6353ad76 2019-02-08 stsp FILE *f = NULL;
825 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
826 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
827 6353ad76 2019-02-08 stsp size_t flen, blen;
828 6353ad76 2019-02-08 stsp struct stat sb;
830 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
832 a378724f 2019-02-10 stsp if (lstat(abspath, &sb) == -1) {
833 a378724f 2019-02-10 stsp if (errno == ENOENT) {
834 a378724f 2019-02-10 stsp *status = GOT_STATUS_MISSING;
835 a378724f 2019-02-10 stsp return NULL;
837 6353ad76 2019-02-08 stsp return got_error_from_errno();
840 6353ad76 2019-02-08 stsp if (!S_ISREG(sb.st_mode)) {
841 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
842 6353ad76 2019-02-08 stsp return NULL;
845 6353ad76 2019-02-08 stsp if (ie->ctime_sec == sb.st_ctime &&
846 6353ad76 2019-02-08 stsp ie->ctime_nsec == sb.st_ctimensec &&
847 6353ad76 2019-02-08 stsp ie->mtime_sec == sb.st_mtime &&
848 6353ad76 2019-02-08 stsp ie->mtime_sec == sb.st_mtime &&
849 6353ad76 2019-02-08 stsp ie->mtime_nsec == sb.st_mtimensec &&
850 6353ad76 2019-02-08 stsp ie->size == (sb.st_size & 0xffffffff))
851 6353ad76 2019-02-08 stsp return NULL;
853 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
854 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
856 6353ad76 2019-02-08 stsp return err;
858 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
859 6353ad76 2019-02-08 stsp if (f == NULL) {
860 6353ad76 2019-02-08 stsp err = got_error_from_errno();
863 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
864 6353ad76 2019-02-08 stsp while (1) {
865 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
866 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
869 6353ad76 2019-02-08 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
870 6353ad76 2019-02-08 stsp if (blen == 0) {
871 6353ad76 2019-02-08 stsp if (flen != 0)
872 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
874 6353ad76 2019-02-08 stsp } else if (flen == 0) {
875 6353ad76 2019-02-08 stsp if (blen != 0)
876 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
878 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
879 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
880 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
881 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
885 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
888 6353ad76 2019-02-08 stsp hdrlen = 0;
892 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
895 9d31a1d8 2018-03-11 stsp return err;
898 9d31a1d8 2018-03-11 stsp static const struct got_error *
899 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
900 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
901 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
902 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
903 8da9e5f4 2019-01-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
905 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
906 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
907 6353ad76 2019-02-08 stsp char *ondisk_path;
908 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
910 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
911 6353ad76 2019-02-08 stsp return got_error_from_errno();
914 6353ad76 2019-02-08 stsp err = get_file_status(&status, ie, ondisk_path, repo);
918 6353ad76 2019-02-08 stsp if (status == GOT_STATUS_OBSTRUCTED) {
919 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg, status, path);
923 a378724f 2019-02-10 stsp if (status == GOT_STATUS_NO_CHANGE) {
924 a378724f 2019-02-10 stsp if (memcmp(ie->commit_sha1,
925 a378724f 2019-02-10 stsp worktree->base_commit_id->sha1,
926 a378724f 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
927 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
931 a378724f 2019-02-10 stsp if (memcmp(ie->blob_sha1,
932 a378724f 2019-02-10 stsp te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
937 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
941 276262e8 2019-02-08 stsp if (status == GOT_STATUS_MODIFY)
942 6353ad76 2019-02-08 stsp err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
943 68ed9ba5 2019-02-10 stsp te->mode, blob, repo, progress_cb, progress_arg);
945 6353ad76 2019-02-08 stsp err = install_blob(worktree, fileindex, ie, ondisk_path, path,
946 a378724f 2019-02-10 stsp te->mode, blob, status == GOT_STATUS_MISSING, repo,
947 a378724f 2019-02-10 stsp progress_cb, progress_arg);
949 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
951 6353ad76 2019-02-08 stsp free(ondisk_path);
952 8da9e5f4 2019-01-12 stsp return err;
955 90285c3b 2019-01-08 stsp static const struct got_error *
956 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
958 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
959 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
961 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
962 90285c3b 2019-01-08 stsp return got_error_from_errno();
964 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
965 c97665ae 2019-01-13 stsp if (errno != ENOENT)
966 c97665ae 2019-01-13 stsp err = got_error_from_errno();
968 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
969 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
970 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
971 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
972 26c4ac4d 2019-01-08 stsp err = got_error_from_errno();
975 90285c3b 2019-01-08 stsp parent = dirname(parent);
978 90285c3b 2019-01-08 stsp free(ondisk_path);
979 90285c3b 2019-01-08 stsp return err;
982 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
983 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
984 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
985 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
986 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
987 8da9e5f4 2019-01-12 stsp void *progress_arg;
988 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
989 8da9e5f4 2019-01-12 stsp void *cancel_arg;
992 512f0d0e 2019-01-02 stsp static const struct got_error *
993 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
994 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
996 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
998 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
999 8da9e5f4 2019-01-12 stsp ie->path, a->repo, a->progress_cb, a->progress_arg,
1000 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1003 8da9e5f4 2019-01-12 stsp static const struct got_error *
1004 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1006 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1007 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1009 8da9e5f4 2019-01-12 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1011 8da9e5f4 2019-01-12 stsp err = remove_ondisk_file(a->worktree->root_path, ie->path);
1013 8da9e5f4 2019-01-12 stsp return err;
1014 8da9e5f4 2019-01-12 stsp got_fileindex_entry_remove(a->fileindex, ie);
1015 8da9e5f4 2019-01-12 stsp return NULL;
1018 9d31a1d8 2018-03-11 stsp static const struct got_error *
1019 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1021 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1022 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1023 8da9e5f4 2019-01-12 stsp char *path;
1025 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1026 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1028 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
1030 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1031 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1033 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1034 8da9e5f4 2019-01-12 stsp a->repo, a->progress_cb, a->progress_arg,
1035 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1037 8da9e5f4 2019-01-12 stsp free(path);
1038 9d31a1d8 2018-03-11 stsp return err;
1041 9d31a1d8 2018-03-11 stsp const struct got_error *
1042 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1043 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1044 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1046 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1047 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1048 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1049 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1050 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1051 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1052 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
1053 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1054 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1056 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1058 9d31a1d8 2018-03-11 stsp return err;
1060 133d2798 2019-01-08 stsp fileindex = got_fileindex_alloc();
1061 133d2798 2019-01-08 stsp if (fileindex == NULL) {
1062 133d2798 2019-01-08 stsp err = got_error_from_errno();
1063 9d31a1d8 2018-03-11 stsp goto done;
1066 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1067 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1068 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1069 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
1070 9d31a1d8 2018-03-11 stsp goto done;
1074 51514078 2018-12-25 stsp * Read the file index.
1075 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1076 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1078 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
1079 51514078 2018-12-25 stsp if (index == NULL) {
1080 cf61d818 2019-01-12 stsp if (errno != ENOENT) {
1081 cf61d818 2019-01-12 stsp err = got_error_from_errno();
1082 cf61d818 2019-01-12 stsp goto done;
1085 cf61d818 2019-01-12 stsp err = got_fileindex_read(fileindex, index);
1086 cf61d818 2019-01-12 stsp fclose(index);
1088 cf61d818 2019-01-12 stsp goto done;
1091 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1092 b8bdcc21 2018-12-24 stsp fileindex_path);
1094 51664889 2018-03-12 stsp goto done;
1096 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1097 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1099 9d31a1d8 2018-03-11 stsp goto done;
1101 8da9e5f4 2019-01-12 stsp err = got_object_id_by_path(&tree_id, repo,
1102 8da9e5f4 2019-01-12 stsp worktree->base_commit_id, worktree->path_prefix);
1104 9d31a1d8 2018-03-11 stsp goto done;
1106 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1108 8da9e5f4 2019-01-12 stsp goto done;
1110 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1111 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1112 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1113 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1114 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1115 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1116 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1117 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1118 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1119 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1120 8da9e5f4 2019-01-12 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1121 8da9e5f4 2019-01-12 stsp &diff_cb, &arg);
1123 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
1124 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
1126 9d31a1d8 2018-03-11 stsp goto done;
1128 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1129 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
1130 9d31a1d8 2018-03-11 stsp goto done;
1133 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1134 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
1138 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1139 9d31a1d8 2018-03-11 stsp if (commit)
1140 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1141 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
1142 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
1143 b8bdcc21 2018-12-24 stsp if (new_index)
1144 b8bdcc21 2018-12-24 stsp fclose(new_index);
1145 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1146 9d31a1d8 2018-03-11 stsp free(fileindex_path);
1147 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1148 a143fb78 2018-12-25 stsp if (checkout_err)
1149 a143fb78 2018-12-25 stsp err = checkout_err;
1150 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1151 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1152 9d31a1d8 2018-03-11 stsp err = unlockerr;
1153 f8d1f275 2019-02-04 stsp return err;
1156 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1157 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1158 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1159 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1160 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1161 f8d1f275 2019-02-04 stsp void *status_arg;
1162 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1163 f8d1f275 2019-02-04 stsp void *cancel_arg;
1166 f8d1f275 2019-02-04 stsp static const struct got_error *
1167 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1168 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1170 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1171 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1172 f8d1f275 2019-02-04 stsp char *abspath;
1173 f8d1f275 2019-02-04 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1175 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1176 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1177 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1178 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1180 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1181 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1182 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1185 bf96b38c 2019-02-05 stsp err = get_file_status(&status, ie, abspath, a->repo);
1186 b72f483a 2019-02-05 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1187 b72f483a 2019-02-05 stsp struct got_object_id id;
1188 b72f483a 2019-02-05 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1189 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, status, ie->path, &id);
1191 f8d1f275 2019-02-04 stsp free(abspath);
1192 9d31a1d8 2018-03-11 stsp return err;
1195 f8d1f275 2019-02-04 stsp static const struct got_error *
1196 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1198 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1199 b72f483a 2019-02-05 stsp struct got_object_id id;
1200 b72f483a 2019-02-05 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1201 b72f483a 2019-02-05 stsp return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1205 f8d1f275 2019-02-04 stsp static const struct got_error *
1206 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1208 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1209 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1210 f8d1f275 2019-02-04 stsp char *path = NULL;
1212 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1213 f8d1f275 2019-02-04 stsp return NULL;
1215 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1216 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1217 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1219 f8d1f275 2019-02-04 stsp path = de->d_name;
1222 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1224 f8d1f275 2019-02-04 stsp if (parent_path[0])
1225 f8d1f275 2019-02-04 stsp free(path);
1226 b72f483a 2019-02-05 stsp return err;
1229 f8d1f275 2019-02-04 stsp const struct got_error *
1230 f8d1f275 2019-02-04 stsp got_worktree_status(struct got_worktree *worktree,
1231 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1232 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1234 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1235 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1236 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1237 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1238 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1239 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1240 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1242 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1243 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1244 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1245 f8d1f275 2019-02-04 stsp goto done;
1248 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1249 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1250 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1251 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1252 f8d1f275 2019-02-04 stsp goto done;
1255 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1256 f8d1f275 2019-02-04 stsp if (index == NULL) {
1257 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1258 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1259 f8d1f275 2019-02-04 stsp goto done;
1262 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1263 f8d1f275 2019-02-04 stsp fclose(index);
1265 f8d1f275 2019-02-04 stsp goto done;
1268 f8d1f275 2019-02-04 stsp workdir = opendir(worktree->root_path);
1269 c513d110 2019-02-05 stsp if (workdir == NULL) {
1270 c513d110 2019-02-05 stsp err = got_error_from_errno();
1271 c513d110 2019-02-05 stsp goto done;
1273 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1274 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1275 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1276 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1277 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1278 f8d1f275 2019-02-04 stsp arg.repo = repo;
1279 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1280 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1281 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1282 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1283 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1284 c7f4312f 2019-02-05 stsp repo, &fdiff_cb, &arg);
1286 f8d1f275 2019-02-04 stsp if (workdir)
1287 f8d1f275 2019-02-04 stsp closedir(workdir);
1288 f8d1f275 2019-02-04 stsp free(fileindex_path);
1289 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1290 f8d1f275 2019-02-04 stsp return err;