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"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 static const struct got_error *
62 create_meta_file(const char *path_got, const char *name, const char *content)
64 const struct got_error *err = NULL;
68 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
69 err = got_error_from_errno("asprintf");
74 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
75 GOT_DEFAULT_FILE_MODE);
77 err = got_error_from_errno2("open", path);
82 int len = dprintf(fd, "%s\n", content);
83 if (len != strlen(content) + 1) {
84 err = got_error_from_errno("dprintf");
90 if (fd != -1 && close(fd) == -1 && err == NULL)
91 err = got_error_from_errno("close");
96 static const struct got_error *
97 update_meta_file(const char *path_got, const char *name, const char *content)
99 const struct got_error *err = NULL;
100 FILE *tmpfile = NULL;
101 char *tmppath = NULL;
104 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
105 err = got_error_from_errno("asprintf");
110 err = got_opentemp_named(&tmppath, &tmpfile, path);
115 int len = fprintf(tmpfile, "%s\n", content);
116 if (len != strlen(content) + 1) {
117 err = got_error_from_errno2("fprintf", tmppath);
122 if (rename(tmppath, path) != 0) {
123 err = got_error_from_errno3("rename", tmppath, path);
129 if (fclose(tmpfile) != 0 && err == NULL)
130 err = got_error_from_errno2("fclose", tmppath);
135 static const struct got_error *
136 read_meta_file(char **content, const char *path_got, const char *name)
138 const struct got_error *err = NULL;
146 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
147 err = got_error_from_errno("asprintf");
152 fd = open(path, O_RDONLY | O_NOFOLLOW);
155 err = got_error(GOT_ERR_WORKTREE_META);
157 err = got_error_from_errno2("open", path);
160 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
161 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
162 : got_error_from_errno2("flock", path));
166 if (lstat(path, &sb) != 0) {
167 err = got_error_from_errno2("lstat", path);
170 *content = calloc(1, sb.st_size);
171 if (*content == NULL) {
172 err = got_error_from_errno("calloc");
176 n = read(fd, *content, sb.st_size);
177 if (n != sb.st_size) {
178 err = (n == -1 ? got_error_from_errno2("read", path) :
179 got_error(GOT_ERR_WORKTREE_META));
182 if ((*content)[sb.st_size - 1] != '\n') {
183 err = got_error(GOT_ERR_WORKTREE_META);
186 (*content)[sb.st_size - 1] = '\0';
189 if (fd != -1 && close(fd) == -1 && err == NULL)
190 err = got_error_from_errno2("close", path_got);
199 static const struct got_error *
200 write_head_ref(const char *path_got, struct got_reference *head_ref)
202 const struct got_error *err = NULL;
205 if (got_ref_is_symbolic(head_ref)) {
206 refstr = got_ref_to_str(head_ref);
208 return got_error_from_errno("got_ref_to_str");
210 refstr = strdup(got_ref_get_name(head_ref));
212 return got_error_from_errno("strdup");
214 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
219 const struct got_error *
220 got_worktree_init(const char *path, struct got_reference *head_ref,
221 const char *prefix, struct got_repository *repo)
223 const struct got_error *err = NULL;
224 struct got_object_id *commit_id = NULL;
226 uint32_t uuid_status;
228 char *path_got = NULL;
229 char *formatstr = NULL;
230 char *absprefix = NULL;
231 char *basestr = NULL;
232 char *uuidstr = NULL;
234 if (strcmp(path, got_repo_get_path(repo)) == 0) {
235 err = got_error(GOT_ERR_WORKTREE_REPO);
239 err = got_ref_resolve(&commit_id, repo, head_ref);
242 err = got_object_get_type(&obj_type, repo, commit_id);
245 if (obj_type != GOT_OBJ_TYPE_COMMIT)
246 return got_error(GOT_ERR_OBJ_TYPE);
248 if (!got_path_is_absolute(prefix)) {
249 if (asprintf(&absprefix, "/%s", prefix) == -1)
250 return got_error_from_errno("asprintf");
253 /* Create top-level directory (may already exist). */
254 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
255 err = got_error_from_errno2("mkdir", path);
259 /* Create .got directory (may already exist). */
260 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
261 err = got_error_from_errno("asprintf");
264 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
265 err = got_error_from_errno2("mkdir", path_got);
269 /* Create an empty lock file. */
270 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
274 /* Create an empty file index. */
275 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
279 /* Write the HEAD reference. */
280 err = write_head_ref(path_got, head_ref);
284 /* Record our base commit. */
285 err = got_object_id_str(&basestr, commit_id);
288 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
292 /* Store path to repository. */
293 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
294 got_repo_get_path(repo));
298 /* Store in-repository path prefix. */
299 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
300 absprefix ? absprefix : prefix);
305 uuid_create(&uuid, &uuid_status);
306 if (uuid_status != uuid_s_ok) {
307 err = got_error_uuid(uuid_status);
310 uuid_to_string(&uuid, &uuidstr, &uuid_status);
311 if (uuid_status != uuid_s_ok) {
312 err = got_error_uuid(uuid_status);
315 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
319 /* Stamp work tree with format file. */
320 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
321 err = got_error_from_errno("asprintf");
324 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
338 static const struct got_error *
339 open_worktree(struct got_worktree **worktree, const char *path)
341 const struct got_error *err = NULL;
343 char *formatstr = NULL;
344 char *uuidstr = NULL;
345 char *path_lock = NULL;
346 char *base_commit_id_str = NULL;
347 int version, fd = -1;
349 struct got_repository *repo = NULL;
350 uint32_t uuid_status;
354 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
355 err = got_error_from_errno("asprintf");
360 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
361 err = got_error_from_errno("asprintf");
366 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
368 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
369 : got_error_from_errno2("open", path_lock));
373 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
377 version = strtonum(formatstr, 1, INT_MAX, &errstr);
379 err = got_error(GOT_ERR_WORKTREE_META);
382 if (version != GOT_WORKTREE_FORMAT_VERSION) {
383 err = got_error(GOT_ERR_WORKTREE_VERS);
387 *worktree = calloc(1, sizeof(**worktree));
388 if (*worktree == NULL) {
389 err = got_error_from_errno("calloc");
392 (*worktree)->lockfd = -1;
394 (*worktree)->root_path = strdup(path);
395 if ((*worktree)->root_path == NULL) {
396 err = got_error_from_errno("strdup");
399 err = read_meta_file(&(*worktree)->repo_path, path_got,
400 GOT_WORKTREE_REPOSITORY);
404 err = read_meta_file(&(*worktree)->path_prefix, path_got,
405 GOT_WORKTREE_PATH_PREFIX);
409 err = read_meta_file(&base_commit_id_str, path_got,
410 GOT_WORKTREE_BASE_COMMIT);
414 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
417 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
418 if (uuid_status != uuid_s_ok) {
419 err = got_error_uuid(uuid_status);
423 err = got_repo_open(&repo, (*worktree)->repo_path);
427 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
432 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
433 GOT_WORKTREE_HEAD_REF);
436 got_repo_close(repo);
439 free(base_commit_id_str);
445 if (*worktree != NULL)
446 got_worktree_close(*worktree);
449 (*worktree)->lockfd = fd;
454 const struct got_error *
455 got_worktree_open(struct got_worktree **worktree, const char *path)
457 const struct got_error *err = NULL;
460 err = open_worktree(worktree, path);
461 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
465 path = dirname(path);
467 return got_error_from_errno2("dirname", path);
468 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
470 return got_error(GOT_ERR_NOT_WORKTREE);
473 const struct got_error *
474 got_worktree_close(struct got_worktree *worktree)
476 const struct got_error *err = NULL;
477 free(worktree->root_path);
478 free(worktree->repo_path);
479 free(worktree->path_prefix);
480 free(worktree->base_commit_id);
481 free(worktree->head_ref_name);
482 if (worktree->lockfd != -1)
483 if (close(worktree->lockfd) != 0)
484 err = got_error_from_errno2("close",
485 got_worktree_get_root_path(worktree));
491 got_worktree_get_root_path(struct got_worktree *worktree)
493 return worktree->root_path;
497 got_worktree_get_repo_path(struct got_worktree *worktree)
499 return worktree->repo_path;
503 got_worktree_get_path_prefix(struct got_worktree *worktree)
505 return worktree->path_prefix;
508 const struct got_error *
509 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
510 const char *path_prefix)
512 char *absprefix = NULL;
514 if (!got_path_is_absolute(path_prefix)) {
515 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
516 return got_error_from_errno("asprintf");
518 *match = (strcmp(absprefix ? absprefix : path_prefix,
519 worktree->path_prefix) == 0);
525 got_worktree_get_head_ref_name(struct got_worktree *worktree)
527 return worktree->head_ref_name;
530 const struct got_error *
531 got_worktree_set_head_ref(struct got_worktree *worktree,
532 struct got_reference *head_ref)
534 const struct got_error *err = NULL;
535 char *path_got = NULL, *head_ref_name = NULL;
537 if (asprintf(&path_got, "%s/%s", worktree->root_path,
538 GOT_WORKTREE_GOT_DIR) == -1) {
539 err = got_error_from_errno("asprintf");
544 head_ref_name = strdup(got_ref_get_name(head_ref));
545 if (head_ref_name == NULL) {
546 err = got_error_from_errno("strdup");
550 err = write_head_ref(path_got, head_ref);
554 free(worktree->head_ref_name);
555 worktree->head_ref_name = head_ref_name;
563 struct got_object_id *
564 got_worktree_get_base_commit_id(struct got_worktree *worktree)
566 return worktree->base_commit_id;
569 const struct got_error *
570 got_worktree_set_base_commit_id(struct got_worktree *worktree,
571 struct got_repository *repo, struct got_object_id *commit_id)
573 const struct got_error *err;
574 struct got_object *obj = NULL;
576 char *path_got = NULL;
578 if (asprintf(&path_got, "%s/%s", worktree->root_path,
579 GOT_WORKTREE_GOT_DIR) == -1) {
580 err = got_error_from_errno("asprintf");
585 err = got_object_open(&obj, repo, commit_id);
589 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
590 err = got_error(GOT_ERR_OBJ_TYPE);
594 /* Record our base commit. */
595 err = got_object_id_str(&id_str, commit_id);
598 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
602 free(worktree->base_commit_id);
603 worktree->base_commit_id = got_object_id_dup(commit_id);
604 if (worktree->base_commit_id == NULL) {
605 err = got_error_from_errno("got_object_id_dup");
610 got_object_close(obj);
616 static const struct got_error *
617 lock_worktree(struct got_worktree *worktree, int operation)
619 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
620 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
621 : got_error_from_errno2("flock",
622 got_worktree_get_root_path(worktree)));
626 static const struct got_error *
627 add_dir_on_disk(struct got_worktree *worktree, const char *path)
629 const struct got_error *err = NULL;
632 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
633 return got_error_from_errno("asprintf");
635 err = got_path_mkdir(abspath);
636 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
639 if (lstat(abspath, &sb) == -1) {
640 err = got_error_from_errno2("lstat", abspath);
641 } else if (!S_ISDIR(sb.st_mode)) {
642 /* TODO directory is obstructed; do something */
643 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
650 static const struct got_error *
651 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
653 const struct got_error *err = NULL;
656 size_t flen1 = 0, flen2 = 0;
661 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
662 if (flen1 == 0 && ferror(f1)) {
663 err = got_error_from_errno("fread");
666 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
667 if (flen2 == 0 && ferror(f2)) {
668 err = got_error_from_errno("fread");
675 } else if (flen2 == 0) {
679 } else if (flen1 == flen2) {
680 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
693 static const struct got_error *
694 check_files_equal(int *same, const char *f1_path, const char *f2_path)
696 const struct got_error *err = NULL;
699 FILE *f1 = NULL, *f2 = NULL;
703 if (lstat(f1_path, &sb) != 0) {
704 err = got_error_from_errno2("lstat", f1_path);
709 if (lstat(f2_path, &sb) != 0) {
710 err = got_error_from_errno2("lstat", f2_path);
715 if (size1 != size2) {
720 f1 = fopen(f1_path, "r");
722 return got_error_from_errno2("open", f1_path);
724 f2 = fopen(f2_path, "r");
726 err = got_error_from_errno2("open", f2_path);
730 err = check_file_contents_equal(same, f1, f2);
732 if (f1 && fclose(f1) != 0 && err == NULL)
733 err = got_error_from_errno("fclose");
734 if (f2 && fclose(f2) != 0 && err == NULL)
735 err = got_error_from_errno("fclose");
741 * Perform a 3-way merge where blob_orig acts as the common ancestor,
742 * blob_deriv acts as the first derived version, and the file on disk
743 * acts as the second derived version.
745 static const struct got_error *
746 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
747 struct got_blob_object *blob_orig, const char *ondisk_path,
748 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
749 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
752 const struct got_error *err = NULL;
754 FILE *f_deriv = NULL, *f_orig = NULL;
755 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
756 char *merged_path = NULL, *base_path = NULL;
762 *local_changes_subsumed = 0;
764 parent = dirname(ondisk_path);
766 return got_error_from_errno2("dirname", ondisk_path);
768 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
769 return got_error_from_errno("asprintf");
771 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
776 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
777 err = got_error_from_errno("asprintf");
782 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
785 err = got_object_blob_dump_to_file(NULL, NULL, f_deriv, blob_deriv);
790 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
791 err = got_error_from_errno("asprintf");
796 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
800 err = got_object_blob_dump_to_file(NULL, NULL, f_orig,
806 * If the file has no blob, this is an "add vs add" conflict,
807 * and we simply use an empty ancestor file to make both files
808 * appear in the merged result in their entirety.
812 err = got_object_id_str(&id_str, worktree->base_commit_id);
815 if (asprintf(&label1, "commit %s", id_str) == -1) {
816 err = got_error_from_errno("asprintf");
820 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
821 blob_orig_path, ondisk_path, label1, path);
825 (*progress_cb)(progress_arg,
826 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
828 if (fsync(merged_fd) != 0) {
829 err = got_error_from_errno("fsync");
833 /* Check if a clean merge has subsumed all local changes. */
834 if (overlapcnt == 0) {
835 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
841 if (chmod(merged_path, st_mode) != 0) {
842 err = got_error_from_errno2("chmod", merged_path);
846 if (rename(merged_path, ondisk_path) != 0) {
847 err = got_error_from_errno3("rename", merged_path,
854 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
855 err = got_error_from_errno("close");
856 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
857 err = got_error_from_errno("fclose");
858 if (f_orig && fclose(f_orig) != 0 && err == NULL)
859 err = got_error_from_errno("fclose");
862 if (blob_deriv_path) {
863 unlink(blob_deriv_path);
864 free(blob_deriv_path);
866 if (blob_orig_path) {
867 unlink(blob_orig_path);
868 free(blob_orig_path);
875 static const struct got_error *
876 update_blob_fileindex_entry(struct got_worktree *worktree,
877 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
878 const char *ondisk_path, const char *path, struct got_blob_object *blob,
879 int update_timestamps)
881 const struct got_error *err = NULL;
884 ie = got_fileindex_entry_get(fileindex, path);
886 err = got_fileindex_entry_update(ie, ondisk_path,
887 blob->id.sha1, worktree->base_commit_id->sha1,
890 struct got_fileindex_entry *new_ie;
891 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
892 path, blob->id.sha1, worktree->base_commit_id->sha1);
894 err = got_fileindex_entry_add(fileindex, new_ie);
899 static const struct got_error *
900 install_blob(struct got_worktree *worktree, const char *ondisk_path,
901 const char *path, uint16_t te_mode, uint16_t st_mode,
902 struct got_blob_object *blob, int restoring_missing_file,
903 int reverting_versioned_file, struct got_repository *repo,
904 got_worktree_checkout_cb progress_cb, void *progress_arg)
906 const struct got_error *err = NULL;
910 char *tmppath = NULL;
912 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
913 GOT_DEFAULT_FILE_MODE);
915 if (errno == ENOENT) {
916 char *parent = dirname(path);
918 return got_error_from_errno2("dirname", path);
919 err = add_dir_on_disk(worktree, parent);
922 fd = open(ondisk_path,
923 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
924 GOT_DEFAULT_FILE_MODE);
926 return got_error_from_errno2("open",
928 } else if (errno == EEXIST) {
929 if (!S_ISREG(st_mode)) {
930 /* TODO file is obstructed; do something */
931 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
934 err = got_opentemp_named_fd(&tmppath, &fd,
941 return got_error_from_errno2("open", ondisk_path);
944 if (restoring_missing_file)
945 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
946 else if (reverting_versioned_file)
947 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
949 (*progress_cb)(progress_arg,
950 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
952 hdrlen = got_object_blob_get_hdrlen(blob);
954 const uint8_t *buf = got_object_blob_get_read_buf(blob);
955 err = got_object_blob_read_block(&len, blob);
959 /* Skip blob object header first time around. */
960 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
962 err = got_error_from_errno("write");
964 } else if (outlen != len - hdrlen) {
965 err = got_error(GOT_ERR_IO);
972 if (fsync(fd) != 0) {
973 err = got_error_from_errno("fsync");
978 if (rename(tmppath, ondisk_path) != 0) {
979 err = got_error_from_errno3("rename", tmppath,
986 if (te_mode & S_IXUSR) {
987 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
988 err = got_error_from_errno2("chmod", ondisk_path);
992 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
993 err = got_error_from_errno2("chmod", ondisk_path);
999 if (fd != -1 && close(fd) != 0 && err == NULL)
1000 err = got_error_from_errno("close");
1005 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1006 static const struct got_error *
1007 get_modified_file_content_status(unsigned char *status, FILE *f)
1009 const struct got_error *err = NULL;
1010 const char *markers[3] = {
1011 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1012 GOT_DIFF_CONFLICT_MARKER_SEP,
1013 GOT_DIFF_CONFLICT_MARKER_END
1018 const char delim[3] = {'\0', '\0', '\0'};
1020 while (*status == GOT_STATUS_MODIFY) {
1021 line = fparseln(f, &len, NULL, delim, 0);
1025 err = got_ferror(f, GOT_ERR_IO);
1029 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1030 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1032 *status = GOT_STATUS_CONFLICT;
1041 static const struct got_error *
1042 get_file_status(unsigned char *status, struct stat *sb,
1043 struct got_fileindex_entry *ie, const char *abspath,
1044 struct got_repository *repo)
1046 const struct got_error *err = NULL;
1047 struct got_object_id id;
1051 struct got_blob_object *blob = NULL;
1054 *status = GOT_STATUS_NO_CHANGE;
1056 if (lstat(abspath, sb) == -1) {
1057 if (errno == ENOENT) {
1059 if (got_fileindex_entry_has_file_on_disk(ie))
1060 *status = GOT_STATUS_MISSING;
1062 *status = GOT_STATUS_DELETE;
1064 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1065 & (S_IRWXU | S_IRWXG | S_IRWXO));
1067 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1070 return got_error_from_errno2("lstat", abspath);
1073 if (!S_ISREG(sb->st_mode)) {
1074 *status = GOT_STATUS_OBSTRUCTED;
1081 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1082 *status = GOT_STATUS_DELETE;
1084 } else if (!got_fileindex_entry_has_blob(ie)) {
1085 *status = GOT_STATUS_ADD;
1089 if (ie->ctime_sec == sb->st_ctime &&
1090 ie->ctime_nsec == sb->st_ctimensec &&
1091 ie->mtime_sec == sb->st_mtime &&
1092 ie->mtime_sec == sb->st_mtime &&
1093 ie->mtime_nsec == sb->st_mtimensec &&
1094 ie->size == (sb->st_size & 0xffffffff))
1097 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1098 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1102 f = fopen(abspath, "r");
1104 err = got_error_from_errno2("fopen", abspath);
1107 hdrlen = got_object_blob_get_hdrlen(blob);
1109 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1110 err = got_object_blob_read_block(&blen, blob);
1113 /* Skip length of blob object header first time around. */
1114 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1115 if (flen == 0 && ferror(f)) {
1116 err = got_error_from_errno("fread");
1121 *status = GOT_STATUS_MODIFY;
1123 } else if (flen == 0) {
1125 *status = GOT_STATUS_MODIFY;
1127 } else if (blen - hdrlen == flen) {
1128 /* Skip blob object header first time around. */
1129 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1130 *status = GOT_STATUS_MODIFY;
1134 *status = GOT_STATUS_MODIFY;
1140 if (*status == GOT_STATUS_MODIFY) {
1142 err = get_modified_file_content_status(status, f);
1146 got_object_blob_close(blob);
1152 static const struct got_error *
1153 update_blob(struct got_worktree *worktree,
1154 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1155 struct got_tree_entry *te, const char *path,
1156 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1159 const struct got_error *err = NULL;
1160 struct got_blob_object *blob = NULL;
1162 unsigned char status = GOT_STATUS_NO_CHANGE;
1165 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1166 return got_error_from_errno("asprintf");
1168 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1172 if (status == GOT_STATUS_OBSTRUCTED) {
1173 (*progress_cb)(progress_arg, status, path);
1177 if (ie && status != GOT_STATUS_MISSING) {
1178 if (got_fileindex_entry_has_commit(ie) &&
1179 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1180 SHA1_DIGEST_LENGTH) == 0) {
1181 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1185 if (got_fileindex_entry_has_blob(ie) &&
1186 memcmp(ie->blob_sha1, te->id->sha1,
1187 SHA1_DIGEST_LENGTH) == 0)
1191 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1195 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1196 int update_timestamps;
1197 struct got_blob_object *blob2 = NULL;
1198 if (got_fileindex_entry_has_blob(ie)) {
1199 struct got_object_id id2;
1200 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1201 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1205 err = merge_blob(&update_timestamps, worktree, blob2,
1206 ondisk_path, path, sb.st_mode, blob, repo,
1207 progress_cb, progress_arg);
1209 got_object_blob_close(blob2);
1211 * Do not update timestamps of files with local changes.
1212 * Otherwise, a future status walk would treat them as
1213 * unmodified files again.
1215 err = got_fileindex_entry_update(ie, ondisk_path,
1216 blob->id.sha1, worktree->base_commit_id->sha1,
1218 } else if (status == GOT_STATUS_DELETE) {
1219 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1220 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1221 ondisk_path, path, blob, 0);
1225 err = install_blob(worktree, ondisk_path, path, te->mode,
1226 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1227 repo, progress_cb, progress_arg);
1230 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1231 ondisk_path, path, blob, 1);
1235 got_object_blob_close(blob);
1241 static const struct got_error *
1242 remove_ondisk_file(const char *root_path, const char *path)
1244 const struct got_error *err = NULL;
1245 char *ondisk_path = NULL;
1247 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1248 return got_error_from_errno("asprintf");
1250 if (unlink(ondisk_path) == -1) {
1251 if (errno != ENOENT)
1252 err = got_error_from_errno2("unlink", ondisk_path);
1254 char *parent = dirname(ondisk_path);
1255 while (parent && strcmp(parent, root_path) != 0) {
1256 if (rmdir(parent) == -1) {
1257 if (errno != ENOTEMPTY)
1258 err = got_error_from_errno2("rmdir",
1262 parent = dirname(parent);
1269 static const struct got_error *
1270 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1271 struct got_fileindex_entry *ie, struct got_repository *repo,
1272 got_worktree_checkout_cb progress_cb, void *progress_arg)
1274 const struct got_error *err = NULL;
1275 unsigned char status;
1279 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1281 return got_error_from_errno("asprintf");
1283 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1287 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1288 status == GOT_STATUS_ADD) {
1289 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1291 * Preserve the working file and change the deleted blob's
1292 * entry into a schedule-add entry.
1294 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1299 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1300 if (status == GOT_STATUS_NO_CHANGE) {
1301 err = remove_ondisk_file(worktree->root_path, ie->path);
1305 got_fileindex_entry_remove(fileindex, ie);
1311 struct diff_cb_arg {
1312 struct got_fileindex *fileindex;
1313 struct got_worktree *worktree;
1314 struct got_repository *repo;
1315 got_worktree_checkout_cb progress_cb;
1317 got_worktree_cancel_cb cancel_cb;
1321 static const struct got_error *
1322 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1323 struct got_tree_entry *te, const char *parent_path)
1325 struct diff_cb_arg *a = arg;
1327 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1328 return got_error(GOT_ERR_CANCELLED);
1330 return update_blob(a->worktree, a->fileindex, ie, te,
1331 ie->path, a->repo, a->progress_cb, a->progress_arg);
1334 static const struct got_error *
1335 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1337 struct diff_cb_arg *a = arg;
1339 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1340 return got_error(GOT_ERR_CANCELLED);
1342 return delete_blob(a->worktree, a->fileindex, ie,
1343 a->repo, a->progress_cb, a->progress_arg);
1346 static const struct got_error *
1347 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1349 struct diff_cb_arg *a = arg;
1350 const struct got_error *err;
1353 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1354 return got_error(GOT_ERR_CANCELLED);
1356 if (asprintf(&path, "%s%s%s", parent_path,
1357 parent_path[0] ? "/" : "", te->name)
1359 return got_error_from_errno("asprintf");
1361 if (S_ISDIR(te->mode))
1362 err = add_dir_on_disk(a->worktree, path);
1364 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1365 a->repo, a->progress_cb, a->progress_arg);
1371 const struct got_error *
1372 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1374 const struct got_error *err = NULL;
1375 char *uuidstr = NULL;
1376 uint32_t uuid_status;
1380 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1381 if (uuid_status != uuid_s_ok)
1382 return got_error_uuid(uuid_status);
1384 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1386 err = got_error_from_errno("asprintf");
1394 * Prevent Git's garbage collector from deleting our base commit by
1395 * setting a reference to our base commit's ID.
1397 static const struct got_error *
1398 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1400 const struct got_error *err = NULL;
1401 struct got_reference *ref = NULL;
1404 err = got_worktree_get_base_ref_name(&refname, worktree);
1408 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1412 err = got_ref_write(ref, repo);
1420 static const struct got_error *
1421 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1422 struct got_worktree *worktree)
1424 const struct got_error *err = NULL;
1427 *fileindex_path = NULL;
1428 *fileindex = got_fileindex_alloc();
1429 if (*fileindex == NULL)
1430 return got_error_from_errno("got_fileindex_alloc");
1432 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1433 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1434 err = got_error_from_errno("asprintf");
1435 *fileindex_path = NULL;
1439 index = fopen(*fileindex_path, "rb");
1440 if (index == NULL) {
1441 if (errno != ENOENT)
1442 err = got_error_from_errno2("fopen", *fileindex_path);
1444 err = got_fileindex_read(*fileindex, index);
1445 if (fclose(index) != 0 && err == NULL)
1446 err = got_error_from_errno("fclose");
1450 free(*fileindex_path);
1451 *fileindex_path = NULL;
1458 struct bump_base_commit_id_arg {
1459 struct got_object_id *base_commit_id;
1462 const char *entry_name;
1463 got_worktree_checkout_cb progress_cb;
1467 /* Bump base commit ID of all files within an updated part of the work tree. */
1468 static const struct got_error *
1469 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1471 struct bump_base_commit_id_arg *a = arg;
1473 if (a->entry_name) {
1474 if (strcmp(ie->path, a->path) != 0)
1476 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1479 (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1480 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1484 static const struct got_error *
1485 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1487 const struct got_error *err = NULL;
1488 char *new_fileindex_path = NULL;
1489 FILE *new_index = NULL;
1491 err = got_opentemp_named(&new_fileindex_path, &new_index,
1496 err = got_fileindex_write(fileindex, new_index);
1500 if (rename(new_fileindex_path, fileindex_path) != 0) {
1501 err = got_error_from_errno3("rename", new_fileindex_path,
1503 unlink(new_fileindex_path);
1508 free(new_fileindex_path);
1512 const struct got_error *
1513 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1514 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1515 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1517 const struct got_error *err = NULL, *sync_err, *unlockerr;
1518 struct got_commit_object *commit = NULL;
1519 struct got_object_id *tree_id = NULL;
1520 struct got_tree_object *tree = NULL;
1521 struct got_fileindex *fileindex = NULL;
1522 char *fileindex_path = NULL;
1523 struct got_fileindex_diff_tree_cb diff_cb;
1524 struct diff_cb_arg arg;
1525 char *relpath = NULL, *entry_name = NULL;
1526 struct bump_base_commit_id_arg bbc_arg;
1528 err = lock_worktree(worktree, LOCK_EX);
1533 * Read the file index.
1534 * Checking out files is supposed to be an idempotent operation.
1535 * If the on-disk file index is incomplete we will try to complete it.
1537 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1541 err = ref_base_commit(worktree, repo);
1545 err = got_object_open_as_commit(&commit, repo,
1546 worktree->base_commit_id);
1553 relpath = strdup(path);
1554 if (relpath == NULL) {
1555 err = got_error_from_errno("strdup");
1558 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1559 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1561 err = got_error_from_errno("asprintf");
1564 err = got_object_id_by_path(&tree_id, repo,
1565 worktree->base_commit_id, tree_path);
1569 err = got_object_get_type(&obj_type, repo, tree_id);
1572 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1573 /* Split provided path into parent dir + entry name. */
1574 if (strchr(path, '/') == NULL) {
1575 relpath = strdup("");
1576 if (relpath == NULL) {
1577 err = got_error_from_errno("strdup");
1580 tree_path = strdup(worktree->path_prefix);
1581 if (tree_path == NULL) {
1582 err = got_error_from_errno("strdup");
1586 err = got_path_dirname(&relpath, path);
1589 if (asprintf(&tree_path, "%s%s%s",
1590 worktree->path_prefix,
1591 got_path_is_root_dir(
1592 worktree->path_prefix) ? "" : "/",
1594 err = got_error_from_errno("asprintf");
1598 err = got_object_id_by_path(&tree_id, repo,
1599 worktree->base_commit_id, tree_path);
1603 entry_name = basename(path);
1604 if (entry_name == NULL) {
1605 err = got_error_from_errno2("basename", path);
1610 relpath = strdup("");
1611 if (relpath == NULL) {
1612 err = got_error_from_errno("strdup");
1615 err = got_object_id_by_path(&tree_id, repo,
1616 worktree->base_commit_id, worktree->path_prefix);
1621 err = got_object_open_as_tree(&tree, repo, tree_id);
1626 got_object_tree_find_entry(tree, entry_name) == NULL) {
1627 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1631 diff_cb.diff_old_new = diff_old_new;
1632 diff_cb.diff_old = diff_old;
1633 diff_cb.diff_new = diff_new;
1634 arg.fileindex = fileindex;
1635 arg.worktree = worktree;
1637 arg.progress_cb = progress_cb;
1638 arg.progress_arg = progress_arg;
1639 arg.cancel_cb = cancel_cb;
1640 arg.cancel_arg = cancel_arg;
1641 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1642 entry_name, repo, &diff_cb, &arg);
1646 bbc_arg.base_commit_id = worktree->base_commit_id;
1647 bbc_arg.entry_name = entry_name;
1648 bbc_arg.path = path;
1649 bbc_arg.path_len = strlen(path);
1650 bbc_arg.progress_cb = progress_cb;
1651 bbc_arg.progress_arg = progress_arg;
1652 err = got_fileindex_for_each_entry_safe(fileindex,
1653 bump_base_commit_id, &bbc_arg);
1655 sync_err = sync_fileindex(fileindex, fileindex_path);
1656 if (sync_err && err == NULL)
1659 free(fileindex_path);
1662 got_object_tree_close(tree);
1664 got_object_commit_close(commit);
1665 got_fileindex_free(fileindex);
1666 unlockerr = lock_worktree(worktree, LOCK_SH);
1667 if (unlockerr && err == NULL)
1672 struct merge_file_cb_arg {
1673 struct got_worktree *worktree;
1674 struct got_fileindex *fileindex;
1675 got_worktree_checkout_cb progress_cb;
1677 got_worktree_cancel_cb cancel_cb;
1681 static const struct got_error *
1682 merge_file_cb(void *arg, struct got_blob_object *blob1,
1683 struct got_blob_object *blob2, struct got_object_id *id1,
1684 struct got_object_id *id2, const char *path1, const char *path2,
1685 struct got_repository *repo)
1687 static const struct got_error *err = NULL;
1688 struct merge_file_cb_arg *a = arg;
1689 struct got_fileindex_entry *ie;
1690 char *ondisk_path = NULL;
1692 unsigned char status;
1693 int local_changes_subsumed;
1695 if (blob1 && blob2) {
1696 ie = got_fileindex_entry_get(a->fileindex, path2);
1698 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1703 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1705 return got_error_from_errno("asprintf");
1707 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1711 if (status == GOT_STATUS_DELETE) {
1712 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1716 if (status != GOT_STATUS_NO_CHANGE &&
1717 status != GOT_STATUS_MODIFY &&
1718 status != GOT_STATUS_CONFLICT &&
1719 status != GOT_STATUS_ADD) {
1720 (*a->progress_cb)(a->progress_arg, status, path2);
1724 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1725 ondisk_path, path2, sb.st_mode, blob2, repo,
1726 a->progress_cb, a->progress_arg);
1728 ie = got_fileindex_entry_get(a->fileindex, path1);
1730 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1735 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1737 return got_error_from_errno("asprintf");
1739 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1744 case GOT_STATUS_NO_CHANGE:
1745 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1747 err = remove_ondisk_file(a->worktree->root_path, path1);
1751 got_fileindex_entry_mark_deleted_from_disk(ie);
1753 case GOT_STATUS_DELETE:
1754 case GOT_STATUS_MISSING:
1755 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1758 got_fileindex_entry_mark_deleted_from_disk(ie);
1760 case GOT_STATUS_ADD:
1761 case GOT_STATUS_MODIFY:
1762 case GOT_STATUS_CONFLICT:
1763 (*a->progress_cb)(a->progress_arg,
1764 GOT_STATUS_CANNOT_DELETE, path1);
1766 case GOT_STATUS_OBSTRUCTED:
1767 (*a->progress_cb)(a->progress_arg, status, path1);
1773 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1775 return got_error_from_errno("asprintf");
1776 ie = got_fileindex_entry_get(a->fileindex, path2);
1778 err = get_file_status(&status, &sb, ie, ondisk_path,
1782 if (status != GOT_STATUS_NO_CHANGE &&
1783 status != GOT_STATUS_MODIFY &&
1784 status != GOT_STATUS_CONFLICT &&
1785 status != GOT_STATUS_ADD) {
1786 (*a->progress_cb)(a->progress_arg, status,
1790 err = merge_blob(&local_changes_subsumed, a->worktree,
1791 NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1792 a->progress_cb, a->progress_arg);
1793 if (status == GOT_STATUS_DELETE) {
1794 err = update_blob_fileindex_entry(a->worktree,
1795 a->fileindex, ie, ondisk_path, ie->path,
1801 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1802 err = install_blob(a->worktree, ondisk_path, path2,
1803 /* XXX get this from parent tree! */
1804 GOT_DEFAULT_FILE_MODE,
1805 sb.st_mode, blob2, 0, 0, repo,
1806 a->progress_cb, a->progress_arg);
1809 err = got_fileindex_entry_alloc(&ie,
1810 ondisk_path, path2, NULL, NULL);
1813 err = got_fileindex_entry_add(a->fileindex, ie);
1815 got_fileindex_entry_free(ie);
1825 struct check_merge_ok_arg {
1826 struct got_worktree *worktree;
1827 struct got_repository *repo;
1830 static const struct got_error *
1831 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1833 const struct got_error *err = NULL;
1834 struct check_merge_ok_arg *a = arg;
1835 unsigned char status;
1839 /* Reject merges into a work tree with mixed base commits. */
1840 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1841 SHA1_DIGEST_LENGTH))
1842 return got_error(GOT_ERR_MIXED_COMMITS);
1844 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1846 return got_error_from_errno("asprintf");
1848 /* Reject merges into a work tree with conflicted files. */
1849 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1852 if (status == GOT_STATUS_CONFLICT)
1853 return got_error(GOT_ERR_CONFLICTS);
1858 const struct got_error *
1859 got_worktree_merge_files(struct got_worktree *worktree,
1860 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1861 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1862 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1864 const struct got_error *err = NULL, *sync_err, *unlockerr;
1865 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1866 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1867 struct merge_file_cb_arg arg;
1868 char *fileindex_path = NULL;
1869 struct got_fileindex *fileindex = NULL;
1870 struct check_merge_ok_arg mok_arg;
1872 err = lock_worktree(worktree, LOCK_EX);
1876 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1880 mok_arg.worktree = worktree;
1881 mok_arg.repo = repo;
1882 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1888 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1889 worktree->path_prefix);
1893 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1898 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1899 worktree->path_prefix);
1903 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1907 arg.worktree = worktree;
1908 arg.fileindex = fileindex;
1909 arg.progress_cb = progress_cb;
1910 arg.progress_arg = progress_arg;
1911 arg.cancel_cb = cancel_cb;
1912 arg.cancel_arg = cancel_arg;
1913 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1914 sync_err = sync_fileindex(fileindex, fileindex_path);
1915 if (sync_err && err == NULL)
1918 got_fileindex_free(fileindex);
1920 got_object_tree_close(tree1);
1922 got_object_tree_close(tree2);
1924 unlockerr = lock_worktree(worktree, LOCK_SH);
1925 if (unlockerr && err == NULL)
1930 struct diff_dir_cb_arg {
1931 struct got_fileindex *fileindex;
1932 struct got_worktree *worktree;
1933 const char *status_path;
1934 size_t status_path_len;
1935 struct got_repository *repo;
1936 got_worktree_status_cb status_cb;
1938 got_worktree_cancel_cb cancel_cb;
1942 static const struct got_error *
1943 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1944 got_worktree_status_cb status_cb, void *status_arg,
1945 struct got_repository *repo)
1947 const struct got_error *err = NULL;
1948 unsigned char status = GOT_STATUS_NO_CHANGE;
1950 struct got_object_id blob_id, commit_id;
1952 err = get_file_status(&status, &sb, ie, abspath, repo);
1953 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1954 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1955 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1956 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1962 static const struct got_error *
1963 status_old_new(void *arg, struct got_fileindex_entry *ie,
1964 struct dirent *de, const char *parent_path)
1966 const struct got_error *err = NULL;
1967 struct diff_dir_cb_arg *a = arg;
1970 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1971 return got_error(GOT_ERR_CANCELLED);
1973 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1974 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1977 if (parent_path[0]) {
1978 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1979 parent_path, de->d_name) == -1)
1980 return got_error_from_errno("asprintf");
1982 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1984 return got_error_from_errno("asprintf");
1987 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1993 static const struct got_error *
1994 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1996 struct diff_dir_cb_arg *a = arg;
1997 struct got_object_id blob_id, commit_id;
1998 unsigned char status;
2000 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2001 return got_error(GOT_ERR_CANCELLED);
2003 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2006 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2007 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2008 if (got_fileindex_entry_has_file_on_disk(ie))
2009 status = GOT_STATUS_MISSING;
2011 status = GOT_STATUS_DELETE;
2012 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2016 static const struct got_error *
2017 status_new(void *arg, struct dirent *de, const char *parent_path)
2019 const struct got_error *err = NULL;
2020 struct diff_dir_cb_arg *a = arg;
2023 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2024 return got_error(GOT_ERR_CANCELLED);
2026 if (de->d_type == DT_DIR)
2029 /* XXX ignore symlinks for now */
2030 if (de->d_type == DT_LNK)
2033 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2036 if (parent_path[0]) {
2037 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2038 return got_error_from_errno("asprintf");
2043 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2050 const struct got_error *
2051 got_worktree_status(struct got_worktree *worktree, const char *path,
2052 struct got_repository *repo, got_worktree_status_cb status_cb,
2053 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2055 const struct got_error *err = NULL;
2056 DIR *workdir = NULL;
2057 char *fileindex_path = NULL;
2058 struct got_fileindex *fileindex = NULL;
2060 struct got_fileindex_diff_dir_cb fdiff_cb;
2061 struct diff_dir_cb_arg arg;
2062 char *ondisk_path = NULL;
2064 fileindex = got_fileindex_alloc();
2065 if (fileindex == NULL) {
2066 err = got_error_from_errno("got_fileindex_alloc");
2070 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2071 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2072 err = got_error_from_errno("asprintf");
2073 fileindex_path = NULL;
2077 index = fopen(fileindex_path, "rb");
2078 if (index == NULL) {
2079 if (errno != ENOENT) {
2080 err = got_error_from_errno2("fopen", fileindex_path);
2084 err = got_fileindex_read(fileindex, index);
2090 if (asprintf(&ondisk_path, "%s%s%s",
2091 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2092 err = got_error_from_errno("asprintf");
2095 workdir = opendir(ondisk_path);
2096 if (workdir == NULL) {
2097 if (errno == ENOTDIR || errno == ENOENT) {
2098 struct got_fileindex_entry *ie;
2099 ie = got_fileindex_entry_get(fileindex, path);
2101 err = got_error(GOT_ERR_BAD_PATH);
2104 err = report_file_status(ie, ondisk_path,
2105 status_cb, status_arg, repo);
2108 err = got_error_from_errno2("opendir", ondisk_path);
2112 fdiff_cb.diff_old_new = status_old_new;
2113 fdiff_cb.diff_old = status_old;
2114 fdiff_cb.diff_new = status_new;
2115 arg.fileindex = fileindex;
2116 arg.worktree = worktree;
2117 arg.status_path = path;
2118 arg.status_path_len = strlen(path);
2120 arg.status_cb = status_cb;
2121 arg.status_arg = status_arg;
2122 arg.cancel_cb = cancel_cb;
2123 arg.cancel_arg = cancel_arg;
2124 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2125 path, repo, &fdiff_cb, &arg);
2130 free(fileindex_path);
2131 got_fileindex_free(fileindex);
2135 const struct got_error *
2136 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2139 const struct got_error *err = NULL;
2140 char *resolved, *path = NULL;
2145 resolved = realpath(arg, NULL);
2146 if (resolved == NULL)
2147 return got_error_from_errno2("realpath", arg);
2149 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2150 strlen(got_worktree_get_root_path(worktree)))) {
2151 err = got_error(GOT_ERR_BAD_PATH);
2155 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2156 err = got_path_skip_common_ancestor(&path,
2157 got_worktree_get_root_path(worktree), resolved);
2163 err = got_error_from_errno("strdup");
2168 /* XXX status walk can't deal with trailing slash! */
2170 while (path[len - 1] == '/') {
2171 path[len - 1] = '\0';
2183 static const struct got_error *
2184 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2185 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2186 struct got_repository *repo)
2188 const struct got_error *err = NULL;
2189 struct got_fileindex_entry *ie;
2191 /* Re-adding an existing entry is a no-op. */
2192 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2195 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2199 err = got_fileindex_entry_add(fileindex, ie);
2201 got_fileindex_entry_free(ie);
2205 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2208 const struct got_error *
2209 got_worktree_schedule_add(struct got_worktree *worktree,
2210 struct got_pathlist_head *ondisk_paths,
2211 got_worktree_status_cb status_cb, void *status_arg,
2212 struct got_repository *repo)
2214 struct got_fileindex *fileindex = NULL;
2215 char *fileindex_path = NULL;
2217 const struct got_error *err = NULL, *sync_err, *unlockerr;
2218 struct got_pathlist_entry *pe;
2220 err = lock_worktree(worktree, LOCK_EX);
2225 fileindex = got_fileindex_alloc();
2226 if (fileindex == NULL) {
2227 err = got_error_from_errno("got_fileindex_alloc");
2231 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2232 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2233 err = got_error_from_errno("asprintf");
2234 fileindex_path = NULL;
2238 index = fopen(fileindex_path, "rb");
2239 if (index == NULL) {
2240 err = got_error_from_errno2("fopen", fileindex_path);
2244 err = got_fileindex_read(fileindex, index);
2248 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2250 err = got_path_skip_common_ancestor(&relpath,
2251 got_worktree_get_root_path(worktree), pe->path);
2254 err = schedule_addition(pe->path, fileindex, relpath,
2255 status_cb, status_arg, repo);
2260 sync_err = sync_fileindex(fileindex, fileindex_path);
2261 if (sync_err && err == NULL)
2265 if (fclose(index) != 0 && err == NULL)
2266 err = got_error_from_errno("fclose");
2269 got_fileindex_free(fileindex);
2270 unlockerr = lock_worktree(worktree, LOCK_SH);
2271 if (unlockerr && err == NULL)
2276 static const struct got_error *
2277 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2278 const char *relpath, int delete_local_mods,
2279 got_worktree_status_cb status_cb, void *status_arg,
2280 struct got_repository *repo)
2282 const struct got_error *err = NULL;
2283 struct got_fileindex_entry *ie = NULL;
2284 unsigned char status;
2287 ie = got_fileindex_entry_get(fileindex, relpath);
2289 return got_error(GOT_ERR_BAD_PATH);
2291 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2295 if (status != GOT_STATUS_NO_CHANGE) {
2296 if (status == GOT_STATUS_DELETE)
2297 return got_error_set_errno(ENOENT, ondisk_path);
2298 if (status != GOT_STATUS_MODIFY)
2299 return got_error(GOT_ERR_FILE_STATUS);
2300 if (!delete_local_mods)
2301 return got_error(GOT_ERR_FILE_MODIFIED);
2304 if (unlink(ondisk_path) != 0)
2305 return got_error_from_errno2("unlink", ondisk_path);
2307 got_fileindex_entry_mark_deleted_from_disk(ie);
2308 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2311 const struct got_error *
2312 got_worktree_schedule_delete(struct got_worktree *worktree,
2313 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2314 got_worktree_status_cb status_cb, void *status_arg,
2315 struct got_repository *repo)
2317 struct got_fileindex *fileindex = NULL;
2318 char *fileindex_path = NULL;
2320 const struct got_error *err = NULL, *sync_err, *unlockerr;
2321 struct got_pathlist_entry *pe;
2323 err = lock_worktree(worktree, LOCK_EX);
2327 fileindex = got_fileindex_alloc();
2328 if (fileindex == NULL) {
2329 err = got_error_from_errno("got_fileindex_alloc");
2333 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2334 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2335 err = got_error_from_errno("asprintf");
2336 fileindex_path = NULL;
2340 index = fopen(fileindex_path, "rb");
2341 if (index == NULL) {
2342 err = got_error_from_errno2("fopen", fileindex_path);
2346 err = got_fileindex_read(fileindex, index);
2350 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2352 err = got_path_skip_common_ancestor(&relpath,
2353 got_worktree_get_root_path(worktree), pe->path);
2356 err = schedule_for_deletion(pe->path, fileindex, relpath,
2357 delete_local_mods, status_cb, status_arg, repo);
2362 sync_err = sync_fileindex(fileindex, fileindex_path);
2363 if (sync_err && err == NULL)
2367 if (fclose(index) != 0 && err == NULL)
2368 err = got_error_from_errno("fclose");
2371 got_fileindex_free(fileindex);
2372 unlockerr = lock_worktree(worktree, LOCK_SH);
2373 if (unlockerr && err == NULL)
2378 static const struct got_error *
2379 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2380 const char *ondisk_path,
2381 got_worktree_checkout_cb progress_cb, void *progress_arg,
2382 struct got_repository *repo)
2384 const struct got_error *err = NULL;
2385 char *relpath = NULL, *parent_path = NULL;
2386 struct got_fileindex_entry *ie;
2387 struct got_tree_object *tree = NULL;
2388 struct got_object_id *tree_id = NULL;
2389 const struct got_tree_entry *te;
2390 char *tree_path = NULL, *te_name;
2391 struct got_blob_object *blob = NULL;
2392 unsigned char status;
2395 err = got_path_skip_common_ancestor(&relpath,
2396 got_worktree_get_root_path(worktree), ondisk_path);
2400 ie = got_fileindex_entry_get(fileindex, relpath);
2402 err = got_error(GOT_ERR_BAD_PATH);
2406 /* Construct in-repository path of tree which contains this blob. */
2407 err = got_path_dirname(&parent_path, ie->path);
2409 if (err->code != GOT_ERR_BAD_PATH)
2411 parent_path = strdup("/");
2412 if (parent_path == NULL) {
2413 err = got_error_from_errno("strdup");
2417 if (got_path_is_root_dir(worktree->path_prefix)) {
2418 tree_path = strdup(parent_path);
2419 if (tree_path == NULL) {
2420 err = got_error_from_errno("strdup");
2424 if (got_path_is_root_dir(parent_path)) {
2425 tree_path = strdup(worktree->path_prefix);
2426 if (tree_path == NULL) {
2427 err = got_error_from_errno("strdup");
2431 if (asprintf(&tree_path, "%s/%s",
2432 worktree->path_prefix, parent_path) == -1) {
2433 err = got_error_from_errno("asprintf");
2439 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2444 err = got_object_open_as_tree(&tree, repo, tree_id);
2448 te_name = basename(ie->path);
2449 if (te_name == NULL) {
2450 err = got_error_from_errno2("basename", ie->path);
2454 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2458 te = got_object_tree_find_entry(tree, te_name);
2459 if (te == NULL && status != GOT_STATUS_ADD) {
2460 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2465 case GOT_STATUS_ADD:
2466 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2467 got_fileindex_entry_remove(fileindex, ie);
2469 case GOT_STATUS_DELETE:
2470 case GOT_STATUS_MODIFY:
2471 case GOT_STATUS_CONFLICT:
2472 case GOT_STATUS_MISSING: {
2473 struct got_object_id id;
2474 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2475 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2478 err = install_blob(worktree, ondisk_path, ie->path,
2479 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2483 if (status == GOT_STATUS_DELETE) {
2484 err = update_blob_fileindex_entry(worktree,
2485 fileindex, ie, ondisk_path, ie->path, blob, 1);
2499 got_object_blob_close(blob);
2501 got_object_tree_close(tree);
2506 const struct got_error *
2507 got_worktree_revert(struct got_worktree *worktree,
2508 struct got_pathlist_head *ondisk_paths,
2509 got_worktree_checkout_cb progress_cb, void *progress_arg,
2510 struct got_repository *repo)
2512 struct got_fileindex *fileindex = NULL;
2513 char *fileindex_path = NULL;
2515 const struct got_error *err = NULL, *unlockerr = NULL;
2516 const struct got_error *sync_err = NULL;
2517 struct got_pathlist_entry *pe;
2519 err = lock_worktree(worktree, LOCK_EX);
2523 fileindex = got_fileindex_alloc();
2524 if (fileindex == NULL) {
2525 err = got_error_from_errno("got_fileindex_alloc");
2529 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2530 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2531 err = got_error_from_errno("asprintf");
2532 fileindex_path = NULL;
2536 index = fopen(fileindex_path, "rb");
2537 if (index == NULL) {
2538 err = got_error_from_errno2("fopen", fileindex_path);
2542 err = got_fileindex_read(fileindex, index);
2546 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2547 err = revert_file(worktree, fileindex, pe->path,
2548 progress_cb, progress_arg, repo);
2552 sync_err = sync_fileindex(fileindex, fileindex_path);
2553 if (sync_err && err == NULL)
2557 if (fclose(index) != 0 && err == NULL)
2558 err = got_error_from_errno("fclose");
2561 got_fileindex_free(fileindex);
2562 unlockerr = lock_worktree(worktree, LOCK_SH);
2563 if (unlockerr && err == NULL)
2569 free_commitable(struct got_commitable *ct)
2572 free(ct->in_repo_path);
2573 free(ct->ondisk_path);
2575 free(ct->base_blob_id);
2576 free(ct->base_commit_id);
2580 struct collect_commitables_arg {
2581 struct got_pathlist_head *commitable_paths;
2582 struct got_repository *repo;
2583 struct got_worktree *worktree;
2586 static const struct got_error *
2587 collect_commitables(void *arg, unsigned char status, const char *relpath,
2588 struct got_object_id *blob_id, struct got_object_id *commit_id)
2590 struct collect_commitables_arg *a = arg;
2591 const struct got_error *err = NULL;
2592 struct got_commitable *ct = NULL;
2593 struct got_pathlist_entry *new = NULL;
2594 char *parent_path = NULL, *path = NULL;
2597 if (status == GOT_STATUS_CONFLICT)
2598 return got_error(GOT_ERR_COMMIT_CONFLICT);
2600 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2601 status != GOT_STATUS_DELETE)
2604 if (asprintf(&path, "/%s", relpath) == -1) {
2605 err = got_error_from_errno("asprintf");
2608 if (strcmp(path, "/") == 0) {
2609 parent_path = strdup("");
2610 if (parent_path == NULL)
2611 return got_error_from_errno("strdup");
2613 err = got_path_dirname(&parent_path, path);
2618 ct = calloc(1, sizeof(*ct));
2620 err = got_error_from_errno("calloc");
2624 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2626 err = got_error_from_errno("asprintf");
2629 if (status == GOT_STATUS_DELETE) {
2630 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2632 if (lstat(ct->ondisk_path, &sb) != 0) {
2633 err = got_error_from_errno2("lstat", ct->ondisk_path);
2636 ct->mode = sb.st_mode;
2639 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2640 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2642 err = got_error_from_errno("asprintf");
2646 ct->status = status;
2647 ct->blob_id = NULL; /* will be filled in when blob gets created */
2648 if (ct->status != GOT_STATUS_ADD) {
2649 ct->base_blob_id = got_object_id_dup(blob_id);
2650 if (ct->base_blob_id == NULL) {
2651 err = got_error_from_errno("got_object_id_dup");
2654 ct->base_commit_id = got_object_id_dup(commit_id);
2655 if (ct->base_commit_id == NULL) {
2656 err = got_error_from_errno("got_object_id_dup");
2660 ct->path = strdup(path);
2661 if (ct->path == NULL) {
2662 err = got_error_from_errno("strdup");
2665 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2667 if (ct && (err || new == NULL))
2668 free_commitable(ct);
2674 static const struct got_error *write_tree(struct got_object_id **,
2675 struct got_tree_object *, const char *, struct got_pathlist_head *,
2676 got_worktree_status_cb status_cb, void *status_arg,
2677 struct got_repository *);
2679 static const struct got_error *
2680 write_subtree(struct got_object_id **new_subtree_id,
2681 struct got_tree_entry *te, const char *parent_path,
2682 struct got_pathlist_head *commitable_paths,
2683 got_worktree_status_cb status_cb, void *status_arg,
2684 struct got_repository *repo)
2686 const struct got_error *err = NULL;
2687 struct got_tree_object *subtree;
2690 if (asprintf(&subpath, "%s%s%s", parent_path,
2691 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2692 return got_error_from_errno("asprintf");
2694 err = got_object_open_as_tree(&subtree, repo, te->id);
2698 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2699 status_cb, status_arg, repo);
2700 got_object_tree_close(subtree);
2705 static const struct got_error *
2706 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2708 const struct got_error *err = NULL;
2709 char *ct_parent_path = NULL;
2713 if (strchr(ct->path, '/') == NULL) {
2714 *match = got_path_is_root_dir(path);
2718 err = got_path_dirname(&ct_parent_path, ct->path);
2721 *match = (strcmp(path, ct_parent_path) == 0);
2722 free(ct_parent_path);
2727 get_ct_file_mode(struct got_commitable *ct)
2729 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2732 static const struct got_error *
2733 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2734 struct got_tree_entry *te, struct got_commitable *ct)
2736 const struct got_error *err = NULL;
2740 err = got_object_tree_entry_dup(new_te, te);
2744 (*new_te)->mode = get_ct_file_mode(ct);
2746 free((*new_te)->id);
2747 (*new_te)->id = got_object_id_dup(ct->blob_id);
2748 if ((*new_te)->id == NULL) {
2749 err = got_error_from_errno("got_object_id_dup");
2753 if (err && *new_te) {
2754 got_object_tree_entry_close(*new_te);
2760 static const struct got_error *
2761 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2762 struct got_commitable *ct)
2764 const struct got_error *err = NULL;
2769 *new_te = calloc(1, sizeof(**new_te));
2770 if (*new_te == NULL)
2771 return got_error_from_errno("calloc");
2773 ct_name = basename(ct->path);
2774 if (ct_name == NULL) {
2775 err = got_error_from_errno2("basename", ct->path);
2778 (*new_te)->name = strdup(ct_name);
2779 if ((*new_te)->name == NULL) {
2780 err = got_error_from_errno("strdup");
2784 (*new_te)->mode = get_ct_file_mode(ct);
2786 (*new_te)->id = got_object_id_dup(ct->blob_id);
2787 if ((*new_te)->id == NULL) {
2788 err = got_error_from_errno("got_object_id_dup");
2792 if (err && *new_te) {
2793 got_object_tree_entry_close(*new_te);
2799 static const struct got_error *
2800 insert_tree_entry(struct got_tree_entry *new_te,
2801 struct got_pathlist_head *paths)
2803 const struct got_error *err = NULL;
2804 struct got_pathlist_entry *new_pe;
2806 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2810 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2814 static const struct got_error *
2815 report_ct_status(struct got_commitable *ct,
2816 got_worktree_status_cb status_cb, void *status_arg)
2818 const char *ct_path = ct->path;
2819 while (ct_path[0] == '/')
2821 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2824 static const struct got_error *
2825 match_modified_subtree(int *modified, struct got_tree_entry *te,
2826 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2828 const struct got_error *err = NULL;
2829 struct got_pathlist_entry *pe;
2834 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2835 got_path_is_root_dir(base_tree_path) ? "" : "/",
2837 return got_error_from_errno("asprintf");
2839 TAILQ_FOREACH(pe, commitable_paths, entry) {
2840 struct got_commitable *ct = pe->data;
2841 *modified = got_path_is_child(ct->in_repo_path, te_path,
2851 static const struct got_error *
2852 match_deleted_or_modified_ct(struct got_commitable **ctp,
2853 struct got_tree_entry *te, const char *base_tree_path,
2854 struct got_pathlist_head *commitable_paths)
2856 const struct got_error *err = NULL;
2857 struct got_pathlist_entry *pe;
2861 TAILQ_FOREACH(pe, commitable_paths, entry) {
2862 struct got_commitable *ct = pe->data;
2863 char *ct_name = NULL;
2866 if (ct->status != GOT_STATUS_MODIFY &&
2867 ct->status != GOT_STATUS_DELETE)
2870 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2873 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2879 ct_name = basename(pe->path);
2880 if (ct_name == NULL)
2881 return got_error_from_errno2("basename", pe->path);
2883 if (strcmp(te->name, ct_name) != 0)
2893 static const struct got_error *
2894 write_tree(struct got_object_id **new_tree_id,
2895 struct got_tree_object *base_tree, const char *path_base_tree,
2896 struct got_pathlist_head *commitable_paths,
2897 got_worktree_status_cb status_cb, void *status_arg,
2898 struct got_repository *repo)
2900 const struct got_error *err = NULL;
2901 const struct got_tree_entries *base_entries = NULL;
2902 struct got_pathlist_head paths;
2903 struct got_tree_entries new_tree_entries;
2904 struct got_tree_entry *te, *new_te = NULL;
2905 struct got_pathlist_entry *pe;
2908 new_tree_entries.nentries = 0;
2909 SIMPLEQ_INIT(&new_tree_entries.head);
2911 /* Insert, and recurse into, newly added entries first. */
2912 TAILQ_FOREACH(pe, commitable_paths, entry) {
2913 struct got_commitable *ct = pe->data;
2914 char *child_path = NULL, *slash;
2916 if (ct->status != GOT_STATUS_ADD ||
2917 (ct->flags & GOT_COMMITABLE_ADDED))
2920 if (!got_path_is_child(pe->path, path_base_tree,
2921 strlen(path_base_tree)))
2924 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2929 slash = strchr(child_path, '/');
2930 if (slash == NULL) {
2931 err = alloc_added_blob_tree_entry(&new_te, ct);
2934 err = report_ct_status(ct, status_cb, status_arg);
2937 ct->flags |= GOT_COMMITABLE_ADDED;
2941 *slash = '\0'; /* trim trailing path components */
2942 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2943 got_path_is_root_dir(path_base_tree) ? "" : "/",
2944 child_path) == -1) {
2945 err = got_error_from_errno("asprintf");
2949 new_te = calloc(1, sizeof(*new_te));
2950 new_te->mode = S_IFDIR;
2951 new_te->name = strdup(child_path);
2952 if (new_te->name == NULL) {
2953 err = got_error_from_errno("strdup");
2954 got_object_tree_entry_close(new_te);
2958 err = write_tree(&new_te->id, NULL, subtree_path,
2959 commitable_paths, status_cb, status_arg, repo);
2962 got_object_tree_entry_close(new_te);
2967 err = insert_tree_entry(new_te, &paths);
2973 /* Handle modified and deleted entries. */
2974 base_entries = got_object_tree_get_entries(base_tree);
2975 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2976 struct got_commitable *ct = NULL;
2978 if (S_ISDIR(te->mode)) {
2980 err = got_object_tree_entry_dup(&new_te, te);
2983 err = match_modified_subtree(&modified, te,
2984 path_base_tree, commitable_paths);
2987 /* Avoid recursion into unmodified subtrees. */
2990 err = write_subtree(&new_te->id, te,
2991 path_base_tree, commitable_paths,
2992 status_cb, status_arg, repo);
2996 err = insert_tree_entry(new_te, &paths);
3002 err = match_deleted_or_modified_ct(&ct, te,
3003 path_base_tree, commitable_paths);
3005 /* NB: Deleted entries get dropped here. */
3006 if (ct->status == GOT_STATUS_MODIFY) {
3007 err = alloc_modified_blob_tree_entry(
3011 err = insert_tree_entry(new_te, &paths);
3015 err = report_ct_status(ct, status_cb,
3020 /* Entry is unchanged; just copy it. */
3021 err = got_object_tree_entry_dup(&new_te, te);
3024 err = insert_tree_entry(new_te, &paths);
3031 /* Write new list of entries; deleted entries have been dropped. */
3032 TAILQ_FOREACH(pe, &paths, entry) {
3033 struct got_tree_entry *te = pe->data;
3034 new_tree_entries.nentries++;
3035 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3037 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3039 got_object_tree_entries_close(&new_tree_entries);
3040 got_pathlist_free(&paths);
3044 static const struct got_error *
3045 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3046 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3048 const struct got_error *err = NULL, *sync_err;
3049 char *fileindex_path = NULL;
3050 struct got_fileindex *fileindex = NULL;
3051 struct got_pathlist_entry *pe;
3053 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3057 TAILQ_FOREACH(pe, commitable_paths, entry) {
3058 struct got_fileindex_entry *ie;
3059 struct got_commitable *ct = pe->data;
3061 ie = got_fileindex_entry_get(fileindex, pe->path);
3063 if (ct->status == GOT_STATUS_DELETE) {
3064 got_fileindex_entry_remove(fileindex, ie);
3065 got_fileindex_entry_free(ie);
3067 err = got_fileindex_entry_update(ie,
3068 ct->ondisk_path, ct->blob_id->sha1,
3069 new_base_commit_id->sha1, 1);
3071 err = got_fileindex_entry_alloc(&ie,
3072 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3073 new_base_commit_id->sha1);
3076 err = got_fileindex_entry_add(fileindex, ie);
3081 sync_err = sync_fileindex(fileindex, fileindex_path);
3082 if (sync_err && err == NULL)
3084 free(fileindex_path);
3085 got_fileindex_free(fileindex);
3089 static const struct got_error *
3090 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3091 struct got_object_id *head_commit_id)
3093 const struct got_error *err = NULL;
3094 struct got_object_id *id_in_head = NULL, *id = NULL;
3095 struct got_commit_object *commit = NULL;
3097 const char *ct_path = ct->in_repo_path;
3099 while (ct_path[0] == '/')
3103 * Ensure that no modifications were made to files *and their parents*
3104 * in commits between the file's base commit and the branch head.
3106 * Checking the parents is important for detecting conflicting tree
3107 * configurations (files or parent folders might have been moved,
3108 * deleted, added again, etc.). Such changes need to be merged with
3109 * local changes before a commit can occur.
3111 * The implication is that the file's (parent) entry in the root
3112 * directory must have the same ID in all relevant commits.
3114 if (ct->status != GOT_STATUS_ADD) {
3115 struct got_object_qid *pid;
3117 struct got_object_id *root_entry_id = NULL;
3119 /* Trivial case: base commit == head commit */
3120 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3123 /* Compute the path to the root directory's entry. */
3124 path = strdup(ct_path);
3126 err = got_error_from_errno("strdup");
3129 slash = strchr(path, '/');
3133 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3137 err = got_object_id_by_path(&root_entry_id, repo,
3138 head_commit_id, path);
3142 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3144 struct got_commit_object *pcommit;
3146 err = got_object_id_by_path(&id, repo, pid->id, path);
3148 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3154 err = got_object_id_by_path(&id, repo, pid->id, path);
3158 if (got_object_id_cmp(id, root_entry_id) != 0) {
3159 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3163 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3164 break; /* all relevant commits scanned */
3166 err = got_object_open_as_commit(&pcommit, repo,
3171 got_object_commit_close(commit);
3173 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3177 /* Require that added files don't exist in the branch head. */
3178 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3180 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3182 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3186 got_object_commit_close(commit);
3193 const struct got_error *
3194 got_worktree_commit(struct got_object_id **new_commit_id,
3195 struct got_worktree *worktree, const char *ondisk_path,
3196 const char *author, const char *committer,
3197 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3198 got_worktree_status_cb status_cb, void *status_arg,
3199 struct got_repository *repo)
3201 const struct got_error *err = NULL, *unlockerr = NULL;
3202 struct collect_commitables_arg cc_arg;
3203 struct got_pathlist_head commitable_paths;
3204 struct got_pathlist_entry *pe;
3205 char *relpath = NULL;
3206 const char *head_ref_name = NULL;
3207 struct got_reference *head_ref = NULL;
3208 struct got_commit_object *head_commit = NULL;
3209 struct got_object_id *head_commit_id = NULL;
3210 struct got_reference *head_ref2 = NULL;
3211 struct got_object_id *head_commit_id2 = NULL;
3212 struct got_tree_object *head_tree = NULL;
3213 struct got_object_id *new_tree_id = NULL;
3214 struct got_object_id_queue parent_ids;
3215 struct got_object_qid *pid = NULL;
3216 char *logmsg = NULL;
3218 *new_commit_id = NULL;
3220 TAILQ_INIT(&commitable_paths);
3221 SIMPLEQ_INIT(&parent_ids);
3224 err = got_path_skip_common_ancestor(&relpath,
3225 worktree->root_path, ondisk_path);
3230 err = lock_worktree(worktree, LOCK_EX);
3234 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3237 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3241 cc_arg.commitable_paths = &commitable_paths;
3242 cc_arg.worktree = worktree;
3244 err = got_worktree_status(worktree, relpath ? relpath : "",
3245 repo, collect_commitables, &cc_arg, NULL, NULL);
3249 if (TAILQ_EMPTY(&commitable_paths)) {
3250 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3254 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3258 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3259 struct got_commitable *ct = pe->data;
3260 err = check_ct_out_of_date(ct, repo, head_commit_id);
3265 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3269 if (commit_msg_cb != NULL) {
3270 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3275 if (logmsg == NULL || strlen(logmsg) == 0) {
3276 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3280 /* Create blobs from added and modified files and record their IDs. */
3281 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3282 struct got_commitable *ct = pe->data;
3285 if (ct->status != GOT_STATUS_ADD &&
3286 ct->status != GOT_STATUS_MODIFY)
3289 if (asprintf(&ondisk_path, "%s/%s",
3290 worktree->root_path, pe->path) == -1) {
3291 err = got_error_from_errno("asprintf");
3294 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3300 /* Recursively write new tree objects. */
3301 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3302 status_cb, status_arg, repo);
3306 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3309 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3310 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3311 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3312 got_object_qid_free(pid);
3318 /* Check if a concurrent commit to our branch has occurred. */
3319 head_ref_name = got_worktree_get_head_ref_name(worktree);
3320 if (head_ref_name == NULL) {
3321 err = got_error_from_errno("got_worktree_get_head_ref_name");
3324 /* Lock the reference here to prevent concurrent modification. */
3325 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3328 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3331 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3332 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3335 /* Update branch head in repository. */
3336 err = got_ref_change_ref(head_ref2, *new_commit_id);
3339 err = got_ref_write(head_ref2, repo);
3343 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3347 err = ref_base_commit(worktree, repo);
3351 err = update_fileindex_after_commit(&commitable_paths,
3352 *new_commit_id, worktree);
3356 unlockerr = lock_worktree(worktree, LOCK_SH);
3357 if (unlockerr && err == NULL)
3359 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3360 struct got_commitable *ct = pe->data;
3361 free_commitable(ct);
3363 got_pathlist_free(&commitable_paths);
3365 got_object_tree_close(head_tree);
3367 got_object_commit_close(head_commit);
3369 free(head_commit_id);
3370 free(head_commit_id2);
3372 got_ref_close(head_ref);
3374 unlockerr = got_ref_unlock(head_ref2);
3375 if (unlockerr && err == NULL)
3377 got_ref_close(head_ref2);
3383 got_commitable_get_path(struct got_commitable *ct)
3389 got_commitable_get_status(struct got_commitable *ct)