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();
127 static const struct got_error *
128 read_meta_file(char **content, const char *path_got, const char *name)
130 const struct got_error *err = NULL;
138 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
139 err = got_error_from_errno();
144 fd = open(path, O_RDONLY | O_NOFOLLOW);
146 err = got_error_from_errno();
149 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
150 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
151 : got_error_from_errno());
156 *content = calloc(1, sb.st_size);
157 if (*content == NULL) {
158 err = got_error_from_errno();
162 n = read(fd, *content, sb.st_size);
163 if (n != sb.st_size) {
164 err = (n == -1 ? got_error_from_errno() :
165 got_error(GOT_ERR_WORKTREE_META));
168 if ((*content)[sb.st_size - 1] != '\n') {
169 err = got_error(GOT_ERR_WORKTREE_META);
172 (*content)[sb.st_size - 1] = '\0';
175 if (fd != -1 && close(fd) == -1 && err == NULL)
176 err = got_error_from_errno();
185 const struct got_error *
186 got_worktree_init(const char *path, struct got_reference *head_ref,
187 const char *prefix, struct got_repository *repo)
189 const struct got_error *err = NULL;
190 struct got_object_id *commit_id = NULL;
192 char *path_got = NULL;
194 char *formatstr = NULL;
195 char *absprefix = NULL;
196 char *basestr = NULL;
198 err = got_ref_resolve(&commit_id, repo, head_ref);
201 err = got_object_get_type(&obj_type, repo, commit_id);
204 if (obj_type != GOT_OBJ_TYPE_COMMIT)
205 return got_error(GOT_ERR_OBJ_TYPE);
207 if (!got_path_is_absolute(prefix)) {
208 if (asprintf(&absprefix, "/%s", prefix) == -1)
209 return got_error_from_errno();
212 /* Create top-level directory (may already exist). */
213 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
214 err = got_error_from_errno();
218 /* Create .got directory (may already exist). */
219 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
220 err = got_error_from_errno();
223 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
224 err = got_error_from_errno();
228 /* Create an empty lock file. */
229 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
233 /* Create an empty file index. */
234 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
238 /* Write the HEAD reference. */
239 refstr = got_ref_to_str(head_ref);
240 if (refstr == NULL) {
241 err = got_error_from_errno();
244 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
248 /* Record our base commit. */
249 err = got_object_id_str(&basestr, commit_id);
252 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
256 /* Store path to repository. */
257 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
258 got_repo_get_path(repo));
262 /* Store in-repository path prefix. */
263 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
264 absprefix ? absprefix : prefix);
268 /* Stamp work tree with format file. */
269 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
270 err = got_error_from_errno();
273 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
287 static const struct got_error *
288 open_worktree(struct got_worktree **worktree, const char *path)
290 const struct got_error *err = NULL;
292 char *formatstr = NULL;
293 char *path_lock = NULL;
294 char *base_commit_id_str = NULL;
295 char *head_ref_str = NULL;
296 int version, fd = -1;
298 struct got_repository *repo = NULL;
302 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
303 err = got_error_from_errno();
308 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
309 err = got_error_from_errno();
314 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
316 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
317 : got_error_from_errno());
321 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
325 version = strtonum(formatstr, 1, INT_MAX, &errstr);
327 err = got_error(GOT_ERR_WORKTREE_META);
330 if (version != GOT_WORKTREE_FORMAT_VERSION) {
331 err = got_error(GOT_ERR_WORKTREE_VERS);
335 *worktree = calloc(1, sizeof(**worktree));
336 if (*worktree == NULL) {
337 err = got_error_from_errno();
340 (*worktree)->lockfd = -1;
342 (*worktree)->root_path = strdup(path);
343 if ((*worktree)->root_path == NULL) {
344 err = got_error_from_errno();
347 err = read_meta_file(&(*worktree)->repo_path, path_got,
348 GOT_WORKTREE_REPOSITORY);
352 err = read_meta_file(&(*worktree)->path_prefix, path_got,
353 GOT_WORKTREE_PATH_PREFIX);
357 err = read_meta_file(&base_commit_id_str, path_got,
358 GOT_WORKTREE_BASE_COMMIT);
362 err = got_repo_open(&repo, (*worktree)->repo_path);
366 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
371 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
375 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
378 got_repo_close(repo);
382 free(base_commit_id_str);
386 if (*worktree != NULL)
387 got_worktree_close(*worktree);
390 (*worktree)->lockfd = fd;
395 const struct got_error *
396 got_worktree_open(struct got_worktree **worktree, const char *path)
398 const struct got_error *err = NULL;
401 err = open_worktree(worktree, path);
402 if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
406 path = dirname(path);
408 return got_error_from_errno();
409 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
411 return got_error(GOT_ERR_NOT_WORKTREE);
415 got_worktree_close(struct got_worktree *worktree)
417 free(worktree->root_path);
418 free(worktree->repo_path);
419 free(worktree->path_prefix);
420 free(worktree->base_commit_id);
421 if (worktree->head_ref)
422 got_ref_close(worktree->head_ref);
423 if (worktree->lockfd != -1)
424 close(worktree->lockfd);
429 got_worktree_get_root_path(struct got_worktree *worktree)
431 return worktree->root_path;
435 got_worktree_get_repo_path(struct got_worktree *worktree)
437 return worktree->repo_path;
441 got_worktree_get_path_prefix(struct got_worktree *worktree)
443 return worktree->path_prefix;
446 const struct got_error *
447 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
448 const char *path_prefix)
450 char *absprefix = NULL;
452 if (!got_path_is_absolute(path_prefix)) {
453 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
454 return got_error_from_errno();
456 *match = (strcmp(absprefix ? absprefix : path_prefix,
457 worktree->path_prefix) == 0);
463 got_worktree_get_head_ref_name(struct got_worktree *worktree)
465 return got_ref_to_str(worktree->head_ref);
468 struct got_reference *
469 got_worktree_get_head_ref(struct got_worktree *worktree)
471 return got_ref_dup(worktree->head_ref);
474 struct got_object_id *
475 got_worktree_get_base_commit_id(struct got_worktree *worktree)
477 return worktree->base_commit_id;
480 const struct got_error *
481 got_worktree_set_base_commit_id(struct got_worktree *worktree,
482 struct got_repository *repo, struct got_object_id *commit_id)
484 const struct got_error *err;
485 struct got_object *obj = NULL;
487 char *path_got = NULL;
489 if (asprintf(&path_got, "%s/%s", worktree->root_path,
490 GOT_WORKTREE_GOT_DIR) == -1) {
491 err = got_error_from_errno();
496 err = got_object_open(&obj, repo, commit_id);
500 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
501 err = got_error(GOT_ERR_OBJ_TYPE);
505 /* Record our base commit. */
506 err = got_object_id_str(&id_str, commit_id);
509 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
513 free(worktree->base_commit_id);
514 worktree->base_commit_id = got_object_id_dup(commit_id);
515 if (worktree->base_commit_id == NULL) {
516 err = got_error_from_errno();
521 got_object_close(obj);
527 static const struct got_error *
528 lock_worktree(struct got_worktree *worktree, int operation)
530 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
531 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
532 : got_error_from_errno());
536 static const struct got_error *
537 make_parent_dirs(const char *abspath)
539 const struct got_error *err = NULL;
541 char *parent = dirname(abspath);
545 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
546 if (errno == ENOENT) {
547 err = make_parent_dirs(parent);
550 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
551 return got_error_from_errno();
553 err = got_error_from_errno();
559 static const struct got_error *
560 add_dir_on_disk(struct got_worktree *worktree, const char *path)
562 const struct got_error *err = NULL;
565 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
566 return got_error_from_errno();
568 /* XXX queue work rather than editing disk directly? */
569 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
572 if (errno == EEXIST) {
573 if (lstat(abspath, &sb) == -1) {
574 err = got_error_from_errno();
578 if (!S_ISDIR(sb.st_mode)) {
579 /* TODO directory is obstructed; do something */
580 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
585 } else if (errno == ENOENT) {
586 err = make_parent_dirs(abspath);
589 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
590 err = got_error_from_errno();
592 err = got_error_from_errno();
601 * Perform a 3-way merge where the file's version in the file index (blob2)
602 * acts as the common ancestor, the incoming blob (blob1) acts as the first
603 * derived version, and the file on disk acts as the second derived version.
605 static const struct got_error *
606 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
607 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
608 struct got_blob_object *blob1, struct got_repository *repo,
609 got_worktree_checkout_cb progress_cb, void *progress_arg)
611 const struct got_error *err = NULL;
613 struct got_blob_object *blob2 = NULL;
614 FILE *f1 = NULL, *f2 = NULL;
615 char *blob1_path = NULL, *blob2_path = NULL;
616 char *merged_path = NULL;
617 struct got_object_id id2;
622 err = got_opentemp_named_fd(&merged_path, &merged_fd, "/tmp/got-merged");
625 err = got_opentemp_named(&blob1_path, &f1, "/tmp/got-merge-blob1");
628 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
632 err = got_opentemp_named(&blob2_path, &f2, "/tmp/got-merge-blob2");
636 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
637 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
640 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
644 err = got_object_id_str(&id_str, worktree->base_commit_id);
647 if (asprintf(&label1, "commit %s", id_str) == -1) {
648 err = got_error_from_errno();
652 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
653 blob2_path, ondisk_path, label1, path);
657 (*progress_cb)(progress_arg,
658 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
663 if (rename(merged_path, ondisk_path) != 0) {
664 err = got_error_from_errno();
668 err = got_fileindex_entry_update(ie, ondisk_path,
669 blob1->id.sha1, worktree->base_commit_id->sha1);
678 got_object_blob_close(blob2);
693 static const struct got_error *
694 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
695 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
696 struct got_blob_object *blob,
697 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
700 const struct got_error *err = NULL;
704 char *tmppath = NULL;
706 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
707 GOT_DEFAULT_FILE_MODE);
709 if (errno == ENOENT) {
710 char *parent = dirname(path);
712 return got_error_from_errno();
713 err = add_dir_on_disk(worktree, parent);
716 fd = open(ondisk_path,
717 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
718 GOT_DEFAULT_FILE_MODE);
720 return got_error_from_errno();
721 } else if (errno == EEXIST) {
723 if (lstat(ondisk_path, &sb) == -1) {
724 err = got_error_from_errno();
726 } else if (!S_ISREG(sb.st_mode)) {
727 /* TODO file is obstructed; do something */
728 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
731 err = got_opentemp_named_fd(&tmppath, &fd,
738 return got_error_from_errno();
741 (*progress_cb)(progress_arg,
742 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
744 hdrlen = got_object_blob_get_hdrlen(blob);
746 const uint8_t *buf = got_object_blob_get_read_buf(blob);
747 err = got_object_blob_read_block(&len, blob);
751 /* Skip blob object header first time around. */
752 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
754 err = got_error_from_errno();
756 } else if (outlen != len - hdrlen) {
757 err = got_error(GOT_ERR_IO);
767 if (rename(tmppath, ondisk_path) != 0) {
768 err = got_error_from_errno();
774 entry = got_fileindex_entry_get(fileindex, path);
776 err = got_fileindex_entry_update(entry, ondisk_path,
777 blob->id.sha1, worktree->base_commit_id->sha1);
779 err = got_fileindex_entry_alloc(&entry, ondisk_path,
780 path, blob->id.sha1, worktree->base_commit_id->sha1);
783 err = got_fileindex_entry_add(fileindex, entry);
792 static const struct got_error *
793 get_file_status(unsigned char *status, struct got_fileindex_entry *ie,
794 const char *abspath, struct got_repository *repo)
796 const struct got_error *err = NULL;
797 struct got_object_id id;
801 struct got_blob_object *blob = NULL;
805 *status = GOT_STATUS_NO_CHANGE;
807 if (lstat(abspath, &sb) == -1)
808 return got_error_from_errno();
810 if (!S_ISREG(sb.st_mode)) {
811 *status = GOT_STATUS_OBSTRUCTED;
815 if (ie->ctime_sec == sb.st_ctime &&
816 ie->ctime_nsec == sb.st_ctimensec &&
817 ie->mtime_sec == sb.st_mtime &&
818 ie->mtime_sec == sb.st_mtime &&
819 ie->mtime_nsec == sb.st_mtimensec &&
820 ie->size == (sb.st_size & 0xffffffff))
823 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
824 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
828 f = fopen(abspath, "r");
830 err = got_error_from_errno();
833 hdrlen = got_object_blob_get_hdrlen(blob);
835 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
836 err = got_object_blob_read_block(&blen, blob);
839 flen = fread(fbuf, 1, sizeof(fbuf), f);
842 *status = GOT_STATUS_MODIFIY;
844 } else if (flen == 0) {
846 *status = GOT_STATUS_MODIFIY;
848 } else if (blen - hdrlen == flen) {
849 /* Skip blob object header first time around. */
850 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
851 *status = GOT_STATUS_MODIFIY;
855 *status = GOT_STATUS_MODIFIY;
862 got_object_blob_close(blob);
868 static const struct got_error *
869 update_blob(struct got_worktree *worktree,
870 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
871 struct got_tree_entry *te, const char *path,
872 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
873 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
875 const struct got_error *err = NULL;
876 struct got_blob_object *blob = NULL;
878 unsigned char status = GOT_STATUS_NO_CHANGE;
880 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
881 return got_error_from_errno();
884 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
885 SHA1_DIGEST_LENGTH) == 0) {
886 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
890 if (memcmp(ie->blob_sha1,
891 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
894 err = get_file_status(&status, ie, ondisk_path, repo);
898 if (status == GOT_STATUS_OBSTRUCTED) {
899 (*progress_cb)(progress_arg, status, path);
904 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
908 if (status == GOT_STATUS_MODIFIY)
909 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
910 blob, repo, progress_cb, progress_arg);
912 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
913 blob, repo, progress_cb, progress_arg);
915 got_object_blob_close(blob);
921 static const struct got_error *
922 remove_ondisk_file(const char *root_path, const char *path)
924 const struct got_error *err = NULL;
925 char *ondisk_path = NULL;
927 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
928 return got_error_from_errno();
930 if (unlink(ondisk_path) == -1) {
932 err = got_error_from_errno();
934 char *parent = dirname(ondisk_path);
935 while (parent && strcmp(parent, root_path) != 0) {
936 if (rmdir(parent) == -1) {
937 if (errno != ENOTEMPTY)
938 err = got_error_from_errno();
941 parent = dirname(parent);
949 struct got_fileindex *fileindex;
950 struct got_worktree *worktree;
951 struct got_repository *repo;
952 got_worktree_checkout_cb progress_cb;
954 got_worktree_cancel_cb cancel_cb;
958 static const struct got_error *
959 diff_old_new(void *arg, struct got_fileindex_entry *ie,
960 struct got_tree_entry *te, const char *parent_path)
962 struct diff_cb_arg *a = arg;
964 return update_blob(a->worktree, a->fileindex, ie, te,
965 ie->path, a->repo, a->progress_cb, a->progress_arg,
966 a->cancel_cb, a->cancel_arg);
969 static const struct got_error *
970 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
972 const struct got_error *err;
973 struct diff_cb_arg *a = arg;
975 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
977 err = remove_ondisk_file(a->worktree->root_path, ie->path);
980 got_fileindex_entry_remove(a->fileindex, ie);
984 static const struct got_error *
985 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
987 struct diff_cb_arg *a = arg;
988 const struct got_error *err;
991 if (asprintf(&path, "%s%s%s", parent_path,
992 parent_path[0] ? "/" : "", te->name)
994 return got_error_from_errno();
996 if (S_ISDIR(te->mode))
997 err = add_dir_on_disk(a->worktree, path);
999 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1000 a->repo, a->progress_cb, a->progress_arg,
1001 a->cancel_cb, a->cancel_arg);
1007 const struct got_error *
1008 got_worktree_checkout_files(struct got_worktree *worktree,
1009 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1010 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1012 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1013 struct got_commit_object *commit = NULL;
1014 struct got_object_id *tree_id = NULL;
1015 struct got_tree_object *tree = NULL;
1016 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1017 struct got_fileindex *fileindex = NULL;
1018 FILE *index = NULL, *new_index = NULL;
1019 struct got_fileindex_diff_tree_cb diff_cb;
1020 struct diff_cb_arg arg;
1022 err = lock_worktree(worktree, LOCK_EX);
1026 fileindex = got_fileindex_alloc();
1027 if (fileindex == NULL) {
1028 err = got_error_from_errno();
1032 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1033 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1034 err = got_error_from_errno();
1035 fileindex_path = NULL;
1040 * Read the file index.
1041 * Checking out files is supposed to be an idempotent operation.
1042 * If the on-disk file index is incomplete we will try to complete it.
1044 index = fopen(fileindex_path, "rb");
1045 if (index == NULL) {
1046 if (errno != ENOENT) {
1047 err = got_error_from_errno();
1051 err = got_fileindex_read(fileindex, index);
1057 err = got_opentemp_named(&new_fileindex_path, &new_index,
1062 err = got_object_open_as_commit(&commit, repo,
1063 worktree->base_commit_id);
1067 err = got_object_id_by_path(&tree_id, repo,
1068 worktree->base_commit_id, worktree->path_prefix);
1072 err = got_object_open_as_tree(&tree, repo, tree_id);
1076 diff_cb.diff_old_new = diff_old_new;
1077 diff_cb.diff_old = diff_old;
1078 diff_cb.diff_new = diff_new;
1079 arg.fileindex = fileindex;
1080 arg.worktree = worktree;
1082 arg.progress_cb = progress_cb;
1083 arg.progress_arg = progress_arg;
1084 arg.cancel_cb = cancel_cb;
1085 arg.cancel_arg = cancel_arg;
1086 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1089 /* Try to sync the fileindex back to disk in any case. */
1090 err = got_fileindex_write(fileindex, new_index);
1094 if (rename(new_fileindex_path, fileindex_path) != 0) {
1095 err = got_error_from_errno();
1099 free(new_fileindex_path);
1100 new_fileindex_path = NULL;
1104 got_object_tree_close(tree);
1106 got_object_commit_close(commit);
1107 if (new_fileindex_path)
1108 unlink(new_fileindex_path);
1111 free(new_fileindex_path);
1112 free(fileindex_path);
1113 got_fileindex_free(fileindex);
1116 unlockerr = lock_worktree(worktree, LOCK_SH);
1117 if (unlockerr && err == NULL)
1122 struct diff_dir_cb_arg {
1123 struct got_fileindex *fileindex;
1124 struct got_worktree *worktree;
1125 struct got_repository *repo;
1126 got_worktree_status_cb status_cb;
1128 got_worktree_cancel_cb cancel_cb;
1132 static const struct got_error *
1133 status_old_new(void *arg, struct got_fileindex_entry *ie,
1134 struct dirent *de, const char *parent_path)
1136 const struct got_error *err = NULL;
1137 struct diff_dir_cb_arg *a = arg;
1139 unsigned char status = GOT_STATUS_NO_CHANGE;
1141 if (parent_path[0]) {
1142 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1143 parent_path, de->d_name) == -1)
1144 return got_error_from_errno();
1146 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1148 return got_error_from_errno();
1151 err = get_file_status(&status, ie, abspath, a->repo);
1152 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1153 struct got_object_id id;
1154 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1155 err = (*a->status_cb)(a->status_arg, status, ie->path, &id);
1161 static const struct got_error *
1162 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1164 struct diff_dir_cb_arg *a = arg;
1165 struct got_object_id id;
1166 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1167 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1171 static const struct got_error *
1172 status_new(void *arg, struct dirent *de, const char *parent_path)
1174 const struct got_error *err = NULL;
1175 struct diff_dir_cb_arg *a = arg;
1178 if (de->d_type == DT_DIR)
1181 if (parent_path[0]) {
1182 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1183 return got_error_from_errno();
1188 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1195 const struct got_error *
1196 got_worktree_status(struct got_worktree *worktree,
1197 struct got_repository *repo, got_worktree_status_cb status_cb,
1198 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1200 const struct got_error *err = NULL;
1201 DIR *workdir = NULL;
1202 char *fileindex_path = NULL;
1203 struct got_fileindex *fileindex = NULL;
1205 struct got_fileindex_diff_dir_cb fdiff_cb;
1206 struct diff_dir_cb_arg arg;
1208 fileindex = got_fileindex_alloc();
1209 if (fileindex == NULL) {
1210 err = got_error_from_errno();
1214 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1215 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1216 err = got_error_from_errno();
1217 fileindex_path = NULL;
1221 index = fopen(fileindex_path, "rb");
1222 if (index == NULL) {
1223 if (errno != ENOENT) {
1224 err = got_error_from_errno();
1228 err = got_fileindex_read(fileindex, index);
1234 workdir = opendir(worktree->root_path);
1235 if (workdir == NULL) {
1236 err = got_error_from_errno();
1239 fdiff_cb.diff_old_new = status_old_new;
1240 fdiff_cb.diff_old = status_old;
1241 fdiff_cb.diff_new = status_new;
1242 arg.fileindex = fileindex;
1243 arg.worktree = worktree;
1245 arg.status_cb = status_cb;
1246 arg.status_arg = status_arg;
1247 arg.cancel_cb = cancel_cb;
1248 arg.cancel_arg = cancel_arg;
1249 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1250 repo, &fdiff_cb, &arg);
1254 free(fileindex_path);
1255 got_fileindex_free(fileindex);