2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/limits.h>
19 #include <sys/queue.h>
35 #include "got_error.h"
36 #include "got_repository.h"
37 #include "got_reference.h"
38 #include "got_object.h"
39 #include "got_worktree.h"
40 #include "got_opentemp.h"
42 #include "got_lib_worktree.h"
43 #include "got_lib_path.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_fileindex.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_diff.h"
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 static const struct got_error *
56 create_meta_file(const char *path_got, const char *name, const char *content)
58 const struct got_error *err = NULL;
62 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
63 err = got_error_from_errno();
68 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
69 GOT_DEFAULT_FILE_MODE);
71 err = got_error_from_errno();
76 int len = dprintf(fd, "%s\n", content);
77 if (len != strlen(content) + 1) {
78 err = got_error_from_errno();
84 if (fd != -1 && close(fd) == -1 && err == NULL)
85 err = got_error_from_errno();
90 static const struct got_error *
91 update_meta_file(const char *path_got, const char *name, const char *content)
93 const struct got_error *err = NULL;
98 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
99 err = got_error_from_errno();
104 err = got_opentemp_named(&tmppath, &tmpfile, path);
109 int len = fprintf(tmpfile, "%s\n", content);
110 if (len != strlen(content) + 1) {
111 err = got_error_from_errno();
116 if (rename(tmppath, path) != 0) {
117 err = got_error_from_errno();
123 if (fclose(tmpfile) != 0 && err == NULL)
124 err = got_error_from_errno();
128 static const struct got_error *
129 read_meta_file(char **content, const char *path_got, const char *name)
131 const struct got_error *err = NULL;
139 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
140 err = got_error_from_errno();
145 fd = open(path, O_RDONLY | O_NOFOLLOW);
147 err = got_error_from_errno();
150 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
151 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
152 : got_error_from_errno());
156 if (lstat(path, &sb) != 0) {
157 err = got_error_from_errno();
160 *content = calloc(1, sb.st_size);
161 if (*content == NULL) {
162 err = got_error_from_errno();
166 n = read(fd, *content, sb.st_size);
167 if (n != sb.st_size) {
168 err = (n == -1 ? got_error_from_errno() :
169 got_error(GOT_ERR_WORKTREE_META));
172 if ((*content)[sb.st_size - 1] != '\n') {
173 err = got_error(GOT_ERR_WORKTREE_META);
176 (*content)[sb.st_size - 1] = '\0';
179 if (fd != -1 && close(fd) == -1 && err == NULL)
180 err = got_error_from_errno();
189 const struct got_error *
190 got_worktree_init(const char *path, struct got_reference *head_ref,
191 const char *prefix, struct got_repository *repo)
193 const struct got_error *err = NULL;
194 struct got_object_id *commit_id = NULL;
196 char *path_got = NULL;
198 char *formatstr = NULL;
199 char *absprefix = NULL;
200 char *basestr = NULL;
202 err = got_ref_resolve(&commit_id, repo, head_ref);
205 err = got_object_get_type(&obj_type, repo, commit_id);
208 if (obj_type != GOT_OBJ_TYPE_COMMIT)
209 return got_error(GOT_ERR_OBJ_TYPE);
211 if (!got_path_is_absolute(prefix)) {
212 if (asprintf(&absprefix, "/%s", prefix) == -1)
213 return got_error_from_errno();
216 /* Create top-level directory (may already exist). */
217 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
218 err = got_error_from_errno();
222 /* Create .got directory (may already exist). */
223 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
224 err = got_error_from_errno();
227 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
228 err = got_error_from_errno();
232 /* Create an empty lock file. */
233 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
237 /* Create an empty file index. */
238 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
242 /* Write the HEAD reference. */
243 refstr = got_ref_to_str(head_ref);
244 if (refstr == NULL) {
245 err = got_error_from_errno();
248 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
252 /* Record our base commit. */
253 err = got_object_id_str(&basestr, commit_id);
256 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
260 /* Store path to repository. */
261 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
262 got_repo_get_path(repo));
266 /* Store in-repository path prefix. */
267 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
268 absprefix ? absprefix : prefix);
272 /* Stamp work tree with format file. */
273 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
274 err = got_error_from_errno();
277 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
291 static const struct got_error *
292 open_worktree(struct got_worktree **worktree, const char *path)
294 const struct got_error *err = NULL;
296 char *formatstr = NULL;
297 char *path_lock = NULL;
298 char *base_commit_id_str = NULL;
299 char *head_ref_str = NULL;
300 int version, fd = -1;
302 struct got_repository *repo = NULL;
306 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
307 err = got_error_from_errno();
312 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
313 err = got_error_from_errno();
318 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
320 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
321 : got_error_from_errno());
325 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
329 version = strtonum(formatstr, 1, INT_MAX, &errstr);
331 err = got_error(GOT_ERR_WORKTREE_META);
334 if (version != GOT_WORKTREE_FORMAT_VERSION) {
335 err = got_error(GOT_ERR_WORKTREE_VERS);
339 *worktree = calloc(1, sizeof(**worktree));
340 if (*worktree == NULL) {
341 err = got_error_from_errno();
344 (*worktree)->lockfd = -1;
346 (*worktree)->root_path = strdup(path);
347 if ((*worktree)->root_path == NULL) {
348 err = got_error_from_errno();
351 err = read_meta_file(&(*worktree)->repo_path, path_got,
352 GOT_WORKTREE_REPOSITORY);
356 err = read_meta_file(&(*worktree)->path_prefix, path_got,
357 GOT_WORKTREE_PATH_PREFIX);
361 err = read_meta_file(&base_commit_id_str, path_got,
362 GOT_WORKTREE_BASE_COMMIT);
366 err = got_repo_open(&repo, (*worktree)->repo_path);
370 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
375 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
379 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
382 got_repo_close(repo);
386 free(base_commit_id_str);
390 if (*worktree != NULL)
391 got_worktree_close(*worktree);
394 (*worktree)->lockfd = fd;
399 const struct got_error *
400 got_worktree_open(struct got_worktree **worktree, const char *path)
402 const struct got_error *err = NULL;
405 err = open_worktree(worktree, path);
406 if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
410 path = dirname(path);
412 return got_error_from_errno();
413 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
415 return got_error(GOT_ERR_NOT_WORKTREE);
418 const struct got_error *
419 got_worktree_close(struct got_worktree *worktree)
421 const struct got_error *err = NULL;
422 free(worktree->root_path);
423 free(worktree->repo_path);
424 free(worktree->path_prefix);
425 free(worktree->base_commit_id);
426 if (worktree->head_ref)
427 got_ref_close(worktree->head_ref);
428 if (worktree->lockfd != -1)
429 if (close(worktree->lockfd) != 0)
430 err = got_error_from_errno();
436 got_worktree_get_root_path(struct got_worktree *worktree)
438 return worktree->root_path;
442 got_worktree_get_repo_path(struct got_worktree *worktree)
444 return worktree->repo_path;
448 got_worktree_get_path_prefix(struct got_worktree *worktree)
450 return worktree->path_prefix;
453 const struct got_error *
454 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
455 const char *path_prefix)
457 char *absprefix = NULL;
459 if (!got_path_is_absolute(path_prefix)) {
460 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
461 return got_error_from_errno();
463 *match = (strcmp(absprefix ? absprefix : path_prefix,
464 worktree->path_prefix) == 0);
470 got_worktree_get_head_ref_name(struct got_worktree *worktree)
472 return got_ref_to_str(worktree->head_ref);
475 struct got_reference *
476 got_worktree_get_head_ref(struct got_worktree *worktree)
478 return got_ref_dup(worktree->head_ref);
481 struct got_object_id *
482 got_worktree_get_base_commit_id(struct got_worktree *worktree)
484 return worktree->base_commit_id;
487 const struct got_error *
488 got_worktree_set_base_commit_id(struct got_worktree *worktree,
489 struct got_repository *repo, struct got_object_id *commit_id)
491 const struct got_error *err;
492 struct got_object *obj = NULL;
494 char *path_got = NULL;
496 if (asprintf(&path_got, "%s/%s", worktree->root_path,
497 GOT_WORKTREE_GOT_DIR) == -1) {
498 err = got_error_from_errno();
503 err = got_object_open(&obj, repo, commit_id);
507 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
508 err = got_error(GOT_ERR_OBJ_TYPE);
512 /* Record our base commit. */
513 err = got_object_id_str(&id_str, commit_id);
516 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
520 free(worktree->base_commit_id);
521 worktree->base_commit_id = got_object_id_dup(commit_id);
522 if (worktree->base_commit_id == NULL) {
523 err = got_error_from_errno();
528 got_object_close(obj);
534 static const struct got_error *
535 lock_worktree(struct got_worktree *worktree, int operation)
537 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
538 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
539 : got_error_from_errno());
543 static const struct got_error *
544 make_parent_dirs(const char *abspath)
546 const struct got_error *err = NULL;
548 char *parent = dirname(abspath);
552 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
553 if (errno == ENOENT) {
554 err = make_parent_dirs(parent);
557 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
558 return got_error_from_errno();
560 err = got_error_from_errno();
566 static const struct got_error *
567 add_dir_on_disk(struct got_worktree *worktree, const char *path)
569 const struct got_error *err = NULL;
572 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
573 return got_error_from_errno();
575 /* XXX queue work rather than editing disk directly? */
576 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
579 if (errno == EEXIST) {
580 if (lstat(abspath, &sb) == -1) {
581 err = got_error_from_errno();
585 if (!S_ISDIR(sb.st_mode)) {
586 /* TODO directory is obstructed; do something */
587 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
592 } else if (errno == ENOENT) {
593 err = make_parent_dirs(abspath);
596 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
597 err = got_error_from_errno();
599 err = got_error_from_errno();
607 static const struct got_error *
608 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
610 const struct got_error *err = NULL;
613 size_t flen1 = 0, flen2 = 0;
618 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
619 if (flen1 == 0 && ferror(f1)) {
620 err = got_error_from_errno();
623 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
624 if (flen2 == 0 && ferror(f2)) {
625 err = got_error_from_errno();
632 } else if (flen2 == 0) {
636 } else if (flen1 == flen2) {
637 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
650 static const struct got_error *
651 check_files_equal(int *same, const char *f1_path, const char *f2_path)
653 const struct got_error *err = NULL;
656 FILE *f1 = NULL, *f2 = NULL;
660 if (lstat(f1_path, &sb) != 0) {
661 err = got_error_from_errno();
666 if (lstat(f2_path, &sb) != 0) {
667 err = got_error_from_errno();
672 if (size1 != size2) {
677 f1 = fopen(f1_path, "r");
679 return got_error_from_errno();
681 f2 = fopen(f2_path, "r");
683 err = got_error_from_errno();
687 err = check_file_contents_equal(same, f1, f2);
689 if (f1 && fclose(f1) != 0 && err == NULL)
690 err = got_error_from_errno();
691 if (f2 && fclose(f2) != 0 && err == NULL)
692 err = got_error_from_errno();
698 * Perform a 3-way merge where the file's version in the file index (blob2)
699 * acts as the common ancestor, the incoming blob (blob1) acts as the first
700 * derived version, and the file on disk acts as the second derived version.
702 static const struct got_error *
703 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
704 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
705 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
706 struct got_repository *repo,
707 got_worktree_checkout_cb progress_cb, void *progress_arg)
709 const struct got_error *err = NULL;
711 struct got_blob_object *blob2 = NULL;
712 FILE *f1 = NULL, *f2 = NULL;
713 char *blob1_path = NULL, *blob2_path = NULL;
714 char *merged_path = NULL, *base_path = NULL;
715 struct got_object_id id2;
718 int overlapcnt = 0, update_timestamps = 0;
721 parent = dirname(ondisk_path);
723 return got_error_from_errno();
725 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
726 return got_error_from_errno();
728 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
733 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
734 err = got_error_from_errno();
739 err = got_opentemp_named(&blob1_path, &f1, base_path);
742 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
747 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
748 err = got_error_from_errno();
753 err = got_opentemp_named(&blob2_path, &f2, base_path);
757 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
758 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
761 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
765 err = got_object_id_str(&id_str, worktree->base_commit_id);
768 if (asprintf(&label1, "commit %s", id_str) == -1) {
769 err = got_error_from_errno();
773 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
774 blob2_path, ondisk_path, label1, path);
778 (*progress_cb)(progress_arg,
779 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
782 if (fsync(merged_fd) != 0) {
783 err = got_error_from_errno();
787 /* Check if a clean merge has subsumed all local changes. */
788 if (overlapcnt == 0) {
789 err = check_files_equal(&update_timestamps, blob1_path,
795 if (rename(merged_path, ondisk_path) != 0) {
796 err = got_error_from_errno();
801 * Do not update timestamps of already modified files. Otherwise,
802 * a future status walk would treat them as unmodified files again.
804 err = got_fileindex_entry_update(ie, ondisk_path,
805 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
807 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
808 err = got_error_from_errno();
809 if (f1 && fclose(f1) != 0 && err == NULL)
810 err = got_error_from_errno();
811 if (f2 && fclose(f2) != 0 && err == NULL)
812 err = got_error_from_errno();
814 got_object_blob_close(blob2);
830 static const struct got_error *
831 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
832 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
833 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
834 int restoring_missing_file, struct got_repository *repo,
835 got_worktree_checkout_cb progress_cb, void *progress_arg)
837 const struct got_error *err = NULL;
841 char *tmppath = NULL;
843 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
844 GOT_DEFAULT_FILE_MODE);
846 if (errno == ENOENT) {
847 char *parent = dirname(path);
849 return got_error_from_errno();
850 err = add_dir_on_disk(worktree, parent);
853 fd = open(ondisk_path,
854 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
855 GOT_DEFAULT_FILE_MODE);
857 return got_error_from_errno();
858 } else if (errno == EEXIST) {
859 if (!S_ISREG(st_mode)) {
860 /* TODO file is obstructed; do something */
861 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
864 err = got_opentemp_named_fd(&tmppath, &fd,
871 return got_error_from_errno();
874 if (restoring_missing_file)
875 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
877 (*progress_cb)(progress_arg,
878 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
880 hdrlen = got_object_blob_get_hdrlen(blob);
882 const uint8_t *buf = got_object_blob_get_read_buf(blob);
883 err = got_object_blob_read_block(&len, blob);
887 /* Skip blob object header first time around. */
888 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
890 err = got_error_from_errno();
892 } else if (outlen != len - hdrlen) {
893 err = got_error(GOT_ERR_IO);
900 if (fsync(fd) != 0) {
901 err = got_error_from_errno();
906 if (rename(tmppath, ondisk_path) != 0) {
907 err = got_error_from_errno();
912 if (te_mode & S_IXUSR) {
913 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
914 err = got_error_from_errno();
918 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
919 err = got_error_from_errno();
925 entry = got_fileindex_entry_get(fileindex, path);
927 err = got_fileindex_entry_update(entry, ondisk_path,
928 blob->id.sha1, worktree->base_commit_id->sha1, 1);
930 err = got_fileindex_entry_alloc(&entry, ondisk_path,
931 path, blob->id.sha1, worktree->base_commit_id->sha1);
934 err = got_fileindex_entry_add(fileindex, entry);
937 if (fd != -1 && close(fd) != 0 && err == NULL)
938 err = got_error_from_errno();
943 static const struct got_error *
944 get_file_status(unsigned char *status, struct stat *sb,
945 struct got_fileindex_entry *ie, const char *abspath,
946 struct got_repository *repo)
948 const struct got_error *err = NULL;
949 struct got_object_id id;
953 struct got_blob_object *blob = NULL;
956 *status = GOT_STATUS_NO_CHANGE;
958 if (lstat(abspath, sb) == -1) {
959 if (errno == ENOENT) {
961 *status = GOT_STATUS_MISSING;
963 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
964 & (S_IRWXU | S_IRWXG | S_IRWXO));
966 sb->st_mode = GOT_DEFAULT_FILE_MODE;
969 return got_error_from_errno();
972 if (!S_ISREG(sb->st_mode)) {
973 *status = GOT_STATUS_OBSTRUCTED;
980 if (ie->ctime_sec == sb->st_ctime &&
981 ie->ctime_nsec == sb->st_ctimensec &&
982 ie->mtime_sec == sb->st_mtime &&
983 ie->mtime_sec == sb->st_mtime &&
984 ie->mtime_nsec == sb->st_mtimensec &&
985 ie->size == (sb->st_size & 0xffffffff))
988 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
989 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
993 f = fopen(abspath, "r");
995 err = got_error_from_errno();
998 hdrlen = got_object_blob_get_hdrlen(blob);
1000 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1001 err = got_object_blob_read_block(&blen, blob);
1004 /* Skip length of blob object header first time around. */
1005 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1006 if (flen == 0 && ferror(f)) {
1007 err = got_error_from_errno();
1012 *status = GOT_STATUS_MODIFY;
1014 } else if (flen == 0) {
1016 *status = GOT_STATUS_MODIFY;
1018 } else if (blen - hdrlen == flen) {
1019 /* Skip blob object header first time around. */
1020 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1021 *status = GOT_STATUS_MODIFY;
1025 *status = GOT_STATUS_MODIFY;
1032 got_object_blob_close(blob);
1038 static const struct got_error *
1039 update_blob(struct got_worktree *worktree,
1040 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1041 struct got_tree_entry *te, const char *path,
1042 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1043 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1045 const struct got_error *err = NULL;
1046 struct got_blob_object *blob = NULL;
1048 unsigned char status = GOT_STATUS_NO_CHANGE;
1051 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1052 return got_error_from_errno();
1054 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1058 if (status == GOT_STATUS_OBSTRUCTED) {
1059 (*progress_cb)(progress_arg, status, path);
1063 if (ie && status != GOT_STATUS_MISSING) {
1064 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1065 SHA1_DIGEST_LENGTH) == 0) {
1066 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1070 if (memcmp(ie->blob_sha1,
1071 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1075 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1079 if (status == GOT_STATUS_MODIFY)
1080 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1081 te->mode, sb.st_mode, blob, repo, progress_cb,
1084 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1085 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1086 repo, progress_cb, progress_arg);
1088 got_object_blob_close(blob);
1094 static const struct got_error *
1095 remove_ondisk_file(const char *root_path, const char *path)
1097 const struct got_error *err = NULL;
1098 char *ondisk_path = NULL;
1100 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1101 return got_error_from_errno();
1103 if (unlink(ondisk_path) == -1) {
1104 if (errno != ENOENT)
1105 err = got_error_from_errno();
1107 char *parent = dirname(ondisk_path);
1108 while (parent && strcmp(parent, root_path) != 0) {
1109 if (rmdir(parent) == -1) {
1110 if (errno != ENOTEMPTY)
1111 err = got_error_from_errno();
1114 parent = dirname(parent);
1121 struct diff_cb_arg {
1122 struct got_fileindex *fileindex;
1123 struct got_worktree *worktree;
1124 struct got_repository *repo;
1125 got_worktree_checkout_cb progress_cb;
1127 got_worktree_cancel_cb cancel_cb;
1131 static const struct got_error *
1132 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1133 struct got_tree_entry *te, const char *parent_path)
1135 struct diff_cb_arg *a = arg;
1137 return update_blob(a->worktree, a->fileindex, ie, te,
1138 ie->path, a->repo, a->progress_cb, a->progress_arg,
1139 a->cancel_cb, a->cancel_arg);
1142 static const struct got_error *
1143 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1145 const struct got_error *err;
1146 struct diff_cb_arg *a = arg;
1148 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1150 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1153 got_fileindex_entry_remove(a->fileindex, ie);
1157 static const struct got_error *
1158 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1160 struct diff_cb_arg *a = arg;
1161 const struct got_error *err;
1164 if (asprintf(&path, "%s%s%s", parent_path,
1165 parent_path[0] ? "/" : "", te->name)
1167 return got_error_from_errno();
1169 if (S_ISDIR(te->mode))
1170 err = add_dir_on_disk(a->worktree, path);
1172 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1173 a->repo, a->progress_cb, a->progress_arg,
1174 a->cancel_cb, a->cancel_arg);
1180 const struct got_error *
1181 got_worktree_checkout_files(struct got_worktree *worktree,
1182 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1183 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1185 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1186 struct got_commit_object *commit = NULL;
1187 struct got_object_id *tree_id = NULL;
1188 struct got_tree_object *tree = NULL;
1189 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1190 struct got_fileindex *fileindex = NULL;
1191 FILE *index = NULL, *new_index = NULL;
1192 struct got_fileindex_diff_tree_cb diff_cb;
1193 struct diff_cb_arg arg;
1195 err = lock_worktree(worktree, LOCK_EX);
1199 fileindex = got_fileindex_alloc();
1200 if (fileindex == NULL) {
1201 err = got_error_from_errno();
1205 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1206 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1207 err = got_error_from_errno();
1208 fileindex_path = NULL;
1213 * Read the file index.
1214 * Checking out files is supposed to be an idempotent operation.
1215 * If the on-disk file index is incomplete we will try to complete it.
1217 index = fopen(fileindex_path, "rb");
1218 if (index == NULL) {
1219 if (errno != ENOENT) {
1220 err = got_error_from_errno();
1224 err = got_fileindex_read(fileindex, index);
1230 err = got_opentemp_named(&new_fileindex_path, &new_index,
1235 err = got_object_open_as_commit(&commit, repo,
1236 worktree->base_commit_id);
1240 err = got_object_id_by_path(&tree_id, repo,
1241 worktree->base_commit_id, worktree->path_prefix);
1245 err = got_object_open_as_tree(&tree, repo, tree_id);
1249 diff_cb.diff_old_new = diff_old_new;
1250 diff_cb.diff_old = diff_old;
1251 diff_cb.diff_new = diff_new;
1252 arg.fileindex = fileindex;
1253 arg.worktree = worktree;
1255 arg.progress_cb = progress_cb;
1256 arg.progress_arg = progress_arg;
1257 arg.cancel_cb = cancel_cb;
1258 arg.cancel_arg = cancel_arg;
1259 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1262 /* Try to sync the fileindex back to disk in any case. */
1263 err = got_fileindex_write(fileindex, new_index);
1267 if (rename(new_fileindex_path, fileindex_path) != 0) {
1268 err = got_error_from_errno();
1272 free(new_fileindex_path);
1273 new_fileindex_path = NULL;
1277 got_object_tree_close(tree);
1279 got_object_commit_close(commit);
1280 if (new_fileindex_path)
1281 unlink(new_fileindex_path);
1284 free(new_fileindex_path);
1285 free(fileindex_path);
1286 got_fileindex_free(fileindex);
1289 unlockerr = lock_worktree(worktree, LOCK_SH);
1290 if (unlockerr && err == NULL)
1295 struct diff_dir_cb_arg {
1296 struct got_fileindex *fileindex;
1297 struct got_worktree *worktree;
1298 const char *status_path;
1299 size_t status_path_len;
1300 struct got_repository *repo;
1301 got_worktree_status_cb status_cb;
1303 got_worktree_cancel_cb cancel_cb;
1307 static const struct got_error *
1308 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1309 got_worktree_status_cb status_cb, void *status_arg,
1310 struct got_repository *repo)
1312 const struct got_error *err = NULL;
1313 unsigned char status = GOT_STATUS_NO_CHANGE;
1315 struct got_object_id id;
1317 err = get_file_status(&status, &sb, ie, abspath, repo);
1318 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1319 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1320 err = (*status_cb)(status_arg, status, ie->path, &id);
1325 static const struct got_error *
1326 status_old_new(void *arg, struct got_fileindex_entry *ie,
1327 struct dirent *de, const char *parent_path)
1329 const struct got_error *err = NULL;
1330 struct diff_dir_cb_arg *a = arg;
1333 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1334 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1337 if (parent_path[0]) {
1338 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1339 parent_path, de->d_name) == -1)
1340 return got_error_from_errno();
1342 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1344 return got_error_from_errno();
1347 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1353 static const struct got_error *
1354 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1356 struct diff_dir_cb_arg *a = arg;
1357 struct got_object_id id;
1359 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1362 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1363 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1367 static const struct got_error *
1368 status_new(void *arg, struct dirent *de, const char *parent_path)
1370 const struct got_error *err = NULL;
1371 struct diff_dir_cb_arg *a = arg;
1374 if (de->d_type == DT_DIR)
1377 /* XXX ignore symlinks for now */
1378 if (de->d_type == DT_LNK)
1381 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1384 if (parent_path[0]) {
1385 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1386 return got_error_from_errno();
1391 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1398 const struct got_error *
1399 got_worktree_status(struct got_worktree *worktree, const char *path,
1400 struct got_repository *repo, got_worktree_status_cb status_cb,
1401 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1403 const struct got_error *err = NULL;
1404 DIR *workdir = NULL;
1405 char *fileindex_path = NULL;
1406 struct got_fileindex *fileindex = NULL;
1408 struct got_fileindex_diff_dir_cb fdiff_cb;
1409 struct diff_dir_cb_arg arg;
1410 char *ondisk_path = NULL;
1412 fileindex = got_fileindex_alloc();
1413 if (fileindex == NULL) {
1414 err = got_error_from_errno();
1418 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1419 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1420 err = got_error_from_errno();
1421 fileindex_path = NULL;
1425 index = fopen(fileindex_path, "rb");
1426 if (index == NULL) {
1427 if (errno != ENOENT) {
1428 err = got_error_from_errno();
1432 err = got_fileindex_read(fileindex, index);
1438 if (asprintf(&ondisk_path, "%s%s%s",
1439 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1440 err = got_error_from_errno();
1443 workdir = opendir(ondisk_path);
1444 if (workdir == NULL) {
1445 if (errno == ENOTDIR) {
1446 struct got_fileindex_entry *ie;
1447 ie = got_fileindex_entry_get(fileindex, path);
1449 err = got_error(GOT_ERR_BAD_PATH);
1452 err = report_file_status(ie, ondisk_path,
1453 status_cb, status_arg, repo);
1456 err = got_error_from_errno();
1460 fdiff_cb.diff_old_new = status_old_new;
1461 fdiff_cb.diff_old = status_old;
1462 fdiff_cb.diff_new = status_new;
1463 arg.fileindex = fileindex;
1464 arg.worktree = worktree;
1465 arg.status_path = path;
1466 arg.status_path_len = strlen(path);
1468 arg.status_cb = status_cb;
1469 arg.status_arg = status_arg;
1470 arg.cancel_cb = cancel_cb;
1471 arg.cancel_arg = cancel_arg;
1472 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1473 path, repo, &fdiff_cb, &arg);
1478 free(fileindex_path);
1479 got_fileindex_free(fileindex);