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 (chmod(merged_path, st_mode) != 0) {
797 err = got_error_from_errno();
801 if (rename(merged_path, ondisk_path) != 0) {
802 err = got_error_from_errno();
808 * Do not update timestamps of already modified files. Otherwise,
809 * a future status walk would treat them as unmodified files again.
811 err = got_fileindex_entry_update(ie, ondisk_path,
812 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
814 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
815 err = got_error_from_errno();
816 if (f1 && fclose(f1) != 0 && err == NULL)
817 err = got_error_from_errno();
818 if (f2 && fclose(f2) != 0 && err == NULL)
819 err = got_error_from_errno();
821 got_object_blob_close(blob2);
837 static const struct got_error *
838 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
839 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
840 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
841 int restoring_missing_file, struct got_repository *repo,
842 got_worktree_checkout_cb progress_cb, void *progress_arg)
844 const struct got_error *err = NULL;
848 char *tmppath = NULL;
850 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
851 GOT_DEFAULT_FILE_MODE);
853 if (errno == ENOENT) {
854 char *parent = dirname(path);
856 return got_error_from_errno();
857 err = add_dir_on_disk(worktree, parent);
860 fd = open(ondisk_path,
861 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
862 GOT_DEFAULT_FILE_MODE);
864 return got_error_from_errno();
865 } else if (errno == EEXIST) {
866 if (!S_ISREG(st_mode)) {
867 /* TODO file is obstructed; do something */
868 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
871 err = got_opentemp_named_fd(&tmppath, &fd,
878 return got_error_from_errno();
881 if (restoring_missing_file)
882 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
884 (*progress_cb)(progress_arg,
885 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
887 hdrlen = got_object_blob_get_hdrlen(blob);
889 const uint8_t *buf = got_object_blob_get_read_buf(blob);
890 err = got_object_blob_read_block(&len, blob);
894 /* Skip blob object header first time around. */
895 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
897 err = got_error_from_errno();
899 } else if (outlen != len - hdrlen) {
900 err = got_error(GOT_ERR_IO);
907 if (fsync(fd) != 0) {
908 err = got_error_from_errno();
913 if (rename(tmppath, ondisk_path) != 0) {
914 err = got_error_from_errno();
920 if (te_mode & S_IXUSR) {
921 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
922 err = got_error_from_errno();
926 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
927 err = got_error_from_errno();
933 entry = got_fileindex_entry_get(fileindex, path);
935 err = got_fileindex_entry_update(entry, ondisk_path,
936 blob->id.sha1, worktree->base_commit_id->sha1, 1);
938 err = got_fileindex_entry_alloc(&entry, ondisk_path,
939 path, blob->id.sha1, worktree->base_commit_id->sha1);
942 err = got_fileindex_entry_add(fileindex, entry);
945 if (fd != -1 && close(fd) != 0 && err == NULL)
946 err = got_error_from_errno();
951 static const struct got_error *
952 get_file_status(unsigned char *status, struct stat *sb,
953 struct got_fileindex_entry *ie, const char *abspath,
954 struct got_repository *repo)
956 const struct got_error *err = NULL;
957 struct got_object_id id;
961 struct got_blob_object *blob = NULL;
964 *status = GOT_STATUS_NO_CHANGE;
966 if (lstat(abspath, sb) == -1) {
967 if (errno == ENOENT) {
969 *status = GOT_STATUS_MISSING;
971 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
972 & (S_IRWXU | S_IRWXG | S_IRWXO));
974 sb->st_mode = GOT_DEFAULT_FILE_MODE;
977 return got_error_from_errno();
980 if (!S_ISREG(sb->st_mode)) {
981 *status = GOT_STATUS_OBSTRUCTED;
988 if (ie->ctime_sec == sb->st_ctime &&
989 ie->ctime_nsec == sb->st_ctimensec &&
990 ie->mtime_sec == sb->st_mtime &&
991 ie->mtime_sec == sb->st_mtime &&
992 ie->mtime_nsec == sb->st_mtimensec &&
993 ie->size == (sb->st_size & 0xffffffff))
996 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
997 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1001 f = fopen(abspath, "r");
1003 err = got_error_from_errno();
1006 hdrlen = got_object_blob_get_hdrlen(blob);
1008 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1009 err = got_object_blob_read_block(&blen, blob);
1012 /* Skip length of blob object header first time around. */
1013 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1014 if (flen == 0 && ferror(f)) {
1015 err = got_error_from_errno();
1020 *status = GOT_STATUS_MODIFY;
1022 } else if (flen == 0) {
1024 *status = GOT_STATUS_MODIFY;
1026 } else if (blen - hdrlen == flen) {
1027 /* Skip blob object header first time around. */
1028 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1029 *status = GOT_STATUS_MODIFY;
1033 *status = GOT_STATUS_MODIFY;
1040 got_object_blob_close(blob);
1046 static const struct got_error *
1047 update_blob(struct got_worktree *worktree,
1048 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1049 struct got_tree_entry *te, const char *path,
1050 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1051 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1053 const struct got_error *err = NULL;
1054 struct got_blob_object *blob = NULL;
1056 unsigned char status = GOT_STATUS_NO_CHANGE;
1059 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1060 return got_error_from_errno();
1062 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1066 if (status == GOT_STATUS_OBSTRUCTED) {
1067 (*progress_cb)(progress_arg, status, path);
1071 if (ie && status != GOT_STATUS_MISSING) {
1072 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1073 SHA1_DIGEST_LENGTH) == 0) {
1074 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1078 if (memcmp(ie->blob_sha1,
1079 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1083 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1087 if (status == GOT_STATUS_MODIFY)
1088 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1089 te->mode, sb.st_mode, blob, repo, progress_cb,
1092 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1093 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1094 repo, progress_cb, progress_arg);
1096 got_object_blob_close(blob);
1102 static const struct got_error *
1103 remove_ondisk_file(const char *root_path, const char *path)
1105 const struct got_error *err = NULL;
1106 char *ondisk_path = NULL;
1108 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1109 return got_error_from_errno();
1111 if (unlink(ondisk_path) == -1) {
1112 if (errno != ENOENT)
1113 err = got_error_from_errno();
1115 char *parent = dirname(ondisk_path);
1116 while (parent && strcmp(parent, root_path) != 0) {
1117 if (rmdir(parent) == -1) {
1118 if (errno != ENOTEMPTY)
1119 err = got_error_from_errno();
1122 parent = dirname(parent);
1129 struct diff_cb_arg {
1130 struct got_fileindex *fileindex;
1131 struct got_worktree *worktree;
1132 struct got_repository *repo;
1133 got_worktree_checkout_cb progress_cb;
1135 got_worktree_cancel_cb cancel_cb;
1139 static const struct got_error *
1140 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1141 struct got_tree_entry *te, const char *parent_path)
1143 struct diff_cb_arg *a = arg;
1145 return update_blob(a->worktree, a->fileindex, ie, te,
1146 ie->path, a->repo, a->progress_cb, a->progress_arg,
1147 a->cancel_cb, a->cancel_arg);
1150 static const struct got_error *
1151 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1153 const struct got_error *err;
1154 struct diff_cb_arg *a = arg;
1156 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1158 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1161 got_fileindex_entry_remove(a->fileindex, ie);
1165 static const struct got_error *
1166 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1168 struct diff_cb_arg *a = arg;
1169 const struct got_error *err;
1172 if (asprintf(&path, "%s%s%s", parent_path,
1173 parent_path[0] ? "/" : "", te->name)
1175 return got_error_from_errno();
1177 if (S_ISDIR(te->mode))
1178 err = add_dir_on_disk(a->worktree, path);
1180 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1181 a->repo, a->progress_cb, a->progress_arg,
1182 a->cancel_cb, a->cancel_arg);
1188 const struct got_error *
1189 got_worktree_checkout_files(struct got_worktree *worktree,
1190 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1191 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1193 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1194 struct got_commit_object *commit = NULL;
1195 struct got_object_id *tree_id = NULL;
1196 struct got_tree_object *tree = NULL;
1197 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1198 struct got_fileindex *fileindex = NULL;
1199 FILE *index = NULL, *new_index = NULL;
1200 struct got_fileindex_diff_tree_cb diff_cb;
1201 struct diff_cb_arg arg;
1203 err = lock_worktree(worktree, LOCK_EX);
1207 fileindex = got_fileindex_alloc();
1208 if (fileindex == NULL) {
1209 err = got_error_from_errno();
1213 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1214 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1215 err = got_error_from_errno();
1216 fileindex_path = NULL;
1221 * Read the file index.
1222 * Checking out files is supposed to be an idempotent operation.
1223 * If the on-disk file index is incomplete we will try to complete it.
1225 index = fopen(fileindex_path, "rb");
1226 if (index == NULL) {
1227 if (errno != ENOENT) {
1228 err = got_error_from_errno();
1232 err = got_fileindex_read(fileindex, index);
1238 err = got_opentemp_named(&new_fileindex_path, &new_index,
1243 err = got_object_open_as_commit(&commit, repo,
1244 worktree->base_commit_id);
1248 err = got_object_id_by_path(&tree_id, repo,
1249 worktree->base_commit_id, worktree->path_prefix);
1253 err = got_object_open_as_tree(&tree, repo, tree_id);
1257 diff_cb.diff_old_new = diff_old_new;
1258 diff_cb.diff_old = diff_old;
1259 diff_cb.diff_new = diff_new;
1260 arg.fileindex = fileindex;
1261 arg.worktree = worktree;
1263 arg.progress_cb = progress_cb;
1264 arg.progress_arg = progress_arg;
1265 arg.cancel_cb = cancel_cb;
1266 arg.cancel_arg = cancel_arg;
1267 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1270 /* Try to sync the fileindex back to disk in any case. */
1271 err = got_fileindex_write(fileindex, new_index);
1275 if (rename(new_fileindex_path, fileindex_path) != 0) {
1276 err = got_error_from_errno();
1277 unlink(new_fileindex_path);
1281 free(new_fileindex_path);
1282 new_fileindex_path = NULL;
1286 got_object_tree_close(tree);
1288 got_object_commit_close(commit);
1289 if (new_fileindex_path)
1290 unlink(new_fileindex_path);
1293 free(new_fileindex_path);
1294 free(fileindex_path);
1295 got_fileindex_free(fileindex);
1298 unlockerr = lock_worktree(worktree, LOCK_SH);
1299 if (unlockerr && err == NULL)
1304 struct diff_dir_cb_arg {
1305 struct got_fileindex *fileindex;
1306 struct got_worktree *worktree;
1307 const char *status_path;
1308 size_t status_path_len;
1309 struct got_repository *repo;
1310 got_worktree_status_cb status_cb;
1312 got_worktree_cancel_cb cancel_cb;
1316 static const struct got_error *
1317 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1318 got_worktree_status_cb status_cb, void *status_arg,
1319 struct got_repository *repo)
1321 const struct got_error *err = NULL;
1322 unsigned char status = GOT_STATUS_NO_CHANGE;
1324 struct got_object_id id;
1326 err = get_file_status(&status, &sb, ie, abspath, repo);
1327 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1328 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1329 err = (*status_cb)(status_arg, status, ie->path, &id);
1334 static const struct got_error *
1335 status_old_new(void *arg, struct got_fileindex_entry *ie,
1336 struct dirent *de, const char *parent_path)
1338 const struct got_error *err = NULL;
1339 struct diff_dir_cb_arg *a = arg;
1342 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1343 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1346 if (parent_path[0]) {
1347 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1348 parent_path, de->d_name) == -1)
1349 return got_error_from_errno();
1351 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1353 return got_error_from_errno();
1356 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1362 static const struct got_error *
1363 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1365 struct diff_dir_cb_arg *a = arg;
1366 struct got_object_id id;
1368 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1371 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1372 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1376 static const struct got_error *
1377 status_new(void *arg, struct dirent *de, const char *parent_path)
1379 const struct got_error *err = NULL;
1380 struct diff_dir_cb_arg *a = arg;
1383 if (de->d_type == DT_DIR)
1386 /* XXX ignore symlinks for now */
1387 if (de->d_type == DT_LNK)
1390 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1393 if (parent_path[0]) {
1394 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1395 return got_error_from_errno();
1400 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1407 const struct got_error *
1408 got_worktree_status(struct got_worktree *worktree, const char *path,
1409 struct got_repository *repo, got_worktree_status_cb status_cb,
1410 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1412 const struct got_error *err = NULL;
1413 DIR *workdir = NULL;
1414 char *fileindex_path = NULL;
1415 struct got_fileindex *fileindex = NULL;
1417 struct got_fileindex_diff_dir_cb fdiff_cb;
1418 struct diff_dir_cb_arg arg;
1419 char *ondisk_path = NULL;
1421 fileindex = got_fileindex_alloc();
1422 if (fileindex == NULL) {
1423 err = got_error_from_errno();
1427 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1428 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1429 err = got_error_from_errno();
1430 fileindex_path = NULL;
1434 index = fopen(fileindex_path, "rb");
1435 if (index == NULL) {
1436 if (errno != ENOENT) {
1437 err = got_error_from_errno();
1441 err = got_fileindex_read(fileindex, index);
1447 if (asprintf(&ondisk_path, "%s%s%s",
1448 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1449 err = got_error_from_errno();
1452 workdir = opendir(ondisk_path);
1453 if (workdir == NULL) {
1454 if (errno == ENOTDIR) {
1455 struct got_fileindex_entry *ie;
1456 ie = got_fileindex_entry_get(fileindex, path);
1458 err = got_error(GOT_ERR_BAD_PATH);
1461 err = report_file_status(ie, ondisk_path,
1462 status_cb, status_arg, repo);
1465 err = got_error_from_errno();
1469 fdiff_cb.diff_old_new = status_old_new;
1470 fdiff_cb.diff_old = status_old;
1471 fdiff_cb.diff_new = status_new;
1472 arg.fileindex = fileindex;
1473 arg.worktree = worktree;
1474 arg.status_path = path;
1475 arg.status_path_len = strlen(path);
1477 arg.status_cb = status_cb;
1478 arg.status_arg = status_arg;
1479 arg.cancel_cb = cancel_cb;
1480 arg.cancel_arg = cancel_arg;
1481 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1482 path, repo, &fdiff_cb, &arg);
1487 free(fileindex_path);
1488 got_fileindex_free(fileindex);