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) != 0)
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 close(worktree->root_fd);
520 got_worktree_get_root_path(struct got_worktree *worktree)
522 return worktree->root_path;
526 got_worktree_get_repo_path(struct got_worktree *worktree)
528 return worktree->repo_path;
531 got_worktree_get_path_prefix(struct got_worktree *worktree)
533 return worktree->path_prefix;
536 const struct got_error *
537 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
538 const char *path_prefix)
540 char *absprefix = NULL;
542 if (!got_path_is_absolute(path_prefix)) {
543 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
544 return got_error_from_errno("asprintf");
546 *match = (strcmp(absprefix ? absprefix : path_prefix,
547 worktree->path_prefix) == 0);
553 got_worktree_get_head_ref_name(struct got_worktree *worktree)
555 return worktree->head_ref_name;
558 const struct got_error *
559 got_worktree_set_head_ref(struct got_worktree *worktree,
560 struct got_reference *head_ref)
562 const struct got_error *err = NULL;
563 char *path_got = NULL, *head_ref_name = NULL;
565 if (asprintf(&path_got, "%s/%s", worktree->root_path,
566 GOT_WORKTREE_GOT_DIR) == -1) {
567 err = got_error_from_errno("asprintf");
572 head_ref_name = strdup(got_ref_get_name(head_ref));
573 if (head_ref_name == NULL) {
574 err = got_error_from_errno("strdup");
578 err = write_head_ref(path_got, head_ref);
582 free(worktree->head_ref_name);
583 worktree->head_ref_name = head_ref_name;
591 struct got_object_id *
592 got_worktree_get_base_commit_id(struct got_worktree *worktree)
594 return worktree->base_commit_id;
597 const struct got_error *
598 got_worktree_set_base_commit_id(struct got_worktree *worktree,
599 struct got_repository *repo, struct got_object_id *commit_id)
601 const struct got_error *err;
602 struct got_object *obj = NULL;
604 char *path_got = NULL;
606 if (asprintf(&path_got, "%s/%s", worktree->root_path,
607 GOT_WORKTREE_GOT_DIR) == -1) {
608 err = got_error_from_errno("asprintf");
613 err = got_object_open(&obj, repo, commit_id);
617 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
618 err = got_error(GOT_ERR_OBJ_TYPE);
622 /* Record our base commit. */
623 err = got_object_id_str(&id_str, commit_id);
626 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
630 free(worktree->base_commit_id);
631 worktree->base_commit_id = got_object_id_dup(commit_id);
632 if (worktree->base_commit_id == NULL) {
633 err = got_error_from_errno("got_object_id_dup");
638 got_object_close(obj);
644 const struct got_gotconfig *
645 got_worktree_get_gotconfig(struct got_worktree *worktree)
647 return worktree->gotconfig;
650 static const struct got_error *
651 lock_worktree(struct got_worktree *worktree, int operation)
653 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
654 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
655 : got_error_from_errno2("flock",
656 got_worktree_get_root_path(worktree)));
660 static const struct got_error *
661 add_dir_on_disk(struct got_worktree *worktree, const char *path)
663 const struct got_error *err = NULL;
666 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
667 return got_error_from_errno("asprintf");
669 err = got_path_mkdir(abspath);
670 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
673 if (lstat(abspath, &sb) == -1) {
674 err = got_error_from_errno2("lstat", abspath);
675 } else if (!S_ISDIR(sb.st_mode)) {
676 /* TODO directory is obstructed; do something */
677 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
684 static const struct got_error *
685 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
687 const struct got_error *err = NULL;
690 size_t flen1 = 0, flen2 = 0;
695 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
696 if (flen1 == 0 && ferror(f1)) {
697 err = got_error_from_errno("fread");
700 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
701 if (flen2 == 0 && ferror(f2)) {
702 err = got_error_from_errno("fread");
709 } else if (flen2 == 0) {
713 } else if (flen1 == flen2) {
714 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
727 static const struct got_error *
728 check_files_equal(int *same, const char *f1_path, const char *f2_path)
730 const struct got_error *err = NULL;
733 FILE *f1 = NULL, *f2 = NULL;
737 if (lstat(f1_path, &sb) != 0) {
738 err = got_error_from_errno2("lstat", f1_path);
743 if (lstat(f2_path, &sb) != 0) {
744 err = got_error_from_errno2("lstat", f2_path);
749 if (size1 != size2) {
754 f1 = fopen(f1_path, "r");
756 return got_error_from_errno2("fopen", f1_path);
758 f2 = fopen(f2_path, "r");
760 err = got_error_from_errno2("fopen", f2_path);
764 err = check_file_contents_equal(same, f1, f2);
766 if (f1 && fclose(f1) == EOF && err == NULL)
767 err = got_error_from_errno("fclose");
768 if (f2 && fclose(f2) == EOF && err == NULL)
769 err = got_error_from_errno("fclose");
775 * Perform a 3-way merge where blob_orig acts as the common ancestor,
776 * the file at deriv_path acts as the first derived version, and the
777 * file on disk acts as the second derived version.
779 static const struct got_error *
780 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
781 struct got_blob_object *blob_orig, const char *ondisk_path,
782 const char *path, uint16_t st_mode, const char *deriv_path,
783 const char *label_orig, const char *label_deriv,
784 struct got_repository *repo,
785 got_worktree_checkout_cb progress_cb, void *progress_arg)
787 const struct got_error *err = NULL;
790 char *blob_orig_path = NULL;
791 char *merged_path = NULL, *base_path = NULL;
794 char *symlink_path = NULL;
795 FILE *symlinkf = NULL;
797 *local_changes_subsumed = 0;
799 err = got_path_dirname(&parent, ondisk_path);
803 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
804 err = got_error_from_errno("asprintf");
808 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
813 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
814 err = got_error_from_errno("asprintf");
819 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
823 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
829 * If the file has no blob, this is an "add vs add" conflict,
830 * and we simply use an empty ancestor file to make both files
831 * appear in the merged result in their entirety.
836 * In order the run a 3-way merge with a symlink we copy the symlink's
837 * target path into a temporary file and use that file with diff3.
839 if (S_ISLNK(st_mode)) {
840 char target_path[PATH_MAX];
845 if (asprintf(&base_path, "%s/got-symlink-merge",
847 err = got_error_from_errno("asprintf");
851 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
854 target_len = readlink(ondisk_path, target_path,
855 sizeof(target_path));
856 if (target_len == -1) {
857 err = got_error_from_errno2("readlink", ondisk_path);
860 n = fwrite(target_path, 1, target_len, symlinkf);
861 if (n != target_len) {
862 err = got_ferror(symlinkf, GOT_ERR_IO);
865 if (fflush(symlinkf) == EOF) {
866 err = got_error_from_errno2("fflush", symlink_path);
871 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
872 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
873 label_deriv, label_orig, NULL);
877 err = (*progress_cb)(progress_arg,
878 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
882 if (fsync(merged_fd) != 0) {
883 err = got_error_from_errno("fsync");
887 /* Check if a clean merge has subsumed all local changes. */
888 if (overlapcnt == 0) {
889 err = check_files_equal(local_changes_subsumed, deriv_path,
895 if (fchmod(merged_fd, st_mode) != 0) {
896 err = got_error_from_errno2("fchmod", merged_path);
900 if (rename(merged_path, ondisk_path) != 0) {
901 err = got_error_from_errno3("rename", merged_path,
911 if (unlink(symlink_path) == -1 && err == NULL)
912 err = got_error_from_errno2("unlink", symlink_path);
914 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
915 err = got_error_from_errno2("fclose", symlink_path);
917 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
918 err = got_error_from_errno("close");
919 if (f_orig && fclose(f_orig) == EOF && err == NULL)
920 err = got_error_from_errno("fclose");
923 if (blob_orig_path) {
924 unlink(blob_orig_path);
925 free(blob_orig_path);
931 static const struct got_error *
932 update_symlink(const char *ondisk_path, const char *target_path,
935 /* This is not atomic but matches what 'ln -sf' does. */
936 if (unlink(ondisk_path) == -1)
937 return got_error_from_errno2("unlink", ondisk_path);
938 if (symlink(target_path, ondisk_path) == -1)
939 return got_error_from_errno3("symlink", target_path,
945 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
946 * in the work tree with a file that contains conflict markers and the
947 * conflicting target paths of the original version, a "derived version"
948 * of a symlink from an incoming change, and a local version of the symlink.
950 * The original versions's target path can be NULL if it is not available,
951 * such as if both derived versions added a new symlink at the same path.
953 * The incoming derived symlink target is NULL in case the incoming change
954 * has deleted this symlink.
956 static const struct got_error *
957 install_symlink_conflict(const char *deriv_target,
958 struct got_object_id *deriv_base_commit_id, const char *orig_target,
959 const char *label_orig, const char *local_target, const char *ondisk_path)
961 const struct got_error *err;
962 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
965 err = got_object_id_str(&id_str, deriv_base_commit_id);
967 return got_error_from_errno("asprintf");
969 if (asprintf(&label_deriv, "%s: commit %s",
970 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
971 err = got_error_from_errno("asprintf");
975 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
979 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
980 err = got_error_from_errno2("fchmod", path);
984 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
985 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
986 deriv_target ? deriv_target : "(symlink was deleted)",
987 orig_target ? label_orig : "",
988 orig_target ? "\n" : "",
989 orig_target ? orig_target : "",
990 orig_target ? "\n" : "",
991 GOT_DIFF_CONFLICT_MARKER_SEP,
992 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
993 err = got_error_from_errno2("fprintf", path);
997 if (unlink(ondisk_path) == -1) {
998 err = got_error_from_errno2("unlink", ondisk_path);
1001 if (rename(path, ondisk_path) == -1) {
1002 err = got_error_from_errno3("rename", path, ondisk_path);
1006 if (f != NULL && fclose(f) == EOF && err == NULL)
1007 err = got_error_from_errno2("fclose", path);
1014 /* forward declaration */
1015 static const struct got_error *
1016 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1017 const char *, const char *, uint16_t, const char *,
1018 struct got_blob_object *, struct got_object_id *,
1019 struct got_repository *, got_worktree_checkout_cb, void *);
1022 * Merge a symlink into the work tree, where blob_orig acts as the common
1023 * ancestor, deriv_target is the link target of the first derived version,
1024 * and the symlink on disk acts as the second derived version.
1025 * Assume that contents of both blobs represent symlinks.
1027 static const struct got_error *
1028 merge_symlink(struct got_worktree *worktree,
1029 struct got_blob_object *blob_orig, const char *ondisk_path,
1030 const char *path, const char *label_orig, const char *deriv_target,
1031 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1032 got_worktree_checkout_cb progress_cb, void *progress_arg)
1034 const struct got_error *err = NULL;
1035 char *ancestor_target = NULL;
1037 ssize_t ondisk_len, deriv_len;
1038 char ondisk_target[PATH_MAX];
1039 int have_local_change = 0;
1040 int have_incoming_change = 0;
1042 if (lstat(ondisk_path, &sb) == -1)
1043 return got_error_from_errno2("lstat", ondisk_path);
1045 ondisk_len = readlink(ondisk_path, ondisk_target,
1046 sizeof(ondisk_target));
1047 if (ondisk_len == -1) {
1048 err = got_error_from_errno2("readlink",
1052 ondisk_target[ondisk_len] = '\0';
1055 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1060 if (ancestor_target == NULL ||
1061 (ondisk_len != strlen(ancestor_target) ||
1062 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1063 have_local_change = 1;
1065 deriv_len = strlen(deriv_target);
1066 if (ancestor_target == NULL ||
1067 (deriv_len != strlen(ancestor_target) ||
1068 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1069 have_incoming_change = 1;
1071 if (!have_local_change && !have_incoming_change) {
1072 if (ancestor_target) {
1073 /* Both sides made the same change. */
1074 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1076 } else if (deriv_len == ondisk_len &&
1077 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1078 /* Both sides added the same symlink. */
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1082 /* Both sides added symlinks which don't match. */
1083 err = install_symlink_conflict(deriv_target,
1084 deriv_base_commit_id, ancestor_target,
1085 label_orig, ondisk_target, ondisk_path);
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1091 } else if (!have_local_change && have_incoming_change) {
1092 /* Apply the incoming change. */
1093 err = update_symlink(ondisk_path, deriv_target,
1094 strlen(deriv_target));
1097 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1098 } else if (have_local_change && have_incoming_change) {
1099 if (deriv_len == ondisk_len &&
1100 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1101 /* Both sides made the same change. */
1102 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1105 err = install_symlink_conflict(deriv_target,
1106 deriv_base_commit_id, ancestor_target, label_orig,
1107 ondisk_target, ondisk_path);
1110 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1116 free(ancestor_target);
1121 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1122 * blob_deriv acts as the first derived version, and the file on disk
1123 * acts as the second derived version.
1125 static const struct got_error *
1126 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1127 struct got_blob_object *blob_orig, const char *ondisk_path,
1128 const char *path, uint16_t st_mode, const char *label_orig,
1129 struct got_blob_object *blob_deriv,
1130 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1131 got_worktree_checkout_cb progress_cb, void *progress_arg)
1133 const struct got_error *err = NULL;
1134 FILE *f_deriv = NULL;
1135 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1136 char *label_deriv = NULL, *parent = NULL;
1138 *local_changes_subsumed = 0;
1140 err = got_path_dirname(&parent, ondisk_path);
1145 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1146 err = got_error_from_errno("asprintf");
1151 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1154 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1159 err = got_object_id_str(&id_str, deriv_base_commit_id);
1162 if (asprintf(&label_deriv, "%s: commit %s",
1163 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1164 err = got_error_from_errno("asprintf");
1168 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1169 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1170 label_deriv, repo, progress_cb, progress_arg);
1172 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1173 err = got_error_from_errno("fclose");
1175 if (blob_deriv_path) {
1176 unlink(blob_deriv_path);
1177 free(blob_deriv_path);
1185 static const struct got_error *
1186 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1187 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1188 int wt_fd, const char *path, struct got_object_id *blob_id)
1190 const struct got_error *err = NULL;
1191 struct got_fileindex_entry *new_ie;
1195 err = got_fileindex_entry_alloc(&new_ie, path);
1199 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1200 blob_id->sha1, base_commit_id->sha1, 1);
1204 err = got_fileindex_entry_add(fileindex, new_ie);
1207 got_fileindex_entry_free(new_ie);
1214 get_ondisk_perms(int executable, mode_t st_mode)
1216 mode_t xbits = S_IXUSR;
1219 /* Map read bits to execute bits. */
1220 if (st_mode & S_IRGRP)
1222 if (st_mode & S_IROTH)
1224 return st_mode | xbits;
1227 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1230 /* forward declaration */
1231 static const struct got_error *
1232 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1233 const char *path, mode_t te_mode, mode_t st_mode,
1234 struct got_blob_object *blob, int restoring_missing_file,
1235 int reverting_versioned_file, int installing_bad_symlink,
1236 int path_is_unversioned, struct got_repository *repo,
1237 got_worktree_checkout_cb progress_cb, void *progress_arg);
1240 * This function assumes that the provided symlink target points at a
1241 * safe location in the work tree!
1243 static const struct got_error *
1244 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1247 const struct got_error *err = NULL;
1249 char etarget[PATH_MAX];
1253 * "Bad" symlinks (those pointing outside the work tree or into the
1254 * .got directory) are installed in the work tree as a regular file
1255 * which contains the bad symlink target path.
1256 * The new symlink target has already been checked for safety by our
1257 * caller. If we can successfully open a regular file then we simply
1258 * replace this file with a symlink below.
1260 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1263 return got_error_from_errno2("open", ondisk_path);
1265 /* We are updating an existing on-disk symlink. */
1266 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1268 return got_error_from_errno2("readlink", ondisk_path);
1270 if (elen == target_len &&
1271 memcmp(etarget, target_path, target_len) == 0)
1272 return NULL; /* nothing to do */
1275 err = update_symlink(ondisk_path, target_path, target_len);
1276 if (fd != -1 && close(fd) == -1 && err == NULL)
1277 err = got_error_from_errno2("close", ondisk_path);
1281 static const struct got_error *
1282 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1283 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1285 const struct got_error *err = NULL;
1286 char canonpath[PATH_MAX];
1287 char *path_got = NULL;
1289 *is_bad_symlink = 0;
1291 if (target_len >= sizeof(canonpath)) {
1292 *is_bad_symlink = 1;
1297 * We do not use realpath(3) to resolve the symlink's target
1298 * path because we don't want to resolve symlinks recursively.
1299 * Instead we make the path absolute and then canonicalize it.
1300 * Relative symlink target lookup should begin at the directory
1301 * in which the blob object is being installed.
1303 if (!got_path_is_absolute(target_path)) {
1304 char *abspath, *parent;
1305 err = got_path_dirname(&parent, ondisk_path);
1308 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1310 return got_error_from_errno("asprintf");
1313 if (strlen(abspath) >= sizeof(canonpath)) {
1314 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1318 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1323 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1328 /* Only allow symlinks pointing at paths within the work tree. */
1329 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1330 *is_bad_symlink = 1;
1334 /* Do not allow symlinks pointing into the .got directory. */
1335 if (asprintf(&path_got, "%s/%s", wtroot_path,
1336 GOT_WORKTREE_GOT_DIR) == -1)
1337 return got_error_from_errno("asprintf");
1338 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1339 *is_bad_symlink = 1;
1345 static const struct got_error *
1346 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1347 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1348 int restoring_missing_file, int reverting_versioned_file,
1349 int path_is_unversioned, struct got_repository *repo,
1350 got_worktree_checkout_cb progress_cb, void *progress_arg)
1352 const struct got_error *err = NULL;
1353 char target_path[PATH_MAX];
1354 size_t len, target_len = 0;
1355 char *path_got = NULL;
1356 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1357 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1359 *is_bad_symlink = 0;
1362 * Blob object content specifies the target path of the link.
1363 * If a symbolic link cannot be installed we instead create
1364 * a regular file which contains the link target path stored
1365 * in the blob object.
1368 err = got_object_blob_read_block(&len, blob);
1369 if (len + target_len >= sizeof(target_path)) {
1370 /* Path too long; install as a regular file. */
1371 *is_bad_symlink = 1;
1372 got_object_blob_rewind(blob);
1373 return install_blob(worktree, ondisk_path, path,
1374 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1375 restoring_missing_file, reverting_versioned_file,
1376 1, path_is_unversioned, repo, progress_cb,
1380 /* Skip blob object header first time around. */
1381 memcpy(target_path + target_len, buf + hdrlen,
1383 target_len += len - hdrlen;
1387 target_path[target_len] = '\0';
1389 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1390 ondisk_path, worktree->root_path);
1394 if (*is_bad_symlink) {
1395 /* install as a regular file */
1396 *is_bad_symlink = 1;
1397 got_object_blob_rewind(blob);
1398 err = install_blob(worktree, ondisk_path, path,
1399 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1400 restoring_missing_file, reverting_versioned_file, 1,
1401 path_is_unversioned, repo, progress_cb, progress_arg);
1405 if (symlink(target_path, ondisk_path) == -1) {
1406 if (errno == EEXIST) {
1407 if (path_is_unversioned) {
1408 err = (*progress_cb)(progress_arg,
1409 GOT_STATUS_UNVERSIONED, path);
1412 err = replace_existing_symlink(ondisk_path,
1413 target_path, target_len);
1417 err = (*progress_cb)(progress_arg,
1418 reverting_versioned_file ?
1419 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1422 goto done; /* Nothing else to do. */
1425 if (errno == ENOENT) {
1427 err = got_path_dirname(&parent, ondisk_path);
1430 err = add_dir_on_disk(worktree, parent);
1435 * Retry, and fall through to error handling
1436 * below if this second attempt fails.
1438 if (symlink(target_path, ondisk_path) != -1) {
1439 err = NULL; /* success */
1444 /* Handle errors from first or second creation attempt. */
1445 if (errno == ENAMETOOLONG) {
1446 /* bad target path; install as a regular file */
1447 *is_bad_symlink = 1;
1448 got_object_blob_rewind(blob);
1449 err = install_blob(worktree, ondisk_path, path,
1450 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1451 restoring_missing_file, reverting_versioned_file, 1,
1452 path_is_unversioned, repo,
1453 progress_cb, progress_arg);
1454 } else if (errno == ENOTDIR) {
1455 err = got_error_path(ondisk_path,
1456 GOT_ERR_FILE_OBSTRUCTED);
1458 err = got_error_from_errno3("symlink",
1459 target_path, ondisk_path);
1461 } else if (progress_cb)
1462 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1463 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1469 static const struct got_error *
1470 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1471 const char *path, mode_t te_mode, mode_t st_mode,
1472 struct got_blob_object *blob, int restoring_missing_file,
1473 int reverting_versioned_file, int installing_bad_symlink,
1474 int path_is_unversioned, struct got_repository *repo,
1475 got_worktree_checkout_cb progress_cb, void *progress_arg)
1477 const struct got_error *err = NULL;
1481 char *tmppath = NULL;
1483 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1484 GOT_DEFAULT_FILE_MODE);
1486 if (errno == ENOENT) {
1488 err = got_path_dirname(&parent, path);
1491 err = add_dir_on_disk(worktree, parent);
1495 fd = open(ondisk_path,
1496 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1497 GOT_DEFAULT_FILE_MODE);
1499 return got_error_from_errno2("open",
1501 } else if (errno == EEXIST) {
1502 if (path_is_unversioned) {
1503 err = (*progress_cb)(progress_arg,
1504 GOT_STATUS_UNVERSIONED, path);
1507 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1508 !S_ISREG(st_mode) && !installing_bad_symlink) {
1509 /* TODO file is obstructed; do something */
1510 err = got_error_path(ondisk_path,
1511 GOT_ERR_FILE_OBSTRUCTED);
1514 err = got_opentemp_named_fd(&tmppath, &fd,
1521 return got_error_from_errno2("open", ondisk_path);
1524 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1525 err = got_error_from_errno2("fchmod",
1526 update ? tmppath : ondisk_path);
1531 if (restoring_missing_file)
1532 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1534 else if (reverting_versioned_file)
1535 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1538 err = (*progress_cb)(progress_arg,
1539 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1544 hdrlen = got_object_blob_get_hdrlen(blob);
1546 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1547 err = got_object_blob_read_block(&len, blob);
1551 /* Skip blob object header first time around. */
1552 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1554 err = got_error_from_errno("write");
1556 } else if (outlen != len - hdrlen) {
1557 err = got_error(GOT_ERR_IO);
1564 if (fsync(fd) != 0) {
1565 err = got_error_from_errno("fsync");
1570 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1571 err = got_error_from_errno2("unlink", ondisk_path);
1574 if (rename(tmppath, ondisk_path) != 0) {
1575 err = got_error_from_errno3("rename", tmppath,
1584 if (fd != -1 && close(fd) != 0 && err == NULL)
1585 err = got_error_from_errno("close");
1586 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1587 err = got_error_from_errno2("unlink", tmppath);
1592 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1593 static const struct got_error *
1594 get_modified_file_content_status(unsigned char *status, FILE *f)
1596 const struct got_error *err = NULL;
1597 const char *markers[3] = {
1598 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1599 GOT_DIFF_CONFLICT_MARKER_SEP,
1600 GOT_DIFF_CONFLICT_MARKER_END
1604 size_t linesize = 0;
1607 while (*status == GOT_STATUS_MODIFY) {
1608 linelen = getline(&line, &linesize, f);
1609 if (linelen == -1) {
1612 err = got_ferror(f, GOT_ERR_IO);
1616 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1617 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1619 *status = GOT_STATUS_CONFLICT;
1630 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1632 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1633 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1637 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1639 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1640 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1641 ie->mtime_sec == sb->st_mtim.tv_sec &&
1642 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1643 ie->size == (sb->st_size & 0xffffffff) &&
1644 !xbit_differs(ie, sb->st_mode));
1647 static unsigned char
1648 get_staged_status(struct got_fileindex_entry *ie)
1650 switch (got_fileindex_entry_stage_get(ie)) {
1651 case GOT_FILEIDX_STAGE_ADD:
1652 return GOT_STATUS_ADD;
1653 case GOT_FILEIDX_STAGE_DELETE:
1654 return GOT_STATUS_DELETE;
1655 case GOT_FILEIDX_STAGE_MODIFY:
1656 return GOT_STATUS_MODIFY;
1658 return GOT_STATUS_NO_CHANGE;
1662 static const struct got_error *
1663 get_symlink_modification_status(unsigned char *status,
1664 struct got_fileindex_entry *ie, const char *abspath,
1665 int dirfd, const char *de_name, struct got_blob_object *blob)
1667 const struct got_error *err = NULL;
1668 char target_path[PATH_MAX];
1669 char etarget[PATH_MAX];
1671 size_t len, target_len = 0;
1672 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1673 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1675 *status = GOT_STATUS_NO_CHANGE;
1677 /* Blob object content specifies the target path of the link. */
1679 err = got_object_blob_read_block(&len, blob);
1682 if (len + target_len >= sizeof(target_path)) {
1684 * Should not happen. The blob contents were OK
1685 * when this symlink was installed.
1687 return got_error(GOT_ERR_NO_SPACE);
1690 /* Skip blob object header first time around. */
1691 memcpy(target_path + target_len, buf + hdrlen,
1693 target_len += len - hdrlen;
1697 target_path[target_len] = '\0';
1700 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1702 return got_error_from_errno2("readlinkat", abspath);
1704 elen = readlink(abspath, etarget, sizeof(etarget));
1706 return got_error_from_errno2("readlink", abspath);
1709 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1710 *status = GOT_STATUS_MODIFY;
1715 static const struct got_error *
1716 get_file_status(unsigned char *status, struct stat *sb,
1717 struct got_fileindex_entry *ie, const char *abspath,
1718 int dirfd, const char *de_name, struct got_repository *repo)
1720 const struct got_error *err = NULL;
1721 struct got_object_id id;
1726 struct got_blob_object *blob = NULL;
1728 unsigned char staged_status = get_staged_status(ie);
1730 *status = GOT_STATUS_NO_CHANGE;
1733 * Whenever the caller provides a directory descriptor and a
1734 * directory entry name for the file, use them! This prevents
1735 * race conditions if filesystem paths change beneath our feet.
1738 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1739 if (errno == ENOENT) {
1740 if (got_fileindex_entry_has_file_on_disk(ie))
1741 *status = GOT_STATUS_MISSING;
1743 *status = GOT_STATUS_DELETE;
1746 err = got_error_from_errno2("fstatat", abspath);
1750 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1751 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1752 return got_error_from_errno2("open", abspath);
1753 else if (fd == -1 && errno == ELOOP) {
1754 if (lstat(abspath, sb) == -1)
1755 return got_error_from_errno2("lstat", abspath);
1756 } else if (fd == -1 || fstat(fd, sb) == -1) {
1757 if (errno == ENOENT) {
1758 if (got_fileindex_entry_has_file_on_disk(ie))
1759 *status = GOT_STATUS_MISSING;
1761 *status = GOT_STATUS_DELETE;
1764 err = got_error_from_errno2("fstat", abspath);
1769 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1770 *status = GOT_STATUS_OBSTRUCTED;
1774 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1775 *status = GOT_STATUS_DELETE;
1777 } else if (!got_fileindex_entry_has_blob(ie) &&
1778 staged_status != GOT_STATUS_ADD) {
1779 *status = GOT_STATUS_ADD;
1783 if (!stat_info_differs(ie, sb))
1786 if (S_ISLNK(sb->st_mode) &&
1787 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1788 *status = GOT_STATUS_MODIFY;
1792 if (staged_status == GOT_STATUS_MODIFY ||
1793 staged_status == GOT_STATUS_ADD)
1794 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1796 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1798 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1802 if (S_ISLNK(sb->st_mode)) {
1803 err = get_symlink_modification_status(status, ie,
1804 abspath, dirfd, de_name, blob);
1809 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1811 err = got_error_from_errno2("openat", abspath);
1816 f = fdopen(fd, "r");
1818 err = got_error_from_errno2("fdopen", abspath);
1822 hdrlen = got_object_blob_get_hdrlen(blob);
1824 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1825 err = got_object_blob_read_block(&blen, blob);
1828 /* Skip length of blob object header first time around. */
1829 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1830 if (flen == 0 && ferror(f)) {
1831 err = got_error_from_errno("fread");
1834 if (blen - hdrlen == 0) {
1836 *status = GOT_STATUS_MODIFY;
1838 } else if (flen == 0) {
1839 if (blen - hdrlen != 0)
1840 *status = GOT_STATUS_MODIFY;
1842 } else if (blen - hdrlen == flen) {
1843 /* Skip blob object header first time around. */
1844 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1845 *status = GOT_STATUS_MODIFY;
1849 *status = GOT_STATUS_MODIFY;
1855 if (*status == GOT_STATUS_MODIFY) {
1857 err = get_modified_file_content_status(status, f);
1858 } else if (xbit_differs(ie, sb->st_mode))
1859 *status = GOT_STATUS_MODE_CHANGE;
1862 got_object_blob_close(blob);
1863 if (f != NULL && fclose(f) == EOF && err == NULL)
1864 err = got_error_from_errno2("fclose", abspath);
1865 if (fd != -1 && close(fd) == -1 && err == NULL)
1866 err = got_error_from_errno2("close", abspath);
1871 * Update timestamps in the file index if a file is unmodified and
1872 * we had to run a full content comparison to find out.
1874 static const struct got_error *
1875 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1876 struct got_fileindex_entry *ie, struct stat *sb)
1878 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1879 return got_fileindex_entry_update(ie, wt_fd, path,
1880 ie->blob_sha1, ie->commit_sha1, 1);
1885 static const struct got_error *
1886 update_blob(struct got_worktree *worktree,
1887 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1888 struct got_tree_entry *te, const char *path,
1889 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1892 const struct got_error *err = NULL;
1893 struct got_blob_object *blob = NULL;
1895 unsigned char status = GOT_STATUS_NO_CHANGE;
1898 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1899 return got_error_from_errno("asprintf");
1902 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1903 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1906 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1910 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1911 sb.st_mode = got_fileindex_perms_to_st(ie);
1913 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1914 status = GOT_STATUS_UNVERSIONED;
1917 if (status == GOT_STATUS_OBSTRUCTED) {
1918 err = (*progress_cb)(progress_arg, status, path);
1921 if (status == GOT_STATUS_CONFLICT) {
1922 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1927 if (ie && status != GOT_STATUS_MISSING &&
1928 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1929 if (got_fileindex_entry_has_commit(ie) &&
1930 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1931 SHA1_DIGEST_LENGTH) == 0) {
1932 err = sync_timestamps(worktree->root_fd,
1933 path, status, ie, &sb);
1936 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1940 if (got_fileindex_entry_has_blob(ie) &&
1941 memcmp(ie->blob_sha1, te->id.sha1,
1942 SHA1_DIGEST_LENGTH) == 0) {
1943 err = sync_timestamps(worktree->root_fd,
1944 path, status, ie, &sb);
1949 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1953 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1954 int update_timestamps;
1955 struct got_blob_object *blob2 = NULL;
1956 char *label_orig = NULL;
1957 if (got_fileindex_entry_has_blob(ie)) {
1958 struct got_object_id id2;
1959 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1960 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1964 if (got_fileindex_entry_has_commit(ie)) {
1965 char id_str[SHA1_DIGEST_STRING_LENGTH];
1966 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1967 sizeof(id_str)) == NULL) {
1968 err = got_error_path(id_str,
1969 GOT_ERR_BAD_OBJ_ID_STR);
1972 if (asprintf(&label_orig, "%s: commit %s",
1973 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1974 err = got_error_from_errno("asprintf");
1978 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1980 err = got_object_blob_read_to_str(&link_target, blob);
1983 err = merge_symlink(worktree, blob2, ondisk_path, path,
1984 label_orig, link_target, worktree->base_commit_id,
1985 repo, progress_cb, progress_arg);
1988 err = merge_blob(&update_timestamps, worktree, blob2,
1989 ondisk_path, path, sb.st_mode, label_orig, blob,
1990 worktree->base_commit_id, repo,
1991 progress_cb, progress_arg);
1995 got_object_blob_close(blob2);
1999 * Do not update timestamps of files with local changes.
2000 * Otherwise, a future status walk would treat them as
2001 * unmodified files again.
2003 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2004 blob->id.sha1, worktree->base_commit_id->sha1,
2006 } else if (status == GOT_STATUS_MODE_CHANGE) {
2007 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2008 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2009 } else if (status == GOT_STATUS_DELETE) {
2010 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2013 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2014 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2018 int is_bad_symlink = 0;
2019 if (S_ISLNK(te->mode)) {
2020 err = install_symlink(&is_bad_symlink, worktree,
2021 ondisk_path, path, blob,
2022 status == GOT_STATUS_MISSING, 0,
2023 status == GOT_STATUS_UNVERSIONED, repo,
2024 progress_cb, progress_arg);
2026 err = install_blob(worktree, ondisk_path, path,
2027 te->mode, sb.st_mode, blob,
2028 status == GOT_STATUS_MISSING, 0, 0,
2029 status == GOT_STATUS_UNVERSIONED, repo,
2030 progress_cb, progress_arg);
2036 err = got_fileindex_entry_update(ie,
2037 worktree->root_fd, path, blob->id.sha1,
2038 worktree->base_commit_id->sha1, 1);
2040 err = create_fileindex_entry(&ie, fileindex,
2041 worktree->base_commit_id, worktree->root_fd, path,
2047 if (is_bad_symlink) {
2048 got_fileindex_entry_filetype_set(ie,
2049 GOT_FILEIDX_MODE_BAD_SYMLINK);
2052 got_object_blob_close(blob);
2058 static const struct got_error *
2059 remove_ondisk_file(const char *root_path, const char *path)
2061 const struct got_error *err = NULL;
2062 char *ondisk_path = NULL;
2064 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2065 return got_error_from_errno("asprintf");
2067 if (unlink(ondisk_path) == -1) {
2068 if (errno != ENOENT)
2069 err = got_error_from_errno2("unlink", ondisk_path);
2071 size_t root_len = strlen(root_path);
2074 err = got_path_dirname(&parent, ondisk_path);
2078 ondisk_path = parent;
2079 if (rmdir(ondisk_path) == -1) {
2080 if (errno != ENOTEMPTY)
2081 err = got_error_from_errno2("rmdir",
2085 } while (got_path_cmp(ondisk_path, root_path,
2086 strlen(ondisk_path), root_len) != 0);
2092 static const struct got_error *
2093 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2094 struct got_fileindex_entry *ie, struct got_repository *repo,
2095 got_worktree_checkout_cb progress_cb, void *progress_arg)
2097 const struct got_error *err = NULL;
2098 unsigned char status;
2102 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2103 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2105 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2107 return got_error_from_errno("asprintf");
2109 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2113 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2114 char ondisk_target[PATH_MAX];
2115 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2116 sizeof(ondisk_target));
2117 if (ondisk_len == -1) {
2118 err = got_error_from_errno2("readlink", ondisk_path);
2121 ondisk_target[ondisk_len] = '\0';
2122 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2123 NULL, NULL, /* XXX pass common ancestor info? */
2124 ondisk_target, ondisk_path);
2127 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2132 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2133 status == GOT_STATUS_ADD) {
2134 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2138 * Preserve the working file and change the deleted blob's
2139 * entry into a schedule-add entry.
2141 err = got_fileindex_entry_update(ie, worktree->root_fd,
2142 ie->path, NULL, NULL, 0);
2144 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2147 if (status == GOT_STATUS_NO_CHANGE) {
2148 err = remove_ondisk_file(worktree->root_path, ie->path);
2152 got_fileindex_entry_remove(fileindex, ie);
2159 struct diff_cb_arg {
2160 struct got_fileindex *fileindex;
2161 struct got_worktree *worktree;
2162 struct got_repository *repo;
2163 got_worktree_checkout_cb progress_cb;
2165 got_cancel_cb cancel_cb;
2169 static const struct got_error *
2170 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2171 struct got_tree_entry *te, const char *parent_path)
2173 struct diff_cb_arg *a = arg;
2175 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2176 return got_error(GOT_ERR_CANCELLED);
2178 return update_blob(a->worktree, a->fileindex, ie, te,
2179 ie->path, a->repo, a->progress_cb, a->progress_arg);
2182 static const struct got_error *
2183 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2185 struct diff_cb_arg *a = arg;
2187 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2188 return got_error(GOT_ERR_CANCELLED);
2190 return delete_blob(a->worktree, a->fileindex, ie,
2191 a->repo, a->progress_cb, a->progress_arg);
2194 static const struct got_error *
2195 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2197 struct diff_cb_arg *a = arg;
2198 const struct got_error *err;
2201 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2202 return got_error(GOT_ERR_CANCELLED);
2204 if (got_object_tree_entry_is_submodule(te))
2207 if (asprintf(&path, "%s%s%s", parent_path,
2208 parent_path[0] ? "/" : "", te->name)
2210 return got_error_from_errno("asprintf");
2212 if (S_ISDIR(te->mode))
2213 err = add_dir_on_disk(a->worktree, path);
2215 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2216 a->repo, a->progress_cb, a->progress_arg);
2222 const struct got_error *
2223 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2225 uint32_t uuid_status;
2227 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2228 if (uuid_status != uuid_s_ok) {
2230 return got_error_uuid(uuid_status, "uuid_to_string");
2236 static const struct got_error *
2237 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2239 const struct got_error *err = NULL;
2240 char *uuidstr = NULL;
2244 err = got_worktree_get_uuid(&uuidstr, worktree);
2248 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2249 err = got_error_from_errno("asprintf");
2256 const struct got_error *
2257 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2259 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2262 static const struct got_error *
2263 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2265 return get_ref_name(refname, worktree,
2266 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2269 static const struct got_error *
2270 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2272 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2275 static const struct got_error *
2276 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2278 return get_ref_name(refname, worktree,
2279 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2282 static const struct got_error *
2283 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2285 return get_ref_name(refname, worktree,
2286 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2289 static const struct got_error *
2290 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2292 return get_ref_name(refname, worktree,
2293 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2296 static const struct got_error *
2297 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2299 return get_ref_name(refname, worktree,
2300 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2303 static const struct got_error *
2304 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2306 return get_ref_name(refname, worktree,
2307 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2310 static const struct got_error *
2311 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2313 return get_ref_name(refname, worktree,
2314 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2317 const struct got_error *
2318 got_worktree_get_histedit_script_path(char **path,
2319 struct got_worktree *worktree)
2321 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2322 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2324 return got_error_from_errno("asprintf");
2330 * Prevent Git's garbage collector from deleting our base commit by
2331 * setting a reference to our base commit's ID.
2333 static const struct got_error *
2334 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2336 const struct got_error *err = NULL;
2337 struct got_reference *ref = NULL;
2340 err = got_worktree_get_base_ref_name(&refname, worktree);
2344 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2348 err = got_ref_write(ref, repo);
2356 static const struct got_error *
2357 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2359 const struct got_error *err = NULL;
2361 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2362 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2363 err = got_error_from_errno("asprintf");
2364 *fileindex_path = NULL;
2370 static const struct got_error *
2371 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2372 struct got_worktree *worktree)
2374 const struct got_error *err = NULL;
2377 *fileindex_path = NULL;
2378 *fileindex = got_fileindex_alloc();
2379 if (*fileindex == NULL)
2380 return got_error_from_errno("got_fileindex_alloc");
2382 err = get_fileindex_path(fileindex_path, worktree);
2386 index = fopen(*fileindex_path, "rb");
2387 if (index == NULL) {
2388 if (errno != ENOENT)
2389 err = got_error_from_errno2("fopen", *fileindex_path);
2391 err = got_fileindex_read(*fileindex, index);
2392 if (fclose(index) == EOF && err == NULL)
2393 err = got_error_from_errno("fclose");
2397 free(*fileindex_path);
2398 *fileindex_path = NULL;
2399 got_fileindex_free(*fileindex);
2405 struct bump_base_commit_id_arg {
2406 struct got_object_id *base_commit_id;
2409 const char *entry_name;
2410 got_worktree_checkout_cb progress_cb;
2414 /* Bump base commit ID of all files within an updated part of the work tree. */
2415 static const struct got_error *
2416 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2418 const struct got_error *err;
2419 struct bump_base_commit_id_arg *a = arg;
2421 if (a->entry_name) {
2422 if (strcmp(ie->path, a->path) != 0)
2424 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2427 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2428 SHA1_DIGEST_LENGTH) == 0)
2431 if (a->progress_cb) {
2432 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2437 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2441 static const struct got_error *
2442 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2443 struct got_fileindex *fileindex,
2444 got_worktree_checkout_cb progress_cb, void *progress_arg)
2446 struct bump_base_commit_id_arg bbc_arg;
2448 bbc_arg.base_commit_id = worktree->base_commit_id;
2449 bbc_arg.entry_name = NULL;
2451 bbc_arg.path_len = 0;
2452 bbc_arg.progress_cb = progress_cb;
2453 bbc_arg.progress_arg = progress_arg;
2455 return got_fileindex_for_each_entry_safe(fileindex,
2456 bump_base_commit_id, &bbc_arg);
2459 static const struct got_error *
2460 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2462 const struct got_error *err = NULL;
2463 char *new_fileindex_path = NULL;
2464 FILE *new_index = NULL;
2465 struct timespec timeout;
2467 err = got_opentemp_named(&new_fileindex_path, &new_index,
2472 err = got_fileindex_write(fileindex, new_index);
2476 if (rename(new_fileindex_path, fileindex_path) != 0) {
2477 err = got_error_from_errno3("rename", new_fileindex_path,
2479 unlink(new_fileindex_path);
2483 * Sleep for a short amount of time to ensure that files modified after
2484 * this program exits have a different time stamp from the one which
2485 * was recorded in the file index.
2488 timeout.tv_nsec = 1;
2489 nanosleep(&timeout, NULL);
2493 free(new_fileindex_path);
2497 static const struct got_error *
2498 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2499 struct got_object_id **tree_id, const char *wt_relpath,
2500 struct got_worktree *worktree, struct got_repository *repo)
2502 const struct got_error *err = NULL;
2503 struct got_object_id *id = NULL;
2504 char *in_repo_path = NULL;
2505 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2507 *entry_type = GOT_OBJ_TYPE_ANY;
2508 *tree_relpath = NULL;
2511 if (wt_relpath[0] == '\0') {
2512 /* Check out all files within the work tree. */
2513 *entry_type = GOT_OBJ_TYPE_TREE;
2514 *tree_relpath = strdup("");
2515 if (*tree_relpath == NULL) {
2516 err = got_error_from_errno("strdup");
2519 err = got_object_id_by_path(tree_id, repo,
2520 worktree->base_commit_id, worktree->path_prefix);
2526 /* Check out a subset of files in the work tree. */
2528 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2529 is_root_wt ? "" : "/", wt_relpath) == -1) {
2530 err = got_error_from_errno("asprintf");
2534 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2540 in_repo_path = NULL;
2542 err = got_object_get_type(entry_type, repo, id);
2546 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2547 /* Check out a single file. */
2548 if (strchr(wt_relpath, '/') == NULL) {
2549 /* Check out a single file in work tree's root dir. */
2550 in_repo_path = strdup(worktree->path_prefix);
2551 if (in_repo_path == NULL) {
2552 err = got_error_from_errno("strdup");
2555 *tree_relpath = strdup("");
2556 if (*tree_relpath == NULL) {
2557 err = got_error_from_errno("strdup");
2561 /* Check out a single file in a subdirectory. */
2562 err = got_path_dirname(tree_relpath, wt_relpath);
2565 if (asprintf(&in_repo_path, "%s%s%s",
2566 worktree->path_prefix, is_root_wt ? "" : "/",
2567 *tree_relpath) == -1) {
2568 err = got_error_from_errno("asprintf");
2572 err = got_object_id_by_path(tree_id, repo,
2573 worktree->base_commit_id, in_repo_path);
2575 /* Check out all files within a subdirectory. */
2576 *tree_id = got_object_id_dup(id);
2577 if (*tree_id == NULL) {
2578 err = got_error_from_errno("got_object_id_dup");
2581 *tree_relpath = strdup(wt_relpath);
2582 if (*tree_relpath == NULL) {
2583 err = got_error_from_errno("strdup");
2591 *entry_type = GOT_OBJ_TYPE_ANY;
2592 free(*tree_relpath);
2593 *tree_relpath = NULL;
2600 static const struct got_error *
2601 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2602 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2603 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2604 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2606 const struct got_error *err = NULL;
2607 struct got_commit_object *commit = NULL;
2608 struct got_tree_object *tree = NULL;
2609 struct got_fileindex_diff_tree_cb diff_cb;
2610 struct diff_cb_arg arg;
2612 err = ref_base_commit(worktree, repo);
2614 if (!(err->code == GOT_ERR_ERRNO &&
2615 (errno == EACCES || errno == EROFS)))
2617 err = (*progress_cb)(progress_arg,
2618 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2623 err = got_object_open_as_commit(&commit, repo,
2624 worktree->base_commit_id);
2628 err = got_object_open_as_tree(&tree, repo, tree_id);
2633 got_object_tree_find_entry(tree, entry_name) == NULL) {
2634 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2638 diff_cb.diff_old_new = diff_old_new;
2639 diff_cb.diff_old = diff_old;
2640 diff_cb.diff_new = diff_new;
2641 arg.fileindex = fileindex;
2642 arg.worktree = worktree;
2644 arg.progress_cb = progress_cb;
2645 arg.progress_arg = progress_arg;
2646 arg.cancel_cb = cancel_cb;
2647 arg.cancel_arg = cancel_arg;
2648 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2649 entry_name, repo, &diff_cb, &arg);
2652 got_object_tree_close(tree);
2654 got_object_commit_close(commit);
2658 const struct got_error *
2659 got_worktree_checkout_files(struct got_worktree *worktree,
2660 struct got_pathlist_head *paths, struct got_repository *repo,
2661 got_worktree_checkout_cb progress_cb, void *progress_arg,
2662 got_cancel_cb cancel_cb, void *cancel_arg)
2664 const struct got_error *err = NULL, *sync_err, *unlockerr;
2665 struct got_commit_object *commit = NULL;
2666 struct got_tree_object *tree = NULL;
2667 struct got_fileindex *fileindex = NULL;
2668 char *fileindex_path = NULL;
2669 struct got_pathlist_entry *pe;
2670 struct tree_path_data {
2671 SIMPLEQ_ENTRY(tree_path_data) entry;
2672 struct got_object_id *tree_id;
2677 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2679 SIMPLEQ_INIT(&tree_paths);
2681 err = lock_worktree(worktree, LOCK_EX);
2685 /* Map all specified paths to in-repository trees. */
2686 TAILQ_FOREACH(pe, paths, entry) {
2687 tpd = malloc(sizeof(*tpd));
2689 err = got_error_from_errno("malloc");
2693 err = find_tree_entry_for_checkout(&tpd->entry_type,
2694 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2700 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2701 err = got_path_basename(&tpd->entry_name, pe->path);
2709 tpd->entry_name = NULL;
2711 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2715 * Read the file index.
2716 * Checking out files is supposed to be an idempotent operation.
2717 * If the on-disk file index is incomplete we will try to complete it.
2719 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2723 tpd = SIMPLEQ_FIRST(&tree_paths);
2724 TAILQ_FOREACH(pe, paths, entry) {
2725 struct bump_base_commit_id_arg bbc_arg;
2727 err = checkout_files(worktree, fileindex, tpd->relpath,
2728 tpd->tree_id, tpd->entry_name, repo,
2729 progress_cb, progress_arg, cancel_cb, cancel_arg);
2733 bbc_arg.base_commit_id = worktree->base_commit_id;
2734 bbc_arg.entry_name = tpd->entry_name;
2735 bbc_arg.path = pe->path;
2736 bbc_arg.path_len = pe->path_len;
2737 bbc_arg.progress_cb = progress_cb;
2738 bbc_arg.progress_arg = progress_arg;
2739 err = got_fileindex_for_each_entry_safe(fileindex,
2740 bump_base_commit_id, &bbc_arg);
2744 tpd = SIMPLEQ_NEXT(tpd, entry);
2746 sync_err = sync_fileindex(fileindex, fileindex_path);
2747 if (sync_err && err == NULL)
2750 free(fileindex_path);
2752 got_object_tree_close(tree);
2754 got_object_commit_close(commit);
2756 got_fileindex_free(fileindex);
2757 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2758 tpd = SIMPLEQ_FIRST(&tree_paths);
2759 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2764 unlockerr = lock_worktree(worktree, LOCK_SH);
2765 if (unlockerr && err == NULL)
2770 struct merge_file_cb_arg {
2771 struct got_worktree *worktree;
2772 struct got_fileindex *fileindex;
2773 got_worktree_checkout_cb progress_cb;
2775 got_cancel_cb cancel_cb;
2777 const char *label_orig;
2778 struct got_object_id *commit_id2;
2781 static const struct got_error *
2782 merge_file_cb(void *arg, struct got_blob_object *blob1,
2783 struct got_blob_object *blob2, struct got_object_id *id1,
2784 struct got_object_id *id2, const char *path1, const char *path2,
2785 mode_t mode1, mode_t mode2, struct got_repository *repo)
2787 static const struct got_error *err = NULL;
2788 struct merge_file_cb_arg *a = arg;
2789 struct got_fileindex_entry *ie;
2790 char *ondisk_path = NULL;
2792 unsigned char status;
2793 int local_changes_subsumed;
2795 if (blob1 && blob2) {
2796 ie = got_fileindex_entry_get(a->fileindex, path2,
2799 return (*a->progress_cb)(a->progress_arg,
2800 GOT_STATUS_MISSING, path2);
2802 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2804 return got_error_from_errno("asprintf");
2806 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2811 if (status == GOT_STATUS_DELETE) {
2812 err = (*a->progress_cb)(a->progress_arg,
2813 GOT_STATUS_MERGE, path2);
2816 if (status != GOT_STATUS_NO_CHANGE &&
2817 status != GOT_STATUS_MODIFY &&
2818 status != GOT_STATUS_CONFLICT &&
2819 status != GOT_STATUS_ADD) {
2820 err = (*a->progress_cb)(a->progress_arg, status, path2);
2824 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2826 err = got_object_blob_read_to_str(&link_target2, blob2);
2829 err = merge_symlink(a->worktree, blob1, ondisk_path,
2830 path2, a->label_orig, link_target2, a->commit_id2,
2831 repo, a->progress_cb, a->progress_arg);
2834 err = merge_blob(&local_changes_subsumed, a->worktree,
2835 blob1, ondisk_path, path2, sb.st_mode,
2836 a->label_orig, blob2, a->commit_id2, repo,
2837 a->progress_cb, a->progress_arg);
2840 ie = got_fileindex_entry_get(a->fileindex, path1,
2843 return (*a->progress_cb)(a->progress_arg,
2844 GOT_STATUS_MISSING, path1);
2846 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2848 return got_error_from_errno("asprintf");
2850 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2856 case GOT_STATUS_NO_CHANGE:
2857 err = (*a->progress_cb)(a->progress_arg,
2858 GOT_STATUS_DELETE, path1);
2861 err = remove_ondisk_file(a->worktree->root_path, path1);
2865 got_fileindex_entry_mark_deleted_from_disk(ie);
2867 case GOT_STATUS_DELETE:
2868 case GOT_STATUS_MISSING:
2869 err = (*a->progress_cb)(a->progress_arg,
2870 GOT_STATUS_DELETE, path1);
2874 got_fileindex_entry_mark_deleted_from_disk(ie);
2876 case GOT_STATUS_ADD: {
2877 struct got_object_id *id;
2880 * Delete the added file only if its content already
2881 * exists in the repository.
2883 err = got_object_blob_file_create(&id, &blob1_f, path1);
2886 if (got_object_id_cmp(id, id1) == 0) {
2887 err = (*a->progress_cb)(a->progress_arg,
2888 GOT_STATUS_DELETE, path1);
2891 err = remove_ondisk_file(a->worktree->root_path,
2896 got_fileindex_entry_remove(a->fileindex,
2899 err = (*a->progress_cb)(a->progress_arg,
2900 GOT_STATUS_CANNOT_DELETE, path1);
2902 if (fclose(blob1_f) == EOF && err == NULL)
2903 err = got_error_from_errno("fclose");
2909 case GOT_STATUS_MODIFY:
2910 case GOT_STATUS_CONFLICT:
2911 err = (*a->progress_cb)(a->progress_arg,
2912 GOT_STATUS_CANNOT_DELETE, path1);
2916 case GOT_STATUS_OBSTRUCTED:
2917 err = (*a->progress_cb)(a->progress_arg, status, path1);
2925 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2927 return got_error_from_errno("asprintf");
2928 ie = got_fileindex_entry_get(a->fileindex, path2,
2931 err = get_file_status(&status, &sb, ie, ondisk_path,
2935 if (status != GOT_STATUS_NO_CHANGE &&
2936 status != GOT_STATUS_MODIFY &&
2937 status != GOT_STATUS_CONFLICT &&
2938 status != GOT_STATUS_ADD) {
2939 err = (*a->progress_cb)(a->progress_arg,
2943 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2945 err = got_object_blob_read_to_str(&link_target2,
2949 err = merge_symlink(a->worktree, NULL,
2950 ondisk_path, path2, a->label_orig,
2951 link_target2, a->commit_id2, repo,
2952 a->progress_cb, a->progress_arg);
2954 } else if (S_ISREG(sb.st_mode)) {
2955 err = merge_blob(&local_changes_subsumed,
2956 a->worktree, NULL, ondisk_path, path2,
2957 sb.st_mode, a->label_orig, blob2,
2958 a->commit_id2, repo, a->progress_cb,
2961 err = got_error_path(ondisk_path,
2962 GOT_ERR_FILE_OBSTRUCTED);
2966 if (status == GOT_STATUS_DELETE) {
2967 err = got_fileindex_entry_update(ie,
2968 a->worktree->root_fd, path2, blob2->id.sha1,
2969 a->worktree->base_commit_id->sha1, 0);
2974 int is_bad_symlink = 0;
2975 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2976 if (S_ISLNK(mode2)) {
2977 err = install_symlink(&is_bad_symlink,
2978 a->worktree, ondisk_path, path2, blob2, 0,
2979 0, 1, repo, a->progress_cb, a->progress_arg);
2981 err = install_blob(a->worktree, ondisk_path, path2,
2982 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2983 a->progress_cb, a->progress_arg);
2987 err = got_fileindex_entry_alloc(&ie, path2);
2990 err = got_fileindex_entry_update(ie,
2991 a->worktree->root_fd, path2, NULL, NULL, 1);
2993 got_fileindex_entry_free(ie);
2996 err = got_fileindex_entry_add(a->fileindex, ie);
2998 got_fileindex_entry_free(ie);
3001 if (is_bad_symlink) {
3002 got_fileindex_entry_filetype_set(ie,
3003 GOT_FILEIDX_MODE_BAD_SYMLINK);
3012 struct check_merge_ok_arg {
3013 struct got_worktree *worktree;
3014 struct got_repository *repo;
3017 static const struct got_error *
3018 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3020 const struct got_error *err = NULL;
3021 struct check_merge_ok_arg *a = arg;
3022 unsigned char status;
3026 /* Reject merges into a work tree with mixed base commits. */
3027 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3028 SHA1_DIGEST_LENGTH))
3029 return got_error(GOT_ERR_MIXED_COMMITS);
3031 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3033 return got_error_from_errno("asprintf");
3035 /* Reject merges into a work tree with conflicted files. */
3036 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3039 if (status == GOT_STATUS_CONFLICT)
3040 return got_error(GOT_ERR_CONFLICTS);
3045 static const struct got_error *
3046 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3047 const char *fileindex_path, struct got_object_id *commit_id1,
3048 struct got_object_id *commit_id2, struct got_repository *repo,
3049 got_worktree_checkout_cb progress_cb, void *progress_arg,
3050 got_cancel_cb cancel_cb, void *cancel_arg)
3052 const struct got_error *err = NULL, *sync_err;
3053 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3054 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3055 struct merge_file_cb_arg arg;
3056 char *label_orig = NULL;
3059 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3060 worktree->path_prefix);
3061 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3067 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3071 err = got_object_id_str(&id_str, commit_id1);
3075 if (asprintf(&label_orig, "%s: commit %s",
3076 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3077 err = got_error_from_errno("asprintf");
3084 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3085 worktree->path_prefix);
3089 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3093 arg.worktree = worktree;
3094 arg.fileindex = fileindex;
3095 arg.progress_cb = progress_cb;
3096 arg.progress_arg = progress_arg;
3097 arg.cancel_cb = cancel_cb;
3098 arg.cancel_arg = cancel_arg;
3099 arg.label_orig = label_orig;
3100 arg.commit_id2 = commit_id2;
3101 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3102 sync_err = sync_fileindex(fileindex, fileindex_path);
3103 if (sync_err && err == NULL)
3107 got_object_tree_close(tree1);
3109 got_object_tree_close(tree2);
3114 const struct got_error *
3115 got_worktree_merge_files(struct got_worktree *worktree,
3116 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3117 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3118 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3120 const struct got_error *err, *unlockerr;
3121 char *fileindex_path = NULL;
3122 struct got_fileindex *fileindex = NULL;
3123 struct check_merge_ok_arg mok_arg;
3125 err = lock_worktree(worktree, LOCK_EX);
3129 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3133 mok_arg.worktree = worktree;
3134 mok_arg.repo = repo;
3135 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3140 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3141 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3144 got_fileindex_free(fileindex);
3145 free(fileindex_path);
3146 unlockerr = lock_worktree(worktree, LOCK_SH);
3147 if (unlockerr && err == NULL)
3152 struct diff_dir_cb_arg {
3153 struct got_fileindex *fileindex;
3154 struct got_worktree *worktree;
3155 const char *status_path;
3156 size_t status_path_len;
3157 struct got_repository *repo;
3158 got_worktree_status_cb status_cb;
3160 got_cancel_cb cancel_cb;
3162 /* A pathlist containing per-directory pathlists of ignore patterns. */
3163 struct got_pathlist_head ignores;
3164 int report_unchanged;
3168 static const struct got_error *
3169 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3170 int dirfd, const char *de_name,
3171 got_worktree_status_cb status_cb, void *status_arg,
3172 struct got_repository *repo, int report_unchanged)
3174 const struct got_error *err = NULL;
3175 unsigned char status = GOT_STATUS_NO_CHANGE;
3176 unsigned char staged_status = get_staged_status(ie);
3178 struct got_object_id blob_id, commit_id, staged_blob_id;
3179 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3180 struct got_object_id *staged_blob_idp = NULL;
3182 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3186 if (status == GOT_STATUS_NO_CHANGE &&
3187 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3190 if (got_fileindex_entry_has_blob(ie)) {
3191 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3192 blob_idp = &blob_id;
3194 if (got_fileindex_entry_has_commit(ie)) {
3195 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3196 commit_idp = &commit_id;
3198 if (staged_status == GOT_STATUS_ADD ||
3199 staged_status == GOT_STATUS_MODIFY) {
3200 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3201 SHA1_DIGEST_LENGTH);
3202 staged_blob_idp = &staged_blob_id;
3205 return (*status_cb)(status_arg, status, staged_status,
3206 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3209 static const struct got_error *
3210 status_old_new(void *arg, struct got_fileindex_entry *ie,
3211 struct dirent *de, const char *parent_path, int dirfd)
3213 const struct got_error *err = NULL;
3214 struct diff_dir_cb_arg *a = arg;
3217 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3218 return got_error(GOT_ERR_CANCELLED);
3220 if (got_path_cmp(parent_path, a->status_path,
3221 strlen(parent_path), a->status_path_len) != 0 &&
3222 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3225 if (parent_path[0]) {
3226 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3227 parent_path, de->d_name) == -1)
3228 return got_error_from_errno("asprintf");
3230 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3232 return got_error_from_errno("asprintf");
3235 err = report_file_status(ie, abspath, dirfd, de->d_name,
3236 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3241 static const struct got_error *
3242 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3244 struct diff_dir_cb_arg *a = arg;
3245 struct got_object_id blob_id, commit_id;
3246 unsigned char status;
3248 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3249 return got_error(GOT_ERR_CANCELLED);
3251 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3254 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3255 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3256 if (got_fileindex_entry_has_file_on_disk(ie))
3257 status = GOT_STATUS_MISSING;
3259 status = GOT_STATUS_DELETE;
3260 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3261 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3265 free_ignorelist(struct got_pathlist_head *ignorelist)
3267 struct got_pathlist_entry *pe;
3269 TAILQ_FOREACH(pe, ignorelist, entry)
3270 free((char *)pe->path);
3271 got_pathlist_free(ignorelist);
3275 free_ignores(struct got_pathlist_head *ignores)
3277 struct got_pathlist_entry *pe;
3279 TAILQ_FOREACH(pe, ignores, entry) {
3280 struct got_pathlist_head *ignorelist = pe->data;
3281 free_ignorelist(ignorelist);
3282 free((char *)pe->path);
3284 got_pathlist_free(ignores);
3287 static const struct got_error *
3288 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3290 const struct got_error *err = NULL;
3291 struct got_pathlist_entry *pe = NULL;
3292 struct got_pathlist_head *ignorelist;
3293 char *line = NULL, *pattern, *dirpath = NULL;
3294 size_t linesize = 0;
3297 ignorelist = calloc(1, sizeof(*ignorelist));
3298 if (ignorelist == NULL)
3299 return got_error_from_errno("calloc");
3300 TAILQ_INIT(ignorelist);
3302 while ((linelen = getline(&line, &linesize, f)) != -1) {
3303 if (linelen > 0 && line[linelen - 1] == '\n')
3304 line[linelen - 1] = '\0';
3306 /* Git's ignores may contain comments. */
3310 /* Git's negated patterns are not (yet?) supported. */
3314 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3316 err = got_error_from_errno("asprintf");
3319 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3324 err = got_error_from_errno("getline");
3328 dirpath = strdup(path);
3329 if (dirpath == NULL) {
3330 err = got_error_from_errno("strdup");
3333 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3336 if (err || pe == NULL) {
3338 free_ignorelist(ignorelist);
3344 match_ignores(struct got_pathlist_head *ignores, const char *path)
3346 struct got_pathlist_entry *pe;
3348 /* Handle patterns which match in all directories. */
3349 TAILQ_FOREACH(pe, ignores, entry) {
3350 struct got_pathlist_head *ignorelist = pe->data;
3351 struct got_pathlist_entry *pi;
3353 TAILQ_FOREACH(pi, ignorelist, entry) {
3354 const char *p, *pattern = pi->path;
3356 if (strncmp(pattern, "**/", 3) != 0)
3361 if (fnmatch(pattern, p,
3362 FNM_PATHNAME | FNM_LEADING_DIR)) {
3363 /* Retry in next directory. */
3364 while (*p && *p != '/')
3376 * The ignores pathlist contains ignore lists from children before
3377 * parents, so we can find the most specific ignorelist by walking
3378 * ignores backwards.
3380 pe = TAILQ_LAST(ignores, got_pathlist_head);
3382 if (got_path_is_child(path, pe->path, pe->path_len)) {
3383 struct got_pathlist_head *ignorelist = pe->data;
3384 struct got_pathlist_entry *pi;
3385 TAILQ_FOREACH(pi, ignorelist, entry) {
3386 const char *pattern = pi->path;
3387 int flags = FNM_LEADING_DIR;
3388 if (strstr(pattern, "/**/") == NULL)
3389 flags |= FNM_PATHNAME;
3390 if (fnmatch(pattern, path, flags))
3395 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3401 static const struct got_error *
3402 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3403 const char *path, int dirfd, const char *ignores_filename)
3405 const struct got_error *err = NULL;
3408 FILE *ignoresfile = NULL;
3410 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3411 path[0] ? "/" : "", ignores_filename) == -1)
3412 return got_error_from_errno("asprintf");
3415 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3417 if (errno != ENOENT && errno != EACCES)
3418 err = got_error_from_errno2("openat",
3421 ignoresfile = fdopen(fd, "r");
3422 if (ignoresfile == NULL)
3423 err = got_error_from_errno2("fdopen",
3427 err = read_ignores(ignores, path, ignoresfile);
3431 ignoresfile = fopen(ignorespath, "r");
3432 if (ignoresfile == NULL) {
3433 if (errno != ENOENT && errno != EACCES)
3434 err = got_error_from_errno2("fopen",
3437 err = read_ignores(ignores, path, ignoresfile);
3440 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3441 err = got_error_from_errno2("fclose", path);
3442 if (fd != -1 && close(fd) == -1 && err == NULL)
3443 err = got_error_from_errno2("close", path);
3448 static const struct got_error *
3449 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3451 const struct got_error *err = NULL;
3452 struct diff_dir_cb_arg *a = arg;
3455 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3456 return got_error(GOT_ERR_CANCELLED);
3458 if (parent_path[0]) {
3459 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3460 return got_error_from_errno("asprintf");
3465 if (de->d_type != DT_DIR &&
3466 got_path_is_child(path, a->status_path, a->status_path_len)
3467 && !match_ignores(&a->ignores, path))
3468 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3469 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3475 static const struct got_error *
3476 status_traverse(void *arg, const char *path, int dirfd)
3478 const struct got_error *err = NULL;
3479 struct diff_dir_cb_arg *a = arg;
3484 err = add_ignores(&a->ignores, a->worktree->root_path,
3485 path, dirfd, ".cvsignore");
3489 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3490 dirfd, ".gitignore");
3495 static const struct got_error *
3496 report_single_file_status(const char *path, const char *ondisk_path,
3497 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3498 void *status_arg, struct got_repository *repo, int report_unchanged)
3500 struct got_fileindex_entry *ie;
3503 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3505 return report_file_status(ie, ondisk_path, -1, NULL,
3506 status_cb, status_arg, repo, report_unchanged);
3508 if (lstat(ondisk_path, &sb) == -1) {
3509 if (errno != ENOENT)
3510 return got_error_from_errno2("lstat", ondisk_path);
3511 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3512 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3516 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3517 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3518 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3523 static const struct got_error *
3524 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3525 const char *root_path, const char *path)
3527 const struct got_error *err;
3528 char *parent_path, *next_parent_path = NULL;
3530 err = add_ignores(ignores, root_path, "", -1,
3535 err = add_ignores(ignores, root_path, "", -1,
3540 err = got_path_dirname(&parent_path, path);
3542 if (err->code == GOT_ERR_BAD_PATH)
3543 return NULL; /* cannot traverse parent */
3547 err = add_ignores(ignores, root_path, parent_path, -1,
3551 err = add_ignores(ignores, root_path, parent_path, -1,
3555 err = got_path_dirname(&next_parent_path, parent_path);
3557 if (err->code == GOT_ERR_BAD_PATH)
3558 err = NULL; /* traversed everything */
3562 parent_path = next_parent_path;
3563 next_parent_path = NULL;
3567 free(next_parent_path);
3571 static const struct got_error *
3572 worktree_status(struct got_worktree *worktree, const char *path,
3573 struct got_fileindex *fileindex, struct got_repository *repo,
3574 got_worktree_status_cb status_cb, void *status_arg,
3575 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3576 int report_unchanged)
3578 const struct got_error *err = NULL;
3580 struct got_fileindex_diff_dir_cb fdiff_cb;
3581 struct diff_dir_cb_arg arg;
3582 char *ondisk_path = NULL;
3584 TAILQ_INIT(&arg.ignores);
3586 if (asprintf(&ondisk_path, "%s%s%s",
3587 worktree->root_path, path[0] ? "/" : "", path) == -1)
3588 return got_error_from_errno("asprintf");
3590 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3592 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3594 err = got_error_from_errno2("open", ondisk_path);
3596 err = report_single_file_status(path, ondisk_path,
3597 fileindex, status_cb, status_arg, repo,
3600 fdiff_cb.diff_old_new = status_old_new;
3601 fdiff_cb.diff_old = status_old;
3602 fdiff_cb.diff_new = status_new;
3603 fdiff_cb.diff_traverse = status_traverse;
3604 arg.fileindex = fileindex;
3605 arg.worktree = worktree;
3606 arg.status_path = path;
3607 arg.status_path_len = strlen(path);
3609 arg.status_cb = status_cb;
3610 arg.status_arg = status_arg;
3611 arg.cancel_cb = cancel_cb;
3612 arg.cancel_arg = cancel_arg;
3613 arg.report_unchanged = report_unchanged;
3614 arg.no_ignores = no_ignores;
3616 err = add_ignores_from_parent_paths(&arg.ignores,
3617 worktree->root_path, path);
3621 err = got_fileindex_diff_dir(fileindex, fd,
3622 worktree->root_path, path, repo, &fdiff_cb, &arg);
3625 free_ignores(&arg.ignores);
3626 if (fd != -1 && close(fd) != 0 && err == NULL)
3627 err = got_error_from_errno("close");
3632 const struct got_error *
3633 got_worktree_status(struct got_worktree *worktree,
3634 struct got_pathlist_head *paths, struct got_repository *repo,
3635 got_worktree_status_cb status_cb, void *status_arg,
3636 got_cancel_cb cancel_cb, void *cancel_arg)
3638 const struct got_error *err = NULL;
3639 char *fileindex_path = NULL;
3640 struct got_fileindex *fileindex = NULL;
3641 struct got_pathlist_entry *pe;
3643 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3647 TAILQ_FOREACH(pe, paths, entry) {
3648 err = worktree_status(worktree, pe->path, fileindex, repo,
3649 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3653 free(fileindex_path);
3654 got_fileindex_free(fileindex);
3658 const struct got_error *
3659 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3662 const struct got_error *err = NULL;
3663 char *resolved = NULL, *cwd = NULL, *path = NULL;
3666 char *abspath = NULL;
3667 char canonpath[PATH_MAX];
3671 cwd = getcwd(NULL, 0);
3673 return got_error_from_errno("getcwd");
3675 if (lstat(arg, &sb) == -1) {
3676 if (errno != ENOENT) {
3677 err = got_error_from_errno2("lstat", arg);
3682 if (S_ISLNK(sb.st_mode)) {
3684 * We cannot use realpath(3) with symlinks since we want to
3685 * operate on the symlink itself.
3686 * But we can make the path absolute, assuming it is relative
3687 * to the current working directory, and then canonicalize it.
3689 if (!got_path_is_absolute(arg)) {
3690 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3691 err = got_error_from_errno("asprintf");
3696 err = got_canonpath(abspath ? abspath : arg, canonpath,
3700 resolved = strdup(canonpath);
3701 if (resolved == NULL) {
3702 err = got_error_from_errno("strdup");
3706 resolved = realpath(arg, NULL);
3707 if (resolved == NULL) {
3708 if (errno != ENOENT) {
3709 err = got_error_from_errno2("realpath", arg);
3712 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3713 err = got_error_from_errno("asprintf");
3716 err = got_canonpath(abspath, canonpath,
3720 resolved = strdup(canonpath);
3721 if (resolved == NULL) {
3722 err = got_error_from_errno("strdup");
3728 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3729 strlen(got_worktree_get_root_path(worktree)))) {
3730 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3734 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3735 err = got_path_skip_common_ancestor(&path,
3736 got_worktree_get_root_path(worktree), resolved);
3742 err = got_error_from_errno("strdup");
3747 /* XXX status walk can't deal with trailing slash! */
3749 while (len > 0 && path[len - 1] == '/') {
3750 path[len - 1] = '\0';
3764 struct schedule_addition_args {
3765 struct got_worktree *worktree;
3766 struct got_fileindex *fileindex;
3767 got_worktree_checkout_cb progress_cb;
3769 struct got_repository *repo;
3772 static const struct got_error *
3773 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3774 const char *relpath, struct got_object_id *blob_id,
3775 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3776 int dirfd, const char *de_name)
3778 struct schedule_addition_args *a = arg;
3779 const struct got_error *err = NULL;
3780 struct got_fileindex_entry *ie;
3784 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3786 return got_error_from_errno("asprintf");
3788 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3790 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3794 /* Re-adding an existing entry is a no-op. */
3795 if (status == GOT_STATUS_ADD)
3797 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3802 if (status != GOT_STATUS_UNVERSIONED) {
3803 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3807 err = got_fileindex_entry_alloc(&ie, relpath);
3810 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3811 relpath, NULL, NULL, 1);
3813 got_fileindex_entry_free(ie);
3816 err = got_fileindex_entry_add(a->fileindex, ie);
3818 got_fileindex_entry_free(ie);
3825 if (status == GOT_STATUS_ADD)
3827 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3830 const struct got_error *
3831 got_worktree_schedule_add(struct got_worktree *worktree,
3832 struct got_pathlist_head *paths,
3833 got_worktree_checkout_cb progress_cb, void *progress_arg,
3834 struct got_repository *repo, int no_ignores)
3836 struct got_fileindex *fileindex = NULL;
3837 char *fileindex_path = NULL;
3838 const struct got_error *err = NULL, *sync_err, *unlockerr;
3839 struct got_pathlist_entry *pe;
3840 struct schedule_addition_args saa;
3842 err = lock_worktree(worktree, LOCK_EX);
3846 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3850 saa.worktree = worktree;
3851 saa.fileindex = fileindex;
3852 saa.progress_cb = progress_cb;
3853 saa.progress_arg = progress_arg;
3856 TAILQ_FOREACH(pe, paths, entry) {
3857 err = worktree_status(worktree, pe->path, fileindex, repo,
3858 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3862 sync_err = sync_fileindex(fileindex, fileindex_path);
3863 if (sync_err && err == NULL)
3866 free(fileindex_path);
3868 got_fileindex_free(fileindex);
3869 unlockerr = lock_worktree(worktree, LOCK_SH);
3870 if (unlockerr && err == NULL)
3875 struct schedule_deletion_args {
3876 struct got_worktree *worktree;
3877 struct got_fileindex *fileindex;
3878 got_worktree_delete_cb progress_cb;
3880 struct got_repository *repo;
3881 int delete_local_mods;
3883 const char *status_codes;
3886 static const struct got_error *
3887 schedule_for_deletion(void *arg, unsigned char status,
3888 unsigned char staged_status, const char *relpath,
3889 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3890 struct got_object_id *commit_id, int dirfd, const char *de_name)
3892 struct schedule_deletion_args *a = arg;
3893 const struct got_error *err = NULL;
3894 struct got_fileindex_entry *ie = NULL;
3898 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3900 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3902 staged_status = get_staged_status(ie);
3903 if (staged_status != GOT_STATUS_NO_CHANGE) {
3904 if (staged_status == GOT_STATUS_DELETE)
3906 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3909 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3911 return got_error_from_errno("asprintf");
3913 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3918 if (a->status_codes) {
3919 size_t ncodes = strlen(a->status_codes);
3921 for (i = 0; i < ncodes ; i++) {
3922 if (status == a->status_codes[i])
3926 /* Do not delete files in non-matching status. */
3930 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3931 a->status_codes[i] != GOT_STATUS_MISSING) {
3932 static char msg[64];
3933 snprintf(msg, sizeof(msg),
3934 "invalid status code '%c'", a->status_codes[i]);
3935 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3940 if (status != GOT_STATUS_NO_CHANGE) {
3941 if (status == GOT_STATUS_DELETE)
3943 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3944 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3947 if (status != GOT_STATUS_MODIFY &&
3948 status != GOT_STATUS_MISSING) {
3949 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3954 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3958 if (unlinkat(dirfd, de_name, 0) != 0) {
3959 err = got_error_from_errno2("unlinkat",
3963 } else if (unlink(ondisk_path) != 0) {
3964 err = got_error_from_errno2("unlink", ondisk_path);
3968 root_len = strlen(a->worktree->root_path);
3971 err = got_path_dirname(&parent, ondisk_path);
3975 ondisk_path = parent;
3976 if (rmdir(ondisk_path) == -1) {
3977 if (errno != ENOTEMPTY)
3978 err = got_error_from_errno2("rmdir",
3982 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3983 strlen(ondisk_path), root_len) != 0);
3986 got_fileindex_entry_mark_deleted_from_disk(ie);
3991 if (status == GOT_STATUS_DELETE)
3993 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3994 staged_status, relpath);
3997 const struct got_error *
3998 got_worktree_schedule_delete(struct got_worktree *worktree,
3999 struct got_pathlist_head *paths, int delete_local_mods,
4000 const char *status_codes,
4001 got_worktree_delete_cb progress_cb, void *progress_arg,
4002 struct got_repository *repo, int keep_on_disk)
4004 struct got_fileindex *fileindex = NULL;
4005 char *fileindex_path = NULL;
4006 const struct got_error *err = NULL, *sync_err, *unlockerr;
4007 struct got_pathlist_entry *pe;
4008 struct schedule_deletion_args sda;
4010 err = lock_worktree(worktree, LOCK_EX);
4014 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4018 sda.worktree = worktree;
4019 sda.fileindex = fileindex;
4020 sda.progress_cb = progress_cb;
4021 sda.progress_arg = progress_arg;
4023 sda.delete_local_mods = delete_local_mods;
4024 sda.keep_on_disk = keep_on_disk;
4025 sda.status_codes = status_codes;
4027 TAILQ_FOREACH(pe, paths, entry) {
4028 err = worktree_status(worktree, pe->path, fileindex, repo,
4029 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4033 sync_err = sync_fileindex(fileindex, fileindex_path);
4034 if (sync_err && err == NULL)
4037 free(fileindex_path);
4039 got_fileindex_free(fileindex);
4040 unlockerr = lock_worktree(worktree, LOCK_SH);
4041 if (unlockerr && err == NULL)
4046 static const struct got_error *
4047 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4049 const struct got_error *err = NULL;
4051 size_t linesize = 0, n;
4054 linelen = getline(&line, &linesize, infile);
4055 if (linelen == -1) {
4056 if (ferror(infile)) {
4057 err = got_error_from_errno("getline");
4063 n = fwrite(line, 1, linelen, outfile);
4065 err = got_ferror(outfile, GOT_ERR_IO);
4070 n = fwrite(line, 1, linelen, rejectfile);
4072 err = got_ferror(outfile, GOT_ERR_IO);
4079 static const struct got_error *
4080 skip_one_line(FILE *f)
4083 size_t linesize = 0;
4086 linelen = getline(&line, &linesize, f);
4087 if (linelen == -1) {
4089 return got_error_from_errno("getline");
4096 static const struct got_error *
4097 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4098 int start_old, int end_old, int start_new, int end_new,
4099 FILE *outfile, FILE *rejectfile)
4101 const struct got_error *err;
4103 /* Copy old file's lines leading up to patch. */
4104 while (!feof(f1) && *line_cur1 < start_old) {
4105 err = copy_one_line(f1, outfile, NULL);
4110 /* Skip new file's lines leading up to patch. */
4111 while (!feof(f2) && *line_cur2 < start_new) {
4113 err = copy_one_line(f2, NULL, rejectfile);
4115 err = skip_one_line(f2);
4120 /* Copy patched lines. */
4121 while (!feof(f2) && *line_cur2 <= end_new) {
4122 err = copy_one_line(f2, outfile, NULL);
4127 /* Skip over old file's replaced lines. */
4128 while (!feof(f1) && *line_cur1 <= end_old) {
4130 err = copy_one_line(f1, NULL, rejectfile);
4132 err = skip_one_line(f1);
4141 static const struct got_error *
4142 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4143 FILE *outfile, FILE *rejectfile)
4145 const struct got_error *err;
4148 /* Copy old file's lines until EOF. */
4150 err = copy_one_line(f1, outfile, NULL);
4157 /* Copy new file's lines until EOF. */
4159 err = copy_one_line(f2, NULL, rejectfile);
4169 static const struct got_error *
4170 apply_or_reject_change(int *choice, int *nchunks_used,
4171 struct diff_result *diff_result, int n,
4172 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4173 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4174 got_worktree_patch_cb patch_cb, void *patch_arg)
4176 const struct got_error *err = NULL;
4177 struct diff_chunk_context cc = {};
4178 int start_old, end_old, start_new, end_new;
4180 struct diff_output_unidiff_state *diff_state;
4181 struct diff_input_info diff_info;
4184 *choice = GOT_PATCH_CHOICE_NONE;
4186 /* Get changed line numbers without context lines for copy_change(). */
4187 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4188 start_old = cc.left.start;
4189 end_old = cc.left.end;
4190 start_new = cc.right.start;
4191 end_new = cc.right.end;
4193 /* Get the same change with context lines for display. */
4194 memset(&cc, 0, sizeof(cc));
4195 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4197 memset(&diff_info, 0, sizeof(diff_info));
4198 diff_info.left_path = relpath;
4199 diff_info.right_path = relpath;
4201 diff_state = diff_output_unidiff_state_alloc();
4202 if (diff_state == NULL)
4203 return got_error_set_errno(ENOMEM,
4204 "diff_output_unidiff_state_alloc");
4206 hunkfile = got_opentemp();
4207 if (hunkfile == NULL) {
4208 err = got_error_from_errno("got_opentemp");
4212 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4214 if (rc != DIFF_RC_OK) {
4215 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4219 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4220 err = got_ferror(hunkfile, GOT_ERR_IO);
4224 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4225 hunkfile, changeno, nchanges);
4230 case GOT_PATCH_CHOICE_YES:
4231 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4232 end_old, start_new, end_new, outfile, rejectfile);
4234 case GOT_PATCH_CHOICE_NO:
4235 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4236 end_old, start_new, end_new, rejectfile, outfile);
4238 case GOT_PATCH_CHOICE_QUIT:
4241 err = got_error(GOT_ERR_PATCH_CHOICE);
4245 diff_output_unidiff_state_free(diff_state);
4246 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4247 err = got_error_from_errno("fclose");
4251 struct revert_file_args {
4252 struct got_worktree *worktree;
4253 struct got_fileindex *fileindex;
4254 got_worktree_checkout_cb progress_cb;
4256 got_worktree_patch_cb patch_cb;
4258 struct got_repository *repo;
4261 static const struct got_error *
4262 create_patched_content(char **path_outfile, int reverse_patch,
4263 struct got_object_id *blob_id, const char *path2,
4264 int dirfd2, const char *de_name2,
4265 const char *relpath, struct got_repository *repo,
4266 got_worktree_patch_cb patch_cb, void *patch_arg)
4268 const struct got_error *err, *free_err;
4269 struct got_blob_object *blob = NULL;
4270 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4272 char link_target[PATH_MAX];
4273 ssize_t link_len = 0;
4274 char *path1 = NULL, *id_str = NULL;
4276 struct got_diffreg_result *diffreg_result = NULL;
4277 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4278 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4280 *path_outfile = NULL;
4282 err = got_object_id_str(&id_str, blob_id);
4287 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4289 if (errno != ELOOP) {
4290 err = got_error_from_errno2("openat", path2);
4293 link_len = readlinkat(dirfd2, de_name2,
4294 link_target, sizeof(link_target));
4296 return got_error_from_errno2("readlinkat", path2);
4297 sb2.st_mode = S_IFLNK;
4298 sb2.st_size = link_len;
4301 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4303 if (errno != ELOOP) {
4304 err = got_error_from_errno2("open", path2);
4307 link_len = readlink(path2, link_target,
4308 sizeof(link_target));
4310 return got_error_from_errno2("readlink", path2);
4311 sb2.st_mode = S_IFLNK;
4312 sb2.st_size = link_len;
4316 if (fstat(fd2, &sb2) == -1) {
4317 err = got_error_from_errno2("fstat", path2);
4321 f2 = fdopen(fd2, "r");
4323 err = got_error_from_errno2("fdopen", path2);
4329 f2 = got_opentemp();
4331 err = got_error_from_errno2("got_opentemp", path2);
4334 n = fwrite(link_target, 1, link_len, f2);
4335 if (n != link_len) {
4336 err = got_ferror(f2, GOT_ERR_IO);
4339 if (fflush(f2) == EOF) {
4340 err = got_error_from_errno("fflush");
4346 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4350 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4354 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4358 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4363 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4367 if (fseek(f1, 0L, SEEK_SET) == -1)
4368 return got_ferror(f1, GOT_ERR_IO);
4369 if (fseek(f2, 0L, SEEK_SET) == -1)
4370 return got_ferror(f2, GOT_ERR_IO);
4372 /* Count the number of actual changes in the diff result. */
4373 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4374 struct diff_chunk_context cc = {};
4375 diff_chunk_context_load_change(&cc, &nchunks_used,
4376 diffreg_result->result, n, 0);
4379 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4381 err = apply_or_reject_change(&choice, &nchunks_used,
4382 diffreg_result->result, n, relpath, f1, f2,
4383 &line_cur1, &line_cur2,
4384 reverse_patch ? NULL : outfile,
4385 reverse_patch ? outfile : NULL,
4386 ++i, nchanges, patch_cb, patch_arg);
4389 if (choice == GOT_PATCH_CHOICE_YES)
4391 else if (choice == GOT_PATCH_CHOICE_QUIT)
4395 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4396 reverse_patch ? NULL : outfile,
4397 reverse_patch ? outfile : NULL);
4401 if (!S_ISLNK(sb2.st_mode)) {
4402 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4403 err = got_error_from_errno2("fchmod", path2);
4411 got_object_blob_close(blob);
4412 free_err = got_diffreg_result_free(diffreg_result);
4415 if (f1 && fclose(f1) == EOF && err == NULL)
4416 err = got_error_from_errno2("fclose", path1);
4417 if (f2 && fclose(f2) == EOF && err == NULL)
4418 err = got_error_from_errno2("fclose", path2);
4419 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4420 err = got_error_from_errno2("close", path2);
4421 if (outfile && fclose(outfile) == EOF && err == NULL)
4422 err = got_error_from_errno2("fclose", *path_outfile);
4423 if (path1 && unlink(path1) == -1 && err == NULL)
4424 err = got_error_from_errno2("unlink", path1);
4425 if (err || !have_content) {
4426 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4427 err = got_error_from_errno2("unlink", *path_outfile);
4428 free(*path_outfile);
4429 *path_outfile = NULL;
4435 static const struct got_error *
4436 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4437 const char *relpath, struct got_object_id *blob_id,
4438 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4439 int dirfd, const char *de_name)
4441 struct revert_file_args *a = arg;
4442 const struct got_error *err = NULL;
4443 char *parent_path = NULL;
4444 struct got_fileindex_entry *ie;
4445 struct got_tree_object *tree = NULL;
4446 struct got_object_id *tree_id = NULL;
4447 const struct got_tree_entry *te = NULL;
4448 char *tree_path = NULL, *te_name;
4449 char *ondisk_path = NULL, *path_content = NULL;
4450 struct got_blob_object *blob = NULL;
4452 /* Reverting a staged deletion is a no-op. */
4453 if (status == GOT_STATUS_DELETE &&
4454 staged_status != GOT_STATUS_NO_CHANGE)
4457 if (status == GOT_STATUS_UNVERSIONED)
4458 return (*a->progress_cb)(a->progress_arg,
4459 GOT_STATUS_UNVERSIONED, relpath);
4461 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4463 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4465 /* Construct in-repository path of tree which contains this blob. */
4466 err = got_path_dirname(&parent_path, ie->path);
4468 if (err->code != GOT_ERR_BAD_PATH)
4470 parent_path = strdup("/");
4471 if (parent_path == NULL) {
4472 err = got_error_from_errno("strdup");
4476 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4477 tree_path = strdup(parent_path);
4478 if (tree_path == NULL) {
4479 err = got_error_from_errno("strdup");
4483 if (got_path_is_root_dir(parent_path)) {
4484 tree_path = strdup(a->worktree->path_prefix);
4485 if (tree_path == NULL) {
4486 err = got_error_from_errno("strdup");
4490 if (asprintf(&tree_path, "%s/%s",
4491 a->worktree->path_prefix, parent_path) == -1) {
4492 err = got_error_from_errno("asprintf");
4498 err = got_object_id_by_path(&tree_id, a->repo,
4499 a->worktree->base_commit_id, tree_path);
4501 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4502 (status == GOT_STATUS_ADD ||
4503 staged_status == GOT_STATUS_ADD)))
4506 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4510 err = got_path_basename(&te_name, ie->path);
4514 te = got_object_tree_find_entry(tree, te_name);
4516 if (te == NULL && status != GOT_STATUS_ADD &&
4517 staged_status != GOT_STATUS_ADD) {
4518 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4524 case GOT_STATUS_ADD:
4526 int choice = GOT_PATCH_CHOICE_NONE;
4527 err = (*a->patch_cb)(&choice, a->patch_arg,
4528 status, ie->path, NULL, 1, 1);
4531 if (choice != GOT_PATCH_CHOICE_YES)
4534 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4538 got_fileindex_entry_remove(a->fileindex, ie);
4540 case GOT_STATUS_DELETE:
4542 int choice = GOT_PATCH_CHOICE_NONE;
4543 err = (*a->patch_cb)(&choice, a->patch_arg,
4544 status, ie->path, NULL, 1, 1);
4547 if (choice != GOT_PATCH_CHOICE_YES)
4551 case GOT_STATUS_MODIFY:
4552 case GOT_STATUS_MODE_CHANGE:
4553 case GOT_STATUS_CONFLICT:
4554 case GOT_STATUS_MISSING: {
4555 struct got_object_id id;
4556 if (staged_status == GOT_STATUS_ADD ||
4557 staged_status == GOT_STATUS_MODIFY) {
4558 memcpy(id.sha1, ie->staged_blob_sha1,
4559 SHA1_DIGEST_LENGTH);
4561 memcpy(id.sha1, ie->blob_sha1,
4562 SHA1_DIGEST_LENGTH);
4563 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4567 if (asprintf(&ondisk_path, "%s/%s",
4568 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4569 err = got_error_from_errno("asprintf");
4573 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4574 status == GOT_STATUS_CONFLICT)) {
4575 int is_bad_symlink = 0;
4576 err = create_patched_content(&path_content, 1, &id,
4577 ondisk_path, dirfd, de_name, ie->path, a->repo,
4578 a->patch_cb, a->patch_arg);
4579 if (err || path_content == NULL)
4581 if (te && S_ISLNK(te->mode)) {
4582 if (unlink(path_content) == -1) {
4583 err = got_error_from_errno2("unlink",
4587 err = install_symlink(&is_bad_symlink,
4588 a->worktree, ondisk_path, ie->path,
4589 blob, 0, 1, 0, a->repo,
4590 a->progress_cb, a->progress_arg);
4592 if (rename(path_content, ondisk_path) == -1) {
4593 err = got_error_from_errno3("rename",
4594 path_content, ondisk_path);
4599 int is_bad_symlink = 0;
4600 if (te && S_ISLNK(te->mode)) {
4601 err = install_symlink(&is_bad_symlink,
4602 a->worktree, ondisk_path, ie->path,
4603 blob, 0, 1, 0, a->repo,
4604 a->progress_cb, a->progress_arg);
4606 err = install_blob(a->worktree, ondisk_path,
4608 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4609 got_fileindex_perms_to_st(ie), blob,
4610 0, 1, 0, 0, a->repo,
4611 a->progress_cb, a->progress_arg);
4615 if (status == GOT_STATUS_DELETE ||
4616 status == GOT_STATUS_MODE_CHANGE) {
4617 err = got_fileindex_entry_update(ie,
4618 a->worktree->root_fd, relpath,
4620 a->worktree->base_commit_id->sha1, 1);
4624 if (is_bad_symlink) {
4625 got_fileindex_entry_filetype_set(ie,
4626 GOT_FILEIDX_MODE_BAD_SYMLINK);
4640 got_object_blob_close(blob);
4642 got_object_tree_close(tree);
4647 const struct got_error *
4648 got_worktree_revert(struct got_worktree *worktree,
4649 struct got_pathlist_head *paths,
4650 got_worktree_checkout_cb progress_cb, void *progress_arg,
4651 got_worktree_patch_cb patch_cb, void *patch_arg,
4652 struct got_repository *repo)
4654 struct got_fileindex *fileindex = NULL;
4655 char *fileindex_path = NULL;
4656 const struct got_error *err = NULL, *unlockerr = NULL;
4657 const struct got_error *sync_err = NULL;
4658 struct got_pathlist_entry *pe;
4659 struct revert_file_args rfa;
4661 err = lock_worktree(worktree, LOCK_EX);
4665 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4669 rfa.worktree = worktree;
4670 rfa.fileindex = fileindex;
4671 rfa.progress_cb = progress_cb;
4672 rfa.progress_arg = progress_arg;
4673 rfa.patch_cb = patch_cb;
4674 rfa.patch_arg = patch_arg;
4676 TAILQ_FOREACH(pe, paths, entry) {
4677 err = worktree_status(worktree, pe->path, fileindex, repo,
4678 revert_file, &rfa, NULL, NULL, 0, 0);
4682 sync_err = sync_fileindex(fileindex, fileindex_path);
4683 if (sync_err && err == NULL)
4686 free(fileindex_path);
4688 got_fileindex_free(fileindex);
4689 unlockerr = lock_worktree(worktree, LOCK_SH);
4690 if (unlockerr && err == NULL)
4696 free_commitable(struct got_commitable *ct)
4699 free(ct->in_repo_path);
4700 free(ct->ondisk_path);
4702 free(ct->base_blob_id);
4703 free(ct->staged_blob_id);
4704 free(ct->base_commit_id);
4708 struct collect_commitables_arg {
4709 struct got_pathlist_head *commitable_paths;
4710 struct got_repository *repo;
4711 struct got_worktree *worktree;
4712 struct got_fileindex *fileindex;
4713 int have_staged_files;
4714 int allow_bad_symlinks;
4717 static const struct got_error *
4718 collect_commitables(void *arg, unsigned char status,
4719 unsigned char staged_status, const char *relpath,
4720 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4721 struct got_object_id *commit_id, int dirfd, const char *de_name)
4723 struct collect_commitables_arg *a = arg;
4724 const struct got_error *err = NULL;
4725 struct got_commitable *ct = NULL;
4726 struct got_pathlist_entry *new = NULL;
4727 char *parent_path = NULL, *path = NULL;
4730 if (a->have_staged_files) {
4731 if (staged_status != GOT_STATUS_MODIFY &&
4732 staged_status != GOT_STATUS_ADD &&
4733 staged_status != GOT_STATUS_DELETE)
4736 if (status == GOT_STATUS_CONFLICT)
4737 return got_error(GOT_ERR_COMMIT_CONFLICT);
4739 if (status != GOT_STATUS_MODIFY &&
4740 status != GOT_STATUS_MODE_CHANGE &&
4741 status != GOT_STATUS_ADD &&
4742 status != GOT_STATUS_DELETE)
4746 if (asprintf(&path, "/%s", relpath) == -1) {
4747 err = got_error_from_errno("asprintf");
4750 if (strcmp(path, "/") == 0) {
4751 parent_path = strdup("");
4752 if (parent_path == NULL)
4753 return got_error_from_errno("strdup");
4755 err = got_path_dirname(&parent_path, path);
4760 ct = calloc(1, sizeof(*ct));
4762 err = got_error_from_errno("calloc");
4766 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4768 err = got_error_from_errno("asprintf");
4772 if (staged_status == GOT_STATUS_ADD ||
4773 staged_status == GOT_STATUS_MODIFY) {
4774 struct got_fileindex_entry *ie;
4775 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4776 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4777 case GOT_FILEIDX_MODE_REGULAR_FILE:
4778 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4781 case GOT_FILEIDX_MODE_SYMLINK:
4785 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4788 ct->mode |= got_fileindex_entry_perms_get(ie);
4789 } else if (status != GOT_STATUS_DELETE &&
4790 staged_status != GOT_STATUS_DELETE) {
4792 if (fstatat(dirfd, de_name, &sb,
4793 AT_SYMLINK_NOFOLLOW) == -1) {
4794 err = got_error_from_errno2("fstatat",
4798 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4799 err = got_error_from_errno2("lstat", ct->ondisk_path);
4802 ct->mode = sb.st_mode;
4805 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4806 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4808 err = got_error_from_errno("asprintf");
4812 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4813 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4815 char target_path[PATH_MAX];
4817 target_len = readlink(ct->ondisk_path, target_path,
4818 sizeof(target_path));
4819 if (target_len == -1) {
4820 err = got_error_from_errno2("readlink",
4824 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4825 target_len, ct->ondisk_path, a->worktree->root_path);
4828 if (is_bad_symlink) {
4829 err = got_error_path(ct->ondisk_path,
4830 GOT_ERR_BAD_SYMLINK);
4836 ct->status = status;
4837 ct->staged_status = staged_status;
4838 ct->blob_id = NULL; /* will be filled in when blob gets created */
4839 if (ct->status != GOT_STATUS_ADD &&
4840 ct->staged_status != GOT_STATUS_ADD) {
4841 ct->base_blob_id = got_object_id_dup(blob_id);
4842 if (ct->base_blob_id == NULL) {
4843 err = got_error_from_errno("got_object_id_dup");
4846 ct->base_commit_id = got_object_id_dup(commit_id);
4847 if (ct->base_commit_id == NULL) {
4848 err = got_error_from_errno("got_object_id_dup");
4852 if (ct->staged_status == GOT_STATUS_ADD ||
4853 ct->staged_status == GOT_STATUS_MODIFY) {
4854 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4855 if (ct->staged_blob_id == NULL) {
4856 err = got_error_from_errno("got_object_id_dup");
4860 ct->path = strdup(path);
4861 if (ct->path == NULL) {
4862 err = got_error_from_errno("strdup");
4865 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4867 if (ct && (err || new == NULL))
4868 free_commitable(ct);
4874 static const struct got_error *write_tree(struct got_object_id **, int *,
4875 struct got_tree_object *, const char *, struct got_pathlist_head *,
4876 got_worktree_status_cb status_cb, void *status_arg,
4877 struct got_repository *);
4879 static const struct got_error *
4880 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4881 struct got_tree_entry *te, const char *parent_path,
4882 struct got_pathlist_head *commitable_paths,
4883 got_worktree_status_cb status_cb, void *status_arg,
4884 struct got_repository *repo)
4886 const struct got_error *err = NULL;
4887 struct got_tree_object *subtree;
4890 if (asprintf(&subpath, "%s%s%s", parent_path,
4891 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4892 return got_error_from_errno("asprintf");
4894 err = got_object_open_as_tree(&subtree, repo, &te->id);
4898 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4899 commitable_paths, status_cb, status_arg, repo);
4900 got_object_tree_close(subtree);
4905 static const struct got_error *
4906 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4908 const struct got_error *err = NULL;
4909 char *ct_parent_path = NULL;
4913 if (strchr(ct->in_repo_path, '/') == NULL) {
4914 *match = got_path_is_root_dir(path);
4918 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4921 *match = (strcmp(path, ct_parent_path) == 0);
4922 free(ct_parent_path);
4927 get_ct_file_mode(struct got_commitable *ct)
4929 if (S_ISLNK(ct->mode))
4932 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4935 static const struct got_error *
4936 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4937 struct got_tree_entry *te, struct got_commitable *ct)
4939 const struct got_error *err = NULL;
4943 err = got_object_tree_entry_dup(new_te, te);
4947 (*new_te)->mode = get_ct_file_mode(ct);
4949 if (ct->staged_status == GOT_STATUS_MODIFY)
4950 memcpy(&(*new_te)->id, ct->staged_blob_id,
4951 sizeof((*new_te)->id));
4953 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4955 if (err && *new_te) {
4962 static const struct got_error *
4963 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4964 struct got_commitable *ct)
4966 const struct got_error *err = NULL;
4967 char *ct_name = NULL;
4971 *new_te = calloc(1, sizeof(**new_te));
4972 if (*new_te == NULL)
4973 return got_error_from_errno("calloc");
4975 err = got_path_basename(&ct_name, ct->path);
4978 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4979 sizeof((*new_te)->name)) {
4980 err = got_error(GOT_ERR_NO_SPACE);
4984 (*new_te)->mode = get_ct_file_mode(ct);
4986 if (ct->staged_status == GOT_STATUS_ADD)
4987 memcpy(&(*new_te)->id, ct->staged_blob_id,
4988 sizeof((*new_te)->id));
4990 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4993 if (err && *new_te) {
5000 static const struct got_error *
5001 insert_tree_entry(struct got_tree_entry *new_te,
5002 struct got_pathlist_head *paths)
5004 const struct got_error *err = NULL;
5005 struct got_pathlist_entry *new_pe;
5007 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5011 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5015 static const struct got_error *
5016 report_ct_status(struct got_commitable *ct,
5017 got_worktree_status_cb status_cb, void *status_arg)
5019 const char *ct_path = ct->path;
5020 unsigned char status;
5022 while (ct_path[0] == '/')
5025 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5026 status = ct->staged_status;
5028 status = ct->status;
5030 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5031 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5034 static const struct got_error *
5035 match_modified_subtree(int *modified, struct got_tree_entry *te,
5036 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5038 const struct got_error *err = NULL;
5039 struct got_pathlist_entry *pe;
5044 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5045 got_path_is_root_dir(base_tree_path) ? "" : "/",
5047 return got_error_from_errno("asprintf");
5049 TAILQ_FOREACH(pe, commitable_paths, entry) {
5050 struct got_commitable *ct = pe->data;
5051 *modified = got_path_is_child(ct->in_repo_path, te_path,
5061 static const struct got_error *
5062 match_deleted_or_modified_ct(struct got_commitable **ctp,
5063 struct got_tree_entry *te, const char *base_tree_path,
5064 struct got_pathlist_head *commitable_paths)
5066 const struct got_error *err = NULL;
5067 struct got_pathlist_entry *pe;
5071 TAILQ_FOREACH(pe, commitable_paths, entry) {
5072 struct got_commitable *ct = pe->data;
5073 char *ct_name = NULL;
5076 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5077 if (ct->status != GOT_STATUS_MODIFY &&
5078 ct->status != GOT_STATUS_MODE_CHANGE &&
5079 ct->status != GOT_STATUS_DELETE)
5082 if (ct->staged_status != GOT_STATUS_MODIFY &&
5083 ct->staged_status != GOT_STATUS_DELETE)
5087 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5090 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5096 err = got_path_basename(&ct_name, pe->path);
5100 if (strcmp(te->name, ct_name) != 0) {
5113 static const struct got_error *
5114 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5115 const char *child_path, const char *path_base_tree,
5116 struct got_pathlist_head *commitable_paths,
5117 got_worktree_status_cb status_cb, void *status_arg,
5118 struct got_repository *repo)
5120 const struct got_error *err = NULL;
5121 struct got_tree_entry *new_te;
5123 struct got_object_id *id = NULL;
5128 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5129 got_path_is_root_dir(path_base_tree) ? "" : "/",
5131 return got_error_from_errno("asprintf");
5133 new_te = calloc(1, sizeof(*new_te));
5135 return got_error_from_errno("calloc");
5136 new_te->mode = S_IFDIR;
5138 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5139 sizeof(new_te->name)) {
5140 err = got_error(GOT_ERR_NO_SPACE);
5143 err = write_tree(&id, &nentries, NULL, subtree_path,
5144 commitable_paths, status_cb, status_arg, repo);
5149 memcpy(&new_te->id, id, sizeof(new_te->id));
5158 static const struct got_error *
5159 write_tree(struct got_object_id **new_tree_id, int *nentries,
5160 struct got_tree_object *base_tree, const char *path_base_tree,
5161 struct got_pathlist_head *commitable_paths,
5162 got_worktree_status_cb status_cb, void *status_arg,
5163 struct got_repository *repo)
5165 const struct got_error *err = NULL;
5166 struct got_pathlist_head paths;
5167 struct got_tree_entry *te, *new_te = NULL;
5168 struct got_pathlist_entry *pe;
5173 /* Insert, and recurse into, newly added entries first. */
5174 TAILQ_FOREACH(pe, commitable_paths, entry) {
5175 struct got_commitable *ct = pe->data;
5176 char *child_path = NULL, *slash;
5178 if ((ct->status != GOT_STATUS_ADD &&
5179 ct->staged_status != GOT_STATUS_ADD) ||
5180 (ct->flags & GOT_COMMITABLE_ADDED))
5183 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5184 strlen(path_base_tree)))
5187 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5192 slash = strchr(child_path, '/');
5193 if (slash == NULL) {
5194 err = alloc_added_blob_tree_entry(&new_te, ct);
5197 err = report_ct_status(ct, status_cb, status_arg);
5200 ct->flags |= GOT_COMMITABLE_ADDED;
5201 err = insert_tree_entry(new_te, &paths);
5206 *slash = '\0'; /* trim trailing path components */
5207 if (base_tree == NULL ||
5208 got_object_tree_find_entry(base_tree, child_path)
5210 err = make_subtree_for_added_blob(&new_te,
5211 child_path, path_base_tree,
5212 commitable_paths, status_cb, status_arg,
5216 err = insert_tree_entry(new_te, &paths);
5225 int i, nbase_entries;
5226 /* Handle modified and deleted entries. */
5227 nbase_entries = got_object_tree_get_nentries(base_tree);
5228 for (i = 0; i < nbase_entries; i++) {
5229 struct got_commitable *ct = NULL;
5231 te = got_object_tree_get_entry(base_tree, i);
5232 if (got_object_tree_entry_is_submodule(te)) {
5233 /* Entry is a submodule; just copy it. */
5234 err = got_object_tree_entry_dup(&new_te, te);
5237 err = insert_tree_entry(new_te, &paths);
5244 if (S_ISDIR(te->mode)) {
5246 err = got_object_tree_entry_dup(&new_te, te);
5249 err = match_modified_subtree(&modified, te,
5250 path_base_tree, commitable_paths);
5253 /* Avoid recursion into unmodified subtrees. */
5255 struct got_object_id *new_id;
5257 err = write_subtree(&new_id,
5259 path_base_tree, commitable_paths,
5260 status_cb, status_arg, repo);
5263 if (nsubentries == 0) {
5264 /* All entries were deleted. */
5268 memcpy(&new_te->id, new_id,
5269 sizeof(new_te->id));
5272 err = insert_tree_entry(new_te, &paths);
5279 err = match_deleted_or_modified_ct(&ct, te,
5280 path_base_tree, commitable_paths);
5284 /* NB: Deleted entries get dropped here. */
5285 if (ct->status == GOT_STATUS_MODIFY ||
5286 ct->status == GOT_STATUS_MODE_CHANGE ||
5287 ct->staged_status == GOT_STATUS_MODIFY) {
5288 err = alloc_modified_blob_tree_entry(
5292 err = insert_tree_entry(new_te, &paths);
5297 err = report_ct_status(ct, status_cb,
5302 /* Entry is unchanged; just copy it. */
5303 err = got_object_tree_entry_dup(&new_te, te);
5306 err = insert_tree_entry(new_te, &paths);
5314 /* Write new list of entries; deleted entries have been dropped. */
5315 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5317 got_pathlist_free(&paths);
5321 static const struct got_error *
5322 update_fileindex_after_commit(struct got_worktree *worktree,
5323 struct got_pathlist_head *commitable_paths,
5324 struct got_object_id *new_base_commit_id,
5325 struct got_fileindex *fileindex, int have_staged_files)
5327 const struct got_error *err = NULL;
5328 struct got_pathlist_entry *pe;
5329 char *relpath = NULL;
5331 TAILQ_FOREACH(pe, commitable_paths, entry) {
5332 struct got_fileindex_entry *ie;
5333 struct got_commitable *ct = pe->data;
5335 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5337 err = got_path_skip_common_ancestor(&relpath,
5338 worktree->root_path, ct->ondisk_path);
5343 if (ct->status == GOT_STATUS_DELETE ||
5344 ct->staged_status == GOT_STATUS_DELETE) {
5345 got_fileindex_entry_remove(fileindex, ie);
5346 } else if (ct->staged_status == GOT_STATUS_ADD ||
5347 ct->staged_status == GOT_STATUS_MODIFY) {
5348 got_fileindex_entry_stage_set(ie,
5349 GOT_FILEIDX_STAGE_NONE);
5350 got_fileindex_entry_staged_filetype_set(ie, 0);
5352 err = got_fileindex_entry_update(ie,
5353 worktree->root_fd, relpath,
5354 ct->staged_blob_id->sha1,
5355 new_base_commit_id->sha1,
5356 !have_staged_files);
5358 err = got_fileindex_entry_update(ie,
5359 worktree->root_fd, relpath,
5361 new_base_commit_id->sha1,
5362 !have_staged_files);
5364 err = got_fileindex_entry_alloc(&ie, pe->path);
5367 err = got_fileindex_entry_update(ie,
5368 worktree->root_fd, relpath, ct->blob_id->sha1,
5369 new_base_commit_id->sha1, 1);
5371 got_fileindex_entry_free(ie);
5374 err = got_fileindex_entry_add(fileindex, ie);
5376 got_fileindex_entry_free(ie);
5389 static const struct got_error *
5390 check_out_of_date(const char *in_repo_path, unsigned char status,
5391 unsigned char staged_status, struct got_object_id *base_blob_id,
5392 struct got_object_id *base_commit_id,
5393 struct got_object_id *head_commit_id, struct got_repository *repo,
5396 const struct got_error *err = NULL;
5397 struct got_object_id *id = NULL;
5399 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5400 /* Trivial case: base commit == head commit */
5401 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5404 * Ensure file content which local changes were based
5405 * on matches file content in the branch head.
5407 err = got_object_id_by_path(&id, repo, head_commit_id,
5410 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5411 err = got_error(ood_errcode);
5413 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5414 err = got_error(ood_errcode);
5416 /* Require that added files don't exist in the branch head. */
5417 err = got_object_id_by_path(&id, repo, head_commit_id,
5419 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5421 err = id ? got_error(ood_errcode) : NULL;
5428 const struct got_error *
5429 commit_worktree(struct got_object_id **new_commit_id,
5430 struct got_pathlist_head *commitable_paths,
5431 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5432 const char *author, const char *committer,
5433 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5434 got_worktree_status_cb status_cb, void *status_arg,
5435 struct got_repository *repo)
5437 const struct got_error *err = NULL, *unlockerr = NULL;
5438 struct got_pathlist_entry *pe;
5439 const char *head_ref_name = NULL;
5440 struct got_commit_object *head_commit = NULL;
5441 struct got_reference *head_ref2 = NULL;
5442 struct got_object_id *head_commit_id2 = NULL;
5443 struct got_tree_object *head_tree = NULL;
5444 struct got_object_id *new_tree_id = NULL;
5446 struct got_object_id_queue parent_ids;
5447 struct got_object_qid *pid = NULL;
5448 char *logmsg = NULL;
5450 *new_commit_id = NULL;
5452 SIMPLEQ_INIT(&parent_ids);
5454 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5458 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5462 if (commit_msg_cb != NULL) {
5463 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5468 if (logmsg == NULL || strlen(logmsg) == 0) {
5469 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5473 /* Create blobs from added and modified files and record their IDs. */
5474 TAILQ_FOREACH(pe, commitable_paths, entry) {
5475 struct got_commitable *ct = pe->data;
5478 /* Blobs for staged files already exist. */
5479 if (ct->staged_status == GOT_STATUS_ADD ||
5480 ct->staged_status == GOT_STATUS_MODIFY)
5483 if (ct->status != GOT_STATUS_ADD &&
5484 ct->status != GOT_STATUS_MODIFY &&
5485 ct->status != GOT_STATUS_MODE_CHANGE)
5488 if (asprintf(&ondisk_path, "%s/%s",
5489 worktree->root_path, pe->path) == -1) {
5490 err = got_error_from_errno("asprintf");
5493 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5499 /* Recursively write new tree objects. */
5500 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5501 commitable_paths, status_cb, status_arg, repo);
5505 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5508 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5509 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5510 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5511 got_object_qid_free(pid);
5517 /* Check if a concurrent commit to our branch has occurred. */
5518 head_ref_name = got_worktree_get_head_ref_name(worktree);
5519 if (head_ref_name == NULL) {
5520 err = got_error_from_errno("got_worktree_get_head_ref_name");
5523 /* Lock the reference here to prevent concurrent modification. */
5524 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5527 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5530 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5531 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5534 /* Update branch head in repository. */
5535 err = got_ref_change_ref(head_ref2, *new_commit_id);
5538 err = got_ref_write(head_ref2, repo);
5542 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5546 err = ref_base_commit(worktree, repo);
5551 got_object_tree_close(head_tree);
5553 got_object_commit_close(head_commit);
5554 free(head_commit_id2);
5556 unlockerr = got_ref_unlock(head_ref2);
5557 if (unlockerr && err == NULL)
5559 got_ref_close(head_ref2);
5564 static const struct got_error *
5565 check_path_is_commitable(const char *path,
5566 struct got_pathlist_head *commitable_paths)
5568 struct got_pathlist_entry *cpe = NULL;
5569 size_t path_len = strlen(path);
5571 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5572 struct got_commitable *ct = cpe->data;
5573 const char *ct_path = ct->path;
5575 while (ct_path[0] == '/')
5578 if (strcmp(path, ct_path) == 0 ||
5579 got_path_is_child(ct_path, path, path_len))
5584 return got_error_path(path, GOT_ERR_BAD_PATH);
5589 static const struct got_error *
5590 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5592 int *have_staged_files = arg;
5594 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5595 *have_staged_files = 1;
5596 return got_error(GOT_ERR_CANCELLED);
5602 static const struct got_error *
5603 check_non_staged_files(struct got_fileindex *fileindex,
5604 struct got_pathlist_head *paths)
5606 struct got_pathlist_entry *pe;
5607 struct got_fileindex_entry *ie;
5609 TAILQ_FOREACH(pe, paths, entry) {
5610 if (pe->path[0] == '\0')
5612 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5614 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5615 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5616 return got_error_path(pe->path,
5617 GOT_ERR_FILE_NOT_STAGED);
5623 const struct got_error *
5624 got_worktree_commit(struct got_object_id **new_commit_id,
5625 struct got_worktree *worktree, struct got_pathlist_head *paths,
5626 const char *author, const char *committer, int allow_bad_symlinks,
5627 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5628 got_worktree_status_cb status_cb, void *status_arg,
5629 struct got_repository *repo)
5631 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5632 struct got_fileindex *fileindex = NULL;
5633 char *fileindex_path = NULL;
5634 struct got_pathlist_head commitable_paths;
5635 struct collect_commitables_arg cc_arg;
5636 struct got_pathlist_entry *pe;
5637 struct got_reference *head_ref = NULL;
5638 struct got_object_id *head_commit_id = NULL;
5639 int have_staged_files = 0;
5641 *new_commit_id = NULL;
5643 TAILQ_INIT(&commitable_paths);
5645 err = lock_worktree(worktree, LOCK_EX);
5649 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5653 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5657 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5661 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5662 &have_staged_files);
5663 if (err && err->code != GOT_ERR_CANCELLED)
5665 if (have_staged_files) {
5666 err = check_non_staged_files(fileindex, paths);
5671 cc_arg.commitable_paths = &commitable_paths;
5672 cc_arg.worktree = worktree;
5673 cc_arg.fileindex = fileindex;
5675 cc_arg.have_staged_files = have_staged_files;
5676 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5677 TAILQ_FOREACH(pe, paths, entry) {
5678 err = worktree_status(worktree, pe->path, fileindex, repo,
5679 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5684 if (TAILQ_EMPTY(&commitable_paths)) {
5685 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5689 TAILQ_FOREACH(pe, paths, entry) {
5690 err = check_path_is_commitable(pe->path, &commitable_paths);
5695 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5696 struct got_commitable *ct = pe->data;
5697 const char *ct_path = ct->in_repo_path;
5699 while (ct_path[0] == '/')
5701 err = check_out_of_date(ct_path, ct->status,
5702 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5703 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5709 err = commit_worktree(new_commit_id, &commitable_paths,
5710 head_commit_id, worktree, author, committer,
5711 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5715 err = update_fileindex_after_commit(worktree, &commitable_paths,
5716 *new_commit_id, fileindex, have_staged_files);
5717 sync_err = sync_fileindex(fileindex, fileindex_path);
5718 if (sync_err && err == NULL)
5722 got_fileindex_free(fileindex);
5723 free(fileindex_path);
5724 unlockerr = lock_worktree(worktree, LOCK_SH);
5725 if (unlockerr && err == NULL)
5727 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5728 struct got_commitable *ct = pe->data;
5729 free_commitable(ct);
5731 got_pathlist_free(&commitable_paths);
5736 got_commitable_get_path(struct got_commitable *ct)
5742 got_commitable_get_status(struct got_commitable *ct)
5747 struct check_rebase_ok_arg {
5748 struct got_worktree *worktree;
5749 struct got_repository *repo;
5752 static const struct got_error *
5753 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5755 const struct got_error *err = NULL;
5756 struct check_rebase_ok_arg *a = arg;
5757 unsigned char status;
5761 /* Reject rebase of a work tree with mixed base commits. */
5762 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5763 SHA1_DIGEST_LENGTH))
5764 return got_error(GOT_ERR_MIXED_COMMITS);
5766 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5768 return got_error_from_errno("asprintf");
5770 /* Reject rebase of a work tree with modified or staged files. */
5771 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5776 if (status != GOT_STATUS_NO_CHANGE)
5777 return got_error(GOT_ERR_MODIFIED);
5778 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5779 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5784 const struct got_error *
5785 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5786 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5787 struct got_worktree *worktree, struct got_reference *branch,
5788 struct got_repository *repo)
5790 const struct got_error *err = NULL;
5791 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5792 char *branch_ref_name = NULL;
5793 char *fileindex_path = NULL;
5794 struct check_rebase_ok_arg ok_arg;
5795 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5796 struct got_object_id *wt_branch_tip = NULL;
5798 *new_base_branch_ref = NULL;
5802 err = lock_worktree(worktree, LOCK_EX);
5806 err = open_fileindex(fileindex, &fileindex_path, worktree);
5810 ok_arg.worktree = worktree;
5812 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5817 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5821 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5825 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5829 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5834 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5837 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5838 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5842 err = got_ref_alloc_symref(new_base_branch_ref,
5843 new_base_branch_ref_name, wt_branch);
5846 err = got_ref_write(*new_base_branch_ref, repo);
5850 /* TODO Lock original branch's ref while rebasing? */
5852 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5856 err = got_ref_write(branch_ref, repo);
5860 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5861 worktree->base_commit_id);
5864 err = got_ref_write(*tmp_branch, repo);
5868 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5872 free(fileindex_path);
5873 free(tmp_branch_name);
5874 free(new_base_branch_ref_name);
5875 free(branch_ref_name);
5877 got_ref_close(branch_ref);
5879 got_ref_close(wt_branch);
5880 free(wt_branch_tip);
5882 if (*new_base_branch_ref) {
5883 got_ref_close(*new_base_branch_ref);
5884 *new_base_branch_ref = NULL;
5887 got_ref_close(*tmp_branch);
5891 got_fileindex_free(*fileindex);
5894 lock_worktree(worktree, LOCK_SH);
5899 const struct got_error *
5900 got_worktree_rebase_continue(struct got_object_id **commit_id,
5901 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5902 struct got_reference **branch, struct got_fileindex **fileindex,
5903 struct got_worktree *worktree, struct got_repository *repo)
5905 const struct got_error *err;
5906 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5907 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5908 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5909 char *fileindex_path = NULL;
5910 int have_staged_files = 0;
5913 *new_base_branch = NULL;
5918 err = lock_worktree(worktree, LOCK_EX);
5922 err = open_fileindex(fileindex, &fileindex_path, worktree);
5926 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5927 &have_staged_files);
5928 if (err && err->code != GOT_ERR_CANCELLED)
5930 if (have_staged_files) {
5931 err = got_error(GOT_ERR_STAGED_PATHS);
5935 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5939 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5943 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5947 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5951 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5955 err = got_ref_open(branch, repo,
5956 got_ref_get_symref_target(branch_ref), 0);
5960 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5964 err = got_ref_resolve(commit_id, repo, commit_ref);
5968 err = got_ref_open(new_base_branch, repo,
5969 new_base_branch_ref_name, 0);
5973 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5977 free(commit_ref_name);
5978 free(branch_ref_name);
5979 free(fileindex_path);
5981 got_ref_close(commit_ref);
5983 got_ref_close(branch_ref);
5988 got_ref_close(*tmp_branch);
5991 if (*new_base_branch) {
5992 got_ref_close(*new_base_branch);
5993 *new_base_branch = NULL;
5996 got_ref_close(*branch);
6000 got_fileindex_free(*fileindex);
6003 lock_worktree(worktree, LOCK_SH);
6008 const struct got_error *
6009 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6011 const struct got_error *err;
6012 char *tmp_branch_name = NULL;
6014 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6018 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6019 free(tmp_branch_name);
6023 static const struct got_error *
6024 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6025 char **logmsg, void *arg)
6031 static const struct got_error *
6032 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6033 const char *path, struct got_object_id *blob_id,
6034 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6035 int dirfd, const char *de_name)
6040 struct collect_merged_paths_arg {
6041 got_worktree_checkout_cb progress_cb;
6043 struct got_pathlist_head *merged_paths;
6046 static const struct got_error *
6047 collect_merged_paths(void *arg, unsigned char status, const char *path)
6049 const struct got_error *err;
6050 struct collect_merged_paths_arg *a = arg;
6052 struct got_pathlist_entry *new;
6054 err = (*a->progress_cb)(a->progress_arg, status, path);
6058 if (status != GOT_STATUS_MERGE &&
6059 status != GOT_STATUS_ADD &&
6060 status != GOT_STATUS_DELETE &&
6061 status != GOT_STATUS_CONFLICT)
6066 return got_error_from_errno("strdup");
6068 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6069 if (err || new == NULL)
6075 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6077 struct got_pathlist_entry *pe;
6079 TAILQ_FOREACH(pe, merged_paths, entry)
6080 free((char *)pe->path);
6082 got_pathlist_free(merged_paths);
6085 static const struct got_error *
6086 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6087 int is_rebase, struct got_repository *repo)
6089 const struct got_error *err;
6090 struct got_reference *commit_ref = NULL;
6092 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6094 if (err->code != GOT_ERR_NOT_REF)
6096 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6099 err = got_ref_write(commit_ref, repo);
6102 } else if (is_rebase) {
6103 struct got_object_id *stored_id;
6106 err = got_ref_resolve(&stored_id, repo, commit_ref);
6109 cmp = got_object_id_cmp(commit_id, stored_id);
6112 err = got_error(GOT_ERR_REBASE_COMMITID);
6118 got_ref_close(commit_ref);
6122 static const struct got_error *
6123 rebase_merge_files(struct got_pathlist_head *merged_paths,
6124 const char *commit_ref_name, struct got_worktree *worktree,
6125 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6126 struct got_object_id *commit_id, struct got_repository *repo,
6127 got_worktree_checkout_cb progress_cb, void *progress_arg,
6128 got_cancel_cb cancel_cb, void *cancel_arg)
6130 const struct got_error *err;
6131 struct got_reference *commit_ref = NULL;
6132 struct collect_merged_paths_arg cmp_arg;
6133 char *fileindex_path;
6135 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6137 err = get_fileindex_path(&fileindex_path, worktree);
6141 cmp_arg.progress_cb = progress_cb;
6142 cmp_arg.progress_arg = progress_arg;
6143 cmp_arg.merged_paths = merged_paths;
6144 err = merge_files(worktree, fileindex, fileindex_path,
6145 parent_commit_id, commit_id, repo, collect_merged_paths,
6146 &cmp_arg, cancel_cb, cancel_arg);
6148 got_ref_close(commit_ref);
6152 const struct got_error *
6153 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6154 struct got_worktree *worktree, struct got_fileindex *fileindex,
6155 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6156 struct got_repository *repo,
6157 got_worktree_checkout_cb progress_cb, void *progress_arg,
6158 got_cancel_cb cancel_cb, void *cancel_arg)
6160 const struct got_error *err;
6161 char *commit_ref_name;
6163 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6167 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6171 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6172 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6173 progress_arg, cancel_cb, cancel_arg);
6175 free(commit_ref_name);
6179 const struct got_error *
6180 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6181 struct got_worktree *worktree, struct got_fileindex *fileindex,
6182 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6183 struct got_repository *repo,
6184 got_worktree_checkout_cb progress_cb, void *progress_arg,
6185 got_cancel_cb cancel_cb, void *cancel_arg)
6187 const struct got_error *err;
6188 char *commit_ref_name;
6190 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6194 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6198 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6199 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6200 progress_arg, cancel_cb, cancel_arg);
6202 free(commit_ref_name);
6206 static const struct got_error *
6207 rebase_commit(struct got_object_id **new_commit_id,
6208 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6209 struct got_worktree *worktree, struct got_fileindex *fileindex,
6210 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6211 const char *new_logmsg, struct got_repository *repo)
6213 const struct got_error *err, *sync_err;
6214 struct got_pathlist_head commitable_paths;
6215 struct collect_commitables_arg cc_arg;
6216 char *fileindex_path = NULL;
6217 struct got_reference *head_ref = NULL;
6218 struct got_object_id *head_commit_id = NULL;
6219 char *logmsg = NULL;
6221 TAILQ_INIT(&commitable_paths);
6222 *new_commit_id = NULL;
6224 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6226 err = get_fileindex_path(&fileindex_path, worktree);
6230 cc_arg.commitable_paths = &commitable_paths;
6231 cc_arg.worktree = worktree;
6233 cc_arg.have_staged_files = 0;
6235 * If possible get the status of individual files directly to
6236 * avoid crawling the entire work tree once per rebased commit.
6237 * TODO: Ideally, merged_paths would contain a list of commitables
6238 * we could use so we could skip worktree_status() entirely.
6241 struct got_pathlist_entry *pe;
6242 TAILQ_FOREACH(pe, merged_paths, entry) {
6243 err = worktree_status(worktree, pe->path, fileindex,
6244 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6250 err = worktree_status(worktree, "", fileindex, repo,
6251 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6256 if (TAILQ_EMPTY(&commitable_paths)) {
6257 /* No-op change; commit will be elided. */
6258 err = got_ref_delete(commit_ref, repo);
6261 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6265 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6269 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6274 logmsg = strdup(new_logmsg);
6275 if (logmsg == NULL) {
6276 err = got_error_from_errno("strdup");
6280 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6285 /* NB: commit_worktree will call free(logmsg) */
6286 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6287 worktree, got_object_commit_get_author(orig_commit),
6288 got_object_commit_get_committer(orig_commit),
6289 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6293 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6297 err = got_ref_delete(commit_ref, repo);
6301 err = update_fileindex_after_commit(worktree, &commitable_paths,
6302 *new_commit_id, fileindex, 0);
6303 sync_err = sync_fileindex(fileindex, fileindex_path);
6304 if (sync_err && err == NULL)
6307 free(fileindex_path);
6308 free(head_commit_id);
6310 got_ref_close(head_ref);
6312 free(*new_commit_id);
6313 *new_commit_id = NULL;
6318 const struct got_error *
6319 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6320 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6321 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6322 struct got_commit_object *orig_commit,
6323 struct got_object_id *orig_commit_id, struct got_repository *repo)
6325 const struct got_error *err;
6326 char *commit_ref_name;
6327 struct got_reference *commit_ref = NULL;
6328 struct got_object_id *commit_id = NULL;
6330 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6334 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6337 err = got_ref_resolve(&commit_id, repo, commit_ref);
6340 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6341 err = got_error(GOT_ERR_REBASE_COMMITID);
6345 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6346 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6349 got_ref_close(commit_ref);
6350 free(commit_ref_name);
6355 const struct got_error *
6356 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6357 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6358 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6359 struct got_commit_object *orig_commit,
6360 struct got_object_id *orig_commit_id, const char *new_logmsg,
6361 struct got_repository *repo)
6363 const struct got_error *err;
6364 char *commit_ref_name;
6365 struct got_reference *commit_ref = NULL;
6367 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6371 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6375 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6376 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6379 got_ref_close(commit_ref);
6380 free(commit_ref_name);
6384 const struct got_error *
6385 got_worktree_rebase_postpone(struct got_worktree *worktree,
6386 struct got_fileindex *fileindex)
6389 got_fileindex_free(fileindex);
6390 return lock_worktree(worktree, LOCK_SH);
6393 static const struct got_error *
6394 delete_ref(const char *name, struct got_repository *repo)
6396 const struct got_error *err;
6397 struct got_reference *ref;
6399 err = got_ref_open(&ref, repo, name, 0);
6401 if (err->code == GOT_ERR_NOT_REF)
6406 err = got_ref_delete(ref, repo);
6411 static const struct got_error *
6412 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6414 const struct got_error *err;
6415 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6416 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6418 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6421 err = delete_ref(tmp_branch_name, repo);
6425 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6428 err = delete_ref(new_base_branch_ref_name, repo);
6432 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6435 err = delete_ref(branch_ref_name, repo);
6439 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6442 err = delete_ref(commit_ref_name, repo);
6447 free(tmp_branch_name);
6448 free(new_base_branch_ref_name);
6449 free(branch_ref_name);
6450 free(commit_ref_name);
6454 const struct got_error *
6455 got_worktree_rebase_complete(struct got_worktree *worktree,
6456 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6457 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6458 struct got_repository *repo)
6460 const struct got_error *err, *unlockerr, *sync_err;
6461 struct got_object_id *new_head_commit_id = NULL;
6462 char *fileindex_path = NULL;
6464 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6468 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6472 err = got_ref_write(rebased_branch, repo);
6476 err = got_worktree_set_head_ref(worktree, rebased_branch);
6480 err = delete_rebase_refs(worktree, repo);
6484 err = get_fileindex_path(&fileindex_path, worktree);
6487 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6488 sync_err = sync_fileindex(fileindex, fileindex_path);
6489 if (sync_err && err == NULL)
6492 got_fileindex_free(fileindex);
6493 free(fileindex_path);
6494 free(new_head_commit_id);
6495 unlockerr = lock_worktree(worktree, LOCK_SH);
6496 if (unlockerr && err == NULL)
6501 const struct got_error *
6502 got_worktree_rebase_abort(struct got_worktree *worktree,
6503 struct got_fileindex *fileindex, struct got_repository *repo,
6504 struct got_reference *new_base_branch,
6505 got_worktree_checkout_cb progress_cb, void *progress_arg)
6507 const struct got_error *err, *unlockerr, *sync_err;
6508 struct got_reference *resolved = NULL;
6509 struct got_object_id *commit_id = NULL;
6510 char *fileindex_path = NULL;
6511 struct revert_file_args rfa;
6512 struct got_object_id *tree_id = NULL;
6514 err = lock_worktree(worktree, LOCK_EX);
6518 err = got_ref_open(&resolved, repo,
6519 got_ref_get_symref_target(new_base_branch), 0);
6523 err = got_worktree_set_head_ref(worktree, resolved);
6528 * XXX commits to the base branch could have happened while
6529 * we were busy rebasing; should we store the original commit ID
6530 * when rebase begins and read it back here?
6532 err = got_ref_resolve(&commit_id, repo, resolved);
6536 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6540 err = got_object_id_by_path(&tree_id, repo,
6541 worktree->base_commit_id, worktree->path_prefix);
6545 err = delete_rebase_refs(worktree, repo);
6549 err = get_fileindex_path(&fileindex_path, worktree);
6553 rfa.worktree = worktree;
6554 rfa.fileindex = fileindex;
6555 rfa.progress_cb = progress_cb;
6556 rfa.progress_arg = progress_arg;
6557 rfa.patch_cb = NULL;
6558 rfa.patch_arg = NULL;
6560 err = worktree_status(worktree, "", fileindex, repo,
6561 revert_file, &rfa, NULL, NULL, 0, 0);
6565 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6566 repo, progress_cb, progress_arg, NULL, NULL);
6568 sync_err = sync_fileindex(fileindex, fileindex_path);
6569 if (sync_err && err == NULL)
6572 got_ref_close(resolved);
6576 got_fileindex_free(fileindex);
6577 free(fileindex_path);
6579 unlockerr = lock_worktree(worktree, LOCK_SH);
6580 if (unlockerr && err == NULL)
6585 const struct got_error *
6586 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6587 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6588 struct got_fileindex **fileindex, struct got_worktree *worktree,
6589 struct got_repository *repo)
6591 const struct got_error *err = NULL;
6592 char *tmp_branch_name = NULL;
6593 char *branch_ref_name = NULL;
6594 char *base_commit_ref_name = NULL;
6595 char *fileindex_path = NULL;
6596 struct check_rebase_ok_arg ok_arg;
6597 struct got_reference *wt_branch = NULL;
6598 struct got_reference *base_commit_ref = NULL;
6602 *base_commit_id = NULL;
6605 err = lock_worktree(worktree, LOCK_EX);
6609 err = open_fileindex(fileindex, &fileindex_path, worktree);
6613 ok_arg.worktree = worktree;
6615 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6620 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6624 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6628 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6633 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6638 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6642 err = got_ref_write(*branch_ref, repo);
6646 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6647 worktree->base_commit_id);
6650 err = got_ref_write(base_commit_ref, repo);
6653 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6654 if (*base_commit_id == NULL) {
6655 err = got_error_from_errno("got_object_id_dup");
6659 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6660 worktree->base_commit_id);
6663 err = got_ref_write(*tmp_branch, repo);
6667 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6671 free(fileindex_path);
6672 free(tmp_branch_name);
6673 free(branch_ref_name);
6674 free(base_commit_ref_name);
6676 got_ref_close(wt_branch);
6679 got_ref_close(*branch_ref);
6683 got_ref_close(*tmp_branch);
6686 free(*base_commit_id);
6688 got_fileindex_free(*fileindex);
6691 lock_worktree(worktree, LOCK_SH);
6696 const struct got_error *
6697 got_worktree_histedit_postpone(struct got_worktree *worktree,
6698 struct got_fileindex *fileindex)
6701 got_fileindex_free(fileindex);
6702 return lock_worktree(worktree, LOCK_SH);
6705 const struct got_error *
6706 got_worktree_histedit_in_progress(int *in_progress,
6707 struct got_worktree *worktree)
6709 const struct got_error *err;
6710 char *tmp_branch_name = NULL;
6712 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6716 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6717 free(tmp_branch_name);
6721 const struct got_error *
6722 got_worktree_histedit_continue(struct got_object_id **commit_id,
6723 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6724 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6725 struct got_worktree *worktree, struct got_repository *repo)
6727 const struct got_error *err;
6728 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6729 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6730 struct got_reference *commit_ref = NULL;
6731 struct got_reference *base_commit_ref = NULL;
6732 char *fileindex_path = NULL;
6733 int have_staged_files = 0;
6737 *base_commit_id = NULL;
6740 err = lock_worktree(worktree, LOCK_EX);
6744 err = open_fileindex(fileindex, &fileindex_path, worktree);
6748 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6749 &have_staged_files);
6750 if (err && err->code != GOT_ERR_CANCELLED)
6752 if (have_staged_files) {
6753 err = got_error(GOT_ERR_STAGED_PATHS);
6757 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6761 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6765 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6769 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6774 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6778 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6781 err = got_ref_resolve(commit_id, repo, commit_ref);
6785 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6788 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6792 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6796 free(commit_ref_name);
6797 free(branch_ref_name);
6798 free(fileindex_path);
6800 got_ref_close(commit_ref);
6801 if (base_commit_ref)
6802 got_ref_close(base_commit_ref);
6806 free(*base_commit_id);
6807 *base_commit_id = NULL;
6809 got_ref_close(*tmp_branch);
6813 got_fileindex_free(*fileindex);
6816 lock_worktree(worktree, LOCK_EX);
6821 static const struct got_error *
6822 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6824 const struct got_error *err;
6825 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6826 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6828 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6831 err = delete_ref(tmp_branch_name, repo);
6835 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6839 err = delete_ref(base_commit_ref_name, repo);
6843 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6846 err = delete_ref(branch_ref_name, repo);
6850 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6853 err = delete_ref(commit_ref_name, repo);
6857 free(tmp_branch_name);
6858 free(base_commit_ref_name);
6859 free(branch_ref_name);
6860 free(commit_ref_name);
6864 const struct got_error *
6865 got_worktree_histedit_abort(struct got_worktree *worktree,
6866 struct got_fileindex *fileindex, struct got_repository *repo,
6867 struct got_reference *branch, struct got_object_id *base_commit_id,
6868 got_worktree_checkout_cb progress_cb, void *progress_arg)
6870 const struct got_error *err, *unlockerr, *sync_err;
6871 struct got_reference *resolved = NULL;
6872 char *fileindex_path = NULL;
6873 struct got_object_id *tree_id = NULL;
6874 struct revert_file_args rfa;
6876 err = lock_worktree(worktree, LOCK_EX);
6880 err = got_ref_open(&resolved, repo,
6881 got_ref_get_symref_target(branch), 0);
6885 err = got_worktree_set_head_ref(worktree, resolved);
6889 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6893 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6894 worktree->path_prefix);
6898 err = delete_histedit_refs(worktree, repo);
6902 err = get_fileindex_path(&fileindex_path, worktree);
6906 rfa.worktree = worktree;
6907 rfa.fileindex = fileindex;
6908 rfa.progress_cb = progress_cb;
6909 rfa.progress_arg = progress_arg;
6910 rfa.patch_cb = NULL;
6911 rfa.patch_arg = NULL;
6913 err = worktree_status(worktree, "", fileindex, repo,
6914 revert_file, &rfa, NULL, NULL, 0, 0);
6918 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6919 repo, progress_cb, progress_arg, NULL, NULL);
6921 sync_err = sync_fileindex(fileindex, fileindex_path);
6922 if (sync_err && err == NULL)
6925 got_ref_close(resolved);
6927 free(fileindex_path);
6929 unlockerr = lock_worktree(worktree, LOCK_SH);
6930 if (unlockerr && err == NULL)
6935 const struct got_error *
6936 got_worktree_histedit_complete(struct got_worktree *worktree,
6937 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6938 struct got_reference *edited_branch, struct got_repository *repo)
6940 const struct got_error *err, *unlockerr, *sync_err;
6941 struct got_object_id *new_head_commit_id = NULL;
6942 struct got_reference *resolved = NULL;
6943 char *fileindex_path = NULL;
6945 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6949 err = got_ref_open(&resolved, repo,
6950 got_ref_get_symref_target(edited_branch), 0);
6954 err = got_ref_change_ref(resolved, new_head_commit_id);
6958 err = got_ref_write(resolved, repo);
6962 err = got_worktree_set_head_ref(worktree, resolved);
6966 err = delete_histedit_refs(worktree, repo);
6970 err = get_fileindex_path(&fileindex_path, worktree);
6973 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6974 sync_err = sync_fileindex(fileindex, fileindex_path);
6975 if (sync_err && err == NULL)
6978 got_fileindex_free(fileindex);
6979 free(fileindex_path);
6980 free(new_head_commit_id);
6981 unlockerr = lock_worktree(worktree, LOCK_SH);
6982 if (unlockerr && err == NULL)
6987 const struct got_error *
6988 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6989 struct got_object_id *commit_id, struct got_repository *repo)
6991 const struct got_error *err;
6992 char *commit_ref_name;
6994 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6998 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7002 err = delete_ref(commit_ref_name, repo);
7004 free(commit_ref_name);
7008 const struct got_error *
7009 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7010 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7011 struct got_worktree *worktree, const char *refname,
7012 struct got_repository *repo)
7014 const struct got_error *err = NULL;
7015 char *fileindex_path = NULL;
7016 struct check_rebase_ok_arg ok_arg;
7020 *base_branch_ref = NULL;
7022 err = lock_worktree(worktree, LOCK_EX);
7026 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7027 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7028 "cannot integrate a branch into itself; "
7029 "update -b or different branch name required");
7033 err = open_fileindex(fileindex, &fileindex_path, worktree);
7037 /* Preconditions are the same as for rebase. */
7038 ok_arg.worktree = worktree;
7040 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7045 err = got_ref_open(branch_ref, repo, refname, 1);
7049 err = got_ref_open(base_branch_ref, repo,
7050 got_worktree_get_head_ref_name(worktree), 1);
7054 got_ref_close(*branch_ref);
7057 if (*base_branch_ref) {
7058 got_ref_close(*base_branch_ref);
7059 *base_branch_ref = NULL;
7062 got_fileindex_free(*fileindex);
7065 lock_worktree(worktree, LOCK_SH);
7070 const struct got_error *
7071 got_worktree_integrate_continue(struct got_worktree *worktree,
7072 struct got_fileindex *fileindex, struct got_repository *repo,
7073 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7074 got_worktree_checkout_cb progress_cb, void *progress_arg,
7075 got_cancel_cb cancel_cb, void *cancel_arg)
7077 const struct got_error *err = NULL, *sync_err, *unlockerr;
7078 char *fileindex_path = NULL;
7079 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7081 err = get_fileindex_path(&fileindex_path, worktree);
7085 err = got_ref_resolve(&commit_id, repo, branch_ref);
7089 err = got_object_id_by_path(&tree_id, repo, commit_id,
7090 worktree->path_prefix);
7094 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7098 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7099 progress_cb, progress_arg, cancel_cb, cancel_arg);
7103 err = got_ref_change_ref(base_branch_ref, commit_id);
7107 err = got_ref_write(base_branch_ref, repo);
7109 sync_err = sync_fileindex(fileindex, fileindex_path);
7110 if (sync_err && err == NULL)
7114 unlockerr = got_ref_unlock(branch_ref);
7115 if (unlockerr && err == NULL)
7117 got_ref_close(branch_ref);
7119 unlockerr = got_ref_unlock(base_branch_ref);
7120 if (unlockerr && err == NULL)
7122 got_ref_close(base_branch_ref);
7124 got_fileindex_free(fileindex);
7125 free(fileindex_path);
7128 unlockerr = lock_worktree(worktree, LOCK_SH);
7129 if (unlockerr && err == NULL)
7134 const struct got_error *
7135 got_worktree_integrate_abort(struct got_worktree *worktree,
7136 struct got_fileindex *fileindex, struct got_repository *repo,
7137 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7139 const struct got_error *err = NULL, *unlockerr = NULL;
7141 got_fileindex_free(fileindex);
7143 err = lock_worktree(worktree, LOCK_SH);
7145 unlockerr = got_ref_unlock(branch_ref);
7146 if (unlockerr && err == NULL)
7148 got_ref_close(branch_ref);
7150 unlockerr = got_ref_unlock(base_branch_ref);
7151 if (unlockerr && err == NULL)
7153 got_ref_close(base_branch_ref);
7158 struct check_stage_ok_arg {
7159 struct got_object_id *head_commit_id;
7160 struct got_worktree *worktree;
7161 struct got_fileindex *fileindex;
7162 struct got_repository *repo;
7166 const struct got_error *
7167 check_stage_ok(void *arg, unsigned char status,
7168 unsigned char staged_status, const char *relpath,
7169 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7170 struct got_object_id *commit_id, int dirfd, const char *de_name)
7172 struct check_stage_ok_arg *a = arg;
7173 const struct got_error *err = NULL;
7174 struct got_fileindex_entry *ie;
7175 struct got_object_id base_commit_id;
7176 struct got_object_id *base_commit_idp = NULL;
7177 char *in_repo_path = NULL, *p;
7179 if (status == GOT_STATUS_UNVERSIONED ||
7180 status == GOT_STATUS_NO_CHANGE)
7182 if (status == GOT_STATUS_NONEXISTENT)
7183 return got_error_set_errno(ENOENT, relpath);
7185 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7187 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7189 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7190 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7192 return got_error_from_errno("asprintf");
7194 if (got_fileindex_entry_has_commit(ie)) {
7195 memcpy(base_commit_id.sha1, ie->commit_sha1,
7196 SHA1_DIGEST_LENGTH);
7197 base_commit_idp = &base_commit_id;
7200 if (status == GOT_STATUS_CONFLICT) {
7201 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7203 } else if (status != GOT_STATUS_ADD &&
7204 status != GOT_STATUS_MODIFY &&
7205 status != GOT_STATUS_DELETE) {
7206 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7210 a->have_changes = 1;
7215 err = check_out_of_date(p, status, staged_status,
7216 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7217 GOT_ERR_STAGE_OUT_OF_DATE);
7223 struct stage_path_arg {
7224 struct got_worktree *worktree;
7225 struct got_fileindex *fileindex;
7226 struct got_repository *repo;
7227 got_worktree_status_cb status_cb;
7229 got_worktree_patch_cb patch_cb;
7231 int staged_something;
7232 int allow_bad_symlinks;
7235 static const struct got_error *
7236 stage_path(void *arg, unsigned char status,
7237 unsigned char staged_status, const char *relpath,
7238 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7239 struct got_object_id *commit_id, int dirfd, const char *de_name)
7241 struct stage_path_arg *a = arg;
7242 const struct got_error *err = NULL;
7243 struct got_fileindex_entry *ie;
7244 char *ondisk_path = NULL, *path_content = NULL;
7246 struct got_object_id *new_staged_blob_id = NULL;
7249 if (status == GOT_STATUS_UNVERSIONED)
7252 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7254 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7256 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7258 return got_error_from_errno("asprintf");
7261 case GOT_STATUS_ADD:
7262 case GOT_STATUS_MODIFY:
7263 /* XXX could sb.st_mode be passed in by our caller? */
7264 if (lstat(ondisk_path, &sb) == -1) {
7265 err = got_error_from_errno2("lstat", ondisk_path);
7269 if (status == GOT_STATUS_ADD) {
7270 int choice = GOT_PATCH_CHOICE_NONE;
7271 err = (*a->patch_cb)(&choice, a->patch_arg,
7272 status, ie->path, NULL, 1, 1);
7275 if (choice != GOT_PATCH_CHOICE_YES)
7278 err = create_patched_content(&path_content, 0,
7279 staged_blob_id ? staged_blob_id : blob_id,
7280 ondisk_path, dirfd, de_name, ie->path,
7281 a->repo, a->patch_cb, a->patch_arg);
7282 if (err || path_content == NULL)
7286 err = got_object_blob_create(&new_staged_blob_id,
7287 path_content ? path_content : ondisk_path, a->repo);
7290 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7291 SHA1_DIGEST_LENGTH);
7292 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7293 stage = GOT_FILEIDX_STAGE_ADD;
7295 stage = GOT_FILEIDX_STAGE_MODIFY;
7296 got_fileindex_entry_stage_set(ie, stage);
7297 if (S_ISLNK(sb.st_mode)) {
7298 int is_bad_symlink = 0;
7299 if (!a->allow_bad_symlinks) {
7300 char target_path[PATH_MAX];
7302 target_len = readlink(ondisk_path, target_path,
7303 sizeof(target_path));
7304 if (target_len == -1) {
7305 err = got_error_from_errno2("readlink",
7309 err = is_bad_symlink_target(&is_bad_symlink,
7310 target_path, target_len, ondisk_path,
7311 a->worktree->root_path);
7314 if (is_bad_symlink) {
7315 err = got_error_path(ondisk_path,
7316 GOT_ERR_BAD_SYMLINK);
7321 got_fileindex_entry_staged_filetype_set(ie,
7322 GOT_FILEIDX_MODE_BAD_SYMLINK);
7324 got_fileindex_entry_staged_filetype_set(ie,
7325 GOT_FILEIDX_MODE_SYMLINK);
7327 got_fileindex_entry_staged_filetype_set(ie,
7328 GOT_FILEIDX_MODE_REGULAR_FILE);
7330 a->staged_something = 1;
7331 if (a->status_cb == NULL)
7333 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7334 get_staged_status(ie), relpath, blob_id,
7335 new_staged_blob_id, NULL, dirfd, de_name);
7337 case GOT_STATUS_DELETE:
7338 if (staged_status == GOT_STATUS_DELETE)
7341 int choice = GOT_PATCH_CHOICE_NONE;
7342 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7343 ie->path, NULL, 1, 1);
7346 if (choice == GOT_PATCH_CHOICE_NO)
7348 if (choice != GOT_PATCH_CHOICE_YES) {
7349 err = got_error(GOT_ERR_PATCH_CHOICE);
7353 stage = GOT_FILEIDX_STAGE_DELETE;
7354 got_fileindex_entry_stage_set(ie, stage);
7355 a->staged_something = 1;
7356 if (a->status_cb == NULL)
7358 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7359 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7362 case GOT_STATUS_NO_CHANGE:
7364 case GOT_STATUS_CONFLICT:
7365 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7367 case GOT_STATUS_NONEXISTENT:
7368 err = got_error_set_errno(ENOENT, relpath);
7371 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7375 if (path_content && unlink(path_content) == -1 && err == NULL)
7376 err = got_error_from_errno2("unlink", path_content);
7379 free(new_staged_blob_id);
7383 const struct got_error *
7384 got_worktree_stage(struct got_worktree *worktree,
7385 struct got_pathlist_head *paths,
7386 got_worktree_status_cb status_cb, void *status_arg,
7387 got_worktree_patch_cb patch_cb, void *patch_arg,
7388 int allow_bad_symlinks, struct got_repository *repo)
7390 const struct got_error *err = NULL, *sync_err, *unlockerr;
7391 struct got_pathlist_entry *pe;
7392 struct got_fileindex *fileindex = NULL;
7393 char *fileindex_path = NULL;
7394 struct got_reference *head_ref = NULL;
7395 struct got_object_id *head_commit_id = NULL;
7396 struct check_stage_ok_arg oka;
7397 struct stage_path_arg spa;
7399 err = lock_worktree(worktree, LOCK_EX);
7403 err = got_ref_open(&head_ref, repo,
7404 got_worktree_get_head_ref_name(worktree), 0);
7407 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7410 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7414 /* Check pre-conditions before staging anything. */
7415 oka.head_commit_id = head_commit_id;
7416 oka.worktree = worktree;
7417 oka.fileindex = fileindex;
7419 oka.have_changes = 0;
7420 TAILQ_FOREACH(pe, paths, entry) {
7421 err = worktree_status(worktree, pe->path, fileindex, repo,
7422 check_stage_ok, &oka, NULL, NULL, 0, 0);
7426 if (!oka.have_changes) {
7427 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7431 spa.worktree = worktree;
7432 spa.fileindex = fileindex;
7434 spa.patch_cb = patch_cb;
7435 spa.patch_arg = patch_arg;
7436 spa.status_cb = status_cb;
7437 spa.status_arg = status_arg;
7438 spa.staged_something = 0;
7439 spa.allow_bad_symlinks = allow_bad_symlinks;
7440 TAILQ_FOREACH(pe, paths, entry) {
7441 err = worktree_status(worktree, pe->path, fileindex, repo,
7442 stage_path, &spa, NULL, NULL, 0, 0);
7446 if (!spa.staged_something) {
7447 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7451 sync_err = sync_fileindex(fileindex, fileindex_path);
7452 if (sync_err && err == NULL)
7456 got_ref_close(head_ref);
7457 free(head_commit_id);
7458 free(fileindex_path);
7460 got_fileindex_free(fileindex);
7461 unlockerr = lock_worktree(worktree, LOCK_SH);
7462 if (unlockerr && err == NULL)
7467 struct unstage_path_arg {
7468 struct got_worktree *worktree;
7469 struct got_fileindex *fileindex;
7470 struct got_repository *repo;
7471 got_worktree_checkout_cb progress_cb;
7473 got_worktree_patch_cb patch_cb;
7477 static const struct got_error *
7478 create_unstaged_content(char **path_unstaged_content,
7479 char **path_new_staged_content, struct got_object_id *blob_id,
7480 struct got_object_id *staged_blob_id, const char *relpath,
7481 struct got_repository *repo,
7482 got_worktree_patch_cb patch_cb, void *patch_arg)
7484 const struct got_error *err, *free_err;
7485 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7486 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7487 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7488 struct got_diffreg_result *diffreg_result = NULL;
7489 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7490 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7492 *path_unstaged_content = NULL;
7493 *path_new_staged_content = NULL;
7495 err = got_object_id_str(&label1, blob_id);
7498 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7502 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7506 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7510 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7514 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7518 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7522 err = got_diff_files(&diffreg_result, f1, label1, f2,
7523 path2, 3, 0, 1, NULL);
7527 err = got_opentemp_named(path_unstaged_content, &outfile,
7528 "got-unstaged-content");
7531 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7532 "got-new-staged-content");
7536 if (fseek(f1, 0L, SEEK_SET) == -1) {
7537 err = got_ferror(f1, GOT_ERR_IO);
7540 if (fseek(f2, 0L, SEEK_SET) == -1) {
7541 err = got_ferror(f2, GOT_ERR_IO);
7544 /* Count the number of actual changes in the diff result. */
7545 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7546 struct diff_chunk_context cc = {};
7547 diff_chunk_context_load_change(&cc, &nchunks_used,
7548 diffreg_result->result, n, 0);
7551 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7553 err = apply_or_reject_change(&choice, &nchunks_used,
7554 diffreg_result->result, n, relpath, f1, f2,
7555 &line_cur1, &line_cur2,
7556 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7559 if (choice == GOT_PATCH_CHOICE_YES)
7562 have_rejected_content = 1;
7563 if (choice == GOT_PATCH_CHOICE_QUIT)
7566 if (have_content || have_rejected_content)
7567 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7568 outfile, rejectfile);
7572 got_object_blob_close(blob);
7574 got_object_blob_close(staged_blob);
7575 free_err = got_diffreg_result_free(diffreg_result);
7576 if (free_err && err == NULL)
7578 if (f1 && fclose(f1) == EOF && err == NULL)
7579 err = got_error_from_errno2("fclose", path1);
7580 if (f2 && fclose(f2) == EOF && err == NULL)
7581 err = got_error_from_errno2("fclose", path2);
7582 if (outfile && fclose(outfile) == EOF && err == NULL)
7583 err = got_error_from_errno2("fclose", *path_unstaged_content);
7584 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7585 err = got_error_from_errno2("fclose", *path_new_staged_content);
7586 if (path1 && unlink(path1) == -1 && err == NULL)
7587 err = got_error_from_errno2("unlink", path1);
7588 if (path2 && unlink(path2) == -1 && err == NULL)
7589 err = got_error_from_errno2("unlink", path2);
7590 if (err || !have_content) {
7591 if (*path_unstaged_content &&
7592 unlink(*path_unstaged_content) == -1 && err == NULL)
7593 err = got_error_from_errno2("unlink",
7594 *path_unstaged_content);
7595 free(*path_unstaged_content);
7596 *path_unstaged_content = NULL;
7598 if (err || !have_content || !have_rejected_content) {
7599 if (*path_new_staged_content &&
7600 unlink(*path_new_staged_content) == -1 && err == NULL)
7601 err = got_error_from_errno2("unlink",
7602 *path_new_staged_content);
7603 free(*path_new_staged_content);
7604 *path_new_staged_content = NULL;
7611 static const struct got_error *
7612 unstage_hunks(struct got_object_id *staged_blob_id,
7613 struct got_blob_object *blob_base,
7614 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7615 const char *ondisk_path, const char *label_orig,
7616 struct got_worktree *worktree, struct got_repository *repo,
7617 got_worktree_patch_cb patch_cb, void *patch_arg,
7618 got_worktree_checkout_cb progress_cb, void *progress_arg)
7620 const struct got_error *err = NULL;
7621 char *path_unstaged_content = NULL;
7622 char *path_new_staged_content = NULL;
7623 struct got_object_id *new_staged_blob_id = NULL;
7627 err = create_unstaged_content(&path_unstaged_content,
7628 &path_new_staged_content, blob_id, staged_blob_id,
7629 ie->path, repo, patch_cb, patch_arg);
7633 if (path_unstaged_content == NULL)
7636 if (path_new_staged_content) {
7637 err = got_object_blob_create(&new_staged_blob_id,
7638 path_new_staged_content, repo);
7643 f = fopen(path_unstaged_content, "r");
7645 err = got_error_from_errno2("fopen",
7646 path_unstaged_content);
7649 if (fstat(fileno(f), &sb) == -1) {
7650 err = got_error_from_errno2("fstat", path_unstaged_content);
7653 if (got_fileindex_entry_staged_filetype_get(ie) ==
7654 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7655 char link_target[PATH_MAX];
7657 r = fread(link_target, 1, sizeof(link_target), f);
7658 if (r == 0 && ferror(f)) {
7659 err = got_error_from_errno("fread");
7662 if (r >= sizeof(link_target)) { /* should not happen */
7663 err = got_error(GOT_ERR_NO_SPACE);
7666 link_target[r] = '\0';
7667 err = merge_symlink(worktree, blob_base,
7668 ondisk_path, ie->path, label_orig, link_target,
7669 worktree->base_commit_id, repo, progress_cb,
7672 int local_changes_subsumed;
7673 err = merge_file(&local_changes_subsumed, worktree,
7674 blob_base, ondisk_path, ie->path,
7675 got_fileindex_perms_to_st(ie),
7676 path_unstaged_content, label_orig, "unstaged",
7677 repo, progress_cb, progress_arg);
7682 if (new_staged_blob_id) {
7683 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7684 SHA1_DIGEST_LENGTH);
7686 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7687 got_fileindex_entry_staged_filetype_set(ie, 0);
7690 free(new_staged_blob_id);
7691 if (path_unstaged_content &&
7692 unlink(path_unstaged_content) == -1 && err == NULL)
7693 err = got_error_from_errno2("unlink", path_unstaged_content);
7694 if (path_new_staged_content &&
7695 unlink(path_new_staged_content) == -1 && err == NULL)
7696 err = got_error_from_errno2("unlink", path_new_staged_content);
7697 if (f && fclose(f) == EOF && err == NULL)
7698 err = got_error_from_errno2("fclose", path_unstaged_content);
7699 free(path_unstaged_content);
7700 free(path_new_staged_content);
7704 static const struct got_error *
7705 unstage_path(void *arg, unsigned char status,
7706 unsigned char staged_status, const char *relpath,
7707 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7708 struct got_object_id *commit_id, int dirfd, const char *de_name)
7710 const struct got_error *err = NULL;
7711 struct unstage_path_arg *a = arg;
7712 struct got_fileindex_entry *ie;
7713 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7714 char *ondisk_path = NULL;
7715 char *id_str = NULL, *label_orig = NULL;
7716 int local_changes_subsumed;
7719 if (staged_status != GOT_STATUS_ADD &&
7720 staged_status != GOT_STATUS_MODIFY &&
7721 staged_status != GOT_STATUS_DELETE)
7724 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7726 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7728 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7730 return got_error_from_errno("asprintf");
7732 err = got_object_id_str(&id_str,
7733 commit_id ? commit_id : a->worktree->base_commit_id);
7736 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7738 err = got_error_from_errno("asprintf");
7742 switch (staged_status) {
7743 case GOT_STATUS_MODIFY:
7744 err = got_object_open_as_blob(&blob_base, a->repo,
7749 case GOT_STATUS_ADD:
7751 if (staged_status == GOT_STATUS_ADD) {
7752 int choice = GOT_PATCH_CHOICE_NONE;
7753 err = (*a->patch_cb)(&choice, a->patch_arg,
7754 staged_status, ie->path, NULL, 1, 1);
7757 if (choice != GOT_PATCH_CHOICE_YES)
7760 err = unstage_hunks(staged_blob_id,
7761 blob_base, blob_id, ie, ondisk_path,
7762 label_orig, a->worktree, a->repo,
7763 a->patch_cb, a->patch_arg,
7764 a->progress_cb, a->progress_arg);
7765 break; /* Done with this file. */
7768 err = got_object_open_as_blob(&blob_staged, a->repo,
7769 staged_blob_id, 8192);
7772 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7773 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7774 case GOT_FILEIDX_MODE_REGULAR_FILE:
7775 err = merge_blob(&local_changes_subsumed, a->worktree,
7776 blob_base, ondisk_path, relpath,
7777 got_fileindex_perms_to_st(ie), label_orig,
7778 blob_staged, commit_id ? commit_id :
7779 a->worktree->base_commit_id, a->repo,
7780 a->progress_cb, a->progress_arg);
7782 case GOT_FILEIDX_MODE_SYMLINK:
7783 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7784 char *staged_target;
7785 err = got_object_blob_read_to_str(
7786 &staged_target, blob_staged);
7789 err = merge_symlink(a->worktree, blob_base,
7790 ondisk_path, relpath, label_orig,
7791 staged_target, commit_id ? commit_id :
7792 a->worktree->base_commit_id,
7793 a->repo, a->progress_cb, a->progress_arg);
7794 free(staged_target);
7796 err = merge_blob(&local_changes_subsumed,
7797 a->worktree, blob_base, ondisk_path,
7798 relpath, got_fileindex_perms_to_st(ie),
7799 label_orig, blob_staged,
7800 commit_id ? commit_id :
7801 a->worktree->base_commit_id, a->repo,
7802 a->progress_cb, a->progress_arg);
7806 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7810 got_fileindex_entry_stage_set(ie,
7811 GOT_FILEIDX_STAGE_NONE);
7812 got_fileindex_entry_staged_filetype_set(ie, 0);
7815 case GOT_STATUS_DELETE:
7817 int choice = GOT_PATCH_CHOICE_NONE;
7818 err = (*a->patch_cb)(&choice, a->patch_arg,
7819 staged_status, ie->path, NULL, 1, 1);
7822 if (choice == GOT_PATCH_CHOICE_NO)
7824 if (choice != GOT_PATCH_CHOICE_YES) {
7825 err = got_error(GOT_ERR_PATCH_CHOICE);
7829 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7830 got_fileindex_entry_staged_filetype_set(ie, 0);
7831 err = get_file_status(&status, &sb, ie, ondisk_path,
7832 dirfd, de_name, a->repo);
7835 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7841 got_object_blob_close(blob_base);
7843 got_object_blob_close(blob_staged);
7849 const struct got_error *
7850 got_worktree_unstage(struct got_worktree *worktree,
7851 struct got_pathlist_head *paths,
7852 got_worktree_checkout_cb progress_cb, void *progress_arg,
7853 got_worktree_patch_cb patch_cb, void *patch_arg,
7854 struct got_repository *repo)
7856 const struct got_error *err = NULL, *sync_err, *unlockerr;
7857 struct got_pathlist_entry *pe;
7858 struct got_fileindex *fileindex = NULL;
7859 char *fileindex_path = NULL;
7860 struct unstage_path_arg upa;
7862 err = lock_worktree(worktree, LOCK_EX);
7866 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7870 upa.worktree = worktree;
7871 upa.fileindex = fileindex;
7873 upa.progress_cb = progress_cb;
7874 upa.progress_arg = progress_arg;
7875 upa.patch_cb = patch_cb;
7876 upa.patch_arg = patch_arg;
7877 TAILQ_FOREACH(pe, paths, entry) {
7878 err = worktree_status(worktree, pe->path, fileindex, repo,
7879 unstage_path, &upa, NULL, NULL, 0, 0);
7884 sync_err = sync_fileindex(fileindex, fileindex_path);
7885 if (sync_err && err == NULL)
7888 free(fileindex_path);
7890 got_fileindex_free(fileindex);
7891 unlockerr = lock_worktree(worktree, LOCK_SH);
7892 if (unlockerr && err == NULL)
7897 struct report_file_info_arg {
7898 struct got_worktree *worktree;
7899 got_worktree_path_info_cb info_cb;
7901 struct got_pathlist_head *paths;
7902 got_cancel_cb cancel_cb;
7906 static const struct got_error *
7907 report_file_info(void *arg, struct got_fileindex_entry *ie)
7909 struct report_file_info_arg *a = arg;
7910 struct got_pathlist_entry *pe;
7911 struct got_object_id blob_id, staged_blob_id, commit_id;
7912 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7913 struct got_object_id *commit_idp = NULL;
7916 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7917 return got_error(GOT_ERR_CANCELLED);
7919 TAILQ_FOREACH(pe, a->paths, entry) {
7920 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7921 got_path_is_child(ie->path, pe->path, pe->path_len))
7924 if (pe == NULL) /* not found */
7927 if (got_fileindex_entry_has_blob(ie)) {
7928 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7929 blob_idp = &blob_id;
7931 stage = got_fileindex_entry_stage_get(ie);
7932 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7933 stage == GOT_FILEIDX_STAGE_ADD) {
7934 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7935 SHA1_DIGEST_LENGTH);
7936 staged_blob_idp = &staged_blob_id;
7939 if (got_fileindex_entry_has_commit(ie)) {
7940 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
7941 commit_idp = &commit_id;
7944 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
7945 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
7948 const struct got_error *
7949 got_worktree_path_info(struct got_worktree *worktree,
7950 struct got_pathlist_head *paths,
7951 got_worktree_path_info_cb info_cb, void *info_arg,
7952 got_cancel_cb cancel_cb, void *cancel_arg)
7955 const struct got_error *err = NULL, *unlockerr;
7956 struct got_fileindex *fileindex = NULL;
7957 char *fileindex_path = NULL;
7958 struct report_file_info_arg arg;
7960 err = lock_worktree(worktree, LOCK_SH);
7964 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7968 arg.worktree = worktree;
7969 arg.info_cb = info_cb;
7970 arg.info_arg = info_arg;
7972 arg.cancel_cb = cancel_cb;
7973 arg.cancel_arg = cancel_arg;
7974 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
7977 free(fileindex_path);
7979 got_fileindex_free(fileindex);
7980 unlockerr = lock_worktree(worktree, LOCK_UN);
7981 if (unlockerr && err == NULL)