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;
503 if (worktree->lockfd != -1) {
504 if (close(worktree->lockfd) == -1)
505 err = got_error_from_errno2("close",
506 got_worktree_get_root_path(worktree));
508 if (close(worktree->root_fd) == -1 && err == NULL)
509 err = got_error_from_errno2("close",
510 got_worktree_get_root_path(worktree));
511 free(worktree->repo_path);
512 free(worktree->path_prefix);
513 free(worktree->base_commit_id);
514 free(worktree->head_ref_name);
515 free(worktree->root_path);
516 free(worktree->gotconfig_path);
517 got_gotconfig_free(worktree->gotconfig);
523 got_worktree_get_root_path(struct got_worktree *worktree)
525 return worktree->root_path;
529 got_worktree_get_repo_path(struct got_worktree *worktree)
531 return worktree->repo_path;
534 got_worktree_get_path_prefix(struct got_worktree *worktree)
536 return worktree->path_prefix;
539 const struct got_error *
540 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
541 const char *path_prefix)
543 char *absprefix = NULL;
545 if (!got_path_is_absolute(path_prefix)) {
546 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
547 return got_error_from_errno("asprintf");
549 *match = (strcmp(absprefix ? absprefix : path_prefix,
550 worktree->path_prefix) == 0);
556 got_worktree_get_head_ref_name(struct got_worktree *worktree)
558 return worktree->head_ref_name;
561 const struct got_error *
562 got_worktree_set_head_ref(struct got_worktree *worktree,
563 struct got_reference *head_ref)
565 const struct got_error *err = NULL;
566 char *path_got = NULL, *head_ref_name = NULL;
568 if (asprintf(&path_got, "%s/%s", worktree->root_path,
569 GOT_WORKTREE_GOT_DIR) == -1) {
570 err = got_error_from_errno("asprintf");
575 head_ref_name = strdup(got_ref_get_name(head_ref));
576 if (head_ref_name == NULL) {
577 err = got_error_from_errno("strdup");
581 err = write_head_ref(path_got, head_ref);
585 free(worktree->head_ref_name);
586 worktree->head_ref_name = head_ref_name;
594 struct got_object_id *
595 got_worktree_get_base_commit_id(struct got_worktree *worktree)
597 return worktree->base_commit_id;
600 const struct got_error *
601 got_worktree_set_base_commit_id(struct got_worktree *worktree,
602 struct got_repository *repo, struct got_object_id *commit_id)
604 const struct got_error *err;
605 struct got_object *obj = NULL;
607 char *path_got = NULL;
609 if (asprintf(&path_got, "%s/%s", worktree->root_path,
610 GOT_WORKTREE_GOT_DIR) == -1) {
611 err = got_error_from_errno("asprintf");
616 err = got_object_open(&obj, repo, commit_id);
620 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
621 err = got_error(GOT_ERR_OBJ_TYPE);
625 /* Record our base commit. */
626 err = got_object_id_str(&id_str, commit_id);
629 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
633 free(worktree->base_commit_id);
634 worktree->base_commit_id = got_object_id_dup(commit_id);
635 if (worktree->base_commit_id == NULL) {
636 err = got_error_from_errno("got_object_id_dup");
641 got_object_close(obj);
647 const struct got_gotconfig *
648 got_worktree_get_gotconfig(struct got_worktree *worktree)
650 return worktree->gotconfig;
653 static const struct got_error *
654 lock_worktree(struct got_worktree *worktree, int operation)
656 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
657 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
658 : got_error_from_errno2("flock",
659 got_worktree_get_root_path(worktree)));
663 static const struct got_error *
664 add_dir_on_disk(struct got_worktree *worktree, const char *path)
666 const struct got_error *err = NULL;
669 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
670 return got_error_from_errno("asprintf");
672 err = got_path_mkdir(abspath);
673 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
676 if (lstat(abspath, &sb) == -1) {
677 err = got_error_from_errno2("lstat", abspath);
678 } else if (!S_ISDIR(sb.st_mode)) {
679 /* TODO directory is obstructed; do something */
680 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
687 static const struct got_error *
688 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
690 const struct got_error *err = NULL;
693 size_t flen1 = 0, flen2 = 0;
698 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
699 if (flen1 == 0 && ferror(f1)) {
700 err = got_error_from_errno("fread");
703 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
704 if (flen2 == 0 && ferror(f2)) {
705 err = got_error_from_errno("fread");
712 } else if (flen2 == 0) {
716 } else if (flen1 == flen2) {
717 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
730 static const struct got_error *
731 check_files_equal(int *same, const char *f1_path, const char *f2_path)
733 const struct got_error *err = NULL;
736 FILE *f1 = NULL, *f2 = NULL;
740 if (lstat(f1_path, &sb) != 0) {
741 err = got_error_from_errno2("lstat", f1_path);
746 if (lstat(f2_path, &sb) != 0) {
747 err = got_error_from_errno2("lstat", f2_path);
752 if (size1 != size2) {
757 f1 = fopen(f1_path, "r");
759 return got_error_from_errno2("fopen", f1_path);
761 f2 = fopen(f2_path, "r");
763 err = got_error_from_errno2("fopen", f2_path);
767 err = check_file_contents_equal(same, f1, f2);
769 if (f1 && fclose(f1) == EOF && err == NULL)
770 err = got_error_from_errno("fclose");
771 if (f2 && fclose(f2) == EOF && err == NULL)
772 err = got_error_from_errno("fclose");
778 * Perform a 3-way merge where blob_orig acts as the common ancestor,
779 * the file at deriv_path acts as the first derived version, and the
780 * file on disk acts as the second derived version.
782 static const struct got_error *
783 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
784 struct got_blob_object *blob_orig, const char *ondisk_path,
785 const char *path, uint16_t st_mode, const char *deriv_path,
786 const char *label_orig, const char *label_deriv,
787 struct got_repository *repo,
788 got_worktree_checkout_cb progress_cb, void *progress_arg)
790 const struct got_error *err = NULL;
793 char *blob_orig_path = NULL;
794 char *merged_path = NULL, *base_path = NULL;
797 char *symlink_path = NULL;
798 FILE *symlinkf = NULL;
800 *local_changes_subsumed = 0;
802 err = got_path_dirname(&parent, ondisk_path);
806 if (asprintf(&base_path, "%s/got-merged", parent) == -1) {
807 err = got_error_from_errno("asprintf");
811 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
816 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
817 err = got_error_from_errno("asprintf");
822 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
826 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
832 * If the file has no blob, this is an "add vs add" conflict,
833 * and we simply use an empty ancestor file to make both files
834 * appear in the merged result in their entirety.
839 * In order the run a 3-way merge with a symlink we copy the symlink's
840 * target path into a temporary file and use that file with diff3.
842 if (S_ISLNK(st_mode)) {
843 char target_path[PATH_MAX];
848 if (asprintf(&base_path, "%s/got-symlink-merge",
850 err = got_error_from_errno("asprintf");
854 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
857 target_len = readlink(ondisk_path, target_path,
858 sizeof(target_path));
859 if (target_len == -1) {
860 err = got_error_from_errno2("readlink", ondisk_path);
863 n = fwrite(target_path, 1, target_len, symlinkf);
864 if (n != target_len) {
865 err = got_ferror(symlinkf, GOT_ERR_IO);
868 if (fflush(symlinkf) == EOF) {
869 err = got_error_from_errno2("fflush", symlink_path);
874 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
875 blob_orig_path, symlink_path ? symlink_path : ondisk_path,
876 label_deriv, label_orig, NULL);
880 err = (*progress_cb)(progress_arg,
881 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
885 if (fsync(merged_fd) != 0) {
886 err = got_error_from_errno("fsync");
890 /* Check if a clean merge has subsumed all local changes. */
891 if (overlapcnt == 0) {
892 err = check_files_equal(local_changes_subsumed, deriv_path,
898 if (fchmod(merged_fd, st_mode) != 0) {
899 err = got_error_from_errno2("fchmod", merged_path);
903 if (rename(merged_path, ondisk_path) != 0) {
904 err = got_error_from_errno3("rename", merged_path,
914 if (unlink(symlink_path) == -1 && err == NULL)
915 err = got_error_from_errno2("unlink", symlink_path);
917 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
918 err = got_error_from_errno2("fclose", symlink_path);
920 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
921 err = got_error_from_errno("close");
922 if (f_orig && fclose(f_orig) == EOF && err == NULL)
923 err = got_error_from_errno("fclose");
926 if (blob_orig_path) {
927 unlink(blob_orig_path);
928 free(blob_orig_path);
934 static const struct got_error *
935 update_symlink(const char *ondisk_path, const char *target_path,
938 /* This is not atomic but matches what 'ln -sf' does. */
939 if (unlink(ondisk_path) == -1)
940 return got_error_from_errno2("unlink", ondisk_path);
941 if (symlink(target_path, ondisk_path) == -1)
942 return got_error_from_errno3("symlink", target_path,
948 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
949 * in the work tree with a file that contains conflict markers and the
950 * conflicting target paths of the original version, a "derived version"
951 * of a symlink from an incoming change, and a local version of the symlink.
953 * The original versions's target path can be NULL if it is not available,
954 * such as if both derived versions added a new symlink at the same path.
956 * The incoming derived symlink target is NULL in case the incoming change
957 * has deleted this symlink.
959 static const struct got_error *
960 install_symlink_conflict(const char *deriv_target,
961 struct got_object_id *deriv_base_commit_id, const char *orig_target,
962 const char *label_orig, const char *local_target, const char *ondisk_path)
964 const struct got_error *err;
965 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
968 err = got_object_id_str(&id_str, deriv_base_commit_id);
970 return got_error_from_errno("asprintf");
972 if (asprintf(&label_deriv, "%s: commit %s",
973 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
974 err = got_error_from_errno("asprintf");
978 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
982 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
983 err = got_error_from_errno2("fchmod", path);
987 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
988 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
989 deriv_target ? deriv_target : "(symlink was deleted)",
990 orig_target ? label_orig : "",
991 orig_target ? "\n" : "",
992 orig_target ? orig_target : "",
993 orig_target ? "\n" : "",
994 GOT_DIFF_CONFLICT_MARKER_SEP,
995 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
996 err = got_error_from_errno2("fprintf", path);
1000 if (unlink(ondisk_path) == -1) {
1001 err = got_error_from_errno2("unlink", ondisk_path);
1004 if (rename(path, ondisk_path) == -1) {
1005 err = got_error_from_errno3("rename", path, ondisk_path);
1009 if (f != NULL && fclose(f) == EOF && err == NULL)
1010 err = got_error_from_errno2("fclose", path);
1017 /* forward declaration */
1018 static const struct got_error *
1019 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1020 const char *, const char *, uint16_t, const char *,
1021 struct got_blob_object *, struct got_object_id *,
1022 struct got_repository *, got_worktree_checkout_cb, void *);
1025 * Merge a symlink into the work tree, where blob_orig acts as the common
1026 * ancestor, deriv_target is the link target of the first derived version,
1027 * and the symlink on disk acts as the second derived version.
1028 * Assume that contents of both blobs represent symlinks.
1030 static const struct got_error *
1031 merge_symlink(struct got_worktree *worktree,
1032 struct got_blob_object *blob_orig, const char *ondisk_path,
1033 const char *path, const char *label_orig, const char *deriv_target,
1034 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1035 got_worktree_checkout_cb progress_cb, void *progress_arg)
1037 const struct got_error *err = NULL;
1038 char *ancestor_target = NULL;
1040 ssize_t ondisk_len, deriv_len;
1041 char ondisk_target[PATH_MAX];
1042 int have_local_change = 0;
1043 int have_incoming_change = 0;
1045 if (lstat(ondisk_path, &sb) == -1)
1046 return got_error_from_errno2("lstat", ondisk_path);
1048 ondisk_len = readlink(ondisk_path, ondisk_target,
1049 sizeof(ondisk_target));
1050 if (ondisk_len == -1) {
1051 err = got_error_from_errno2("readlink",
1055 ondisk_target[ondisk_len] = '\0';
1058 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1063 if (ancestor_target == NULL ||
1064 (ondisk_len != strlen(ancestor_target) ||
1065 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1066 have_local_change = 1;
1068 deriv_len = strlen(deriv_target);
1069 if (ancestor_target == NULL ||
1070 (deriv_len != strlen(ancestor_target) ||
1071 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1072 have_incoming_change = 1;
1074 if (!have_local_change && !have_incoming_change) {
1075 if (ancestor_target) {
1076 /* Both sides made the same change. */
1077 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1079 } else if (deriv_len == ondisk_len &&
1080 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1081 /* Both sides added the same symlink. */
1082 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1085 /* Both sides added symlinks which don't match. */
1086 err = install_symlink_conflict(deriv_target,
1087 deriv_base_commit_id, ancestor_target,
1088 label_orig, ondisk_target, ondisk_path);
1091 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1094 } else if (!have_local_change && have_incoming_change) {
1095 /* Apply the incoming change. */
1096 err = update_symlink(ondisk_path, deriv_target,
1097 strlen(deriv_target));
1100 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1101 } else if (have_local_change && have_incoming_change) {
1102 if (deriv_len == ondisk_len &&
1103 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1104 /* Both sides made the same change. */
1105 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1108 err = install_symlink_conflict(deriv_target,
1109 deriv_base_commit_id, ancestor_target, label_orig,
1110 ondisk_target, ondisk_path);
1113 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1119 free(ancestor_target);
1124 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1125 * blob_deriv acts as the first derived version, and the file on disk
1126 * acts as the second derived version.
1128 static const struct got_error *
1129 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1130 struct got_blob_object *blob_orig, const char *ondisk_path,
1131 const char *path, uint16_t st_mode, const char *label_orig,
1132 struct got_blob_object *blob_deriv,
1133 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1134 got_worktree_checkout_cb progress_cb, void *progress_arg)
1136 const struct got_error *err = NULL;
1137 FILE *f_deriv = NULL;
1138 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1139 char *label_deriv = NULL, *parent = NULL;
1141 *local_changes_subsumed = 0;
1143 err = got_path_dirname(&parent, ondisk_path);
1148 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1149 err = got_error_from_errno("asprintf");
1154 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1157 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1162 err = got_object_id_str(&id_str, deriv_base_commit_id);
1165 if (asprintf(&label_deriv, "%s: commit %s",
1166 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1167 err = got_error_from_errno("asprintf");
1171 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1172 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1173 label_deriv, repo, progress_cb, progress_arg);
1175 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1176 err = got_error_from_errno("fclose");
1178 if (blob_deriv_path) {
1179 unlink(blob_deriv_path);
1180 free(blob_deriv_path);
1188 static const struct got_error *
1189 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1190 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1191 int wt_fd, const char *path, struct got_object_id *blob_id)
1193 const struct got_error *err = NULL;
1194 struct got_fileindex_entry *new_ie;
1198 err = got_fileindex_entry_alloc(&new_ie, path);
1202 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1203 blob_id->sha1, base_commit_id->sha1, 1);
1207 err = got_fileindex_entry_add(fileindex, new_ie);
1210 got_fileindex_entry_free(new_ie);
1217 get_ondisk_perms(int executable, mode_t st_mode)
1219 mode_t xbits = S_IXUSR;
1222 /* Map read bits to execute bits. */
1223 if (st_mode & S_IRGRP)
1225 if (st_mode & S_IROTH)
1227 return st_mode | xbits;
1230 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1233 /* forward declaration */
1234 static const struct got_error *
1235 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1236 const char *path, mode_t te_mode, mode_t st_mode,
1237 struct got_blob_object *blob, int restoring_missing_file,
1238 int reverting_versioned_file, int installing_bad_symlink,
1239 int path_is_unversioned, struct got_repository *repo,
1240 got_worktree_checkout_cb progress_cb, void *progress_arg);
1243 * This function assumes that the provided symlink target points at a
1244 * safe location in the work tree!
1246 static const struct got_error *
1247 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1250 const struct got_error *err = NULL;
1252 char etarget[PATH_MAX];
1256 * "Bad" symlinks (those pointing outside the work tree or into the
1257 * .got directory) are installed in the work tree as a regular file
1258 * which contains the bad symlink target path.
1259 * The new symlink target has already been checked for safety by our
1260 * caller. If we can successfully open a regular file then we simply
1261 * replace this file with a symlink below.
1263 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1266 return got_error_from_errno2("open", ondisk_path);
1268 /* We are updating an existing on-disk symlink. */
1269 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1271 return got_error_from_errno2("readlink", ondisk_path);
1273 if (elen == target_len &&
1274 memcmp(etarget, target_path, target_len) == 0)
1275 return NULL; /* nothing to do */
1278 err = update_symlink(ondisk_path, target_path, target_len);
1279 if (fd != -1 && close(fd) == -1 && err == NULL)
1280 err = got_error_from_errno2("close", ondisk_path);
1284 static const struct got_error *
1285 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1286 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1288 const struct got_error *err = NULL;
1289 char canonpath[PATH_MAX];
1290 char *path_got = NULL;
1292 *is_bad_symlink = 0;
1294 if (target_len >= sizeof(canonpath)) {
1295 *is_bad_symlink = 1;
1300 * We do not use realpath(3) to resolve the symlink's target
1301 * path because we don't want to resolve symlinks recursively.
1302 * Instead we make the path absolute and then canonicalize it.
1303 * Relative symlink target lookup should begin at the directory
1304 * in which the blob object is being installed.
1306 if (!got_path_is_absolute(target_path)) {
1307 char *abspath, *parent;
1308 err = got_path_dirname(&parent, ondisk_path);
1311 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1313 return got_error_from_errno("asprintf");
1316 if (strlen(abspath) >= sizeof(canonpath)) {
1317 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1321 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1326 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1331 /* Only allow symlinks pointing at paths within the work tree. */
1332 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1333 *is_bad_symlink = 1;
1337 /* Do not allow symlinks pointing into the .got directory. */
1338 if (asprintf(&path_got, "%s/%s", wtroot_path,
1339 GOT_WORKTREE_GOT_DIR) == -1)
1340 return got_error_from_errno("asprintf");
1341 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1342 *is_bad_symlink = 1;
1348 static const struct got_error *
1349 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1350 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1351 int restoring_missing_file, int reverting_versioned_file,
1352 int path_is_unversioned, struct got_repository *repo,
1353 got_worktree_checkout_cb progress_cb, void *progress_arg)
1355 const struct got_error *err = NULL;
1356 char target_path[PATH_MAX];
1357 size_t len, target_len = 0;
1358 char *path_got = NULL;
1359 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1360 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1362 *is_bad_symlink = 0;
1365 * Blob object content specifies the target path of the link.
1366 * If a symbolic link cannot be installed we instead create
1367 * a regular file which contains the link target path stored
1368 * in the blob object.
1371 err = got_object_blob_read_block(&len, blob);
1372 if (len + target_len >= sizeof(target_path)) {
1373 /* Path too long; install as a regular file. */
1374 *is_bad_symlink = 1;
1375 got_object_blob_rewind(blob);
1376 return install_blob(worktree, ondisk_path, path,
1377 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1378 restoring_missing_file, reverting_versioned_file,
1379 1, path_is_unversioned, repo, progress_cb,
1383 /* Skip blob object header first time around. */
1384 memcpy(target_path + target_len, buf + hdrlen,
1386 target_len += len - hdrlen;
1390 target_path[target_len] = '\0';
1392 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1393 ondisk_path, worktree->root_path);
1397 if (*is_bad_symlink) {
1398 /* install as a regular file */
1399 *is_bad_symlink = 1;
1400 got_object_blob_rewind(blob);
1401 err = install_blob(worktree, ondisk_path, path,
1402 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1403 restoring_missing_file, reverting_versioned_file, 1,
1404 path_is_unversioned, repo, progress_cb, progress_arg);
1408 if (symlink(target_path, ondisk_path) == -1) {
1409 if (errno == EEXIST) {
1410 if (path_is_unversioned) {
1411 err = (*progress_cb)(progress_arg,
1412 GOT_STATUS_UNVERSIONED, path);
1415 err = replace_existing_symlink(ondisk_path,
1416 target_path, target_len);
1420 err = (*progress_cb)(progress_arg,
1421 reverting_versioned_file ?
1422 GOT_STATUS_REVERT : GOT_STATUS_UPDATE,
1425 goto done; /* Nothing else to do. */
1428 if (errno == ENOENT) {
1430 err = got_path_dirname(&parent, ondisk_path);
1433 err = add_dir_on_disk(worktree, parent);
1438 * Retry, and fall through to error handling
1439 * below if this second attempt fails.
1441 if (symlink(target_path, ondisk_path) != -1) {
1442 err = NULL; /* success */
1447 /* Handle errors from first or second creation attempt. */
1448 if (errno == ENAMETOOLONG) {
1449 /* bad target path; install as a regular file */
1450 *is_bad_symlink = 1;
1451 got_object_blob_rewind(blob);
1452 err = install_blob(worktree, ondisk_path, path,
1453 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1454 restoring_missing_file, reverting_versioned_file, 1,
1455 path_is_unversioned, repo,
1456 progress_cb, progress_arg);
1457 } else if (errno == ENOTDIR) {
1458 err = got_error_path(ondisk_path,
1459 GOT_ERR_FILE_OBSTRUCTED);
1461 err = got_error_from_errno3("symlink",
1462 target_path, ondisk_path);
1464 } else if (progress_cb)
1465 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1466 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1472 static const struct got_error *
1473 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1474 const char *path, mode_t te_mode, mode_t st_mode,
1475 struct got_blob_object *blob, int restoring_missing_file,
1476 int reverting_versioned_file, int installing_bad_symlink,
1477 int path_is_unversioned, struct got_repository *repo,
1478 got_worktree_checkout_cb progress_cb, void *progress_arg)
1480 const struct got_error *err = NULL;
1484 char *tmppath = NULL;
1486 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1487 GOT_DEFAULT_FILE_MODE);
1489 if (errno == ENOENT) {
1491 err = got_path_dirname(&parent, path);
1494 err = add_dir_on_disk(worktree, parent);
1498 fd = open(ondisk_path,
1499 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1500 GOT_DEFAULT_FILE_MODE);
1502 return got_error_from_errno2("open",
1504 } else if (errno == EEXIST) {
1505 if (path_is_unversioned) {
1506 err = (*progress_cb)(progress_arg,
1507 GOT_STATUS_UNVERSIONED, path);
1510 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1511 !S_ISREG(st_mode) && !installing_bad_symlink) {
1512 /* TODO file is obstructed; do something */
1513 err = got_error_path(ondisk_path,
1514 GOT_ERR_FILE_OBSTRUCTED);
1517 err = got_opentemp_named_fd(&tmppath, &fd,
1524 return got_error_from_errno2("open", ondisk_path);
1527 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1528 err = got_error_from_errno2("fchmod",
1529 update ? tmppath : ondisk_path);
1534 if (restoring_missing_file)
1535 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1537 else if (reverting_versioned_file)
1538 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1541 err = (*progress_cb)(progress_arg,
1542 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1547 hdrlen = got_object_blob_get_hdrlen(blob);
1549 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1550 err = got_object_blob_read_block(&len, blob);
1554 /* Skip blob object header first time around. */
1555 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1557 err = got_error_from_errno("write");
1559 } else if (outlen != len - hdrlen) {
1560 err = got_error(GOT_ERR_IO);
1567 if (fsync(fd) != 0) {
1568 err = got_error_from_errno("fsync");
1573 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1574 err = got_error_from_errno2("unlink", ondisk_path);
1577 if (rename(tmppath, ondisk_path) != 0) {
1578 err = got_error_from_errno3("rename", tmppath,
1587 if (fd != -1 && close(fd) == -1 && err == NULL)
1588 err = got_error_from_errno("close");
1589 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1590 err = got_error_from_errno2("unlink", tmppath);
1595 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1596 static const struct got_error *
1597 get_modified_file_content_status(unsigned char *status, FILE *f)
1599 const struct got_error *err = NULL;
1600 const char *markers[3] = {
1601 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1602 GOT_DIFF_CONFLICT_MARKER_SEP,
1603 GOT_DIFF_CONFLICT_MARKER_END
1607 size_t linesize = 0;
1610 while (*status == GOT_STATUS_MODIFY) {
1611 linelen = getline(&line, &linesize, f);
1612 if (linelen == -1) {
1615 err = got_ferror(f, GOT_ERR_IO);
1619 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1620 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1622 *status = GOT_STATUS_CONFLICT;
1633 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1635 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1636 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1640 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1642 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1643 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1644 ie->mtime_sec == sb->st_mtim.tv_sec &&
1645 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1646 ie->size == (sb->st_size & 0xffffffff) &&
1647 !xbit_differs(ie, sb->st_mode));
1650 static unsigned char
1651 get_staged_status(struct got_fileindex_entry *ie)
1653 switch (got_fileindex_entry_stage_get(ie)) {
1654 case GOT_FILEIDX_STAGE_ADD:
1655 return GOT_STATUS_ADD;
1656 case GOT_FILEIDX_STAGE_DELETE:
1657 return GOT_STATUS_DELETE;
1658 case GOT_FILEIDX_STAGE_MODIFY:
1659 return GOT_STATUS_MODIFY;
1661 return GOT_STATUS_NO_CHANGE;
1665 static const struct got_error *
1666 get_symlink_modification_status(unsigned char *status,
1667 struct got_fileindex_entry *ie, const char *abspath,
1668 int dirfd, const char *de_name, struct got_blob_object *blob)
1670 const struct got_error *err = NULL;
1671 char target_path[PATH_MAX];
1672 char etarget[PATH_MAX];
1674 size_t len, target_len = 0;
1675 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1676 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1678 *status = GOT_STATUS_NO_CHANGE;
1680 /* Blob object content specifies the target path of the link. */
1682 err = got_object_blob_read_block(&len, blob);
1685 if (len + target_len >= sizeof(target_path)) {
1687 * Should not happen. The blob contents were OK
1688 * when this symlink was installed.
1690 return got_error(GOT_ERR_NO_SPACE);
1693 /* Skip blob object header first time around. */
1694 memcpy(target_path + target_len, buf + hdrlen,
1696 target_len += len - hdrlen;
1700 target_path[target_len] = '\0';
1703 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1705 return got_error_from_errno2("readlinkat", abspath);
1707 elen = readlink(abspath, etarget, sizeof(etarget));
1709 return got_error_from_errno2("readlink", abspath);
1712 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1713 *status = GOT_STATUS_MODIFY;
1718 static const struct got_error *
1719 get_file_status(unsigned char *status, struct stat *sb,
1720 struct got_fileindex_entry *ie, const char *abspath,
1721 int dirfd, const char *de_name, struct got_repository *repo)
1723 const struct got_error *err = NULL;
1724 struct got_object_id id;
1729 struct got_blob_object *blob = NULL;
1731 unsigned char staged_status = get_staged_status(ie);
1733 *status = GOT_STATUS_NO_CHANGE;
1734 memset(sb, 0, sizeof(*sb));
1737 * Whenever the caller provides a directory descriptor and a
1738 * directory entry name for the file, use them! This prevents
1739 * race conditions if filesystem paths change beneath our feet.
1742 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1743 if (errno == ENOENT) {
1744 if (got_fileindex_entry_has_file_on_disk(ie))
1745 *status = GOT_STATUS_MISSING;
1747 *status = GOT_STATUS_DELETE;
1750 err = got_error_from_errno2("fstatat", abspath);
1754 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1755 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1756 return got_error_from_errno2("open", abspath);
1757 else if (fd == -1 && errno == ELOOP) {
1758 if (lstat(abspath, sb) == -1)
1759 return got_error_from_errno2("lstat", abspath);
1760 } else if (fd == -1 || fstat(fd, sb) == -1) {
1761 if (errno == ENOENT) {
1762 if (got_fileindex_entry_has_file_on_disk(ie))
1763 *status = GOT_STATUS_MISSING;
1765 *status = GOT_STATUS_DELETE;
1768 err = got_error_from_errno2("fstat", abspath);
1773 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1774 *status = GOT_STATUS_OBSTRUCTED;
1778 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1779 *status = GOT_STATUS_DELETE;
1781 } else if (!got_fileindex_entry_has_blob(ie) &&
1782 staged_status != GOT_STATUS_ADD) {
1783 *status = GOT_STATUS_ADD;
1787 if (!stat_info_differs(ie, sb))
1790 if (S_ISLNK(sb->st_mode) &&
1791 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1792 *status = GOT_STATUS_MODIFY;
1796 if (staged_status == GOT_STATUS_MODIFY ||
1797 staged_status == GOT_STATUS_ADD)
1798 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1800 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1802 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1806 if (S_ISLNK(sb->st_mode)) {
1807 err = get_symlink_modification_status(status, ie,
1808 abspath, dirfd, de_name, blob);
1813 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1815 err = got_error_from_errno2("openat", abspath);
1820 f = fdopen(fd, "r");
1822 err = got_error_from_errno2("fdopen", abspath);
1826 hdrlen = got_object_blob_get_hdrlen(blob);
1828 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1829 err = got_object_blob_read_block(&blen, blob);
1832 /* Skip length of blob object header first time around. */
1833 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1834 if (flen == 0 && ferror(f)) {
1835 err = got_error_from_errno("fread");
1838 if (blen - hdrlen == 0) {
1840 *status = GOT_STATUS_MODIFY;
1842 } else if (flen == 0) {
1843 if (blen - hdrlen != 0)
1844 *status = GOT_STATUS_MODIFY;
1846 } else if (blen - hdrlen == flen) {
1847 /* Skip blob object header first time around. */
1848 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1849 *status = GOT_STATUS_MODIFY;
1853 *status = GOT_STATUS_MODIFY;
1859 if (*status == GOT_STATUS_MODIFY) {
1861 err = get_modified_file_content_status(status, f);
1862 } else if (xbit_differs(ie, sb->st_mode))
1863 *status = GOT_STATUS_MODE_CHANGE;
1866 got_object_blob_close(blob);
1867 if (f != NULL && fclose(f) == EOF && err == NULL)
1868 err = got_error_from_errno2("fclose", abspath);
1869 if (fd != -1 && close(fd) == -1 && err == NULL)
1870 err = got_error_from_errno2("close", abspath);
1875 * Update timestamps in the file index if a file is unmodified and
1876 * we had to run a full content comparison to find out.
1878 static const struct got_error *
1879 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1880 struct got_fileindex_entry *ie, struct stat *sb)
1882 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1883 return got_fileindex_entry_update(ie, wt_fd, path,
1884 ie->blob_sha1, ie->commit_sha1, 1);
1889 static const struct got_error *
1890 update_blob(struct got_worktree *worktree,
1891 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1892 struct got_tree_entry *te, const char *path,
1893 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1896 const struct got_error *err = NULL;
1897 struct got_blob_object *blob = NULL;
1899 unsigned char status = GOT_STATUS_NO_CHANGE;
1902 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1903 return got_error_from_errno("asprintf");
1906 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1907 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1910 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1914 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1915 sb.st_mode = got_fileindex_perms_to_st(ie);
1917 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1918 status = GOT_STATUS_UNVERSIONED;
1921 if (status == GOT_STATUS_OBSTRUCTED) {
1922 err = (*progress_cb)(progress_arg, status, path);
1925 if (status == GOT_STATUS_CONFLICT) {
1926 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1931 if (ie && status != GOT_STATUS_MISSING &&
1932 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1933 if (got_fileindex_entry_has_commit(ie) &&
1934 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1935 SHA1_DIGEST_LENGTH) == 0) {
1936 err = sync_timestamps(worktree->root_fd,
1937 path, status, ie, &sb);
1940 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1944 if (got_fileindex_entry_has_blob(ie) &&
1945 memcmp(ie->blob_sha1, te->id.sha1,
1946 SHA1_DIGEST_LENGTH) == 0) {
1947 err = sync_timestamps(worktree->root_fd,
1948 path, status, ie, &sb);
1953 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1957 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1958 int update_timestamps;
1959 struct got_blob_object *blob2 = NULL;
1960 char *label_orig = NULL;
1961 if (got_fileindex_entry_has_blob(ie)) {
1962 struct got_object_id id2;
1963 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1964 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1968 if (got_fileindex_entry_has_commit(ie)) {
1969 char id_str[SHA1_DIGEST_STRING_LENGTH];
1970 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1971 sizeof(id_str)) == NULL) {
1972 err = got_error_path(id_str,
1973 GOT_ERR_BAD_OBJ_ID_STR);
1976 if (asprintf(&label_orig, "%s: commit %s",
1977 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1978 err = got_error_from_errno("asprintf");
1982 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
1984 err = got_object_blob_read_to_str(&link_target, blob);
1987 err = merge_symlink(worktree, blob2, ondisk_path, path,
1988 label_orig, link_target, worktree->base_commit_id,
1989 repo, progress_cb, progress_arg);
1992 err = merge_blob(&update_timestamps, worktree, blob2,
1993 ondisk_path, path, sb.st_mode, label_orig, blob,
1994 worktree->base_commit_id, repo,
1995 progress_cb, progress_arg);
1999 got_object_blob_close(blob2);
2003 * Do not update timestamps of files with local changes.
2004 * Otherwise, a future status walk would treat them as
2005 * unmodified files again.
2007 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2008 blob->id.sha1, worktree->base_commit_id->sha1,
2010 } else if (status == GOT_STATUS_MODE_CHANGE) {
2011 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2012 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2013 } else if (status == GOT_STATUS_DELETE) {
2014 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2017 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2018 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2022 int is_bad_symlink = 0;
2023 if (S_ISLNK(te->mode)) {
2024 err = install_symlink(&is_bad_symlink, worktree,
2025 ondisk_path, path, blob,
2026 status == GOT_STATUS_MISSING, 0,
2027 status == GOT_STATUS_UNVERSIONED, repo,
2028 progress_cb, progress_arg);
2030 err = install_blob(worktree, ondisk_path, path,
2031 te->mode, sb.st_mode, blob,
2032 status == GOT_STATUS_MISSING, 0, 0,
2033 status == GOT_STATUS_UNVERSIONED, repo,
2034 progress_cb, progress_arg);
2040 err = got_fileindex_entry_update(ie,
2041 worktree->root_fd, path, blob->id.sha1,
2042 worktree->base_commit_id->sha1, 1);
2044 err = create_fileindex_entry(&ie, fileindex,
2045 worktree->base_commit_id, worktree->root_fd, path,
2051 if (is_bad_symlink) {
2052 got_fileindex_entry_filetype_set(ie,
2053 GOT_FILEIDX_MODE_BAD_SYMLINK);
2056 got_object_blob_close(blob);
2062 static const struct got_error *
2063 remove_ondisk_file(const char *root_path, const char *path)
2065 const struct got_error *err = NULL;
2066 char *ondisk_path = NULL;
2068 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2069 return got_error_from_errno("asprintf");
2071 if (unlink(ondisk_path) == -1) {
2072 if (errno != ENOENT)
2073 err = got_error_from_errno2("unlink", ondisk_path);
2075 size_t root_len = strlen(root_path);
2078 err = got_path_dirname(&parent, ondisk_path);
2082 ondisk_path = parent;
2083 if (rmdir(ondisk_path) == -1) {
2084 if (errno != ENOTEMPTY)
2085 err = got_error_from_errno2("rmdir",
2089 } while (got_path_cmp(ondisk_path, root_path,
2090 strlen(ondisk_path), root_len) != 0);
2096 static const struct got_error *
2097 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2098 struct got_fileindex_entry *ie, struct got_repository *repo,
2099 got_worktree_checkout_cb progress_cb, void *progress_arg)
2101 const struct got_error *err = NULL;
2102 unsigned char status;
2106 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2107 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2109 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2111 return got_error_from_errno("asprintf");
2113 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2117 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2118 char ondisk_target[PATH_MAX];
2119 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2120 sizeof(ondisk_target));
2121 if (ondisk_len == -1) {
2122 err = got_error_from_errno2("readlink", ondisk_path);
2125 ondisk_target[ondisk_len] = '\0';
2126 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2127 NULL, NULL, /* XXX pass common ancestor info? */
2128 ondisk_target, ondisk_path);
2131 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2136 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2137 status == GOT_STATUS_ADD) {
2138 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2142 * Preserve the working file and change the deleted blob's
2143 * entry into a schedule-add entry.
2145 err = got_fileindex_entry_update(ie, worktree->root_fd,
2146 ie->path, NULL, NULL, 0);
2148 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2151 if (status == GOT_STATUS_NO_CHANGE) {
2152 err = remove_ondisk_file(worktree->root_path, ie->path);
2156 got_fileindex_entry_remove(fileindex, ie);
2163 struct diff_cb_arg {
2164 struct got_fileindex *fileindex;
2165 struct got_worktree *worktree;
2166 struct got_repository *repo;
2167 got_worktree_checkout_cb progress_cb;
2169 got_cancel_cb cancel_cb;
2173 static const struct got_error *
2174 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2175 struct got_tree_entry *te, const char *parent_path)
2177 struct diff_cb_arg *a = arg;
2179 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2180 return got_error(GOT_ERR_CANCELLED);
2182 return update_blob(a->worktree, a->fileindex, ie, te,
2183 ie->path, a->repo, a->progress_cb, a->progress_arg);
2186 static const struct got_error *
2187 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2189 struct diff_cb_arg *a = arg;
2191 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2192 return got_error(GOT_ERR_CANCELLED);
2194 return delete_blob(a->worktree, a->fileindex, ie,
2195 a->repo, a->progress_cb, a->progress_arg);
2198 static const struct got_error *
2199 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2201 struct diff_cb_arg *a = arg;
2202 const struct got_error *err;
2205 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2206 return got_error(GOT_ERR_CANCELLED);
2208 if (got_object_tree_entry_is_submodule(te))
2211 if (asprintf(&path, "%s%s%s", parent_path,
2212 parent_path[0] ? "/" : "", te->name)
2214 return got_error_from_errno("asprintf");
2216 if (S_ISDIR(te->mode))
2217 err = add_dir_on_disk(a->worktree, path);
2219 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2220 a->repo, a->progress_cb, a->progress_arg);
2226 const struct got_error *
2227 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2229 uint32_t uuid_status;
2231 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2232 if (uuid_status != uuid_s_ok) {
2234 return got_error_uuid(uuid_status, "uuid_to_string");
2240 static const struct got_error *
2241 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2243 const struct got_error *err = NULL;
2244 char *uuidstr = NULL;
2248 err = got_worktree_get_uuid(&uuidstr, worktree);
2252 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2253 err = got_error_from_errno("asprintf");
2260 const struct got_error *
2261 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2263 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2266 static const struct got_error *
2267 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2269 return get_ref_name(refname, worktree,
2270 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2273 static const struct got_error *
2274 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2276 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2279 static const struct got_error *
2280 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2282 return get_ref_name(refname, worktree,
2283 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2286 static const struct got_error *
2287 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2289 return get_ref_name(refname, worktree,
2290 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2293 static const struct got_error *
2294 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2296 return get_ref_name(refname, worktree,
2297 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2300 static const struct got_error *
2301 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2303 return get_ref_name(refname, worktree,
2304 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2307 static const struct got_error *
2308 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2310 return get_ref_name(refname, worktree,
2311 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2314 static const struct got_error *
2315 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2317 return get_ref_name(refname, worktree,
2318 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2321 const struct got_error *
2322 got_worktree_get_histedit_script_path(char **path,
2323 struct got_worktree *worktree)
2325 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2326 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2328 return got_error_from_errno("asprintf");
2334 * Prevent Git's garbage collector from deleting our base commit by
2335 * setting a reference to our base commit's ID.
2337 static const struct got_error *
2338 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2340 const struct got_error *err = NULL;
2341 struct got_reference *ref = NULL;
2344 err = got_worktree_get_base_ref_name(&refname, worktree);
2348 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2352 err = got_ref_write(ref, repo);
2360 static const struct got_error *
2361 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2363 const struct got_error *err = NULL;
2365 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2366 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2367 err = got_error_from_errno("asprintf");
2368 *fileindex_path = NULL;
2374 static const struct got_error *
2375 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2376 struct got_worktree *worktree)
2378 const struct got_error *err = NULL;
2381 *fileindex_path = NULL;
2382 *fileindex = got_fileindex_alloc();
2383 if (*fileindex == NULL)
2384 return got_error_from_errno("got_fileindex_alloc");
2386 err = get_fileindex_path(fileindex_path, worktree);
2390 index = fopen(*fileindex_path, "rb");
2391 if (index == NULL) {
2392 if (errno != ENOENT)
2393 err = got_error_from_errno2("fopen", *fileindex_path);
2395 err = got_fileindex_read(*fileindex, index);
2396 if (fclose(index) == EOF && err == NULL)
2397 err = got_error_from_errno("fclose");
2401 free(*fileindex_path);
2402 *fileindex_path = NULL;
2403 got_fileindex_free(*fileindex);
2409 struct bump_base_commit_id_arg {
2410 struct got_object_id *base_commit_id;
2413 const char *entry_name;
2414 got_worktree_checkout_cb progress_cb;
2418 /* Bump base commit ID of all files within an updated part of the work tree. */
2419 static const struct got_error *
2420 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2422 const struct got_error *err;
2423 struct bump_base_commit_id_arg *a = arg;
2425 if (a->entry_name) {
2426 if (strcmp(ie->path, a->path) != 0)
2428 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2431 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2432 SHA1_DIGEST_LENGTH) == 0)
2435 if (a->progress_cb) {
2436 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2441 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2445 static const struct got_error *
2446 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2447 struct got_fileindex *fileindex,
2448 got_worktree_checkout_cb progress_cb, void *progress_arg)
2450 struct bump_base_commit_id_arg bbc_arg;
2452 bbc_arg.base_commit_id = worktree->base_commit_id;
2453 bbc_arg.entry_name = NULL;
2455 bbc_arg.path_len = 0;
2456 bbc_arg.progress_cb = progress_cb;
2457 bbc_arg.progress_arg = progress_arg;
2459 return got_fileindex_for_each_entry_safe(fileindex,
2460 bump_base_commit_id, &bbc_arg);
2463 static const struct got_error *
2464 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2466 const struct got_error *err = NULL;
2467 char *new_fileindex_path = NULL;
2468 FILE *new_index = NULL;
2469 struct timespec timeout;
2471 err = got_opentemp_named(&new_fileindex_path, &new_index,
2476 err = got_fileindex_write(fileindex, new_index);
2480 if (rename(new_fileindex_path, fileindex_path) != 0) {
2481 err = got_error_from_errno3("rename", new_fileindex_path,
2483 unlink(new_fileindex_path);
2487 * Sleep for a short amount of time to ensure that files modified after
2488 * this program exits have a different time stamp from the one which
2489 * was recorded in the file index.
2492 timeout.tv_nsec = 1;
2493 nanosleep(&timeout, NULL);
2497 free(new_fileindex_path);
2501 static const struct got_error *
2502 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2503 struct got_object_id **tree_id, const char *wt_relpath,
2504 struct got_worktree *worktree, struct got_repository *repo)
2506 const struct got_error *err = NULL;
2507 struct got_object_id *id = NULL;
2508 char *in_repo_path = NULL;
2509 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2511 *entry_type = GOT_OBJ_TYPE_ANY;
2512 *tree_relpath = NULL;
2515 if (wt_relpath[0] == '\0') {
2516 /* Check out all files within the work tree. */
2517 *entry_type = GOT_OBJ_TYPE_TREE;
2518 *tree_relpath = strdup("");
2519 if (*tree_relpath == NULL) {
2520 err = got_error_from_errno("strdup");
2523 err = got_object_id_by_path(tree_id, repo,
2524 worktree->base_commit_id, worktree->path_prefix);
2530 /* Check out a subset of files in the work tree. */
2532 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2533 is_root_wt ? "" : "/", wt_relpath) == -1) {
2534 err = got_error_from_errno("asprintf");
2538 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2544 in_repo_path = NULL;
2546 err = got_object_get_type(entry_type, repo, id);
2550 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2551 /* Check out a single file. */
2552 if (strchr(wt_relpath, '/') == NULL) {
2553 /* Check out a single file in work tree's root dir. */
2554 in_repo_path = strdup(worktree->path_prefix);
2555 if (in_repo_path == NULL) {
2556 err = got_error_from_errno("strdup");
2559 *tree_relpath = strdup("");
2560 if (*tree_relpath == NULL) {
2561 err = got_error_from_errno("strdup");
2565 /* Check out a single file in a subdirectory. */
2566 err = got_path_dirname(tree_relpath, wt_relpath);
2569 if (asprintf(&in_repo_path, "%s%s%s",
2570 worktree->path_prefix, is_root_wt ? "" : "/",
2571 *tree_relpath) == -1) {
2572 err = got_error_from_errno("asprintf");
2576 err = got_object_id_by_path(tree_id, repo,
2577 worktree->base_commit_id, in_repo_path);
2579 /* Check out all files within a subdirectory. */
2580 *tree_id = got_object_id_dup(id);
2581 if (*tree_id == NULL) {
2582 err = got_error_from_errno("got_object_id_dup");
2585 *tree_relpath = strdup(wt_relpath);
2586 if (*tree_relpath == NULL) {
2587 err = got_error_from_errno("strdup");
2595 *entry_type = GOT_OBJ_TYPE_ANY;
2596 free(*tree_relpath);
2597 *tree_relpath = NULL;
2604 static const struct got_error *
2605 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2606 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2607 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2608 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2610 const struct got_error *err = NULL;
2611 struct got_commit_object *commit = NULL;
2612 struct got_tree_object *tree = NULL;
2613 struct got_fileindex_diff_tree_cb diff_cb;
2614 struct diff_cb_arg arg;
2616 err = ref_base_commit(worktree, repo);
2618 if (!(err->code == GOT_ERR_ERRNO &&
2619 (errno == EACCES || errno == EROFS)))
2621 err = (*progress_cb)(progress_arg,
2622 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2627 err = got_object_open_as_commit(&commit, repo,
2628 worktree->base_commit_id);
2632 err = got_object_open_as_tree(&tree, repo, tree_id);
2637 got_object_tree_find_entry(tree, entry_name) == NULL) {
2638 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2642 diff_cb.diff_old_new = diff_old_new;
2643 diff_cb.diff_old = diff_old;
2644 diff_cb.diff_new = diff_new;
2645 arg.fileindex = fileindex;
2646 arg.worktree = worktree;
2648 arg.progress_cb = progress_cb;
2649 arg.progress_arg = progress_arg;
2650 arg.cancel_cb = cancel_cb;
2651 arg.cancel_arg = cancel_arg;
2652 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2653 entry_name, repo, &diff_cb, &arg);
2656 got_object_tree_close(tree);
2658 got_object_commit_close(commit);
2662 const struct got_error *
2663 got_worktree_checkout_files(struct got_worktree *worktree,
2664 struct got_pathlist_head *paths, struct got_repository *repo,
2665 got_worktree_checkout_cb progress_cb, void *progress_arg,
2666 got_cancel_cb cancel_cb, void *cancel_arg)
2668 const struct got_error *err = NULL, *sync_err, *unlockerr;
2669 struct got_commit_object *commit = NULL;
2670 struct got_tree_object *tree = NULL;
2671 struct got_fileindex *fileindex = NULL;
2672 char *fileindex_path = NULL;
2673 struct got_pathlist_entry *pe;
2674 struct tree_path_data {
2675 SIMPLEQ_ENTRY(tree_path_data) entry;
2676 struct got_object_id *tree_id;
2681 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2683 SIMPLEQ_INIT(&tree_paths);
2685 err = lock_worktree(worktree, LOCK_EX);
2689 /* Map all specified paths to in-repository trees. */
2690 TAILQ_FOREACH(pe, paths, entry) {
2691 tpd = malloc(sizeof(*tpd));
2693 err = got_error_from_errno("malloc");
2697 err = find_tree_entry_for_checkout(&tpd->entry_type,
2698 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2704 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2705 err = got_path_basename(&tpd->entry_name, pe->path);
2713 tpd->entry_name = NULL;
2715 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2719 * Read the file index.
2720 * Checking out files is supposed to be an idempotent operation.
2721 * If the on-disk file index is incomplete we will try to complete it.
2723 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2727 tpd = SIMPLEQ_FIRST(&tree_paths);
2728 TAILQ_FOREACH(pe, paths, entry) {
2729 struct bump_base_commit_id_arg bbc_arg;
2731 err = checkout_files(worktree, fileindex, tpd->relpath,
2732 tpd->tree_id, tpd->entry_name, repo,
2733 progress_cb, progress_arg, cancel_cb, cancel_arg);
2737 bbc_arg.base_commit_id = worktree->base_commit_id;
2738 bbc_arg.entry_name = tpd->entry_name;
2739 bbc_arg.path = pe->path;
2740 bbc_arg.path_len = pe->path_len;
2741 bbc_arg.progress_cb = progress_cb;
2742 bbc_arg.progress_arg = progress_arg;
2743 err = got_fileindex_for_each_entry_safe(fileindex,
2744 bump_base_commit_id, &bbc_arg);
2748 tpd = SIMPLEQ_NEXT(tpd, entry);
2750 sync_err = sync_fileindex(fileindex, fileindex_path);
2751 if (sync_err && err == NULL)
2754 free(fileindex_path);
2756 got_object_tree_close(tree);
2758 got_object_commit_close(commit);
2760 got_fileindex_free(fileindex);
2761 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2762 tpd = SIMPLEQ_FIRST(&tree_paths);
2763 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2768 unlockerr = lock_worktree(worktree, LOCK_SH);
2769 if (unlockerr && err == NULL)
2774 struct merge_file_cb_arg {
2775 struct got_worktree *worktree;
2776 struct got_fileindex *fileindex;
2777 got_worktree_checkout_cb progress_cb;
2779 got_cancel_cb cancel_cb;
2781 const char *label_orig;
2782 struct got_object_id *commit_id2;
2785 static const struct got_error *
2786 merge_file_cb(void *arg, struct got_blob_object *blob1,
2787 struct got_blob_object *blob2, struct got_object_id *id1,
2788 struct got_object_id *id2, const char *path1, const char *path2,
2789 mode_t mode1, mode_t mode2, struct got_repository *repo)
2791 static const struct got_error *err = NULL;
2792 struct merge_file_cb_arg *a = arg;
2793 struct got_fileindex_entry *ie;
2794 char *ondisk_path = NULL;
2796 unsigned char status;
2797 int local_changes_subsumed;
2799 if (blob1 && blob2) {
2800 ie = got_fileindex_entry_get(a->fileindex, path2,
2803 return (*a->progress_cb)(a->progress_arg,
2804 GOT_STATUS_MISSING, path2);
2806 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2808 return got_error_from_errno("asprintf");
2810 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2815 if (status == GOT_STATUS_DELETE) {
2816 err = (*a->progress_cb)(a->progress_arg,
2817 GOT_STATUS_MERGE, path2);
2820 if (status != GOT_STATUS_NO_CHANGE &&
2821 status != GOT_STATUS_MODIFY &&
2822 status != GOT_STATUS_CONFLICT &&
2823 status != GOT_STATUS_ADD) {
2824 err = (*a->progress_cb)(a->progress_arg, status, path2);
2828 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2830 err = got_object_blob_read_to_str(&link_target2, blob2);
2833 err = merge_symlink(a->worktree, blob1, ondisk_path,
2834 path2, a->label_orig, link_target2, a->commit_id2,
2835 repo, a->progress_cb, a->progress_arg);
2838 err = merge_blob(&local_changes_subsumed, a->worktree,
2839 blob1, ondisk_path, path2, sb.st_mode,
2840 a->label_orig, blob2, a->commit_id2, repo,
2841 a->progress_cb, a->progress_arg);
2844 ie = got_fileindex_entry_get(a->fileindex, path1,
2847 return (*a->progress_cb)(a->progress_arg,
2848 GOT_STATUS_MISSING, path1);
2850 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2852 return got_error_from_errno("asprintf");
2854 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2860 case GOT_STATUS_NO_CHANGE:
2861 err = (*a->progress_cb)(a->progress_arg,
2862 GOT_STATUS_DELETE, path1);
2865 err = remove_ondisk_file(a->worktree->root_path, path1);
2869 got_fileindex_entry_mark_deleted_from_disk(ie);
2871 case GOT_STATUS_DELETE:
2872 case GOT_STATUS_MISSING:
2873 err = (*a->progress_cb)(a->progress_arg,
2874 GOT_STATUS_DELETE, path1);
2878 got_fileindex_entry_mark_deleted_from_disk(ie);
2880 case GOT_STATUS_ADD: {
2881 struct got_object_id *id;
2884 * Delete the added file only if its content already
2885 * exists in the repository.
2887 err = got_object_blob_file_create(&id, &blob1_f, path1);
2890 if (got_object_id_cmp(id, id1) == 0) {
2891 err = (*a->progress_cb)(a->progress_arg,
2892 GOT_STATUS_DELETE, path1);
2895 err = remove_ondisk_file(a->worktree->root_path,
2900 got_fileindex_entry_remove(a->fileindex,
2903 err = (*a->progress_cb)(a->progress_arg,
2904 GOT_STATUS_CANNOT_DELETE, path1);
2906 if (fclose(blob1_f) == EOF && err == NULL)
2907 err = got_error_from_errno("fclose");
2913 case GOT_STATUS_MODIFY:
2914 case GOT_STATUS_CONFLICT:
2915 err = (*a->progress_cb)(a->progress_arg,
2916 GOT_STATUS_CANNOT_DELETE, path1);
2920 case GOT_STATUS_OBSTRUCTED:
2921 err = (*a->progress_cb)(a->progress_arg, status, path1);
2929 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2931 return got_error_from_errno("asprintf");
2932 ie = got_fileindex_entry_get(a->fileindex, path2,
2935 err = get_file_status(&status, &sb, ie, ondisk_path,
2939 if (status != GOT_STATUS_NO_CHANGE &&
2940 status != GOT_STATUS_MODIFY &&
2941 status != GOT_STATUS_CONFLICT &&
2942 status != GOT_STATUS_ADD) {
2943 err = (*a->progress_cb)(a->progress_arg,
2947 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2949 err = got_object_blob_read_to_str(&link_target2,
2953 err = merge_symlink(a->worktree, NULL,
2954 ondisk_path, path2, a->label_orig,
2955 link_target2, a->commit_id2, repo,
2956 a->progress_cb, a->progress_arg);
2958 } else if (S_ISREG(sb.st_mode)) {
2959 err = merge_blob(&local_changes_subsumed,
2960 a->worktree, NULL, ondisk_path, path2,
2961 sb.st_mode, a->label_orig, blob2,
2962 a->commit_id2, repo, a->progress_cb,
2965 err = got_error_path(ondisk_path,
2966 GOT_ERR_FILE_OBSTRUCTED);
2970 if (status == GOT_STATUS_DELETE) {
2971 err = got_fileindex_entry_update(ie,
2972 a->worktree->root_fd, path2, blob2->id.sha1,
2973 a->worktree->base_commit_id->sha1, 0);
2978 int is_bad_symlink = 0;
2979 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2980 if (S_ISLNK(mode2)) {
2981 err = install_symlink(&is_bad_symlink,
2982 a->worktree, ondisk_path, path2, blob2, 0,
2983 0, 1, repo, a->progress_cb, a->progress_arg);
2985 err = install_blob(a->worktree, ondisk_path, path2,
2986 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
2987 a->progress_cb, a->progress_arg);
2991 err = got_fileindex_entry_alloc(&ie, path2);
2994 err = got_fileindex_entry_update(ie,
2995 a->worktree->root_fd, path2, NULL, NULL, 1);
2997 got_fileindex_entry_free(ie);
3000 err = got_fileindex_entry_add(a->fileindex, ie);
3002 got_fileindex_entry_free(ie);
3005 if (is_bad_symlink) {
3006 got_fileindex_entry_filetype_set(ie,
3007 GOT_FILEIDX_MODE_BAD_SYMLINK);
3016 struct check_merge_ok_arg {
3017 struct got_worktree *worktree;
3018 struct got_repository *repo;
3021 static const struct got_error *
3022 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3024 const struct got_error *err = NULL;
3025 struct check_merge_ok_arg *a = arg;
3026 unsigned char status;
3030 /* Reject merges into a work tree with mixed base commits. */
3031 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3032 SHA1_DIGEST_LENGTH))
3033 return got_error(GOT_ERR_MIXED_COMMITS);
3035 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3037 return got_error_from_errno("asprintf");
3039 /* Reject merges into a work tree with conflicted files. */
3040 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3043 if (status == GOT_STATUS_CONFLICT)
3044 return got_error(GOT_ERR_CONFLICTS);
3049 static const struct got_error *
3050 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3051 const char *fileindex_path, struct got_object_id *commit_id1,
3052 struct got_object_id *commit_id2, struct got_repository *repo,
3053 got_worktree_checkout_cb progress_cb, void *progress_arg,
3054 got_cancel_cb cancel_cb, void *cancel_arg)
3056 const struct got_error *err = NULL, *sync_err;
3057 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3058 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3059 struct merge_file_cb_arg arg;
3060 char *label_orig = NULL;
3063 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3064 worktree->path_prefix);
3065 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3071 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3075 err = got_object_id_str(&id_str, commit_id1);
3079 if (asprintf(&label_orig, "%s: commit %s",
3080 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3081 err = got_error_from_errno("asprintf");
3088 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3089 worktree->path_prefix);
3093 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3097 arg.worktree = worktree;
3098 arg.fileindex = fileindex;
3099 arg.progress_cb = progress_cb;
3100 arg.progress_arg = progress_arg;
3101 arg.cancel_cb = cancel_cb;
3102 arg.cancel_arg = cancel_arg;
3103 arg.label_orig = label_orig;
3104 arg.commit_id2 = commit_id2;
3105 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3106 sync_err = sync_fileindex(fileindex, fileindex_path);
3107 if (sync_err && err == NULL)
3111 got_object_tree_close(tree1);
3113 got_object_tree_close(tree2);
3118 const struct got_error *
3119 got_worktree_merge_files(struct got_worktree *worktree,
3120 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3121 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3122 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3124 const struct got_error *err, *unlockerr;
3125 char *fileindex_path = NULL;
3126 struct got_fileindex *fileindex = NULL;
3127 struct check_merge_ok_arg mok_arg;
3129 err = lock_worktree(worktree, LOCK_EX);
3133 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3137 mok_arg.worktree = worktree;
3138 mok_arg.repo = repo;
3139 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3144 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3145 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3148 got_fileindex_free(fileindex);
3149 free(fileindex_path);
3150 unlockerr = lock_worktree(worktree, LOCK_SH);
3151 if (unlockerr && err == NULL)
3156 struct diff_dir_cb_arg {
3157 struct got_fileindex *fileindex;
3158 struct got_worktree *worktree;
3159 const char *status_path;
3160 size_t status_path_len;
3161 struct got_repository *repo;
3162 got_worktree_status_cb status_cb;
3164 got_cancel_cb cancel_cb;
3166 /* A pathlist containing per-directory pathlists of ignore patterns. */
3167 struct got_pathlist_head ignores;
3168 int report_unchanged;
3172 static const struct got_error *
3173 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3174 int dirfd, const char *de_name,
3175 got_worktree_status_cb status_cb, void *status_arg,
3176 struct got_repository *repo, int report_unchanged)
3178 const struct got_error *err = NULL;
3179 unsigned char status = GOT_STATUS_NO_CHANGE;
3180 unsigned char staged_status = get_staged_status(ie);
3182 struct got_object_id blob_id, commit_id, staged_blob_id;
3183 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3184 struct got_object_id *staged_blob_idp = NULL;
3186 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3190 if (status == GOT_STATUS_NO_CHANGE &&
3191 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3194 if (got_fileindex_entry_has_blob(ie)) {
3195 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3196 blob_idp = &blob_id;
3198 if (got_fileindex_entry_has_commit(ie)) {
3199 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3200 commit_idp = &commit_id;
3202 if (staged_status == GOT_STATUS_ADD ||
3203 staged_status == GOT_STATUS_MODIFY) {
3204 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3205 SHA1_DIGEST_LENGTH);
3206 staged_blob_idp = &staged_blob_id;
3209 return (*status_cb)(status_arg, status, staged_status,
3210 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3213 static const struct got_error *
3214 status_old_new(void *arg, struct got_fileindex_entry *ie,
3215 struct dirent *de, const char *parent_path, int dirfd)
3217 const struct got_error *err = NULL;
3218 struct diff_dir_cb_arg *a = arg;
3221 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3222 return got_error(GOT_ERR_CANCELLED);
3224 if (got_path_cmp(parent_path, a->status_path,
3225 strlen(parent_path), a->status_path_len) != 0 &&
3226 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3229 if (parent_path[0]) {
3230 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3231 parent_path, de->d_name) == -1)
3232 return got_error_from_errno("asprintf");
3234 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3236 return got_error_from_errno("asprintf");
3239 err = report_file_status(ie, abspath, dirfd, de->d_name,
3240 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3245 static const struct got_error *
3246 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3248 struct diff_dir_cb_arg *a = arg;
3249 struct got_object_id blob_id, commit_id;
3250 unsigned char status;
3252 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3253 return got_error(GOT_ERR_CANCELLED);
3255 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3258 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3259 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3260 if (got_fileindex_entry_has_file_on_disk(ie))
3261 status = GOT_STATUS_MISSING;
3263 status = GOT_STATUS_DELETE;
3264 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3265 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3269 free_ignorelist(struct got_pathlist_head *ignorelist)
3271 struct got_pathlist_entry *pe;
3273 TAILQ_FOREACH(pe, ignorelist, entry)
3274 free((char *)pe->path);
3275 got_pathlist_free(ignorelist);
3279 free_ignores(struct got_pathlist_head *ignores)
3281 struct got_pathlist_entry *pe;
3283 TAILQ_FOREACH(pe, ignores, entry) {
3284 struct got_pathlist_head *ignorelist = pe->data;
3285 free_ignorelist(ignorelist);
3286 free((char *)pe->path);
3288 got_pathlist_free(ignores);
3291 static const struct got_error *
3292 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3294 const struct got_error *err = NULL;
3295 struct got_pathlist_entry *pe = NULL;
3296 struct got_pathlist_head *ignorelist;
3297 char *line = NULL, *pattern, *dirpath = NULL;
3298 size_t linesize = 0;
3301 ignorelist = calloc(1, sizeof(*ignorelist));
3302 if (ignorelist == NULL)
3303 return got_error_from_errno("calloc");
3304 TAILQ_INIT(ignorelist);
3306 while ((linelen = getline(&line, &linesize, f)) != -1) {
3307 if (linelen > 0 && line[linelen - 1] == '\n')
3308 line[linelen - 1] = '\0';
3310 /* Git's ignores may contain comments. */
3314 /* Git's negated patterns are not (yet?) supported. */
3318 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3320 err = got_error_from_errno("asprintf");
3323 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3328 err = got_error_from_errno("getline");
3332 dirpath = strdup(path);
3333 if (dirpath == NULL) {
3334 err = got_error_from_errno("strdup");
3337 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3340 if (err || pe == NULL) {
3342 free_ignorelist(ignorelist);
3348 match_ignores(struct got_pathlist_head *ignores, const char *path)
3350 struct got_pathlist_entry *pe;
3352 /* Handle patterns which match in all directories. */
3353 TAILQ_FOREACH(pe, ignores, entry) {
3354 struct got_pathlist_head *ignorelist = pe->data;
3355 struct got_pathlist_entry *pi;
3357 TAILQ_FOREACH(pi, ignorelist, entry) {
3358 const char *p, *pattern = pi->path;
3360 if (strncmp(pattern, "**/", 3) != 0)
3365 if (fnmatch(pattern, p,
3366 FNM_PATHNAME | FNM_LEADING_DIR)) {
3367 /* Retry in next directory. */
3368 while (*p && *p != '/')
3380 * The ignores pathlist contains ignore lists from children before
3381 * parents, so we can find the most specific ignorelist by walking
3382 * ignores backwards.
3384 pe = TAILQ_LAST(ignores, got_pathlist_head);
3386 if (got_path_is_child(path, pe->path, pe->path_len)) {
3387 struct got_pathlist_head *ignorelist = pe->data;
3388 struct got_pathlist_entry *pi;
3389 TAILQ_FOREACH(pi, ignorelist, entry) {
3390 const char *pattern = pi->path;
3391 int flags = FNM_LEADING_DIR;
3392 if (strstr(pattern, "/**/") == NULL)
3393 flags |= FNM_PATHNAME;
3394 if (fnmatch(pattern, path, flags))
3399 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3405 static const struct got_error *
3406 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3407 const char *path, int dirfd, const char *ignores_filename)
3409 const struct got_error *err = NULL;
3412 FILE *ignoresfile = NULL;
3414 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3415 path[0] ? "/" : "", ignores_filename) == -1)
3416 return got_error_from_errno("asprintf");
3419 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3421 if (errno != ENOENT && errno != EACCES)
3422 err = got_error_from_errno2("openat",
3425 ignoresfile = fdopen(fd, "r");
3426 if (ignoresfile == NULL)
3427 err = got_error_from_errno2("fdopen",
3431 err = read_ignores(ignores, path, ignoresfile);
3435 ignoresfile = fopen(ignorespath, "r");
3436 if (ignoresfile == NULL) {
3437 if (errno != ENOENT && errno != EACCES)
3438 err = got_error_from_errno2("fopen",
3441 err = read_ignores(ignores, path, ignoresfile);
3444 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3445 err = got_error_from_errno2("fclose", path);
3446 if (fd != -1 && close(fd) == -1 && err == NULL)
3447 err = got_error_from_errno2("close", path);
3452 static const struct got_error *
3453 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3455 const struct got_error *err = NULL;
3456 struct diff_dir_cb_arg *a = arg;
3459 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3460 return got_error(GOT_ERR_CANCELLED);
3462 if (parent_path[0]) {
3463 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3464 return got_error_from_errno("asprintf");
3469 if (de->d_type != DT_DIR &&
3470 got_path_is_child(path, a->status_path, a->status_path_len)
3471 && !match_ignores(&a->ignores, path))
3472 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3473 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3479 static const struct got_error *
3480 status_traverse(void *arg, const char *path, int dirfd)
3482 const struct got_error *err = NULL;
3483 struct diff_dir_cb_arg *a = arg;
3488 err = add_ignores(&a->ignores, a->worktree->root_path,
3489 path, dirfd, ".cvsignore");
3493 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3494 dirfd, ".gitignore");
3499 static const struct got_error *
3500 report_single_file_status(const char *path, const char *ondisk_path,
3501 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3502 void *status_arg, struct got_repository *repo, int report_unchanged)
3504 struct got_fileindex_entry *ie;
3507 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3509 return report_file_status(ie, ondisk_path, -1, NULL,
3510 status_cb, status_arg, repo, report_unchanged);
3512 if (lstat(ondisk_path, &sb) == -1) {
3513 if (errno != ENOENT)
3514 return got_error_from_errno2("lstat", ondisk_path);
3515 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3516 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3520 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3521 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3522 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3527 static const struct got_error *
3528 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3529 const char *root_path, const char *path)
3531 const struct got_error *err;
3532 char *parent_path, *next_parent_path = NULL;
3534 err = add_ignores(ignores, root_path, "", -1,
3539 err = add_ignores(ignores, root_path, "", -1,
3544 err = got_path_dirname(&parent_path, path);
3546 if (err->code == GOT_ERR_BAD_PATH)
3547 return NULL; /* cannot traverse parent */
3551 err = add_ignores(ignores, root_path, parent_path, -1,
3555 err = add_ignores(ignores, root_path, parent_path, -1,
3559 err = got_path_dirname(&next_parent_path, parent_path);
3561 if (err->code == GOT_ERR_BAD_PATH)
3562 err = NULL; /* traversed everything */
3566 parent_path = next_parent_path;
3567 next_parent_path = NULL;
3571 free(next_parent_path);
3575 static const struct got_error *
3576 worktree_status(struct got_worktree *worktree, const char *path,
3577 struct got_fileindex *fileindex, struct got_repository *repo,
3578 got_worktree_status_cb status_cb, void *status_arg,
3579 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3580 int report_unchanged)
3582 const struct got_error *err = NULL;
3584 struct got_fileindex_diff_dir_cb fdiff_cb;
3585 struct diff_dir_cb_arg arg;
3586 char *ondisk_path = NULL;
3588 TAILQ_INIT(&arg.ignores);
3590 if (asprintf(&ondisk_path, "%s%s%s",
3591 worktree->root_path, path[0] ? "/" : "", path) == -1)
3592 return got_error_from_errno("asprintf");
3594 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3596 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3598 err = got_error_from_errno2("open", ondisk_path);
3600 err = report_single_file_status(path, ondisk_path,
3601 fileindex, status_cb, status_arg, repo,
3604 fdiff_cb.diff_old_new = status_old_new;
3605 fdiff_cb.diff_old = status_old;
3606 fdiff_cb.diff_new = status_new;
3607 fdiff_cb.diff_traverse = status_traverse;
3608 arg.fileindex = fileindex;
3609 arg.worktree = worktree;
3610 arg.status_path = path;
3611 arg.status_path_len = strlen(path);
3613 arg.status_cb = status_cb;
3614 arg.status_arg = status_arg;
3615 arg.cancel_cb = cancel_cb;
3616 arg.cancel_arg = cancel_arg;
3617 arg.report_unchanged = report_unchanged;
3618 arg.no_ignores = no_ignores;
3620 err = add_ignores_from_parent_paths(&arg.ignores,
3621 worktree->root_path, path);
3625 err = got_fileindex_diff_dir(fileindex, fd,
3626 worktree->root_path, path, repo, &fdiff_cb, &arg);
3629 free_ignores(&arg.ignores);
3630 if (fd != -1 && close(fd) == -1 && err == NULL)
3631 err = got_error_from_errno("close");
3636 const struct got_error *
3637 got_worktree_status(struct got_worktree *worktree,
3638 struct got_pathlist_head *paths, struct got_repository *repo,
3639 got_worktree_status_cb status_cb, void *status_arg,
3640 got_cancel_cb cancel_cb, void *cancel_arg)
3642 const struct got_error *err = NULL;
3643 char *fileindex_path = NULL;
3644 struct got_fileindex *fileindex = NULL;
3645 struct got_pathlist_entry *pe;
3647 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3651 TAILQ_FOREACH(pe, paths, entry) {
3652 err = worktree_status(worktree, pe->path, fileindex, repo,
3653 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3657 free(fileindex_path);
3658 got_fileindex_free(fileindex);
3662 const struct got_error *
3663 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3666 const struct got_error *err = NULL;
3667 char *resolved = NULL, *cwd = NULL, *path = NULL;
3670 char *abspath = NULL;
3671 char canonpath[PATH_MAX];
3675 cwd = getcwd(NULL, 0);
3677 return got_error_from_errno("getcwd");
3679 if (lstat(arg, &sb) == -1) {
3680 if (errno != ENOENT) {
3681 err = got_error_from_errno2("lstat", arg);
3686 if (S_ISLNK(sb.st_mode)) {
3688 * We cannot use realpath(3) with symlinks since we want to
3689 * operate on the symlink itself.
3690 * But we can make the path absolute, assuming it is relative
3691 * to the current working directory, and then canonicalize it.
3693 if (!got_path_is_absolute(arg)) {
3694 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3695 err = got_error_from_errno("asprintf");
3700 err = got_canonpath(abspath ? abspath : arg, canonpath,
3704 resolved = strdup(canonpath);
3705 if (resolved == NULL) {
3706 err = got_error_from_errno("strdup");
3710 resolved = realpath(arg, NULL);
3711 if (resolved == NULL) {
3712 if (errno != ENOENT) {
3713 err = got_error_from_errno2("realpath", arg);
3716 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3717 err = got_error_from_errno("asprintf");
3720 err = got_canonpath(abspath, canonpath,
3724 resolved = strdup(canonpath);
3725 if (resolved == NULL) {
3726 err = got_error_from_errno("strdup");
3732 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3733 strlen(got_worktree_get_root_path(worktree)))) {
3734 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3738 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3739 err = got_path_skip_common_ancestor(&path,
3740 got_worktree_get_root_path(worktree), resolved);
3746 err = got_error_from_errno("strdup");
3751 /* XXX status walk can't deal with trailing slash! */
3753 while (len > 0 && path[len - 1] == '/') {
3754 path[len - 1] = '\0';
3768 struct schedule_addition_args {
3769 struct got_worktree *worktree;
3770 struct got_fileindex *fileindex;
3771 got_worktree_checkout_cb progress_cb;
3773 struct got_repository *repo;
3776 static const struct got_error *
3777 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3778 const char *relpath, struct got_object_id *blob_id,
3779 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3780 int dirfd, const char *de_name)
3782 struct schedule_addition_args *a = arg;
3783 const struct got_error *err = NULL;
3784 struct got_fileindex_entry *ie;
3788 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3790 return got_error_from_errno("asprintf");
3792 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3794 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3798 /* Re-adding an existing entry is a no-op. */
3799 if (status == GOT_STATUS_ADD)
3801 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3806 if (status != GOT_STATUS_UNVERSIONED) {
3807 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3811 err = got_fileindex_entry_alloc(&ie, relpath);
3814 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3815 relpath, NULL, NULL, 1);
3817 got_fileindex_entry_free(ie);
3820 err = got_fileindex_entry_add(a->fileindex, ie);
3822 got_fileindex_entry_free(ie);
3829 if (status == GOT_STATUS_ADD)
3831 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3834 const struct got_error *
3835 got_worktree_schedule_add(struct got_worktree *worktree,
3836 struct got_pathlist_head *paths,
3837 got_worktree_checkout_cb progress_cb, void *progress_arg,
3838 struct got_repository *repo, int no_ignores)
3840 struct got_fileindex *fileindex = NULL;
3841 char *fileindex_path = NULL;
3842 const struct got_error *err = NULL, *sync_err, *unlockerr;
3843 struct got_pathlist_entry *pe;
3844 struct schedule_addition_args saa;
3846 err = lock_worktree(worktree, LOCK_EX);
3850 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3854 saa.worktree = worktree;
3855 saa.fileindex = fileindex;
3856 saa.progress_cb = progress_cb;
3857 saa.progress_arg = progress_arg;
3860 TAILQ_FOREACH(pe, paths, entry) {
3861 err = worktree_status(worktree, pe->path, fileindex, repo,
3862 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3866 sync_err = sync_fileindex(fileindex, fileindex_path);
3867 if (sync_err && err == NULL)
3870 free(fileindex_path);
3872 got_fileindex_free(fileindex);
3873 unlockerr = lock_worktree(worktree, LOCK_SH);
3874 if (unlockerr && err == NULL)
3879 struct schedule_deletion_args {
3880 struct got_worktree *worktree;
3881 struct got_fileindex *fileindex;
3882 got_worktree_delete_cb progress_cb;
3884 struct got_repository *repo;
3885 int delete_local_mods;
3887 const char *status_codes;
3890 static const struct got_error *
3891 schedule_for_deletion(void *arg, unsigned char status,
3892 unsigned char staged_status, const char *relpath,
3893 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3894 struct got_object_id *commit_id, int dirfd, const char *de_name)
3896 struct schedule_deletion_args *a = arg;
3897 const struct got_error *err = NULL;
3898 struct got_fileindex_entry *ie = NULL;
3902 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3904 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3906 staged_status = get_staged_status(ie);
3907 if (staged_status != GOT_STATUS_NO_CHANGE) {
3908 if (staged_status == GOT_STATUS_DELETE)
3910 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3913 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3915 return got_error_from_errno("asprintf");
3917 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3922 if (a->status_codes) {
3923 size_t ncodes = strlen(a->status_codes);
3925 for (i = 0; i < ncodes ; i++) {
3926 if (status == a->status_codes[i])
3930 /* Do not delete files in non-matching status. */
3934 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3935 a->status_codes[i] != GOT_STATUS_MISSING) {
3936 static char msg[64];
3937 snprintf(msg, sizeof(msg),
3938 "invalid status code '%c'", a->status_codes[i]);
3939 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3944 if (status != GOT_STATUS_NO_CHANGE) {
3945 if (status == GOT_STATUS_DELETE)
3947 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3948 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3951 if (status != GOT_STATUS_MODIFY &&
3952 status != GOT_STATUS_MISSING) {
3953 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3958 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3962 if (unlinkat(dirfd, de_name, 0) != 0) {
3963 err = got_error_from_errno2("unlinkat",
3967 } else if (unlink(ondisk_path) != 0) {
3968 err = got_error_from_errno2("unlink", ondisk_path);
3972 root_len = strlen(a->worktree->root_path);
3975 err = got_path_dirname(&parent, ondisk_path);
3979 ondisk_path = parent;
3980 if (rmdir(ondisk_path) == -1) {
3981 if (errno != ENOTEMPTY)
3982 err = got_error_from_errno2("rmdir",
3986 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
3987 strlen(ondisk_path), root_len) != 0);
3990 got_fileindex_entry_mark_deleted_from_disk(ie);
3995 if (status == GOT_STATUS_DELETE)
3997 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3998 staged_status, relpath);
4001 const struct got_error *
4002 got_worktree_schedule_delete(struct got_worktree *worktree,
4003 struct got_pathlist_head *paths, int delete_local_mods,
4004 const char *status_codes,
4005 got_worktree_delete_cb progress_cb, void *progress_arg,
4006 struct got_repository *repo, int keep_on_disk)
4008 struct got_fileindex *fileindex = NULL;
4009 char *fileindex_path = NULL;
4010 const struct got_error *err = NULL, *sync_err, *unlockerr;
4011 struct got_pathlist_entry *pe;
4012 struct schedule_deletion_args sda;
4014 err = lock_worktree(worktree, LOCK_EX);
4018 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4022 sda.worktree = worktree;
4023 sda.fileindex = fileindex;
4024 sda.progress_cb = progress_cb;
4025 sda.progress_arg = progress_arg;
4027 sda.delete_local_mods = delete_local_mods;
4028 sda.keep_on_disk = keep_on_disk;
4029 sda.status_codes = status_codes;
4031 TAILQ_FOREACH(pe, paths, entry) {
4032 err = worktree_status(worktree, pe->path, fileindex, repo,
4033 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4037 sync_err = sync_fileindex(fileindex, fileindex_path);
4038 if (sync_err && err == NULL)
4041 free(fileindex_path);
4043 got_fileindex_free(fileindex);
4044 unlockerr = lock_worktree(worktree, LOCK_SH);
4045 if (unlockerr && err == NULL)
4050 static const struct got_error *
4051 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4053 const struct got_error *err = NULL;
4055 size_t linesize = 0, n;
4058 linelen = getline(&line, &linesize, infile);
4059 if (linelen == -1) {
4060 if (ferror(infile)) {
4061 err = got_error_from_errno("getline");
4067 n = fwrite(line, 1, linelen, outfile);
4069 err = got_ferror(outfile, GOT_ERR_IO);
4074 n = fwrite(line, 1, linelen, rejectfile);
4076 err = got_ferror(outfile, GOT_ERR_IO);
4083 static const struct got_error *
4084 skip_one_line(FILE *f)
4087 size_t linesize = 0;
4090 linelen = getline(&line, &linesize, f);
4091 if (linelen == -1) {
4093 return got_error_from_errno("getline");
4100 static const struct got_error *
4101 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4102 int start_old, int end_old, int start_new, int end_new,
4103 FILE *outfile, FILE *rejectfile)
4105 const struct got_error *err;
4107 /* Copy old file's lines leading up to patch. */
4108 while (!feof(f1) && *line_cur1 < start_old) {
4109 err = copy_one_line(f1, outfile, NULL);
4114 /* Skip new file's lines leading up to patch. */
4115 while (!feof(f2) && *line_cur2 < start_new) {
4117 err = copy_one_line(f2, NULL, rejectfile);
4119 err = skip_one_line(f2);
4124 /* Copy patched lines. */
4125 while (!feof(f2) && *line_cur2 <= end_new) {
4126 err = copy_one_line(f2, outfile, NULL);
4131 /* Skip over old file's replaced lines. */
4132 while (!feof(f1) && *line_cur1 <= end_old) {
4134 err = copy_one_line(f1, NULL, rejectfile);
4136 err = skip_one_line(f1);
4145 static const struct got_error *
4146 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4147 FILE *outfile, FILE *rejectfile)
4149 const struct got_error *err;
4152 /* Copy old file's lines until EOF. */
4154 err = copy_one_line(f1, outfile, NULL);
4161 /* Copy new file's lines until EOF. */
4163 err = copy_one_line(f2, NULL, rejectfile);
4173 static const struct got_error *
4174 apply_or_reject_change(int *choice, int *nchunks_used,
4175 struct diff_result *diff_result, int n,
4176 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4177 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4178 got_worktree_patch_cb patch_cb, void *patch_arg)
4180 const struct got_error *err = NULL;
4181 struct diff_chunk_context cc = {};
4182 int start_old, end_old, start_new, end_new;
4184 struct diff_output_unidiff_state *diff_state;
4185 struct diff_input_info diff_info;
4188 *choice = GOT_PATCH_CHOICE_NONE;
4190 /* Get changed line numbers without context lines for copy_change(). */
4191 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4192 start_old = cc.left.start;
4193 end_old = cc.left.end;
4194 start_new = cc.right.start;
4195 end_new = cc.right.end;
4197 /* Get the same change with context lines for display. */
4198 memset(&cc, 0, sizeof(cc));
4199 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4201 memset(&diff_info, 0, sizeof(diff_info));
4202 diff_info.left_path = relpath;
4203 diff_info.right_path = relpath;
4205 diff_state = diff_output_unidiff_state_alloc();
4206 if (diff_state == NULL)
4207 return got_error_set_errno(ENOMEM,
4208 "diff_output_unidiff_state_alloc");
4210 hunkfile = got_opentemp();
4211 if (hunkfile == NULL) {
4212 err = got_error_from_errno("got_opentemp");
4216 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4218 if (rc != DIFF_RC_OK) {
4219 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4223 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4224 err = got_ferror(hunkfile, GOT_ERR_IO);
4228 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4229 hunkfile, changeno, nchanges);
4234 case GOT_PATCH_CHOICE_YES:
4235 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4236 end_old, start_new, end_new, outfile, rejectfile);
4238 case GOT_PATCH_CHOICE_NO:
4239 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4240 end_old, start_new, end_new, rejectfile, outfile);
4242 case GOT_PATCH_CHOICE_QUIT:
4245 err = got_error(GOT_ERR_PATCH_CHOICE);
4249 diff_output_unidiff_state_free(diff_state);
4250 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4251 err = got_error_from_errno("fclose");
4255 struct revert_file_args {
4256 struct got_worktree *worktree;
4257 struct got_fileindex *fileindex;
4258 got_worktree_checkout_cb progress_cb;
4260 got_worktree_patch_cb patch_cb;
4262 struct got_repository *repo;
4265 static const struct got_error *
4266 create_patched_content(char **path_outfile, int reverse_patch,
4267 struct got_object_id *blob_id, const char *path2,
4268 int dirfd2, const char *de_name2,
4269 const char *relpath, struct got_repository *repo,
4270 got_worktree_patch_cb patch_cb, void *patch_arg)
4272 const struct got_error *err, *free_err;
4273 struct got_blob_object *blob = NULL;
4274 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4276 char link_target[PATH_MAX];
4277 ssize_t link_len = 0;
4278 char *path1 = NULL, *id_str = NULL;
4280 struct got_diffreg_result *diffreg_result = NULL;
4281 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4282 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4284 *path_outfile = NULL;
4286 err = got_object_id_str(&id_str, blob_id);
4291 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4293 if (errno != ELOOP) {
4294 err = got_error_from_errno2("openat", path2);
4297 link_len = readlinkat(dirfd2, de_name2,
4298 link_target, sizeof(link_target));
4300 return got_error_from_errno2("readlinkat", path2);
4301 sb2.st_mode = S_IFLNK;
4302 sb2.st_size = link_len;
4305 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4307 if (errno != ELOOP) {
4308 err = got_error_from_errno2("open", path2);
4311 link_len = readlink(path2, link_target,
4312 sizeof(link_target));
4314 return got_error_from_errno2("readlink", path2);
4315 sb2.st_mode = S_IFLNK;
4316 sb2.st_size = link_len;
4320 if (fstat(fd2, &sb2) == -1) {
4321 err = got_error_from_errno2("fstat", path2);
4325 f2 = fdopen(fd2, "r");
4327 err = got_error_from_errno2("fdopen", path2);
4333 f2 = got_opentemp();
4335 err = got_error_from_errno2("got_opentemp", path2);
4338 n = fwrite(link_target, 1, link_len, f2);
4339 if (n != link_len) {
4340 err = got_ferror(f2, GOT_ERR_IO);
4343 if (fflush(f2) == EOF) {
4344 err = got_error_from_errno("fflush");
4350 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4354 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4358 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4362 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4367 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4371 if (fseek(f1, 0L, SEEK_SET) == -1)
4372 return got_ferror(f1, GOT_ERR_IO);
4373 if (fseek(f2, 0L, SEEK_SET) == -1)
4374 return got_ferror(f2, GOT_ERR_IO);
4376 /* Count the number of actual changes in the diff result. */
4377 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4378 struct diff_chunk_context cc = {};
4379 diff_chunk_context_load_change(&cc, &nchunks_used,
4380 diffreg_result->result, n, 0);
4383 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4385 err = apply_or_reject_change(&choice, &nchunks_used,
4386 diffreg_result->result, n, relpath, f1, f2,
4387 &line_cur1, &line_cur2,
4388 reverse_patch ? NULL : outfile,
4389 reverse_patch ? outfile : NULL,
4390 ++i, nchanges, patch_cb, patch_arg);
4393 if (choice == GOT_PATCH_CHOICE_YES)
4395 else if (choice == GOT_PATCH_CHOICE_QUIT)
4399 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4400 reverse_patch ? NULL : outfile,
4401 reverse_patch ? outfile : NULL);
4405 if (!S_ISLNK(sb2.st_mode)) {
4406 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4407 err = got_error_from_errno2("fchmod", path2);
4415 got_object_blob_close(blob);
4416 free_err = got_diffreg_result_free(diffreg_result);
4419 if (f1 && fclose(f1) == EOF && err == NULL)
4420 err = got_error_from_errno2("fclose", path1);
4421 if (f2 && fclose(f2) == EOF && err == NULL)
4422 err = got_error_from_errno2("fclose", path2);
4423 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4424 err = got_error_from_errno2("close", path2);
4425 if (outfile && fclose(outfile) == EOF && err == NULL)
4426 err = got_error_from_errno2("fclose", *path_outfile);
4427 if (path1 && unlink(path1) == -1 && err == NULL)
4428 err = got_error_from_errno2("unlink", path1);
4429 if (err || !have_content) {
4430 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4431 err = got_error_from_errno2("unlink", *path_outfile);
4432 free(*path_outfile);
4433 *path_outfile = NULL;
4439 static const struct got_error *
4440 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4441 const char *relpath, struct got_object_id *blob_id,
4442 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4443 int dirfd, const char *de_name)
4445 struct revert_file_args *a = arg;
4446 const struct got_error *err = NULL;
4447 char *parent_path = NULL;
4448 struct got_fileindex_entry *ie;
4449 struct got_tree_object *tree = NULL;
4450 struct got_object_id *tree_id = NULL;
4451 const struct got_tree_entry *te = NULL;
4452 char *tree_path = NULL, *te_name;
4453 char *ondisk_path = NULL, *path_content = NULL;
4454 struct got_blob_object *blob = NULL;
4456 /* Reverting a staged deletion is a no-op. */
4457 if (status == GOT_STATUS_DELETE &&
4458 staged_status != GOT_STATUS_NO_CHANGE)
4461 if (status == GOT_STATUS_UNVERSIONED)
4462 return (*a->progress_cb)(a->progress_arg,
4463 GOT_STATUS_UNVERSIONED, relpath);
4465 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4467 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4469 /* Construct in-repository path of tree which contains this blob. */
4470 err = got_path_dirname(&parent_path, ie->path);
4472 if (err->code != GOT_ERR_BAD_PATH)
4474 parent_path = strdup("/");
4475 if (parent_path == NULL) {
4476 err = got_error_from_errno("strdup");
4480 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4481 tree_path = strdup(parent_path);
4482 if (tree_path == NULL) {
4483 err = got_error_from_errno("strdup");
4487 if (got_path_is_root_dir(parent_path)) {
4488 tree_path = strdup(a->worktree->path_prefix);
4489 if (tree_path == NULL) {
4490 err = got_error_from_errno("strdup");
4494 if (asprintf(&tree_path, "%s/%s",
4495 a->worktree->path_prefix, parent_path) == -1) {
4496 err = got_error_from_errno("asprintf");
4502 err = got_object_id_by_path(&tree_id, a->repo,
4503 a->worktree->base_commit_id, tree_path);
4505 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4506 (status == GOT_STATUS_ADD ||
4507 staged_status == GOT_STATUS_ADD)))
4510 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4514 err = got_path_basename(&te_name, ie->path);
4518 te = got_object_tree_find_entry(tree, te_name);
4520 if (te == NULL && status != GOT_STATUS_ADD &&
4521 staged_status != GOT_STATUS_ADD) {
4522 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4528 case GOT_STATUS_ADD:
4530 int choice = GOT_PATCH_CHOICE_NONE;
4531 err = (*a->patch_cb)(&choice, a->patch_arg,
4532 status, ie->path, NULL, 1, 1);
4535 if (choice != GOT_PATCH_CHOICE_YES)
4538 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4542 got_fileindex_entry_remove(a->fileindex, ie);
4544 case GOT_STATUS_DELETE:
4546 int choice = GOT_PATCH_CHOICE_NONE;
4547 err = (*a->patch_cb)(&choice, a->patch_arg,
4548 status, ie->path, NULL, 1, 1);
4551 if (choice != GOT_PATCH_CHOICE_YES)
4555 case GOT_STATUS_MODIFY:
4556 case GOT_STATUS_MODE_CHANGE:
4557 case GOT_STATUS_CONFLICT:
4558 case GOT_STATUS_MISSING: {
4559 struct got_object_id id;
4560 if (staged_status == GOT_STATUS_ADD ||
4561 staged_status == GOT_STATUS_MODIFY) {
4562 memcpy(id.sha1, ie->staged_blob_sha1,
4563 SHA1_DIGEST_LENGTH);
4565 memcpy(id.sha1, ie->blob_sha1,
4566 SHA1_DIGEST_LENGTH);
4567 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4571 if (asprintf(&ondisk_path, "%s/%s",
4572 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4573 err = got_error_from_errno("asprintf");
4577 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4578 status == GOT_STATUS_CONFLICT)) {
4579 int is_bad_symlink = 0;
4580 err = create_patched_content(&path_content, 1, &id,
4581 ondisk_path, dirfd, de_name, ie->path, a->repo,
4582 a->patch_cb, a->patch_arg);
4583 if (err || path_content == NULL)
4585 if (te && S_ISLNK(te->mode)) {
4586 if (unlink(path_content) == -1) {
4587 err = got_error_from_errno2("unlink",
4591 err = install_symlink(&is_bad_symlink,
4592 a->worktree, ondisk_path, ie->path,
4593 blob, 0, 1, 0, a->repo,
4594 a->progress_cb, a->progress_arg);
4596 if (rename(path_content, ondisk_path) == -1) {
4597 err = got_error_from_errno3("rename",
4598 path_content, ondisk_path);
4603 int is_bad_symlink = 0;
4604 if (te && S_ISLNK(te->mode)) {
4605 err = install_symlink(&is_bad_symlink,
4606 a->worktree, ondisk_path, ie->path,
4607 blob, 0, 1, 0, a->repo,
4608 a->progress_cb, a->progress_arg);
4610 err = install_blob(a->worktree, ondisk_path,
4612 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4613 got_fileindex_perms_to_st(ie), blob,
4614 0, 1, 0, 0, a->repo,
4615 a->progress_cb, a->progress_arg);
4619 if (status == GOT_STATUS_DELETE ||
4620 status == GOT_STATUS_MODE_CHANGE) {
4621 err = got_fileindex_entry_update(ie,
4622 a->worktree->root_fd, relpath,
4624 a->worktree->base_commit_id->sha1, 1);
4628 if (is_bad_symlink) {
4629 got_fileindex_entry_filetype_set(ie,
4630 GOT_FILEIDX_MODE_BAD_SYMLINK);
4644 got_object_blob_close(blob);
4646 got_object_tree_close(tree);
4651 const struct got_error *
4652 got_worktree_revert(struct got_worktree *worktree,
4653 struct got_pathlist_head *paths,
4654 got_worktree_checkout_cb progress_cb, void *progress_arg,
4655 got_worktree_patch_cb patch_cb, void *patch_arg,
4656 struct got_repository *repo)
4658 struct got_fileindex *fileindex = NULL;
4659 char *fileindex_path = NULL;
4660 const struct got_error *err = NULL, *unlockerr = NULL;
4661 const struct got_error *sync_err = NULL;
4662 struct got_pathlist_entry *pe;
4663 struct revert_file_args rfa;
4665 err = lock_worktree(worktree, LOCK_EX);
4669 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4673 rfa.worktree = worktree;
4674 rfa.fileindex = fileindex;
4675 rfa.progress_cb = progress_cb;
4676 rfa.progress_arg = progress_arg;
4677 rfa.patch_cb = patch_cb;
4678 rfa.patch_arg = patch_arg;
4680 TAILQ_FOREACH(pe, paths, entry) {
4681 err = worktree_status(worktree, pe->path, fileindex, repo,
4682 revert_file, &rfa, NULL, NULL, 0, 0);
4686 sync_err = sync_fileindex(fileindex, fileindex_path);
4687 if (sync_err && err == NULL)
4690 free(fileindex_path);
4692 got_fileindex_free(fileindex);
4693 unlockerr = lock_worktree(worktree, LOCK_SH);
4694 if (unlockerr && err == NULL)
4700 free_commitable(struct got_commitable *ct)
4703 free(ct->in_repo_path);
4704 free(ct->ondisk_path);
4706 free(ct->base_blob_id);
4707 free(ct->staged_blob_id);
4708 free(ct->base_commit_id);
4712 struct collect_commitables_arg {
4713 struct got_pathlist_head *commitable_paths;
4714 struct got_repository *repo;
4715 struct got_worktree *worktree;
4716 struct got_fileindex *fileindex;
4717 int have_staged_files;
4718 int allow_bad_symlinks;
4721 static const struct got_error *
4722 collect_commitables(void *arg, unsigned char status,
4723 unsigned char staged_status, const char *relpath,
4724 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4725 struct got_object_id *commit_id, int dirfd, const char *de_name)
4727 struct collect_commitables_arg *a = arg;
4728 const struct got_error *err = NULL;
4729 struct got_commitable *ct = NULL;
4730 struct got_pathlist_entry *new = NULL;
4731 char *parent_path = NULL, *path = NULL;
4734 if (a->have_staged_files) {
4735 if (staged_status != GOT_STATUS_MODIFY &&
4736 staged_status != GOT_STATUS_ADD &&
4737 staged_status != GOT_STATUS_DELETE)
4740 if (status == GOT_STATUS_CONFLICT)
4741 return got_error(GOT_ERR_COMMIT_CONFLICT);
4743 if (status != GOT_STATUS_MODIFY &&
4744 status != GOT_STATUS_MODE_CHANGE &&
4745 status != GOT_STATUS_ADD &&
4746 status != GOT_STATUS_DELETE)
4750 if (asprintf(&path, "/%s", relpath) == -1) {
4751 err = got_error_from_errno("asprintf");
4754 if (strcmp(path, "/") == 0) {
4755 parent_path = strdup("");
4756 if (parent_path == NULL)
4757 return got_error_from_errno("strdup");
4759 err = got_path_dirname(&parent_path, path);
4764 ct = calloc(1, sizeof(*ct));
4766 err = got_error_from_errno("calloc");
4770 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4772 err = got_error_from_errno("asprintf");
4776 if (staged_status == GOT_STATUS_ADD ||
4777 staged_status == GOT_STATUS_MODIFY) {
4778 struct got_fileindex_entry *ie;
4779 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4780 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4781 case GOT_FILEIDX_MODE_REGULAR_FILE:
4782 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4785 case GOT_FILEIDX_MODE_SYMLINK:
4789 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4792 ct->mode |= got_fileindex_entry_perms_get(ie);
4793 } else if (status != GOT_STATUS_DELETE &&
4794 staged_status != GOT_STATUS_DELETE) {
4796 if (fstatat(dirfd, de_name, &sb,
4797 AT_SYMLINK_NOFOLLOW) == -1) {
4798 err = got_error_from_errno2("fstatat",
4802 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4803 err = got_error_from_errno2("lstat", ct->ondisk_path);
4806 ct->mode = sb.st_mode;
4809 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4810 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4812 err = got_error_from_errno("asprintf");
4816 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4817 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4819 char target_path[PATH_MAX];
4821 target_len = readlink(ct->ondisk_path, target_path,
4822 sizeof(target_path));
4823 if (target_len == -1) {
4824 err = got_error_from_errno2("readlink",
4828 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4829 target_len, ct->ondisk_path, a->worktree->root_path);
4832 if (is_bad_symlink) {
4833 err = got_error_path(ct->ondisk_path,
4834 GOT_ERR_BAD_SYMLINK);
4840 ct->status = status;
4841 ct->staged_status = staged_status;
4842 ct->blob_id = NULL; /* will be filled in when blob gets created */
4843 if (ct->status != GOT_STATUS_ADD &&
4844 ct->staged_status != GOT_STATUS_ADD) {
4845 ct->base_blob_id = got_object_id_dup(blob_id);
4846 if (ct->base_blob_id == NULL) {
4847 err = got_error_from_errno("got_object_id_dup");
4850 ct->base_commit_id = got_object_id_dup(commit_id);
4851 if (ct->base_commit_id == NULL) {
4852 err = got_error_from_errno("got_object_id_dup");
4856 if (ct->staged_status == GOT_STATUS_ADD ||
4857 ct->staged_status == GOT_STATUS_MODIFY) {
4858 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4859 if (ct->staged_blob_id == NULL) {
4860 err = got_error_from_errno("got_object_id_dup");
4864 ct->path = strdup(path);
4865 if (ct->path == NULL) {
4866 err = got_error_from_errno("strdup");
4869 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4871 if (ct && (err || new == NULL))
4872 free_commitable(ct);
4878 static const struct got_error *write_tree(struct got_object_id **, int *,
4879 struct got_tree_object *, const char *, struct got_pathlist_head *,
4880 got_worktree_status_cb status_cb, void *status_arg,
4881 struct got_repository *);
4883 static const struct got_error *
4884 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4885 struct got_tree_entry *te, const char *parent_path,
4886 struct got_pathlist_head *commitable_paths,
4887 got_worktree_status_cb status_cb, void *status_arg,
4888 struct got_repository *repo)
4890 const struct got_error *err = NULL;
4891 struct got_tree_object *subtree;
4894 if (asprintf(&subpath, "%s%s%s", parent_path,
4895 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4896 return got_error_from_errno("asprintf");
4898 err = got_object_open_as_tree(&subtree, repo, &te->id);
4902 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4903 commitable_paths, status_cb, status_arg, repo);
4904 got_object_tree_close(subtree);
4909 static const struct got_error *
4910 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4912 const struct got_error *err = NULL;
4913 char *ct_parent_path = NULL;
4917 if (strchr(ct->in_repo_path, '/') == NULL) {
4918 *match = got_path_is_root_dir(path);
4922 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4925 *match = (strcmp(path, ct_parent_path) == 0);
4926 free(ct_parent_path);
4931 get_ct_file_mode(struct got_commitable *ct)
4933 if (S_ISLNK(ct->mode))
4936 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4939 static const struct got_error *
4940 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4941 struct got_tree_entry *te, struct got_commitable *ct)
4943 const struct got_error *err = NULL;
4947 err = got_object_tree_entry_dup(new_te, te);
4951 (*new_te)->mode = get_ct_file_mode(ct);
4953 if (ct->staged_status == GOT_STATUS_MODIFY)
4954 memcpy(&(*new_te)->id, ct->staged_blob_id,
4955 sizeof((*new_te)->id));
4957 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4959 if (err && *new_te) {
4966 static const struct got_error *
4967 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4968 struct got_commitable *ct)
4970 const struct got_error *err = NULL;
4971 char *ct_name = NULL;
4975 *new_te = calloc(1, sizeof(**new_te));
4976 if (*new_te == NULL)
4977 return got_error_from_errno("calloc");
4979 err = got_path_basename(&ct_name, ct->path);
4982 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4983 sizeof((*new_te)->name)) {
4984 err = got_error(GOT_ERR_NO_SPACE);
4988 (*new_te)->mode = get_ct_file_mode(ct);
4990 if (ct->staged_status == GOT_STATUS_ADD)
4991 memcpy(&(*new_te)->id, ct->staged_blob_id,
4992 sizeof((*new_te)->id));
4994 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4997 if (err && *new_te) {
5004 static const struct got_error *
5005 insert_tree_entry(struct got_tree_entry *new_te,
5006 struct got_pathlist_head *paths)
5008 const struct got_error *err = NULL;
5009 struct got_pathlist_entry *new_pe;
5011 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5015 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5019 static const struct got_error *
5020 report_ct_status(struct got_commitable *ct,
5021 got_worktree_status_cb status_cb, void *status_arg)
5023 const char *ct_path = ct->path;
5024 unsigned char status;
5026 while (ct_path[0] == '/')
5029 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5030 status = ct->staged_status;
5032 status = ct->status;
5034 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5035 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5038 static const struct got_error *
5039 match_modified_subtree(int *modified, struct got_tree_entry *te,
5040 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5042 const struct got_error *err = NULL;
5043 struct got_pathlist_entry *pe;
5048 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5049 got_path_is_root_dir(base_tree_path) ? "" : "/",
5051 return got_error_from_errno("asprintf");
5053 TAILQ_FOREACH(pe, commitable_paths, entry) {
5054 struct got_commitable *ct = pe->data;
5055 *modified = got_path_is_child(ct->in_repo_path, te_path,
5065 static const struct got_error *
5066 match_deleted_or_modified_ct(struct got_commitable **ctp,
5067 struct got_tree_entry *te, const char *base_tree_path,
5068 struct got_pathlist_head *commitable_paths)
5070 const struct got_error *err = NULL;
5071 struct got_pathlist_entry *pe;
5075 TAILQ_FOREACH(pe, commitable_paths, entry) {
5076 struct got_commitable *ct = pe->data;
5077 char *ct_name = NULL;
5080 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5081 if (ct->status != GOT_STATUS_MODIFY &&
5082 ct->status != GOT_STATUS_MODE_CHANGE &&
5083 ct->status != GOT_STATUS_DELETE)
5086 if (ct->staged_status != GOT_STATUS_MODIFY &&
5087 ct->staged_status != GOT_STATUS_DELETE)
5091 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5094 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5100 err = got_path_basename(&ct_name, pe->path);
5104 if (strcmp(te->name, ct_name) != 0) {
5117 static const struct got_error *
5118 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5119 const char *child_path, const char *path_base_tree,
5120 struct got_pathlist_head *commitable_paths,
5121 got_worktree_status_cb status_cb, void *status_arg,
5122 struct got_repository *repo)
5124 const struct got_error *err = NULL;
5125 struct got_tree_entry *new_te;
5127 struct got_object_id *id = NULL;
5132 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5133 got_path_is_root_dir(path_base_tree) ? "" : "/",
5135 return got_error_from_errno("asprintf");
5137 new_te = calloc(1, sizeof(*new_te));
5139 return got_error_from_errno("calloc");
5140 new_te->mode = S_IFDIR;
5142 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5143 sizeof(new_te->name)) {
5144 err = got_error(GOT_ERR_NO_SPACE);
5147 err = write_tree(&id, &nentries, NULL, subtree_path,
5148 commitable_paths, status_cb, status_arg, repo);
5153 memcpy(&new_te->id, id, sizeof(new_te->id));
5162 static const struct got_error *
5163 write_tree(struct got_object_id **new_tree_id, int *nentries,
5164 struct got_tree_object *base_tree, const char *path_base_tree,
5165 struct got_pathlist_head *commitable_paths,
5166 got_worktree_status_cb status_cb, void *status_arg,
5167 struct got_repository *repo)
5169 const struct got_error *err = NULL;
5170 struct got_pathlist_head paths;
5171 struct got_tree_entry *te, *new_te = NULL;
5172 struct got_pathlist_entry *pe;
5177 /* Insert, and recurse into, newly added entries first. */
5178 TAILQ_FOREACH(pe, commitable_paths, entry) {
5179 struct got_commitable *ct = pe->data;
5180 char *child_path = NULL, *slash;
5182 if ((ct->status != GOT_STATUS_ADD &&
5183 ct->staged_status != GOT_STATUS_ADD) ||
5184 (ct->flags & GOT_COMMITABLE_ADDED))
5187 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5188 strlen(path_base_tree)))
5191 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5196 slash = strchr(child_path, '/');
5197 if (slash == NULL) {
5198 err = alloc_added_blob_tree_entry(&new_te, ct);
5201 err = report_ct_status(ct, status_cb, status_arg);
5204 ct->flags |= GOT_COMMITABLE_ADDED;
5205 err = insert_tree_entry(new_te, &paths);
5210 *slash = '\0'; /* trim trailing path components */
5211 if (base_tree == NULL ||
5212 got_object_tree_find_entry(base_tree, child_path)
5214 err = make_subtree_for_added_blob(&new_te,
5215 child_path, path_base_tree,
5216 commitable_paths, status_cb, status_arg,
5220 err = insert_tree_entry(new_te, &paths);
5229 int i, nbase_entries;
5230 /* Handle modified and deleted entries. */
5231 nbase_entries = got_object_tree_get_nentries(base_tree);
5232 for (i = 0; i < nbase_entries; i++) {
5233 struct got_commitable *ct = NULL;
5235 te = got_object_tree_get_entry(base_tree, i);
5236 if (got_object_tree_entry_is_submodule(te)) {
5237 /* Entry is a submodule; just copy it. */
5238 err = got_object_tree_entry_dup(&new_te, te);
5241 err = insert_tree_entry(new_te, &paths);
5248 if (S_ISDIR(te->mode)) {
5250 err = got_object_tree_entry_dup(&new_te, te);
5253 err = match_modified_subtree(&modified, te,
5254 path_base_tree, commitable_paths);
5257 /* Avoid recursion into unmodified subtrees. */
5259 struct got_object_id *new_id;
5261 err = write_subtree(&new_id,
5263 path_base_tree, commitable_paths,
5264 status_cb, status_arg, repo);
5267 if (nsubentries == 0) {
5268 /* All entries were deleted. */
5272 memcpy(&new_te->id, new_id,
5273 sizeof(new_te->id));
5276 err = insert_tree_entry(new_te, &paths);
5283 err = match_deleted_or_modified_ct(&ct, te,
5284 path_base_tree, commitable_paths);
5288 /* NB: Deleted entries get dropped here. */
5289 if (ct->status == GOT_STATUS_MODIFY ||
5290 ct->status == GOT_STATUS_MODE_CHANGE ||
5291 ct->staged_status == GOT_STATUS_MODIFY) {
5292 err = alloc_modified_blob_tree_entry(
5296 err = insert_tree_entry(new_te, &paths);
5301 err = report_ct_status(ct, status_cb,
5306 /* Entry is unchanged; just copy it. */
5307 err = got_object_tree_entry_dup(&new_te, te);
5310 err = insert_tree_entry(new_te, &paths);
5318 /* Write new list of entries; deleted entries have been dropped. */
5319 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5321 got_pathlist_free(&paths);
5325 static const struct got_error *
5326 update_fileindex_after_commit(struct got_worktree *worktree,
5327 struct got_pathlist_head *commitable_paths,
5328 struct got_object_id *new_base_commit_id,
5329 struct got_fileindex *fileindex, int have_staged_files)
5331 const struct got_error *err = NULL;
5332 struct got_pathlist_entry *pe;
5333 char *relpath = NULL;
5335 TAILQ_FOREACH(pe, commitable_paths, entry) {
5336 struct got_fileindex_entry *ie;
5337 struct got_commitable *ct = pe->data;
5339 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5341 err = got_path_skip_common_ancestor(&relpath,
5342 worktree->root_path, ct->ondisk_path);
5347 if (ct->status == GOT_STATUS_DELETE ||
5348 ct->staged_status == GOT_STATUS_DELETE) {
5349 got_fileindex_entry_remove(fileindex, ie);
5350 } else if (ct->staged_status == GOT_STATUS_ADD ||
5351 ct->staged_status == GOT_STATUS_MODIFY) {
5352 got_fileindex_entry_stage_set(ie,
5353 GOT_FILEIDX_STAGE_NONE);
5354 got_fileindex_entry_staged_filetype_set(ie, 0);
5356 err = got_fileindex_entry_update(ie,
5357 worktree->root_fd, relpath,
5358 ct->staged_blob_id->sha1,
5359 new_base_commit_id->sha1,
5360 !have_staged_files);
5362 err = got_fileindex_entry_update(ie,
5363 worktree->root_fd, relpath,
5365 new_base_commit_id->sha1,
5366 !have_staged_files);
5368 err = got_fileindex_entry_alloc(&ie, pe->path);
5371 err = got_fileindex_entry_update(ie,
5372 worktree->root_fd, relpath, ct->blob_id->sha1,
5373 new_base_commit_id->sha1, 1);
5375 got_fileindex_entry_free(ie);
5378 err = got_fileindex_entry_add(fileindex, ie);
5380 got_fileindex_entry_free(ie);
5393 static const struct got_error *
5394 check_out_of_date(const char *in_repo_path, unsigned char status,
5395 unsigned char staged_status, struct got_object_id *base_blob_id,
5396 struct got_object_id *base_commit_id,
5397 struct got_object_id *head_commit_id, struct got_repository *repo,
5400 const struct got_error *err = NULL;
5401 struct got_object_id *id = NULL;
5403 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5404 /* Trivial case: base commit == head commit */
5405 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5408 * Ensure file content which local changes were based
5409 * on matches file content in the branch head.
5411 err = got_object_id_by_path(&id, repo, head_commit_id,
5414 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5415 err = got_error(ood_errcode);
5417 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5418 err = got_error(ood_errcode);
5420 /* Require that added files don't exist in the branch head. */
5421 err = got_object_id_by_path(&id, repo, head_commit_id,
5423 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5425 err = id ? got_error(ood_errcode) : NULL;
5432 const struct got_error *
5433 commit_worktree(struct got_object_id **new_commit_id,
5434 struct got_pathlist_head *commitable_paths,
5435 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5436 const char *author, const char *committer,
5437 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5438 got_worktree_status_cb status_cb, void *status_arg,
5439 struct got_repository *repo)
5441 const struct got_error *err = NULL, *unlockerr = NULL;
5442 struct got_pathlist_entry *pe;
5443 const char *head_ref_name = NULL;
5444 struct got_commit_object *head_commit = NULL;
5445 struct got_reference *head_ref2 = NULL;
5446 struct got_object_id *head_commit_id2 = NULL;
5447 struct got_tree_object *head_tree = NULL;
5448 struct got_object_id *new_tree_id = NULL;
5450 struct got_object_id_queue parent_ids;
5451 struct got_object_qid *pid = NULL;
5452 char *logmsg = NULL;
5454 *new_commit_id = NULL;
5456 SIMPLEQ_INIT(&parent_ids);
5458 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5462 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5466 if (commit_msg_cb != NULL) {
5467 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5472 if (logmsg == NULL || strlen(logmsg) == 0) {
5473 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5477 /* Create blobs from added and modified files and record their IDs. */
5478 TAILQ_FOREACH(pe, commitable_paths, entry) {
5479 struct got_commitable *ct = pe->data;
5482 /* Blobs for staged files already exist. */
5483 if (ct->staged_status == GOT_STATUS_ADD ||
5484 ct->staged_status == GOT_STATUS_MODIFY)
5487 if (ct->status != GOT_STATUS_ADD &&
5488 ct->status != GOT_STATUS_MODIFY &&
5489 ct->status != GOT_STATUS_MODE_CHANGE)
5492 if (asprintf(&ondisk_path, "%s/%s",
5493 worktree->root_path, pe->path) == -1) {
5494 err = got_error_from_errno("asprintf");
5497 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5503 /* Recursively write new tree objects. */
5504 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5505 commitable_paths, status_cb, status_arg, repo);
5509 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5512 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5513 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5514 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5515 got_object_qid_free(pid);
5521 /* Check if a concurrent commit to our branch has occurred. */
5522 head_ref_name = got_worktree_get_head_ref_name(worktree);
5523 if (head_ref_name == NULL) {
5524 err = got_error_from_errno("got_worktree_get_head_ref_name");
5527 /* Lock the reference here to prevent concurrent modification. */
5528 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5531 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5534 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5535 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5538 /* Update branch head in repository. */
5539 err = got_ref_change_ref(head_ref2, *new_commit_id);
5542 err = got_ref_write(head_ref2, repo);
5546 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5550 err = ref_base_commit(worktree, repo);
5555 got_object_tree_close(head_tree);
5557 got_object_commit_close(head_commit);
5558 free(head_commit_id2);
5560 unlockerr = got_ref_unlock(head_ref2);
5561 if (unlockerr && err == NULL)
5563 got_ref_close(head_ref2);
5568 static const struct got_error *
5569 check_path_is_commitable(const char *path,
5570 struct got_pathlist_head *commitable_paths)
5572 struct got_pathlist_entry *cpe = NULL;
5573 size_t path_len = strlen(path);
5575 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5576 struct got_commitable *ct = cpe->data;
5577 const char *ct_path = ct->path;
5579 while (ct_path[0] == '/')
5582 if (strcmp(path, ct_path) == 0 ||
5583 got_path_is_child(ct_path, path, path_len))
5588 return got_error_path(path, GOT_ERR_BAD_PATH);
5593 static const struct got_error *
5594 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5596 int *have_staged_files = arg;
5598 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5599 *have_staged_files = 1;
5600 return got_error(GOT_ERR_CANCELLED);
5606 static const struct got_error *
5607 check_non_staged_files(struct got_fileindex *fileindex,
5608 struct got_pathlist_head *paths)
5610 struct got_pathlist_entry *pe;
5611 struct got_fileindex_entry *ie;
5613 TAILQ_FOREACH(pe, paths, entry) {
5614 if (pe->path[0] == '\0')
5616 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5618 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5619 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5620 return got_error_path(pe->path,
5621 GOT_ERR_FILE_NOT_STAGED);
5627 const struct got_error *
5628 got_worktree_commit(struct got_object_id **new_commit_id,
5629 struct got_worktree *worktree, struct got_pathlist_head *paths,
5630 const char *author, const char *committer, int allow_bad_symlinks,
5631 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5632 got_worktree_status_cb status_cb, void *status_arg,
5633 struct got_repository *repo)
5635 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5636 struct got_fileindex *fileindex = NULL;
5637 char *fileindex_path = NULL;
5638 struct got_pathlist_head commitable_paths;
5639 struct collect_commitables_arg cc_arg;
5640 struct got_pathlist_entry *pe;
5641 struct got_reference *head_ref = NULL;
5642 struct got_object_id *head_commit_id = NULL;
5643 int have_staged_files = 0;
5645 *new_commit_id = NULL;
5647 TAILQ_INIT(&commitable_paths);
5649 err = lock_worktree(worktree, LOCK_EX);
5653 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5657 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5661 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5665 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5666 &have_staged_files);
5667 if (err && err->code != GOT_ERR_CANCELLED)
5669 if (have_staged_files) {
5670 err = check_non_staged_files(fileindex, paths);
5675 cc_arg.commitable_paths = &commitable_paths;
5676 cc_arg.worktree = worktree;
5677 cc_arg.fileindex = fileindex;
5679 cc_arg.have_staged_files = have_staged_files;
5680 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5681 TAILQ_FOREACH(pe, paths, entry) {
5682 err = worktree_status(worktree, pe->path, fileindex, repo,
5683 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5688 if (TAILQ_EMPTY(&commitable_paths)) {
5689 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5693 TAILQ_FOREACH(pe, paths, entry) {
5694 err = check_path_is_commitable(pe->path, &commitable_paths);
5699 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5700 struct got_commitable *ct = pe->data;
5701 const char *ct_path = ct->in_repo_path;
5703 while (ct_path[0] == '/')
5705 err = check_out_of_date(ct_path, ct->status,
5706 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5707 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5713 err = commit_worktree(new_commit_id, &commitable_paths,
5714 head_commit_id, worktree, author, committer,
5715 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5719 err = update_fileindex_after_commit(worktree, &commitable_paths,
5720 *new_commit_id, fileindex, have_staged_files);
5721 sync_err = sync_fileindex(fileindex, fileindex_path);
5722 if (sync_err && err == NULL)
5726 got_fileindex_free(fileindex);
5727 free(fileindex_path);
5728 unlockerr = lock_worktree(worktree, LOCK_SH);
5729 if (unlockerr && err == NULL)
5731 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5732 struct got_commitable *ct = pe->data;
5733 free_commitable(ct);
5735 got_pathlist_free(&commitable_paths);
5740 got_commitable_get_path(struct got_commitable *ct)
5746 got_commitable_get_status(struct got_commitable *ct)
5751 struct check_rebase_ok_arg {
5752 struct got_worktree *worktree;
5753 struct got_repository *repo;
5756 static const struct got_error *
5757 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5759 const struct got_error *err = NULL;
5760 struct check_rebase_ok_arg *a = arg;
5761 unsigned char status;
5765 /* Reject rebase of a work tree with mixed base commits. */
5766 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5767 SHA1_DIGEST_LENGTH))
5768 return got_error(GOT_ERR_MIXED_COMMITS);
5770 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5772 return got_error_from_errno("asprintf");
5774 /* Reject rebase of a work tree with modified or staged files. */
5775 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5780 if (status != GOT_STATUS_NO_CHANGE)
5781 return got_error(GOT_ERR_MODIFIED);
5782 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5783 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5788 const struct got_error *
5789 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5790 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5791 struct got_worktree *worktree, struct got_reference *branch,
5792 struct got_repository *repo)
5794 const struct got_error *err = NULL;
5795 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5796 char *branch_ref_name = NULL;
5797 char *fileindex_path = NULL;
5798 struct check_rebase_ok_arg ok_arg;
5799 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5800 struct got_object_id *wt_branch_tip = NULL;
5802 *new_base_branch_ref = NULL;
5806 err = lock_worktree(worktree, LOCK_EX);
5810 err = open_fileindex(fileindex, &fileindex_path, worktree);
5814 ok_arg.worktree = worktree;
5816 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5821 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5825 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5829 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5833 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5838 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5841 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5842 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5846 err = got_ref_alloc_symref(new_base_branch_ref,
5847 new_base_branch_ref_name, wt_branch);
5850 err = got_ref_write(*new_base_branch_ref, repo);
5854 /* TODO Lock original branch's ref while rebasing? */
5856 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5860 err = got_ref_write(branch_ref, repo);
5864 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5865 worktree->base_commit_id);
5868 err = got_ref_write(*tmp_branch, repo);
5872 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5876 free(fileindex_path);
5877 free(tmp_branch_name);
5878 free(new_base_branch_ref_name);
5879 free(branch_ref_name);
5881 got_ref_close(branch_ref);
5883 got_ref_close(wt_branch);
5884 free(wt_branch_tip);
5886 if (*new_base_branch_ref) {
5887 got_ref_close(*new_base_branch_ref);
5888 *new_base_branch_ref = NULL;
5891 got_ref_close(*tmp_branch);
5895 got_fileindex_free(*fileindex);
5898 lock_worktree(worktree, LOCK_SH);
5903 const struct got_error *
5904 got_worktree_rebase_continue(struct got_object_id **commit_id,
5905 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5906 struct got_reference **branch, struct got_fileindex **fileindex,
5907 struct got_worktree *worktree, struct got_repository *repo)
5909 const struct got_error *err;
5910 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5911 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5912 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5913 char *fileindex_path = NULL;
5914 int have_staged_files = 0;
5917 *new_base_branch = NULL;
5922 err = lock_worktree(worktree, LOCK_EX);
5926 err = open_fileindex(fileindex, &fileindex_path, worktree);
5930 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5931 &have_staged_files);
5932 if (err && err->code != GOT_ERR_CANCELLED)
5934 if (have_staged_files) {
5935 err = got_error(GOT_ERR_STAGED_PATHS);
5939 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5943 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5947 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5951 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5955 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5959 err = got_ref_open(branch, repo,
5960 got_ref_get_symref_target(branch_ref), 0);
5964 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5968 err = got_ref_resolve(commit_id, repo, commit_ref);
5972 err = got_ref_open(new_base_branch, repo,
5973 new_base_branch_ref_name, 0);
5977 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5981 free(commit_ref_name);
5982 free(branch_ref_name);
5983 free(fileindex_path);
5985 got_ref_close(commit_ref);
5987 got_ref_close(branch_ref);
5992 got_ref_close(*tmp_branch);
5995 if (*new_base_branch) {
5996 got_ref_close(*new_base_branch);
5997 *new_base_branch = NULL;
6000 got_ref_close(*branch);
6004 got_fileindex_free(*fileindex);
6007 lock_worktree(worktree, LOCK_SH);
6012 const struct got_error *
6013 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6015 const struct got_error *err;
6016 char *tmp_branch_name = NULL;
6018 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6022 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6023 free(tmp_branch_name);
6027 static const struct got_error *
6028 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6029 char **logmsg, void *arg)
6035 static const struct got_error *
6036 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6037 const char *path, struct got_object_id *blob_id,
6038 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6039 int dirfd, const char *de_name)
6044 struct collect_merged_paths_arg {
6045 got_worktree_checkout_cb progress_cb;
6047 struct got_pathlist_head *merged_paths;
6050 static const struct got_error *
6051 collect_merged_paths(void *arg, unsigned char status, const char *path)
6053 const struct got_error *err;
6054 struct collect_merged_paths_arg *a = arg;
6056 struct got_pathlist_entry *new;
6058 err = (*a->progress_cb)(a->progress_arg, status, path);
6062 if (status != GOT_STATUS_MERGE &&
6063 status != GOT_STATUS_ADD &&
6064 status != GOT_STATUS_DELETE &&
6065 status != GOT_STATUS_CONFLICT)
6070 return got_error_from_errno("strdup");
6072 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6073 if (err || new == NULL)
6079 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6081 struct got_pathlist_entry *pe;
6083 TAILQ_FOREACH(pe, merged_paths, entry)
6084 free((char *)pe->path);
6086 got_pathlist_free(merged_paths);
6089 static const struct got_error *
6090 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6091 int is_rebase, struct got_repository *repo)
6093 const struct got_error *err;
6094 struct got_reference *commit_ref = NULL;
6096 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6098 if (err->code != GOT_ERR_NOT_REF)
6100 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6103 err = got_ref_write(commit_ref, repo);
6106 } else if (is_rebase) {
6107 struct got_object_id *stored_id;
6110 err = got_ref_resolve(&stored_id, repo, commit_ref);
6113 cmp = got_object_id_cmp(commit_id, stored_id);
6116 err = got_error(GOT_ERR_REBASE_COMMITID);
6122 got_ref_close(commit_ref);
6126 static const struct got_error *
6127 rebase_merge_files(struct got_pathlist_head *merged_paths,
6128 const char *commit_ref_name, struct got_worktree *worktree,
6129 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6130 struct got_object_id *commit_id, struct got_repository *repo,
6131 got_worktree_checkout_cb progress_cb, void *progress_arg,
6132 got_cancel_cb cancel_cb, void *cancel_arg)
6134 const struct got_error *err;
6135 struct got_reference *commit_ref = NULL;
6136 struct collect_merged_paths_arg cmp_arg;
6137 char *fileindex_path;
6139 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6141 err = get_fileindex_path(&fileindex_path, worktree);
6145 cmp_arg.progress_cb = progress_cb;
6146 cmp_arg.progress_arg = progress_arg;
6147 cmp_arg.merged_paths = merged_paths;
6148 err = merge_files(worktree, fileindex, fileindex_path,
6149 parent_commit_id, commit_id, repo, collect_merged_paths,
6150 &cmp_arg, cancel_cb, cancel_arg);
6152 got_ref_close(commit_ref);
6156 const struct got_error *
6157 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6158 struct got_worktree *worktree, struct got_fileindex *fileindex,
6159 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6160 struct got_repository *repo,
6161 got_worktree_checkout_cb progress_cb, void *progress_arg,
6162 got_cancel_cb cancel_cb, void *cancel_arg)
6164 const struct got_error *err;
6165 char *commit_ref_name;
6167 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6171 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6175 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6176 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6177 progress_arg, cancel_cb, cancel_arg);
6179 free(commit_ref_name);
6183 const struct got_error *
6184 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6185 struct got_worktree *worktree, struct got_fileindex *fileindex,
6186 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6187 struct got_repository *repo,
6188 got_worktree_checkout_cb progress_cb, void *progress_arg,
6189 got_cancel_cb cancel_cb, void *cancel_arg)
6191 const struct got_error *err;
6192 char *commit_ref_name;
6194 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6198 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6202 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6203 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6204 progress_arg, cancel_cb, cancel_arg);
6206 free(commit_ref_name);
6210 static const struct got_error *
6211 rebase_commit(struct got_object_id **new_commit_id,
6212 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6213 struct got_worktree *worktree, struct got_fileindex *fileindex,
6214 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6215 const char *new_logmsg, struct got_repository *repo)
6217 const struct got_error *err, *sync_err;
6218 struct got_pathlist_head commitable_paths;
6219 struct collect_commitables_arg cc_arg;
6220 char *fileindex_path = NULL;
6221 struct got_reference *head_ref = NULL;
6222 struct got_object_id *head_commit_id = NULL;
6223 char *logmsg = NULL;
6225 TAILQ_INIT(&commitable_paths);
6226 *new_commit_id = NULL;
6228 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6230 err = get_fileindex_path(&fileindex_path, worktree);
6234 cc_arg.commitable_paths = &commitable_paths;
6235 cc_arg.worktree = worktree;
6237 cc_arg.have_staged_files = 0;
6239 * If possible get the status of individual files directly to
6240 * avoid crawling the entire work tree once per rebased commit.
6241 * TODO: Ideally, merged_paths would contain a list of commitables
6242 * we could use so we could skip worktree_status() entirely.
6245 struct got_pathlist_entry *pe;
6246 TAILQ_FOREACH(pe, merged_paths, entry) {
6247 err = worktree_status(worktree, pe->path, fileindex,
6248 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6254 err = worktree_status(worktree, "", fileindex, repo,
6255 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6260 if (TAILQ_EMPTY(&commitable_paths)) {
6261 /* No-op change; commit will be elided. */
6262 err = got_ref_delete(commit_ref, repo);
6265 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6269 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6273 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6278 logmsg = strdup(new_logmsg);
6279 if (logmsg == NULL) {
6280 err = got_error_from_errno("strdup");
6284 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6289 /* NB: commit_worktree will call free(logmsg) */
6290 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6291 worktree, got_object_commit_get_author(orig_commit),
6292 got_object_commit_get_committer(orig_commit),
6293 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6297 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6301 err = got_ref_delete(commit_ref, repo);
6305 err = update_fileindex_after_commit(worktree, &commitable_paths,
6306 *new_commit_id, fileindex, 0);
6307 sync_err = sync_fileindex(fileindex, fileindex_path);
6308 if (sync_err && err == NULL)
6311 free(fileindex_path);
6312 free(head_commit_id);
6314 got_ref_close(head_ref);
6316 free(*new_commit_id);
6317 *new_commit_id = NULL;
6322 const struct got_error *
6323 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6324 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6325 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6326 struct got_commit_object *orig_commit,
6327 struct got_object_id *orig_commit_id, struct got_repository *repo)
6329 const struct got_error *err;
6330 char *commit_ref_name;
6331 struct got_reference *commit_ref = NULL;
6332 struct got_object_id *commit_id = NULL;
6334 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6338 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6341 err = got_ref_resolve(&commit_id, repo, commit_ref);
6344 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6345 err = got_error(GOT_ERR_REBASE_COMMITID);
6349 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6350 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6353 got_ref_close(commit_ref);
6354 free(commit_ref_name);
6359 const struct got_error *
6360 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6361 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6362 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6363 struct got_commit_object *orig_commit,
6364 struct got_object_id *orig_commit_id, const char *new_logmsg,
6365 struct got_repository *repo)
6367 const struct got_error *err;
6368 char *commit_ref_name;
6369 struct got_reference *commit_ref = NULL;
6371 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6375 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6379 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6380 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6383 got_ref_close(commit_ref);
6384 free(commit_ref_name);
6388 const struct got_error *
6389 got_worktree_rebase_postpone(struct got_worktree *worktree,
6390 struct got_fileindex *fileindex)
6393 got_fileindex_free(fileindex);
6394 return lock_worktree(worktree, LOCK_SH);
6397 static const struct got_error *
6398 delete_ref(const char *name, struct got_repository *repo)
6400 const struct got_error *err;
6401 struct got_reference *ref;
6403 err = got_ref_open(&ref, repo, name, 0);
6405 if (err->code == GOT_ERR_NOT_REF)
6410 err = got_ref_delete(ref, repo);
6415 static const struct got_error *
6416 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6418 const struct got_error *err;
6419 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6420 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6422 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6425 err = delete_ref(tmp_branch_name, repo);
6429 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6432 err = delete_ref(new_base_branch_ref_name, repo);
6436 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6439 err = delete_ref(branch_ref_name, repo);
6443 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6446 err = delete_ref(commit_ref_name, repo);
6451 free(tmp_branch_name);
6452 free(new_base_branch_ref_name);
6453 free(branch_ref_name);
6454 free(commit_ref_name);
6458 const struct got_error *
6459 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6460 struct got_object_id *new_commit_id, struct got_repository *repo)
6462 const struct got_error *err;
6463 struct got_reference *ref = NULL;
6464 struct got_object_id *old_commit_id = NULL;
6465 const char *branch_name = NULL;
6466 char *new_id_str = NULL;
6467 char *refname = NULL;
6469 branch_name = got_ref_get_name(branch);
6470 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6471 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6474 err = got_object_id_str(&new_id_str, new_commit_id);
6478 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6479 new_id_str) == -1) {
6480 err = got_error_from_errno("asprintf");
6484 err = got_ref_resolve(&old_commit_id, repo, branch);
6488 err = got_ref_alloc(&ref, refname, old_commit_id);
6492 err = got_ref_write(ref, repo);
6496 free(old_commit_id);
6502 const struct got_error *
6503 got_worktree_rebase_complete(struct got_worktree *worktree,
6504 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6505 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6506 struct got_repository *repo, int create_backup)
6508 const struct got_error *err, *unlockerr, *sync_err;
6509 struct got_object_id *new_head_commit_id = NULL;
6510 char *fileindex_path = NULL;
6512 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6516 if (create_backup) {
6517 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6518 rebased_branch, new_head_commit_id, repo);
6523 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6527 err = got_ref_write(rebased_branch, repo);
6531 err = got_worktree_set_head_ref(worktree, rebased_branch);
6535 err = delete_rebase_refs(worktree, repo);
6539 err = get_fileindex_path(&fileindex_path, worktree);
6542 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6543 sync_err = sync_fileindex(fileindex, fileindex_path);
6544 if (sync_err && err == NULL)
6547 got_fileindex_free(fileindex);
6548 free(fileindex_path);
6549 free(new_head_commit_id);
6550 unlockerr = lock_worktree(worktree, LOCK_SH);
6551 if (unlockerr && err == NULL)
6556 const struct got_error *
6557 got_worktree_rebase_abort(struct got_worktree *worktree,
6558 struct got_fileindex *fileindex, struct got_repository *repo,
6559 struct got_reference *new_base_branch,
6560 got_worktree_checkout_cb progress_cb, void *progress_arg)
6562 const struct got_error *err, *unlockerr, *sync_err;
6563 struct got_reference *resolved = NULL;
6564 struct got_object_id *commit_id = NULL;
6565 char *fileindex_path = NULL;
6566 struct revert_file_args rfa;
6567 struct got_object_id *tree_id = NULL;
6569 err = lock_worktree(worktree, LOCK_EX);
6573 err = got_ref_open(&resolved, repo,
6574 got_ref_get_symref_target(new_base_branch), 0);
6578 err = got_worktree_set_head_ref(worktree, resolved);
6583 * XXX commits to the base branch could have happened while
6584 * we were busy rebasing; should we store the original commit ID
6585 * when rebase begins and read it back here?
6587 err = got_ref_resolve(&commit_id, repo, resolved);
6591 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6595 err = got_object_id_by_path(&tree_id, repo,
6596 worktree->base_commit_id, worktree->path_prefix);
6600 err = delete_rebase_refs(worktree, repo);
6604 err = get_fileindex_path(&fileindex_path, worktree);
6608 rfa.worktree = worktree;
6609 rfa.fileindex = fileindex;
6610 rfa.progress_cb = progress_cb;
6611 rfa.progress_arg = progress_arg;
6612 rfa.patch_cb = NULL;
6613 rfa.patch_arg = NULL;
6615 err = worktree_status(worktree, "", fileindex, repo,
6616 revert_file, &rfa, NULL, NULL, 0, 0);
6620 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6621 repo, progress_cb, progress_arg, NULL, NULL);
6623 sync_err = sync_fileindex(fileindex, fileindex_path);
6624 if (sync_err && err == NULL)
6627 got_ref_close(resolved);
6631 got_fileindex_free(fileindex);
6632 free(fileindex_path);
6634 unlockerr = lock_worktree(worktree, LOCK_SH);
6635 if (unlockerr && err == NULL)
6640 const struct got_error *
6641 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6642 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6643 struct got_fileindex **fileindex, struct got_worktree *worktree,
6644 struct got_repository *repo)
6646 const struct got_error *err = NULL;
6647 char *tmp_branch_name = NULL;
6648 char *branch_ref_name = NULL;
6649 char *base_commit_ref_name = NULL;
6650 char *fileindex_path = NULL;
6651 struct check_rebase_ok_arg ok_arg;
6652 struct got_reference *wt_branch = NULL;
6653 struct got_reference *base_commit_ref = NULL;
6657 *base_commit_id = NULL;
6660 err = lock_worktree(worktree, LOCK_EX);
6664 err = open_fileindex(fileindex, &fileindex_path, worktree);
6668 ok_arg.worktree = worktree;
6670 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6675 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6679 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6683 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6688 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6693 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6697 err = got_ref_write(*branch_ref, repo);
6701 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6702 worktree->base_commit_id);
6705 err = got_ref_write(base_commit_ref, repo);
6708 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6709 if (*base_commit_id == NULL) {
6710 err = got_error_from_errno("got_object_id_dup");
6714 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6715 worktree->base_commit_id);
6718 err = got_ref_write(*tmp_branch, repo);
6722 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6726 free(fileindex_path);
6727 free(tmp_branch_name);
6728 free(branch_ref_name);
6729 free(base_commit_ref_name);
6731 got_ref_close(wt_branch);
6734 got_ref_close(*branch_ref);
6738 got_ref_close(*tmp_branch);
6741 free(*base_commit_id);
6743 got_fileindex_free(*fileindex);
6746 lock_worktree(worktree, LOCK_SH);
6751 const struct got_error *
6752 got_worktree_histedit_postpone(struct got_worktree *worktree,
6753 struct got_fileindex *fileindex)
6756 got_fileindex_free(fileindex);
6757 return lock_worktree(worktree, LOCK_SH);
6760 const struct got_error *
6761 got_worktree_histedit_in_progress(int *in_progress,
6762 struct got_worktree *worktree)
6764 const struct got_error *err;
6765 char *tmp_branch_name = NULL;
6767 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6771 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6772 free(tmp_branch_name);
6776 const struct got_error *
6777 got_worktree_histedit_continue(struct got_object_id **commit_id,
6778 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6779 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6780 struct got_worktree *worktree, struct got_repository *repo)
6782 const struct got_error *err;
6783 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6784 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6785 struct got_reference *commit_ref = NULL;
6786 struct got_reference *base_commit_ref = NULL;
6787 char *fileindex_path = NULL;
6788 int have_staged_files = 0;
6792 *base_commit_id = NULL;
6795 err = lock_worktree(worktree, LOCK_EX);
6799 err = open_fileindex(fileindex, &fileindex_path, worktree);
6803 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6804 &have_staged_files);
6805 if (err && err->code != GOT_ERR_CANCELLED)
6807 if (have_staged_files) {
6808 err = got_error(GOT_ERR_STAGED_PATHS);
6812 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6816 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6820 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6824 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6829 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6833 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6836 err = got_ref_resolve(commit_id, repo, commit_ref);
6840 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6843 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6847 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6851 free(commit_ref_name);
6852 free(branch_ref_name);
6853 free(fileindex_path);
6855 got_ref_close(commit_ref);
6856 if (base_commit_ref)
6857 got_ref_close(base_commit_ref);
6861 free(*base_commit_id);
6862 *base_commit_id = NULL;
6864 got_ref_close(*tmp_branch);
6868 got_fileindex_free(*fileindex);
6871 lock_worktree(worktree, LOCK_EX);
6876 static const struct got_error *
6877 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6879 const struct got_error *err;
6880 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6881 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6883 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6886 err = delete_ref(tmp_branch_name, repo);
6890 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6894 err = delete_ref(base_commit_ref_name, repo);
6898 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6901 err = delete_ref(branch_ref_name, repo);
6905 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6908 err = delete_ref(commit_ref_name, repo);
6912 free(tmp_branch_name);
6913 free(base_commit_ref_name);
6914 free(branch_ref_name);
6915 free(commit_ref_name);
6919 const struct got_error *
6920 got_worktree_histedit_abort(struct got_worktree *worktree,
6921 struct got_fileindex *fileindex, struct got_repository *repo,
6922 struct got_reference *branch, struct got_object_id *base_commit_id,
6923 got_worktree_checkout_cb progress_cb, void *progress_arg)
6925 const struct got_error *err, *unlockerr, *sync_err;
6926 struct got_reference *resolved = NULL;
6927 char *fileindex_path = NULL;
6928 struct got_object_id *tree_id = NULL;
6929 struct revert_file_args rfa;
6931 err = lock_worktree(worktree, LOCK_EX);
6935 err = got_ref_open(&resolved, repo,
6936 got_ref_get_symref_target(branch), 0);
6940 err = got_worktree_set_head_ref(worktree, resolved);
6944 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6948 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6949 worktree->path_prefix);
6953 err = delete_histedit_refs(worktree, repo);
6957 err = get_fileindex_path(&fileindex_path, worktree);
6961 rfa.worktree = worktree;
6962 rfa.fileindex = fileindex;
6963 rfa.progress_cb = progress_cb;
6964 rfa.progress_arg = progress_arg;
6965 rfa.patch_cb = NULL;
6966 rfa.patch_arg = NULL;
6968 err = worktree_status(worktree, "", fileindex, repo,
6969 revert_file, &rfa, NULL, NULL, 0, 0);
6973 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6974 repo, progress_cb, progress_arg, NULL, NULL);
6976 sync_err = sync_fileindex(fileindex, fileindex_path);
6977 if (sync_err && err == NULL)
6980 got_ref_close(resolved);
6982 free(fileindex_path);
6984 unlockerr = lock_worktree(worktree, LOCK_SH);
6985 if (unlockerr && err == NULL)
6990 const struct got_error *
6991 got_worktree_histedit_complete(struct got_worktree *worktree,
6992 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6993 struct got_reference *edited_branch, struct got_repository *repo)
6995 const struct got_error *err, *unlockerr, *sync_err;
6996 struct got_object_id *new_head_commit_id = NULL;
6997 struct got_reference *resolved = NULL;
6998 char *fileindex_path = NULL;
7000 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7004 err = got_ref_open(&resolved, repo,
7005 got_ref_get_symref_target(edited_branch), 0);
7009 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7010 resolved, new_head_commit_id, repo);
7014 err = got_ref_change_ref(resolved, new_head_commit_id);
7018 err = got_ref_write(resolved, repo);
7022 err = got_worktree_set_head_ref(worktree, resolved);
7026 err = delete_histedit_refs(worktree, repo);
7030 err = get_fileindex_path(&fileindex_path, worktree);
7033 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7034 sync_err = sync_fileindex(fileindex, fileindex_path);
7035 if (sync_err && err == NULL)
7038 got_fileindex_free(fileindex);
7039 free(fileindex_path);
7040 free(new_head_commit_id);
7041 unlockerr = lock_worktree(worktree, LOCK_SH);
7042 if (unlockerr && err == NULL)
7047 const struct got_error *
7048 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7049 struct got_object_id *commit_id, struct got_repository *repo)
7051 const struct got_error *err;
7052 char *commit_ref_name;
7054 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7058 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7062 err = delete_ref(commit_ref_name, repo);
7064 free(commit_ref_name);
7068 const struct got_error *
7069 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7070 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7071 struct got_worktree *worktree, const char *refname,
7072 struct got_repository *repo)
7074 const struct got_error *err = NULL;
7075 char *fileindex_path = NULL;
7076 struct check_rebase_ok_arg ok_arg;
7080 *base_branch_ref = NULL;
7082 err = lock_worktree(worktree, LOCK_EX);
7086 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7087 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7088 "cannot integrate a branch into itself; "
7089 "update -b or different branch name required");
7093 err = open_fileindex(fileindex, &fileindex_path, worktree);
7097 /* Preconditions are the same as for rebase. */
7098 ok_arg.worktree = worktree;
7100 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7105 err = got_ref_open(branch_ref, repo, refname, 1);
7109 err = got_ref_open(base_branch_ref, repo,
7110 got_worktree_get_head_ref_name(worktree), 1);
7114 got_ref_close(*branch_ref);
7117 if (*base_branch_ref) {
7118 got_ref_close(*base_branch_ref);
7119 *base_branch_ref = NULL;
7122 got_fileindex_free(*fileindex);
7125 lock_worktree(worktree, LOCK_SH);
7130 const struct got_error *
7131 got_worktree_integrate_continue(struct got_worktree *worktree,
7132 struct got_fileindex *fileindex, struct got_repository *repo,
7133 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7134 got_worktree_checkout_cb progress_cb, void *progress_arg,
7135 got_cancel_cb cancel_cb, void *cancel_arg)
7137 const struct got_error *err = NULL, *sync_err, *unlockerr;
7138 char *fileindex_path = NULL;
7139 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7141 err = get_fileindex_path(&fileindex_path, worktree);
7145 err = got_ref_resolve(&commit_id, repo, branch_ref);
7149 err = got_object_id_by_path(&tree_id, repo, commit_id,
7150 worktree->path_prefix);
7154 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7158 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7159 progress_cb, progress_arg, cancel_cb, cancel_arg);
7163 err = got_ref_change_ref(base_branch_ref, commit_id);
7167 err = got_ref_write(base_branch_ref, repo);
7171 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7173 sync_err = sync_fileindex(fileindex, fileindex_path);
7174 if (sync_err && err == NULL)
7178 unlockerr = got_ref_unlock(branch_ref);
7179 if (unlockerr && err == NULL)
7181 got_ref_close(branch_ref);
7183 unlockerr = got_ref_unlock(base_branch_ref);
7184 if (unlockerr && err == NULL)
7186 got_ref_close(base_branch_ref);
7188 got_fileindex_free(fileindex);
7189 free(fileindex_path);
7192 unlockerr = lock_worktree(worktree, LOCK_SH);
7193 if (unlockerr && err == NULL)
7198 const struct got_error *
7199 got_worktree_integrate_abort(struct got_worktree *worktree,
7200 struct got_fileindex *fileindex, struct got_repository *repo,
7201 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7203 const struct got_error *err = NULL, *unlockerr = NULL;
7205 got_fileindex_free(fileindex);
7207 err = lock_worktree(worktree, LOCK_SH);
7209 unlockerr = got_ref_unlock(branch_ref);
7210 if (unlockerr && err == NULL)
7212 got_ref_close(branch_ref);
7214 unlockerr = got_ref_unlock(base_branch_ref);
7215 if (unlockerr && err == NULL)
7217 got_ref_close(base_branch_ref);
7222 struct check_stage_ok_arg {
7223 struct got_object_id *head_commit_id;
7224 struct got_worktree *worktree;
7225 struct got_fileindex *fileindex;
7226 struct got_repository *repo;
7230 const struct got_error *
7231 check_stage_ok(void *arg, unsigned char status,
7232 unsigned char staged_status, const char *relpath,
7233 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7234 struct got_object_id *commit_id, int dirfd, const char *de_name)
7236 struct check_stage_ok_arg *a = arg;
7237 const struct got_error *err = NULL;
7238 struct got_fileindex_entry *ie;
7239 struct got_object_id base_commit_id;
7240 struct got_object_id *base_commit_idp = NULL;
7241 char *in_repo_path = NULL, *p;
7243 if (status == GOT_STATUS_UNVERSIONED ||
7244 status == GOT_STATUS_NO_CHANGE)
7246 if (status == GOT_STATUS_NONEXISTENT)
7247 return got_error_set_errno(ENOENT, relpath);
7249 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7251 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7253 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7254 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7256 return got_error_from_errno("asprintf");
7258 if (got_fileindex_entry_has_commit(ie)) {
7259 memcpy(base_commit_id.sha1, ie->commit_sha1,
7260 SHA1_DIGEST_LENGTH);
7261 base_commit_idp = &base_commit_id;
7264 if (status == GOT_STATUS_CONFLICT) {
7265 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7267 } else if (status != GOT_STATUS_ADD &&
7268 status != GOT_STATUS_MODIFY &&
7269 status != GOT_STATUS_DELETE) {
7270 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7274 a->have_changes = 1;
7279 err = check_out_of_date(p, status, staged_status,
7280 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7281 GOT_ERR_STAGE_OUT_OF_DATE);
7287 struct stage_path_arg {
7288 struct got_worktree *worktree;
7289 struct got_fileindex *fileindex;
7290 struct got_repository *repo;
7291 got_worktree_status_cb status_cb;
7293 got_worktree_patch_cb patch_cb;
7295 int staged_something;
7296 int allow_bad_symlinks;
7299 static const struct got_error *
7300 stage_path(void *arg, unsigned char status,
7301 unsigned char staged_status, const char *relpath,
7302 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7303 struct got_object_id *commit_id, int dirfd, const char *de_name)
7305 struct stage_path_arg *a = arg;
7306 const struct got_error *err = NULL;
7307 struct got_fileindex_entry *ie;
7308 char *ondisk_path = NULL, *path_content = NULL;
7310 struct got_object_id *new_staged_blob_id = NULL;
7313 if (status == GOT_STATUS_UNVERSIONED)
7316 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7318 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7320 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7322 return got_error_from_errno("asprintf");
7325 case GOT_STATUS_ADD:
7326 case GOT_STATUS_MODIFY:
7327 /* XXX could sb.st_mode be passed in by our caller? */
7328 if (lstat(ondisk_path, &sb) == -1) {
7329 err = got_error_from_errno2("lstat", ondisk_path);
7333 if (status == GOT_STATUS_ADD) {
7334 int choice = GOT_PATCH_CHOICE_NONE;
7335 err = (*a->patch_cb)(&choice, a->patch_arg,
7336 status, ie->path, NULL, 1, 1);
7339 if (choice != GOT_PATCH_CHOICE_YES)
7342 err = create_patched_content(&path_content, 0,
7343 staged_blob_id ? staged_blob_id : blob_id,
7344 ondisk_path, dirfd, de_name, ie->path,
7345 a->repo, a->patch_cb, a->patch_arg);
7346 if (err || path_content == NULL)
7350 err = got_object_blob_create(&new_staged_blob_id,
7351 path_content ? path_content : ondisk_path, a->repo);
7354 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7355 SHA1_DIGEST_LENGTH);
7356 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7357 stage = GOT_FILEIDX_STAGE_ADD;
7359 stage = GOT_FILEIDX_STAGE_MODIFY;
7360 got_fileindex_entry_stage_set(ie, stage);
7361 if (S_ISLNK(sb.st_mode)) {
7362 int is_bad_symlink = 0;
7363 if (!a->allow_bad_symlinks) {
7364 char target_path[PATH_MAX];
7366 target_len = readlink(ondisk_path, target_path,
7367 sizeof(target_path));
7368 if (target_len == -1) {
7369 err = got_error_from_errno2("readlink",
7373 err = is_bad_symlink_target(&is_bad_symlink,
7374 target_path, target_len, ondisk_path,
7375 a->worktree->root_path);
7378 if (is_bad_symlink) {
7379 err = got_error_path(ondisk_path,
7380 GOT_ERR_BAD_SYMLINK);
7385 got_fileindex_entry_staged_filetype_set(ie,
7386 GOT_FILEIDX_MODE_BAD_SYMLINK);
7388 got_fileindex_entry_staged_filetype_set(ie,
7389 GOT_FILEIDX_MODE_SYMLINK);
7391 got_fileindex_entry_staged_filetype_set(ie,
7392 GOT_FILEIDX_MODE_REGULAR_FILE);
7394 a->staged_something = 1;
7395 if (a->status_cb == NULL)
7397 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7398 get_staged_status(ie), relpath, blob_id,
7399 new_staged_blob_id, NULL, dirfd, de_name);
7401 case GOT_STATUS_DELETE:
7402 if (staged_status == GOT_STATUS_DELETE)
7405 int choice = GOT_PATCH_CHOICE_NONE;
7406 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7407 ie->path, NULL, 1, 1);
7410 if (choice == GOT_PATCH_CHOICE_NO)
7412 if (choice != GOT_PATCH_CHOICE_YES) {
7413 err = got_error(GOT_ERR_PATCH_CHOICE);
7417 stage = GOT_FILEIDX_STAGE_DELETE;
7418 got_fileindex_entry_stage_set(ie, stage);
7419 a->staged_something = 1;
7420 if (a->status_cb == NULL)
7422 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7423 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7426 case GOT_STATUS_NO_CHANGE:
7428 case GOT_STATUS_CONFLICT:
7429 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7431 case GOT_STATUS_NONEXISTENT:
7432 err = got_error_set_errno(ENOENT, relpath);
7435 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7439 if (path_content && unlink(path_content) == -1 && err == NULL)
7440 err = got_error_from_errno2("unlink", path_content);
7443 free(new_staged_blob_id);
7447 const struct got_error *
7448 got_worktree_stage(struct got_worktree *worktree,
7449 struct got_pathlist_head *paths,
7450 got_worktree_status_cb status_cb, void *status_arg,
7451 got_worktree_patch_cb patch_cb, void *patch_arg,
7452 int allow_bad_symlinks, struct got_repository *repo)
7454 const struct got_error *err = NULL, *sync_err, *unlockerr;
7455 struct got_pathlist_entry *pe;
7456 struct got_fileindex *fileindex = NULL;
7457 char *fileindex_path = NULL;
7458 struct got_reference *head_ref = NULL;
7459 struct got_object_id *head_commit_id = NULL;
7460 struct check_stage_ok_arg oka;
7461 struct stage_path_arg spa;
7463 err = lock_worktree(worktree, LOCK_EX);
7467 err = got_ref_open(&head_ref, repo,
7468 got_worktree_get_head_ref_name(worktree), 0);
7471 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7474 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7478 /* Check pre-conditions before staging anything. */
7479 oka.head_commit_id = head_commit_id;
7480 oka.worktree = worktree;
7481 oka.fileindex = fileindex;
7483 oka.have_changes = 0;
7484 TAILQ_FOREACH(pe, paths, entry) {
7485 err = worktree_status(worktree, pe->path, fileindex, repo,
7486 check_stage_ok, &oka, NULL, NULL, 0, 0);
7490 if (!oka.have_changes) {
7491 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7495 spa.worktree = worktree;
7496 spa.fileindex = fileindex;
7498 spa.patch_cb = patch_cb;
7499 spa.patch_arg = patch_arg;
7500 spa.status_cb = status_cb;
7501 spa.status_arg = status_arg;
7502 spa.staged_something = 0;
7503 spa.allow_bad_symlinks = allow_bad_symlinks;
7504 TAILQ_FOREACH(pe, paths, entry) {
7505 err = worktree_status(worktree, pe->path, fileindex, repo,
7506 stage_path, &spa, NULL, NULL, 0, 0);
7510 if (!spa.staged_something) {
7511 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7515 sync_err = sync_fileindex(fileindex, fileindex_path);
7516 if (sync_err && err == NULL)
7520 got_ref_close(head_ref);
7521 free(head_commit_id);
7522 free(fileindex_path);
7524 got_fileindex_free(fileindex);
7525 unlockerr = lock_worktree(worktree, LOCK_SH);
7526 if (unlockerr && err == NULL)
7531 struct unstage_path_arg {
7532 struct got_worktree *worktree;
7533 struct got_fileindex *fileindex;
7534 struct got_repository *repo;
7535 got_worktree_checkout_cb progress_cb;
7537 got_worktree_patch_cb patch_cb;
7541 static const struct got_error *
7542 create_unstaged_content(char **path_unstaged_content,
7543 char **path_new_staged_content, struct got_object_id *blob_id,
7544 struct got_object_id *staged_blob_id, const char *relpath,
7545 struct got_repository *repo,
7546 got_worktree_patch_cb patch_cb, void *patch_arg)
7548 const struct got_error *err, *free_err;
7549 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7550 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7551 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7552 struct got_diffreg_result *diffreg_result = NULL;
7553 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7554 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7556 *path_unstaged_content = NULL;
7557 *path_new_staged_content = NULL;
7559 err = got_object_id_str(&label1, blob_id);
7562 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7566 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7570 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7574 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7578 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7582 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7586 err = got_diff_files(&diffreg_result, f1, label1, f2,
7587 path2, 3, 0, 1, NULL);
7591 err = got_opentemp_named(path_unstaged_content, &outfile,
7592 "got-unstaged-content");
7595 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7596 "got-new-staged-content");
7600 if (fseek(f1, 0L, SEEK_SET) == -1) {
7601 err = got_ferror(f1, GOT_ERR_IO);
7604 if (fseek(f2, 0L, SEEK_SET) == -1) {
7605 err = got_ferror(f2, GOT_ERR_IO);
7608 /* Count the number of actual changes in the diff result. */
7609 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7610 struct diff_chunk_context cc = {};
7611 diff_chunk_context_load_change(&cc, &nchunks_used,
7612 diffreg_result->result, n, 0);
7615 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7617 err = apply_or_reject_change(&choice, &nchunks_used,
7618 diffreg_result->result, n, relpath, f1, f2,
7619 &line_cur1, &line_cur2,
7620 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7623 if (choice == GOT_PATCH_CHOICE_YES)
7626 have_rejected_content = 1;
7627 if (choice == GOT_PATCH_CHOICE_QUIT)
7630 if (have_content || have_rejected_content)
7631 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7632 outfile, rejectfile);
7636 got_object_blob_close(blob);
7638 got_object_blob_close(staged_blob);
7639 free_err = got_diffreg_result_free(diffreg_result);
7640 if (free_err && err == NULL)
7642 if (f1 && fclose(f1) == EOF && err == NULL)
7643 err = got_error_from_errno2("fclose", path1);
7644 if (f2 && fclose(f2) == EOF && err == NULL)
7645 err = got_error_from_errno2("fclose", path2);
7646 if (outfile && fclose(outfile) == EOF && err == NULL)
7647 err = got_error_from_errno2("fclose", *path_unstaged_content);
7648 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7649 err = got_error_from_errno2("fclose", *path_new_staged_content);
7650 if (path1 && unlink(path1) == -1 && err == NULL)
7651 err = got_error_from_errno2("unlink", path1);
7652 if (path2 && unlink(path2) == -1 && err == NULL)
7653 err = got_error_from_errno2("unlink", path2);
7654 if (err || !have_content) {
7655 if (*path_unstaged_content &&
7656 unlink(*path_unstaged_content) == -1 && err == NULL)
7657 err = got_error_from_errno2("unlink",
7658 *path_unstaged_content);
7659 free(*path_unstaged_content);
7660 *path_unstaged_content = NULL;
7662 if (err || !have_content || !have_rejected_content) {
7663 if (*path_new_staged_content &&
7664 unlink(*path_new_staged_content) == -1 && err == NULL)
7665 err = got_error_from_errno2("unlink",
7666 *path_new_staged_content);
7667 free(*path_new_staged_content);
7668 *path_new_staged_content = NULL;
7675 static const struct got_error *
7676 unstage_hunks(struct got_object_id *staged_blob_id,
7677 struct got_blob_object *blob_base,
7678 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7679 const char *ondisk_path, const char *label_orig,
7680 struct got_worktree *worktree, struct got_repository *repo,
7681 got_worktree_patch_cb patch_cb, void *patch_arg,
7682 got_worktree_checkout_cb progress_cb, void *progress_arg)
7684 const struct got_error *err = NULL;
7685 char *path_unstaged_content = NULL;
7686 char *path_new_staged_content = NULL;
7687 struct got_object_id *new_staged_blob_id = NULL;
7691 err = create_unstaged_content(&path_unstaged_content,
7692 &path_new_staged_content, blob_id, staged_blob_id,
7693 ie->path, repo, patch_cb, patch_arg);
7697 if (path_unstaged_content == NULL)
7700 if (path_new_staged_content) {
7701 err = got_object_blob_create(&new_staged_blob_id,
7702 path_new_staged_content, repo);
7707 f = fopen(path_unstaged_content, "r");
7709 err = got_error_from_errno2("fopen",
7710 path_unstaged_content);
7713 if (fstat(fileno(f), &sb) == -1) {
7714 err = got_error_from_errno2("fstat", path_unstaged_content);
7717 if (got_fileindex_entry_staged_filetype_get(ie) ==
7718 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7719 char link_target[PATH_MAX];
7721 r = fread(link_target, 1, sizeof(link_target), f);
7722 if (r == 0 && ferror(f)) {
7723 err = got_error_from_errno("fread");
7726 if (r >= sizeof(link_target)) { /* should not happen */
7727 err = got_error(GOT_ERR_NO_SPACE);
7730 link_target[r] = '\0';
7731 err = merge_symlink(worktree, blob_base,
7732 ondisk_path, ie->path, label_orig, link_target,
7733 worktree->base_commit_id, repo, progress_cb,
7736 int local_changes_subsumed;
7737 err = merge_file(&local_changes_subsumed, worktree,
7738 blob_base, ondisk_path, ie->path,
7739 got_fileindex_perms_to_st(ie),
7740 path_unstaged_content, label_orig, "unstaged",
7741 repo, progress_cb, progress_arg);
7746 if (new_staged_blob_id) {
7747 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7748 SHA1_DIGEST_LENGTH);
7750 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7751 got_fileindex_entry_staged_filetype_set(ie, 0);
7754 free(new_staged_blob_id);
7755 if (path_unstaged_content &&
7756 unlink(path_unstaged_content) == -1 && err == NULL)
7757 err = got_error_from_errno2("unlink", path_unstaged_content);
7758 if (path_new_staged_content &&
7759 unlink(path_new_staged_content) == -1 && err == NULL)
7760 err = got_error_from_errno2("unlink", path_new_staged_content);
7761 if (f && fclose(f) == EOF && err == NULL)
7762 err = got_error_from_errno2("fclose", path_unstaged_content);
7763 free(path_unstaged_content);
7764 free(path_new_staged_content);
7768 static const struct got_error *
7769 unstage_path(void *arg, unsigned char status,
7770 unsigned char staged_status, const char *relpath,
7771 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7772 struct got_object_id *commit_id, int dirfd, const char *de_name)
7774 const struct got_error *err = NULL;
7775 struct unstage_path_arg *a = arg;
7776 struct got_fileindex_entry *ie;
7777 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7778 char *ondisk_path = NULL;
7779 char *id_str = NULL, *label_orig = NULL;
7780 int local_changes_subsumed;
7783 if (staged_status != GOT_STATUS_ADD &&
7784 staged_status != GOT_STATUS_MODIFY &&
7785 staged_status != GOT_STATUS_DELETE)
7788 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7790 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7792 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7794 return got_error_from_errno("asprintf");
7796 err = got_object_id_str(&id_str,
7797 commit_id ? commit_id : a->worktree->base_commit_id);
7800 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7802 err = got_error_from_errno("asprintf");
7806 switch (staged_status) {
7807 case GOT_STATUS_MODIFY:
7808 err = got_object_open_as_blob(&blob_base, a->repo,
7813 case GOT_STATUS_ADD:
7815 if (staged_status == GOT_STATUS_ADD) {
7816 int choice = GOT_PATCH_CHOICE_NONE;
7817 err = (*a->patch_cb)(&choice, a->patch_arg,
7818 staged_status, ie->path, NULL, 1, 1);
7821 if (choice != GOT_PATCH_CHOICE_YES)
7824 err = unstage_hunks(staged_blob_id,
7825 blob_base, blob_id, ie, ondisk_path,
7826 label_orig, a->worktree, a->repo,
7827 a->patch_cb, a->patch_arg,
7828 a->progress_cb, a->progress_arg);
7829 break; /* Done with this file. */
7832 err = got_object_open_as_blob(&blob_staged, a->repo,
7833 staged_blob_id, 8192);
7836 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7837 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7838 case GOT_FILEIDX_MODE_REGULAR_FILE:
7839 err = merge_blob(&local_changes_subsumed, a->worktree,
7840 blob_base, ondisk_path, relpath,
7841 got_fileindex_perms_to_st(ie), label_orig,
7842 blob_staged, commit_id ? commit_id :
7843 a->worktree->base_commit_id, a->repo,
7844 a->progress_cb, a->progress_arg);
7846 case GOT_FILEIDX_MODE_SYMLINK:
7847 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7848 char *staged_target;
7849 err = got_object_blob_read_to_str(
7850 &staged_target, blob_staged);
7853 err = merge_symlink(a->worktree, blob_base,
7854 ondisk_path, relpath, label_orig,
7855 staged_target, commit_id ? commit_id :
7856 a->worktree->base_commit_id,
7857 a->repo, a->progress_cb, a->progress_arg);
7858 free(staged_target);
7860 err = merge_blob(&local_changes_subsumed,
7861 a->worktree, blob_base, ondisk_path,
7862 relpath, got_fileindex_perms_to_st(ie),
7863 label_orig, blob_staged,
7864 commit_id ? commit_id :
7865 a->worktree->base_commit_id, a->repo,
7866 a->progress_cb, a->progress_arg);
7870 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7874 got_fileindex_entry_stage_set(ie,
7875 GOT_FILEIDX_STAGE_NONE);
7876 got_fileindex_entry_staged_filetype_set(ie, 0);
7879 case GOT_STATUS_DELETE:
7881 int choice = GOT_PATCH_CHOICE_NONE;
7882 err = (*a->patch_cb)(&choice, a->patch_arg,
7883 staged_status, ie->path, NULL, 1, 1);
7886 if (choice == GOT_PATCH_CHOICE_NO)
7888 if (choice != GOT_PATCH_CHOICE_YES) {
7889 err = got_error(GOT_ERR_PATCH_CHOICE);
7893 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7894 got_fileindex_entry_staged_filetype_set(ie, 0);
7895 err = get_file_status(&status, &sb, ie, ondisk_path,
7896 dirfd, de_name, a->repo);
7899 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7905 got_object_blob_close(blob_base);
7907 got_object_blob_close(blob_staged);
7913 const struct got_error *
7914 got_worktree_unstage(struct got_worktree *worktree,
7915 struct got_pathlist_head *paths,
7916 got_worktree_checkout_cb progress_cb, void *progress_arg,
7917 got_worktree_patch_cb patch_cb, void *patch_arg,
7918 struct got_repository *repo)
7920 const struct got_error *err = NULL, *sync_err, *unlockerr;
7921 struct got_pathlist_entry *pe;
7922 struct got_fileindex *fileindex = NULL;
7923 char *fileindex_path = NULL;
7924 struct unstage_path_arg upa;
7926 err = lock_worktree(worktree, LOCK_EX);
7930 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7934 upa.worktree = worktree;
7935 upa.fileindex = fileindex;
7937 upa.progress_cb = progress_cb;
7938 upa.progress_arg = progress_arg;
7939 upa.patch_cb = patch_cb;
7940 upa.patch_arg = patch_arg;
7941 TAILQ_FOREACH(pe, paths, entry) {
7942 err = worktree_status(worktree, pe->path, fileindex, repo,
7943 unstage_path, &upa, NULL, NULL, 0, 0);
7948 sync_err = sync_fileindex(fileindex, fileindex_path);
7949 if (sync_err && err == NULL)
7952 free(fileindex_path);
7954 got_fileindex_free(fileindex);
7955 unlockerr = lock_worktree(worktree, LOCK_SH);
7956 if (unlockerr && err == NULL)
7961 struct report_file_info_arg {
7962 struct got_worktree *worktree;
7963 got_worktree_path_info_cb info_cb;
7965 struct got_pathlist_head *paths;
7966 got_cancel_cb cancel_cb;
7970 static const struct got_error *
7971 report_file_info(void *arg, struct got_fileindex_entry *ie)
7973 struct report_file_info_arg *a = arg;
7974 struct got_pathlist_entry *pe;
7975 struct got_object_id blob_id, staged_blob_id, commit_id;
7976 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
7977 struct got_object_id *commit_idp = NULL;
7980 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
7981 return got_error(GOT_ERR_CANCELLED);
7983 TAILQ_FOREACH(pe, a->paths, entry) {
7984 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
7985 got_path_is_child(ie->path, pe->path, pe->path_len))
7988 if (pe == NULL) /* not found */
7991 if (got_fileindex_entry_has_blob(ie)) {
7992 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
7993 blob_idp = &blob_id;
7995 stage = got_fileindex_entry_stage_get(ie);
7996 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
7997 stage == GOT_FILEIDX_STAGE_ADD) {
7998 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
7999 SHA1_DIGEST_LENGTH);
8000 staged_blob_idp = &staged_blob_id;
8003 if (got_fileindex_entry_has_commit(ie)) {
8004 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8005 commit_idp = &commit_id;
8008 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8009 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8012 const struct got_error *
8013 got_worktree_path_info(struct got_worktree *worktree,
8014 struct got_pathlist_head *paths,
8015 got_worktree_path_info_cb info_cb, void *info_arg,
8016 got_cancel_cb cancel_cb, void *cancel_arg)
8019 const struct got_error *err = NULL, *unlockerr;
8020 struct got_fileindex *fileindex = NULL;
8021 char *fileindex_path = NULL;
8022 struct report_file_info_arg arg;
8024 err = lock_worktree(worktree, LOCK_SH);
8028 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8032 arg.worktree = worktree;
8033 arg.info_cb = info_cb;
8034 arg.info_arg = info_arg;
8036 arg.cancel_cb = cancel_cb;
8037 arg.cancel_arg = cancel_arg;
8038 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8041 free(fileindex_path);
8043 got_fileindex_free(fileindex);
8044 unlockerr = lock_worktree(worktree, LOCK_UN);
8045 if (unlockerr && err == NULL)