2 * Copyright (c) 2018, 2019, 2020 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/queue.h>
38 #include "got_error.h"
39 #include "got_repository.h"
40 #include "got_reference.h"
41 #include "got_object.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_opentemp.h"
48 #include "got_lib_worktree.h"
49 #include "got_lib_sha1.h"
50 #include "got_lib_fileindex.h"
51 #include "got_lib_inflate.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_parse.h"
55 #include "got_lib_object_create.h"
56 #include "got_lib_object_idset.h"
57 #include "got_lib_diff.h"
58 #include "got_lib_gotconfig.h"
61 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define GOT_MERGE_LABEL_MERGED "merged change"
65 #define GOT_MERGE_LABEL_BASE "3-way merge base"
67 static const struct got_error *
68 create_meta_file(const char *path_got, const char *name, const char *content)
70 const struct got_error *err = NULL;
73 if (asprintf(&path, "%s/%s", path_got, name) == -1)
74 return got_error_from_errno("asprintf");
76 err = got_path_create_file(path, content);
81 static const struct got_error *
82 update_meta_file(const char *path_got, const char *name, const char *content)
84 const struct got_error *err = NULL;
89 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
90 err = got_error_from_errno("asprintf");
95 err = got_opentemp_named(&tmppath, &tmpfile, path);
100 int len = fprintf(tmpfile, "%s\n", content);
101 if (len != strlen(content) + 1) {
102 err = got_error_from_errno2("fprintf", tmppath);
107 if (rename(tmppath, path) != 0) {
108 err = got_error_from_errno3("rename", tmppath, path);
114 if (fclose(tmpfile) == EOF && err == NULL)
115 err = got_error_from_errno2("fclose", tmppath);
120 static const struct got_error *
121 read_meta_file(char **content, const char *path_got, const char *name)
123 const struct got_error *err = NULL;
131 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
132 err = got_error_from_errno("asprintf");
137 fd = open(path, O_RDONLY | O_NOFOLLOW);
140 err = got_error_path(path, GOT_ERR_WORKTREE_META);
142 err = got_error_from_errno2("open", path);
145 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
146 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
147 : got_error_from_errno2("flock", path));
151 if (fstat(fd, &sb) != 0) {
152 err = got_error_from_errno2("fstat", path);
155 *content = calloc(1, sb.st_size);
156 if (*content == NULL) {
157 err = got_error_from_errno("calloc");
161 n = read(fd, *content, sb.st_size);
162 if (n != sb.st_size) {
163 err = (n == -1 ? got_error_from_errno2("read", path) :
164 got_error_path(path, GOT_ERR_WORKTREE_META));
167 if ((*content)[sb.st_size - 1] != '\n') {
168 err = got_error_path(path, GOT_ERR_WORKTREE_META);
171 (*content)[sb.st_size - 1] = '\0';
174 if (fd != -1 && close(fd) == -1 && err == NULL)
175 err = got_error_from_errno2("close", path_got);
184 static const struct got_error *
185 write_head_ref(const char *path_got, struct got_reference *head_ref)
187 const struct got_error *err = NULL;
190 if (got_ref_is_symbolic(head_ref)) {
191 refstr = got_ref_to_str(head_ref);
193 return got_error_from_errno("got_ref_to_str");
195 refstr = strdup(got_ref_get_name(head_ref));
197 return got_error_from_errno("strdup");
199 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 const struct got_error *
205 got_worktree_init(const char *path, struct got_reference *head_ref,
206 const char *prefix, struct got_repository *repo)
208 const struct got_error *err = NULL;
209 struct got_object_id *commit_id = NULL;
211 uint32_t uuid_status;
213 char *path_got = NULL;
214 char *formatstr = NULL;
215 char *absprefix = NULL;
216 char *basestr = NULL;
217 char *uuidstr = NULL;
219 if (strcmp(path, got_repo_get_path(repo)) == 0) {
220 err = got_error(GOT_ERR_WORKTREE_REPO);
224 err = got_ref_resolve(&commit_id, repo, head_ref);
227 err = got_object_get_type(&obj_type, repo, commit_id);
230 if (obj_type != GOT_OBJ_TYPE_COMMIT)
231 return got_error(GOT_ERR_OBJ_TYPE);
233 if (!got_path_is_absolute(prefix)) {
234 if (asprintf(&absprefix, "/%s", prefix) == -1)
235 return got_error_from_errno("asprintf");
238 /* Create top-level directory (may already exist). */
239 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
240 err = got_error_from_errno2("mkdir", path);
244 /* Create .got directory (may already exist). */
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error_from_errno("asprintf");
249 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
250 err = got_error_from_errno2("mkdir", path_got);
254 /* Create an empty lock file. */
255 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
259 /* Create an empty file index. */
260 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
264 /* Write the HEAD reference. */
265 err = write_head_ref(path_got, head_ref);
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, "uuid_create");
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status, "uuid_to_string");
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("asprintf");
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
323 static const struct got_error *
324 open_worktree(struct got_worktree **worktree, const char *path)
326 const struct got_error *err = NULL;
328 char *formatstr = NULL;
329 char *uuidstr = NULL;
330 char *path_lock = NULL;
331 char *base_commit_id_str = NULL;
332 int version, fd = -1;
334 struct got_repository *repo = NULL;
335 uint32_t uuid_status;
339 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
340 err = got_error_from_errno("asprintf");
345 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
346 err = got_error_from_errno("asprintf");
351 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno2("open", path_lock));
358 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
362 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 err = got_error_msg(GOT_ERR_WORKTREE_META,
365 "could not parse work tree format version number");
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("calloc");
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = realpath(path, NULL);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno2("realpath", path);
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, "uuid_from_string");
409 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
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);
423 if (asprintf(&(*worktree)->gotconfig_path, "%s/%s/%s",
424 (*worktree)->root_path,
425 GOT_WORKTREE_GOT_DIR, GOT_GOTCONFIG_FILENAME) == -1) {
426 err = got_error_from_errno("asprintf");
430 err = got_gotconfig_read(&(*worktree)->gotconfig,
431 (*worktree)->gotconfig_path);
433 (*worktree)->root_fd = open((*worktree)->root_path, O_DIRECTORY);
434 if ((*worktree)->root_fd == -1) {
435 err = got_error_from_errno2("open", (*worktree)->root_path);
440 got_repo_close(repo);
443 free(base_commit_id_str);
449 if (*worktree != NULL)
450 got_worktree_close(*worktree);
453 (*worktree)->lockfd = fd;
458 const struct got_error *
459 got_worktree_open(struct got_worktree **worktree, const char *path)
461 const struct got_error *err = NULL;
464 worktree_path = strdup(path);
465 if (worktree_path == NULL)
466 return got_error_from_errno("strdup");
471 err = open_worktree(worktree, worktree_path);
472 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) {
480 if (worktree_path[0] == '/' && worktree_path[1] == '\0')
482 err = got_path_dirname(&parent_path, worktree_path);
484 if (err->code != GOT_ERR_BAD_PATH) {
491 worktree_path = parent_path;
495 return got_error(GOT_ERR_NOT_WORKTREE);
498 const struct got_error *
499 got_worktree_close(struct got_worktree *worktree)
501 const struct got_error *err = NULL;
502 free(worktree->repo_path);
503 free(worktree->path_prefix);
504 free(worktree->base_commit_id);
505 free(worktree->head_ref_name);
506 if (worktree->lockfd != -1) {
507 if (close(worktree->lockfd) == -1)
508 err = got_error_from_errno2("close",
509 got_worktree_get_root_path(worktree));
511 free(worktree->root_path);
512 free(worktree->gotconfig_path);
513 got_gotconfig_free(worktree->gotconfig);
514 if (close(worktree->root_fd) == -1 && err == NULL)
515 err = got_error_from_errno2("close",
516 got_worktree_get_root_path(worktree));
522 got_worktree_get_root_path(struct got_worktree *worktree)
524 return worktree->root_path;
528 got_worktree_get_repo_path(struct got_worktree *worktree)
530 return worktree->repo_path;
533 got_worktree_get_path_prefix(struct got_worktree *worktree)
535 return worktree->path_prefix;
538 const struct got_error *
539 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
540 const char *path_prefix)
542 char *absprefix = NULL;
544 if (!got_path_is_absolute(path_prefix)) {
545 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
546 return got_error_from_errno("asprintf");
548 *match = (strcmp(absprefix ? absprefix : path_prefix,
549 worktree->path_prefix) == 0);
555 got_worktree_get_head_ref_name(struct got_worktree *worktree)
557 return worktree->head_ref_name;
560 const struct got_error *
561 got_worktree_set_head_ref(struct got_worktree *worktree,
562 struct got_reference *head_ref)
564 const struct got_error *err = NULL;
565 char *path_got = NULL, *head_ref_name = NULL;
567 if (asprintf(&path_got, "%s/%s", worktree->root_path,
568 GOT_WORKTREE_GOT_DIR) == -1) {
569 err = got_error_from_errno("asprintf");
574 head_ref_name = strdup(got_ref_get_name(head_ref));
575 if (head_ref_name == NULL) {
576 err = got_error_from_errno("strdup");
580 err = write_head_ref(path_got, head_ref);
584 free(worktree->head_ref_name);
585 worktree->head_ref_name = head_ref_name;
593 struct got_object_id *
594 got_worktree_get_base_commit_id(struct got_worktree *worktree)
596 return worktree->base_commit_id;
599 const struct got_error *
600 got_worktree_set_base_commit_id(struct got_worktree *worktree,
601 struct got_repository *repo, struct got_object_id *commit_id)
603 const struct got_error *err;
604 struct got_object *obj = NULL;
606 char *path_got = NULL;
608 if (asprintf(&path_got, "%s/%s", worktree->root_path,
609 GOT_WORKTREE_GOT_DIR) == -1) {
610 err = got_error_from_errno("asprintf");
615 err = got_object_open(&obj, repo, commit_id);
619 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
620 err = got_error(GOT_ERR_OBJ_TYPE);
624 /* Record our base commit. */
625 err = got_object_id_str(&id_str, commit_id);
628 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
632 free(worktree->base_commit_id);
633 worktree->base_commit_id = got_object_id_dup(commit_id);
634 if (worktree->base_commit_id == NULL) {
635 err = got_error_from_errno("got_object_id_dup");
640 got_object_close(obj);
646 const struct got_gotconfig *
647 got_worktree_get_gotconfig(struct got_worktree *worktree)
649 return worktree->gotconfig;
652 static const struct got_error *
653 lock_worktree(struct got_worktree *worktree, int operation)
655 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
656 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
657 : got_error_from_errno2("flock",
658 got_worktree_get_root_path(worktree)));
662 static const struct got_error *
663 add_dir_on_disk(struct got_worktree *worktree, const char *path)
665 const struct got_error *err = NULL;
668 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
669 return got_error_from_errno("asprintf");
671 err = got_path_mkdir(abspath);
672 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
675 if (lstat(abspath, &sb) == -1) {
676 err = got_error_from_errno2("lstat", abspath);
677 } else if (!S_ISDIR(sb.st_mode)) {
678 /* TODO directory is obstructed; do something */
679 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
686 static const struct got_error *
687 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
689 const struct got_error *err = NULL;
692 size_t flen1 = 0, flen2 = 0;
697 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
698 if (flen1 == 0 && ferror(f1)) {
699 err = got_error_from_errno("fread");
702 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
703 if (flen2 == 0 && ferror(f2)) {
704 err = got_error_from_errno("fread");
711 } else if (flen2 == 0) {
715 } else if (flen1 == flen2) {
716 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
729 static const struct got_error *
730 check_files_equal(int *same, const char *f1_path, const char *f2_path)
732 const struct got_error *err = NULL;
735 FILE *f1 = NULL, *f2 = NULL;
739 if (lstat(f1_path, &sb) != 0) {
740 err = got_error_from_errno2("lstat", f1_path);
745 if (lstat(f2_path, &sb) != 0) {
746 err = got_error_from_errno2("lstat", f2_path);
751 if (size1 != size2) {
756 f1 = fopen(f1_path, "r");
758 return got_error_from_errno2("fopen", f1_path);
760 f2 = fopen(f2_path, "r");
762 err = got_error_from_errno2("fopen", f2_path);
766 err = check_file_contents_equal(same, f1, f2);
768 if (f1 && fclose(f1) == EOF && err == NULL)
769 err = got_error_from_errno("fclose");
770 if (f2 && fclose(f2) == EOF && err == NULL)
771 err = got_error_from_errno("fclose");
777 * Perform a 3-way merge where blob_orig acts as the common ancestor,
778 * the file at deriv_path acts as the first derived version, and the
779 * file on disk acts as the second derived version.
781 static const struct got_error *
782 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
783 struct got_blob_object *blob_orig, const char *ondisk_path,
784 const char *path, uint16_t st_mode, const char *deriv_path,
785 const char *label_orig, const char *label_deriv,
786 struct got_repository *repo,
787 got_worktree_checkout_cb progress_cb, void *progress_arg)
789 const struct got_error *err = NULL;
792 char *blob_orig_path = NULL;
793 char *merged_path = NULL, *base_path = NULL;
796 char *symlink_path = NULL;
797 FILE *symlinkf = NULL;
799 *local_changes_subsumed = 0;
801 err = got_path_dirname(&parent, ondisk_path);
805 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
806 err = got_error_from_errno("asprintf");
810 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
815 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
816 err = got_error_from_errno("asprintf");
821 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
825 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
831 * If the file has no blob, this is an "add vs add" conflict,
832 * and we simply use an empty ancestor file to make both files
833 * appear in the merged result in their entirety.
838 * In order the run a 3-way merge with a symlink we copy the symlink's
839 * target path into a temporary file and use that file with diff3.
841 if (S_ISLNK(st_mode)) {
842 char target_path[PATH_MAX];
847 if (asprintf(&base_path, "%s/got-symlink-merge",
849 err = got_error_from_errno("asprintf");
853 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
856 target_len = readlink(ondisk_path, target_path,
857 sizeof(target_path));
858 if (target_len == -1) {
859 err = got_error_from_errno2("readlink", ondisk_path);
862 n = fwrite(target_path, 1, target_len, symlinkf);
863 if (n != target_len) {
864 err = got_ferror(symlinkf, GOT_ERR_IO);
867 if (fflush(symlinkf) == EOF) {
868 err = got_error_from_errno2("fflush", symlink_path);
873 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
874 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
875 label_deriv, label_orig, NULL);
879 err = (*progress_cb)(progress_arg,
880 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
884 if (fsync(merged_fd) != 0) {
885 err = got_error_from_errno("fsync");
889 /* Check if a clean merge has subsumed all local changes. */
890 if (overlapcnt == 0) {
891 err = check_files_equal(local_changes_subsumed, deriv_path,
897 if (fchmod(merged_fd, st_mode) != 0) {
898 err = got_error_from_errno2("fchmod", merged_path);
902 if (rename(merged_path, ondisk_path) != 0) {
903 err = got_error_from_errno3("rename", merged_path,
913 if (unlink(symlink_path) == -1 && err == NULL)
914 err = got_error_from_errno2("unlink", symlink_path);
916 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
917 err = got_error_from_errno2("fclose", symlink_path);
919 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
920 err = got_error_from_errno("close");
921 if (f_orig && fclose(f_orig) == EOF && err == NULL)
922 err = got_error_from_errno("fclose");
925 if (blob_orig_path) {
926 unlink(blob_orig_path);
927 free(blob_orig_path);
933 static const struct got_error *
934 update_symlink(const char *ondisk_path, const char *target_path,
937 /* This is not atomic but matches what 'ln -sf' does. */
938 if (unlink(ondisk_path) == -1)
939 return got_error_from_errno2("unlink", ondisk_path);
940 if (symlink(target_path, ondisk_path) == -1)
941 return got_error_from_errno3("symlink", target_path,
947 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
948 * in the work tree with a file that contains conflict markers and the
949 * conflicting target paths of the original version, a "derived version"
950 * of a symlink from an incoming change, and a local version of the symlink.
952 * The original versions's target path can be NULL if it is not available,
953 * such as if both derived versions added a new symlink at the same path.
955 * The incoming derived symlink target is NULL in case the incoming change
956 * has deleted this symlink.
958 static const struct got_error *
959 install_symlink_conflict(const char *deriv_target,
960 struct got_object_id *deriv_base_commit_id, const char *orig_target,
961 const char *label_orig, const char *local_target, const char *ondisk_path)
963 const struct got_error *err;
964 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
967 err = got_object_id_str(&id_str, deriv_base_commit_id);
969 return got_error_from_errno("asprintf");
971 if (asprintf(&label_deriv, "%s: commit %s",
972 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
973 err = got_error_from_errno("asprintf");
977 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
981 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
982 err = got_error_from_errno2("fchmod", path);
986 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
987 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
988 deriv_target ? deriv_target : "(symlink was deleted)",
989 orig_target ? label_orig : "",
990 orig_target ? "\n" : "",
991 orig_target ? orig_target : "",
992 orig_target ? "\n" : "",
993 GOT_DIFF_CONFLICT_MARKER_SEP,
994 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
995 err = got_error_from_errno2("fprintf", path);
999 if (unlink(ondisk_path) == -1) {
1000 err = got_error_from_errno2("unlink", ondisk_path);
1003 if (rename(path, ondisk_path) == -1) {
1004 err = got_error_from_errno3("rename", path, ondisk_path);
1008 if (f != NULL && fclose(f) == EOF && err == NULL)
1009 err = got_error_from_errno2("fclose", path);
1016 /* forward declaration */
1017 static const struct got_error *
1018 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1019 const char *, const char *, uint16_t, const char *,
1020 struct got_blob_object *, struct got_object_id *,
1021 struct got_repository *, got_worktree_checkout_cb, void *);
1024 * Merge a symlink into the work tree, where blob_orig acts as the common
1025 * ancestor, deriv_target is the link target of the first derived version,
1026 * and the symlink on disk acts as the second derived version.
1027 * Assume that contents of both blobs represent symlinks.
1029 static const struct got_error *
1030 merge_symlink(struct got_worktree *worktree,
1031 struct got_blob_object *blob_orig, const char *ondisk_path,
1032 const char *path, const char *label_orig, const char *deriv_target,
1033 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1034 got_worktree_checkout_cb progress_cb, void *progress_arg)
1036 const struct got_error *err = NULL;
1037 char *ancestor_target = NULL;
1039 ssize_t ondisk_len, deriv_len;
1040 char ondisk_target[PATH_MAX];
1041 int have_local_change = 0;
1042 int have_incoming_change = 0;
1044 if (lstat(ondisk_path, &sb) == -1)
1045 return got_error_from_errno2("lstat", ondisk_path);
1047 ondisk_len = readlink(ondisk_path, ondisk_target,
1048 sizeof(ondisk_target));
1049 if (ondisk_len == -1) {
1050 err = got_error_from_errno2("readlink",
1054 ondisk_target[ondisk_len] = '\0';
1057 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1062 if (ancestor_target == NULL ||
1063 (ondisk_len != strlen(ancestor_target) ||
1064 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1065 have_local_change = 1;
1067 deriv_len = strlen(deriv_target);
1068 if (ancestor_target == NULL ||
1069 (deriv_len != strlen(ancestor_target) ||
1070 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1071 have_incoming_change = 1;
1073 if (!have_local_change && !have_incoming_change) {
1074 if (ancestor_target) {
1075 /* Both sides made the same change. */
1076 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1078 } else if (deriv_len == ondisk_len &&
1079 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1080 /* Both sides added the same symlink. */
1081 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1084 /* Both sides added symlinks which don't match. */
1085 err = install_symlink_conflict(deriv_target,
1086 deriv_base_commit_id, ancestor_target,
1087 label_orig, ondisk_target, ondisk_path);
1090 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1093 } else if (!have_local_change && have_incoming_change) {
1094 /* Apply the incoming change. */
1095 err = update_symlink(ondisk_path, deriv_target,
1096 strlen(deriv_target));
1099 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1100 } else if (have_local_change && have_incoming_change) {
1101 if (deriv_len == ondisk_len &&
1102 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1103 /* Both sides made the same change. */
1104 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1107 err = install_symlink_conflict(deriv_target,
1108 deriv_base_commit_id, ancestor_target, label_orig,
1109 ondisk_target, ondisk_path);
1112 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1118 free(ancestor_target);
1123 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1124 * blob_deriv acts as the first derived version, and the file on disk
1125 * acts as the second derived version.
1127 static const struct got_error *
1128 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1129 struct got_blob_object *blob_orig, const char *ondisk_path,
1130 const char *path, uint16_t st_mode, const char *label_orig,
1131 struct got_blob_object *blob_deriv,
1132 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1133 got_worktree_checkout_cb progress_cb, void *progress_arg)
1135 const struct got_error *err = NULL;
1136 FILE *f_deriv = NULL;
1137 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1138 char *label_deriv = NULL, *parent = NULL;
1140 *local_changes_subsumed = 0;
1142 err = got_path_dirname(&parent, ondisk_path);
1147 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1148 err = got_error_from_errno("asprintf");
1153 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1156 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1161 err = got_object_id_str(&id_str, deriv_base_commit_id);
1164 if (asprintf(&label_deriv, "%s: commit %s",
1165 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1166 err = got_error_from_errno("asprintf");
1170 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1171 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1172 label_deriv, repo, progress_cb, progress_arg);
1174 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1175 err = got_error_from_errno("fclose");
1177 if (blob_deriv_path) {
1178 unlink(blob_deriv_path);
1179 free(blob_deriv_path);
1187 static const struct got_error *
1188 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1189 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1190 int wt_fd, const char *path, struct got_object_id *blob_id)
1192 const struct got_error *err = NULL;
1193 struct got_fileindex_entry *new_ie;
1197 err = got_fileindex_entry_alloc(&new_ie, path);
1201 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1202 blob_id->sha1, base_commit_id->sha1, 1);
1206 err = got_fileindex_entry_add(fileindex, new_ie);
1209 got_fileindex_entry_free(new_ie);
1216 get_ondisk_perms(int executable, mode_t st_mode)
1218 mode_t xbits = S_IXUSR;
1221 /* Map read bits to execute bits. */
1222 if (st_mode & S_IRGRP)
1224 if (st_mode & S_IROTH)
1226 return st_mode | xbits;
1229 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1232 /* forward declaration */
1233 static const struct got_error *
1234 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1235 const char *path, mode_t te_mode, mode_t st_mode,
1236 struct got_blob_object *blob, int restoring_missing_file,
1237 int reverting_versioned_file, int installing_bad_symlink,
1238 int path_is_unversioned, struct got_repository *repo,
1239 got_worktree_checkout_cb progress_cb, void *progress_arg);
1242 * This function assumes that the provided symlink target points at a
1243 * safe location in the work tree!
1245 static const struct got_error *
1246 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1249 const struct got_error *err = NULL;
1251 char etarget[PATH_MAX];
1255 * "Bad" symlinks (those pointing outside the work tree or into the
1256 * .got directory) are installed in the work tree as a regular file
1257 * which contains the bad symlink target path.
1258 * The new symlink target has already been checked for safety by our
1259 * caller. If we can successfully open a regular file then we simply
1260 * replace this file with a symlink below.
1262 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1265 return got_error_from_errno2("open", ondisk_path);
1267 /* We are updating an existing on-disk symlink. */
1268 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1270 return got_error_from_errno2("readlink", ondisk_path);
1272 if (elen == target_len &&
1273 memcmp(etarget, target_path, target_len) == 0)
1274 return NULL; /* nothing to do */
1277 err = update_symlink(ondisk_path, target_path, target_len);
1278 if (fd != -1 && close(fd) == -1 && err == NULL)
1279 err = got_error_from_errno2("close", ondisk_path);
1283 static const struct got_error *
1284 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1285 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1287 const struct got_error *err = NULL;
1288 char canonpath[PATH_MAX];
1289 char *path_got = NULL;
1291 *is_bad_symlink = 0;
1293 if (target_len >= sizeof(canonpath)) {
1294 *is_bad_symlink = 1;
1299 * We do not use realpath(3) to resolve the symlink's target
1300 * path because we don't want to resolve symlinks recursively.
1301 * Instead we make the path absolute and then canonicalize it.
1302 * Relative symlink target lookup should begin at the directory
1303 * in which the blob object is being installed.
1305 if (!got_path_is_absolute(target_path)) {
1306 char *abspath, *parent;
1307 err = got_path_dirname(&parent, ondisk_path);
1310 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1312 return got_error_from_errno("asprintf");
1315 if (strlen(abspath) >= sizeof(canonpath)) {
1316 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1320 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1325 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1330 /* Only allow symlinks pointing at paths within the work tree. */
1331 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1332 *is_bad_symlink = 1;
1336 /* Do not allow symlinks pointing into the .got directory. */
1337 if (asprintf(&path_got, "%s/%s", wtroot_path,
1338 GOT_WORKTREE_GOT_DIR) == -1)
1339 return got_error_from_errno("asprintf");
1340 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1341 *is_bad_symlink = 1;
1347 static const struct got_error *
1348 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1349 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1350 int restoring_missing_file, int reverting_versioned_file,
1351 int path_is_unversioned, struct got_repository *repo,
1352 got_worktree_checkout_cb progress_cb, void *progress_arg)
1354 const struct got_error *err = NULL;
1355 char target_path[PATH_MAX];
1356 size_t len, target_len = 0;
1357 char *path_got = NULL;
1358 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1359 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1361 *is_bad_symlink = 0;
1364 * Blob object content specifies the target path of the link.
1365 * If a symbolic link cannot be installed we instead create
1366 * a regular file which contains the link target path stored
1367 * in the blob object.
1370 err = got_object_blob_read_block(&len, blob);
1371 if (len + target_len >= sizeof(target_path)) {
1372 /* Path too long; install as a regular file. */
1373 *is_bad_symlink = 1;
1374 got_object_blob_rewind(blob);
1375 return install_blob(worktree, ondisk_path, path,
1376 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1377 restoring_missing_file, reverting_versioned_file,
1378 1, path_is_unversioned, repo, progress_cb,
1382 /* Skip blob object header first time around. */
1383 memcpy(target_path + target_len, buf + hdrlen,
1385 target_len += len - hdrlen;
1389 target_path[target_len] = '\0';
1391 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1392 ondisk_path, worktree->root_path);
1396 if (*is_bad_symlink) {
1397 /* install as a regular file */
1398 *is_bad_symlink = 1;
1399 got_object_blob_rewind(blob);
1400 err = install_blob(worktree, ondisk_path, path,
1401 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1402 restoring_missing_file, reverting_versioned_file, 1,
1403 path_is_unversioned, repo, progress_cb, progress_arg);
1407 if (symlink(target_path, ondisk_path) == -1) {
1408 if (errno == EEXIST) {
1409 if (path_is_unversioned) {
1410 err = (*progress_cb)(progress_arg,
1411 GOT_STATUS_UNVERSIONED, path);
1414 err = replace_existing_symlink(ondisk_path,
1415 target_path, target_len);
1419 err = (*progress_cb)(progress_arg,
1420 reverting_versioned_file ?
1421 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1424 goto done; /* Nothing else to do. */
1427 if (errno == ENOENT) {
1429 err = got_path_dirname(&parent, ondisk_path);
1432 err = add_dir_on_disk(worktree, parent);
1437 * Retry, and fall through to error handling
1438 * below if this second attempt fails.
1440 if (symlink(target_path, ondisk_path) != -1) {
1441 err = NULL; /* success */
1446 /* Handle errors from first or second creation attempt. */
1447 if (errno == ENAMETOOLONG) {
1448 /* bad target path; install as a regular file */
1449 *is_bad_symlink = 1;
1450 got_object_blob_rewind(blob);
1451 err = install_blob(worktree, ondisk_path, path,
1452 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1453 restoring_missing_file, reverting_versioned_file, 1,
1454 path_is_unversioned, repo,
1455 progress_cb, progress_arg);
1456 } else if (errno == ENOTDIR) {
1457 err = got_error_path(ondisk_path,
1458 GOT_ERR_FILE_OBSTRUCTED);
1460 err = got_error_from_errno3("symlink",
1461 target_path, ondisk_path);
1463 } else if (progress_cb)
1464 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1465 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1471 static const struct got_error *
1472 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1473 const char *path, mode_t te_mode, mode_t st_mode,
1474 struct got_blob_object *blob, int restoring_missing_file,
1475 int reverting_versioned_file, int installing_bad_symlink,
1476 int path_is_unversioned, struct got_repository *repo,
1477 got_worktree_checkout_cb progress_cb, void *progress_arg)
1479 const struct got_error *err = NULL;
1483 char *tmppath = NULL;
1485 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1486 GOT_DEFAULT_FILE_MODE);
1488 if (errno == ENOENT) {
1490 err = got_path_dirname(&parent, path);
1493 err = add_dir_on_disk(worktree, parent);
1497 fd = open(ondisk_path,
1498 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1499 GOT_DEFAULT_FILE_MODE);
1501 return got_error_from_errno2("open",
1503 } else if (errno == EEXIST) {
1504 if (path_is_unversioned) {
1505 err = (*progress_cb)(progress_arg,
1506 GOT_STATUS_UNVERSIONED, path);
1509 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1510 !S_ISREG(st_mode) && !installing_bad_symlink) {
1511 /* TODO file is obstructed; do something */
1512 err = got_error_path(ondisk_path,
1513 GOT_ERR_FILE_OBSTRUCTED);
1516 err = got_opentemp_named_fd(&tmppath, &fd,
1523 return got_error_from_errno2("open", ondisk_path);
1526 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1527 err = got_error_from_errno2("fchmod",
1528 update ? tmppath : ondisk_path);
1533 if (restoring_missing_file)
1534 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1536 else if (reverting_versioned_file)
1537 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1540 err = (*progress_cb)(progress_arg,
1541 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1546 hdrlen = got_object_blob_get_hdrlen(blob);
1548 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1549 err = got_object_blob_read_block(&len, blob);
1553 /* Skip blob object header first time around. */
1554 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1556 err = got_error_from_errno("write");
1558 } else if (outlen != len - hdrlen) {
1559 err = got_error(GOT_ERR_IO);
1566 if (fsync(fd) != 0) {
1567 err = got_error_from_errno("fsync");
1572 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1573 err = got_error_from_errno2("unlink", ondisk_path);
1576 if (rename(tmppath, ondisk_path) != 0) {
1577 err = got_error_from_errno3("rename", tmppath,
1586 if (fd != -1 && close(fd) == -1 && err == NULL)
1587 err = got_error_from_errno("close");
1588 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1589 err = got_error_from_errno2("unlink", tmppath);
1594 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1595 static const struct got_error *
1596 get_modified_file_content_status(unsigned char *status, FILE *f)
1598 const struct got_error *err = NULL;
1599 const char *markers[3] = {
1600 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1601 GOT_DIFF_CONFLICT_MARKER_SEP,
1602 GOT_DIFF_CONFLICT_MARKER_END
1606 size_t linesize = 0;
1609 while (*status == GOT_STATUS_MODIFY) {
1610 linelen = getline(&line, &linesize, f);
1611 if (linelen == -1) {
1614 err = got_ferror(f, GOT_ERR_IO);
1618 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1619 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1621 *status = GOT_STATUS_CONFLICT;
1632 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1634 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1635 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1639 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1641 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1642 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1643 ie->mtime_sec == sb->st_mtim.tv_sec &&
1644 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1645 ie->size == (sb->st_size & 0xffffffff) &&
1646 !xbit_differs(ie, sb->st_mode));
1649 static unsigned char
1650 get_staged_status(struct got_fileindex_entry *ie)
1652 switch (got_fileindex_entry_stage_get(ie)) {
1653 case GOT_FILEIDX_STAGE_ADD:
1654 return GOT_STATUS_ADD;
1655 case GOT_FILEIDX_STAGE_DELETE:
1656 return GOT_STATUS_DELETE;
1657 case GOT_FILEIDX_STAGE_MODIFY:
1658 return GOT_STATUS_MODIFY;
1660 return GOT_STATUS_NO_CHANGE;
1664 static const struct got_error *
1665 get_symlink_modification_status(unsigned char *status,
1666 struct got_fileindex_entry *ie, const char *abspath,
1667 int dirfd, const char *de_name, struct got_blob_object *blob)
1669 const struct got_error *err = NULL;
1670 char target_path[PATH_MAX];
1671 char etarget[PATH_MAX];
1673 size_t len, target_len = 0;
1674 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1675 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1677 *status = GOT_STATUS_NO_CHANGE;
1679 /* Blob object content specifies the target path of the link. */
1681 err = got_object_blob_read_block(&len, blob);
1684 if (len + target_len >= sizeof(target_path)) {
1686 * Should not happen. The blob contents were OK
1687 * when this symlink was installed.
1689 return got_error(GOT_ERR_NO_SPACE);
1692 /* Skip blob object header first time around. */
1693 memcpy(target_path + target_len, buf + hdrlen,
1695 target_len += len - hdrlen;
1699 target_path[target_len] = '\0';
1702 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1704 return got_error_from_errno2("readlinkat", abspath);
1706 elen = readlink(abspath, etarget, sizeof(etarget));
1708 return got_error_from_errno2("readlink", abspath);
1711 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1712 *status = GOT_STATUS_MODIFY;
1717 static const struct got_error *
1718 get_file_status(unsigned char *status, struct stat *sb,
1719 struct got_fileindex_entry *ie, const char *abspath,
1720 int dirfd, const char *de_name, struct got_repository *repo)
1722 const struct got_error *err = NULL;
1723 struct got_object_id id;
1728 struct got_blob_object *blob = NULL;
1730 unsigned char staged_status = get_staged_status(ie);
1732 *status = GOT_STATUS_NO_CHANGE;
1735 * Whenever the caller provides a directory descriptor and a
1736 * directory entry name for the file, use them! This prevents
1737 * race conditions if filesystem paths change beneath our feet.
1740 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1741 if (errno == ENOENT) {
1742 if (got_fileindex_entry_has_file_on_disk(ie))
1743 *status = GOT_STATUS_MISSING;
1745 *status = GOT_STATUS_DELETE;
1748 err = got_error_from_errno2("fstatat", abspath);
1752 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1753 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1754 return got_error_from_errno2("open", abspath);
1755 else if (fd == -1 && errno == ELOOP) {
1756 if (lstat(abspath, sb) == -1)
1757 return got_error_from_errno2("lstat", abspath);
1758 } else if (fd == -1 || fstat(fd, sb) == -1) {
1759 if (errno == ENOENT) {
1760 if (got_fileindex_entry_has_file_on_disk(ie))
1761 *status = GOT_STATUS_MISSING;
1763 *status = GOT_STATUS_DELETE;
1766 err = got_error_from_errno2("fstat", abspath);
1771 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1772 *status = GOT_STATUS_OBSTRUCTED;
1776 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1777 *status = GOT_STATUS_DELETE;
1779 } else if (!got_fileindex_entry_has_blob(ie) &&
1780 staged_status != GOT_STATUS_ADD) {
1781 *status = GOT_STATUS_ADD;
1785 if (!stat_info_differs(ie, sb))
1788 if (S_ISLNK(sb->st_mode) &&
1789 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1790 *status = GOT_STATUS_MODIFY;
1794 if (staged_status == GOT_STATUS_MODIFY ||
1795 staged_status == GOT_STATUS_ADD)
1796 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1798 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1800 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1804 if (S_ISLNK(sb->st_mode)) {
1805 err = get_symlink_modification_status(status, ie,
1806 abspath, dirfd, de_name, blob);
1811 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1813 err = got_error_from_errno2("openat", abspath);
1818 f = fdopen(fd, "r");
1820 err = got_error_from_errno2("fdopen", abspath);
1824 hdrlen = got_object_blob_get_hdrlen(blob);
1826 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1827 err = got_object_blob_read_block(&blen, blob);
1830 /* Skip length of blob object header first time around. */
1831 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1832 if (flen == 0 && ferror(f)) {
1833 err = got_error_from_errno("fread");
1836 if (blen - hdrlen == 0) {
1838 *status = GOT_STATUS_MODIFY;
1840 } else if (flen == 0) {
1841 if (blen - hdrlen != 0)
1842 *status = GOT_STATUS_MODIFY;
1844 } else if (blen - hdrlen == flen) {
1845 /* Skip blob object header first time around. */
1846 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1847 *status = GOT_STATUS_MODIFY;
1851 *status = GOT_STATUS_MODIFY;
1857 if (*status == GOT_STATUS_MODIFY) {
1859 err = get_modified_file_content_status(status, f);
1860 } else if (xbit_differs(ie, sb->st_mode))
1861 *status = GOT_STATUS_MODE_CHANGE;
1864 got_object_blob_close(blob);
1865 if (f != NULL && fclose(f) == EOF && err == NULL)
1866 err = got_error_from_errno2("fclose", abspath);
1867 if (fd != -1 && close(fd) == -1 && err == NULL)
1868 err = got_error_from_errno2("close", abspath);
1873 * Update timestamps in the file index if a file is unmodified and
1874 * we had to run a full content comparison to find out.
1876 static const struct got_error *
1877 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1878 struct got_fileindex_entry *ie, struct stat *sb)
1880 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1881 return got_fileindex_entry_update(ie, wt_fd, path,
1882 ie->blob_sha1, ie->commit_sha1, 1);
1887 static const struct got_error *
1888 update_blob(struct got_worktree *worktree,
1889 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1890 struct got_tree_entry *te, const char *path,
1891 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1894 const struct got_error *err = NULL;
1895 struct got_blob_object *blob = NULL;
1897 unsigned char status = GOT_STATUS_NO_CHANGE;
1900 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1901 return got_error_from_errno("asprintf");
1904 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1905 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1908 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1912 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1913 sb.st_mode = got_fileindex_perms_to_st(ie);
1915 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1916 status = GOT_STATUS_UNVERSIONED;
1919 if (status == GOT_STATUS_OBSTRUCTED) {
1920 err = (*progress_cb)(progress_arg, status, path);
1923 if (status == GOT_STATUS_CONFLICT) {
1924 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1929 if (ie && status != GOT_STATUS_MISSING &&
1930 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1931 if (got_fileindex_entry_has_commit(ie) &&
1932 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1933 SHA1_DIGEST_LENGTH) == 0) {
1934 err = sync_timestamps(worktree->root_fd,
1935 path, status, ie, &sb);
1938 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1942 if (got_fileindex_entry_has_blob(ie) &&
1943 memcmp(ie->blob_sha1, te->id.sha1,
1944 SHA1_DIGEST_LENGTH) == 0) {
1945 err = sync_timestamps(worktree->root_fd,
1946 path, status, ie, &sb);
1951 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1955 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1956 int update_timestamps;
1957 struct got_blob_object *blob2 = NULL;
1958 char *label_orig = NULL;
1959 if (got_fileindex_entry_has_blob(ie)) {
1960 struct got_object_id id2;
1961 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1962 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1966 if (got_fileindex_entry_has_commit(ie)) {
1967 char id_str[SHA1_DIGEST_STRING_LENGTH];
1968 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1969 sizeof(id_str)) == NULL) {
1970 err = got_error_path(id_str,
1971 GOT_ERR_BAD_OBJ_ID_STR);
1974 if (asprintf(&label_orig, "%s: commit %s",
1975 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1976 err = got_error_from_errno("asprintf");
1980 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1982 err = got_object_blob_read_to_str(&link_target, blob);
1985 err = merge_symlink(worktree, blob2, ondisk_path, path,
1986 label_orig, link_target, worktree->base_commit_id,
1987 repo, progress_cb, progress_arg);
1990 err = merge_blob(&update_timestamps, worktree, blob2,
1991 ondisk_path, path, sb.st_mode, label_orig, blob,
1992 worktree->base_commit_id, repo,
1993 progress_cb, progress_arg);
1997 got_object_blob_close(blob2);
2001 * Do not update timestamps of files with local changes.
2002 * Otherwise, a future status walk would treat them as
2003 * unmodified files again.
2005 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2006 blob->id.sha1, worktree->base_commit_id->sha1,
2008 } else if (status == GOT_STATUS_MODE_CHANGE) {
2009 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2010 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2011 } else if (status == GOT_STATUS_DELETE) {
2012 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2015 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2016 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2020 int is_bad_symlink = 0;
2021 if (S_ISLNK(te->mode)) {
2022 err = install_symlink(&is_bad_symlink, worktree,
2023 ondisk_path, path, blob,
2024 status == GOT_STATUS_MISSING, 0,
2025 status == GOT_STATUS_UNVERSIONED, repo,
2026 progress_cb, progress_arg);
2028 err = install_blob(worktree, ondisk_path, path,
2029 te->mode, sb.st_mode, blob,
2030 status == GOT_STATUS_MISSING, 0, 0,
2031 status == GOT_STATUS_UNVERSIONED, repo,
2032 progress_cb, progress_arg);
2038 err = got_fileindex_entry_update(ie,
2039 worktree->root_fd, path, blob->id.sha1,
2040 worktree->base_commit_id->sha1, 1);
2042 err = create_fileindex_entry(&ie, fileindex,
2043 worktree->base_commit_id, worktree->root_fd, path,
2049 if (is_bad_symlink) {
2050 got_fileindex_entry_filetype_set(ie,
2051 GOT_FILEIDX_MODE_BAD_SYMLINK);
2054 got_object_blob_close(blob);
2060 static const struct got_error *
2061 remove_ondisk_file(const char *root_path, const char *path)
2063 const struct got_error *err = NULL;
2064 char *ondisk_path = NULL;
2066 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2067 return got_error_from_errno("asprintf");
2069 if (unlink(ondisk_path) == -1) {
2070 if (errno != ENOENT)
2071 err = got_error_from_errno2("unlink", ondisk_path);
2073 size_t root_len = strlen(root_path);
2076 err = got_path_dirname(&parent, ondisk_path);
2080 ondisk_path = parent;
2081 if (rmdir(ondisk_path) == -1) {
2082 if (errno != ENOTEMPTY)
2083 err = got_error_from_errno2("rmdir",
2087 } while (got_path_cmp(ondisk_path, root_path,
2088 strlen(ondisk_path), root_len) != 0);
2094 static const struct got_error *
2095 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2096 struct got_fileindex_entry *ie, struct got_repository *repo,
2097 got_worktree_checkout_cb progress_cb, void *progress_arg)
2099 const struct got_error *err = NULL;
2100 unsigned char status;
2104 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2105 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2107 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2109 return got_error_from_errno("asprintf");
2111 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2115 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2116 char ondisk_target[PATH_MAX];
2117 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2118 sizeof(ondisk_target));
2119 if (ondisk_len == -1) {
2120 err = got_error_from_errno2("readlink", ondisk_path);
2123 ondisk_target[ondisk_len] = '\0';
2124 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2125 NULL, NULL, /* XXX pass common ancestor info? */
2126 ondisk_target, ondisk_path);
2129 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2134 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2135 status == GOT_STATUS_ADD) {
2136 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2140 * Preserve the working file and change the deleted blob's
2141 * entry into a schedule-add entry.
2143 err = got_fileindex_entry_update(ie, worktree->root_fd,
2144 ie->path, NULL, NULL, 0);
2146 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2149 if (status == GOT_STATUS_NO_CHANGE) {
2150 err = remove_ondisk_file(worktree->root_path, ie->path);
2154 got_fileindex_entry_remove(fileindex, ie);
2161 struct diff_cb_arg {
2162 struct got_fileindex *fileindex;
2163 struct got_worktree *worktree;
2164 struct got_repository *repo;
2165 got_worktree_checkout_cb progress_cb;
2167 got_cancel_cb cancel_cb;
2171 static const struct got_error *
2172 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2173 struct got_tree_entry *te, const char *parent_path)
2175 struct diff_cb_arg *a = arg;
2177 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2178 return got_error(GOT_ERR_CANCELLED);
2180 return update_blob(a->worktree, a->fileindex, ie, te,
2181 ie->path, a->repo, a->progress_cb, a->progress_arg);
2184 static const struct got_error *
2185 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2187 struct diff_cb_arg *a = arg;
2189 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2190 return got_error(GOT_ERR_CANCELLED);
2192 return delete_blob(a->worktree, a->fileindex, ie,
2193 a->repo, a->progress_cb, a->progress_arg);
2196 static const struct got_error *
2197 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2199 struct diff_cb_arg *a = arg;
2200 const struct got_error *err;
2203 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2204 return got_error(GOT_ERR_CANCELLED);
2206 if (got_object_tree_entry_is_submodule(te))
2209 if (asprintf(&path, "%s%s%s", parent_path,
2210 parent_path[0] ? "/" : "", te->name)
2212 return got_error_from_errno("asprintf");
2214 if (S_ISDIR(te->mode))
2215 err = add_dir_on_disk(a->worktree, path);
2217 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2218 a->repo, a->progress_cb, a->progress_arg);
2224 const struct got_error *
2225 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2227 uint32_t uuid_status;
2229 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2230 if (uuid_status != uuid_s_ok) {
2232 return got_error_uuid(uuid_status, "uuid_to_string");
2238 static const struct got_error *
2239 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2241 const struct got_error *err = NULL;
2242 char *uuidstr = NULL;
2246 err = got_worktree_get_uuid(&uuidstr, worktree);
2250 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2251 err = got_error_from_errno("asprintf");
2258 const struct got_error *
2259 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2261 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2264 static const struct got_error *
2265 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2267 return get_ref_name(refname, worktree,
2268 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2271 static const struct got_error *
2272 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2274 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2277 static const struct got_error *
2278 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2280 return get_ref_name(refname, worktree,
2281 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2284 static const struct got_error *
2285 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2287 return get_ref_name(refname, worktree,
2288 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2291 static const struct got_error *
2292 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2294 return get_ref_name(refname, worktree,
2295 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2298 static const struct got_error *
2299 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2301 return get_ref_name(refname, worktree,
2302 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2305 static const struct got_error *
2306 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2308 return get_ref_name(refname, worktree,
2309 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2312 static const struct got_error *
2313 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2315 return get_ref_name(refname, worktree,
2316 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2319 const struct got_error *
2320 got_worktree_get_histedit_script_path(char **path,
2321 struct got_worktree *worktree)
2323 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2324 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2326 return got_error_from_errno("asprintf");
2332 * Prevent Git's garbage collector from deleting our base commit by
2333 * setting a reference to our base commit's ID.
2335 static const struct got_error *
2336 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2338 const struct got_error *err = NULL;
2339 struct got_reference *ref = NULL;
2342 err = got_worktree_get_base_ref_name(&refname, worktree);
2346 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2350 err = got_ref_write(ref, repo);
2358 static const struct got_error *
2359 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2361 const struct got_error *err = NULL;
2363 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2364 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2365 err = got_error_from_errno("asprintf");
2366 *fileindex_path = NULL;
2372 static const struct got_error *
2373 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2374 struct got_worktree *worktree)
2376 const struct got_error *err = NULL;
2379 *fileindex_path = NULL;
2380 *fileindex = got_fileindex_alloc();
2381 if (*fileindex == NULL)
2382 return got_error_from_errno("got_fileindex_alloc");
2384 err = get_fileindex_path(fileindex_path, worktree);
2388 index = fopen(*fileindex_path, "rb");
2389 if (index == NULL) {
2390 if (errno != ENOENT)
2391 err = got_error_from_errno2("fopen", *fileindex_path);
2393 err = got_fileindex_read(*fileindex, index);
2394 if (fclose(index) == EOF && err == NULL)
2395 err = got_error_from_errno("fclose");
2399 free(*fileindex_path);
2400 *fileindex_path = NULL;
2401 got_fileindex_free(*fileindex);
2407 struct bump_base_commit_id_arg {
2408 struct got_object_id *base_commit_id;
2411 const char *entry_name;
2412 got_worktree_checkout_cb progress_cb;
2416 /* Bump base commit ID of all files within an updated part of the work tree. */
2417 static const struct got_error *
2418 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2420 const struct got_error *err;
2421 struct bump_base_commit_id_arg *a = arg;
2423 if (a->entry_name) {
2424 if (strcmp(ie->path, a->path) != 0)
2426 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2429 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2430 SHA1_DIGEST_LENGTH) == 0)
2433 if (a->progress_cb) {
2434 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2439 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2443 static const struct got_error *
2444 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2445 struct got_fileindex *fileindex,
2446 got_worktree_checkout_cb progress_cb, void *progress_arg)
2448 struct bump_base_commit_id_arg bbc_arg;
2450 bbc_arg.base_commit_id = worktree->base_commit_id;
2451 bbc_arg.entry_name = NULL;
2453 bbc_arg.path_len = 0;
2454 bbc_arg.progress_cb = progress_cb;
2455 bbc_arg.progress_arg = progress_arg;
2457 return got_fileindex_for_each_entry_safe(fileindex,
2458 bump_base_commit_id, &bbc_arg);
2461 static const struct got_error *
2462 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2464 const struct got_error *err = NULL;
2465 char *new_fileindex_path = NULL;
2466 FILE *new_index = NULL;
2467 struct timespec timeout;
2469 err = got_opentemp_named(&new_fileindex_path, &new_index,
2474 err = got_fileindex_write(fileindex, new_index);
2478 if (rename(new_fileindex_path, fileindex_path) != 0) {
2479 err = got_error_from_errno3("rename", new_fileindex_path,
2481 unlink(new_fileindex_path);
2485 * Sleep for a short amount of time to ensure that files modified after
2486 * this program exits have a different time stamp from the one which
2487 * was recorded in the file index.
2490 timeout.tv_nsec = 1;
2491 nanosleep(&timeout, NULL);
2495 free(new_fileindex_path);
2499 static const struct got_error *
2500 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2501 struct got_object_id **tree_id, const char *wt_relpath,
2502 struct got_worktree *worktree, struct got_repository *repo)
2504 const struct got_error *err = NULL;
2505 struct got_object_id *id = NULL;
2506 char *in_repo_path = NULL;
2507 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2509 *entry_type = GOT_OBJ_TYPE_ANY;
2510 *tree_relpath = NULL;
2513 if (wt_relpath[0] == '\0') {
2514 /* Check out all files within the work tree. */
2515 *entry_type = GOT_OBJ_TYPE_TREE;
2516 *tree_relpath = strdup("");
2517 if (*tree_relpath == NULL) {
2518 err = got_error_from_errno("strdup");
2521 err = got_object_id_by_path(tree_id, repo,
2522 worktree->base_commit_id, worktree->path_prefix);
2528 /* Check out a subset of files in the work tree. */
2530 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2531 is_root_wt ? "" : "/", wt_relpath) == -1) {
2532 err = got_error_from_errno("asprintf");
2536 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2542 in_repo_path = NULL;
2544 err = got_object_get_type(entry_type, repo, id);
2548 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2549 /* Check out a single file. */
2550 if (strchr(wt_relpath, '/') == NULL) {
2551 /* Check out a single file in work tree's root dir. */
2552 in_repo_path = strdup(worktree->path_prefix);
2553 if (in_repo_path == NULL) {
2554 err = got_error_from_errno("strdup");
2557 *tree_relpath = strdup("");
2558 if (*tree_relpath == NULL) {
2559 err = got_error_from_errno("strdup");
2563 /* Check out a single file in a subdirectory. */
2564 err = got_path_dirname(tree_relpath, wt_relpath);
2567 if (asprintf(&in_repo_path, "%s%s%s",
2568 worktree->path_prefix, is_root_wt ? "" : "/",
2569 *tree_relpath) == -1) {
2570 err = got_error_from_errno("asprintf");
2574 err = got_object_id_by_path(tree_id, repo,
2575 worktree->base_commit_id, in_repo_path);
2577 /* Check out all files within a subdirectory. */
2578 *tree_id = got_object_id_dup(id);
2579 if (*tree_id == NULL) {
2580 err = got_error_from_errno("got_object_id_dup");
2583 *tree_relpath = strdup(wt_relpath);
2584 if (*tree_relpath == NULL) {
2585 err = got_error_from_errno("strdup");
2593 *entry_type = GOT_OBJ_TYPE_ANY;
2594 free(*tree_relpath);
2595 *tree_relpath = NULL;
2602 static const struct got_error *
2603 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2604 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2605 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2606 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2608 const struct got_error *err = NULL;
2609 struct got_commit_object *commit = NULL;
2610 struct got_tree_object *tree = NULL;
2611 struct got_fileindex_diff_tree_cb diff_cb;
2612 struct diff_cb_arg arg;
2614 err = ref_base_commit(worktree, repo);
2616 if (!(err->code == GOT_ERR_ERRNO &&
2617 (errno == EACCES || errno == EROFS)))
2619 err = (*progress_cb)(progress_arg,
2620 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2625 err = got_object_open_as_commit(&commit, repo,
2626 worktree->base_commit_id);
2630 err = got_object_open_as_tree(&tree, repo, tree_id);
2635 got_object_tree_find_entry(tree, entry_name) == NULL) {
2636 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2640 diff_cb.diff_old_new = diff_old_new;
2641 diff_cb.diff_old = diff_old;
2642 diff_cb.diff_new = diff_new;
2643 arg.fileindex = fileindex;
2644 arg.worktree = worktree;
2646 arg.progress_cb = progress_cb;
2647 arg.progress_arg = progress_arg;
2648 arg.cancel_cb = cancel_cb;
2649 arg.cancel_arg = cancel_arg;
2650 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2651 entry_name, repo, &diff_cb, &arg);
2654 got_object_tree_close(tree);
2656 got_object_commit_close(commit);
2660 const struct got_error *
2661 got_worktree_checkout_files(struct got_worktree *worktree,
2662 struct got_pathlist_head *paths, struct got_repository *repo,
2663 got_worktree_checkout_cb progress_cb, void *progress_arg,
2664 got_cancel_cb cancel_cb, void *cancel_arg)
2666 const struct got_error *err = NULL, *sync_err, *unlockerr;
2667 struct got_commit_object *commit = NULL;
2668 struct got_tree_object *tree = NULL;
2669 struct got_fileindex *fileindex = NULL;
2670 char *fileindex_path = NULL;
2671 struct got_pathlist_entry *pe;
2672 struct tree_path_data {
2673 SIMPLEQ_ENTRY(tree_path_data) entry;
2674 struct got_object_id *tree_id;
2679 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2681 SIMPLEQ_INIT(&tree_paths);
2683 err = lock_worktree(worktree, LOCK_EX);
2687 /* Map all specified paths to in-repository trees. */
2688 TAILQ_FOREACH(pe, paths, entry) {
2689 tpd = malloc(sizeof(*tpd));
2691 err = got_error_from_errno("malloc");
2695 err = find_tree_entry_for_checkout(&tpd->entry_type,
2696 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2702 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2703 err = got_path_basename(&tpd->entry_name, pe->path);
2711 tpd->entry_name = NULL;
2713 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2717 * Read the file index.
2718 * Checking out files is supposed to be an idempotent operation.
2719 * If the on-disk file index is incomplete we will try to complete it.
2721 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2725 tpd = SIMPLEQ_FIRST(&tree_paths);
2726 TAILQ_FOREACH(pe, paths, entry) {
2727 struct bump_base_commit_id_arg bbc_arg;
2729 err = checkout_files(worktree, fileindex, tpd->relpath,
2730 tpd->tree_id, tpd->entry_name, repo,
2731 progress_cb, progress_arg, cancel_cb, cancel_arg);
2735 bbc_arg.base_commit_id = worktree->base_commit_id;
2736 bbc_arg.entry_name = tpd->entry_name;
2737 bbc_arg.path = pe->path;
2738 bbc_arg.path_len = pe->path_len;
2739 bbc_arg.progress_cb = progress_cb;
2740 bbc_arg.progress_arg = progress_arg;
2741 err = got_fileindex_for_each_entry_safe(fileindex,
2742 bump_base_commit_id, &bbc_arg);
2746 tpd = SIMPLEQ_NEXT(tpd, entry);
2748 sync_err = sync_fileindex(fileindex, fileindex_path);
2749 if (sync_err && err == NULL)
2752 free(fileindex_path);
2754 got_object_tree_close(tree);
2756 got_object_commit_close(commit);
2758 got_fileindex_free(fileindex);
2759 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2760 tpd = SIMPLEQ_FIRST(&tree_paths);
2761 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2766 unlockerr = lock_worktree(worktree, LOCK_SH);
2767 if (unlockerr && err == NULL)
2772 struct merge_file_cb_arg {
2773 struct got_worktree *worktree;
2774 struct got_fileindex *fileindex;
2775 got_worktree_checkout_cb progress_cb;
2777 got_cancel_cb cancel_cb;
2779 const char *label_orig;
2780 struct got_object_id *commit_id2;
2783 static const struct got_error *
2784 merge_file_cb(void *arg, struct got_blob_object *blob1,
2785 struct got_blob_object *blob2, struct got_object_id *id1,
2786 struct got_object_id *id2, const char *path1, const char *path2,
2787 mode_t mode1, mode_t mode2, struct got_repository *repo)
2789 static const struct got_error *err = NULL;
2790 struct merge_file_cb_arg *a = arg;
2791 struct got_fileindex_entry *ie;
2792 char *ondisk_path = NULL;
2794 unsigned char status;
2795 int local_changes_subsumed;
2797 if (blob1 && blob2) {
2798 ie = got_fileindex_entry_get(a->fileindex, path2,
2801 return (*a->progress_cb)(a->progress_arg,
2802 GOT_STATUS_MISSING, path2);
2804 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2806 return got_error_from_errno("asprintf");
2808 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2813 if (status == GOT_STATUS_DELETE) {
2814 err = (*a->progress_cb)(a->progress_arg,
2815 GOT_STATUS_MERGE, path2);
2818 if (status != GOT_STATUS_NO_CHANGE &&
2819 status != GOT_STATUS_MODIFY &&
2820 status != GOT_STATUS_CONFLICT &&
2821 status != GOT_STATUS_ADD) {
2822 err = (*a->progress_cb)(a->progress_arg, status, path2);
2826 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2828 err = got_object_blob_read_to_str(&link_target2, blob2);
2831 err = merge_symlink(a->worktree, blob1, ondisk_path,
2832 path2, a->label_orig, link_target2, a->commit_id2,
2833 repo, a->progress_cb, a->progress_arg);
2836 err = merge_blob(&local_changes_subsumed, a->worktree,
2837 blob1, ondisk_path, path2, sb.st_mode,
2838 a->label_orig, blob2, a->commit_id2, repo,
2839 a->progress_cb, a->progress_arg);
2842 ie = got_fileindex_entry_get(a->fileindex, path1,
2845 return (*a->progress_cb)(a->progress_arg,
2846 GOT_STATUS_MISSING, path1);
2848 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2850 return got_error_from_errno("asprintf");
2852 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2858 case GOT_STATUS_NO_CHANGE:
2859 err = (*a->progress_cb)(a->progress_arg,
2860 GOT_STATUS_DELETE, path1);
2863 err = remove_ondisk_file(a->worktree->root_path, path1);
2867 got_fileindex_entry_mark_deleted_from_disk(ie);
2869 case GOT_STATUS_DELETE:
2870 case GOT_STATUS_MISSING:
2871 err = (*a->progress_cb)(a->progress_arg,
2872 GOT_STATUS_DELETE, path1);
2876 got_fileindex_entry_mark_deleted_from_disk(ie);
2878 case GOT_STATUS_ADD: {
2879 struct got_object_id *id;
2882 * Delete the added file only if its content already
2883 * exists in the repository.
2885 err = got_object_blob_file_create(&id, &blob1_f, path1);
2888 if (got_object_id_cmp(id, id1) == 0) {
2889 err = (*a->progress_cb)(a->progress_arg,
2890 GOT_STATUS_DELETE, path1);
2893 err = remove_ondisk_file(a->worktree->root_path,
2898 got_fileindex_entry_remove(a->fileindex,
2901 err = (*a->progress_cb)(a->progress_arg,
2902 GOT_STATUS_CANNOT_DELETE, path1);
2904 if (fclose(blob1_f) == EOF && err == NULL)
2905 err = got_error_from_errno("fclose");
2911 case GOT_STATUS_MODIFY:
2912 case GOT_STATUS_CONFLICT:
2913 err = (*a->progress_cb)(a->progress_arg,
2914 GOT_STATUS_CANNOT_DELETE, path1);
2918 case GOT_STATUS_OBSTRUCTED:
2919 err = (*a->progress_cb)(a->progress_arg, status, path1);
2927 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2929 return got_error_from_errno("asprintf");
2930 ie = got_fileindex_entry_get(a->fileindex, path2,
2933 err = get_file_status(&status, &sb, ie, ondisk_path,
2937 if (status != GOT_STATUS_NO_CHANGE &&
2938 status != GOT_STATUS_MODIFY &&
2939 status != GOT_STATUS_CONFLICT &&
2940 status != GOT_STATUS_ADD) {
2941 err = (*a->progress_cb)(a->progress_arg,
2945 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2947 err = got_object_blob_read_to_str(&link_target2,
2951 err = merge_symlink(a->worktree, NULL,
2952 ondisk_path, path2, a->label_orig,
2953 link_target2, a->commit_id2, repo,
2954 a->progress_cb, a->progress_arg);
2956 } else if (S_ISREG(sb.st_mode)) {
2957 err = merge_blob(&local_changes_subsumed,
2958 a->worktree, NULL, ondisk_path, path2,
2959 sb.st_mode, a->label_orig, blob2,
2960 a->commit_id2, repo, a->progress_cb,
2963 err = got_error_path(ondisk_path,
2964 GOT_ERR_FILE_OBSTRUCTED);
2968 if (status == GOT_STATUS_DELETE) {
2969 err = got_fileindex_entry_update(ie,
2970 a->worktree->root_fd, path2, blob2->id.sha1,
2971 a->worktree->base_commit_id->sha1, 0);
2976 int is_bad_symlink = 0;
2977 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2978 if (S_ISLNK(mode2)) {
2979 err = install_symlink(&is_bad_symlink,
2980 a->worktree, ondisk_path, path2, blob2, 0,
2981 0, 1, repo, a->progress_cb, a->progress_arg);
2983 err = install_blob(a->worktree, ondisk_path, path2,
2984 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2985 a->progress_cb, a->progress_arg);
2989 err = got_fileindex_entry_alloc(&ie, path2);
2992 err = got_fileindex_entry_update(ie,
2993 a->worktree->root_fd, path2, NULL, NULL, 1);
2995 got_fileindex_entry_free(ie);
2998 err = got_fileindex_entry_add(a->fileindex, ie);
3000 got_fileindex_entry_free(ie);
3003 if (is_bad_symlink) {
3004 got_fileindex_entry_filetype_set(ie,
3005 GOT_FILEIDX_MODE_BAD_SYMLINK);
3014 struct check_merge_ok_arg {
3015 struct got_worktree *worktree;
3016 struct got_repository *repo;
3019 static const struct got_error *
3020 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3022 const struct got_error *err = NULL;
3023 struct check_merge_ok_arg *a = arg;
3024 unsigned char status;
3028 /* Reject merges into a work tree with mixed base commits. */
3029 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3030 SHA1_DIGEST_LENGTH))
3031 return got_error(GOT_ERR_MIXED_COMMITS);
3033 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3035 return got_error_from_errno("asprintf");
3037 /* Reject merges into a work tree with conflicted files. */
3038 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3041 if (status == GOT_STATUS_CONFLICT)
3042 return got_error(GOT_ERR_CONFLICTS);
3047 static const struct got_error *
3048 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3049 const char *fileindex_path, struct got_object_id *commit_id1,
3050 struct got_object_id *commit_id2, struct got_repository *repo,
3051 got_worktree_checkout_cb progress_cb, void *progress_arg,
3052 got_cancel_cb cancel_cb, void *cancel_arg)
3054 const struct got_error *err = NULL, *sync_err;
3055 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3056 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3057 struct merge_file_cb_arg arg;
3058 char *label_orig = NULL;
3061 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3062 worktree->path_prefix);
3063 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3069 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3073 err = got_object_id_str(&id_str, commit_id1);
3077 if (asprintf(&label_orig, "%s: commit %s",
3078 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3079 err = got_error_from_errno("asprintf");
3086 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3087 worktree->path_prefix);
3091 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3095 arg.worktree = worktree;
3096 arg.fileindex = fileindex;
3097 arg.progress_cb = progress_cb;
3098 arg.progress_arg = progress_arg;
3099 arg.cancel_cb = cancel_cb;
3100 arg.cancel_arg = cancel_arg;
3101 arg.label_orig = label_orig;
3102 arg.commit_id2 = commit_id2;
3103 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3104 sync_err = sync_fileindex(fileindex, fileindex_path);
3105 if (sync_err && err == NULL)
3109 got_object_tree_close(tree1);
3111 got_object_tree_close(tree2);
3116 const struct got_error *
3117 got_worktree_merge_files(struct got_worktree *worktree,
3118 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3119 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3120 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3122 const struct got_error *err, *unlockerr;
3123 char *fileindex_path = NULL;
3124 struct got_fileindex *fileindex = NULL;
3125 struct check_merge_ok_arg mok_arg;
3127 err = lock_worktree(worktree, LOCK_EX);
3131 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3135 mok_arg.worktree = worktree;
3136 mok_arg.repo = repo;
3137 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3142 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3143 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3146 got_fileindex_free(fileindex);
3147 free(fileindex_path);
3148 unlockerr = lock_worktree(worktree, LOCK_SH);
3149 if (unlockerr && err == NULL)
3154 struct diff_dir_cb_arg {
3155 struct got_fileindex *fileindex;
3156 struct got_worktree *worktree;
3157 const char *status_path;
3158 size_t status_path_len;
3159 struct got_repository *repo;
3160 got_worktree_status_cb status_cb;
3162 got_cancel_cb cancel_cb;
3164 /* A pathlist containing per-directory pathlists of ignore patterns. */
3165 struct got_pathlist_head ignores;
3166 int report_unchanged;
3170 static const struct got_error *
3171 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3172 int dirfd, const char *de_name,
3173 got_worktree_status_cb status_cb, void *status_arg,
3174 struct got_repository *repo, int report_unchanged)
3176 const struct got_error *err = NULL;
3177 unsigned char status = GOT_STATUS_NO_CHANGE;
3178 unsigned char staged_status = get_staged_status(ie);
3180 struct got_object_id blob_id, commit_id, staged_blob_id;
3181 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3182 struct got_object_id *staged_blob_idp = NULL;
3184 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3188 if (status == GOT_STATUS_NO_CHANGE &&
3189 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3192 if (got_fileindex_entry_has_blob(ie)) {
3193 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3194 blob_idp = &blob_id;
3196 if (got_fileindex_entry_has_commit(ie)) {
3197 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3198 commit_idp = &commit_id;
3200 if (staged_status == GOT_STATUS_ADD ||
3201 staged_status == GOT_STATUS_MODIFY) {
3202 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3203 SHA1_DIGEST_LENGTH);
3204 staged_blob_idp = &staged_blob_id;
3207 return (*status_cb)(status_arg, status, staged_status,
3208 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3211 static const struct got_error *
3212 status_old_new(void *arg, struct got_fileindex_entry *ie,
3213 struct dirent *de, const char *parent_path, int dirfd)
3215 const struct got_error *err = NULL;
3216 struct diff_dir_cb_arg *a = arg;
3219 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3220 return got_error(GOT_ERR_CANCELLED);
3222 if (got_path_cmp(parent_path, a->status_path,
3223 strlen(parent_path), a->status_path_len) != 0 &&
3224 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3227 if (parent_path[0]) {
3228 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3229 parent_path, de->d_name) == -1)
3230 return got_error_from_errno("asprintf");
3232 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3234 return got_error_from_errno("asprintf");
3237 err = report_file_status(ie, abspath, dirfd, de->d_name,
3238 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3243 static const struct got_error *
3244 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3246 struct diff_dir_cb_arg *a = arg;
3247 struct got_object_id blob_id, commit_id;
3248 unsigned char status;
3250 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3251 return got_error(GOT_ERR_CANCELLED);
3253 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3256 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3257 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3258 if (got_fileindex_entry_has_file_on_disk(ie))
3259 status = GOT_STATUS_MISSING;
3261 status = GOT_STATUS_DELETE;
3262 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3263 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3267 free_ignorelist(struct got_pathlist_head *ignorelist)
3269 struct got_pathlist_entry *pe;
3271 TAILQ_FOREACH(pe, ignorelist, entry)
3272 free((char *)pe->path);
3273 got_pathlist_free(ignorelist);
3277 free_ignores(struct got_pathlist_head *ignores)
3279 struct got_pathlist_entry *pe;
3281 TAILQ_FOREACH(pe, ignores, entry) {
3282 struct got_pathlist_head *ignorelist = pe->data;
3283 free_ignorelist(ignorelist);
3284 free((char *)pe->path);
3286 got_pathlist_free(ignores);
3289 static const struct got_error *
3290 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3292 const struct got_error *err = NULL;
3293 struct got_pathlist_entry *pe = NULL;
3294 struct got_pathlist_head *ignorelist;
3295 char *line = NULL, *pattern, *dirpath = NULL;
3296 size_t linesize = 0;
3299 ignorelist = calloc(1, sizeof(*ignorelist));
3300 if (ignorelist == NULL)
3301 return got_error_from_errno("calloc");
3302 TAILQ_INIT(ignorelist);
3304 while ((linelen = getline(&line, &linesize, f)) != -1) {
3305 if (linelen > 0 && line[linelen - 1] == '\n')
3306 line[linelen - 1] = '\0';
3308 /* Git's ignores may contain comments. */
3312 /* Git's negated patterns are not (yet?) supported. */
3316 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3318 err = got_error_from_errno("asprintf");
3321 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3326 err = got_error_from_errno("getline");
3330 dirpath = strdup(path);
3331 if (dirpath == NULL) {
3332 err = got_error_from_errno("strdup");
3335 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3338 if (err || pe == NULL) {
3340 free_ignorelist(ignorelist);
3346 match_ignores(struct got_pathlist_head *ignores, const char *path)
3348 struct got_pathlist_entry *pe;
3350 /* Handle patterns which match in all directories. */
3351 TAILQ_FOREACH(pe, ignores, entry) {
3352 struct got_pathlist_head *ignorelist = pe->data;
3353 struct got_pathlist_entry *pi;
3355 TAILQ_FOREACH(pi, ignorelist, entry) {
3356 const char *p, *pattern = pi->path;
3358 if (strncmp(pattern, "**/", 3) != 0)
3363 if (fnmatch(pattern, p,
3364 FNM_PATHNAME | FNM_LEADING_DIR)) {
3365 /* Retry in next directory. */
3366 while (*p && *p != '/')
3378 * The ignores pathlist contains ignore lists from children before
3379 * parents, so we can find the most specific ignorelist by walking
3380 * ignores backwards.
3382 pe = TAILQ_LAST(ignores, got_pathlist_head);
3384 if (got_path_is_child(path, pe->path, pe->path_len)) {
3385 struct got_pathlist_head *ignorelist = pe->data;
3386 struct got_pathlist_entry *pi;
3387 TAILQ_FOREACH(pi, ignorelist, entry) {
3388 const char *pattern = pi->path;
3389 int flags = FNM_LEADING_DIR;
3390 if (strstr(pattern, "/**/") == NULL)
3391 flags |= FNM_PATHNAME;
3392 if (fnmatch(pattern, path, flags))
3397 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3403 static const struct got_error *
3404 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3405 const char *path, int dirfd, const char *ignores_filename)
3407 const struct got_error *err = NULL;
3410 FILE *ignoresfile = NULL;
3412 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3413 path[0] ? "/" : "", ignores_filename) == -1)
3414 return got_error_from_errno("asprintf");
3417 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3419 if (errno != ENOENT && errno != EACCES)
3420 err = got_error_from_errno2("openat",
3423 ignoresfile = fdopen(fd, "r");
3424 if (ignoresfile == NULL)
3425 err = got_error_from_errno2("fdopen",
3429 err = read_ignores(ignores, path, ignoresfile);
3433 ignoresfile = fopen(ignorespath, "r");
3434 if (ignoresfile == NULL) {
3435 if (errno != ENOENT && errno != EACCES)
3436 err = got_error_from_errno2("fopen",
3439 err = read_ignores(ignores, path, ignoresfile);
3442 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3443 err = got_error_from_errno2("fclose", path);
3444 if (fd != -1 && close(fd) == -1 && err == NULL)
3445 err = got_error_from_errno2("close", path);
3450 static const struct got_error *
3451 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3453 const struct got_error *err = NULL;
3454 struct diff_dir_cb_arg *a = arg;
3457 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3458 return got_error(GOT_ERR_CANCELLED);
3460 if (parent_path[0]) {
3461 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3462 return got_error_from_errno("asprintf");
3467 if (de->d_type != DT_DIR &&
3468 got_path_is_child(path, a->status_path, a->status_path_len)
3469 && !match_ignores(&a->ignores, path))
3470 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3471 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3477 static const struct got_error *
3478 status_traverse(void *arg, const char *path, int dirfd)
3480 const struct got_error *err = NULL;
3481 struct diff_dir_cb_arg *a = arg;
3486 err = add_ignores(&a->ignores, a->worktree->root_path,
3487 path, dirfd, ".cvsignore");
3491 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3492 dirfd, ".gitignore");
3497 static const struct got_error *
3498 report_single_file_status(const char *path, const char *ondisk_path,
3499 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3500 void *status_arg, struct got_repository *repo, int report_unchanged)
3502 struct got_fileindex_entry *ie;
3505 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3507 return report_file_status(ie, ondisk_path, -1, NULL,
3508 status_cb, status_arg, repo, report_unchanged);
3510 if (lstat(ondisk_path, &sb) == -1) {
3511 if (errno != ENOENT)
3512 return got_error_from_errno2("lstat", ondisk_path);
3513 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3514 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3518 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3519 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3520 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3525 static const struct got_error *
3526 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3527 const char *root_path, const char *path)
3529 const struct got_error *err;
3530 char *parent_path, *next_parent_path = NULL;
3532 err = add_ignores(ignores, root_path, "", -1,
3537 err = add_ignores(ignores, root_path, "", -1,
3542 err = got_path_dirname(&parent_path, path);
3544 if (err->code == GOT_ERR_BAD_PATH)
3545 return NULL; /* cannot traverse parent */
3549 err = add_ignores(ignores, root_path, parent_path, -1,
3553 err = add_ignores(ignores, root_path, parent_path, -1,
3557 err = got_path_dirname(&next_parent_path, parent_path);
3559 if (err->code == GOT_ERR_BAD_PATH)
3560 err = NULL; /* traversed everything */
3564 parent_path = next_parent_path;
3565 next_parent_path = NULL;
3569 free(next_parent_path);
3573 static const struct got_error *
3574 worktree_status(struct got_worktree *worktree, const char *path,
3575 struct got_fileindex *fileindex, struct got_repository *repo,
3576 got_worktree_status_cb status_cb, void *status_arg,
3577 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3578 int report_unchanged)
3580 const struct got_error *err = NULL;
3582 struct got_fileindex_diff_dir_cb fdiff_cb;
3583 struct diff_dir_cb_arg arg;
3584 char *ondisk_path = NULL;
3586 TAILQ_INIT(&arg.ignores);
3588 if (asprintf(&ondisk_path, "%s%s%s",
3589 worktree->root_path, path[0] ? "/" : "", path) == -1)
3590 return got_error_from_errno("asprintf");
3592 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3594 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3596 err = got_error_from_errno2("open", ondisk_path);
3598 err = report_single_file_status(path, ondisk_path,
3599 fileindex, status_cb, status_arg, repo,
3602 fdiff_cb.diff_old_new = status_old_new;
3603 fdiff_cb.diff_old = status_old;
3604 fdiff_cb.diff_new = status_new;
3605 fdiff_cb.diff_traverse = status_traverse;
3606 arg.fileindex = fileindex;
3607 arg.worktree = worktree;
3608 arg.status_path = path;
3609 arg.status_path_len = strlen(path);
3611 arg.status_cb = status_cb;
3612 arg.status_arg = status_arg;
3613 arg.cancel_cb = cancel_cb;
3614 arg.cancel_arg = cancel_arg;
3615 arg.report_unchanged = report_unchanged;
3616 arg.no_ignores = no_ignores;
3618 err = add_ignores_from_parent_paths(&arg.ignores,
3619 worktree->root_path, path);
3623 err = got_fileindex_diff_dir(fileindex, fd,
3624 worktree->root_path, path, repo, &fdiff_cb, &arg);
3627 free_ignores(&arg.ignores);
3628 if (fd != -1 && close(fd) == -1 && err == NULL)
3629 err = got_error_from_errno("close");
3634 const struct got_error *
3635 got_worktree_status(struct got_worktree *worktree,
3636 struct got_pathlist_head *paths, struct got_repository *repo,
3637 got_worktree_status_cb status_cb, void *status_arg,
3638 got_cancel_cb cancel_cb, void *cancel_arg)
3640 const struct got_error *err = NULL;
3641 char *fileindex_path = NULL;
3642 struct got_fileindex *fileindex = NULL;
3643 struct got_pathlist_entry *pe;
3645 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3649 TAILQ_FOREACH(pe, paths, entry) {
3650 err = worktree_status(worktree, pe->path, fileindex, repo,
3651 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3655 free(fileindex_path);
3656 got_fileindex_free(fileindex);
3660 const struct got_error *
3661 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3664 const struct got_error *err = NULL;
3665 char *resolved = NULL, *cwd = NULL, *path = NULL;
3668 char *abspath = NULL;
3669 char canonpath[PATH_MAX];
3673 cwd = getcwd(NULL, 0);
3675 return got_error_from_errno("getcwd");
3677 if (lstat(arg, &sb) == -1) {
3678 if (errno != ENOENT) {
3679 err = got_error_from_errno2("lstat", arg);
3684 if (S_ISLNK(sb.st_mode)) {
3686 * We cannot use realpath(3) with symlinks since we want to
3687 * operate on the symlink itself.
3688 * But we can make the path absolute, assuming it is relative
3689 * to the current working directory, and then canonicalize it.
3691 if (!got_path_is_absolute(arg)) {
3692 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3693 err = got_error_from_errno("asprintf");
3698 err = got_canonpath(abspath ? abspath : arg, canonpath,
3702 resolved = strdup(canonpath);
3703 if (resolved == NULL) {
3704 err = got_error_from_errno("strdup");
3708 resolved = realpath(arg, NULL);
3709 if (resolved == NULL) {
3710 if (errno != ENOENT) {
3711 err = got_error_from_errno2("realpath", arg);
3714 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3715 err = got_error_from_errno("asprintf");
3718 err = got_canonpath(abspath, canonpath,
3722 resolved = strdup(canonpath);
3723 if (resolved == NULL) {
3724 err = got_error_from_errno("strdup");
3730 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3731 strlen(got_worktree_get_root_path(worktree)))) {
3732 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3736 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3737 err = got_path_skip_common_ancestor(&path,
3738 got_worktree_get_root_path(worktree), resolved);
3744 err = got_error_from_errno("strdup");
3749 /* XXX status walk can't deal with trailing slash! */
3751 while (len > 0 && path[len - 1] == '/') {
3752 path[len - 1] = '\0';
3766 struct schedule_addition_args {
3767 struct got_worktree *worktree;
3768 struct got_fileindex *fileindex;
3769 got_worktree_checkout_cb progress_cb;
3771 struct got_repository *repo;
3774 static const struct got_error *
3775 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3776 const char *relpath, struct got_object_id *blob_id,
3777 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3778 int dirfd, const char *de_name)
3780 struct schedule_addition_args *a = arg;
3781 const struct got_error *err = NULL;
3782 struct got_fileindex_entry *ie;
3786 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3788 return got_error_from_errno("asprintf");
3790 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3792 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3796 /* Re-adding an existing entry is a no-op. */
3797 if (status == GOT_STATUS_ADD)
3799 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3804 if (status != GOT_STATUS_UNVERSIONED) {
3805 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3809 err = got_fileindex_entry_alloc(&ie, relpath);
3812 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3813 relpath, NULL, NULL, 1);
3815 got_fileindex_entry_free(ie);
3818 err = got_fileindex_entry_add(a->fileindex, ie);
3820 got_fileindex_entry_free(ie);
3827 if (status == GOT_STATUS_ADD)
3829 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3832 const struct got_error *
3833 got_worktree_schedule_add(struct got_worktree *worktree,
3834 struct got_pathlist_head *paths,
3835 got_worktree_checkout_cb progress_cb, void *progress_arg,
3836 struct got_repository *repo, int no_ignores)
3838 struct got_fileindex *fileindex = NULL;
3839 char *fileindex_path = NULL;
3840 const struct got_error *err = NULL, *sync_err, *unlockerr;
3841 struct got_pathlist_entry *pe;
3842 struct schedule_addition_args saa;
3844 err = lock_worktree(worktree, LOCK_EX);
3848 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3852 saa.worktree = worktree;
3853 saa.fileindex = fileindex;
3854 saa.progress_cb = progress_cb;
3855 saa.progress_arg = progress_arg;
3858 TAILQ_FOREACH(pe, paths, entry) {
3859 err = worktree_status(worktree, pe->path, fileindex, repo,
3860 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3864 sync_err = sync_fileindex(fileindex, fileindex_path);
3865 if (sync_err && err == NULL)
3868 free(fileindex_path);
3870 got_fileindex_free(fileindex);
3871 unlockerr = lock_worktree(worktree, LOCK_SH);
3872 if (unlockerr && err == NULL)
3877 struct schedule_deletion_args {
3878 struct got_worktree *worktree;
3879 struct got_fileindex *fileindex;
3880 got_worktree_delete_cb progress_cb;
3882 struct got_repository *repo;
3883 int delete_local_mods;
3885 const char *status_codes;
3888 static const struct got_error *
3889 schedule_for_deletion(void *arg, unsigned char status,
3890 unsigned char staged_status, const char *relpath,
3891 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3892 struct got_object_id *commit_id, int dirfd, const char *de_name)
3894 struct schedule_deletion_args *a = arg;
3895 const struct got_error *err = NULL;
3896 struct got_fileindex_entry *ie = NULL;
3900 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3902 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3904 staged_status = get_staged_status(ie);
3905 if (staged_status != GOT_STATUS_NO_CHANGE) {
3906 if (staged_status == GOT_STATUS_DELETE)
3908 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3911 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3913 return got_error_from_errno("asprintf");
3915 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3920 if (a->status_codes) {
3921 size_t ncodes = strlen(a->status_codes);
3923 for (i = 0; i < ncodes ; i++) {
3924 if (status == a->status_codes[i])
3928 /* Do not delete files in non-matching status. */
3932 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3933 a->status_codes[i] != GOT_STATUS_MISSING) {
3934 static char msg[64];
3935 snprintf(msg, sizeof(msg),
3936 "invalid status code '%c'", a->status_codes[i]);
3937 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3942 if (status != GOT_STATUS_NO_CHANGE) {
3943 if (status == GOT_STATUS_DELETE)
3945 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3946 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3949 if (status != GOT_STATUS_MODIFY &&
3950 status != GOT_STATUS_MISSING) {
3951 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3956 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3960 if (unlinkat(dirfd, de_name, 0) != 0) {
3961 err = got_error_from_errno2("unlinkat",
3965 } else if (unlink(ondisk_path) != 0) {
3966 err = got_error_from_errno2("unlink", ondisk_path);
3970 root_len = strlen(a->worktree->root_path);
3973 err = got_path_dirname(&parent, ondisk_path);
3977 ondisk_path = parent;
3978 if (rmdir(ondisk_path) == -1) {
3979 if (errno != ENOTEMPTY)
3980 err = got_error_from_errno2("rmdir",
3984 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3985 strlen(ondisk_path), root_len) != 0);
3988 got_fileindex_entry_mark_deleted_from_disk(ie);
3993 if (status == GOT_STATUS_DELETE)
3995 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3996 staged_status, relpath);
3999 const struct got_error *
4000 got_worktree_schedule_delete(struct got_worktree *worktree,
4001 struct got_pathlist_head *paths, int delete_local_mods,
4002 const char *status_codes,
4003 got_worktree_delete_cb progress_cb, void *progress_arg,
4004 struct got_repository *repo, int keep_on_disk)
4006 struct got_fileindex *fileindex = NULL;
4007 char *fileindex_path = NULL;
4008 const struct got_error *err = NULL, *sync_err, *unlockerr;
4009 struct got_pathlist_entry *pe;
4010 struct schedule_deletion_args sda;
4012 err = lock_worktree(worktree, LOCK_EX);
4016 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4020 sda.worktree = worktree;
4021 sda.fileindex = fileindex;
4022 sda.progress_cb = progress_cb;
4023 sda.progress_arg = progress_arg;
4025 sda.delete_local_mods = delete_local_mods;
4026 sda.keep_on_disk = keep_on_disk;
4027 sda.status_codes = status_codes;
4029 TAILQ_FOREACH(pe, paths, entry) {
4030 err = worktree_status(worktree, pe->path, fileindex, repo,
4031 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4035 sync_err = sync_fileindex(fileindex, fileindex_path);
4036 if (sync_err && err == NULL)
4039 free(fileindex_path);
4041 got_fileindex_free(fileindex);
4042 unlockerr = lock_worktree(worktree, LOCK_SH);
4043 if (unlockerr && err == NULL)
4048 static const struct got_error *
4049 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4051 const struct got_error *err = NULL;
4053 size_t linesize = 0, n;
4056 linelen = getline(&line, &linesize, infile);
4057 if (linelen == -1) {
4058 if (ferror(infile)) {
4059 err = got_error_from_errno("getline");
4065 n = fwrite(line, 1, linelen, outfile);
4067 err = got_ferror(outfile, GOT_ERR_IO);
4072 n = fwrite(line, 1, linelen, rejectfile);
4074 err = got_ferror(outfile, GOT_ERR_IO);
4081 static const struct got_error *
4082 skip_one_line(FILE *f)
4085 size_t linesize = 0;
4088 linelen = getline(&line, &linesize, f);
4089 if (linelen == -1) {
4091 return got_error_from_errno("getline");
4098 static const struct got_error *
4099 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4100 int start_old, int end_old, int start_new, int end_new,
4101 FILE *outfile, FILE *rejectfile)
4103 const struct got_error *err;
4105 /* Copy old file's lines leading up to patch. */
4106 while (!feof(f1) && *line_cur1 < start_old) {
4107 err = copy_one_line(f1, outfile, NULL);
4112 /* Skip new file's lines leading up to patch. */
4113 while (!feof(f2) && *line_cur2 < start_new) {
4115 err = copy_one_line(f2, NULL, rejectfile);
4117 err = skip_one_line(f2);
4122 /* Copy patched lines. */
4123 while (!feof(f2) && *line_cur2 <= end_new) {
4124 err = copy_one_line(f2, outfile, NULL);
4129 /* Skip over old file's replaced lines. */
4130 while (!feof(f1) && *line_cur1 <= end_old) {
4132 err = copy_one_line(f1, NULL, rejectfile);
4134 err = skip_one_line(f1);
4143 static const struct got_error *
4144 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4145 FILE *outfile, FILE *rejectfile)
4147 const struct got_error *err;
4150 /* Copy old file's lines until EOF. */
4152 err = copy_one_line(f1, outfile, NULL);
4159 /* Copy new file's lines until EOF. */
4161 err = copy_one_line(f2, NULL, rejectfile);
4171 static const struct got_error *
4172 apply_or_reject_change(int *choice, int *nchunks_used,
4173 struct diff_result *diff_result, int n,
4174 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4175 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4176 got_worktree_patch_cb patch_cb, void *patch_arg)
4178 const struct got_error *err = NULL;
4179 struct diff_chunk_context cc = {};
4180 int start_old, end_old, start_new, end_new;
4182 struct diff_output_unidiff_state *diff_state;
4183 struct diff_input_info diff_info;
4186 *choice = GOT_PATCH_CHOICE_NONE;
4188 /* Get changed line numbers without context lines for copy_change(). */
4189 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4190 start_old = cc.left.start;
4191 end_old = cc.left.end;
4192 start_new = cc.right.start;
4193 end_new = cc.right.end;
4195 /* Get the same change with context lines for display. */
4196 memset(&cc, 0, sizeof(cc));
4197 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4199 memset(&diff_info, 0, sizeof(diff_info));
4200 diff_info.left_path = relpath;
4201 diff_info.right_path = relpath;
4203 diff_state = diff_output_unidiff_state_alloc();
4204 if (diff_state == NULL)
4205 return got_error_set_errno(ENOMEM,
4206 "diff_output_unidiff_state_alloc");
4208 hunkfile = got_opentemp();
4209 if (hunkfile == NULL) {
4210 err = got_error_from_errno("got_opentemp");
4214 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4216 if (rc != DIFF_RC_OK) {
4217 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4221 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4222 err = got_ferror(hunkfile, GOT_ERR_IO);
4226 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4227 hunkfile, changeno, nchanges);
4232 case GOT_PATCH_CHOICE_YES:
4233 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4234 end_old, start_new, end_new, outfile, rejectfile);
4236 case GOT_PATCH_CHOICE_NO:
4237 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4238 end_old, start_new, end_new, rejectfile, outfile);
4240 case GOT_PATCH_CHOICE_QUIT:
4243 err = got_error(GOT_ERR_PATCH_CHOICE);
4247 diff_output_unidiff_state_free(diff_state);
4248 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4249 err = got_error_from_errno("fclose");
4253 struct revert_file_args {
4254 struct got_worktree *worktree;
4255 struct got_fileindex *fileindex;
4256 got_worktree_checkout_cb progress_cb;
4258 got_worktree_patch_cb patch_cb;
4260 struct got_repository *repo;
4263 static const struct got_error *
4264 create_patched_content(char **path_outfile, int reverse_patch,
4265 struct got_object_id *blob_id, const char *path2,
4266 int dirfd2, const char *de_name2,
4267 const char *relpath, struct got_repository *repo,
4268 got_worktree_patch_cb patch_cb, void *patch_arg)
4270 const struct got_error *err, *free_err;
4271 struct got_blob_object *blob = NULL;
4272 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4274 char link_target[PATH_MAX];
4275 ssize_t link_len = 0;
4276 char *path1 = NULL, *id_str = NULL;
4278 struct got_diffreg_result *diffreg_result = NULL;
4279 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4280 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4282 *path_outfile = NULL;
4284 err = got_object_id_str(&id_str, blob_id);
4289 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4291 if (errno != ELOOP) {
4292 err = got_error_from_errno2("openat", path2);
4295 link_len = readlinkat(dirfd2, de_name2,
4296 link_target, sizeof(link_target));
4298 return got_error_from_errno2("readlinkat", path2);
4299 sb2.st_mode = S_IFLNK;
4300 sb2.st_size = link_len;
4303 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4305 if (errno != ELOOP) {
4306 err = got_error_from_errno2("open", path2);
4309 link_len = readlink(path2, link_target,
4310 sizeof(link_target));
4312 return got_error_from_errno2("readlink", path2);
4313 sb2.st_mode = S_IFLNK;
4314 sb2.st_size = link_len;
4318 if (fstat(fd2, &sb2) == -1) {
4319 err = got_error_from_errno2("fstat", path2);
4323 f2 = fdopen(fd2, "r");
4325 err = got_error_from_errno2("fdopen", path2);
4331 f2 = got_opentemp();
4333 err = got_error_from_errno2("got_opentemp", path2);
4336 n = fwrite(link_target, 1, link_len, f2);
4337 if (n != link_len) {
4338 err = got_ferror(f2, GOT_ERR_IO);
4341 if (fflush(f2) == EOF) {
4342 err = got_error_from_errno("fflush");
4348 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4352 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4356 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4360 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4365 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4369 if (fseek(f1, 0L, SEEK_SET) == -1)
4370 return got_ferror(f1, GOT_ERR_IO);
4371 if (fseek(f2, 0L, SEEK_SET) == -1)
4372 return got_ferror(f2, GOT_ERR_IO);
4374 /* Count the number of actual changes in the diff result. */
4375 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4376 struct diff_chunk_context cc = {};
4377 diff_chunk_context_load_change(&cc, &nchunks_used,
4378 diffreg_result->result, n, 0);
4381 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4383 err = apply_or_reject_change(&choice, &nchunks_used,
4384 diffreg_result->result, n, relpath, f1, f2,
4385 &line_cur1, &line_cur2,
4386 reverse_patch ? NULL : outfile,
4387 reverse_patch ? outfile : NULL,
4388 ++i, nchanges, patch_cb, patch_arg);
4391 if (choice == GOT_PATCH_CHOICE_YES)
4393 else if (choice == GOT_PATCH_CHOICE_QUIT)
4397 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4398 reverse_patch ? NULL : outfile,
4399 reverse_patch ? outfile : NULL);
4403 if (!S_ISLNK(sb2.st_mode)) {
4404 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4405 err = got_error_from_errno2("fchmod", path2);
4413 got_object_blob_close(blob);
4414 free_err = got_diffreg_result_free(diffreg_result);
4417 if (f1 && fclose(f1) == EOF && err == NULL)
4418 err = got_error_from_errno2("fclose", path1);
4419 if (f2 && fclose(f2) == EOF && err == NULL)
4420 err = got_error_from_errno2("fclose", path2);
4421 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4422 err = got_error_from_errno2("close", path2);
4423 if (outfile && fclose(outfile) == EOF && err == NULL)
4424 err = got_error_from_errno2("fclose", *path_outfile);
4425 if (path1 && unlink(path1) == -1 && err == NULL)
4426 err = got_error_from_errno2("unlink", path1);
4427 if (err || !have_content) {
4428 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4429 err = got_error_from_errno2("unlink", *path_outfile);
4430 free(*path_outfile);
4431 *path_outfile = NULL;
4437 static const struct got_error *
4438 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4439 const char *relpath, struct got_object_id *blob_id,
4440 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4441 int dirfd, const char *de_name)
4443 struct revert_file_args *a = arg;
4444 const struct got_error *err = NULL;
4445 char *parent_path = NULL;
4446 struct got_fileindex_entry *ie;
4447 struct got_tree_object *tree = NULL;
4448 struct got_object_id *tree_id = NULL;
4449 const struct got_tree_entry *te = NULL;
4450 char *tree_path = NULL, *te_name;
4451 char *ondisk_path = NULL, *path_content = NULL;
4452 struct got_blob_object *blob = NULL;
4454 /* Reverting a staged deletion is a no-op. */
4455 if (status == GOT_STATUS_DELETE &&
4456 staged_status != GOT_STATUS_NO_CHANGE)
4459 if (status == GOT_STATUS_UNVERSIONED)
4460 return (*a->progress_cb)(a->progress_arg,
4461 GOT_STATUS_UNVERSIONED, relpath);
4463 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4465 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4467 /* Construct in-repository path of tree which contains this blob. */
4468 err = got_path_dirname(&parent_path, ie->path);
4470 if (err->code != GOT_ERR_BAD_PATH)
4472 parent_path = strdup("/");
4473 if (parent_path == NULL) {
4474 err = got_error_from_errno("strdup");
4478 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4479 tree_path = strdup(parent_path);
4480 if (tree_path == NULL) {
4481 err = got_error_from_errno("strdup");
4485 if (got_path_is_root_dir(parent_path)) {
4486 tree_path = strdup(a->worktree->path_prefix);
4487 if (tree_path == NULL) {
4488 err = got_error_from_errno("strdup");
4492 if (asprintf(&tree_path, "%s/%s",
4493 a->worktree->path_prefix, parent_path) == -1) {
4494 err = got_error_from_errno("asprintf");
4500 err = got_object_id_by_path(&tree_id, a->repo,
4501 a->worktree->base_commit_id, tree_path);
4503 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4504 (status == GOT_STATUS_ADD ||
4505 staged_status == GOT_STATUS_ADD)))
4508 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4512 err = got_path_basename(&te_name, ie->path);
4516 te = got_object_tree_find_entry(tree, te_name);
4518 if (te == NULL && status != GOT_STATUS_ADD &&
4519 staged_status != GOT_STATUS_ADD) {
4520 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4526 case GOT_STATUS_ADD:
4528 int choice = GOT_PATCH_CHOICE_NONE;
4529 err = (*a->patch_cb)(&choice, a->patch_arg,
4530 status, ie->path, NULL, 1, 1);
4533 if (choice != GOT_PATCH_CHOICE_YES)
4536 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4540 got_fileindex_entry_remove(a->fileindex, ie);
4542 case GOT_STATUS_DELETE:
4544 int choice = GOT_PATCH_CHOICE_NONE;
4545 err = (*a->patch_cb)(&choice, a->patch_arg,
4546 status, ie->path, NULL, 1, 1);
4549 if (choice != GOT_PATCH_CHOICE_YES)
4553 case GOT_STATUS_MODIFY:
4554 case GOT_STATUS_MODE_CHANGE:
4555 case GOT_STATUS_CONFLICT:
4556 case GOT_STATUS_MISSING: {
4557 struct got_object_id id;
4558 if (staged_status == GOT_STATUS_ADD ||
4559 staged_status == GOT_STATUS_MODIFY) {
4560 memcpy(id.sha1, ie->staged_blob_sha1,
4561 SHA1_DIGEST_LENGTH);
4563 memcpy(id.sha1, ie->blob_sha1,
4564 SHA1_DIGEST_LENGTH);
4565 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4569 if (asprintf(&ondisk_path, "%s/%s",
4570 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4571 err = got_error_from_errno("asprintf");
4575 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4576 status == GOT_STATUS_CONFLICT)) {
4577 int is_bad_symlink = 0;
4578 err = create_patched_content(&path_content, 1, &id,
4579 ondisk_path, dirfd, de_name, ie->path, a->repo,
4580 a->patch_cb, a->patch_arg);
4581 if (err || path_content == NULL)
4583 if (te && S_ISLNK(te->mode)) {
4584 if (unlink(path_content) == -1) {
4585 err = got_error_from_errno2("unlink",
4589 err = install_symlink(&is_bad_symlink,
4590 a->worktree, ondisk_path, ie->path,
4591 blob, 0, 1, 0, a->repo,
4592 a->progress_cb, a->progress_arg);
4594 if (rename(path_content, ondisk_path) == -1) {
4595 err = got_error_from_errno3("rename",
4596 path_content, ondisk_path);
4601 int is_bad_symlink = 0;
4602 if (te && S_ISLNK(te->mode)) {
4603 err = install_symlink(&is_bad_symlink,
4604 a->worktree, ondisk_path, ie->path,
4605 blob, 0, 1, 0, a->repo,
4606 a->progress_cb, a->progress_arg);
4608 err = install_blob(a->worktree, ondisk_path,
4610 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4611 got_fileindex_perms_to_st(ie), blob,
4612 0, 1, 0, 0, a->repo,
4613 a->progress_cb, a->progress_arg);
4617 if (status == GOT_STATUS_DELETE ||
4618 status == GOT_STATUS_MODE_CHANGE) {
4619 err = got_fileindex_entry_update(ie,
4620 a->worktree->root_fd, relpath,
4622 a->worktree->base_commit_id->sha1, 1);
4626 if (is_bad_symlink) {
4627 got_fileindex_entry_filetype_set(ie,
4628 GOT_FILEIDX_MODE_BAD_SYMLINK);
4642 got_object_blob_close(blob);
4644 got_object_tree_close(tree);
4649 const struct got_error *
4650 got_worktree_revert(struct got_worktree *worktree,
4651 struct got_pathlist_head *paths,
4652 got_worktree_checkout_cb progress_cb, void *progress_arg,
4653 got_worktree_patch_cb patch_cb, void *patch_arg,
4654 struct got_repository *repo)
4656 struct got_fileindex *fileindex = NULL;
4657 char *fileindex_path = NULL;
4658 const struct got_error *err = NULL, *unlockerr = NULL;
4659 const struct got_error *sync_err = NULL;
4660 struct got_pathlist_entry *pe;
4661 struct revert_file_args rfa;
4663 err = lock_worktree(worktree, LOCK_EX);
4667 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4671 rfa.worktree = worktree;
4672 rfa.fileindex = fileindex;
4673 rfa.progress_cb = progress_cb;
4674 rfa.progress_arg = progress_arg;
4675 rfa.patch_cb = patch_cb;
4676 rfa.patch_arg = patch_arg;
4678 TAILQ_FOREACH(pe, paths, entry) {
4679 err = worktree_status(worktree, pe->path, fileindex, repo,
4680 revert_file, &rfa, NULL, NULL, 0, 0);
4684 sync_err = sync_fileindex(fileindex, fileindex_path);
4685 if (sync_err && err == NULL)
4688 free(fileindex_path);
4690 got_fileindex_free(fileindex);
4691 unlockerr = lock_worktree(worktree, LOCK_SH);
4692 if (unlockerr && err == NULL)
4698 free_commitable(struct got_commitable *ct)
4701 free(ct->in_repo_path);
4702 free(ct->ondisk_path);
4704 free(ct->base_blob_id);
4705 free(ct->staged_blob_id);
4706 free(ct->base_commit_id);
4710 struct collect_commitables_arg {
4711 struct got_pathlist_head *commitable_paths;
4712 struct got_repository *repo;
4713 struct got_worktree *worktree;
4714 struct got_fileindex *fileindex;
4715 int have_staged_files;
4716 int allow_bad_symlinks;
4719 static const struct got_error *
4720 collect_commitables(void *arg, unsigned char status,
4721 unsigned char staged_status, const char *relpath,
4722 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4723 struct got_object_id *commit_id, int dirfd, const char *de_name)
4725 struct collect_commitables_arg *a = arg;
4726 const struct got_error *err = NULL;
4727 struct got_commitable *ct = NULL;
4728 struct got_pathlist_entry *new = NULL;
4729 char *parent_path = NULL, *path = NULL;
4732 if (a->have_staged_files) {
4733 if (staged_status != GOT_STATUS_MODIFY &&
4734 staged_status != GOT_STATUS_ADD &&
4735 staged_status != GOT_STATUS_DELETE)
4738 if (status == GOT_STATUS_CONFLICT)
4739 return got_error(GOT_ERR_COMMIT_CONFLICT);
4741 if (status != GOT_STATUS_MODIFY &&
4742 status != GOT_STATUS_MODE_CHANGE &&
4743 status != GOT_STATUS_ADD &&
4744 status != GOT_STATUS_DELETE)
4748 if (asprintf(&path, "/%s", relpath) == -1) {
4749 err = got_error_from_errno("asprintf");
4752 if (strcmp(path, "/") == 0) {
4753 parent_path = strdup("");
4754 if (parent_path == NULL)
4755 return got_error_from_errno("strdup");
4757 err = got_path_dirname(&parent_path, path);
4762 ct = calloc(1, sizeof(*ct));
4764 err = got_error_from_errno("calloc");
4768 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4770 err = got_error_from_errno("asprintf");
4774 if (staged_status == GOT_STATUS_ADD ||
4775 staged_status == GOT_STATUS_MODIFY) {
4776 struct got_fileindex_entry *ie;
4777 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4778 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4779 case GOT_FILEIDX_MODE_REGULAR_FILE:
4780 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4783 case GOT_FILEIDX_MODE_SYMLINK:
4787 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4790 ct->mode |= got_fileindex_entry_perms_get(ie);
4791 } else if (status != GOT_STATUS_DELETE &&
4792 staged_status != GOT_STATUS_DELETE) {
4794 if (fstatat(dirfd, de_name, &sb,
4795 AT_SYMLINK_NOFOLLOW) == -1) {
4796 err = got_error_from_errno2("fstatat",
4800 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4801 err = got_error_from_errno2("lstat", ct->ondisk_path);
4804 ct->mode = sb.st_mode;
4807 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4808 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4810 err = got_error_from_errno("asprintf");
4814 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4815 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4817 char target_path[PATH_MAX];
4819 target_len = readlink(ct->ondisk_path, target_path,
4820 sizeof(target_path));
4821 if (target_len == -1) {
4822 err = got_error_from_errno2("readlink",
4826 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4827 target_len, ct->ondisk_path, a->worktree->root_path);
4830 if (is_bad_symlink) {
4831 err = got_error_path(ct->ondisk_path,
4832 GOT_ERR_BAD_SYMLINK);
4838 ct->status = status;
4839 ct->staged_status = staged_status;
4840 ct->blob_id = NULL; /* will be filled in when blob gets created */
4841 if (ct->status != GOT_STATUS_ADD &&
4842 ct->staged_status != GOT_STATUS_ADD) {
4843 ct->base_blob_id = got_object_id_dup(blob_id);
4844 if (ct->base_blob_id == NULL) {
4845 err = got_error_from_errno("got_object_id_dup");
4848 ct->base_commit_id = got_object_id_dup(commit_id);
4849 if (ct->base_commit_id == NULL) {
4850 err = got_error_from_errno("got_object_id_dup");
4854 if (ct->staged_status == GOT_STATUS_ADD ||
4855 ct->staged_status == GOT_STATUS_MODIFY) {
4856 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4857 if (ct->staged_blob_id == NULL) {
4858 err = got_error_from_errno("got_object_id_dup");
4862 ct->path = strdup(path);
4863 if (ct->path == NULL) {
4864 err = got_error_from_errno("strdup");
4867 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4869 if (ct && (err || new == NULL))
4870 free_commitable(ct);
4876 static const struct got_error *write_tree(struct got_object_id **, int *,
4877 struct got_tree_object *, const char *, struct got_pathlist_head *,
4878 got_worktree_status_cb status_cb, void *status_arg,
4879 struct got_repository *);
4881 static const struct got_error *
4882 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4883 struct got_tree_entry *te, const char *parent_path,
4884 struct got_pathlist_head *commitable_paths,
4885 got_worktree_status_cb status_cb, void *status_arg,
4886 struct got_repository *repo)
4888 const struct got_error *err = NULL;
4889 struct got_tree_object *subtree;
4892 if (asprintf(&subpath, "%s%s%s", parent_path,
4893 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4894 return got_error_from_errno("asprintf");
4896 err = got_object_open_as_tree(&subtree, repo, &te->id);
4900 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4901 commitable_paths, status_cb, status_arg, repo);
4902 got_object_tree_close(subtree);
4907 static const struct got_error *
4908 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4910 const struct got_error *err = NULL;
4911 char *ct_parent_path = NULL;
4915 if (strchr(ct->in_repo_path, '/') == NULL) {
4916 *match = got_path_is_root_dir(path);
4920 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4923 *match = (strcmp(path, ct_parent_path) == 0);
4924 free(ct_parent_path);
4929 get_ct_file_mode(struct got_commitable *ct)
4931 if (S_ISLNK(ct->mode))
4934 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4937 static const struct got_error *
4938 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4939 struct got_tree_entry *te, struct got_commitable *ct)
4941 const struct got_error *err = NULL;
4945 err = got_object_tree_entry_dup(new_te, te);
4949 (*new_te)->mode = get_ct_file_mode(ct);
4951 if (ct->staged_status == GOT_STATUS_MODIFY)
4952 memcpy(&(*new_te)->id, ct->staged_blob_id,
4953 sizeof((*new_te)->id));
4955 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4957 if (err && *new_te) {
4964 static const struct got_error *
4965 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4966 struct got_commitable *ct)
4968 const struct got_error *err = NULL;
4969 char *ct_name = NULL;
4973 *new_te = calloc(1, sizeof(**new_te));
4974 if (*new_te == NULL)
4975 return got_error_from_errno("calloc");
4977 err = got_path_basename(&ct_name, ct->path);
4980 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4981 sizeof((*new_te)->name)) {
4982 err = got_error(GOT_ERR_NO_SPACE);
4986 (*new_te)->mode = get_ct_file_mode(ct);
4988 if (ct->staged_status == GOT_STATUS_ADD)
4989 memcpy(&(*new_te)->id, ct->staged_blob_id,
4990 sizeof((*new_te)->id));
4992 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4995 if (err && *new_te) {
5002 static const struct got_error *
5003 insert_tree_entry(struct got_tree_entry *new_te,
5004 struct got_pathlist_head *paths)
5006 const struct got_error *err = NULL;
5007 struct got_pathlist_entry *new_pe;
5009 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5013 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5017 static const struct got_error *
5018 report_ct_status(struct got_commitable *ct,
5019 got_worktree_status_cb status_cb, void *status_arg)
5021 const char *ct_path = ct->path;
5022 unsigned char status;
5024 while (ct_path[0] == '/')
5027 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5028 status = ct->staged_status;
5030 status = ct->status;
5032 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5033 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5036 static const struct got_error *
5037 match_modified_subtree(int *modified, struct got_tree_entry *te,
5038 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5040 const struct got_error *err = NULL;
5041 struct got_pathlist_entry *pe;
5046 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5047 got_path_is_root_dir(base_tree_path) ? "" : "/",
5049 return got_error_from_errno("asprintf");
5051 TAILQ_FOREACH(pe, commitable_paths, entry) {
5052 struct got_commitable *ct = pe->data;
5053 *modified = got_path_is_child(ct->in_repo_path, te_path,
5063 static const struct got_error *
5064 match_deleted_or_modified_ct(struct got_commitable **ctp,
5065 struct got_tree_entry *te, const char *base_tree_path,
5066 struct got_pathlist_head *commitable_paths)
5068 const struct got_error *err = NULL;
5069 struct got_pathlist_entry *pe;
5073 TAILQ_FOREACH(pe, commitable_paths, entry) {
5074 struct got_commitable *ct = pe->data;
5075 char *ct_name = NULL;
5078 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5079 if (ct->status != GOT_STATUS_MODIFY &&
5080 ct->status != GOT_STATUS_MODE_CHANGE &&
5081 ct->status != GOT_STATUS_DELETE)
5084 if (ct->staged_status != GOT_STATUS_MODIFY &&
5085 ct->staged_status != GOT_STATUS_DELETE)
5089 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5092 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5098 err = got_path_basename(&ct_name, pe->path);
5102 if (strcmp(te->name, ct_name) != 0) {
5115 static const struct got_error *
5116 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5117 const char *child_path, const char *path_base_tree,
5118 struct got_pathlist_head *commitable_paths,
5119 got_worktree_status_cb status_cb, void *status_arg,
5120 struct got_repository *repo)
5122 const struct got_error *err = NULL;
5123 struct got_tree_entry *new_te;
5125 struct got_object_id *id = NULL;
5130 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5131 got_path_is_root_dir(path_base_tree) ? "" : "/",
5133 return got_error_from_errno("asprintf");
5135 new_te = calloc(1, sizeof(*new_te));
5137 return got_error_from_errno("calloc");
5138 new_te->mode = S_IFDIR;
5140 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5141 sizeof(new_te->name)) {
5142 err = got_error(GOT_ERR_NO_SPACE);
5145 err = write_tree(&id, &nentries, NULL, subtree_path,
5146 commitable_paths, status_cb, status_arg, repo);
5151 memcpy(&new_te->id, id, sizeof(new_te->id));
5160 static const struct got_error *
5161 write_tree(struct got_object_id **new_tree_id, int *nentries,
5162 struct got_tree_object *base_tree, const char *path_base_tree,
5163 struct got_pathlist_head *commitable_paths,
5164 got_worktree_status_cb status_cb, void *status_arg,
5165 struct got_repository *repo)
5167 const struct got_error *err = NULL;
5168 struct got_pathlist_head paths;
5169 struct got_tree_entry *te, *new_te = NULL;
5170 struct got_pathlist_entry *pe;
5175 /* Insert, and recurse into, newly added entries first. */
5176 TAILQ_FOREACH(pe, commitable_paths, entry) {
5177 struct got_commitable *ct = pe->data;
5178 char *child_path = NULL, *slash;
5180 if ((ct->status != GOT_STATUS_ADD &&
5181 ct->staged_status != GOT_STATUS_ADD) ||
5182 (ct->flags & GOT_COMMITABLE_ADDED))
5185 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5186 strlen(path_base_tree)))
5189 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5194 slash = strchr(child_path, '/');
5195 if (slash == NULL) {
5196 err = alloc_added_blob_tree_entry(&new_te, ct);
5199 err = report_ct_status(ct, status_cb, status_arg);
5202 ct->flags |= GOT_COMMITABLE_ADDED;
5203 err = insert_tree_entry(new_te, &paths);
5208 *slash = '\0'; /* trim trailing path components */
5209 if (base_tree == NULL ||
5210 got_object_tree_find_entry(base_tree, child_path)
5212 err = make_subtree_for_added_blob(&new_te,
5213 child_path, path_base_tree,
5214 commitable_paths, status_cb, status_arg,
5218 err = insert_tree_entry(new_te, &paths);
5227 int i, nbase_entries;
5228 /* Handle modified and deleted entries. */
5229 nbase_entries = got_object_tree_get_nentries(base_tree);
5230 for (i = 0; i < nbase_entries; i++) {
5231 struct got_commitable *ct = NULL;
5233 te = got_object_tree_get_entry(base_tree, i);
5234 if (got_object_tree_entry_is_submodule(te)) {
5235 /* Entry is a submodule; just copy it. */
5236 err = got_object_tree_entry_dup(&new_te, te);
5239 err = insert_tree_entry(new_te, &paths);
5246 if (S_ISDIR(te->mode)) {
5248 err = got_object_tree_entry_dup(&new_te, te);
5251 err = match_modified_subtree(&modified, te,
5252 path_base_tree, commitable_paths);
5255 /* Avoid recursion into unmodified subtrees. */
5257 struct got_object_id *new_id;
5259 err = write_subtree(&new_id,
5261 path_base_tree, commitable_paths,
5262 status_cb, status_arg, repo);
5265 if (nsubentries == 0) {
5266 /* All entries were deleted. */
5270 memcpy(&new_te->id, new_id,
5271 sizeof(new_te->id));
5274 err = insert_tree_entry(new_te, &paths);
5281 err = match_deleted_or_modified_ct(&ct, te,
5282 path_base_tree, commitable_paths);
5286 /* NB: Deleted entries get dropped here. */
5287 if (ct->status == GOT_STATUS_MODIFY ||
5288 ct->status == GOT_STATUS_MODE_CHANGE ||
5289 ct->staged_status == GOT_STATUS_MODIFY) {
5290 err = alloc_modified_blob_tree_entry(
5294 err = insert_tree_entry(new_te, &paths);
5299 err = report_ct_status(ct, status_cb,
5304 /* Entry is unchanged; just copy it. */
5305 err = got_object_tree_entry_dup(&new_te, te);
5308 err = insert_tree_entry(new_te, &paths);
5316 /* Write new list of entries; deleted entries have been dropped. */
5317 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5319 got_pathlist_free(&paths);
5323 static const struct got_error *
5324 update_fileindex_after_commit(struct got_worktree *worktree,
5325 struct got_pathlist_head *commitable_paths,
5326 struct got_object_id *new_base_commit_id,
5327 struct got_fileindex *fileindex, int have_staged_files)
5329 const struct got_error *err = NULL;
5330 struct got_pathlist_entry *pe;
5331 char *relpath = NULL;
5333 TAILQ_FOREACH(pe, commitable_paths, entry) {
5334 struct got_fileindex_entry *ie;
5335 struct got_commitable *ct = pe->data;
5337 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5339 err = got_path_skip_common_ancestor(&relpath,
5340 worktree->root_path, ct->ondisk_path);
5345 if (ct->status == GOT_STATUS_DELETE ||
5346 ct->staged_status == GOT_STATUS_DELETE) {
5347 got_fileindex_entry_remove(fileindex, ie);
5348 } else if (ct->staged_status == GOT_STATUS_ADD ||
5349 ct->staged_status == GOT_STATUS_MODIFY) {
5350 got_fileindex_entry_stage_set(ie,
5351 GOT_FILEIDX_STAGE_NONE);
5352 got_fileindex_entry_staged_filetype_set(ie, 0);
5354 err = got_fileindex_entry_update(ie,
5355 worktree->root_fd, relpath,
5356 ct->staged_blob_id->sha1,
5357 new_base_commit_id->sha1,
5358 !have_staged_files);
5360 err = got_fileindex_entry_update(ie,
5361 worktree->root_fd, relpath,
5363 new_base_commit_id->sha1,
5364 !have_staged_files);
5366 err = got_fileindex_entry_alloc(&ie, pe->path);
5369 err = got_fileindex_entry_update(ie,
5370 worktree->root_fd, relpath, ct->blob_id->sha1,
5371 new_base_commit_id->sha1, 1);
5373 got_fileindex_entry_free(ie);
5376 err = got_fileindex_entry_add(fileindex, ie);
5378 got_fileindex_entry_free(ie);
5391 static const struct got_error *
5392 check_out_of_date(const char *in_repo_path, unsigned char status,
5393 unsigned char staged_status, struct got_object_id *base_blob_id,
5394 struct got_object_id *base_commit_id,
5395 struct got_object_id *head_commit_id, struct got_repository *repo,
5398 const struct got_error *err = NULL;
5399 struct got_object_id *id = NULL;
5401 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5402 /* Trivial case: base commit == head commit */
5403 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5406 * Ensure file content which local changes were based
5407 * on matches file content in the branch head.
5409 err = got_object_id_by_path(&id, repo, head_commit_id,
5412 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5413 err = got_error(ood_errcode);
5415 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5416 err = got_error(ood_errcode);
5418 /* Require that added files don't exist in the branch head. */
5419 err = got_object_id_by_path(&id, repo, head_commit_id,
5421 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5423 err = id ? got_error(ood_errcode) : NULL;
5430 const struct got_error *
5431 commit_worktree(struct got_object_id **new_commit_id,
5432 struct got_pathlist_head *commitable_paths,
5433 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5434 const char *author, const char *committer,
5435 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5436 got_worktree_status_cb status_cb, void *status_arg,
5437 struct got_repository *repo)
5439 const struct got_error *err = NULL, *unlockerr = NULL;
5440 struct got_pathlist_entry *pe;
5441 const char *head_ref_name = NULL;
5442 struct got_commit_object *head_commit = NULL;
5443 struct got_reference *head_ref2 = NULL;
5444 struct got_object_id *head_commit_id2 = NULL;
5445 struct got_tree_object *head_tree = NULL;
5446 struct got_object_id *new_tree_id = NULL;
5448 struct got_object_id_queue parent_ids;
5449 struct got_object_qid *pid = NULL;
5450 char *logmsg = NULL;
5452 *new_commit_id = NULL;
5454 SIMPLEQ_INIT(&parent_ids);
5456 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5460 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5464 if (commit_msg_cb != NULL) {
5465 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5470 if (logmsg == NULL || strlen(logmsg) == 0) {
5471 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5475 /* Create blobs from added and modified files and record their IDs. */
5476 TAILQ_FOREACH(pe, commitable_paths, entry) {
5477 struct got_commitable *ct = pe->data;
5480 /* Blobs for staged files already exist. */
5481 if (ct->staged_status == GOT_STATUS_ADD ||
5482 ct->staged_status == GOT_STATUS_MODIFY)
5485 if (ct->status != GOT_STATUS_ADD &&
5486 ct->status != GOT_STATUS_MODIFY &&
5487 ct->status != GOT_STATUS_MODE_CHANGE)
5490 if (asprintf(&ondisk_path, "%s/%s",
5491 worktree->root_path, pe->path) == -1) {
5492 err = got_error_from_errno("asprintf");
5495 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5501 /* Recursively write new tree objects. */
5502 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5503 commitable_paths, status_cb, status_arg, repo);
5507 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5510 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5511 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5512 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5513 got_object_qid_free(pid);
5519 /* Check if a concurrent commit to our branch has occurred. */
5520 head_ref_name = got_worktree_get_head_ref_name(worktree);
5521 if (head_ref_name == NULL) {
5522 err = got_error_from_errno("got_worktree_get_head_ref_name");
5525 /* Lock the reference here to prevent concurrent modification. */
5526 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5529 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5532 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5533 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5536 /* Update branch head in repository. */
5537 err = got_ref_change_ref(head_ref2, *new_commit_id);
5540 err = got_ref_write(head_ref2, repo);
5544 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5548 err = ref_base_commit(worktree, repo);
5553 got_object_tree_close(head_tree);
5555 got_object_commit_close(head_commit);
5556 free(head_commit_id2);
5558 unlockerr = got_ref_unlock(head_ref2);
5559 if (unlockerr && err == NULL)
5561 got_ref_close(head_ref2);
5566 static const struct got_error *
5567 check_path_is_commitable(const char *path,
5568 struct got_pathlist_head *commitable_paths)
5570 struct got_pathlist_entry *cpe = NULL;
5571 size_t path_len = strlen(path);
5573 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5574 struct got_commitable *ct = cpe->data;
5575 const char *ct_path = ct->path;
5577 while (ct_path[0] == '/')
5580 if (strcmp(path, ct_path) == 0 ||
5581 got_path_is_child(ct_path, path, path_len))
5586 return got_error_path(path, GOT_ERR_BAD_PATH);
5591 static const struct got_error *
5592 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5594 int *have_staged_files = arg;
5596 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5597 *have_staged_files = 1;
5598 return got_error(GOT_ERR_CANCELLED);
5604 static const struct got_error *
5605 check_non_staged_files(struct got_fileindex *fileindex,
5606 struct got_pathlist_head *paths)
5608 struct got_pathlist_entry *pe;
5609 struct got_fileindex_entry *ie;
5611 TAILQ_FOREACH(pe, paths, entry) {
5612 if (pe->path[0] == '\0')
5614 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5616 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5617 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5618 return got_error_path(pe->path,
5619 GOT_ERR_FILE_NOT_STAGED);
5625 const struct got_error *
5626 got_worktree_commit(struct got_object_id **new_commit_id,
5627 struct got_worktree *worktree, struct got_pathlist_head *paths,
5628 const char *author, const char *committer, int allow_bad_symlinks,
5629 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5630 got_worktree_status_cb status_cb, void *status_arg,
5631 struct got_repository *repo)
5633 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5634 struct got_fileindex *fileindex = NULL;
5635 char *fileindex_path = NULL;
5636 struct got_pathlist_head commitable_paths;
5637 struct collect_commitables_arg cc_arg;
5638 struct got_pathlist_entry *pe;
5639 struct got_reference *head_ref = NULL;
5640 struct got_object_id *head_commit_id = NULL;
5641 int have_staged_files = 0;
5643 *new_commit_id = NULL;
5645 TAILQ_INIT(&commitable_paths);
5647 err = lock_worktree(worktree, LOCK_EX);
5651 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5655 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5659 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5663 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5664 &have_staged_files);
5665 if (err && err->code != GOT_ERR_CANCELLED)
5667 if (have_staged_files) {
5668 err = check_non_staged_files(fileindex, paths);
5673 cc_arg.commitable_paths = &commitable_paths;
5674 cc_arg.worktree = worktree;
5675 cc_arg.fileindex = fileindex;
5677 cc_arg.have_staged_files = have_staged_files;
5678 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5679 TAILQ_FOREACH(pe, paths, entry) {
5680 err = worktree_status(worktree, pe->path, fileindex, repo,
5681 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5686 if (TAILQ_EMPTY(&commitable_paths)) {
5687 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5691 TAILQ_FOREACH(pe, paths, entry) {
5692 err = check_path_is_commitable(pe->path, &commitable_paths);
5697 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5698 struct got_commitable *ct = pe->data;
5699 const char *ct_path = ct->in_repo_path;
5701 while (ct_path[0] == '/')
5703 err = check_out_of_date(ct_path, ct->status,
5704 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5705 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5711 err = commit_worktree(new_commit_id, &commitable_paths,
5712 head_commit_id, worktree, author, committer,
5713 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5717 err = update_fileindex_after_commit(worktree, &commitable_paths,
5718 *new_commit_id, fileindex, have_staged_files);
5719 sync_err = sync_fileindex(fileindex, fileindex_path);
5720 if (sync_err && err == NULL)
5724 got_fileindex_free(fileindex);
5725 free(fileindex_path);
5726 unlockerr = lock_worktree(worktree, LOCK_SH);
5727 if (unlockerr && err == NULL)
5729 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5730 struct got_commitable *ct = pe->data;
5731 free_commitable(ct);
5733 got_pathlist_free(&commitable_paths);
5738 got_commitable_get_path(struct got_commitable *ct)
5744 got_commitable_get_status(struct got_commitable *ct)
5749 struct check_rebase_ok_arg {
5750 struct got_worktree *worktree;
5751 struct got_repository *repo;
5754 static const struct got_error *
5755 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5757 const struct got_error *err = NULL;
5758 struct check_rebase_ok_arg *a = arg;
5759 unsigned char status;
5763 /* Reject rebase of a work tree with mixed base commits. */
5764 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5765 SHA1_DIGEST_LENGTH))
5766 return got_error(GOT_ERR_MIXED_COMMITS);
5768 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5770 return got_error_from_errno("asprintf");
5772 /* Reject rebase of a work tree with modified or staged files. */
5773 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5778 if (status != GOT_STATUS_NO_CHANGE)
5779 return got_error(GOT_ERR_MODIFIED);
5780 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5781 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5786 const struct got_error *
5787 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5788 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5789 struct got_worktree *worktree, struct got_reference *branch,
5790 struct got_repository *repo)
5792 const struct got_error *err = NULL;
5793 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5794 char *branch_ref_name = NULL;
5795 char *fileindex_path = NULL;
5796 struct check_rebase_ok_arg ok_arg;
5797 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5798 struct got_object_id *wt_branch_tip = NULL;
5800 *new_base_branch_ref = NULL;
5804 err = lock_worktree(worktree, LOCK_EX);
5808 err = open_fileindex(fileindex, &fileindex_path, worktree);
5812 ok_arg.worktree = worktree;
5814 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5819 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5823 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5827 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5831 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5836 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5839 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5840 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5844 err = got_ref_alloc_symref(new_base_branch_ref,
5845 new_base_branch_ref_name, wt_branch);
5848 err = got_ref_write(*new_base_branch_ref, repo);
5852 /* TODO Lock original branch's ref while rebasing? */
5854 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5858 err = got_ref_write(branch_ref, repo);
5862 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5863 worktree->base_commit_id);
5866 err = got_ref_write(*tmp_branch, repo);
5870 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5874 free(fileindex_path);
5875 free(tmp_branch_name);
5876 free(new_base_branch_ref_name);
5877 free(branch_ref_name);
5879 got_ref_close(branch_ref);
5881 got_ref_close(wt_branch);
5882 free(wt_branch_tip);
5884 if (*new_base_branch_ref) {
5885 got_ref_close(*new_base_branch_ref);
5886 *new_base_branch_ref = NULL;
5889 got_ref_close(*tmp_branch);
5893 got_fileindex_free(*fileindex);
5896 lock_worktree(worktree, LOCK_SH);
5901 const struct got_error *
5902 got_worktree_rebase_continue(struct got_object_id **commit_id,
5903 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5904 struct got_reference **branch, struct got_fileindex **fileindex,
5905 struct got_worktree *worktree, struct got_repository *repo)
5907 const struct got_error *err;
5908 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5909 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5910 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5911 char *fileindex_path = NULL;
5912 int have_staged_files = 0;
5915 *new_base_branch = NULL;
5920 err = lock_worktree(worktree, LOCK_EX);
5924 err = open_fileindex(fileindex, &fileindex_path, worktree);
5928 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5929 &have_staged_files);
5930 if (err && err->code != GOT_ERR_CANCELLED)
5932 if (have_staged_files) {
5933 err = got_error(GOT_ERR_STAGED_PATHS);
5937 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5941 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5945 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5949 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5953 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5957 err = got_ref_open(branch, repo,
5958 got_ref_get_symref_target(branch_ref), 0);
5962 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5966 err = got_ref_resolve(commit_id, repo, commit_ref);
5970 err = got_ref_open(new_base_branch, repo,
5971 new_base_branch_ref_name, 0);
5975 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5979 free(commit_ref_name);
5980 free(branch_ref_name);
5981 free(fileindex_path);
5983 got_ref_close(commit_ref);
5985 got_ref_close(branch_ref);
5990 got_ref_close(*tmp_branch);
5993 if (*new_base_branch) {
5994 got_ref_close(*new_base_branch);
5995 *new_base_branch = NULL;
5998 got_ref_close(*branch);
6002 got_fileindex_free(*fileindex);
6005 lock_worktree(worktree, LOCK_SH);
6010 const struct got_error *
6011 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6013 const struct got_error *err;
6014 char *tmp_branch_name = NULL;
6016 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6020 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6021 free(tmp_branch_name);
6025 static const struct got_error *
6026 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6027 char **logmsg, void *arg)
6033 static const struct got_error *
6034 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6035 const char *path, struct got_object_id *blob_id,
6036 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6037 int dirfd, const char *de_name)
6042 struct collect_merged_paths_arg {
6043 got_worktree_checkout_cb progress_cb;
6045 struct got_pathlist_head *merged_paths;
6048 static const struct got_error *
6049 collect_merged_paths(void *arg, unsigned char status, const char *path)
6051 const struct got_error *err;
6052 struct collect_merged_paths_arg *a = arg;
6054 struct got_pathlist_entry *new;
6056 err = (*a->progress_cb)(a->progress_arg, status, path);
6060 if (status != GOT_STATUS_MERGE &&
6061 status != GOT_STATUS_ADD &&
6062 status != GOT_STATUS_DELETE &&
6063 status != GOT_STATUS_CONFLICT)
6068 return got_error_from_errno("strdup");
6070 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6071 if (err || new == NULL)
6077 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6079 struct got_pathlist_entry *pe;
6081 TAILQ_FOREACH(pe, merged_paths, entry)
6082 free((char *)pe->path);
6084 got_pathlist_free(merged_paths);
6087 static const struct got_error *
6088 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6089 int is_rebase, struct got_repository *repo)
6091 const struct got_error *err;
6092 struct got_reference *commit_ref = NULL;
6094 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6096 if (err->code != GOT_ERR_NOT_REF)
6098 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6101 err = got_ref_write(commit_ref, repo);
6104 } else if (is_rebase) {
6105 struct got_object_id *stored_id;
6108 err = got_ref_resolve(&stored_id, repo, commit_ref);
6111 cmp = got_object_id_cmp(commit_id, stored_id);
6114 err = got_error(GOT_ERR_REBASE_COMMITID);
6120 got_ref_close(commit_ref);
6124 static const struct got_error *
6125 rebase_merge_files(struct got_pathlist_head *merged_paths,
6126 const char *commit_ref_name, struct got_worktree *worktree,
6127 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6128 struct got_object_id *commit_id, struct got_repository *repo,
6129 got_worktree_checkout_cb progress_cb, void *progress_arg,
6130 got_cancel_cb cancel_cb, void *cancel_arg)
6132 const struct got_error *err;
6133 struct got_reference *commit_ref = NULL;
6134 struct collect_merged_paths_arg cmp_arg;
6135 char *fileindex_path;
6137 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6139 err = get_fileindex_path(&fileindex_path, worktree);
6143 cmp_arg.progress_cb = progress_cb;
6144 cmp_arg.progress_arg = progress_arg;
6145 cmp_arg.merged_paths = merged_paths;
6146 err = merge_files(worktree, fileindex, fileindex_path,
6147 parent_commit_id, commit_id, repo, collect_merged_paths,
6148 &cmp_arg, cancel_cb, cancel_arg);
6150 got_ref_close(commit_ref);
6154 const struct got_error *
6155 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6156 struct got_worktree *worktree, struct got_fileindex *fileindex,
6157 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6158 struct got_repository *repo,
6159 got_worktree_checkout_cb progress_cb, void *progress_arg,
6160 got_cancel_cb cancel_cb, void *cancel_arg)
6162 const struct got_error *err;
6163 char *commit_ref_name;
6165 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6169 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6173 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6174 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6175 progress_arg, cancel_cb, cancel_arg);
6177 free(commit_ref_name);
6181 const struct got_error *
6182 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6183 struct got_worktree *worktree, struct got_fileindex *fileindex,
6184 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6185 struct got_repository *repo,
6186 got_worktree_checkout_cb progress_cb, void *progress_arg,
6187 got_cancel_cb cancel_cb, void *cancel_arg)
6189 const struct got_error *err;
6190 char *commit_ref_name;
6192 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6196 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6200 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6201 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6202 progress_arg, cancel_cb, cancel_arg);
6204 free(commit_ref_name);
6208 static const struct got_error *
6209 rebase_commit(struct got_object_id **new_commit_id,
6210 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6211 struct got_worktree *worktree, struct got_fileindex *fileindex,
6212 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6213 const char *new_logmsg, struct got_repository *repo)
6215 const struct got_error *err, *sync_err;
6216 struct got_pathlist_head commitable_paths;
6217 struct collect_commitables_arg cc_arg;
6218 char *fileindex_path = NULL;
6219 struct got_reference *head_ref = NULL;
6220 struct got_object_id *head_commit_id = NULL;
6221 char *logmsg = NULL;
6223 TAILQ_INIT(&commitable_paths);
6224 *new_commit_id = NULL;
6226 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6228 err = get_fileindex_path(&fileindex_path, worktree);
6232 cc_arg.commitable_paths = &commitable_paths;
6233 cc_arg.worktree = worktree;
6235 cc_arg.have_staged_files = 0;
6237 * If possible get the status of individual files directly to
6238 * avoid crawling the entire work tree once per rebased commit.
6239 * TODO: Ideally, merged_paths would contain a list of commitables
6240 * we could use so we could skip worktree_status() entirely.
6243 struct got_pathlist_entry *pe;
6244 TAILQ_FOREACH(pe, merged_paths, entry) {
6245 err = worktree_status(worktree, pe->path, fileindex,
6246 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6252 err = worktree_status(worktree, "", fileindex, repo,
6253 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6258 if (TAILQ_EMPTY(&commitable_paths)) {
6259 /* No-op change; commit will be elided. */
6260 err = got_ref_delete(commit_ref, repo);
6263 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6267 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6271 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6276 logmsg = strdup(new_logmsg);
6277 if (logmsg == NULL) {
6278 err = got_error_from_errno("strdup");
6282 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6287 /* NB: commit_worktree will call free(logmsg) */
6288 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6289 worktree, got_object_commit_get_author(orig_commit),
6290 got_object_commit_get_committer(orig_commit),
6291 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6295 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6299 err = got_ref_delete(commit_ref, repo);
6303 err = update_fileindex_after_commit(worktree, &commitable_paths,
6304 *new_commit_id, fileindex, 0);
6305 sync_err = sync_fileindex(fileindex, fileindex_path);
6306 if (sync_err && err == NULL)
6309 free(fileindex_path);
6310 free(head_commit_id);
6312 got_ref_close(head_ref);
6314 free(*new_commit_id);
6315 *new_commit_id = NULL;
6320 const struct got_error *
6321 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6322 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6323 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6324 struct got_commit_object *orig_commit,
6325 struct got_object_id *orig_commit_id, struct got_repository *repo)
6327 const struct got_error *err;
6328 char *commit_ref_name;
6329 struct got_reference *commit_ref = NULL;
6330 struct got_object_id *commit_id = NULL;
6332 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6336 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6339 err = got_ref_resolve(&commit_id, repo, commit_ref);
6342 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6343 err = got_error(GOT_ERR_REBASE_COMMITID);
6347 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6348 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6351 got_ref_close(commit_ref);
6352 free(commit_ref_name);
6357 const struct got_error *
6358 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6359 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6360 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6361 struct got_commit_object *orig_commit,
6362 struct got_object_id *orig_commit_id, const char *new_logmsg,
6363 struct got_repository *repo)
6365 const struct got_error *err;
6366 char *commit_ref_name;
6367 struct got_reference *commit_ref = NULL;
6369 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6373 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6377 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6378 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6381 got_ref_close(commit_ref);
6382 free(commit_ref_name);
6386 const struct got_error *
6387 got_worktree_rebase_postpone(struct got_worktree *worktree,
6388 struct got_fileindex *fileindex)
6391 got_fileindex_free(fileindex);
6392 return lock_worktree(worktree, LOCK_SH);
6395 static const struct got_error *
6396 delete_ref(const char *name, struct got_repository *repo)
6398 const struct got_error *err;
6399 struct got_reference *ref;
6401 err = got_ref_open(&ref, repo, name, 0);
6403 if (err->code == GOT_ERR_NOT_REF)
6408 err = got_ref_delete(ref, repo);
6413 static const struct got_error *
6414 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6416 const struct got_error *err;
6417 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6418 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6420 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6423 err = delete_ref(tmp_branch_name, repo);
6427 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6430 err = delete_ref(new_base_branch_ref_name, repo);
6434 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6437 err = delete_ref(branch_ref_name, repo);
6441 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6444 err = delete_ref(commit_ref_name, repo);
6449 free(tmp_branch_name);
6450 free(new_base_branch_ref_name);
6451 free(branch_ref_name);
6452 free(commit_ref_name);
6456 const struct got_error *
6457 got_worktree_rebase_complete(struct got_worktree *worktree,
6458 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6459 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6460 struct got_repository *repo)
6462 const struct got_error *err, *unlockerr, *sync_err;
6463 struct got_object_id *new_head_commit_id = NULL;
6464 char *fileindex_path = NULL;
6466 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6470 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6474 err = got_ref_write(rebased_branch, repo);
6478 err = got_worktree_set_head_ref(worktree, rebased_branch);
6482 err = delete_rebase_refs(worktree, repo);
6486 err = get_fileindex_path(&fileindex_path, worktree);
6489 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6490 sync_err = sync_fileindex(fileindex, fileindex_path);
6491 if (sync_err && err == NULL)
6494 got_fileindex_free(fileindex);
6495 free(fileindex_path);
6496 free(new_head_commit_id);
6497 unlockerr = lock_worktree(worktree, LOCK_SH);
6498 if (unlockerr && err == NULL)
6503 const struct got_error *
6504 got_worktree_rebase_abort(struct got_worktree *worktree,
6505 struct got_fileindex *fileindex, struct got_repository *repo,
6506 struct got_reference *new_base_branch,
6507 got_worktree_checkout_cb progress_cb, void *progress_arg)
6509 const struct got_error *err, *unlockerr, *sync_err;
6510 struct got_reference *resolved = NULL;
6511 struct got_object_id *commit_id = NULL;
6512 char *fileindex_path = NULL;
6513 struct revert_file_args rfa;
6514 struct got_object_id *tree_id = NULL;
6516 err = lock_worktree(worktree, LOCK_EX);
6520 err = got_ref_open(&resolved, repo,
6521 got_ref_get_symref_target(new_base_branch), 0);
6525 err = got_worktree_set_head_ref(worktree, resolved);
6530 * XXX commits to the base branch could have happened while
6531 * we were busy rebasing; should we store the original commit ID
6532 * when rebase begins and read it back here?
6534 err = got_ref_resolve(&commit_id, repo, resolved);
6538 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6542 err = got_object_id_by_path(&tree_id, repo,
6543 worktree->base_commit_id, worktree->path_prefix);
6547 err = delete_rebase_refs(worktree, repo);
6551 err = get_fileindex_path(&fileindex_path, worktree);
6555 rfa.worktree = worktree;
6556 rfa.fileindex = fileindex;
6557 rfa.progress_cb = progress_cb;
6558 rfa.progress_arg = progress_arg;
6559 rfa.patch_cb = NULL;
6560 rfa.patch_arg = NULL;
6562 err = worktree_status(worktree, "", fileindex, repo,
6563 revert_file, &rfa, NULL, NULL, 0, 0);
6567 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6568 repo, progress_cb, progress_arg, NULL, NULL);
6570 sync_err = sync_fileindex(fileindex, fileindex_path);
6571 if (sync_err && err == NULL)
6574 got_ref_close(resolved);
6578 got_fileindex_free(fileindex);
6579 free(fileindex_path);
6581 unlockerr = lock_worktree(worktree, LOCK_SH);
6582 if (unlockerr && err == NULL)
6587 const struct got_error *
6588 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6589 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6590 struct got_fileindex **fileindex, struct got_worktree *worktree,
6591 struct got_repository *repo)
6593 const struct got_error *err = NULL;
6594 char *tmp_branch_name = NULL;
6595 char *branch_ref_name = NULL;
6596 char *base_commit_ref_name = NULL;
6597 char *fileindex_path = NULL;
6598 struct check_rebase_ok_arg ok_arg;
6599 struct got_reference *wt_branch = NULL;
6600 struct got_reference *base_commit_ref = NULL;
6604 *base_commit_id = NULL;
6607 err = lock_worktree(worktree, LOCK_EX);
6611 err = open_fileindex(fileindex, &fileindex_path, worktree);
6615 ok_arg.worktree = worktree;
6617 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6622 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6626 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6630 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6635 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6640 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6644 err = got_ref_write(*branch_ref, repo);
6648 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6649 worktree->base_commit_id);
6652 err = got_ref_write(base_commit_ref, repo);
6655 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6656 if (*base_commit_id == NULL) {
6657 err = got_error_from_errno("got_object_id_dup");
6661 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6662 worktree->base_commit_id);
6665 err = got_ref_write(*tmp_branch, repo);
6669 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6673 free(fileindex_path);
6674 free(tmp_branch_name);
6675 free(branch_ref_name);
6676 free(base_commit_ref_name);
6678 got_ref_close(wt_branch);
6681 got_ref_close(*branch_ref);
6685 got_ref_close(*tmp_branch);
6688 free(*base_commit_id);
6690 got_fileindex_free(*fileindex);
6693 lock_worktree(worktree, LOCK_SH);
6698 const struct got_error *
6699 got_worktree_histedit_postpone(struct got_worktree *worktree,
6700 struct got_fileindex *fileindex)
6703 got_fileindex_free(fileindex);
6704 return lock_worktree(worktree, LOCK_SH);
6707 const struct got_error *
6708 got_worktree_histedit_in_progress(int *in_progress,
6709 struct got_worktree *worktree)
6711 const struct got_error *err;
6712 char *tmp_branch_name = NULL;
6714 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6718 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6719 free(tmp_branch_name);
6723 const struct got_error *
6724 got_worktree_histedit_continue(struct got_object_id **commit_id,
6725 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6726 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6727 struct got_worktree *worktree, struct got_repository *repo)
6729 const struct got_error *err;
6730 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6731 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6732 struct got_reference *commit_ref = NULL;
6733 struct got_reference *base_commit_ref = NULL;
6734 char *fileindex_path = NULL;
6735 int have_staged_files = 0;
6739 *base_commit_id = NULL;
6742 err = lock_worktree(worktree, LOCK_EX);
6746 err = open_fileindex(fileindex, &fileindex_path, worktree);
6750 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6751 &have_staged_files);
6752 if (err && err->code != GOT_ERR_CANCELLED)
6754 if (have_staged_files) {
6755 err = got_error(GOT_ERR_STAGED_PATHS);
6759 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6763 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6767 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6771 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6776 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6780 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6783 err = got_ref_resolve(commit_id, repo, commit_ref);
6787 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6790 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6794 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6798 free(commit_ref_name);
6799 free(branch_ref_name);
6800 free(fileindex_path);
6802 got_ref_close(commit_ref);
6803 if (base_commit_ref)
6804 got_ref_close(base_commit_ref);
6808 free(*base_commit_id);
6809 *base_commit_id = NULL;
6811 got_ref_close(*tmp_branch);
6815 got_fileindex_free(*fileindex);
6818 lock_worktree(worktree, LOCK_EX);
6823 static const struct got_error *
6824 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6826 const struct got_error *err;
6827 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6828 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6830 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6833 err = delete_ref(tmp_branch_name, repo);
6837 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6841 err = delete_ref(base_commit_ref_name, repo);
6845 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6848 err = delete_ref(branch_ref_name, repo);
6852 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6855 err = delete_ref(commit_ref_name, repo);
6859 free(tmp_branch_name);
6860 free(base_commit_ref_name);
6861 free(branch_ref_name);
6862 free(commit_ref_name);
6866 const struct got_error *
6867 got_worktree_histedit_abort(struct got_worktree *worktree,
6868 struct got_fileindex *fileindex, struct got_repository *repo,
6869 struct got_reference *branch, struct got_object_id *base_commit_id,
6870 got_worktree_checkout_cb progress_cb, void *progress_arg)
6872 const struct got_error *err, *unlockerr, *sync_err;
6873 struct got_reference *resolved = NULL;
6874 char *fileindex_path = NULL;
6875 struct got_object_id *tree_id = NULL;
6876 struct revert_file_args rfa;
6878 err = lock_worktree(worktree, LOCK_EX);
6882 err = got_ref_open(&resolved, repo,
6883 got_ref_get_symref_target(branch), 0);
6887 err = got_worktree_set_head_ref(worktree, resolved);
6891 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6895 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6896 worktree->path_prefix);
6900 err = delete_histedit_refs(worktree, repo);
6904 err = get_fileindex_path(&fileindex_path, worktree);
6908 rfa.worktree = worktree;
6909 rfa.fileindex = fileindex;
6910 rfa.progress_cb = progress_cb;
6911 rfa.progress_arg = progress_arg;
6912 rfa.patch_cb = NULL;
6913 rfa.patch_arg = NULL;
6915 err = worktree_status(worktree, "", fileindex, repo,
6916 revert_file, &rfa, NULL, NULL, 0, 0);
6920 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6921 repo, progress_cb, progress_arg, NULL, NULL);
6923 sync_err = sync_fileindex(fileindex, fileindex_path);
6924 if (sync_err && err == NULL)
6927 got_ref_close(resolved);
6929 free(fileindex_path);
6931 unlockerr = lock_worktree(worktree, LOCK_SH);
6932 if (unlockerr && err == NULL)
6937 const struct got_error *
6938 got_worktree_histedit_complete(struct got_worktree *worktree,
6939 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6940 struct got_reference *edited_branch, struct got_repository *repo)
6942 const struct got_error *err, *unlockerr, *sync_err;
6943 struct got_object_id *new_head_commit_id = NULL;
6944 struct got_reference *resolved = NULL;
6945 char *fileindex_path = NULL;
6947 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6951 err = got_ref_open(&resolved, repo,
6952 got_ref_get_symref_target(edited_branch), 0);
6956 err = got_ref_change_ref(resolved, new_head_commit_id);
6960 err = got_ref_write(resolved, repo);
6964 err = got_worktree_set_head_ref(worktree, resolved);
6968 err = delete_histedit_refs(worktree, repo);
6972 err = get_fileindex_path(&fileindex_path, worktree);
6975 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6976 sync_err = sync_fileindex(fileindex, fileindex_path);
6977 if (sync_err && err == NULL)
6980 got_fileindex_free(fileindex);
6981 free(fileindex_path);
6982 free(new_head_commit_id);
6983 unlockerr = lock_worktree(worktree, LOCK_SH);
6984 if (unlockerr && err == NULL)
6989 const struct got_error *
6990 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6991 struct got_object_id *commit_id, struct got_repository *repo)
6993 const struct got_error *err;
6994 char *commit_ref_name;
6996 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7000 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7004 err = delete_ref(commit_ref_name, repo);
7006 free(commit_ref_name);
7010 const struct got_error *
7011 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7012 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7013 struct got_worktree *worktree, const char *refname,
7014 struct got_repository *repo)
7016 const struct got_error *err = NULL;
7017 char *fileindex_path = NULL;
7018 struct check_rebase_ok_arg ok_arg;
7022 *base_branch_ref = NULL;
7024 err = lock_worktree(worktree, LOCK_EX);
7028 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7029 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7030 "cannot integrate a branch into itself; "
7031 "update -b or different branch name required");
7035 err = open_fileindex(fileindex, &fileindex_path, worktree);
7039 /* Preconditions are the same as for rebase. */
7040 ok_arg.worktree = worktree;
7042 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7047 err = got_ref_open(branch_ref, repo, refname, 1);
7051 err = got_ref_open(base_branch_ref, repo,
7052 got_worktree_get_head_ref_name(worktree), 1);
7056 got_ref_close(*branch_ref);
7059 if (*base_branch_ref) {
7060 got_ref_close(*base_branch_ref);
7061 *base_branch_ref = NULL;
7064 got_fileindex_free(*fileindex);
7067 lock_worktree(worktree, LOCK_SH);
7072 const struct got_error *
7073 got_worktree_integrate_continue(struct got_worktree *worktree,
7074 struct got_fileindex *fileindex, struct got_repository *repo,
7075 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7076 got_worktree_checkout_cb progress_cb, void *progress_arg,
7077 got_cancel_cb cancel_cb, void *cancel_arg)
7079 const struct got_error *err = NULL, *sync_err, *unlockerr;
7080 char *fileindex_path = NULL;
7081 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7083 err = get_fileindex_path(&fileindex_path, worktree);
7087 err = got_ref_resolve(&commit_id, repo, branch_ref);
7091 err = got_object_id_by_path(&tree_id, repo, commit_id,
7092 worktree->path_prefix);
7096 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7100 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7101 progress_cb, progress_arg, cancel_cb, cancel_arg);
7105 err = got_ref_change_ref(base_branch_ref, commit_id);
7109 err = got_ref_write(base_branch_ref, repo);
7111 sync_err = sync_fileindex(fileindex, fileindex_path);
7112 if (sync_err && err == NULL)
7116 unlockerr = got_ref_unlock(branch_ref);
7117 if (unlockerr && err == NULL)
7119 got_ref_close(branch_ref);
7121 unlockerr = got_ref_unlock(base_branch_ref);
7122 if (unlockerr && err == NULL)
7124 got_ref_close(base_branch_ref);
7126 got_fileindex_free(fileindex);
7127 free(fileindex_path);
7130 unlockerr = lock_worktree(worktree, LOCK_SH);
7131 if (unlockerr && err == NULL)
7136 const struct got_error *
7137 got_worktree_integrate_abort(struct got_worktree *worktree,
7138 struct got_fileindex *fileindex, struct got_repository *repo,
7139 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7141 const struct got_error *err = NULL, *unlockerr = NULL;
7143 got_fileindex_free(fileindex);
7145 err = lock_worktree(worktree, LOCK_SH);
7147 unlockerr = got_ref_unlock(branch_ref);
7148 if (unlockerr && err == NULL)
7150 got_ref_close(branch_ref);
7152 unlockerr = got_ref_unlock(base_branch_ref);
7153 if (unlockerr && err == NULL)
7155 got_ref_close(base_branch_ref);
7160 struct check_stage_ok_arg {
7161 struct got_object_id *head_commit_id;
7162 struct got_worktree *worktree;
7163 struct got_fileindex *fileindex;
7164 struct got_repository *repo;
7168 const struct got_error *
7169 check_stage_ok(void *arg, unsigned char status,
7170 unsigned char staged_status, const char *relpath,
7171 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7172 struct got_object_id *commit_id, int dirfd, const char *de_name)
7174 struct check_stage_ok_arg *a = arg;
7175 const struct got_error *err = NULL;
7176 struct got_fileindex_entry *ie;
7177 struct got_object_id base_commit_id;
7178 struct got_object_id *base_commit_idp = NULL;
7179 char *in_repo_path = NULL, *p;
7181 if (status == GOT_STATUS_UNVERSIONED ||
7182 status == GOT_STATUS_NO_CHANGE)
7184 if (status == GOT_STATUS_NONEXISTENT)
7185 return got_error_set_errno(ENOENT, relpath);
7187 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7189 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7191 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7192 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7194 return got_error_from_errno("asprintf");
7196 if (got_fileindex_entry_has_commit(ie)) {
7197 memcpy(base_commit_id.sha1, ie->commit_sha1,
7198 SHA1_DIGEST_LENGTH);
7199 base_commit_idp = &base_commit_id;
7202 if (status == GOT_STATUS_CONFLICT) {
7203 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7205 } else if (status != GOT_STATUS_ADD &&
7206 status != GOT_STATUS_MODIFY &&
7207 status != GOT_STATUS_DELETE) {
7208 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7212 a->have_changes = 1;
7217 err = check_out_of_date(p, status, staged_status,
7218 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7219 GOT_ERR_STAGE_OUT_OF_DATE);
7225 struct stage_path_arg {
7226 struct got_worktree *worktree;
7227 struct got_fileindex *fileindex;
7228 struct got_repository *repo;
7229 got_worktree_status_cb status_cb;
7231 got_worktree_patch_cb patch_cb;
7233 int staged_something;
7234 int allow_bad_symlinks;
7237 static const struct got_error *
7238 stage_path(void *arg, unsigned char status,
7239 unsigned char staged_status, const char *relpath,
7240 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7241 struct got_object_id *commit_id, int dirfd, const char *de_name)
7243 struct stage_path_arg *a = arg;
7244 const struct got_error *err = NULL;
7245 struct got_fileindex_entry *ie;
7246 char *ondisk_path = NULL, *path_content = NULL;
7248 struct got_object_id *new_staged_blob_id = NULL;
7251 if (status == GOT_STATUS_UNVERSIONED)
7254 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7256 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7258 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7260 return got_error_from_errno("asprintf");
7263 case GOT_STATUS_ADD:
7264 case GOT_STATUS_MODIFY:
7265 /* XXX could sb.st_mode be passed in by our caller? */
7266 if (lstat(ondisk_path, &sb) == -1) {
7267 err = got_error_from_errno2("lstat", ondisk_path);
7271 if (status == GOT_STATUS_ADD) {
7272 int choice = GOT_PATCH_CHOICE_NONE;
7273 err = (*a->patch_cb)(&choice, a->patch_arg,
7274 status, ie->path, NULL, 1, 1);
7277 if (choice != GOT_PATCH_CHOICE_YES)
7280 err = create_patched_content(&path_content, 0,
7281 staged_blob_id ? staged_blob_id : blob_id,
7282 ondisk_path, dirfd, de_name, ie->path,
7283 a->repo, a->patch_cb, a->patch_arg);
7284 if (err || path_content == NULL)
7288 err = got_object_blob_create(&new_staged_blob_id,
7289 path_content ? path_content : ondisk_path, a->repo);
7292 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7293 SHA1_DIGEST_LENGTH);
7294 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7295 stage = GOT_FILEIDX_STAGE_ADD;
7297 stage = GOT_FILEIDX_STAGE_MODIFY;
7298 got_fileindex_entry_stage_set(ie, stage);
7299 if (S_ISLNK(sb.st_mode)) {
7300 int is_bad_symlink = 0;
7301 if (!a->allow_bad_symlinks) {
7302 char target_path[PATH_MAX];
7304 target_len = readlink(ondisk_path, target_path,
7305 sizeof(target_path));
7306 if (target_len == -1) {
7307 err = got_error_from_errno2("readlink",
7311 err = is_bad_symlink_target(&is_bad_symlink,
7312 target_path, target_len, ondisk_path,
7313 a->worktree->root_path);
7316 if (is_bad_symlink) {
7317 err = got_error_path(ondisk_path,
7318 GOT_ERR_BAD_SYMLINK);
7323 got_fileindex_entry_staged_filetype_set(ie,
7324 GOT_FILEIDX_MODE_BAD_SYMLINK);
7326 got_fileindex_entry_staged_filetype_set(ie,
7327 GOT_FILEIDX_MODE_SYMLINK);
7329 got_fileindex_entry_staged_filetype_set(ie,
7330 GOT_FILEIDX_MODE_REGULAR_FILE);
7332 a->staged_something = 1;
7333 if (a->status_cb == NULL)
7335 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7336 get_staged_status(ie), relpath, blob_id,
7337 new_staged_blob_id, NULL, dirfd, de_name);
7339 case GOT_STATUS_DELETE:
7340 if (staged_status == GOT_STATUS_DELETE)
7343 int choice = GOT_PATCH_CHOICE_NONE;
7344 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7345 ie->path, NULL, 1, 1);
7348 if (choice == GOT_PATCH_CHOICE_NO)
7350 if (choice != GOT_PATCH_CHOICE_YES) {
7351 err = got_error(GOT_ERR_PATCH_CHOICE);
7355 stage = GOT_FILEIDX_STAGE_DELETE;
7356 got_fileindex_entry_stage_set(ie, stage);
7357 a->staged_something = 1;
7358 if (a->status_cb == NULL)
7360 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7361 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7364 case GOT_STATUS_NO_CHANGE:
7366 case GOT_STATUS_CONFLICT:
7367 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7369 case GOT_STATUS_NONEXISTENT:
7370 err = got_error_set_errno(ENOENT, relpath);
7373 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7377 if (path_content && unlink(path_content) == -1 && err == NULL)
7378 err = got_error_from_errno2("unlink", path_content);
7381 free(new_staged_blob_id);
7385 const struct got_error *
7386 got_worktree_stage(struct got_worktree *worktree,
7387 struct got_pathlist_head *paths,
7388 got_worktree_status_cb status_cb, void *status_arg,
7389 got_worktree_patch_cb patch_cb, void *patch_arg,
7390 int allow_bad_symlinks, struct got_repository *repo)
7392 const struct got_error *err = NULL, *sync_err, *unlockerr;
7393 struct got_pathlist_entry *pe;
7394 struct got_fileindex *fileindex = NULL;
7395 char *fileindex_path = NULL;
7396 struct got_reference *head_ref = NULL;
7397 struct got_object_id *head_commit_id = NULL;
7398 struct check_stage_ok_arg oka;
7399 struct stage_path_arg spa;
7401 err = lock_worktree(worktree, LOCK_EX);
7405 err = got_ref_open(&head_ref, repo,
7406 got_worktree_get_head_ref_name(worktree), 0);
7409 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7412 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7416 /* Check pre-conditions before staging anything. */
7417 oka.head_commit_id = head_commit_id;
7418 oka.worktree = worktree;
7419 oka.fileindex = fileindex;
7421 oka.have_changes = 0;
7422 TAILQ_FOREACH(pe, paths, entry) {
7423 err = worktree_status(worktree, pe->path, fileindex, repo,
7424 check_stage_ok, &oka, NULL, NULL, 0, 0);
7428 if (!oka.have_changes) {
7429 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7433 spa.worktree = worktree;
7434 spa.fileindex = fileindex;
7436 spa.patch_cb = patch_cb;
7437 spa.patch_arg = patch_arg;
7438 spa.status_cb = status_cb;
7439 spa.status_arg = status_arg;
7440 spa.staged_something = 0;
7441 spa.allow_bad_symlinks = allow_bad_symlinks;
7442 TAILQ_FOREACH(pe, paths, entry) {
7443 err = worktree_status(worktree, pe->path, fileindex, repo,
7444 stage_path, &spa, NULL, NULL, 0, 0);
7448 if (!spa.staged_something) {
7449 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7453 sync_err = sync_fileindex(fileindex, fileindex_path);
7454 if (sync_err && err == NULL)
7458 got_ref_close(head_ref);
7459 free(head_commit_id);
7460 free(fileindex_path);
7462 got_fileindex_free(fileindex);
7463 unlockerr = lock_worktree(worktree, LOCK_SH);
7464 if (unlockerr && err == NULL)
7469 struct unstage_path_arg {
7470 struct got_worktree *worktree;
7471 struct got_fileindex *fileindex;
7472 struct got_repository *repo;
7473 got_worktree_checkout_cb progress_cb;
7475 got_worktree_patch_cb patch_cb;
7479 static const struct got_error *
7480 create_unstaged_content(char **path_unstaged_content,
7481 char **path_new_staged_content, struct got_object_id *blob_id,
7482 struct got_object_id *staged_blob_id, const char *relpath,
7483 struct got_repository *repo,
7484 got_worktree_patch_cb patch_cb, void *patch_arg)
7486 const struct got_error *err, *free_err;
7487 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7488 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7489 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7490 struct got_diffreg_result *diffreg_result = NULL;
7491 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7492 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7494 *path_unstaged_content = NULL;
7495 *path_new_staged_content = NULL;
7497 err = got_object_id_str(&label1, blob_id);
7500 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7504 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7508 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7512 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7516 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7520 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7524 err = got_diff_files(&diffreg_result, f1, label1, f2,
7525 path2, 3, 0, 1, NULL);
7529 err = got_opentemp_named(path_unstaged_content, &outfile,
7530 "got-unstaged-content");
7533 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7534 "got-new-staged-content");
7538 if (fseek(f1, 0L, SEEK_SET) == -1) {
7539 err = got_ferror(f1, GOT_ERR_IO);
7542 if (fseek(f2, 0L, SEEK_SET) == -1) {
7543 err = got_ferror(f2, GOT_ERR_IO);
7546 /* Count the number of actual changes in the diff result. */
7547 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7548 struct diff_chunk_context cc = {};
7549 diff_chunk_context_load_change(&cc, &nchunks_used,
7550 diffreg_result->result, n, 0);
7553 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7555 err = apply_or_reject_change(&choice, &nchunks_used,
7556 diffreg_result->result, n, relpath, f1, f2,
7557 &line_cur1, &line_cur2,
7558 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7561 if (choice == GOT_PATCH_CHOICE_YES)
7564 have_rejected_content = 1;
7565 if (choice == GOT_PATCH_CHOICE_QUIT)
7568 if (have_content || have_rejected_content)
7569 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7570 outfile, rejectfile);
7574 got_object_blob_close(blob);
7576 got_object_blob_close(staged_blob);
7577 free_err = got_diffreg_result_free(diffreg_result);
7578 if (free_err && err == NULL)
7580 if (f1 && fclose(f1) == EOF && err == NULL)
7581 err = got_error_from_errno2("fclose", path1);
7582 if (f2 && fclose(f2) == EOF && err == NULL)
7583 err = got_error_from_errno2("fclose", path2);
7584 if (outfile && fclose(outfile) == EOF && err == NULL)
7585 err = got_error_from_errno2("fclose", *path_unstaged_content);
7586 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7587 err = got_error_from_errno2("fclose", *path_new_staged_content);
7588 if (path1 && unlink(path1) == -1 && err == NULL)
7589 err = got_error_from_errno2("unlink", path1);
7590 if (path2 && unlink(path2) == -1 && err == NULL)
7591 err = got_error_from_errno2("unlink", path2);
7592 if (err || !have_content) {
7593 if (*path_unstaged_content &&
7594 unlink(*path_unstaged_content) == -1 && err == NULL)
7595 err = got_error_from_errno2("unlink",
7596 *path_unstaged_content);
7597 free(*path_unstaged_content);
7598 *path_unstaged_content = NULL;
7600 if (err || !have_content || !have_rejected_content) {
7601 if (*path_new_staged_content &&
7602 unlink(*path_new_staged_content) == -1 && err == NULL)
7603 err = got_error_from_errno2("unlink",
7604 *path_new_staged_content);
7605 free(*path_new_staged_content);
7606 *path_new_staged_content = NULL;
7613 static const struct got_error *
7614 unstage_hunks(struct got_object_id *staged_blob_id,
7615 struct got_blob_object *blob_base,
7616 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7617 const char *ondisk_path, const char *label_orig,
7618 struct got_worktree *worktree, struct got_repository *repo,
7619 got_worktree_patch_cb patch_cb, void *patch_arg,
7620 got_worktree_checkout_cb progress_cb, void *progress_arg)
7622 const struct got_error *err = NULL;
7623 char *path_unstaged_content = NULL;
7624 char *path_new_staged_content = NULL;
7625 struct got_object_id *new_staged_blob_id = NULL;
7629 err = create_unstaged_content(&path_unstaged_content,
7630 &path_new_staged_content, blob_id, staged_blob_id,
7631 ie->path, repo, patch_cb, patch_arg);
7635 if (path_unstaged_content == NULL)
7638 if (path_new_staged_content) {
7639 err = got_object_blob_create(&new_staged_blob_id,
7640 path_new_staged_content, repo);
7645 f = fopen(path_unstaged_content, "r");
7647 err = got_error_from_errno2("fopen",
7648 path_unstaged_content);
7651 if (fstat(fileno(f), &sb) == -1) {
7652 err = got_error_from_errno2("fstat", path_unstaged_content);
7655 if (got_fileindex_entry_staged_filetype_get(ie) ==
7656 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7657 char link_target[PATH_MAX];
7659 r = fread(link_target, 1, sizeof(link_target), f);
7660 if (r == 0 && ferror(f)) {
7661 err = got_error_from_errno("fread");
7664 if (r >= sizeof(link_target)) { /* should not happen */
7665 err = got_error(GOT_ERR_NO_SPACE);
7668 link_target[r] = '\0';
7669 err = merge_symlink(worktree, blob_base,
7670 ondisk_path, ie->path, label_orig, link_target,
7671 worktree->base_commit_id, repo, progress_cb,
7674 int local_changes_subsumed;
7675 err = merge_file(&local_changes_subsumed, worktree,
7676 blob_base, ondisk_path, ie->path,
7677 got_fileindex_perms_to_st(ie),
7678 path_unstaged_content, label_orig, "unstaged",
7679 repo, progress_cb, progress_arg);
7684 if (new_staged_blob_id) {
7685 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7686 SHA1_DIGEST_LENGTH);
7688 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7689 got_fileindex_entry_staged_filetype_set(ie, 0);
7692 free(new_staged_blob_id);
7693 if (path_unstaged_content &&
7694 unlink(path_unstaged_content) == -1 && err == NULL)
7695 err = got_error_from_errno2("unlink", path_unstaged_content);
7696 if (path_new_staged_content &&
7697 unlink(path_new_staged_content) == -1 && err == NULL)
7698 err = got_error_from_errno2("unlink", path_new_staged_content);
7699 if (f && fclose(f) == EOF && err == NULL)
7700 err = got_error_from_errno2("fclose", path_unstaged_content);
7701 free(path_unstaged_content);
7702 free(path_new_staged_content);
7706 static const struct got_error *
7707 unstage_path(void *arg, unsigned char status,
7708 unsigned char staged_status, const char *relpath,
7709 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7710 struct got_object_id *commit_id, int dirfd, const char *de_name)
7712 const struct got_error *err = NULL;
7713 struct unstage_path_arg *a = arg;
7714 struct got_fileindex_entry *ie;
7715 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7716 char *ondisk_path = NULL;
7717 char *id_str = NULL, *label_orig = NULL;
7718 int local_changes_subsumed;
7721 if (staged_status != GOT_STATUS_ADD &&
7722 staged_status != GOT_STATUS_MODIFY &&
7723 staged_status != GOT_STATUS_DELETE)
7726 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7728 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7730 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7732 return got_error_from_errno("asprintf");
7734 err = got_object_id_str(&id_str,
7735 commit_id ? commit_id : a->worktree->base_commit_id);
7738 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7740 err = got_error_from_errno("asprintf");
7744 switch (staged_status) {
7745 case GOT_STATUS_MODIFY:
7746 err = got_object_open_as_blob(&blob_base, a->repo,
7751 case GOT_STATUS_ADD:
7753 if (staged_status == GOT_STATUS_ADD) {
7754 int choice = GOT_PATCH_CHOICE_NONE;
7755 err = (*a->patch_cb)(&choice, a->patch_arg,
7756 staged_status, ie->path, NULL, 1, 1);
7759 if (choice != GOT_PATCH_CHOICE_YES)
7762 err = unstage_hunks(staged_blob_id,
7763 blob_base, blob_id, ie, ondisk_path,
7764 label_orig, a->worktree, a->repo,
7765 a->patch_cb, a->patch_arg,
7766 a->progress_cb, a->progress_arg);
7767 break; /* Done with this file. */
7770 err = got_object_open_as_blob(&blob_staged, a->repo,
7771 staged_blob_id, 8192);
7774 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7775 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7776 case GOT_FILEIDX_MODE_REGULAR_FILE:
7777 err = merge_blob(&local_changes_subsumed, a->worktree,
7778 blob_base, ondisk_path, relpath,
7779 got_fileindex_perms_to_st(ie), label_orig,
7780 blob_staged, commit_id ? commit_id :
7781 a->worktree->base_commit_id, a->repo,
7782 a->progress_cb, a->progress_arg);
7784 case GOT_FILEIDX_MODE_SYMLINK:
7785 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7786 char *staged_target;
7787 err = got_object_blob_read_to_str(
7788 &staged_target, blob_staged);
7791 err = merge_symlink(a->worktree, blob_base,
7792 ondisk_path, relpath, label_orig,
7793 staged_target, commit_id ? commit_id :
7794 a->worktree->base_commit_id,
7795 a->repo, a->progress_cb, a->progress_arg);
7796 free(staged_target);
7798 err = merge_blob(&local_changes_subsumed,
7799 a->worktree, blob_base, ondisk_path,
7800 relpath, got_fileindex_perms_to_st(ie),
7801 label_orig, blob_staged,
7802 commit_id ? commit_id :
7803 a->worktree->base_commit_id, a->repo,
7804 a->progress_cb, a->progress_arg);
7808 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7812 got_fileindex_entry_stage_set(ie,
7813 GOT_FILEIDX_STAGE_NONE);
7814 got_fileindex_entry_staged_filetype_set(ie, 0);
7817 case GOT_STATUS_DELETE:
7819 int choice = GOT_PATCH_CHOICE_NONE;
7820 err = (*a->patch_cb)(&choice, a->patch_arg,
7821 staged_status, ie->path, NULL, 1, 1);
7824 if (choice == GOT_PATCH_CHOICE_NO)
7826 if (choice != GOT_PATCH_CHOICE_YES) {
7827 err = got_error(GOT_ERR_PATCH_CHOICE);
7831 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7832 got_fileindex_entry_staged_filetype_set(ie, 0);
7833 err = get_file_status(&status, &sb, ie, ondisk_path,
7834 dirfd, de_name, a->repo);
7837 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7843 got_object_blob_close(blob_base);
7845 got_object_blob_close(blob_staged);
7851 const struct got_error *
7852 got_worktree_unstage(struct got_worktree *worktree,
7853 struct got_pathlist_head *paths,
7854 got_worktree_checkout_cb progress_cb, void *progress_arg,
7855 got_worktree_patch_cb patch_cb, void *patch_arg,
7856 struct got_repository *repo)
7858 const struct got_error *err = NULL, *sync_err, *unlockerr;
7859 struct got_pathlist_entry *pe;
7860 struct got_fileindex *fileindex = NULL;
7861 char *fileindex_path = NULL;
7862 struct unstage_path_arg upa;
7864 err = lock_worktree(worktree, LOCK_EX);
7868 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7872 upa.worktree = worktree;
7873 upa.fileindex = fileindex;
7875 upa.progress_cb = progress_cb;
7876 upa.progress_arg = progress_arg;
7877 upa.patch_cb = patch_cb;
7878 upa.patch_arg = patch_arg;
7879 TAILQ_FOREACH(pe, paths, entry) {
7880 err = worktree_status(worktree, pe->path, fileindex, repo,
7881 unstage_path, &upa, NULL, NULL, 0, 0);
7886 sync_err = sync_fileindex(fileindex, fileindex_path);
7887 if (sync_err && err == NULL)
7890 free(fileindex_path);
7892 got_fileindex_free(fileindex);
7893 unlockerr = lock_worktree(worktree, LOCK_SH);
7894 if (unlockerr && err == NULL)
7899 struct report_file_info_arg {
7900 struct got_worktree *worktree;
7901 got_worktree_path_info_cb info_cb;
7903 struct got_pathlist_head *paths;
7904 got_cancel_cb cancel_cb;
7908 static const struct got_error *
7909 report_file_info(void *arg, struct got_fileindex_entry *ie)
7911 struct report_file_info_arg *a = arg;
7912 struct got_pathlist_entry *pe;
7913 struct got_object_id blob_id, staged_blob_id, commit_id;
7914 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7915 struct got_object_id *commit_idp = NULL;
7918 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7919 return got_error(GOT_ERR_CANCELLED);
7921 TAILQ_FOREACH(pe, a->paths, entry) {
7922 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7923 got_path_is_child(ie->path, pe->path, pe->path_len))
7926 if (pe == NULL) /* not found */
7929 if (got_fileindex_entry_has_blob(ie)) {
7930 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7931 blob_idp = &blob_id;
7933 stage = got_fileindex_entry_stage_get(ie);
7934 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7935 stage == GOT_FILEIDX_STAGE_ADD) {
7936 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7937 SHA1_DIGEST_LENGTH);
7938 staged_blob_idp = &staged_blob_id;
7941 if (got_fileindex_entry_has_commit(ie)) {
7942 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7943 commit_idp = &commit_id;
7946 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7947 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7950 const struct got_error *
7951 got_worktree_path_info(struct got_worktree *worktree,
7952 struct got_pathlist_head *paths,
7953 got_worktree_path_info_cb info_cb, void *info_arg,
7954 got_cancel_cb cancel_cb, void *cancel_arg)
7957 const struct got_error *err = NULL, *unlockerr;
7958 struct got_fileindex *fileindex = NULL;
7959 char *fileindex_path = NULL;
7960 struct report_file_info_arg arg;
7962 err = lock_worktree(worktree, LOCK_SH);
7966 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7970 arg.worktree = worktree;
7971 arg.info_cb = info_cb;
7972 arg.info_arg = info_arg;
7974 arg.cancel_cb = cancel_cb;
7975 arg.cancel_arg = cancel_arg;
7976 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7979 free(fileindex_path);
7981 got_fileindex_free(fileindex);
7982 unlockerr = lock_worktree(worktree, LOCK_UN);
7983 if (unlockerr && err == NULL)