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();
124 if (fclose(tmpfile) != 0 && err == NULL)
125 err = got_error_from_errno();
129 static const struct got_error *
130 read_meta_file(char **content, const char *path_got, const char *name)
132 const struct got_error *err = NULL;
140 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
141 err = got_error_from_errno();
146 fd = open(path, O_RDONLY | O_NOFOLLOW);
148 err = got_error_from_errno();
151 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
152 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
153 : got_error_from_errno());
157 if (lstat(path, &sb) != 0) {
158 err = got_error_from_errno();
161 *content = calloc(1, sb.st_size);
162 if (*content == NULL) {
163 err = got_error_from_errno();
167 n = read(fd, *content, sb.st_size);
168 if (n != sb.st_size) {
169 err = (n == -1 ? got_error_from_errno() :
170 got_error(GOT_ERR_WORKTREE_META));
173 if ((*content)[sb.st_size - 1] != '\n') {
174 err = got_error(GOT_ERR_WORKTREE_META);
177 (*content)[sb.st_size - 1] = '\0';
180 if (fd != -1 && close(fd) == -1 && err == NULL)
181 err = got_error_from_errno();
190 const struct got_error *
191 got_worktree_init(const char *path, struct got_reference *head_ref,
192 const char *prefix, struct got_repository *repo)
194 const struct got_error *err = NULL;
195 struct got_object_id *commit_id = NULL;
197 char *path_got = NULL;
199 char *formatstr = NULL;
200 char *absprefix = NULL;
201 char *basestr = NULL;
203 err = got_ref_resolve(&commit_id, repo, head_ref);
206 err = got_object_get_type(&obj_type, repo, commit_id);
209 if (obj_type != GOT_OBJ_TYPE_COMMIT)
210 return got_error(GOT_ERR_OBJ_TYPE);
212 if (!got_path_is_absolute(prefix)) {
213 if (asprintf(&absprefix, "/%s", prefix) == -1)
214 return got_error_from_errno();
217 /* Create top-level directory (may already exist). */
218 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
219 err = got_error_from_errno();
223 /* Create .got directory (may already exist). */
224 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
225 err = got_error_from_errno();
228 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
229 err = got_error_from_errno();
233 /* Create an empty lock file. */
234 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
238 /* Create an empty file index. */
239 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
243 /* Write the HEAD reference. */
244 refstr = got_ref_to_str(head_ref);
245 if (refstr == NULL) {
246 err = got_error_from_errno();
249 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
253 /* Record our base commit. */
254 err = got_object_id_str(&basestr, commit_id);
257 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
261 /* Store path to repository. */
262 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
263 got_repo_get_path(repo));
267 /* Store in-repository path prefix. */
268 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
269 absprefix ? absprefix : prefix);
273 /* Stamp work tree with format file. */
274 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
275 err = got_error_from_errno();
278 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
292 static const struct got_error *
293 open_worktree(struct got_worktree **worktree, const char *path)
295 const struct got_error *err = NULL;
297 char *formatstr = NULL;
298 char *path_lock = NULL;
299 char *base_commit_id_str = NULL;
300 char *head_ref_str = NULL;
301 int version, fd = -1;
303 struct got_repository *repo = NULL;
307 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
308 err = got_error_from_errno();
313 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
314 err = got_error_from_errno();
319 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
321 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
322 : got_error_from_errno());
326 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
330 version = strtonum(formatstr, 1, INT_MAX, &errstr);
332 err = got_error(GOT_ERR_WORKTREE_META);
335 if (version != GOT_WORKTREE_FORMAT_VERSION) {
336 err = got_error(GOT_ERR_WORKTREE_VERS);
340 *worktree = calloc(1, sizeof(**worktree));
341 if (*worktree == NULL) {
342 err = got_error_from_errno();
345 (*worktree)->lockfd = -1;
347 (*worktree)->root_path = strdup(path);
348 if ((*worktree)->root_path == NULL) {
349 err = got_error_from_errno();
352 err = read_meta_file(&(*worktree)->repo_path, path_got,
353 GOT_WORKTREE_REPOSITORY);
357 err = read_meta_file(&(*worktree)->path_prefix, path_got,
358 GOT_WORKTREE_PATH_PREFIX);
362 err = read_meta_file(&base_commit_id_str, path_got,
363 GOT_WORKTREE_BASE_COMMIT);
367 err = got_repo_open(&repo, (*worktree)->repo_path);
371 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
376 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
380 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
383 got_repo_close(repo);
387 free(base_commit_id_str);
391 if (*worktree != NULL)
392 got_worktree_close(*worktree);
395 (*worktree)->lockfd = fd;
400 const struct got_error *
401 got_worktree_open(struct got_worktree **worktree, const char *path)
403 const struct got_error *err = NULL;
406 err = open_worktree(worktree, path);
407 if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
411 path = dirname(path);
413 return got_error_from_errno();
414 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
416 return got_error(GOT_ERR_NOT_WORKTREE);
419 const struct got_error *
420 got_worktree_close(struct got_worktree *worktree)
422 const struct got_error *err = NULL;
423 free(worktree->root_path);
424 free(worktree->repo_path);
425 free(worktree->path_prefix);
426 free(worktree->base_commit_id);
427 if (worktree->head_ref)
428 got_ref_close(worktree->head_ref);
429 if (worktree->lockfd != -1)
430 if (close(worktree->lockfd) != 0)
431 err = got_error_from_errno();
437 got_worktree_get_root_path(struct got_worktree *worktree)
439 return worktree->root_path;
443 got_worktree_get_repo_path(struct got_worktree *worktree)
445 return worktree->repo_path;
449 got_worktree_get_path_prefix(struct got_worktree *worktree)
451 return worktree->path_prefix;
454 const struct got_error *
455 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
456 const char *path_prefix)
458 char *absprefix = NULL;
460 if (!got_path_is_absolute(path_prefix)) {
461 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
462 return got_error_from_errno();
464 *match = (strcmp(absprefix ? absprefix : path_prefix,
465 worktree->path_prefix) == 0);
471 got_worktree_get_head_ref_name(struct got_worktree *worktree)
473 return got_ref_to_str(worktree->head_ref);
476 struct got_reference *
477 got_worktree_get_head_ref(struct got_worktree *worktree)
479 return got_ref_dup(worktree->head_ref);
482 struct got_object_id *
483 got_worktree_get_base_commit_id(struct got_worktree *worktree)
485 return worktree->base_commit_id;
488 const struct got_error *
489 got_worktree_set_base_commit_id(struct got_worktree *worktree,
490 struct got_repository *repo, struct got_object_id *commit_id)
492 const struct got_error *err;
493 struct got_object *obj = NULL;
495 char *path_got = NULL;
497 if (asprintf(&path_got, "%s/%s", worktree->root_path,
498 GOT_WORKTREE_GOT_DIR) == -1) {
499 err = got_error_from_errno();
504 err = got_object_open(&obj, repo, commit_id);
508 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
509 err = got_error(GOT_ERR_OBJ_TYPE);
513 /* Record our base commit. */
514 err = got_object_id_str(&id_str, commit_id);
517 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
521 free(worktree->base_commit_id);
522 worktree->base_commit_id = got_object_id_dup(commit_id);
523 if (worktree->base_commit_id == NULL) {
524 err = got_error_from_errno();
529 got_object_close(obj);
535 static const struct got_error *
536 lock_worktree(struct got_worktree *worktree, int operation)
538 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
539 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
540 : got_error_from_errno());
544 static const struct got_error *
545 make_parent_dirs(const char *abspath)
547 const struct got_error *err = NULL;
549 char *parent = dirname(abspath);
553 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
554 if (errno == ENOENT) {
555 err = make_parent_dirs(parent);
558 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
559 return got_error_from_errno();
561 err = got_error_from_errno();
567 static const struct got_error *
568 add_dir_on_disk(struct got_worktree *worktree, const char *path)
570 const struct got_error *err = NULL;
573 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
574 return got_error_from_errno();
576 /* XXX queue work rather than editing disk directly? */
577 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
580 if (errno == EEXIST) {
581 if (lstat(abspath, &sb) == -1) {
582 err = got_error_from_errno();
586 if (!S_ISDIR(sb.st_mode)) {
587 /* TODO directory is obstructed; do something */
588 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
593 } else if (errno == ENOENT) {
594 err = make_parent_dirs(abspath);
597 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
598 err = got_error_from_errno();
600 err = got_error_from_errno();
608 static const struct got_error *
609 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
611 const struct got_error *err = NULL;
614 size_t flen1 = 0, flen2 = 0;
619 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
620 if (flen1 == 0 && ferror(f1)) {
621 err = got_error_from_errno();
624 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
625 if (flen2 == 0 && ferror(f2)) {
626 err = got_error_from_errno();
633 } else if (flen2 == 0) {
637 } else if (flen1 == flen2) {
638 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
651 static const struct got_error *
652 check_files_equal(int *same, const char *f1_path, const char *f2_path)
654 const struct got_error *err = NULL;
657 FILE *f1 = NULL, *f2 = NULL;
661 if (lstat(f1_path, &sb) != 0) {
662 err = got_error_from_errno();
667 if (lstat(f2_path, &sb) != 0) {
668 err = got_error_from_errno();
673 if (size1 != size2) {
678 f1 = fopen(f1_path, "r");
680 return got_error_from_errno();
682 f2 = fopen(f2_path, "r");
684 err = got_error_from_errno();
688 err = check_file_contents_equal(same, f1, f2);
690 if (f1 && fclose(f1) != 0 && err == NULL)
691 err = got_error_from_errno();
692 if (f2 && fclose(f2) != 0 && err == NULL)
693 err = got_error_from_errno();
699 * Perform a 3-way merge where the file's version in the file index (blob2)
700 * acts as the common ancestor, the incoming blob (blob1) acts as the first
701 * derived version, and the file on disk acts as the second derived version.
703 static const struct got_error *
704 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
705 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
706 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
707 struct got_repository *repo,
708 got_worktree_checkout_cb progress_cb, void *progress_arg)
710 const struct got_error *err = NULL;
712 struct got_blob_object *blob2 = NULL;
713 FILE *f1 = NULL, *f2 = NULL;
714 char *blob1_path = NULL, *blob2_path = NULL;
715 char *merged_path = NULL, *base_path = NULL;
716 struct got_object_id id2;
719 int overlapcnt = 0, update_timestamps = 0;
722 parent = dirname(ondisk_path);
724 return got_error_from_errno();
726 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
727 return got_error_from_errno();
729 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
734 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
735 err = got_error_from_errno();
740 err = got_opentemp_named(&blob1_path, &f1, base_path);
743 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
748 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
749 err = got_error_from_errno();
754 err = got_opentemp_named(&blob2_path, &f2, base_path);
758 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
759 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
762 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
766 err = got_object_id_str(&id_str, worktree->base_commit_id);
769 if (asprintf(&label1, "commit %s", id_str) == -1) {
770 err = got_error_from_errno();
774 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
775 blob2_path, ondisk_path, label1, path);
779 (*progress_cb)(progress_arg,
780 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
783 if (fsync(merged_fd) != 0) {
784 err = got_error_from_errno();
788 /* Check if a clean merge has subsumed all local changes. */
789 if (overlapcnt == 0) {
790 err = check_files_equal(&update_timestamps, blob1_path,
796 if (rename(merged_path, ondisk_path) != 0) {
797 err = got_error_from_errno();
803 * Do not update timestamps of already modified files. Otherwise,
804 * a future status walk would treat them as unmodified files again.
806 err = got_fileindex_entry_update(ie, ondisk_path,
807 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
809 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
810 err = got_error_from_errno();
811 if (f1 && fclose(f1) != 0 && err == NULL)
812 err = got_error_from_errno();
813 if (f2 && fclose(f2) != 0 && err == NULL)
814 err = got_error_from_errno();
816 got_object_blob_close(blob2);
832 static const struct got_error *
833 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
834 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
835 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
836 int restoring_missing_file, struct got_repository *repo,
837 got_worktree_checkout_cb progress_cb, void *progress_arg)
839 const struct got_error *err = NULL;
843 char *tmppath = NULL;
845 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
846 GOT_DEFAULT_FILE_MODE);
848 if (errno == ENOENT) {
849 char *parent = dirname(path);
851 return got_error_from_errno();
852 err = add_dir_on_disk(worktree, parent);
855 fd = open(ondisk_path,
856 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
857 GOT_DEFAULT_FILE_MODE);
859 return got_error_from_errno();
860 } else if (errno == EEXIST) {
861 if (!S_ISREG(st_mode)) {
862 /* TODO file is obstructed; do something */
863 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
866 err = got_opentemp_named_fd(&tmppath, &fd,
873 return got_error_from_errno();
876 if (restoring_missing_file)
877 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
879 (*progress_cb)(progress_arg,
880 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
882 hdrlen = got_object_blob_get_hdrlen(blob);
884 const uint8_t *buf = got_object_blob_get_read_buf(blob);
885 err = got_object_blob_read_block(&len, blob);
889 /* Skip blob object header first time around. */
890 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
892 err = got_error_from_errno();
894 } else if (outlen != len - hdrlen) {
895 err = got_error(GOT_ERR_IO);
902 if (fsync(fd) != 0) {
903 err = got_error_from_errno();
908 if (rename(tmppath, ondisk_path) != 0) {
909 err = got_error_from_errno();
915 if (te_mode & S_IXUSR) {
916 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
917 err = got_error_from_errno();
921 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
922 err = got_error_from_errno();
928 entry = got_fileindex_entry_get(fileindex, path);
930 err = got_fileindex_entry_update(entry, ondisk_path,
931 blob->id.sha1, worktree->base_commit_id->sha1, 1);
933 err = got_fileindex_entry_alloc(&entry, ondisk_path,
934 path, blob->id.sha1, worktree->base_commit_id->sha1);
937 err = got_fileindex_entry_add(fileindex, entry);
940 if (fd != -1 && close(fd) != 0 && err == NULL)
941 err = got_error_from_errno();
946 static const struct got_error *
947 get_file_status(unsigned char *status, struct stat *sb,
948 struct got_fileindex_entry *ie, const char *abspath,
949 struct got_repository *repo)
951 const struct got_error *err = NULL;
952 struct got_object_id id;
956 struct got_blob_object *blob = NULL;
959 *status = GOT_STATUS_NO_CHANGE;
961 if (lstat(abspath, sb) == -1) {
962 if (errno == ENOENT) {
964 *status = GOT_STATUS_MISSING;
966 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
967 & (S_IRWXU | S_IRWXG | S_IRWXO));
969 sb->st_mode = GOT_DEFAULT_FILE_MODE;
972 return got_error_from_errno();
975 if (!S_ISREG(sb->st_mode)) {
976 *status = GOT_STATUS_OBSTRUCTED;
983 if (ie->ctime_sec == sb->st_ctime &&
984 ie->ctime_nsec == sb->st_ctimensec &&
985 ie->mtime_sec == sb->st_mtime &&
986 ie->mtime_sec == sb->st_mtime &&
987 ie->mtime_nsec == sb->st_mtimensec &&
988 ie->size == (sb->st_size & 0xffffffff))
991 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
992 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
996 f = fopen(abspath, "r");
998 err = got_error_from_errno();
1001 hdrlen = got_object_blob_get_hdrlen(blob);
1003 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1004 err = got_object_blob_read_block(&blen, blob);
1007 /* Skip length of blob object header first time around. */
1008 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1009 if (flen == 0 && ferror(f)) {
1010 err = got_error_from_errno();
1015 *status = GOT_STATUS_MODIFY;
1017 } else if (flen == 0) {
1019 *status = GOT_STATUS_MODIFY;
1021 } else if (blen - hdrlen == flen) {
1022 /* Skip blob object header first time around. */
1023 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1024 *status = GOT_STATUS_MODIFY;
1028 *status = GOT_STATUS_MODIFY;
1035 got_object_blob_close(blob);
1041 static const struct got_error *
1042 update_blob(struct got_worktree *worktree,
1043 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1044 struct got_tree_entry *te, const char *path,
1045 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1046 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1048 const struct got_error *err = NULL;
1049 struct got_blob_object *blob = NULL;
1051 unsigned char status = GOT_STATUS_NO_CHANGE;
1054 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1055 return got_error_from_errno();
1057 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1061 if (status == GOT_STATUS_OBSTRUCTED) {
1062 (*progress_cb)(progress_arg, status, path);
1066 if (ie && status != GOT_STATUS_MISSING) {
1067 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1068 SHA1_DIGEST_LENGTH) == 0) {
1069 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1073 if (memcmp(ie->blob_sha1,
1074 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1078 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1082 if (status == GOT_STATUS_MODIFY)
1083 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1084 te->mode, sb.st_mode, blob, repo, progress_cb,
1087 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1088 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1089 repo, progress_cb, progress_arg);
1091 got_object_blob_close(blob);
1097 static const struct got_error *
1098 remove_ondisk_file(const char *root_path, const char *path)
1100 const struct got_error *err = NULL;
1101 char *ondisk_path = NULL;
1103 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1104 return got_error_from_errno();
1106 if (unlink(ondisk_path) == -1) {
1107 if (errno != ENOENT)
1108 err = got_error_from_errno();
1110 char *parent = dirname(ondisk_path);
1111 while (parent && strcmp(parent, root_path) != 0) {
1112 if (rmdir(parent) == -1) {
1113 if (errno != ENOTEMPTY)
1114 err = got_error_from_errno();
1117 parent = dirname(parent);
1124 struct diff_cb_arg {
1125 struct got_fileindex *fileindex;
1126 struct got_worktree *worktree;
1127 struct got_repository *repo;
1128 got_worktree_checkout_cb progress_cb;
1130 got_worktree_cancel_cb cancel_cb;
1134 static const struct got_error *
1135 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1136 struct got_tree_entry *te, const char *parent_path)
1138 struct diff_cb_arg *a = arg;
1140 return update_blob(a->worktree, a->fileindex, ie, te,
1141 ie->path, a->repo, a->progress_cb, a->progress_arg,
1142 a->cancel_cb, a->cancel_arg);
1145 static const struct got_error *
1146 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1148 const struct got_error *err;
1149 struct diff_cb_arg *a = arg;
1151 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1153 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1156 got_fileindex_entry_remove(a->fileindex, ie);
1160 static const struct got_error *
1161 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1163 struct diff_cb_arg *a = arg;
1164 const struct got_error *err;
1167 if (asprintf(&path, "%s%s%s", parent_path,
1168 parent_path[0] ? "/" : "", te->name)
1170 return got_error_from_errno();
1172 if (S_ISDIR(te->mode))
1173 err = add_dir_on_disk(a->worktree, path);
1175 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1176 a->repo, a->progress_cb, a->progress_arg,
1177 a->cancel_cb, a->cancel_arg);
1183 const struct got_error *
1184 got_worktree_checkout_files(struct got_worktree *worktree,
1185 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1186 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1188 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1189 struct got_commit_object *commit = NULL;
1190 struct got_object_id *tree_id = NULL;
1191 struct got_tree_object *tree = NULL;
1192 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1193 struct got_fileindex *fileindex = NULL;
1194 FILE *index = NULL, *new_index = NULL;
1195 struct got_fileindex_diff_tree_cb diff_cb;
1196 struct diff_cb_arg arg;
1198 err = lock_worktree(worktree, LOCK_EX);
1202 fileindex = got_fileindex_alloc();
1203 if (fileindex == NULL) {
1204 err = got_error_from_errno();
1208 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1209 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1210 err = got_error_from_errno();
1211 fileindex_path = NULL;
1216 * Read the file index.
1217 * Checking out files is supposed to be an idempotent operation.
1218 * If the on-disk file index is incomplete we will try to complete it.
1220 index = fopen(fileindex_path, "rb");
1221 if (index == NULL) {
1222 if (errno != ENOENT) {
1223 err = got_error_from_errno();
1227 err = got_fileindex_read(fileindex, index);
1233 err = got_opentemp_named(&new_fileindex_path, &new_index,
1238 err = got_object_open_as_commit(&commit, repo,
1239 worktree->base_commit_id);
1243 err = got_object_id_by_path(&tree_id, repo,
1244 worktree->base_commit_id, worktree->path_prefix);
1248 err = got_object_open_as_tree(&tree, repo, tree_id);
1252 diff_cb.diff_old_new = diff_old_new;
1253 diff_cb.diff_old = diff_old;
1254 diff_cb.diff_new = diff_new;
1255 arg.fileindex = fileindex;
1256 arg.worktree = worktree;
1258 arg.progress_cb = progress_cb;
1259 arg.progress_arg = progress_arg;
1260 arg.cancel_cb = cancel_cb;
1261 arg.cancel_arg = cancel_arg;
1262 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1265 /* Try to sync the fileindex back to disk in any case. */
1266 err = got_fileindex_write(fileindex, new_index);
1270 if (rename(new_fileindex_path, fileindex_path) != 0) {
1271 err = got_error_from_errno();
1272 unlink(new_fileindex_path);
1276 free(new_fileindex_path);
1277 new_fileindex_path = NULL;
1281 got_object_tree_close(tree);
1283 got_object_commit_close(commit);
1284 if (new_fileindex_path)
1285 unlink(new_fileindex_path);
1288 free(new_fileindex_path);
1289 free(fileindex_path);
1290 got_fileindex_free(fileindex);
1293 unlockerr = lock_worktree(worktree, LOCK_SH);
1294 if (unlockerr && err == NULL)
1299 struct diff_dir_cb_arg {
1300 struct got_fileindex *fileindex;
1301 struct got_worktree *worktree;
1302 const char *status_path;
1303 size_t status_path_len;
1304 struct got_repository *repo;
1305 got_worktree_status_cb status_cb;
1307 got_worktree_cancel_cb cancel_cb;
1311 static const struct got_error *
1312 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1313 got_worktree_status_cb status_cb, void *status_arg,
1314 struct got_repository *repo)
1316 const struct got_error *err = NULL;
1317 unsigned char status = GOT_STATUS_NO_CHANGE;
1319 struct got_object_id id;
1321 err = get_file_status(&status, &sb, ie, abspath, repo);
1322 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1323 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1324 err = (*status_cb)(status_arg, status, ie->path, &id);
1329 static const struct got_error *
1330 status_old_new(void *arg, struct got_fileindex_entry *ie,
1331 struct dirent *de, const char *parent_path)
1333 const struct got_error *err = NULL;
1334 struct diff_dir_cb_arg *a = arg;
1337 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1338 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1341 if (parent_path[0]) {
1342 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1343 parent_path, de->d_name) == -1)
1344 return got_error_from_errno();
1346 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1348 return got_error_from_errno();
1351 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1357 static const struct got_error *
1358 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1360 struct diff_dir_cb_arg *a = arg;
1361 struct got_object_id id;
1363 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1366 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1367 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1371 static const struct got_error *
1372 status_new(void *arg, struct dirent *de, const char *parent_path)
1374 const struct got_error *err = NULL;
1375 struct diff_dir_cb_arg *a = arg;
1378 if (de->d_type == DT_DIR)
1381 /* XXX ignore symlinks for now */
1382 if (de->d_type == DT_LNK)
1385 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1388 if (parent_path[0]) {
1389 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1390 return got_error_from_errno();
1395 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1402 const struct got_error *
1403 got_worktree_status(struct got_worktree *worktree, const char *path,
1404 struct got_repository *repo, got_worktree_status_cb status_cb,
1405 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1407 const struct got_error *err = NULL;
1408 DIR *workdir = NULL;
1409 char *fileindex_path = NULL;
1410 struct got_fileindex *fileindex = NULL;
1412 struct got_fileindex_diff_dir_cb fdiff_cb;
1413 struct diff_dir_cb_arg arg;
1414 char *ondisk_path = NULL;
1416 fileindex = got_fileindex_alloc();
1417 if (fileindex == NULL) {
1418 err = got_error_from_errno();
1422 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1423 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1424 err = got_error_from_errno();
1425 fileindex_path = NULL;
1429 index = fopen(fileindex_path, "rb");
1430 if (index == NULL) {
1431 if (errno != ENOENT) {
1432 err = got_error_from_errno();
1436 err = got_fileindex_read(fileindex, index);
1442 if (asprintf(&ondisk_path, "%s%s%s",
1443 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1444 err = got_error_from_errno();
1447 workdir = opendir(ondisk_path);
1448 if (workdir == NULL) {
1449 if (errno == ENOTDIR) {
1450 struct got_fileindex_entry *ie;
1451 ie = got_fileindex_entry_get(fileindex, path);
1453 err = got_error(GOT_ERR_BAD_PATH);
1456 err = report_file_status(ie, ondisk_path,
1457 status_cb, status_arg, repo);
1460 err = got_error_from_errno();
1464 fdiff_cb.diff_old_new = status_old_new;
1465 fdiff_cb.diff_old = status_old;
1466 fdiff_cb.diff_new = status_new;
1467 arg.fileindex = fileindex;
1468 arg.worktree = worktree;
1469 arg.status_path = path;
1470 arg.status_path_len = strlen(path);
1472 arg.status_cb = status_cb;
1473 arg.status_arg = status_arg;
1474 arg.cancel_cb = cancel_cb;
1475 arg.cancel_arg = cancel_arg;
1476 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1477 path, repo, &fdiff_cb, &arg);
1482 free(fileindex_path);
1483 got_fileindex_free(fileindex);