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 const struct got_error *
1451 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1452 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1453 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1455 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1456 struct got_commit_object *commit = NULL;
1457 struct got_object_id *tree_id = NULL;
1458 struct got_tree_object *tree = NULL;
1459 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1460 struct got_fileindex *fileindex = NULL;
1461 FILE *new_index = NULL;
1462 struct got_fileindex_diff_tree_cb diff_cb;
1463 struct diff_cb_arg arg;
1464 char *relpath = NULL, *entry_name = NULL;
1466 err = lock_worktree(worktree, LOCK_EX);
1471 * Read the file index.
1472 * Checking out files is supposed to be an idempotent operation.
1473 * If the on-disk file index is incomplete we will try to complete it.
1475 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1479 err = got_opentemp_named(&new_fileindex_path, &new_index,
1484 err = ref_base_commit(worktree, repo);
1488 err = got_object_open_as_commit(&commit, repo,
1489 worktree->base_commit_id);
1496 relpath = strdup(path);
1497 if (relpath == NULL) {
1498 err = got_error_from_errno("strdup");
1501 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1502 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1504 err = got_error_from_errno("asprintf");
1507 err = got_object_id_by_path(&tree_id, repo,
1508 worktree->base_commit_id, tree_path);
1512 err = got_object_get_type(&obj_type, repo, tree_id);
1515 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1516 /* Split provided path into parent dir + entry name. */
1517 if (strchr(path, '/') == NULL) {
1518 relpath = strdup("");
1519 if (relpath == NULL) {
1520 err = got_error_from_errno("strdup");
1523 tree_path = strdup(worktree->path_prefix);
1524 if (tree_path == NULL) {
1525 err = got_error_from_errno("strdup");
1529 err = got_path_dirname(&relpath, path);
1532 if (asprintf(&tree_path, "%s%s%s",
1533 worktree->path_prefix,
1534 got_path_is_root_dir(
1535 worktree->path_prefix) ? "" : "/",
1537 err = got_error_from_errno("asprintf");
1541 err = got_object_id_by_path(&tree_id, repo,
1542 worktree->base_commit_id, tree_path);
1546 entry_name = basename(path);
1547 if (entry_name == NULL) {
1548 err = got_error_from_errno2("basename", path);
1553 relpath = strdup("");
1554 if (relpath == NULL) {
1555 err = got_error_from_errno("strdup");
1558 err = got_object_id_by_path(&tree_id, repo,
1559 worktree->base_commit_id, worktree->path_prefix);
1564 err = got_object_open_as_tree(&tree, repo, tree_id);
1569 got_object_tree_find_entry(tree, entry_name) == NULL) {
1570 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1574 diff_cb.diff_old_new = diff_old_new;
1575 diff_cb.diff_old = diff_old;
1576 diff_cb.diff_new = diff_new;
1577 arg.fileindex = fileindex;
1578 arg.worktree = worktree;
1580 arg.progress_cb = progress_cb;
1581 arg.progress_arg = progress_arg;
1582 arg.cancel_cb = cancel_cb;
1583 arg.cancel_arg = cancel_arg;
1584 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1585 entry_name, repo, &diff_cb, &arg);
1587 /* Try to sync the fileindex back to disk in any case. */
1588 err = got_fileindex_write(fileindex, new_index);
1592 if (rename(new_fileindex_path, fileindex_path) != 0) {
1593 err = got_error_from_errno3("rename", new_fileindex_path,
1595 unlink(new_fileindex_path);
1599 free(new_fileindex_path);
1600 new_fileindex_path = NULL;
1605 got_object_tree_close(tree);
1607 got_object_commit_close(commit);
1608 if (new_fileindex_path)
1609 unlink(new_fileindex_path);
1612 free(new_fileindex_path);
1613 free(fileindex_path);
1614 got_fileindex_free(fileindex);
1617 unlockerr = lock_worktree(worktree, LOCK_SH);
1618 if (unlockerr && err == NULL)
1623 struct diff_dir_cb_arg {
1624 struct got_fileindex *fileindex;
1625 struct got_worktree *worktree;
1626 const char *status_path;
1627 size_t status_path_len;
1628 struct got_repository *repo;
1629 got_worktree_status_cb status_cb;
1631 got_worktree_cancel_cb cancel_cb;
1635 static const struct got_error *
1636 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1637 got_worktree_status_cb status_cb, void *status_arg,
1638 struct got_repository *repo)
1640 const struct got_error *err = NULL;
1641 unsigned char status = GOT_STATUS_NO_CHANGE;
1643 struct got_object_id blob_id, commit_id;
1645 err = get_file_status(&status, &sb, ie, abspath, repo);
1646 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1647 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1648 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1649 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1655 static const struct got_error *
1656 status_old_new(void *arg, struct got_fileindex_entry *ie,
1657 struct dirent *de, const char *parent_path)
1659 const struct got_error *err = NULL;
1660 struct diff_dir_cb_arg *a = arg;
1663 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1664 return got_error(GOT_ERR_CANCELLED);
1666 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1667 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1670 if (parent_path[0]) {
1671 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1672 parent_path, de->d_name) == -1)
1673 return got_error_from_errno("asprintf");
1675 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1677 return got_error_from_errno("asprintf");
1680 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1686 static const struct got_error *
1687 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1689 struct diff_dir_cb_arg *a = arg;
1690 struct got_object_id blob_id, commit_id;
1691 unsigned char status;
1693 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1694 return got_error(GOT_ERR_CANCELLED);
1696 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1699 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1700 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1701 if (got_fileindex_entry_has_file_on_disk(ie))
1702 status = GOT_STATUS_MISSING;
1704 status = GOT_STATUS_DELETE;
1705 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1709 static const struct got_error *
1710 status_new(void *arg, struct dirent *de, const char *parent_path)
1712 const struct got_error *err = NULL;
1713 struct diff_dir_cb_arg *a = arg;
1716 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1717 return got_error(GOT_ERR_CANCELLED);
1719 if (de->d_type == DT_DIR)
1722 /* XXX ignore symlinks for now */
1723 if (de->d_type == DT_LNK)
1726 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1729 if (parent_path[0]) {
1730 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1731 return got_error_from_errno("asprintf");
1736 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1743 const struct got_error *
1744 got_worktree_status(struct got_worktree *worktree, const char *path,
1745 struct got_repository *repo, got_worktree_status_cb status_cb,
1746 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1748 const struct got_error *err = NULL;
1749 DIR *workdir = NULL;
1750 char *fileindex_path = NULL;
1751 struct got_fileindex *fileindex = NULL;
1753 struct got_fileindex_diff_dir_cb fdiff_cb;
1754 struct diff_dir_cb_arg arg;
1755 char *ondisk_path = NULL;
1757 fileindex = got_fileindex_alloc();
1758 if (fileindex == NULL) {
1759 err = got_error_from_errno("got_fileindex_alloc");
1763 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1764 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1765 err = got_error_from_errno("asprintf");
1766 fileindex_path = NULL;
1770 index = fopen(fileindex_path, "rb");
1771 if (index == NULL) {
1772 if (errno != ENOENT) {
1773 err = got_error_from_errno2("fopen", fileindex_path);
1777 err = got_fileindex_read(fileindex, index);
1783 if (asprintf(&ondisk_path, "%s%s%s",
1784 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1785 err = got_error_from_errno("asprintf");
1788 workdir = opendir(ondisk_path);
1789 if (workdir == NULL) {
1790 if (errno == ENOTDIR || errno == ENOENT) {
1791 struct got_fileindex_entry *ie;
1792 ie = got_fileindex_entry_get(fileindex, path);
1794 err = got_error(GOT_ERR_BAD_PATH);
1797 err = report_file_status(ie, ondisk_path,
1798 status_cb, status_arg, repo);
1801 err = got_error_from_errno2("opendir", ondisk_path);
1805 fdiff_cb.diff_old_new = status_old_new;
1806 fdiff_cb.diff_old = status_old;
1807 fdiff_cb.diff_new = status_new;
1808 arg.fileindex = fileindex;
1809 arg.worktree = worktree;
1810 arg.status_path = path;
1811 arg.status_path_len = strlen(path);
1813 arg.status_cb = status_cb;
1814 arg.status_arg = status_arg;
1815 arg.cancel_cb = cancel_cb;
1816 arg.cancel_arg = cancel_arg;
1817 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1818 path, repo, &fdiff_cb, &arg);
1823 free(fileindex_path);
1824 got_fileindex_free(fileindex);
1828 const struct got_error *
1829 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1832 const struct got_error *err = NULL;
1833 char *resolved, *path = NULL;
1838 resolved = realpath(arg, NULL);
1839 if (resolved == NULL)
1840 return got_error_from_errno2("realpath", arg);
1842 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1843 strlen(got_worktree_get_root_path(worktree)))) {
1844 err = got_error(GOT_ERR_BAD_PATH);
1848 path = strdup(resolved +
1849 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1851 err = got_error_from_errno("strdup");
1855 /* XXX status walk can't deal with trailing slash! */
1857 while (path[len - 1] == '/') {
1858 path[len - 1] = '\0';
1870 const struct got_error *
1871 got_worktree_schedule_add(struct got_worktree *worktree,
1872 struct got_pathlist_head *ondisk_paths,
1873 got_worktree_status_cb status_cb, void *status_arg,
1874 struct got_repository *repo)
1876 struct got_fileindex *fileindex = NULL;
1877 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1878 FILE *index = NULL, *new_index = NULL;
1879 const struct got_error *err = NULL, *unlockerr = NULL;
1880 struct got_pathlist_entry *pe;
1882 err = lock_worktree(worktree, LOCK_EX);
1887 fileindex = got_fileindex_alloc();
1888 if (fileindex == NULL) {
1889 err = got_error_from_errno("got_fileindex_alloc");
1893 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1894 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1895 err = got_error_from_errno("asprintf");
1896 fileindex_path = NULL;
1900 index = fopen(fileindex_path, "rb");
1901 if (index == NULL) {
1902 err = got_error_from_errno2("fopen", fileindex_path);
1906 err = got_fileindex_read(fileindex, index);
1910 TAILQ_FOREACH(pe, ondisk_paths, entry) {
1911 struct got_fileindex_entry *ie = NULL;
1914 err = got_path_skip_common_ancestor(&relpath,
1915 got_worktree_get_root_path(worktree), pe->path);
1919 /* Re-adding an existing entry is a no-op. */
1920 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
1923 err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
1929 err = got_fileindex_entry_add(fileindex, ie);
1931 got_fileindex_entry_free(ie);
1935 err = report_file_status(ie, pe->path, status_cb, status_arg,
1941 err = got_opentemp_named(&new_fileindex_path, &new_index,
1946 err = got_fileindex_write(fileindex, new_index);
1950 if (rename(new_fileindex_path, fileindex_path) != 0) {
1951 err = got_error_from_errno3("rename", new_fileindex_path,
1956 free(new_fileindex_path);
1957 new_fileindex_path = NULL;
1961 if (fclose(index) != 0 && err == NULL)
1962 err = got_error_from_errno("fclose");
1964 if (new_fileindex_path) {
1965 if (unlink(new_fileindex_path) != 0 && err == NULL)
1966 err = got_error_from_errno2("unlink",
1967 new_fileindex_path);
1968 free(new_fileindex_path);
1971 got_fileindex_free(fileindex);
1972 unlockerr = lock_worktree(worktree, LOCK_SH);
1973 if (unlockerr && err == NULL)
1978 const struct got_error *
1979 got_worktree_schedule_delete(struct got_worktree *worktree,
1980 const char *ondisk_path, int delete_local_mods,
1981 got_worktree_status_cb status_cb, void *status_arg,
1982 struct got_repository *repo)
1984 struct got_fileindex *fileindex = NULL;
1985 struct got_fileindex_entry *ie = NULL;
1986 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1987 FILE *index = NULL, *new_index = NULL;
1988 const struct got_error *err = NULL, *unlockerr = NULL;
1989 unsigned char status;
1992 err = lock_worktree(worktree, LOCK_EX);
1996 err = got_path_skip_common_ancestor(&relpath,
1997 got_worktree_get_root_path(worktree), ondisk_path);
2001 fileindex = got_fileindex_alloc();
2002 if (fileindex == NULL) {
2003 err = got_error_from_errno("got_fileindex_alloc");
2007 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2008 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2009 err = got_error_from_errno("asprintf");
2010 fileindex_path = NULL;
2014 index = fopen(fileindex_path, "rb");
2015 if (index == NULL) {
2016 err = got_error_from_errno2("fopen", fileindex_path);
2020 err = got_fileindex_read(fileindex, index);
2024 ie = got_fileindex_entry_get(fileindex, relpath);
2026 err = got_error(GOT_ERR_BAD_PATH);
2030 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2034 if (status != GOT_STATUS_NO_CHANGE) {
2035 if (status == GOT_STATUS_DELETE) {
2036 err = got_error_set_errno(ENOENT, ondisk_path);
2039 if (status != GOT_STATUS_MODIFY) {
2040 err = got_error(GOT_ERR_FILE_STATUS);
2043 if (!delete_local_mods) {
2044 err = got_error(GOT_ERR_FILE_MODIFIED);
2049 if (unlink(ondisk_path) != 0) {
2050 err = got_error_from_errno2("unlink", ondisk_path);
2054 got_fileindex_entry_mark_deleted_from_disk(ie);
2056 err = got_opentemp_named(&new_fileindex_path, &new_index,
2061 err = got_fileindex_write(fileindex, new_index);
2065 if (rename(new_fileindex_path, fileindex_path) != 0) {
2066 err = got_error_from_errno3("rename", new_fileindex_path,
2071 free(new_fileindex_path);
2072 new_fileindex_path = NULL;
2074 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2078 if (fclose(index) != 0 && err == NULL)
2079 err = got_error_from_errno("fclose");
2081 if (new_fileindex_path) {
2082 if (unlink(new_fileindex_path) != 0 && err == NULL)
2083 err = got_error_from_errno2("unlink",
2084 new_fileindex_path);
2085 free(new_fileindex_path);
2088 got_fileindex_free(fileindex);
2089 unlockerr = lock_worktree(worktree, LOCK_SH);
2090 if (unlockerr && err == NULL)
2095 const struct got_error *
2096 got_worktree_revert(struct got_worktree *worktree,
2097 const char *ondisk_path,
2098 got_worktree_checkout_cb progress_cb, void *progress_arg,
2099 struct got_repository *repo)
2101 struct got_fileindex *fileindex = NULL;
2102 struct got_fileindex_entry *ie = NULL;
2103 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2104 char *tree_path = NULL, *parent_path, *te_name;
2105 FILE *index = NULL, *new_index = NULL;
2106 const struct got_error *err = NULL, *unlockerr = NULL;
2107 struct got_tree_object *tree = NULL;
2108 struct got_object_id id, *tree_id = NULL;
2109 const struct got_tree_entry *te;
2110 struct got_blob_object *blob = NULL;
2111 unsigned char status;
2114 err = lock_worktree(worktree, LOCK_EX);
2118 err = got_path_skip_common_ancestor(&relpath,
2119 got_worktree_get_root_path(worktree), ondisk_path);
2123 fileindex = got_fileindex_alloc();
2124 if (fileindex == NULL) {
2125 err = got_error_from_errno("got_fileindex_alloc");
2129 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2130 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2131 err = got_error_from_errno("asprintf");
2132 fileindex_path = NULL;
2136 index = fopen(fileindex_path, "rb");
2137 if (index == NULL) {
2138 err = got_error_from_errno2("fopen", fileindex_path);
2142 err = got_fileindex_read(fileindex, index);
2146 ie = got_fileindex_entry_get(fileindex, relpath);
2148 err = got_error(GOT_ERR_BAD_PATH);
2152 /* Construct in-repository path of tree which contains this blob. */
2153 err = got_path_dirname(&parent_path, ie->path);
2155 if (err->code != GOT_ERR_BAD_PATH)
2159 if (got_path_is_root_dir(worktree->path_prefix)) {
2160 tree_path = strdup(parent_path);
2161 if (tree_path == NULL) {
2162 err = got_error_from_errno("strdup");
2166 if (got_path_is_root_dir(parent_path)) {
2167 tree_path = strdup(worktree->path_prefix);
2168 if (tree_path == NULL) {
2169 err = got_error_from_errno("strdup");
2173 if (asprintf(&tree_path, "%s/%s",
2174 worktree->path_prefix, parent_path) == -1) {
2175 err = got_error_from_errno("asprintf");
2181 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2186 err = got_object_open_as_tree(&tree, repo, tree_id);
2190 te_name = basename(ie->path);
2191 if (te_name == NULL) {
2192 err = got_error_from_errno2("basename", ie->path);
2196 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2200 te = got_object_tree_find_entry(tree, te_name);
2201 if (te == NULL && status != GOT_STATUS_ADD) {
2202 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2207 case GOT_STATUS_ADD:
2208 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2209 got_fileindex_entry_remove(fileindex, ie);
2211 case GOT_STATUS_DELETE:
2212 case GOT_STATUS_MODIFY:
2213 case GOT_STATUS_CONFLICT:
2214 case GOT_STATUS_MISSING:
2215 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2216 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2219 err = install_blob(worktree, ondisk_path, ie->path,
2220 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2224 if (status == GOT_STATUS_DELETE) {
2225 err = update_blob_fileindex_entry(worktree,
2226 fileindex, ie, ondisk_path, ie->path, blob, 1);
2235 err = got_opentemp_named(&new_fileindex_path, &new_index,
2240 err = got_fileindex_write(fileindex, new_index);
2244 if (rename(new_fileindex_path, fileindex_path) != 0) {
2245 err = got_error_from_errno3("rename", new_fileindex_path,
2250 free(new_fileindex_path);
2251 new_fileindex_path = NULL;
2256 got_object_blob_close(blob);
2258 got_object_tree_close(tree);
2261 if (fclose(index) != 0 && err == NULL)
2262 err = got_error_from_errno("fclose");
2264 if (new_fileindex_path) {
2265 if (unlink(new_fileindex_path) != 0 && err == NULL)
2266 err = got_error_from_errno2("unlink",
2267 new_fileindex_path);
2268 free(new_fileindex_path);
2271 got_fileindex_free(fileindex);
2272 unlockerr = lock_worktree(worktree, LOCK_SH);
2273 if (unlockerr && err == NULL)
2279 free_commitable(struct got_commitable *ct)
2282 free(ct->in_repo_path);
2283 free(ct->ondisk_path);
2285 free(ct->base_blob_id);
2286 free(ct->base_commit_id);
2290 struct collect_commitables_arg {
2291 struct got_pathlist_head *commitable_paths;
2292 struct got_repository *repo;
2293 struct got_worktree *worktree;
2296 static const struct got_error *
2297 collect_commitables(void *arg, unsigned char status, const char *relpath,
2298 struct got_object_id *blob_id, struct got_object_id *commit_id)
2300 struct collect_commitables_arg *a = arg;
2301 const struct got_error *err = NULL;
2302 struct got_commitable *ct = NULL;
2303 struct got_pathlist_entry *new = NULL;
2304 char *parent_path = NULL, *path = NULL;
2307 if (status == GOT_STATUS_CONFLICT)
2308 return got_error(GOT_ERR_COMMIT_CONFLICT);
2310 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2311 status != GOT_STATUS_DELETE)
2314 if (asprintf(&path, "/%s", relpath) == -1) {
2315 err = got_error_from_errno("asprintf");
2318 if (strcmp(path, "/") == 0) {
2319 parent_path = strdup("");
2320 if (parent_path == NULL)
2321 return got_error_from_errno("strdup");
2323 err = got_path_dirname(&parent_path, path);
2328 ct = calloc(1, sizeof(*ct));
2330 err = got_error_from_errno("calloc");
2334 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2336 err = got_error_from_errno("asprintf");
2339 if (status == GOT_STATUS_DELETE) {
2340 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2342 if (lstat(ct->ondisk_path, &sb) != 0) {
2343 err = got_error_from_errno2("lstat", ct->ondisk_path);
2346 ct->mode = sb.st_mode;
2349 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2350 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2352 err = got_error_from_errno("asprintf");
2356 ct->status = status;
2357 ct->blob_id = NULL; /* will be filled in when blob gets created */
2358 if (ct->status != GOT_STATUS_ADD) {
2359 ct->base_blob_id = got_object_id_dup(blob_id);
2360 if (ct->base_blob_id == NULL) {
2361 err = got_error_from_errno("got_object_id_dup");
2364 ct->base_commit_id = got_object_id_dup(commit_id);
2365 if (ct->base_commit_id == NULL) {
2366 err = got_error_from_errno("got_object_id_dup");
2370 ct->path = strdup(path);
2371 if (ct->path == NULL) {
2372 err = got_error_from_errno("strdup");
2375 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2377 if (ct && (err || new == NULL))
2378 free_commitable(ct);
2384 static const struct got_error *write_tree(struct got_object_id **,
2385 struct got_tree_object *, const char *, struct got_pathlist_head *,
2386 got_worktree_status_cb status_cb, void *status_arg,
2387 struct got_repository *);
2389 static const struct got_error *
2390 write_subtree(struct got_object_id **new_subtree_id,
2391 struct got_tree_entry *te, const char *parent_path,
2392 struct got_pathlist_head *commitable_paths,
2393 got_worktree_status_cb status_cb, void *status_arg,
2394 struct got_repository *repo)
2396 const struct got_error *err = NULL;
2397 struct got_tree_object *subtree;
2400 if (asprintf(&subpath, "%s%s%s", parent_path,
2401 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2402 return got_error_from_errno("asprintf");
2404 err = got_object_open_as_tree(&subtree, repo, te->id);
2408 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2409 status_cb, status_arg, repo);
2410 got_object_tree_close(subtree);
2415 static const struct got_error *
2416 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2418 const struct got_error *err = NULL;
2419 char *ct_parent_path = NULL;
2423 if (strchr(ct->path, '/') == NULL) {
2424 *match = got_path_is_root_dir(path);
2428 err = got_path_dirname(&ct_parent_path, ct->path);
2431 *match = (strcmp(path, ct_parent_path) == 0);
2432 free(ct_parent_path);
2437 get_ct_file_mode(struct got_commitable *ct)
2439 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2442 static const struct got_error *
2443 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2444 struct got_tree_entry *te, struct got_commitable *ct)
2446 const struct got_error *err = NULL;
2450 err = got_object_tree_entry_dup(new_te, te);
2454 (*new_te)->mode = get_ct_file_mode(ct);
2456 free((*new_te)->id);
2457 (*new_te)->id = got_object_id_dup(ct->blob_id);
2458 if ((*new_te)->id == NULL) {
2459 err = got_error_from_errno("got_object_id_dup");
2463 if (err && *new_te) {
2464 got_object_tree_entry_close(*new_te);
2470 static const struct got_error *
2471 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2472 struct got_commitable *ct)
2474 const struct got_error *err = NULL;
2479 *new_te = calloc(1, sizeof(**new_te));
2480 if (*new_te == NULL)
2481 return got_error_from_errno("calloc");
2483 ct_name = basename(ct->path);
2484 if (ct_name == NULL) {
2485 err = got_error_from_errno2("basename", ct->path);
2488 (*new_te)->name = strdup(ct_name);
2489 if ((*new_te)->name == NULL) {
2490 err = got_error_from_errno("strdup");
2494 (*new_te)->mode = get_ct_file_mode(ct);
2496 (*new_te)->id = got_object_id_dup(ct->blob_id);
2497 if ((*new_te)->id == NULL) {
2498 err = got_error_from_errno("got_object_id_dup");
2502 if (err && *new_te) {
2503 got_object_tree_entry_close(*new_te);
2509 static const struct got_error *
2510 insert_tree_entry(struct got_tree_entry *new_te,
2511 struct got_pathlist_head *paths)
2513 const struct got_error *err = NULL;
2514 struct got_pathlist_entry *new_pe;
2516 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2520 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2524 static const struct got_error *
2525 report_ct_status(struct got_commitable *ct,
2526 got_worktree_status_cb status_cb, void *status_arg)
2528 const char *ct_path = ct->path;
2529 while (ct_path[0] == '/')
2531 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2534 static const struct got_error *
2535 match_modified_subtree(int *modified, struct got_tree_entry *te,
2536 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2538 const struct got_error *err = NULL;
2539 struct got_pathlist_entry *pe;
2544 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2545 got_path_is_root_dir(base_tree_path) ? "" : "/",
2547 return got_error_from_errno("asprintf");
2549 TAILQ_FOREACH(pe, commitable_paths, entry) {
2550 struct got_commitable *ct = pe->data;
2551 *modified = got_path_is_child(ct->in_repo_path, te_path,
2561 static const struct got_error *
2562 match_deleted_or_modified_ct(struct got_commitable **ctp,
2563 struct got_tree_entry *te, const char *base_tree_path,
2564 struct got_pathlist_head *commitable_paths)
2566 const struct got_error *err = NULL;
2567 struct got_pathlist_entry *pe;
2571 TAILQ_FOREACH(pe, commitable_paths, entry) {
2572 struct got_commitable *ct = pe->data;
2573 char *ct_name = NULL;
2576 if (ct->status != GOT_STATUS_MODIFY &&
2577 ct->status != GOT_STATUS_DELETE)
2580 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2583 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2589 ct_name = basename(pe->path);
2590 if (ct_name == NULL)
2591 return got_error_from_errno2("basename", pe->path);
2593 if (strcmp(te->name, ct_name) != 0)
2603 static const struct got_error *
2604 write_tree(struct got_object_id **new_tree_id,
2605 struct got_tree_object *base_tree, const char *path_base_tree,
2606 struct got_pathlist_head *commitable_paths,
2607 got_worktree_status_cb status_cb, void *status_arg,
2608 struct got_repository *repo)
2610 const struct got_error *err = NULL;
2611 const struct got_tree_entries *base_entries = NULL;
2612 struct got_pathlist_head paths;
2613 struct got_tree_entries new_tree_entries;
2614 struct got_tree_entry *te, *new_te = NULL;
2615 struct got_pathlist_entry *pe;
2618 new_tree_entries.nentries = 0;
2619 SIMPLEQ_INIT(&new_tree_entries.head);
2621 /* Insert, and recurse into, newly added entries first. */
2622 TAILQ_FOREACH(pe, commitable_paths, entry) {
2623 struct got_commitable *ct = pe->data;
2624 char *child_path = NULL, *slash;
2626 if (ct->status != GOT_STATUS_ADD)
2629 if (!got_path_is_child(pe->path, path_base_tree,
2630 strlen(path_base_tree)))
2633 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2638 slash = strchr(child_path, '/');
2639 if (slash == NULL) {
2640 err = alloc_added_blob_tree_entry(&new_te, ct);
2643 err = report_ct_status(ct, status_cb, status_arg);
2648 struct got_pathlist_entry *pe2;
2651 *slash = '\0'; /* trim trailing path components */
2652 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2653 got_path_is_root_dir(path_base_tree) ? "" : "/",
2654 child_path) == -1) {
2655 err = got_error_from_errno("asprintf");
2658 TAILQ_FOREACH(pe2, &paths, entry) {
2659 if (got_path_cmp(subtree_path, pe2->path) != 0)
2669 new_te = calloc(1, sizeof(*new_te));
2670 new_te->mode = S_IFDIR;
2671 new_te->name = strdup(child_path);
2672 if (new_te->name == NULL) {
2673 err = got_error_from_errno("strdup");
2674 got_object_tree_entry_close(new_te);
2678 err = write_tree(&new_te->id, NULL, subtree_path,
2679 commitable_paths, status_cb, status_arg, repo);
2682 got_object_tree_entry_close(new_te);
2687 err = insert_tree_entry(new_te, &paths);
2693 /* Handle modified and deleted entries. */
2694 base_entries = got_object_tree_get_entries(base_tree);
2695 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2696 struct got_commitable *ct = NULL;
2698 if (S_ISDIR(te->mode)) {
2700 err = got_object_tree_entry_dup(&new_te, te);
2703 err = match_modified_subtree(&modified, te,
2704 path_base_tree, commitable_paths);
2707 /* Avoid recursion into unmodified subtrees. */
2710 err = write_subtree(&new_te->id, te,
2711 path_base_tree, commitable_paths,
2712 status_cb, status_arg, repo);
2716 err = insert_tree_entry(new_te, &paths);
2722 err = match_deleted_or_modified_ct(&ct, te,
2723 path_base_tree, commitable_paths);
2725 /* NB: Deleted entries get dropped here. */
2726 if (ct->status == GOT_STATUS_MODIFY) {
2727 err = alloc_modified_blob_tree_entry(
2731 err = insert_tree_entry(new_te, &paths);
2735 err = report_ct_status(ct, status_cb,
2740 /* Entry is unchanged; just copy it. */
2741 err = got_object_tree_entry_dup(&new_te, te);
2744 err = insert_tree_entry(new_te, &paths);
2751 /* Write new list of entries; deleted entries have been dropped. */
2752 TAILQ_FOREACH(pe, &paths, entry) {
2753 struct got_tree_entry *te = pe->data;
2754 new_tree_entries.nentries++;
2755 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2757 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2759 got_object_tree_entries_close(&new_tree_entries);
2760 got_pathlist_free(&paths);
2764 static const struct got_error *
2765 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2766 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2768 const struct got_error *err = NULL;
2769 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2770 struct got_fileindex *fileindex = NULL;
2771 FILE *new_index = NULL;
2772 struct got_pathlist_entry *pe;
2774 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2778 err = got_opentemp_named(&new_fileindex_path, &new_index,
2783 TAILQ_FOREACH(pe, commitable_paths, entry) {
2784 struct got_fileindex_entry *ie;
2785 struct got_commitable *ct = pe->data;
2787 ie = got_fileindex_entry_get(fileindex, pe->path);
2789 if (ct->status == GOT_STATUS_DELETE) {
2790 got_fileindex_entry_remove(fileindex, ie);
2791 got_fileindex_entry_free(ie);
2793 err = got_fileindex_entry_update(ie,
2794 ct->ondisk_path, ct->blob_id->sha1,
2795 new_base_commit_id->sha1, 1);
2797 err = got_fileindex_entry_alloc(&ie,
2798 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2799 new_base_commit_id->sha1);
2802 err = got_fileindex_entry_add(fileindex, ie);
2808 err = got_fileindex_write(fileindex, new_index);
2812 if (rename(new_fileindex_path, fileindex_path) != 0) {
2813 err = got_error_from_errno3("rename", new_fileindex_path,
2815 unlink(new_fileindex_path);
2819 free(new_fileindex_path);
2820 new_fileindex_path = NULL;
2823 if (new_fileindex_path)
2824 unlink(new_fileindex_path);
2827 free(new_fileindex_path);
2828 free(fileindex_path);
2829 got_fileindex_free(fileindex);
2833 static const struct got_error *
2834 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
2835 struct got_object_id *head_commit_id)
2837 const struct got_error *err = NULL;
2838 struct got_object_id *id_in_head;
2841 * Require that modified/deleted files are based on the branch head.
2842 * This requirement could be relaxed to force less update operations
2843 * on users but, for now, we want to play it safe and see how it goes.
2845 * If this check is relaxed, it must still ensure that no modifications
2846 * were made to files *and their parents* in commits between the file's
2847 * base commit and the branch head. Otherwise, tree conflicts will not
2848 * be detected reliably.
2850 if (ct->status != GOT_STATUS_ADD) {
2851 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
2852 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2856 /* Require that added files don't exist in the branch head. */
2857 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
2859 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
2863 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2870 const struct got_error *
2871 got_worktree_commit(struct got_object_id **new_commit_id,
2872 struct got_worktree *worktree, const char *ondisk_path,
2873 const char *author, const char *committer,
2874 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
2875 got_worktree_status_cb status_cb, void *status_arg,
2876 struct got_repository *repo)
2878 const struct got_error *err = NULL, *unlockerr = NULL;
2879 struct collect_commitables_arg cc_arg;
2880 struct got_pathlist_head commitable_paths;
2881 struct got_pathlist_entry *pe;
2882 char *relpath = NULL;
2883 const char *head_ref_name = NULL;
2884 struct got_reference *head_ref = NULL;
2885 struct got_commit_object *head_commit = NULL;
2886 struct got_object_id *head_commit_id = NULL;
2887 struct got_reference *head_ref2 = NULL;
2888 struct got_object_id *head_commit_id2 = NULL;
2889 struct got_tree_object *head_tree = NULL;
2890 struct got_object_id *new_tree_id = NULL;
2891 struct got_object_id_queue parent_ids;
2892 struct got_object_qid *pid = NULL;
2893 char *logmsg = NULL;
2895 *new_commit_id = NULL;
2897 TAILQ_INIT(&commitable_paths);
2898 SIMPLEQ_INIT(&parent_ids);
2901 err = got_path_skip_common_ancestor(&relpath,
2902 worktree->root_path, ondisk_path);
2907 err = lock_worktree(worktree, LOCK_EX);
2911 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
2914 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2918 cc_arg.commitable_paths = &commitable_paths;
2919 cc_arg.worktree = worktree;
2921 err = got_worktree_status(worktree, relpath ? relpath : "",
2922 repo, collect_commitables, &cc_arg, NULL, NULL);
2926 if (TAILQ_EMPTY(&commitable_paths)) {
2927 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
2931 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2935 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2936 struct got_commitable *ct = pe->data;
2937 err = check_ct_out_of_date(ct, repo, head_commit_id);
2942 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2946 if (commit_msg_cb != NULL) {
2947 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
2952 if (logmsg == NULL || strlen(logmsg) == 0) {
2953 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
2957 /* Create blobs from added and modified files and record their IDs. */
2958 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2959 struct got_commitable *ct = pe->data;
2962 if (ct->status != GOT_STATUS_ADD &&
2963 ct->status != GOT_STATUS_MODIFY)
2966 if (asprintf(&ondisk_path, "%s/%s",
2967 worktree->root_path, pe->path) == -1) {
2968 err = got_error_from_errno("asprintf");
2971 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2977 /* Recursively write new tree objects. */
2978 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2979 status_cb, status_arg, repo);
2983 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2986 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2987 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2988 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2989 got_object_qid_free(pid);
2995 /* Check if a concurrent commit to our branch has occurred. */
2996 head_ref_name = got_worktree_get_head_ref_name(worktree);
2997 if (head_ref_name == NULL) {
2998 err = got_error_from_errno("got_worktree_get_head_ref_name");
3001 /* Lock the reference here to prevent concurrent modification. */
3002 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3005 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3008 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3009 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3012 /* Update branch head in repository. */
3013 err = got_ref_change_ref(head_ref2, *new_commit_id);
3016 err = got_ref_write(head_ref2, repo);
3020 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3024 err = ref_base_commit(worktree, repo);
3028 err = update_fileindex_after_commit(&commitable_paths,
3029 *new_commit_id, worktree);
3033 unlockerr = lock_worktree(worktree, LOCK_SH);
3034 if (unlockerr && err == NULL)
3036 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3037 struct got_commitable *ct = pe->data;
3038 free_commitable(ct);
3040 got_pathlist_free(&commitable_paths);
3042 got_object_tree_close(head_tree);
3044 got_object_commit_close(head_commit);
3046 free(head_commit_id);
3047 free(head_commit_id2);
3049 got_ref_close(head_ref);
3051 unlockerr = got_ref_unlock(head_ref2);
3052 if (unlockerr && err == NULL)
3054 got_ref_close(head_ref2);