Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/limits.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
22 #include <dirent.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
34 #include <uuid.h>
35 #include <util.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
44 #include "got_lib_worktree.h"
45 #include "got_lib_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_diff.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 static const struct got_error *
58 create_meta_file(const char *path_got, const char *name, const char *content)
59 {
60 const struct got_error *err = NULL;
61 char *path;
62 int fd = -1;
64 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
65 err = got_error_from_errno();
66 path = NULL;
67 goto done;
68 }
70 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
71 GOT_DEFAULT_FILE_MODE);
72 if (fd == -1) {
73 err = got_error_from_errno();
74 goto done;
75 }
77 if (content) {
78 int len = dprintf(fd, "%s\n", content);
79 if (len != strlen(content) + 1) {
80 err = got_error_from_errno();
81 goto done;
82 }
83 }
85 done:
86 if (fd != -1 && close(fd) == -1 && err == NULL)
87 err = got_error_from_errno();
88 free(path);
89 return err;
90 }
92 static const struct got_error *
93 update_meta_file(const char *path_got, const char *name, const char *content)
94 {
95 const struct got_error *err = NULL;
96 FILE *tmpfile = NULL;
97 char *tmppath = NULL;
98 char *path = NULL;
100 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 err = got_error_from_errno();
102 path = NULL;
103 goto done;
106 err = got_opentemp_named(&tmppath, &tmpfile, path);
107 if (err)
108 goto done;
110 if (content) {
111 int len = fprintf(tmpfile, "%s\n", content);
112 if (len != strlen(content) + 1) {
113 err = got_error_from_errno();
114 goto done;
118 if (rename(tmppath, path) != 0) {
119 err = got_error_from_errno();
120 unlink(tmppath);
121 goto done;
124 done:
125 free(tmppath);
126 if (fclose(tmpfile) != 0 && err == NULL)
127 err = got_error_from_errno();
128 return err;
131 static const struct got_error *
132 read_meta_file(char **content, const char *path_got, const char *name)
134 const struct got_error *err = NULL;
135 char *path;
136 int fd = -1;
137 ssize_t n;
138 struct stat sb;
140 *content = NULL;
142 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
143 err = got_error_from_errno();
144 path = NULL;
145 goto done;
148 fd = open(path, O_RDONLY | O_NOFOLLOW);
149 if (fd == -1) {
150 if (errno == ENOENT)
151 err = got_error(GOT_ERR_WORKTREE_META);
152 else
153 err = got_error_from_errno();
154 goto done;
156 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
157 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
158 : got_error_from_errno());
159 goto done;
162 if (lstat(path, &sb) != 0) {
163 err = got_error_from_errno();
164 goto done;
166 *content = calloc(1, sb.st_size);
167 if (*content == NULL) {
168 err = got_error_from_errno();
169 goto done;
172 n = read(fd, *content, sb.st_size);
173 if (n != sb.st_size) {
174 err = (n == -1 ? got_error_from_errno() :
175 got_error(GOT_ERR_WORKTREE_META));
176 goto done;
178 if ((*content)[sb.st_size - 1] != '\n') {
179 err = got_error(GOT_ERR_WORKTREE_META);
180 goto done;
182 (*content)[sb.st_size - 1] = '\0';
184 done:
185 if (fd != -1 && close(fd) == -1 && err == NULL)
186 err = got_error_from_errno();
187 free(path);
188 if (err) {
189 free(*content);
190 *content = NULL;
192 return err;
195 const struct got_error *
196 got_worktree_init(const char *path, struct got_reference *head_ref,
197 const char *prefix, struct got_repository *repo)
199 const struct got_error *err = NULL;
200 struct got_object_id *commit_id = NULL;
201 uuid_t uuid;
202 uint32_t uuid_status;
203 int obj_type;
204 char *path_got = NULL;
205 char *refstr = NULL;
206 char *formatstr = NULL;
207 char *absprefix = NULL;
208 char *basestr = NULL;
209 char *uuidstr = NULL;
211 if (strcmp(path, got_repo_get_path(repo)) == 0) {
212 err = got_error(GOT_ERR_WORKTREE_REPO);
213 goto done;
216 err = got_ref_resolve(&commit_id, repo, head_ref);
217 if (err)
218 return err;
219 err = got_object_get_type(&obj_type, repo, commit_id);
220 if (err)
221 return err;
222 if (obj_type != GOT_OBJ_TYPE_COMMIT)
223 return got_error(GOT_ERR_OBJ_TYPE);
225 if (!got_path_is_absolute(prefix)) {
226 if (asprintf(&absprefix, "/%s", prefix) == -1)
227 return got_error_from_errno();
230 /* Create top-level directory (may already exist). */
231 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
232 err = got_error_from_errno();
233 goto done;
236 /* Create .got directory (may already exist). */
237 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
238 err = got_error_from_errno();
239 goto done;
241 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
242 err = got_error_from_errno();
243 goto done;
246 /* Create an empty lock file. */
247 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
248 if (err)
249 goto done;
251 /* Create an empty file index. */
252 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
253 if (err)
254 goto done;
256 /* Write the HEAD reference. */
257 refstr = got_ref_to_str(head_ref);
258 if (refstr == NULL) {
259 err = got_error_from_errno();
260 goto done;
262 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
263 if (err)
264 goto done;
266 /* Record our base commit. */
267 err = got_object_id_str(&basestr, commit_id);
268 if (err)
269 goto done;
270 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 if (err)
272 goto done;
274 /* Store path to repository. */
275 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
276 got_repo_get_path(repo));
277 if (err)
278 goto done;
280 /* Store in-repository path prefix. */
281 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
282 absprefix ? absprefix : prefix);
283 if (err)
284 goto done;
286 /* Generate UUID. */
287 uuid_create(&uuid, &uuid_status);
288 if (uuid_status != uuid_s_ok) {
289 err = got_error_uuid(uuid_status);
290 goto done;
292 uuid_to_string(&uuid, &uuidstr, &uuid_status);
293 if (uuid_status != uuid_s_ok) {
294 err = got_error_uuid(uuid_status);
295 goto done;
297 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 if (err)
299 goto done;
301 /* Stamp work tree with format file. */
302 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
303 err = got_error_from_errno();
304 goto done;
306 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
307 if (err)
308 goto done;
310 done:
311 free(commit_id);
312 free(path_got);
313 free(formatstr);
314 free(refstr);
315 free(absprefix);
316 free(basestr);
317 free(uuidstr);
318 return err;
321 static const struct got_error *
322 open_worktree(struct got_worktree **worktree, const char *path)
324 const struct got_error *err = NULL;
325 char *path_got;
326 char *formatstr = NULL;
327 char *uuidstr = NULL;
328 char *path_lock = NULL;
329 char *base_commit_id_str = NULL;
330 char *head_ref_str = NULL;
331 int version, fd = -1;
332 const char *errstr;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
336 *worktree = NULL;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno();
340 path_got = NULL;
341 goto done;
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno();
346 path_lock = NULL;
347 goto done;
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 if (fd == -1) {
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno());
354 goto done;
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
358 if (err)
359 goto done;
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 if (errstr) {
363 err = got_error(GOT_ERR_WORKTREE_META);
364 goto done;
366 if (version != GOT_WORKTREE_FORMAT_VERSION) {
367 err = got_error(GOT_ERR_WORKTREE_VERS);
368 goto done;
371 *worktree = calloc(1, sizeof(**worktree));
372 if (*worktree == NULL) {
373 err = got_error_from_errno();
374 goto done;
376 (*worktree)->lockfd = -1;
378 (*worktree)->root_path = strdup(path);
379 if ((*worktree)->root_path == NULL) {
380 err = got_error_from_errno();
381 goto done;
383 err = read_meta_file(&(*worktree)->repo_path, path_got,
384 GOT_WORKTREE_REPOSITORY);
385 if (err)
386 goto done;
388 err = read_meta_file(&(*worktree)->path_prefix, path_got,
389 GOT_WORKTREE_PATH_PREFIX);
390 if (err)
391 goto done;
393 err = read_meta_file(&base_commit_id_str, path_got,
394 GOT_WORKTREE_BASE_COMMIT);
395 if (err)
396 goto done;
398 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
399 if (err)
400 goto done;
401 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
402 if (uuid_status != uuid_s_ok) {
403 err = got_error_uuid(uuid_status);
404 goto done;
407 err = got_repo_open(&repo, (*worktree)->repo_path);
408 if (err)
409 goto done;
411 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
412 base_commit_id_str);
413 if (err)
414 goto done;
416 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
417 if (err)
418 goto done;
420 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
421 done:
422 if (repo)
423 got_repo_close(repo);
424 free(path_got);
425 free(path_lock);
426 free(head_ref_str);
427 free(base_commit_id_str);
428 free(uuidstr);
429 free(formatstr);
430 if (err) {
431 if (fd != -1)
432 close(fd);
433 if (*worktree != NULL)
434 got_worktree_close(*worktree);
435 *worktree = NULL;
436 } else
437 (*worktree)->lockfd = fd;
439 return err;
442 const struct got_error *
443 got_worktree_open(struct got_worktree **worktree, const char *path)
445 const struct got_error *err = NULL;
447 do {
448 err = open_worktree(worktree, path);
449 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 return err;
451 if (*worktree)
452 return NULL;
453 path = dirname(path);
454 if (path == NULL)
455 return got_error_from_errno();
456 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
458 return got_error(GOT_ERR_NOT_WORKTREE);
461 const struct got_error *
462 got_worktree_close(struct got_worktree *worktree)
464 const struct got_error *err = NULL;
465 free(worktree->root_path);
466 free(worktree->repo_path);
467 free(worktree->path_prefix);
468 free(worktree->base_commit_id);
469 if (worktree->head_ref)
470 got_ref_close(worktree->head_ref);
471 if (worktree->lockfd != -1)
472 if (close(worktree->lockfd) != 0)
473 err = got_error_from_errno();
474 free(worktree);
475 return err;
478 const char *
479 got_worktree_get_root_path(struct got_worktree *worktree)
481 return worktree->root_path;
484 const char *
485 got_worktree_get_repo_path(struct got_worktree *worktree)
487 return worktree->repo_path;
490 const char *
491 got_worktree_get_path_prefix(struct got_worktree *worktree)
493 return worktree->path_prefix;
496 const struct got_error *
497 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
498 const char *path_prefix)
500 char *absprefix = NULL;
502 if (!got_path_is_absolute(path_prefix)) {
503 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
504 return got_error_from_errno();
506 *match = (strcmp(absprefix ? absprefix : path_prefix,
507 worktree->path_prefix) == 0);
508 free(absprefix);
509 return NULL;
512 char *
513 got_worktree_get_head_ref_name(struct got_worktree *worktree)
515 return got_ref_to_str(worktree->head_ref);
518 struct got_reference *
519 got_worktree_get_head_ref(struct got_worktree *worktree)
521 return got_ref_dup(worktree->head_ref);
524 struct got_object_id *
525 got_worktree_get_base_commit_id(struct got_worktree *worktree)
527 return worktree->base_commit_id;
530 const struct got_error *
531 got_worktree_set_base_commit_id(struct got_worktree *worktree,
532 struct got_repository *repo, struct got_object_id *commit_id)
534 const struct got_error *err;
535 struct got_object *obj = NULL;
536 char *id_str = NULL;
537 char *path_got = NULL;
539 if (asprintf(&path_got, "%s/%s", worktree->root_path,
540 GOT_WORKTREE_GOT_DIR) == -1) {
541 err = got_error_from_errno();
542 path_got = NULL;
543 goto done;
546 err = got_object_open(&obj, repo, commit_id);
547 if (err)
548 return err;
550 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
551 err = got_error(GOT_ERR_OBJ_TYPE);
552 goto done;
555 /* Record our base commit. */
556 err = got_object_id_str(&id_str, commit_id);
557 if (err)
558 goto done;
559 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
560 if (err)
561 goto done;
563 free(worktree->base_commit_id);
564 worktree->base_commit_id = got_object_id_dup(commit_id);
565 if (worktree->base_commit_id == NULL) {
566 err = got_error_from_errno();
567 goto done;
569 done:
570 if (obj)
571 got_object_close(obj);
572 free(id_str);
573 free(path_got);
574 return err;
577 static const struct got_error *
578 lock_worktree(struct got_worktree *worktree, int operation)
580 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
581 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
582 : got_error_from_errno());
583 return NULL;
586 static const struct got_error *
587 add_dir_on_disk(struct got_worktree *worktree, const char *path)
589 const struct got_error *err = NULL;
590 char *abspath;
592 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
593 return got_error_from_errno();
595 err = got_path_mkdir(abspath);
596 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
597 struct stat sb;
598 err = NULL;
599 if (lstat(abspath, &sb) == -1) {
600 err = got_error_from_errno();
601 } else if (!S_ISDIR(sb.st_mode)) {
602 /* TODO directory is obstructed; do something */
603 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
606 free(abspath);
607 return err;
610 static const struct got_error *
611 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
613 const struct got_error *err = NULL;
614 uint8_t fbuf1[8192];
615 uint8_t fbuf2[8192];
616 size_t flen1 = 0, flen2 = 0;
618 *same = 1;
620 while (1) {
621 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
622 if (flen1 == 0 && ferror(f1)) {
623 err = got_error_from_errno();
624 break;
626 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
627 if (flen2 == 0 && ferror(f2)) {
628 err = got_error_from_errno();
629 break;
631 if (flen1 == 0) {
632 if (flen2 != 0)
633 *same = 0;
634 break;
635 } else if (flen2 == 0) {
636 if (flen1 != 0)
637 *same = 0;
638 break;
639 } else if (flen1 == flen2) {
640 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
641 *same = 0;
642 break;
644 } else {
645 *same = 0;
646 break;
650 return err;
653 static const struct got_error *
654 check_files_equal(int *same, const char *f1_path, const char *f2_path)
656 const struct got_error *err = NULL;
657 struct stat sb;
658 size_t size1, size2;
659 FILE *f1 = NULL, *f2 = NULL;
661 *same = 1;
663 if (lstat(f1_path, &sb) != 0) {
664 err = got_error_from_errno();
665 goto done;
667 size1 = sb.st_size;
669 if (lstat(f2_path, &sb) != 0) {
670 err = got_error_from_errno();
671 goto done;
673 size2 = sb.st_size;
675 if (size1 != size2) {
676 *same = 0;
677 return NULL;
680 f1 = fopen(f1_path, "r");
681 if (f1 == NULL)
682 return got_error_from_errno();
684 f2 = fopen(f2_path, "r");
685 if (f2 == NULL) {
686 err = got_error_from_errno();
687 goto done;
690 err = check_file_contents_equal(same, f1, f2);
691 done:
692 if (f1 && fclose(f1) != 0 && err == NULL)
693 err = got_error_from_errno();
694 if (f2 && fclose(f2) != 0 && err == NULL)
695 err = got_error_from_errno();
697 return err;
700 /*
701 * Perform a 3-way merge where the file's version in the file index (blob2)
702 * acts as the common ancestor, the incoming blob (blob1) acts as the first
703 * derived version, and the file on disk acts as the second derived version.
704 */
705 static const struct got_error *
706 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
707 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
708 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
709 struct got_repository *repo,
710 got_worktree_checkout_cb progress_cb, void *progress_arg)
712 const struct got_error *err = NULL;
713 int merged_fd = -1;
714 struct got_blob_object *blob2 = NULL;
715 FILE *f1 = NULL, *f2 = NULL;
716 char *blob1_path = NULL, *blob2_path = NULL;
717 char *merged_path = NULL, *base_path = NULL;
718 char *id_str = NULL;
719 char *label1 = NULL;
720 int overlapcnt = 0, update_timestamps = 0;
721 char *parent;
723 parent = dirname(ondisk_path);
724 if (parent == NULL)
725 return got_error_from_errno();
727 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
728 return got_error_from_errno();
730 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
731 if (err)
732 goto done;
734 free(base_path);
735 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
736 err = got_error_from_errno();
737 base_path = NULL;
738 goto done;
741 err = got_opentemp_named(&blob1_path, &f1, base_path);
742 if (err)
743 goto done;
744 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
745 if (err)
746 goto done;
748 free(base_path);
749 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
750 err = got_error_from_errno();
751 base_path = NULL;
752 goto done;
755 err = got_opentemp_named(&blob2_path, &f2, base_path);
756 if (err)
757 goto done;
758 if (got_fileindex_entry_has_blob(ie)) {
759 struct got_object_id id2;
760 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
761 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
762 if (err)
763 goto done;
764 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
765 if (err)
766 goto done;
767 } else {
768 /*
769 * If the file has no blob, this is an "add vs add" conflict,
770 * and we simply use an empty ancestor file to make both files
771 * appear in the merged result in their entirety.
772 */
775 err = got_object_id_str(&id_str, worktree->base_commit_id);
776 if (err)
777 goto done;
778 if (asprintf(&label1, "commit %s", id_str) == -1) {
779 err = got_error_from_errno();
780 goto done;
783 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
784 blob2_path, ondisk_path, label1, path);
785 if (err)
786 goto done;
788 (*progress_cb)(progress_arg,
789 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno();
794 goto done;
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(&update_timestamps, blob1_path,
800 merged_path);
801 if (err)
802 goto done;
805 if (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno();
807 goto done;
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno();
812 unlink(merged_path);
813 goto done;
816 /*
817 * Do not update timestamps of already modified files. Otherwise,
818 * a future status walk would treat them as unmodified files again.
819 */
820 err = got_fileindex_entry_update(ie, ondisk_path,
821 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
822 done:
823 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
824 err = got_error_from_errno();
825 if (f1 && fclose(f1) != 0 && err == NULL)
826 err = got_error_from_errno();
827 if (f2 && fclose(f2) != 0 && err == NULL)
828 err = got_error_from_errno();
829 if (blob2)
830 got_object_blob_close(blob2);
831 free(merged_path);
832 free(base_path);
833 if (blob1_path) {
834 unlink(blob1_path);
835 free(blob1_path);
837 if (blob2_path) {
838 unlink(blob2_path);
839 free(blob2_path);
841 free(id_str);
842 free(label1);
843 return err;
846 static const struct got_error *
847 update_blob_fileindex_entry(struct got_worktree *worktree,
848 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
849 const char *ondisk_path, const char *path, struct got_blob_object *blob,
850 int update_timestamps)
852 const struct got_error *err = NULL;
854 if (ie == NULL)
855 ie = got_fileindex_entry_get(fileindex, path);
856 if (ie)
857 err = got_fileindex_entry_update(ie, ondisk_path,
858 blob->id.sha1, worktree->base_commit_id->sha1,
859 update_timestamps);
860 else {
861 struct got_fileindex_entry *new_ie;
862 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
863 path, blob->id.sha1, worktree->base_commit_id->sha1);
864 if (!err)
865 err = got_fileindex_entry_add(fileindex, new_ie);
867 return err;
870 static const struct got_error *
871 install_blob(struct got_worktree *worktree, const char *ondisk_path,
872 const char *path, uint16_t te_mode, uint16_t st_mode,
873 struct got_blob_object *blob, int restoring_missing_file,
874 int reverting_versioned_file, struct got_repository *repo,
875 got_worktree_checkout_cb progress_cb, void *progress_arg)
877 const struct got_error *err = NULL;
878 int fd = -1;
879 size_t len, hdrlen;
880 int update = 0;
881 char *tmppath = NULL;
883 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
884 GOT_DEFAULT_FILE_MODE);
885 if (fd == -1) {
886 if (errno == ENOENT) {
887 char *parent = dirname(path);
888 if (parent == NULL)
889 return got_error_from_errno();
890 err = add_dir_on_disk(worktree, parent);
891 if (err)
892 return err;
893 fd = open(ondisk_path,
894 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
895 GOT_DEFAULT_FILE_MODE);
896 if (fd == -1)
897 return got_error_from_errno();
898 } else if (errno == EEXIST) {
899 if (!S_ISREG(st_mode)) {
900 /* TODO file is obstructed; do something */
901 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
902 goto done;
903 } else {
904 err = got_opentemp_named_fd(&tmppath, &fd,
905 ondisk_path);
906 if (err)
907 goto done;
908 update = 1;
910 } else
911 return got_error_from_errno();
914 if (restoring_missing_file)
915 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
916 else if (reverting_versioned_file)
917 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
918 else
919 (*progress_cb)(progress_arg,
920 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
922 hdrlen = got_object_blob_get_hdrlen(blob);
923 do {
924 const uint8_t *buf = got_object_blob_get_read_buf(blob);
925 err = got_object_blob_read_block(&len, blob);
926 if (err)
927 break;
928 if (len > 0) {
929 /* Skip blob object header first time around. */
930 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
931 if (outlen == -1) {
932 err = got_error_from_errno();
933 goto done;
934 } else if (outlen != len - hdrlen) {
935 err = got_error(GOT_ERR_IO);
936 goto done;
938 hdrlen = 0;
940 } while (len != 0);
942 if (fsync(fd) != 0) {
943 err = got_error_from_errno();
944 goto done;
947 if (update) {
948 if (rename(tmppath, ondisk_path) != 0) {
949 err = got_error_from_errno();
950 unlink(tmppath);
951 goto done;
955 if (te_mode & S_IXUSR) {
956 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
957 err = got_error_from_errno();
958 goto done;
960 } else {
961 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
962 err = got_error_from_errno();
963 goto done;
967 done:
968 if (fd != -1 && close(fd) != 0 && err == NULL)
969 err = got_error_from_errno();
970 free(tmppath);
971 return err;
974 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
975 static const struct got_error *
976 get_modified_file_content_status(unsigned char *status, FILE *f)
978 const struct got_error *err = NULL;
979 const char *markers[3] = {
980 GOT_DIFF_CONFLICT_MARKER_BEGIN,
981 GOT_DIFF_CONFLICT_MARKER_SEP,
982 GOT_DIFF_CONFLICT_MARKER_END
983 };
984 int i = 0;
985 char *line;
986 size_t len;
987 const char delim[3] = {'\0', '\0', '\0'};
989 while (*status == GOT_STATUS_MODIFY) {
990 line = fparseln(f, &len, NULL, delim, 0);
991 if (line == NULL) {
992 if (feof(f))
993 break;
994 err = got_ferror(f, GOT_ERR_IO);
995 break;
998 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
999 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
1000 *status = GOT_STATUS_CONFLICT;
1001 else
1002 i++;
1006 return err;
1009 static const struct got_error *
1010 get_file_status(unsigned char *status, struct stat *sb,
1011 struct got_fileindex_entry *ie, const char *abspath,
1012 struct got_repository *repo)
1014 const struct got_error *err = NULL;
1015 struct got_object_id id;
1016 size_t hdrlen;
1017 FILE *f = NULL;
1018 uint8_t fbuf[8192];
1019 struct got_blob_object *blob = NULL;
1020 size_t flen, blen;
1022 *status = GOT_STATUS_NO_CHANGE;
1024 if (lstat(abspath, sb) == -1) {
1025 if (errno == ENOENT) {
1026 if (ie) {
1027 if (got_fileindex_entry_has_file_on_disk(ie))
1028 *status = GOT_STATUS_MISSING;
1029 else
1030 *status = GOT_STATUS_DELETE;
1031 sb->st_mode =
1032 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1033 & (S_IRWXU | S_IRWXG | S_IRWXO));
1034 } else
1035 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1036 return NULL;
1038 return got_error_from_errno();
1041 if (!S_ISREG(sb->st_mode)) {
1042 *status = GOT_STATUS_OBSTRUCTED;
1043 return NULL;
1046 if (ie == NULL)
1047 return NULL;
1049 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1050 *status = GOT_STATUS_DELETE;
1051 return NULL;
1052 } else if (!got_fileindex_entry_has_blob(ie)) {
1053 *status = GOT_STATUS_ADD;
1054 return NULL;
1057 if (ie->ctime_sec == sb->st_ctime &&
1058 ie->ctime_nsec == sb->st_ctimensec &&
1059 ie->mtime_sec == sb->st_mtime &&
1060 ie->mtime_sec == sb->st_mtime &&
1061 ie->mtime_nsec == sb->st_mtimensec &&
1062 ie->size == (sb->st_size & 0xffffffff))
1063 return NULL;
1065 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1066 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1067 if (err)
1068 return err;
1070 f = fopen(abspath, "r");
1071 if (f == NULL) {
1072 err = got_error_from_errno();
1073 goto done;
1075 hdrlen = got_object_blob_get_hdrlen(blob);
1076 while (1) {
1077 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1078 err = got_object_blob_read_block(&blen, blob);
1079 if (err)
1080 goto done;
1081 /* Skip length of blob object header first time around. */
1082 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1083 if (flen == 0 && ferror(f)) {
1084 err = got_error_from_errno();
1085 goto done;
1087 if (blen == 0) {
1088 if (flen != 0)
1089 *status = GOT_STATUS_MODIFY;
1090 break;
1091 } else if (flen == 0) {
1092 if (blen != 0)
1093 *status = GOT_STATUS_MODIFY;
1094 break;
1095 } else if (blen - hdrlen == flen) {
1096 /* Skip blob object header first time around. */
1097 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1098 *status = GOT_STATUS_MODIFY;
1099 break;
1101 } else {
1102 *status = GOT_STATUS_MODIFY;
1103 break;
1105 hdrlen = 0;
1108 if (*status == GOT_STATUS_MODIFY) {
1109 rewind(f);
1110 err = get_modified_file_content_status(status, f);
1112 done:
1113 if (blob)
1114 got_object_blob_close(blob);
1115 if (f)
1116 fclose(f);
1117 return err;
1120 static const struct got_error *
1121 update_blob(struct got_worktree *worktree,
1122 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1123 struct got_tree_entry *te, const char *path,
1124 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1125 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1127 const struct got_error *err = NULL;
1128 struct got_blob_object *blob = NULL;
1129 char *ondisk_path;
1130 unsigned char status = GOT_STATUS_NO_CHANGE;
1131 struct stat sb;
1133 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1134 return got_error_from_errno();
1136 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1137 if (err)
1138 goto done;
1140 if (status == GOT_STATUS_OBSTRUCTED) {
1141 (*progress_cb)(progress_arg, status, path);
1142 goto done;
1145 if (ie && status != GOT_STATUS_MISSING) {
1146 if (got_fileindex_entry_has_commit(ie) &&
1147 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1148 SHA1_DIGEST_LENGTH) == 0) {
1149 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1150 path);
1151 goto done;
1153 if (got_fileindex_entry_has_blob(ie) &&
1154 memcmp(ie->blob_sha1, te->id->sha1,
1155 SHA1_DIGEST_LENGTH) == 0)
1156 goto done;
1159 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1160 if (err)
1161 goto done;
1163 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1164 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1165 te->mode, sb.st_mode, blob, repo, progress_cb,
1166 progress_arg);
1167 else if (status == GOT_STATUS_DELETE) {
1168 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1169 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1170 ondisk_path, path, blob, 0);
1171 if (err)
1172 goto done;
1173 } else {
1174 err = install_blob(worktree, ondisk_path, path, te->mode,
1175 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1176 repo, progress_cb, progress_arg);
1177 if (err)
1178 goto done;
1179 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1180 ondisk_path, path, blob, 1);
1181 if (err)
1182 goto done;
1184 got_object_blob_close(blob);
1185 done:
1186 free(ondisk_path);
1187 return err;
1190 static const struct got_error *
1191 remove_ondisk_file(const char *root_path, const char *path)
1193 const struct got_error *err = NULL;
1194 char *ondisk_path = NULL;
1196 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1197 return got_error_from_errno();
1199 if (unlink(ondisk_path) == -1) {
1200 if (errno != ENOENT)
1201 err = got_error_from_errno();
1202 } else {
1203 char *parent = dirname(ondisk_path);
1204 while (parent && strcmp(parent, root_path) != 0) {
1205 if (rmdir(parent) == -1) {
1206 if (errno != ENOTEMPTY)
1207 err = got_error_from_errno();
1208 break;
1210 parent = dirname(parent);
1213 free(ondisk_path);
1214 return err;
1217 static const struct got_error *
1218 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1219 struct got_fileindex_entry *ie, const char *parent_path,
1220 struct got_repository *repo,
1221 got_worktree_checkout_cb progress_cb, void *progress_arg,
1222 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1224 const struct got_error *err = NULL;
1225 unsigned char status;
1226 struct stat sb;
1227 char *ondisk_path;
1229 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1230 == -1)
1231 return got_error_from_errno();
1233 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1234 if (err)
1235 return err;
1237 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1238 status == GOT_STATUS_ADD) {
1239 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1241 * Preserve the working file and change the deleted blob's
1242 * entry into a schedule-add entry.
1244 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1245 0);
1246 if (err)
1247 return err;
1248 } else {
1249 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1250 if (status == GOT_STATUS_NO_CHANGE) {
1251 err = remove_ondisk_file(worktree->root_path, ie->path);
1252 if (err)
1253 return err;
1255 got_fileindex_entry_remove(fileindex, ie);
1258 return err;
1261 struct diff_cb_arg {
1262 struct got_fileindex *fileindex;
1263 struct got_worktree *worktree;
1264 struct got_repository *repo;
1265 got_worktree_checkout_cb progress_cb;
1266 void *progress_arg;
1267 got_worktree_cancel_cb cancel_cb;
1268 void *cancel_arg;
1271 static const struct got_error *
1272 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1273 struct got_tree_entry *te, const char *parent_path)
1275 struct diff_cb_arg *a = arg;
1277 return update_blob(a->worktree, a->fileindex, ie, te,
1278 ie->path, a->repo, a->progress_cb, a->progress_arg,
1279 a->cancel_cb, a->cancel_arg);
1282 static const struct got_error *
1283 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1285 struct diff_cb_arg *a = arg;
1287 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1288 a->repo, a->progress_cb, a->progress_arg,
1289 a->cancel_cb, a->cancel_arg);
1292 static const struct got_error *
1293 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1295 struct diff_cb_arg *a = arg;
1296 const struct got_error *err;
1297 char *path;
1299 if (asprintf(&path, "%s%s%s", parent_path,
1300 parent_path[0] ? "/" : "", te->name)
1301 == -1)
1302 return got_error_from_errno();
1304 if (S_ISDIR(te->mode))
1305 err = add_dir_on_disk(a->worktree, path);
1306 else
1307 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1308 a->repo, a->progress_cb, a->progress_arg,
1309 a->cancel_cb, a->cancel_arg);
1311 free(path);
1312 return err;
1315 const struct got_error *
1316 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1318 const struct got_error *err = NULL;
1319 char *uuidstr = NULL;
1320 uint32_t uuid_status;
1322 *refname = NULL;
1324 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1325 if (uuid_status != uuid_s_ok)
1326 return got_error_uuid(uuid_status);
1328 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1329 == -1) {
1330 err = got_error_from_errno();
1331 *refname = NULL;
1333 free(uuidstr);
1334 return err;
1338 * Prevent Git's garbage collector from deleting our base commit by
1339 * setting a reference to our base commit's ID.
1341 static const struct got_error *
1342 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1344 const struct got_error *err = NULL;
1345 struct got_reference *ref = NULL;
1346 char *refname;
1348 err = got_worktree_get_base_ref_name(&refname, worktree);
1349 if (err)
1350 return err;
1352 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1353 if (err)
1354 goto done;
1356 err = got_ref_write(ref, repo);
1357 done:
1358 free(refname);
1359 if (ref)
1360 got_ref_close(ref);
1361 return err;
1365 const struct got_error *
1366 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1367 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1368 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1370 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1371 struct got_commit_object *commit = NULL;
1372 struct got_object_id *tree_id = NULL;
1373 struct got_tree_object *tree = NULL;
1374 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1375 struct got_fileindex *fileindex = NULL;
1376 FILE *index = NULL, *new_index = NULL;
1377 struct got_fileindex_diff_tree_cb diff_cb;
1378 struct diff_cb_arg arg;
1379 char *relpath = NULL, *entry_name = NULL;
1381 err = lock_worktree(worktree, LOCK_EX);
1382 if (err)
1383 return err;
1385 fileindex = got_fileindex_alloc();
1386 if (fileindex == NULL) {
1387 err = got_error_from_errno();
1388 goto done;
1391 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1392 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1393 err = got_error_from_errno();
1394 fileindex_path = NULL;
1395 goto done;
1399 * Read the file index.
1400 * Checking out files is supposed to be an idempotent operation.
1401 * If the on-disk file index is incomplete we will try to complete it.
1403 index = fopen(fileindex_path, "rb");
1404 if (index == NULL) {
1405 if (errno != ENOENT) {
1406 err = got_error_from_errno();
1407 goto done;
1409 } else {
1410 err = got_fileindex_read(fileindex, index);
1411 fclose(index);
1412 if (err)
1413 goto done;
1416 err = got_opentemp_named(&new_fileindex_path, &new_index,
1417 fileindex_path);
1418 if (err)
1419 goto done;
1421 err = ref_base_commit(worktree, repo);
1422 if (err)
1423 goto done;
1425 err = got_object_open_as_commit(&commit, repo,
1426 worktree->base_commit_id);
1427 if (err)
1428 goto done;
1430 if (path[0]) {
1431 char *tree_path;
1432 int obj_type;
1433 relpath = strdup(path);
1434 if (relpath == NULL) {
1435 err = got_error_from_errno();
1436 goto done;
1438 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1439 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1440 path) == -1) {
1441 err = got_error_from_errno();
1442 goto done;
1444 err = got_object_id_by_path(&tree_id, repo,
1445 worktree->base_commit_id, tree_path);
1446 free(tree_path);
1447 if (err)
1448 goto done;
1449 err = got_object_get_type(&obj_type, repo, tree_id);
1450 if (err)
1451 goto done;
1452 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1453 /* Split provided path into parent dir + entry name. */
1454 if (strchr(path, '/') == NULL) {
1455 relpath = strdup("");
1456 if (relpath == NULL) {
1457 err = got_error_from_errno();
1458 goto done;
1460 tree_path = strdup(worktree->path_prefix);
1461 if (tree_path == NULL) {
1462 err = got_error_from_errno();
1463 goto done;
1465 } else {
1466 err = got_path_dirname(&relpath, path);
1467 if (err)
1468 goto done;
1469 if (asprintf(&tree_path, "%s%s%s",
1470 worktree->path_prefix,
1471 got_path_is_root_dir(
1472 worktree->path_prefix) ? "" : "/",
1473 relpath) == -1) {
1474 err = got_error_from_errno();
1475 goto done;
1478 err = got_object_id_by_path(&tree_id, repo,
1479 worktree->base_commit_id, tree_path);
1480 free(tree_path);
1481 if (err)
1482 goto done;
1483 entry_name = basename(path);
1484 if (entry_name == NULL) {
1485 err = got_error_from_errno();
1486 goto done;
1489 } else {
1490 relpath = strdup("");
1491 if (relpath == NULL) {
1492 err = got_error_from_errno();
1493 goto done;
1495 err = got_object_id_by_path(&tree_id, repo,
1496 worktree->base_commit_id, worktree->path_prefix);
1497 if (err)
1498 goto done;
1501 err = got_object_open_as_tree(&tree, repo, tree_id);
1502 if (err)
1503 goto done;
1505 if (entry_name &&
1506 got_object_tree_find_entry(tree, entry_name) == NULL) {
1507 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1508 goto done;
1511 diff_cb.diff_old_new = diff_old_new;
1512 diff_cb.diff_old = diff_old;
1513 diff_cb.diff_new = diff_new;
1514 arg.fileindex = fileindex;
1515 arg.worktree = worktree;
1516 arg.repo = repo;
1517 arg.progress_cb = progress_cb;
1518 arg.progress_arg = progress_arg;
1519 arg.cancel_cb = cancel_cb;
1520 arg.cancel_arg = cancel_arg;
1521 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1522 entry_name, repo, &diff_cb, &arg);
1524 /* Try to sync the fileindex back to disk in any case. */
1525 err = got_fileindex_write(fileindex, new_index);
1526 if (err)
1527 goto done;
1529 if (rename(new_fileindex_path, fileindex_path) != 0) {
1530 err = got_error_from_errno();
1531 unlink(new_fileindex_path);
1532 goto done;
1535 free(new_fileindex_path);
1536 new_fileindex_path = NULL;
1538 done:
1539 free(relpath);
1540 if (tree)
1541 got_object_tree_close(tree);
1542 if (commit)
1543 got_object_commit_close(commit);
1544 if (new_fileindex_path)
1545 unlink(new_fileindex_path);
1546 if (new_index)
1547 fclose(new_index);
1548 free(new_fileindex_path);
1549 free(fileindex_path);
1550 got_fileindex_free(fileindex);
1551 if (checkout_err)
1552 err = checkout_err;
1553 unlockerr = lock_worktree(worktree, LOCK_SH);
1554 if (unlockerr && err == NULL)
1555 err = unlockerr;
1556 return err;
1559 struct diff_dir_cb_arg {
1560 struct got_fileindex *fileindex;
1561 struct got_worktree *worktree;
1562 const char *status_path;
1563 size_t status_path_len;
1564 struct got_repository *repo;
1565 got_worktree_status_cb status_cb;
1566 void *status_arg;
1567 got_worktree_cancel_cb cancel_cb;
1568 void *cancel_arg;
1571 static const struct got_error *
1572 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1573 got_worktree_status_cb status_cb, void *status_arg,
1574 struct got_repository *repo)
1576 const struct got_error *err = NULL;
1577 unsigned char status = GOT_STATUS_NO_CHANGE;
1578 struct stat sb;
1579 struct got_object_id id;
1581 err = get_file_status(&status, &sb, ie, abspath, repo);
1582 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1583 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1584 err = (*status_cb)(status_arg, status, ie->path, &id);
1586 return err;
1589 static const struct got_error *
1590 status_old_new(void *arg, struct got_fileindex_entry *ie,
1591 struct dirent *de, const char *parent_path)
1593 const struct got_error *err = NULL;
1594 struct diff_dir_cb_arg *a = arg;
1595 char *abspath;
1597 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1598 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1599 return NULL;
1601 if (parent_path[0]) {
1602 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1603 parent_path, de->d_name) == -1)
1604 return got_error_from_errno();
1605 } else {
1606 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1607 de->d_name) == -1)
1608 return got_error_from_errno();
1611 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1612 a->repo);
1613 free(abspath);
1614 return err;
1617 static const struct got_error *
1618 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1620 struct diff_dir_cb_arg *a = arg;
1621 struct got_object_id id;
1622 unsigned char status;
1624 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1625 return NULL;
1627 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1628 if (got_fileindex_entry_has_file_on_disk(ie))
1629 status = GOT_STATUS_MISSING;
1630 else
1631 status = GOT_STATUS_DELETE;
1632 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1635 static const struct got_error *
1636 status_new(void *arg, struct dirent *de, const char *parent_path)
1638 const struct got_error *err = NULL;
1639 struct diff_dir_cb_arg *a = arg;
1640 char *path = NULL;
1642 if (de->d_type == DT_DIR)
1643 return NULL;
1645 /* XXX ignore symlinks for now */
1646 if (de->d_type == DT_LNK)
1647 return NULL;
1649 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1650 return NULL;
1652 if (parent_path[0]) {
1653 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1654 return got_error_from_errno();
1655 } else {
1656 path = de->d_name;
1659 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1660 NULL);
1661 if (parent_path[0])
1662 free(path);
1663 return err;
1666 const struct got_error *
1667 got_worktree_status(struct got_worktree *worktree, const char *path,
1668 struct got_repository *repo, got_worktree_status_cb status_cb,
1669 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1671 const struct got_error *err = NULL;
1672 DIR *workdir = NULL;
1673 char *fileindex_path = NULL;
1674 struct got_fileindex *fileindex = NULL;
1675 FILE *index = NULL;
1676 struct got_fileindex_diff_dir_cb fdiff_cb;
1677 struct diff_dir_cb_arg arg;
1678 char *ondisk_path = NULL;
1680 fileindex = got_fileindex_alloc();
1681 if (fileindex == NULL) {
1682 err = got_error_from_errno();
1683 goto done;
1686 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1687 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1688 err = got_error_from_errno();
1689 fileindex_path = NULL;
1690 goto done;
1693 index = fopen(fileindex_path, "rb");
1694 if (index == NULL) {
1695 if (errno != ENOENT) {
1696 err = got_error_from_errno();
1697 goto done;
1699 } else {
1700 err = got_fileindex_read(fileindex, index);
1701 fclose(index);
1702 if (err)
1703 goto done;
1706 if (asprintf(&ondisk_path, "%s%s%s",
1707 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1708 err = got_error_from_errno();
1709 goto done;
1711 workdir = opendir(ondisk_path);
1712 if (workdir == NULL) {
1713 if (errno == ENOTDIR || errno == ENOENT) {
1714 struct got_fileindex_entry *ie;
1715 ie = got_fileindex_entry_get(fileindex, path);
1716 if (ie == NULL) {
1717 err = got_error(GOT_ERR_BAD_PATH);
1718 goto done;
1720 err = report_file_status(ie, ondisk_path,
1721 status_cb, status_arg, repo);
1722 goto done;
1723 } else {
1724 err = got_error_from_errno();
1725 goto done;
1728 fdiff_cb.diff_old_new = status_old_new;
1729 fdiff_cb.diff_old = status_old;
1730 fdiff_cb.diff_new = status_new;
1731 arg.fileindex = fileindex;
1732 arg.worktree = worktree;
1733 arg.status_path = path;
1734 arg.status_path_len = strlen(path);
1735 arg.repo = repo;
1736 arg.status_cb = status_cb;
1737 arg.status_arg = status_arg;
1738 arg.cancel_cb = cancel_cb;
1739 arg.cancel_arg = cancel_arg;
1740 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1741 path, repo, &fdiff_cb, &arg);
1742 done:
1743 if (workdir)
1744 closedir(workdir);
1745 free(ondisk_path);
1746 free(fileindex_path);
1747 got_fileindex_free(fileindex);
1748 return err;
1751 const struct got_error *
1752 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1753 const char *arg)
1755 const struct got_error *err = NULL;
1756 char *resolved, *path = NULL;
1757 size_t len;
1759 *wt_path = NULL;
1761 resolved = realpath(arg, NULL);
1762 if (resolved == NULL)
1763 return got_error_from_errno();
1765 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1766 strlen(got_worktree_get_root_path(worktree)))) {
1767 err = got_error(GOT_ERR_BAD_PATH);
1768 goto done;
1771 path = strdup(resolved +
1772 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1773 if (path == NULL) {
1774 err = got_error_from_errno();
1775 goto done;
1778 /* XXX status walk can't deal with trailing slash! */
1779 len = strlen(path);
1780 while (path[len - 1] == '/') {
1781 path[len - 1] = '\0';
1782 len--;
1784 done:
1785 free(resolved);
1786 if (err == NULL)
1787 *wt_path = path;
1788 else
1789 free(path);
1790 return err;
1793 const struct got_error *
1794 got_worktree_schedule_add(struct got_worktree *worktree,
1795 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1796 struct got_repository *repo)
1798 struct got_fileindex *fileindex = NULL;
1799 struct got_fileindex_entry *ie = NULL;
1800 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1801 FILE *index = NULL, *new_index = NULL;
1802 const struct got_error *err = NULL, *unlockerr = NULL;
1803 int ie_added = 0;
1805 err = lock_worktree(worktree, LOCK_EX);
1806 if (err)
1807 return err;
1809 err = got_path_skip_common_ancestor(&relpath,
1810 got_worktree_get_root_path(worktree), ondisk_path);
1811 if (err)
1812 goto done;
1814 fileindex = got_fileindex_alloc();
1815 if (fileindex == NULL) {
1816 err = got_error_from_errno();
1817 goto done;
1820 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1821 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1822 err = got_error_from_errno();
1823 fileindex_path = NULL;
1824 goto done;
1827 index = fopen(fileindex_path, "rb");
1828 if (index == NULL) {
1829 err = got_error_from_errno();
1830 goto done;
1833 err = got_fileindex_read(fileindex, index);
1834 if (err)
1835 goto done;
1837 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1838 err = got_error_set_errno(EEXIST);
1839 goto done;
1842 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1843 if (err)
1844 goto done;
1846 err = got_fileindex_entry_add(fileindex, ie);
1847 if (err)
1848 goto done;
1849 ie_added = 1; /* now owned by fileindex; don't free separately */
1851 err = got_opentemp_named(&new_fileindex_path, &new_index,
1852 fileindex_path);
1853 if (err)
1854 goto done;
1856 err = got_fileindex_write(fileindex, new_index);
1857 if (err)
1858 goto done;
1860 if (rename(new_fileindex_path, fileindex_path) != 0) {
1861 err = got_error_from_errno();
1862 goto done;
1865 free(new_fileindex_path);
1866 new_fileindex_path = NULL;
1868 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1869 done:
1870 if (index) {
1871 if (fclose(index) != 0 && err == NULL)
1872 err = got_error_from_errno();
1874 if (new_fileindex_path) {
1875 if (unlink(new_fileindex_path) != 0 && err == NULL)
1876 err = got_error_from_errno();
1877 free(new_fileindex_path);
1879 if (ie && !ie_added)
1880 got_fileindex_entry_free(ie);
1881 if (fileindex)
1882 got_fileindex_free(fileindex);
1883 unlockerr = lock_worktree(worktree, LOCK_SH);
1884 if (unlockerr && err == NULL)
1885 err = unlockerr;
1886 free(relpath);
1887 return err;
1890 const struct got_error *
1891 got_worktree_schedule_delete(struct got_worktree *worktree,
1892 const char *ondisk_path, int delete_local_mods,
1893 got_worktree_status_cb status_cb, void *status_arg,
1894 struct got_repository *repo)
1896 struct got_fileindex *fileindex = NULL;
1897 struct got_fileindex_entry *ie = NULL;
1898 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1899 FILE *index = NULL, *new_index = NULL;
1900 const struct got_error *err = NULL, *unlockerr = NULL;
1901 unsigned char status;
1902 struct stat sb;
1904 err = lock_worktree(worktree, LOCK_EX);
1905 if (err)
1906 return err;
1908 err = got_path_skip_common_ancestor(&relpath,
1909 got_worktree_get_root_path(worktree), ondisk_path);
1910 if (err)
1911 goto done;
1913 fileindex = got_fileindex_alloc();
1914 if (fileindex == NULL) {
1915 err = got_error_from_errno();
1916 goto done;
1919 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1920 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1921 err = got_error_from_errno();
1922 fileindex_path = NULL;
1923 goto done;
1926 index = fopen(fileindex_path, "rb");
1927 if (index == NULL) {
1928 err = got_error_from_errno();
1929 goto done;
1932 err = got_fileindex_read(fileindex, index);
1933 if (err)
1934 goto done;
1936 ie = got_fileindex_entry_get(fileindex, relpath);
1937 if (ie == NULL) {
1938 err = got_error(GOT_ERR_BAD_PATH);
1939 goto done;
1942 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1943 if (err)
1944 goto done;
1946 if (status != GOT_STATUS_NO_CHANGE) {
1947 if (status == GOT_STATUS_DELETE) {
1948 err = got_error_set_errno(ENOENT);
1949 goto done;
1951 if (status != GOT_STATUS_MODIFY) {
1952 err = got_error(GOT_ERR_FILE_STATUS);
1953 goto done;
1955 if (!delete_local_mods) {
1956 err = got_error(GOT_ERR_FILE_MODIFIED);
1957 goto done;
1961 if (unlink(ondisk_path) != 0) {
1962 err = got_error_from_errno();
1963 goto done;
1966 got_fileindex_entry_mark_deleted_from_disk(ie);
1968 err = got_opentemp_named(&new_fileindex_path, &new_index,
1969 fileindex_path);
1970 if (err)
1971 goto done;
1973 err = got_fileindex_write(fileindex, new_index);
1974 if (err)
1975 goto done;
1977 if (rename(new_fileindex_path, fileindex_path) != 0) {
1978 err = got_error_from_errno();
1979 goto done;
1982 free(new_fileindex_path);
1983 new_fileindex_path = NULL;
1985 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1986 done:
1987 free(relpath);
1988 if (index) {
1989 if (fclose(index) != 0 && err == NULL)
1990 err = got_error_from_errno();
1992 if (new_fileindex_path) {
1993 if (unlink(new_fileindex_path) != 0 && err == NULL)
1994 err = got_error_from_errno();
1995 free(new_fileindex_path);
1997 if (fileindex)
1998 got_fileindex_free(fileindex);
1999 unlockerr = lock_worktree(worktree, LOCK_SH);
2000 if (unlockerr && err == NULL)
2001 err = unlockerr;
2002 return err;
2005 const struct got_error *
2006 got_worktree_revert(struct got_worktree *worktree,
2007 const char *ondisk_path,
2008 got_worktree_checkout_cb progress_cb, void *progress_arg,
2009 struct got_repository *repo)
2011 struct got_fileindex *fileindex = NULL;
2012 struct got_fileindex_entry *ie = NULL;
2013 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2014 char *tree_path = NULL, *parent_path, *te_name;
2015 FILE *index = NULL, *new_index = NULL;
2016 const struct got_error *err = NULL, *unlockerr = NULL;
2017 struct got_tree_object *tree = NULL;
2018 struct got_object_id id, *tree_id = NULL;
2019 const struct got_tree_entry *te;
2020 struct got_blob_object *blob = NULL;
2021 unsigned char status;
2022 struct stat sb;
2024 err = lock_worktree(worktree, LOCK_EX);
2025 if (err)
2026 return err;
2028 err = got_path_skip_common_ancestor(&relpath,
2029 got_worktree_get_root_path(worktree), ondisk_path);
2030 if (err)
2031 goto done;
2033 fileindex = got_fileindex_alloc();
2034 if (fileindex == NULL) {
2035 err = got_error_from_errno();
2036 goto done;
2039 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2040 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2041 err = got_error_from_errno();
2042 fileindex_path = NULL;
2043 goto done;
2046 index = fopen(fileindex_path, "rb");
2047 if (index == NULL) {
2048 err = got_error_from_errno();
2049 goto done;
2052 err = got_fileindex_read(fileindex, index);
2053 if (err)
2054 goto done;
2056 ie = got_fileindex_entry_get(fileindex, relpath);
2057 if (ie == NULL) {
2058 err = got_error(GOT_ERR_BAD_PATH);
2059 goto done;
2062 /* Construct in-repository path of tree which contains this blob. */
2063 err = got_path_dirname(&parent_path, ie->path);
2064 if (err) {
2065 if (err->code != GOT_ERR_BAD_PATH)
2066 goto done;
2067 parent_path = "/";
2069 if (got_path_is_root_dir(worktree->path_prefix)) {
2070 tree_path = strdup(parent_path);
2071 if (tree_path == NULL) {
2072 err = got_error_from_errno();
2073 goto done;
2075 } else {
2076 if (got_path_is_root_dir(parent_path)) {
2077 tree_path = strdup(worktree->path_prefix);
2078 if (tree_path == NULL) {
2079 err = got_error_from_errno();
2080 goto done;
2082 } else {
2083 if (asprintf(&tree_path, "%s/%s",
2084 worktree->path_prefix, parent_path) == -1) {
2085 err = got_error_from_errno();
2086 goto done;
2091 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2092 tree_path);
2093 if (err)
2094 goto done;
2096 err = got_object_open_as_tree(&tree, repo, tree_id);
2097 if (err)
2098 goto done;
2100 te_name = basename(ie->path);
2101 if (te_name == NULL) {
2102 err = got_error_from_errno();
2103 goto done;
2106 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2107 if (err)
2108 goto done;
2110 te = got_object_tree_find_entry(tree, te_name);
2111 if (te == NULL && status != GOT_STATUS_ADD) {
2112 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2113 goto done;
2116 switch (status) {
2117 case GOT_STATUS_ADD:
2118 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2119 got_fileindex_entry_remove(fileindex, ie);
2120 break;
2121 case GOT_STATUS_DELETE:
2122 case GOT_STATUS_MODIFY:
2123 case GOT_STATUS_CONFLICT:
2124 case GOT_STATUS_MISSING:
2125 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2126 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2127 if (err)
2128 goto done;
2129 err = install_blob(worktree, ondisk_path, ie->path,
2130 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2131 progress_arg);
2132 if (err)
2133 goto done;
2134 if (status == GOT_STATUS_DELETE) {
2135 err = update_blob_fileindex_entry(worktree,
2136 fileindex, ie, ondisk_path, ie->path, blob, 1);
2137 if (err)
2138 goto done;
2140 break;
2141 default:
2142 goto done;
2145 err = got_opentemp_named(&new_fileindex_path, &new_index,
2146 fileindex_path);
2147 if (err)
2148 goto done;
2150 err = got_fileindex_write(fileindex, new_index);
2151 if (err)
2152 goto done;
2154 if (rename(new_fileindex_path, fileindex_path) != 0) {
2155 err = got_error_from_errno();
2156 goto done;
2159 free(new_fileindex_path);
2160 new_fileindex_path = NULL;
2161 done:
2162 free(relpath);
2163 free(tree_path);
2164 if (blob)
2165 got_object_blob_close(blob);
2166 if (tree)
2167 got_object_tree_close(tree);
2168 free(tree_id);
2169 if (index) {
2170 if (fclose(index) != 0 && err == NULL)
2171 err = got_error_from_errno();
2173 if (new_fileindex_path) {
2174 if (unlink(new_fileindex_path) != 0 && err == NULL)
2175 err = got_error_from_errno();
2176 free(new_fileindex_path);
2178 if (fileindex)
2179 got_fileindex_free(fileindex);
2180 unlockerr = lock_worktree(worktree, LOCK_SH);
2181 if (unlockerr && err == NULL)
2182 err = unlockerr;
2183 return err;