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_create.h"
52 #include "got_lib_diff.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 static const struct got_error *
59 create_meta_file(const char *path_got, const char *name, const char *content)
61 const struct got_error *err = NULL;
65 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
66 err = got_error_from_errno();
71 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
72 GOT_DEFAULT_FILE_MODE);
74 err = got_error_from_errno();
79 int len = dprintf(fd, "%s\n", content);
80 if (len != strlen(content) + 1) {
81 err = got_error_from_errno();
87 if (fd != -1 && close(fd) == -1 && err == NULL)
88 err = got_error_from_errno();
93 static const struct got_error *
94 update_meta_file(const char *path_got, const char *name, const char *content)
96 const struct got_error *err = NULL;
101 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
102 err = got_error_from_errno();
107 err = got_opentemp_named(&tmppath, &tmpfile, path);
112 int len = fprintf(tmpfile, "%s\n", content);
113 if (len != strlen(content) + 1) {
114 err = got_error_from_errno();
119 if (rename(tmppath, path) != 0) {
120 err = got_error_from_errno();
127 if (fclose(tmpfile) != 0 && err == NULL)
128 err = got_error_from_errno();
132 static const struct got_error *
133 read_meta_file(char **content, const char *path_got, const char *name)
135 const struct got_error *err = NULL;
143 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
144 err = got_error_from_errno();
149 fd = open(path, O_RDONLY | O_NOFOLLOW);
152 err = got_error(GOT_ERR_WORKTREE_META);
154 err = got_error_from_errno();
157 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
158 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
159 : got_error_from_errno());
163 if (lstat(path, &sb) != 0) {
164 err = got_error_from_errno();
167 *content = calloc(1, sb.st_size);
168 if (*content == NULL) {
169 err = got_error_from_errno();
173 n = read(fd, *content, sb.st_size);
174 if (n != sb.st_size) {
175 err = (n == -1 ? got_error_from_errno() :
176 got_error(GOT_ERR_WORKTREE_META));
179 if ((*content)[sb.st_size - 1] != '\n') {
180 err = got_error(GOT_ERR_WORKTREE_META);
183 (*content)[sb.st_size - 1] = '\0';
186 if (fd != -1 && close(fd) == -1 && err == NULL)
187 err = got_error_from_errno();
196 const struct got_error *
197 got_worktree_init(const char *path, struct got_reference *head_ref,
198 const char *prefix, struct got_repository *repo)
200 const struct got_error *err = NULL;
201 struct got_object_id *commit_id = NULL;
203 uint32_t uuid_status;
205 char *path_got = NULL;
207 char *formatstr = NULL;
208 char *absprefix = NULL;
209 char *basestr = NULL;
210 char *uuidstr = NULL;
212 if (strcmp(path, got_repo_get_path(repo)) == 0) {
213 err = got_error(GOT_ERR_WORKTREE_REPO);
217 err = got_ref_resolve(&commit_id, repo, head_ref);
220 err = got_object_get_type(&obj_type, repo, commit_id);
223 if (obj_type != GOT_OBJ_TYPE_COMMIT)
224 return got_error(GOT_ERR_OBJ_TYPE);
226 if (!got_path_is_absolute(prefix)) {
227 if (asprintf(&absprefix, "/%s", prefix) == -1)
228 return got_error_from_errno();
231 /* Create top-level directory (may already exist). */
232 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
233 err = got_error_from_errno();
237 /* Create .got directory (may already exist). */
238 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
239 err = got_error_from_errno();
242 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
243 err = got_error_from_errno();
247 /* Create an empty lock file. */
248 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
252 /* Create an empty file index. */
253 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
257 /* Write the HEAD reference. */
258 refstr = got_ref_to_str(head_ref);
259 if (refstr == NULL) {
260 err = got_error_from_errno();
263 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
267 /* Record our base commit. */
268 err = got_object_id_str(&basestr, commit_id);
271 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
275 /* Store path to repository. */
276 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
277 got_repo_get_path(repo));
281 /* Store in-repository path prefix. */
282 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
283 absprefix ? absprefix : prefix);
288 uuid_create(&uuid, &uuid_status);
289 if (uuid_status != uuid_s_ok) {
290 err = got_error_uuid(uuid_status);
293 uuid_to_string(&uuid, &uuidstr, &uuid_status);
294 if (uuid_status != uuid_s_ok) {
295 err = got_error_uuid(uuid_status);
298 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
302 /* Stamp work tree with format file. */
303 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
304 err = got_error_from_errno();
307 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 char *head_ref_str = NULL;
332 int version, fd = -1;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno();
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno();
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno());
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 err = got_error(GOT_ERR_WORKTREE_META);
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno();
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = strdup(path);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno();
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status);
408 err = got_repo_open(&repo, (*worktree)->repo_path);
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
417 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
421 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
424 got_repo_close(repo);
428 free(base_commit_id_str);
434 if (*worktree != NULL)
435 got_worktree_close(*worktree);
438 (*worktree)->lockfd = fd;
443 const struct got_error *
444 got_worktree_open(struct got_worktree **worktree, const char *path)
446 const struct got_error *err = NULL;
449 err = open_worktree(worktree, path);
450 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
454 path = dirname(path);
456 return got_error_from_errno();
457 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
459 return got_error(GOT_ERR_NOT_WORKTREE);
462 const struct got_error *
463 got_worktree_close(struct got_worktree *worktree)
465 const struct got_error *err = NULL;
466 free(worktree->root_path);
467 free(worktree->repo_path);
468 free(worktree->path_prefix);
469 free(worktree->base_commit_id);
470 if (worktree->head_ref)
471 got_ref_close(worktree->head_ref);
472 if (worktree->lockfd != -1)
473 if (close(worktree->lockfd) != 0)
474 err = got_error_from_errno();
480 got_worktree_get_root_path(struct got_worktree *worktree)
482 return worktree->root_path;
486 got_worktree_get_repo_path(struct got_worktree *worktree)
488 return worktree->repo_path;
492 got_worktree_get_path_prefix(struct got_worktree *worktree)
494 return worktree->path_prefix;
497 const struct got_error *
498 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
499 const char *path_prefix)
501 char *absprefix = NULL;
503 if (!got_path_is_absolute(path_prefix)) {
504 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
505 return got_error_from_errno();
507 *match = (strcmp(absprefix ? absprefix : path_prefix,
508 worktree->path_prefix) == 0);
514 got_worktree_get_head_ref_name(struct got_worktree *worktree)
516 return got_ref_to_str(worktree->head_ref);
519 struct got_reference *
520 got_worktree_get_head_ref(struct got_worktree *worktree)
522 return got_ref_dup(worktree->head_ref);
525 struct got_object_id *
526 got_worktree_get_base_commit_id(struct got_worktree *worktree)
528 return worktree->base_commit_id;
531 const struct got_error *
532 got_worktree_set_base_commit_id(struct got_worktree *worktree,
533 struct got_repository *repo, struct got_object_id *commit_id)
535 const struct got_error *err;
536 struct got_object *obj = NULL;
538 char *path_got = NULL;
540 if (asprintf(&path_got, "%s/%s", worktree->root_path,
541 GOT_WORKTREE_GOT_DIR) == -1) {
542 err = got_error_from_errno();
547 err = got_object_open(&obj, repo, commit_id);
551 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
552 err = got_error(GOT_ERR_OBJ_TYPE);
556 /* Record our base commit. */
557 err = got_object_id_str(&id_str, commit_id);
560 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
564 free(worktree->base_commit_id);
565 worktree->base_commit_id = got_object_id_dup(commit_id);
566 if (worktree->base_commit_id == NULL) {
567 err = got_error_from_errno();
572 got_object_close(obj);
578 static const struct got_error *
579 lock_worktree(struct got_worktree *worktree, int operation)
581 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
582 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
583 : got_error_from_errno());
587 static const struct got_error *
588 add_dir_on_disk(struct got_worktree *worktree, const char *path)
590 const struct got_error *err = NULL;
593 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
594 return got_error_from_errno();
596 err = got_path_mkdir(abspath);
597 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
600 if (lstat(abspath, &sb) == -1) {
601 err = got_error_from_errno();
602 } else if (!S_ISDIR(sb.st_mode)) {
603 /* TODO directory is obstructed; do something */
604 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
611 static const struct got_error *
612 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
614 const struct got_error *err = NULL;
617 size_t flen1 = 0, flen2 = 0;
622 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
623 if (flen1 == 0 && ferror(f1)) {
624 err = got_error_from_errno();
627 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
628 if (flen2 == 0 && ferror(f2)) {
629 err = got_error_from_errno();
636 } else if (flen2 == 0) {
640 } else if (flen1 == flen2) {
641 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
654 static const struct got_error *
655 check_files_equal(int *same, const char *f1_path, const char *f2_path)
657 const struct got_error *err = NULL;
660 FILE *f1 = NULL, *f2 = NULL;
664 if (lstat(f1_path, &sb) != 0) {
665 err = got_error_from_errno();
670 if (lstat(f2_path, &sb) != 0) {
671 err = got_error_from_errno();
676 if (size1 != size2) {
681 f1 = fopen(f1_path, "r");
683 return got_error_from_errno();
685 f2 = fopen(f2_path, "r");
687 err = got_error_from_errno();
691 err = check_file_contents_equal(same, f1, f2);
693 if (f1 && fclose(f1) != 0 && err == NULL)
694 err = got_error_from_errno();
695 if (f2 && fclose(f2) != 0 && err == NULL)
696 err = got_error_from_errno();
702 * Perform a 3-way merge where the file's version in the file index (blob2)
703 * acts as the common ancestor, the incoming blob (blob1) acts as the first
704 * derived version, and the file on disk acts as the second derived version.
706 static const struct got_error *
707 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
708 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
709 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
710 struct got_repository *repo,
711 got_worktree_checkout_cb progress_cb, void *progress_arg)
713 const struct got_error *err = NULL;
715 struct got_blob_object *blob2 = NULL;
716 FILE *f1 = NULL, *f2 = NULL;
717 char *blob1_path = NULL, *blob2_path = NULL;
718 char *merged_path = NULL, *base_path = NULL;
721 int overlapcnt = 0, update_timestamps = 0;
724 parent = dirname(ondisk_path);
726 return got_error_from_errno();
728 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
729 return got_error_from_errno();
731 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
736 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
737 err = got_error_from_errno();
742 err = got_opentemp_named(&blob1_path, &f1, base_path);
745 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
750 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
751 err = got_error_from_errno();
756 err = got_opentemp_named(&blob2_path, &f2, base_path);
759 if (got_fileindex_entry_has_blob(ie)) {
760 struct got_object_id id2;
761 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
762 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
765 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
770 * If the file has no blob, this is an "add vs add" conflict,
771 * and we simply use an empty ancestor file to make both files
772 * appear in the merged result in their entirety.
776 err = got_object_id_str(&id_str, worktree->base_commit_id);
779 if (asprintf(&label1, "commit %s", id_str) == -1) {
780 err = got_error_from_errno();
784 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
785 blob2_path, ondisk_path, label1, path);
789 (*progress_cb)(progress_arg,
790 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
793 if (fsync(merged_fd) != 0) {
794 err = got_error_from_errno();
798 /* Check if a clean merge has subsumed all local changes. */
799 if (overlapcnt == 0) {
800 err = check_files_equal(&update_timestamps, blob1_path,
806 if (chmod(merged_path, st_mode) != 0) {
807 err = got_error_from_errno();
811 if (rename(merged_path, ondisk_path) != 0) {
812 err = got_error_from_errno();
818 * Do not update timestamps of already modified files. Otherwise,
819 * a future status walk would treat them as unmodified files again.
821 err = got_fileindex_entry_update(ie, ondisk_path,
822 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
824 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
825 err = got_error_from_errno();
826 if (f1 && fclose(f1) != 0 && err == NULL)
827 err = got_error_from_errno();
828 if (f2 && fclose(f2) != 0 && err == NULL)
829 err = got_error_from_errno();
831 got_object_blob_close(blob2);
847 static const struct got_error *
848 update_blob_fileindex_entry(struct got_worktree *worktree,
849 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
850 const char *ondisk_path, const char *path, struct got_blob_object *blob,
851 int update_timestamps)
853 const struct got_error *err = NULL;
856 ie = got_fileindex_entry_get(fileindex, path);
858 err = got_fileindex_entry_update(ie, ondisk_path,
859 blob->id.sha1, worktree->base_commit_id->sha1,
862 struct got_fileindex_entry *new_ie;
863 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
864 path, blob->id.sha1, worktree->base_commit_id->sha1);
866 err = got_fileindex_entry_add(fileindex, new_ie);
871 static const struct got_error *
872 install_blob(struct got_worktree *worktree, const char *ondisk_path,
873 const char *path, uint16_t te_mode, uint16_t st_mode,
874 struct got_blob_object *blob, int restoring_missing_file,
875 int reverting_versioned_file, struct got_repository *repo,
876 got_worktree_checkout_cb progress_cb, void *progress_arg)
878 const struct got_error *err = NULL;
882 char *tmppath = NULL;
884 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
885 GOT_DEFAULT_FILE_MODE);
887 if (errno == ENOENT) {
888 char *parent = dirname(path);
890 return got_error_from_errno();
891 err = add_dir_on_disk(worktree, parent);
894 fd = open(ondisk_path,
895 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
896 GOT_DEFAULT_FILE_MODE);
898 return got_error_from_errno();
899 } else if (errno == EEXIST) {
900 if (!S_ISREG(st_mode)) {
901 /* TODO file is obstructed; do something */
902 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
905 err = got_opentemp_named_fd(&tmppath, &fd,
912 return got_error_from_errno();
915 if (restoring_missing_file)
916 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
917 else if (reverting_versioned_file)
918 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
920 (*progress_cb)(progress_arg,
921 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
923 hdrlen = got_object_blob_get_hdrlen(blob);
925 const uint8_t *buf = got_object_blob_get_read_buf(blob);
926 err = got_object_blob_read_block(&len, blob);
930 /* Skip blob object header first time around. */
931 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
933 err = got_error_from_errno();
935 } else if (outlen != len - hdrlen) {
936 err = got_error(GOT_ERR_IO);
943 if (fsync(fd) != 0) {
944 err = got_error_from_errno();
949 if (rename(tmppath, ondisk_path) != 0) {
950 err = got_error_from_errno();
956 if (te_mode & S_IXUSR) {
957 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
958 err = got_error_from_errno();
962 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
963 err = got_error_from_errno();
969 if (fd != -1 && close(fd) != 0 && err == NULL)
970 err = got_error_from_errno();
975 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
976 static const struct got_error *
977 get_modified_file_content_status(unsigned char *status, FILE *f)
979 const struct got_error *err = NULL;
980 const char *markers[3] = {
981 GOT_DIFF_CONFLICT_MARKER_BEGIN,
982 GOT_DIFF_CONFLICT_MARKER_SEP,
983 GOT_DIFF_CONFLICT_MARKER_END
988 const char delim[3] = {'\0', '\0', '\0'};
990 while (*status == GOT_STATUS_MODIFY) {
991 line = fparseln(f, &len, NULL, delim, 0);
995 err = got_ferror(f, GOT_ERR_IO);
999 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1000 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
1001 *status = GOT_STATUS_CONFLICT;
1010 static const struct got_error *
1011 get_file_status(unsigned char *status, struct stat *sb,
1012 struct got_fileindex_entry *ie, const char *abspath,
1013 struct got_repository *repo)
1015 const struct got_error *err = NULL;
1016 struct got_object_id id;
1020 struct got_blob_object *blob = NULL;
1023 *status = GOT_STATUS_NO_CHANGE;
1025 if (lstat(abspath, sb) == -1) {
1026 if (errno == ENOENT) {
1028 if (got_fileindex_entry_has_file_on_disk(ie))
1029 *status = GOT_STATUS_MISSING;
1031 *status = GOT_STATUS_DELETE;
1033 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1034 & (S_IRWXU | S_IRWXG | S_IRWXO));
1036 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1039 return got_error_from_errno();
1042 if (!S_ISREG(sb->st_mode)) {
1043 *status = GOT_STATUS_OBSTRUCTED;
1050 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1051 *status = GOT_STATUS_DELETE;
1053 } else if (!got_fileindex_entry_has_blob(ie)) {
1054 *status = GOT_STATUS_ADD;
1058 if (ie->ctime_sec == sb->st_ctime &&
1059 ie->ctime_nsec == sb->st_ctimensec &&
1060 ie->mtime_sec == sb->st_mtime &&
1061 ie->mtime_sec == sb->st_mtime &&
1062 ie->mtime_nsec == sb->st_mtimensec &&
1063 ie->size == (sb->st_size & 0xffffffff))
1066 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1067 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1071 f = fopen(abspath, "r");
1073 err = got_error_from_errno();
1076 hdrlen = got_object_blob_get_hdrlen(blob);
1078 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1079 err = got_object_blob_read_block(&blen, blob);
1082 /* Skip length of blob object header first time around. */
1083 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1084 if (flen == 0 && ferror(f)) {
1085 err = got_error_from_errno();
1090 *status = GOT_STATUS_MODIFY;
1092 } else if (flen == 0) {
1094 *status = GOT_STATUS_MODIFY;
1096 } else if (blen - hdrlen == flen) {
1097 /* Skip blob object header first time around. */
1098 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1099 *status = GOT_STATUS_MODIFY;
1103 *status = GOT_STATUS_MODIFY;
1109 if (*status == GOT_STATUS_MODIFY) {
1111 err = get_modified_file_content_status(status, f);
1115 got_object_blob_close(blob);
1121 static const struct got_error *
1122 update_blob(struct got_worktree *worktree,
1123 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1124 struct got_tree_entry *te, const char *path,
1125 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1128 const struct got_error *err = NULL;
1129 struct got_blob_object *blob = NULL;
1131 unsigned char status = GOT_STATUS_NO_CHANGE;
1134 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1135 return got_error_from_errno();
1137 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1141 if (status == GOT_STATUS_OBSTRUCTED) {
1142 (*progress_cb)(progress_arg, status, path);
1146 if (ie && status != GOT_STATUS_MISSING) {
1147 if (got_fileindex_entry_has_commit(ie) &&
1148 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1149 SHA1_DIGEST_LENGTH) == 0) {
1150 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1154 if (got_fileindex_entry_has_blob(ie) &&
1155 memcmp(ie->blob_sha1, te->id->sha1,
1156 SHA1_DIGEST_LENGTH) == 0)
1160 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1164 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1165 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1166 te->mode, sb.st_mode, blob, repo, progress_cb,
1168 else if (status == GOT_STATUS_DELETE) {
1169 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1170 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1171 ondisk_path, path, blob, 0);
1175 err = install_blob(worktree, ondisk_path, path, te->mode,
1176 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1177 repo, progress_cb, progress_arg);
1180 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1181 ondisk_path, path, blob, 1);
1185 got_object_blob_close(blob);
1191 static const struct got_error *
1192 remove_ondisk_file(const char *root_path, const char *path)
1194 const struct got_error *err = NULL;
1195 char *ondisk_path = NULL;
1197 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1198 return got_error_from_errno();
1200 if (unlink(ondisk_path) == -1) {
1201 if (errno != ENOENT)
1202 err = got_error_from_errno();
1204 char *parent = dirname(ondisk_path);
1205 while (parent && strcmp(parent, root_path) != 0) {
1206 if (rmdir(parent) == -1) {
1207 if (errno != ENOTEMPTY)
1208 err = got_error_from_errno();
1211 parent = dirname(parent);
1218 static const struct got_error *
1219 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1220 struct got_fileindex_entry *ie, const char *parent_path,
1221 struct got_repository *repo,
1222 got_worktree_checkout_cb progress_cb, void *progress_arg)
1224 const struct got_error *err = NULL;
1225 unsigned char status;
1229 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1231 return got_error_from_errno();
1233 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1237 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1238 status == GOT_STATUS_ADD) {
1239 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1241 * Preserve the working file and change the deleted blob's
1242 * entry into a schedule-add entry.
1244 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1249 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1250 if (status == GOT_STATUS_NO_CHANGE) {
1251 err = remove_ondisk_file(worktree->root_path, ie->path);
1255 got_fileindex_entry_remove(fileindex, ie);
1261 struct diff_cb_arg {
1262 struct got_fileindex *fileindex;
1263 struct got_worktree *worktree;
1264 struct got_repository *repo;
1265 got_worktree_checkout_cb progress_cb;
1267 got_worktree_cancel_cb cancel_cb;
1271 static const struct got_error *
1272 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1273 struct got_tree_entry *te, const char *parent_path)
1275 struct diff_cb_arg *a = arg;
1277 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1278 return got_error(GOT_ERR_CANCELLED);
1280 return update_blob(a->worktree, a->fileindex, ie, te,
1281 ie->path, a->repo, a->progress_cb, a->progress_arg);
1284 static const struct got_error *
1285 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1287 struct diff_cb_arg *a = arg;
1289 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1290 return got_error(GOT_ERR_CANCELLED);
1292 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1293 a->repo, a->progress_cb, a->progress_arg);
1296 static const struct got_error *
1297 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1299 struct diff_cb_arg *a = arg;
1300 const struct got_error *err;
1303 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1304 return got_error(GOT_ERR_CANCELLED);
1306 if (asprintf(&path, "%s%s%s", parent_path,
1307 parent_path[0] ? "/" : "", te->name)
1309 return got_error_from_errno();
1311 if (S_ISDIR(te->mode))
1312 err = add_dir_on_disk(a->worktree, path);
1314 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1315 a->repo, a->progress_cb, a->progress_arg);
1321 const struct got_error *
1322 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1324 const struct got_error *err = NULL;
1325 char *uuidstr = NULL;
1326 uint32_t uuid_status;
1330 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1331 if (uuid_status != uuid_s_ok)
1332 return got_error_uuid(uuid_status);
1334 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1336 err = got_error_from_errno();
1344 * Prevent Git's garbage collector from deleting our base commit by
1345 * setting a reference to our base commit's ID.
1347 static const struct got_error *
1348 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1350 const struct got_error *err = NULL;
1351 struct got_reference *ref = NULL;
1354 err = got_worktree_get_base_ref_name(&refname, worktree);
1358 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1362 err = got_ref_write(ref, repo);
1371 const struct got_error *
1372 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1373 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1374 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1376 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1377 struct got_commit_object *commit = NULL;
1378 struct got_object_id *tree_id = NULL;
1379 struct got_tree_object *tree = NULL;
1380 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1381 struct got_fileindex *fileindex = NULL;
1382 FILE *index = NULL, *new_index = NULL;
1383 struct got_fileindex_diff_tree_cb diff_cb;
1384 struct diff_cb_arg arg;
1385 char *relpath = NULL, *entry_name = NULL;
1387 err = lock_worktree(worktree, LOCK_EX);
1391 fileindex = got_fileindex_alloc();
1392 if (fileindex == NULL) {
1393 err = got_error_from_errno();
1397 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1398 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1399 err = got_error_from_errno();
1400 fileindex_path = NULL;
1405 * Read the file index.
1406 * Checking out files is supposed to be an idempotent operation.
1407 * If the on-disk file index is incomplete we will try to complete it.
1409 index = fopen(fileindex_path, "rb");
1410 if (index == NULL) {
1411 if (errno != ENOENT) {
1412 err = got_error_from_errno();
1416 err = got_fileindex_read(fileindex, index);
1422 err = got_opentemp_named(&new_fileindex_path, &new_index,
1427 err = ref_base_commit(worktree, repo);
1431 err = got_object_open_as_commit(&commit, repo,
1432 worktree->base_commit_id);
1439 relpath = strdup(path);
1440 if (relpath == NULL) {
1441 err = got_error_from_errno();
1444 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1445 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1447 err = got_error_from_errno();
1450 err = got_object_id_by_path(&tree_id, repo,
1451 worktree->base_commit_id, tree_path);
1455 err = got_object_get_type(&obj_type, repo, tree_id);
1458 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1459 /* Split provided path into parent dir + entry name. */
1460 if (strchr(path, '/') == NULL) {
1461 relpath = strdup("");
1462 if (relpath == NULL) {
1463 err = got_error_from_errno();
1466 tree_path = strdup(worktree->path_prefix);
1467 if (tree_path == NULL) {
1468 err = got_error_from_errno();
1472 err = got_path_dirname(&relpath, path);
1475 if (asprintf(&tree_path, "%s%s%s",
1476 worktree->path_prefix,
1477 got_path_is_root_dir(
1478 worktree->path_prefix) ? "" : "/",
1480 err = got_error_from_errno();
1484 err = got_object_id_by_path(&tree_id, repo,
1485 worktree->base_commit_id, tree_path);
1489 entry_name = basename(path);
1490 if (entry_name == NULL) {
1491 err = got_error_from_errno();
1496 relpath = strdup("");
1497 if (relpath == NULL) {
1498 err = got_error_from_errno();
1501 err = got_object_id_by_path(&tree_id, repo,
1502 worktree->base_commit_id, worktree->path_prefix);
1507 err = got_object_open_as_tree(&tree, repo, tree_id);
1512 got_object_tree_find_entry(tree, entry_name) == NULL) {
1513 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1517 diff_cb.diff_old_new = diff_old_new;
1518 diff_cb.diff_old = diff_old;
1519 diff_cb.diff_new = diff_new;
1520 arg.fileindex = fileindex;
1521 arg.worktree = worktree;
1523 arg.progress_cb = progress_cb;
1524 arg.progress_arg = progress_arg;
1525 arg.cancel_cb = cancel_cb;
1526 arg.cancel_arg = cancel_arg;
1527 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1528 entry_name, repo, &diff_cb, &arg);
1530 /* Try to sync the fileindex back to disk in any case. */
1531 err = got_fileindex_write(fileindex, new_index);
1535 if (rename(new_fileindex_path, fileindex_path) != 0) {
1536 err = got_error_from_errno();
1537 unlink(new_fileindex_path);
1541 free(new_fileindex_path);
1542 new_fileindex_path = NULL;
1547 got_object_tree_close(tree);
1549 got_object_commit_close(commit);
1550 if (new_fileindex_path)
1551 unlink(new_fileindex_path);
1554 free(new_fileindex_path);
1555 free(fileindex_path);
1556 got_fileindex_free(fileindex);
1559 unlockerr = lock_worktree(worktree, LOCK_SH);
1560 if (unlockerr && err == NULL)
1565 struct diff_dir_cb_arg {
1566 struct got_fileindex *fileindex;
1567 struct got_worktree *worktree;
1568 const char *status_path;
1569 size_t status_path_len;
1570 struct got_repository *repo;
1571 got_worktree_status_cb status_cb;
1573 got_worktree_cancel_cb cancel_cb;
1577 static const struct got_error *
1578 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1579 got_worktree_status_cb status_cb, void *status_arg,
1580 struct got_repository *repo)
1582 const struct got_error *err = NULL;
1583 unsigned char status = GOT_STATUS_NO_CHANGE;
1585 struct got_object_id id;
1587 err = get_file_status(&status, &sb, ie, abspath, repo);
1588 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1589 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1590 err = (*status_cb)(status_arg, status, ie->path, &id);
1595 static const struct got_error *
1596 status_old_new(void *arg, struct got_fileindex_entry *ie,
1597 struct dirent *de, const char *parent_path)
1599 const struct got_error *err = NULL;
1600 struct diff_dir_cb_arg *a = arg;
1603 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1604 return got_error(GOT_ERR_CANCELLED);
1606 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1607 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1610 if (parent_path[0]) {
1611 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1612 parent_path, de->d_name) == -1)
1613 return got_error_from_errno();
1615 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1617 return got_error_from_errno();
1620 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1626 static const struct got_error *
1627 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1629 struct diff_dir_cb_arg *a = arg;
1630 struct got_object_id id;
1631 unsigned char status;
1633 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1634 return got_error(GOT_ERR_CANCELLED);
1636 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1639 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1640 if (got_fileindex_entry_has_file_on_disk(ie))
1641 status = GOT_STATUS_MISSING;
1643 status = GOT_STATUS_DELETE;
1644 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1647 static const struct got_error *
1648 status_new(void *arg, struct dirent *de, const char *parent_path)
1650 const struct got_error *err = NULL;
1651 struct diff_dir_cb_arg *a = arg;
1654 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1655 return got_error(GOT_ERR_CANCELLED);
1657 if (de->d_type == DT_DIR)
1660 /* XXX ignore symlinks for now */
1661 if (de->d_type == DT_LNK)
1664 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1667 if (parent_path[0]) {
1668 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1669 return got_error_from_errno();
1674 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1681 const struct got_error *
1682 got_worktree_status(struct got_worktree *worktree, const char *path,
1683 struct got_repository *repo, got_worktree_status_cb status_cb,
1684 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1686 const struct got_error *err = NULL;
1687 DIR *workdir = NULL;
1688 char *fileindex_path = NULL;
1689 struct got_fileindex *fileindex = NULL;
1691 struct got_fileindex_diff_dir_cb fdiff_cb;
1692 struct diff_dir_cb_arg arg;
1693 char *ondisk_path = NULL;
1695 fileindex = got_fileindex_alloc();
1696 if (fileindex == NULL) {
1697 err = got_error_from_errno();
1701 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1702 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1703 err = got_error_from_errno();
1704 fileindex_path = NULL;
1708 index = fopen(fileindex_path, "rb");
1709 if (index == NULL) {
1710 if (errno != ENOENT) {
1711 err = got_error_from_errno();
1715 err = got_fileindex_read(fileindex, index);
1721 if (asprintf(&ondisk_path, "%s%s%s",
1722 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1723 err = got_error_from_errno();
1726 workdir = opendir(ondisk_path);
1727 if (workdir == NULL) {
1728 if (errno == ENOTDIR || errno == ENOENT) {
1729 struct got_fileindex_entry *ie;
1730 ie = got_fileindex_entry_get(fileindex, path);
1732 err = got_error(GOT_ERR_BAD_PATH);
1735 err = report_file_status(ie, ondisk_path,
1736 status_cb, status_arg, repo);
1739 err = got_error_from_errno();
1743 fdiff_cb.diff_old_new = status_old_new;
1744 fdiff_cb.diff_old = status_old;
1745 fdiff_cb.diff_new = status_new;
1746 arg.fileindex = fileindex;
1747 arg.worktree = worktree;
1748 arg.status_path = path;
1749 arg.status_path_len = strlen(path);
1751 arg.status_cb = status_cb;
1752 arg.status_arg = status_arg;
1753 arg.cancel_cb = cancel_cb;
1754 arg.cancel_arg = cancel_arg;
1755 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1756 path, repo, &fdiff_cb, &arg);
1761 free(fileindex_path);
1762 got_fileindex_free(fileindex);
1766 const struct got_error *
1767 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1770 const struct got_error *err = NULL;
1771 char *resolved, *path = NULL;
1776 resolved = realpath(arg, NULL);
1777 if (resolved == NULL)
1778 return got_error_from_errno();
1780 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1781 strlen(got_worktree_get_root_path(worktree)))) {
1782 err = got_error(GOT_ERR_BAD_PATH);
1786 path = strdup(resolved +
1787 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1789 err = got_error_from_errno();
1793 /* XXX status walk can't deal with trailing slash! */
1795 while (path[len - 1] == '/') {
1796 path[len - 1] = '\0';
1808 const struct got_error *
1809 got_worktree_schedule_add(struct got_worktree *worktree,
1810 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1811 struct got_repository *repo)
1813 struct got_fileindex *fileindex = NULL;
1814 struct got_fileindex_entry *ie = NULL;
1815 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1816 FILE *index = NULL, *new_index = NULL;
1817 const struct got_error *err = NULL, *unlockerr = NULL;
1820 err = lock_worktree(worktree, LOCK_EX);
1824 err = got_path_skip_common_ancestor(&relpath,
1825 got_worktree_get_root_path(worktree), ondisk_path);
1829 fileindex = got_fileindex_alloc();
1830 if (fileindex == NULL) {
1831 err = got_error_from_errno();
1835 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1836 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1837 err = got_error_from_errno();
1838 fileindex_path = NULL;
1842 index = fopen(fileindex_path, "rb");
1843 if (index == NULL) {
1844 err = got_error_from_errno();
1848 err = got_fileindex_read(fileindex, index);
1852 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1853 err = got_error_set_errno(EEXIST);
1857 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1861 err = got_fileindex_entry_add(fileindex, ie);
1864 ie_added = 1; /* now owned by fileindex; don't free separately */
1866 err = got_opentemp_named(&new_fileindex_path, &new_index,
1871 err = got_fileindex_write(fileindex, new_index);
1875 if (rename(new_fileindex_path, fileindex_path) != 0) {
1876 err = got_error_from_errno();
1880 free(new_fileindex_path);
1881 new_fileindex_path = NULL;
1883 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1886 if (fclose(index) != 0 && err == NULL)
1887 err = got_error_from_errno();
1889 if (new_fileindex_path) {
1890 if (unlink(new_fileindex_path) != 0 && err == NULL)
1891 err = got_error_from_errno();
1892 free(new_fileindex_path);
1894 if (ie && !ie_added)
1895 got_fileindex_entry_free(ie);
1897 got_fileindex_free(fileindex);
1898 unlockerr = lock_worktree(worktree, LOCK_SH);
1899 if (unlockerr && err == NULL)
1905 const struct got_error *
1906 got_worktree_schedule_delete(struct got_worktree *worktree,
1907 const char *ondisk_path, int delete_local_mods,
1908 got_worktree_status_cb status_cb, void *status_arg,
1909 struct got_repository *repo)
1911 struct got_fileindex *fileindex = NULL;
1912 struct got_fileindex_entry *ie = NULL;
1913 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1914 FILE *index = NULL, *new_index = NULL;
1915 const struct got_error *err = NULL, *unlockerr = NULL;
1916 unsigned char status;
1919 err = lock_worktree(worktree, LOCK_EX);
1923 err = got_path_skip_common_ancestor(&relpath,
1924 got_worktree_get_root_path(worktree), ondisk_path);
1928 fileindex = got_fileindex_alloc();
1929 if (fileindex == NULL) {
1930 err = got_error_from_errno();
1934 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1935 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1936 err = got_error_from_errno();
1937 fileindex_path = NULL;
1941 index = fopen(fileindex_path, "rb");
1942 if (index == NULL) {
1943 err = got_error_from_errno();
1947 err = got_fileindex_read(fileindex, index);
1951 ie = got_fileindex_entry_get(fileindex, relpath);
1953 err = got_error(GOT_ERR_BAD_PATH);
1957 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1961 if (status != GOT_STATUS_NO_CHANGE) {
1962 if (status == GOT_STATUS_DELETE) {
1963 err = got_error_set_errno(ENOENT);
1966 if (status != GOT_STATUS_MODIFY) {
1967 err = got_error(GOT_ERR_FILE_STATUS);
1970 if (!delete_local_mods) {
1971 err = got_error(GOT_ERR_FILE_MODIFIED);
1976 if (unlink(ondisk_path) != 0) {
1977 err = got_error_from_errno();
1981 got_fileindex_entry_mark_deleted_from_disk(ie);
1983 err = got_opentemp_named(&new_fileindex_path, &new_index,
1988 err = got_fileindex_write(fileindex, new_index);
1992 if (rename(new_fileindex_path, fileindex_path) != 0) {
1993 err = got_error_from_errno();
1997 free(new_fileindex_path);
1998 new_fileindex_path = NULL;
2000 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2004 if (fclose(index) != 0 && err == NULL)
2005 err = got_error_from_errno();
2007 if (new_fileindex_path) {
2008 if (unlink(new_fileindex_path) != 0 && err == NULL)
2009 err = got_error_from_errno();
2010 free(new_fileindex_path);
2013 got_fileindex_free(fileindex);
2014 unlockerr = lock_worktree(worktree, LOCK_SH);
2015 if (unlockerr && err == NULL)
2020 const struct got_error *
2021 got_worktree_revert(struct got_worktree *worktree,
2022 const char *ondisk_path,
2023 got_worktree_checkout_cb progress_cb, void *progress_arg,
2024 struct got_repository *repo)
2026 struct got_fileindex *fileindex = NULL;
2027 struct got_fileindex_entry *ie = NULL;
2028 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2029 char *tree_path = NULL, *parent_path, *te_name;
2030 FILE *index = NULL, *new_index = NULL;
2031 const struct got_error *err = NULL, *unlockerr = NULL;
2032 struct got_tree_object *tree = NULL;
2033 struct got_object_id id, *tree_id = NULL;
2034 const struct got_tree_entry *te;
2035 struct got_blob_object *blob = NULL;
2036 unsigned char status;
2039 err = lock_worktree(worktree, LOCK_EX);
2043 err = got_path_skip_common_ancestor(&relpath,
2044 got_worktree_get_root_path(worktree), ondisk_path);
2048 fileindex = got_fileindex_alloc();
2049 if (fileindex == NULL) {
2050 err = got_error_from_errno();
2054 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2055 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2056 err = got_error_from_errno();
2057 fileindex_path = NULL;
2061 index = fopen(fileindex_path, "rb");
2062 if (index == NULL) {
2063 err = got_error_from_errno();
2067 err = got_fileindex_read(fileindex, index);
2071 ie = got_fileindex_entry_get(fileindex, relpath);
2073 err = got_error(GOT_ERR_BAD_PATH);
2077 /* Construct in-repository path of tree which contains this blob. */
2078 err = got_path_dirname(&parent_path, ie->path);
2080 if (err->code != GOT_ERR_BAD_PATH)
2084 if (got_path_is_root_dir(worktree->path_prefix)) {
2085 tree_path = strdup(parent_path);
2086 if (tree_path == NULL) {
2087 err = got_error_from_errno();
2091 if (got_path_is_root_dir(parent_path)) {
2092 tree_path = strdup(worktree->path_prefix);
2093 if (tree_path == NULL) {
2094 err = got_error_from_errno();
2098 if (asprintf(&tree_path, "%s/%s",
2099 worktree->path_prefix, parent_path) == -1) {
2100 err = got_error_from_errno();
2106 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2111 err = got_object_open_as_tree(&tree, repo, tree_id);
2115 te_name = basename(ie->path);
2116 if (te_name == NULL) {
2117 err = got_error_from_errno();
2121 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2125 te = got_object_tree_find_entry(tree, te_name);
2126 if (te == NULL && status != GOT_STATUS_ADD) {
2127 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2132 case GOT_STATUS_ADD:
2133 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2134 got_fileindex_entry_remove(fileindex, ie);
2136 case GOT_STATUS_DELETE:
2137 case GOT_STATUS_MODIFY:
2138 case GOT_STATUS_CONFLICT:
2139 case GOT_STATUS_MISSING:
2140 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2141 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2144 err = install_blob(worktree, ondisk_path, ie->path,
2145 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2149 if (status == GOT_STATUS_DELETE) {
2150 err = update_blob_fileindex_entry(worktree,
2151 fileindex, ie, ondisk_path, ie->path, blob, 1);
2160 err = got_opentemp_named(&new_fileindex_path, &new_index,
2165 err = got_fileindex_write(fileindex, new_index);
2169 if (rename(new_fileindex_path, fileindex_path) != 0) {
2170 err = got_error_from_errno();
2174 free(new_fileindex_path);
2175 new_fileindex_path = NULL;
2180 got_object_blob_close(blob);
2182 got_object_tree_close(tree);
2185 if (fclose(index) != 0 && err == NULL)
2186 err = got_error_from_errno();
2188 if (new_fileindex_path) {
2189 if (unlink(new_fileindex_path) != 0 && err == NULL)
2190 err = got_error_from_errno();
2191 free(new_fileindex_path);
2194 got_fileindex_free(fileindex);
2195 unlockerr = lock_worktree(worktree, LOCK_SH);
2196 if (unlockerr && err == NULL)
2201 struct committable {
2202 unsigned char status;
2203 struct got_object_id *id;
2207 free_committable(struct committable *ct)
2213 static const struct got_error *
2214 collect_committables(void *arg, unsigned char status, const char *path,
2215 struct got_object_id *id)
2217 struct got_pathlist_head *paths = arg;
2218 const struct got_error *err = NULL;
2219 char *new_path = NULL;
2220 struct committable *ct = NULL;
2221 struct got_pathlist_entry *new = NULL;
2223 if (status == GOT_STATUS_CONFLICT)
2224 return got_error(GOT_ERR_COMMIT_CONFLICT);
2226 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2227 status != GOT_STATUS_DELETE)
2230 new_path = strdup(path);
2231 if (new_path == NULL)
2232 return got_error_from_errno();
2234 ct = malloc(sizeof(*ct));
2236 err = got_error_from_errno();
2240 ct->status = status;
2242 err = got_pathlist_insert(&new, paths, new_path, ct);
2244 if (err || new == NULL) {
2246 free_committable(ct);
2251 const struct got_error *
2252 got_worktree_commit(struct got_object_id **new_commit_id,
2253 struct got_worktree *worktree, const char *ondisk_path,
2254 const char *logmsg, struct got_repository *repo)
2256 const struct got_error *err = NULL, *unlockerr = NULL;
2257 struct got_pathlist_head paths;
2258 struct got_pathlist_entry *pe;
2259 char *relpath = NULL;
2260 struct got_commit_object *base_commit = NULL;
2261 struct got_tree_object *base_tree = NULL;
2263 *new_commit_id = NULL;
2268 err = got_path_skip_common_ancestor(&relpath,
2269 worktree->root_path, ondisk_path);
2274 err = lock_worktree(worktree, LOCK_EX);
2278 err = got_worktree_status(worktree, relpath ? relpath : "",
2279 repo, collect_committables, &paths, NULL, NULL);
2283 /* TODO: collect commit message if not specified */
2285 /* Create blobs from added and modified files and record their IDs. */
2286 TAILQ_FOREACH(pe, &paths, entry) {
2287 struct committable *ct = pe->data;
2290 if (ct->status != GOT_STATUS_ADD &&
2291 ct->status != GOT_STATUS_MODIFY)
2294 if (asprintf(&ondisk_path, "%s/%s",
2295 worktree->root_path, pe->path) == -1) {
2296 err = got_error_from_errno();
2299 err = got_object_blob_create(&ct->id, pe->path, repo);
2304 err = got_object_open_as_commit(&base_commit, repo,
2305 worktree->base_commit_id);
2308 err = got_object_open_as_tree(&base_tree, repo,
2309 base_commit->tree_id);
2313 /* TODO: walk base tree and patch it to create a new tree */
2314 printf("committables:\n");
2315 TAILQ_FOREACH(pe, &paths, entry) {
2316 unsigned char *status = pe->data;
2317 printf("%c %s\n", *status, pe->path);
2320 unlockerr = lock_worktree(worktree, LOCK_SH);
2321 if (unlockerr && err == NULL)
2323 TAILQ_FOREACH(pe, &paths, entry) {
2324 struct committable *ct = pe->data;
2325 free_committable(ct);
2327 got_pathlist_free(&paths);
2328 got_object_tree_close(base_tree);