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_diff.h"
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 static const struct got_error *
58 create_meta_file(const char *path_got, const char *name, const char *content)
60 const struct got_error *err = NULL;
64 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
65 err = got_error_from_errno();
70 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
71 GOT_DEFAULT_FILE_MODE);
73 err = got_error_from_errno();
78 int len = dprintf(fd, "%s\n", content);
79 if (len != strlen(content) + 1) {
80 err = got_error_from_errno();
86 if (fd != -1 && close(fd) == -1 && err == NULL)
87 err = got_error_from_errno();
92 static const struct got_error *
93 update_meta_file(const char *path_got, const char *name, const char *content)
95 const struct got_error *err = NULL;
100 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
101 err = got_error_from_errno();
106 err = got_opentemp_named(&tmppath, &tmpfile, path);
111 int len = fprintf(tmpfile, "%s\n", content);
112 if (len != strlen(content) + 1) {
113 err = got_error_from_errno();
118 if (rename(tmppath, path) != 0) {
119 err = got_error_from_errno();
126 if (fclose(tmpfile) != 0 && err == NULL)
127 err = got_error_from_errno();
131 static const struct got_error *
132 read_meta_file(char **content, const char *path_got, const char *name)
134 const struct got_error *err = NULL;
142 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
143 err = got_error_from_errno();
148 fd = open(path, O_RDONLY | O_NOFOLLOW);
151 err = got_error(GOT_ERR_WORKTREE_META);
153 err = got_error_from_errno();
156 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
157 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
158 : got_error_from_errno());
162 if (lstat(path, &sb) != 0) {
163 err = got_error_from_errno();
166 *content = calloc(1, sb.st_size);
167 if (*content == NULL) {
168 err = got_error_from_errno();
172 n = read(fd, *content, sb.st_size);
173 if (n != sb.st_size) {
174 err = (n == -1 ? got_error_from_errno() :
175 got_error(GOT_ERR_WORKTREE_META));
178 if ((*content)[sb.st_size - 1] != '\n') {
179 err = got_error(GOT_ERR_WORKTREE_META);
182 (*content)[sb.st_size - 1] = '\0';
185 if (fd != -1 && close(fd) == -1 && err == NULL)
186 err = got_error_from_errno();
195 const struct got_error *
196 got_worktree_init(const char *path, struct got_reference *head_ref,
197 const char *prefix, struct got_repository *repo)
199 const struct got_error *err = NULL;
200 struct got_object_id *commit_id = NULL;
202 uint32_t uuid_status;
204 char *path_got = NULL;
206 char *formatstr = NULL;
207 char *absprefix = NULL;
208 char *basestr = NULL;
209 char *uuidstr = NULL;
211 if (strcmp(path, got_repo_get_path(repo)) == 0) {
212 err = got_error(GOT_ERR_WORKTREE_REPO);
216 err = got_ref_resolve(&commit_id, repo, head_ref);
219 err = got_object_get_type(&obj_type, repo, commit_id);
222 if (obj_type != GOT_OBJ_TYPE_COMMIT)
223 return got_error(GOT_ERR_OBJ_TYPE);
225 if (!got_path_is_absolute(prefix)) {
226 if (asprintf(&absprefix, "/%s", prefix) == -1)
227 return got_error_from_errno();
230 /* Create top-level directory (may already exist). */
231 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
232 err = got_error_from_errno();
236 /* Create .got directory (may already exist). */
237 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
238 err = got_error_from_errno();
241 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
242 err = got_error_from_errno();
246 /* Create an empty lock file. */
247 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
251 /* Create an empty file index. */
252 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
256 /* Write the HEAD reference. */
257 refstr = got_ref_to_str(head_ref);
258 if (refstr == NULL) {
259 err = got_error_from_errno();
262 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
266 /* Record our base commit. */
267 err = got_object_id_str(&basestr, commit_id);
270 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 /* Store path to repository. */
275 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
276 got_repo_get_path(repo));
280 /* Store in-repository path prefix. */
281 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
282 absprefix ? absprefix : prefix);
287 uuid_create(&uuid, &uuid_status);
288 if (uuid_status != uuid_s_ok) {
289 err = got_error_uuid(uuid_status);
292 uuid_to_string(&uuid, &uuidstr, &uuid_status);
293 if (uuid_status != uuid_s_ok) {
294 err = got_error_uuid(uuid_status);
297 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 /* Stamp work tree with format file. */
302 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
303 err = got_error_from_errno();
306 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
321 static const struct got_error *
322 open_worktree(struct got_worktree **worktree, const char *path)
324 const struct got_error *err = NULL;
326 char *formatstr = NULL;
327 char *uuidstr = NULL;
328 char *path_lock = NULL;
329 char *base_commit_id_str = NULL;
330 char *head_ref_str = NULL;
331 int version, fd = -1;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno();
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno();
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno());
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 err = got_error(GOT_ERR_WORKTREE_META);
366 if (version != GOT_WORKTREE_FORMAT_VERSION) {
367 err = got_error(GOT_ERR_WORKTREE_VERS);
371 *worktree = calloc(1, sizeof(**worktree));
372 if (*worktree == NULL) {
373 err = got_error_from_errno();
376 (*worktree)->lockfd = -1;
378 (*worktree)->root_path = strdup(path);
379 if ((*worktree)->root_path == NULL) {
380 err = got_error_from_errno();
383 err = read_meta_file(&(*worktree)->repo_path, path_got,
384 GOT_WORKTREE_REPOSITORY);
388 err = read_meta_file(&(*worktree)->path_prefix, path_got,
389 GOT_WORKTREE_PATH_PREFIX);
393 err = read_meta_file(&base_commit_id_str, path_got,
394 GOT_WORKTREE_BASE_COMMIT);
398 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
402 if (uuid_status != uuid_s_ok) {
403 err = got_error_uuid(uuid_status);
407 err = got_repo_open(&repo, (*worktree)->repo_path);
411 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
416 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
420 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
423 got_repo_close(repo);
427 free(base_commit_id_str);
433 if (*worktree != NULL)
434 got_worktree_close(*worktree);
437 (*worktree)->lockfd = fd;
442 const struct got_error *
443 got_worktree_open(struct got_worktree **worktree, const char *path)
445 const struct got_error *err = NULL;
448 err = open_worktree(worktree, path);
449 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
453 path = dirname(path);
455 return got_error_from_errno();
456 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
458 return got_error(GOT_ERR_NOT_WORKTREE);
461 const struct got_error *
462 got_worktree_close(struct got_worktree *worktree)
464 const struct got_error *err = NULL;
465 free(worktree->root_path);
466 free(worktree->repo_path);
467 free(worktree->path_prefix);
468 free(worktree->base_commit_id);
469 if (worktree->head_ref)
470 got_ref_close(worktree->head_ref);
471 if (worktree->lockfd != -1)
472 if (close(worktree->lockfd) != 0)
473 err = got_error_from_errno();
479 got_worktree_get_root_path(struct got_worktree *worktree)
481 return worktree->root_path;
485 got_worktree_get_repo_path(struct got_worktree *worktree)
487 return worktree->repo_path;
491 got_worktree_get_path_prefix(struct got_worktree *worktree)
493 return worktree->path_prefix;
496 const struct got_error *
497 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
498 const char *path_prefix)
500 char *absprefix = NULL;
502 if (!got_path_is_absolute(path_prefix)) {
503 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
504 return got_error_from_errno();
506 *match = (strcmp(absprefix ? absprefix : path_prefix,
507 worktree->path_prefix) == 0);
513 got_worktree_get_head_ref_name(struct got_worktree *worktree)
515 return got_ref_to_str(worktree->head_ref);
518 struct got_reference *
519 got_worktree_get_head_ref(struct got_worktree *worktree)
521 return got_ref_dup(worktree->head_ref);
524 struct got_object_id *
525 got_worktree_get_base_commit_id(struct got_worktree *worktree)
527 return worktree->base_commit_id;
530 const struct got_error *
531 got_worktree_set_base_commit_id(struct got_worktree *worktree,
532 struct got_repository *repo, struct got_object_id *commit_id)
534 const struct got_error *err;
535 struct got_object *obj = NULL;
537 char *path_got = NULL;
539 if (asprintf(&path_got, "%s/%s", worktree->root_path,
540 GOT_WORKTREE_GOT_DIR) == -1) {
541 err = got_error_from_errno();
546 err = got_object_open(&obj, repo, commit_id);
550 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
551 err = got_error(GOT_ERR_OBJ_TYPE);
555 /* Record our base commit. */
556 err = got_object_id_str(&id_str, commit_id);
559 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
563 free(worktree->base_commit_id);
564 worktree->base_commit_id = got_object_id_dup(commit_id);
565 if (worktree->base_commit_id == NULL) {
566 err = got_error_from_errno();
571 got_object_close(obj);
577 static const struct got_error *
578 lock_worktree(struct got_worktree *worktree, int operation)
580 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
581 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
582 : got_error_from_errno());
586 static const struct got_error *
587 add_dir_on_disk(struct got_worktree *worktree, const char *path)
589 const struct got_error *err = NULL;
592 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
593 return got_error_from_errno();
595 err = got_path_mkdir(abspath);
596 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
599 if (lstat(abspath, &sb) == -1) {
600 err = got_error_from_errno();
601 } else if (!S_ISDIR(sb.st_mode)) {
602 /* TODO directory is obstructed; do something */
603 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
610 static const struct got_error *
611 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
613 const struct got_error *err = NULL;
616 size_t flen1 = 0, flen2 = 0;
621 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
622 if (flen1 == 0 && ferror(f1)) {
623 err = got_error_from_errno();
626 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
627 if (flen2 == 0 && ferror(f2)) {
628 err = got_error_from_errno();
635 } else if (flen2 == 0) {
639 } else if (flen1 == flen2) {
640 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
653 static const struct got_error *
654 check_files_equal(int *same, const char *f1_path, const char *f2_path)
656 const struct got_error *err = NULL;
659 FILE *f1 = NULL, *f2 = NULL;
663 if (lstat(f1_path, &sb) != 0) {
664 err = got_error_from_errno();
669 if (lstat(f2_path, &sb) != 0) {
670 err = got_error_from_errno();
675 if (size1 != size2) {
680 f1 = fopen(f1_path, "r");
682 return got_error_from_errno();
684 f2 = fopen(f2_path, "r");
686 err = got_error_from_errno();
690 err = check_file_contents_equal(same, f1, f2);
692 if (f1 && fclose(f1) != 0 && err == NULL)
693 err = got_error_from_errno();
694 if (f2 && fclose(f2) != 0 && err == NULL)
695 err = got_error_from_errno();
701 * Perform a 3-way merge where the file's version in the file index (blob2)
702 * acts as the common ancestor, the incoming blob (blob1) acts as the first
703 * derived version, and the file on disk acts as the second derived version.
705 static const struct got_error *
706 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
707 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
708 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
709 struct got_repository *repo,
710 got_worktree_checkout_cb progress_cb, void *progress_arg)
712 const struct got_error *err = NULL;
714 struct got_blob_object *blob2 = NULL;
715 FILE *f1 = NULL, *f2 = NULL;
716 char *blob1_path = NULL, *blob2_path = NULL;
717 char *merged_path = NULL, *base_path = NULL;
720 int overlapcnt = 0, update_timestamps = 0;
723 parent = dirname(ondisk_path);
725 return got_error_from_errno();
727 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
728 return got_error_from_errno();
730 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
735 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
736 err = got_error_from_errno();
741 err = got_opentemp_named(&blob1_path, &f1, base_path);
744 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
749 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
750 err = got_error_from_errno();
755 err = got_opentemp_named(&blob2_path, &f2, base_path);
758 if (got_fileindex_entry_has_blob(ie)) {
759 struct got_object_id id2;
760 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
761 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
764 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
769 * If the file has no blob, this is an "add vs add" conflict,
770 * and we simply use an empty ancestor file to make both files
771 * appear in the merged result in their entirety.
775 err = got_object_id_str(&id_str, worktree->base_commit_id);
778 if (asprintf(&label1, "commit %s", id_str) == -1) {
779 err = got_error_from_errno();
783 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
784 blob2_path, ondisk_path, label1, path);
788 (*progress_cb)(progress_arg,
789 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno();
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(&update_timestamps, blob1_path,
805 if (chmod(merged_path, st_mode) != 0) {
806 err = got_error_from_errno();
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno();
817 * Do not update timestamps of already modified files. Otherwise,
818 * a future status walk would treat them as unmodified files again.
820 err = got_fileindex_entry_update(ie, ondisk_path,
821 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
823 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
824 err = got_error_from_errno();
825 if (f1 && fclose(f1) != 0 && err == NULL)
826 err = got_error_from_errno();
827 if (f2 && fclose(f2) != 0 && err == NULL)
828 err = got_error_from_errno();
830 got_object_blob_close(blob2);
846 static const struct got_error *
847 update_blob_fileindex_entry(struct got_worktree *worktree,
848 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
849 const char *ondisk_path, const char *path, struct got_blob_object *blob,
850 int update_timestamps)
852 const struct got_error *err = NULL;
855 ie = got_fileindex_entry_get(fileindex, path);
857 err = got_fileindex_entry_update(ie, ondisk_path,
858 blob->id.sha1, worktree->base_commit_id->sha1,
861 struct got_fileindex_entry *new_ie;
862 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
863 path, blob->id.sha1, worktree->base_commit_id->sha1);
865 err = got_fileindex_entry_add(fileindex, new_ie);
870 static const struct got_error *
871 install_blob(struct got_worktree *worktree, const char *ondisk_path,
872 const char *path, uint16_t te_mode, uint16_t st_mode,
873 struct got_blob_object *blob, int restoring_missing_file,
874 int reverting_versioned_file, struct got_repository *repo,
875 got_worktree_checkout_cb progress_cb, void *progress_arg)
877 const struct got_error *err = NULL;
881 char *tmppath = NULL;
883 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
884 GOT_DEFAULT_FILE_MODE);
886 if (errno == ENOENT) {
887 char *parent = dirname(path);
889 return got_error_from_errno();
890 err = add_dir_on_disk(worktree, parent);
893 fd = open(ondisk_path,
894 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
895 GOT_DEFAULT_FILE_MODE);
897 return got_error_from_errno();
898 } else if (errno == EEXIST) {
899 if (!S_ISREG(st_mode)) {
900 /* TODO file is obstructed; do something */
901 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
904 err = got_opentemp_named_fd(&tmppath, &fd,
911 return got_error_from_errno();
914 if (restoring_missing_file)
915 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
916 else if (reverting_versioned_file)
917 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
919 (*progress_cb)(progress_arg,
920 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
922 hdrlen = got_object_blob_get_hdrlen(blob);
924 const uint8_t *buf = got_object_blob_get_read_buf(blob);
925 err = got_object_blob_read_block(&len, blob);
929 /* Skip blob object header first time around. */
930 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
932 err = got_error_from_errno();
934 } else if (outlen != len - hdrlen) {
935 err = got_error(GOT_ERR_IO);
942 if (fsync(fd) != 0) {
943 err = got_error_from_errno();
948 if (rename(tmppath, ondisk_path) != 0) {
949 err = got_error_from_errno();
955 if (te_mode & S_IXUSR) {
956 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
957 err = got_error_from_errno();
961 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
962 err = got_error_from_errno();
968 if (fd != -1 && close(fd) != 0 && err == NULL)
969 err = got_error_from_errno();
974 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
975 static const struct got_error *
976 get_modified_file_content_status(unsigned char *status, FILE *f)
978 const struct got_error *err = NULL;
979 const char *markers[3] = {
980 GOT_DIFF_CONFLICT_MARKER_BEGIN,
981 GOT_DIFF_CONFLICT_MARKER_SEP,
982 GOT_DIFF_CONFLICT_MARKER_END
987 const char delim[3] = {'\0', '\0', '\0'};
989 while (*status == GOT_STATUS_MODIFY) {
990 line = fparseln(f, &len, NULL, delim, 0);
994 err = got_ferror(f, GOT_ERR_IO);
998 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
999 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
1000 *status = GOT_STATUS_CONFLICT;
1009 static const struct got_error *
1010 get_file_status(unsigned char *status, struct stat *sb,
1011 struct got_fileindex_entry *ie, const char *abspath,
1012 struct got_repository *repo)
1014 const struct got_error *err = NULL;
1015 struct got_object_id id;
1019 struct got_blob_object *blob = NULL;
1022 *status = GOT_STATUS_NO_CHANGE;
1024 if (lstat(abspath, sb) == -1) {
1025 if (errno == ENOENT) {
1027 if (got_fileindex_entry_has_file_on_disk(ie))
1028 *status = GOT_STATUS_MISSING;
1030 *status = GOT_STATUS_DELETE;
1032 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1033 & (S_IRWXU | S_IRWXG | S_IRWXO));
1035 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1038 return got_error_from_errno();
1041 if (!S_ISREG(sb->st_mode)) {
1042 *status = GOT_STATUS_OBSTRUCTED;
1049 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1050 *status = GOT_STATUS_DELETE;
1052 } else if (!got_fileindex_entry_has_blob(ie)) {
1053 *status = GOT_STATUS_ADD;
1057 if (ie->ctime_sec == sb->st_ctime &&
1058 ie->ctime_nsec == sb->st_ctimensec &&
1059 ie->mtime_sec == sb->st_mtime &&
1060 ie->mtime_sec == sb->st_mtime &&
1061 ie->mtime_nsec == sb->st_mtimensec &&
1062 ie->size == (sb->st_size & 0xffffffff))
1065 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1066 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1070 f = fopen(abspath, "r");
1072 err = got_error_from_errno();
1075 hdrlen = got_object_blob_get_hdrlen(blob);
1077 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1078 err = got_object_blob_read_block(&blen, blob);
1081 /* Skip length of blob object header first time around. */
1082 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1083 if (flen == 0 && ferror(f)) {
1084 err = got_error_from_errno();
1089 *status = GOT_STATUS_MODIFY;
1091 } else if (flen == 0) {
1093 *status = GOT_STATUS_MODIFY;
1095 } else if (blen - hdrlen == flen) {
1096 /* Skip blob object header first time around. */
1097 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1098 *status = GOT_STATUS_MODIFY;
1102 *status = GOT_STATUS_MODIFY;
1108 if (*status == GOT_STATUS_MODIFY) {
1110 err = get_modified_file_content_status(status, f);
1114 got_object_blob_close(blob);
1120 static const struct got_error *
1121 update_blob(struct got_worktree *worktree,
1122 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1123 struct got_tree_entry *te, const char *path,
1124 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1127 const struct got_error *err = NULL;
1128 struct got_blob_object *blob = NULL;
1130 unsigned char status = GOT_STATUS_NO_CHANGE;
1133 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1134 return got_error_from_errno();
1136 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1140 if (status == GOT_STATUS_OBSTRUCTED) {
1141 (*progress_cb)(progress_arg, status, path);
1145 if (ie && status != GOT_STATUS_MISSING) {
1146 if (got_fileindex_entry_has_commit(ie) &&
1147 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1148 SHA1_DIGEST_LENGTH) == 0) {
1149 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1153 if (got_fileindex_entry_has_blob(ie) &&
1154 memcmp(ie->blob_sha1, te->id->sha1,
1155 SHA1_DIGEST_LENGTH) == 0)
1159 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1163 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1164 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1165 te->mode, sb.st_mode, blob, repo, progress_cb,
1167 else if (status == GOT_STATUS_DELETE) {
1168 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1169 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1170 ondisk_path, path, blob, 0);
1174 err = install_blob(worktree, ondisk_path, path, te->mode,
1175 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1176 repo, progress_cb, progress_arg);
1179 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1180 ondisk_path, path, blob, 1);
1184 got_object_blob_close(blob);
1190 static const struct got_error *
1191 remove_ondisk_file(const char *root_path, const char *path)
1193 const struct got_error *err = NULL;
1194 char *ondisk_path = NULL;
1196 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1197 return got_error_from_errno();
1199 if (unlink(ondisk_path) == -1) {
1200 if (errno != ENOENT)
1201 err = got_error_from_errno();
1203 char *parent = dirname(ondisk_path);
1204 while (parent && strcmp(parent, root_path) != 0) {
1205 if (rmdir(parent) == -1) {
1206 if (errno != ENOTEMPTY)
1207 err = got_error_from_errno();
1210 parent = dirname(parent);
1217 static const struct got_error *
1218 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1219 struct got_fileindex_entry *ie, const char *parent_path,
1220 struct got_repository *repo,
1221 got_worktree_checkout_cb progress_cb, void *progress_arg)
1223 const struct got_error *err = NULL;
1224 unsigned char status;
1228 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1230 return got_error_from_errno();
1232 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1236 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1237 status == GOT_STATUS_ADD) {
1238 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1240 * Preserve the working file and change the deleted blob's
1241 * entry into a schedule-add entry.
1243 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1248 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1249 if (status == GOT_STATUS_NO_CHANGE) {
1250 err = remove_ondisk_file(worktree->root_path, ie->path);
1254 got_fileindex_entry_remove(fileindex, ie);
1260 struct diff_cb_arg {
1261 struct got_fileindex *fileindex;
1262 struct got_worktree *worktree;
1263 struct got_repository *repo;
1264 got_worktree_checkout_cb progress_cb;
1266 got_worktree_cancel_cb cancel_cb;
1270 static const struct got_error *
1271 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1272 struct got_tree_entry *te, const char *parent_path)
1274 struct diff_cb_arg *a = arg;
1276 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1277 return got_error(GOT_ERR_CANCELLED);
1279 return update_blob(a->worktree, a->fileindex, ie, te,
1280 ie->path, a->repo, a->progress_cb, a->progress_arg);
1283 static const struct got_error *
1284 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1286 struct diff_cb_arg *a = arg;
1288 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1289 return got_error(GOT_ERR_CANCELLED);
1291 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1292 a->repo, a->progress_cb, a->progress_arg);
1295 static const struct got_error *
1296 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1298 struct diff_cb_arg *a = arg;
1299 const struct got_error *err;
1302 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1303 return got_error(GOT_ERR_CANCELLED);
1305 if (asprintf(&path, "%s%s%s", parent_path,
1306 parent_path[0] ? "/" : "", te->name)
1308 return got_error_from_errno();
1310 if (S_ISDIR(te->mode))
1311 err = add_dir_on_disk(a->worktree, path);
1313 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1314 a->repo, a->progress_cb, a->progress_arg);
1320 const struct got_error *
1321 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1323 const struct got_error *err = NULL;
1324 char *uuidstr = NULL;
1325 uint32_t uuid_status;
1329 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1330 if (uuid_status != uuid_s_ok)
1331 return got_error_uuid(uuid_status);
1333 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1335 err = got_error_from_errno();
1343 * Prevent Git's garbage collector from deleting our base commit by
1344 * setting a reference to our base commit's ID.
1346 static const struct got_error *
1347 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1349 const struct got_error *err = NULL;
1350 struct got_reference *ref = NULL;
1353 err = got_worktree_get_base_ref_name(&refname, worktree);
1357 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1361 err = got_ref_write(ref, repo);
1370 const struct got_error *
1371 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1372 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1373 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1375 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1376 struct got_commit_object *commit = NULL;
1377 struct got_object_id *tree_id = NULL;
1378 struct got_tree_object *tree = NULL;
1379 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1380 struct got_fileindex *fileindex = NULL;
1381 FILE *index = NULL, *new_index = NULL;
1382 struct got_fileindex_diff_tree_cb diff_cb;
1383 struct diff_cb_arg arg;
1384 char *relpath = NULL, *entry_name = NULL;
1386 err = lock_worktree(worktree, LOCK_EX);
1390 fileindex = got_fileindex_alloc();
1391 if (fileindex == NULL) {
1392 err = got_error_from_errno();
1396 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1397 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1398 err = got_error_from_errno();
1399 fileindex_path = NULL;
1404 * Read the file index.
1405 * Checking out files is supposed to be an idempotent operation.
1406 * If the on-disk file index is incomplete we will try to complete it.
1408 index = fopen(fileindex_path, "rb");
1409 if (index == NULL) {
1410 if (errno != ENOENT) {
1411 err = got_error_from_errno();
1415 err = got_fileindex_read(fileindex, index);
1421 err = got_opentemp_named(&new_fileindex_path, &new_index,
1426 err = ref_base_commit(worktree, repo);
1430 err = got_object_open_as_commit(&commit, repo,
1431 worktree->base_commit_id);
1438 relpath = strdup(path);
1439 if (relpath == NULL) {
1440 err = got_error_from_errno();
1443 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1444 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1446 err = got_error_from_errno();
1449 err = got_object_id_by_path(&tree_id, repo,
1450 worktree->base_commit_id, tree_path);
1454 err = got_object_get_type(&obj_type, repo, tree_id);
1457 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1458 /* Split provided path into parent dir + entry name. */
1459 if (strchr(path, '/') == NULL) {
1460 relpath = strdup("");
1461 if (relpath == NULL) {
1462 err = got_error_from_errno();
1465 tree_path = strdup(worktree->path_prefix);
1466 if (tree_path == NULL) {
1467 err = got_error_from_errno();
1471 err = got_path_dirname(&relpath, path);
1474 if (asprintf(&tree_path, "%s%s%s",
1475 worktree->path_prefix,
1476 got_path_is_root_dir(
1477 worktree->path_prefix) ? "" : "/",
1479 err = got_error_from_errno();
1483 err = got_object_id_by_path(&tree_id, repo,
1484 worktree->base_commit_id, tree_path);
1488 entry_name = basename(path);
1489 if (entry_name == NULL) {
1490 err = got_error_from_errno();
1495 relpath = strdup("");
1496 if (relpath == NULL) {
1497 err = got_error_from_errno();
1500 err = got_object_id_by_path(&tree_id, repo,
1501 worktree->base_commit_id, worktree->path_prefix);
1506 err = got_object_open_as_tree(&tree, repo, tree_id);
1511 got_object_tree_find_entry(tree, entry_name) == NULL) {
1512 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1516 diff_cb.diff_old_new = diff_old_new;
1517 diff_cb.diff_old = diff_old;
1518 diff_cb.diff_new = diff_new;
1519 arg.fileindex = fileindex;
1520 arg.worktree = worktree;
1522 arg.progress_cb = progress_cb;
1523 arg.progress_arg = progress_arg;
1524 arg.cancel_cb = cancel_cb;
1525 arg.cancel_arg = cancel_arg;
1526 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1527 entry_name, repo, &diff_cb, &arg);
1529 /* Try to sync the fileindex back to disk in any case. */
1530 err = got_fileindex_write(fileindex, new_index);
1534 if (rename(new_fileindex_path, fileindex_path) != 0) {
1535 err = got_error_from_errno();
1536 unlink(new_fileindex_path);
1540 free(new_fileindex_path);
1541 new_fileindex_path = NULL;
1546 got_object_tree_close(tree);
1548 got_object_commit_close(commit);
1549 if (new_fileindex_path)
1550 unlink(new_fileindex_path);
1553 free(new_fileindex_path);
1554 free(fileindex_path);
1555 got_fileindex_free(fileindex);
1558 unlockerr = lock_worktree(worktree, LOCK_SH);
1559 if (unlockerr && err == NULL)
1564 struct diff_dir_cb_arg {
1565 struct got_fileindex *fileindex;
1566 struct got_worktree *worktree;
1567 const char *status_path;
1568 size_t status_path_len;
1569 struct got_repository *repo;
1570 got_worktree_status_cb status_cb;
1572 got_worktree_cancel_cb cancel_cb;
1576 static const struct got_error *
1577 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1578 got_worktree_status_cb status_cb, void *status_arg,
1579 struct got_repository *repo)
1581 const struct got_error *err = NULL;
1582 unsigned char status = GOT_STATUS_NO_CHANGE;
1584 struct got_object_id id;
1586 err = get_file_status(&status, &sb, ie, abspath, repo);
1587 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1588 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1589 err = (*status_cb)(status_arg, status, ie->path, &id);
1594 static const struct got_error *
1595 status_old_new(void *arg, struct got_fileindex_entry *ie,
1596 struct dirent *de, const char *parent_path)
1598 const struct got_error *err = NULL;
1599 struct diff_dir_cb_arg *a = arg;
1602 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1603 return got_error(GOT_ERR_CANCELLED);
1605 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1606 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1609 if (parent_path[0]) {
1610 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1611 parent_path, de->d_name) == -1)
1612 return got_error_from_errno();
1614 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1616 return got_error_from_errno();
1619 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1625 static const struct got_error *
1626 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1628 struct diff_dir_cb_arg *a = arg;
1629 struct got_object_id id;
1630 unsigned char status;
1632 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1633 return got_error(GOT_ERR_CANCELLED);
1635 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1638 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1639 if (got_fileindex_entry_has_file_on_disk(ie))
1640 status = GOT_STATUS_MISSING;
1642 status = GOT_STATUS_DELETE;
1643 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1646 static const struct got_error *
1647 status_new(void *arg, struct dirent *de, const char *parent_path)
1649 const struct got_error *err = NULL;
1650 struct diff_dir_cb_arg *a = arg;
1653 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1654 return got_error(GOT_ERR_CANCELLED);
1656 if (de->d_type == DT_DIR)
1659 /* XXX ignore symlinks for now */
1660 if (de->d_type == DT_LNK)
1663 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1666 if (parent_path[0]) {
1667 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1668 return got_error_from_errno();
1673 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1680 const struct got_error *
1681 got_worktree_status(struct got_worktree *worktree, const char *path,
1682 struct got_repository *repo, got_worktree_status_cb status_cb,
1683 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1685 const struct got_error *err = NULL;
1686 DIR *workdir = NULL;
1687 char *fileindex_path = NULL;
1688 struct got_fileindex *fileindex = NULL;
1690 struct got_fileindex_diff_dir_cb fdiff_cb;
1691 struct diff_dir_cb_arg arg;
1692 char *ondisk_path = NULL;
1694 fileindex = got_fileindex_alloc();
1695 if (fileindex == NULL) {
1696 err = got_error_from_errno();
1700 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1701 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1702 err = got_error_from_errno();
1703 fileindex_path = NULL;
1707 index = fopen(fileindex_path, "rb");
1708 if (index == NULL) {
1709 if (errno != ENOENT) {
1710 err = got_error_from_errno();
1714 err = got_fileindex_read(fileindex, index);
1720 if (asprintf(&ondisk_path, "%s%s%s",
1721 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1722 err = got_error_from_errno();
1725 workdir = opendir(ondisk_path);
1726 if (workdir == NULL) {
1727 if (errno == ENOTDIR || errno == ENOENT) {
1728 struct got_fileindex_entry *ie;
1729 ie = got_fileindex_entry_get(fileindex, path);
1731 err = got_error(GOT_ERR_BAD_PATH);
1734 err = report_file_status(ie, ondisk_path,
1735 status_cb, status_arg, repo);
1738 err = got_error_from_errno();
1742 fdiff_cb.diff_old_new = status_old_new;
1743 fdiff_cb.diff_old = status_old;
1744 fdiff_cb.diff_new = status_new;
1745 arg.fileindex = fileindex;
1746 arg.worktree = worktree;
1747 arg.status_path = path;
1748 arg.status_path_len = strlen(path);
1750 arg.status_cb = status_cb;
1751 arg.status_arg = status_arg;
1752 arg.cancel_cb = cancel_cb;
1753 arg.cancel_arg = cancel_arg;
1754 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1755 path, repo, &fdiff_cb, &arg);
1760 free(fileindex_path);
1761 got_fileindex_free(fileindex);
1765 const struct got_error *
1766 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1769 const struct got_error *err = NULL;
1770 char *resolved, *path = NULL;
1775 resolved = realpath(arg, NULL);
1776 if (resolved == NULL)
1777 return got_error_from_errno();
1779 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1780 strlen(got_worktree_get_root_path(worktree)))) {
1781 err = got_error(GOT_ERR_BAD_PATH);
1785 path = strdup(resolved +
1786 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1788 err = got_error_from_errno();
1792 /* XXX status walk can't deal with trailing slash! */
1794 while (path[len - 1] == '/') {
1795 path[len - 1] = '\0';
1807 const struct got_error *
1808 got_worktree_schedule_add(struct got_worktree *worktree,
1809 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1810 struct got_repository *repo)
1812 struct got_fileindex *fileindex = NULL;
1813 struct got_fileindex_entry *ie = NULL;
1814 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1815 FILE *index = NULL, *new_index = NULL;
1816 const struct got_error *err = NULL, *unlockerr = NULL;
1819 err = lock_worktree(worktree, LOCK_EX);
1823 err = got_path_skip_common_ancestor(&relpath,
1824 got_worktree_get_root_path(worktree), ondisk_path);
1828 fileindex = got_fileindex_alloc();
1829 if (fileindex == NULL) {
1830 err = got_error_from_errno();
1834 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1835 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1836 err = got_error_from_errno();
1837 fileindex_path = NULL;
1841 index = fopen(fileindex_path, "rb");
1842 if (index == NULL) {
1843 err = got_error_from_errno();
1847 err = got_fileindex_read(fileindex, index);
1851 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1852 err = got_error_set_errno(EEXIST);
1856 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1860 err = got_fileindex_entry_add(fileindex, ie);
1863 ie_added = 1; /* now owned by fileindex; don't free separately */
1865 err = got_opentemp_named(&new_fileindex_path, &new_index,
1870 err = got_fileindex_write(fileindex, new_index);
1874 if (rename(new_fileindex_path, fileindex_path) != 0) {
1875 err = got_error_from_errno();
1879 free(new_fileindex_path);
1880 new_fileindex_path = NULL;
1882 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1885 if (fclose(index) != 0 && err == NULL)
1886 err = got_error_from_errno();
1888 if (new_fileindex_path) {
1889 if (unlink(new_fileindex_path) != 0 && err == NULL)
1890 err = got_error_from_errno();
1891 free(new_fileindex_path);
1893 if (ie && !ie_added)
1894 got_fileindex_entry_free(ie);
1896 got_fileindex_free(fileindex);
1897 unlockerr = lock_worktree(worktree, LOCK_SH);
1898 if (unlockerr && err == NULL)
1904 const struct got_error *
1905 got_worktree_schedule_delete(struct got_worktree *worktree,
1906 const char *ondisk_path, int delete_local_mods,
1907 got_worktree_status_cb status_cb, void *status_arg,
1908 struct got_repository *repo)
1910 struct got_fileindex *fileindex = NULL;
1911 struct got_fileindex_entry *ie = NULL;
1912 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1913 FILE *index = NULL, *new_index = NULL;
1914 const struct got_error *err = NULL, *unlockerr = NULL;
1915 unsigned char status;
1918 err = lock_worktree(worktree, LOCK_EX);
1922 err = got_path_skip_common_ancestor(&relpath,
1923 got_worktree_get_root_path(worktree), ondisk_path);
1927 fileindex = got_fileindex_alloc();
1928 if (fileindex == NULL) {
1929 err = got_error_from_errno();
1933 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1934 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1935 err = got_error_from_errno();
1936 fileindex_path = NULL;
1940 index = fopen(fileindex_path, "rb");
1941 if (index == NULL) {
1942 err = got_error_from_errno();
1946 err = got_fileindex_read(fileindex, index);
1950 ie = got_fileindex_entry_get(fileindex, relpath);
1952 err = got_error(GOT_ERR_BAD_PATH);
1956 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1960 if (status != GOT_STATUS_NO_CHANGE) {
1961 if (status == GOT_STATUS_DELETE) {
1962 err = got_error_set_errno(ENOENT);
1965 if (status != GOT_STATUS_MODIFY) {
1966 err = got_error(GOT_ERR_FILE_STATUS);
1969 if (!delete_local_mods) {
1970 err = got_error(GOT_ERR_FILE_MODIFIED);
1975 if (unlink(ondisk_path) != 0) {
1976 err = got_error_from_errno();
1980 got_fileindex_entry_mark_deleted_from_disk(ie);
1982 err = got_opentemp_named(&new_fileindex_path, &new_index,
1987 err = got_fileindex_write(fileindex, new_index);
1991 if (rename(new_fileindex_path, fileindex_path) != 0) {
1992 err = got_error_from_errno();
1996 free(new_fileindex_path);
1997 new_fileindex_path = NULL;
1999 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2003 if (fclose(index) != 0 && err == NULL)
2004 err = got_error_from_errno();
2006 if (new_fileindex_path) {
2007 if (unlink(new_fileindex_path) != 0 && err == NULL)
2008 err = got_error_from_errno();
2009 free(new_fileindex_path);
2012 got_fileindex_free(fileindex);
2013 unlockerr = lock_worktree(worktree, LOCK_SH);
2014 if (unlockerr && err == NULL)
2019 const struct got_error *
2020 got_worktree_revert(struct got_worktree *worktree,
2021 const char *ondisk_path,
2022 got_worktree_checkout_cb progress_cb, void *progress_arg,
2023 struct got_repository *repo)
2025 struct got_fileindex *fileindex = NULL;
2026 struct got_fileindex_entry *ie = NULL;
2027 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2028 char *tree_path = NULL, *parent_path, *te_name;
2029 FILE *index = NULL, *new_index = NULL;
2030 const struct got_error *err = NULL, *unlockerr = NULL;
2031 struct got_tree_object *tree = NULL;
2032 struct got_object_id id, *tree_id = NULL;
2033 const struct got_tree_entry *te;
2034 struct got_blob_object *blob = NULL;
2035 unsigned char status;
2038 err = lock_worktree(worktree, LOCK_EX);
2042 err = got_path_skip_common_ancestor(&relpath,
2043 got_worktree_get_root_path(worktree), ondisk_path);
2047 fileindex = got_fileindex_alloc();
2048 if (fileindex == NULL) {
2049 err = got_error_from_errno();
2053 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2054 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2055 err = got_error_from_errno();
2056 fileindex_path = NULL;
2060 index = fopen(fileindex_path, "rb");
2061 if (index == NULL) {
2062 err = got_error_from_errno();
2066 err = got_fileindex_read(fileindex, index);
2070 ie = got_fileindex_entry_get(fileindex, relpath);
2072 err = got_error(GOT_ERR_BAD_PATH);
2076 /* Construct in-repository path of tree which contains this blob. */
2077 err = got_path_dirname(&parent_path, ie->path);
2079 if (err->code != GOT_ERR_BAD_PATH)
2083 if (got_path_is_root_dir(worktree->path_prefix)) {
2084 tree_path = strdup(parent_path);
2085 if (tree_path == NULL) {
2086 err = got_error_from_errno();
2090 if (got_path_is_root_dir(parent_path)) {
2091 tree_path = strdup(worktree->path_prefix);
2092 if (tree_path == NULL) {
2093 err = got_error_from_errno();
2097 if (asprintf(&tree_path, "%s/%s",
2098 worktree->path_prefix, parent_path) == -1) {
2099 err = got_error_from_errno();
2105 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2110 err = got_object_open_as_tree(&tree, repo, tree_id);
2114 te_name = basename(ie->path);
2115 if (te_name == NULL) {
2116 err = got_error_from_errno();
2120 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2124 te = got_object_tree_find_entry(tree, te_name);
2125 if (te == NULL && status != GOT_STATUS_ADD) {
2126 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2131 case GOT_STATUS_ADD:
2132 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2133 got_fileindex_entry_remove(fileindex, ie);
2135 case GOT_STATUS_DELETE:
2136 case GOT_STATUS_MODIFY:
2137 case GOT_STATUS_CONFLICT:
2138 case GOT_STATUS_MISSING:
2139 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2140 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2143 err = install_blob(worktree, ondisk_path, ie->path,
2144 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2148 if (status == GOT_STATUS_DELETE) {
2149 err = update_blob_fileindex_entry(worktree,
2150 fileindex, ie, ondisk_path, ie->path, blob, 1);
2159 err = got_opentemp_named(&new_fileindex_path, &new_index,
2164 err = got_fileindex_write(fileindex, new_index);
2168 if (rename(new_fileindex_path, fileindex_path) != 0) {
2169 err = got_error_from_errno();
2173 free(new_fileindex_path);
2174 new_fileindex_path = NULL;
2179 got_object_blob_close(blob);
2181 got_object_tree_close(tree);
2184 if (fclose(index) != 0 && err == NULL)
2185 err = got_error_from_errno();
2187 if (new_fileindex_path) {
2188 if (unlink(new_fileindex_path) != 0 && err == NULL)
2189 err = got_error_from_errno();
2190 free(new_fileindex_path);
2193 got_fileindex_free(fileindex);
2194 unlockerr = lock_worktree(worktree, LOCK_SH);
2195 if (unlockerr && err == NULL)
2200 static const struct got_error *
2201 collect_committables(void *arg, unsigned char status, const char *path,
2202 struct got_object_id *id)
2204 struct got_pathlist_head *paths = arg;
2205 const struct got_error *err = NULL;
2206 char *new_path = NULL;
2207 unsigned char *new_status = NULL;
2208 struct got_pathlist_entry *new = NULL;
2210 if (status == GOT_STATUS_CONFLICT)
2211 return got_error(GOT_ERR_COMMIT_CONFLICT);
2213 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2214 status != GOT_STATUS_DELETE)
2217 new_path = strdup(path);
2218 if (new_path == NULL)
2219 return got_error_from_errno();
2221 new_status = malloc(sizeof(*new_status));
2222 if (new_status == NULL) {
2223 err = got_error_from_errno();
2227 *new_status = status;
2228 err = got_pathlist_insert(&new, paths, new_path, new_status);
2230 if (err || new == NULL) {
2237 const struct got_error *
2238 got_worktree_commit(struct got_object_id **new_commit_id,
2239 struct got_worktree *worktree, const char *ondisk_path,
2240 const char *logmsg, struct got_repository *repo)
2242 const struct got_error *err = NULL, *unlockerr = NULL;
2243 struct got_pathlist_head paths;
2244 struct got_pathlist_entry *pe;
2245 char *relpath = NULL;
2246 struct got_commit_object *base_commit = NULL;
2247 struct got_tree_object *base_tree = NULL;
2249 *new_commit_id = NULL;
2254 err = got_path_skip_common_ancestor(&relpath,
2255 worktree->root_path, ondisk_path);
2260 err = lock_worktree(worktree, LOCK_EX);
2264 err = got_object_open_as_commit(&base_commit, repo,
2265 worktree->base_commit_id);
2268 err = got_object_open_as_tree(&base_tree, repo,
2269 worktree->base_commit_id);
2273 err = got_worktree_status(worktree, relpath ? relpath : "",
2274 repo, collect_committables, &paths, NULL, NULL);
2278 /* TODO: walk base tree and patch it to create a new tree */
2279 printf("committables:\n");
2280 TAILQ_FOREACH(pe, &paths, entry) {
2281 unsigned char *status = pe->data;
2282 printf("%c %s\n", *status, pe->path);
2285 unlockerr = lock_worktree(worktree, LOCK_SH);
2286 if (unlockerr && err == NULL)
2288 got_object_tree_close(base_tree);