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>
36 #include "got_error.h"
37 #include "got_repository.h"
38 #include "got_reference.h"
39 #include "got_object.h"
40 #include "got_worktree.h"
41 #include "got_opentemp.h"
43 #include "got_lib_worktree.h"
44 #include "got_lib_path.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_fileindex.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_diff.h"
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 static const struct got_error *
57 create_meta_file(const char *path_got, const char *name, const char *content)
59 const struct got_error *err = NULL;
63 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
64 err = got_error_from_errno();
69 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
70 GOT_DEFAULT_FILE_MODE);
72 err = got_error_from_errno();
77 int len = dprintf(fd, "%s\n", content);
78 if (len != strlen(content) + 1) {
79 err = got_error_from_errno();
85 if (fd != -1 && close(fd) == -1 && err == NULL)
86 err = got_error_from_errno();
91 static const struct got_error *
92 update_meta_file(const char *path_got, const char *name, const char *content)
94 const struct got_error *err = NULL;
99 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
100 err = got_error_from_errno();
105 err = got_opentemp_named(&tmppath, &tmpfile, path);
110 int len = fprintf(tmpfile, "%s\n", content);
111 if (len != strlen(content) + 1) {
112 err = got_error_from_errno();
117 if (rename(tmppath, path) != 0) {
118 err = got_error_from_errno();
125 if (fclose(tmpfile) != 0 && err == NULL)
126 err = got_error_from_errno();
130 static const struct got_error *
131 read_meta_file(char **content, const char *path_got, const char *name)
133 const struct got_error *err = NULL;
141 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
142 err = got_error_from_errno();
147 fd = open(path, O_RDONLY | O_NOFOLLOW);
150 err = got_error(GOT_ERR_WORKTREE_META);
152 err = got_error_from_errno();
155 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
156 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
157 : got_error_from_errno());
161 if (lstat(path, &sb) != 0) {
162 err = got_error_from_errno();
165 *content = calloc(1, sb.st_size);
166 if (*content == NULL) {
167 err = got_error_from_errno();
171 n = read(fd, *content, sb.st_size);
172 if (n != sb.st_size) {
173 err = (n == -1 ? got_error_from_errno() :
174 got_error(GOT_ERR_WORKTREE_META));
177 if ((*content)[sb.st_size - 1] != '\n') {
178 err = got_error(GOT_ERR_WORKTREE_META);
181 (*content)[sb.st_size - 1] = '\0';
184 if (fd != -1 && close(fd) == -1 && err == NULL)
185 err = got_error_from_errno();
194 const struct got_error *
195 got_worktree_init(const char *path, struct got_reference *head_ref,
196 const char *prefix, struct got_repository *repo)
198 const struct got_error *err = NULL;
199 struct got_object_id *commit_id = NULL;
201 uint32_t uuid_status;
203 char *path_got = NULL;
205 char *formatstr = NULL;
206 char *absprefix = NULL;
207 char *basestr = NULL;
208 char *uuidstr = NULL;
210 if (strcmp(path, got_repo_get_path(repo)) == 0) {
211 err = got_error(GOT_ERR_WORKTREE_REPO);
215 err = got_ref_resolve(&commit_id, repo, head_ref);
218 err = got_object_get_type(&obj_type, repo, commit_id);
221 if (obj_type != GOT_OBJ_TYPE_COMMIT)
222 return got_error(GOT_ERR_OBJ_TYPE);
224 if (!got_path_is_absolute(prefix)) {
225 if (asprintf(&absprefix, "/%s", prefix) == -1)
226 return got_error_from_errno();
229 /* Create top-level directory (may already exist). */
230 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
231 err = got_error_from_errno();
235 /* Create .got directory (may already exist). */
236 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
237 err = got_error_from_errno();
240 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
241 err = got_error_from_errno();
245 /* Create an empty lock file. */
246 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
250 /* Create an empty file index. */
251 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
255 /* Write the HEAD reference. */
256 refstr = got_ref_to_str(head_ref);
257 if (refstr == NULL) {
258 err = got_error_from_errno();
261 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
265 /* Record our base commit. */
266 err = got_object_id_str(&basestr, commit_id);
269 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
273 /* Store path to repository. */
274 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
275 got_repo_get_path(repo));
279 /* Store in-repository path prefix. */
280 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
281 absprefix ? absprefix : prefix);
286 uuid_create(&uuid, &uuid_status);
287 if (uuid_status != uuid_s_ok) {
288 err = got_error_uuid(uuid_status);
291 uuid_to_string(&uuid, &uuidstr, &uuid_status);
292 if (uuid_status != uuid_s_ok) {
293 err = got_error_uuid(uuid_status);
296 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
300 /* Stamp work tree with format file. */
301 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
302 err = got_error_from_errno();
305 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
320 static const struct got_error *
321 open_worktree(struct got_worktree **worktree, const char *path)
323 const struct got_error *err = NULL;
325 char *formatstr = NULL;
326 char *uuidstr = NULL;
327 char *path_lock = NULL;
328 char *base_commit_id_str = NULL;
329 char *head_ref_str = NULL;
330 int version, fd = -1;
332 struct got_repository *repo = NULL;
333 uint32_t uuid_status;
337 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
338 err = got_error_from_errno();
343 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
344 err = got_error_from_errno();
349 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
351 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
352 : got_error_from_errno());
356 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
360 version = strtonum(formatstr, 1, INT_MAX, &errstr);
362 err = got_error(GOT_ERR_WORKTREE_META);
365 if (version != GOT_WORKTREE_FORMAT_VERSION) {
366 err = got_error(GOT_ERR_WORKTREE_VERS);
370 *worktree = calloc(1, sizeof(**worktree));
371 if (*worktree == NULL) {
372 err = got_error_from_errno();
375 (*worktree)->lockfd = -1;
377 (*worktree)->root_path = strdup(path);
378 if ((*worktree)->root_path == NULL) {
379 err = got_error_from_errno();
382 err = read_meta_file(&(*worktree)->repo_path, path_got,
383 GOT_WORKTREE_REPOSITORY);
387 err = read_meta_file(&(*worktree)->path_prefix, path_got,
388 GOT_WORKTREE_PATH_PREFIX);
392 err = read_meta_file(&base_commit_id_str, path_got,
393 GOT_WORKTREE_BASE_COMMIT);
397 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
400 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
401 if (uuid_status != uuid_s_ok) {
402 err = got_error_uuid(uuid_status);
406 err = got_repo_open(&repo, (*worktree)->repo_path);
410 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
415 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
419 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
422 got_repo_close(repo);
426 free(base_commit_id_str);
432 if (*worktree != NULL)
433 got_worktree_close(*worktree);
436 (*worktree)->lockfd = fd;
441 const struct got_error *
442 got_worktree_open(struct got_worktree **worktree, const char *path)
444 const struct got_error *err = NULL;
447 err = open_worktree(worktree, path);
448 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
452 path = dirname(path);
454 return got_error_from_errno();
455 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
457 return got_error(GOT_ERR_NOT_WORKTREE);
460 const struct got_error *
461 got_worktree_close(struct got_worktree *worktree)
463 const struct got_error *err = NULL;
464 free(worktree->root_path);
465 free(worktree->repo_path);
466 free(worktree->path_prefix);
467 free(worktree->base_commit_id);
468 if (worktree->head_ref)
469 got_ref_close(worktree->head_ref);
470 if (worktree->lockfd != -1)
471 if (close(worktree->lockfd) != 0)
472 err = got_error_from_errno();
478 got_worktree_get_root_path(struct got_worktree *worktree)
480 return worktree->root_path;
484 got_worktree_get_repo_path(struct got_worktree *worktree)
486 return worktree->repo_path;
490 got_worktree_get_path_prefix(struct got_worktree *worktree)
492 return worktree->path_prefix;
495 const struct got_error *
496 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
497 const char *path_prefix)
499 char *absprefix = NULL;
501 if (!got_path_is_absolute(path_prefix)) {
502 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
503 return got_error_from_errno();
505 *match = (strcmp(absprefix ? absprefix : path_prefix,
506 worktree->path_prefix) == 0);
512 got_worktree_get_head_ref_name(struct got_worktree *worktree)
514 return got_ref_to_str(worktree->head_ref);
517 struct got_reference *
518 got_worktree_get_head_ref(struct got_worktree *worktree)
520 return got_ref_dup(worktree->head_ref);
523 struct got_object_id *
524 got_worktree_get_base_commit_id(struct got_worktree *worktree)
526 return worktree->base_commit_id;
529 const struct got_error *
530 got_worktree_set_base_commit_id(struct got_worktree *worktree,
531 struct got_repository *repo, struct got_object_id *commit_id)
533 const struct got_error *err;
534 struct got_object *obj = NULL;
536 char *path_got = NULL;
538 if (asprintf(&path_got, "%s/%s", worktree->root_path,
539 GOT_WORKTREE_GOT_DIR) == -1) {
540 err = got_error_from_errno();
545 err = got_object_open(&obj, repo, commit_id);
549 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
550 err = got_error(GOT_ERR_OBJ_TYPE);
554 /* Record our base commit. */
555 err = got_object_id_str(&id_str, commit_id);
558 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
562 free(worktree->base_commit_id);
563 worktree->base_commit_id = got_object_id_dup(commit_id);
564 if (worktree->base_commit_id == NULL) {
565 err = got_error_from_errno();
570 got_object_close(obj);
576 static const struct got_error *
577 lock_worktree(struct got_worktree *worktree, int operation)
579 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
580 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
581 : got_error_from_errno());
585 static const struct got_error *
586 add_dir_on_disk(struct got_worktree *worktree, const char *path)
588 const struct got_error *err = NULL;
591 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
592 return got_error_from_errno();
594 err = got_path_mkdir(abspath);
595 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
598 if (lstat(abspath, &sb) == -1) {
599 err = got_error_from_errno();
600 } else if (!S_ISDIR(sb.st_mode)) {
601 /* TODO directory is obstructed; do something */
602 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
609 static const struct got_error *
610 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
612 const struct got_error *err = NULL;
615 size_t flen1 = 0, flen2 = 0;
620 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
621 if (flen1 == 0 && ferror(f1)) {
622 err = got_error_from_errno();
625 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
626 if (flen2 == 0 && ferror(f2)) {
627 err = got_error_from_errno();
634 } else if (flen2 == 0) {
638 } else if (flen1 == flen2) {
639 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
652 static const struct got_error *
653 check_files_equal(int *same, const char *f1_path, const char *f2_path)
655 const struct got_error *err = NULL;
658 FILE *f1 = NULL, *f2 = NULL;
662 if (lstat(f1_path, &sb) != 0) {
663 err = got_error_from_errno();
668 if (lstat(f2_path, &sb) != 0) {
669 err = got_error_from_errno();
674 if (size1 != size2) {
679 f1 = fopen(f1_path, "r");
681 return got_error_from_errno();
683 f2 = fopen(f2_path, "r");
685 err = got_error_from_errno();
689 err = check_file_contents_equal(same, f1, f2);
691 if (f1 && fclose(f1) != 0 && err == NULL)
692 err = got_error_from_errno();
693 if (f2 && fclose(f2) != 0 && err == NULL)
694 err = got_error_from_errno();
700 * Perform a 3-way merge where the file's version in the file index (blob2)
701 * acts as the common ancestor, the incoming blob (blob1) acts as the first
702 * derived version, and the file on disk acts as the second derived version.
704 static const struct got_error *
705 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
706 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
707 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
708 struct got_repository *repo,
709 got_worktree_checkout_cb progress_cb, void *progress_arg)
711 const struct got_error *err = NULL;
713 struct got_blob_object *blob2 = NULL;
714 FILE *f1 = NULL, *f2 = NULL;
715 char *blob1_path = NULL, *blob2_path = NULL;
716 char *merged_path = NULL, *base_path = NULL;
717 struct got_object_id id2;
720 int overlapcnt = 0, update_timestamps = 0;
723 parent = dirname(ondisk_path);
725 return got_error_from_errno();
727 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
728 return got_error_from_errno();
730 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
735 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
736 err = got_error_from_errno();
741 err = got_opentemp_named(&blob1_path, &f1, base_path);
744 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
749 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
750 err = got_error_from_errno();
755 err = got_opentemp_named(&blob2_path, &f2, base_path);
759 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
760 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
763 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
767 err = got_object_id_str(&id_str, worktree->base_commit_id);
770 if (asprintf(&label1, "commit %s", id_str) == -1) {
771 err = got_error_from_errno();
775 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
776 blob2_path, ondisk_path, label1, path);
780 (*progress_cb)(progress_arg,
781 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
784 if (fsync(merged_fd) != 0) {
785 err = got_error_from_errno();
789 /* Check if a clean merge has subsumed all local changes. */
790 if (overlapcnt == 0) {
791 err = check_files_equal(&update_timestamps, blob1_path,
797 if (chmod(merged_path, st_mode) != 0) {
798 err = got_error_from_errno();
802 if (rename(merged_path, ondisk_path) != 0) {
803 err = got_error_from_errno();
809 * Do not update timestamps of already modified files. Otherwise,
810 * a future status walk would treat them as unmodified files again.
812 err = got_fileindex_entry_update(ie, ondisk_path,
813 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
815 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
816 err = got_error_from_errno();
817 if (f1 && fclose(f1) != 0 && err == NULL)
818 err = got_error_from_errno();
819 if (f2 && fclose(f2) != 0 && err == NULL)
820 err = got_error_from_errno();
822 got_object_blob_close(blob2);
838 static const struct got_error *
839 install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
840 struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
841 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
842 int restoring_missing_file, struct got_repository *repo,
843 got_worktree_checkout_cb progress_cb, void *progress_arg)
845 const struct got_error *err = NULL;
849 char *tmppath = NULL;
851 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
852 GOT_DEFAULT_FILE_MODE);
854 if (errno == ENOENT) {
855 char *parent = dirname(path);
857 return got_error_from_errno();
858 err = add_dir_on_disk(worktree, parent);
861 fd = open(ondisk_path,
862 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
863 GOT_DEFAULT_FILE_MODE);
865 return got_error_from_errno();
866 } else if (errno == EEXIST) {
867 if (!S_ISREG(st_mode)) {
868 /* TODO file is obstructed; do something */
869 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
872 err = got_opentemp_named_fd(&tmppath, &fd,
879 return got_error_from_errno();
882 if (restoring_missing_file)
883 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
885 (*progress_cb)(progress_arg,
886 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
888 hdrlen = got_object_blob_get_hdrlen(blob);
890 const uint8_t *buf = got_object_blob_get_read_buf(blob);
891 err = got_object_blob_read_block(&len, blob);
895 /* Skip blob object header first time around. */
896 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
898 err = got_error_from_errno();
900 } else if (outlen != len - hdrlen) {
901 err = got_error(GOT_ERR_IO);
908 if (fsync(fd) != 0) {
909 err = got_error_from_errno();
914 if (rename(tmppath, ondisk_path) != 0) {
915 err = got_error_from_errno();
921 if (te_mode & S_IXUSR) {
922 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
923 err = got_error_from_errno();
927 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
928 err = got_error_from_errno();
934 entry = got_fileindex_entry_get(fileindex, path);
936 err = got_fileindex_entry_update(entry, ondisk_path,
937 blob->id.sha1, worktree->base_commit_id->sha1, 1);
939 err = got_fileindex_entry_alloc(&entry, ondisk_path,
940 path, blob->id.sha1, worktree->base_commit_id->sha1);
943 err = got_fileindex_entry_add(fileindex, entry);
946 if (fd != -1 && close(fd) != 0 && err == NULL)
947 err = got_error_from_errno();
952 static const struct got_error *
953 get_file_status(unsigned char *status, struct stat *sb,
954 struct got_fileindex_entry *ie, const char *abspath,
955 struct got_repository *repo)
957 const struct got_error *err = NULL;
958 struct got_object_id id;
962 struct got_blob_object *blob = NULL;
965 *status = GOT_STATUS_NO_CHANGE;
967 if (lstat(abspath, sb) == -1) {
968 if (errno == ENOENT) {
970 *status = GOT_STATUS_MISSING;
972 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
973 & (S_IRWXU | S_IRWXG | S_IRWXO));
975 sb->st_mode = GOT_DEFAULT_FILE_MODE;
978 return got_error_from_errno();
981 if (!S_ISREG(sb->st_mode)) {
982 *status = GOT_STATUS_OBSTRUCTED;
989 if (ie->ctime_sec == sb->st_ctime &&
990 ie->ctime_nsec == sb->st_ctimensec &&
991 ie->mtime_sec == sb->st_mtime &&
992 ie->mtime_sec == sb->st_mtime &&
993 ie->mtime_nsec == sb->st_mtimensec &&
994 ie->size == (sb->st_size & 0xffffffff))
997 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
998 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1002 f = fopen(abspath, "r");
1004 err = got_error_from_errno();
1007 hdrlen = got_object_blob_get_hdrlen(blob);
1009 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1010 err = got_object_blob_read_block(&blen, blob);
1013 /* Skip length of blob object header first time around. */
1014 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1015 if (flen == 0 && ferror(f)) {
1016 err = got_error_from_errno();
1021 *status = GOT_STATUS_MODIFY;
1023 } else if (flen == 0) {
1025 *status = GOT_STATUS_MODIFY;
1027 } else if (blen - hdrlen == flen) {
1028 /* Skip blob object header first time around. */
1029 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1030 *status = GOT_STATUS_MODIFY;
1034 *status = GOT_STATUS_MODIFY;
1041 got_object_blob_close(blob);
1047 static const struct got_error *
1048 update_blob(struct got_worktree *worktree,
1049 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1050 struct got_tree_entry *te, const char *path,
1051 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1052 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1054 const struct got_error *err = NULL;
1055 struct got_blob_object *blob = NULL;
1057 unsigned char status = GOT_STATUS_NO_CHANGE;
1060 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1061 return got_error_from_errno();
1063 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1067 if (status == GOT_STATUS_OBSTRUCTED) {
1068 (*progress_cb)(progress_arg, status, path);
1072 if (ie && status != GOT_STATUS_MISSING) {
1073 if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1074 SHA1_DIGEST_LENGTH) == 0) {
1075 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1079 if (memcmp(ie->blob_sha1,
1080 te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1084 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1088 if (status == GOT_STATUS_MODIFY)
1089 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1090 te->mode, sb.st_mode, blob, repo, progress_cb,
1093 err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1094 te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1095 repo, progress_cb, progress_arg);
1097 got_object_blob_close(blob);
1103 static const struct got_error *
1104 remove_ondisk_file(const char *root_path, const char *path)
1106 const struct got_error *err = NULL;
1107 char *ondisk_path = NULL;
1109 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1110 return got_error_from_errno();
1112 if (unlink(ondisk_path) == -1) {
1113 if (errno != ENOENT)
1114 err = got_error_from_errno();
1116 char *parent = dirname(ondisk_path);
1117 while (parent && strcmp(parent, root_path) != 0) {
1118 if (rmdir(parent) == -1) {
1119 if (errno != ENOTEMPTY)
1120 err = got_error_from_errno();
1123 parent = dirname(parent);
1130 struct diff_cb_arg {
1131 struct got_fileindex *fileindex;
1132 struct got_worktree *worktree;
1133 struct got_repository *repo;
1134 got_worktree_checkout_cb progress_cb;
1136 got_worktree_cancel_cb cancel_cb;
1140 static const struct got_error *
1141 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1142 struct got_tree_entry *te, const char *parent_path)
1144 struct diff_cb_arg *a = arg;
1146 return update_blob(a->worktree, a->fileindex, ie, te,
1147 ie->path, a->repo, a->progress_cb, a->progress_arg,
1148 a->cancel_cb, a->cancel_arg);
1151 static const struct got_error *
1152 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1154 const struct got_error *err;
1155 struct diff_cb_arg *a = arg;
1157 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1159 err = remove_ondisk_file(a->worktree->root_path, ie->path);
1162 got_fileindex_entry_remove(a->fileindex, ie);
1166 static const struct got_error *
1167 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1169 struct diff_cb_arg *a = arg;
1170 const struct got_error *err;
1173 if (asprintf(&path, "%s%s%s", parent_path,
1174 parent_path[0] ? "/" : "", te->name)
1176 return got_error_from_errno();
1178 if (S_ISDIR(te->mode))
1179 err = add_dir_on_disk(a->worktree, path);
1181 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1182 a->repo, a->progress_cb, a->progress_arg,
1183 a->cancel_cb, a->cancel_arg);
1189 const struct got_error *
1190 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1192 const struct got_error *err = NULL;
1193 char *uuidstr = NULL;
1194 uint32_t uuid_status;
1198 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1199 if (uuid_status != uuid_s_ok)
1200 return got_error_uuid(uuid_status);
1202 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1204 err = got_error_from_errno();
1212 * Prevent Git's garbage collector from deleting our base commit by
1213 * setting a reference to our base commit's ID.
1215 static const struct got_error *
1216 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1218 const struct got_error *err = NULL;
1219 struct got_reference *ref = NULL;
1222 err = got_worktree_get_base_ref_name(&refname, worktree);
1226 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1230 err = got_ref_write(ref, repo);
1239 const struct got_error *
1240 got_worktree_checkout_files(struct got_worktree *worktree,
1241 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1242 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1244 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1245 struct got_commit_object *commit = NULL;
1246 struct got_object_id *tree_id = NULL;
1247 struct got_tree_object *tree = NULL;
1248 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1249 struct got_fileindex *fileindex = NULL;
1250 FILE *index = NULL, *new_index = NULL;
1251 struct got_fileindex_diff_tree_cb diff_cb;
1252 struct diff_cb_arg arg;
1254 err = lock_worktree(worktree, LOCK_EX);
1258 fileindex = got_fileindex_alloc();
1259 if (fileindex == NULL) {
1260 err = got_error_from_errno();
1264 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1265 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1266 err = got_error_from_errno();
1267 fileindex_path = NULL;
1272 * Read the file index.
1273 * Checking out files is supposed to be an idempotent operation.
1274 * If the on-disk file index is incomplete we will try to complete it.
1276 index = fopen(fileindex_path, "rb");
1277 if (index == NULL) {
1278 if (errno != ENOENT) {
1279 err = got_error_from_errno();
1283 err = got_fileindex_read(fileindex, index);
1289 err = got_opentemp_named(&new_fileindex_path, &new_index,
1294 err = ref_base_commit(worktree, repo);
1298 err = got_object_open_as_commit(&commit, repo,
1299 worktree->base_commit_id);
1303 err = got_object_id_by_path(&tree_id, repo,
1304 worktree->base_commit_id, worktree->path_prefix);
1308 err = got_object_open_as_tree(&tree, repo, tree_id);
1312 diff_cb.diff_old_new = diff_old_new;
1313 diff_cb.diff_old = diff_old;
1314 diff_cb.diff_new = diff_new;
1315 arg.fileindex = fileindex;
1316 arg.worktree = worktree;
1318 arg.progress_cb = progress_cb;
1319 arg.progress_arg = progress_arg;
1320 arg.cancel_cb = cancel_cb;
1321 arg.cancel_arg = cancel_arg;
1322 checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1325 /* Try to sync the fileindex back to disk in any case. */
1326 err = got_fileindex_write(fileindex, new_index);
1330 if (rename(new_fileindex_path, fileindex_path) != 0) {
1331 err = got_error_from_errno();
1332 unlink(new_fileindex_path);
1336 free(new_fileindex_path);
1337 new_fileindex_path = NULL;
1341 got_object_tree_close(tree);
1343 got_object_commit_close(commit);
1344 if (new_fileindex_path)
1345 unlink(new_fileindex_path);
1348 free(new_fileindex_path);
1349 free(fileindex_path);
1350 got_fileindex_free(fileindex);
1353 unlockerr = lock_worktree(worktree, LOCK_SH);
1354 if (unlockerr && err == NULL)
1359 struct diff_dir_cb_arg {
1360 struct got_fileindex *fileindex;
1361 struct got_worktree *worktree;
1362 const char *status_path;
1363 size_t status_path_len;
1364 struct got_repository *repo;
1365 got_worktree_status_cb status_cb;
1367 got_worktree_cancel_cb cancel_cb;
1371 static const struct got_error *
1372 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1373 got_worktree_status_cb status_cb, void *status_arg,
1374 struct got_repository *repo)
1376 const struct got_error *err = NULL;
1377 unsigned char status = GOT_STATUS_NO_CHANGE;
1379 struct got_object_id id;
1381 err = get_file_status(&status, &sb, ie, abspath, repo);
1382 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1383 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1384 err = (*status_cb)(status_arg, status, ie->path, &id);
1389 static const struct got_error *
1390 status_old_new(void *arg, struct got_fileindex_entry *ie,
1391 struct dirent *de, const char *parent_path)
1393 const struct got_error *err = NULL;
1394 struct diff_dir_cb_arg *a = arg;
1397 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1398 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1401 if (parent_path[0]) {
1402 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1403 parent_path, de->d_name) == -1)
1404 return got_error_from_errno();
1406 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1408 return got_error_from_errno();
1411 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1417 static const struct got_error *
1418 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1420 struct diff_dir_cb_arg *a = arg;
1421 struct got_object_id id;
1423 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1426 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1427 return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1431 static const struct got_error *
1432 status_new(void *arg, struct dirent *de, const char *parent_path)
1434 const struct got_error *err = NULL;
1435 struct diff_dir_cb_arg *a = arg;
1438 if (de->d_type == DT_DIR)
1441 /* XXX ignore symlinks for now */
1442 if (de->d_type == DT_LNK)
1445 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1448 if (parent_path[0]) {
1449 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1450 return got_error_from_errno();
1455 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1462 const struct got_error *
1463 got_worktree_status(struct got_worktree *worktree, const char *path,
1464 struct got_repository *repo, got_worktree_status_cb status_cb,
1465 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1467 const struct got_error *err = NULL;
1468 DIR *workdir = NULL;
1469 char *fileindex_path = NULL;
1470 struct got_fileindex *fileindex = NULL;
1472 struct got_fileindex_diff_dir_cb fdiff_cb;
1473 struct diff_dir_cb_arg arg;
1474 char *ondisk_path = NULL;
1476 fileindex = got_fileindex_alloc();
1477 if (fileindex == NULL) {
1478 err = got_error_from_errno();
1482 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1483 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1484 err = got_error_from_errno();
1485 fileindex_path = NULL;
1489 index = fopen(fileindex_path, "rb");
1490 if (index == NULL) {
1491 if (errno != ENOENT) {
1492 err = got_error_from_errno();
1496 err = got_fileindex_read(fileindex, index);
1502 if (asprintf(&ondisk_path, "%s%s%s",
1503 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1504 err = got_error_from_errno();
1507 workdir = opendir(ondisk_path);
1508 if (workdir == NULL) {
1509 if (errno == ENOTDIR) {
1510 struct got_fileindex_entry *ie;
1511 ie = got_fileindex_entry_get(fileindex, path);
1513 err = got_error(GOT_ERR_BAD_PATH);
1516 err = report_file_status(ie, ondisk_path,
1517 status_cb, status_arg, repo);
1520 err = got_error_from_errno();
1524 fdiff_cb.diff_old_new = status_old_new;
1525 fdiff_cb.diff_old = status_old;
1526 fdiff_cb.diff_new = status_new;
1527 arg.fileindex = fileindex;
1528 arg.worktree = worktree;
1529 arg.status_path = path;
1530 arg.status_path_len = strlen(path);
1532 arg.status_cb = status_cb;
1533 arg.status_arg = status_arg;
1534 arg.cancel_cb = cancel_cb;
1535 arg.cancel_arg = cancel_arg;
1536 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1537 path, repo, &fdiff_cb, &arg);
1542 free(fileindex_path);
1543 got_fileindex_free(fileindex);