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());
157 *content = calloc(1, sb.st_size);
158 if (*content == NULL) {
159 err = got_error_from_errno();
163 n = read(fd, *content, sb.st_size);
164 if (n != sb.st_size) {
165 err = (n == -1 ? got_error_from_errno() :
166 got_error(GOT_ERR_WORKTREE_META));
169 if ((*content)[sb.st_size - 1] != '\n') {
170 err = got_error(GOT_ERR_WORKTREE_META);
173 (*content)[sb.st_size - 1] = '\0';
176 if (fd != -1 && close(fd) == -1 && err == NULL)
177 err = got_error_from_errno();
186 const struct got_error *
187 got_worktree_init(const char *path, struct got_reference *head_ref,
188 const char *prefix, struct got_repository *repo)
190 const struct got_error *err = NULL;
191 struct got_object_id *commit_id = NULL;
193 char *path_got = NULL;
195 char *formatstr = NULL;
196 char *absprefix = NULL;
197 char *basestr = NULL;
199 err = got_ref_resolve(&commit_id, repo, head_ref);
202 err = got_object_get_type(&obj_type, repo, commit_id);
205 if (obj_type != GOT_OBJ_TYPE_COMMIT)
206 return got_error(GOT_ERR_OBJ_TYPE);
208 if (!got_path_is_absolute(prefix)) {
209 if (asprintf(&absprefix, "/%s", prefix) == -1)
210 return got_error_from_errno();
213 /* Create top-level directory (may already exist). */
214 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
215 err = got_error_from_errno();
219 /* Create .got directory (may already exist). */
220 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
221 err = got_error_from_errno();
224 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
225 err = got_error_from_errno();
229 /* Create an empty lock file. */
230 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
234 /* Create an empty file index. */
235 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
239 /* Write the HEAD reference. */
240 refstr = got_ref_to_str(head_ref);
241 if (refstr == NULL) {
242 err = got_error_from_errno();
245 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
249 /* Record our base commit. */
250 err = got_object_id_str(&basestr, commit_id);
253 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
257 /* Store path to repository. */
258 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
259 got_repo_get_path(repo));
263 /* Store in-repository path prefix. */
264 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
265 absprefix ? absprefix : prefix);
269 /* Stamp work tree with format file. */
270 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
271 err = got_error_from_errno();
274 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
288 static const struct got_error *
289 open_worktree(struct got_worktree **worktree, const char *path)
291 const struct got_error *err = NULL;
293 char *formatstr = NULL;
294 char *path_lock = NULL;
295 char *base_commit_id_str = NULL;
296 char *head_ref_str = NULL;
297 int version, fd = -1;
299 struct got_repository *repo = NULL;
303 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
304 err = got_error_from_errno();
309 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
310 err = got_error_from_errno();
315 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
317 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
318 : got_error_from_errno());
322 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
326 version = strtonum(formatstr, 1, INT_MAX, &errstr);
328 err = got_error(GOT_ERR_WORKTREE_META);
331 if (version != GOT_WORKTREE_FORMAT_VERSION) {
332 err = got_error(GOT_ERR_WORKTREE_VERS);
336 *worktree = calloc(1, sizeof(**worktree));
337 if (*worktree == NULL) {
338 err = got_error_from_errno();
341 (*worktree)->lockfd = -1;
343 (*worktree)->root_path = strdup(path);
344 if ((*worktree)->root_path == NULL) {
345 err = got_error_from_errno();
348 err = read_meta_file(&(*worktree)->repo_path, path_got,
349 GOT_WORKTREE_REPOSITORY);
353 err = read_meta_file(&(*worktree)->path_prefix, path_got,
354 GOT_WORKTREE_PATH_PREFIX);
358 err = read_meta_file(&base_commit_id_str, path_got,
359 GOT_WORKTREE_BASE_COMMIT);
363 err = got_repo_open(&repo, (*worktree)->repo_path);
367 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
372 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
376 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
379 got_repo_close(repo);
383 free(base_commit_id_str);
387 if (*worktree != NULL)
388 got_worktree_close(*worktree);
391 (*worktree)->lockfd = fd;
396 const struct got_error *
397 got_worktree_open(struct got_worktree **worktree, const char *path)
399 const struct got_error *err = NULL;
402 err = open_worktree(worktree, path);
403 if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
407 path = dirname(path);
409 return got_error_from_errno();
410 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
412 return got_error(GOT_ERR_NOT_WORKTREE);
415 const struct got_error *
416 got_worktree_close(struct got_worktree *worktree)
418 const struct got_error *err = NULL;
419 free(worktree->root_path);
420 free(worktree->repo_path);
421 free(worktree->path_prefix);
422 free(worktree->base_commit_id);
423 if (worktree->head_ref)
424 got_ref_close(worktree->head_ref);
425 if (worktree->lockfd != -1)
426 if (close(worktree->lockfd) != 0)
427 err = got_error_from_errno();
433 got_worktree_get_root_path(struct got_worktree *worktree)
435 return worktree->root_path;
439 got_worktree_get_repo_path(struct got_worktree *worktree)
441 return worktree->repo_path;
445 got_worktree_get_path_prefix(struct got_worktree *worktree)
447 return worktree->path_prefix;
450 const struct got_error *
451 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
452 const char *path_prefix)
454 char *absprefix = NULL;
456 if (!got_path_is_absolute(path_prefix)) {
457 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
458 return got_error_from_errno();
460 *match = (strcmp(absprefix ? absprefix : path_prefix,
461 worktree->path_prefix) == 0);
467 got_worktree_get_head_ref_name(struct got_worktree *worktree)
469 return got_ref_to_str(worktree->head_ref);
472 struct got_reference *
473 got_worktree_get_head_ref(struct got_worktree *worktree)
475 return got_ref_dup(worktree->head_ref);
478 struct got_object_id *
479 got_worktree_get_base_commit_id(struct got_worktree *worktree)
481 return worktree->base_commit_id;
484 const struct got_error *
485 got_worktree_set_base_commit_id(struct got_worktree *worktree,
486 struct got_repository *repo, struct got_object_id *commit_id)
488 const struct got_error *err;
489 struct got_object *obj = NULL;
491 char *path_got = NULL;
493 if (asprintf(&path_got, "%s/%s", worktree->root_path,
494 GOT_WORKTREE_GOT_DIR) == -1) {
495 err = got_error_from_errno();
500 err = got_object_open(&obj, repo, commit_id);
504 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
505 err = got_error(GOT_ERR_OBJ_TYPE);
509 /* Record our base commit. */
510 err = got_object_id_str(&id_str, commit_id);
513 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
517 free(worktree->base_commit_id);
518 worktree->base_commit_id = got_object_id_dup(commit_id);
519 if (worktree->base_commit_id == NULL) {
520 err = got_error_from_errno();
525 got_object_close(obj);
531 static const struct got_error *
532 lock_worktree(struct got_worktree *worktree, int operation)
534 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
535 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
536 : got_error_from_errno());
540 static const struct got_error *
541 make_parent_dirs(const char *abspath)
543 const struct got_error *err = NULL;
545 char *parent = dirname(abspath);
549 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
550 if (errno == ENOENT) {
551 err = make_parent_dirs(parent);
554 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
555 return got_error_from_errno();
557 err = got_error_from_errno();
563 static const struct got_error *
564 add_dir_on_disk(struct got_worktree *worktree, const char *path)
566 const struct got_error *err = NULL;
569 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
570 return got_error_from_errno();
572 /* XXX queue work rather than editing disk directly? */
573 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
576 if (errno == EEXIST) {
577 if (lstat(abspath, &sb) == -1) {
578 err = got_error_from_errno();
582 if (!S_ISDIR(sb.st_mode)) {
583 /* TODO directory is obstructed; do something */
584 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
589 } else if (errno == ENOENT) {
590 err = make_parent_dirs(abspath);
593 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
594 err = got_error_from_errno();
596 err = got_error_from_errno();
605 * Perform a 3-way merge where the file's version in the file index (blob2)
606 * acts as the common ancestor, the incoming blob (blob1) acts as the first
607 * derived version, and the file on disk acts as the second derived version.
609 static const struct got_error *
610 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
611 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
612 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
613 struct got_repository *repo,
614 got_worktree_checkout_cb progress_cb, void *progress_arg)
616 const struct got_error *err = NULL;
618 struct got_blob_object *blob2 = NULL;
619 FILE *f1 = NULL, *f2 = NULL;
620 char *blob1_path = NULL, *blob2_path = NULL;
621 char *merged_path = NULL;
622 struct got_object_id id2;
627 err = got_opentemp_named_fd(&merged_path, &merged_fd, "/tmp/got-merged");
630 err = got_opentemp_named(&blob1_path, &f1, "/tmp/got-merge-blob1");
633 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
637 err = got_opentemp_named(&blob2_path, &f2, "/tmp/got-merge-blob2");
641 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
642 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
645 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
649 err = got_object_id_str(&id_str, worktree->base_commit_id);
652 if (asprintf(&label1, "commit %s", id_str) == -1) {
653 err = got_error_from_errno();
657 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
658 blob2_path, ondisk_path, label1, path);
662 (*progress_cb)(progress_arg,
663 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
668 if (rename(merged_path, ondisk_path) != 0) {
669 err = got_error_from_errno();
674 * Do not update timestamps of already modified files. Otherwise,
675 * the status walk would treat them as unmodified files again.
677 err = got_fileindex_entry_update(ie, ondisk_path,
678 blob1->id.sha1, worktree->base_commit_id->sha1, 0);
680 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
681 err = got_error_from_errno();
682 if (f1 && fclose(f1) != 0 && err == NULL)
683 err = got_error_from_errno();
684 if (f2 && fclose(f2) != 0 && err == NULL)
685 err = got_error_from_errno();
687 got_object_blob_close(blob2);
702 static const struct got_error *
703 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
704 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
705 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
706 int restoring_missing_file, struct got_repository *repo,
707 got_worktree_checkout_cb progress_cb, void *progress_arg)
709 const struct got_error *err = NULL;
713 char *tmppath = NULL;
715 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
716 GOT_DEFAULT_FILE_MODE);
718 if (errno == ENOENT) {
719 char *parent = dirname(path);
721 return got_error_from_errno();
722 err = add_dir_on_disk(worktree, parent);
725 fd = open(ondisk_path,
726 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
727 GOT_DEFAULT_FILE_MODE);
729 return got_error_from_errno();
730 } else if (errno == EEXIST) {
731 if (!S_ISREG(st_mode)) {
732 /* TODO file is obstructed; do something */
733 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
736 err = got_opentemp_named_fd(&tmppath, &fd,
743 return got_error_from_errno();
746 if (restoring_missing_file)
747 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
749 (*progress_cb)(progress_arg,
750 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
752 hdrlen = got_object_blob_get_hdrlen(blob);
754 const uint8_t *buf = got_object_blob_get_read_buf(blob);
755 err = got_object_blob_read_block(&len, blob);
759 /* Skip blob object header first time around. */
760 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
762 err = got_error_from_errno();
764 } else if (outlen != len - hdrlen) {
765 err = got_error(GOT_ERR_IO);
775 if (rename(tmppath, ondisk_path) != 0) {
776 err = got_error_from_errno();
781 if (te_mode & S_IXUSR) {
782 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
783 err = got_error_from_errno();
787 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
788 err = got_error_from_errno();
794 entry = got_fileindex_entry_get(fileindex, path);
796 err = got_fileindex_entry_update(entry, ondisk_path,
797 blob->id.sha1, worktree->base_commit_id->sha1, 1);
799 err = got_fileindex_entry_alloc(&entry, ondisk_path,
800 path, blob->id.sha1, worktree->base_commit_id->sha1);
803 err = got_fileindex_entry_add(fileindex, entry);
806 if (fd != -1 && close(fd) != 0 && err == NULL)
807 err = got_error_from_errno();
812 static const struct got_error *
813 get_file_status(unsigned char *status, struct stat *sb,
814 struct got_fileindex_entry *ie, const char *abspath,
815 struct got_repository *repo)
817 const struct got_error *err = NULL;
818 struct got_object_id id;
822 struct got_blob_object *blob = NULL;
825 *status = GOT_STATUS_NO_CHANGE;
827 if (lstat(abspath, sb) == -1) {
828 if (errno == ENOENT) {
830 *status = GOT_STATUS_MISSING;
832 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
833 & (S_IRWXU | S_IRWXG | S_IRWXO));
835 sb->st_mode = GOT_DEFAULT_FILE_MODE;
838 return got_error_from_errno();
841 if (!S_ISREG(sb->st_mode)) {
842 *status = GOT_STATUS_OBSTRUCTED;
849 if (ie->ctime_sec == sb->st_ctime &&
850 ie->ctime_nsec == sb->st_ctimensec &&
851 ie->mtime_sec == sb->st_mtime &&
852 ie->mtime_sec == sb->st_mtime &&
853 ie->mtime_nsec == sb->st_mtimensec &&
854 ie->size == (sb->st_size & 0xffffffff))
857 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
858 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
862 f = fopen(abspath, "r");
864 err = got_error_from_errno();
867 hdrlen = got_object_blob_get_hdrlen(blob);
869 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
870 err = got_object_blob_read_block(&blen, blob);
873 flen = fread(fbuf, 1, sizeof(fbuf), f);
876 *status = GOT_STATUS_MODIFY;
878 } else if (flen == 0) {
880 *status = GOT_STATUS_MODIFY;
882 } else if (blen - hdrlen == flen) {
883 /* Skip blob object header first time around. */
884 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
885 *status = GOT_STATUS_MODIFY;
889 *status = GOT_STATUS_MODIFY;
896 got_object_blob_close(blob);
902 static const struct got_error *
903 update_blob(struct got_worktree *worktree,
904 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
905 struct got_tree_entry *te, const char *path,
906 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
907 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
909 const struct got_error *err = NULL;
910 struct got_blob_object *blob = NULL;
912 unsigned char status = GOT_STATUS_NO_CHANGE;
915 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
916 return got_error_from_errno();
918 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
922 if (status == GOT_STATUS_OBSTRUCTED) {
923 (*progress_cb)(progress_arg, status, path);
927 if (ie && status != GOT_STATUS_MISSING) {
928 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
929 SHA1_DIGEST_LENGTH) == 0) {
930 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
934 if (memcmp(ie->blob_sha1,
935 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
939 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
943 if (status == GOT_STATUS_MODIFY)
944 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
945 te->mode, sb.st_mode, blob, repo, progress_cb,
948 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
949 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
950 repo, progress_cb, progress_arg);
952 got_object_blob_close(blob);
958 static const struct got_error *
959 remove_ondisk_file(const char *root_path, const char *path)
961 const struct got_error *err = NULL;
962 char *ondisk_path = NULL;
964 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
965 return got_error_from_errno();
967 if (unlink(ondisk_path) == -1) {
969 err = got_error_from_errno();
971 char *parent = dirname(ondisk_path);
972 while (parent && strcmp(parent, root_path) != 0) {
973 if (rmdir(parent) == -1) {
974 if (errno != ENOTEMPTY)
975 err = got_error_from_errno();
978 parent = dirname(parent);
986 struct got_fileindex *fileindex;
987 struct got_worktree *worktree;
988 struct got_repository *repo;
989 got_worktree_checkout_cb progress_cb;
991 got_worktree_cancel_cb cancel_cb;
995 static const struct got_error *
996 diff_old_new(void *arg, struct got_fileindex_entry *ie,
997 struct got_tree_entry *te, const char *parent_path)
999 struct diff_cb_arg *a = arg;
1001 return update_blob(a->worktree, a->fileindex, ie, te,
1002 ie->path, a->repo, a->progress_cb, a->progress_arg,
1003 a->cancel_cb, a->cancel_arg);
1006 static const struct got_error *
1007 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1009 const struct got_error *err;
1010 struct diff_cb_arg *a = arg;
1012 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1014 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1017 got_fileindex_entry_remove(a->fileindex, ie);
1021 static const struct got_error *
1022 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1024 struct diff_cb_arg *a = arg;
1025 const struct got_error *err;
1028 if (asprintf(&path, "%s%s%s", parent_path,
1029 parent_path[0] ? "/" : "", te->name)
1031 return got_error_from_errno();
1033 if (S_ISDIR(te->mode))
1034 err = add_dir_on_disk(a->worktree, path);
1036 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1037 a->repo, a->progress_cb, a->progress_arg,
1038 a->cancel_cb, a->cancel_arg);
1044 const struct got_error *
1045 got_worktree_checkout_files(struct got_worktree *worktree,
1046 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1047 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1049 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1050 struct got_commit_object *commit = NULL;
1051 struct got_object_id *tree_id = NULL;
1052 struct got_tree_object *tree = NULL;
1053 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1054 struct got_fileindex *fileindex = NULL;
1055 FILE *index = NULL, *new_index = NULL;
1056 struct got_fileindex_diff_tree_cb diff_cb;
1057 struct diff_cb_arg arg;
1059 err = lock_worktree(worktree, LOCK_EX);
1063 fileindex = got_fileindex_alloc();
1064 if (fileindex == NULL) {
1065 err = got_error_from_errno();
1069 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1070 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1071 err = got_error_from_errno();
1072 fileindex_path = NULL;
1077 * Read the file index.
1078 * Checking out files is supposed to be an idempotent operation.
1079 * If the on-disk file index is incomplete we will try to complete it.
1081 index = fopen(fileindex_path, "rb");
1082 if (index == NULL) {
1083 if (errno != ENOENT) {
1084 err = got_error_from_errno();
1088 err = got_fileindex_read(fileindex, index);
1094 err = got_opentemp_named(&new_fileindex_path, &new_index,
1099 err = got_object_open_as_commit(&commit, repo,
1100 worktree->base_commit_id);
1104 err = got_object_id_by_path(&tree_id, repo,
1105 worktree->base_commit_id, worktree->path_prefix);
1109 err = got_object_open_as_tree(&tree, repo, tree_id);
1113 diff_cb.diff_old_new = diff_old_new;
1114 diff_cb.diff_old = diff_old;
1115 diff_cb.diff_new = diff_new;
1116 arg.fileindex = fileindex;
1117 arg.worktree = worktree;
1119 arg.progress_cb = progress_cb;
1120 arg.progress_arg = progress_arg;
1121 arg.cancel_cb = cancel_cb;
1122 arg.cancel_arg = cancel_arg;
1123 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1126 /* Try to sync the fileindex back to disk in any case. */
1127 err = got_fileindex_write(fileindex, new_index);
1131 if (rename(new_fileindex_path, fileindex_path) != 0) {
1132 err = got_error_from_errno();
1136 free(new_fileindex_path);
1137 new_fileindex_path = NULL;
1141 got_object_tree_close(tree);
1143 got_object_commit_close(commit);
1144 if (new_fileindex_path)
1145 unlink(new_fileindex_path);
1148 free(new_fileindex_path);
1149 free(fileindex_path);
1150 got_fileindex_free(fileindex);
1153 unlockerr = lock_worktree(worktree, LOCK_SH);
1154 if (unlockerr && err == NULL)
1159 struct diff_dir_cb_arg {
1160 struct got_fileindex *fileindex;
1161 struct got_worktree *worktree;
1162 const char *status_path;
1163 size_t status_path_len;
1164 struct got_repository *repo;
1165 got_worktree_status_cb status_cb;
1167 got_worktree_cancel_cb cancel_cb;
1171 static const struct got_error *
1172 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1173 got_worktree_status_cb status_cb, void *status_arg,
1174 struct got_repository *repo)
1176 const struct got_error *err = NULL;
1177 unsigned char status = GOT_STATUS_NO_CHANGE;
1179 struct got_object_id id;
1181 err = get_file_status(&status, &sb, ie, abspath, repo);
1182 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1183 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1184 err = (*status_cb)(status_arg, status, ie->path, &id);
1189 static const struct got_error *
1190 status_old_new(void *arg, struct got_fileindex_entry *ie,
1191 struct dirent *de, const char *parent_path)
1193 const struct got_error *err = NULL;
1194 struct diff_dir_cb_arg *a = arg;
1197 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1198 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1201 if (parent_path[0]) {
1202 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1203 parent_path, de->d_name) == -1)
1204 return got_error_from_errno();
1206 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1208 return got_error_from_errno();
1211 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1217 static const struct got_error *
1218 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1220 struct diff_dir_cb_arg *a = arg;
1221 struct got_object_id id;
1223 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1226 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1227 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1231 static const struct got_error *
1232 status_new(void *arg, struct dirent *de, const char *parent_path)
1234 const struct got_error *err = NULL;
1235 struct diff_dir_cb_arg *a = arg;
1238 if (de->d_type == DT_DIR)
1241 /* XXX ignore symlinks for now */
1242 if (de->d_type == DT_LNK)
1245 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1248 if (parent_path[0]) {
1249 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1250 return got_error_from_errno();
1255 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1262 const struct got_error *
1263 got_worktree_status(struct got_worktree *worktree, const char *path,
1264 struct got_repository *repo, got_worktree_status_cb status_cb,
1265 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1267 const struct got_error *err = NULL;
1268 DIR *workdir = NULL;
1269 char *fileindex_path = NULL;
1270 struct got_fileindex *fileindex = NULL;
1272 struct got_fileindex_diff_dir_cb fdiff_cb;
1273 struct diff_dir_cb_arg arg;
1274 char *ondisk_path = NULL;
1276 fileindex = got_fileindex_alloc();
1277 if (fileindex == NULL) {
1278 err = got_error_from_errno();
1282 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1283 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1284 err = got_error_from_errno();
1285 fileindex_path = NULL;
1289 index = fopen(fileindex_path, "rb");
1290 if (index == NULL) {
1291 if (errno != ENOENT) {
1292 err = got_error_from_errno();
1296 err = got_fileindex_read(fileindex, index);
1302 if (asprintf(&ondisk_path, "%s%s%s",
1303 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1304 err = got_error_from_errno();
1307 workdir = opendir(ondisk_path);
1308 if (workdir == NULL) {
1309 if (errno == ENOTDIR) {
1310 struct got_fileindex_entry *ie;
1311 ie = got_fileindex_entry_get(fileindex, path);
1313 err = got_error(GOT_ERR_BAD_PATH);
1316 err = report_file_status(ie, ondisk_path,
1317 status_cb, status_arg, repo);
1320 err = got_error_from_errno();
1324 fdiff_cb.diff_old_new = status_old_new;
1325 fdiff_cb.diff_old = status_old;
1326 fdiff_cb.diff_new = status_new;
1327 arg.fileindex = fileindex;
1328 arg.worktree = worktree;
1329 arg.status_path = path;
1330 arg.status_path_len = strlen(path);
1332 arg.status_cb = status_cb;
1333 arg.status_arg = status_arg;
1334 arg.cancel_cb = cancel_cb;
1335 arg.cancel_arg = cancel_arg;
1336 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1337 path, repo, &fdiff_cb, &arg);
1342 free(fileindex_path);
1343 got_fileindex_free(fileindex);