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, NULL, f_deriv,
770 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
771 err = got_error_from_errno("asprintf");
776 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
780 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
786 * If the file has no blob, this is an "add vs add" conflict,
787 * and we simply use an empty ancestor file to make both files
788 * appear in the merged result in their entirety.
792 err = got_object_id_str(&id_str, worktree->base_commit_id);
795 if (asprintf(&label1, "commit %s", id_str) == -1) {
796 err = got_error_from_errno("asprintf");
800 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
801 blob_orig_path, ondisk_path, label1, path);
805 (*progress_cb)(progress_arg,
806 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
808 if (fsync(merged_fd) != 0) {
809 err = got_error_from_errno("fsync");
813 /* Check if a clean merge has subsumed all local changes. */
814 if (overlapcnt == 0) {
815 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
821 if (chmod(merged_path, st_mode) != 0) {
822 err = got_error_from_errno2("chmod", merged_path);
826 if (rename(merged_path, ondisk_path) != 0) {
827 err = got_error_from_errno3("rename", merged_path,
834 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
835 err = got_error_from_errno("close");
836 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
837 err = got_error_from_errno("fclose");
838 if (f_orig && fclose(f_orig) != 0 && err == NULL)
839 err = got_error_from_errno("fclose");
842 if (blob_deriv_path) {
843 unlink(blob_deriv_path);
844 free(blob_deriv_path);
846 if (blob_orig_path) {
847 unlink(blob_orig_path);
848 free(blob_orig_path);
855 static const struct got_error *
856 update_blob_fileindex_entry(struct got_worktree *worktree,
857 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
858 const char *ondisk_path, const char *path, struct got_blob_object *blob,
859 int update_timestamps)
861 const struct got_error *err = NULL;
864 ie = got_fileindex_entry_get(fileindex, path);
866 err = got_fileindex_entry_update(ie, ondisk_path,
867 blob->id.sha1, worktree->base_commit_id->sha1,
870 struct got_fileindex_entry *new_ie;
871 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
872 path, blob->id.sha1, worktree->base_commit_id->sha1);
874 err = got_fileindex_entry_add(fileindex, new_ie);
879 static const struct got_error *
880 install_blob(struct got_worktree *worktree, const char *ondisk_path,
881 const char *path, uint16_t te_mode, uint16_t st_mode,
882 struct got_blob_object *blob, int restoring_missing_file,
883 int reverting_versioned_file, struct got_repository *repo,
884 got_worktree_checkout_cb progress_cb, void *progress_arg)
886 const struct got_error *err = NULL;
890 char *tmppath = NULL;
892 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
893 GOT_DEFAULT_FILE_MODE);
895 if (errno == ENOENT) {
896 char *parent = dirname(path);
898 return got_error_from_errno2("dirname", path);
899 err = add_dir_on_disk(worktree, parent);
902 fd = open(ondisk_path,
903 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
904 GOT_DEFAULT_FILE_MODE);
906 return got_error_from_errno2("open",
908 } else if (errno == EEXIST) {
909 if (!S_ISREG(st_mode)) {
910 /* TODO file is obstructed; do something */
911 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
914 err = got_opentemp_named_fd(&tmppath, &fd,
921 return got_error_from_errno2("open", ondisk_path);
924 if (restoring_missing_file)
925 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
926 else if (reverting_versioned_file)
927 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
929 (*progress_cb)(progress_arg,
930 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
932 hdrlen = got_object_blob_get_hdrlen(blob);
934 const uint8_t *buf = got_object_blob_get_read_buf(blob);
935 err = got_object_blob_read_block(&len, blob);
939 /* Skip blob object header first time around. */
940 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
942 err = got_error_from_errno("write");
944 } else if (outlen != len - hdrlen) {
945 err = got_error(GOT_ERR_IO);
952 if (fsync(fd) != 0) {
953 err = got_error_from_errno("fsync");
958 if (rename(tmppath, ondisk_path) != 0) {
959 err = got_error_from_errno3("rename", tmppath,
966 if (te_mode & S_IXUSR) {
967 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
968 err = got_error_from_errno2("chmod", ondisk_path);
972 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
973 err = got_error_from_errno2("chmod", ondisk_path);
979 if (fd != -1 && close(fd) != 0 && err == NULL)
980 err = got_error_from_errno("close");
985 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
986 static const struct got_error *
987 get_modified_file_content_status(unsigned char *status, FILE *f)
989 const struct got_error *err = NULL;
990 const char *markers[3] = {
991 GOT_DIFF_CONFLICT_MARKER_BEGIN,
992 GOT_DIFF_CONFLICT_MARKER_SEP,
993 GOT_DIFF_CONFLICT_MARKER_END
998 const char delim[3] = {'\0', '\0', '\0'};
1000 while (*status == GOT_STATUS_MODIFY) {
1001 line = fparseln(f, &len, NULL, delim, 0);
1005 err = got_ferror(f, GOT_ERR_IO);
1009 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1010 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1012 *status = GOT_STATUS_CONFLICT;
1021 static const struct got_error *
1022 get_file_status(unsigned char *status, struct stat *sb,
1023 struct got_fileindex_entry *ie, const char *abspath,
1024 struct got_repository *repo)
1026 const struct got_error *err = NULL;
1027 struct got_object_id id;
1031 struct got_blob_object *blob = NULL;
1034 *status = GOT_STATUS_NO_CHANGE;
1036 if (lstat(abspath, sb) == -1) {
1037 if (errno == ENOENT) {
1039 if (got_fileindex_entry_has_file_on_disk(ie))
1040 *status = GOT_STATUS_MISSING;
1042 *status = GOT_STATUS_DELETE;
1044 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1045 & (S_IRWXU | S_IRWXG | S_IRWXO));
1047 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1050 return got_error_from_errno2("lstat", abspath);
1053 if (!S_ISREG(sb->st_mode)) {
1054 *status = GOT_STATUS_OBSTRUCTED;
1061 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1062 *status = GOT_STATUS_DELETE;
1064 } else if (!got_fileindex_entry_has_blob(ie)) {
1065 *status = GOT_STATUS_ADD;
1069 if (ie->ctime_sec == sb->st_ctime &&
1070 ie->ctime_nsec == sb->st_ctimensec &&
1071 ie->mtime_sec == sb->st_mtime &&
1072 ie->mtime_sec == sb->st_mtime &&
1073 ie->mtime_nsec == sb->st_mtimensec &&
1074 ie->size == (sb->st_size & 0xffffffff))
1077 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1078 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1082 f = fopen(abspath, "r");
1084 err = got_error_from_errno2("fopen", abspath);
1087 hdrlen = got_object_blob_get_hdrlen(blob);
1089 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1090 err = got_object_blob_read_block(&blen, blob);
1093 /* Skip length of blob object header first time around. */
1094 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1095 if (flen == 0 && ferror(f)) {
1096 err = got_error_from_errno("fread");
1101 *status = GOT_STATUS_MODIFY;
1103 } else if (flen == 0) {
1105 *status = GOT_STATUS_MODIFY;
1107 } else if (blen - hdrlen == flen) {
1108 /* Skip blob object header first time around. */
1109 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1110 *status = GOT_STATUS_MODIFY;
1114 *status = GOT_STATUS_MODIFY;
1120 if (*status == GOT_STATUS_MODIFY) {
1122 err = get_modified_file_content_status(status, f);
1126 got_object_blob_close(blob);
1132 static const struct got_error *
1133 update_blob(struct got_worktree *worktree,
1134 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1135 struct got_tree_entry *te, const char *path,
1136 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1139 const struct got_error *err = NULL;
1140 struct got_blob_object *blob = NULL;
1142 unsigned char status = GOT_STATUS_NO_CHANGE;
1145 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1146 return got_error_from_errno("asprintf");
1148 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1152 if (status == GOT_STATUS_OBSTRUCTED) {
1153 (*progress_cb)(progress_arg, status, path);
1157 if (ie && status != GOT_STATUS_MISSING) {
1158 if (got_fileindex_entry_has_commit(ie) &&
1159 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1160 SHA1_DIGEST_LENGTH) == 0) {
1161 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1165 if (got_fileindex_entry_has_blob(ie) &&
1166 memcmp(ie->blob_sha1, te->id->sha1,
1167 SHA1_DIGEST_LENGTH) == 0)
1171 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1175 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1176 int update_timestamps;
1177 struct got_blob_object *blob2 = NULL;
1178 if (got_fileindex_entry_has_blob(ie)) {
1179 struct got_object_id id2;
1180 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1181 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1185 err = merge_blob(&update_timestamps, worktree, blob2,
1186 ondisk_path, path, sb.st_mode, blob, repo,
1187 progress_cb, progress_arg);
1189 got_object_blob_close(blob2);
1191 * Do not update timestamps of files with local changes.
1192 * Otherwise, a future status walk would treat them as
1193 * unmodified files again.
1195 err = got_fileindex_entry_update(ie, ondisk_path,
1196 blob->id.sha1, worktree->base_commit_id->sha1,
1198 } else if (status == GOT_STATUS_DELETE) {
1199 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1200 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1201 ondisk_path, path, blob, 0);
1205 err = install_blob(worktree, ondisk_path, path, te->mode,
1206 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1207 repo, progress_cb, progress_arg);
1210 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1211 ondisk_path, path, blob, 1);
1215 got_object_blob_close(blob);
1221 static const struct got_error *
1222 remove_ondisk_file(const char *root_path, const char *path)
1224 const struct got_error *err = NULL;
1225 char *ondisk_path = NULL;
1227 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1228 return got_error_from_errno("asprintf");
1230 if (unlink(ondisk_path) == -1) {
1231 if (errno != ENOENT)
1232 err = got_error_from_errno2("unlink", ondisk_path);
1234 char *parent = dirname(ondisk_path);
1235 while (parent && strcmp(parent, root_path) != 0) {
1236 if (rmdir(parent) == -1) {
1237 if (errno != ENOTEMPTY)
1238 err = got_error_from_errno2("rmdir",
1242 parent = dirname(parent);
1249 static const struct got_error *
1250 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1251 struct got_fileindex_entry *ie, struct got_repository *repo,
1252 got_worktree_checkout_cb progress_cb, void *progress_arg)
1254 const struct got_error *err = NULL;
1255 unsigned char status;
1259 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1261 return got_error_from_errno("asprintf");
1263 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1267 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1268 status == GOT_STATUS_ADD) {
1269 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1271 * Preserve the working file and change the deleted blob's
1272 * entry into a schedule-add entry.
1274 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1279 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1280 if (status == GOT_STATUS_NO_CHANGE) {
1281 err = remove_ondisk_file(worktree->root_path, ie->path);
1285 got_fileindex_entry_remove(fileindex, ie);
1291 struct diff_cb_arg {
1292 struct got_fileindex *fileindex;
1293 struct got_worktree *worktree;
1294 struct got_repository *repo;
1295 got_worktree_checkout_cb progress_cb;
1297 got_worktree_cancel_cb cancel_cb;
1301 static const struct got_error *
1302 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1303 struct got_tree_entry *te, const char *parent_path)
1305 struct diff_cb_arg *a = arg;
1307 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1308 return got_error(GOT_ERR_CANCELLED);
1310 return update_blob(a->worktree, a->fileindex, ie, te,
1311 ie->path, a->repo, a->progress_cb, a->progress_arg);
1314 static const struct got_error *
1315 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1317 struct diff_cb_arg *a = arg;
1319 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 return got_error(GOT_ERR_CANCELLED);
1322 return delete_blob(a->worktree, a->fileindex, ie,
1323 a->repo, a->progress_cb, a->progress_arg);
1326 static const struct got_error *
1327 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1329 struct diff_cb_arg *a = arg;
1330 const struct got_error *err;
1333 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1334 return got_error(GOT_ERR_CANCELLED);
1336 if (asprintf(&path, "%s%s%s", parent_path,
1337 parent_path[0] ? "/" : "", te->name)
1339 return got_error_from_errno("asprintf");
1341 if (S_ISDIR(te->mode))
1342 err = add_dir_on_disk(a->worktree, path);
1344 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1345 a->repo, a->progress_cb, a->progress_arg);
1351 const struct got_error *
1352 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1354 const struct got_error *err = NULL;
1355 char *uuidstr = NULL;
1356 uint32_t uuid_status;
1360 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1361 if (uuid_status != uuid_s_ok)
1362 return got_error_uuid(uuid_status);
1364 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1366 err = got_error_from_errno("asprintf");
1374 * Prevent Git's garbage collector from deleting our base commit by
1375 * setting a reference to our base commit's ID.
1377 static const struct got_error *
1378 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1380 const struct got_error *err = NULL;
1381 struct got_reference *ref = NULL;
1384 err = got_worktree_get_base_ref_name(&refname, worktree);
1388 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1392 err = got_ref_write(ref, repo);
1400 static const struct got_error *
1401 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1402 struct got_worktree *worktree)
1404 const struct got_error *err = NULL;
1407 *fileindex_path = NULL;
1408 *fileindex = got_fileindex_alloc();
1409 if (*fileindex == NULL)
1410 return got_error_from_errno("got_fileindex_alloc");
1412 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1413 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1414 err = got_error_from_errno("asprintf");
1415 *fileindex_path = NULL;
1419 index = fopen(*fileindex_path, "rb");
1420 if (index == NULL) {
1421 if (errno != ENOENT)
1422 err = got_error_from_errno2("fopen", *fileindex_path);
1424 err = got_fileindex_read(*fileindex, index);
1425 if (fclose(index) != 0 && err == NULL)
1426 err = got_error_from_errno("fclose");
1430 free(*fileindex_path);
1431 *fileindex_path = NULL;
1438 struct bump_base_commit_id_arg {
1439 struct got_object_id *base_commit_id;
1442 const char *entry_name;
1443 got_worktree_checkout_cb progress_cb;
1447 /* Bump base commit ID of all files within an updated part of the work tree. */
1448 static const struct got_error *
1449 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1451 struct bump_base_commit_id_arg *a = arg;
1453 if (a->entry_name) {
1454 if (strcmp(ie->path, a->path) != 0)
1456 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1459 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1460 SHA1_DIGEST_LENGTH) == 0)
1463 (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1464 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1468 static const struct got_error *
1469 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1471 const struct got_error *err = NULL;
1472 char *new_fileindex_path = NULL;
1473 FILE *new_index = NULL;
1475 err = got_opentemp_named(&new_fileindex_path, &new_index,
1480 err = got_fileindex_write(fileindex, new_index);
1484 if (rename(new_fileindex_path, fileindex_path) != 0) {
1485 err = got_error_from_errno3("rename", new_fileindex_path,
1487 unlink(new_fileindex_path);
1492 free(new_fileindex_path);
1496 const struct got_error *
1497 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1498 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1499 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1501 const struct got_error *err = NULL, *sync_err, *unlockerr;
1502 struct got_commit_object *commit = NULL;
1503 struct got_object_id *tree_id = NULL;
1504 struct got_tree_object *tree = NULL;
1505 struct got_fileindex *fileindex = NULL;
1506 char *fileindex_path = NULL;
1507 struct got_fileindex_diff_tree_cb diff_cb;
1508 struct diff_cb_arg arg;
1509 char *relpath = NULL, *entry_name = NULL;
1510 struct bump_base_commit_id_arg bbc_arg;
1512 err = lock_worktree(worktree, LOCK_EX);
1517 * Read the file index.
1518 * Checking out files is supposed to be an idempotent operation.
1519 * If the on-disk file index is incomplete we will try to complete it.
1521 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1525 err = ref_base_commit(worktree, repo);
1529 err = got_object_open_as_commit(&commit, repo,
1530 worktree->base_commit_id);
1537 relpath = strdup(path);
1538 if (relpath == NULL) {
1539 err = got_error_from_errno("strdup");
1542 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1543 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1545 err = got_error_from_errno("asprintf");
1548 err = got_object_id_by_path(&tree_id, repo,
1549 worktree->base_commit_id, tree_path);
1553 err = got_object_get_type(&obj_type, repo, tree_id);
1556 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1557 /* Split provided path into parent dir + entry name. */
1558 if (strchr(path, '/') == NULL) {
1559 relpath = strdup("");
1560 if (relpath == NULL) {
1561 err = got_error_from_errno("strdup");
1564 tree_path = strdup(worktree->path_prefix);
1565 if (tree_path == NULL) {
1566 err = got_error_from_errno("strdup");
1570 err = got_path_dirname(&relpath, path);
1573 if (asprintf(&tree_path, "%s%s%s",
1574 worktree->path_prefix,
1575 got_path_is_root_dir(
1576 worktree->path_prefix) ? "" : "/",
1578 err = got_error_from_errno("asprintf");
1582 err = got_object_id_by_path(&tree_id, repo,
1583 worktree->base_commit_id, tree_path);
1587 entry_name = basename(path);
1588 if (entry_name == NULL) {
1589 err = got_error_from_errno2("basename", path);
1594 relpath = strdup("");
1595 if (relpath == NULL) {
1596 err = got_error_from_errno("strdup");
1599 err = got_object_id_by_path(&tree_id, repo,
1600 worktree->base_commit_id, worktree->path_prefix);
1605 err = got_object_open_as_tree(&tree, repo, tree_id);
1610 got_object_tree_find_entry(tree, entry_name) == NULL) {
1611 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1615 diff_cb.diff_old_new = diff_old_new;
1616 diff_cb.diff_old = diff_old;
1617 diff_cb.diff_new = diff_new;
1618 arg.fileindex = fileindex;
1619 arg.worktree = worktree;
1621 arg.progress_cb = progress_cb;
1622 arg.progress_arg = progress_arg;
1623 arg.cancel_cb = cancel_cb;
1624 arg.cancel_arg = cancel_arg;
1625 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1626 entry_name, repo, &diff_cb, &arg);
1630 bbc_arg.base_commit_id = worktree->base_commit_id;
1631 bbc_arg.entry_name = entry_name;
1632 bbc_arg.path = path;
1633 bbc_arg.path_len = strlen(path);
1634 bbc_arg.progress_cb = progress_cb;
1635 bbc_arg.progress_arg = progress_arg;
1636 err = got_fileindex_for_each_entry_safe(fileindex,
1637 bump_base_commit_id, &bbc_arg);
1639 sync_err = sync_fileindex(fileindex, fileindex_path);
1640 if (sync_err && err == NULL)
1643 free(fileindex_path);
1646 got_object_tree_close(tree);
1648 got_object_commit_close(commit);
1649 got_fileindex_free(fileindex);
1650 unlockerr = lock_worktree(worktree, LOCK_SH);
1651 if (unlockerr && err == NULL)
1656 struct merge_file_cb_arg {
1657 struct got_worktree *worktree;
1658 struct got_fileindex *fileindex;
1659 got_worktree_checkout_cb progress_cb;
1661 got_worktree_cancel_cb cancel_cb;
1665 static const struct got_error *
1666 merge_file_cb(void *arg, struct got_blob_object *blob1,
1667 struct got_blob_object *blob2, struct got_object_id *id1,
1668 struct got_object_id *id2, const char *path1, const char *path2,
1669 struct got_repository *repo)
1671 static const struct got_error *err = NULL;
1672 struct merge_file_cb_arg *a = arg;
1673 struct got_fileindex_entry *ie;
1674 char *ondisk_path = NULL;
1676 unsigned char status;
1677 int local_changes_subsumed;
1679 if (blob1 && blob2) {
1680 ie = got_fileindex_entry_get(a->fileindex, path2);
1682 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1687 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1689 return got_error_from_errno("asprintf");
1691 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1695 if (status == GOT_STATUS_DELETE) {
1696 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1700 if (status != GOT_STATUS_NO_CHANGE &&
1701 status != GOT_STATUS_MODIFY &&
1702 status != GOT_STATUS_CONFLICT &&
1703 status != GOT_STATUS_ADD) {
1704 (*a->progress_cb)(a->progress_arg, status, path2);
1708 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1709 ondisk_path, path2, sb.st_mode, blob2, repo,
1710 a->progress_cb, a->progress_arg);
1712 ie = got_fileindex_entry_get(a->fileindex, path1);
1714 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1719 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1721 return got_error_from_errno("asprintf");
1723 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1728 case GOT_STATUS_NO_CHANGE:
1729 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1731 err = remove_ondisk_file(a->worktree->root_path, path1);
1735 got_fileindex_entry_mark_deleted_from_disk(ie);
1737 case GOT_STATUS_DELETE:
1738 case GOT_STATUS_MISSING:
1739 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1742 got_fileindex_entry_mark_deleted_from_disk(ie);
1744 case GOT_STATUS_ADD:
1745 case GOT_STATUS_MODIFY:
1746 case GOT_STATUS_CONFLICT:
1747 (*a->progress_cb)(a->progress_arg,
1748 GOT_STATUS_CANNOT_DELETE, path1);
1750 case GOT_STATUS_OBSTRUCTED:
1751 (*a->progress_cb)(a->progress_arg, status, path1);
1757 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1759 return got_error_from_errno("asprintf");
1760 ie = got_fileindex_entry_get(a->fileindex, path2);
1762 err = get_file_status(&status, &sb, ie, ondisk_path,
1766 if (status != GOT_STATUS_NO_CHANGE &&
1767 status != GOT_STATUS_MODIFY &&
1768 status != GOT_STATUS_CONFLICT &&
1769 status != GOT_STATUS_ADD) {
1770 (*a->progress_cb)(a->progress_arg, status,
1774 err = merge_blob(&local_changes_subsumed, a->worktree,
1775 NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1776 a->progress_cb, a->progress_arg);
1777 if (status == GOT_STATUS_DELETE) {
1778 err = update_blob_fileindex_entry(a->worktree,
1779 a->fileindex, ie, ondisk_path, ie->path,
1785 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1786 err = install_blob(a->worktree, ondisk_path, path2,
1787 /* XXX get this from parent tree! */
1788 GOT_DEFAULT_FILE_MODE,
1789 sb.st_mode, blob2, 0, 0, repo,
1790 a->progress_cb, a->progress_arg);
1793 err = got_fileindex_entry_alloc(&ie,
1794 ondisk_path, path2, NULL, NULL);
1797 err = got_fileindex_entry_add(a->fileindex, ie);
1799 got_fileindex_entry_free(ie);
1809 struct check_merge_ok_arg {
1810 struct got_worktree *worktree;
1811 struct got_repository *repo;
1814 static const struct got_error *
1815 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1817 const struct got_error *err = NULL;
1818 struct check_merge_ok_arg *a = arg;
1819 unsigned char status;
1823 /* Reject merges into a work tree with mixed base commits. */
1824 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1825 SHA1_DIGEST_LENGTH))
1826 return got_error(GOT_ERR_MIXED_COMMITS);
1828 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1830 return got_error_from_errno("asprintf");
1832 /* Reject merges into a work tree with conflicted files. */
1833 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1836 if (status == GOT_STATUS_CONFLICT)
1837 return got_error(GOT_ERR_CONFLICTS);
1842 const struct got_error *
1843 got_worktree_merge_files(struct got_worktree *worktree,
1844 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1845 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1846 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1848 const struct got_error *err = NULL, *sync_err, *unlockerr;
1849 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1850 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1851 struct merge_file_cb_arg arg;
1852 char *fileindex_path = NULL;
1853 struct got_fileindex *fileindex = NULL;
1854 struct check_merge_ok_arg mok_arg;
1856 err = lock_worktree(worktree, LOCK_EX);
1860 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1864 mok_arg.worktree = worktree;
1865 mok_arg.repo = repo;
1866 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1872 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1873 worktree->path_prefix);
1877 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1882 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1883 worktree->path_prefix);
1887 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1891 arg.worktree = worktree;
1892 arg.fileindex = fileindex;
1893 arg.progress_cb = progress_cb;
1894 arg.progress_arg = progress_arg;
1895 arg.cancel_cb = cancel_cb;
1896 arg.cancel_arg = cancel_arg;
1897 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1898 sync_err = sync_fileindex(fileindex, fileindex_path);
1899 if (sync_err && err == NULL)
1902 got_fileindex_free(fileindex);
1904 got_object_tree_close(tree1);
1906 got_object_tree_close(tree2);
1908 unlockerr = lock_worktree(worktree, LOCK_SH);
1909 if (unlockerr && err == NULL)
1914 struct diff_dir_cb_arg {
1915 struct got_fileindex *fileindex;
1916 struct got_worktree *worktree;
1917 const char *status_path;
1918 size_t status_path_len;
1919 struct got_repository *repo;
1920 got_worktree_status_cb status_cb;
1922 got_worktree_cancel_cb cancel_cb;
1926 static const struct got_error *
1927 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1928 got_worktree_status_cb status_cb, void *status_arg,
1929 struct got_repository *repo)
1931 const struct got_error *err = NULL;
1932 unsigned char status = GOT_STATUS_NO_CHANGE;
1934 struct got_object_id blob_id, commit_id;
1936 err = get_file_status(&status, &sb, ie, abspath, repo);
1937 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1938 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1939 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1940 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1946 static const struct got_error *
1947 status_old_new(void *arg, struct got_fileindex_entry *ie,
1948 struct dirent *de, const char *parent_path)
1950 const struct got_error *err = NULL;
1951 struct diff_dir_cb_arg *a = arg;
1954 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1955 return got_error(GOT_ERR_CANCELLED);
1957 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1958 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1961 if (parent_path[0]) {
1962 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1963 parent_path, de->d_name) == -1)
1964 return got_error_from_errno("asprintf");
1966 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1968 return got_error_from_errno("asprintf");
1971 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1977 static const struct got_error *
1978 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1980 struct diff_dir_cb_arg *a = arg;
1981 struct got_object_id blob_id, commit_id;
1982 unsigned char status;
1984 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1985 return got_error(GOT_ERR_CANCELLED);
1987 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1990 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1991 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1992 if (got_fileindex_entry_has_file_on_disk(ie))
1993 status = GOT_STATUS_MISSING;
1995 status = GOT_STATUS_DELETE;
1996 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2000 static const struct got_error *
2001 status_new(void *arg, struct dirent *de, const char *parent_path)
2003 const struct got_error *err = NULL;
2004 struct diff_dir_cb_arg *a = arg;
2007 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2008 return got_error(GOT_ERR_CANCELLED);
2010 if (de->d_type == DT_DIR)
2013 /* XXX ignore symlinks for now */
2014 if (de->d_type == DT_LNK)
2017 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2020 if (parent_path[0]) {
2021 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2022 return got_error_from_errno("asprintf");
2027 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2034 const struct got_error *
2035 got_worktree_status(struct got_worktree *worktree, const char *path,
2036 struct got_repository *repo, got_worktree_status_cb status_cb,
2037 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2039 const struct got_error *err = NULL;
2040 DIR *workdir = NULL;
2041 char *fileindex_path = NULL;
2042 struct got_fileindex *fileindex = NULL;
2044 struct got_fileindex_diff_dir_cb fdiff_cb;
2045 struct diff_dir_cb_arg arg;
2046 char *ondisk_path = NULL;
2048 fileindex = got_fileindex_alloc();
2049 if (fileindex == NULL) {
2050 err = got_error_from_errno("got_fileindex_alloc");
2054 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2055 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2056 err = got_error_from_errno("asprintf");
2057 fileindex_path = NULL;
2061 index = fopen(fileindex_path, "rb");
2062 if (index == NULL) {
2063 if (errno != ENOENT) {
2064 err = got_error_from_errno2("fopen", fileindex_path);
2068 err = got_fileindex_read(fileindex, index);
2074 if (asprintf(&ondisk_path, "%s%s%s",
2075 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2076 err = got_error_from_errno("asprintf");
2079 workdir = opendir(ondisk_path);
2080 if (workdir == NULL) {
2081 if (errno == ENOTDIR || errno == ENOENT) {
2082 struct got_fileindex_entry *ie;
2083 ie = got_fileindex_entry_get(fileindex, path);
2085 err = got_error(GOT_ERR_BAD_PATH);
2088 err = report_file_status(ie, ondisk_path,
2089 status_cb, status_arg, repo);
2092 err = got_error_from_errno2("opendir", ondisk_path);
2096 fdiff_cb.diff_old_new = status_old_new;
2097 fdiff_cb.diff_old = status_old;
2098 fdiff_cb.diff_new = status_new;
2099 arg.fileindex = fileindex;
2100 arg.worktree = worktree;
2101 arg.status_path = path;
2102 arg.status_path_len = strlen(path);
2104 arg.status_cb = status_cb;
2105 arg.status_arg = status_arg;
2106 arg.cancel_cb = cancel_cb;
2107 arg.cancel_arg = cancel_arg;
2108 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2109 path, repo, &fdiff_cb, &arg);
2114 free(fileindex_path);
2115 got_fileindex_free(fileindex);
2119 const struct got_error *
2120 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2123 const struct got_error *err = NULL;
2124 char *resolved, *path = NULL;
2129 resolved = realpath(arg, NULL);
2130 if (resolved == NULL)
2131 return got_error_from_errno2("realpath", arg);
2133 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2134 strlen(got_worktree_get_root_path(worktree)))) {
2135 err = got_error(GOT_ERR_BAD_PATH);
2139 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2140 err = got_path_skip_common_ancestor(&path,
2141 got_worktree_get_root_path(worktree), resolved);
2147 err = got_error_from_errno("strdup");
2152 /* XXX status walk can't deal with trailing slash! */
2154 while (path[len - 1] == '/') {
2155 path[len - 1] = '\0';
2167 static const struct got_error *
2168 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2169 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2170 struct got_repository *repo)
2172 const struct got_error *err = NULL;
2173 struct got_fileindex_entry *ie;
2175 /* Re-adding an existing entry is a no-op. */
2176 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2179 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2183 err = got_fileindex_entry_add(fileindex, ie);
2185 got_fileindex_entry_free(ie);
2189 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2192 const struct got_error *
2193 got_worktree_schedule_add(struct got_worktree *worktree,
2194 struct got_pathlist_head *ondisk_paths,
2195 got_worktree_status_cb status_cb, void *status_arg,
2196 struct got_repository *repo)
2198 struct got_fileindex *fileindex = NULL;
2199 char *fileindex_path = NULL;
2201 const struct got_error *err = NULL, *sync_err, *unlockerr;
2202 struct got_pathlist_entry *pe;
2204 err = lock_worktree(worktree, LOCK_EX);
2209 fileindex = got_fileindex_alloc();
2210 if (fileindex == NULL) {
2211 err = got_error_from_errno("got_fileindex_alloc");
2215 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2216 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2217 err = got_error_from_errno("asprintf");
2218 fileindex_path = NULL;
2222 index = fopen(fileindex_path, "rb");
2223 if (index == NULL) {
2224 err = got_error_from_errno2("fopen", fileindex_path);
2228 err = got_fileindex_read(fileindex, index);
2232 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2234 err = got_path_skip_common_ancestor(&relpath,
2235 got_worktree_get_root_path(worktree), pe->path);
2238 err = schedule_addition(pe->path, fileindex, relpath,
2239 status_cb, status_arg, repo);
2244 sync_err = sync_fileindex(fileindex, fileindex_path);
2245 if (sync_err && err == NULL)
2249 if (fclose(index) != 0 && err == NULL)
2250 err = got_error_from_errno("fclose");
2253 got_fileindex_free(fileindex);
2254 unlockerr = lock_worktree(worktree, LOCK_SH);
2255 if (unlockerr && err == NULL)
2260 static const struct got_error *
2261 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2262 const char *relpath, int delete_local_mods,
2263 got_worktree_status_cb status_cb, void *status_arg,
2264 struct got_repository *repo)
2266 const struct got_error *err = NULL;
2267 struct got_fileindex_entry *ie = NULL;
2268 unsigned char status;
2271 ie = got_fileindex_entry_get(fileindex, relpath);
2273 return got_error(GOT_ERR_BAD_PATH);
2275 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2279 if (status != GOT_STATUS_NO_CHANGE) {
2280 if (status == GOT_STATUS_DELETE)
2281 return got_error_set_errno(ENOENT, ondisk_path);
2282 if (status != GOT_STATUS_MODIFY)
2283 return got_error(GOT_ERR_FILE_STATUS);
2284 if (!delete_local_mods)
2285 return got_error(GOT_ERR_FILE_MODIFIED);
2288 if (unlink(ondisk_path) != 0)
2289 return got_error_from_errno2("unlink", ondisk_path);
2291 got_fileindex_entry_mark_deleted_from_disk(ie);
2292 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2295 const struct got_error *
2296 got_worktree_schedule_delete(struct got_worktree *worktree,
2297 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2298 got_worktree_status_cb status_cb, void *status_arg,
2299 struct got_repository *repo)
2301 struct got_fileindex *fileindex = NULL;
2302 char *fileindex_path = NULL;
2304 const struct got_error *err = NULL, *sync_err, *unlockerr;
2305 struct got_pathlist_entry *pe;
2307 err = lock_worktree(worktree, LOCK_EX);
2311 fileindex = got_fileindex_alloc();
2312 if (fileindex == NULL) {
2313 err = got_error_from_errno("got_fileindex_alloc");
2317 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 err = got_error_from_errno("asprintf");
2320 fileindex_path = NULL;
2324 index = fopen(fileindex_path, "rb");
2325 if (index == NULL) {
2326 err = got_error_from_errno2("fopen", fileindex_path);
2330 err = got_fileindex_read(fileindex, index);
2334 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2336 err = got_path_skip_common_ancestor(&relpath,
2337 got_worktree_get_root_path(worktree), pe->path);
2340 err = schedule_for_deletion(pe->path, fileindex, relpath,
2341 delete_local_mods, status_cb, status_arg, repo);
2346 sync_err = sync_fileindex(fileindex, fileindex_path);
2347 if (sync_err && err == NULL)
2351 if (fclose(index) != 0 && err == NULL)
2352 err = got_error_from_errno("fclose");
2355 got_fileindex_free(fileindex);
2356 unlockerr = lock_worktree(worktree, LOCK_SH);
2357 if (unlockerr && err == NULL)
2362 static const struct got_error *
2363 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2364 const char *ondisk_path,
2365 got_worktree_checkout_cb progress_cb, void *progress_arg,
2366 struct got_repository *repo)
2368 const struct got_error *err = NULL;
2369 char *relpath = NULL, *parent_path = NULL;
2370 struct got_fileindex_entry *ie;
2371 struct got_tree_object *tree = NULL;
2372 struct got_object_id *tree_id = NULL;
2373 const struct got_tree_entry *te;
2374 char *tree_path = NULL, *te_name;
2375 struct got_blob_object *blob = NULL;
2376 unsigned char status;
2379 err = got_path_skip_common_ancestor(&relpath,
2380 got_worktree_get_root_path(worktree), ondisk_path);
2384 ie = got_fileindex_entry_get(fileindex, relpath);
2386 err = got_error(GOT_ERR_BAD_PATH);
2390 /* Construct in-repository path of tree which contains this blob. */
2391 err = got_path_dirname(&parent_path, ie->path);
2393 if (err->code != GOT_ERR_BAD_PATH)
2395 parent_path = strdup("/");
2396 if (parent_path == NULL) {
2397 err = got_error_from_errno("strdup");
2401 if (got_path_is_root_dir(worktree->path_prefix)) {
2402 tree_path = strdup(parent_path);
2403 if (tree_path == NULL) {
2404 err = got_error_from_errno("strdup");
2408 if (got_path_is_root_dir(parent_path)) {
2409 tree_path = strdup(worktree->path_prefix);
2410 if (tree_path == NULL) {
2411 err = got_error_from_errno("strdup");
2415 if (asprintf(&tree_path, "%s/%s",
2416 worktree->path_prefix, parent_path) == -1) {
2417 err = got_error_from_errno("asprintf");
2423 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2428 err = got_object_open_as_tree(&tree, repo, tree_id);
2432 te_name = basename(ie->path);
2433 if (te_name == NULL) {
2434 err = got_error_from_errno2("basename", ie->path);
2438 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2442 te = got_object_tree_find_entry(tree, te_name);
2443 if (te == NULL && status != GOT_STATUS_ADD) {
2444 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2449 case GOT_STATUS_ADD:
2450 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2451 got_fileindex_entry_remove(fileindex, ie);
2453 case GOT_STATUS_DELETE:
2454 case GOT_STATUS_MODIFY:
2455 case GOT_STATUS_CONFLICT:
2456 case GOT_STATUS_MISSING: {
2457 struct got_object_id id;
2458 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2459 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2462 err = install_blob(worktree, ondisk_path, ie->path,
2463 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2467 if (status == GOT_STATUS_DELETE) {
2468 err = update_blob_fileindex_entry(worktree,
2469 fileindex, ie, ondisk_path, ie->path, blob, 1);
2483 got_object_blob_close(blob);
2485 got_object_tree_close(tree);
2490 const struct got_error *
2491 got_worktree_revert(struct got_worktree *worktree,
2492 struct got_pathlist_head *ondisk_paths,
2493 got_worktree_checkout_cb progress_cb, void *progress_arg,
2494 struct got_repository *repo)
2496 struct got_fileindex *fileindex = NULL;
2497 char *fileindex_path = NULL;
2499 const struct got_error *err = NULL, *unlockerr = NULL;
2500 const struct got_error *sync_err = NULL;
2501 struct got_pathlist_entry *pe;
2503 err = lock_worktree(worktree, LOCK_EX);
2507 fileindex = got_fileindex_alloc();
2508 if (fileindex == NULL) {
2509 err = got_error_from_errno("got_fileindex_alloc");
2513 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2514 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2515 err = got_error_from_errno("asprintf");
2516 fileindex_path = NULL;
2520 index = fopen(fileindex_path, "rb");
2521 if (index == NULL) {
2522 err = got_error_from_errno2("fopen", fileindex_path);
2526 err = got_fileindex_read(fileindex, index);
2530 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2531 err = revert_file(worktree, fileindex, pe->path,
2532 progress_cb, progress_arg, repo);
2536 sync_err = sync_fileindex(fileindex, fileindex_path);
2537 if (sync_err && err == NULL)
2541 if (fclose(index) != 0 && err == NULL)
2542 err = got_error_from_errno("fclose");
2545 got_fileindex_free(fileindex);
2546 unlockerr = lock_worktree(worktree, LOCK_SH);
2547 if (unlockerr && err == NULL)
2553 free_commitable(struct got_commitable *ct)
2556 free(ct->in_repo_path);
2557 free(ct->ondisk_path);
2559 free(ct->base_blob_id);
2560 free(ct->base_commit_id);
2564 struct collect_commitables_arg {
2565 struct got_pathlist_head *commitable_paths;
2566 struct got_repository *repo;
2567 struct got_worktree *worktree;
2570 static const struct got_error *
2571 collect_commitables(void *arg, unsigned char status, const char *relpath,
2572 struct got_object_id *blob_id, struct got_object_id *commit_id)
2574 struct collect_commitables_arg *a = arg;
2575 const struct got_error *err = NULL;
2576 struct got_commitable *ct = NULL;
2577 struct got_pathlist_entry *new = NULL;
2578 char *parent_path = NULL, *path = NULL;
2581 if (status == GOT_STATUS_CONFLICT)
2582 return got_error(GOT_ERR_COMMIT_CONFLICT);
2584 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2585 status != GOT_STATUS_DELETE)
2588 if (asprintf(&path, "/%s", relpath) == -1) {
2589 err = got_error_from_errno("asprintf");
2592 if (strcmp(path, "/") == 0) {
2593 parent_path = strdup("");
2594 if (parent_path == NULL)
2595 return got_error_from_errno("strdup");
2597 err = got_path_dirname(&parent_path, path);
2602 ct = calloc(1, sizeof(*ct));
2604 err = got_error_from_errno("calloc");
2608 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2610 err = got_error_from_errno("asprintf");
2613 if (status == GOT_STATUS_DELETE) {
2614 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2616 if (lstat(ct->ondisk_path, &sb) != 0) {
2617 err = got_error_from_errno2("lstat", ct->ondisk_path);
2620 ct->mode = sb.st_mode;
2623 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2624 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2626 err = got_error_from_errno("asprintf");
2630 ct->status = status;
2631 ct->blob_id = NULL; /* will be filled in when blob gets created */
2632 if (ct->status != GOT_STATUS_ADD) {
2633 ct->base_blob_id = got_object_id_dup(blob_id);
2634 if (ct->base_blob_id == NULL) {
2635 err = got_error_from_errno("got_object_id_dup");
2638 ct->base_commit_id = got_object_id_dup(commit_id);
2639 if (ct->base_commit_id == NULL) {
2640 err = got_error_from_errno("got_object_id_dup");
2644 ct->path = strdup(path);
2645 if (ct->path == NULL) {
2646 err = got_error_from_errno("strdup");
2649 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2651 if (ct && (err || new == NULL))
2652 free_commitable(ct);
2658 static const struct got_error *write_tree(struct got_object_id **,
2659 struct got_tree_object *, const char *, struct got_pathlist_head *,
2660 got_worktree_status_cb status_cb, void *status_arg,
2661 struct got_repository *);
2663 static const struct got_error *
2664 write_subtree(struct got_object_id **new_subtree_id,
2665 struct got_tree_entry *te, const char *parent_path,
2666 struct got_pathlist_head *commitable_paths,
2667 got_worktree_status_cb status_cb, void *status_arg,
2668 struct got_repository *repo)
2670 const struct got_error *err = NULL;
2671 struct got_tree_object *subtree;
2674 if (asprintf(&subpath, "%s%s%s", parent_path,
2675 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2676 return got_error_from_errno("asprintf");
2678 err = got_object_open_as_tree(&subtree, repo, te->id);
2682 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2683 status_cb, status_arg, repo);
2684 got_object_tree_close(subtree);
2689 static const struct got_error *
2690 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2692 const struct got_error *err = NULL;
2693 char *ct_parent_path = NULL;
2697 if (strchr(ct->path, '/') == NULL) {
2698 *match = got_path_is_root_dir(path);
2702 err = got_path_dirname(&ct_parent_path, ct->path);
2705 *match = (strcmp(path, ct_parent_path) == 0);
2706 free(ct_parent_path);
2711 get_ct_file_mode(struct got_commitable *ct)
2713 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2716 static const struct got_error *
2717 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2718 struct got_tree_entry *te, struct got_commitable *ct)
2720 const struct got_error *err = NULL;
2724 err = got_object_tree_entry_dup(new_te, te);
2728 (*new_te)->mode = get_ct_file_mode(ct);
2730 free((*new_te)->id);
2731 (*new_te)->id = got_object_id_dup(ct->blob_id);
2732 if ((*new_te)->id == NULL) {
2733 err = got_error_from_errno("got_object_id_dup");
2737 if (err && *new_te) {
2738 got_object_tree_entry_close(*new_te);
2744 static const struct got_error *
2745 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2746 struct got_commitable *ct)
2748 const struct got_error *err = NULL;
2753 *new_te = calloc(1, sizeof(**new_te));
2754 if (*new_te == NULL)
2755 return got_error_from_errno("calloc");
2757 ct_name = basename(ct->path);
2758 if (ct_name == NULL) {
2759 err = got_error_from_errno2("basename", ct->path);
2762 (*new_te)->name = strdup(ct_name);
2763 if ((*new_te)->name == NULL) {
2764 err = got_error_from_errno("strdup");
2768 (*new_te)->mode = get_ct_file_mode(ct);
2770 (*new_te)->id = got_object_id_dup(ct->blob_id);
2771 if ((*new_te)->id == NULL) {
2772 err = got_error_from_errno("got_object_id_dup");
2776 if (err && *new_te) {
2777 got_object_tree_entry_close(*new_te);
2783 static const struct got_error *
2784 insert_tree_entry(struct got_tree_entry *new_te,
2785 struct got_pathlist_head *paths)
2787 const struct got_error *err = NULL;
2788 struct got_pathlist_entry *new_pe;
2790 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2794 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2798 static const struct got_error *
2799 report_ct_status(struct got_commitable *ct,
2800 got_worktree_status_cb status_cb, void *status_arg)
2802 const char *ct_path = ct->path;
2803 while (ct_path[0] == '/')
2805 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2808 static const struct got_error *
2809 match_modified_subtree(int *modified, struct got_tree_entry *te,
2810 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2812 const struct got_error *err = NULL;
2813 struct got_pathlist_entry *pe;
2818 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2819 got_path_is_root_dir(base_tree_path) ? "" : "/",
2821 return got_error_from_errno("asprintf");
2823 TAILQ_FOREACH(pe, commitable_paths, entry) {
2824 struct got_commitable *ct = pe->data;
2825 *modified = got_path_is_child(ct->in_repo_path, te_path,
2835 static const struct got_error *
2836 match_deleted_or_modified_ct(struct got_commitable **ctp,
2837 struct got_tree_entry *te, const char *base_tree_path,
2838 struct got_pathlist_head *commitable_paths)
2840 const struct got_error *err = NULL;
2841 struct got_pathlist_entry *pe;
2845 TAILQ_FOREACH(pe, commitable_paths, entry) {
2846 struct got_commitable *ct = pe->data;
2847 char *ct_name = NULL;
2850 if (ct->status != GOT_STATUS_MODIFY &&
2851 ct->status != GOT_STATUS_DELETE)
2854 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2857 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2863 ct_name = basename(pe->path);
2864 if (ct_name == NULL)
2865 return got_error_from_errno2("basename", pe->path);
2867 if (strcmp(te->name, ct_name) != 0)
2877 static const struct got_error *
2878 write_tree(struct got_object_id **new_tree_id,
2879 struct got_tree_object *base_tree, const char *path_base_tree,
2880 struct got_pathlist_head *commitable_paths,
2881 got_worktree_status_cb status_cb, void *status_arg,
2882 struct got_repository *repo)
2884 const struct got_error *err = NULL;
2885 const struct got_tree_entries *base_entries = NULL;
2886 struct got_pathlist_head paths;
2887 struct got_tree_entries new_tree_entries;
2888 struct got_tree_entry *te, *new_te = NULL;
2889 struct got_pathlist_entry *pe;
2892 new_tree_entries.nentries = 0;
2893 SIMPLEQ_INIT(&new_tree_entries.head);
2895 /* Insert, and recurse into, newly added entries first. */
2896 TAILQ_FOREACH(pe, commitable_paths, entry) {
2897 struct got_commitable *ct = pe->data;
2898 char *child_path = NULL, *slash;
2900 if (ct->status != GOT_STATUS_ADD ||
2901 (ct->flags & GOT_COMMITABLE_ADDED))
2904 if (!got_path_is_child(pe->path, path_base_tree,
2905 strlen(path_base_tree)))
2908 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2913 slash = strchr(child_path, '/');
2914 if (slash == NULL) {
2915 err = alloc_added_blob_tree_entry(&new_te, ct);
2918 err = report_ct_status(ct, status_cb, status_arg);
2921 ct->flags |= GOT_COMMITABLE_ADDED;
2925 *slash = '\0'; /* trim trailing path components */
2926 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2927 got_path_is_root_dir(path_base_tree) ? "" : "/",
2928 child_path) == -1) {
2929 err = got_error_from_errno("asprintf");
2933 new_te = calloc(1, sizeof(*new_te));
2934 new_te->mode = S_IFDIR;
2935 new_te->name = strdup(child_path);
2936 if (new_te->name == NULL) {
2937 err = got_error_from_errno("strdup");
2938 got_object_tree_entry_close(new_te);
2942 err = write_tree(&new_te->id, NULL, subtree_path,
2943 commitable_paths, status_cb, status_arg, repo);
2946 got_object_tree_entry_close(new_te);
2951 err = insert_tree_entry(new_te, &paths);
2957 /* Handle modified and deleted entries. */
2958 base_entries = got_object_tree_get_entries(base_tree);
2959 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2960 struct got_commitable *ct = NULL;
2962 if (S_ISDIR(te->mode)) {
2964 err = got_object_tree_entry_dup(&new_te, te);
2967 err = match_modified_subtree(&modified, te,
2968 path_base_tree, commitable_paths);
2971 /* Avoid recursion into unmodified subtrees. */
2974 err = write_subtree(&new_te->id, te,
2975 path_base_tree, commitable_paths,
2976 status_cb, status_arg, repo);
2980 err = insert_tree_entry(new_te, &paths);
2986 err = match_deleted_or_modified_ct(&ct, te,
2987 path_base_tree, commitable_paths);
2989 /* NB: Deleted entries get dropped here. */
2990 if (ct->status == GOT_STATUS_MODIFY) {
2991 err = alloc_modified_blob_tree_entry(
2995 err = insert_tree_entry(new_te, &paths);
2999 err = report_ct_status(ct, status_cb,
3004 /* Entry is unchanged; just copy it. */
3005 err = got_object_tree_entry_dup(&new_te, te);
3008 err = insert_tree_entry(new_te, &paths);
3015 /* Write new list of entries; deleted entries have been dropped. */
3016 TAILQ_FOREACH(pe, &paths, entry) {
3017 struct got_tree_entry *te = pe->data;
3018 new_tree_entries.nentries++;
3019 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3021 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3023 got_object_tree_entries_close(&new_tree_entries);
3024 got_pathlist_free(&paths);
3028 static const struct got_error *
3029 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3030 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3032 const struct got_error *err = NULL, *sync_err;
3033 char *fileindex_path = NULL;
3034 struct got_fileindex *fileindex = NULL;
3035 struct got_pathlist_entry *pe;
3037 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3041 TAILQ_FOREACH(pe, commitable_paths, entry) {
3042 struct got_fileindex_entry *ie;
3043 struct got_commitable *ct = pe->data;
3045 ie = got_fileindex_entry_get(fileindex, pe->path);
3047 if (ct->status == GOT_STATUS_DELETE) {
3048 got_fileindex_entry_remove(fileindex, ie);
3049 got_fileindex_entry_free(ie);
3051 err = got_fileindex_entry_update(ie,
3052 ct->ondisk_path, ct->blob_id->sha1,
3053 new_base_commit_id->sha1, 1);
3055 err = got_fileindex_entry_alloc(&ie,
3056 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3057 new_base_commit_id->sha1);
3060 err = got_fileindex_entry_add(fileindex, ie);
3065 sync_err = sync_fileindex(fileindex, fileindex_path);
3066 if (sync_err && err == NULL)
3068 free(fileindex_path);
3069 got_fileindex_free(fileindex);
3073 static const struct got_error *
3074 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3075 struct got_object_id *head_commit_id)
3077 const struct got_error *err = NULL;
3078 struct got_object_id *id_in_head = NULL, *id = NULL;
3079 struct got_commit_object *commit = NULL;
3081 const char *ct_path = ct->in_repo_path;
3083 while (ct_path[0] == '/')
3087 * Ensure that no modifications were made to files *and their parents*
3088 * in commits between the file's base commit and the branch head.
3090 * Checking the parents is important for detecting conflicting tree
3091 * configurations (files or parent folders might have been moved,
3092 * deleted, added again, etc.). Such changes need to be merged with
3093 * local changes before a commit can occur.
3095 * The implication is that the file's (parent) entry in the root
3096 * directory must have the same ID in all relevant commits.
3098 if (ct->status != GOT_STATUS_ADD) {
3099 struct got_object_qid *pid;
3101 struct got_object_id *root_entry_id = NULL;
3103 /* Trivial case: base commit == head commit */
3104 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3107 /* Compute the path to the root directory's entry. */
3108 path = strdup(ct_path);
3110 err = got_error_from_errno("strdup");
3113 slash = strchr(path, '/');
3117 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3121 err = got_object_id_by_path(&root_entry_id, repo,
3122 head_commit_id, path);
3126 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3128 struct got_commit_object *pcommit;
3130 err = got_object_id_by_path(&id, repo, pid->id, path);
3132 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3138 err = got_object_id_by_path(&id, repo, pid->id, path);
3142 if (got_object_id_cmp(id, root_entry_id) != 0) {
3143 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3147 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3148 break; /* all relevant commits scanned */
3150 err = got_object_open_as_commit(&pcommit, repo,
3155 got_object_commit_close(commit);
3157 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3161 /* Require that added files don't exist in the branch head. */
3162 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3164 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3166 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3170 got_object_commit_close(commit);
3177 const struct got_error *
3178 got_worktree_commit(struct got_object_id **new_commit_id,
3179 struct got_worktree *worktree, const char *ondisk_path,
3180 const char *author, const char *committer,
3181 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3182 got_worktree_status_cb status_cb, void *status_arg,
3183 struct got_repository *repo)
3185 const struct got_error *err = NULL, *unlockerr = NULL;
3186 struct collect_commitables_arg cc_arg;
3187 struct got_pathlist_head commitable_paths;
3188 struct got_pathlist_entry *pe;
3189 char *relpath = NULL;
3190 const char *head_ref_name = NULL;
3191 struct got_reference *head_ref = NULL;
3192 struct got_commit_object *head_commit = NULL;
3193 struct got_object_id *head_commit_id = NULL;
3194 struct got_reference *head_ref2 = NULL;
3195 struct got_object_id *head_commit_id2 = NULL;
3196 struct got_tree_object *head_tree = NULL;
3197 struct got_object_id *new_tree_id = NULL;
3198 struct got_object_id_queue parent_ids;
3199 struct got_object_qid *pid = NULL;
3200 char *logmsg = NULL;
3202 *new_commit_id = NULL;
3204 TAILQ_INIT(&commitable_paths);
3205 SIMPLEQ_INIT(&parent_ids);
3208 err = got_path_skip_common_ancestor(&relpath,
3209 worktree->root_path, ondisk_path);
3214 err = lock_worktree(worktree, LOCK_EX);
3218 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3221 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3225 cc_arg.commitable_paths = &commitable_paths;
3226 cc_arg.worktree = worktree;
3228 err = got_worktree_status(worktree, relpath ? relpath : "",
3229 repo, collect_commitables, &cc_arg, NULL, NULL);
3233 if (TAILQ_EMPTY(&commitable_paths)) {
3234 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3238 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3242 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3243 struct got_commitable *ct = pe->data;
3244 err = check_ct_out_of_date(ct, repo, head_commit_id);
3249 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3253 if (commit_msg_cb != NULL) {
3254 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3259 if (logmsg == NULL || strlen(logmsg) == 0) {
3260 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3264 /* Create blobs from added and modified files and record their IDs. */
3265 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3266 struct got_commitable *ct = pe->data;
3269 if (ct->status != GOT_STATUS_ADD &&
3270 ct->status != GOT_STATUS_MODIFY)
3273 if (asprintf(&ondisk_path, "%s/%s",
3274 worktree->root_path, pe->path) == -1) {
3275 err = got_error_from_errno("asprintf");
3278 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3284 /* Recursively write new tree objects. */
3285 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3286 status_cb, status_arg, repo);
3290 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3293 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3294 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3295 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3296 got_object_qid_free(pid);
3302 /* Check if a concurrent commit to our branch has occurred. */
3303 head_ref_name = got_worktree_get_head_ref_name(worktree);
3304 if (head_ref_name == NULL) {
3305 err = got_error_from_errno("got_worktree_get_head_ref_name");
3308 /* Lock the reference here to prevent concurrent modification. */
3309 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3312 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3315 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3316 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3319 /* Update branch head in repository. */
3320 err = got_ref_change_ref(head_ref2, *new_commit_id);
3323 err = got_ref_write(head_ref2, repo);
3327 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3331 err = ref_base_commit(worktree, repo);
3335 err = update_fileindex_after_commit(&commitable_paths,
3336 *new_commit_id, worktree);
3340 unlockerr = lock_worktree(worktree, LOCK_SH);
3341 if (unlockerr && err == NULL)
3343 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3344 struct got_commitable *ct = pe->data;
3345 free_commitable(ct);
3347 got_pathlist_free(&commitable_paths);
3349 got_object_tree_close(head_tree);
3351 got_object_commit_close(head_commit);
3353 free(head_commit_id);
3354 free(head_commit_id2);
3356 got_ref_close(head_ref);
3358 unlockerr = got_ref_unlock(head_ref2);
3359 if (unlockerr && err == NULL)
3361 got_ref_close(head_ref2);
3367 got_commitable_get_path(struct got_commitable *ct)
3373 got_commitable_get_status(struct got_commitable *ct)