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"
41 #include "got_worktree.h"
42 #include "got_opentemp.h"
44 #include "got_lib_worktree.h"
45 #include "got_lib_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 static const struct got_error *
61 create_meta_file(const char *path_got, const char *name, const char *content)
63 const struct got_error *err = NULL;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
68 err = got_error_from_errno();
73 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
74 GOT_DEFAULT_FILE_MODE);
76 err = got_error_from_errno();
81 int len = dprintf(fd, "%s\n", content);
82 if (len != strlen(content) + 1) {
83 err = got_error_from_errno();
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_from_errno();
95 static const struct got_error *
96 update_meta_file(const char *path_got, const char *name, const char *content)
98 const struct got_error *err = NULL;
100 char *tmppath = NULL;
103 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
104 err = got_error_from_errno();
109 err = got_opentemp_named(&tmppath, &tmpfile, path);
114 int len = fprintf(tmpfile, "%s\n", content);
115 if (len != strlen(content) + 1) {
116 err = got_error_from_errno();
121 if (rename(tmppath, path) != 0) {
122 err = got_error_from_errno();
129 if (fclose(tmpfile) != 0 && err == NULL)
130 err = got_error_from_errno();
134 static const struct got_error *
135 read_meta_file(char **content, const char *path_got, const char *name)
137 const struct got_error *err = NULL;
145 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
146 err = got_error_from_errno();
151 fd = open(path, O_RDONLY | O_NOFOLLOW);
154 err = got_error(GOT_ERR_WORKTREE_META);
156 err = got_error_from_errno();
159 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
160 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
161 : got_error_from_errno());
165 if (lstat(path, &sb) != 0) {
166 err = got_error_from_errno();
169 *content = calloc(1, sb.st_size);
170 if (*content == NULL) {
171 err = got_error_from_errno();
175 n = read(fd, *content, sb.st_size);
176 if (n != sb.st_size) {
177 err = (n == -1 ? got_error_from_errno() :
178 got_error(GOT_ERR_WORKTREE_META));
181 if ((*content)[sb.st_size - 1] != '\n') {
182 err = got_error(GOT_ERR_WORKTREE_META);
185 (*content)[sb.st_size - 1] = '\0';
188 if (fd != -1 && close(fd) == -1 && err == NULL)
189 err = got_error_from_errno();
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;
209 char *formatstr = NULL;
210 char *absprefix = NULL;
211 char *basestr = NULL;
212 char *uuidstr = NULL;
214 if (strcmp(path, got_repo_get_path(repo)) == 0) {
215 err = got_error(GOT_ERR_WORKTREE_REPO);
219 err = got_ref_resolve(&commit_id, repo, head_ref);
222 err = got_object_get_type(&obj_type, repo, commit_id);
225 if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 return got_error(GOT_ERR_OBJ_TYPE);
228 if (!got_path_is_absolute(prefix)) {
229 if (asprintf(&absprefix, "/%s", prefix) == -1)
230 return got_error_from_errno();
233 /* Create top-level directory (may already exist). */
234 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 err = got_error_from_errno();
239 /* Create .got directory (may already exist). */
240 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 err = got_error_from_errno();
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_from_errno();
249 /* Create an empty lock file. */
250 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
254 /* Create an empty file index. */
255 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
259 /* Write the HEAD reference. */
260 refstr = got_ref_to_str(head_ref);
261 if (refstr == NULL) {
262 err = got_error_from_errno();
265 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status);
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status);
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno();
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
324 static const struct got_error *
325 open_worktree(struct got_worktree **worktree, const char *path)
327 const struct got_error *err = NULL;
329 char *formatstr = NULL;
330 char *uuidstr = NULL;
331 char *path_lock = NULL;
332 char *base_commit_id_str = NULL;
333 char *head_ref_str = NULL;
334 int version, fd = -1;
336 struct got_repository *repo = NULL;
337 uint32_t uuid_status;
341 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
342 err = got_error_from_errno();
347 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
348 err = got_error_from_errno();
353 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
355 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
356 : got_error_from_errno());
360 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
364 version = strtonum(formatstr, 1, INT_MAX, &errstr);
366 err = got_error(GOT_ERR_WORKTREE_META);
369 if (version != GOT_WORKTREE_FORMAT_VERSION) {
370 err = got_error(GOT_ERR_WORKTREE_VERS);
374 *worktree = calloc(1, sizeof(**worktree));
375 if (*worktree == NULL) {
376 err = got_error_from_errno();
379 (*worktree)->lockfd = -1;
381 (*worktree)->root_path = strdup(path);
382 if ((*worktree)->root_path == NULL) {
383 err = got_error_from_errno();
386 err = read_meta_file(&(*worktree)->repo_path, path_got,
387 GOT_WORKTREE_REPOSITORY);
391 err = read_meta_file(&(*worktree)->path_prefix, path_got,
392 GOT_WORKTREE_PATH_PREFIX);
396 err = read_meta_file(&base_commit_id_str, path_got,
397 GOT_WORKTREE_BASE_COMMIT);
401 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
404 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
405 if (uuid_status != uuid_s_ok) {
406 err = got_error_uuid(uuid_status);
410 err = got_repo_open(&repo, (*worktree)->repo_path);
414 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
419 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
423 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
426 got_repo_close(repo);
430 free(base_commit_id_str);
436 if (*worktree != NULL)
437 got_worktree_close(*worktree);
440 (*worktree)->lockfd = fd;
445 const struct got_error *
446 got_worktree_open(struct got_worktree **worktree, const char *path)
448 const struct got_error *err = NULL;
451 err = open_worktree(worktree, path);
452 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
456 path = dirname(path);
458 return got_error_from_errno();
459 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
461 return got_error(GOT_ERR_NOT_WORKTREE);
464 const struct got_error *
465 got_worktree_close(struct got_worktree *worktree)
467 const struct got_error *err = NULL;
468 free(worktree->root_path);
469 free(worktree->repo_path);
470 free(worktree->path_prefix);
471 free(worktree->base_commit_id);
472 if (worktree->head_ref)
473 got_ref_close(worktree->head_ref);
474 if (worktree->lockfd != -1)
475 if (close(worktree->lockfd) != 0)
476 err = got_error_from_errno();
482 got_worktree_get_root_path(struct got_worktree *worktree)
484 return worktree->root_path;
488 got_worktree_get_repo_path(struct got_worktree *worktree)
490 return worktree->repo_path;
494 got_worktree_get_path_prefix(struct got_worktree *worktree)
496 return worktree->path_prefix;
499 const struct got_error *
500 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
501 const char *path_prefix)
503 char *absprefix = NULL;
505 if (!got_path_is_absolute(path_prefix)) {
506 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
507 return got_error_from_errno();
509 *match = (strcmp(absprefix ? absprefix : path_prefix,
510 worktree->path_prefix) == 0);
516 got_worktree_get_head_ref_name(struct got_worktree *worktree)
518 return got_ref_get_name(worktree->head_ref);
521 struct got_reference *
522 got_worktree_get_head_ref(struct got_worktree *worktree)
524 return got_ref_dup(worktree->head_ref);
527 struct got_object_id *
528 got_worktree_get_base_commit_id(struct got_worktree *worktree)
530 return worktree->base_commit_id;
533 const struct got_error *
534 got_worktree_set_base_commit_id(struct got_worktree *worktree,
535 struct got_repository *repo, struct got_object_id *commit_id)
537 const struct got_error *err;
538 struct got_object *obj = NULL;
540 char *path_got = NULL;
542 if (asprintf(&path_got, "%s/%s", worktree->root_path,
543 GOT_WORKTREE_GOT_DIR) == -1) {
544 err = got_error_from_errno();
549 err = got_object_open(&obj, repo, commit_id);
553 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
554 err = got_error(GOT_ERR_OBJ_TYPE);
558 /* Record our base commit. */
559 err = got_object_id_str(&id_str, commit_id);
562 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
566 free(worktree->base_commit_id);
567 worktree->base_commit_id = got_object_id_dup(commit_id);
568 if (worktree->base_commit_id == NULL) {
569 err = got_error_from_errno();
574 got_object_close(obj);
580 static const struct got_error *
581 lock_worktree(struct got_worktree *worktree, int operation)
583 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
584 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
585 : got_error_from_errno());
589 static const struct got_error *
590 add_dir_on_disk(struct got_worktree *worktree, const char *path)
592 const struct got_error *err = NULL;
595 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
596 return got_error_from_errno();
598 err = got_path_mkdir(abspath);
599 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
602 if (lstat(abspath, &sb) == -1) {
603 err = got_error_from_errno();
604 } else if (!S_ISDIR(sb.st_mode)) {
605 /* TODO directory is obstructed; do something */
606 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
613 static const struct got_error *
614 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
616 const struct got_error *err = NULL;
619 size_t flen1 = 0, flen2 = 0;
624 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
625 if (flen1 == 0 && ferror(f1)) {
626 err = got_error_from_errno();
629 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
630 if (flen2 == 0 && ferror(f2)) {
631 err = got_error_from_errno();
638 } else if (flen2 == 0) {
642 } else if (flen1 == flen2) {
643 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
656 static const struct got_error *
657 check_files_equal(int *same, const char *f1_path, const char *f2_path)
659 const struct got_error *err = NULL;
662 FILE *f1 = NULL, *f2 = NULL;
666 if (lstat(f1_path, &sb) != 0) {
667 err = got_error_from_errno();
672 if (lstat(f2_path, &sb) != 0) {
673 err = got_error_from_errno();
678 if (size1 != size2) {
683 f1 = fopen(f1_path, "r");
685 return got_error_from_errno();
687 f2 = fopen(f2_path, "r");
689 err = got_error_from_errno();
693 err = check_file_contents_equal(same, f1, f2);
695 if (f1 && fclose(f1) != 0 && err == NULL)
696 err = got_error_from_errno();
697 if (f2 && fclose(f2) != 0 && err == NULL)
698 err = got_error_from_errno();
704 * Perform a 3-way merge where the file's version in the file index (blob2)
705 * acts as the common ancestor, the incoming blob (blob1) acts as the first
706 * derived version, and the file on disk acts as the second derived version.
708 static const struct got_error *
709 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
710 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
711 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
712 struct got_repository *repo,
713 got_worktree_checkout_cb progress_cb, void *progress_arg)
715 const struct got_error *err = NULL;
717 struct got_blob_object *blob2 = NULL;
718 FILE *f1 = NULL, *f2 = NULL;
719 char *blob1_path = NULL, *blob2_path = NULL;
720 char *merged_path = NULL, *base_path = NULL;
723 int overlapcnt = 0, update_timestamps = 0;
726 parent = dirname(ondisk_path);
728 return got_error_from_errno();
730 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
731 return got_error_from_errno();
733 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
738 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
739 err = got_error_from_errno();
744 err = got_opentemp_named(&blob1_path, &f1, base_path);
747 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
752 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
753 err = got_error_from_errno();
758 err = got_opentemp_named(&blob2_path, &f2, base_path);
761 if (got_fileindex_entry_has_blob(ie)) {
762 struct got_object_id id2;
763 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
764 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
767 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
772 * If the file has no blob, this is an "add vs add" conflict,
773 * and we simply use an empty ancestor file to make both files
774 * appear in the merged result in their entirety.
778 err = got_object_id_str(&id_str, worktree->base_commit_id);
781 if (asprintf(&label1, "commit %s", id_str) == -1) {
782 err = got_error_from_errno();
786 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
787 blob2_path, ondisk_path, label1, path);
791 (*progress_cb)(progress_arg,
792 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
795 if (fsync(merged_fd) != 0) {
796 err = got_error_from_errno();
800 /* Check if a clean merge has subsumed all local changes. */
801 if (overlapcnt == 0) {
802 err = check_files_equal(&update_timestamps, blob1_path,
808 if (chmod(merged_path, st_mode) != 0) {
809 err = got_error_from_errno();
813 if (rename(merged_path, ondisk_path) != 0) {
814 err = got_error_from_errno();
820 * Do not update timestamps of already modified files. Otherwise,
821 * a future status walk would treat them as unmodified files again.
823 err = got_fileindex_entry_update(ie, ondisk_path,
824 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
826 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
827 err = got_error_from_errno();
828 if (f1 && fclose(f1) != 0 && err == NULL)
829 err = got_error_from_errno();
830 if (f2 && fclose(f2) != 0 && err == NULL)
831 err = got_error_from_errno();
833 got_object_blob_close(blob2);
849 static const struct got_error *
850 update_blob_fileindex_entry(struct got_worktree *worktree,
851 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
852 const char *ondisk_path, const char *path, struct got_blob_object *blob,
853 int update_timestamps)
855 const struct got_error *err = NULL;
858 ie = got_fileindex_entry_get(fileindex, path);
860 err = got_fileindex_entry_update(ie, ondisk_path,
861 blob->id.sha1, worktree->base_commit_id->sha1,
864 struct got_fileindex_entry *new_ie;
865 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
866 path, blob->id.sha1, worktree->base_commit_id->sha1);
868 err = got_fileindex_entry_add(fileindex, new_ie);
873 static const struct got_error *
874 install_blob(struct got_worktree *worktree, const char *ondisk_path,
875 const char *path, uint16_t te_mode, uint16_t st_mode,
876 struct got_blob_object *blob, int restoring_missing_file,
877 int reverting_versioned_file, struct got_repository *repo,
878 got_worktree_checkout_cb progress_cb, void *progress_arg)
880 const struct got_error *err = NULL;
884 char *tmppath = NULL;
886 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
887 GOT_DEFAULT_FILE_MODE);
889 if (errno == ENOENT) {
890 char *parent = dirname(path);
892 return got_error_from_errno();
893 err = add_dir_on_disk(worktree, parent);
896 fd = open(ondisk_path,
897 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
898 GOT_DEFAULT_FILE_MODE);
900 return got_error_from_errno();
901 } else if (errno == EEXIST) {
902 if (!S_ISREG(st_mode)) {
903 /* TODO file is obstructed; do something */
904 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
907 err = got_opentemp_named_fd(&tmppath, &fd,
914 return got_error_from_errno();
917 if (restoring_missing_file)
918 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
919 else if (reverting_versioned_file)
920 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
922 (*progress_cb)(progress_arg,
923 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
925 hdrlen = got_object_blob_get_hdrlen(blob);
927 const uint8_t *buf = got_object_blob_get_read_buf(blob);
928 err = got_object_blob_read_block(&len, blob);
932 /* Skip blob object header first time around. */
933 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
935 err = got_error_from_errno();
937 } else if (outlen != len - hdrlen) {
938 err = got_error(GOT_ERR_IO);
945 if (fsync(fd) != 0) {
946 err = got_error_from_errno();
951 if (rename(tmppath, ondisk_path) != 0) {
952 err = got_error_from_errno();
958 if (te_mode & S_IXUSR) {
959 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
960 err = got_error_from_errno();
964 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
965 err = got_error_from_errno();
971 if (fd != -1 && close(fd) != 0 && err == NULL)
972 err = got_error_from_errno();
977 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
978 static const struct got_error *
979 get_modified_file_content_status(unsigned char *status, FILE *f)
981 const struct got_error *err = NULL;
982 const char *markers[3] = {
983 GOT_DIFF_CONFLICT_MARKER_BEGIN,
984 GOT_DIFF_CONFLICT_MARKER_SEP,
985 GOT_DIFF_CONFLICT_MARKER_END
990 const char delim[3] = {'\0', '\0', '\0'};
992 while (*status == GOT_STATUS_MODIFY) {
993 line = fparseln(f, &len, NULL, delim, 0);
997 err = got_ferror(f, GOT_ERR_IO);
1001 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1002 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
1003 *status = GOT_STATUS_CONFLICT;
1012 static const struct got_error *
1013 get_file_status(unsigned char *status, struct stat *sb,
1014 struct got_fileindex_entry *ie, const char *abspath,
1015 struct got_repository *repo)
1017 const struct got_error *err = NULL;
1018 struct got_object_id id;
1022 struct got_blob_object *blob = NULL;
1025 *status = GOT_STATUS_NO_CHANGE;
1027 if (lstat(abspath, sb) == -1) {
1028 if (errno == ENOENT) {
1030 if (got_fileindex_entry_has_file_on_disk(ie))
1031 *status = GOT_STATUS_MISSING;
1033 *status = GOT_STATUS_DELETE;
1035 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1036 & (S_IRWXU | S_IRWXG | S_IRWXO));
1038 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1041 return got_error_from_errno();
1044 if (!S_ISREG(sb->st_mode)) {
1045 *status = GOT_STATUS_OBSTRUCTED;
1052 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1053 *status = GOT_STATUS_DELETE;
1055 } else if (!got_fileindex_entry_has_blob(ie)) {
1056 *status = GOT_STATUS_ADD;
1060 if (ie->ctime_sec == sb->st_ctime &&
1061 ie->ctime_nsec == sb->st_ctimensec &&
1062 ie->mtime_sec == sb->st_mtime &&
1063 ie->mtime_sec == sb->st_mtime &&
1064 ie->mtime_nsec == sb->st_mtimensec &&
1065 ie->size == (sb->st_size & 0xffffffff))
1068 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1069 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1073 f = fopen(abspath, "r");
1075 err = got_error_from_errno();
1078 hdrlen = got_object_blob_get_hdrlen(blob);
1080 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1081 err = got_object_blob_read_block(&blen, blob);
1084 /* Skip length of blob object header first time around. */
1085 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1086 if (flen == 0 && ferror(f)) {
1087 err = got_error_from_errno();
1092 *status = GOT_STATUS_MODIFY;
1094 } else if (flen == 0) {
1096 *status = GOT_STATUS_MODIFY;
1098 } else if (blen - hdrlen == flen) {
1099 /* Skip blob object header first time around. */
1100 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1101 *status = GOT_STATUS_MODIFY;
1105 *status = GOT_STATUS_MODIFY;
1111 if (*status == GOT_STATUS_MODIFY) {
1113 err = get_modified_file_content_status(status, f);
1117 got_object_blob_close(blob);
1123 static const struct got_error *
1124 update_blob(struct got_worktree *worktree,
1125 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1126 struct got_tree_entry *te, const char *path,
1127 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1130 const struct got_error *err = NULL;
1131 struct got_blob_object *blob = NULL;
1133 unsigned char status = GOT_STATUS_NO_CHANGE;
1136 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1137 return got_error_from_errno();
1139 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1143 if (status == GOT_STATUS_OBSTRUCTED) {
1144 (*progress_cb)(progress_arg, status, path);
1148 if (ie && status != GOT_STATUS_MISSING) {
1149 if (got_fileindex_entry_has_commit(ie) &&
1150 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1151 SHA1_DIGEST_LENGTH) == 0) {
1152 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1156 if (got_fileindex_entry_has_blob(ie) &&
1157 memcmp(ie->blob_sha1, te->id->sha1,
1158 SHA1_DIGEST_LENGTH) == 0)
1162 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1166 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1167 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1168 te->mode, sb.st_mode, blob, repo, progress_cb,
1170 else if (status == GOT_STATUS_DELETE) {
1171 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1172 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1173 ondisk_path, path, blob, 0);
1177 err = install_blob(worktree, ondisk_path, path, te->mode,
1178 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1179 repo, progress_cb, progress_arg);
1182 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1183 ondisk_path, path, blob, 1);
1187 got_object_blob_close(blob);
1193 static const struct got_error *
1194 remove_ondisk_file(const char *root_path, const char *path)
1196 const struct got_error *err = NULL;
1197 char *ondisk_path = NULL;
1199 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1200 return got_error_from_errno();
1202 if (unlink(ondisk_path) == -1) {
1203 if (errno != ENOENT)
1204 err = got_error_from_errno();
1206 char *parent = dirname(ondisk_path);
1207 while (parent && strcmp(parent, root_path) != 0) {
1208 if (rmdir(parent) == -1) {
1209 if (errno != ENOTEMPTY)
1210 err = got_error_from_errno();
1213 parent = dirname(parent);
1220 static const struct got_error *
1221 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1222 struct got_fileindex_entry *ie, const char *parent_path,
1223 struct got_repository *repo,
1224 got_worktree_checkout_cb progress_cb, void *progress_arg)
1226 const struct got_error *err = NULL;
1227 unsigned char status;
1231 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1233 return got_error_from_errno();
1235 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1239 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1240 status == GOT_STATUS_ADD) {
1241 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1243 * Preserve the working file and change the deleted blob's
1244 * entry into a schedule-add entry.
1246 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1251 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1252 if (status == GOT_STATUS_NO_CHANGE) {
1253 err = remove_ondisk_file(worktree->root_path, ie->path);
1257 got_fileindex_entry_remove(fileindex, ie);
1263 struct diff_cb_arg {
1264 struct got_fileindex *fileindex;
1265 struct got_worktree *worktree;
1266 struct got_repository *repo;
1267 got_worktree_checkout_cb progress_cb;
1269 got_worktree_cancel_cb cancel_cb;
1273 static const struct got_error *
1274 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1275 struct got_tree_entry *te, const char *parent_path)
1277 struct diff_cb_arg *a = arg;
1279 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1280 return got_error(GOT_ERR_CANCELLED);
1282 return update_blob(a->worktree, a->fileindex, ie, te,
1283 ie->path, a->repo, a->progress_cb, a->progress_arg);
1286 static const struct got_error *
1287 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1289 struct diff_cb_arg *a = arg;
1291 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1292 return got_error(GOT_ERR_CANCELLED);
1294 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1295 a->repo, a->progress_cb, a->progress_arg);
1298 static const struct got_error *
1299 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1301 struct diff_cb_arg *a = arg;
1302 const struct got_error *err;
1305 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1306 return got_error(GOT_ERR_CANCELLED);
1308 if (asprintf(&path, "%s%s%s", parent_path,
1309 parent_path[0] ? "/" : "", te->name)
1311 return got_error_from_errno();
1313 if (S_ISDIR(te->mode))
1314 err = add_dir_on_disk(a->worktree, path);
1316 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1317 a->repo, a->progress_cb, a->progress_arg);
1323 const struct got_error *
1324 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1326 const struct got_error *err = NULL;
1327 char *uuidstr = NULL;
1328 uint32_t uuid_status;
1332 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1333 if (uuid_status != uuid_s_ok)
1334 return got_error_uuid(uuid_status);
1336 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1338 err = got_error_from_errno();
1346 * Prevent Git's garbage collector from deleting our base commit by
1347 * setting a reference to our base commit's ID.
1349 static const struct got_error *
1350 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1352 const struct got_error *err = NULL;
1353 struct got_reference *ref = NULL;
1356 err = got_worktree_get_base_ref_name(&refname, worktree);
1360 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1364 err = got_ref_write(ref, repo);
1372 static const struct got_error *
1373 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1374 struct got_worktree *worktree)
1376 const struct got_error *err = NULL;
1379 *fileindex_path = NULL;
1380 *fileindex = got_fileindex_alloc();
1381 if (*fileindex == NULL)
1382 return got_error_from_errno();
1384 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1385 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1386 err = got_error_from_errno();
1387 *fileindex_path = NULL;
1391 index = fopen(*fileindex_path, "rb");
1392 if (index == NULL) {
1393 if (errno != ENOENT)
1394 err = got_error_from_errno();
1396 err = got_fileindex_read(*fileindex, index);
1397 if (fclose(index) != 0 && err == NULL)
1398 err = got_error_from_errno();
1402 free(*fileindex_path);
1403 *fileindex_path = NULL;
1410 const struct got_error *
1411 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1412 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1413 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1415 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1416 struct got_commit_object *commit = NULL;
1417 struct got_object_id *tree_id = NULL;
1418 struct got_tree_object *tree = NULL;
1419 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1420 struct got_fileindex *fileindex = NULL;
1421 FILE *new_index = NULL;
1422 struct got_fileindex_diff_tree_cb diff_cb;
1423 struct diff_cb_arg arg;
1424 char *relpath = NULL, *entry_name = NULL;
1426 err = lock_worktree(worktree, LOCK_EX);
1431 * Read the file index.
1432 * Checking out files is supposed to be an idempotent operation.
1433 * If the on-disk file index is incomplete we will try to complete it.
1435 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1439 err = got_opentemp_named(&new_fileindex_path, &new_index,
1444 err = ref_base_commit(worktree, repo);
1448 err = got_object_open_as_commit(&commit, repo,
1449 worktree->base_commit_id);
1456 relpath = strdup(path);
1457 if (relpath == NULL) {
1458 err = got_error_from_errno();
1461 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1462 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1464 err = got_error_from_errno();
1467 err = got_object_id_by_path(&tree_id, repo,
1468 worktree->base_commit_id, tree_path);
1472 err = got_object_get_type(&obj_type, repo, tree_id);
1475 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1476 /* Split provided path into parent dir + entry name. */
1477 if (strchr(path, '/') == NULL) {
1478 relpath = strdup("");
1479 if (relpath == NULL) {
1480 err = got_error_from_errno();
1483 tree_path = strdup(worktree->path_prefix);
1484 if (tree_path == NULL) {
1485 err = got_error_from_errno();
1489 err = got_path_dirname(&relpath, path);
1492 if (asprintf(&tree_path, "%s%s%s",
1493 worktree->path_prefix,
1494 got_path_is_root_dir(
1495 worktree->path_prefix) ? "" : "/",
1497 err = got_error_from_errno();
1501 err = got_object_id_by_path(&tree_id, repo,
1502 worktree->base_commit_id, tree_path);
1506 entry_name = basename(path);
1507 if (entry_name == NULL) {
1508 err = got_error_from_errno();
1513 relpath = strdup("");
1514 if (relpath == NULL) {
1515 err = got_error_from_errno();
1518 err = got_object_id_by_path(&tree_id, repo,
1519 worktree->base_commit_id, worktree->path_prefix);
1524 err = got_object_open_as_tree(&tree, repo, tree_id);
1529 got_object_tree_find_entry(tree, entry_name) == NULL) {
1530 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1534 diff_cb.diff_old_new = diff_old_new;
1535 diff_cb.diff_old = diff_old;
1536 diff_cb.diff_new = diff_new;
1537 arg.fileindex = fileindex;
1538 arg.worktree = worktree;
1540 arg.progress_cb = progress_cb;
1541 arg.progress_arg = progress_arg;
1542 arg.cancel_cb = cancel_cb;
1543 arg.cancel_arg = cancel_arg;
1544 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1545 entry_name, repo, &diff_cb, &arg);
1547 /* Try to sync the fileindex back to disk in any case. */
1548 err = got_fileindex_write(fileindex, new_index);
1552 if (rename(new_fileindex_path, fileindex_path) != 0) {
1553 err = got_error_from_errno();
1554 unlink(new_fileindex_path);
1558 free(new_fileindex_path);
1559 new_fileindex_path = NULL;
1564 got_object_tree_close(tree);
1566 got_object_commit_close(commit);
1567 if (new_fileindex_path)
1568 unlink(new_fileindex_path);
1571 free(new_fileindex_path);
1572 free(fileindex_path);
1573 got_fileindex_free(fileindex);
1576 unlockerr = lock_worktree(worktree, LOCK_SH);
1577 if (unlockerr && err == NULL)
1582 struct diff_dir_cb_arg {
1583 struct got_fileindex *fileindex;
1584 struct got_worktree *worktree;
1585 const char *status_path;
1586 size_t status_path_len;
1587 struct got_repository *repo;
1588 got_worktree_status_cb status_cb;
1590 got_worktree_cancel_cb cancel_cb;
1594 static const struct got_error *
1595 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1596 got_worktree_status_cb status_cb, void *status_arg,
1597 struct got_repository *repo)
1599 const struct got_error *err = NULL;
1600 unsigned char status = GOT_STATUS_NO_CHANGE;
1602 struct got_object_id id;
1604 err = get_file_status(&status, &sb, ie, abspath, repo);
1605 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1606 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1607 err = (*status_cb)(status_arg, status, ie->path, &id);
1612 static const struct got_error *
1613 status_old_new(void *arg, struct got_fileindex_entry *ie,
1614 struct dirent *de, const char *parent_path)
1616 const struct got_error *err = NULL;
1617 struct diff_dir_cb_arg *a = arg;
1620 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1621 return got_error(GOT_ERR_CANCELLED);
1623 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1624 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1627 if (parent_path[0]) {
1628 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1629 parent_path, de->d_name) == -1)
1630 return got_error_from_errno();
1632 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1634 return got_error_from_errno();
1637 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1643 static const struct got_error *
1644 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1646 struct diff_dir_cb_arg *a = arg;
1647 struct got_object_id id;
1648 unsigned char status;
1650 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1651 return got_error(GOT_ERR_CANCELLED);
1653 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1656 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1657 if (got_fileindex_entry_has_file_on_disk(ie))
1658 status = GOT_STATUS_MISSING;
1660 status = GOT_STATUS_DELETE;
1661 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1664 static const struct got_error *
1665 status_new(void *arg, struct dirent *de, const char *parent_path)
1667 const struct got_error *err = NULL;
1668 struct diff_dir_cb_arg *a = arg;
1671 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1672 return got_error(GOT_ERR_CANCELLED);
1674 if (de->d_type == DT_DIR)
1677 /* XXX ignore symlinks for now */
1678 if (de->d_type == DT_LNK)
1681 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1684 if (parent_path[0]) {
1685 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1686 return got_error_from_errno();
1691 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1698 const struct got_error *
1699 got_worktree_status(struct got_worktree *worktree, const char *path,
1700 struct got_repository *repo, got_worktree_status_cb status_cb,
1701 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1703 const struct got_error *err = NULL;
1704 DIR *workdir = NULL;
1705 char *fileindex_path = NULL;
1706 struct got_fileindex *fileindex = NULL;
1708 struct got_fileindex_diff_dir_cb fdiff_cb;
1709 struct diff_dir_cb_arg arg;
1710 char *ondisk_path = NULL;
1712 fileindex = got_fileindex_alloc();
1713 if (fileindex == NULL) {
1714 err = got_error_from_errno();
1718 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1719 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1720 err = got_error_from_errno();
1721 fileindex_path = NULL;
1725 index = fopen(fileindex_path, "rb");
1726 if (index == NULL) {
1727 if (errno != ENOENT) {
1728 err = got_error_from_errno();
1732 err = got_fileindex_read(fileindex, index);
1738 if (asprintf(&ondisk_path, "%s%s%s",
1739 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1740 err = got_error_from_errno();
1743 workdir = opendir(ondisk_path);
1744 if (workdir == NULL) {
1745 if (errno == ENOTDIR || errno == ENOENT) {
1746 struct got_fileindex_entry *ie;
1747 ie = got_fileindex_entry_get(fileindex, path);
1749 err = got_error(GOT_ERR_BAD_PATH);
1752 err = report_file_status(ie, ondisk_path,
1753 status_cb, status_arg, repo);
1756 err = got_error_from_errno();
1760 fdiff_cb.diff_old_new = status_old_new;
1761 fdiff_cb.diff_old = status_old;
1762 fdiff_cb.diff_new = status_new;
1763 arg.fileindex = fileindex;
1764 arg.worktree = worktree;
1765 arg.status_path = path;
1766 arg.status_path_len = strlen(path);
1768 arg.status_cb = status_cb;
1769 arg.status_arg = status_arg;
1770 arg.cancel_cb = cancel_cb;
1771 arg.cancel_arg = cancel_arg;
1772 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1773 path, repo, &fdiff_cb, &arg);
1778 free(fileindex_path);
1779 got_fileindex_free(fileindex);
1783 const struct got_error *
1784 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1787 const struct got_error *err = NULL;
1788 char *resolved, *path = NULL;
1793 resolved = realpath(arg, NULL);
1794 if (resolved == NULL)
1795 return got_error_from_errno();
1797 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1798 strlen(got_worktree_get_root_path(worktree)))) {
1799 err = got_error(GOT_ERR_BAD_PATH);
1803 path = strdup(resolved +
1804 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1806 err = got_error_from_errno();
1810 /* XXX status walk can't deal with trailing slash! */
1812 while (path[len - 1] == '/') {
1813 path[len - 1] = '\0';
1825 const struct got_error *
1826 got_worktree_schedule_add(struct got_worktree *worktree,
1827 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1828 struct got_repository *repo)
1830 struct got_fileindex *fileindex = NULL;
1831 struct got_fileindex_entry *ie = NULL;
1832 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1833 FILE *index = NULL, *new_index = NULL;
1834 const struct got_error *err = NULL, *unlockerr = NULL;
1837 err = lock_worktree(worktree, LOCK_EX);
1841 err = got_path_skip_common_ancestor(&relpath,
1842 got_worktree_get_root_path(worktree), ondisk_path);
1846 fileindex = got_fileindex_alloc();
1847 if (fileindex == NULL) {
1848 err = got_error_from_errno();
1852 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1853 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1854 err = got_error_from_errno();
1855 fileindex_path = NULL;
1859 index = fopen(fileindex_path, "rb");
1860 if (index == NULL) {
1861 err = got_error_from_errno();
1865 err = got_fileindex_read(fileindex, index);
1869 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1870 err = got_error_set_errno(EEXIST);
1874 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1878 err = got_fileindex_entry_add(fileindex, ie);
1881 ie_added = 1; /* now owned by fileindex; don't free separately */
1883 err = got_opentemp_named(&new_fileindex_path, &new_index,
1888 err = got_fileindex_write(fileindex, new_index);
1892 if (rename(new_fileindex_path, fileindex_path) != 0) {
1893 err = got_error_from_errno();
1897 free(new_fileindex_path);
1898 new_fileindex_path = NULL;
1900 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1903 if (fclose(index) != 0 && err == NULL)
1904 err = got_error_from_errno();
1906 if (new_fileindex_path) {
1907 if (unlink(new_fileindex_path) != 0 && err == NULL)
1908 err = got_error_from_errno();
1909 free(new_fileindex_path);
1911 if (ie && !ie_added)
1912 got_fileindex_entry_free(ie);
1914 got_fileindex_free(fileindex);
1915 unlockerr = lock_worktree(worktree, LOCK_SH);
1916 if (unlockerr && err == NULL)
1922 const struct got_error *
1923 got_worktree_schedule_delete(struct got_worktree *worktree,
1924 const char *ondisk_path, int delete_local_mods,
1925 got_worktree_status_cb status_cb, void *status_arg,
1926 struct got_repository *repo)
1928 struct got_fileindex *fileindex = NULL;
1929 struct got_fileindex_entry *ie = NULL;
1930 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1931 FILE *index = NULL, *new_index = NULL;
1932 const struct got_error *err = NULL, *unlockerr = NULL;
1933 unsigned char status;
1936 err = lock_worktree(worktree, LOCK_EX);
1940 err = got_path_skip_common_ancestor(&relpath,
1941 got_worktree_get_root_path(worktree), ondisk_path);
1945 fileindex = got_fileindex_alloc();
1946 if (fileindex == NULL) {
1947 err = got_error_from_errno();
1951 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1952 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1953 err = got_error_from_errno();
1954 fileindex_path = NULL;
1958 index = fopen(fileindex_path, "rb");
1959 if (index == NULL) {
1960 err = got_error_from_errno();
1964 err = got_fileindex_read(fileindex, index);
1968 ie = got_fileindex_entry_get(fileindex, relpath);
1970 err = got_error(GOT_ERR_BAD_PATH);
1974 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1978 if (status != GOT_STATUS_NO_CHANGE) {
1979 if (status == GOT_STATUS_DELETE) {
1980 err = got_error_set_errno(ENOENT);
1983 if (status != GOT_STATUS_MODIFY) {
1984 err = got_error(GOT_ERR_FILE_STATUS);
1987 if (!delete_local_mods) {
1988 err = got_error(GOT_ERR_FILE_MODIFIED);
1993 if (unlink(ondisk_path) != 0) {
1994 err = got_error_from_errno();
1998 got_fileindex_entry_mark_deleted_from_disk(ie);
2000 err = got_opentemp_named(&new_fileindex_path, &new_index,
2005 err = got_fileindex_write(fileindex, new_index);
2009 if (rename(new_fileindex_path, fileindex_path) != 0) {
2010 err = got_error_from_errno();
2014 free(new_fileindex_path);
2015 new_fileindex_path = NULL;
2017 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2021 if (fclose(index) != 0 && err == NULL)
2022 err = got_error_from_errno();
2024 if (new_fileindex_path) {
2025 if (unlink(new_fileindex_path) != 0 && err == NULL)
2026 err = got_error_from_errno();
2027 free(new_fileindex_path);
2030 got_fileindex_free(fileindex);
2031 unlockerr = lock_worktree(worktree, LOCK_SH);
2032 if (unlockerr && err == NULL)
2037 const struct got_error *
2038 got_worktree_revert(struct got_worktree *worktree,
2039 const char *ondisk_path,
2040 got_worktree_checkout_cb progress_cb, void *progress_arg,
2041 struct got_repository *repo)
2043 struct got_fileindex *fileindex = NULL;
2044 struct got_fileindex_entry *ie = NULL;
2045 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2046 char *tree_path = NULL, *parent_path, *te_name;
2047 FILE *index = NULL, *new_index = NULL;
2048 const struct got_error *err = NULL, *unlockerr = NULL;
2049 struct got_tree_object *tree = NULL;
2050 struct got_object_id id, *tree_id = NULL;
2051 const struct got_tree_entry *te;
2052 struct got_blob_object *blob = NULL;
2053 unsigned char status;
2056 err = lock_worktree(worktree, LOCK_EX);
2060 err = got_path_skip_common_ancestor(&relpath,
2061 got_worktree_get_root_path(worktree), ondisk_path);
2065 fileindex = got_fileindex_alloc();
2066 if (fileindex == NULL) {
2067 err = got_error_from_errno();
2071 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2072 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2073 err = got_error_from_errno();
2074 fileindex_path = NULL;
2078 index = fopen(fileindex_path, "rb");
2079 if (index == NULL) {
2080 err = got_error_from_errno();
2084 err = got_fileindex_read(fileindex, index);
2088 ie = got_fileindex_entry_get(fileindex, relpath);
2090 err = got_error(GOT_ERR_BAD_PATH);
2094 /* Construct in-repository path of tree which contains this blob. */
2095 err = got_path_dirname(&parent_path, ie->path);
2097 if (err->code != GOT_ERR_BAD_PATH)
2101 if (got_path_is_root_dir(worktree->path_prefix)) {
2102 tree_path = strdup(parent_path);
2103 if (tree_path == NULL) {
2104 err = got_error_from_errno();
2108 if (got_path_is_root_dir(parent_path)) {
2109 tree_path = strdup(worktree->path_prefix);
2110 if (tree_path == NULL) {
2111 err = got_error_from_errno();
2115 if (asprintf(&tree_path, "%s/%s",
2116 worktree->path_prefix, parent_path) == -1) {
2117 err = got_error_from_errno();
2123 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2128 err = got_object_open_as_tree(&tree, repo, tree_id);
2132 te_name = basename(ie->path);
2133 if (te_name == NULL) {
2134 err = got_error_from_errno();
2138 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2142 te = got_object_tree_find_entry(tree, te_name);
2143 if (te == NULL && status != GOT_STATUS_ADD) {
2144 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2149 case GOT_STATUS_ADD:
2150 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2151 got_fileindex_entry_remove(fileindex, ie);
2153 case GOT_STATUS_DELETE:
2154 case GOT_STATUS_MODIFY:
2155 case GOT_STATUS_CONFLICT:
2156 case GOT_STATUS_MISSING:
2157 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2158 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2161 err = install_blob(worktree, ondisk_path, ie->path,
2162 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2166 if (status == GOT_STATUS_DELETE) {
2167 err = update_blob_fileindex_entry(worktree,
2168 fileindex, ie, ondisk_path, ie->path, blob, 1);
2177 err = got_opentemp_named(&new_fileindex_path, &new_index,
2182 err = got_fileindex_write(fileindex, new_index);
2186 if (rename(new_fileindex_path, fileindex_path) != 0) {
2187 err = got_error_from_errno();
2191 free(new_fileindex_path);
2192 new_fileindex_path = NULL;
2197 got_object_blob_close(blob);
2199 got_object_tree_close(tree);
2202 if (fclose(index) != 0 && err == NULL)
2203 err = got_error_from_errno();
2205 if (new_fileindex_path) {
2206 if (unlink(new_fileindex_path) != 0 && err == NULL)
2207 err = got_error_from_errno();
2208 free(new_fileindex_path);
2211 got_fileindex_free(fileindex);
2212 unlockerr = lock_worktree(worktree, LOCK_SH);
2213 if (unlockerr && err == NULL)
2222 unsigned char status;
2223 struct got_object_id *id;
2224 struct got_object_id *base_id;
2229 free_commitable(struct commitable *ct)
2232 free(ct->in_repo_path);
2233 free(ct->ondisk_path);
2239 struct collect_commitables_arg {
2240 struct got_pathlist_head *commitable_paths;
2241 struct got_repository *repo;
2242 struct got_worktree *worktree;
2245 static const struct got_error *
2246 collect_commitables(void *arg, unsigned char status, const char *relpath,
2247 struct got_object_id *id)
2249 struct collect_commitables_arg *a = arg;
2250 const struct got_error *err = NULL;
2251 struct commitable *ct = NULL;
2252 struct got_pathlist_entry *new = NULL;
2253 char *parent_path = NULL, *path = NULL;
2256 if (status == GOT_STATUS_CONFLICT)
2257 return got_error(GOT_ERR_COMMIT_CONFLICT);
2259 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2260 status != GOT_STATUS_DELETE)
2263 if (asprintf(&path, "/%s", relpath) == -1) {
2264 err = got_error_from_errno();
2267 if (strcmp(path, "/") == 0) {
2268 parent_path = strdup("");
2269 if (parent_path == NULL)
2270 return got_error_from_errno();
2272 err = got_path_dirname(&parent_path, path);
2277 ct = calloc(1, sizeof(*ct));
2279 err = got_error_from_errno();
2283 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2285 err = got_error_from_errno();
2288 if (status == GOT_STATUS_DELETE) {
2289 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2291 if (lstat(ct->ondisk_path, &sb) != 0) {
2292 err = got_error_from_errno();
2295 ct->mode = sb.st_mode;
2298 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2299 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2301 err = got_error_from_errno();
2305 ct->status = status;
2306 ct->id = NULL; /* will be filled in when blob gets created */
2307 if (ct->status != GOT_STATUS_ADD) {
2308 ct->base_id = got_object_id_dup(id);
2309 if (ct->base_id == NULL) {
2310 err = got_error_from_errno();
2314 ct->path = strdup(path);
2315 if (ct->path == NULL) {
2316 err = got_error_from_errno();
2319 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2321 if (ct && (err || new == NULL))
2322 free_commitable(ct);
2328 static const struct got_error *write_tree(struct got_object_id **,
2329 struct got_tree_object *, const char *, struct got_pathlist_head *,
2330 got_worktree_status_cb status_cb, void *status_arg,
2331 struct got_repository *);
2333 static const struct got_error *
2334 write_subtree(struct got_object_id **new_subtree_id,
2335 struct got_tree_entry *te, const char *parent_path,
2336 struct got_pathlist_head *commitable_paths,
2337 got_worktree_status_cb status_cb, void *status_arg,
2338 struct got_repository *repo)
2340 const struct got_error *err = NULL;
2341 struct got_tree_object *subtree;
2344 if (asprintf(&subpath, "%s%s%s", parent_path,
2345 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2346 return got_error_from_errno();
2348 err = got_object_open_as_tree(&subtree, repo, te->id);
2352 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2353 status_cb, status_arg, repo);
2354 got_object_tree_close(subtree);
2359 static const struct got_error *
2360 match_ct_parent_path(int *match, struct commitable *ct, const char *path)
2362 const struct got_error *err = NULL;
2363 char *ct_parent_path = NULL;
2367 if (strchr(ct->path, '/') == NULL) {
2368 ct_parent_path = strdup("/");
2369 if (ct_parent_path == NULL)
2370 return got_error_from_errno();
2372 err = got_path_dirname(&ct_parent_path, ct->path);
2377 *match = (strcmp(path, ct_parent_path) == 0);
2378 free(ct_parent_path);
2383 get_ct_file_mode(struct commitable *ct)
2385 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2388 static const struct got_error *
2389 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2390 struct got_tree_entry *te, struct commitable *ct)
2392 const struct got_error *err = NULL;
2396 err = got_object_tree_entry_dup(new_te, te);
2400 (*new_te)->mode = get_ct_file_mode(ct);
2402 free((*new_te)->id);
2403 (*new_te)->id = got_object_id_dup(ct->id);
2404 if ((*new_te)->id == NULL) {
2405 err = got_error_from_errno();
2409 if (err && *new_te) {
2410 got_object_tree_entry_close(*new_te);
2416 static const struct got_error *
2417 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2418 struct commitable *ct)
2420 const struct got_error *err = NULL;
2425 *new_te = calloc(1, sizeof(*new_te));
2426 if (*new_te == NULL)
2427 return got_error_from_errno();
2429 ct_name = basename(ct->path);
2430 if (ct_name == NULL) {
2431 err = got_error_from_errno();
2434 (*new_te)->name = strdup(ct_name);
2435 if ((*new_te)->name == NULL) {
2436 err = got_error_from_errno();
2440 (*new_te)->mode = get_ct_file_mode(ct);
2442 (*new_te)->id = got_object_id_dup(ct->id);
2443 if ((*new_te)->id == NULL) {
2444 err = got_error_from_errno();
2448 if (err && *new_te) {
2449 got_object_tree_entry_close(*new_te);
2455 static const struct got_error *
2456 insert_tree_entry(struct got_tree_entry *new_te,
2457 struct got_pathlist_head *paths)
2459 const struct got_error *err = NULL;
2460 struct got_pathlist_entry *new_pe;
2462 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2466 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2470 static const struct got_error *
2471 report_ct_status(struct commitable *ct,
2472 got_worktree_status_cb status_cb, void *status_arg)
2474 const char *ct_path = ct->path;
2475 while (ct_path[0] == '/')
2477 return (*status_cb)(status_arg, ct->status, ct_path, ct->id);
2480 static const struct got_error *
2481 match_modified_subtree(int *modified, struct got_tree_entry *te,
2482 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2484 const struct got_error *err = NULL;
2485 struct got_pathlist_entry *pe;
2490 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2491 got_path_is_root_dir(base_tree_path) ? "" : "/",
2493 return got_error_from_errno();
2495 TAILQ_FOREACH(pe, commitable_paths, entry) {
2496 struct commitable *ct = pe->data;
2499 if (asprintf(&ct_path, "%s%s%s", base_tree_path,
2500 got_path_is_root_dir(base_tree_path) ? "" : "/",
2502 err = got_error_from_errno();
2506 *modified = got_path_is_child(ct->in_repo_path, te_path,
2516 static const struct got_error *
2517 match_deleted_or_modified_ct(struct commitable **ctp,
2518 struct got_tree_entry *te, const char *base_tree_path,
2519 struct got_pathlist_head *commitable_paths)
2521 const struct got_error *err = NULL;
2522 struct got_pathlist_entry *pe;
2526 TAILQ_FOREACH(pe, commitable_paths, entry) {
2527 struct commitable *ct = pe->data;
2528 char *ct_name = NULL;
2531 if (ct->status != GOT_STATUS_MODIFY &&
2532 ct->status != GOT_STATUS_DELETE)
2535 if (got_object_id_cmp(ct->base_id, te->id) != 0)
2538 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2544 ct_name = basename(pe->path);
2545 if (ct_name == NULL)
2546 return got_error_from_errno();
2548 if (strcmp(te->name, ct_name) != 0)
2558 static const struct got_error *
2559 write_tree(struct got_object_id **new_tree_id,
2560 struct got_tree_object *base_tree, const char *path_base_tree,
2561 struct got_pathlist_head *commitable_paths,
2562 got_worktree_status_cb status_cb, void *status_arg,
2563 struct got_repository *repo)
2565 const struct got_error *err = NULL;
2566 const struct got_tree_entries *base_entries = NULL;
2567 struct got_pathlist_head paths;
2568 struct got_tree_entries new_tree_entries;
2569 struct got_tree_entry *te, *new_te = NULL;
2570 struct got_pathlist_entry *pe;
2573 new_tree_entries.nentries = 0;
2574 SIMPLEQ_INIT(&new_tree_entries.head);
2576 /* Insert, and recurse into, newly added entries first. */
2577 TAILQ_FOREACH(pe, commitable_paths, entry) {
2578 struct commitable *ct = pe->data;
2579 char *child_path = NULL, *slash;
2581 if (ct->status != GOT_STATUS_ADD)
2584 if (!got_path_is_child(pe->path, path_base_tree,
2585 strlen(path_base_tree)))
2588 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2593 slash = strchr(child_path, '/');
2594 if (slash == NULL) {
2595 err = alloc_added_blob_tree_entry(&new_te, ct);
2598 err = report_ct_status(ct, status_cb, status_arg);
2603 struct got_pathlist_entry *pe2;
2606 *slash = '\0'; /* trim trailing path components */
2608 new_te = calloc(1, sizeof(*new_te));
2609 new_te->mode = S_IFDIR;
2610 new_te->name = strdup(child_path);
2611 if (new_te->name == NULL) {
2612 got_object_tree_entry_close(new_te);
2613 err = got_error_from_errno();
2616 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2617 got_path_is_root_dir(path_base_tree) ? "" : "/",
2618 child_path) == -1) {
2619 err = got_error_from_errno();
2622 TAILQ_FOREACH(pe2, &paths, entry) {
2623 if (got_path_cmp(subtree_path, pe2->path) != 0)
2631 err = write_tree(&new_te->id, NULL, subtree_path,
2632 commitable_paths, status_cb, status_arg, repo);
2637 err = insert_tree_entry(new_te, &paths);
2643 /* Handle modified and deleted entries. */
2644 base_entries = got_object_tree_get_entries(base_tree);
2645 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2646 struct commitable *ct = NULL;
2648 if (S_ISDIR(te->mode)) {
2650 err = got_object_tree_entry_dup(&new_te, te);
2653 err = match_modified_subtree(&modified, te,
2654 path_base_tree, commitable_paths);
2657 /* Avoid recursion into unmodified subtrees. */
2660 err = write_subtree(&new_te->id, te,
2661 path_base_tree, commitable_paths,
2662 status_cb, status_arg, repo);
2666 err = insert_tree_entry(new_te, &paths);
2672 err = match_deleted_or_modified_ct(&ct, te,
2673 path_base_tree, commitable_paths);
2675 /* NB: Deleted entries get dropped here. */
2676 if (ct->status == GOT_STATUS_MODIFY) {
2677 err = alloc_modified_blob_tree_entry(
2681 err = insert_tree_entry(new_te, &paths);
2685 err = report_ct_status(ct, status_cb, status_arg);
2689 /* Entry is unchanged; just copy it. */
2690 err = got_object_tree_entry_dup(&new_te, te);
2693 err = insert_tree_entry(new_te, &paths);
2700 /* Write new list of entries; deleted entries have been dropped. */
2701 TAILQ_FOREACH(pe, &paths, entry) {
2702 struct got_tree_entry *te = pe->data;
2703 new_tree_entries.nentries++;
2704 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2706 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2708 got_object_tree_entries_close(&new_tree_entries);
2709 got_pathlist_free(&paths);
2713 static const struct got_error *
2714 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2715 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2717 const struct got_error *err = NULL;
2718 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2719 struct got_fileindex *fileindex = NULL;
2720 FILE *new_index = NULL;
2721 struct got_pathlist_entry *pe;
2723 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2727 err = got_opentemp_named(&new_fileindex_path, &new_index,
2732 TAILQ_FOREACH(pe, commitable_paths, entry) {
2733 struct got_fileindex_entry *ie;
2734 struct commitable *ct = pe->data;
2736 ie = got_fileindex_entry_get(fileindex, pe->path);
2738 if (ct->status == GOT_STATUS_DELETE) {
2739 got_fileindex_entry_remove(fileindex, ie);
2740 got_fileindex_entry_free(ie);
2742 err = got_fileindex_entry_update(ie,
2743 ct->ondisk_path, ct->id->sha1,
2744 new_base_commit_id->sha1, 1);
2746 err = got_fileindex_entry_alloc(&ie,
2747 ct->ondisk_path, pe->path, ct->id->sha1,
2748 new_base_commit_id->sha1);
2751 err = got_fileindex_entry_add(fileindex, ie);
2757 err = got_fileindex_write(fileindex, new_index);
2761 if (rename(new_fileindex_path, fileindex_path) != 0) {
2762 err = got_error_from_errno();
2763 unlink(new_fileindex_path);
2767 free(new_fileindex_path);
2768 new_fileindex_path = NULL;
2771 if (new_fileindex_path)
2772 unlink(new_fileindex_path);
2775 free(new_fileindex_path);
2776 free(fileindex_path);
2777 got_fileindex_free(fileindex);
2781 const struct got_error *
2782 got_worktree_commit(struct got_object_id **new_commit_id,
2783 struct got_worktree *worktree, const char *ondisk_path,
2784 const char *author, const char *committer, const char *logmsg,
2785 got_worktree_status_cb status_cb, void *status_arg,
2786 struct got_repository *repo)
2788 const struct got_error *err = NULL, *unlockerr = NULL;
2789 struct collect_commitables_arg cc_arg;
2790 struct got_pathlist_head commitable_paths;
2791 struct got_pathlist_entry *pe;
2792 char *relpath = NULL;
2793 const char *head_ref_name = NULL;
2794 struct got_commit_object *base_commit = NULL;
2795 struct got_object_id *head_commit_id = NULL;
2796 struct got_reference *head_ref2 = NULL;
2797 struct got_object_id *head_commit_id2 = NULL;
2798 struct got_tree_object *base_tree = NULL;
2799 struct got_object_id *new_tree_id = NULL;
2800 struct got_object_id_queue parent_ids;
2801 struct got_object_qid *pid = NULL;
2803 *new_commit_id = NULL;
2805 TAILQ_INIT(&commitable_paths);
2806 SIMPLEQ_INIT(&parent_ids);
2809 err = got_path_skip_common_ancestor(&relpath,
2810 worktree->root_path, ondisk_path);
2815 err = lock_worktree(worktree, LOCK_EX);
2819 /* XXX should re-read head ref here now that work tree is locked */
2820 err = got_ref_resolve(&head_commit_id, repo, worktree->head_ref);
2824 cc_arg.commitable_paths = &commitable_paths;
2825 cc_arg.worktree = worktree;
2827 err = got_worktree_status(worktree, relpath ? relpath : "",
2828 repo, collect_commitables, &cc_arg, NULL, NULL);
2832 /* TODO: out-of-dateness check */
2834 /* TODO: collect commit message if not specified */
2836 /* Create blobs from added and modified files and record their IDs. */
2837 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2838 struct commitable *ct = pe->data;
2841 if (ct->status != GOT_STATUS_ADD &&
2842 ct->status != GOT_STATUS_MODIFY)
2845 if (asprintf(&ondisk_path, "%s/%s",
2846 worktree->root_path, pe->path) == -1) {
2847 err = got_error_from_errno();
2850 err = got_object_blob_create(&ct->id, ondisk_path, repo);
2856 err = got_object_open_as_commit(&base_commit, repo,
2857 worktree->base_commit_id);
2860 err = got_object_open_as_tree(&base_tree, repo, base_commit->tree_id);
2864 /* Recursively write new tree objects. */
2865 err = write_tree(&new_tree_id, base_tree, "/", &commitable_paths,
2866 status_cb, status_arg, repo);
2870 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2873 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2874 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2875 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2876 got_object_qid_free(pid);
2880 /* Check if a concurrent commit to our branch has occurred. */
2881 /* XXX ideally we'd lock the reference file here to avoid a race */
2882 head_ref_name = got_worktree_get_head_ref_name(worktree);
2883 if (head_ref_name == NULL) {
2884 err = got_error_from_errno();
2887 err = got_ref_open(&head_ref2, repo, head_ref_name);
2890 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
2893 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
2894 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
2897 /* Update branch head in repository. */
2898 err = got_ref_change_ref(worktree->head_ref, *new_commit_id);
2901 err = got_ref_write(worktree->head_ref, repo);
2904 /* XXX race has ended here */
2906 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
2910 err = ref_base_commit(worktree, repo);
2914 err = update_fileindex_after_commit(&commitable_paths,
2915 *new_commit_id, worktree);
2919 unlockerr = lock_worktree(worktree, LOCK_SH);
2920 if (unlockerr && err == NULL)
2922 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2923 struct commitable *ct = pe->data;
2924 free_commitable(ct);
2926 got_pathlist_free(&commitable_paths);
2928 got_object_tree_close(base_tree);
2930 got_object_commit_close(base_commit);
2932 free(head_commit_id);
2933 free(head_commit_id2);
2935 got_ref_close(head_ref2);