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;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 return got_error_from_errno("asprintf");
70 err = got_path_create_file(path, content);
75 static const struct got_error *
76 update_meta_file(const char *path_got, const char *name, const char *content)
78 const struct got_error *err = NULL;
83 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 err = got_error_from_errno("asprintf");
89 err = got_opentemp_named(&tmppath, &tmpfile, path);
94 int len = fprintf(tmpfile, "%s\n", content);
95 if (len != strlen(content) + 1) {
96 err = got_error_from_errno2("fprintf", tmppath);
101 if (rename(tmppath, path) != 0) {
102 err = got_error_from_errno3("rename", tmppath, path);
108 if (fclose(tmpfile) != 0 && err == NULL)
109 err = got_error_from_errno2("fclose", tmppath);
114 static const struct got_error *
115 read_meta_file(char **content, const char *path_got, const char *name)
117 const struct got_error *err = NULL;
125 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 err = got_error_from_errno("asprintf");
131 fd = open(path, O_RDONLY | O_NOFOLLOW);
134 err = got_error(GOT_ERR_WORKTREE_META);
136 err = got_error_from_errno2("open", path);
139 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 : got_error_from_errno2("flock", path));
145 if (lstat(path, &sb) != 0) {
146 err = got_error_from_errno2("lstat", path);
149 *content = calloc(1, sb.st_size);
150 if (*content == NULL) {
151 err = got_error_from_errno("calloc");
155 n = read(fd, *content, sb.st_size);
156 if (n != sb.st_size) {
157 err = (n == -1 ? got_error_from_errno2("read", path) :
158 got_error(GOT_ERR_WORKTREE_META));
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error(GOT_ERR_WORKTREE_META);
165 (*content)[sb.st_size - 1] = '\0';
168 if (fd != -1 && close(fd) == -1 && err == NULL)
169 err = got_error_from_errno2("close", path_got);
178 static const struct got_error *
179 write_head_ref(const char *path_got, struct got_reference *head_ref)
181 const struct got_error *err = NULL;
184 if (got_ref_is_symbolic(head_ref)) {
185 refstr = got_ref_to_str(head_ref);
187 return got_error_from_errno("got_ref_to_str");
189 refstr = strdup(got_ref_get_name(head_ref));
191 return got_error_from_errno("strdup");
193 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
205 uint32_t uuid_status;
207 char *path_got = NULL;
208 char *formatstr = NULL;
209 char *absprefix = NULL;
210 char *basestr = NULL;
211 char *uuidstr = NULL;
213 if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 err = got_error(GOT_ERR_WORKTREE_REPO);
218 err = got_ref_resolve(&commit_id, repo, head_ref);
221 err = got_object_get_type(&obj_type, repo, commit_id);
224 if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 return got_error(GOT_ERR_OBJ_TYPE);
227 if (!got_path_is_absolute(prefix)) {
228 if (asprintf(&absprefix, "/%s", prefix) == -1)
229 return got_error_from_errno("asprintf");
232 /* Create top-level directory (may already exist). */
233 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 err = got_error_from_errno2("mkdir", path);
238 /* Create .got directory (may already exist). */
239 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 err = got_error_from_errno("asprintf");
243 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 err = got_error_from_errno2("mkdir", path_got);
248 /* Create an empty lock file. */
249 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
253 /* Create an empty file index. */
254 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
258 /* Write the HEAD reference. */
259 err = write_head_ref(path_got, head_ref);
263 /* Record our base commit. */
264 err = got_object_id_str(&basestr, commit_id);
267 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
271 /* Store path to repository. */
272 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 got_repo_get_path(repo));
277 /* Store in-repository path prefix. */
278 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 absprefix ? absprefix : prefix);
284 uuid_create(&uuid, &uuid_status);
285 if (uuid_status != uuid_s_ok) {
286 err = got_error_uuid(uuid_status);
289 uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status);
294 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
298 /* Stamp work tree with format file. */
299 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 err = got_error_from_errno("asprintf");
303 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
317 static const struct got_error *
318 open_worktree(struct got_worktree **worktree, const char *path)
320 const struct got_error *err = NULL;
322 char *formatstr = NULL;
323 char *uuidstr = NULL;
324 char *path_lock = NULL;
325 char *base_commit_id_str = NULL;
326 int version, fd = -1;
328 struct got_repository *repo = NULL;
329 uint32_t uuid_status;
333 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
339 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 err = got_error_from_errno("asprintf");
345 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
347 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 : got_error_from_errno2("open", path_lock));
352 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
356 version = strtonum(formatstr, 1, INT_MAX, &errstr);
358 err = got_error(GOT_ERR_WORKTREE_META);
361 if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 err = got_error(GOT_ERR_WORKTREE_VERS);
366 *worktree = calloc(1, sizeof(**worktree));
367 if (*worktree == NULL) {
368 err = got_error_from_errno("calloc");
371 (*worktree)->lockfd = -1;
373 (*worktree)->root_path = strdup(path);
374 if ((*worktree)->root_path == NULL) {
375 err = got_error_from_errno("strdup");
378 err = read_meta_file(&(*worktree)->repo_path, path_got,
379 GOT_WORKTREE_REPOSITORY);
383 err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 GOT_WORKTREE_PATH_PREFIX);
388 err = read_meta_file(&base_commit_id_str, path_got,
389 GOT_WORKTREE_BASE_COMMIT);
393 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
396 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 if (uuid_status != uuid_s_ok) {
398 err = got_error_uuid(uuid_status);
402 err = got_repo_open(&repo, (*worktree)->repo_path);
406 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
411 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 GOT_WORKTREE_HEAD_REF);
415 got_repo_close(repo);
418 free(base_commit_id_str);
424 if (*worktree != NULL)
425 got_worktree_close(*worktree);
428 (*worktree)->lockfd = fd;
433 const struct got_error *
434 got_worktree_open(struct got_worktree **worktree, const char *path)
436 const struct got_error *err = NULL;
439 err = open_worktree(worktree, path);
440 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
444 path = dirname(path);
446 return got_error_from_errno2("dirname", path);
447 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 return got_error(GOT_ERR_NOT_WORKTREE);
452 const struct got_error *
453 got_worktree_close(struct got_worktree *worktree)
455 const struct got_error *err = NULL;
456 free(worktree->root_path);
457 free(worktree->repo_path);
458 free(worktree->path_prefix);
459 free(worktree->base_commit_id);
460 free(worktree->head_ref_name);
461 if (worktree->lockfd != -1)
462 if (close(worktree->lockfd) != 0)
463 err = got_error_from_errno2("close",
464 got_worktree_get_root_path(worktree));
470 got_worktree_get_root_path(struct got_worktree *worktree)
472 return worktree->root_path;
476 got_worktree_get_repo_path(struct got_worktree *worktree)
478 return worktree->repo_path;
482 got_worktree_get_path_prefix(struct got_worktree *worktree)
484 return worktree->path_prefix;
487 const struct got_error *
488 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 const char *path_prefix)
491 char *absprefix = NULL;
493 if (!got_path_is_absolute(path_prefix)) {
494 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 return got_error_from_errno("asprintf");
497 *match = (strcmp(absprefix ? absprefix : path_prefix,
498 worktree->path_prefix) == 0);
504 got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 return worktree->head_ref_name;
509 const struct got_error *
510 got_worktree_set_head_ref(struct got_worktree *worktree,
511 struct got_reference *head_ref)
513 const struct got_error *err = NULL;
514 char *path_got = NULL, *head_ref_name = NULL;
516 if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 GOT_WORKTREE_GOT_DIR) == -1) {
518 err = got_error_from_errno("asprintf");
523 head_ref_name = strdup(got_ref_get_name(head_ref));
524 if (head_ref_name == NULL) {
525 err = got_error_from_errno("strdup");
529 err = write_head_ref(path_got, head_ref);
533 free(worktree->head_ref_name);
534 worktree->head_ref_name = head_ref_name;
542 struct got_object_id *
543 got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 return worktree->base_commit_id;
548 const struct got_error *
549 got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 struct got_repository *repo, struct got_object_id *commit_id)
552 const struct got_error *err;
553 struct got_object *obj = NULL;
555 char *path_got = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
564 err = got_object_open(&obj, repo, commit_id);
568 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 err = got_error(GOT_ERR_OBJ_TYPE);
573 /* Record our base commit. */
574 err = got_object_id_str(&id_str, commit_id);
577 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
581 free(worktree->base_commit_id);
582 worktree->base_commit_id = got_object_id_dup(commit_id);
583 if (worktree->base_commit_id == NULL) {
584 err = got_error_from_errno("got_object_id_dup");
589 got_object_close(obj);
595 static const struct got_error *
596 lock_worktree(struct got_worktree *worktree, int operation)
598 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 : got_error_from_errno2("flock",
601 got_worktree_get_root_path(worktree)));
605 static const struct got_error *
606 add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 const struct got_error *err = NULL;
611 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 return got_error_from_errno("asprintf");
614 err = got_path_mkdir(abspath);
615 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
618 if (lstat(abspath, &sb) == -1) {
619 err = got_error_from_errno2("lstat", abspath);
620 } else if (!S_ISDIR(sb.st_mode)) {
621 /* TODO directory is obstructed; do something */
622 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
629 static const struct got_error *
630 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 const struct got_error *err = NULL;
635 size_t flen1 = 0, flen2 = 0;
640 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 if (flen1 == 0 && ferror(f1)) {
642 err = got_error_from_errno("fread");
645 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 if (flen2 == 0 && ferror(f2)) {
647 err = got_error_from_errno("fread");
654 } else if (flen2 == 0) {
658 } else if (flen1 == flen2) {
659 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
672 static const struct got_error *
673 check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 const struct got_error *err = NULL;
678 FILE *f1 = NULL, *f2 = NULL;
682 if (lstat(f1_path, &sb) != 0) {
683 err = got_error_from_errno2("lstat", f1_path);
688 if (lstat(f2_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f2_path);
694 if (size1 != size2) {
699 f1 = fopen(f1_path, "r");
701 return got_error_from_errno2("open", f1_path);
703 f2 = fopen(f2_path, "r");
705 err = got_error_from_errno2("open", f2_path);
709 err = check_file_contents_equal(same, f1, f2);
711 if (f1 && fclose(f1) != 0 && err == NULL)
712 err = got_error_from_errno("fclose");
713 if (f2 && fclose(f2) != 0 && err == NULL)
714 err = got_error_from_errno("fclose");
720 * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 * blob_deriv acts as the first derived version, and the file on disk
722 * acts as the second derived version.
724 static const struct got_error *
725 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 struct got_blob_object *blob_orig, const char *ondisk_path,
727 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
731 const struct got_error *err = NULL;
733 FILE *f_deriv = NULL, *f_orig = NULL;
734 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
735 char *merged_path = NULL, *base_path = NULL;
741 *local_changes_subsumed = 0;
743 parent = dirname(ondisk_path);
745 return got_error_from_errno2("dirname", ondisk_path);
747 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
748 return got_error_from_errno("asprintf");
750 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
755 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
756 err = got_error_from_errno("asprintf");
761 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
764 err = got_object_blob_dump_to_file(NULL, NULL, f_deriv, blob_deriv);
769 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
770 err = got_error_from_errno("asprintf");
775 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
779 err = got_object_blob_dump_to_file(NULL, NULL, f_orig,
785 * If the file has no blob, this is an "add vs add" conflict,
786 * and we simply use an empty ancestor file to make both files
787 * appear in the merged result in their entirety.
791 err = got_object_id_str(&id_str, worktree->base_commit_id);
794 if (asprintf(&label1, "commit %s", id_str) == -1) {
795 err = got_error_from_errno("asprintf");
799 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
800 blob_orig_path, ondisk_path, label1, path);
804 (*progress_cb)(progress_arg,
805 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
807 if (fsync(merged_fd) != 0) {
808 err = got_error_from_errno("fsync");
812 /* Check if a clean merge has subsumed all local changes. */
813 if (overlapcnt == 0) {
814 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
820 if (chmod(merged_path, st_mode) != 0) {
821 err = got_error_from_errno2("chmod", merged_path);
825 if (rename(merged_path, ondisk_path) != 0) {
826 err = got_error_from_errno3("rename", merged_path,
833 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
834 err = got_error_from_errno("close");
835 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
836 err = got_error_from_errno("fclose");
837 if (f_orig && fclose(f_orig) != 0 && err == NULL)
838 err = got_error_from_errno("fclose");
841 if (blob_deriv_path) {
842 unlink(blob_deriv_path);
843 free(blob_deriv_path);
845 if (blob_orig_path) {
846 unlink(blob_orig_path);
847 free(blob_orig_path);
854 static const struct got_error *
855 update_blob_fileindex_entry(struct got_worktree *worktree,
856 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
857 const char *ondisk_path, const char *path, struct got_blob_object *blob,
858 int update_timestamps)
860 const struct got_error *err = NULL;
863 ie = got_fileindex_entry_get(fileindex, path);
865 err = got_fileindex_entry_update(ie, ondisk_path,
866 blob->id.sha1, worktree->base_commit_id->sha1,
869 struct got_fileindex_entry *new_ie;
870 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
871 path, blob->id.sha1, worktree->base_commit_id->sha1);
873 err = got_fileindex_entry_add(fileindex, new_ie);
878 static const struct got_error *
879 install_blob(struct got_worktree *worktree, const char *ondisk_path,
880 const char *path, uint16_t te_mode, uint16_t st_mode,
881 struct got_blob_object *blob, int restoring_missing_file,
882 int reverting_versioned_file, struct got_repository *repo,
883 got_worktree_checkout_cb progress_cb, void *progress_arg)
885 const struct got_error *err = NULL;
889 char *tmppath = NULL;
891 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
892 GOT_DEFAULT_FILE_MODE);
894 if (errno == ENOENT) {
895 char *parent = dirname(path);
897 return got_error_from_errno2("dirname", path);
898 err = add_dir_on_disk(worktree, parent);
901 fd = open(ondisk_path,
902 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
903 GOT_DEFAULT_FILE_MODE);
905 return got_error_from_errno2("open",
907 } else if (errno == EEXIST) {
908 if (!S_ISREG(st_mode)) {
909 /* TODO file is obstructed; do something */
910 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
913 err = got_opentemp_named_fd(&tmppath, &fd,
920 return got_error_from_errno2("open", ondisk_path);
923 if (restoring_missing_file)
924 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
925 else if (reverting_versioned_file)
926 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
928 (*progress_cb)(progress_arg,
929 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
931 hdrlen = got_object_blob_get_hdrlen(blob);
933 const uint8_t *buf = got_object_blob_get_read_buf(blob);
934 err = got_object_blob_read_block(&len, blob);
938 /* Skip blob object header first time around. */
939 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
941 err = got_error_from_errno("write");
943 } else if (outlen != len - hdrlen) {
944 err = got_error(GOT_ERR_IO);
951 if (fsync(fd) != 0) {
952 err = got_error_from_errno("fsync");
957 if (rename(tmppath, ondisk_path) != 0) {
958 err = got_error_from_errno3("rename", tmppath,
965 if (te_mode & S_IXUSR) {
966 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
967 err = got_error_from_errno2("chmod", ondisk_path);
971 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
972 err = got_error_from_errno2("chmod", ondisk_path);
978 if (fd != -1 && close(fd) != 0 && err == NULL)
979 err = got_error_from_errno("close");
984 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
985 static const struct got_error *
986 get_modified_file_content_status(unsigned char *status, FILE *f)
988 const struct got_error *err = NULL;
989 const char *markers[3] = {
990 GOT_DIFF_CONFLICT_MARKER_BEGIN,
991 GOT_DIFF_CONFLICT_MARKER_SEP,
992 GOT_DIFF_CONFLICT_MARKER_END
997 const char delim[3] = {'\0', '\0', '\0'};
999 while (*status == GOT_STATUS_MODIFY) {
1000 line = fparseln(f, &len, NULL, delim, 0);
1004 err = got_ferror(f, GOT_ERR_IO);
1008 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1009 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1011 *status = GOT_STATUS_CONFLICT;
1020 static const struct got_error *
1021 get_file_status(unsigned char *status, struct stat *sb,
1022 struct got_fileindex_entry *ie, const char *abspath,
1023 struct got_repository *repo)
1025 const struct got_error *err = NULL;
1026 struct got_object_id id;
1030 struct got_blob_object *blob = NULL;
1033 *status = GOT_STATUS_NO_CHANGE;
1035 if (lstat(abspath, sb) == -1) {
1036 if (errno == ENOENT) {
1038 if (got_fileindex_entry_has_file_on_disk(ie))
1039 *status = GOT_STATUS_MISSING;
1041 *status = GOT_STATUS_DELETE;
1043 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1044 & (S_IRWXU | S_IRWXG | S_IRWXO));
1046 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1049 return got_error_from_errno2("lstat", abspath);
1052 if (!S_ISREG(sb->st_mode)) {
1053 *status = GOT_STATUS_OBSTRUCTED;
1060 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1061 *status = GOT_STATUS_DELETE;
1063 } else if (!got_fileindex_entry_has_blob(ie)) {
1064 *status = GOT_STATUS_ADD;
1068 if (ie->ctime_sec == sb->st_ctime &&
1069 ie->ctime_nsec == sb->st_ctimensec &&
1070 ie->mtime_sec == sb->st_mtime &&
1071 ie->mtime_sec == sb->st_mtime &&
1072 ie->mtime_nsec == sb->st_mtimensec &&
1073 ie->size == (sb->st_size & 0xffffffff))
1076 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1077 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1081 f = fopen(abspath, "r");
1083 err = got_error_from_errno2("fopen", abspath);
1086 hdrlen = got_object_blob_get_hdrlen(blob);
1088 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1089 err = got_object_blob_read_block(&blen, blob);
1092 /* Skip length of blob object header first time around. */
1093 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1094 if (flen == 0 && ferror(f)) {
1095 err = got_error_from_errno("fread");
1100 *status = GOT_STATUS_MODIFY;
1102 } else if (flen == 0) {
1104 *status = GOT_STATUS_MODIFY;
1106 } else if (blen - hdrlen == flen) {
1107 /* Skip blob object header first time around. */
1108 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1109 *status = GOT_STATUS_MODIFY;
1113 *status = GOT_STATUS_MODIFY;
1119 if (*status == GOT_STATUS_MODIFY) {
1121 err = get_modified_file_content_status(status, f);
1125 got_object_blob_close(blob);
1131 static const struct got_error *
1132 update_blob(struct got_worktree *worktree,
1133 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1134 struct got_tree_entry *te, const char *path,
1135 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1138 const struct got_error *err = NULL;
1139 struct got_blob_object *blob = NULL;
1141 unsigned char status = GOT_STATUS_NO_CHANGE;
1144 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1145 return got_error_from_errno("asprintf");
1147 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1151 if (status == GOT_STATUS_OBSTRUCTED) {
1152 (*progress_cb)(progress_arg, status, path);
1156 if (ie && status != GOT_STATUS_MISSING) {
1157 if (got_fileindex_entry_has_commit(ie) &&
1158 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1159 SHA1_DIGEST_LENGTH) == 0) {
1160 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1164 if (got_fileindex_entry_has_blob(ie) &&
1165 memcmp(ie->blob_sha1, te->id->sha1,
1166 SHA1_DIGEST_LENGTH) == 0)
1170 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1174 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1175 int update_timestamps;
1176 struct got_blob_object *blob2 = NULL;
1177 if (got_fileindex_entry_has_blob(ie)) {
1178 struct got_object_id id2;
1179 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1180 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1184 err = merge_blob(&update_timestamps, worktree, blob2,
1185 ondisk_path, path, sb.st_mode, blob, repo,
1186 progress_cb, progress_arg);
1188 got_object_blob_close(blob2);
1190 * Do not update timestamps of files with local changes.
1191 * Otherwise, a future status walk would treat them as
1192 * unmodified files again.
1194 err = got_fileindex_entry_update(ie, ondisk_path,
1195 blob->id.sha1, worktree->base_commit_id->sha1,
1197 } else if (status == GOT_STATUS_DELETE) {
1198 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1199 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1200 ondisk_path, path, blob, 0);
1204 err = install_blob(worktree, ondisk_path, path, te->mode,
1205 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1206 repo, progress_cb, progress_arg);
1209 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1210 ondisk_path, path, blob, 1);
1214 got_object_blob_close(blob);
1220 static const struct got_error *
1221 remove_ondisk_file(const char *root_path, const char *path)
1223 const struct got_error *err = NULL;
1224 char *ondisk_path = NULL;
1226 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1227 return got_error_from_errno("asprintf");
1229 if (unlink(ondisk_path) == -1) {
1230 if (errno != ENOENT)
1231 err = got_error_from_errno2("unlink", ondisk_path);
1233 char *parent = dirname(ondisk_path);
1234 while (parent && strcmp(parent, root_path) != 0) {
1235 if (rmdir(parent) == -1) {
1236 if (errno != ENOTEMPTY)
1237 err = got_error_from_errno2("rmdir",
1241 parent = dirname(parent);
1248 static const struct got_error *
1249 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1250 struct got_fileindex_entry *ie, struct got_repository *repo,
1251 got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 const struct got_error *err = NULL;
1254 unsigned char status;
1258 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1260 return got_error_from_errno("asprintf");
1262 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1266 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1267 status == GOT_STATUS_ADD) {
1268 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1270 * Preserve the working file and change the deleted blob's
1271 * entry into a schedule-add entry.
1273 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1278 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1279 if (status == GOT_STATUS_NO_CHANGE) {
1280 err = remove_ondisk_file(worktree->root_path, ie->path);
1284 got_fileindex_entry_remove(fileindex, ie);
1290 struct diff_cb_arg {
1291 struct got_fileindex *fileindex;
1292 struct got_worktree *worktree;
1293 struct got_repository *repo;
1294 got_worktree_checkout_cb progress_cb;
1296 got_worktree_cancel_cb cancel_cb;
1300 static const struct got_error *
1301 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1302 struct got_tree_entry *te, const char *parent_path)
1304 struct diff_cb_arg *a = arg;
1306 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1307 return got_error(GOT_ERR_CANCELLED);
1309 return update_blob(a->worktree, a->fileindex, ie, te,
1310 ie->path, a->repo, a->progress_cb, a->progress_arg);
1313 static const struct got_error *
1314 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1316 struct diff_cb_arg *a = arg;
1318 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1319 return got_error(GOT_ERR_CANCELLED);
1321 return delete_blob(a->worktree, a->fileindex, ie,
1322 a->repo, a->progress_cb, a->progress_arg);
1325 static const struct got_error *
1326 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1328 struct diff_cb_arg *a = arg;
1329 const struct got_error *err;
1332 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1333 return got_error(GOT_ERR_CANCELLED);
1335 if (asprintf(&path, "%s%s%s", parent_path,
1336 parent_path[0] ? "/" : "", te->name)
1338 return got_error_from_errno("asprintf");
1340 if (S_ISDIR(te->mode))
1341 err = add_dir_on_disk(a->worktree, path);
1343 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1344 a->repo, a->progress_cb, a->progress_arg);
1350 const struct got_error *
1351 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1353 const struct got_error *err = NULL;
1354 char *uuidstr = NULL;
1355 uint32_t uuid_status;
1359 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1360 if (uuid_status != uuid_s_ok)
1361 return got_error_uuid(uuid_status);
1363 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1365 err = got_error_from_errno("asprintf");
1373 * Prevent Git's garbage collector from deleting our base commit by
1374 * setting a reference to our base commit's ID.
1376 static const struct got_error *
1377 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1379 const struct got_error *err = NULL;
1380 struct got_reference *ref = NULL;
1383 err = got_worktree_get_base_ref_name(&refname, worktree);
1387 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1391 err = got_ref_write(ref, repo);
1399 static const struct got_error *
1400 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1401 struct got_worktree *worktree)
1403 const struct got_error *err = NULL;
1406 *fileindex_path = NULL;
1407 *fileindex = got_fileindex_alloc();
1408 if (*fileindex == NULL)
1409 return got_error_from_errno("got_fileindex_alloc");
1411 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1412 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1413 err = got_error_from_errno("asprintf");
1414 *fileindex_path = NULL;
1418 index = fopen(*fileindex_path, "rb");
1419 if (index == NULL) {
1420 if (errno != ENOENT)
1421 err = got_error_from_errno2("fopen", *fileindex_path);
1423 err = got_fileindex_read(*fileindex, index);
1424 if (fclose(index) != 0 && err == NULL)
1425 err = got_error_from_errno("fclose");
1429 free(*fileindex_path);
1430 *fileindex_path = NULL;
1437 struct bump_base_commit_id_arg {
1438 struct got_object_id *base_commit_id;
1441 const char *entry_name;
1442 got_worktree_checkout_cb progress_cb;
1446 /* Bump base commit ID of all files within an updated part of the work tree. */
1447 static const struct got_error *
1448 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1450 struct bump_base_commit_id_arg *a = arg;
1452 if (a->entry_name) {
1453 if (strcmp(ie->path, a->path) != 0)
1455 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1458 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1459 SHA1_DIGEST_LENGTH) == 0)
1462 (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1463 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1467 static const struct got_error *
1468 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1470 const struct got_error *err = NULL;
1471 char *new_fileindex_path = NULL;
1472 FILE *new_index = NULL;
1474 err = got_opentemp_named(&new_fileindex_path, &new_index,
1479 err = got_fileindex_write(fileindex, new_index);
1483 if (rename(new_fileindex_path, fileindex_path) != 0) {
1484 err = got_error_from_errno3("rename", new_fileindex_path,
1486 unlink(new_fileindex_path);
1491 free(new_fileindex_path);
1495 const struct got_error *
1496 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1497 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1498 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1500 const struct got_error *err = NULL, *sync_err, *unlockerr;
1501 struct got_commit_object *commit = NULL;
1502 struct got_object_id *tree_id = NULL;
1503 struct got_tree_object *tree = NULL;
1504 struct got_fileindex *fileindex = NULL;
1505 char *fileindex_path = NULL;
1506 struct got_fileindex_diff_tree_cb diff_cb;
1507 struct diff_cb_arg arg;
1508 char *relpath = NULL, *entry_name = NULL;
1509 struct bump_base_commit_id_arg bbc_arg;
1511 err = lock_worktree(worktree, LOCK_EX);
1516 * Read the file index.
1517 * Checking out files is supposed to be an idempotent operation.
1518 * If the on-disk file index is incomplete we will try to complete it.
1520 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1524 err = ref_base_commit(worktree, repo);
1528 err = got_object_open_as_commit(&commit, repo,
1529 worktree->base_commit_id);
1536 relpath = strdup(path);
1537 if (relpath == NULL) {
1538 err = got_error_from_errno("strdup");
1541 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1542 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1544 err = got_error_from_errno("asprintf");
1547 err = got_object_id_by_path(&tree_id, repo,
1548 worktree->base_commit_id, tree_path);
1552 err = got_object_get_type(&obj_type, repo, tree_id);
1555 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1556 /* Split provided path into parent dir + entry name. */
1557 if (strchr(path, '/') == NULL) {
1558 relpath = strdup("");
1559 if (relpath == NULL) {
1560 err = got_error_from_errno("strdup");
1563 tree_path = strdup(worktree->path_prefix);
1564 if (tree_path == NULL) {
1565 err = got_error_from_errno("strdup");
1569 err = got_path_dirname(&relpath, path);
1572 if (asprintf(&tree_path, "%s%s%s",
1573 worktree->path_prefix,
1574 got_path_is_root_dir(
1575 worktree->path_prefix) ? "" : "/",
1577 err = got_error_from_errno("asprintf");
1581 err = got_object_id_by_path(&tree_id, repo,
1582 worktree->base_commit_id, tree_path);
1586 entry_name = basename(path);
1587 if (entry_name == NULL) {
1588 err = got_error_from_errno2("basename", path);
1593 relpath = strdup("");
1594 if (relpath == NULL) {
1595 err = got_error_from_errno("strdup");
1598 err = got_object_id_by_path(&tree_id, repo,
1599 worktree->base_commit_id, worktree->path_prefix);
1604 err = got_object_open_as_tree(&tree, repo, tree_id);
1609 got_object_tree_find_entry(tree, entry_name) == NULL) {
1610 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1614 diff_cb.diff_old_new = diff_old_new;
1615 diff_cb.diff_old = diff_old;
1616 diff_cb.diff_new = diff_new;
1617 arg.fileindex = fileindex;
1618 arg.worktree = worktree;
1620 arg.progress_cb = progress_cb;
1621 arg.progress_arg = progress_arg;
1622 arg.cancel_cb = cancel_cb;
1623 arg.cancel_arg = cancel_arg;
1624 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1625 entry_name, repo, &diff_cb, &arg);
1629 bbc_arg.base_commit_id = worktree->base_commit_id;
1630 bbc_arg.entry_name = entry_name;
1631 bbc_arg.path = path;
1632 bbc_arg.path_len = strlen(path);
1633 bbc_arg.progress_cb = progress_cb;
1634 bbc_arg.progress_arg = progress_arg;
1635 err = got_fileindex_for_each_entry_safe(fileindex,
1636 bump_base_commit_id, &bbc_arg);
1638 sync_err = sync_fileindex(fileindex, fileindex_path);
1639 if (sync_err && err == NULL)
1642 free(fileindex_path);
1645 got_object_tree_close(tree);
1647 got_object_commit_close(commit);
1648 got_fileindex_free(fileindex);
1649 unlockerr = lock_worktree(worktree, LOCK_SH);
1650 if (unlockerr && err == NULL)
1655 struct merge_file_cb_arg {
1656 struct got_worktree *worktree;
1657 struct got_fileindex *fileindex;
1658 got_worktree_checkout_cb progress_cb;
1660 got_worktree_cancel_cb cancel_cb;
1664 static const struct got_error *
1665 merge_file_cb(void *arg, struct got_blob_object *blob1,
1666 struct got_blob_object *blob2, struct got_object_id *id1,
1667 struct got_object_id *id2, const char *path1, const char *path2,
1668 struct got_repository *repo)
1670 static const struct got_error *err = NULL;
1671 struct merge_file_cb_arg *a = arg;
1672 struct got_fileindex_entry *ie;
1673 char *ondisk_path = NULL;
1675 unsigned char status;
1676 int local_changes_subsumed;
1678 if (blob1 && blob2) {
1679 ie = got_fileindex_entry_get(a->fileindex, path2);
1681 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1686 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1688 return got_error_from_errno("asprintf");
1690 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1694 if (status == GOT_STATUS_DELETE) {
1695 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1699 if (status != GOT_STATUS_NO_CHANGE &&
1700 status != GOT_STATUS_MODIFY &&
1701 status != GOT_STATUS_CONFLICT &&
1702 status != GOT_STATUS_ADD) {
1703 (*a->progress_cb)(a->progress_arg, status, path2);
1707 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1708 ondisk_path, path2, sb.st_mode, blob2, repo,
1709 a->progress_cb, a->progress_arg);
1711 ie = got_fileindex_entry_get(a->fileindex, path1);
1713 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1718 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1720 return got_error_from_errno("asprintf");
1722 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1727 case GOT_STATUS_NO_CHANGE:
1728 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1730 err = remove_ondisk_file(a->worktree->root_path, path1);
1734 got_fileindex_entry_mark_deleted_from_disk(ie);
1736 case GOT_STATUS_DELETE:
1737 case GOT_STATUS_MISSING:
1738 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1741 got_fileindex_entry_mark_deleted_from_disk(ie);
1743 case GOT_STATUS_ADD:
1744 case GOT_STATUS_MODIFY:
1745 case GOT_STATUS_CONFLICT:
1746 (*a->progress_cb)(a->progress_arg,
1747 GOT_STATUS_CANNOT_DELETE, path1);
1749 case GOT_STATUS_OBSTRUCTED:
1750 (*a->progress_cb)(a->progress_arg, status, path1);
1756 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1758 return got_error_from_errno("asprintf");
1759 ie = got_fileindex_entry_get(a->fileindex, path2);
1761 err = get_file_status(&status, &sb, ie, ondisk_path,
1765 if (status != GOT_STATUS_NO_CHANGE &&
1766 status != GOT_STATUS_MODIFY &&
1767 status != GOT_STATUS_CONFLICT &&
1768 status != GOT_STATUS_ADD) {
1769 (*a->progress_cb)(a->progress_arg, status,
1773 err = merge_blob(&local_changes_subsumed, a->worktree,
1774 NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1775 a->progress_cb, a->progress_arg);
1776 if (status == GOT_STATUS_DELETE) {
1777 err = update_blob_fileindex_entry(a->worktree,
1778 a->fileindex, ie, ondisk_path, ie->path,
1784 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1785 err = install_blob(a->worktree, ondisk_path, path2,
1786 /* XXX get this from parent tree! */
1787 GOT_DEFAULT_FILE_MODE,
1788 sb.st_mode, blob2, 0, 0, repo,
1789 a->progress_cb, a->progress_arg);
1792 err = got_fileindex_entry_alloc(&ie,
1793 ondisk_path, path2, NULL, NULL);
1796 err = got_fileindex_entry_add(a->fileindex, ie);
1798 got_fileindex_entry_free(ie);
1808 struct check_merge_ok_arg {
1809 struct got_worktree *worktree;
1810 struct got_repository *repo;
1813 static const struct got_error *
1814 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1816 const struct got_error *err = NULL;
1817 struct check_merge_ok_arg *a = arg;
1818 unsigned char status;
1822 /* Reject merges into a work tree with mixed base commits. */
1823 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1824 SHA1_DIGEST_LENGTH))
1825 return got_error(GOT_ERR_MIXED_COMMITS);
1827 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1829 return got_error_from_errno("asprintf");
1831 /* Reject merges into a work tree with conflicted files. */
1832 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1835 if (status == GOT_STATUS_CONFLICT)
1836 return got_error(GOT_ERR_CONFLICTS);
1841 const struct got_error *
1842 got_worktree_merge_files(struct got_worktree *worktree,
1843 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1844 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1845 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1847 const struct got_error *err = NULL, *sync_err, *unlockerr;
1848 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1849 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1850 struct merge_file_cb_arg arg;
1851 char *fileindex_path = NULL;
1852 struct got_fileindex *fileindex = NULL;
1853 struct check_merge_ok_arg mok_arg;
1855 err = lock_worktree(worktree, LOCK_EX);
1859 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1863 mok_arg.worktree = worktree;
1864 mok_arg.repo = repo;
1865 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1871 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1872 worktree->path_prefix);
1876 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1881 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1882 worktree->path_prefix);
1886 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1890 arg.worktree = worktree;
1891 arg.fileindex = fileindex;
1892 arg.progress_cb = progress_cb;
1893 arg.progress_arg = progress_arg;
1894 arg.cancel_cb = cancel_cb;
1895 arg.cancel_arg = cancel_arg;
1896 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1897 sync_err = sync_fileindex(fileindex, fileindex_path);
1898 if (sync_err && err == NULL)
1901 got_fileindex_free(fileindex);
1903 got_object_tree_close(tree1);
1905 got_object_tree_close(tree2);
1907 unlockerr = lock_worktree(worktree, LOCK_SH);
1908 if (unlockerr && err == NULL)
1913 struct diff_dir_cb_arg {
1914 struct got_fileindex *fileindex;
1915 struct got_worktree *worktree;
1916 const char *status_path;
1917 size_t status_path_len;
1918 struct got_repository *repo;
1919 got_worktree_status_cb status_cb;
1921 got_worktree_cancel_cb cancel_cb;
1925 static const struct got_error *
1926 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1927 got_worktree_status_cb status_cb, void *status_arg,
1928 struct got_repository *repo)
1930 const struct got_error *err = NULL;
1931 unsigned char status = GOT_STATUS_NO_CHANGE;
1933 struct got_object_id blob_id, commit_id;
1935 err = get_file_status(&status, &sb, ie, abspath, repo);
1936 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1937 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1938 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1939 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1945 static const struct got_error *
1946 status_old_new(void *arg, struct got_fileindex_entry *ie,
1947 struct dirent *de, const char *parent_path)
1949 const struct got_error *err = NULL;
1950 struct diff_dir_cb_arg *a = arg;
1953 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1954 return got_error(GOT_ERR_CANCELLED);
1956 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1957 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1960 if (parent_path[0]) {
1961 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1962 parent_path, de->d_name) == -1)
1963 return got_error_from_errno("asprintf");
1965 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1967 return got_error_from_errno("asprintf");
1970 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1976 static const struct got_error *
1977 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1979 struct diff_dir_cb_arg *a = arg;
1980 struct got_object_id blob_id, commit_id;
1981 unsigned char status;
1983 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1984 return got_error(GOT_ERR_CANCELLED);
1986 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1989 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1990 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1991 if (got_fileindex_entry_has_file_on_disk(ie))
1992 status = GOT_STATUS_MISSING;
1994 status = GOT_STATUS_DELETE;
1995 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1999 static const struct got_error *
2000 status_new(void *arg, struct dirent *de, const char *parent_path)
2002 const struct got_error *err = NULL;
2003 struct diff_dir_cb_arg *a = arg;
2006 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2007 return got_error(GOT_ERR_CANCELLED);
2009 if (de->d_type == DT_DIR)
2012 /* XXX ignore symlinks for now */
2013 if (de->d_type == DT_LNK)
2016 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2019 if (parent_path[0]) {
2020 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2021 return got_error_from_errno("asprintf");
2026 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2033 const struct got_error *
2034 got_worktree_status(struct got_worktree *worktree, const char *path,
2035 struct got_repository *repo, got_worktree_status_cb status_cb,
2036 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2038 const struct got_error *err = NULL;
2039 DIR *workdir = NULL;
2040 char *fileindex_path = NULL;
2041 struct got_fileindex *fileindex = NULL;
2043 struct got_fileindex_diff_dir_cb fdiff_cb;
2044 struct diff_dir_cb_arg arg;
2045 char *ondisk_path = NULL;
2047 fileindex = got_fileindex_alloc();
2048 if (fileindex == NULL) {
2049 err = got_error_from_errno("got_fileindex_alloc");
2053 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2054 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2055 err = got_error_from_errno("asprintf");
2056 fileindex_path = NULL;
2060 index = fopen(fileindex_path, "rb");
2061 if (index == NULL) {
2062 if (errno != ENOENT) {
2063 err = got_error_from_errno2("fopen", fileindex_path);
2067 err = got_fileindex_read(fileindex, index);
2073 if (asprintf(&ondisk_path, "%s%s%s",
2074 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2075 err = got_error_from_errno("asprintf");
2078 workdir = opendir(ondisk_path);
2079 if (workdir == NULL) {
2080 if (errno == ENOTDIR || errno == ENOENT) {
2081 struct got_fileindex_entry *ie;
2082 ie = got_fileindex_entry_get(fileindex, path);
2084 err = got_error(GOT_ERR_BAD_PATH);
2087 err = report_file_status(ie, ondisk_path,
2088 status_cb, status_arg, repo);
2091 err = got_error_from_errno2("opendir", ondisk_path);
2095 fdiff_cb.diff_old_new = status_old_new;
2096 fdiff_cb.diff_old = status_old;
2097 fdiff_cb.diff_new = status_new;
2098 arg.fileindex = fileindex;
2099 arg.worktree = worktree;
2100 arg.status_path = path;
2101 arg.status_path_len = strlen(path);
2103 arg.status_cb = status_cb;
2104 arg.status_arg = status_arg;
2105 arg.cancel_cb = cancel_cb;
2106 arg.cancel_arg = cancel_arg;
2107 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2108 path, repo, &fdiff_cb, &arg);
2113 free(fileindex_path);
2114 got_fileindex_free(fileindex);
2118 const struct got_error *
2119 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2122 const struct got_error *err = NULL;
2123 char *resolved, *path = NULL;
2128 resolved = realpath(arg, NULL);
2129 if (resolved == NULL)
2130 return got_error_from_errno2("realpath", arg);
2132 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2133 strlen(got_worktree_get_root_path(worktree)))) {
2134 err = got_error(GOT_ERR_BAD_PATH);
2138 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2139 err = got_path_skip_common_ancestor(&path,
2140 got_worktree_get_root_path(worktree), resolved);
2146 err = got_error_from_errno("strdup");
2151 /* XXX status walk can't deal with trailing slash! */
2153 while (path[len - 1] == '/') {
2154 path[len - 1] = '\0';
2166 static const struct got_error *
2167 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2168 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2169 struct got_repository *repo)
2171 const struct got_error *err = NULL;
2172 struct got_fileindex_entry *ie;
2174 /* Re-adding an existing entry is a no-op. */
2175 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2178 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2182 err = got_fileindex_entry_add(fileindex, ie);
2184 got_fileindex_entry_free(ie);
2188 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2191 const struct got_error *
2192 got_worktree_schedule_add(struct got_worktree *worktree,
2193 struct got_pathlist_head *ondisk_paths,
2194 got_worktree_status_cb status_cb, void *status_arg,
2195 struct got_repository *repo)
2197 struct got_fileindex *fileindex = NULL;
2198 char *fileindex_path = NULL;
2200 const struct got_error *err = NULL, *sync_err, *unlockerr;
2201 struct got_pathlist_entry *pe;
2203 err = lock_worktree(worktree, LOCK_EX);
2208 fileindex = got_fileindex_alloc();
2209 if (fileindex == NULL) {
2210 err = got_error_from_errno("got_fileindex_alloc");
2214 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2215 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2216 err = got_error_from_errno("asprintf");
2217 fileindex_path = NULL;
2221 index = fopen(fileindex_path, "rb");
2222 if (index == NULL) {
2223 err = got_error_from_errno2("fopen", fileindex_path);
2227 err = got_fileindex_read(fileindex, index);
2231 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2233 err = got_path_skip_common_ancestor(&relpath,
2234 got_worktree_get_root_path(worktree), pe->path);
2237 err = schedule_addition(pe->path, fileindex, relpath,
2238 status_cb, status_arg, repo);
2243 sync_err = sync_fileindex(fileindex, fileindex_path);
2244 if (sync_err && err == NULL)
2248 if (fclose(index) != 0 && err == NULL)
2249 err = got_error_from_errno("fclose");
2252 got_fileindex_free(fileindex);
2253 unlockerr = lock_worktree(worktree, LOCK_SH);
2254 if (unlockerr && err == NULL)
2259 static const struct got_error *
2260 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2261 const char *relpath, int delete_local_mods,
2262 got_worktree_status_cb status_cb, void *status_arg,
2263 struct got_repository *repo)
2265 const struct got_error *err = NULL;
2266 struct got_fileindex_entry *ie = NULL;
2267 unsigned char status;
2270 ie = got_fileindex_entry_get(fileindex, relpath);
2272 return got_error(GOT_ERR_BAD_PATH);
2274 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2278 if (status != GOT_STATUS_NO_CHANGE) {
2279 if (status == GOT_STATUS_DELETE)
2280 return got_error_set_errno(ENOENT, ondisk_path);
2281 if (status != GOT_STATUS_MODIFY)
2282 return got_error(GOT_ERR_FILE_STATUS);
2283 if (!delete_local_mods)
2284 return got_error(GOT_ERR_FILE_MODIFIED);
2287 if (unlink(ondisk_path) != 0)
2288 return got_error_from_errno2("unlink", ondisk_path);
2290 got_fileindex_entry_mark_deleted_from_disk(ie);
2291 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2294 const struct got_error *
2295 got_worktree_schedule_delete(struct got_worktree *worktree,
2296 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2297 got_worktree_status_cb status_cb, void *status_arg,
2298 struct got_repository *repo)
2300 struct got_fileindex *fileindex = NULL;
2301 char *fileindex_path = NULL;
2303 const struct got_error *err = NULL, *sync_err, *unlockerr;
2304 struct got_pathlist_entry *pe;
2306 err = lock_worktree(worktree, LOCK_EX);
2310 fileindex = got_fileindex_alloc();
2311 if (fileindex == NULL) {
2312 err = got_error_from_errno("got_fileindex_alloc");
2316 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2317 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2318 err = got_error_from_errno("asprintf");
2319 fileindex_path = NULL;
2323 index = fopen(fileindex_path, "rb");
2324 if (index == NULL) {
2325 err = got_error_from_errno2("fopen", fileindex_path);
2329 err = got_fileindex_read(fileindex, index);
2333 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2335 err = got_path_skip_common_ancestor(&relpath,
2336 got_worktree_get_root_path(worktree), pe->path);
2339 err = schedule_for_deletion(pe->path, fileindex, relpath,
2340 delete_local_mods, status_cb, status_arg, repo);
2345 sync_err = sync_fileindex(fileindex, fileindex_path);
2346 if (sync_err && err == NULL)
2350 if (fclose(index) != 0 && err == NULL)
2351 err = got_error_from_errno("fclose");
2354 got_fileindex_free(fileindex);
2355 unlockerr = lock_worktree(worktree, LOCK_SH);
2356 if (unlockerr && err == NULL)
2361 static const struct got_error *
2362 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2363 const char *ondisk_path,
2364 got_worktree_checkout_cb progress_cb, void *progress_arg,
2365 struct got_repository *repo)
2367 const struct got_error *err = NULL;
2368 char *relpath = NULL, *parent_path = NULL;
2369 struct got_fileindex_entry *ie;
2370 struct got_tree_object *tree = NULL;
2371 struct got_object_id *tree_id = NULL;
2372 const struct got_tree_entry *te;
2373 char *tree_path = NULL, *te_name;
2374 struct got_blob_object *blob = NULL;
2375 unsigned char status;
2378 err = got_path_skip_common_ancestor(&relpath,
2379 got_worktree_get_root_path(worktree), ondisk_path);
2383 ie = got_fileindex_entry_get(fileindex, relpath);
2385 err = got_error(GOT_ERR_BAD_PATH);
2389 /* Construct in-repository path of tree which contains this blob. */
2390 err = got_path_dirname(&parent_path, ie->path);
2392 if (err->code != GOT_ERR_BAD_PATH)
2394 parent_path = strdup("/");
2395 if (parent_path == NULL) {
2396 err = got_error_from_errno("strdup");
2400 if (got_path_is_root_dir(worktree->path_prefix)) {
2401 tree_path = strdup(parent_path);
2402 if (tree_path == NULL) {
2403 err = got_error_from_errno("strdup");
2407 if (got_path_is_root_dir(parent_path)) {
2408 tree_path = strdup(worktree->path_prefix);
2409 if (tree_path == NULL) {
2410 err = got_error_from_errno("strdup");
2414 if (asprintf(&tree_path, "%s/%s",
2415 worktree->path_prefix, parent_path) == -1) {
2416 err = got_error_from_errno("asprintf");
2422 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2427 err = got_object_open_as_tree(&tree, repo, tree_id);
2431 te_name = basename(ie->path);
2432 if (te_name == NULL) {
2433 err = got_error_from_errno2("basename", ie->path);
2437 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2441 te = got_object_tree_find_entry(tree, te_name);
2442 if (te == NULL && status != GOT_STATUS_ADD) {
2443 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2448 case GOT_STATUS_ADD:
2449 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2450 got_fileindex_entry_remove(fileindex, ie);
2452 case GOT_STATUS_DELETE:
2453 case GOT_STATUS_MODIFY:
2454 case GOT_STATUS_CONFLICT:
2455 case GOT_STATUS_MISSING: {
2456 struct got_object_id id;
2457 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2458 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2461 err = install_blob(worktree, ondisk_path, ie->path,
2462 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2466 if (status == GOT_STATUS_DELETE) {
2467 err = update_blob_fileindex_entry(worktree,
2468 fileindex, ie, ondisk_path, ie->path, blob, 1);
2482 got_object_blob_close(blob);
2484 got_object_tree_close(tree);
2489 const struct got_error *
2490 got_worktree_revert(struct got_worktree *worktree,
2491 struct got_pathlist_head *ondisk_paths,
2492 got_worktree_checkout_cb progress_cb, void *progress_arg,
2493 struct got_repository *repo)
2495 struct got_fileindex *fileindex = NULL;
2496 char *fileindex_path = NULL;
2498 const struct got_error *err = NULL, *unlockerr = NULL;
2499 const struct got_error *sync_err = NULL;
2500 struct got_pathlist_entry *pe;
2502 err = lock_worktree(worktree, LOCK_EX);
2506 fileindex = got_fileindex_alloc();
2507 if (fileindex == NULL) {
2508 err = got_error_from_errno("got_fileindex_alloc");
2512 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2513 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2514 err = got_error_from_errno("asprintf");
2515 fileindex_path = NULL;
2519 index = fopen(fileindex_path, "rb");
2520 if (index == NULL) {
2521 err = got_error_from_errno2("fopen", fileindex_path);
2525 err = got_fileindex_read(fileindex, index);
2529 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2530 err = revert_file(worktree, fileindex, pe->path,
2531 progress_cb, progress_arg, repo);
2535 sync_err = sync_fileindex(fileindex, fileindex_path);
2536 if (sync_err && err == NULL)
2540 if (fclose(index) != 0 && err == NULL)
2541 err = got_error_from_errno("fclose");
2544 got_fileindex_free(fileindex);
2545 unlockerr = lock_worktree(worktree, LOCK_SH);
2546 if (unlockerr && err == NULL)
2552 free_commitable(struct got_commitable *ct)
2555 free(ct->in_repo_path);
2556 free(ct->ondisk_path);
2558 free(ct->base_blob_id);
2559 free(ct->base_commit_id);
2563 struct collect_commitables_arg {
2564 struct got_pathlist_head *commitable_paths;
2565 struct got_repository *repo;
2566 struct got_worktree *worktree;
2569 static const struct got_error *
2570 collect_commitables(void *arg, unsigned char status, const char *relpath,
2571 struct got_object_id *blob_id, struct got_object_id *commit_id)
2573 struct collect_commitables_arg *a = arg;
2574 const struct got_error *err = NULL;
2575 struct got_commitable *ct = NULL;
2576 struct got_pathlist_entry *new = NULL;
2577 char *parent_path = NULL, *path = NULL;
2580 if (status == GOT_STATUS_CONFLICT)
2581 return got_error(GOT_ERR_COMMIT_CONFLICT);
2583 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2584 status != GOT_STATUS_DELETE)
2587 if (asprintf(&path, "/%s", relpath) == -1) {
2588 err = got_error_from_errno("asprintf");
2591 if (strcmp(path, "/") == 0) {
2592 parent_path = strdup("");
2593 if (parent_path == NULL)
2594 return got_error_from_errno("strdup");
2596 err = got_path_dirname(&parent_path, path);
2601 ct = calloc(1, sizeof(*ct));
2603 err = got_error_from_errno("calloc");
2607 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2609 err = got_error_from_errno("asprintf");
2612 if (status == GOT_STATUS_DELETE) {
2613 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2615 if (lstat(ct->ondisk_path, &sb) != 0) {
2616 err = got_error_from_errno2("lstat", ct->ondisk_path);
2619 ct->mode = sb.st_mode;
2622 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2623 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2625 err = got_error_from_errno("asprintf");
2629 ct->status = status;
2630 ct->blob_id = NULL; /* will be filled in when blob gets created */
2631 if (ct->status != GOT_STATUS_ADD) {
2632 ct->base_blob_id = got_object_id_dup(blob_id);
2633 if (ct->base_blob_id == NULL) {
2634 err = got_error_from_errno("got_object_id_dup");
2637 ct->base_commit_id = got_object_id_dup(commit_id);
2638 if (ct->base_commit_id == NULL) {
2639 err = got_error_from_errno("got_object_id_dup");
2643 ct->path = strdup(path);
2644 if (ct->path == NULL) {
2645 err = got_error_from_errno("strdup");
2648 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2650 if (ct && (err || new == NULL))
2651 free_commitable(ct);
2657 static const struct got_error *write_tree(struct got_object_id **,
2658 struct got_tree_object *, const char *, struct got_pathlist_head *,
2659 got_worktree_status_cb status_cb, void *status_arg,
2660 struct got_repository *);
2662 static const struct got_error *
2663 write_subtree(struct got_object_id **new_subtree_id,
2664 struct got_tree_entry *te, const char *parent_path,
2665 struct got_pathlist_head *commitable_paths,
2666 got_worktree_status_cb status_cb, void *status_arg,
2667 struct got_repository *repo)
2669 const struct got_error *err = NULL;
2670 struct got_tree_object *subtree;
2673 if (asprintf(&subpath, "%s%s%s", parent_path,
2674 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2675 return got_error_from_errno("asprintf");
2677 err = got_object_open_as_tree(&subtree, repo, te->id);
2681 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2682 status_cb, status_arg, repo);
2683 got_object_tree_close(subtree);
2688 static const struct got_error *
2689 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2691 const struct got_error *err = NULL;
2692 char *ct_parent_path = NULL;
2696 if (strchr(ct->path, '/') == NULL) {
2697 *match = got_path_is_root_dir(path);
2701 err = got_path_dirname(&ct_parent_path, ct->path);
2704 *match = (strcmp(path, ct_parent_path) == 0);
2705 free(ct_parent_path);
2710 get_ct_file_mode(struct got_commitable *ct)
2712 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2715 static const struct got_error *
2716 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2717 struct got_tree_entry *te, struct got_commitable *ct)
2719 const struct got_error *err = NULL;
2723 err = got_object_tree_entry_dup(new_te, te);
2727 (*new_te)->mode = get_ct_file_mode(ct);
2729 free((*new_te)->id);
2730 (*new_te)->id = got_object_id_dup(ct->blob_id);
2731 if ((*new_te)->id == NULL) {
2732 err = got_error_from_errno("got_object_id_dup");
2736 if (err && *new_te) {
2737 got_object_tree_entry_close(*new_te);
2743 static const struct got_error *
2744 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2745 struct got_commitable *ct)
2747 const struct got_error *err = NULL;
2752 *new_te = calloc(1, sizeof(**new_te));
2753 if (*new_te == NULL)
2754 return got_error_from_errno("calloc");
2756 ct_name = basename(ct->path);
2757 if (ct_name == NULL) {
2758 err = got_error_from_errno2("basename", ct->path);
2761 (*new_te)->name = strdup(ct_name);
2762 if ((*new_te)->name == NULL) {
2763 err = got_error_from_errno("strdup");
2767 (*new_te)->mode = get_ct_file_mode(ct);
2769 (*new_te)->id = got_object_id_dup(ct->blob_id);
2770 if ((*new_te)->id == NULL) {
2771 err = got_error_from_errno("got_object_id_dup");
2775 if (err && *new_te) {
2776 got_object_tree_entry_close(*new_te);
2782 static const struct got_error *
2783 insert_tree_entry(struct got_tree_entry *new_te,
2784 struct got_pathlist_head *paths)
2786 const struct got_error *err = NULL;
2787 struct got_pathlist_entry *new_pe;
2789 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2793 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2797 static const struct got_error *
2798 report_ct_status(struct got_commitable *ct,
2799 got_worktree_status_cb status_cb, void *status_arg)
2801 const char *ct_path = ct->path;
2802 while (ct_path[0] == '/')
2804 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2807 static const struct got_error *
2808 match_modified_subtree(int *modified, struct got_tree_entry *te,
2809 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2811 const struct got_error *err = NULL;
2812 struct got_pathlist_entry *pe;
2817 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2818 got_path_is_root_dir(base_tree_path) ? "" : "/",
2820 return got_error_from_errno("asprintf");
2822 TAILQ_FOREACH(pe, commitable_paths, entry) {
2823 struct got_commitable *ct = pe->data;
2824 *modified = got_path_is_child(ct->in_repo_path, te_path,
2834 static const struct got_error *
2835 match_deleted_or_modified_ct(struct got_commitable **ctp,
2836 struct got_tree_entry *te, const char *base_tree_path,
2837 struct got_pathlist_head *commitable_paths)
2839 const struct got_error *err = NULL;
2840 struct got_pathlist_entry *pe;
2844 TAILQ_FOREACH(pe, commitable_paths, entry) {
2845 struct got_commitable *ct = pe->data;
2846 char *ct_name = NULL;
2849 if (ct->status != GOT_STATUS_MODIFY &&
2850 ct->status != GOT_STATUS_DELETE)
2853 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2856 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2862 ct_name = basename(pe->path);
2863 if (ct_name == NULL)
2864 return got_error_from_errno2("basename", pe->path);
2866 if (strcmp(te->name, ct_name) != 0)
2876 static const struct got_error *
2877 write_tree(struct got_object_id **new_tree_id,
2878 struct got_tree_object *base_tree, const char *path_base_tree,
2879 struct got_pathlist_head *commitable_paths,
2880 got_worktree_status_cb status_cb, void *status_arg,
2881 struct got_repository *repo)
2883 const struct got_error *err = NULL;
2884 const struct got_tree_entries *base_entries = NULL;
2885 struct got_pathlist_head paths;
2886 struct got_tree_entries new_tree_entries;
2887 struct got_tree_entry *te, *new_te = NULL;
2888 struct got_pathlist_entry *pe;
2891 new_tree_entries.nentries = 0;
2892 SIMPLEQ_INIT(&new_tree_entries.head);
2894 /* Insert, and recurse into, newly added entries first. */
2895 TAILQ_FOREACH(pe, commitable_paths, entry) {
2896 struct got_commitable *ct = pe->data;
2897 char *child_path = NULL, *slash;
2899 if (ct->status != GOT_STATUS_ADD ||
2900 (ct->flags & GOT_COMMITABLE_ADDED))
2903 if (!got_path_is_child(pe->path, path_base_tree,
2904 strlen(path_base_tree)))
2907 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2912 slash = strchr(child_path, '/');
2913 if (slash == NULL) {
2914 err = alloc_added_blob_tree_entry(&new_te, ct);
2917 err = report_ct_status(ct, status_cb, status_arg);
2920 ct->flags |= GOT_COMMITABLE_ADDED;
2924 *slash = '\0'; /* trim trailing path components */
2925 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2926 got_path_is_root_dir(path_base_tree) ? "" : "/",
2927 child_path) == -1) {
2928 err = got_error_from_errno("asprintf");
2932 new_te = calloc(1, sizeof(*new_te));
2933 new_te->mode = S_IFDIR;
2934 new_te->name = strdup(child_path);
2935 if (new_te->name == NULL) {
2936 err = got_error_from_errno("strdup");
2937 got_object_tree_entry_close(new_te);
2941 err = write_tree(&new_te->id, NULL, subtree_path,
2942 commitable_paths, status_cb, status_arg, repo);
2945 got_object_tree_entry_close(new_te);
2950 err = insert_tree_entry(new_te, &paths);
2956 /* Handle modified and deleted entries. */
2957 base_entries = got_object_tree_get_entries(base_tree);
2958 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2959 struct got_commitable *ct = NULL;
2961 if (S_ISDIR(te->mode)) {
2963 err = got_object_tree_entry_dup(&new_te, te);
2966 err = match_modified_subtree(&modified, te,
2967 path_base_tree, commitable_paths);
2970 /* Avoid recursion into unmodified subtrees. */
2973 err = write_subtree(&new_te->id, te,
2974 path_base_tree, commitable_paths,
2975 status_cb, status_arg, repo);
2979 err = insert_tree_entry(new_te, &paths);
2985 err = match_deleted_or_modified_ct(&ct, te,
2986 path_base_tree, commitable_paths);
2988 /* NB: Deleted entries get dropped here. */
2989 if (ct->status == GOT_STATUS_MODIFY) {
2990 err = alloc_modified_blob_tree_entry(
2994 err = insert_tree_entry(new_te, &paths);
2998 err = report_ct_status(ct, status_cb,
3003 /* Entry is unchanged; just copy it. */
3004 err = got_object_tree_entry_dup(&new_te, te);
3007 err = insert_tree_entry(new_te, &paths);
3014 /* Write new list of entries; deleted entries have been dropped. */
3015 TAILQ_FOREACH(pe, &paths, entry) {
3016 struct got_tree_entry *te = pe->data;
3017 new_tree_entries.nentries++;
3018 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3020 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3022 got_object_tree_entries_close(&new_tree_entries);
3023 got_pathlist_free(&paths);
3027 static const struct got_error *
3028 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3029 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3031 const struct got_error *err = NULL, *sync_err;
3032 char *fileindex_path = NULL;
3033 struct got_fileindex *fileindex = NULL;
3034 struct got_pathlist_entry *pe;
3036 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3040 TAILQ_FOREACH(pe, commitable_paths, entry) {
3041 struct got_fileindex_entry *ie;
3042 struct got_commitable *ct = pe->data;
3044 ie = got_fileindex_entry_get(fileindex, pe->path);
3046 if (ct->status == GOT_STATUS_DELETE) {
3047 got_fileindex_entry_remove(fileindex, ie);
3048 got_fileindex_entry_free(ie);
3050 err = got_fileindex_entry_update(ie,
3051 ct->ondisk_path, ct->blob_id->sha1,
3052 new_base_commit_id->sha1, 1);
3054 err = got_fileindex_entry_alloc(&ie,
3055 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3056 new_base_commit_id->sha1);
3059 err = got_fileindex_entry_add(fileindex, ie);
3064 sync_err = sync_fileindex(fileindex, fileindex_path);
3065 if (sync_err && err == NULL)
3067 free(fileindex_path);
3068 got_fileindex_free(fileindex);
3072 static const struct got_error *
3073 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3074 struct got_object_id *head_commit_id)
3076 const struct got_error *err = NULL;
3077 struct got_object_id *id_in_head = NULL, *id = NULL;
3078 struct got_commit_object *commit = NULL;
3080 const char *ct_path = ct->in_repo_path;
3082 while (ct_path[0] == '/')
3086 * Ensure that no modifications were made to files *and their parents*
3087 * in commits between the file's base commit and the branch head.
3089 * Checking the parents is important for detecting conflicting tree
3090 * configurations (files or parent folders might have been moved,
3091 * deleted, added again, etc.). Such changes need to be merged with
3092 * local changes before a commit can occur.
3094 * The implication is that the file's (parent) entry in the root
3095 * directory must have the same ID in all relevant commits.
3097 if (ct->status != GOT_STATUS_ADD) {
3098 struct got_object_qid *pid;
3100 struct got_object_id *root_entry_id = NULL;
3102 /* Trivial case: base commit == head commit */
3103 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3106 /* Compute the path to the root directory's entry. */
3107 path = strdup(ct_path);
3109 err = got_error_from_errno("strdup");
3112 slash = strchr(path, '/');
3116 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3120 err = got_object_id_by_path(&root_entry_id, repo,
3121 head_commit_id, path);
3125 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3127 struct got_commit_object *pcommit;
3129 err = got_object_id_by_path(&id, repo, pid->id, path);
3131 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3137 err = got_object_id_by_path(&id, repo, pid->id, path);
3141 if (got_object_id_cmp(id, root_entry_id) != 0) {
3142 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3146 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3147 break; /* all relevant commits scanned */
3149 err = got_object_open_as_commit(&pcommit, repo,
3154 got_object_commit_close(commit);
3156 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3160 /* Require that added files don't exist in the branch head. */
3161 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3163 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3165 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3169 got_object_commit_close(commit);
3176 const struct got_error *
3177 got_worktree_commit(struct got_object_id **new_commit_id,
3178 struct got_worktree *worktree, const char *ondisk_path,
3179 const char *author, const char *committer,
3180 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3181 got_worktree_status_cb status_cb, void *status_arg,
3182 struct got_repository *repo)
3184 const struct got_error *err = NULL, *unlockerr = NULL;
3185 struct collect_commitables_arg cc_arg;
3186 struct got_pathlist_head commitable_paths;
3187 struct got_pathlist_entry *pe;
3188 char *relpath = NULL;
3189 const char *head_ref_name = NULL;
3190 struct got_reference *head_ref = NULL;
3191 struct got_commit_object *head_commit = NULL;
3192 struct got_object_id *head_commit_id = NULL;
3193 struct got_reference *head_ref2 = NULL;
3194 struct got_object_id *head_commit_id2 = NULL;
3195 struct got_tree_object *head_tree = NULL;
3196 struct got_object_id *new_tree_id = NULL;
3197 struct got_object_id_queue parent_ids;
3198 struct got_object_qid *pid = NULL;
3199 char *logmsg = NULL;
3201 *new_commit_id = NULL;
3203 TAILQ_INIT(&commitable_paths);
3204 SIMPLEQ_INIT(&parent_ids);
3207 err = got_path_skip_common_ancestor(&relpath,
3208 worktree->root_path, ondisk_path);
3213 err = lock_worktree(worktree, LOCK_EX);
3217 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3220 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3224 cc_arg.commitable_paths = &commitable_paths;
3225 cc_arg.worktree = worktree;
3227 err = got_worktree_status(worktree, relpath ? relpath : "",
3228 repo, collect_commitables, &cc_arg, NULL, NULL);
3232 if (TAILQ_EMPTY(&commitable_paths)) {
3233 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3237 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3241 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3242 struct got_commitable *ct = pe->data;
3243 err = check_ct_out_of_date(ct, repo, head_commit_id);
3248 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3252 if (commit_msg_cb != NULL) {
3253 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3258 if (logmsg == NULL || strlen(logmsg) == 0) {
3259 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3263 /* Create blobs from added and modified files and record their IDs. */
3264 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3265 struct got_commitable *ct = pe->data;
3268 if (ct->status != GOT_STATUS_ADD &&
3269 ct->status != GOT_STATUS_MODIFY)
3272 if (asprintf(&ondisk_path, "%s/%s",
3273 worktree->root_path, pe->path) == -1) {
3274 err = got_error_from_errno("asprintf");
3277 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3283 /* Recursively write new tree objects. */
3284 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3285 status_cb, status_arg, repo);
3289 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3292 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3293 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3294 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3295 got_object_qid_free(pid);
3301 /* Check if a concurrent commit to our branch has occurred. */
3302 head_ref_name = got_worktree_get_head_ref_name(worktree);
3303 if (head_ref_name == NULL) {
3304 err = got_error_from_errno("got_worktree_get_head_ref_name");
3307 /* Lock the reference here to prevent concurrent modification. */
3308 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3311 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3314 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3315 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3318 /* Update branch head in repository. */
3319 err = got_ref_change_ref(head_ref2, *new_commit_id);
3322 err = got_ref_write(head_ref2, repo);
3326 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3330 err = ref_base_commit(worktree, repo);
3334 err = update_fileindex_after_commit(&commitable_paths,
3335 *new_commit_id, worktree);
3339 unlockerr = lock_worktree(worktree, LOCK_SH);
3340 if (unlockerr && err == NULL)
3342 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3343 struct got_commitable *ct = pe->data;
3344 free_commitable(ct);
3346 got_pathlist_free(&commitable_paths);
3348 got_object_tree_close(head_tree);
3350 got_object_commit_close(head_commit);
3352 free(head_commit_id);
3353 free(head_commit_id2);
3355 got_ref_close(head_ref);
3357 unlockerr = got_ref_unlock(head_ref2);
3358 if (unlockerr && err == NULL)
3360 got_ref_close(head_ref2);
3366 got_commitable_get_path(struct got_commitable *ct)
3372 got_commitable_get_status(struct got_commitable *ct)