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 the file at orig_path acts as the common
779 * ancestor, the file at deriv_path acts as the first derived version,
780 * and the file at ondisk_path acts as the second derived version.
782 static const struct got_error *
783 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
784 const char *orig_path, 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;
792 FILE *f_empty = NULL;
793 char *f_empty_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 if (orig_path == NULL) {
824 * If the file has no blob, this is an "add vs add" conflict,
825 * and we simply use an empty ancestor file to make both files
826 * appear in the merged result in their entirety.
828 err = got_opentemp_named(&f_empty_path, &f_empty, base_path);
834 * In order the run a 3-way merge with a symlink we copy the symlink's
835 * target path into a temporary file and use that file with diff3.
837 if (S_ISLNK(st_mode)) {
838 char target_path[PATH_MAX];
843 if (asprintf(&base_path, "%s/got-symlink-merge",
845 err = got_error_from_errno("asprintf");
849 err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
852 target_len = readlink(ondisk_path, target_path,
853 sizeof(target_path));
854 if (target_len == -1) {
855 err = got_error_from_errno2("readlink", ondisk_path);
858 n = fwrite(target_path, 1, target_len, symlinkf);
859 if (n != target_len) {
860 err = got_ferror(symlinkf, GOT_ERR_IO);
863 if (fflush(symlinkf) == EOF) {
864 err = got_error_from_errno2("fflush", symlink_path);
869 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
870 orig_path ? orig_path : f_empty_path,
871 symlink_path ? symlink_path : ondisk_path,
872 label_deriv, label_orig, NULL);
876 err = (*progress_cb)(progress_arg,
877 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
881 if (fsync(merged_fd) != 0) {
882 err = got_error_from_errno("fsync");
886 /* Check if a clean merge has subsumed all local changes. */
887 if (overlapcnt == 0) {
888 err = check_files_equal(local_changes_subsumed, deriv_path,
894 if (fchmod(merged_fd, st_mode) != 0) {
895 err = got_error_from_errno2("fchmod", merged_path);
899 if (rename(merged_path, ondisk_path) != 0) {
900 err = got_error_from_errno3("rename", merged_path,
910 if (unlink(symlink_path) == -1 && err == NULL)
911 err = got_error_from_errno2("unlink", symlink_path);
913 if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
914 err = got_error_from_errno2("fclose", symlink_path);
916 if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
917 err = got_error_from_errno("close");
918 if (f_empty && fclose(f_empty) == EOF && err == NULL)
919 err = got_error_from_errno("fclose");
923 if (unlink(f_empty_path) == -1 && err == NULL)
924 err = got_error_from_errno2("unlink", f_empty_path);
931 static const struct got_error *
932 update_symlink(const char *ondisk_path, const char *target_path,
935 /* This is not atomic but matches what 'ln -sf' does. */
936 if (unlink(ondisk_path) == -1)
937 return got_error_from_errno2("unlink", ondisk_path);
938 if (symlink(target_path, ondisk_path) == -1)
939 return got_error_from_errno3("symlink", target_path,
945 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
946 * in the work tree with a file that contains conflict markers and the
947 * conflicting target paths of the original version, a "derived version"
948 * of a symlink from an incoming change, and a local version of the symlink.
950 * The original versions's target path can be NULL if it is not available,
951 * such as if both derived versions added a new symlink at the same path.
953 * The incoming derived symlink target is NULL in case the incoming change
954 * has deleted this symlink.
956 static const struct got_error *
957 install_symlink_conflict(const char *deriv_target,
958 struct got_object_id *deriv_base_commit_id, const char *orig_target,
959 const char *label_orig, const char *local_target, const char *ondisk_path)
961 const struct got_error *err;
962 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
965 err = got_object_id_str(&id_str, deriv_base_commit_id);
967 return got_error_from_errno("asprintf");
969 if (asprintf(&label_deriv, "%s: commit %s",
970 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
971 err = got_error_from_errno("asprintf");
975 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
979 if (fchmod(fileno(f), GOT_DEFAULT_FILE_MODE) == -1) {
980 err = got_error_from_errno2("fchmod", path);
984 if (fprintf(f, "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n",
985 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv,
986 deriv_target ? deriv_target : "(symlink was deleted)",
987 orig_target ? label_orig : "",
988 orig_target ? "\n" : "",
989 orig_target ? orig_target : "",
990 orig_target ? "\n" : "",
991 GOT_DIFF_CONFLICT_MARKER_SEP,
992 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
993 err = got_error_from_errno2("fprintf", path);
997 if (unlink(ondisk_path) == -1) {
998 err = got_error_from_errno2("unlink", ondisk_path);
1001 if (rename(path, ondisk_path) == -1) {
1002 err = got_error_from_errno3("rename", path, ondisk_path);
1006 if (f != NULL && fclose(f) == EOF && err == NULL)
1007 err = got_error_from_errno2("fclose", path);
1014 /* forward declaration */
1015 static const struct got_error *
1016 merge_blob(int *, struct got_worktree *, struct got_blob_object *,
1017 const char *, const char *, uint16_t, const char *,
1018 struct got_blob_object *, struct got_object_id *,
1019 struct got_repository *, got_worktree_checkout_cb, void *);
1022 * Merge a symlink into the work tree, where blob_orig acts as the common
1023 * ancestor, deriv_target is the link target of the first derived version,
1024 * and the symlink on disk acts as the second derived version.
1025 * Assume that contents of both blobs represent symlinks.
1027 static const struct got_error *
1028 merge_symlink(struct got_worktree *worktree,
1029 struct got_blob_object *blob_orig, const char *ondisk_path,
1030 const char *path, const char *label_orig, const char *deriv_target,
1031 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1032 got_worktree_checkout_cb progress_cb, void *progress_arg)
1034 const struct got_error *err = NULL;
1035 char *ancestor_target = NULL;
1037 ssize_t ondisk_len, deriv_len;
1038 char ondisk_target[PATH_MAX];
1039 int have_local_change = 0;
1040 int have_incoming_change = 0;
1042 if (lstat(ondisk_path, &sb) == -1)
1043 return got_error_from_errno2("lstat", ondisk_path);
1045 ondisk_len = readlink(ondisk_path, ondisk_target,
1046 sizeof(ondisk_target));
1047 if (ondisk_len == -1) {
1048 err = got_error_from_errno2("readlink",
1052 ondisk_target[ondisk_len] = '\0';
1055 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
1060 if (ancestor_target == NULL ||
1061 (ondisk_len != strlen(ancestor_target) ||
1062 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
1063 have_local_change = 1;
1065 deriv_len = strlen(deriv_target);
1066 if (ancestor_target == NULL ||
1067 (deriv_len != strlen(ancestor_target) ||
1068 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
1069 have_incoming_change = 1;
1071 if (!have_local_change && !have_incoming_change) {
1072 if (ancestor_target) {
1073 /* Both sides made the same change. */
1074 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1076 } else if (deriv_len == ondisk_len &&
1077 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
1078 /* Both sides added the same symlink. */
1079 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1082 /* Both sides added symlinks which don't match. */
1083 err = install_symlink_conflict(deriv_target,
1084 deriv_base_commit_id, ancestor_target,
1085 label_orig, ondisk_target, ondisk_path);
1088 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1091 } else if (!have_local_change && have_incoming_change) {
1092 /* Apply the incoming change. */
1093 err = update_symlink(ondisk_path, deriv_target,
1094 strlen(deriv_target));
1097 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1098 } else if (have_local_change && have_incoming_change) {
1099 if (deriv_len == ondisk_len &&
1100 memcmp(deriv_target, ondisk_target, deriv_len) == 0) {
1101 /* Both sides made the same change. */
1102 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
1105 err = install_symlink_conflict(deriv_target,
1106 deriv_base_commit_id, ancestor_target, label_orig,
1107 ondisk_target, ondisk_path);
1110 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1116 free(ancestor_target);
1121 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1122 * blob_deriv acts as the first derived version, and the file on disk
1123 * acts as the second derived version.
1125 static const struct got_error *
1126 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1127 struct got_blob_object *blob_orig, const char *ondisk_path,
1128 const char *path, uint16_t st_mode, const char *label_orig,
1129 struct got_blob_object *blob_deriv,
1130 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1131 got_worktree_checkout_cb progress_cb, void *progress_arg)
1133 const struct got_error *err = NULL;
1134 FILE *f_orig = NULL, *f_deriv = NULL;
1135 char *blob_orig_path = NULL;
1136 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1137 char *label_deriv = NULL, *parent = NULL;
1139 *local_changes_subsumed = 0;
1141 err = got_path_dirname(&parent, ondisk_path);
1146 if (asprintf(&base_path, "%s/got-merge-blob-orig",
1148 err = got_error_from_errno("asprintf");
1153 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
1156 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
1164 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1165 err = got_error_from_errno("asprintf");
1170 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1173 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1178 err = got_object_id_str(&id_str, deriv_base_commit_id);
1181 if (asprintf(&label_deriv, "%s: commit %s",
1182 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1183 err = got_error_from_errno("asprintf");
1187 err = merge_file(local_changes_subsumed, worktree, blob_orig_path,
1188 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1189 label_deriv, repo, progress_cb, progress_arg);
1191 if (f_orig && fclose(f_orig) == EOF && err == NULL)
1192 err = got_error_from_errno("fclose");
1193 if (f_deriv && fclose(f_deriv) == EOF && err == NULL)
1194 err = got_error_from_errno("fclose");
1196 if (blob_orig_path) {
1197 unlink(blob_orig_path);
1198 free(blob_orig_path);
1200 if (blob_deriv_path) {
1201 unlink(blob_deriv_path);
1202 free(blob_deriv_path);
1210 static const struct got_error *
1211 create_fileindex_entry(struct got_fileindex_entry **new_iep,
1212 struct got_fileindex *fileindex, struct got_object_id *base_commit_id,
1213 int wt_fd, const char *path, struct got_object_id *blob_id)
1215 const struct got_error *err = NULL;
1216 struct got_fileindex_entry *new_ie;
1220 err = got_fileindex_entry_alloc(&new_ie, path);
1224 err = got_fileindex_entry_update(new_ie, wt_fd, path,
1225 blob_id->sha1, base_commit_id->sha1, 1);
1229 err = got_fileindex_entry_add(fileindex, new_ie);
1232 got_fileindex_entry_free(new_ie);
1239 get_ondisk_perms(int executable, mode_t st_mode)
1241 mode_t xbits = S_IXUSR;
1244 /* Map read bits to execute bits. */
1245 if (st_mode & S_IRGRP)
1247 if (st_mode & S_IROTH)
1249 return st_mode | xbits;
1252 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1255 /* forward declaration */
1256 static const struct got_error *
1257 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1258 const char *path, mode_t te_mode, mode_t st_mode,
1259 struct got_blob_object *blob, int restoring_missing_file,
1260 int reverting_versioned_file, int installing_bad_symlink,
1261 int path_is_unversioned, struct got_repository *repo,
1262 got_worktree_checkout_cb progress_cb, void *progress_arg);
1265 * This function assumes that the provided symlink target points at a
1266 * safe location in the work tree!
1268 static const struct got_error *
1269 replace_existing_symlink(int *did_something, const char *ondisk_path,
1270 const char *target_path, size_t target_len)
1272 const struct got_error *err = NULL;
1274 char etarget[PATH_MAX];
1280 * "Bad" symlinks (those pointing outside the work tree or into the
1281 * .got directory) are installed in the work tree as a regular file
1282 * which contains the bad symlink target path.
1283 * The new symlink target has already been checked for safety by our
1284 * caller. If we can successfully open a regular file then we simply
1285 * replace this file with a symlink below.
1287 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1290 return got_error_from_errno2("open", ondisk_path);
1292 /* We are updating an existing on-disk symlink. */
1293 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1295 return got_error_from_errno2("readlink", ondisk_path);
1297 if (elen == target_len &&
1298 memcmp(etarget, target_path, target_len) == 0)
1299 return NULL; /* nothing to do */
1303 err = update_symlink(ondisk_path, target_path, target_len);
1304 if (fd != -1 && close(fd) == -1 && err == NULL)
1305 err = got_error_from_errno2("close", ondisk_path);
1309 static const struct got_error *
1310 is_bad_symlink_target(int *is_bad_symlink, const char *target_path,
1311 size_t target_len, const char *ondisk_path, const char *wtroot_path)
1313 const struct got_error *err = NULL;
1314 char canonpath[PATH_MAX];
1315 char *path_got = NULL;
1317 *is_bad_symlink = 0;
1319 if (target_len >= sizeof(canonpath)) {
1320 *is_bad_symlink = 1;
1325 * We do not use realpath(3) to resolve the symlink's target
1326 * path because we don't want to resolve symlinks recursively.
1327 * Instead we make the path absolute and then canonicalize it.
1328 * Relative symlink target lookup should begin at the directory
1329 * in which the blob object is being installed.
1331 if (!got_path_is_absolute(target_path)) {
1332 char *abspath, *parent;
1333 err = got_path_dirname(&parent, ondisk_path);
1336 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1338 return got_error_from_errno("asprintf");
1341 if (strlen(abspath) >= sizeof(canonpath)) {
1342 err = got_error_path(abspath, GOT_ERR_BAD_PATH);
1346 err = got_canonpath(abspath, canonpath, sizeof(canonpath));
1351 err = got_canonpath(target_path, canonpath, sizeof(canonpath));
1356 /* Only allow symlinks pointing at paths within the work tree. */
1357 if (!got_path_is_child(canonpath, wtroot_path, strlen(wtroot_path))) {
1358 *is_bad_symlink = 1;
1362 /* Do not allow symlinks pointing into the .got directory. */
1363 if (asprintf(&path_got, "%s/%s", wtroot_path,
1364 GOT_WORKTREE_GOT_DIR) == -1)
1365 return got_error_from_errno("asprintf");
1366 if (got_path_is_child(canonpath, path_got, strlen(path_got)))
1367 *is_bad_symlink = 1;
1373 static const struct got_error *
1374 install_symlink(int *is_bad_symlink, struct got_worktree *worktree,
1375 const char *ondisk_path, const char *path, struct got_blob_object *blob,
1376 int restoring_missing_file, int reverting_versioned_file,
1377 int path_is_unversioned, struct got_repository *repo,
1378 got_worktree_checkout_cb progress_cb, void *progress_arg)
1380 const struct got_error *err = NULL;
1381 char target_path[PATH_MAX];
1382 size_t len, target_len = 0;
1383 char *path_got = NULL;
1384 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1385 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1387 *is_bad_symlink = 0;
1390 * Blob object content specifies the target path of the link.
1391 * If a symbolic link cannot be installed we instead create
1392 * a regular file which contains the link target path stored
1393 * in the blob object.
1396 err = got_object_blob_read_block(&len, blob);
1397 if (len + target_len >= sizeof(target_path)) {
1398 /* Path too long; install as a regular file. */
1399 *is_bad_symlink = 1;
1400 got_object_blob_rewind(blob);
1401 return install_blob(worktree, ondisk_path, path,
1402 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1403 restoring_missing_file, reverting_versioned_file,
1404 1, path_is_unversioned, repo, progress_cb,
1408 /* Skip blob object header first time around. */
1409 memcpy(target_path + target_len, buf + hdrlen,
1411 target_len += len - hdrlen;
1415 target_path[target_len] = '\0';
1417 err = is_bad_symlink_target(is_bad_symlink, target_path, target_len,
1418 ondisk_path, worktree->root_path);
1422 if (*is_bad_symlink) {
1423 /* install as a regular file */
1424 got_object_blob_rewind(blob);
1425 err = install_blob(worktree, ondisk_path, path,
1426 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1427 restoring_missing_file, reverting_versioned_file, 1,
1428 path_is_unversioned, repo, progress_cb, progress_arg);
1432 if (symlink(target_path, ondisk_path) == -1) {
1433 if (errno == EEXIST) {
1434 int symlink_replaced;
1435 if (path_is_unversioned) {
1436 err = (*progress_cb)(progress_arg,
1437 GOT_STATUS_UNVERSIONED, path);
1440 err = replace_existing_symlink(&symlink_replaced,
1441 ondisk_path, target_path, target_len);
1445 if (symlink_replaced) {
1446 err = (*progress_cb)(progress_arg,
1447 reverting_versioned_file ?
1449 GOT_STATUS_UPDATE, path);
1451 err = (*progress_cb)(progress_arg,
1452 GOT_STATUS_EXISTS, path);
1455 goto done; /* Nothing else to do. */
1458 if (errno == ENOENT) {
1460 err = got_path_dirname(&parent, ondisk_path);
1463 err = add_dir_on_disk(worktree, parent);
1468 * Retry, and fall through to error handling
1469 * below if this second attempt fails.
1471 if (symlink(target_path, ondisk_path) != -1) {
1472 err = NULL; /* success */
1477 /* Handle errors from first or second creation attempt. */
1478 if (errno == ENAMETOOLONG) {
1479 /* bad target path; install as a regular file */
1480 *is_bad_symlink = 1;
1481 got_object_blob_rewind(blob);
1482 err = install_blob(worktree, ondisk_path, path,
1483 GOT_DEFAULT_FILE_MODE, GOT_DEFAULT_FILE_MODE, blob,
1484 restoring_missing_file, reverting_versioned_file, 1,
1485 path_is_unversioned, repo,
1486 progress_cb, progress_arg);
1487 } else if (errno == ENOTDIR) {
1488 err = got_error_path(ondisk_path,
1489 GOT_ERR_FILE_OBSTRUCTED);
1491 err = got_error_from_errno3("symlink",
1492 target_path, ondisk_path);
1494 } else if (progress_cb)
1495 err = (*progress_cb)(progress_arg, reverting_versioned_file ?
1496 GOT_STATUS_REVERT : GOT_STATUS_ADD, path);
1502 static const struct got_error *
1503 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1504 const char *path, mode_t te_mode, mode_t st_mode,
1505 struct got_blob_object *blob, int restoring_missing_file,
1506 int reverting_versioned_file, int installing_bad_symlink,
1507 int path_is_unversioned, struct got_repository *repo,
1508 got_worktree_checkout_cb progress_cb, void *progress_arg)
1510 const struct got_error *err = NULL;
1514 char *tmppath = NULL;
1516 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1517 GOT_DEFAULT_FILE_MODE);
1519 if (errno == ENOENT) {
1521 err = got_path_dirname(&parent, path);
1524 err = add_dir_on_disk(worktree, parent);
1528 fd = open(ondisk_path,
1529 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1530 GOT_DEFAULT_FILE_MODE);
1532 return got_error_from_errno2("open",
1534 } else if (errno == EEXIST) {
1535 if (path_is_unversioned) {
1536 err = (*progress_cb)(progress_arg,
1537 GOT_STATUS_UNVERSIONED, path);
1540 if (!(S_ISLNK(st_mode) && S_ISREG(te_mode)) &&
1541 !S_ISREG(st_mode) && !installing_bad_symlink) {
1542 /* TODO file is obstructed; do something */
1543 err = got_error_path(ondisk_path,
1544 GOT_ERR_FILE_OBSTRUCTED);
1547 err = got_opentemp_named_fd(&tmppath, &fd,
1554 return got_error_from_errno2("open", ondisk_path);
1557 if (fchmod(fd, get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1558 err = got_error_from_errno2("fchmod",
1559 update ? tmppath : ondisk_path);
1564 if (restoring_missing_file)
1565 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1567 else if (reverting_versioned_file)
1568 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1571 err = (*progress_cb)(progress_arg,
1572 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1577 hdrlen = got_object_blob_get_hdrlen(blob);
1579 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1580 err = got_object_blob_read_block(&len, blob);
1584 /* Skip blob object header first time around. */
1585 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1587 err = got_error_from_errno("write");
1589 } else if (outlen != len - hdrlen) {
1590 err = got_error(GOT_ERR_IO);
1597 if (fsync(fd) != 0) {
1598 err = got_error_from_errno("fsync");
1603 if (S_ISLNK(st_mode) && unlink(ondisk_path) == -1) {
1604 err = got_error_from_errno2("unlink", ondisk_path);
1607 if (rename(tmppath, ondisk_path) != 0) {
1608 err = got_error_from_errno3("rename", tmppath,
1617 if (fd != -1 && close(fd) == -1 && err == NULL)
1618 err = got_error_from_errno("close");
1619 if (tmppath != NULL && unlink(tmppath) == -1 && err == NULL)
1620 err = got_error_from_errno2("unlink", tmppath);
1625 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1626 static const struct got_error *
1627 get_modified_file_content_status(unsigned char *status, FILE *f)
1629 const struct got_error *err = NULL;
1630 const char *markers[3] = {
1631 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1632 GOT_DIFF_CONFLICT_MARKER_SEP,
1633 GOT_DIFF_CONFLICT_MARKER_END
1637 size_t linesize = 0;
1640 while (*status == GOT_STATUS_MODIFY) {
1641 linelen = getline(&line, &linesize, f);
1642 if (linelen == -1) {
1645 err = got_ferror(f, GOT_ERR_IO);
1649 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1650 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1652 *status = GOT_STATUS_CONFLICT;
1663 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1665 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1666 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1670 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1672 return !(ie->ctime_sec == sb->st_ctim.tv_sec &&
1673 ie->ctime_nsec == sb->st_ctim.tv_nsec &&
1674 ie->mtime_sec == sb->st_mtim.tv_sec &&
1675 ie->mtime_nsec == sb->st_mtim.tv_nsec &&
1676 ie->size == (sb->st_size & 0xffffffff) &&
1677 !xbit_differs(ie, sb->st_mode));
1680 static unsigned char
1681 get_staged_status(struct got_fileindex_entry *ie)
1683 switch (got_fileindex_entry_stage_get(ie)) {
1684 case GOT_FILEIDX_STAGE_ADD:
1685 return GOT_STATUS_ADD;
1686 case GOT_FILEIDX_STAGE_DELETE:
1687 return GOT_STATUS_DELETE;
1688 case GOT_FILEIDX_STAGE_MODIFY:
1689 return GOT_STATUS_MODIFY;
1691 return GOT_STATUS_NO_CHANGE;
1695 static const struct got_error *
1696 get_symlink_modification_status(unsigned char *status,
1697 struct got_fileindex_entry *ie, const char *abspath,
1698 int dirfd, const char *de_name, struct got_blob_object *blob)
1700 const struct got_error *err = NULL;
1701 char target_path[PATH_MAX];
1702 char etarget[PATH_MAX];
1704 size_t len, target_len = 0;
1705 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1706 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1708 *status = GOT_STATUS_NO_CHANGE;
1710 /* Blob object content specifies the target path of the link. */
1712 err = got_object_blob_read_block(&len, blob);
1715 if (len + target_len >= sizeof(target_path)) {
1717 * Should not happen. The blob contents were OK
1718 * when this symlink was installed.
1720 return got_error(GOT_ERR_NO_SPACE);
1723 /* Skip blob object header first time around. */
1724 memcpy(target_path + target_len, buf + hdrlen,
1726 target_len += len - hdrlen;
1730 target_path[target_len] = '\0';
1733 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1735 return got_error_from_errno2("readlinkat", abspath);
1737 elen = readlink(abspath, etarget, sizeof(etarget));
1739 return got_error_from_errno2("readlink", abspath);
1742 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1743 *status = GOT_STATUS_MODIFY;
1748 static const struct got_error *
1749 get_file_status(unsigned char *status, struct stat *sb,
1750 struct got_fileindex_entry *ie, const char *abspath,
1751 int dirfd, const char *de_name, struct got_repository *repo)
1753 const struct got_error *err = NULL;
1754 struct got_object_id id;
1759 struct got_blob_object *blob = NULL;
1761 unsigned char staged_status = get_staged_status(ie);
1763 *status = GOT_STATUS_NO_CHANGE;
1764 memset(sb, 0, sizeof(*sb));
1767 * Whenever the caller provides a directory descriptor and a
1768 * directory entry name for the file, use them! This prevents
1769 * race conditions if filesystem paths change beneath our feet.
1772 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1773 if (errno == ENOENT) {
1774 if (got_fileindex_entry_has_file_on_disk(ie))
1775 *status = GOT_STATUS_MISSING;
1777 *status = GOT_STATUS_DELETE;
1780 err = got_error_from_errno2("fstatat", abspath);
1784 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1785 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1786 return got_error_from_errno2("open", abspath);
1787 else if (fd == -1 && errno == ELOOP) {
1788 if (lstat(abspath, sb) == -1)
1789 return got_error_from_errno2("lstat", abspath);
1790 } else if (fd == -1 || fstat(fd, sb) == -1) {
1791 if (errno == ENOENT) {
1792 if (got_fileindex_entry_has_file_on_disk(ie))
1793 *status = GOT_STATUS_MISSING;
1795 *status = GOT_STATUS_DELETE;
1798 err = got_error_from_errno2("fstat", abspath);
1803 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1804 *status = GOT_STATUS_OBSTRUCTED;
1808 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1809 *status = GOT_STATUS_DELETE;
1811 } else if (!got_fileindex_entry_has_blob(ie) &&
1812 staged_status != GOT_STATUS_ADD) {
1813 *status = GOT_STATUS_ADD;
1817 if (!stat_info_differs(ie, sb))
1820 if (S_ISLNK(sb->st_mode) &&
1821 got_fileindex_entry_filetype_get(ie) != GOT_FILEIDX_MODE_SYMLINK) {
1822 *status = GOT_STATUS_MODIFY;
1826 if (staged_status == GOT_STATUS_MODIFY ||
1827 staged_status == GOT_STATUS_ADD)
1828 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1830 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1832 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1836 if (S_ISLNK(sb->st_mode)) {
1837 err = get_symlink_modification_status(status, ie,
1838 abspath, dirfd, de_name, blob);
1843 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1845 err = got_error_from_errno2("openat", abspath);
1850 f = fdopen(fd, "r");
1852 err = got_error_from_errno2("fdopen", abspath);
1856 hdrlen = got_object_blob_get_hdrlen(blob);
1858 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1859 err = got_object_blob_read_block(&blen, blob);
1862 /* Skip length of blob object header first time around. */
1863 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1864 if (flen == 0 && ferror(f)) {
1865 err = got_error_from_errno("fread");
1868 if (blen - hdrlen == 0) {
1870 *status = GOT_STATUS_MODIFY;
1872 } else if (flen == 0) {
1873 if (blen - hdrlen != 0)
1874 *status = GOT_STATUS_MODIFY;
1876 } else if (blen - hdrlen == flen) {
1877 /* Skip blob object header first time around. */
1878 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1879 *status = GOT_STATUS_MODIFY;
1883 *status = GOT_STATUS_MODIFY;
1889 if (*status == GOT_STATUS_MODIFY) {
1891 err = get_modified_file_content_status(status, f);
1892 } else if (xbit_differs(ie, sb->st_mode))
1893 *status = GOT_STATUS_MODE_CHANGE;
1896 got_object_blob_close(blob);
1897 if (f != NULL && fclose(f) == EOF && err == NULL)
1898 err = got_error_from_errno2("fclose", abspath);
1899 if (fd != -1 && close(fd) == -1 && err == NULL)
1900 err = got_error_from_errno2("close", abspath);
1905 * Update timestamps in the file index if a file is unmodified and
1906 * we had to run a full content comparison to find out.
1908 static const struct got_error *
1909 sync_timestamps(int wt_fd, const char *path, unsigned char status,
1910 struct got_fileindex_entry *ie, struct stat *sb)
1912 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1913 return got_fileindex_entry_update(ie, wt_fd, path,
1914 ie->blob_sha1, ie->commit_sha1, 1);
1919 static const struct got_error *
1920 update_blob(struct got_worktree *worktree,
1921 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1922 struct got_tree_entry *te, const char *path,
1923 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1926 const struct got_error *err = NULL;
1927 struct got_blob_object *blob = NULL;
1929 unsigned char status = GOT_STATUS_NO_CHANGE;
1932 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1933 return got_error_from_errno("asprintf");
1936 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1937 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1940 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1944 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1945 sb.st_mode = got_fileindex_perms_to_st(ie);
1947 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1948 status = GOT_STATUS_UNVERSIONED;
1951 if (status == GOT_STATUS_OBSTRUCTED) {
1952 err = (*progress_cb)(progress_arg, status, path);
1955 if (status == GOT_STATUS_CONFLICT) {
1956 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1961 if (ie && status != GOT_STATUS_MISSING && S_ISREG(sb.st_mode) &&
1962 (S_ISLNK(te->mode) ||
1963 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR))) {
1965 * This is a regular file or an installed bad symlink.
1966 * If the file index indicates that this file is already
1967 * up-to-date with respect to the repository we can skip
1968 * updating contents of this file.
1970 if (got_fileindex_entry_has_commit(ie) &&
1971 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1972 SHA1_DIGEST_LENGTH) == 0) {
1974 err = sync_timestamps(worktree->root_fd,
1975 path, status, ie, &sb);
1978 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1982 if (got_fileindex_entry_has_blob(ie) &&
1983 memcmp(ie->blob_sha1, te->id.sha1,
1984 SHA1_DIGEST_LENGTH) == 0) {
1985 /* Different commit but the same blob. */
1986 err = sync_timestamps(worktree->root_fd,
1987 path, status, ie, &sb);
1990 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1996 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
2000 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
2001 int update_timestamps;
2002 struct got_blob_object *blob2 = NULL;
2003 char *label_orig = NULL;
2004 if (got_fileindex_entry_has_blob(ie)) {
2005 struct got_object_id id2;
2006 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2007 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
2011 if (got_fileindex_entry_has_commit(ie)) {
2012 char id_str[SHA1_DIGEST_STRING_LENGTH];
2013 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
2014 sizeof(id_str)) == NULL) {
2015 err = got_error_path(id_str,
2016 GOT_ERR_BAD_OBJ_ID_STR);
2019 if (asprintf(&label_orig, "%s: commit %s",
2020 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2021 err = got_error_from_errno("asprintf");
2025 if (S_ISLNK(te->mode) && S_ISLNK(sb.st_mode)) {
2027 err = got_object_blob_read_to_str(&link_target, blob);
2030 err = merge_symlink(worktree, blob2, ondisk_path, path,
2031 label_orig, link_target, worktree->base_commit_id,
2032 repo, progress_cb, progress_arg);
2035 err = merge_blob(&update_timestamps, worktree, blob2,
2036 ondisk_path, path, sb.st_mode, label_orig, blob,
2037 worktree->base_commit_id, repo,
2038 progress_cb, progress_arg);
2042 got_object_blob_close(blob2);
2046 * Do not update timestamps of files with local changes.
2047 * Otherwise, a future status walk would treat them as
2048 * unmodified files again.
2050 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2051 blob->id.sha1, worktree->base_commit_id->sha1,
2053 } else if (status == GOT_STATUS_MODE_CHANGE) {
2054 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2055 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2056 } else if (status == GOT_STATUS_DELETE) {
2057 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
2060 err = got_fileindex_entry_update(ie, worktree->root_fd, path,
2061 blob->id.sha1, worktree->base_commit_id->sha1, 0);
2065 int is_bad_symlink = 0;
2066 if (S_ISLNK(te->mode)) {
2067 err = install_symlink(&is_bad_symlink, worktree,
2068 ondisk_path, path, blob,
2069 status == GOT_STATUS_MISSING, 0,
2070 status == GOT_STATUS_UNVERSIONED, repo,
2071 progress_cb, progress_arg);
2073 err = install_blob(worktree, ondisk_path, path,
2074 te->mode, sb.st_mode, blob,
2075 status == GOT_STATUS_MISSING, 0, 0,
2076 status == GOT_STATUS_UNVERSIONED, repo,
2077 progress_cb, progress_arg);
2083 err = got_fileindex_entry_update(ie,
2084 worktree->root_fd, path, blob->id.sha1,
2085 worktree->base_commit_id->sha1, 1);
2087 err = create_fileindex_entry(&ie, fileindex,
2088 worktree->base_commit_id, worktree->root_fd, path,
2094 if (is_bad_symlink) {
2095 got_fileindex_entry_filetype_set(ie,
2096 GOT_FILEIDX_MODE_BAD_SYMLINK);
2099 got_object_blob_close(blob);
2105 static const struct got_error *
2106 remove_ondisk_file(const char *root_path, const char *path)
2108 const struct got_error *err = NULL;
2109 char *ondisk_path = NULL;
2111 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
2112 return got_error_from_errno("asprintf");
2114 if (unlink(ondisk_path) == -1) {
2115 if (errno != ENOENT)
2116 err = got_error_from_errno2("unlink", ondisk_path);
2118 size_t root_len = strlen(root_path);
2121 err = got_path_dirname(&parent, ondisk_path);
2125 ondisk_path = parent;
2126 if (rmdir(ondisk_path) == -1) {
2127 if (errno != ENOTEMPTY)
2128 err = got_error_from_errno2("rmdir",
2132 } while (got_path_cmp(ondisk_path, root_path,
2133 strlen(ondisk_path), root_len) != 0);
2139 static const struct got_error *
2140 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
2141 struct got_fileindex_entry *ie, struct got_repository *repo,
2142 got_worktree_checkout_cb progress_cb, void *progress_arg)
2144 const struct got_error *err = NULL;
2145 unsigned char status;
2149 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
2150 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
2152 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
2154 return got_error_from_errno("asprintf");
2156 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
2160 if (S_ISLNK(sb.st_mode) && status != GOT_STATUS_NO_CHANGE) {
2161 char ondisk_target[PATH_MAX];
2162 ssize_t ondisk_len = readlink(ondisk_path, ondisk_target,
2163 sizeof(ondisk_target));
2164 if (ondisk_len == -1) {
2165 err = got_error_from_errno2("readlink", ondisk_path);
2168 ondisk_target[ondisk_len] = '\0';
2169 err = install_symlink_conflict(NULL, worktree->base_commit_id,
2170 NULL, NULL, /* XXX pass common ancestor info? */
2171 ondisk_target, ondisk_path);
2174 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
2179 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
2180 status == GOT_STATUS_ADD) {
2181 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
2185 * Preserve the working file and change the deleted blob's
2186 * entry into a schedule-add entry.
2188 err = got_fileindex_entry_update(ie, worktree->root_fd,
2189 ie->path, NULL, NULL, 0);
2191 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
2194 if (status == GOT_STATUS_NO_CHANGE) {
2195 err = remove_ondisk_file(worktree->root_path, ie->path);
2199 got_fileindex_entry_remove(fileindex, ie);
2206 struct diff_cb_arg {
2207 struct got_fileindex *fileindex;
2208 struct got_worktree *worktree;
2209 struct got_repository *repo;
2210 got_worktree_checkout_cb progress_cb;
2212 got_cancel_cb cancel_cb;
2216 static const struct got_error *
2217 diff_old_new(void *arg, struct got_fileindex_entry *ie,
2218 struct got_tree_entry *te, const char *parent_path)
2220 struct diff_cb_arg *a = arg;
2222 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2223 return got_error(GOT_ERR_CANCELLED);
2225 return update_blob(a->worktree, a->fileindex, ie, te,
2226 ie->path, a->repo, a->progress_cb, a->progress_arg);
2229 static const struct got_error *
2230 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2232 struct diff_cb_arg *a = arg;
2234 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2235 return got_error(GOT_ERR_CANCELLED);
2237 return delete_blob(a->worktree, a->fileindex, ie,
2238 a->repo, a->progress_cb, a->progress_arg);
2241 static const struct got_error *
2242 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
2244 struct diff_cb_arg *a = arg;
2245 const struct got_error *err;
2248 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2249 return got_error(GOT_ERR_CANCELLED);
2251 if (got_object_tree_entry_is_submodule(te))
2254 if (asprintf(&path, "%s%s%s", parent_path,
2255 parent_path[0] ? "/" : "", te->name)
2257 return got_error_from_errno("asprintf");
2259 if (S_ISDIR(te->mode))
2260 err = add_dir_on_disk(a->worktree, path);
2262 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2263 a->repo, a->progress_cb, a->progress_arg);
2269 const struct got_error *
2270 got_worktree_get_uuid(char **uuidstr, struct got_worktree *worktree)
2272 uint32_t uuid_status;
2274 uuid_to_string(&worktree->uuid, uuidstr, &uuid_status);
2275 if (uuid_status != uuid_s_ok) {
2277 return got_error_uuid(uuid_status, "uuid_to_string");
2283 static const struct got_error *
2284 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2286 const struct got_error *err = NULL;
2287 char *uuidstr = NULL;
2291 err = got_worktree_get_uuid(&uuidstr, worktree);
2295 if (asprintf(refname, "%s-%s", prefix, uuidstr) == -1) {
2296 err = got_error_from_errno("asprintf");
2303 const struct got_error *
2304 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2306 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2309 static const struct got_error *
2310 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2312 return get_ref_name(refname, worktree,
2313 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2316 static const struct got_error *
2317 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2319 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2322 static const struct got_error *
2323 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2325 return get_ref_name(refname, worktree,
2326 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2329 static const struct got_error *
2330 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2332 return get_ref_name(refname, worktree,
2333 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2336 static const struct got_error *
2337 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2339 return get_ref_name(refname, worktree,
2340 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2343 static const struct got_error *
2344 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2346 return get_ref_name(refname, worktree,
2347 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2350 static const struct got_error *
2351 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2353 return get_ref_name(refname, worktree,
2354 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2357 static const struct got_error *
2358 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2360 return get_ref_name(refname, worktree,
2361 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2364 const struct got_error *
2365 got_worktree_get_histedit_script_path(char **path,
2366 struct got_worktree *worktree)
2368 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2369 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2371 return got_error_from_errno("asprintf");
2377 * Prevent Git's garbage collector from deleting our base commit by
2378 * setting a reference to our base commit's ID.
2380 static const struct got_error *
2381 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2383 const struct got_error *err = NULL;
2384 struct got_reference *ref = NULL;
2387 err = got_worktree_get_base_ref_name(&refname, worktree);
2391 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2395 err = got_ref_write(ref, repo);
2403 static const struct got_error *
2404 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2406 const struct got_error *err = NULL;
2408 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2409 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2410 err = got_error_from_errno("asprintf");
2411 *fileindex_path = NULL;
2417 static const struct got_error *
2418 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2419 struct got_worktree *worktree)
2421 const struct got_error *err = NULL;
2424 *fileindex_path = NULL;
2425 *fileindex = got_fileindex_alloc();
2426 if (*fileindex == NULL)
2427 return got_error_from_errno("got_fileindex_alloc");
2429 err = get_fileindex_path(fileindex_path, worktree);
2433 index = fopen(*fileindex_path, "rb");
2434 if (index == NULL) {
2435 if (errno != ENOENT)
2436 err = got_error_from_errno2("fopen", *fileindex_path);
2438 err = got_fileindex_read(*fileindex, index);
2439 if (fclose(index) == EOF && err == NULL)
2440 err = got_error_from_errno("fclose");
2444 free(*fileindex_path);
2445 *fileindex_path = NULL;
2446 got_fileindex_free(*fileindex);
2452 struct bump_base_commit_id_arg {
2453 struct got_object_id *base_commit_id;
2456 const char *entry_name;
2457 got_worktree_checkout_cb progress_cb;
2461 /* Bump base commit ID of all files within an updated part of the work tree. */
2462 static const struct got_error *
2463 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2465 const struct got_error *err;
2466 struct bump_base_commit_id_arg *a = arg;
2468 if (a->entry_name) {
2469 if (strcmp(ie->path, a->path) != 0)
2471 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2474 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2475 SHA1_DIGEST_LENGTH) == 0)
2478 if (a->progress_cb) {
2479 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2484 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2488 static const struct got_error *
2489 bump_base_commit_id_everywhere(struct got_worktree *worktree,
2490 struct got_fileindex *fileindex,
2491 got_worktree_checkout_cb progress_cb, void *progress_arg)
2493 struct bump_base_commit_id_arg bbc_arg;
2495 bbc_arg.base_commit_id = worktree->base_commit_id;
2496 bbc_arg.entry_name = NULL;
2498 bbc_arg.path_len = 0;
2499 bbc_arg.progress_cb = progress_cb;
2500 bbc_arg.progress_arg = progress_arg;
2502 return got_fileindex_for_each_entry_safe(fileindex,
2503 bump_base_commit_id, &bbc_arg);
2506 static const struct got_error *
2507 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2509 const struct got_error *err = NULL;
2510 char *new_fileindex_path = NULL;
2511 FILE *new_index = NULL;
2512 struct timespec timeout;
2514 err = got_opentemp_named(&new_fileindex_path, &new_index,
2519 err = got_fileindex_write(fileindex, new_index);
2523 if (rename(new_fileindex_path, fileindex_path) != 0) {
2524 err = got_error_from_errno3("rename", new_fileindex_path,
2526 unlink(new_fileindex_path);
2530 * Sleep for a short amount of time to ensure that files modified after
2531 * this program exits have a different time stamp from the one which
2532 * was recorded in the file index.
2535 timeout.tv_nsec = 1;
2536 nanosleep(&timeout, NULL);
2540 free(new_fileindex_path);
2544 static const struct got_error *
2545 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2546 struct got_object_id **tree_id, const char *wt_relpath,
2547 struct got_worktree *worktree, struct got_repository *repo)
2549 const struct got_error *err = NULL;
2550 struct got_object_id *id = NULL;
2551 char *in_repo_path = NULL;
2552 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2554 *entry_type = GOT_OBJ_TYPE_ANY;
2555 *tree_relpath = NULL;
2558 if (wt_relpath[0] == '\0') {
2559 /* Check out all files within the work tree. */
2560 *entry_type = GOT_OBJ_TYPE_TREE;
2561 *tree_relpath = strdup("");
2562 if (*tree_relpath == NULL) {
2563 err = got_error_from_errno("strdup");
2566 err = got_object_id_by_path(tree_id, repo,
2567 worktree->base_commit_id, worktree->path_prefix);
2573 /* Check out a subset of files in the work tree. */
2575 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2576 is_root_wt ? "" : "/", wt_relpath) == -1) {
2577 err = got_error_from_errno("asprintf");
2581 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2587 in_repo_path = NULL;
2589 err = got_object_get_type(entry_type, repo, id);
2593 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2594 /* Check out a single file. */
2595 if (strchr(wt_relpath, '/') == NULL) {
2596 /* Check out a single file in work tree's root dir. */
2597 in_repo_path = strdup(worktree->path_prefix);
2598 if (in_repo_path == NULL) {
2599 err = got_error_from_errno("strdup");
2602 *tree_relpath = strdup("");
2603 if (*tree_relpath == NULL) {
2604 err = got_error_from_errno("strdup");
2608 /* Check out a single file in a subdirectory. */
2609 err = got_path_dirname(tree_relpath, wt_relpath);
2612 if (asprintf(&in_repo_path, "%s%s%s",
2613 worktree->path_prefix, is_root_wt ? "" : "/",
2614 *tree_relpath) == -1) {
2615 err = got_error_from_errno("asprintf");
2619 err = got_object_id_by_path(tree_id, repo,
2620 worktree->base_commit_id, in_repo_path);
2622 /* Check out all files within a subdirectory. */
2623 *tree_id = got_object_id_dup(id);
2624 if (*tree_id == NULL) {
2625 err = got_error_from_errno("got_object_id_dup");
2628 *tree_relpath = strdup(wt_relpath);
2629 if (*tree_relpath == NULL) {
2630 err = got_error_from_errno("strdup");
2638 *entry_type = GOT_OBJ_TYPE_ANY;
2639 free(*tree_relpath);
2640 *tree_relpath = NULL;
2647 static const struct got_error *
2648 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2649 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2650 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2651 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2653 const struct got_error *err = NULL;
2654 struct got_commit_object *commit = NULL;
2655 struct got_tree_object *tree = NULL;
2656 struct got_fileindex_diff_tree_cb diff_cb;
2657 struct diff_cb_arg arg;
2659 err = ref_base_commit(worktree, repo);
2661 if (!(err->code == GOT_ERR_ERRNO &&
2662 (errno == EACCES || errno == EROFS)))
2664 err = (*progress_cb)(progress_arg,
2665 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2670 err = got_object_open_as_commit(&commit, repo,
2671 worktree->base_commit_id);
2675 err = got_object_open_as_tree(&tree, repo, tree_id);
2680 got_object_tree_find_entry(tree, entry_name) == NULL) {
2681 err = got_error_path(entry_name, GOT_ERR_NO_TREE_ENTRY);
2685 diff_cb.diff_old_new = diff_old_new;
2686 diff_cb.diff_old = diff_old;
2687 diff_cb.diff_new = diff_new;
2688 arg.fileindex = fileindex;
2689 arg.worktree = worktree;
2691 arg.progress_cb = progress_cb;
2692 arg.progress_arg = progress_arg;
2693 arg.cancel_cb = cancel_cb;
2694 arg.cancel_arg = cancel_arg;
2695 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2696 entry_name, repo, &diff_cb, &arg);
2699 got_object_tree_close(tree);
2701 got_object_commit_close(commit);
2705 const struct got_error *
2706 got_worktree_checkout_files(struct got_worktree *worktree,
2707 struct got_pathlist_head *paths, struct got_repository *repo,
2708 got_worktree_checkout_cb progress_cb, void *progress_arg,
2709 got_cancel_cb cancel_cb, void *cancel_arg)
2711 const struct got_error *err = NULL, *sync_err, *unlockerr;
2712 struct got_commit_object *commit = NULL;
2713 struct got_tree_object *tree = NULL;
2714 struct got_fileindex *fileindex = NULL;
2715 char *fileindex_path = NULL;
2716 struct got_pathlist_entry *pe;
2717 struct tree_path_data {
2718 SIMPLEQ_ENTRY(tree_path_data) entry;
2719 struct got_object_id *tree_id;
2724 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2726 SIMPLEQ_INIT(&tree_paths);
2728 err = lock_worktree(worktree, LOCK_EX);
2732 /* Map all specified paths to in-repository trees. */
2733 TAILQ_FOREACH(pe, paths, entry) {
2734 tpd = malloc(sizeof(*tpd));
2736 err = got_error_from_errno("malloc");
2740 err = find_tree_entry_for_checkout(&tpd->entry_type,
2741 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2747 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2748 err = got_path_basename(&tpd->entry_name, pe->path);
2756 tpd->entry_name = NULL;
2758 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2762 * Read the file index.
2763 * Checking out files is supposed to be an idempotent operation.
2764 * If the on-disk file index is incomplete we will try to complete it.
2766 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2770 tpd = SIMPLEQ_FIRST(&tree_paths);
2771 TAILQ_FOREACH(pe, paths, entry) {
2772 struct bump_base_commit_id_arg bbc_arg;
2774 err = checkout_files(worktree, fileindex, tpd->relpath,
2775 tpd->tree_id, tpd->entry_name, repo,
2776 progress_cb, progress_arg, cancel_cb, cancel_arg);
2780 bbc_arg.base_commit_id = worktree->base_commit_id;
2781 bbc_arg.entry_name = tpd->entry_name;
2782 bbc_arg.path = pe->path;
2783 bbc_arg.path_len = pe->path_len;
2784 bbc_arg.progress_cb = progress_cb;
2785 bbc_arg.progress_arg = progress_arg;
2786 err = got_fileindex_for_each_entry_safe(fileindex,
2787 bump_base_commit_id, &bbc_arg);
2791 tpd = SIMPLEQ_NEXT(tpd, entry);
2793 sync_err = sync_fileindex(fileindex, fileindex_path);
2794 if (sync_err && err == NULL)
2797 free(fileindex_path);
2799 got_object_tree_close(tree);
2801 got_object_commit_close(commit);
2803 got_fileindex_free(fileindex);
2804 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2805 tpd = SIMPLEQ_FIRST(&tree_paths);
2806 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2811 unlockerr = lock_worktree(worktree, LOCK_SH);
2812 if (unlockerr && err == NULL)
2817 struct merge_file_cb_arg {
2818 struct got_worktree *worktree;
2819 struct got_fileindex *fileindex;
2820 got_worktree_checkout_cb progress_cb;
2822 got_cancel_cb cancel_cb;
2824 const char *label_orig;
2825 struct got_object_id *commit_id2;
2828 static const struct got_error *
2829 merge_file_cb(void *arg, struct got_blob_object *blob1,
2830 struct got_blob_object *blob2, struct got_object_id *id1,
2831 struct got_object_id *id2, const char *path1, const char *path2,
2832 mode_t mode1, mode_t mode2, struct got_repository *repo)
2834 static const struct got_error *err = NULL;
2835 struct merge_file_cb_arg *a = arg;
2836 struct got_fileindex_entry *ie;
2837 char *ondisk_path = NULL;
2839 unsigned char status;
2840 int local_changes_subsumed;
2842 if (blob1 && blob2) {
2843 ie = got_fileindex_entry_get(a->fileindex, path2,
2846 return (*a->progress_cb)(a->progress_arg,
2847 GOT_STATUS_MISSING, path2);
2849 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2851 return got_error_from_errno("asprintf");
2853 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2858 if (status == GOT_STATUS_DELETE) {
2859 err = (*a->progress_cb)(a->progress_arg,
2860 GOT_STATUS_MERGE, path2);
2863 if (status != GOT_STATUS_NO_CHANGE &&
2864 status != GOT_STATUS_MODIFY &&
2865 status != GOT_STATUS_CONFLICT &&
2866 status != GOT_STATUS_ADD) {
2867 err = (*a->progress_cb)(a->progress_arg, status, path2);
2871 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2873 err = got_object_blob_read_to_str(&link_target2, blob2);
2876 err = merge_symlink(a->worktree, blob1, ondisk_path,
2877 path2, a->label_orig, link_target2, a->commit_id2,
2878 repo, a->progress_cb, a->progress_arg);
2881 err = merge_blob(&local_changes_subsumed, a->worktree,
2882 blob1, ondisk_path, path2, sb.st_mode,
2883 a->label_orig, blob2, a->commit_id2, repo,
2884 a->progress_cb, a->progress_arg);
2887 ie = got_fileindex_entry_get(a->fileindex, path1,
2890 return (*a->progress_cb)(a->progress_arg,
2891 GOT_STATUS_MISSING, path1);
2893 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2895 return got_error_from_errno("asprintf");
2897 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2903 case GOT_STATUS_NO_CHANGE:
2904 err = (*a->progress_cb)(a->progress_arg,
2905 GOT_STATUS_DELETE, path1);
2908 err = remove_ondisk_file(a->worktree->root_path, path1);
2912 got_fileindex_entry_mark_deleted_from_disk(ie);
2914 case GOT_STATUS_DELETE:
2915 case GOT_STATUS_MISSING:
2916 err = (*a->progress_cb)(a->progress_arg,
2917 GOT_STATUS_DELETE, path1);
2921 got_fileindex_entry_mark_deleted_from_disk(ie);
2923 case GOT_STATUS_ADD: {
2924 struct got_object_id *id;
2927 * Delete the added file only if its content already
2928 * exists in the repository.
2930 err = got_object_blob_file_create(&id, &blob1_f, path1);
2933 if (got_object_id_cmp(id, id1) == 0) {
2934 err = (*a->progress_cb)(a->progress_arg,
2935 GOT_STATUS_DELETE, path1);
2938 err = remove_ondisk_file(a->worktree->root_path,
2943 got_fileindex_entry_remove(a->fileindex,
2946 err = (*a->progress_cb)(a->progress_arg,
2947 GOT_STATUS_CANNOT_DELETE, path1);
2949 if (fclose(blob1_f) == EOF && err == NULL)
2950 err = got_error_from_errno("fclose");
2956 case GOT_STATUS_MODIFY:
2957 case GOT_STATUS_CONFLICT:
2958 err = (*a->progress_cb)(a->progress_arg,
2959 GOT_STATUS_CANNOT_DELETE, path1);
2963 case GOT_STATUS_OBSTRUCTED:
2964 err = (*a->progress_cb)(a->progress_arg, status, path1);
2972 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2974 return got_error_from_errno("asprintf");
2975 ie = got_fileindex_entry_get(a->fileindex, path2,
2978 err = get_file_status(&status, &sb, ie, ondisk_path,
2982 if (status != GOT_STATUS_NO_CHANGE &&
2983 status != GOT_STATUS_MODIFY &&
2984 status != GOT_STATUS_CONFLICT &&
2985 status != GOT_STATUS_ADD) {
2986 err = (*a->progress_cb)(a->progress_arg,
2990 if (S_ISLNK(mode2) && S_ISLNK(sb.st_mode)) {
2992 err = got_object_blob_read_to_str(&link_target2,
2996 err = merge_symlink(a->worktree, NULL,
2997 ondisk_path, path2, a->label_orig,
2998 link_target2, a->commit_id2, repo,
2999 a->progress_cb, a->progress_arg);
3001 } else if (S_ISREG(sb.st_mode)) {
3002 err = merge_blob(&local_changes_subsumed,
3003 a->worktree, NULL, ondisk_path, path2,
3004 sb.st_mode, a->label_orig, blob2,
3005 a->commit_id2, repo, a->progress_cb,
3008 err = got_error_path(ondisk_path,
3009 GOT_ERR_FILE_OBSTRUCTED);
3013 if (status == GOT_STATUS_DELETE) {
3014 err = got_fileindex_entry_update(ie,
3015 a->worktree->root_fd, path2, blob2->id.sha1,
3016 a->worktree->base_commit_id->sha1, 0);
3021 int is_bad_symlink = 0;
3022 sb.st_mode = GOT_DEFAULT_FILE_MODE;
3023 if (S_ISLNK(mode2)) {
3024 err = install_symlink(&is_bad_symlink,
3025 a->worktree, ondisk_path, path2, blob2, 0,
3026 0, 1, repo, a->progress_cb, a->progress_arg);
3028 err = install_blob(a->worktree, ondisk_path, path2,
3029 mode2, sb.st_mode, blob2, 0, 0, 0, 1, repo,
3030 a->progress_cb, a->progress_arg);
3034 err = got_fileindex_entry_alloc(&ie, path2);
3037 err = got_fileindex_entry_update(ie,
3038 a->worktree->root_fd, path2, NULL, NULL, 1);
3040 got_fileindex_entry_free(ie);
3043 err = got_fileindex_entry_add(a->fileindex, ie);
3045 got_fileindex_entry_free(ie);
3048 if (is_bad_symlink) {
3049 got_fileindex_entry_filetype_set(ie,
3050 GOT_FILEIDX_MODE_BAD_SYMLINK);
3059 struct check_merge_ok_arg {
3060 struct got_worktree *worktree;
3061 struct got_repository *repo;
3064 static const struct got_error *
3065 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
3067 const struct got_error *err = NULL;
3068 struct check_merge_ok_arg *a = arg;
3069 unsigned char status;
3073 /* Reject merges into a work tree with mixed base commits. */
3074 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3075 SHA1_DIGEST_LENGTH))
3076 return got_error(GOT_ERR_MIXED_COMMITS);
3078 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3080 return got_error_from_errno("asprintf");
3082 /* Reject merges into a work tree with conflicted files. */
3083 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
3086 if (status == GOT_STATUS_CONFLICT)
3087 return got_error(GOT_ERR_CONFLICTS);
3092 static const struct got_error *
3093 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
3094 const char *fileindex_path, struct got_object_id *commit_id1,
3095 struct got_object_id *commit_id2, struct got_repository *repo,
3096 got_worktree_checkout_cb progress_cb, void *progress_arg,
3097 got_cancel_cb cancel_cb, void *cancel_arg)
3099 const struct got_error *err = NULL, *sync_err;
3100 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3101 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3102 struct merge_file_cb_arg arg;
3103 char *label_orig = NULL;
3106 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
3107 worktree->path_prefix);
3108 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3114 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3118 err = got_object_id_str(&id_str, commit_id1);
3122 if (asprintf(&label_orig, "%s: commit %s",
3123 GOT_MERGE_LABEL_BASE, id_str) == -1) {
3124 err = got_error_from_errno("asprintf");
3131 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
3132 worktree->path_prefix);
3136 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3140 arg.worktree = worktree;
3141 arg.fileindex = fileindex;
3142 arg.progress_cb = progress_cb;
3143 arg.progress_arg = progress_arg;
3144 arg.cancel_cb = cancel_cb;
3145 arg.cancel_arg = cancel_arg;
3146 arg.label_orig = label_orig;
3147 arg.commit_id2 = commit_id2;
3148 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
3149 sync_err = sync_fileindex(fileindex, fileindex_path);
3150 if (sync_err && err == NULL)
3154 got_object_tree_close(tree1);
3156 got_object_tree_close(tree2);
3161 const struct got_error *
3162 got_worktree_merge_files(struct got_worktree *worktree,
3163 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
3164 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3165 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
3167 const struct got_error *err, *unlockerr;
3168 char *fileindex_path = NULL;
3169 struct got_fileindex *fileindex = NULL;
3170 struct check_merge_ok_arg mok_arg;
3172 err = lock_worktree(worktree, LOCK_EX);
3176 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3180 mok_arg.worktree = worktree;
3181 mok_arg.repo = repo;
3182 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
3187 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
3188 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
3191 got_fileindex_free(fileindex);
3192 free(fileindex_path);
3193 unlockerr = lock_worktree(worktree, LOCK_SH);
3194 if (unlockerr && err == NULL)
3199 struct diff_dir_cb_arg {
3200 struct got_fileindex *fileindex;
3201 struct got_worktree *worktree;
3202 const char *status_path;
3203 size_t status_path_len;
3204 struct got_repository *repo;
3205 got_worktree_status_cb status_cb;
3207 got_cancel_cb cancel_cb;
3209 /* A pathlist containing per-directory pathlists of ignore patterns. */
3210 struct got_pathlist_head ignores;
3211 int report_unchanged;
3215 static const struct got_error *
3216 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
3217 int dirfd, const char *de_name,
3218 got_worktree_status_cb status_cb, void *status_arg,
3219 struct got_repository *repo, int report_unchanged)
3221 const struct got_error *err = NULL;
3222 unsigned char status = GOT_STATUS_NO_CHANGE;
3223 unsigned char staged_status = get_staged_status(ie);
3225 struct got_object_id blob_id, commit_id, staged_blob_id;
3226 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
3227 struct got_object_id *staged_blob_idp = NULL;
3229 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
3233 if (status == GOT_STATUS_NO_CHANGE &&
3234 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
3237 if (got_fileindex_entry_has_blob(ie)) {
3238 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3239 blob_idp = &blob_id;
3241 if (got_fileindex_entry_has_commit(ie)) {
3242 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3243 commit_idp = &commit_id;
3245 if (staged_status == GOT_STATUS_ADD ||
3246 staged_status == GOT_STATUS_MODIFY) {
3247 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
3248 SHA1_DIGEST_LENGTH);
3249 staged_blob_idp = &staged_blob_id;
3252 return (*status_cb)(status_arg, status, staged_status,
3253 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
3256 static const struct got_error *
3257 status_old_new(void *arg, struct got_fileindex_entry *ie,
3258 struct dirent *de, const char *parent_path, int dirfd)
3260 const struct got_error *err = NULL;
3261 struct diff_dir_cb_arg *a = arg;
3264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3265 return got_error(GOT_ERR_CANCELLED);
3267 if (got_path_cmp(parent_path, a->status_path,
3268 strlen(parent_path), a->status_path_len) != 0 &&
3269 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
3272 if (parent_path[0]) {
3273 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
3274 parent_path, de->d_name) == -1)
3275 return got_error_from_errno("asprintf");
3277 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
3279 return got_error_from_errno("asprintf");
3282 err = report_file_status(ie, abspath, dirfd, de->d_name,
3283 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
3288 static const struct got_error *
3289 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
3291 struct diff_dir_cb_arg *a = arg;
3292 struct got_object_id blob_id, commit_id;
3293 unsigned char status;
3295 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3296 return got_error(GOT_ERR_CANCELLED);
3298 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
3301 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
3302 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
3303 if (got_fileindex_entry_has_file_on_disk(ie))
3304 status = GOT_STATUS_MISSING;
3306 status = GOT_STATUS_DELETE;
3307 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
3308 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
3312 free_ignorelist(struct got_pathlist_head *ignorelist)
3314 struct got_pathlist_entry *pe;
3316 TAILQ_FOREACH(pe, ignorelist, entry)
3317 free((char *)pe->path);
3318 got_pathlist_free(ignorelist);
3322 free_ignores(struct got_pathlist_head *ignores)
3324 struct got_pathlist_entry *pe;
3326 TAILQ_FOREACH(pe, ignores, entry) {
3327 struct got_pathlist_head *ignorelist = pe->data;
3328 free_ignorelist(ignorelist);
3329 free((char *)pe->path);
3331 got_pathlist_free(ignores);
3334 static const struct got_error *
3335 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
3337 const struct got_error *err = NULL;
3338 struct got_pathlist_entry *pe = NULL;
3339 struct got_pathlist_head *ignorelist;
3340 char *line = NULL, *pattern, *dirpath = NULL;
3341 size_t linesize = 0;
3344 ignorelist = calloc(1, sizeof(*ignorelist));
3345 if (ignorelist == NULL)
3346 return got_error_from_errno("calloc");
3347 TAILQ_INIT(ignorelist);
3349 while ((linelen = getline(&line, &linesize, f)) != -1) {
3350 if (linelen > 0 && line[linelen - 1] == '\n')
3351 line[linelen - 1] = '\0';
3353 /* Git's ignores may contain comments. */
3357 /* Git's negated patterns are not (yet?) supported. */
3361 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3363 err = got_error_from_errno("asprintf");
3366 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3371 err = got_error_from_errno("getline");
3375 dirpath = strdup(path);
3376 if (dirpath == NULL) {
3377 err = got_error_from_errno("strdup");
3380 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3383 if (err || pe == NULL) {
3385 free_ignorelist(ignorelist);
3391 match_ignores(struct got_pathlist_head *ignores, const char *path)
3393 struct got_pathlist_entry *pe;
3395 /* Handle patterns which match in all directories. */
3396 TAILQ_FOREACH(pe, ignores, entry) {
3397 struct got_pathlist_head *ignorelist = pe->data;
3398 struct got_pathlist_entry *pi;
3400 TAILQ_FOREACH(pi, ignorelist, entry) {
3401 const char *p, *pattern = pi->path;
3403 if (strncmp(pattern, "**/", 3) != 0)
3408 if (fnmatch(pattern, p,
3409 FNM_PATHNAME | FNM_LEADING_DIR)) {
3410 /* Retry in next directory. */
3411 while (*p && *p != '/')
3423 * The ignores pathlist contains ignore lists from children before
3424 * parents, so we can find the most specific ignorelist by walking
3425 * ignores backwards.
3427 pe = TAILQ_LAST(ignores, got_pathlist_head);
3429 if (got_path_is_child(path, pe->path, pe->path_len)) {
3430 struct got_pathlist_head *ignorelist = pe->data;
3431 struct got_pathlist_entry *pi;
3432 TAILQ_FOREACH(pi, ignorelist, entry) {
3433 const char *pattern = pi->path;
3434 int flags = FNM_LEADING_DIR;
3435 if (strstr(pattern, "/**/") == NULL)
3436 flags |= FNM_PATHNAME;
3437 if (fnmatch(pattern, path, flags))
3442 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3448 static const struct got_error *
3449 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3450 const char *path, int dirfd, const char *ignores_filename)
3452 const struct got_error *err = NULL;
3455 FILE *ignoresfile = NULL;
3457 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3458 path[0] ? "/" : "", ignores_filename) == -1)
3459 return got_error_from_errno("asprintf");
3462 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3464 if (errno != ENOENT && errno != EACCES)
3465 err = got_error_from_errno2("openat",
3468 ignoresfile = fdopen(fd, "r");
3469 if (ignoresfile == NULL)
3470 err = got_error_from_errno2("fdopen",
3474 err = read_ignores(ignores, path, ignoresfile);
3478 ignoresfile = fopen(ignorespath, "r");
3479 if (ignoresfile == NULL) {
3480 if (errno != ENOENT && errno != EACCES)
3481 err = got_error_from_errno2("fopen",
3484 err = read_ignores(ignores, path, ignoresfile);
3487 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3488 err = got_error_from_errno2("fclose", path);
3489 if (fd != -1 && close(fd) == -1 && err == NULL)
3490 err = got_error_from_errno2("close", path);
3495 static const struct got_error *
3496 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3498 const struct got_error *err = NULL;
3499 struct diff_dir_cb_arg *a = arg;
3502 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3503 return got_error(GOT_ERR_CANCELLED);
3505 if (parent_path[0]) {
3506 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3507 return got_error_from_errno("asprintf");
3512 if (de->d_type != DT_DIR &&
3513 got_path_is_child(path, a->status_path, a->status_path_len)
3514 && !match_ignores(&a->ignores, path))
3515 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3516 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3522 static const struct got_error *
3523 status_traverse(void *arg, const char *path, int dirfd)
3525 const struct got_error *err = NULL;
3526 struct diff_dir_cb_arg *a = arg;
3531 err = add_ignores(&a->ignores, a->worktree->root_path,
3532 path, dirfd, ".cvsignore");
3536 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3537 dirfd, ".gitignore");
3542 static const struct got_error *
3543 report_single_file_status(const char *path, const char *ondisk_path,
3544 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3545 void *status_arg, struct got_repository *repo, int report_unchanged)
3547 struct got_fileindex_entry *ie;
3550 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3552 return report_file_status(ie, ondisk_path, -1, NULL,
3553 status_cb, status_arg, repo, report_unchanged);
3555 if (lstat(ondisk_path, &sb) == -1) {
3556 if (errno != ENOENT)
3557 return got_error_from_errno2("lstat", ondisk_path);
3558 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3559 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3563 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3564 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3565 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3570 static const struct got_error *
3571 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3572 const char *root_path, const char *path)
3574 const struct got_error *err;
3575 char *parent_path, *next_parent_path = NULL;
3577 err = add_ignores(ignores, root_path, "", -1,
3582 err = add_ignores(ignores, root_path, "", -1,
3587 err = got_path_dirname(&parent_path, path);
3589 if (err->code == GOT_ERR_BAD_PATH)
3590 return NULL; /* cannot traverse parent */
3594 err = add_ignores(ignores, root_path, parent_path, -1,
3598 err = add_ignores(ignores, root_path, parent_path, -1,
3602 err = got_path_dirname(&next_parent_path, parent_path);
3604 if (err->code == GOT_ERR_BAD_PATH)
3605 err = NULL; /* traversed everything */
3609 parent_path = next_parent_path;
3610 next_parent_path = NULL;
3614 free(next_parent_path);
3618 static const struct got_error *
3619 worktree_status(struct got_worktree *worktree, const char *path,
3620 struct got_fileindex *fileindex, struct got_repository *repo,
3621 got_worktree_status_cb status_cb, void *status_arg,
3622 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3623 int report_unchanged)
3625 const struct got_error *err = NULL;
3627 struct got_fileindex_diff_dir_cb fdiff_cb;
3628 struct diff_dir_cb_arg arg;
3629 char *ondisk_path = NULL;
3631 TAILQ_INIT(&arg.ignores);
3633 if (asprintf(&ondisk_path, "%s%s%s",
3634 worktree->root_path, path[0] ? "/" : "", path) == -1)
3635 return got_error_from_errno("asprintf");
3637 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3639 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3641 err = got_error_from_errno2("open", ondisk_path);
3643 err = report_single_file_status(path, ondisk_path,
3644 fileindex, status_cb, status_arg, repo,
3647 fdiff_cb.diff_old_new = status_old_new;
3648 fdiff_cb.diff_old = status_old;
3649 fdiff_cb.diff_new = status_new;
3650 fdiff_cb.diff_traverse = status_traverse;
3651 arg.fileindex = fileindex;
3652 arg.worktree = worktree;
3653 arg.status_path = path;
3654 arg.status_path_len = strlen(path);
3656 arg.status_cb = status_cb;
3657 arg.status_arg = status_arg;
3658 arg.cancel_cb = cancel_cb;
3659 arg.cancel_arg = cancel_arg;
3660 arg.report_unchanged = report_unchanged;
3661 arg.no_ignores = no_ignores;
3663 err = add_ignores_from_parent_paths(&arg.ignores,
3664 worktree->root_path, path);
3668 err = got_fileindex_diff_dir(fileindex, fd,
3669 worktree->root_path, path, repo, &fdiff_cb, &arg);
3672 free_ignores(&arg.ignores);
3673 if (fd != -1 && close(fd) == -1 && err == NULL)
3674 err = got_error_from_errno("close");
3679 const struct got_error *
3680 got_worktree_status(struct got_worktree *worktree,
3681 struct got_pathlist_head *paths, struct got_repository *repo,
3682 got_worktree_status_cb status_cb, void *status_arg,
3683 got_cancel_cb cancel_cb, void *cancel_arg)
3685 const struct got_error *err = NULL;
3686 char *fileindex_path = NULL;
3687 struct got_fileindex *fileindex = NULL;
3688 struct got_pathlist_entry *pe;
3690 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3694 TAILQ_FOREACH(pe, paths, entry) {
3695 err = worktree_status(worktree, pe->path, fileindex, repo,
3696 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3700 free(fileindex_path);
3701 got_fileindex_free(fileindex);
3705 const struct got_error *
3706 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3709 const struct got_error *err = NULL;
3710 char *resolved = NULL, *cwd = NULL, *path = NULL;
3713 char *abspath = NULL;
3714 char canonpath[PATH_MAX];
3718 cwd = getcwd(NULL, 0);
3720 return got_error_from_errno("getcwd");
3722 if (lstat(arg, &sb) == -1) {
3723 if (errno != ENOENT) {
3724 err = got_error_from_errno2("lstat", arg);
3729 if (S_ISLNK(sb.st_mode)) {
3731 * We cannot use realpath(3) with symlinks since we want to
3732 * operate on the symlink itself.
3733 * But we can make the path absolute, assuming it is relative
3734 * to the current working directory, and then canonicalize it.
3736 if (!got_path_is_absolute(arg)) {
3737 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3738 err = got_error_from_errno("asprintf");
3743 err = got_canonpath(abspath ? abspath : arg, canonpath,
3747 resolved = strdup(canonpath);
3748 if (resolved == NULL) {
3749 err = got_error_from_errno("strdup");
3753 resolved = realpath(arg, NULL);
3754 if (resolved == NULL) {
3755 if (errno != ENOENT) {
3756 err = got_error_from_errno2("realpath", arg);
3759 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3760 err = got_error_from_errno("asprintf");
3763 err = got_canonpath(abspath, canonpath,
3767 resolved = strdup(canonpath);
3768 if (resolved == NULL) {
3769 err = got_error_from_errno("strdup");
3775 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3776 strlen(got_worktree_get_root_path(worktree)))) {
3777 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3781 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3782 err = got_path_skip_common_ancestor(&path,
3783 got_worktree_get_root_path(worktree), resolved);
3789 err = got_error_from_errno("strdup");
3794 /* XXX status walk can't deal with trailing slash! */
3796 while (len > 0 && path[len - 1] == '/') {
3797 path[len - 1] = '\0';
3811 struct schedule_addition_args {
3812 struct got_worktree *worktree;
3813 struct got_fileindex *fileindex;
3814 got_worktree_checkout_cb progress_cb;
3816 struct got_repository *repo;
3819 static const struct got_error *
3820 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3821 const char *relpath, struct got_object_id *blob_id,
3822 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3823 int dirfd, const char *de_name)
3825 struct schedule_addition_args *a = arg;
3826 const struct got_error *err = NULL;
3827 struct got_fileindex_entry *ie;
3831 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3833 return got_error_from_errno("asprintf");
3835 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3837 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3841 /* Re-adding an existing entry is a no-op. */
3842 if (status == GOT_STATUS_ADD)
3844 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3849 if (status != GOT_STATUS_UNVERSIONED) {
3850 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3854 err = got_fileindex_entry_alloc(&ie, relpath);
3857 err = got_fileindex_entry_update(ie, a->worktree->root_fd,
3858 relpath, NULL, NULL, 1);
3860 got_fileindex_entry_free(ie);
3863 err = got_fileindex_entry_add(a->fileindex, ie);
3865 got_fileindex_entry_free(ie);
3872 if (status == GOT_STATUS_ADD)
3874 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3877 const struct got_error *
3878 got_worktree_schedule_add(struct got_worktree *worktree,
3879 struct got_pathlist_head *paths,
3880 got_worktree_checkout_cb progress_cb, void *progress_arg,
3881 struct got_repository *repo, int no_ignores)
3883 struct got_fileindex *fileindex = NULL;
3884 char *fileindex_path = NULL;
3885 const struct got_error *err = NULL, *sync_err, *unlockerr;
3886 struct got_pathlist_entry *pe;
3887 struct schedule_addition_args saa;
3889 err = lock_worktree(worktree, LOCK_EX);
3893 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3897 saa.worktree = worktree;
3898 saa.fileindex = fileindex;
3899 saa.progress_cb = progress_cb;
3900 saa.progress_arg = progress_arg;
3903 TAILQ_FOREACH(pe, paths, entry) {
3904 err = worktree_status(worktree, pe->path, fileindex, repo,
3905 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3909 sync_err = sync_fileindex(fileindex, fileindex_path);
3910 if (sync_err && err == NULL)
3913 free(fileindex_path);
3915 got_fileindex_free(fileindex);
3916 unlockerr = lock_worktree(worktree, LOCK_SH);
3917 if (unlockerr && err == NULL)
3922 struct schedule_deletion_args {
3923 struct got_worktree *worktree;
3924 struct got_fileindex *fileindex;
3925 got_worktree_delete_cb progress_cb;
3927 struct got_repository *repo;
3928 int delete_local_mods;
3930 const char *status_codes;
3933 static const struct got_error *
3934 schedule_for_deletion(void *arg, unsigned char status,
3935 unsigned char staged_status, const char *relpath,
3936 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3937 struct got_object_id *commit_id, int dirfd, const char *de_name)
3939 struct schedule_deletion_args *a = arg;
3940 const struct got_error *err = NULL;
3941 struct got_fileindex_entry *ie = NULL;
3945 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3947 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3949 staged_status = get_staged_status(ie);
3950 if (staged_status != GOT_STATUS_NO_CHANGE) {
3951 if (staged_status == GOT_STATUS_DELETE)
3953 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3956 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3958 return got_error_from_errno("asprintf");
3960 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3965 if (a->status_codes) {
3966 size_t ncodes = strlen(a->status_codes);
3968 for (i = 0; i < ncodes ; i++) {
3969 if (status == a->status_codes[i])
3973 /* Do not delete files in non-matching status. */
3977 if (a->status_codes[i] != GOT_STATUS_MODIFY &&
3978 a->status_codes[i] != GOT_STATUS_MISSING) {
3979 static char msg[64];
3980 snprintf(msg, sizeof(msg),
3981 "invalid status code '%c'", a->status_codes[i]);
3982 err = got_error_msg(GOT_ERR_FILE_STATUS, msg);
3987 if (status != GOT_STATUS_NO_CHANGE) {
3988 if (status == GOT_STATUS_DELETE)
3990 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3991 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3994 if (status != GOT_STATUS_MODIFY &&
3995 status != GOT_STATUS_MISSING) {
3996 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4001 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
4005 if (unlinkat(dirfd, de_name, 0) != 0) {
4006 err = got_error_from_errno2("unlinkat",
4010 } else if (unlink(ondisk_path) != 0) {
4011 err = got_error_from_errno2("unlink", ondisk_path);
4015 root_len = strlen(a->worktree->root_path);
4018 err = got_path_dirname(&parent, ondisk_path);
4022 ondisk_path = parent;
4023 if (rmdir(ondisk_path) == -1) {
4024 if (errno != ENOTEMPTY)
4025 err = got_error_from_errno2("rmdir",
4029 } while (got_path_cmp(ondisk_path, a->worktree->root_path,
4030 strlen(ondisk_path), root_len) != 0);
4033 got_fileindex_entry_mark_deleted_from_disk(ie);
4038 if (status == GOT_STATUS_DELETE)
4040 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
4041 staged_status, relpath);
4044 const struct got_error *
4045 got_worktree_schedule_delete(struct got_worktree *worktree,
4046 struct got_pathlist_head *paths, int delete_local_mods,
4047 const char *status_codes,
4048 got_worktree_delete_cb progress_cb, void *progress_arg,
4049 struct got_repository *repo, int keep_on_disk)
4051 struct got_fileindex *fileindex = NULL;
4052 char *fileindex_path = NULL;
4053 const struct got_error *err = NULL, *sync_err, *unlockerr;
4054 struct got_pathlist_entry *pe;
4055 struct schedule_deletion_args sda;
4057 err = lock_worktree(worktree, LOCK_EX);
4061 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4065 sda.worktree = worktree;
4066 sda.fileindex = fileindex;
4067 sda.progress_cb = progress_cb;
4068 sda.progress_arg = progress_arg;
4070 sda.delete_local_mods = delete_local_mods;
4071 sda.keep_on_disk = keep_on_disk;
4072 sda.status_codes = status_codes;
4074 TAILQ_FOREACH(pe, paths, entry) {
4075 err = worktree_status(worktree, pe->path, fileindex, repo,
4076 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
4080 sync_err = sync_fileindex(fileindex, fileindex_path);
4081 if (sync_err && err == NULL)
4084 free(fileindex_path);
4086 got_fileindex_free(fileindex);
4087 unlockerr = lock_worktree(worktree, LOCK_SH);
4088 if (unlockerr && err == NULL)
4093 static const struct got_error *
4094 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
4096 const struct got_error *err = NULL;
4098 size_t linesize = 0, n;
4101 linelen = getline(&line, &linesize, infile);
4102 if (linelen == -1) {
4103 if (ferror(infile)) {
4104 err = got_error_from_errno("getline");
4110 n = fwrite(line, 1, linelen, outfile);
4112 err = got_ferror(outfile, GOT_ERR_IO);
4117 n = fwrite(line, 1, linelen, rejectfile);
4119 err = got_ferror(outfile, GOT_ERR_IO);
4126 static const struct got_error *
4127 skip_one_line(FILE *f)
4130 size_t linesize = 0;
4133 linelen = getline(&line, &linesize, f);
4134 if (linelen == -1) {
4136 return got_error_from_errno("getline");
4143 static const struct got_error *
4144 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4145 int start_old, int end_old, int start_new, int end_new,
4146 FILE *outfile, FILE *rejectfile)
4148 const struct got_error *err;
4150 /* Copy old file's lines leading up to patch. */
4151 while (!feof(f1) && *line_cur1 < start_old) {
4152 err = copy_one_line(f1, outfile, NULL);
4157 /* Skip new file's lines leading up to patch. */
4158 while (!feof(f2) && *line_cur2 < start_new) {
4160 err = copy_one_line(f2, NULL, rejectfile);
4162 err = skip_one_line(f2);
4167 /* Copy patched lines. */
4168 while (!feof(f2) && *line_cur2 <= end_new) {
4169 err = copy_one_line(f2, outfile, NULL);
4174 /* Skip over old file's replaced lines. */
4175 while (!feof(f1) && *line_cur1 <= end_old) {
4177 err = copy_one_line(f1, NULL, rejectfile);
4179 err = skip_one_line(f1);
4188 static const struct got_error *
4189 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4190 FILE *outfile, FILE *rejectfile)
4192 const struct got_error *err;
4195 /* Copy old file's lines until EOF. */
4197 err = copy_one_line(f1, outfile, NULL);
4204 /* Copy new file's lines until EOF. */
4206 err = copy_one_line(f2, NULL, rejectfile);
4216 static const struct got_error *
4217 apply_or_reject_change(int *choice, int *nchunks_used,
4218 struct diff_result *diff_result, int n,
4219 const char *relpath, FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
4220 FILE *outfile, FILE *rejectfile, int changeno, int nchanges,
4221 got_worktree_patch_cb patch_cb, void *patch_arg)
4223 const struct got_error *err = NULL;
4224 struct diff_chunk_context cc = {};
4225 int start_old, end_old, start_new, end_new;
4227 struct diff_output_unidiff_state *diff_state;
4228 struct diff_input_info diff_info;
4231 *choice = GOT_PATCH_CHOICE_NONE;
4233 /* Get changed line numbers without context lines for copy_change(). */
4234 diff_chunk_context_load_change(&cc, NULL, diff_result, n, 0);
4235 start_old = cc.left.start;
4236 end_old = cc.left.end;
4237 start_new = cc.right.start;
4238 end_new = cc.right.end;
4240 /* Get the same change with context lines for display. */
4241 memset(&cc, 0, sizeof(cc));
4242 diff_chunk_context_load_change(&cc, nchunks_used, diff_result, n, 3);
4244 memset(&diff_info, 0, sizeof(diff_info));
4245 diff_info.left_path = relpath;
4246 diff_info.right_path = relpath;
4248 diff_state = diff_output_unidiff_state_alloc();
4249 if (diff_state == NULL)
4250 return got_error_set_errno(ENOMEM,
4251 "diff_output_unidiff_state_alloc");
4253 hunkfile = got_opentemp();
4254 if (hunkfile == NULL) {
4255 err = got_error_from_errno("got_opentemp");
4259 rc = diff_output_unidiff_chunk(NULL, hunkfile, diff_state, &diff_info,
4261 if (rc != DIFF_RC_OK) {
4262 err = got_error_set_errno(rc, "diff_output_unidiff_chunk");
4266 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
4267 err = got_ferror(hunkfile, GOT_ERR_IO);
4271 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
4272 hunkfile, changeno, nchanges);
4277 case GOT_PATCH_CHOICE_YES:
4278 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4279 end_old, start_new, end_new, outfile, rejectfile);
4281 case GOT_PATCH_CHOICE_NO:
4282 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
4283 end_old, start_new, end_new, rejectfile, outfile);
4285 case GOT_PATCH_CHOICE_QUIT:
4288 err = got_error(GOT_ERR_PATCH_CHOICE);
4292 diff_output_unidiff_state_free(diff_state);
4293 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
4294 err = got_error_from_errno("fclose");
4298 struct revert_file_args {
4299 struct got_worktree *worktree;
4300 struct got_fileindex *fileindex;
4301 got_worktree_checkout_cb progress_cb;
4303 got_worktree_patch_cb patch_cb;
4305 struct got_repository *repo;
4308 static const struct got_error *
4309 create_patched_content(char **path_outfile, int reverse_patch,
4310 struct got_object_id *blob_id, const char *path2,
4311 int dirfd2, const char *de_name2,
4312 const char *relpath, struct got_repository *repo,
4313 got_worktree_patch_cb patch_cb, void *patch_arg)
4315 const struct got_error *err, *free_err;
4316 struct got_blob_object *blob = NULL;
4317 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
4319 char link_target[PATH_MAX];
4320 ssize_t link_len = 0;
4321 char *path1 = NULL, *id_str = NULL;
4323 struct got_diffreg_result *diffreg_result = NULL;
4324 int line_cur1 = 1, line_cur2 = 1, have_content = 0;
4325 int i = 0, n = 0, nchunks_used = 0, nchanges = 0;
4327 *path_outfile = NULL;
4329 err = got_object_id_str(&id_str, blob_id);
4334 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
4336 if (errno != ELOOP) {
4337 err = got_error_from_errno2("openat", path2);
4340 link_len = readlinkat(dirfd2, de_name2,
4341 link_target, sizeof(link_target));
4343 return got_error_from_errno2("readlinkat", path2);
4344 sb2.st_mode = S_IFLNK;
4345 sb2.st_size = link_len;
4348 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
4350 if (errno != ELOOP) {
4351 err = got_error_from_errno2("open", path2);
4354 link_len = readlink(path2, link_target,
4355 sizeof(link_target));
4357 return got_error_from_errno2("readlink", path2);
4358 sb2.st_mode = S_IFLNK;
4359 sb2.st_size = link_len;
4363 if (fstat(fd2, &sb2) == -1) {
4364 err = got_error_from_errno2("fstat", path2);
4368 f2 = fdopen(fd2, "r");
4370 err = got_error_from_errno2("fdopen", path2);
4376 f2 = got_opentemp();
4378 err = got_error_from_errno2("got_opentemp", path2);
4381 n = fwrite(link_target, 1, link_len, f2);
4382 if (n != link_len) {
4383 err = got_ferror(f2, GOT_ERR_IO);
4386 if (fflush(f2) == EOF) {
4387 err = got_error_from_errno("fflush");
4393 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
4397 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
4401 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
4405 err = got_diff_files(&diffreg_result, f1, id_str, f2, path2, 3, 0, 1,
4410 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
4414 if (fseek(f1, 0L, SEEK_SET) == -1)
4415 return got_ferror(f1, GOT_ERR_IO);
4416 if (fseek(f2, 0L, SEEK_SET) == -1)
4417 return got_ferror(f2, GOT_ERR_IO);
4419 /* Count the number of actual changes in the diff result. */
4420 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4421 struct diff_chunk_context cc = {};
4422 diff_chunk_context_load_change(&cc, &nchunks_used,
4423 diffreg_result->result, n, 0);
4426 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
4428 err = apply_or_reject_change(&choice, &nchunks_used,
4429 diffreg_result->result, n, relpath, f1, f2,
4430 &line_cur1, &line_cur2,
4431 reverse_patch ? NULL : outfile,
4432 reverse_patch ? outfile : NULL,
4433 ++i, nchanges, patch_cb, patch_arg);
4436 if (choice == GOT_PATCH_CHOICE_YES)
4438 else if (choice == GOT_PATCH_CHOICE_QUIT)
4442 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4443 reverse_patch ? NULL : outfile,
4444 reverse_patch ? outfile : NULL);
4448 if (!S_ISLNK(sb2.st_mode)) {
4449 if (fchmod(fileno(outfile), sb2.st_mode) == -1) {
4450 err = got_error_from_errno2("fchmod", path2);
4458 got_object_blob_close(blob);
4459 free_err = got_diffreg_result_free(diffreg_result);
4462 if (f1 && fclose(f1) == EOF && err == NULL)
4463 err = got_error_from_errno2("fclose", path1);
4464 if (f2 && fclose(f2) == EOF && err == NULL)
4465 err = got_error_from_errno2("fclose", path2);
4466 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4467 err = got_error_from_errno2("close", path2);
4468 if (outfile && fclose(outfile) == EOF && err == NULL)
4469 err = got_error_from_errno2("fclose", *path_outfile);
4470 if (path1 && unlink(path1) == -1 && err == NULL)
4471 err = got_error_from_errno2("unlink", path1);
4472 if (err || !have_content) {
4473 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4474 err = got_error_from_errno2("unlink", *path_outfile);
4475 free(*path_outfile);
4476 *path_outfile = NULL;
4482 static const struct got_error *
4483 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4484 const char *relpath, struct got_object_id *blob_id,
4485 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4486 int dirfd, const char *de_name)
4488 struct revert_file_args *a = arg;
4489 const struct got_error *err = NULL;
4490 char *parent_path = NULL;
4491 struct got_fileindex_entry *ie;
4492 struct got_tree_object *tree = NULL;
4493 struct got_object_id *tree_id = NULL;
4494 const struct got_tree_entry *te = NULL;
4495 char *tree_path = NULL, *te_name;
4496 char *ondisk_path = NULL, *path_content = NULL;
4497 struct got_blob_object *blob = NULL;
4499 /* Reverting a staged deletion is a no-op. */
4500 if (status == GOT_STATUS_DELETE &&
4501 staged_status != GOT_STATUS_NO_CHANGE)
4504 if (status == GOT_STATUS_UNVERSIONED)
4505 return (*a->progress_cb)(a->progress_arg,
4506 GOT_STATUS_UNVERSIONED, relpath);
4508 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4510 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4512 /* Construct in-repository path of tree which contains this blob. */
4513 err = got_path_dirname(&parent_path, ie->path);
4515 if (err->code != GOT_ERR_BAD_PATH)
4517 parent_path = strdup("/");
4518 if (parent_path == NULL) {
4519 err = got_error_from_errno("strdup");
4523 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4524 tree_path = strdup(parent_path);
4525 if (tree_path == NULL) {
4526 err = got_error_from_errno("strdup");
4530 if (got_path_is_root_dir(parent_path)) {
4531 tree_path = strdup(a->worktree->path_prefix);
4532 if (tree_path == NULL) {
4533 err = got_error_from_errno("strdup");
4537 if (asprintf(&tree_path, "%s/%s",
4538 a->worktree->path_prefix, parent_path) == -1) {
4539 err = got_error_from_errno("asprintf");
4545 err = got_object_id_by_path(&tree_id, a->repo,
4546 a->worktree->base_commit_id, tree_path);
4548 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4549 (status == GOT_STATUS_ADD ||
4550 staged_status == GOT_STATUS_ADD)))
4553 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4557 err = got_path_basename(&te_name, ie->path);
4561 te = got_object_tree_find_entry(tree, te_name);
4563 if (te == NULL && status != GOT_STATUS_ADD &&
4564 staged_status != GOT_STATUS_ADD) {
4565 err = got_error_path(ie->path, GOT_ERR_NO_TREE_ENTRY);
4571 case GOT_STATUS_ADD:
4573 int choice = GOT_PATCH_CHOICE_NONE;
4574 err = (*a->patch_cb)(&choice, a->patch_arg,
4575 status, ie->path, NULL, 1, 1);
4578 if (choice != GOT_PATCH_CHOICE_YES)
4581 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4585 got_fileindex_entry_remove(a->fileindex, ie);
4587 case GOT_STATUS_DELETE:
4589 int choice = GOT_PATCH_CHOICE_NONE;
4590 err = (*a->patch_cb)(&choice, a->patch_arg,
4591 status, ie->path, NULL, 1, 1);
4594 if (choice != GOT_PATCH_CHOICE_YES)
4598 case GOT_STATUS_MODIFY:
4599 case GOT_STATUS_MODE_CHANGE:
4600 case GOT_STATUS_CONFLICT:
4601 case GOT_STATUS_MISSING: {
4602 struct got_object_id id;
4603 if (staged_status == GOT_STATUS_ADD ||
4604 staged_status == GOT_STATUS_MODIFY) {
4605 memcpy(id.sha1, ie->staged_blob_sha1,
4606 SHA1_DIGEST_LENGTH);
4608 memcpy(id.sha1, ie->blob_sha1,
4609 SHA1_DIGEST_LENGTH);
4610 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4614 if (asprintf(&ondisk_path, "%s/%s",
4615 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4616 err = got_error_from_errno("asprintf");
4620 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4621 status == GOT_STATUS_CONFLICT)) {
4622 int is_bad_symlink = 0;
4623 err = create_patched_content(&path_content, 1, &id,
4624 ondisk_path, dirfd, de_name, ie->path, a->repo,
4625 a->patch_cb, a->patch_arg);
4626 if (err || path_content == NULL)
4628 if (te && S_ISLNK(te->mode)) {
4629 if (unlink(path_content) == -1) {
4630 err = got_error_from_errno2("unlink",
4634 err = install_symlink(&is_bad_symlink,
4635 a->worktree, ondisk_path, ie->path,
4636 blob, 0, 1, 0, a->repo,
4637 a->progress_cb, a->progress_arg);
4639 if (rename(path_content, ondisk_path) == -1) {
4640 err = got_error_from_errno3("rename",
4641 path_content, ondisk_path);
4646 int is_bad_symlink = 0;
4647 if (te && S_ISLNK(te->mode)) {
4648 err = install_symlink(&is_bad_symlink,
4649 a->worktree, ondisk_path, ie->path,
4650 blob, 0, 1, 0, a->repo,
4651 a->progress_cb, a->progress_arg);
4653 err = install_blob(a->worktree, ondisk_path,
4655 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4656 got_fileindex_perms_to_st(ie), blob,
4657 0, 1, 0, 0, a->repo,
4658 a->progress_cb, a->progress_arg);
4662 if (status == GOT_STATUS_DELETE ||
4663 status == GOT_STATUS_MODE_CHANGE) {
4664 err = got_fileindex_entry_update(ie,
4665 a->worktree->root_fd, relpath,
4667 a->worktree->base_commit_id->sha1, 1);
4671 if (is_bad_symlink) {
4672 got_fileindex_entry_filetype_set(ie,
4673 GOT_FILEIDX_MODE_BAD_SYMLINK);
4687 got_object_blob_close(blob);
4689 got_object_tree_close(tree);
4694 const struct got_error *
4695 got_worktree_revert(struct got_worktree *worktree,
4696 struct got_pathlist_head *paths,
4697 got_worktree_checkout_cb progress_cb, void *progress_arg,
4698 got_worktree_patch_cb patch_cb, void *patch_arg,
4699 struct got_repository *repo)
4701 struct got_fileindex *fileindex = NULL;
4702 char *fileindex_path = NULL;
4703 const struct got_error *err = NULL, *unlockerr = NULL;
4704 const struct got_error *sync_err = NULL;
4705 struct got_pathlist_entry *pe;
4706 struct revert_file_args rfa;
4708 err = lock_worktree(worktree, LOCK_EX);
4712 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4716 rfa.worktree = worktree;
4717 rfa.fileindex = fileindex;
4718 rfa.progress_cb = progress_cb;
4719 rfa.progress_arg = progress_arg;
4720 rfa.patch_cb = patch_cb;
4721 rfa.patch_arg = patch_arg;
4723 TAILQ_FOREACH(pe, paths, entry) {
4724 err = worktree_status(worktree, pe->path, fileindex, repo,
4725 revert_file, &rfa, NULL, NULL, 0, 0);
4729 sync_err = sync_fileindex(fileindex, fileindex_path);
4730 if (sync_err && err == NULL)
4733 free(fileindex_path);
4735 got_fileindex_free(fileindex);
4736 unlockerr = lock_worktree(worktree, LOCK_SH);
4737 if (unlockerr && err == NULL)
4743 free_commitable(struct got_commitable *ct)
4746 free(ct->in_repo_path);
4747 free(ct->ondisk_path);
4749 free(ct->base_blob_id);
4750 free(ct->staged_blob_id);
4751 free(ct->base_commit_id);
4755 struct collect_commitables_arg {
4756 struct got_pathlist_head *commitable_paths;
4757 struct got_repository *repo;
4758 struct got_worktree *worktree;
4759 struct got_fileindex *fileindex;
4760 int have_staged_files;
4761 int allow_bad_symlinks;
4764 static const struct got_error *
4765 collect_commitables(void *arg, unsigned char status,
4766 unsigned char staged_status, const char *relpath,
4767 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4768 struct got_object_id *commit_id, int dirfd, const char *de_name)
4770 struct collect_commitables_arg *a = arg;
4771 const struct got_error *err = NULL;
4772 struct got_commitable *ct = NULL;
4773 struct got_pathlist_entry *new = NULL;
4774 char *parent_path = NULL, *path = NULL;
4777 if (a->have_staged_files) {
4778 if (staged_status != GOT_STATUS_MODIFY &&
4779 staged_status != GOT_STATUS_ADD &&
4780 staged_status != GOT_STATUS_DELETE)
4783 if (status == GOT_STATUS_CONFLICT)
4784 return got_error(GOT_ERR_COMMIT_CONFLICT);
4786 if (status != GOT_STATUS_MODIFY &&
4787 status != GOT_STATUS_MODE_CHANGE &&
4788 status != GOT_STATUS_ADD &&
4789 status != GOT_STATUS_DELETE)
4793 if (asprintf(&path, "/%s", relpath) == -1) {
4794 err = got_error_from_errno("asprintf");
4797 if (strcmp(path, "/") == 0) {
4798 parent_path = strdup("");
4799 if (parent_path == NULL)
4800 return got_error_from_errno("strdup");
4802 err = got_path_dirname(&parent_path, path);
4807 ct = calloc(1, sizeof(*ct));
4809 err = got_error_from_errno("calloc");
4813 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4815 err = got_error_from_errno("asprintf");
4819 if (staged_status == GOT_STATUS_ADD ||
4820 staged_status == GOT_STATUS_MODIFY) {
4821 struct got_fileindex_entry *ie;
4822 ie = got_fileindex_entry_get(a->fileindex, path, strlen(path));
4823 switch (got_fileindex_entry_staged_filetype_get(ie)) {
4824 case GOT_FILEIDX_MODE_REGULAR_FILE:
4825 case GOT_FILEIDX_MODE_BAD_SYMLINK:
4828 case GOT_FILEIDX_MODE_SYMLINK:
4832 err = got_error_path(path, GOT_ERR_BAD_FILETYPE);
4835 ct->mode |= got_fileindex_entry_perms_get(ie);
4836 } else if (status != GOT_STATUS_DELETE &&
4837 staged_status != GOT_STATUS_DELETE) {
4839 if (fstatat(dirfd, de_name, &sb,
4840 AT_SYMLINK_NOFOLLOW) == -1) {
4841 err = got_error_from_errno2("fstatat",
4845 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4846 err = got_error_from_errno2("lstat", ct->ondisk_path);
4849 ct->mode = sb.st_mode;
4852 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4853 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4855 err = got_error_from_errno("asprintf");
4859 if (S_ISLNK(ct->mode) && staged_status == GOT_STATUS_NO_CHANGE &&
4860 status == GOT_STATUS_ADD && !a->allow_bad_symlinks) {
4862 char target_path[PATH_MAX];
4864 target_len = readlink(ct->ondisk_path, target_path,
4865 sizeof(target_path));
4866 if (target_len == -1) {
4867 err = got_error_from_errno2("readlink",
4871 err = is_bad_symlink_target(&is_bad_symlink, target_path,
4872 target_len, ct->ondisk_path, a->worktree->root_path);
4875 if (is_bad_symlink) {
4876 err = got_error_path(ct->ondisk_path,
4877 GOT_ERR_BAD_SYMLINK);
4883 ct->status = status;
4884 ct->staged_status = staged_status;
4885 ct->blob_id = NULL; /* will be filled in when blob gets created */
4886 if (ct->status != GOT_STATUS_ADD &&
4887 ct->staged_status != GOT_STATUS_ADD) {
4888 ct->base_blob_id = got_object_id_dup(blob_id);
4889 if (ct->base_blob_id == NULL) {
4890 err = got_error_from_errno("got_object_id_dup");
4893 ct->base_commit_id = got_object_id_dup(commit_id);
4894 if (ct->base_commit_id == NULL) {
4895 err = got_error_from_errno("got_object_id_dup");
4899 if (ct->staged_status == GOT_STATUS_ADD ||
4900 ct->staged_status == GOT_STATUS_MODIFY) {
4901 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4902 if (ct->staged_blob_id == NULL) {
4903 err = got_error_from_errno("got_object_id_dup");
4907 ct->path = strdup(path);
4908 if (ct->path == NULL) {
4909 err = got_error_from_errno("strdup");
4912 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4914 if (ct && (err || new == NULL))
4915 free_commitable(ct);
4921 static const struct got_error *write_tree(struct got_object_id **, int *,
4922 struct got_tree_object *, const char *, struct got_pathlist_head *,
4923 got_worktree_status_cb status_cb, void *status_arg,
4924 struct got_repository *);
4926 static const struct got_error *
4927 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4928 struct got_tree_entry *te, const char *parent_path,
4929 struct got_pathlist_head *commitable_paths,
4930 got_worktree_status_cb status_cb, void *status_arg,
4931 struct got_repository *repo)
4933 const struct got_error *err = NULL;
4934 struct got_tree_object *subtree;
4937 if (asprintf(&subpath, "%s%s%s", parent_path,
4938 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4939 return got_error_from_errno("asprintf");
4941 err = got_object_open_as_tree(&subtree, repo, &te->id);
4945 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4946 commitable_paths, status_cb, status_arg, repo);
4947 got_object_tree_close(subtree);
4952 static const struct got_error *
4953 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4955 const struct got_error *err = NULL;
4956 char *ct_parent_path = NULL;
4960 if (strchr(ct->in_repo_path, '/') == NULL) {
4961 *match = got_path_is_root_dir(path);
4965 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4968 *match = (strcmp(path, ct_parent_path) == 0);
4969 free(ct_parent_path);
4974 get_ct_file_mode(struct got_commitable *ct)
4976 if (S_ISLNK(ct->mode))
4979 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4982 static const struct got_error *
4983 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4984 struct got_tree_entry *te, struct got_commitable *ct)
4986 const struct got_error *err = NULL;
4990 err = got_object_tree_entry_dup(new_te, te);
4994 (*new_te)->mode = get_ct_file_mode(ct);
4996 if (ct->staged_status == GOT_STATUS_MODIFY)
4997 memcpy(&(*new_te)->id, ct->staged_blob_id,
4998 sizeof((*new_te)->id));
5000 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5002 if (err && *new_te) {
5009 static const struct got_error *
5010 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
5011 struct got_commitable *ct)
5013 const struct got_error *err = NULL;
5014 char *ct_name = NULL;
5018 *new_te = calloc(1, sizeof(**new_te));
5019 if (*new_te == NULL)
5020 return got_error_from_errno("calloc");
5022 err = got_path_basename(&ct_name, ct->path);
5025 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
5026 sizeof((*new_te)->name)) {
5027 err = got_error(GOT_ERR_NO_SPACE);
5031 (*new_te)->mode = get_ct_file_mode(ct);
5033 if (ct->staged_status == GOT_STATUS_ADD)
5034 memcpy(&(*new_te)->id, ct->staged_blob_id,
5035 sizeof((*new_te)->id));
5037 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
5040 if (err && *new_te) {
5047 static const struct got_error *
5048 insert_tree_entry(struct got_tree_entry *new_te,
5049 struct got_pathlist_head *paths)
5051 const struct got_error *err = NULL;
5052 struct got_pathlist_entry *new_pe;
5054 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
5058 return got_error(GOT_ERR_TREE_DUP_ENTRY);
5062 static const struct got_error *
5063 report_ct_status(struct got_commitable *ct,
5064 got_worktree_status_cb status_cb, void *status_arg)
5066 const char *ct_path = ct->path;
5067 unsigned char status;
5069 while (ct_path[0] == '/')
5072 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
5073 status = ct->staged_status;
5075 status = ct->status;
5077 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
5078 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
5081 static const struct got_error *
5082 match_modified_subtree(int *modified, struct got_tree_entry *te,
5083 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
5085 const struct got_error *err = NULL;
5086 struct got_pathlist_entry *pe;
5091 if (asprintf(&te_path, "%s%s%s", base_tree_path,
5092 got_path_is_root_dir(base_tree_path) ? "" : "/",
5094 return got_error_from_errno("asprintf");
5096 TAILQ_FOREACH(pe, commitable_paths, entry) {
5097 struct got_commitable *ct = pe->data;
5098 *modified = got_path_is_child(ct->in_repo_path, te_path,
5108 static const struct got_error *
5109 match_deleted_or_modified_ct(struct got_commitable **ctp,
5110 struct got_tree_entry *te, const char *base_tree_path,
5111 struct got_pathlist_head *commitable_paths)
5113 const struct got_error *err = NULL;
5114 struct got_pathlist_entry *pe;
5118 TAILQ_FOREACH(pe, commitable_paths, entry) {
5119 struct got_commitable *ct = pe->data;
5120 char *ct_name = NULL;
5123 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
5124 if (ct->status != GOT_STATUS_MODIFY &&
5125 ct->status != GOT_STATUS_MODE_CHANGE &&
5126 ct->status != GOT_STATUS_DELETE)
5129 if (ct->staged_status != GOT_STATUS_MODIFY &&
5130 ct->staged_status != GOT_STATUS_DELETE)
5134 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
5137 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
5143 err = got_path_basename(&ct_name, pe->path);
5147 if (strcmp(te->name, ct_name) != 0) {
5160 static const struct got_error *
5161 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
5162 const char *child_path, const char *path_base_tree,
5163 struct got_pathlist_head *commitable_paths,
5164 got_worktree_status_cb status_cb, void *status_arg,
5165 struct got_repository *repo)
5167 const struct got_error *err = NULL;
5168 struct got_tree_entry *new_te;
5170 struct got_object_id *id = NULL;
5175 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
5176 got_path_is_root_dir(path_base_tree) ? "" : "/",
5178 return got_error_from_errno("asprintf");
5180 new_te = calloc(1, sizeof(*new_te));
5182 return got_error_from_errno("calloc");
5183 new_te->mode = S_IFDIR;
5185 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
5186 sizeof(new_te->name)) {
5187 err = got_error(GOT_ERR_NO_SPACE);
5190 err = write_tree(&id, &nentries, NULL, subtree_path,
5191 commitable_paths, status_cb, status_arg, repo);
5196 memcpy(&new_te->id, id, sizeof(new_te->id));
5205 static const struct got_error *
5206 write_tree(struct got_object_id **new_tree_id, int *nentries,
5207 struct got_tree_object *base_tree, const char *path_base_tree,
5208 struct got_pathlist_head *commitable_paths,
5209 got_worktree_status_cb status_cb, void *status_arg,
5210 struct got_repository *repo)
5212 const struct got_error *err = NULL;
5213 struct got_pathlist_head paths;
5214 struct got_tree_entry *te, *new_te = NULL;
5215 struct got_pathlist_entry *pe;
5220 /* Insert, and recurse into, newly added entries first. */
5221 TAILQ_FOREACH(pe, commitable_paths, entry) {
5222 struct got_commitable *ct = pe->data;
5223 char *child_path = NULL, *slash;
5225 if ((ct->status != GOT_STATUS_ADD &&
5226 ct->staged_status != GOT_STATUS_ADD) ||
5227 (ct->flags & GOT_COMMITABLE_ADDED))
5230 if (!got_path_is_child(ct->in_repo_path, path_base_tree,
5231 strlen(path_base_tree)))
5234 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
5239 slash = strchr(child_path, '/');
5240 if (slash == NULL) {
5241 err = alloc_added_blob_tree_entry(&new_te, ct);
5244 err = report_ct_status(ct, status_cb, status_arg);
5247 ct->flags |= GOT_COMMITABLE_ADDED;
5248 err = insert_tree_entry(new_te, &paths);
5253 *slash = '\0'; /* trim trailing path components */
5254 if (base_tree == NULL ||
5255 got_object_tree_find_entry(base_tree, child_path)
5257 err = make_subtree_for_added_blob(&new_te,
5258 child_path, path_base_tree,
5259 commitable_paths, status_cb, status_arg,
5263 err = insert_tree_entry(new_te, &paths);
5272 int i, nbase_entries;
5273 /* Handle modified and deleted entries. */
5274 nbase_entries = got_object_tree_get_nentries(base_tree);
5275 for (i = 0; i < nbase_entries; i++) {
5276 struct got_commitable *ct = NULL;
5278 te = got_object_tree_get_entry(base_tree, i);
5279 if (got_object_tree_entry_is_submodule(te)) {
5280 /* Entry is a submodule; just copy it. */
5281 err = got_object_tree_entry_dup(&new_te, te);
5284 err = insert_tree_entry(new_te, &paths);
5291 if (S_ISDIR(te->mode)) {
5293 err = got_object_tree_entry_dup(&new_te, te);
5296 err = match_modified_subtree(&modified, te,
5297 path_base_tree, commitable_paths);
5300 /* Avoid recursion into unmodified subtrees. */
5302 struct got_object_id *new_id;
5304 err = write_subtree(&new_id,
5306 path_base_tree, commitable_paths,
5307 status_cb, status_arg, repo);
5310 if (nsubentries == 0) {
5311 /* All entries were deleted. */
5315 memcpy(&new_te->id, new_id,
5316 sizeof(new_te->id));
5319 err = insert_tree_entry(new_te, &paths);
5326 err = match_deleted_or_modified_ct(&ct, te,
5327 path_base_tree, commitable_paths);
5331 /* NB: Deleted entries get dropped here. */
5332 if (ct->status == GOT_STATUS_MODIFY ||
5333 ct->status == GOT_STATUS_MODE_CHANGE ||
5334 ct->staged_status == GOT_STATUS_MODIFY) {
5335 err = alloc_modified_blob_tree_entry(
5339 err = insert_tree_entry(new_te, &paths);
5344 err = report_ct_status(ct, status_cb,
5349 /* Entry is unchanged; just copy it. */
5350 err = got_object_tree_entry_dup(&new_te, te);
5353 err = insert_tree_entry(new_te, &paths);
5361 /* Write new list of entries; deleted entries have been dropped. */
5362 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
5364 got_pathlist_free(&paths);
5368 static const struct got_error *
5369 update_fileindex_after_commit(struct got_worktree *worktree,
5370 struct got_pathlist_head *commitable_paths,
5371 struct got_object_id *new_base_commit_id,
5372 struct got_fileindex *fileindex, int have_staged_files)
5374 const struct got_error *err = NULL;
5375 struct got_pathlist_entry *pe;
5376 char *relpath = NULL;
5378 TAILQ_FOREACH(pe, commitable_paths, entry) {
5379 struct got_fileindex_entry *ie;
5380 struct got_commitable *ct = pe->data;
5382 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5384 err = got_path_skip_common_ancestor(&relpath,
5385 worktree->root_path, ct->ondisk_path);
5390 if (ct->status == GOT_STATUS_DELETE ||
5391 ct->staged_status == GOT_STATUS_DELETE) {
5392 got_fileindex_entry_remove(fileindex, ie);
5393 } else if (ct->staged_status == GOT_STATUS_ADD ||
5394 ct->staged_status == GOT_STATUS_MODIFY) {
5395 got_fileindex_entry_stage_set(ie,
5396 GOT_FILEIDX_STAGE_NONE);
5397 got_fileindex_entry_staged_filetype_set(ie, 0);
5399 err = got_fileindex_entry_update(ie,
5400 worktree->root_fd, relpath,
5401 ct->staged_blob_id->sha1,
5402 new_base_commit_id->sha1,
5403 !have_staged_files);
5405 err = got_fileindex_entry_update(ie,
5406 worktree->root_fd, relpath,
5408 new_base_commit_id->sha1,
5409 !have_staged_files);
5411 err = got_fileindex_entry_alloc(&ie, pe->path);
5414 err = got_fileindex_entry_update(ie,
5415 worktree->root_fd, relpath, ct->blob_id->sha1,
5416 new_base_commit_id->sha1, 1);
5418 got_fileindex_entry_free(ie);
5421 err = got_fileindex_entry_add(fileindex, ie);
5423 got_fileindex_entry_free(ie);
5436 static const struct got_error *
5437 check_out_of_date(const char *in_repo_path, unsigned char status,
5438 unsigned char staged_status, struct got_object_id *base_blob_id,
5439 struct got_object_id *base_commit_id,
5440 struct got_object_id *head_commit_id, struct got_repository *repo,
5443 const struct got_error *err = NULL;
5444 struct got_object_id *id = NULL;
5446 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
5447 /* Trivial case: base commit == head commit */
5448 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
5451 * Ensure file content which local changes were based
5452 * on matches file content in the branch head.
5454 err = got_object_id_by_path(&id, repo, head_commit_id,
5457 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5458 err = got_error(ood_errcode);
5460 } else if (got_object_id_cmp(id, base_blob_id) != 0)
5461 err = got_error(ood_errcode);
5463 /* Require that added files don't exist in the branch head. */
5464 err = got_object_id_by_path(&id, repo, head_commit_id,
5466 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
5468 err = id ? got_error(ood_errcode) : NULL;
5475 const struct got_error *
5476 commit_worktree(struct got_object_id **new_commit_id,
5477 struct got_pathlist_head *commitable_paths,
5478 struct got_object_id *head_commit_id, struct got_worktree *worktree,
5479 const char *author, const char *committer,
5480 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5481 got_worktree_status_cb status_cb, void *status_arg,
5482 struct got_repository *repo)
5484 const struct got_error *err = NULL, *unlockerr = NULL;
5485 struct got_pathlist_entry *pe;
5486 const char *head_ref_name = NULL;
5487 struct got_commit_object *head_commit = NULL;
5488 struct got_reference *head_ref2 = NULL;
5489 struct got_object_id *head_commit_id2 = NULL;
5490 struct got_tree_object *head_tree = NULL;
5491 struct got_object_id *new_tree_id = NULL;
5493 struct got_object_id_queue parent_ids;
5494 struct got_object_qid *pid = NULL;
5495 char *logmsg = NULL;
5497 *new_commit_id = NULL;
5499 SIMPLEQ_INIT(&parent_ids);
5501 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
5505 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
5509 if (commit_msg_cb != NULL) {
5510 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5515 if (logmsg == NULL || strlen(logmsg) == 0) {
5516 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5520 /* Create blobs from added and modified files and record their IDs. */
5521 TAILQ_FOREACH(pe, commitable_paths, entry) {
5522 struct got_commitable *ct = pe->data;
5525 /* Blobs for staged files already exist. */
5526 if (ct->staged_status == GOT_STATUS_ADD ||
5527 ct->staged_status == GOT_STATUS_MODIFY)
5530 if (ct->status != GOT_STATUS_ADD &&
5531 ct->status != GOT_STATUS_MODIFY &&
5532 ct->status != GOT_STATUS_MODE_CHANGE)
5535 if (asprintf(&ondisk_path, "%s/%s",
5536 worktree->root_path, pe->path) == -1) {
5537 err = got_error_from_errno("asprintf");
5540 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5546 /* Recursively write new tree objects. */
5547 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5548 commitable_paths, status_cb, status_arg, repo);
5552 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5555 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5556 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5557 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5558 got_object_qid_free(pid);
5564 /* Check if a concurrent commit to our branch has occurred. */
5565 head_ref_name = got_worktree_get_head_ref_name(worktree);
5566 if (head_ref_name == NULL) {
5567 err = got_error_from_errno("got_worktree_get_head_ref_name");
5570 /* Lock the reference here to prevent concurrent modification. */
5571 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5574 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5577 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5578 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5581 /* Update branch head in repository. */
5582 err = got_ref_change_ref(head_ref2, *new_commit_id);
5585 err = got_ref_write(head_ref2, repo);
5589 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5593 err = ref_base_commit(worktree, repo);
5598 got_object_tree_close(head_tree);
5600 got_object_commit_close(head_commit);
5601 free(head_commit_id2);
5603 unlockerr = got_ref_unlock(head_ref2);
5604 if (unlockerr && err == NULL)
5606 got_ref_close(head_ref2);
5611 static const struct got_error *
5612 check_path_is_commitable(const char *path,
5613 struct got_pathlist_head *commitable_paths)
5615 struct got_pathlist_entry *cpe = NULL;
5616 size_t path_len = strlen(path);
5618 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5619 struct got_commitable *ct = cpe->data;
5620 const char *ct_path = ct->path;
5622 while (ct_path[0] == '/')
5625 if (strcmp(path, ct_path) == 0 ||
5626 got_path_is_child(ct_path, path, path_len))
5631 return got_error_path(path, GOT_ERR_BAD_PATH);
5636 static const struct got_error *
5637 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5639 int *have_staged_files = arg;
5641 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5642 *have_staged_files = 1;
5643 return got_error(GOT_ERR_CANCELLED);
5649 static const struct got_error *
5650 check_non_staged_files(struct got_fileindex *fileindex,
5651 struct got_pathlist_head *paths)
5653 struct got_pathlist_entry *pe;
5654 struct got_fileindex_entry *ie;
5656 TAILQ_FOREACH(pe, paths, entry) {
5657 if (pe->path[0] == '\0')
5659 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5661 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5662 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5663 return got_error_path(pe->path,
5664 GOT_ERR_FILE_NOT_STAGED);
5670 const struct got_error *
5671 got_worktree_commit(struct got_object_id **new_commit_id,
5672 struct got_worktree *worktree, struct got_pathlist_head *paths,
5673 const char *author, const char *committer, int allow_bad_symlinks,
5674 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5675 got_worktree_status_cb status_cb, void *status_arg,
5676 struct got_repository *repo)
5678 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5679 struct got_fileindex *fileindex = NULL;
5680 char *fileindex_path = NULL;
5681 struct got_pathlist_head commitable_paths;
5682 struct collect_commitables_arg cc_arg;
5683 struct got_pathlist_entry *pe;
5684 struct got_reference *head_ref = NULL;
5685 struct got_object_id *head_commit_id = NULL;
5686 int have_staged_files = 0;
5688 *new_commit_id = NULL;
5690 TAILQ_INIT(&commitable_paths);
5692 err = lock_worktree(worktree, LOCK_EX);
5696 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5700 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5704 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5708 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5709 &have_staged_files);
5710 if (err && err->code != GOT_ERR_CANCELLED)
5712 if (have_staged_files) {
5713 err = check_non_staged_files(fileindex, paths);
5718 cc_arg.commitable_paths = &commitable_paths;
5719 cc_arg.worktree = worktree;
5720 cc_arg.fileindex = fileindex;
5722 cc_arg.have_staged_files = have_staged_files;
5723 cc_arg.allow_bad_symlinks = allow_bad_symlinks;
5724 TAILQ_FOREACH(pe, paths, entry) {
5725 err = worktree_status(worktree, pe->path, fileindex, repo,
5726 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5731 if (TAILQ_EMPTY(&commitable_paths)) {
5732 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5736 TAILQ_FOREACH(pe, paths, entry) {
5737 err = check_path_is_commitable(pe->path, &commitable_paths);
5742 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5743 struct got_commitable *ct = pe->data;
5744 const char *ct_path = ct->in_repo_path;
5746 while (ct_path[0] == '/')
5748 err = check_out_of_date(ct_path, ct->status,
5749 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5750 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5756 err = commit_worktree(new_commit_id, &commitable_paths,
5757 head_commit_id, worktree, author, committer,
5758 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5762 err = update_fileindex_after_commit(worktree, &commitable_paths,
5763 *new_commit_id, fileindex, have_staged_files);
5764 sync_err = sync_fileindex(fileindex, fileindex_path);
5765 if (sync_err && err == NULL)
5769 got_fileindex_free(fileindex);
5770 free(fileindex_path);
5771 unlockerr = lock_worktree(worktree, LOCK_SH);
5772 if (unlockerr && err == NULL)
5774 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5775 struct got_commitable *ct = pe->data;
5776 free_commitable(ct);
5778 got_pathlist_free(&commitable_paths);
5783 got_commitable_get_path(struct got_commitable *ct)
5789 got_commitable_get_status(struct got_commitable *ct)
5794 struct check_rebase_ok_arg {
5795 struct got_worktree *worktree;
5796 struct got_repository *repo;
5799 static const struct got_error *
5800 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5802 const struct got_error *err = NULL;
5803 struct check_rebase_ok_arg *a = arg;
5804 unsigned char status;
5808 /* Reject rebase of a work tree with mixed base commits. */
5809 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5810 SHA1_DIGEST_LENGTH))
5811 return got_error(GOT_ERR_MIXED_COMMITS);
5813 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5815 return got_error_from_errno("asprintf");
5817 /* Reject rebase of a work tree with modified or staged files. */
5818 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5823 if (status != GOT_STATUS_NO_CHANGE)
5824 return got_error(GOT_ERR_MODIFIED);
5825 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5826 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5831 const struct got_error *
5832 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5833 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5834 struct got_worktree *worktree, struct got_reference *branch,
5835 struct got_repository *repo)
5837 const struct got_error *err = NULL;
5838 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5839 char *branch_ref_name = NULL;
5840 char *fileindex_path = NULL;
5841 struct check_rebase_ok_arg ok_arg;
5842 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5843 struct got_object_id *wt_branch_tip = NULL;
5845 *new_base_branch_ref = NULL;
5849 err = lock_worktree(worktree, LOCK_EX);
5853 err = open_fileindex(fileindex, &fileindex_path, worktree);
5857 ok_arg.worktree = worktree;
5859 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5864 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5868 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5872 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5876 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5881 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5884 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5885 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5889 err = got_ref_alloc_symref(new_base_branch_ref,
5890 new_base_branch_ref_name, wt_branch);
5893 err = got_ref_write(*new_base_branch_ref, repo);
5897 /* TODO Lock original branch's ref while rebasing? */
5899 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5903 err = got_ref_write(branch_ref, repo);
5907 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5908 worktree->base_commit_id);
5911 err = got_ref_write(*tmp_branch, repo);
5915 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5919 free(fileindex_path);
5920 free(tmp_branch_name);
5921 free(new_base_branch_ref_name);
5922 free(branch_ref_name);
5924 got_ref_close(branch_ref);
5926 got_ref_close(wt_branch);
5927 free(wt_branch_tip);
5929 if (*new_base_branch_ref) {
5930 got_ref_close(*new_base_branch_ref);
5931 *new_base_branch_ref = NULL;
5934 got_ref_close(*tmp_branch);
5938 got_fileindex_free(*fileindex);
5941 lock_worktree(worktree, LOCK_SH);
5946 const struct got_error *
5947 got_worktree_rebase_continue(struct got_object_id **commit_id,
5948 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5949 struct got_reference **branch, struct got_fileindex **fileindex,
5950 struct got_worktree *worktree, struct got_repository *repo)
5952 const struct got_error *err;
5953 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5954 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5955 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5956 char *fileindex_path = NULL;
5957 int have_staged_files = 0;
5960 *new_base_branch = NULL;
5965 err = lock_worktree(worktree, LOCK_EX);
5969 err = open_fileindex(fileindex, &fileindex_path, worktree);
5973 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5974 &have_staged_files);
5975 if (err && err->code != GOT_ERR_CANCELLED)
5977 if (have_staged_files) {
5978 err = got_error(GOT_ERR_STAGED_PATHS);
5982 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5986 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5990 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5994 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5998 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
6002 err = got_ref_open(branch, repo,
6003 got_ref_get_symref_target(branch_ref), 0);
6007 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6011 err = got_ref_resolve(commit_id, repo, commit_ref);
6015 err = got_ref_open(new_base_branch, repo,
6016 new_base_branch_ref_name, 0);
6020 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6024 free(commit_ref_name);
6025 free(branch_ref_name);
6026 free(fileindex_path);
6028 got_ref_close(commit_ref);
6030 got_ref_close(branch_ref);
6035 got_ref_close(*tmp_branch);
6038 if (*new_base_branch) {
6039 got_ref_close(*new_base_branch);
6040 *new_base_branch = NULL;
6043 got_ref_close(*branch);
6047 got_fileindex_free(*fileindex);
6050 lock_worktree(worktree, LOCK_SH);
6055 const struct got_error *
6056 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
6058 const struct got_error *err;
6059 char *tmp_branch_name = NULL;
6061 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6065 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6066 free(tmp_branch_name);
6070 static const struct got_error *
6071 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
6072 char **logmsg, void *arg)
6078 static const struct got_error *
6079 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
6080 const char *path, struct got_object_id *blob_id,
6081 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6082 int dirfd, const char *de_name)
6087 struct collect_merged_paths_arg {
6088 got_worktree_checkout_cb progress_cb;
6090 struct got_pathlist_head *merged_paths;
6093 static const struct got_error *
6094 collect_merged_paths(void *arg, unsigned char status, const char *path)
6096 const struct got_error *err;
6097 struct collect_merged_paths_arg *a = arg;
6099 struct got_pathlist_entry *new;
6101 err = (*a->progress_cb)(a->progress_arg, status, path);
6105 if (status != GOT_STATUS_MERGE &&
6106 status != GOT_STATUS_ADD &&
6107 status != GOT_STATUS_DELETE &&
6108 status != GOT_STATUS_CONFLICT)
6113 return got_error_from_errno("strdup");
6115 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
6116 if (err || new == NULL)
6122 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
6124 struct got_pathlist_entry *pe;
6126 TAILQ_FOREACH(pe, merged_paths, entry)
6127 free((char *)pe->path);
6129 got_pathlist_free(merged_paths);
6132 static const struct got_error *
6133 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
6134 int is_rebase, struct got_repository *repo)
6136 const struct got_error *err;
6137 struct got_reference *commit_ref = NULL;
6139 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6141 if (err->code != GOT_ERR_NOT_REF)
6143 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
6146 err = got_ref_write(commit_ref, repo);
6149 } else if (is_rebase) {
6150 struct got_object_id *stored_id;
6153 err = got_ref_resolve(&stored_id, repo, commit_ref);
6156 cmp = got_object_id_cmp(commit_id, stored_id);
6159 err = got_error(GOT_ERR_REBASE_COMMITID);
6165 got_ref_close(commit_ref);
6169 static const struct got_error *
6170 rebase_merge_files(struct got_pathlist_head *merged_paths,
6171 const char *commit_ref_name, struct got_worktree *worktree,
6172 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
6173 struct got_object_id *commit_id, struct got_repository *repo,
6174 got_worktree_checkout_cb progress_cb, void *progress_arg,
6175 got_cancel_cb cancel_cb, void *cancel_arg)
6177 const struct got_error *err;
6178 struct got_reference *commit_ref = NULL;
6179 struct collect_merged_paths_arg cmp_arg;
6180 char *fileindex_path;
6182 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6184 err = get_fileindex_path(&fileindex_path, worktree);
6188 cmp_arg.progress_cb = progress_cb;
6189 cmp_arg.progress_arg = progress_arg;
6190 cmp_arg.merged_paths = merged_paths;
6191 err = merge_files(worktree, fileindex, fileindex_path,
6192 parent_commit_id, commit_id, repo, collect_merged_paths,
6193 &cmp_arg, cancel_cb, cancel_arg);
6195 got_ref_close(commit_ref);
6199 const struct got_error *
6200 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
6201 struct got_worktree *worktree, struct got_fileindex *fileindex,
6202 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6203 struct got_repository *repo,
6204 got_worktree_checkout_cb progress_cb, void *progress_arg,
6205 got_cancel_cb cancel_cb, void *cancel_arg)
6207 const struct got_error *err;
6208 char *commit_ref_name;
6210 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6214 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
6218 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6219 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6220 progress_arg, cancel_cb, cancel_arg);
6222 free(commit_ref_name);
6226 const struct got_error *
6227 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
6228 struct got_worktree *worktree, struct got_fileindex *fileindex,
6229 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
6230 struct got_repository *repo,
6231 got_worktree_checkout_cb progress_cb, void *progress_arg,
6232 got_cancel_cb cancel_cb, void *cancel_arg)
6234 const struct got_error *err;
6235 char *commit_ref_name;
6237 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6241 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6245 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
6246 fileindex, parent_commit_id, commit_id, repo, progress_cb,
6247 progress_arg, cancel_cb, cancel_arg);
6249 free(commit_ref_name);
6253 static const struct got_error *
6254 rebase_commit(struct got_object_id **new_commit_id,
6255 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
6256 struct got_worktree *worktree, struct got_fileindex *fileindex,
6257 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
6258 const char *new_logmsg, struct got_repository *repo)
6260 const struct got_error *err, *sync_err;
6261 struct got_pathlist_head commitable_paths;
6262 struct collect_commitables_arg cc_arg;
6263 char *fileindex_path = NULL;
6264 struct got_reference *head_ref = NULL;
6265 struct got_object_id *head_commit_id = NULL;
6266 char *logmsg = NULL;
6268 TAILQ_INIT(&commitable_paths);
6269 *new_commit_id = NULL;
6271 /* Work tree is locked/unlocked during rebase preparation/teardown. */
6273 err = get_fileindex_path(&fileindex_path, worktree);
6277 cc_arg.commitable_paths = &commitable_paths;
6278 cc_arg.worktree = worktree;
6280 cc_arg.have_staged_files = 0;
6282 * If possible get the status of individual files directly to
6283 * avoid crawling the entire work tree once per rebased commit.
6284 * TODO: Ideally, merged_paths would contain a list of commitables
6285 * we could use so we could skip worktree_status() entirely.
6288 struct got_pathlist_entry *pe;
6289 TAILQ_FOREACH(pe, merged_paths, entry) {
6290 err = worktree_status(worktree, pe->path, fileindex,
6291 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
6297 err = worktree_status(worktree, "", fileindex, repo,
6298 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
6303 if (TAILQ_EMPTY(&commitable_paths)) {
6304 /* No-op change; commit will be elided. */
6305 err = got_ref_delete(commit_ref, repo);
6308 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
6312 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
6316 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6321 logmsg = strdup(new_logmsg);
6322 if (logmsg == NULL) {
6323 err = got_error_from_errno("strdup");
6327 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
6332 /* NB: commit_worktree will call free(logmsg) */
6333 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
6334 worktree, got_object_commit_get_author(orig_commit),
6335 got_object_commit_get_committer(orig_commit),
6336 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
6340 err = got_ref_change_ref(tmp_branch, *new_commit_id);
6344 err = got_ref_delete(commit_ref, repo);
6348 err = update_fileindex_after_commit(worktree, &commitable_paths,
6349 *new_commit_id, fileindex, 0);
6350 sync_err = sync_fileindex(fileindex, fileindex_path);
6351 if (sync_err && err == NULL)
6354 free(fileindex_path);
6355 free(head_commit_id);
6357 got_ref_close(head_ref);
6359 free(*new_commit_id);
6360 *new_commit_id = NULL;
6365 const struct got_error *
6366 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
6367 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6368 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6369 struct got_commit_object *orig_commit,
6370 struct got_object_id *orig_commit_id, struct got_repository *repo)
6372 const struct got_error *err;
6373 char *commit_ref_name;
6374 struct got_reference *commit_ref = NULL;
6375 struct got_object_id *commit_id = NULL;
6377 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6381 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6384 err = got_ref_resolve(&commit_id, repo, commit_ref);
6387 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
6388 err = got_error(GOT_ERR_REBASE_COMMITID);
6392 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6393 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
6396 got_ref_close(commit_ref);
6397 free(commit_ref_name);
6402 const struct got_error *
6403 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
6404 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
6405 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6406 struct got_commit_object *orig_commit,
6407 struct got_object_id *orig_commit_id, const char *new_logmsg,
6408 struct got_repository *repo)
6410 const struct got_error *err;
6411 char *commit_ref_name;
6412 struct got_reference *commit_ref = NULL;
6414 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6418 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6422 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
6423 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
6426 got_ref_close(commit_ref);
6427 free(commit_ref_name);
6431 const struct got_error *
6432 got_worktree_rebase_postpone(struct got_worktree *worktree,
6433 struct got_fileindex *fileindex)
6436 got_fileindex_free(fileindex);
6437 return lock_worktree(worktree, LOCK_SH);
6440 static const struct got_error *
6441 delete_ref(const char *name, struct got_repository *repo)
6443 const struct got_error *err;
6444 struct got_reference *ref;
6446 err = got_ref_open(&ref, repo, name, 0);
6448 if (err->code == GOT_ERR_NOT_REF)
6453 err = got_ref_delete(ref, repo);
6458 static const struct got_error *
6459 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
6461 const struct got_error *err;
6462 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
6463 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6465 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
6468 err = delete_ref(tmp_branch_name, repo);
6472 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
6475 err = delete_ref(new_base_branch_ref_name, repo);
6479 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
6482 err = delete_ref(branch_ref_name, repo);
6486 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
6489 err = delete_ref(commit_ref_name, repo);
6494 free(tmp_branch_name);
6495 free(new_base_branch_ref_name);
6496 free(branch_ref_name);
6497 free(commit_ref_name);
6501 const struct got_error *
6502 create_backup_ref(const char *backup_ref_prefix, struct got_reference *branch,
6503 struct got_object_id *new_commit_id, struct got_repository *repo)
6505 const struct got_error *err;
6506 struct got_reference *ref = NULL;
6507 struct got_object_id *old_commit_id = NULL;
6508 const char *branch_name = NULL;
6509 char *new_id_str = NULL;
6510 char *refname = NULL;
6512 branch_name = got_ref_get_name(branch);
6513 if (strncmp(branch_name, "refs/heads/", 11) != 0)
6514 return got_error(GOT_ERR_BAD_REF_NAME); /* should not happen */
6517 err = got_object_id_str(&new_id_str, new_commit_id);
6521 if (asprintf(&refname, "%s/%s/%s", backup_ref_prefix, branch_name,
6522 new_id_str) == -1) {
6523 err = got_error_from_errno("asprintf");
6527 err = got_ref_resolve(&old_commit_id, repo, branch);
6531 err = got_ref_alloc(&ref, refname, old_commit_id);
6535 err = got_ref_write(ref, repo);
6539 free(old_commit_id);
6545 const struct got_error *
6546 got_worktree_rebase_complete(struct got_worktree *worktree,
6547 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6548 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6549 struct got_repository *repo, int create_backup)
6551 const struct got_error *err, *unlockerr, *sync_err;
6552 struct got_object_id *new_head_commit_id = NULL;
6553 char *fileindex_path = NULL;
6555 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6559 if (create_backup) {
6560 err = create_backup_ref(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
6561 rebased_branch, new_head_commit_id, repo);
6566 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6570 err = got_ref_write(rebased_branch, repo);
6574 err = got_worktree_set_head_ref(worktree, rebased_branch);
6578 err = delete_rebase_refs(worktree, repo);
6582 err = get_fileindex_path(&fileindex_path, worktree);
6585 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
6586 sync_err = sync_fileindex(fileindex, fileindex_path);
6587 if (sync_err && err == NULL)
6590 got_fileindex_free(fileindex);
6591 free(fileindex_path);
6592 free(new_head_commit_id);
6593 unlockerr = lock_worktree(worktree, LOCK_SH);
6594 if (unlockerr && err == NULL)
6599 const struct got_error *
6600 got_worktree_rebase_abort(struct got_worktree *worktree,
6601 struct got_fileindex *fileindex, struct got_repository *repo,
6602 struct got_reference *new_base_branch,
6603 got_worktree_checkout_cb progress_cb, void *progress_arg)
6605 const struct got_error *err, *unlockerr, *sync_err;
6606 struct got_reference *resolved = NULL;
6607 struct got_object_id *commit_id = NULL;
6608 char *fileindex_path = NULL;
6609 struct revert_file_args rfa;
6610 struct got_object_id *tree_id = NULL;
6612 err = lock_worktree(worktree, LOCK_EX);
6616 err = got_ref_open(&resolved, repo,
6617 got_ref_get_symref_target(new_base_branch), 0);
6621 err = got_worktree_set_head_ref(worktree, resolved);
6626 * XXX commits to the base branch could have happened while
6627 * we were busy rebasing; should we store the original commit ID
6628 * when rebase begins and read it back here?
6630 err = got_ref_resolve(&commit_id, repo, resolved);
6634 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6638 err = got_object_id_by_path(&tree_id, repo,
6639 worktree->base_commit_id, worktree->path_prefix);
6643 err = delete_rebase_refs(worktree, repo);
6647 err = get_fileindex_path(&fileindex_path, worktree);
6651 rfa.worktree = worktree;
6652 rfa.fileindex = fileindex;
6653 rfa.progress_cb = progress_cb;
6654 rfa.progress_arg = progress_arg;
6655 rfa.patch_cb = NULL;
6656 rfa.patch_arg = NULL;
6658 err = worktree_status(worktree, "", fileindex, repo,
6659 revert_file, &rfa, NULL, NULL, 0, 0);
6663 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6664 repo, progress_cb, progress_arg, NULL, NULL);
6666 sync_err = sync_fileindex(fileindex, fileindex_path);
6667 if (sync_err && err == NULL)
6670 got_ref_close(resolved);
6674 got_fileindex_free(fileindex);
6675 free(fileindex_path);
6677 unlockerr = lock_worktree(worktree, LOCK_SH);
6678 if (unlockerr && err == NULL)
6683 const struct got_error *
6684 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6685 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6686 struct got_fileindex **fileindex, struct got_worktree *worktree,
6687 struct got_repository *repo)
6689 const struct got_error *err = NULL;
6690 char *tmp_branch_name = NULL;
6691 char *branch_ref_name = NULL;
6692 char *base_commit_ref_name = NULL;
6693 char *fileindex_path = NULL;
6694 struct check_rebase_ok_arg ok_arg;
6695 struct got_reference *wt_branch = NULL;
6696 struct got_reference *base_commit_ref = NULL;
6700 *base_commit_id = NULL;
6703 err = lock_worktree(worktree, LOCK_EX);
6707 err = open_fileindex(fileindex, &fileindex_path, worktree);
6711 ok_arg.worktree = worktree;
6713 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6718 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6722 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6726 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6731 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6736 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6740 err = got_ref_write(*branch_ref, repo);
6744 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6745 worktree->base_commit_id);
6748 err = got_ref_write(base_commit_ref, repo);
6751 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6752 if (*base_commit_id == NULL) {
6753 err = got_error_from_errno("got_object_id_dup");
6757 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6758 worktree->base_commit_id);
6761 err = got_ref_write(*tmp_branch, repo);
6765 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6769 free(fileindex_path);
6770 free(tmp_branch_name);
6771 free(branch_ref_name);
6772 free(base_commit_ref_name);
6774 got_ref_close(wt_branch);
6777 got_ref_close(*branch_ref);
6781 got_ref_close(*tmp_branch);
6784 free(*base_commit_id);
6786 got_fileindex_free(*fileindex);
6789 lock_worktree(worktree, LOCK_SH);
6794 const struct got_error *
6795 got_worktree_histedit_postpone(struct got_worktree *worktree,
6796 struct got_fileindex *fileindex)
6799 got_fileindex_free(fileindex);
6800 return lock_worktree(worktree, LOCK_SH);
6803 const struct got_error *
6804 got_worktree_histedit_in_progress(int *in_progress,
6805 struct got_worktree *worktree)
6807 const struct got_error *err;
6808 char *tmp_branch_name = NULL;
6810 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6814 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6815 free(tmp_branch_name);
6819 const struct got_error *
6820 got_worktree_histedit_continue(struct got_object_id **commit_id,
6821 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6822 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6823 struct got_worktree *worktree, struct got_repository *repo)
6825 const struct got_error *err;
6826 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6827 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6828 struct got_reference *commit_ref = NULL;
6829 struct got_reference *base_commit_ref = NULL;
6830 char *fileindex_path = NULL;
6831 int have_staged_files = 0;
6835 *base_commit_id = NULL;
6838 err = lock_worktree(worktree, LOCK_EX);
6842 err = open_fileindex(fileindex, &fileindex_path, worktree);
6846 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6847 &have_staged_files);
6848 if (err && err->code != GOT_ERR_CANCELLED)
6850 if (have_staged_files) {
6851 err = got_error(GOT_ERR_STAGED_PATHS);
6855 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6859 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6863 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6867 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6872 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6876 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6879 err = got_ref_resolve(commit_id, repo, commit_ref);
6883 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6886 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6890 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6894 free(commit_ref_name);
6895 free(branch_ref_name);
6896 free(fileindex_path);
6898 got_ref_close(commit_ref);
6899 if (base_commit_ref)
6900 got_ref_close(base_commit_ref);
6904 free(*base_commit_id);
6905 *base_commit_id = NULL;
6907 got_ref_close(*tmp_branch);
6911 got_fileindex_free(*fileindex);
6914 lock_worktree(worktree, LOCK_EX);
6919 static const struct got_error *
6920 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6922 const struct got_error *err;
6923 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6924 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6926 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6929 err = delete_ref(tmp_branch_name, repo);
6933 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6937 err = delete_ref(base_commit_ref_name, repo);
6941 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6944 err = delete_ref(branch_ref_name, repo);
6948 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6951 err = delete_ref(commit_ref_name, repo);
6955 free(tmp_branch_name);
6956 free(base_commit_ref_name);
6957 free(branch_ref_name);
6958 free(commit_ref_name);
6962 const struct got_error *
6963 got_worktree_histedit_abort(struct got_worktree *worktree,
6964 struct got_fileindex *fileindex, struct got_repository *repo,
6965 struct got_reference *branch, struct got_object_id *base_commit_id,
6966 got_worktree_checkout_cb progress_cb, void *progress_arg)
6968 const struct got_error *err, *unlockerr, *sync_err;
6969 struct got_reference *resolved = NULL;
6970 char *fileindex_path = NULL;
6971 struct got_object_id *tree_id = NULL;
6972 struct revert_file_args rfa;
6974 err = lock_worktree(worktree, LOCK_EX);
6978 err = got_ref_open(&resolved, repo,
6979 got_ref_get_symref_target(branch), 0);
6983 err = got_worktree_set_head_ref(worktree, resolved);
6987 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6991 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6992 worktree->path_prefix);
6996 err = delete_histedit_refs(worktree, repo);
7000 err = get_fileindex_path(&fileindex_path, worktree);
7004 rfa.worktree = worktree;
7005 rfa.fileindex = fileindex;
7006 rfa.progress_cb = progress_cb;
7007 rfa.progress_arg = progress_arg;
7008 rfa.patch_cb = NULL;
7009 rfa.patch_arg = NULL;
7011 err = worktree_status(worktree, "", fileindex, repo,
7012 revert_file, &rfa, NULL, NULL, 0, 0);
7016 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
7017 repo, progress_cb, progress_arg, NULL, NULL);
7019 sync_err = sync_fileindex(fileindex, fileindex_path);
7020 if (sync_err && err == NULL)
7023 got_ref_close(resolved);
7025 free(fileindex_path);
7027 unlockerr = lock_worktree(worktree, LOCK_SH);
7028 if (unlockerr && err == NULL)
7033 const struct got_error *
7034 got_worktree_histedit_complete(struct got_worktree *worktree,
7035 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7036 struct got_reference *edited_branch, struct got_repository *repo)
7038 const struct got_error *err, *unlockerr, *sync_err;
7039 struct got_object_id *new_head_commit_id = NULL;
7040 struct got_reference *resolved = NULL;
7041 char *fileindex_path = NULL;
7043 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
7047 err = got_ref_open(&resolved, repo,
7048 got_ref_get_symref_target(edited_branch), 0);
7052 err = create_backup_ref(GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
7053 resolved, new_head_commit_id, repo);
7057 err = got_ref_change_ref(resolved, new_head_commit_id);
7061 err = got_ref_write(resolved, repo);
7065 err = got_worktree_set_head_ref(worktree, resolved);
7069 err = delete_histedit_refs(worktree, repo);
7073 err = get_fileindex_path(&fileindex_path, worktree);
7076 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7077 sync_err = sync_fileindex(fileindex, fileindex_path);
7078 if (sync_err && err == NULL)
7081 got_fileindex_free(fileindex);
7082 free(fileindex_path);
7083 free(new_head_commit_id);
7084 unlockerr = lock_worktree(worktree, LOCK_SH);
7085 if (unlockerr && err == NULL)
7090 const struct got_error *
7091 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
7092 struct got_object_id *commit_id, struct got_repository *repo)
7094 const struct got_error *err;
7095 char *commit_ref_name;
7097 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
7101 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
7105 err = delete_ref(commit_ref_name, repo);
7107 free(commit_ref_name);
7111 const struct got_error *
7112 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
7113 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
7114 struct got_worktree *worktree, const char *refname,
7115 struct got_repository *repo)
7117 const struct got_error *err = NULL;
7118 char *fileindex_path = NULL;
7119 struct check_rebase_ok_arg ok_arg;
7123 *base_branch_ref = NULL;
7125 err = lock_worktree(worktree, LOCK_EX);
7129 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
7130 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7131 "cannot integrate a branch into itself; "
7132 "update -b or different branch name required");
7136 err = open_fileindex(fileindex, &fileindex_path, worktree);
7140 /* Preconditions are the same as for rebase. */
7141 ok_arg.worktree = worktree;
7143 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
7148 err = got_ref_open(branch_ref, repo, refname, 1);
7152 err = got_ref_open(base_branch_ref, repo,
7153 got_worktree_get_head_ref_name(worktree), 1);
7157 got_ref_close(*branch_ref);
7160 if (*base_branch_ref) {
7161 got_ref_close(*base_branch_ref);
7162 *base_branch_ref = NULL;
7165 got_fileindex_free(*fileindex);
7168 lock_worktree(worktree, LOCK_SH);
7173 const struct got_error *
7174 got_worktree_integrate_continue(struct got_worktree *worktree,
7175 struct got_fileindex *fileindex, struct got_repository *repo,
7176 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
7177 got_worktree_checkout_cb progress_cb, void *progress_arg,
7178 got_cancel_cb cancel_cb, void *cancel_arg)
7180 const struct got_error *err = NULL, *sync_err, *unlockerr;
7181 char *fileindex_path = NULL;
7182 struct got_object_id *tree_id = NULL, *commit_id = NULL;
7184 err = get_fileindex_path(&fileindex_path, worktree);
7188 err = got_ref_resolve(&commit_id, repo, branch_ref);
7192 err = got_object_id_by_path(&tree_id, repo, commit_id,
7193 worktree->path_prefix);
7197 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
7201 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
7202 progress_cb, progress_arg, cancel_cb, cancel_arg);
7206 err = got_ref_change_ref(base_branch_ref, commit_id);
7210 err = got_ref_write(base_branch_ref, repo);
7214 err = bump_base_commit_id_everywhere(worktree, fileindex, NULL, NULL);
7216 sync_err = sync_fileindex(fileindex, fileindex_path);
7217 if (sync_err && err == NULL)
7221 unlockerr = got_ref_unlock(branch_ref);
7222 if (unlockerr && err == NULL)
7224 got_ref_close(branch_ref);
7226 unlockerr = got_ref_unlock(base_branch_ref);
7227 if (unlockerr && err == NULL)
7229 got_ref_close(base_branch_ref);
7231 got_fileindex_free(fileindex);
7232 free(fileindex_path);
7235 unlockerr = lock_worktree(worktree, LOCK_SH);
7236 if (unlockerr && err == NULL)
7241 const struct got_error *
7242 got_worktree_integrate_abort(struct got_worktree *worktree,
7243 struct got_fileindex *fileindex, struct got_repository *repo,
7244 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
7246 const struct got_error *err = NULL, *unlockerr = NULL;
7248 got_fileindex_free(fileindex);
7250 err = lock_worktree(worktree, LOCK_SH);
7252 unlockerr = got_ref_unlock(branch_ref);
7253 if (unlockerr && err == NULL)
7255 got_ref_close(branch_ref);
7257 unlockerr = got_ref_unlock(base_branch_ref);
7258 if (unlockerr && err == NULL)
7260 got_ref_close(base_branch_ref);
7265 struct check_stage_ok_arg {
7266 struct got_object_id *head_commit_id;
7267 struct got_worktree *worktree;
7268 struct got_fileindex *fileindex;
7269 struct got_repository *repo;
7273 const struct got_error *
7274 check_stage_ok(void *arg, unsigned char status,
7275 unsigned char staged_status, const char *relpath,
7276 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7277 struct got_object_id *commit_id, int dirfd, const char *de_name)
7279 struct check_stage_ok_arg *a = arg;
7280 const struct got_error *err = NULL;
7281 struct got_fileindex_entry *ie;
7282 struct got_object_id base_commit_id;
7283 struct got_object_id *base_commit_idp = NULL;
7284 char *in_repo_path = NULL, *p;
7286 if (status == GOT_STATUS_UNVERSIONED ||
7287 status == GOT_STATUS_NO_CHANGE)
7289 if (status == GOT_STATUS_NONEXISTENT)
7290 return got_error_set_errno(ENOENT, relpath);
7292 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7294 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7296 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
7297 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
7299 return got_error_from_errno("asprintf");
7301 if (got_fileindex_entry_has_commit(ie)) {
7302 memcpy(base_commit_id.sha1, ie->commit_sha1,
7303 SHA1_DIGEST_LENGTH);
7304 base_commit_idp = &base_commit_id;
7307 if (status == GOT_STATUS_CONFLICT) {
7308 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
7310 } else if (status != GOT_STATUS_ADD &&
7311 status != GOT_STATUS_MODIFY &&
7312 status != GOT_STATUS_DELETE) {
7313 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
7317 a->have_changes = 1;
7322 err = check_out_of_date(p, status, staged_status,
7323 blob_id, base_commit_idp, a->head_commit_id, a->repo,
7324 GOT_ERR_STAGE_OUT_OF_DATE);
7330 struct stage_path_arg {
7331 struct got_worktree *worktree;
7332 struct got_fileindex *fileindex;
7333 struct got_repository *repo;
7334 got_worktree_status_cb status_cb;
7336 got_worktree_patch_cb patch_cb;
7338 int staged_something;
7339 int allow_bad_symlinks;
7342 static const struct got_error *
7343 stage_path(void *arg, unsigned char status,
7344 unsigned char staged_status, const char *relpath,
7345 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7346 struct got_object_id *commit_id, int dirfd, const char *de_name)
7348 struct stage_path_arg *a = arg;
7349 const struct got_error *err = NULL;
7350 struct got_fileindex_entry *ie;
7351 char *ondisk_path = NULL, *path_content = NULL;
7353 struct got_object_id *new_staged_blob_id = NULL;
7356 if (status == GOT_STATUS_UNVERSIONED)
7359 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7361 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7363 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
7365 return got_error_from_errno("asprintf");
7368 case GOT_STATUS_ADD:
7369 case GOT_STATUS_MODIFY:
7370 /* XXX could sb.st_mode be passed in by our caller? */
7371 if (lstat(ondisk_path, &sb) == -1) {
7372 err = got_error_from_errno2("lstat", ondisk_path);
7376 if (status == GOT_STATUS_ADD) {
7377 int choice = GOT_PATCH_CHOICE_NONE;
7378 err = (*a->patch_cb)(&choice, a->patch_arg,
7379 status, ie->path, NULL, 1, 1);
7382 if (choice != GOT_PATCH_CHOICE_YES)
7385 err = create_patched_content(&path_content, 0,
7386 staged_blob_id ? staged_blob_id : blob_id,
7387 ondisk_path, dirfd, de_name, ie->path,
7388 a->repo, a->patch_cb, a->patch_arg);
7389 if (err || path_content == NULL)
7393 err = got_object_blob_create(&new_staged_blob_id,
7394 path_content ? path_content : ondisk_path, a->repo);
7397 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7398 SHA1_DIGEST_LENGTH);
7399 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
7400 stage = GOT_FILEIDX_STAGE_ADD;
7402 stage = GOT_FILEIDX_STAGE_MODIFY;
7403 got_fileindex_entry_stage_set(ie, stage);
7404 if (S_ISLNK(sb.st_mode)) {
7405 int is_bad_symlink = 0;
7406 if (!a->allow_bad_symlinks) {
7407 char target_path[PATH_MAX];
7409 target_len = readlink(ondisk_path, target_path,
7410 sizeof(target_path));
7411 if (target_len == -1) {
7412 err = got_error_from_errno2("readlink",
7416 err = is_bad_symlink_target(&is_bad_symlink,
7417 target_path, target_len, ondisk_path,
7418 a->worktree->root_path);
7421 if (is_bad_symlink) {
7422 err = got_error_path(ondisk_path,
7423 GOT_ERR_BAD_SYMLINK);
7428 got_fileindex_entry_staged_filetype_set(ie,
7429 GOT_FILEIDX_MODE_BAD_SYMLINK);
7431 got_fileindex_entry_staged_filetype_set(ie,
7432 GOT_FILEIDX_MODE_SYMLINK);
7434 got_fileindex_entry_staged_filetype_set(ie,
7435 GOT_FILEIDX_MODE_REGULAR_FILE);
7437 a->staged_something = 1;
7438 if (a->status_cb == NULL)
7440 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7441 get_staged_status(ie), relpath, blob_id,
7442 new_staged_blob_id, NULL, dirfd, de_name);
7444 case GOT_STATUS_DELETE:
7445 if (staged_status == GOT_STATUS_DELETE)
7448 int choice = GOT_PATCH_CHOICE_NONE;
7449 err = (*a->patch_cb)(&choice, a->patch_arg, status,
7450 ie->path, NULL, 1, 1);
7453 if (choice == GOT_PATCH_CHOICE_NO)
7455 if (choice != GOT_PATCH_CHOICE_YES) {
7456 err = got_error(GOT_ERR_PATCH_CHOICE);
7460 stage = GOT_FILEIDX_STAGE_DELETE;
7461 got_fileindex_entry_stage_set(ie, stage);
7462 a->staged_something = 1;
7463 if (a->status_cb == NULL)
7465 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
7466 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
7469 case GOT_STATUS_NO_CHANGE:
7471 case GOT_STATUS_CONFLICT:
7472 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
7474 case GOT_STATUS_NONEXISTENT:
7475 err = got_error_set_errno(ENOENT, relpath);
7478 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
7482 if (path_content && unlink(path_content) == -1 && err == NULL)
7483 err = got_error_from_errno2("unlink", path_content);
7486 free(new_staged_blob_id);
7490 const struct got_error *
7491 got_worktree_stage(struct got_worktree *worktree,
7492 struct got_pathlist_head *paths,
7493 got_worktree_status_cb status_cb, void *status_arg,
7494 got_worktree_patch_cb patch_cb, void *patch_arg,
7495 int allow_bad_symlinks, struct got_repository *repo)
7497 const struct got_error *err = NULL, *sync_err, *unlockerr;
7498 struct got_pathlist_entry *pe;
7499 struct got_fileindex *fileindex = NULL;
7500 char *fileindex_path = NULL;
7501 struct got_reference *head_ref = NULL;
7502 struct got_object_id *head_commit_id = NULL;
7503 struct check_stage_ok_arg oka;
7504 struct stage_path_arg spa;
7506 err = lock_worktree(worktree, LOCK_EX);
7510 err = got_ref_open(&head_ref, repo,
7511 got_worktree_get_head_ref_name(worktree), 0);
7514 err = got_ref_resolve(&head_commit_id, repo, head_ref);
7517 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7521 /* Check pre-conditions before staging anything. */
7522 oka.head_commit_id = head_commit_id;
7523 oka.worktree = worktree;
7524 oka.fileindex = fileindex;
7526 oka.have_changes = 0;
7527 TAILQ_FOREACH(pe, paths, entry) {
7528 err = worktree_status(worktree, pe->path, fileindex, repo,
7529 check_stage_ok, &oka, NULL, NULL, 0, 0);
7533 if (!oka.have_changes) {
7534 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7538 spa.worktree = worktree;
7539 spa.fileindex = fileindex;
7541 spa.patch_cb = patch_cb;
7542 spa.patch_arg = patch_arg;
7543 spa.status_cb = status_cb;
7544 spa.status_arg = status_arg;
7545 spa.staged_something = 0;
7546 spa.allow_bad_symlinks = allow_bad_symlinks;
7547 TAILQ_FOREACH(pe, paths, entry) {
7548 err = worktree_status(worktree, pe->path, fileindex, repo,
7549 stage_path, &spa, NULL, NULL, 0, 0);
7553 if (!spa.staged_something) {
7554 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
7558 sync_err = sync_fileindex(fileindex, fileindex_path);
7559 if (sync_err && err == NULL)
7563 got_ref_close(head_ref);
7564 free(head_commit_id);
7565 free(fileindex_path);
7567 got_fileindex_free(fileindex);
7568 unlockerr = lock_worktree(worktree, LOCK_SH);
7569 if (unlockerr && err == NULL)
7574 struct unstage_path_arg {
7575 struct got_worktree *worktree;
7576 struct got_fileindex *fileindex;
7577 struct got_repository *repo;
7578 got_worktree_checkout_cb progress_cb;
7580 got_worktree_patch_cb patch_cb;
7584 static const struct got_error *
7585 create_unstaged_content(char **path_unstaged_content,
7586 char **path_new_staged_content, struct got_object_id *blob_id,
7587 struct got_object_id *staged_blob_id, const char *relpath,
7588 struct got_repository *repo,
7589 got_worktree_patch_cb patch_cb, void *patch_arg)
7591 const struct got_error *err, *free_err;
7592 struct got_blob_object *blob = NULL, *staged_blob = NULL;
7593 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
7594 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
7595 struct got_diffreg_result *diffreg_result = NULL;
7596 int line_cur1 = 1, line_cur2 = 1, n = 0, nchunks_used = 0;
7597 int have_content = 0, have_rejected_content = 0, i = 0, nchanges = 0;
7599 *path_unstaged_content = NULL;
7600 *path_new_staged_content = NULL;
7602 err = got_object_id_str(&label1, blob_id);
7605 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
7609 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7613 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7617 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7621 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7625 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7629 err = got_diff_files(&diffreg_result, f1, label1, f2,
7630 path2, 3, 0, 1, NULL);
7634 err = got_opentemp_named(path_unstaged_content, &outfile,
7635 "got-unstaged-content");
7638 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7639 "got-new-staged-content");
7643 if (fseek(f1, 0L, SEEK_SET) == -1) {
7644 err = got_ferror(f1, GOT_ERR_IO);
7647 if (fseek(f2, 0L, SEEK_SET) == -1) {
7648 err = got_ferror(f2, GOT_ERR_IO);
7651 /* Count the number of actual changes in the diff result. */
7652 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7653 struct diff_chunk_context cc = {};
7654 diff_chunk_context_load_change(&cc, &nchunks_used,
7655 diffreg_result->result, n, 0);
7658 for (n = 0; n < diffreg_result->result->chunks.len; n += nchunks_used) {
7660 err = apply_or_reject_change(&choice, &nchunks_used,
7661 diffreg_result->result, n, relpath, f1, f2,
7662 &line_cur1, &line_cur2,
7663 outfile, rejectfile, ++i, nchanges, patch_cb, patch_arg);
7666 if (choice == GOT_PATCH_CHOICE_YES)
7669 have_rejected_content = 1;
7670 if (choice == GOT_PATCH_CHOICE_QUIT)
7673 if (have_content || have_rejected_content)
7674 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7675 outfile, rejectfile);
7679 got_object_blob_close(blob);
7681 got_object_blob_close(staged_blob);
7682 free_err = got_diffreg_result_free(diffreg_result);
7683 if (free_err && err == NULL)
7685 if (f1 && fclose(f1) == EOF && err == NULL)
7686 err = got_error_from_errno2("fclose", path1);
7687 if (f2 && fclose(f2) == EOF && err == NULL)
7688 err = got_error_from_errno2("fclose", path2);
7689 if (outfile && fclose(outfile) == EOF && err == NULL)
7690 err = got_error_from_errno2("fclose", *path_unstaged_content);
7691 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7692 err = got_error_from_errno2("fclose", *path_new_staged_content);
7693 if (path1 && unlink(path1) == -1 && err == NULL)
7694 err = got_error_from_errno2("unlink", path1);
7695 if (path2 && unlink(path2) == -1 && err == NULL)
7696 err = got_error_from_errno2("unlink", path2);
7697 if (err || !have_content) {
7698 if (*path_unstaged_content &&
7699 unlink(*path_unstaged_content) == -1 && err == NULL)
7700 err = got_error_from_errno2("unlink",
7701 *path_unstaged_content);
7702 free(*path_unstaged_content);
7703 *path_unstaged_content = NULL;
7705 if (err || !have_content || !have_rejected_content) {
7706 if (*path_new_staged_content &&
7707 unlink(*path_new_staged_content) == -1 && err == NULL)
7708 err = got_error_from_errno2("unlink",
7709 *path_new_staged_content);
7710 free(*path_new_staged_content);
7711 *path_new_staged_content = NULL;
7718 static const struct got_error *
7719 unstage_hunks(struct got_object_id *staged_blob_id,
7720 struct got_blob_object *blob_base,
7721 struct got_object_id *blob_id, struct got_fileindex_entry *ie,
7722 const char *ondisk_path, const char *label_orig,
7723 struct got_worktree *worktree, struct got_repository *repo,
7724 got_worktree_patch_cb patch_cb, void *patch_arg,
7725 got_worktree_checkout_cb progress_cb, void *progress_arg)
7727 const struct got_error *err = NULL;
7728 char *path_unstaged_content = NULL;
7729 char *path_new_staged_content = NULL;
7730 char *parent = NULL, *base_path = NULL;
7731 char *blob_base_path = NULL;
7732 struct got_object_id *new_staged_blob_id = NULL;
7733 FILE *f = NULL, *f_base = NULL;
7736 err = create_unstaged_content(&path_unstaged_content,
7737 &path_new_staged_content, blob_id, staged_blob_id,
7738 ie->path, repo, patch_cb, patch_arg);
7742 if (path_unstaged_content == NULL)
7745 if (path_new_staged_content) {
7746 err = got_object_blob_create(&new_staged_blob_id,
7747 path_new_staged_content, repo);
7752 f = fopen(path_unstaged_content, "r");
7754 err = got_error_from_errno2("fopen",
7755 path_unstaged_content);
7758 if (fstat(fileno(f), &sb) == -1) {
7759 err = got_error_from_errno2("fstat", path_unstaged_content);
7762 if (got_fileindex_entry_staged_filetype_get(ie) ==
7763 GOT_FILEIDX_MODE_SYMLINK && sb.st_size < PATH_MAX) {
7764 char link_target[PATH_MAX];
7766 r = fread(link_target, 1, sizeof(link_target), f);
7767 if (r == 0 && ferror(f)) {
7768 err = got_error_from_errno("fread");
7771 if (r >= sizeof(link_target)) { /* should not happen */
7772 err = got_error(GOT_ERR_NO_SPACE);
7775 link_target[r] = '\0';
7776 err = merge_symlink(worktree, blob_base,
7777 ondisk_path, ie->path, label_orig, link_target,
7778 worktree->base_commit_id, repo, progress_cb,
7781 int local_changes_subsumed;
7783 err = got_path_dirname(&parent, ondisk_path);
7787 if (asprintf(&base_path, "%s/got-unstage-blob-orig",
7789 err = got_error_from_errno("asprintf");
7794 err = got_opentemp_named(&blob_base_path, &f_base, base_path);
7797 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_base,
7802 err = merge_file(&local_changes_subsumed, worktree,
7803 blob_base_path, ondisk_path, ie->path,
7804 got_fileindex_perms_to_st(ie),
7805 path_unstaged_content, label_orig, "unstaged",
7806 repo, progress_cb, progress_arg);
7811 if (new_staged_blob_id) {
7812 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
7813 SHA1_DIGEST_LENGTH);
7815 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7816 got_fileindex_entry_staged_filetype_set(ie, 0);
7819 free(new_staged_blob_id);
7820 if (path_unstaged_content &&
7821 unlink(path_unstaged_content) == -1 && err == NULL)
7822 err = got_error_from_errno2("unlink", path_unstaged_content);
7823 if (path_new_staged_content &&
7824 unlink(path_new_staged_content) == -1 && err == NULL)
7825 err = got_error_from_errno2("unlink", path_new_staged_content);
7826 if (blob_base_path && unlink(blob_base_path) == -1 && err == NULL)
7827 err = got_error_from_errno2("unlink", blob_base_path);
7828 if (f_base && fclose(f_base) == EOF && err == NULL)
7829 err = got_error_from_errno2("fclose", path_unstaged_content);
7830 if (f && fclose(f) == EOF && err == NULL)
7831 err = got_error_from_errno2("fclose", path_unstaged_content);
7832 free(path_unstaged_content);
7833 free(path_new_staged_content);
7834 free(blob_base_path);
7840 static const struct got_error *
7841 unstage_path(void *arg, unsigned char status,
7842 unsigned char staged_status, const char *relpath,
7843 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7844 struct got_object_id *commit_id, int dirfd, const char *de_name)
7846 const struct got_error *err = NULL;
7847 struct unstage_path_arg *a = arg;
7848 struct got_fileindex_entry *ie;
7849 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7850 char *ondisk_path = NULL;
7851 char *id_str = NULL, *label_orig = NULL;
7852 int local_changes_subsumed;
7855 if (staged_status != GOT_STATUS_ADD &&
7856 staged_status != GOT_STATUS_MODIFY &&
7857 staged_status != GOT_STATUS_DELETE)
7860 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7862 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7864 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7866 return got_error_from_errno("asprintf");
7868 err = got_object_id_str(&id_str,
7869 commit_id ? commit_id : a->worktree->base_commit_id);
7872 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7874 err = got_error_from_errno("asprintf");
7878 switch (staged_status) {
7879 case GOT_STATUS_MODIFY:
7880 err = got_object_open_as_blob(&blob_base, a->repo,
7885 case GOT_STATUS_ADD:
7887 if (staged_status == GOT_STATUS_ADD) {
7888 int choice = GOT_PATCH_CHOICE_NONE;
7889 err = (*a->patch_cb)(&choice, a->patch_arg,
7890 staged_status, ie->path, NULL, 1, 1);
7893 if (choice != GOT_PATCH_CHOICE_YES)
7896 err = unstage_hunks(staged_blob_id,
7897 blob_base, blob_id, ie, ondisk_path,
7898 label_orig, a->worktree, a->repo,
7899 a->patch_cb, a->patch_arg,
7900 a->progress_cb, a->progress_arg);
7901 break; /* Done with this file. */
7904 err = got_object_open_as_blob(&blob_staged, a->repo,
7905 staged_blob_id, 8192);
7908 switch (got_fileindex_entry_staged_filetype_get(ie)) {
7909 case GOT_FILEIDX_MODE_BAD_SYMLINK:
7910 case GOT_FILEIDX_MODE_REGULAR_FILE:
7911 err = merge_blob(&local_changes_subsumed, a->worktree,
7912 blob_base, ondisk_path, relpath,
7913 got_fileindex_perms_to_st(ie), label_orig,
7914 blob_staged, commit_id ? commit_id :
7915 a->worktree->base_commit_id, a->repo,
7916 a->progress_cb, a->progress_arg);
7918 case GOT_FILEIDX_MODE_SYMLINK:
7919 if (S_ISLNK(got_fileindex_perms_to_st(ie))) {
7920 char *staged_target;
7921 err = got_object_blob_read_to_str(
7922 &staged_target, blob_staged);
7925 err = merge_symlink(a->worktree, blob_base,
7926 ondisk_path, relpath, label_orig,
7927 staged_target, commit_id ? commit_id :
7928 a->worktree->base_commit_id,
7929 a->repo, a->progress_cb, a->progress_arg);
7930 free(staged_target);
7932 err = merge_blob(&local_changes_subsumed,
7933 a->worktree, blob_base, ondisk_path,
7934 relpath, got_fileindex_perms_to_st(ie),
7935 label_orig, blob_staged,
7936 commit_id ? commit_id :
7937 a->worktree->base_commit_id, a->repo,
7938 a->progress_cb, a->progress_arg);
7942 err = got_error_path(relpath, GOT_ERR_BAD_FILETYPE);
7946 got_fileindex_entry_stage_set(ie,
7947 GOT_FILEIDX_STAGE_NONE);
7948 got_fileindex_entry_staged_filetype_set(ie, 0);
7951 case GOT_STATUS_DELETE:
7953 int choice = GOT_PATCH_CHOICE_NONE;
7954 err = (*a->patch_cb)(&choice, a->patch_arg,
7955 staged_status, ie->path, NULL, 1, 1);
7958 if (choice == GOT_PATCH_CHOICE_NO)
7960 if (choice != GOT_PATCH_CHOICE_YES) {
7961 err = got_error(GOT_ERR_PATCH_CHOICE);
7965 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7966 got_fileindex_entry_staged_filetype_set(ie, 0);
7967 err = get_file_status(&status, &sb, ie, ondisk_path,
7968 dirfd, de_name, a->repo);
7971 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7977 got_object_blob_close(blob_base);
7979 got_object_blob_close(blob_staged);
7985 const struct got_error *
7986 got_worktree_unstage(struct got_worktree *worktree,
7987 struct got_pathlist_head *paths,
7988 got_worktree_checkout_cb progress_cb, void *progress_arg,
7989 got_worktree_patch_cb patch_cb, void *patch_arg,
7990 struct got_repository *repo)
7992 const struct got_error *err = NULL, *sync_err, *unlockerr;
7993 struct got_pathlist_entry *pe;
7994 struct got_fileindex *fileindex = NULL;
7995 char *fileindex_path = NULL;
7996 struct unstage_path_arg upa;
7998 err = lock_worktree(worktree, LOCK_EX);
8002 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8006 upa.worktree = worktree;
8007 upa.fileindex = fileindex;
8009 upa.progress_cb = progress_cb;
8010 upa.progress_arg = progress_arg;
8011 upa.patch_cb = patch_cb;
8012 upa.patch_arg = patch_arg;
8013 TAILQ_FOREACH(pe, paths, entry) {
8014 err = worktree_status(worktree, pe->path, fileindex, repo,
8015 unstage_path, &upa, NULL, NULL, 0, 0);
8020 sync_err = sync_fileindex(fileindex, fileindex_path);
8021 if (sync_err && err == NULL)
8024 free(fileindex_path);
8026 got_fileindex_free(fileindex);
8027 unlockerr = lock_worktree(worktree, LOCK_SH);
8028 if (unlockerr && err == NULL)
8033 struct report_file_info_arg {
8034 struct got_worktree *worktree;
8035 got_worktree_path_info_cb info_cb;
8037 struct got_pathlist_head *paths;
8038 got_cancel_cb cancel_cb;
8042 static const struct got_error *
8043 report_file_info(void *arg, struct got_fileindex_entry *ie)
8045 struct report_file_info_arg *a = arg;
8046 struct got_pathlist_entry *pe;
8047 struct got_object_id blob_id, staged_blob_id, commit_id;
8048 struct got_object_id *blob_idp = NULL, *staged_blob_idp = NULL;
8049 struct got_object_id *commit_idp = NULL;
8052 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
8053 return got_error(GOT_ERR_CANCELLED);
8055 TAILQ_FOREACH(pe, a->paths, entry) {
8056 if (pe->path_len == 0 || strcmp(pe->path, ie->path) == 0 ||
8057 got_path_is_child(ie->path, pe->path, pe->path_len))
8060 if (pe == NULL) /* not found */
8063 if (got_fileindex_entry_has_blob(ie)) {
8064 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
8065 blob_idp = &blob_id;
8067 stage = got_fileindex_entry_stage_get(ie);
8068 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
8069 stage == GOT_FILEIDX_STAGE_ADD) {
8070 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
8071 SHA1_DIGEST_LENGTH);
8072 staged_blob_idp = &staged_blob_id;
8075 if (got_fileindex_entry_has_commit(ie)) {
8076 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
8077 commit_idp = &commit_id;
8080 return a->info_cb(a->info_arg, ie->path, got_fileindex_perms_to_st(ie),
8081 (time_t)ie->mtime_sec, blob_idp, staged_blob_idp, commit_idp);
8084 const struct got_error *
8085 got_worktree_path_info(struct got_worktree *worktree,
8086 struct got_pathlist_head *paths,
8087 got_worktree_path_info_cb info_cb, void *info_arg,
8088 got_cancel_cb cancel_cb, void *cancel_arg)
8091 const struct got_error *err = NULL, *unlockerr;
8092 struct got_fileindex *fileindex = NULL;
8093 char *fileindex_path = NULL;
8094 struct report_file_info_arg arg;
8096 err = lock_worktree(worktree, LOCK_SH);
8100 err = open_fileindex(&fileindex, &fileindex_path, worktree);
8104 arg.worktree = worktree;
8105 arg.info_cb = info_cb;
8106 arg.info_arg = info_arg;
8108 arg.cancel_cb = cancel_cb;
8109 arg.cancel_arg = cancel_arg;
8110 err = got_fileindex_for_each_entry_safe(fileindex, report_file_info,
8113 free(fileindex_path);
8115 got_fileindex_free(fileindex);
8116 unlockerr = lock_worktree(worktree, LOCK_UN);
8117 if (unlockerr && err == NULL)