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"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
45 #include "got_lib_worktree.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 static const struct got_error *
61 create_meta_file(const char *path_got, const char *name, const char *content)
63 const struct got_error *err = NULL;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
68 err = got_error_from_errno("asprintf");
73 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
74 GOT_DEFAULT_FILE_MODE);
76 err = got_error_from_errno2("open", path);
81 int len = dprintf(fd, "%s\n", content);
82 if (len != strlen(content) + 1) {
83 err = got_error_from_errno("dprintf");
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_from_errno("close");
95 static const struct got_error *
96 update_meta_file(const char *path_got, const char *name, const char *content)
98 const struct got_error *err = NULL;
100 char *tmppath = NULL;
103 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
104 err = got_error_from_errno("asprintf");
109 err = got_opentemp_named(&tmppath, &tmpfile, path);
114 int len = fprintf(tmpfile, "%s\n", content);
115 if (len != strlen(content) + 1) {
116 err = got_error_from_errno2("fprintf", tmppath);
121 if (rename(tmppath, path) != 0) {
122 err = got_error_from_errno3("rename", tmppath, path);
128 if (fclose(tmpfile) != 0 && err == NULL)
129 err = got_error_from_errno2("fclose", tmppath);
134 static const struct got_error *
135 read_meta_file(char **content, const char *path_got, const char *name)
137 const struct got_error *err = NULL;
145 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
146 err = got_error_from_errno("asprintf");
151 fd = open(path, O_RDONLY | O_NOFOLLOW);
154 err = got_error(GOT_ERR_WORKTREE_META);
156 err = got_error_from_errno2("open", path);
159 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
160 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
161 : got_error_from_errno2("flock", path));
165 if (lstat(path, &sb) != 0) {
166 err = got_error_from_errno2("lstat", path);
169 *content = calloc(1, sb.st_size);
170 if (*content == NULL) {
171 err = got_error_from_errno("calloc");
175 n = read(fd, *content, sb.st_size);
176 if (n != sb.st_size) {
177 err = (n == -1 ? got_error_from_errno2("read", path) :
178 got_error(GOT_ERR_WORKTREE_META));
181 if ((*content)[sb.st_size - 1] != '\n') {
182 err = got_error(GOT_ERR_WORKTREE_META);
185 (*content)[sb.st_size - 1] = '\0';
188 if (fd != -1 && close(fd) == -1 && err == NULL)
189 err = got_error_from_errno2("close", path_got);
198 static const struct got_error *
199 write_head_ref(const char *path_got, struct got_reference *head_ref)
201 const struct got_error *err = NULL;
204 if (got_ref_is_symbolic(head_ref)) {
205 refstr = got_ref_to_str(head_ref);
207 return got_error_from_errno("got_ref_to_str");
209 refstr = strdup(got_ref_get_name(head_ref));
211 return got_error_from_errno("strdup");
213 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
218 const struct got_error *
219 got_worktree_init(const char *path, struct got_reference *head_ref,
220 const char *prefix, struct got_repository *repo)
222 const struct got_error *err = NULL;
223 struct got_object_id *commit_id = NULL;
225 uint32_t uuid_status;
227 char *path_got = NULL;
228 char *formatstr = NULL;
229 char *absprefix = NULL;
230 char *basestr = NULL;
231 char *uuidstr = NULL;
233 if (strcmp(path, got_repo_get_path(repo)) == 0) {
234 err = got_error(GOT_ERR_WORKTREE_REPO);
238 err = got_ref_resolve(&commit_id, repo, head_ref);
241 err = got_object_get_type(&obj_type, repo, commit_id);
244 if (obj_type != GOT_OBJ_TYPE_COMMIT)
245 return got_error(GOT_ERR_OBJ_TYPE);
247 if (!got_path_is_absolute(prefix)) {
248 if (asprintf(&absprefix, "/%s", prefix) == -1)
249 return got_error_from_errno("asprintf");
252 /* Create top-level directory (may already exist). */
253 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
254 err = got_error_from_errno2("mkdir", path);
258 /* Create .got directory (may already exist). */
259 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
260 err = got_error_from_errno("asprintf");
263 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
264 err = got_error_from_errno2("mkdir", path_got);
268 /* Create an empty lock file. */
269 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
273 /* Create an empty file index. */
274 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
278 /* Write the HEAD reference. */
279 err = write_head_ref(path_got, head_ref);
283 /* Record our base commit. */
284 err = got_object_id_str(&basestr, commit_id);
287 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
291 /* Store path to repository. */
292 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
293 got_repo_get_path(repo));
297 /* Store in-repository path prefix. */
298 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
299 absprefix ? absprefix : prefix);
304 uuid_create(&uuid, &uuid_status);
305 if (uuid_status != uuid_s_ok) {
306 err = got_error_uuid(uuid_status);
309 uuid_to_string(&uuid, &uuidstr, &uuid_status);
310 if (uuid_status != uuid_s_ok) {
311 err = got_error_uuid(uuid_status);
314 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
318 /* Stamp work tree with format file. */
319 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
320 err = got_error_from_errno("asprintf");
323 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
337 static const struct got_error *
338 open_worktree(struct got_worktree **worktree, const char *path)
340 const struct got_error *err = NULL;
342 char *formatstr = NULL;
343 char *uuidstr = NULL;
344 char *path_lock = NULL;
345 char *base_commit_id_str = NULL;
346 int version, fd = -1;
348 struct got_repository *repo = NULL;
349 uint32_t uuid_status;
353 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
354 err = got_error_from_errno("asprintf");
359 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
360 err = got_error_from_errno("asprintf");
365 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
367 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
368 : got_error_from_errno2("open", path_lock));
372 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
376 version = strtonum(formatstr, 1, INT_MAX, &errstr);
378 err = got_error(GOT_ERR_WORKTREE_META);
381 if (version != GOT_WORKTREE_FORMAT_VERSION) {
382 err = got_error(GOT_ERR_WORKTREE_VERS);
386 *worktree = calloc(1, sizeof(**worktree));
387 if (*worktree == NULL) {
388 err = got_error_from_errno("calloc");
391 (*worktree)->lockfd = -1;
393 (*worktree)->root_path = strdup(path);
394 if ((*worktree)->root_path == NULL) {
395 err = got_error_from_errno("strdup");
398 err = read_meta_file(&(*worktree)->repo_path, path_got,
399 GOT_WORKTREE_REPOSITORY);
403 err = read_meta_file(&(*worktree)->path_prefix, path_got,
404 GOT_WORKTREE_PATH_PREFIX);
408 err = read_meta_file(&base_commit_id_str, path_got,
409 GOT_WORKTREE_BASE_COMMIT);
413 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
416 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
417 if (uuid_status != uuid_s_ok) {
418 err = got_error_uuid(uuid_status);
422 err = got_repo_open(&repo, (*worktree)->repo_path);
426 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
431 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
432 GOT_WORKTREE_HEAD_REF);
435 got_repo_close(repo);
438 free(base_commit_id_str);
444 if (*worktree != NULL)
445 got_worktree_close(*worktree);
448 (*worktree)->lockfd = fd;
453 const struct got_error *
454 got_worktree_open(struct got_worktree **worktree, const char *path)
456 const struct got_error *err = NULL;
459 err = open_worktree(worktree, path);
460 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
464 path = dirname(path);
466 return got_error_from_errno2("dirname", path);
467 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
469 return got_error(GOT_ERR_NOT_WORKTREE);
472 const struct got_error *
473 got_worktree_close(struct got_worktree *worktree)
475 const struct got_error *err = NULL;
476 free(worktree->root_path);
477 free(worktree->repo_path);
478 free(worktree->path_prefix);
479 free(worktree->base_commit_id);
480 free(worktree->head_ref_name);
481 if (worktree->lockfd != -1)
482 if (close(worktree->lockfd) != 0)
483 err = got_error_from_errno2("close",
484 got_worktree_get_root_path(worktree));
490 got_worktree_get_root_path(struct got_worktree *worktree)
492 return worktree->root_path;
496 got_worktree_get_repo_path(struct got_worktree *worktree)
498 return worktree->repo_path;
502 got_worktree_get_path_prefix(struct got_worktree *worktree)
504 return worktree->path_prefix;
507 const struct got_error *
508 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
509 const char *path_prefix)
511 char *absprefix = NULL;
513 if (!got_path_is_absolute(path_prefix)) {
514 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
515 return got_error_from_errno("asprintf");
517 *match = (strcmp(absprefix ? absprefix : path_prefix,
518 worktree->path_prefix) == 0);
524 got_worktree_get_head_ref_name(struct got_worktree *worktree)
526 return worktree->head_ref_name;
529 const struct got_error *
530 got_worktree_set_head_ref(struct got_worktree *worktree,
531 struct got_reference *head_ref)
533 const struct got_error *err = NULL;
534 char *path_got = NULL, *head_ref_name = NULL;
536 if (asprintf(&path_got, "%s/%s", worktree->root_path,
537 GOT_WORKTREE_GOT_DIR) == -1) {
538 err = got_error_from_errno("asprintf");
543 head_ref_name = strdup(got_ref_get_name(head_ref));
544 if (head_ref_name == NULL) {
545 err = got_error_from_errno("strdup");
549 err = write_head_ref(path_got, head_ref);
553 free(worktree->head_ref_name);
554 worktree->head_ref_name = head_ref_name;
562 struct got_object_id *
563 got_worktree_get_base_commit_id(struct got_worktree *worktree)
565 return worktree->base_commit_id;
568 const struct got_error *
569 got_worktree_set_base_commit_id(struct got_worktree *worktree,
570 struct got_repository *repo, struct got_object_id *commit_id)
572 const struct got_error *err;
573 struct got_object *obj = NULL;
575 char *path_got = NULL;
577 if (asprintf(&path_got, "%s/%s", worktree->root_path,
578 GOT_WORKTREE_GOT_DIR) == -1) {
579 err = got_error_from_errno("asprintf");
584 err = got_object_open(&obj, repo, commit_id);
588 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
589 err = got_error(GOT_ERR_OBJ_TYPE);
593 /* Record our base commit. */
594 err = got_object_id_str(&id_str, commit_id);
597 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
601 free(worktree->base_commit_id);
602 worktree->base_commit_id = got_object_id_dup(commit_id);
603 if (worktree->base_commit_id == NULL) {
604 err = got_error_from_errno("got_object_id_dup");
609 got_object_close(obj);
615 static const struct got_error *
616 lock_worktree(struct got_worktree *worktree, int operation)
618 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
619 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
620 : got_error_from_errno2("flock",
621 got_worktree_get_root_path(worktree)));
625 static const struct got_error *
626 add_dir_on_disk(struct got_worktree *worktree, const char *path)
628 const struct got_error *err = NULL;
631 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
632 return got_error_from_errno("asprintf");
634 err = got_path_mkdir(abspath);
635 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
638 if (lstat(abspath, &sb) == -1) {
639 err = got_error_from_errno2("lstat", abspath);
640 } else if (!S_ISDIR(sb.st_mode)) {
641 /* TODO directory is obstructed; do something */
642 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
649 static const struct got_error *
650 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
652 const struct got_error *err = NULL;
655 size_t flen1 = 0, flen2 = 0;
660 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
661 if (flen1 == 0 && ferror(f1)) {
662 err = got_error_from_errno("fread");
665 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
666 if (flen2 == 0 && ferror(f2)) {
667 err = got_error_from_errno("fread");
674 } else if (flen2 == 0) {
678 } else if (flen1 == flen2) {
679 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
692 static const struct got_error *
693 check_files_equal(int *same, const char *f1_path, const char *f2_path)
695 const struct got_error *err = NULL;
698 FILE *f1 = NULL, *f2 = NULL;
702 if (lstat(f1_path, &sb) != 0) {
703 err = got_error_from_errno2("lstat", f1_path);
708 if (lstat(f2_path, &sb) != 0) {
709 err = got_error_from_errno2("lstat", f2_path);
714 if (size1 != size2) {
719 f1 = fopen(f1_path, "r");
721 return got_error_from_errno2("open", f1_path);
723 f2 = fopen(f2_path, "r");
725 err = got_error_from_errno2("open", f2_path);
729 err = check_file_contents_equal(same, f1, f2);
731 if (f1 && fclose(f1) != 0 && err == NULL)
732 err = got_error_from_errno("fclose");
733 if (f2 && fclose(f2) != 0 && err == NULL)
734 err = got_error_from_errno("fclose");
740 * Perform a 3-way merge where the file's version in the file index (blob2)
741 * acts as the common ancestor, the incoming blob (blob1) acts as the first
742 * derived version, and the file on disk acts as the second derived version.
744 static const struct got_error *
745 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
746 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
747 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
748 struct got_repository *repo,
749 got_worktree_checkout_cb progress_cb, void *progress_arg)
751 const struct got_error *err = NULL;
753 struct got_blob_object *blob2 = NULL;
754 FILE *f1 = NULL, *f2 = NULL;
755 char *blob1_path = NULL, *blob2_path = NULL;
756 char *merged_path = NULL, *base_path = NULL;
759 int overlapcnt = 0, update_timestamps = 0;
762 parent = dirname(ondisk_path);
764 return got_error_from_errno2("dirname", ondisk_path);
766 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
767 return got_error_from_errno("asprintf");
769 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
774 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
775 err = got_error_from_errno("asprintf");
780 err = got_opentemp_named(&blob1_path, &f1, base_path);
783 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
788 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
789 err = got_error_from_errno("asprintf");
794 err = got_opentemp_named(&blob2_path, &f2, base_path);
797 if (got_fileindex_entry_has_blob(ie)) {
798 struct got_object_id id2;
799 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
800 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
803 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
808 * If the file has no blob, this is an "add vs add" conflict,
809 * and we simply use an empty ancestor file to make both files
810 * appear in the merged result in their entirety.
814 err = got_object_id_str(&id_str, worktree->base_commit_id);
817 if (asprintf(&label1, "commit %s", id_str) == -1) {
818 err = got_error_from_errno("asprintf");
822 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
823 blob2_path, ondisk_path, label1, path);
827 (*progress_cb)(progress_arg,
828 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
830 if (fsync(merged_fd) != 0) {
831 err = got_error_from_errno("fsync");
835 /* Check if a clean merge has subsumed all local changes. */
836 if (overlapcnt == 0) {
837 err = check_files_equal(&update_timestamps, blob1_path,
843 if (chmod(merged_path, st_mode) != 0) {
844 err = got_error_from_errno2("chmod", merged_path);
848 if (rename(merged_path, ondisk_path) != 0) {
849 err = got_error_from_errno3("rename", merged_path,
856 * Do not update timestamps of already modified files. Otherwise,
857 * a future status walk would treat them as unmodified files again.
859 err = got_fileindex_entry_update(ie, ondisk_path,
860 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
862 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
863 err = got_error_from_errno("close");
864 if (f1 && fclose(f1) != 0 && err == NULL)
865 err = got_error_from_errno("fclose");
866 if (f2 && fclose(f2) != 0 && err == NULL)
867 err = got_error_from_errno("fclose");
869 got_object_blob_close(blob2);
885 static const struct got_error *
886 update_blob_fileindex_entry(struct got_worktree *worktree,
887 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
888 const char *ondisk_path, const char *path, struct got_blob_object *blob,
889 int update_timestamps)
891 const struct got_error *err = NULL;
894 ie = got_fileindex_entry_get(fileindex, path);
896 err = got_fileindex_entry_update(ie, ondisk_path,
897 blob->id.sha1, worktree->base_commit_id->sha1,
900 struct got_fileindex_entry *new_ie;
901 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
902 path, blob->id.sha1, worktree->base_commit_id->sha1);
904 err = got_fileindex_entry_add(fileindex, new_ie);
909 static const struct got_error *
910 install_blob(struct got_worktree *worktree, const char *ondisk_path,
911 const char *path, uint16_t te_mode, uint16_t st_mode,
912 struct got_blob_object *blob, int restoring_missing_file,
913 int reverting_versioned_file, struct got_repository *repo,
914 got_worktree_checkout_cb progress_cb, void *progress_arg)
916 const struct got_error *err = NULL;
920 char *tmppath = NULL;
922 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
923 GOT_DEFAULT_FILE_MODE);
925 if (errno == ENOENT) {
926 char *parent = dirname(path);
928 return got_error_from_errno2("dirname", path);
929 err = add_dir_on_disk(worktree, parent);
932 fd = open(ondisk_path,
933 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
934 GOT_DEFAULT_FILE_MODE);
936 return got_error_from_errno2("open",
938 } else if (errno == EEXIST) {
939 if (!S_ISREG(st_mode)) {
940 /* TODO file is obstructed; do something */
941 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
944 err = got_opentemp_named_fd(&tmppath, &fd,
951 return got_error_from_errno2("open", ondisk_path);
954 if (restoring_missing_file)
955 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
956 else if (reverting_versioned_file)
957 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
959 (*progress_cb)(progress_arg,
960 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
962 hdrlen = got_object_blob_get_hdrlen(blob);
964 const uint8_t *buf = got_object_blob_get_read_buf(blob);
965 err = got_object_blob_read_block(&len, blob);
969 /* Skip blob object header first time around. */
970 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
972 err = got_error_from_errno("write");
974 } else if (outlen != len - hdrlen) {
975 err = got_error(GOT_ERR_IO);
982 if (fsync(fd) != 0) {
983 err = got_error_from_errno("fsync");
988 if (rename(tmppath, ondisk_path) != 0) {
989 err = got_error_from_errno3("rename", tmppath,
996 if (te_mode & S_IXUSR) {
997 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
998 err = got_error_from_errno2("chmod", ondisk_path);
1002 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
1003 err = got_error_from_errno2("chmod", ondisk_path);
1009 if (fd != -1 && close(fd) != 0 && err == NULL)
1010 err = got_error_from_errno("close");
1015 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1016 static const struct got_error *
1017 get_modified_file_content_status(unsigned char *status, FILE *f)
1019 const struct got_error *err = NULL;
1020 const char *markers[3] = {
1021 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1022 GOT_DIFF_CONFLICT_MARKER_SEP,
1023 GOT_DIFF_CONFLICT_MARKER_END
1028 const char delim[3] = {'\0', '\0', '\0'};
1030 while (*status == GOT_STATUS_MODIFY) {
1031 line = fparseln(f, &len, NULL, delim, 0);
1035 err = got_ferror(f, GOT_ERR_IO);
1039 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1040 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1042 *status = GOT_STATUS_CONFLICT;
1051 static const struct got_error *
1052 get_file_status(unsigned char *status, struct stat *sb,
1053 struct got_fileindex_entry *ie, const char *abspath,
1054 struct got_repository *repo)
1056 const struct got_error *err = NULL;
1057 struct got_object_id id;
1061 struct got_blob_object *blob = NULL;
1064 *status = GOT_STATUS_NO_CHANGE;
1066 if (lstat(abspath, sb) == -1) {
1067 if (errno == ENOENT) {
1069 if (got_fileindex_entry_has_file_on_disk(ie))
1070 *status = GOT_STATUS_MISSING;
1072 *status = GOT_STATUS_DELETE;
1074 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1075 & (S_IRWXU | S_IRWXG | S_IRWXO));
1077 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1080 return got_error_from_errno2("lstat", abspath);
1083 if (!S_ISREG(sb->st_mode)) {
1084 *status = GOT_STATUS_OBSTRUCTED;
1091 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1092 *status = GOT_STATUS_DELETE;
1094 } else if (!got_fileindex_entry_has_blob(ie)) {
1095 *status = GOT_STATUS_ADD;
1099 if (ie->ctime_sec == sb->st_ctime &&
1100 ie->ctime_nsec == sb->st_ctimensec &&
1101 ie->mtime_sec == sb->st_mtime &&
1102 ie->mtime_sec == sb->st_mtime &&
1103 ie->mtime_nsec == sb->st_mtimensec &&
1104 ie->size == (sb->st_size & 0xffffffff))
1107 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1108 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1112 f = fopen(abspath, "r");
1114 err = got_error_from_errno2("fopen", abspath);
1117 hdrlen = got_object_blob_get_hdrlen(blob);
1119 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1120 err = got_object_blob_read_block(&blen, blob);
1123 /* Skip length of blob object header first time around. */
1124 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1125 if (flen == 0 && ferror(f)) {
1126 err = got_error_from_errno("fread");
1131 *status = GOT_STATUS_MODIFY;
1133 } else if (flen == 0) {
1135 *status = GOT_STATUS_MODIFY;
1137 } else if (blen - hdrlen == flen) {
1138 /* Skip blob object header first time around. */
1139 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1140 *status = GOT_STATUS_MODIFY;
1144 *status = GOT_STATUS_MODIFY;
1150 if (*status == GOT_STATUS_MODIFY) {
1152 err = get_modified_file_content_status(status, f);
1156 got_object_blob_close(blob);
1162 static const struct got_error *
1163 update_blob(struct got_worktree *worktree,
1164 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1165 struct got_tree_entry *te, const char *path,
1166 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1169 const struct got_error *err = NULL;
1170 struct got_blob_object *blob = NULL;
1172 unsigned char status = GOT_STATUS_NO_CHANGE;
1175 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1176 return got_error_from_errno("asprintf");
1178 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1182 if (status == GOT_STATUS_OBSTRUCTED) {
1183 (*progress_cb)(progress_arg, status, path);
1187 if (ie && status != GOT_STATUS_MISSING) {
1188 if (got_fileindex_entry_has_commit(ie) &&
1189 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1190 SHA1_DIGEST_LENGTH) == 0) {
1191 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1195 if (got_fileindex_entry_has_blob(ie) &&
1196 memcmp(ie->blob_sha1, te->id->sha1,
1197 SHA1_DIGEST_LENGTH) == 0)
1201 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1205 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1206 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1207 te->mode, sb.st_mode, blob, repo, progress_cb,
1209 else if (status == GOT_STATUS_DELETE) {
1210 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1211 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1212 ondisk_path, path, blob, 0);
1216 err = install_blob(worktree, ondisk_path, path, te->mode,
1217 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1218 repo, progress_cb, progress_arg);
1221 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1222 ondisk_path, path, blob, 1);
1226 got_object_blob_close(blob);
1232 static const struct got_error *
1233 remove_ondisk_file(const char *root_path, const char *path)
1235 const struct got_error *err = NULL;
1236 char *ondisk_path = NULL;
1238 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1239 return got_error_from_errno("asprintf");
1241 if (unlink(ondisk_path) == -1) {
1242 if (errno != ENOENT)
1243 err = got_error_from_errno2("unlink", ondisk_path);
1245 char *parent = dirname(ondisk_path);
1246 while (parent && strcmp(parent, root_path) != 0) {
1247 if (rmdir(parent) == -1) {
1248 if (errno != ENOTEMPTY)
1249 err = got_error_from_errno2("rmdir",
1253 parent = dirname(parent);
1260 static const struct got_error *
1261 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1262 struct got_fileindex_entry *ie, const char *parent_path,
1263 struct got_repository *repo,
1264 got_worktree_checkout_cb progress_cb, void *progress_arg)
1266 const struct got_error *err = NULL;
1267 unsigned char status;
1271 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1273 return got_error_from_errno("asprintf");
1275 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1279 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1280 status == GOT_STATUS_ADD) {
1281 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1283 * Preserve the working file and change the deleted blob's
1284 * entry into a schedule-add entry.
1286 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1291 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1292 if (status == GOT_STATUS_NO_CHANGE) {
1293 err = remove_ondisk_file(worktree->root_path, ie->path);
1297 got_fileindex_entry_remove(fileindex, ie);
1303 struct diff_cb_arg {
1304 struct got_fileindex *fileindex;
1305 struct got_worktree *worktree;
1306 struct got_repository *repo;
1307 got_worktree_checkout_cb progress_cb;
1309 got_worktree_cancel_cb cancel_cb;
1313 static const struct got_error *
1314 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1315 struct got_tree_entry *te, const char *parent_path)
1317 struct diff_cb_arg *a = arg;
1319 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 return got_error(GOT_ERR_CANCELLED);
1322 return update_blob(a->worktree, a->fileindex, ie, te,
1323 ie->path, a->repo, a->progress_cb, a->progress_arg);
1326 static const struct got_error *
1327 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1329 struct diff_cb_arg *a = arg;
1331 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1332 return got_error(GOT_ERR_CANCELLED);
1334 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1335 a->repo, a->progress_cb, a->progress_arg);
1338 static const struct got_error *
1339 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1341 struct diff_cb_arg *a = arg;
1342 const struct got_error *err;
1345 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1346 return got_error(GOT_ERR_CANCELLED);
1348 if (asprintf(&path, "%s%s%s", parent_path,
1349 parent_path[0] ? "/" : "", te->name)
1351 return got_error_from_errno("asprintf");
1353 if (S_ISDIR(te->mode))
1354 err = add_dir_on_disk(a->worktree, path);
1356 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1357 a->repo, a->progress_cb, a->progress_arg);
1363 const struct got_error *
1364 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1366 const struct got_error *err = NULL;
1367 char *uuidstr = NULL;
1368 uint32_t uuid_status;
1372 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1373 if (uuid_status != uuid_s_ok)
1374 return got_error_uuid(uuid_status);
1376 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1378 err = got_error_from_errno("asprintf");
1386 * Prevent Git's garbage collector from deleting our base commit by
1387 * setting a reference to our base commit's ID.
1389 static const struct got_error *
1390 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1392 const struct got_error *err = NULL;
1393 struct got_reference *ref = NULL;
1396 err = got_worktree_get_base_ref_name(&refname, worktree);
1400 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1404 err = got_ref_write(ref, repo);
1412 static const struct got_error *
1413 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1414 struct got_worktree *worktree)
1416 const struct got_error *err = NULL;
1419 *fileindex_path = NULL;
1420 *fileindex = got_fileindex_alloc();
1421 if (*fileindex == NULL)
1422 return got_error_from_errno("got_fileindex_alloc");
1424 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1425 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1426 err = got_error_from_errno("asprintf");
1427 *fileindex_path = NULL;
1431 index = fopen(*fileindex_path, "rb");
1432 if (index == NULL) {
1433 if (errno != ENOENT)
1434 err = got_error_from_errno2("fopen", *fileindex_path);
1436 err = got_fileindex_read(*fileindex, index);
1437 if (fclose(index) != 0 && err == NULL)
1438 err = got_error_from_errno("fclose");
1442 free(*fileindex_path);
1443 *fileindex_path = NULL;
1450 struct bump_base_commit_id_arg {
1451 struct got_object_id *base_commit_id;
1454 const char *entry_name;
1457 /* Bump base commit ID of all files within an updated part of the work tree. */
1458 static const struct got_error *
1459 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1461 struct bump_base_commit_id_arg *a = arg;
1463 if (a->entry_name) {
1464 if (strcmp(ie->path, a->path) != 0)
1466 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1469 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1473 const struct got_error *
1474 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1475 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1476 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1478 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1479 struct got_commit_object *commit = NULL;
1480 struct got_object_id *tree_id = NULL;
1481 struct got_tree_object *tree = NULL;
1482 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1483 struct got_fileindex *fileindex = NULL;
1484 FILE *new_index = NULL;
1485 struct got_fileindex_diff_tree_cb diff_cb;
1486 struct diff_cb_arg arg;
1487 char *relpath = NULL, *entry_name = NULL;
1489 err = lock_worktree(worktree, LOCK_EX);
1494 * Read the file index.
1495 * Checking out files is supposed to be an idempotent operation.
1496 * If the on-disk file index is incomplete we will try to complete it.
1498 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1502 err = got_opentemp_named(&new_fileindex_path, &new_index,
1507 err = ref_base_commit(worktree, repo);
1511 err = got_object_open_as_commit(&commit, repo,
1512 worktree->base_commit_id);
1519 relpath = strdup(path);
1520 if (relpath == NULL) {
1521 err = got_error_from_errno("strdup");
1524 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1525 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1527 err = got_error_from_errno("asprintf");
1530 err = got_object_id_by_path(&tree_id, repo,
1531 worktree->base_commit_id, tree_path);
1535 err = got_object_get_type(&obj_type, repo, tree_id);
1538 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1539 /* Split provided path into parent dir + entry name. */
1540 if (strchr(path, '/') == NULL) {
1541 relpath = strdup("");
1542 if (relpath == NULL) {
1543 err = got_error_from_errno("strdup");
1546 tree_path = strdup(worktree->path_prefix);
1547 if (tree_path == NULL) {
1548 err = got_error_from_errno("strdup");
1552 err = got_path_dirname(&relpath, path);
1555 if (asprintf(&tree_path, "%s%s%s",
1556 worktree->path_prefix,
1557 got_path_is_root_dir(
1558 worktree->path_prefix) ? "" : "/",
1560 err = got_error_from_errno("asprintf");
1564 err = got_object_id_by_path(&tree_id, repo,
1565 worktree->base_commit_id, tree_path);
1569 entry_name = basename(path);
1570 if (entry_name == NULL) {
1571 err = got_error_from_errno2("basename", path);
1576 relpath = strdup("");
1577 if (relpath == NULL) {
1578 err = got_error_from_errno("strdup");
1581 err = got_object_id_by_path(&tree_id, repo,
1582 worktree->base_commit_id, worktree->path_prefix);
1587 err = got_object_open_as_tree(&tree, repo, tree_id);
1592 got_object_tree_find_entry(tree, entry_name) == NULL) {
1593 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1597 diff_cb.diff_old_new = diff_old_new;
1598 diff_cb.diff_old = diff_old;
1599 diff_cb.diff_new = diff_new;
1600 arg.fileindex = fileindex;
1601 arg.worktree = worktree;
1603 arg.progress_cb = progress_cb;
1604 arg.progress_arg = progress_arg;
1605 arg.cancel_cb = cancel_cb;
1606 arg.cancel_arg = cancel_arg;
1607 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1608 entry_name, repo, &diff_cb, &arg);
1610 if (checkout_err == NULL) {
1611 struct bump_base_commit_id_arg bbc_arg;
1612 bbc_arg.base_commit_id = worktree->base_commit_id;
1613 bbc_arg.entry_name = entry_name;
1614 bbc_arg.path = path;
1615 bbc_arg.path_len = strlen(path);
1616 err = got_fileindex_for_each_entry_safe(fileindex,
1617 bump_base_commit_id, &bbc_arg);
1622 /* Try to sync the fileindex back to disk in any case. */
1623 err = got_fileindex_write(fileindex, new_index);
1627 if (rename(new_fileindex_path, fileindex_path) != 0) {
1628 err = got_error_from_errno3("rename", new_fileindex_path,
1630 unlink(new_fileindex_path);
1634 free(new_fileindex_path);
1635 new_fileindex_path = NULL;
1640 got_object_tree_close(tree);
1642 got_object_commit_close(commit);
1643 if (new_fileindex_path)
1644 unlink(new_fileindex_path);
1647 free(new_fileindex_path);
1648 free(fileindex_path);
1649 got_fileindex_free(fileindex);
1652 unlockerr = lock_worktree(worktree, LOCK_SH);
1653 if (unlockerr && err == NULL)
1658 struct diff_dir_cb_arg {
1659 struct got_fileindex *fileindex;
1660 struct got_worktree *worktree;
1661 const char *status_path;
1662 size_t status_path_len;
1663 struct got_repository *repo;
1664 got_worktree_status_cb status_cb;
1666 got_worktree_cancel_cb cancel_cb;
1670 static const struct got_error *
1671 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1672 got_worktree_status_cb status_cb, void *status_arg,
1673 struct got_repository *repo)
1675 const struct got_error *err = NULL;
1676 unsigned char status = GOT_STATUS_NO_CHANGE;
1678 struct got_object_id blob_id, commit_id;
1680 err = get_file_status(&status, &sb, ie, abspath, repo);
1681 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1682 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1683 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1684 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1690 static const struct got_error *
1691 status_old_new(void *arg, struct got_fileindex_entry *ie,
1692 struct dirent *de, const char *parent_path)
1694 const struct got_error *err = NULL;
1695 struct diff_dir_cb_arg *a = arg;
1698 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1699 return got_error(GOT_ERR_CANCELLED);
1701 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1702 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1705 if (parent_path[0]) {
1706 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1707 parent_path, de->d_name) == -1)
1708 return got_error_from_errno("asprintf");
1710 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1712 return got_error_from_errno("asprintf");
1715 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1721 static const struct got_error *
1722 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1724 struct diff_dir_cb_arg *a = arg;
1725 struct got_object_id blob_id, commit_id;
1726 unsigned char status;
1728 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1729 return got_error(GOT_ERR_CANCELLED);
1731 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1734 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1735 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1736 if (got_fileindex_entry_has_file_on_disk(ie))
1737 status = GOT_STATUS_MISSING;
1739 status = GOT_STATUS_DELETE;
1740 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1744 static const struct got_error *
1745 status_new(void *arg, struct dirent *de, const char *parent_path)
1747 const struct got_error *err = NULL;
1748 struct diff_dir_cb_arg *a = arg;
1751 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1752 return got_error(GOT_ERR_CANCELLED);
1754 if (de->d_type == DT_DIR)
1757 /* XXX ignore symlinks for now */
1758 if (de->d_type == DT_LNK)
1761 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1764 if (parent_path[0]) {
1765 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1766 return got_error_from_errno("asprintf");
1771 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1778 const struct got_error *
1779 got_worktree_status(struct got_worktree *worktree, const char *path,
1780 struct got_repository *repo, got_worktree_status_cb status_cb,
1781 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1783 const struct got_error *err = NULL;
1784 DIR *workdir = NULL;
1785 char *fileindex_path = NULL;
1786 struct got_fileindex *fileindex = NULL;
1788 struct got_fileindex_diff_dir_cb fdiff_cb;
1789 struct diff_dir_cb_arg arg;
1790 char *ondisk_path = NULL;
1792 fileindex = got_fileindex_alloc();
1793 if (fileindex == NULL) {
1794 err = got_error_from_errno("got_fileindex_alloc");
1798 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1799 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1800 err = got_error_from_errno("asprintf");
1801 fileindex_path = NULL;
1805 index = fopen(fileindex_path, "rb");
1806 if (index == NULL) {
1807 if (errno != ENOENT) {
1808 err = got_error_from_errno2("fopen", fileindex_path);
1812 err = got_fileindex_read(fileindex, index);
1818 if (asprintf(&ondisk_path, "%s%s%s",
1819 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1820 err = got_error_from_errno("asprintf");
1823 workdir = opendir(ondisk_path);
1824 if (workdir == NULL) {
1825 if (errno == ENOTDIR || errno == ENOENT) {
1826 struct got_fileindex_entry *ie;
1827 ie = got_fileindex_entry_get(fileindex, path);
1829 err = got_error(GOT_ERR_BAD_PATH);
1832 err = report_file_status(ie, ondisk_path,
1833 status_cb, status_arg, repo);
1836 err = got_error_from_errno2("opendir", ondisk_path);
1840 fdiff_cb.diff_old_new = status_old_new;
1841 fdiff_cb.diff_old = status_old;
1842 fdiff_cb.diff_new = status_new;
1843 arg.fileindex = fileindex;
1844 arg.worktree = worktree;
1845 arg.status_path = path;
1846 arg.status_path_len = strlen(path);
1848 arg.status_cb = status_cb;
1849 arg.status_arg = status_arg;
1850 arg.cancel_cb = cancel_cb;
1851 arg.cancel_arg = cancel_arg;
1852 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1853 path, repo, &fdiff_cb, &arg);
1858 free(fileindex_path);
1859 got_fileindex_free(fileindex);
1863 const struct got_error *
1864 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1867 const struct got_error *err = NULL;
1868 char *resolved, *path = NULL;
1873 resolved = realpath(arg, NULL);
1874 if (resolved == NULL)
1875 return got_error_from_errno2("realpath", arg);
1877 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1878 strlen(got_worktree_get_root_path(worktree)))) {
1879 err = got_error(GOT_ERR_BAD_PATH);
1883 path = strdup(resolved +
1884 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1886 err = got_error_from_errno("strdup");
1890 /* XXX status walk can't deal with trailing slash! */
1892 while (path[len - 1] == '/') {
1893 path[len - 1] = '\0';
1905 const struct got_error *
1906 got_worktree_schedule_add(struct got_worktree *worktree,
1907 struct got_pathlist_head *ondisk_paths,
1908 got_worktree_status_cb status_cb, void *status_arg,
1909 struct got_repository *repo)
1911 struct got_fileindex *fileindex = NULL;
1912 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1913 FILE *index = NULL, *new_index = NULL;
1914 const struct got_error *err = NULL, *unlockerr = NULL;
1915 struct got_pathlist_entry *pe;
1917 err = lock_worktree(worktree, LOCK_EX);
1922 fileindex = got_fileindex_alloc();
1923 if (fileindex == NULL) {
1924 err = got_error_from_errno("got_fileindex_alloc");
1928 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1929 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1930 err = got_error_from_errno("asprintf");
1931 fileindex_path = NULL;
1935 index = fopen(fileindex_path, "rb");
1936 if (index == NULL) {
1937 err = got_error_from_errno2("fopen", fileindex_path);
1941 err = got_fileindex_read(fileindex, index);
1945 TAILQ_FOREACH(pe, ondisk_paths, entry) {
1946 struct got_fileindex_entry *ie = NULL;
1949 err = got_path_skip_common_ancestor(&relpath,
1950 got_worktree_get_root_path(worktree), pe->path);
1954 /* Re-adding an existing entry is a no-op. */
1955 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1958 err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1964 err = got_fileindex_entry_add(fileindex, ie);
1966 got_fileindex_entry_free(ie);
1970 err = report_file_status(ie, pe->path, status_cb, status_arg,
1976 err = got_opentemp_named(&new_fileindex_path, &new_index,
1981 err = got_fileindex_write(fileindex, new_index);
1985 if (rename(new_fileindex_path, fileindex_path) != 0) {
1986 err = got_error_from_errno3("rename", new_fileindex_path,
1991 free(new_fileindex_path);
1992 new_fileindex_path = NULL;
1996 if (fclose(index) != 0 && err == NULL)
1997 err = got_error_from_errno("fclose");
1999 if (new_fileindex_path) {
2000 if (unlink(new_fileindex_path) != 0 && err == NULL)
2001 err = got_error_from_errno2("unlink",
2002 new_fileindex_path);
2003 free(new_fileindex_path);
2006 got_fileindex_free(fileindex);
2007 unlockerr = lock_worktree(worktree, LOCK_SH);
2008 if (unlockerr && err == NULL)
2013 const struct got_error *
2014 got_worktree_schedule_delete(struct got_worktree *worktree,
2015 const char *ondisk_path, int delete_local_mods,
2016 got_worktree_status_cb status_cb, void *status_arg,
2017 struct got_repository *repo)
2019 struct got_fileindex *fileindex = NULL;
2020 struct got_fileindex_entry *ie = NULL;
2021 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2022 FILE *index = NULL, *new_index = NULL;
2023 const struct got_error *err = NULL, *unlockerr = NULL;
2024 unsigned char status;
2027 err = lock_worktree(worktree, LOCK_EX);
2031 err = got_path_skip_common_ancestor(&relpath,
2032 got_worktree_get_root_path(worktree), ondisk_path);
2036 fileindex = got_fileindex_alloc();
2037 if (fileindex == NULL) {
2038 err = got_error_from_errno("got_fileindex_alloc");
2042 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2043 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2044 err = got_error_from_errno("asprintf");
2045 fileindex_path = NULL;
2049 index = fopen(fileindex_path, "rb");
2050 if (index == NULL) {
2051 err = got_error_from_errno2("fopen", fileindex_path);
2055 err = got_fileindex_read(fileindex, index);
2059 ie = got_fileindex_entry_get(fileindex, relpath);
2061 err = got_error(GOT_ERR_BAD_PATH);
2065 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2069 if (status != GOT_STATUS_NO_CHANGE) {
2070 if (status == GOT_STATUS_DELETE) {
2071 err = got_error_set_errno(ENOENT, ondisk_path);
2074 if (status != GOT_STATUS_MODIFY) {
2075 err = got_error(GOT_ERR_FILE_STATUS);
2078 if (!delete_local_mods) {
2079 err = got_error(GOT_ERR_FILE_MODIFIED);
2084 if (unlink(ondisk_path) != 0) {
2085 err = got_error_from_errno2("unlink", ondisk_path);
2089 got_fileindex_entry_mark_deleted_from_disk(ie);
2091 err = got_opentemp_named(&new_fileindex_path, &new_index,
2096 err = got_fileindex_write(fileindex, new_index);
2100 if (rename(new_fileindex_path, fileindex_path) != 0) {
2101 err = got_error_from_errno3("rename", new_fileindex_path,
2106 free(new_fileindex_path);
2107 new_fileindex_path = NULL;
2109 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2113 if (fclose(index) != 0 && err == NULL)
2114 err = got_error_from_errno("fclose");
2116 if (new_fileindex_path) {
2117 if (unlink(new_fileindex_path) != 0 && err == NULL)
2118 err = got_error_from_errno2("unlink",
2119 new_fileindex_path);
2120 free(new_fileindex_path);
2123 got_fileindex_free(fileindex);
2124 unlockerr = lock_worktree(worktree, LOCK_SH);
2125 if (unlockerr && err == NULL)
2130 const struct got_error *
2131 got_worktree_revert(struct got_worktree *worktree,
2132 const char *ondisk_path,
2133 got_worktree_checkout_cb progress_cb, void *progress_arg,
2134 struct got_repository *repo)
2136 struct got_fileindex *fileindex = NULL;
2137 struct got_fileindex_entry *ie = NULL;
2138 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2139 char *tree_path = NULL, *parent_path, *te_name;
2140 FILE *index = NULL, *new_index = NULL;
2141 const struct got_error *err = NULL, *unlockerr = NULL;
2142 struct got_tree_object *tree = NULL;
2143 struct got_object_id id, *tree_id = NULL;
2144 const struct got_tree_entry *te;
2145 struct got_blob_object *blob = NULL;
2146 unsigned char status;
2149 err = lock_worktree(worktree, LOCK_EX);
2153 err = got_path_skip_common_ancestor(&relpath,
2154 got_worktree_get_root_path(worktree), ondisk_path);
2158 fileindex = got_fileindex_alloc();
2159 if (fileindex == NULL) {
2160 err = got_error_from_errno("got_fileindex_alloc");
2164 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2165 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2166 err = got_error_from_errno("asprintf");
2167 fileindex_path = NULL;
2171 index = fopen(fileindex_path, "rb");
2172 if (index == NULL) {
2173 err = got_error_from_errno2("fopen", fileindex_path);
2177 err = got_fileindex_read(fileindex, index);
2181 ie = got_fileindex_entry_get(fileindex, relpath);
2183 err = got_error(GOT_ERR_BAD_PATH);
2187 /* Construct in-repository path of tree which contains this blob. */
2188 err = got_path_dirname(&parent_path, ie->path);
2190 if (err->code != GOT_ERR_BAD_PATH)
2194 if (got_path_is_root_dir(worktree->path_prefix)) {
2195 tree_path = strdup(parent_path);
2196 if (tree_path == NULL) {
2197 err = got_error_from_errno("strdup");
2201 if (got_path_is_root_dir(parent_path)) {
2202 tree_path = strdup(worktree->path_prefix);
2203 if (tree_path == NULL) {
2204 err = got_error_from_errno("strdup");
2208 if (asprintf(&tree_path, "%s/%s",
2209 worktree->path_prefix, parent_path) == -1) {
2210 err = got_error_from_errno("asprintf");
2216 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2221 err = got_object_open_as_tree(&tree, repo, tree_id);
2225 te_name = basename(ie->path);
2226 if (te_name == NULL) {
2227 err = got_error_from_errno2("basename", ie->path);
2231 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2235 te = got_object_tree_find_entry(tree, te_name);
2236 if (te == NULL && status != GOT_STATUS_ADD) {
2237 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2242 case GOT_STATUS_ADD:
2243 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2244 got_fileindex_entry_remove(fileindex, ie);
2246 case GOT_STATUS_DELETE:
2247 case GOT_STATUS_MODIFY:
2248 case GOT_STATUS_CONFLICT:
2249 case GOT_STATUS_MISSING:
2250 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2251 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2254 err = install_blob(worktree, ondisk_path, ie->path,
2255 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2259 if (status == GOT_STATUS_DELETE) {
2260 err = update_blob_fileindex_entry(worktree,
2261 fileindex, ie, ondisk_path, ie->path, blob, 1);
2270 err = got_opentemp_named(&new_fileindex_path, &new_index,
2275 err = got_fileindex_write(fileindex, new_index);
2279 if (rename(new_fileindex_path, fileindex_path) != 0) {
2280 err = got_error_from_errno3("rename", new_fileindex_path,
2285 free(new_fileindex_path);
2286 new_fileindex_path = NULL;
2291 got_object_blob_close(blob);
2293 got_object_tree_close(tree);
2296 if (fclose(index) != 0 && err == NULL)
2297 err = got_error_from_errno("fclose");
2299 if (new_fileindex_path) {
2300 if (unlink(new_fileindex_path) != 0 && err == NULL)
2301 err = got_error_from_errno2("unlink",
2302 new_fileindex_path);
2303 free(new_fileindex_path);
2306 got_fileindex_free(fileindex);
2307 unlockerr = lock_worktree(worktree, LOCK_SH);
2308 if (unlockerr && err == NULL)
2314 free_commitable(struct got_commitable *ct)
2317 free(ct->in_repo_path);
2318 free(ct->ondisk_path);
2320 free(ct->base_blob_id);
2321 free(ct->base_commit_id);
2325 struct collect_commitables_arg {
2326 struct got_pathlist_head *commitable_paths;
2327 struct got_repository *repo;
2328 struct got_worktree *worktree;
2331 static const struct got_error *
2332 collect_commitables(void *arg, unsigned char status, const char *relpath,
2333 struct got_object_id *blob_id, struct got_object_id *commit_id)
2335 struct collect_commitables_arg *a = arg;
2336 const struct got_error *err = NULL;
2337 struct got_commitable *ct = NULL;
2338 struct got_pathlist_entry *new = NULL;
2339 char *parent_path = NULL, *path = NULL;
2342 if (status == GOT_STATUS_CONFLICT)
2343 return got_error(GOT_ERR_COMMIT_CONFLICT);
2345 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2346 status != GOT_STATUS_DELETE)
2349 if (asprintf(&path, "/%s", relpath) == -1) {
2350 err = got_error_from_errno("asprintf");
2353 if (strcmp(path, "/") == 0) {
2354 parent_path = strdup("");
2355 if (parent_path == NULL)
2356 return got_error_from_errno("strdup");
2358 err = got_path_dirname(&parent_path, path);
2363 ct = calloc(1, sizeof(*ct));
2365 err = got_error_from_errno("calloc");
2369 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2371 err = got_error_from_errno("asprintf");
2374 if (status == GOT_STATUS_DELETE) {
2375 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2377 if (lstat(ct->ondisk_path, &sb) != 0) {
2378 err = got_error_from_errno2("lstat", ct->ondisk_path);
2381 ct->mode = sb.st_mode;
2384 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2385 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2387 err = got_error_from_errno("asprintf");
2391 ct->status = status;
2392 ct->blob_id = NULL; /* will be filled in when blob gets created */
2393 if (ct->status != GOT_STATUS_ADD) {
2394 ct->base_blob_id = got_object_id_dup(blob_id);
2395 if (ct->base_blob_id == NULL) {
2396 err = got_error_from_errno("got_object_id_dup");
2399 ct->base_commit_id = got_object_id_dup(commit_id);
2400 if (ct->base_commit_id == NULL) {
2401 err = got_error_from_errno("got_object_id_dup");
2405 ct->path = strdup(path);
2406 if (ct->path == NULL) {
2407 err = got_error_from_errno("strdup");
2410 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2412 if (ct && (err || new == NULL))
2413 free_commitable(ct);
2419 static const struct got_error *write_tree(struct got_object_id **,
2420 struct got_tree_object *, const char *, struct got_pathlist_head *,
2421 got_worktree_status_cb status_cb, void *status_arg,
2422 struct got_repository *);
2424 static const struct got_error *
2425 write_subtree(struct got_object_id **new_subtree_id,
2426 struct got_tree_entry *te, const char *parent_path,
2427 struct got_pathlist_head *commitable_paths,
2428 got_worktree_status_cb status_cb, void *status_arg,
2429 struct got_repository *repo)
2431 const struct got_error *err = NULL;
2432 struct got_tree_object *subtree;
2435 if (asprintf(&subpath, "%s%s%s", parent_path,
2436 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2437 return got_error_from_errno("asprintf");
2439 err = got_object_open_as_tree(&subtree, repo, te->id);
2443 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2444 status_cb, status_arg, repo);
2445 got_object_tree_close(subtree);
2450 static const struct got_error *
2451 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2453 const struct got_error *err = NULL;
2454 char *ct_parent_path = NULL;
2458 if (strchr(ct->path, '/') == NULL) {
2459 *match = got_path_is_root_dir(path);
2463 err = got_path_dirname(&ct_parent_path, ct->path);
2466 *match = (strcmp(path, ct_parent_path) == 0);
2467 free(ct_parent_path);
2472 get_ct_file_mode(struct got_commitable *ct)
2474 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2477 static const struct got_error *
2478 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2479 struct got_tree_entry *te, struct got_commitable *ct)
2481 const struct got_error *err = NULL;
2485 err = got_object_tree_entry_dup(new_te, te);
2489 (*new_te)->mode = get_ct_file_mode(ct);
2491 free((*new_te)->id);
2492 (*new_te)->id = got_object_id_dup(ct->blob_id);
2493 if ((*new_te)->id == NULL) {
2494 err = got_error_from_errno("got_object_id_dup");
2498 if (err && *new_te) {
2499 got_object_tree_entry_close(*new_te);
2505 static const struct got_error *
2506 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2507 struct got_commitable *ct)
2509 const struct got_error *err = NULL;
2514 *new_te = calloc(1, sizeof(**new_te));
2515 if (*new_te == NULL)
2516 return got_error_from_errno("calloc");
2518 ct_name = basename(ct->path);
2519 if (ct_name == NULL) {
2520 err = got_error_from_errno2("basename", ct->path);
2523 (*new_te)->name = strdup(ct_name);
2524 if ((*new_te)->name == NULL) {
2525 err = got_error_from_errno("strdup");
2529 (*new_te)->mode = get_ct_file_mode(ct);
2531 (*new_te)->id = got_object_id_dup(ct->blob_id);
2532 if ((*new_te)->id == NULL) {
2533 err = got_error_from_errno("got_object_id_dup");
2537 if (err && *new_te) {
2538 got_object_tree_entry_close(*new_te);
2544 static const struct got_error *
2545 insert_tree_entry(struct got_tree_entry *new_te,
2546 struct got_pathlist_head *paths)
2548 const struct got_error *err = NULL;
2549 struct got_pathlist_entry *new_pe;
2551 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2555 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2559 static const struct got_error *
2560 report_ct_status(struct got_commitable *ct,
2561 got_worktree_status_cb status_cb, void *status_arg)
2563 const char *ct_path = ct->path;
2564 while (ct_path[0] == '/')
2566 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2569 static const struct got_error *
2570 match_modified_subtree(int *modified, struct got_tree_entry *te,
2571 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2573 const struct got_error *err = NULL;
2574 struct got_pathlist_entry *pe;
2579 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2580 got_path_is_root_dir(base_tree_path) ? "" : "/",
2582 return got_error_from_errno("asprintf");
2584 TAILQ_FOREACH(pe, commitable_paths, entry) {
2585 struct got_commitable *ct = pe->data;
2586 *modified = got_path_is_child(ct->in_repo_path, te_path,
2596 static const struct got_error *
2597 match_deleted_or_modified_ct(struct got_commitable **ctp,
2598 struct got_tree_entry *te, const char *base_tree_path,
2599 struct got_pathlist_head *commitable_paths)
2601 const struct got_error *err = NULL;
2602 struct got_pathlist_entry *pe;
2606 TAILQ_FOREACH(pe, commitable_paths, entry) {
2607 struct got_commitable *ct = pe->data;
2608 char *ct_name = NULL;
2611 if (ct->status != GOT_STATUS_MODIFY &&
2612 ct->status != GOT_STATUS_DELETE)
2615 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2618 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2624 ct_name = basename(pe->path);
2625 if (ct_name == NULL)
2626 return got_error_from_errno2("basename", pe->path);
2628 if (strcmp(te->name, ct_name) != 0)
2638 static const struct got_error *
2639 write_tree(struct got_object_id **new_tree_id,
2640 struct got_tree_object *base_tree, const char *path_base_tree,
2641 struct got_pathlist_head *commitable_paths,
2642 got_worktree_status_cb status_cb, void *status_arg,
2643 struct got_repository *repo)
2645 const struct got_error *err = NULL;
2646 const struct got_tree_entries *base_entries = NULL;
2647 struct got_pathlist_head paths;
2648 struct got_tree_entries new_tree_entries;
2649 struct got_tree_entry *te, *new_te = NULL;
2650 struct got_pathlist_entry *pe;
2653 new_tree_entries.nentries = 0;
2654 SIMPLEQ_INIT(&new_tree_entries.head);
2656 /* Insert, and recurse into, newly added entries first. */
2657 TAILQ_FOREACH(pe, commitable_paths, entry) {
2658 struct got_commitable *ct = pe->data;
2659 char *child_path = NULL, *slash;
2661 if (ct->status != GOT_STATUS_ADD ||
2662 (ct->flags & GOT_COMMITABLE_ADDED))
2665 if (!got_path_is_child(pe->path, path_base_tree,
2666 strlen(path_base_tree)))
2669 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2674 slash = strchr(child_path, '/');
2675 if (slash == NULL) {
2676 err = alloc_added_blob_tree_entry(&new_te, ct);
2679 err = report_ct_status(ct, status_cb, status_arg);
2682 ct->flags |= GOT_COMMITABLE_ADDED;
2686 *slash = '\0'; /* trim trailing path components */
2687 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2688 got_path_is_root_dir(path_base_tree) ? "" : "/",
2689 child_path) == -1) {
2690 err = got_error_from_errno("asprintf");
2694 new_te = calloc(1, sizeof(*new_te));
2695 new_te->mode = S_IFDIR;
2696 new_te->name = strdup(child_path);
2697 if (new_te->name == NULL) {
2698 err = got_error_from_errno("strdup");
2699 got_object_tree_entry_close(new_te);
2703 err = write_tree(&new_te->id, NULL, subtree_path,
2704 commitable_paths, status_cb, status_arg, repo);
2707 got_object_tree_entry_close(new_te);
2712 err = insert_tree_entry(new_te, &paths);
2718 /* Handle modified and deleted entries. */
2719 base_entries = got_object_tree_get_entries(base_tree);
2720 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2721 struct got_commitable *ct = NULL;
2723 if (S_ISDIR(te->mode)) {
2725 err = got_object_tree_entry_dup(&new_te, te);
2728 err = match_modified_subtree(&modified, te,
2729 path_base_tree, commitable_paths);
2732 /* Avoid recursion into unmodified subtrees. */
2735 err = write_subtree(&new_te->id, te,
2736 path_base_tree, commitable_paths,
2737 status_cb, status_arg, repo);
2741 err = insert_tree_entry(new_te, &paths);
2747 err = match_deleted_or_modified_ct(&ct, te,
2748 path_base_tree, commitable_paths);
2750 /* NB: Deleted entries get dropped here. */
2751 if (ct->status == GOT_STATUS_MODIFY) {
2752 err = alloc_modified_blob_tree_entry(
2756 err = insert_tree_entry(new_te, &paths);
2760 err = report_ct_status(ct, status_cb,
2765 /* Entry is unchanged; just copy it. */
2766 err = got_object_tree_entry_dup(&new_te, te);
2769 err = insert_tree_entry(new_te, &paths);
2776 /* Write new list of entries; deleted entries have been dropped. */
2777 TAILQ_FOREACH(pe, &paths, entry) {
2778 struct got_tree_entry *te = pe->data;
2779 new_tree_entries.nentries++;
2780 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2782 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2784 got_object_tree_entries_close(&new_tree_entries);
2785 got_pathlist_free(&paths);
2789 static const struct got_error *
2790 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2791 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2793 const struct got_error *err = NULL;
2794 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2795 struct got_fileindex *fileindex = NULL;
2796 FILE *new_index = NULL;
2797 struct got_pathlist_entry *pe;
2799 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2803 err = got_opentemp_named(&new_fileindex_path, &new_index,
2808 TAILQ_FOREACH(pe, commitable_paths, entry) {
2809 struct got_fileindex_entry *ie;
2810 struct got_commitable *ct = pe->data;
2812 ie = got_fileindex_entry_get(fileindex, pe->path);
2814 if (ct->status == GOT_STATUS_DELETE) {
2815 got_fileindex_entry_remove(fileindex, ie);
2816 got_fileindex_entry_free(ie);
2818 err = got_fileindex_entry_update(ie,
2819 ct->ondisk_path, ct->blob_id->sha1,
2820 new_base_commit_id->sha1, 1);
2822 err = got_fileindex_entry_alloc(&ie,
2823 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2824 new_base_commit_id->sha1);
2827 err = got_fileindex_entry_add(fileindex, ie);
2833 err = got_fileindex_write(fileindex, new_index);
2837 if (rename(new_fileindex_path, fileindex_path) != 0) {
2838 err = got_error_from_errno3("rename", new_fileindex_path,
2840 unlink(new_fileindex_path);
2844 free(new_fileindex_path);
2845 new_fileindex_path = NULL;
2848 if (new_fileindex_path)
2849 unlink(new_fileindex_path);
2852 free(new_fileindex_path);
2853 free(fileindex_path);
2854 got_fileindex_free(fileindex);
2858 static const struct got_error *
2859 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
2860 struct got_object_id *head_commit_id)
2862 const struct got_error *err = NULL;
2863 struct got_object_id *id_in_head;
2866 * Require that modified/deleted files are based on the branch head.
2867 * This requirement could be relaxed to force less update operations
2868 * on users but, for now, we want to play it safe and see how it goes.
2870 * If this check is relaxed, it must still ensure that no modifications
2871 * were made to files *and their parents* in commits between the file's
2872 * base commit and the branch head. Otherwise, tree conflicts will not
2873 * be detected reliably.
2875 if (ct->status != GOT_STATUS_ADD) {
2876 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
2877 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2881 /* Require that added files don't exist in the branch head. */
2882 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
2884 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2888 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2895 const struct got_error *
2896 got_worktree_commit(struct got_object_id **new_commit_id,
2897 struct got_worktree *worktree, const char *ondisk_path,
2898 const char *author, const char *committer,
2899 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2900 got_worktree_status_cb status_cb, void *status_arg,
2901 struct got_repository *repo)
2903 const struct got_error *err = NULL, *unlockerr = NULL;
2904 struct collect_commitables_arg cc_arg;
2905 struct got_pathlist_head commitable_paths;
2906 struct got_pathlist_entry *pe;
2907 char *relpath = NULL;
2908 const char *head_ref_name = NULL;
2909 struct got_reference *head_ref = NULL;
2910 struct got_commit_object *head_commit = NULL;
2911 struct got_object_id *head_commit_id = NULL;
2912 struct got_reference *head_ref2 = NULL;
2913 struct got_object_id *head_commit_id2 = NULL;
2914 struct got_tree_object *head_tree = NULL;
2915 struct got_object_id *new_tree_id = NULL;
2916 struct got_object_id_queue parent_ids;
2917 struct got_object_qid *pid = NULL;
2918 char *logmsg = NULL;
2920 *new_commit_id = NULL;
2922 TAILQ_INIT(&commitable_paths);
2923 SIMPLEQ_INIT(&parent_ids);
2926 err = got_path_skip_common_ancestor(&relpath,
2927 worktree->root_path, ondisk_path);
2932 err = lock_worktree(worktree, LOCK_EX);
2936 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
2939 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2943 cc_arg.commitable_paths = &commitable_paths;
2944 cc_arg.worktree = worktree;
2946 err = got_worktree_status(worktree, relpath ? relpath : "",
2947 repo, collect_commitables, &cc_arg, NULL, NULL);
2951 if (TAILQ_EMPTY(&commitable_paths)) {
2952 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
2956 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2960 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2961 struct got_commitable *ct = pe->data;
2962 err = check_ct_out_of_date(ct, repo, head_commit_id);
2967 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2971 if (commit_msg_cb != NULL) {
2972 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
2977 if (logmsg == NULL || strlen(logmsg) == 0) {
2978 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2982 /* Create blobs from added and modified files and record their IDs. */
2983 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2984 struct got_commitable *ct = pe->data;
2987 if (ct->status != GOT_STATUS_ADD &&
2988 ct->status != GOT_STATUS_MODIFY)
2991 if (asprintf(&ondisk_path, "%s/%s",
2992 worktree->root_path, pe->path) == -1) {
2993 err = got_error_from_errno("asprintf");
2996 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3002 /* Recursively write new tree objects. */
3003 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3004 status_cb, status_arg, repo);
3008 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3011 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3012 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3013 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3014 got_object_qid_free(pid);
3020 /* Check if a concurrent commit to our branch has occurred. */
3021 head_ref_name = got_worktree_get_head_ref_name(worktree);
3022 if (head_ref_name == NULL) {
3023 err = got_error_from_errno("got_worktree_get_head_ref_name");
3026 /* Lock the reference here to prevent concurrent modification. */
3027 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3030 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3033 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3034 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3037 /* Update branch head in repository. */
3038 err = got_ref_change_ref(head_ref2, *new_commit_id);
3041 err = got_ref_write(head_ref2, repo);
3045 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3049 err = ref_base_commit(worktree, repo);
3053 err = update_fileindex_after_commit(&commitable_paths,
3054 *new_commit_id, worktree);
3058 unlockerr = lock_worktree(worktree, LOCK_SH);
3059 if (unlockerr && err == NULL)
3061 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3062 struct got_commitable *ct = pe->data;
3063 free_commitable(ct);
3065 got_pathlist_free(&commitable_paths);
3067 got_object_tree_close(head_tree);
3069 got_object_commit_close(head_commit);
3071 free(head_commit_id);
3072 free(head_commit_id2);
3074 got_ref_close(head_ref);
3076 unlockerr = got_ref_unlock(head_ref2);
3077 if (unlockerr && err == NULL)
3079 got_ref_close(head_ref2);
3085 got_commitable_get_path(struct got_commitable *ct)
3091 got_commitable_get_status(struct got_commitable *ct)