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 int version, fd = -1;
335 struct got_repository *repo = NULL;
336 uint32_t uuid_status;
340 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
341 err = got_error_from_errno();
346 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
347 err = got_error_from_errno();
352 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
354 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
355 : got_error_from_errno());
359 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
363 version = strtonum(formatstr, 1, INT_MAX, &errstr);
365 err = got_error(GOT_ERR_WORKTREE_META);
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno();
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = strdup(path);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno();
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status);
409 err = got_repo_open(&repo, (*worktree)->repo_path);
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
422 got_repo_close(repo);
425 free(base_commit_id_str);
431 if (*worktree != NULL)
432 got_worktree_close(*worktree);
435 (*worktree)->lockfd = fd;
440 const struct got_error *
441 got_worktree_open(struct got_worktree **worktree, const char *path)
443 const struct got_error *err = NULL;
446 err = open_worktree(worktree, path);
447 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
451 path = dirname(path);
453 return got_error_from_errno();
454 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
456 return got_error(GOT_ERR_NOT_WORKTREE);
459 const struct got_error *
460 got_worktree_close(struct got_worktree *worktree)
462 const struct got_error *err = NULL;
463 free(worktree->root_path);
464 free(worktree->repo_path);
465 free(worktree->path_prefix);
466 free(worktree->base_commit_id);
467 free(worktree->head_ref_name);
468 if (worktree->lockfd != -1)
469 if (close(worktree->lockfd) != 0)
470 err = got_error_from_errno();
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno();
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 struct got_object_id *
516 got_worktree_get_base_commit_id(struct got_worktree *worktree)
518 return worktree->base_commit_id;
521 const struct got_error *
522 got_worktree_set_base_commit_id(struct got_worktree *worktree,
523 struct got_repository *repo, struct got_object_id *commit_id)
525 const struct got_error *err;
526 struct got_object *obj = NULL;
528 char *path_got = NULL;
530 if (asprintf(&path_got, "%s/%s", worktree->root_path,
531 GOT_WORKTREE_GOT_DIR) == -1) {
532 err = got_error_from_errno();
537 err = got_object_open(&obj, repo, commit_id);
541 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
542 err = got_error(GOT_ERR_OBJ_TYPE);
546 /* Record our base commit. */
547 err = got_object_id_str(&id_str, commit_id);
550 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
554 free(worktree->base_commit_id);
555 worktree->base_commit_id = got_object_id_dup(commit_id);
556 if (worktree->base_commit_id == NULL) {
557 err = got_error_from_errno();
562 got_object_close(obj);
568 static const struct got_error *
569 lock_worktree(struct got_worktree *worktree, int operation)
571 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
572 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
573 : got_error_from_errno());
577 static const struct got_error *
578 add_dir_on_disk(struct got_worktree *worktree, const char *path)
580 const struct got_error *err = NULL;
583 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
584 return got_error_from_errno();
586 err = got_path_mkdir(abspath);
587 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
590 if (lstat(abspath, &sb) == -1) {
591 err = got_error_from_errno();
592 } else if (!S_ISDIR(sb.st_mode)) {
593 /* TODO directory is obstructed; do something */
594 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
601 static const struct got_error *
602 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
604 const struct got_error *err = NULL;
607 size_t flen1 = 0, flen2 = 0;
612 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
613 if (flen1 == 0 && ferror(f1)) {
614 err = got_error_from_errno();
617 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
618 if (flen2 == 0 && ferror(f2)) {
619 err = got_error_from_errno();
626 } else if (flen2 == 0) {
630 } else if (flen1 == flen2) {
631 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
644 static const struct got_error *
645 check_files_equal(int *same, const char *f1_path, const char *f2_path)
647 const struct got_error *err = NULL;
650 FILE *f1 = NULL, *f2 = NULL;
654 if (lstat(f1_path, &sb) != 0) {
655 err = got_error_from_errno();
660 if (lstat(f2_path, &sb) != 0) {
661 err = got_error_from_errno();
666 if (size1 != size2) {
671 f1 = fopen(f1_path, "r");
673 return got_error_from_errno();
675 f2 = fopen(f2_path, "r");
677 err = got_error_from_errno();
681 err = check_file_contents_equal(same, f1, f2);
683 if (f1 && fclose(f1) != 0 && err == NULL)
684 err = got_error_from_errno();
685 if (f2 && fclose(f2) != 0 && err == NULL)
686 err = got_error_from_errno();
692 * Perform a 3-way merge where the file's version in the file index (blob2)
693 * acts as the common ancestor, the incoming blob (blob1) acts as the first
694 * derived version, and the file on disk acts as the second derived version.
696 static const struct got_error *
697 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
698 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
699 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
700 struct got_repository *repo,
701 got_worktree_checkout_cb progress_cb, void *progress_arg)
703 const struct got_error *err = NULL;
705 struct got_blob_object *blob2 = NULL;
706 FILE *f1 = NULL, *f2 = NULL;
707 char *blob1_path = NULL, *blob2_path = NULL;
708 char *merged_path = NULL, *base_path = NULL;
711 int overlapcnt = 0, update_timestamps = 0;
714 parent = dirname(ondisk_path);
716 return got_error_from_errno();
718 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
719 return got_error_from_errno();
721 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
726 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
727 err = got_error_from_errno();
732 err = got_opentemp_named(&blob1_path, &f1, base_path);
735 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
740 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
741 err = got_error_from_errno();
746 err = got_opentemp_named(&blob2_path, &f2, base_path);
749 if (got_fileindex_entry_has_blob(ie)) {
750 struct got_object_id id2;
751 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
752 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
755 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
760 * If the file has no blob, this is an "add vs add" conflict,
761 * and we simply use an empty ancestor file to make both files
762 * appear in the merged result in their entirety.
766 err = got_object_id_str(&id_str, worktree->base_commit_id);
769 if (asprintf(&label1, "commit %s", id_str) == -1) {
770 err = got_error_from_errno();
774 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
775 blob2_path, ondisk_path, label1, path);
779 (*progress_cb)(progress_arg,
780 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
783 if (fsync(merged_fd) != 0) {
784 err = got_error_from_errno();
788 /* Check if a clean merge has subsumed all local changes. */
789 if (overlapcnt == 0) {
790 err = check_files_equal(&update_timestamps, blob1_path,
796 if (chmod(merged_path, st_mode) != 0) {
797 err = got_error_from_errno();
801 if (rename(merged_path, ondisk_path) != 0) {
802 err = got_error_from_errno();
808 * Do not update timestamps of already modified files. Otherwise,
809 * a future status walk would treat them as unmodified files again.
811 err = got_fileindex_entry_update(ie, ondisk_path,
812 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
814 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
815 err = got_error_from_errno();
816 if (f1 && fclose(f1) != 0 && err == NULL)
817 err = got_error_from_errno();
818 if (f2 && fclose(f2) != 0 && err == NULL)
819 err = got_error_from_errno();
821 got_object_blob_close(blob2);
837 static const struct got_error *
838 update_blob_fileindex_entry(struct got_worktree *worktree,
839 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
840 const char *ondisk_path, const char *path, struct got_blob_object *blob,
841 int update_timestamps)
843 const struct got_error *err = NULL;
846 ie = got_fileindex_entry_get(fileindex, path);
848 err = got_fileindex_entry_update(ie, ondisk_path,
849 blob->id.sha1, worktree->base_commit_id->sha1,
852 struct got_fileindex_entry *new_ie;
853 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
854 path, blob->id.sha1, worktree->base_commit_id->sha1);
856 err = got_fileindex_entry_add(fileindex, new_ie);
861 static const struct got_error *
862 install_blob(struct got_worktree *worktree, const char *ondisk_path,
863 const char *path, uint16_t te_mode, uint16_t st_mode,
864 struct got_blob_object *blob, int restoring_missing_file,
865 int reverting_versioned_file, struct got_repository *repo,
866 got_worktree_checkout_cb progress_cb, void *progress_arg)
868 const struct got_error *err = NULL;
872 char *tmppath = NULL;
874 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
875 GOT_DEFAULT_FILE_MODE);
877 if (errno == ENOENT) {
878 char *parent = dirname(path);
880 return got_error_from_errno();
881 err = add_dir_on_disk(worktree, parent);
884 fd = open(ondisk_path,
885 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
886 GOT_DEFAULT_FILE_MODE);
888 return got_error_from_errno();
889 } else if (errno == EEXIST) {
890 if (!S_ISREG(st_mode)) {
891 /* TODO file is obstructed; do something */
892 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
895 err = got_opentemp_named_fd(&tmppath, &fd,
902 return got_error_from_errno();
905 if (restoring_missing_file)
906 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
907 else if (reverting_versioned_file)
908 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
910 (*progress_cb)(progress_arg,
911 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
913 hdrlen = got_object_blob_get_hdrlen(blob);
915 const uint8_t *buf = got_object_blob_get_read_buf(blob);
916 err = got_object_blob_read_block(&len, blob);
920 /* Skip blob object header first time around. */
921 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
923 err = got_error_from_errno();
925 } else if (outlen != len - hdrlen) {
926 err = got_error(GOT_ERR_IO);
933 if (fsync(fd) != 0) {
934 err = got_error_from_errno();
939 if (rename(tmppath, ondisk_path) != 0) {
940 err = got_error_from_errno();
946 if (te_mode & S_IXUSR) {
947 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
948 err = got_error_from_errno();
952 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
953 err = got_error_from_errno();
959 if (fd != -1 && close(fd) != 0 && err == NULL)
960 err = got_error_from_errno();
965 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
966 static const struct got_error *
967 get_modified_file_content_status(unsigned char *status, FILE *f)
969 const struct got_error *err = NULL;
970 const char *markers[3] = {
971 GOT_DIFF_CONFLICT_MARKER_BEGIN,
972 GOT_DIFF_CONFLICT_MARKER_SEP,
973 GOT_DIFF_CONFLICT_MARKER_END
978 const char delim[3] = {'\0', '\0', '\0'};
980 while (*status == GOT_STATUS_MODIFY) {
981 line = fparseln(f, &len, NULL, delim, 0);
985 err = got_ferror(f, GOT_ERR_IO);
989 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
990 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
991 *status = GOT_STATUS_CONFLICT;
1000 static const struct got_error *
1001 get_file_status(unsigned char *status, struct stat *sb,
1002 struct got_fileindex_entry *ie, const char *abspath,
1003 struct got_repository *repo)
1005 const struct got_error *err = NULL;
1006 struct got_object_id id;
1010 struct got_blob_object *blob = NULL;
1013 *status = GOT_STATUS_NO_CHANGE;
1015 if (lstat(abspath, sb) == -1) {
1016 if (errno == ENOENT) {
1018 if (got_fileindex_entry_has_file_on_disk(ie))
1019 *status = GOT_STATUS_MISSING;
1021 *status = GOT_STATUS_DELETE;
1023 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1024 & (S_IRWXU | S_IRWXG | S_IRWXO));
1026 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1029 return got_error_from_errno();
1032 if (!S_ISREG(sb->st_mode)) {
1033 *status = GOT_STATUS_OBSTRUCTED;
1040 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1041 *status = GOT_STATUS_DELETE;
1043 } else if (!got_fileindex_entry_has_blob(ie)) {
1044 *status = GOT_STATUS_ADD;
1048 if (ie->ctime_sec == sb->st_ctime &&
1049 ie->ctime_nsec == sb->st_ctimensec &&
1050 ie->mtime_sec == sb->st_mtime &&
1051 ie->mtime_sec == sb->st_mtime &&
1052 ie->mtime_nsec == sb->st_mtimensec &&
1053 ie->size == (sb->st_size & 0xffffffff))
1056 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1057 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1061 f = fopen(abspath, "r");
1063 err = got_error_from_errno();
1066 hdrlen = got_object_blob_get_hdrlen(blob);
1068 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1069 err = got_object_blob_read_block(&blen, blob);
1072 /* Skip length of blob object header first time around. */
1073 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1074 if (flen == 0 && ferror(f)) {
1075 err = got_error_from_errno();
1080 *status = GOT_STATUS_MODIFY;
1082 } else if (flen == 0) {
1084 *status = GOT_STATUS_MODIFY;
1086 } else if (blen - hdrlen == flen) {
1087 /* Skip blob object header first time around. */
1088 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1089 *status = GOT_STATUS_MODIFY;
1093 *status = GOT_STATUS_MODIFY;
1099 if (*status == GOT_STATUS_MODIFY) {
1101 err = get_modified_file_content_status(status, f);
1105 got_object_blob_close(blob);
1111 static const struct got_error *
1112 update_blob(struct got_worktree *worktree,
1113 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1114 struct got_tree_entry *te, const char *path,
1115 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1118 const struct got_error *err = NULL;
1119 struct got_blob_object *blob = NULL;
1121 unsigned char status = GOT_STATUS_NO_CHANGE;
1124 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1125 return got_error_from_errno();
1127 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1131 if (status == GOT_STATUS_OBSTRUCTED) {
1132 (*progress_cb)(progress_arg, status, path);
1136 if (ie && status != GOT_STATUS_MISSING) {
1137 if (got_fileindex_entry_has_commit(ie) &&
1138 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1139 SHA1_DIGEST_LENGTH) == 0) {
1140 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1144 if (got_fileindex_entry_has_blob(ie) &&
1145 memcmp(ie->blob_sha1, te->id->sha1,
1146 SHA1_DIGEST_LENGTH) == 0)
1150 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1154 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1155 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1156 te->mode, sb.st_mode, blob, repo, progress_cb,
1158 else if (status == GOT_STATUS_DELETE) {
1159 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1160 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1161 ondisk_path, path, blob, 0);
1165 err = install_blob(worktree, ondisk_path, path, te->mode,
1166 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1167 repo, progress_cb, progress_arg);
1170 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1171 ondisk_path, path, blob, 1);
1175 got_object_blob_close(blob);
1181 static const struct got_error *
1182 remove_ondisk_file(const char *root_path, const char *path)
1184 const struct got_error *err = NULL;
1185 char *ondisk_path = NULL;
1187 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1188 return got_error_from_errno();
1190 if (unlink(ondisk_path) == -1) {
1191 if (errno != ENOENT)
1192 err = got_error_from_errno();
1194 char *parent = dirname(ondisk_path);
1195 while (parent && strcmp(parent, root_path) != 0) {
1196 if (rmdir(parent) == -1) {
1197 if (errno != ENOTEMPTY)
1198 err = got_error_from_errno();
1201 parent = dirname(parent);
1208 static const struct got_error *
1209 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1210 struct got_fileindex_entry *ie, const char *parent_path,
1211 struct got_repository *repo,
1212 got_worktree_checkout_cb progress_cb, void *progress_arg)
1214 const struct got_error *err = NULL;
1215 unsigned char status;
1219 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1221 return got_error_from_errno();
1223 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1227 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1228 status == GOT_STATUS_ADD) {
1229 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1231 * Preserve the working file and change the deleted blob's
1232 * entry into a schedule-add entry.
1234 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1239 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1240 if (status == GOT_STATUS_NO_CHANGE) {
1241 err = remove_ondisk_file(worktree->root_path, ie->path);
1245 got_fileindex_entry_remove(fileindex, ie);
1251 struct diff_cb_arg {
1252 struct got_fileindex *fileindex;
1253 struct got_worktree *worktree;
1254 struct got_repository *repo;
1255 got_worktree_checkout_cb progress_cb;
1257 got_worktree_cancel_cb cancel_cb;
1261 static const struct got_error *
1262 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1263 struct got_tree_entry *te, const char *parent_path)
1265 struct diff_cb_arg *a = arg;
1267 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1268 return got_error(GOT_ERR_CANCELLED);
1270 return update_blob(a->worktree, a->fileindex, ie, te,
1271 ie->path, a->repo, a->progress_cb, a->progress_arg);
1274 static const struct got_error *
1275 diff_old(void *arg, struct got_fileindex_entry *ie, 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 delete_blob(a->worktree, a->fileindex, ie, parent_path,
1283 a->repo, a->progress_cb, a->progress_arg);
1286 static const struct got_error *
1287 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1289 struct diff_cb_arg *a = arg;
1290 const struct got_error *err;
1293 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1294 return got_error(GOT_ERR_CANCELLED);
1296 if (asprintf(&path, "%s%s%s", parent_path,
1297 parent_path[0] ? "/" : "", te->name)
1299 return got_error_from_errno();
1301 if (S_ISDIR(te->mode))
1302 err = add_dir_on_disk(a->worktree, path);
1304 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1305 a->repo, a->progress_cb, a->progress_arg);
1311 const struct got_error *
1312 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1314 const struct got_error *err = NULL;
1315 char *uuidstr = NULL;
1316 uint32_t uuid_status;
1320 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1321 if (uuid_status != uuid_s_ok)
1322 return got_error_uuid(uuid_status);
1324 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1326 err = got_error_from_errno();
1334 * Prevent Git's garbage collector from deleting our base commit by
1335 * setting a reference to our base commit's ID.
1337 static const struct got_error *
1338 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1340 const struct got_error *err = NULL;
1341 struct got_reference *ref = NULL;
1344 err = got_worktree_get_base_ref_name(&refname, worktree);
1348 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1352 err = got_ref_write(ref, repo);
1360 static const struct got_error *
1361 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1362 struct got_worktree *worktree)
1364 const struct got_error *err = NULL;
1367 *fileindex_path = NULL;
1368 *fileindex = got_fileindex_alloc();
1369 if (*fileindex == NULL)
1370 return got_error_from_errno();
1372 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1373 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1374 err = got_error_from_errno();
1375 *fileindex_path = NULL;
1379 index = fopen(*fileindex_path, "rb");
1380 if (index == NULL) {
1381 if (errno != ENOENT)
1382 err = got_error_from_errno();
1384 err = got_fileindex_read(*fileindex, index);
1385 if (fclose(index) != 0 && err == NULL)
1386 err = got_error_from_errno();
1390 free(*fileindex_path);
1391 *fileindex_path = NULL;
1398 const struct got_error *
1399 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1400 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1401 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1403 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1404 struct got_commit_object *commit = NULL;
1405 struct got_object_id *tree_id = NULL;
1406 struct got_tree_object *tree = NULL;
1407 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1408 struct got_fileindex *fileindex = NULL;
1409 FILE *new_index = NULL;
1410 struct got_fileindex_diff_tree_cb diff_cb;
1411 struct diff_cb_arg arg;
1412 char *relpath = NULL, *entry_name = NULL;
1414 err = lock_worktree(worktree, LOCK_EX);
1419 * Read the file index.
1420 * Checking out files is supposed to be an idempotent operation.
1421 * If the on-disk file index is incomplete we will try to complete it.
1423 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1427 err = got_opentemp_named(&new_fileindex_path, &new_index,
1432 err = ref_base_commit(worktree, repo);
1436 err = got_object_open_as_commit(&commit, repo,
1437 worktree->base_commit_id);
1444 relpath = strdup(path);
1445 if (relpath == NULL) {
1446 err = got_error_from_errno();
1449 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1450 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1452 err = got_error_from_errno();
1455 err = got_object_id_by_path(&tree_id, repo,
1456 worktree->base_commit_id, tree_path);
1460 err = got_object_get_type(&obj_type, repo, tree_id);
1463 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1464 /* Split provided path into parent dir + entry name. */
1465 if (strchr(path, '/') == NULL) {
1466 relpath = strdup("");
1467 if (relpath == NULL) {
1468 err = got_error_from_errno();
1471 tree_path = strdup(worktree->path_prefix);
1472 if (tree_path == NULL) {
1473 err = got_error_from_errno();
1477 err = got_path_dirname(&relpath, path);
1480 if (asprintf(&tree_path, "%s%s%s",
1481 worktree->path_prefix,
1482 got_path_is_root_dir(
1483 worktree->path_prefix) ? "" : "/",
1485 err = got_error_from_errno();
1489 err = got_object_id_by_path(&tree_id, repo,
1490 worktree->base_commit_id, tree_path);
1494 entry_name = basename(path);
1495 if (entry_name == NULL) {
1496 err = got_error_from_errno();
1501 relpath = strdup("");
1502 if (relpath == NULL) {
1503 err = got_error_from_errno();
1506 err = got_object_id_by_path(&tree_id, repo,
1507 worktree->base_commit_id, worktree->path_prefix);
1512 err = got_object_open_as_tree(&tree, repo, tree_id);
1517 got_object_tree_find_entry(tree, entry_name) == NULL) {
1518 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1522 diff_cb.diff_old_new = diff_old_new;
1523 diff_cb.diff_old = diff_old;
1524 diff_cb.diff_new = diff_new;
1525 arg.fileindex = fileindex;
1526 arg.worktree = worktree;
1528 arg.progress_cb = progress_cb;
1529 arg.progress_arg = progress_arg;
1530 arg.cancel_cb = cancel_cb;
1531 arg.cancel_arg = cancel_arg;
1532 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1533 entry_name, repo, &diff_cb, &arg);
1535 /* Try to sync the fileindex back to disk in any case. */
1536 err = got_fileindex_write(fileindex, new_index);
1540 if (rename(new_fileindex_path, fileindex_path) != 0) {
1541 err = got_error_from_errno();
1542 unlink(new_fileindex_path);
1546 free(new_fileindex_path);
1547 new_fileindex_path = NULL;
1552 got_object_tree_close(tree);
1554 got_object_commit_close(commit);
1555 if (new_fileindex_path)
1556 unlink(new_fileindex_path);
1559 free(new_fileindex_path);
1560 free(fileindex_path);
1561 got_fileindex_free(fileindex);
1564 unlockerr = lock_worktree(worktree, LOCK_SH);
1565 if (unlockerr && err == NULL)
1570 struct diff_dir_cb_arg {
1571 struct got_fileindex *fileindex;
1572 struct got_worktree *worktree;
1573 const char *status_path;
1574 size_t status_path_len;
1575 struct got_repository *repo;
1576 got_worktree_status_cb status_cb;
1578 got_worktree_cancel_cb cancel_cb;
1582 static const struct got_error *
1583 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1584 got_worktree_status_cb status_cb, void *status_arg,
1585 struct got_repository *repo)
1587 const struct got_error *err = NULL;
1588 unsigned char status = GOT_STATUS_NO_CHANGE;
1590 struct got_object_id id;
1592 err = get_file_status(&status, &sb, ie, abspath, repo);
1593 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1594 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1595 err = (*status_cb)(status_arg, status, ie->path, &id);
1600 static const struct got_error *
1601 status_old_new(void *arg, struct got_fileindex_entry *ie,
1602 struct dirent *de, const char *parent_path)
1604 const struct got_error *err = NULL;
1605 struct diff_dir_cb_arg *a = arg;
1608 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1609 return got_error(GOT_ERR_CANCELLED);
1611 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1612 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1615 if (parent_path[0]) {
1616 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1617 parent_path, de->d_name) == -1)
1618 return got_error_from_errno();
1620 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1622 return got_error_from_errno();
1625 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1631 static const struct got_error *
1632 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1634 struct diff_dir_cb_arg *a = arg;
1635 struct got_object_id id;
1636 unsigned char status;
1638 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1639 return got_error(GOT_ERR_CANCELLED);
1641 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1644 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1645 if (got_fileindex_entry_has_file_on_disk(ie))
1646 status = GOT_STATUS_MISSING;
1648 status = GOT_STATUS_DELETE;
1649 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1652 static const struct got_error *
1653 status_new(void *arg, struct dirent *de, const char *parent_path)
1655 const struct got_error *err = NULL;
1656 struct diff_dir_cb_arg *a = arg;
1659 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1660 return got_error(GOT_ERR_CANCELLED);
1662 if (de->d_type == DT_DIR)
1665 /* XXX ignore symlinks for now */
1666 if (de->d_type == DT_LNK)
1669 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1672 if (parent_path[0]) {
1673 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1674 return got_error_from_errno();
1679 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1686 const struct got_error *
1687 got_worktree_status(struct got_worktree *worktree, const char *path,
1688 struct got_repository *repo, got_worktree_status_cb status_cb,
1689 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1691 const struct got_error *err = NULL;
1692 DIR *workdir = NULL;
1693 char *fileindex_path = NULL;
1694 struct got_fileindex *fileindex = NULL;
1696 struct got_fileindex_diff_dir_cb fdiff_cb;
1697 struct diff_dir_cb_arg arg;
1698 char *ondisk_path = NULL;
1700 fileindex = got_fileindex_alloc();
1701 if (fileindex == NULL) {
1702 err = got_error_from_errno();
1706 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1707 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1708 err = got_error_from_errno();
1709 fileindex_path = NULL;
1713 index = fopen(fileindex_path, "rb");
1714 if (index == NULL) {
1715 if (errno != ENOENT) {
1716 err = got_error_from_errno();
1720 err = got_fileindex_read(fileindex, index);
1726 if (asprintf(&ondisk_path, "%s%s%s",
1727 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1728 err = got_error_from_errno();
1731 workdir = opendir(ondisk_path);
1732 if (workdir == NULL) {
1733 if (errno == ENOTDIR || errno == ENOENT) {
1734 struct got_fileindex_entry *ie;
1735 ie = got_fileindex_entry_get(fileindex, path);
1737 err = got_error(GOT_ERR_BAD_PATH);
1740 err = report_file_status(ie, ondisk_path,
1741 status_cb, status_arg, repo);
1744 err = got_error_from_errno();
1748 fdiff_cb.diff_old_new = status_old_new;
1749 fdiff_cb.diff_old = status_old;
1750 fdiff_cb.diff_new = status_new;
1751 arg.fileindex = fileindex;
1752 arg.worktree = worktree;
1753 arg.status_path = path;
1754 arg.status_path_len = strlen(path);
1756 arg.status_cb = status_cb;
1757 arg.status_arg = status_arg;
1758 arg.cancel_cb = cancel_cb;
1759 arg.cancel_arg = cancel_arg;
1760 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1761 path, repo, &fdiff_cb, &arg);
1766 free(fileindex_path);
1767 got_fileindex_free(fileindex);
1771 const struct got_error *
1772 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1775 const struct got_error *err = NULL;
1776 char *resolved, *path = NULL;
1781 resolved = realpath(arg, NULL);
1782 if (resolved == NULL)
1783 return got_error_from_errno();
1785 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1786 strlen(got_worktree_get_root_path(worktree)))) {
1787 err = got_error(GOT_ERR_BAD_PATH);
1791 path = strdup(resolved +
1792 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1794 err = got_error_from_errno();
1798 /* XXX status walk can't deal with trailing slash! */
1800 while (path[len - 1] == '/') {
1801 path[len - 1] = '\0';
1813 const struct got_error *
1814 got_worktree_schedule_add(struct got_worktree *worktree,
1815 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1816 struct got_repository *repo)
1818 struct got_fileindex *fileindex = NULL;
1819 struct got_fileindex_entry *ie = NULL;
1820 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1821 FILE *index = NULL, *new_index = NULL;
1822 const struct got_error *err = NULL, *unlockerr = NULL;
1825 err = lock_worktree(worktree, LOCK_EX);
1829 err = got_path_skip_common_ancestor(&relpath,
1830 got_worktree_get_root_path(worktree), ondisk_path);
1834 fileindex = got_fileindex_alloc();
1835 if (fileindex == NULL) {
1836 err = got_error_from_errno();
1840 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1841 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1842 err = got_error_from_errno();
1843 fileindex_path = NULL;
1847 index = fopen(fileindex_path, "rb");
1848 if (index == NULL) {
1849 err = got_error_from_errno();
1853 err = got_fileindex_read(fileindex, index);
1857 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1858 err = got_error_set_errno(EEXIST);
1862 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1866 err = got_fileindex_entry_add(fileindex, ie);
1869 ie_added = 1; /* now owned by fileindex; don't free separately */
1871 err = got_opentemp_named(&new_fileindex_path, &new_index,
1876 err = got_fileindex_write(fileindex, new_index);
1880 if (rename(new_fileindex_path, fileindex_path) != 0) {
1881 err = got_error_from_errno();
1885 free(new_fileindex_path);
1886 new_fileindex_path = NULL;
1888 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1891 if (fclose(index) != 0 && err == NULL)
1892 err = got_error_from_errno();
1894 if (new_fileindex_path) {
1895 if (unlink(new_fileindex_path) != 0 && err == NULL)
1896 err = got_error_from_errno();
1897 free(new_fileindex_path);
1899 if (ie && !ie_added)
1900 got_fileindex_entry_free(ie);
1902 got_fileindex_free(fileindex);
1903 unlockerr = lock_worktree(worktree, LOCK_SH);
1904 if (unlockerr && err == NULL)
1910 const struct got_error *
1911 got_worktree_schedule_delete(struct got_worktree *worktree,
1912 const char *ondisk_path, int delete_local_mods,
1913 got_worktree_status_cb status_cb, void *status_arg,
1914 struct got_repository *repo)
1916 struct got_fileindex *fileindex = NULL;
1917 struct got_fileindex_entry *ie = NULL;
1918 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1919 FILE *index = NULL, *new_index = NULL;
1920 const struct got_error *err = NULL, *unlockerr = NULL;
1921 unsigned char status;
1924 err = lock_worktree(worktree, LOCK_EX);
1928 err = got_path_skip_common_ancestor(&relpath,
1929 got_worktree_get_root_path(worktree), ondisk_path);
1933 fileindex = got_fileindex_alloc();
1934 if (fileindex == NULL) {
1935 err = got_error_from_errno();
1939 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1940 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1941 err = got_error_from_errno();
1942 fileindex_path = NULL;
1946 index = fopen(fileindex_path, "rb");
1947 if (index == NULL) {
1948 err = got_error_from_errno();
1952 err = got_fileindex_read(fileindex, index);
1956 ie = got_fileindex_entry_get(fileindex, relpath);
1958 err = got_error(GOT_ERR_BAD_PATH);
1962 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1966 if (status != GOT_STATUS_NO_CHANGE) {
1967 if (status == GOT_STATUS_DELETE) {
1968 err = got_error_set_errno(ENOENT);
1971 if (status != GOT_STATUS_MODIFY) {
1972 err = got_error(GOT_ERR_FILE_STATUS);
1975 if (!delete_local_mods) {
1976 err = got_error(GOT_ERR_FILE_MODIFIED);
1981 if (unlink(ondisk_path) != 0) {
1982 err = got_error_from_errno();
1986 got_fileindex_entry_mark_deleted_from_disk(ie);
1988 err = got_opentemp_named(&new_fileindex_path, &new_index,
1993 err = got_fileindex_write(fileindex, new_index);
1997 if (rename(new_fileindex_path, fileindex_path) != 0) {
1998 err = got_error_from_errno();
2002 free(new_fileindex_path);
2003 new_fileindex_path = NULL;
2005 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2009 if (fclose(index) != 0 && err == NULL)
2010 err = got_error_from_errno();
2012 if (new_fileindex_path) {
2013 if (unlink(new_fileindex_path) != 0 && err == NULL)
2014 err = got_error_from_errno();
2015 free(new_fileindex_path);
2018 got_fileindex_free(fileindex);
2019 unlockerr = lock_worktree(worktree, LOCK_SH);
2020 if (unlockerr && err == NULL)
2025 const struct got_error *
2026 got_worktree_revert(struct got_worktree *worktree,
2027 const char *ondisk_path,
2028 got_worktree_checkout_cb progress_cb, void *progress_arg,
2029 struct got_repository *repo)
2031 struct got_fileindex *fileindex = NULL;
2032 struct got_fileindex_entry *ie = NULL;
2033 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2034 char *tree_path = NULL, *parent_path, *te_name;
2035 FILE *index = NULL, *new_index = NULL;
2036 const struct got_error *err = NULL, *unlockerr = NULL;
2037 struct got_tree_object *tree = NULL;
2038 struct got_object_id id, *tree_id = NULL;
2039 const struct got_tree_entry *te;
2040 struct got_blob_object *blob = NULL;
2041 unsigned char status;
2044 err = lock_worktree(worktree, LOCK_EX);
2048 err = got_path_skip_common_ancestor(&relpath,
2049 got_worktree_get_root_path(worktree), ondisk_path);
2053 fileindex = got_fileindex_alloc();
2054 if (fileindex == NULL) {
2055 err = got_error_from_errno();
2059 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2060 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2061 err = got_error_from_errno();
2062 fileindex_path = NULL;
2066 index = fopen(fileindex_path, "rb");
2067 if (index == NULL) {
2068 err = got_error_from_errno();
2072 err = got_fileindex_read(fileindex, index);
2076 ie = got_fileindex_entry_get(fileindex, relpath);
2078 err = got_error(GOT_ERR_BAD_PATH);
2082 /* Construct in-repository path of tree which contains this blob. */
2083 err = got_path_dirname(&parent_path, ie->path);
2085 if (err->code != GOT_ERR_BAD_PATH)
2089 if (got_path_is_root_dir(worktree->path_prefix)) {
2090 tree_path = strdup(parent_path);
2091 if (tree_path == NULL) {
2092 err = got_error_from_errno();
2096 if (got_path_is_root_dir(parent_path)) {
2097 tree_path = strdup(worktree->path_prefix);
2098 if (tree_path == NULL) {
2099 err = got_error_from_errno();
2103 if (asprintf(&tree_path, "%s/%s",
2104 worktree->path_prefix, parent_path) == -1) {
2105 err = got_error_from_errno();
2111 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2116 err = got_object_open_as_tree(&tree, repo, tree_id);
2120 te_name = basename(ie->path);
2121 if (te_name == NULL) {
2122 err = got_error_from_errno();
2126 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2130 te = got_object_tree_find_entry(tree, te_name);
2131 if (te == NULL && status != GOT_STATUS_ADD) {
2132 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2137 case GOT_STATUS_ADD:
2138 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2139 got_fileindex_entry_remove(fileindex, ie);
2141 case GOT_STATUS_DELETE:
2142 case GOT_STATUS_MODIFY:
2143 case GOT_STATUS_CONFLICT:
2144 case GOT_STATUS_MISSING:
2145 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2146 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2149 err = install_blob(worktree, ondisk_path, ie->path,
2150 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2154 if (status == GOT_STATUS_DELETE) {
2155 err = update_blob_fileindex_entry(worktree,
2156 fileindex, ie, ondisk_path, ie->path, blob, 1);
2165 err = got_opentemp_named(&new_fileindex_path, &new_index,
2170 err = got_fileindex_write(fileindex, new_index);
2174 if (rename(new_fileindex_path, fileindex_path) != 0) {
2175 err = got_error_from_errno();
2179 free(new_fileindex_path);
2180 new_fileindex_path = NULL;
2185 got_object_blob_close(blob);
2187 got_object_tree_close(tree);
2190 if (fclose(index) != 0 && err == NULL)
2191 err = got_error_from_errno();
2193 if (new_fileindex_path) {
2194 if (unlink(new_fileindex_path) != 0 && err == NULL)
2195 err = got_error_from_errno();
2196 free(new_fileindex_path);
2199 got_fileindex_free(fileindex);
2200 unlockerr = lock_worktree(worktree, LOCK_SH);
2201 if (unlockerr && err == NULL)
2210 unsigned char status;
2211 struct got_object_id *blob_id;
2212 struct got_object_id *base_id;
2217 free_commitable(struct commitable *ct)
2220 free(ct->in_repo_path);
2221 free(ct->ondisk_path);
2227 struct collect_commitables_arg {
2228 struct got_pathlist_head *commitable_paths;
2229 struct got_repository *repo;
2230 struct got_worktree *worktree;
2233 static const struct got_error *
2234 collect_commitables(void *arg, unsigned char status, const char *relpath,
2235 struct got_object_id *id)
2237 struct collect_commitables_arg *a = arg;
2238 const struct got_error *err = NULL;
2239 struct commitable *ct = NULL;
2240 struct got_pathlist_entry *new = NULL;
2241 char *parent_path = NULL, *path = NULL;
2244 if (status == GOT_STATUS_CONFLICT)
2245 return got_error(GOT_ERR_COMMIT_CONFLICT);
2247 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2248 status != GOT_STATUS_DELETE)
2251 if (asprintf(&path, "/%s", relpath) == -1) {
2252 err = got_error_from_errno();
2255 if (strcmp(path, "/") == 0) {
2256 parent_path = strdup("");
2257 if (parent_path == NULL)
2258 return got_error_from_errno();
2260 err = got_path_dirname(&parent_path, path);
2265 ct = calloc(1, sizeof(*ct));
2267 err = got_error_from_errno();
2271 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2273 err = got_error_from_errno();
2276 if (status == GOT_STATUS_DELETE) {
2277 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2279 if (lstat(ct->ondisk_path, &sb) != 0) {
2280 err = got_error_from_errno();
2283 ct->mode = sb.st_mode;
2286 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2287 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2289 err = got_error_from_errno();
2293 ct->status = status;
2294 ct->blob_id = NULL; /* will be filled in when blob gets created */
2295 if (ct->status != GOT_STATUS_ADD) {
2296 ct->base_id = got_object_id_dup(id);
2297 if (ct->base_id == NULL) {
2298 err = got_error_from_errno();
2302 ct->path = strdup(path);
2303 if (ct->path == NULL) {
2304 err = got_error_from_errno();
2307 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2309 if (ct && (err || new == NULL))
2310 free_commitable(ct);
2316 static const struct got_error *write_tree(struct got_object_id **,
2317 struct got_tree_object *, const char *, struct got_pathlist_head *,
2318 got_worktree_status_cb status_cb, void *status_arg,
2319 struct got_repository *);
2321 static const struct got_error *
2322 write_subtree(struct got_object_id **new_subtree_id,
2323 struct got_tree_entry *te, const char *parent_path,
2324 struct got_pathlist_head *commitable_paths,
2325 got_worktree_status_cb status_cb, void *status_arg,
2326 struct got_repository *repo)
2328 const struct got_error *err = NULL;
2329 struct got_tree_object *subtree;
2332 if (asprintf(&subpath, "%s%s%s", parent_path,
2333 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2334 return got_error_from_errno();
2336 err = got_object_open_as_tree(&subtree, repo, te->id);
2340 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2341 status_cb, status_arg, repo);
2342 got_object_tree_close(subtree);
2347 static const struct got_error *
2348 match_ct_parent_path(int *match, struct commitable *ct, const char *path)
2350 const struct got_error *err = NULL;
2351 char *ct_parent_path = NULL;
2355 if (strchr(ct->path, '/') == NULL) {
2356 ct_parent_path = strdup("/");
2357 if (ct_parent_path == NULL)
2358 return got_error_from_errno();
2360 err = got_path_dirname(&ct_parent_path, ct->path);
2365 *match = (strcmp(path, ct_parent_path) == 0);
2366 free(ct_parent_path);
2371 get_ct_file_mode(struct commitable *ct)
2373 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2376 static const struct got_error *
2377 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2378 struct got_tree_entry *te, struct commitable *ct)
2380 const struct got_error *err = NULL;
2384 err = got_object_tree_entry_dup(new_te, te);
2388 (*new_te)->mode = get_ct_file_mode(ct);
2390 free((*new_te)->id);
2391 (*new_te)->id = got_object_id_dup(ct->blob_id);
2392 if ((*new_te)->id == NULL) {
2393 err = got_error_from_errno();
2397 if (err && *new_te) {
2398 got_object_tree_entry_close(*new_te);
2404 static const struct got_error *
2405 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2406 struct commitable *ct)
2408 const struct got_error *err = NULL;
2413 *new_te = calloc(1, sizeof(*new_te));
2414 if (*new_te == NULL)
2415 return got_error_from_errno();
2417 ct_name = basename(ct->path);
2418 if (ct_name == NULL) {
2419 err = got_error_from_errno();
2422 (*new_te)->name = strdup(ct_name);
2423 if ((*new_te)->name == NULL) {
2424 err = got_error_from_errno();
2428 (*new_te)->mode = get_ct_file_mode(ct);
2430 (*new_te)->id = got_object_id_dup(ct->blob_id);
2431 if ((*new_te)->id == NULL) {
2432 err = got_error_from_errno();
2436 if (err && *new_te) {
2437 got_object_tree_entry_close(*new_te);
2443 static const struct got_error *
2444 insert_tree_entry(struct got_tree_entry *new_te,
2445 struct got_pathlist_head *paths)
2447 const struct got_error *err = NULL;
2448 struct got_pathlist_entry *new_pe;
2450 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2454 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2458 static const struct got_error *
2459 report_ct_status(struct commitable *ct,
2460 got_worktree_status_cb status_cb, void *status_arg)
2462 const char *ct_path = ct->path;
2463 while (ct_path[0] == '/')
2465 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id);
2468 static const struct got_error *
2469 match_modified_subtree(int *modified, struct got_tree_entry *te,
2470 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2472 const struct got_error *err = NULL;
2473 struct got_pathlist_entry *pe;
2478 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2479 got_path_is_root_dir(base_tree_path) ? "" : "/",
2481 return got_error_from_errno();
2483 TAILQ_FOREACH(pe, commitable_paths, entry) {
2484 struct commitable *ct = pe->data;
2487 if (asprintf(&ct_path, "%s%s%s", base_tree_path,
2488 got_path_is_root_dir(base_tree_path) ? "" : "/",
2490 err = got_error_from_errno();
2494 *modified = got_path_is_child(ct->in_repo_path, te_path,
2504 static const struct got_error *
2505 match_deleted_or_modified_ct(struct commitable **ctp,
2506 struct got_tree_entry *te, const char *base_tree_path,
2507 struct got_pathlist_head *commitable_paths)
2509 const struct got_error *err = NULL;
2510 struct got_pathlist_entry *pe;
2514 TAILQ_FOREACH(pe, commitable_paths, entry) {
2515 struct commitable *ct = pe->data;
2516 char *ct_name = NULL;
2519 if (ct->status != GOT_STATUS_MODIFY &&
2520 ct->status != GOT_STATUS_DELETE)
2523 if (got_object_id_cmp(ct->base_id, te->id) != 0)
2526 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2532 ct_name = basename(pe->path);
2533 if (ct_name == NULL)
2534 return got_error_from_errno();
2536 if (strcmp(te->name, ct_name) != 0)
2546 static const struct got_error *
2547 write_tree(struct got_object_id **new_tree_id,
2548 struct got_tree_object *base_tree, const char *path_base_tree,
2549 struct got_pathlist_head *commitable_paths,
2550 got_worktree_status_cb status_cb, void *status_arg,
2551 struct got_repository *repo)
2553 const struct got_error *err = NULL;
2554 const struct got_tree_entries *base_entries = NULL;
2555 struct got_pathlist_head paths;
2556 struct got_tree_entries new_tree_entries;
2557 struct got_tree_entry *te, *new_te = NULL;
2558 struct got_pathlist_entry *pe;
2561 new_tree_entries.nentries = 0;
2562 SIMPLEQ_INIT(&new_tree_entries.head);
2564 /* Insert, and recurse into, newly added entries first. */
2565 TAILQ_FOREACH(pe, commitable_paths, entry) {
2566 struct commitable *ct = pe->data;
2567 char *child_path = NULL, *slash;
2569 if (ct->status != GOT_STATUS_ADD)
2572 if (!got_path_is_child(pe->path, path_base_tree,
2573 strlen(path_base_tree)))
2576 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2581 slash = strchr(child_path, '/');
2582 if (slash == NULL) {
2583 err = alloc_added_blob_tree_entry(&new_te, ct);
2586 err = report_ct_status(ct, status_cb, status_arg);
2591 struct got_pathlist_entry *pe2;
2594 *slash = '\0'; /* trim trailing path components */
2595 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2596 got_path_is_root_dir(path_base_tree) ? "" : "/",
2597 child_path) == -1) {
2598 err = got_error_from_errno();
2601 TAILQ_FOREACH(pe2, &paths, entry) {
2602 if (got_path_cmp(subtree_path, pe2->path) != 0)
2610 new_te = calloc(1, sizeof(*new_te));
2611 new_te->mode = S_IFDIR;
2612 new_te->name = strdup(child_path);
2613 if (new_te->name == NULL) {
2614 err = got_error_from_errno();
2615 got_object_tree_entry_close(new_te);
2619 err = write_tree(&new_te->id, NULL, subtree_path,
2620 commitable_paths, status_cb, status_arg, repo);
2623 got_object_tree_entry_close(new_te);
2628 err = insert_tree_entry(new_te, &paths);
2634 /* Handle modified and deleted entries. */
2635 base_entries = got_object_tree_get_entries(base_tree);
2636 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2637 struct commitable *ct = NULL;
2639 if (S_ISDIR(te->mode)) {
2641 err = got_object_tree_entry_dup(&new_te, te);
2644 err = match_modified_subtree(&modified, te,
2645 path_base_tree, commitable_paths);
2648 /* Avoid recursion into unmodified subtrees. */
2651 err = write_subtree(&new_te->id, te,
2652 path_base_tree, commitable_paths,
2653 status_cb, status_arg, repo);
2657 err = insert_tree_entry(new_te, &paths);
2663 err = match_deleted_or_modified_ct(&ct, te,
2664 path_base_tree, commitable_paths);
2666 /* NB: Deleted entries get dropped here. */
2667 if (ct->status == GOT_STATUS_MODIFY) {
2668 err = alloc_modified_blob_tree_entry(
2672 err = insert_tree_entry(new_te, &paths);
2676 err = report_ct_status(ct, status_cb, status_arg);
2680 /* Entry is unchanged; just copy it. */
2681 err = got_object_tree_entry_dup(&new_te, te);
2684 err = insert_tree_entry(new_te, &paths);
2691 /* Write new list of entries; deleted entries have been dropped. */
2692 TAILQ_FOREACH(pe, &paths, entry) {
2693 struct got_tree_entry *te = pe->data;
2694 new_tree_entries.nentries++;
2695 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2697 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2699 got_object_tree_entries_close(&new_tree_entries);
2700 got_pathlist_free(&paths);
2704 static const struct got_error *
2705 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2706 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2708 const struct got_error *err = NULL;
2709 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2710 struct got_fileindex *fileindex = NULL;
2711 FILE *new_index = NULL;
2712 struct got_pathlist_entry *pe;
2714 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2718 err = got_opentemp_named(&new_fileindex_path, &new_index,
2723 TAILQ_FOREACH(pe, commitable_paths, entry) {
2724 struct got_fileindex_entry *ie;
2725 struct commitable *ct = pe->data;
2727 ie = got_fileindex_entry_get(fileindex, pe->path);
2729 if (ct->status == GOT_STATUS_DELETE) {
2730 got_fileindex_entry_remove(fileindex, ie);
2731 got_fileindex_entry_free(ie);
2733 err = got_fileindex_entry_update(ie,
2734 ct->ondisk_path, ct->blob_id->sha1,
2735 new_base_commit_id->sha1, 1);
2737 err = got_fileindex_entry_alloc(&ie,
2738 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2739 new_base_commit_id->sha1);
2742 err = got_fileindex_entry_add(fileindex, ie);
2748 err = got_fileindex_write(fileindex, new_index);
2752 if (rename(new_fileindex_path, fileindex_path) != 0) {
2753 err = got_error_from_errno();
2754 unlink(new_fileindex_path);
2758 free(new_fileindex_path);
2759 new_fileindex_path = NULL;
2762 if (new_fileindex_path)
2763 unlink(new_fileindex_path);
2766 free(new_fileindex_path);
2767 free(fileindex_path);
2768 got_fileindex_free(fileindex);
2772 static const struct got_error *
2773 check_ct_out_of_date(struct commitable *ct, struct got_repository *repo,
2774 struct got_object_id *head_commit_id)
2776 const struct got_error *err = NULL;
2777 struct got_object_id *id_in_head;
2780 * XXX This should probably be checking each commit from
2781 * worktree's base_commit -> head and verify that the
2782 * same blob exists in each of these commits.
2783 * Removals+additions within this line of history could mean
2784 * that renames have occured in which case we should really
2785 * be forcing the user to run an update...
2787 err = got_object_id_by_path(&id_in_head, repo,
2788 head_commit_id, ct->in_repo_path);
2790 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2792 if (ct->status != GOT_STATUS_ADD)
2793 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2798 if (id_in_head && got_object_id_cmp(id_in_head, ct->base_id) != 0)
2799 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2805 const struct got_error *
2806 got_worktree_commit(struct got_object_id **new_commit_id,
2807 struct got_worktree *worktree, const char *ondisk_path,
2808 const char *author, const char *committer, const char *logmsg,
2809 got_worktree_status_cb status_cb, void *status_arg,
2810 struct got_repository *repo)
2812 const struct got_error *err = NULL, *unlockerr = NULL;
2813 struct collect_commitables_arg cc_arg;
2814 struct got_pathlist_head commitable_paths;
2815 struct got_pathlist_entry *pe;
2816 char *relpath = NULL;
2817 const char *head_ref_name = NULL;
2818 struct got_reference *head_ref = NULL;
2819 struct got_commit_object *head_commit = NULL;
2820 struct got_object_id *head_commit_id = NULL;
2821 struct got_reference *head_ref2 = NULL;
2822 struct got_object_id *head_commit_id2 = NULL;
2823 struct got_tree_object *head_tree = NULL;
2824 struct got_object_id *new_tree_id = NULL;
2825 struct got_object_id_queue parent_ids;
2826 struct got_object_qid *pid = NULL;
2828 *new_commit_id = NULL;
2830 TAILQ_INIT(&commitable_paths);
2831 SIMPLEQ_INIT(&parent_ids);
2834 err = got_path_skip_common_ancestor(&relpath,
2835 worktree->root_path, ondisk_path);
2840 err = lock_worktree(worktree, LOCK_EX);
2844 err = got_ref_open(&head_ref, repo, worktree->head_ref_name);
2847 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2851 cc_arg.commitable_paths = &commitable_paths;
2852 cc_arg.worktree = worktree;
2854 err = got_worktree_status(worktree, relpath ? relpath : "",
2855 repo, collect_commitables, &cc_arg, NULL, NULL);
2859 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2863 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2864 struct commitable *ct = pe->data;
2865 err = check_ct_out_of_date(ct, repo, head_commit_id);
2870 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2874 /* TODO: collect commit message if not specified */
2876 /* Create blobs from added and modified files and record their IDs. */
2877 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2878 struct commitable *ct = pe->data;
2881 if (ct->status != GOT_STATUS_ADD &&
2882 ct->status != GOT_STATUS_MODIFY)
2885 if (asprintf(&ondisk_path, "%s/%s",
2886 worktree->root_path, pe->path) == -1) {
2887 err = got_error_from_errno();
2890 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2896 /* Recursively write new tree objects. */
2897 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2898 status_cb, status_arg, repo);
2902 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2905 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2906 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2907 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2908 got_object_qid_free(pid);
2912 /* Check if a concurrent commit to our branch has occurred. */
2913 /* XXX ideally we'd lock the reference file here to avoid a race */
2914 head_ref_name = got_worktree_get_head_ref_name(worktree);
2915 if (head_ref_name == NULL) {
2916 err = got_error_from_errno();
2919 err = got_ref_open(&head_ref2, repo, head_ref_name);
2922 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
2925 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
2926 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
2929 /* Update branch head in repository. */
2930 err = got_ref_change_ref(head_ref, *new_commit_id);
2933 err = got_ref_write(head_ref, repo);
2936 /* XXX race has ended here */
2938 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
2942 err = ref_base_commit(worktree, repo);
2946 err = update_fileindex_after_commit(&commitable_paths,
2947 *new_commit_id, worktree);
2951 unlockerr = lock_worktree(worktree, LOCK_SH);
2952 if (unlockerr && err == NULL)
2954 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2955 struct commitable *ct = pe->data;
2956 free_commitable(ct);
2958 got_pathlist_free(&commitable_paths);
2960 got_object_tree_close(head_tree);
2962 got_object_commit_close(head_commit);
2964 free(head_commit_id);
2965 free(head_commit_id2);
2967 got_ref_close(head_ref);
2969 got_ref_close(head_ref2);