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>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
44 #include "got_lib_worktree.h"
45 #include "got_lib_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 static const struct got_error *
61 create_meta_file(const char *path_got, const char *name, const char *content)
63 const struct got_error *err = NULL;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
68 err = got_error_from_errno();
73 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
74 GOT_DEFAULT_FILE_MODE);
76 err = got_error_from_errno();
81 int len = dprintf(fd, "%s\n", content);
82 if (len != strlen(content) + 1) {
83 err = got_error_from_errno();
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_from_errno();
95 static const struct got_error *
96 update_meta_file(const char *path_got, const char *name, const char *content)
98 const struct got_error *err = NULL;
100 char *tmppath = NULL;
103 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
104 err = got_error_from_errno();
109 err = got_opentemp_named(&tmppath, &tmpfile, path);
114 int len = fprintf(tmpfile, "%s\n", content);
115 if (len != strlen(content) + 1) {
116 err = got_error_from_errno();
121 if (rename(tmppath, path) != 0) {
122 err = got_error_from_errno();
129 if (fclose(tmpfile) != 0 && err == NULL)
130 err = got_error_from_errno();
134 static const struct got_error *
135 read_meta_file(char **content, const char *path_got, const char *name)
137 const struct got_error *err = NULL;
145 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
146 err = got_error_from_errno();
151 fd = open(path, O_RDONLY | O_NOFOLLOW);
154 err = got_error(GOT_ERR_WORKTREE_META);
156 err = got_error_from_errno();
159 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
160 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
161 : got_error_from_errno());
165 if (lstat(path, &sb) != 0) {
166 err = got_error_from_errno();
169 *content = calloc(1, sb.st_size);
170 if (*content == NULL) {
171 err = got_error_from_errno();
175 n = read(fd, *content, sb.st_size);
176 if (n != sb.st_size) {
177 err = (n == -1 ? got_error_from_errno() :
178 got_error(GOT_ERR_WORKTREE_META));
181 if ((*content)[sb.st_size - 1] != '\n') {
182 err = got_error(GOT_ERR_WORKTREE_META);
185 (*content)[sb.st_size - 1] = '\0';
188 if (fd != -1 && close(fd) == -1 && err == NULL)
189 err = got_error_from_errno();
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
205 uint32_t uuid_status;
207 char *path_got = NULL;
209 char *formatstr = NULL;
210 char *absprefix = NULL;
211 char *basestr = NULL;
212 char *uuidstr = NULL;
214 if (strcmp(path, got_repo_get_path(repo)) == 0) {
215 err = got_error(GOT_ERR_WORKTREE_REPO);
219 err = got_ref_resolve(&commit_id, repo, head_ref);
222 err = got_object_get_type(&obj_type, repo, commit_id);
225 if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 return got_error(GOT_ERR_OBJ_TYPE);
228 if (!got_path_is_absolute(prefix)) {
229 if (asprintf(&absprefix, "/%s", prefix) == -1)
230 return got_error_from_errno();
233 /* Create top-level directory (may already exist). */
234 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 err = got_error_from_errno();
239 /* Create .got directory (may already exist). */
240 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 err = got_error_from_errno();
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_from_errno();
249 /* Create an empty lock file. */
250 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 /* Create an empty file index. */
255 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 /* Write the HEAD reference. */
260 refstr = got_ref_to_str(head_ref);
261 if (refstr == NULL) {
262 err = got_error_from_errno();
265 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status);
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status);
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno();
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
324 static const struct got_error *
325 open_worktree(struct got_worktree **worktree, const char *path)
327 const struct got_error *err = NULL;
329 char *formatstr = NULL;
330 char *uuidstr = NULL;
331 char *path_lock = NULL;
332 char *base_commit_id_str = NULL;
333 char *head_ref_str = NULL;
334 int version, fd = -1;
336 struct got_repository *repo = NULL;
337 uint32_t uuid_status;
341 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
342 err = got_error_from_errno();
347 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
348 err = got_error_from_errno();
353 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
355 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
356 : got_error_from_errno());
360 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
364 version = strtonum(formatstr, 1, INT_MAX, &errstr);
366 err = got_error(GOT_ERR_WORKTREE_META);
369 if (version != GOT_WORKTREE_FORMAT_VERSION) {
370 err = got_error(GOT_ERR_WORKTREE_VERS);
374 *worktree = calloc(1, sizeof(**worktree));
375 if (*worktree == NULL) {
376 err = got_error_from_errno();
379 (*worktree)->lockfd = -1;
381 (*worktree)->root_path = strdup(path);
382 if ((*worktree)->root_path == NULL) {
383 err = got_error_from_errno();
386 err = read_meta_file(&(*worktree)->repo_path, path_got,
387 GOT_WORKTREE_REPOSITORY);
391 err = read_meta_file(&(*worktree)->path_prefix, path_got,
392 GOT_WORKTREE_PATH_PREFIX);
396 err = read_meta_file(&base_commit_id_str, path_got,
397 GOT_WORKTREE_BASE_COMMIT);
401 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
404 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
405 if (uuid_status != uuid_s_ok) {
406 err = got_error_uuid(uuid_status);
410 err = got_repo_open(&repo, (*worktree)->repo_path);
414 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
419 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
423 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
426 got_repo_close(repo);
430 free(base_commit_id_str);
436 if (*worktree != NULL)
437 got_worktree_close(*worktree);
440 (*worktree)->lockfd = fd;
445 const struct got_error *
446 got_worktree_open(struct got_worktree **worktree, const char *path)
448 const struct got_error *err = NULL;
451 err = open_worktree(worktree, path);
452 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
456 path = dirname(path);
458 return got_error_from_errno();
459 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
461 return got_error(GOT_ERR_NOT_WORKTREE);
464 const struct got_error *
465 got_worktree_close(struct got_worktree *worktree)
467 const struct got_error *err = NULL;
468 free(worktree->root_path);
469 free(worktree->repo_path);
470 free(worktree->path_prefix);
471 free(worktree->base_commit_id);
472 if (worktree->head_ref)
473 got_ref_close(worktree->head_ref);
474 if (worktree->lockfd != -1)
475 if (close(worktree->lockfd) != 0)
476 err = got_error_from_errno();
482 got_worktree_get_root_path(struct got_worktree *worktree)
484 return worktree->root_path;
488 got_worktree_get_repo_path(struct got_worktree *worktree)
490 return worktree->repo_path;
494 got_worktree_get_path_prefix(struct got_worktree *worktree)
496 return worktree->path_prefix;
499 const struct got_error *
500 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
501 const char *path_prefix)
503 char *absprefix = NULL;
505 if (!got_path_is_absolute(path_prefix)) {
506 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
507 return got_error_from_errno();
509 *match = (strcmp(absprefix ? absprefix : path_prefix,
510 worktree->path_prefix) == 0);
516 got_worktree_get_head_ref_name(struct got_worktree *worktree)
518 return got_ref_to_str(worktree->head_ref);
521 struct got_reference *
522 got_worktree_get_head_ref(struct got_worktree *worktree)
524 return got_ref_dup(worktree->head_ref);
527 struct got_object_id *
528 got_worktree_get_base_commit_id(struct got_worktree *worktree)
530 return worktree->base_commit_id;
533 const struct got_error *
534 got_worktree_set_base_commit_id(struct got_worktree *worktree,
535 struct got_repository *repo, struct got_object_id *commit_id)
537 const struct got_error *err;
538 struct got_object *obj = NULL;
540 char *path_got = NULL;
542 if (asprintf(&path_got, "%s/%s", worktree->root_path,
543 GOT_WORKTREE_GOT_DIR) == -1) {
544 err = got_error_from_errno();
549 err = got_object_open(&obj, repo, commit_id);
553 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
554 err = got_error(GOT_ERR_OBJ_TYPE);
558 /* Record our base commit. */
559 err = got_object_id_str(&id_str, commit_id);
562 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
566 free(worktree->base_commit_id);
567 worktree->base_commit_id = got_object_id_dup(commit_id);
568 if (worktree->base_commit_id == NULL) {
569 err = got_error_from_errno();
574 got_object_close(obj);
580 static const struct got_error *
581 lock_worktree(struct got_worktree *worktree, int operation)
583 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
584 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
585 : got_error_from_errno());
589 static const struct got_error *
590 add_dir_on_disk(struct got_worktree *worktree, const char *path)
592 const struct got_error *err = NULL;
595 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
596 return got_error_from_errno();
598 err = got_path_mkdir(abspath);
599 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
602 if (lstat(abspath, &sb) == -1) {
603 err = got_error_from_errno();
604 } else if (!S_ISDIR(sb.st_mode)) {
605 /* TODO directory is obstructed; do something */
606 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
613 static const struct got_error *
614 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
616 const struct got_error *err = NULL;
619 size_t flen1 = 0, flen2 = 0;
624 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
625 if (flen1 == 0 && ferror(f1)) {
626 err = got_error_from_errno();
629 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
630 if (flen2 == 0 && ferror(f2)) {
631 err = got_error_from_errno();
638 } else if (flen2 == 0) {
642 } else if (flen1 == flen2) {
643 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
656 static const struct got_error *
657 check_files_equal(int *same, const char *f1_path, const char *f2_path)
659 const struct got_error *err = NULL;
662 FILE *f1 = NULL, *f2 = NULL;
666 if (lstat(f1_path, &sb) != 0) {
667 err = got_error_from_errno();
672 if (lstat(f2_path, &sb) != 0) {
673 err = got_error_from_errno();
678 if (size1 != size2) {
683 f1 = fopen(f1_path, "r");
685 return got_error_from_errno();
687 f2 = fopen(f2_path, "r");
689 err = got_error_from_errno();
693 err = check_file_contents_equal(same, f1, f2);
695 if (f1 && fclose(f1) != 0 && err == NULL)
696 err = got_error_from_errno();
697 if (f2 && fclose(f2) != 0 && err == NULL)
698 err = got_error_from_errno();
704 * Perform a 3-way merge where the file's version in the file index (blob2)
705 * acts as the common ancestor, the incoming blob (blob1) acts as the first
706 * derived version, and the file on disk acts as the second derived version.
708 static const struct got_error *
709 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
710 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
711 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
712 struct got_repository *repo,
713 got_worktree_checkout_cb progress_cb, void *progress_arg)
715 const struct got_error *err = NULL;
717 struct got_blob_object *blob2 = NULL;
718 FILE *f1 = NULL, *f2 = NULL;
719 char *blob1_path = NULL, *blob2_path = NULL;
720 char *merged_path = NULL, *base_path = NULL;
723 int overlapcnt = 0, update_timestamps = 0;
726 parent = dirname(ondisk_path);
728 return got_error_from_errno();
730 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
731 return got_error_from_errno();
733 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
738 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
739 err = got_error_from_errno();
744 err = got_opentemp_named(&blob1_path, &f1, base_path);
747 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
752 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
753 err = got_error_from_errno();
758 err = got_opentemp_named(&blob2_path, &f2, base_path);
761 if (got_fileindex_entry_has_blob(ie)) {
762 struct got_object_id id2;
763 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
764 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
767 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
772 * If the file has no blob, this is an "add vs add" conflict,
773 * and we simply use an empty ancestor file to make both files
774 * appear in the merged result in their entirety.
778 err = got_object_id_str(&id_str, worktree->base_commit_id);
781 if (asprintf(&label1, "commit %s", id_str) == -1) {
782 err = got_error_from_errno();
786 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
787 blob2_path, ondisk_path, label1, path);
791 (*progress_cb)(progress_arg,
792 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
795 if (fsync(merged_fd) != 0) {
796 err = got_error_from_errno();
800 /* Check if a clean merge has subsumed all local changes. */
801 if (overlapcnt == 0) {
802 err = check_files_equal(&update_timestamps, blob1_path,
808 if (chmod(merged_path, st_mode) != 0) {
809 err = got_error_from_errno();
813 if (rename(merged_path, ondisk_path) != 0) {
814 err = got_error_from_errno();
820 * Do not update timestamps of already modified files. Otherwise,
821 * a future status walk would treat them as unmodified files again.
823 err = got_fileindex_entry_update(ie, ondisk_path,
824 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
826 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
827 err = got_error_from_errno();
828 if (f1 && fclose(f1) != 0 && err == NULL)
829 err = got_error_from_errno();
830 if (f2 && fclose(f2) != 0 && err == NULL)
831 err = got_error_from_errno();
833 got_object_blob_close(blob2);
849 static const struct got_error *
850 update_blob_fileindex_entry(struct got_worktree *worktree,
851 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
852 const char *ondisk_path, const char *path, struct got_blob_object *blob,
853 int update_timestamps)
855 const struct got_error *err = NULL;
858 ie = got_fileindex_entry_get(fileindex, path);
860 err = got_fileindex_entry_update(ie, ondisk_path,
861 blob->id.sha1, worktree->base_commit_id->sha1,
864 struct got_fileindex_entry *new_ie;
865 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
866 path, blob->id.sha1, worktree->base_commit_id->sha1);
868 err = got_fileindex_entry_add(fileindex, new_ie);
873 static const struct got_error *
874 install_blob(struct got_worktree *worktree, const char *ondisk_path,
875 const char *path, uint16_t te_mode, uint16_t st_mode,
876 struct got_blob_object *blob, int restoring_missing_file,
877 int reverting_versioned_file, struct got_repository *repo,
878 got_worktree_checkout_cb progress_cb, void *progress_arg)
880 const struct got_error *err = NULL;
884 char *tmppath = NULL;
886 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
887 GOT_DEFAULT_FILE_MODE);
889 if (errno == ENOENT) {
890 char *parent = dirname(path);
892 return got_error_from_errno();
893 err = add_dir_on_disk(worktree, parent);
896 fd = open(ondisk_path,
897 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
898 GOT_DEFAULT_FILE_MODE);
900 return got_error_from_errno();
901 } else if (errno == EEXIST) {
902 if (!S_ISREG(st_mode)) {
903 /* TODO file is obstructed; do something */
904 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
907 err = got_opentemp_named_fd(&tmppath, &fd,
914 return got_error_from_errno();
917 if (restoring_missing_file)
918 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
919 else if (reverting_versioned_file)
920 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
922 (*progress_cb)(progress_arg,
923 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
925 hdrlen = got_object_blob_get_hdrlen(blob);
927 const uint8_t *buf = got_object_blob_get_read_buf(blob);
928 err = got_object_blob_read_block(&len, blob);
932 /* Skip blob object header first time around. */
933 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
935 err = got_error_from_errno();
937 } else if (outlen != len - hdrlen) {
938 err = got_error(GOT_ERR_IO);
945 if (fsync(fd) != 0) {
946 err = got_error_from_errno();
951 if (rename(tmppath, ondisk_path) != 0) {
952 err = got_error_from_errno();
958 if (te_mode & S_IXUSR) {
959 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
960 err = got_error_from_errno();
964 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
965 err = got_error_from_errno();
971 if (fd != -1 && close(fd) != 0 && err == NULL)
972 err = got_error_from_errno();
977 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
978 static const struct got_error *
979 get_modified_file_content_status(unsigned char *status, FILE *f)
981 const struct got_error *err = NULL;
982 const char *markers[3] = {
983 GOT_DIFF_CONFLICT_MARKER_BEGIN,
984 GOT_DIFF_CONFLICT_MARKER_SEP,
985 GOT_DIFF_CONFLICT_MARKER_END
990 const char delim[3] = {'\0', '\0', '\0'};
992 while (*status == GOT_STATUS_MODIFY) {
993 line = fparseln(f, &len, NULL, delim, 0);
997 err = got_ferror(f, GOT_ERR_IO);
1001 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1002 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
1003 *status = GOT_STATUS_CONFLICT;
1012 static const struct got_error *
1013 get_file_status(unsigned char *status, struct stat *sb,
1014 struct got_fileindex_entry *ie, const char *abspath,
1015 struct got_repository *repo)
1017 const struct got_error *err = NULL;
1018 struct got_object_id id;
1022 struct got_blob_object *blob = NULL;
1025 *status = GOT_STATUS_NO_CHANGE;
1027 if (lstat(abspath, sb) == -1) {
1028 if (errno == ENOENT) {
1030 if (got_fileindex_entry_has_file_on_disk(ie))
1031 *status = GOT_STATUS_MISSING;
1033 *status = GOT_STATUS_DELETE;
1035 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1036 & (S_IRWXU | S_IRWXG | S_IRWXO));
1038 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1041 return got_error_from_errno();
1044 if (!S_ISREG(sb->st_mode)) {
1045 *status = GOT_STATUS_OBSTRUCTED;
1052 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1053 *status = GOT_STATUS_DELETE;
1055 } else if (!got_fileindex_entry_has_blob(ie)) {
1056 *status = GOT_STATUS_ADD;
1060 if (ie->ctime_sec == sb->st_ctime &&
1061 ie->ctime_nsec == sb->st_ctimensec &&
1062 ie->mtime_sec == sb->st_mtime &&
1063 ie->mtime_sec == sb->st_mtime &&
1064 ie->mtime_nsec == sb->st_mtimensec &&
1065 ie->size == (sb->st_size & 0xffffffff))
1068 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1069 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1073 f = fopen(abspath, "r");
1075 err = got_error_from_errno();
1078 hdrlen = got_object_blob_get_hdrlen(blob);
1080 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1081 err = got_object_blob_read_block(&blen, blob);
1084 /* Skip length of blob object header first time around. */
1085 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1086 if (flen == 0 && ferror(f)) {
1087 err = got_error_from_errno();
1092 *status = GOT_STATUS_MODIFY;
1094 } else if (flen == 0) {
1096 *status = GOT_STATUS_MODIFY;
1098 } else if (blen - hdrlen == flen) {
1099 /* Skip blob object header first time around. */
1100 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1101 *status = GOT_STATUS_MODIFY;
1105 *status = GOT_STATUS_MODIFY;
1111 if (*status == GOT_STATUS_MODIFY) {
1113 err = get_modified_file_content_status(status, f);
1117 got_object_blob_close(blob);
1123 static const struct got_error *
1124 update_blob(struct got_worktree *worktree,
1125 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1126 struct got_tree_entry *te, const char *path,
1127 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1130 const struct got_error *err = NULL;
1131 struct got_blob_object *blob = NULL;
1133 unsigned char status = GOT_STATUS_NO_CHANGE;
1136 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1137 return got_error_from_errno();
1139 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1143 if (status == GOT_STATUS_OBSTRUCTED) {
1144 (*progress_cb)(progress_arg, status, path);
1148 if (ie && status != GOT_STATUS_MISSING) {
1149 if (got_fileindex_entry_has_commit(ie) &&
1150 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1151 SHA1_DIGEST_LENGTH) == 0) {
1152 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1156 if (got_fileindex_entry_has_blob(ie) &&
1157 memcmp(ie->blob_sha1, te->id->sha1,
1158 SHA1_DIGEST_LENGTH) == 0)
1162 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1166 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1167 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1168 te->mode, sb.st_mode, blob, repo, progress_cb,
1170 else if (status == GOT_STATUS_DELETE) {
1171 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1172 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1173 ondisk_path, path, blob, 0);
1177 err = install_blob(worktree, ondisk_path, path, te->mode,
1178 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1179 repo, progress_cb, progress_arg);
1182 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1183 ondisk_path, path, blob, 1);
1187 got_object_blob_close(blob);
1193 static const struct got_error *
1194 remove_ondisk_file(const char *root_path, const char *path)
1196 const struct got_error *err = NULL;
1197 char *ondisk_path = NULL;
1199 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1200 return got_error_from_errno();
1202 if (unlink(ondisk_path) == -1) {
1203 if (errno != ENOENT)
1204 err = got_error_from_errno();
1206 char *parent = dirname(ondisk_path);
1207 while (parent && strcmp(parent, root_path) != 0) {
1208 if (rmdir(parent) == -1) {
1209 if (errno != ENOTEMPTY)
1210 err = got_error_from_errno();
1213 parent = dirname(parent);
1220 static const struct got_error *
1221 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1222 struct got_fileindex_entry *ie, const char *parent_path,
1223 struct got_repository *repo,
1224 got_worktree_checkout_cb progress_cb, void *progress_arg)
1226 const struct got_error *err = NULL;
1227 unsigned char status;
1231 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1233 return got_error_from_errno();
1235 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1239 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1240 status == GOT_STATUS_ADD) {
1241 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1243 * Preserve the working file and change the deleted blob's
1244 * entry into a schedule-add entry.
1246 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1251 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1252 if (status == GOT_STATUS_NO_CHANGE) {
1253 err = remove_ondisk_file(worktree->root_path, ie->path);
1257 got_fileindex_entry_remove(fileindex, ie);
1263 struct diff_cb_arg {
1264 struct got_fileindex *fileindex;
1265 struct got_worktree *worktree;
1266 struct got_repository *repo;
1267 got_worktree_checkout_cb progress_cb;
1269 got_worktree_cancel_cb cancel_cb;
1273 static const struct got_error *
1274 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1275 struct got_tree_entry *te, const char *parent_path)
1277 struct diff_cb_arg *a = arg;
1279 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1280 return got_error(GOT_ERR_CANCELLED);
1282 return update_blob(a->worktree, a->fileindex, ie, te,
1283 ie->path, a->repo, a->progress_cb, a->progress_arg);
1286 static const struct got_error *
1287 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1289 struct diff_cb_arg *a = arg;
1291 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1292 return got_error(GOT_ERR_CANCELLED);
1294 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1295 a->repo, a->progress_cb, a->progress_arg);
1298 static const struct got_error *
1299 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1301 struct diff_cb_arg *a = arg;
1302 const struct got_error *err;
1305 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1306 return got_error(GOT_ERR_CANCELLED);
1308 if (asprintf(&path, "%s%s%s", parent_path,
1309 parent_path[0] ? "/" : "", te->name)
1311 return got_error_from_errno();
1313 if (S_ISDIR(te->mode))
1314 err = add_dir_on_disk(a->worktree, path);
1316 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1317 a->repo, a->progress_cb, a->progress_arg);
1323 const struct got_error *
1324 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1326 const struct got_error *err = NULL;
1327 char *uuidstr = NULL;
1328 uint32_t uuid_status;
1332 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1333 if (uuid_status != uuid_s_ok)
1334 return got_error_uuid(uuid_status);
1336 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1338 err = got_error_from_errno();
1346 * Prevent Git's garbage collector from deleting our base commit by
1347 * setting a reference to our base commit's ID.
1349 static const struct got_error *
1350 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1352 const struct got_error *err = NULL;
1353 struct got_reference *ref = NULL;
1356 err = got_worktree_get_base_ref_name(&refname, worktree);
1360 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1364 err = got_ref_write(ref, repo);
1373 const struct got_error *
1374 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1375 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1376 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1378 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1379 struct got_commit_object *commit = NULL;
1380 struct got_object_id *tree_id = NULL;
1381 struct got_tree_object *tree = NULL;
1382 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1383 struct got_fileindex *fileindex = NULL;
1384 FILE *index = NULL, *new_index = NULL;
1385 struct got_fileindex_diff_tree_cb diff_cb;
1386 struct diff_cb_arg arg;
1387 char *relpath = NULL, *entry_name = NULL;
1389 err = lock_worktree(worktree, LOCK_EX);
1393 fileindex = got_fileindex_alloc();
1394 if (fileindex == NULL) {
1395 err = got_error_from_errno();
1399 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1400 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1401 err = got_error_from_errno();
1402 fileindex_path = NULL;
1407 * Read the file index.
1408 * Checking out files is supposed to be an idempotent operation.
1409 * If the on-disk file index is incomplete we will try to complete it.
1411 index = fopen(fileindex_path, "rb");
1412 if (index == NULL) {
1413 if (errno != ENOENT) {
1414 err = got_error_from_errno();
1418 err = got_fileindex_read(fileindex, index);
1424 err = got_opentemp_named(&new_fileindex_path, &new_index,
1429 err = ref_base_commit(worktree, repo);
1433 err = got_object_open_as_commit(&commit, repo,
1434 worktree->base_commit_id);
1441 relpath = strdup(path);
1442 if (relpath == NULL) {
1443 err = got_error_from_errno();
1446 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1447 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1449 err = got_error_from_errno();
1452 err = got_object_id_by_path(&tree_id, repo,
1453 worktree->base_commit_id, tree_path);
1457 err = got_object_get_type(&obj_type, repo, tree_id);
1460 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1461 /* Split provided path into parent dir + entry name. */
1462 if (strchr(path, '/') == NULL) {
1463 relpath = strdup("");
1464 if (relpath == NULL) {
1465 err = got_error_from_errno();
1468 tree_path = strdup(worktree->path_prefix);
1469 if (tree_path == NULL) {
1470 err = got_error_from_errno();
1474 err = got_path_dirname(&relpath, path);
1477 if (asprintf(&tree_path, "%s%s%s",
1478 worktree->path_prefix,
1479 got_path_is_root_dir(
1480 worktree->path_prefix) ? "" : "/",
1482 err = got_error_from_errno();
1486 err = got_object_id_by_path(&tree_id, repo,
1487 worktree->base_commit_id, tree_path);
1491 entry_name = basename(path);
1492 if (entry_name == NULL) {
1493 err = got_error_from_errno();
1498 relpath = strdup("");
1499 if (relpath == NULL) {
1500 err = got_error_from_errno();
1503 err = got_object_id_by_path(&tree_id, repo,
1504 worktree->base_commit_id, worktree->path_prefix);
1509 err = got_object_open_as_tree(&tree, repo, tree_id);
1514 got_object_tree_find_entry(tree, entry_name) == NULL) {
1515 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1519 diff_cb.diff_old_new = diff_old_new;
1520 diff_cb.diff_old = diff_old;
1521 diff_cb.diff_new = diff_new;
1522 arg.fileindex = fileindex;
1523 arg.worktree = worktree;
1525 arg.progress_cb = progress_cb;
1526 arg.progress_arg = progress_arg;
1527 arg.cancel_cb = cancel_cb;
1528 arg.cancel_arg = cancel_arg;
1529 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1530 entry_name, repo, &diff_cb, &arg);
1532 /* Try to sync the fileindex back to disk in any case. */
1533 err = got_fileindex_write(fileindex, new_index);
1537 if (rename(new_fileindex_path, fileindex_path) != 0) {
1538 err = got_error_from_errno();
1539 unlink(new_fileindex_path);
1543 free(new_fileindex_path);
1544 new_fileindex_path = NULL;
1549 got_object_tree_close(tree);
1551 got_object_commit_close(commit);
1552 if (new_fileindex_path)
1553 unlink(new_fileindex_path);
1556 free(new_fileindex_path);
1557 free(fileindex_path);
1558 got_fileindex_free(fileindex);
1561 unlockerr = lock_worktree(worktree, LOCK_SH);
1562 if (unlockerr && err == NULL)
1567 struct diff_dir_cb_arg {
1568 struct got_fileindex *fileindex;
1569 struct got_worktree *worktree;
1570 const char *status_path;
1571 size_t status_path_len;
1572 struct got_repository *repo;
1573 got_worktree_status_cb status_cb;
1575 got_worktree_cancel_cb cancel_cb;
1579 static const struct got_error *
1580 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1581 got_worktree_status_cb status_cb, void *status_arg,
1582 struct got_repository *repo)
1584 const struct got_error *err = NULL;
1585 unsigned char status = GOT_STATUS_NO_CHANGE;
1587 struct got_object_id id;
1589 err = get_file_status(&status, &sb, ie, abspath, repo);
1590 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1591 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1592 err = (*status_cb)(status_arg, status, ie->path, &id);
1597 static const struct got_error *
1598 status_old_new(void *arg, struct got_fileindex_entry *ie,
1599 struct dirent *de, const char *parent_path)
1601 const struct got_error *err = NULL;
1602 struct diff_dir_cb_arg *a = arg;
1605 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1606 return got_error(GOT_ERR_CANCELLED);
1608 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1609 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1612 if (parent_path[0]) {
1613 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1614 parent_path, de->d_name) == -1)
1615 return got_error_from_errno();
1617 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1619 return got_error_from_errno();
1622 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1628 static const struct got_error *
1629 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1631 struct diff_dir_cb_arg *a = arg;
1632 struct got_object_id id;
1633 unsigned char status;
1635 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1636 return got_error(GOT_ERR_CANCELLED);
1638 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1641 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1642 if (got_fileindex_entry_has_file_on_disk(ie))
1643 status = GOT_STATUS_MISSING;
1645 status = GOT_STATUS_DELETE;
1646 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1649 static const struct got_error *
1650 status_new(void *arg, struct dirent *de, const char *parent_path)
1652 const struct got_error *err = NULL;
1653 struct diff_dir_cb_arg *a = arg;
1656 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1657 return got_error(GOT_ERR_CANCELLED);
1659 if (de->d_type == DT_DIR)
1662 /* XXX ignore symlinks for now */
1663 if (de->d_type == DT_LNK)
1666 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1669 if (parent_path[0]) {
1670 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1671 return got_error_from_errno();
1676 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1683 const struct got_error *
1684 got_worktree_status(struct got_worktree *worktree, const char *path,
1685 struct got_repository *repo, got_worktree_status_cb status_cb,
1686 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1688 const struct got_error *err = NULL;
1689 DIR *workdir = NULL;
1690 char *fileindex_path = NULL;
1691 struct got_fileindex *fileindex = NULL;
1693 struct got_fileindex_diff_dir_cb fdiff_cb;
1694 struct diff_dir_cb_arg arg;
1695 char *ondisk_path = NULL;
1697 fileindex = got_fileindex_alloc();
1698 if (fileindex == NULL) {
1699 err = got_error_from_errno();
1703 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1704 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1705 err = got_error_from_errno();
1706 fileindex_path = NULL;
1710 index = fopen(fileindex_path, "rb");
1711 if (index == NULL) {
1712 if (errno != ENOENT) {
1713 err = got_error_from_errno();
1717 err = got_fileindex_read(fileindex, index);
1723 if (asprintf(&ondisk_path, "%s%s%s",
1724 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1725 err = got_error_from_errno();
1728 workdir = opendir(ondisk_path);
1729 if (workdir == NULL) {
1730 if (errno == ENOTDIR || errno == ENOENT) {
1731 struct got_fileindex_entry *ie;
1732 ie = got_fileindex_entry_get(fileindex, path);
1734 err = got_error(GOT_ERR_BAD_PATH);
1737 err = report_file_status(ie, ondisk_path,
1738 status_cb, status_arg, repo);
1741 err = got_error_from_errno();
1745 fdiff_cb.diff_old_new = status_old_new;
1746 fdiff_cb.diff_old = status_old;
1747 fdiff_cb.diff_new = status_new;
1748 arg.fileindex = fileindex;
1749 arg.worktree = worktree;
1750 arg.status_path = path;
1751 arg.status_path_len = strlen(path);
1753 arg.status_cb = status_cb;
1754 arg.status_arg = status_arg;
1755 arg.cancel_cb = cancel_cb;
1756 arg.cancel_arg = cancel_arg;
1757 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1758 path, repo, &fdiff_cb, &arg);
1763 free(fileindex_path);
1764 got_fileindex_free(fileindex);
1768 const struct got_error *
1769 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1772 const struct got_error *err = NULL;
1773 char *resolved, *path = NULL;
1778 resolved = realpath(arg, NULL);
1779 if (resolved == NULL)
1780 return got_error_from_errno();
1782 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1783 strlen(got_worktree_get_root_path(worktree)))) {
1784 err = got_error(GOT_ERR_BAD_PATH);
1788 path = strdup(resolved +
1789 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1791 err = got_error_from_errno();
1795 /* XXX status walk can't deal with trailing slash! */
1797 while (path[len - 1] == '/') {
1798 path[len - 1] = '\0';
1810 const struct got_error *
1811 got_worktree_schedule_add(struct got_worktree *worktree,
1812 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1813 struct got_repository *repo)
1815 struct got_fileindex *fileindex = NULL;
1816 struct got_fileindex_entry *ie = NULL;
1817 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1818 FILE *index = NULL, *new_index = NULL;
1819 const struct got_error *err = NULL, *unlockerr = NULL;
1822 err = lock_worktree(worktree, LOCK_EX);
1826 err = got_path_skip_common_ancestor(&relpath,
1827 got_worktree_get_root_path(worktree), ondisk_path);
1831 fileindex = got_fileindex_alloc();
1832 if (fileindex == NULL) {
1833 err = got_error_from_errno();
1837 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1838 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1839 err = got_error_from_errno();
1840 fileindex_path = NULL;
1844 index = fopen(fileindex_path, "rb");
1845 if (index == NULL) {
1846 err = got_error_from_errno();
1850 err = got_fileindex_read(fileindex, index);
1854 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1855 err = got_error_set_errno(EEXIST);
1859 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1863 err = got_fileindex_entry_add(fileindex, ie);
1866 ie_added = 1; /* now owned by fileindex; don't free separately */
1868 err = got_opentemp_named(&new_fileindex_path, &new_index,
1873 err = got_fileindex_write(fileindex, new_index);
1877 if (rename(new_fileindex_path, fileindex_path) != 0) {
1878 err = got_error_from_errno();
1882 free(new_fileindex_path);
1883 new_fileindex_path = NULL;
1885 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1888 if (fclose(index) != 0 && err == NULL)
1889 err = got_error_from_errno();
1891 if (new_fileindex_path) {
1892 if (unlink(new_fileindex_path) != 0 && err == NULL)
1893 err = got_error_from_errno();
1894 free(new_fileindex_path);
1896 if (ie && !ie_added)
1897 got_fileindex_entry_free(ie);
1899 got_fileindex_free(fileindex);
1900 unlockerr = lock_worktree(worktree, LOCK_SH);
1901 if (unlockerr && err == NULL)
1907 const struct got_error *
1908 got_worktree_schedule_delete(struct got_worktree *worktree,
1909 const char *ondisk_path, int delete_local_mods,
1910 got_worktree_status_cb status_cb, void *status_arg,
1911 struct got_repository *repo)
1913 struct got_fileindex *fileindex = NULL;
1914 struct got_fileindex_entry *ie = NULL;
1915 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1916 FILE *index = NULL, *new_index = NULL;
1917 const struct got_error *err = NULL, *unlockerr = NULL;
1918 unsigned char status;
1921 err = lock_worktree(worktree, LOCK_EX);
1925 err = got_path_skip_common_ancestor(&relpath,
1926 got_worktree_get_root_path(worktree), ondisk_path);
1930 fileindex = got_fileindex_alloc();
1931 if (fileindex == NULL) {
1932 err = got_error_from_errno();
1936 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1937 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1938 err = got_error_from_errno();
1939 fileindex_path = NULL;
1943 index = fopen(fileindex_path, "rb");
1944 if (index == NULL) {
1945 err = got_error_from_errno();
1949 err = got_fileindex_read(fileindex, index);
1953 ie = got_fileindex_entry_get(fileindex, relpath);
1955 err = got_error(GOT_ERR_BAD_PATH);
1959 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1963 if (status != GOT_STATUS_NO_CHANGE) {
1964 if (status == GOT_STATUS_DELETE) {
1965 err = got_error_set_errno(ENOENT);
1968 if (status != GOT_STATUS_MODIFY) {
1969 err = got_error(GOT_ERR_FILE_STATUS);
1972 if (!delete_local_mods) {
1973 err = got_error(GOT_ERR_FILE_MODIFIED);
1978 if (unlink(ondisk_path) != 0) {
1979 err = got_error_from_errno();
1983 got_fileindex_entry_mark_deleted_from_disk(ie);
1985 err = got_opentemp_named(&new_fileindex_path, &new_index,
1990 err = got_fileindex_write(fileindex, new_index);
1994 if (rename(new_fileindex_path, fileindex_path) != 0) {
1995 err = got_error_from_errno();
1999 free(new_fileindex_path);
2000 new_fileindex_path = NULL;
2002 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2006 if (fclose(index) != 0 && err == NULL)
2007 err = got_error_from_errno();
2009 if (new_fileindex_path) {
2010 if (unlink(new_fileindex_path) != 0 && err == NULL)
2011 err = got_error_from_errno();
2012 free(new_fileindex_path);
2015 got_fileindex_free(fileindex);
2016 unlockerr = lock_worktree(worktree, LOCK_SH);
2017 if (unlockerr && err == NULL)
2022 const struct got_error *
2023 got_worktree_revert(struct got_worktree *worktree,
2024 const char *ondisk_path,
2025 got_worktree_checkout_cb progress_cb, void *progress_arg,
2026 struct got_repository *repo)
2028 struct got_fileindex *fileindex = NULL;
2029 struct got_fileindex_entry *ie = NULL;
2030 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2031 char *tree_path = NULL, *parent_path, *te_name;
2032 FILE *index = NULL, *new_index = NULL;
2033 const struct got_error *err = NULL, *unlockerr = NULL;
2034 struct got_tree_object *tree = NULL;
2035 struct got_object_id id, *tree_id = NULL;
2036 const struct got_tree_entry *te;
2037 struct got_blob_object *blob = NULL;
2038 unsigned char status;
2041 err = lock_worktree(worktree, LOCK_EX);
2045 err = got_path_skip_common_ancestor(&relpath,
2046 got_worktree_get_root_path(worktree), ondisk_path);
2050 fileindex = got_fileindex_alloc();
2051 if (fileindex == NULL) {
2052 err = got_error_from_errno();
2056 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2057 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2058 err = got_error_from_errno();
2059 fileindex_path = NULL;
2063 index = fopen(fileindex_path, "rb");
2064 if (index == NULL) {
2065 err = got_error_from_errno();
2069 err = got_fileindex_read(fileindex, index);
2073 ie = got_fileindex_entry_get(fileindex, relpath);
2075 err = got_error(GOT_ERR_BAD_PATH);
2079 /* Construct in-repository path of tree which contains this blob. */
2080 err = got_path_dirname(&parent_path, ie->path);
2082 if (err->code != GOT_ERR_BAD_PATH)
2086 if (got_path_is_root_dir(worktree->path_prefix)) {
2087 tree_path = strdup(parent_path);
2088 if (tree_path == NULL) {
2089 err = got_error_from_errno();
2093 if (got_path_is_root_dir(parent_path)) {
2094 tree_path = strdup(worktree->path_prefix);
2095 if (tree_path == NULL) {
2096 err = got_error_from_errno();
2100 if (asprintf(&tree_path, "%s/%s",
2101 worktree->path_prefix, parent_path) == -1) {
2102 err = got_error_from_errno();
2108 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2113 err = got_object_open_as_tree(&tree, repo, tree_id);
2117 te_name = basename(ie->path);
2118 if (te_name == NULL) {
2119 err = got_error_from_errno();
2123 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2127 te = got_object_tree_find_entry(tree, te_name);
2128 if (te == NULL && status != GOT_STATUS_ADD) {
2129 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2134 case GOT_STATUS_ADD:
2135 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2136 got_fileindex_entry_remove(fileindex, ie);
2138 case GOT_STATUS_DELETE:
2139 case GOT_STATUS_MODIFY:
2140 case GOT_STATUS_CONFLICT:
2141 case GOT_STATUS_MISSING:
2142 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2143 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2146 err = install_blob(worktree, ondisk_path, ie->path,
2147 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2151 if (status == GOT_STATUS_DELETE) {
2152 err = update_blob_fileindex_entry(worktree,
2153 fileindex, ie, ondisk_path, ie->path, blob, 1);
2162 err = got_opentemp_named(&new_fileindex_path, &new_index,
2167 err = got_fileindex_write(fileindex, new_index);
2171 if (rename(new_fileindex_path, fileindex_path) != 0) {
2172 err = got_error_from_errno();
2176 free(new_fileindex_path);
2177 new_fileindex_path = NULL;
2182 got_object_blob_close(blob);
2184 got_object_tree_close(tree);
2187 if (fclose(index) != 0 && err == NULL)
2188 err = got_error_from_errno();
2190 if (new_fileindex_path) {
2191 if (unlink(new_fileindex_path) != 0 && err == NULL)
2192 err = got_error_from_errno();
2193 free(new_fileindex_path);
2196 got_fileindex_free(fileindex);
2197 unlockerr = lock_worktree(worktree, LOCK_SH);
2198 if (unlockerr && err == NULL)
2205 unsigned char status;
2206 struct got_object_id *id;
2207 struct got_object_id *tree_id;
2211 free_commitable(struct commitable *ct)
2219 struct collect_commitables_arg {
2220 struct got_pathlist_head *paths;
2221 struct got_repository *repo;
2222 struct got_worktree *worktree;
2225 static const struct got_error *
2226 collect_commitables(void *arg, unsigned char status, const char *path,
2227 struct got_object_id *id)
2229 struct collect_commitables_arg *a = arg;
2230 const struct got_error *err = NULL;
2231 struct commitable *ct = NULL;
2232 struct got_pathlist_entry *new = NULL;
2233 char *parent_path = NULL;
2235 if (status == GOT_STATUS_CONFLICT)
2236 return got_error(GOT_ERR_COMMIT_CONFLICT);
2238 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2239 status != GOT_STATUS_DELETE)
2242 if (strchr(path, '/') == NULL) {
2243 parent_path = strdup("/");
2244 if (parent_path == NULL)
2245 return got_error_from_errno();
2247 err = got_path_dirname(&parent_path, path);
2252 ct = malloc(sizeof(*ct));
2254 err = got_error_from_errno();
2258 ct->status = status;
2260 err = got_object_id_by_path(&ct->tree_id, a->repo,
2261 a->worktree->base_commit_id, parent_path);
2264 ct->path = strdup(path);
2265 if (ct->path == NULL) {
2266 err = got_error_from_errno();
2269 err = got_pathlist_insert(&new, a->paths, ct->path, ct);
2271 if (ct && (err || new == NULL))
2272 free_commitable(ct);
2277 struct write_tree_arg {
2278 struct got_pathlist_head *commitable_paths;
2279 struct got_object_idset *affected_trees;
2280 struct got_repository *repo;
2283 static const struct got_error *
2284 write_tree(struct got_object_id *base_tree_id, void *data, void *arg)
2286 const struct got_error *err = NULL;
2287 struct write_tree_arg *a = arg;
2288 const struct got_tree_entries *base_entries = NULL;
2289 struct got_pathlist_head new_entries;
2290 struct got_tree_entries new_tree_entries;
2291 struct got_tree_object *base_tree = NULL;
2292 struct got_tree_entry *te;
2293 struct got_pathlist_entry *pe;
2294 struct got_object_id *new_tree_id = NULL;
2296 TAILQ_INIT(&new_entries);
2298 err = got_object_open_as_tree(&base_tree, a->repo, base_tree_id);
2302 base_entries = got_object_tree_get_entries(base_tree);
2304 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2305 TAILQ_FOREACH(pe, a->commitable_paths, entry) {
2306 struct commitable *ct = NULL;
2307 struct got_tree_entry *new_te = NULL;
2308 struct got_pathlist_entry *new_pe = NULL;
2309 char *ct_name = NULL;
2313 if (got_object_id_cmp(ct->tree_id, te->id) != 0)
2314 continue; /* not part of this tree */
2316 ct_name = basename(pe->path);
2317 if (ct_name == NULL) {
2318 err = got_error_from_errno();
2321 /* Commitable and tree entry must correspond. */
2322 if (strcmp(te->name, ct_name) != 0)
2325 if (ct->status == GOT_STATUS_DELETE) {
2326 /* Deleted entries disappear. */
2330 /* Modified entries get updated mode and ID. */
2331 if (ct->status == GOT_STATUS_MODIFY) {
2332 err = got_object_tree_entry_dup(&new_te, te);
2335 new_te->mode = GOT_DEFAULT_FILE_MODE; /* XXX */
2337 } else if (ct->status == GOT_STATUS_ADD) {
2338 /* Added entries get... well, added. */
2339 new_te = calloc(1, sizeof(*new_te));
2340 if (new_te == NULL) {
2341 err = got_error_from_errno();
2344 new_te->mode = GOT_DEFAULT_FILE_MODE; /* XXX */
2345 new_te->name = strdup(ct_name);
2346 if (new_te->name == NULL) {
2347 err = got_error_from_errno();
2351 new_te->id = got_object_id_dup(ct->id);
2352 if (new_te->id == NULL) {
2353 err = got_error_from_errno();
2357 err = got_pathlist_insert(&new_pe, &new_entries,
2361 if (new_pe == NULL) {
2362 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
2368 new_tree_entries.nentries = 0;
2369 SIMPLEQ_INIT(&new_tree_entries.head);
2370 TAILQ_FOREACH(pe, &new_entries, entry) {
2371 struct got_tree_entry *te = pe->data;
2372 new_tree_entries.nentries++;
2373 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2376 err = got_object_tree_create(&new_tree_id, &new_tree_entries, a->repo);
2380 got_object_tree_close(base_tree);
2381 got_pathlist_free(&new_entries);
2382 got_object_tree_entries_close(&new_tree_entries);
2386 const struct got_error *
2387 got_worktree_commit(struct got_object_id **new_commit_id,
2388 struct got_worktree *worktree, const char *ondisk_path,
2389 const char *logmsg, struct got_repository *repo)
2391 const struct got_error *err = NULL, *unlockerr = NULL;
2392 struct collect_commitables_arg cc_arg;
2393 struct write_tree_arg wt_arg;
2394 struct got_pathlist_head paths;
2395 struct got_pathlist_entry *pe;
2396 char *relpath = NULL;
2397 struct got_commit_object *base_commit = NULL;
2398 struct got_tree_object *base_tree = NULL;
2399 struct got_object_idset *tree_ids = NULL;
2401 *new_commit_id = NULL;
2406 err = got_path_skip_common_ancestor(&relpath,
2407 worktree->root_path, ondisk_path);
2412 tree_ids = got_object_idset_alloc();
2413 if (tree_ids == NULL)
2414 return got_error_from_errno();
2416 err = lock_worktree(worktree, LOCK_EX);
2420 cc_arg.paths = &paths;
2421 cc_arg.worktree = worktree;
2423 err = got_worktree_status(worktree, relpath ? relpath : "",
2424 repo, collect_commitables, &cc_arg, NULL, NULL);
2428 /* TODO: collect commit message if not specified */
2430 /* Collect IDs of affected leaf trees. */
2431 TAILQ_FOREACH(pe, &paths, entry) {
2432 struct commitable *ct = pe->data;
2434 if (got_object_idset_contains(tree_ids, ct->tree_id))
2437 err = got_object_idset_add(tree_ids, ct->tree_id, NULL);
2442 /* Create blobs from added and modified files and record their IDs. */
2443 TAILQ_FOREACH(pe, &paths, entry) {
2444 struct commitable *ct = pe->data;
2447 if (ct->status != GOT_STATUS_ADD &&
2448 ct->status != GOT_STATUS_MODIFY)
2451 if (asprintf(&ondisk_path, "%s/%s",
2452 worktree->root_path, pe->path) == -1) {
2453 err = got_error_from_errno();
2456 err = got_object_blob_create(&ct->id, ondisk_path, repo);
2462 /* Write new leaf tree objects. */
2463 wt_arg.commitable_paths = &paths;
2464 wt_arg.affected_trees = got_object_idset_alloc();
2465 if (wt_arg.affected_trees == NULL) {
2466 err = got_error_from_errno();
2470 err = got_object_idset_for_each(tree_ids, write_tree, &wt_arg);
2474 err = got_object_open_as_commit(&base_commit, repo,
2475 worktree->base_commit_id);
2478 err = got_object_open_as_tree(&base_tree, repo,
2479 base_commit->tree_id);
2483 unlockerr = lock_worktree(worktree, LOCK_SH);
2484 if (unlockerr && err == NULL)
2486 TAILQ_FOREACH(pe, &paths, entry) {
2487 struct commitable *ct = pe->data;
2488 free_commitable(ct);
2490 got_object_idset_free(tree_ids);
2491 got_pathlist_free(&paths);
2493 got_object_tree_close(base_tree);