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"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #define GOT_MERGE_LABEL_MERGED "merged change"
64 #define GOT_MERGE_LABEL_BASE "3-way merge base"
66 static const struct got_error *
67 create_meta_file(const char *path_got, const char *name, const char *content)
69 const struct got_error *err = NULL;
72 if (asprintf(&path, "%s/%s", path_got, name) == -1)
73 return got_error_from_errno("asprintf");
75 err = got_path_create_file(path, content);
80 static const struct got_error *
81 update_meta_file(const char *path_got, const char *name, const char *content)
83 const struct got_error *err = NULL;
88 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
89 err = got_error_from_errno("asprintf");
94 err = got_opentemp_named(&tmppath, &tmpfile, path);
99 int len = fprintf(tmpfile, "%s\n", content);
100 if (len != strlen(content) + 1) {
101 err = got_error_from_errno2("fprintf", tmppath);
106 if (rename(tmppath, path) != 0) {
107 err = got_error_from_errno3("rename", tmppath, path);
113 if (fclose(tmpfile) != 0 && err == NULL)
114 err = got_error_from_errno2("fclose", tmppath);
119 static const struct got_error *
120 read_meta_file(char **content, const char *path_got, const char *name)
122 const struct got_error *err = NULL;
130 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
131 err = got_error_from_errno("asprintf");
136 fd = open(path, O_RDONLY | O_NOFOLLOW);
139 err = got_error_path(path, GOT_ERR_WORKTREE_META);
141 err = got_error_from_errno2("open", path);
144 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
145 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
146 : got_error_from_errno2("flock", path));
150 if (fstat(fd, &sb) != 0) {
151 err = got_error_from_errno2("fstat", path);
154 *content = calloc(1, sb.st_size);
155 if (*content == NULL) {
156 err = got_error_from_errno("calloc");
160 n = read(fd, *content, sb.st_size);
161 if (n != sb.st_size) {
162 err = (n == -1 ? got_error_from_errno2("read", path) :
163 got_error_path(path, GOT_ERR_WORKTREE_META));
166 if ((*content)[sb.st_size - 1] != '\n') {
167 err = got_error_path(path, GOT_ERR_WORKTREE_META);
170 (*content)[sb.st_size - 1] = '\0';
173 if (fd != -1 && close(fd) == -1 && err == NULL)
174 err = got_error_from_errno2("close", path_got);
183 static const struct got_error *
184 write_head_ref(const char *path_got, struct got_reference *head_ref)
186 const struct got_error *err = NULL;
189 if (got_ref_is_symbolic(head_ref)) {
190 refstr = got_ref_to_str(head_ref);
192 return got_error_from_errno("got_ref_to_str");
194 refstr = strdup(got_ref_get_name(head_ref));
196 return got_error_from_errno("strdup");
198 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
203 const struct got_error *
204 got_worktree_init(const char *path, struct got_reference *head_ref,
205 const char *prefix, struct got_repository *repo)
207 const struct got_error *err = NULL;
208 struct got_object_id *commit_id = NULL;
210 uint32_t uuid_status;
212 char *path_got = NULL;
213 char *formatstr = NULL;
214 char *absprefix = NULL;
215 char *basestr = NULL;
216 char *uuidstr = NULL;
218 if (strcmp(path, got_repo_get_path(repo)) == 0) {
219 err = got_error(GOT_ERR_WORKTREE_REPO);
223 err = got_ref_resolve(&commit_id, repo, head_ref);
226 err = got_object_get_type(&obj_type, repo, commit_id);
229 if (obj_type != GOT_OBJ_TYPE_COMMIT)
230 return got_error(GOT_ERR_OBJ_TYPE);
232 if (!got_path_is_absolute(prefix)) {
233 if (asprintf(&absprefix, "/%s", prefix) == -1)
234 return got_error_from_errno("asprintf");
237 /* Create top-level directory (may already exist). */
238 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
239 err = got_error_from_errno2("mkdir", path);
243 /* Create .got directory (may already exist). */
244 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
245 err = got_error_from_errno("asprintf");
248 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
249 err = got_error_from_errno2("mkdir", path_got);
253 /* Create an empty lock file. */
254 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
258 /* Create an empty file index. */
259 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
263 /* Write the HEAD reference. */
264 err = write_head_ref(path_got, head_ref);
268 /* Record our base commit. */
269 err = got_object_id_str(&basestr, commit_id);
272 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
276 /* Store path to repository. */
277 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
278 got_repo_get_path(repo));
282 /* Store in-repository path prefix. */
283 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
284 absprefix ? absprefix : prefix);
289 uuid_create(&uuid, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status, "uuid_create");
294 uuid_to_string(&uuid, &uuidstr, &uuid_status);
295 if (uuid_status != uuid_s_ok) {
296 err = got_error_uuid(uuid_status, "uuid_to_string");
299 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
303 /* Stamp work tree with format file. */
304 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
305 err = got_error_from_errno("asprintf");
308 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
322 static const struct got_error *
323 open_worktree(struct got_worktree **worktree, const char *path)
325 const struct got_error *err = NULL;
327 char *formatstr = NULL;
328 char *uuidstr = NULL;
329 char *path_lock = NULL;
330 char *base_commit_id_str = NULL;
331 int version, fd = -1;
333 struct got_repository *repo = NULL;
334 uint32_t uuid_status;
338 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
339 err = got_error_from_errno("asprintf");
344 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
345 err = got_error_from_errno("asprintf");
350 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
352 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
353 : got_error_from_errno2("open", path_lock));
357 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 version = strtonum(formatstr, 1, INT_MAX, &errstr);
363 err = got_error_msg(GOT_ERR_WORKTREE_META,
364 "could not parse work tree format version number");
367 if (version != GOT_WORKTREE_FORMAT_VERSION) {
368 err = got_error(GOT_ERR_WORKTREE_VERS);
372 *worktree = calloc(1, sizeof(**worktree));
373 if (*worktree == NULL) {
374 err = got_error_from_errno("calloc");
377 (*worktree)->lockfd = -1;
379 (*worktree)->root_path = realpath(path, NULL);
380 if ((*worktree)->root_path == NULL) {
381 err = got_error_from_errno2("realpath", path);
384 err = read_meta_file(&(*worktree)->repo_path, path_got,
385 GOT_WORKTREE_REPOSITORY);
389 err = read_meta_file(&(*worktree)->path_prefix, path_got,
390 GOT_WORKTREE_PATH_PREFIX);
394 err = read_meta_file(&base_commit_id_str, path_got,
395 GOT_WORKTREE_BASE_COMMIT);
399 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
403 if (uuid_status != uuid_s_ok) {
404 err = got_error_uuid(uuid_status, "uuid_from_string");
408 err = got_repo_open(&repo, (*worktree)->repo_path, NULL);
412 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
417 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
418 GOT_WORKTREE_HEAD_REF);
421 got_repo_close(repo);
424 free(base_commit_id_str);
430 if (*worktree != NULL)
431 got_worktree_close(*worktree);
434 (*worktree)->lockfd = fd;
439 const struct got_error *
440 got_worktree_open(struct got_worktree **worktree, const char *path)
442 const struct got_error *err = NULL;
445 err = open_worktree(worktree, path);
446 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
450 path = dirname(path);
452 return got_error_from_errno2("dirname", path);
453 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
455 return got_error(GOT_ERR_NOT_WORKTREE);
458 const struct got_error *
459 got_worktree_close(struct got_worktree *worktree)
461 const struct got_error *err = NULL;
462 free(worktree->repo_path);
463 free(worktree->path_prefix);
464 free(worktree->base_commit_id);
465 free(worktree->head_ref_name);
466 if (worktree->lockfd != -1)
467 if (close(worktree->lockfd) != 0)
468 err = got_error_from_errno2("close",
469 got_worktree_get_root_path(worktree));
470 free(worktree->root_path);
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno("asprintf");
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 const struct got_error *
516 got_worktree_set_head_ref(struct got_worktree *worktree,
517 struct got_reference *head_ref)
519 const struct got_error *err = NULL;
520 char *path_got = NULL, *head_ref_name = NULL;
522 if (asprintf(&path_got, "%s/%s", worktree->root_path,
523 GOT_WORKTREE_GOT_DIR) == -1) {
524 err = got_error_from_errno("asprintf");
529 head_ref_name = strdup(got_ref_get_name(head_ref));
530 if (head_ref_name == NULL) {
531 err = got_error_from_errno("strdup");
535 err = write_head_ref(path_got, head_ref);
539 free(worktree->head_ref_name);
540 worktree->head_ref_name = head_ref_name;
548 struct got_object_id *
549 got_worktree_get_base_commit_id(struct got_worktree *worktree)
551 return worktree->base_commit_id;
554 const struct got_error *
555 got_worktree_set_base_commit_id(struct got_worktree *worktree,
556 struct got_repository *repo, struct got_object_id *commit_id)
558 const struct got_error *err;
559 struct got_object *obj = NULL;
561 char *path_got = NULL;
563 if (asprintf(&path_got, "%s/%s", worktree->root_path,
564 GOT_WORKTREE_GOT_DIR) == -1) {
565 err = got_error_from_errno("asprintf");
570 err = got_object_open(&obj, repo, commit_id);
574 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
575 err = got_error(GOT_ERR_OBJ_TYPE);
579 /* Record our base commit. */
580 err = got_object_id_str(&id_str, commit_id);
583 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
587 free(worktree->base_commit_id);
588 worktree->base_commit_id = got_object_id_dup(commit_id);
589 if (worktree->base_commit_id == NULL) {
590 err = got_error_from_errno("got_object_id_dup");
595 got_object_close(obj);
601 static const struct got_error *
602 lock_worktree(struct got_worktree *worktree, int operation)
604 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
605 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
606 : got_error_from_errno2("flock",
607 got_worktree_get_root_path(worktree)));
611 static const struct got_error *
612 add_dir_on_disk(struct got_worktree *worktree, const char *path)
614 const struct got_error *err = NULL;
617 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
618 return got_error_from_errno("asprintf");
620 err = got_path_mkdir(abspath);
621 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
624 if (lstat(abspath, &sb) == -1) {
625 err = got_error_from_errno2("lstat", abspath);
626 } else if (!S_ISDIR(sb.st_mode)) {
627 /* TODO directory is obstructed; do something */
628 err = got_error_path(abspath, GOT_ERR_FILE_OBSTRUCTED);
635 static const struct got_error *
636 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
638 const struct got_error *err = NULL;
641 size_t flen1 = 0, flen2 = 0;
646 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
647 if (flen1 == 0 && ferror(f1)) {
648 err = got_error_from_errno("fread");
651 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
652 if (flen2 == 0 && ferror(f2)) {
653 err = got_error_from_errno("fread");
660 } else if (flen2 == 0) {
664 } else if (flen1 == flen2) {
665 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
678 static const struct got_error *
679 check_files_equal(int *same, const char *f1_path, const char *f2_path)
681 const struct got_error *err = NULL;
684 FILE *f1 = NULL, *f2 = NULL;
688 if (lstat(f1_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f1_path);
694 if (lstat(f2_path, &sb) != 0) {
695 err = got_error_from_errno2("lstat", f2_path);
700 if (size1 != size2) {
705 f1 = fopen(f1_path, "r");
707 return got_error_from_errno2("fopen", f1_path);
709 f2 = fopen(f2_path, "r");
711 err = got_error_from_errno2("fopen", f2_path);
715 err = check_file_contents_equal(same, f1, f2);
717 if (f1 && fclose(f1) != 0 && err == NULL)
718 err = got_error_from_errno("fclose");
719 if (f2 && fclose(f2) != 0 && err == NULL)
720 err = got_error_from_errno("fclose");
726 * Perform a 3-way merge where blob_orig acts as the common ancestor,
727 * the file at deriv_path acts as the first derived version, and the
728 * file on disk acts as the second derived version.
730 static const struct got_error *
731 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
732 struct got_blob_object *blob_orig, const char *ondisk_path,
733 const char *path, uint16_t st_mode, const char *deriv_path,
734 const char *label_orig, const char *label_deriv,
735 struct got_repository *repo,
736 got_worktree_checkout_cb progress_cb, void *progress_arg)
738 const struct got_error *err = NULL;
741 char *blob_orig_path = NULL;
742 char *merged_path = NULL, *base_path = NULL;
746 *local_changes_subsumed = 0;
748 parent = dirname(ondisk_path);
750 return got_error_from_errno2("dirname", ondisk_path);
752 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
753 return got_error_from_errno("asprintf");
755 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
760 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
761 err = got_error_from_errno("asprintf");
766 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
770 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
776 * If the file has no blob, this is an "add vs add" conflict,
777 * and we simply use an empty ancestor file to make both files
778 * appear in the merged result in their entirety.
782 err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
783 blob_orig_path, ondisk_path, label_deriv, label_orig, NULL);
787 err = (*progress_cb)(progress_arg,
788 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
792 if (fsync(merged_fd) != 0) {
793 err = got_error_from_errno("fsync");
797 /* Check if a clean merge has subsumed all local changes. */
798 if (overlapcnt == 0) {
799 err = check_files_equal(local_changes_subsumed, deriv_path,
805 if (fchmod(merged_fd, st_mode) != 0) {
806 err = got_error_from_errno2("fchmod", merged_path);
810 if (rename(merged_path, ondisk_path) != 0) {
811 err = got_error_from_errno3("rename", merged_path,
820 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
821 err = got_error_from_errno("close");
822 if (f_orig && fclose(f_orig) != 0 && err == NULL)
823 err = got_error_from_errno("fclose");
826 if (blob_orig_path) {
827 unlink(blob_orig_path);
828 free(blob_orig_path);
833 static const struct got_error *
834 update_symlink(const char *ondisk_path, const char *target_path,
837 /* This is not atomic but matches what 'ln -sf' does. */
838 if (unlink(ondisk_path) == -1)
839 return got_error_from_errno2("unlink", ondisk_path);
840 if (symlink(target_path, ondisk_path) == -1)
841 return got_error_from_errno3("symlink", target_path,
847 * Overwrite a symlink (or a regular file in case there was a "bad" symlink)
848 * in the work tree with a file that contains conflict markers and the
849 * conflicting target paths of the original version and two derived versions
852 static const struct got_error *
853 install_symlink_conflict(const char *deriv_target,
854 struct got_object_id *deriv_base_commit_id, const char *orig_target,
855 const char *label_orig, const char *local_target, const char *ondisk_path)
857 const struct got_error *err;
858 char *id_str = NULL, *label_deriv = NULL, *path = NULL;
861 err = got_object_id_str(&id_str, deriv_base_commit_id);
863 return got_error_from_errno("asprintf");
865 if (asprintf(&label_deriv, "%s: commit %s",
866 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
867 err = got_error_from_errno("asprintf");
871 err = got_opentemp_named(&path, &f, "got-symlink-conflict");
875 if (fprintf(f, "%s: Could not install symbolic link because of merge "
876 "conflict.\nln(1) may be used to fix the situation. If this is "
877 "intended to be a\nregular file instead then its expected "
878 "contents may be filled in.\nThe following conflicting symlink "
879 "target paths were found:\n"
880 "%s %s\n%s\n%s%s%s%s%s\n%s\n%s\n", getprogname(),
881 GOT_DIFF_CONFLICT_MARKER_BEGIN, label_deriv, deriv_target,
882 orig_target ? label_orig : "",
883 orig_target ? "\n" : "",
884 orig_target ? orig_target : "",
885 orig_target ? "\n" : "",
886 GOT_DIFF_CONFLICT_MARKER_SEP,
887 local_target, GOT_DIFF_CONFLICT_MARKER_END) < 0) {
888 err = got_error_from_errno2("fprintf", path);
892 if (unlink(ondisk_path) == -1) {
893 err = got_error_from_errno2("unlink", ondisk_path);
896 if (rename(path, ondisk_path) == -1) {
897 err = got_error_from_errno3("rename", path, ondisk_path);
900 if (chmod(ondisk_path, GOT_DEFAULT_FILE_MODE) == -1) {
901 err = got_error_from_errno2("chmod", ondisk_path);
905 if (f != NULL && fclose(f) == EOF && err == NULL)
906 err = got_error_from_errno2("fclose", path);
914 * Merge a symlink into the work tree, where blob_orig acts as the common
915 * ancestor, blob_deriv acts as the first derived version, and the symlink
916 * on disk acts as the second derived version.
917 * Assume that contents of both blobs represent symlinks.
919 static const struct got_error *
920 merge_symlink(struct got_worktree *worktree,
921 struct got_blob_object *blob_orig, const char *ondisk_path,
922 const char *path, uint16_t st_mode, const char *label_orig,
923 struct got_blob_object *blob_deriv,
924 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
925 got_worktree_checkout_cb progress_cb, void *progress_arg)
927 const struct got_error *err = NULL;
928 char *ancestor_target = NULL, *deriv_target = NULL;
930 ssize_t ondisk_len, deriv_len;
931 char ondisk_target[PATH_MAX];
932 int have_local_change = 0;
933 int have_incoming_change = 0;
935 if (lstat(ondisk_path, &sb) == -1)
936 return got_error_from_errno2("lstat", ondisk_path);
938 if (!S_ISLNK(sb.st_mode)) {
939 /* TODO symlink is obstructed; do something */
940 return got_error_path(ondisk_path, GOT_ERR_FILE_OBSTRUCTED);
943 ondisk_len = readlink(ondisk_path, ondisk_target,
944 sizeof(ondisk_target));
945 if (ondisk_len == -1) {
946 err = got_error_from_errno2("readlink",
950 ondisk_target[ondisk_len] = '\0';
953 err = got_object_blob_read_to_str(&ancestor_target, blob_orig);
958 err = got_object_blob_read_to_str(&deriv_target, blob_deriv);
962 if (ancestor_target == NULL ||
963 (ondisk_len != strlen(ancestor_target) ||
964 memcmp(ondisk_target, ancestor_target, ondisk_len) != 0))
965 have_local_change = 1;
967 deriv_len = strlen(deriv_target);
968 if (ancestor_target == NULL ||
969 (deriv_len != strlen(ancestor_target) ||
970 memcmp(deriv_target, ancestor_target, deriv_len) != 0))
971 have_incoming_change = 1;
973 if (!have_local_change && !have_incoming_change) {
974 if (ancestor_target) {
975 /* Both sides made the same change. */
976 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
978 } else if (deriv_len == ondisk_len &&
979 memcmp(ondisk_target, deriv_target, deriv_len) == 0) {
980 /* Both sides added the same symlink. */
981 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE,
984 /* Both sides added symlinks which don't match. */
985 err = install_symlink_conflict(deriv_target,
986 deriv_base_commit_id, ancestor_target,
987 label_orig, ondisk_target, ondisk_path);
990 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
993 } else if (!have_local_change && have_incoming_change) {
994 /* Apply the incoming change. */
995 err = update_symlink(ondisk_path, deriv_target,
996 strlen(deriv_target));
999 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1000 } else if (have_local_change && have_incoming_change) {
1001 err = install_symlink_conflict(deriv_target,
1002 deriv_base_commit_id, ancestor_target, label_orig,
1003 ondisk_target, ondisk_path);
1006 err = (*progress_cb)(progress_arg, GOT_STATUS_CONFLICT,
1011 free(ancestor_target);
1017 * Perform a 3-way merge where blob_orig acts as the common ancestor,
1018 * blob_deriv acts as the first derived version, and the file on disk
1019 * acts as the second derived version.
1021 static const struct got_error *
1022 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
1023 struct got_blob_object *blob_orig, const char *ondisk_path,
1024 const char *path, uint16_t st_mode, const char *label_orig,
1025 struct got_blob_object *blob_deriv,
1026 struct got_object_id *deriv_base_commit_id, struct got_repository *repo,
1027 got_worktree_checkout_cb progress_cb, void *progress_arg)
1029 const struct got_error *err = NULL;
1030 FILE *f_deriv = NULL;
1031 char *blob_deriv_path = NULL, *base_path = NULL, *id_str = NULL;
1032 char *label_deriv = NULL, *parent;
1034 *local_changes_subsumed = 0;
1036 parent = dirname(ondisk_path);
1038 return got_error_from_errno2("dirname", ondisk_path);
1041 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
1042 err = got_error_from_errno("asprintf");
1047 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
1050 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
1055 err = got_object_id_str(&id_str, deriv_base_commit_id);
1058 if (asprintf(&label_deriv, "%s: commit %s",
1059 GOT_MERGE_LABEL_MERGED, id_str) == -1) {
1060 err = got_error_from_errno("asprintf");
1064 err = merge_file(local_changes_subsumed, worktree, blob_orig,
1065 ondisk_path, path, st_mode, blob_deriv_path, label_orig,
1066 label_deriv, repo, progress_cb, progress_arg);
1068 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
1069 err = got_error_from_errno("fclose");
1071 if (blob_deriv_path) {
1072 unlink(blob_deriv_path);
1073 free(blob_deriv_path);
1080 static const struct got_error *
1081 create_fileindex_entry(struct got_fileindex *fileindex,
1082 struct got_object_id *base_commit_id, const char *ondisk_path,
1083 const char *path, struct got_object_id *blob_id)
1085 const struct got_error *err = NULL;
1086 struct got_fileindex_entry *new_ie;
1088 err = got_fileindex_entry_alloc(&new_ie, path);
1092 err = got_fileindex_entry_update(new_ie, ondisk_path,
1093 blob_id->sha1, base_commit_id->sha1, 1);
1097 err = got_fileindex_entry_add(fileindex, new_ie);
1100 got_fileindex_entry_free(new_ie);
1105 get_ondisk_perms(int executable, mode_t st_mode)
1107 mode_t xbits = S_IXUSR;
1110 /* Map read bits to execute bits. */
1111 if (st_mode & S_IRGRP)
1113 if (st_mode & S_IROTH)
1115 return st_mode | xbits;
1118 return (st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
1121 /* forward declaration */
1122 static const struct got_error *
1123 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1124 const char *path, mode_t te_mode, mode_t st_mode,
1125 struct got_blob_object *blob, int restoring_missing_file,
1126 int reverting_versioned_file, int installing_bad_symlink,
1127 struct got_repository *repo,
1128 got_worktree_checkout_cb progress_cb, void *progress_arg);
1131 * This function assumes that the provided symlink target points at a
1132 * safe location in the work tree!
1134 static const struct got_error *
1135 replace_existing_symlink(const char *ondisk_path, const char *target_path,
1138 const struct got_error *err = NULL;
1140 char etarget[PATH_MAX];
1144 * "Bad" symlinks (those pointing outside the work tree or into the
1145 * .got directory) are installed in the work tree as a regular file
1146 * which contains the bad symlink target path.
1147 * The new symlink target has already been checked for safety by our
1148 * caller. If we can successfully open a regular file then we simply
1149 * replace this file with a symlink below.
1151 fd = open(ondisk_path, O_RDWR | O_EXCL | O_NOFOLLOW);
1154 return got_error_from_errno2("open", ondisk_path);
1156 /* We are updating an existing on-disk symlink. */
1157 elen = readlink(ondisk_path, etarget, sizeof(etarget));
1159 return got_error_from_errno2("readlink", ondisk_path);
1161 if (elen == target_len &&
1162 memcmp(etarget, target_path, target_len) == 0)
1163 return NULL; /* nothing to do */
1166 err = update_symlink(ondisk_path, target_path, target_len);
1167 if (fd != -1 && close(fd) == -1 && err == NULL)
1168 err = got_error_from_errno2("close", ondisk_path);
1172 static const struct got_error *
1173 install_symlink(struct got_worktree *worktree, const char *ondisk_path,
1174 const char *path, mode_t te_mode, mode_t st_mode,
1175 struct got_blob_object *blob, int restoring_missing_file,
1176 int reverting_versioned_file, struct got_repository *repo,
1177 got_worktree_checkout_cb progress_cb, void *progress_arg)
1179 const struct got_error *err = NULL;
1180 char target_path[PATH_MAX];
1181 size_t len, target_len = 0;
1182 char *resolved_path = NULL, *abspath = NULL;
1183 char *path_got = NULL;
1184 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1185 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1188 * Blob object content specifies the target path of the link.
1189 * If a symbolic link cannot be installed we instead create
1190 * a regular file which contains the link target path stored
1191 * in the blob object.
1194 err = got_object_blob_read_block(&len, blob);
1195 if (len + target_len >= sizeof(target_path)) {
1196 /* Path too long; install as a regular file. */
1197 got_object_blob_rewind(blob);
1198 return install_blob(worktree, ondisk_path, path,
1199 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1200 restoring_missing_file, reverting_versioned_file,
1201 1, repo, progress_cb, progress_arg);
1204 /* Skip blob object header first time around. */
1205 memcpy(target_path + target_len, buf + hdrlen,
1207 target_len += len - hdrlen;
1211 target_path[target_len] = '\0';
1214 * Relative symlink target lookup should begin at the directory
1215 * in which the blob object is being installed.
1217 if (!got_path_is_absolute(target_path)) {
1218 char *parent = dirname(ondisk_path);
1219 if (parent == NULL) {
1220 err = got_error_from_errno2("dirname", ondisk_path);
1223 if (asprintf(&abspath, "%s/%s", parent, target_path) == -1) {
1224 err = got_error_from_errno("asprintf");
1230 * unveil(2) restricts our view of paths in the filesystem.
1231 * ENOENT will occur if a link target path does not exist or
1232 * if it points outside our unveiled path space.
1234 resolved_path = realpath(abspath ? abspath : target_path, NULL);
1235 if (resolved_path == NULL) {
1236 if (errno != ENOENT) {
1237 err = got_error_from_errno2("realpath", target_path);
1242 /* Only allow symlinks pointing at paths within the work tree. */
1243 if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
1244 abspath : target_path), worktree->root_path,
1245 strlen(worktree->root_path))) {
1246 /* install as a regular file */
1247 got_object_blob_rewind(blob);
1248 err = install_blob(worktree, ondisk_path, path,
1249 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1250 restoring_missing_file, reverting_versioned_file, 1,
1251 repo, progress_cb, progress_arg);
1255 /* Do not allow symlinks pointing into the .got directory. */
1256 if (asprintf(&path_got, "%s/%s", worktree->root_path,
1257 GOT_WORKTREE_GOT_DIR) == -1) {
1258 err = got_error_from_errno("asprintf");
1261 if (got_path_is_child(resolved_path ? resolved_path : (abspath ?
1262 abspath : target_path), path_got, strlen(path_got))) {
1263 /* install as a regular file */
1264 got_object_blob_rewind(blob);
1265 err = install_blob(worktree, ondisk_path, path,
1266 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1267 restoring_missing_file, reverting_versioned_file, 1,
1268 repo, progress_cb, progress_arg);
1272 if (symlink(target_path, ondisk_path) == -1) {
1273 if (errno == EEXIST) {
1274 err = replace_existing_symlink(ondisk_path,
1275 target_path, target_len);
1279 err = (*progress_cb)(progress_arg,
1280 GOT_STATUS_UPDATE, path);
1282 goto done; /* Nothing else to do. */
1285 if (errno == ENOENT) {
1286 char *parent = dirname(ondisk_path);
1287 if (parent == NULL) {
1288 err = got_error_from_errno2("dirname",
1292 err = add_dir_on_disk(worktree, parent);
1296 * Retry, and fall through to error handling
1297 * below if this second attempt fails.
1299 if (symlink(target_path, ondisk_path) != -1) {
1300 err = NULL; /* success */
1305 /* Handle errors from first or second creation attempt. */
1306 if (errno == ENAMETOOLONG) {
1307 /* bad target path; install as a regular file */
1308 got_object_blob_rewind(blob);
1309 err = install_blob(worktree, ondisk_path, path,
1310 GOT_DEFAULT_FILE_MODE, st_mode, blob,
1311 restoring_missing_file, reverting_versioned_file, 1,
1312 repo, progress_cb, progress_arg);
1313 } else if (errno == ENOTDIR) {
1314 err = got_error_path(ondisk_path,
1315 GOT_ERR_FILE_OBSTRUCTED);
1317 err = got_error_from_errno3("symlink",
1318 target_path, ondisk_path);
1320 } else if (progress_cb)
1321 err = (*progress_cb)(progress_arg, GOT_STATUS_ADD, path);
1323 free(resolved_path);
1329 static const struct got_error *
1330 install_blob(struct got_worktree *worktree, const char *ondisk_path,
1331 const char *path, mode_t te_mode, mode_t st_mode,
1332 struct got_blob_object *blob, int restoring_missing_file,
1333 int reverting_versioned_file, int installing_bad_symlink,
1334 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1337 const struct got_error *err = NULL;
1341 char *tmppath = NULL;
1343 if (S_ISLNK(te_mode))
1344 return install_symlink(worktree, ondisk_path, path, te_mode,
1345 st_mode, blob, restoring_missing_file,
1346 reverting_versioned_file, repo, progress_cb, progress_arg);
1348 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1349 GOT_DEFAULT_FILE_MODE);
1351 if (errno == ENOENT) {
1352 char *parent = dirname(path);
1354 return got_error_from_errno2("dirname", path);
1355 err = add_dir_on_disk(worktree, parent);
1358 fd = open(ondisk_path,
1359 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
1360 GOT_DEFAULT_FILE_MODE);
1362 return got_error_from_errno2("open",
1364 } else if (errno == EEXIST) {
1365 if (!S_ISREG(st_mode) && !installing_bad_symlink) {
1366 /* TODO file is obstructed; do something */
1367 err = got_error_path(ondisk_path,
1368 GOT_ERR_FILE_OBSTRUCTED);
1371 err = got_opentemp_named_fd(&tmppath, &fd,
1378 return got_error_from_errno2("open", ondisk_path);
1382 if (restoring_missing_file)
1383 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING,
1385 else if (reverting_versioned_file)
1386 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT,
1389 err = (*progress_cb)(progress_arg,
1390 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
1395 hdrlen = got_object_blob_get_hdrlen(blob);
1397 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1398 err = got_object_blob_read_block(&len, blob);
1402 /* Skip blob object header first time around. */
1403 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
1405 err = got_error_from_errno("write");
1407 } else if (outlen != len - hdrlen) {
1408 err = got_error(GOT_ERR_IO);
1415 if (fsync(fd) != 0) {
1416 err = got_error_from_errno("fsync");
1421 if (rename(tmppath, ondisk_path) != 0) {
1422 err = got_error_from_errno3("rename", tmppath,
1429 if (chmod(ondisk_path,
1430 get_ondisk_perms(te_mode & S_IXUSR, st_mode)) == -1) {
1431 err = got_error_from_errno2("chmod", ondisk_path);
1436 if (fd != -1 && close(fd) != 0 && err == NULL)
1437 err = got_error_from_errno("close");
1442 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1443 static const struct got_error *
1444 get_modified_file_content_status(unsigned char *status, FILE *f)
1446 const struct got_error *err = NULL;
1447 const char *markers[3] = {
1448 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1449 GOT_DIFF_CONFLICT_MARKER_SEP,
1450 GOT_DIFF_CONFLICT_MARKER_END
1455 const char delim[3] = {'\0', '\0', '\0'};
1457 while (*status == GOT_STATUS_MODIFY) {
1458 line = fparseln(f, &len, NULL, delim, 0);
1462 err = got_ferror(f, GOT_ERR_IO);
1466 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1467 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1469 *status = GOT_STATUS_CONFLICT;
1479 xbit_differs(struct got_fileindex_entry *ie, uint16_t st_mode)
1481 mode_t ie_mode = got_fileindex_perms_to_st(ie);
1482 return ((ie_mode & S_IXUSR) != (st_mode & S_IXUSR));
1486 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1488 return !(ie->ctime_sec == sb->st_ctime &&
1489 ie->ctime_nsec == sb->st_ctimensec &&
1490 ie->mtime_sec == sb->st_mtime &&
1491 ie->mtime_nsec == sb->st_mtimensec &&
1492 ie->size == (sb->st_size & 0xffffffff) &&
1493 !xbit_differs(ie, sb->st_mode));
1496 static unsigned char
1497 get_staged_status(struct got_fileindex_entry *ie)
1499 switch (got_fileindex_entry_stage_get(ie)) {
1500 case GOT_FILEIDX_STAGE_ADD:
1501 return GOT_STATUS_ADD;
1502 case GOT_FILEIDX_STAGE_DELETE:
1503 return GOT_STATUS_DELETE;
1504 case GOT_FILEIDX_STAGE_MODIFY:
1505 return GOT_STATUS_MODIFY;
1507 return GOT_STATUS_NO_CHANGE;
1511 static const struct got_error *
1512 get_symlink_status(unsigned char *status, struct stat *sb,
1513 struct got_fileindex_entry *ie, const char *abspath,
1514 int dirfd, const char *de_name, struct got_blob_object *blob)
1516 const struct got_error *err = NULL;
1517 char target_path[PATH_MAX];
1518 char etarget[PATH_MAX];
1520 size_t len, target_len = 0;
1521 const uint8_t *buf = got_object_blob_get_read_buf(blob);
1522 size_t hdrlen = got_object_blob_get_hdrlen(blob);
1524 *status = GOT_STATUS_NO_CHANGE;
1526 /* Blob object content specifies the target path of the link. */
1528 err = got_object_blob_read_block(&len, blob);
1531 if (len + target_len >= sizeof(target_path)) {
1533 * Should not happen. The blob contents were OK
1534 * when this symlink was installed.
1536 return got_error(GOT_ERR_NO_SPACE);
1539 /* Skip blob object header first time around. */
1540 memcpy(target_path + target_len, buf + hdrlen,
1542 target_len += len - hdrlen;
1546 target_path[target_len] = '\0';
1549 elen = readlinkat(dirfd, de_name, etarget, sizeof(etarget));
1551 return got_error_from_errno2("readlinkat", abspath);
1553 elen = readlink(abspath, etarget, sizeof(etarget));
1555 return got_error_from_errno2("readlinkat", abspath);
1558 if (elen != target_len || memcmp(etarget, target_path, target_len) != 0)
1559 *status = GOT_STATUS_MODIFY;
1564 static const struct got_error *
1565 get_file_status(unsigned char *status, struct stat *sb,
1566 struct got_fileindex_entry *ie, const char *abspath,
1567 int dirfd, const char *de_name, struct got_repository *repo)
1569 const struct got_error *err = NULL;
1570 struct got_object_id id;
1575 struct got_blob_object *blob = NULL;
1577 unsigned char staged_status = get_staged_status(ie);
1579 *status = GOT_STATUS_NO_CHANGE;
1582 * Whenever the caller provides a directory descriptor and a
1583 * directory entry name for the file, use them! This prevents
1584 * race conditions if filesystem paths change beneath our feet.
1587 if (fstatat(dirfd, de_name, sb, AT_SYMLINK_NOFOLLOW) == -1) {
1588 if (errno == ENOENT) {
1589 if (got_fileindex_entry_has_file_on_disk(ie))
1590 *status = GOT_STATUS_MISSING;
1592 *status = GOT_STATUS_DELETE;
1595 err = got_error_from_errno2("fstatat", abspath);
1599 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
1600 if (fd == -1 && errno != ENOENT && errno != ELOOP)
1601 return got_error_from_errno2("open", abspath);
1602 else if (fd == -1 && errno == ELOOP) {
1603 if (lstat(abspath, sb) == -1)
1604 return got_error_from_errno2("lstat", abspath);
1605 } else if (fd == -1 || fstat(fd, sb) == -1) {
1606 if (errno == ENOENT) {
1607 if (got_fileindex_entry_has_file_on_disk(ie))
1608 *status = GOT_STATUS_MISSING;
1610 *status = GOT_STATUS_DELETE;
1613 err = got_error_from_errno2("fstat", abspath);
1618 if (!S_ISREG(sb->st_mode) && !S_ISLNK(sb->st_mode)) {
1619 *status = GOT_STATUS_OBSTRUCTED;
1623 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1624 *status = GOT_STATUS_DELETE;
1626 } else if (!got_fileindex_entry_has_blob(ie) &&
1627 staged_status != GOT_STATUS_ADD) {
1628 *status = GOT_STATUS_ADD;
1632 if (!stat_info_differs(ie, sb))
1635 if (staged_status == GOT_STATUS_MODIFY ||
1636 staged_status == GOT_STATUS_ADD)
1637 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1639 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1641 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1645 if (S_ISLNK(sb->st_mode)) {
1646 /* Staging changes to symlinks is not yet(?) supported. */
1647 if (staged_status != GOT_STATUS_NO_CHANGE) {
1648 err = got_error_path(abspath, GOT_ERR_FILE_STATUS);
1651 err = get_symlink_status(status, sb, ie, abspath, dirfd,
1658 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
1660 err = got_error_from_errno2("openat", abspath);
1665 f = fdopen(fd, "r");
1667 err = got_error_from_errno2("fdopen", abspath);
1671 hdrlen = got_object_blob_get_hdrlen(blob);
1673 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1674 err = got_object_blob_read_block(&blen, blob);
1677 /* Skip length of blob object header first time around. */
1678 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1679 if (flen == 0 && ferror(f)) {
1680 err = got_error_from_errno("fread");
1685 *status = GOT_STATUS_MODIFY;
1687 } else if (flen == 0) {
1689 *status = GOT_STATUS_MODIFY;
1691 } else if (blen - hdrlen == flen) {
1692 /* Skip blob object header first time around. */
1693 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1694 *status = GOT_STATUS_MODIFY;
1698 *status = GOT_STATUS_MODIFY;
1704 if (*status == GOT_STATUS_MODIFY) {
1706 err = get_modified_file_content_status(status, f);
1707 } else if (xbit_differs(ie, sb->st_mode))
1708 *status = GOT_STATUS_MODE_CHANGE;
1711 got_object_blob_close(blob);
1712 if (f != NULL && fclose(f) == EOF && err == NULL)
1713 err = got_error_from_errno2("fclose", abspath);
1714 if (fd != -1 && close(fd) == -1 && err == NULL)
1715 err = got_error_from_errno2("close", abspath);
1720 * Update timestamps in the file index if a file is unmodified and
1721 * we had to run a full content comparison to find out.
1723 static const struct got_error *
1724 sync_timestamps(char *ondisk_path, unsigned char status,
1725 struct got_fileindex_entry *ie, struct stat *sb)
1727 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1728 return got_fileindex_entry_update(ie, ondisk_path,
1729 ie->blob_sha1, ie->commit_sha1, 1);
1734 static const struct got_error *
1735 update_blob(struct got_worktree *worktree,
1736 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1737 struct got_tree_entry *te, const char *path,
1738 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1741 const struct got_error *err = NULL;
1742 struct got_blob_object *blob = NULL;
1744 unsigned char status = GOT_STATUS_NO_CHANGE;
1747 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1748 return got_error_from_errno("asprintf");
1751 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE) {
1752 err = got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1755 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
1759 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1760 sb.st_mode = got_fileindex_perms_to_st(ie);
1762 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1764 if (status == GOT_STATUS_OBSTRUCTED) {
1765 err = (*progress_cb)(progress_arg, status, path);
1768 if (status == GOT_STATUS_CONFLICT) {
1769 err = (*progress_cb)(progress_arg, GOT_STATUS_CANNOT_UPDATE,
1774 if (ie && status != GOT_STATUS_MISSING &&
1775 (te->mode & S_IXUSR) == (sb.st_mode & S_IXUSR)) {
1776 if (got_fileindex_entry_has_commit(ie) &&
1777 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1778 SHA1_DIGEST_LENGTH) == 0) {
1779 err = sync_timestamps(ondisk_path, status, ie, &sb);
1782 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1786 if (got_fileindex_entry_has_blob(ie) &&
1787 memcmp(ie->blob_sha1, te->id.sha1,
1788 SHA1_DIGEST_LENGTH) == 0) {
1789 err = sync_timestamps(ondisk_path, status, ie, &sb);
1794 err = got_object_open_as_blob(&blob, repo, &te->id, 8192);
1798 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1799 int update_timestamps;
1800 struct got_blob_object *blob2 = NULL;
1801 char *label_orig = NULL;
1802 if (got_fileindex_entry_has_blob(ie)) {
1803 struct got_object_id id2;
1804 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1805 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1809 if (got_fileindex_entry_has_commit(ie)) {
1810 char id_str[SHA1_DIGEST_STRING_LENGTH];
1811 if (got_sha1_digest_to_str(ie->commit_sha1, id_str,
1812 sizeof(id_str)) == NULL) {
1813 err = got_error_path(id_str,
1814 GOT_ERR_BAD_OBJ_ID_STR);
1817 if (asprintf(&label_orig, "%s: commit %s",
1818 GOT_MERGE_LABEL_BASE, id_str) == -1) {
1819 err = got_error_from_errno("asprintf");
1823 err = merge_blob(&update_timestamps, worktree, blob2,
1824 ondisk_path, path, sb.st_mode, label_orig, blob,
1825 worktree->base_commit_id, repo,
1826 progress_cb, progress_arg);
1829 got_object_blob_close(blob2);
1833 * Do not update timestamps of files with local changes.
1834 * Otherwise, a future status walk would treat them as
1835 * unmodified files again.
1837 err = got_fileindex_entry_update(ie, ondisk_path,
1838 blob->id.sha1, worktree->base_commit_id->sha1,
1840 } else if (status == GOT_STATUS_MODE_CHANGE) {
1841 err = got_fileindex_entry_update(ie, ondisk_path,
1842 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1843 } else if (status == GOT_STATUS_DELETE) {
1844 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1847 err = got_fileindex_entry_update(ie, ondisk_path,
1848 blob->id.sha1, worktree->base_commit_id->sha1, 0);
1852 err = install_blob(worktree, ondisk_path, path, te->mode,
1853 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0, 0,
1854 repo, progress_cb, progress_arg);
1858 err = got_fileindex_entry_update(ie, ondisk_path,
1859 blob->id.sha1, worktree->base_commit_id->sha1, 1);
1861 err = create_fileindex_entry(fileindex,
1862 worktree->base_commit_id, ondisk_path, path,
1868 got_object_blob_close(blob);
1874 static const struct got_error *
1875 remove_ondisk_file(const char *root_path, const char *path)
1877 const struct got_error *err = NULL;
1878 char *ondisk_path = NULL;
1880 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1881 return got_error_from_errno("asprintf");
1883 if (unlink(ondisk_path) == -1) {
1884 if (errno != ENOENT)
1885 err = got_error_from_errno2("unlink", ondisk_path);
1887 char *parent = dirname(ondisk_path);
1888 while (parent && strcmp(parent, root_path) != 0) {
1889 if (rmdir(parent) == -1) {
1890 if (errno != ENOTEMPTY)
1891 err = got_error_from_errno2("rmdir",
1895 parent = dirname(parent);
1902 static const struct got_error *
1903 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1904 struct got_fileindex_entry *ie, struct got_repository *repo,
1905 got_worktree_checkout_cb progress_cb, void *progress_arg)
1907 const struct got_error *err = NULL;
1908 unsigned char status;
1912 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
1913 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
1915 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1917 return got_error_from_errno("asprintf");
1919 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, repo);
1923 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1924 status == GOT_STATUS_ADD) {
1925 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1929 * Preserve the working file and change the deleted blob's
1930 * entry into a schedule-add entry.
1932 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1935 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1938 if (status == GOT_STATUS_NO_CHANGE) {
1939 err = remove_ondisk_file(worktree->root_path, ie->path);
1943 got_fileindex_entry_remove(fileindex, ie);
1950 struct diff_cb_arg {
1951 struct got_fileindex *fileindex;
1952 struct got_worktree *worktree;
1953 struct got_repository *repo;
1954 got_worktree_checkout_cb progress_cb;
1956 got_cancel_cb cancel_cb;
1960 static const struct got_error *
1961 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1962 struct got_tree_entry *te, const char *parent_path)
1964 struct diff_cb_arg *a = arg;
1966 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1967 return got_error(GOT_ERR_CANCELLED);
1969 return update_blob(a->worktree, a->fileindex, ie, te,
1970 ie->path, a->repo, a->progress_cb, a->progress_arg);
1973 static const struct got_error *
1974 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1976 struct diff_cb_arg *a = arg;
1978 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1979 return got_error(GOT_ERR_CANCELLED);
1981 return delete_blob(a->worktree, a->fileindex, ie,
1982 a->repo, a->progress_cb, a->progress_arg);
1985 static const struct got_error *
1986 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1988 struct diff_cb_arg *a = arg;
1989 const struct got_error *err;
1992 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1993 return got_error(GOT_ERR_CANCELLED);
1995 if (got_object_tree_entry_is_submodule(te))
1998 if (asprintf(&path, "%s%s%s", parent_path,
1999 parent_path[0] ? "/" : "", te->name)
2001 return got_error_from_errno("asprintf");
2003 if (S_ISDIR(te->mode))
2004 err = add_dir_on_disk(a->worktree, path);
2006 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
2007 a->repo, a->progress_cb, a->progress_arg);
2013 static const struct got_error *
2014 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
2016 const struct got_error *err = NULL;
2017 char *uuidstr = NULL;
2018 uint32_t uuid_status;
2022 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
2023 if (uuid_status != uuid_s_ok)
2024 return got_error_uuid(uuid_status, "uuid_to_string");
2026 if (asprintf(refname, "%s-%s", prefix, uuidstr)
2028 err = got_error_from_errno("asprintf");
2035 const struct got_error *
2036 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
2038 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
2041 static const struct got_error *
2042 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
2044 return get_ref_name(refname, worktree,
2045 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
2048 static const struct got_error *
2049 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
2051 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
2054 static const struct got_error *
2055 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
2057 return get_ref_name(refname, worktree,
2058 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
2061 static const struct got_error *
2062 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
2064 return get_ref_name(refname, worktree,
2065 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
2068 static const struct got_error *
2069 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
2071 return get_ref_name(refname, worktree,
2072 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
2075 static const struct got_error *
2076 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
2078 return get_ref_name(refname, worktree,
2079 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
2082 static const struct got_error *
2083 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
2085 return get_ref_name(refname, worktree,
2086 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
2089 static const struct got_error *
2090 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
2092 return get_ref_name(refname, worktree,
2093 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
2096 const struct got_error *
2097 got_worktree_get_histedit_script_path(char **path,
2098 struct got_worktree *worktree)
2100 if (asprintf(path, "%s/%s/%s", worktree->root_path,
2101 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
2103 return got_error_from_errno("asprintf");
2109 * Prevent Git's garbage collector from deleting our base commit by
2110 * setting a reference to our base commit's ID.
2112 static const struct got_error *
2113 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
2115 const struct got_error *err = NULL;
2116 struct got_reference *ref = NULL;
2119 err = got_worktree_get_base_ref_name(&refname, worktree);
2123 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
2127 err = got_ref_write(ref, repo);
2135 static const struct got_error *
2136 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
2138 const struct got_error *err = NULL;
2140 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
2141 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2142 err = got_error_from_errno("asprintf");
2143 *fileindex_path = NULL;
2149 static const struct got_error *
2150 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
2151 struct got_worktree *worktree)
2153 const struct got_error *err = NULL;
2156 *fileindex_path = NULL;
2157 *fileindex = got_fileindex_alloc();
2158 if (*fileindex == NULL)
2159 return got_error_from_errno("got_fileindex_alloc");
2161 err = get_fileindex_path(fileindex_path, worktree);
2165 index = fopen(*fileindex_path, "rb");
2166 if (index == NULL) {
2167 if (errno != ENOENT)
2168 err = got_error_from_errno2("fopen", *fileindex_path);
2170 err = got_fileindex_read(*fileindex, index);
2171 if (fclose(index) != 0 && err == NULL)
2172 err = got_error_from_errno("fclose");
2176 free(*fileindex_path);
2177 *fileindex_path = NULL;
2178 got_fileindex_free(*fileindex);
2184 struct bump_base_commit_id_arg {
2185 struct got_object_id *base_commit_id;
2188 const char *entry_name;
2189 got_worktree_checkout_cb progress_cb;
2193 /* Bump base commit ID of all files within an updated part of the work tree. */
2194 static const struct got_error *
2195 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
2197 const struct got_error *err;
2198 struct bump_base_commit_id_arg *a = arg;
2200 if (a->entry_name) {
2201 if (strcmp(ie->path, a->path) != 0)
2203 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
2206 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
2207 SHA1_DIGEST_LENGTH) == 0)
2210 if (a->progress_cb) {
2211 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
2216 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
2220 static const struct got_error *
2221 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
2223 const struct got_error *err = NULL;
2224 char *new_fileindex_path = NULL;
2225 FILE *new_index = NULL;
2226 struct timespec timeout;
2228 err = got_opentemp_named(&new_fileindex_path, &new_index,
2233 err = got_fileindex_write(fileindex, new_index);
2237 if (rename(new_fileindex_path, fileindex_path) != 0) {
2238 err = got_error_from_errno3("rename", new_fileindex_path,
2240 unlink(new_fileindex_path);
2244 * Sleep for a short amount of time to ensure that files modified after
2245 * this program exits have a different time stamp from the one which
2246 * was recorded in the file index.
2249 timeout.tv_nsec = 1;
2250 nanosleep(&timeout, NULL);
2254 free(new_fileindex_path);
2258 static const struct got_error *
2259 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
2260 struct got_object_id **tree_id, const char *wt_relpath,
2261 struct got_worktree *worktree, struct got_repository *repo)
2263 const struct got_error *err = NULL;
2264 struct got_object_id *id = NULL;
2265 char *in_repo_path = NULL;
2266 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
2268 *entry_type = GOT_OBJ_TYPE_ANY;
2269 *tree_relpath = NULL;
2272 if (wt_relpath[0] == '\0') {
2273 /* Check out all files within the work tree. */
2274 *entry_type = GOT_OBJ_TYPE_TREE;
2275 *tree_relpath = strdup("");
2276 if (*tree_relpath == NULL) {
2277 err = got_error_from_errno("strdup");
2280 err = got_object_id_by_path(tree_id, repo,
2281 worktree->base_commit_id, worktree->path_prefix);
2287 /* Check out a subset of files in the work tree. */
2289 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
2290 is_root_wt ? "" : "/", wt_relpath) == -1) {
2291 err = got_error_from_errno("asprintf");
2295 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
2301 in_repo_path = NULL;
2303 err = got_object_get_type(entry_type, repo, id);
2307 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
2308 /* Check out a single file. */
2309 if (strchr(wt_relpath, '/') == NULL) {
2310 /* Check out a single file in work tree's root dir. */
2311 in_repo_path = strdup(worktree->path_prefix);
2312 if (in_repo_path == NULL) {
2313 err = got_error_from_errno("strdup");
2316 *tree_relpath = strdup("");
2317 if (*tree_relpath == NULL) {
2318 err = got_error_from_errno("strdup");
2322 /* Check out a single file in a subdirectory. */
2323 err = got_path_dirname(tree_relpath, wt_relpath);
2326 if (asprintf(&in_repo_path, "%s%s%s",
2327 worktree->path_prefix, is_root_wt ? "" : "/",
2328 *tree_relpath) == -1) {
2329 err = got_error_from_errno("asprintf");
2333 err = got_object_id_by_path(tree_id, repo,
2334 worktree->base_commit_id, in_repo_path);
2336 /* Check out all files within a subdirectory. */
2337 *tree_id = got_object_id_dup(id);
2338 if (*tree_id == NULL) {
2339 err = got_error_from_errno("got_object_id_dup");
2342 *tree_relpath = strdup(wt_relpath);
2343 if (*tree_relpath == NULL) {
2344 err = got_error_from_errno("strdup");
2352 *entry_type = GOT_OBJ_TYPE_ANY;
2353 free(*tree_relpath);
2354 *tree_relpath = NULL;
2361 static const struct got_error *
2362 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2363 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
2364 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2365 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2367 const struct got_error *err = NULL;
2368 struct got_commit_object *commit = NULL;
2369 struct got_tree_object *tree = NULL;
2370 struct got_fileindex_diff_tree_cb diff_cb;
2371 struct diff_cb_arg arg;
2373 err = ref_base_commit(worktree, repo);
2375 if (!(err->code == GOT_ERR_ERRNO &&
2376 (errno == EACCES || errno == EROFS)))
2378 err = (*progress_cb)(progress_arg,
2379 GOT_STATUS_BASE_REF_ERR, worktree->root_path);
2384 err = got_object_open_as_commit(&commit, repo,
2385 worktree->base_commit_id);
2389 err = got_object_open_as_tree(&tree, repo, tree_id);
2394 got_object_tree_find_entry(tree, entry_name) == NULL) {
2395 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2399 diff_cb.diff_old_new = diff_old_new;
2400 diff_cb.diff_old = diff_old;
2401 diff_cb.diff_new = diff_new;
2402 arg.fileindex = fileindex;
2403 arg.worktree = worktree;
2405 arg.progress_cb = progress_cb;
2406 arg.progress_arg = progress_arg;
2407 arg.cancel_cb = cancel_cb;
2408 arg.cancel_arg = cancel_arg;
2409 err = got_fileindex_diff_tree(fileindex, tree, relpath,
2410 entry_name, repo, &diff_cb, &arg);
2413 got_object_tree_close(tree);
2415 got_object_commit_close(commit);
2419 const struct got_error *
2420 got_worktree_checkout_files(struct got_worktree *worktree,
2421 struct got_pathlist_head *paths, struct got_repository *repo,
2422 got_worktree_checkout_cb progress_cb, void *progress_arg,
2423 got_cancel_cb cancel_cb, void *cancel_arg)
2425 const struct got_error *err = NULL, *sync_err, *unlockerr;
2426 struct got_commit_object *commit = NULL;
2427 struct got_tree_object *tree = NULL;
2428 struct got_fileindex *fileindex = NULL;
2429 char *fileindex_path = NULL;
2430 struct got_pathlist_entry *pe;
2431 struct tree_path_data {
2432 SIMPLEQ_ENTRY(tree_path_data) entry;
2433 struct got_object_id *tree_id;
2438 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
2440 SIMPLEQ_INIT(&tree_paths);
2442 err = lock_worktree(worktree, LOCK_EX);
2446 /* Map all specified paths to in-repository trees. */
2447 TAILQ_FOREACH(pe, paths, entry) {
2448 tpd = malloc(sizeof(*tpd));
2450 err = got_error_from_errno("malloc");
2454 err = find_tree_entry_for_checkout(&tpd->entry_type,
2455 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
2461 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
2462 err = got_path_basename(&tpd->entry_name, pe->path);
2470 tpd->entry_name = NULL;
2472 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
2476 * Read the file index.
2477 * Checking out files is supposed to be an idempotent operation.
2478 * If the on-disk file index is incomplete we will try to complete it.
2480 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2484 tpd = SIMPLEQ_FIRST(&tree_paths);
2485 TAILQ_FOREACH(pe, paths, entry) {
2486 struct bump_base_commit_id_arg bbc_arg;
2488 err = checkout_files(worktree, fileindex, tpd->relpath,
2489 tpd->tree_id, tpd->entry_name, repo,
2490 progress_cb, progress_arg, cancel_cb, cancel_arg);
2494 bbc_arg.base_commit_id = worktree->base_commit_id;
2495 bbc_arg.entry_name = tpd->entry_name;
2496 bbc_arg.path = pe->path;
2497 bbc_arg.path_len = pe->path_len;
2498 bbc_arg.progress_cb = progress_cb;
2499 bbc_arg.progress_arg = progress_arg;
2500 err = got_fileindex_for_each_entry_safe(fileindex,
2501 bump_base_commit_id, &bbc_arg);
2505 tpd = SIMPLEQ_NEXT(tpd, entry);
2507 sync_err = sync_fileindex(fileindex, fileindex_path);
2508 if (sync_err && err == NULL)
2511 free(fileindex_path);
2513 got_object_tree_close(tree);
2515 got_object_commit_close(commit);
2517 got_fileindex_free(fileindex);
2518 while (!SIMPLEQ_EMPTY(&tree_paths)) {
2519 tpd = SIMPLEQ_FIRST(&tree_paths);
2520 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
2525 unlockerr = lock_worktree(worktree, LOCK_SH);
2526 if (unlockerr && err == NULL)
2531 struct merge_file_cb_arg {
2532 struct got_worktree *worktree;
2533 struct got_fileindex *fileindex;
2534 got_worktree_checkout_cb progress_cb;
2536 got_cancel_cb cancel_cb;
2538 const char *label_orig;
2539 struct got_object_id *commit_id2;
2542 static const struct got_error *
2543 merge_file_cb(void *arg, struct got_blob_object *blob1,
2544 struct got_blob_object *blob2, struct got_object_id *id1,
2545 struct got_object_id *id2, const char *path1, const char *path2,
2546 mode_t mode1, mode_t mode2, struct got_repository *repo)
2548 static const struct got_error *err = NULL;
2549 struct merge_file_cb_arg *a = arg;
2550 struct got_fileindex_entry *ie;
2551 char *ondisk_path = NULL;
2553 unsigned char status;
2554 int local_changes_subsumed;
2556 if (blob1 && blob2) {
2557 ie = got_fileindex_entry_get(a->fileindex, path2,
2560 return (*a->progress_cb)(a->progress_arg,
2561 GOT_STATUS_MISSING, path2);
2563 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2565 return got_error_from_errno("asprintf");
2567 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2572 if (status == GOT_STATUS_DELETE) {
2573 err = (*a->progress_cb)(a->progress_arg,
2574 GOT_STATUS_MERGE, path2);
2577 if (status != GOT_STATUS_NO_CHANGE &&
2578 status != GOT_STATUS_MODIFY &&
2579 status != GOT_STATUS_CONFLICT &&
2580 status != GOT_STATUS_ADD) {
2581 err = (*a->progress_cb)(a->progress_arg, status, path2);
2585 if (S_ISLNK(mode1) && S_ISLNK(mode2)) {
2586 err = merge_symlink(a->worktree, blob1,
2587 ondisk_path, path2, sb.st_mode, a->label_orig,
2588 blob2, a->commit_id2, repo, a->progress_cb,
2591 err = merge_blob(&local_changes_subsumed, a->worktree,
2592 blob1, ondisk_path, path2, sb.st_mode,
2593 a->label_orig, blob2, a->commit_id2, repo,
2594 a->progress_cb, a->progress_arg);
2597 ie = got_fileindex_entry_get(a->fileindex, path1,
2600 return (*a->progress_cb)(a->progress_arg,
2601 GOT_STATUS_MISSING, path1);
2603 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2605 return got_error_from_errno("asprintf");
2607 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL,
2613 case GOT_STATUS_NO_CHANGE:
2614 err = (*a->progress_cb)(a->progress_arg,
2615 GOT_STATUS_DELETE, path1);
2618 err = remove_ondisk_file(a->worktree->root_path, path1);
2622 got_fileindex_entry_mark_deleted_from_disk(ie);
2624 case GOT_STATUS_DELETE:
2625 case GOT_STATUS_MISSING:
2626 err = (*a->progress_cb)(a->progress_arg,
2627 GOT_STATUS_DELETE, path1);
2631 got_fileindex_entry_mark_deleted_from_disk(ie);
2633 case GOT_STATUS_ADD:
2634 case GOT_STATUS_MODIFY:
2635 case GOT_STATUS_CONFLICT:
2636 err = (*a->progress_cb)(a->progress_arg,
2637 GOT_STATUS_CANNOT_DELETE, path1);
2641 case GOT_STATUS_OBSTRUCTED:
2642 err = (*a->progress_cb)(a->progress_arg, status, path1);
2650 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2652 return got_error_from_errno("asprintf");
2653 ie = got_fileindex_entry_get(a->fileindex, path2,
2656 err = get_file_status(&status, &sb, ie, ondisk_path,
2660 if (status != GOT_STATUS_NO_CHANGE &&
2661 status != GOT_STATUS_MODIFY &&
2662 status != GOT_STATUS_CONFLICT &&
2663 status != GOT_STATUS_ADD) {
2664 err = (*a->progress_cb)(a->progress_arg,
2668 if (S_ISLNK(mode2)) {
2669 err = merge_symlink(a->worktree, NULL,
2670 ondisk_path, path2, sb.st_mode,
2671 a->label_orig, blob2, a->commit_id2,
2672 repo, a->progress_cb, a->progress_arg);
2676 err = merge_blob(&local_changes_subsumed,
2677 a->worktree, NULL, ondisk_path, path2,
2678 sb.st_mode, a->label_orig, blob2,
2679 a->commit_id2, repo, a->progress_cb,
2684 if (status == GOT_STATUS_DELETE) {
2685 err = got_fileindex_entry_update(ie,
2686 ondisk_path, blob2->id.sha1,
2687 a->worktree->base_commit_id->sha1, 0);
2692 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2693 err = install_blob(a->worktree, ondisk_path, path2,
2694 /* XXX get this from parent tree! */
2695 GOT_DEFAULT_FILE_MODE,
2696 sb.st_mode, blob2, 0, 0, 0, repo,
2697 a->progress_cb, a->progress_arg);
2700 err = got_fileindex_entry_alloc(&ie, path2);
2703 err = got_fileindex_entry_update(ie, ondisk_path,
2706 got_fileindex_entry_free(ie);
2709 err = got_fileindex_entry_add(a->fileindex, ie);
2711 got_fileindex_entry_free(ie);
2721 struct check_merge_ok_arg {
2722 struct got_worktree *worktree;
2723 struct got_repository *repo;
2726 static const struct got_error *
2727 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2729 const struct got_error *err = NULL;
2730 struct check_merge_ok_arg *a = arg;
2731 unsigned char status;
2735 /* Reject merges into a work tree with mixed base commits. */
2736 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2737 SHA1_DIGEST_LENGTH))
2738 return got_error(GOT_ERR_MIXED_COMMITS);
2740 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2742 return got_error_from_errno("asprintf");
2744 /* Reject merges into a work tree with conflicted files. */
2745 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
2748 if (status == GOT_STATUS_CONFLICT)
2749 return got_error(GOT_ERR_CONFLICTS);
2754 static const struct got_error *
2755 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2756 const char *fileindex_path, struct got_object_id *commit_id1,
2757 struct got_object_id *commit_id2, struct got_repository *repo,
2758 got_worktree_checkout_cb progress_cb, void *progress_arg,
2759 got_cancel_cb cancel_cb, void *cancel_arg)
2761 const struct got_error *err = NULL, *sync_err;
2762 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2763 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2764 struct merge_file_cb_arg arg;
2765 char *label_orig = NULL;
2770 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2771 worktree->path_prefix);
2775 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2779 err = got_object_id_str(&id_str, commit_id1);
2783 if (asprintf(&label_orig, "%s: commit %s",
2784 GOT_MERGE_LABEL_BASE, id_str) == -1) {
2785 err = got_error_from_errno("asprintf");
2792 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2793 worktree->path_prefix);
2797 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2801 arg.worktree = worktree;
2802 arg.fileindex = fileindex;
2803 arg.progress_cb = progress_cb;
2804 arg.progress_arg = progress_arg;
2805 arg.cancel_cb = cancel_cb;
2806 arg.cancel_arg = cancel_arg;
2807 arg.label_orig = label_orig;
2808 arg.commit_id2 = commit_id2;
2809 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2810 sync_err = sync_fileindex(fileindex, fileindex_path);
2811 if (sync_err && err == NULL)
2815 got_object_tree_close(tree1);
2817 got_object_tree_close(tree2);
2822 const struct got_error *
2823 got_worktree_merge_files(struct got_worktree *worktree,
2824 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2825 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2826 void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
2828 const struct got_error *err, *unlockerr;
2829 char *fileindex_path = NULL;
2830 struct got_fileindex *fileindex = NULL;
2831 struct check_merge_ok_arg mok_arg;
2833 err = lock_worktree(worktree, LOCK_EX);
2837 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2841 mok_arg.worktree = worktree;
2842 mok_arg.repo = repo;
2843 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2848 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2849 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2852 got_fileindex_free(fileindex);
2853 free(fileindex_path);
2854 unlockerr = lock_worktree(worktree, LOCK_SH);
2855 if (unlockerr && err == NULL)
2860 struct diff_dir_cb_arg {
2861 struct got_fileindex *fileindex;
2862 struct got_worktree *worktree;
2863 const char *status_path;
2864 size_t status_path_len;
2865 struct got_repository *repo;
2866 got_worktree_status_cb status_cb;
2868 got_cancel_cb cancel_cb;
2870 /* A pathlist containing per-directory pathlists of ignore patterns. */
2871 struct got_pathlist_head ignores;
2872 int report_unchanged;
2876 static const struct got_error *
2877 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2878 int dirfd, const char *de_name,
2879 got_worktree_status_cb status_cb, void *status_arg,
2880 struct got_repository *repo, int report_unchanged)
2882 const struct got_error *err = NULL;
2883 unsigned char status = GOT_STATUS_NO_CHANGE;
2884 unsigned char staged_status = get_staged_status(ie);
2886 struct got_object_id blob_id, commit_id, staged_blob_id;
2887 struct got_object_id *blob_idp = NULL, *commit_idp = NULL;
2888 struct got_object_id *staged_blob_idp = NULL;
2890 err = get_file_status(&status, &sb, ie, abspath, dirfd, de_name, repo);
2894 if (status == GOT_STATUS_NO_CHANGE &&
2895 staged_status == GOT_STATUS_NO_CHANGE && !report_unchanged)
2898 if (got_fileindex_entry_has_blob(ie)) {
2899 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2900 blob_idp = &blob_id;
2902 if (got_fileindex_entry_has_commit(ie)) {
2903 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2904 commit_idp = &commit_id;
2906 if (staged_status == GOT_STATUS_ADD ||
2907 staged_status == GOT_STATUS_MODIFY) {
2908 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2909 SHA1_DIGEST_LENGTH);
2910 staged_blob_idp = &staged_blob_id;
2913 return (*status_cb)(status_arg, status, staged_status,
2914 ie->path, blob_idp, staged_blob_idp, commit_idp, dirfd, de_name);
2917 static const struct got_error *
2918 status_old_new(void *arg, struct got_fileindex_entry *ie,
2919 struct dirent *de, const char *parent_path, int dirfd)
2921 const struct got_error *err = NULL;
2922 struct diff_dir_cb_arg *a = arg;
2925 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2926 return got_error(GOT_ERR_CANCELLED);
2928 if (got_path_cmp(parent_path, a->status_path,
2929 strlen(parent_path), a->status_path_len) != 0 &&
2930 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2933 if (parent_path[0]) {
2934 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2935 parent_path, de->d_name) == -1)
2936 return got_error_from_errno("asprintf");
2938 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2940 return got_error_from_errno("asprintf");
2943 err = report_file_status(ie, abspath, dirfd, de->d_name,
2944 a->status_cb, a->status_arg, a->repo, a->report_unchanged);
2949 static const struct got_error *
2950 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2952 struct diff_dir_cb_arg *a = arg;
2953 struct got_object_id blob_id, commit_id;
2954 unsigned char status;
2956 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2957 return got_error(GOT_ERR_CANCELLED);
2959 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2962 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2963 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2964 if (got_fileindex_entry_has_file_on_disk(ie))
2965 status = GOT_STATUS_MISSING;
2967 status = GOT_STATUS_DELETE;
2968 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2969 ie->path, &blob_id, NULL, &commit_id, -1, NULL);
2973 free_ignorelist(struct got_pathlist_head *ignorelist)
2975 struct got_pathlist_entry *pe;
2977 TAILQ_FOREACH(pe, ignorelist, entry)
2978 free((char *)pe->path);
2979 got_pathlist_free(ignorelist);
2983 free_ignores(struct got_pathlist_head *ignores)
2985 struct got_pathlist_entry *pe;
2987 TAILQ_FOREACH(pe, ignores, entry) {
2988 struct got_pathlist_head *ignorelist = pe->data;
2989 free_ignorelist(ignorelist);
2990 free((char *)pe->path);
2992 got_pathlist_free(ignores);
2995 static const struct got_error *
2996 read_ignores(struct got_pathlist_head *ignores, const char *path, FILE *f)
2998 const struct got_error *err = NULL;
2999 struct got_pathlist_entry *pe = NULL;
3000 struct got_pathlist_head *ignorelist;
3001 char *line = NULL, *pattern, *dirpath = NULL;
3002 size_t linesize = 0;
3005 ignorelist = calloc(1, sizeof(*ignorelist));
3006 if (ignorelist == NULL)
3007 return got_error_from_errno("calloc");
3008 TAILQ_INIT(ignorelist);
3010 while ((linelen = getline(&line, &linesize, f)) != -1) {
3011 if (linelen > 0 && line[linelen - 1] == '\n')
3012 line[linelen - 1] = '\0';
3014 /* Git's ignores may contain comments. */
3018 /* Git's negated patterns are not (yet?) supported. */
3022 if (asprintf(&pattern, "%s%s%s", path, path[0] ? "/" : "",
3024 err = got_error_from_errno("asprintf");
3027 err = got_pathlist_insert(NULL, ignorelist, pattern, NULL);
3032 err = got_error_from_errno("getline");
3036 dirpath = strdup(path);
3037 if (dirpath == NULL) {
3038 err = got_error_from_errno("strdup");
3041 err = got_pathlist_insert(&pe, ignores, dirpath, ignorelist);
3044 if (err || pe == NULL) {
3046 free_ignorelist(ignorelist);
3052 match_ignores(struct got_pathlist_head *ignores, const char *path)
3054 struct got_pathlist_entry *pe;
3056 /* Handle patterns which match in all directories. */
3057 TAILQ_FOREACH(pe, ignores, entry) {
3058 struct got_pathlist_head *ignorelist = pe->data;
3059 struct got_pathlist_entry *pi;
3061 TAILQ_FOREACH(pi, ignorelist, entry) {
3062 const char *p, *pattern = pi->path;
3064 if (strncmp(pattern, "**/", 3) != 0)
3069 if (fnmatch(pattern, p,
3070 FNM_PATHNAME | FNM_LEADING_DIR)) {
3071 /* Retry in next directory. */
3072 while (*p && *p != '/')
3084 * The ignores pathlist contains ignore lists from children before
3085 * parents, so we can find the most specific ignorelist by walking
3086 * ignores backwards.
3088 pe = TAILQ_LAST(ignores, got_pathlist_head);
3090 if (got_path_is_child(path, pe->path, pe->path_len)) {
3091 struct got_pathlist_head *ignorelist = pe->data;
3092 struct got_pathlist_entry *pi;
3093 TAILQ_FOREACH(pi, ignorelist, entry) {
3094 const char *pattern = pi->path;
3095 int flags = FNM_LEADING_DIR;
3096 if (strstr(pattern, "/**/") == NULL)
3097 flags |= FNM_PATHNAME;
3098 if (fnmatch(pattern, path, flags))
3103 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
3109 static const struct got_error *
3110 add_ignores(struct got_pathlist_head *ignores, const char *root_path,
3111 const char *path, int dirfd, const char *ignores_filename)
3113 const struct got_error *err = NULL;
3116 FILE *ignoresfile = NULL;
3118 if (asprintf(&ignorespath, "%s/%s%s%s", root_path, path,
3119 path[0] ? "/" : "", ignores_filename) == -1)
3120 return got_error_from_errno("asprintf");
3123 fd = openat(dirfd, ignores_filename, O_RDONLY | O_NOFOLLOW);
3125 if (errno != ENOENT && errno != EACCES)
3126 err = got_error_from_errno2("openat",
3129 ignoresfile = fdopen(fd, "r");
3130 if (ignoresfile == NULL)
3131 err = got_error_from_errno2("fdopen",
3135 err = read_ignores(ignores, path, ignoresfile);
3139 ignoresfile = fopen(ignorespath, "r");
3140 if (ignoresfile == NULL) {
3141 if (errno != ENOENT && errno != EACCES)
3142 err = got_error_from_errno2("fopen",
3145 err = read_ignores(ignores, path, ignoresfile);
3148 if (ignoresfile && fclose(ignoresfile) == EOF && err == NULL)
3149 err = got_error_from_errno2("fclose", path);
3150 if (fd != -1 && close(fd) == -1 && err == NULL)
3151 err = got_error_from_errno2("close", path);
3156 static const struct got_error *
3157 status_new(void *arg, struct dirent *de, const char *parent_path, int dirfd)
3159 const struct got_error *err = NULL;
3160 struct diff_dir_cb_arg *a = arg;
3163 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
3164 return got_error(GOT_ERR_CANCELLED);
3166 if (parent_path[0]) {
3167 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
3168 return got_error_from_errno("asprintf");
3173 if (de->d_type != DT_DIR &&
3174 got_path_is_child(path, a->status_path, a->status_path_len)
3175 && !match_ignores(&a->ignores, path))
3176 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
3177 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3183 static const struct got_error *
3184 status_traverse(void *arg, const char *path, int dirfd)
3186 const struct got_error *err = NULL;
3187 struct diff_dir_cb_arg *a = arg;
3192 err = add_ignores(&a->ignores, a->worktree->root_path,
3193 path, dirfd, ".cvsignore");
3197 err = add_ignores(&a->ignores, a->worktree->root_path, path,
3198 dirfd, ".gitignore");
3203 static const struct got_error *
3204 report_single_file_status(const char *path, const char *ondisk_path,
3205 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
3206 void *status_arg, struct got_repository *repo, int report_unchanged)
3208 struct got_fileindex_entry *ie;
3211 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
3213 return report_file_status(ie, ondisk_path, -1, NULL,
3214 status_cb, status_arg, repo, report_unchanged);
3216 if (lstat(ondisk_path, &sb) == -1) {
3217 if (errno != ENOENT)
3218 return got_error_from_errno2("lstat", ondisk_path);
3219 return (*status_cb)(status_arg, GOT_STATUS_NONEXISTENT,
3220 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3224 if (S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode))
3225 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
3226 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL, -1, NULL);
3231 static const struct got_error *
3232 add_ignores_from_parent_paths(struct got_pathlist_head *ignores,
3233 const char *root_path, const char *path)
3235 const struct got_error *err;
3236 char *parent_path, *next_parent_path = NULL;
3238 err = add_ignores(ignores, root_path, "", -1,
3243 err = add_ignores(ignores, root_path, "", -1,
3248 err = got_path_dirname(&parent_path, path);
3250 if (err->code == GOT_ERR_BAD_PATH)
3251 return NULL; /* cannot traverse parent */
3255 err = add_ignores(ignores, root_path, parent_path, -1,
3259 err = add_ignores(ignores, root_path, parent_path, -1,
3263 err = got_path_dirname(&next_parent_path, parent_path);
3265 if (err->code == GOT_ERR_BAD_PATH)
3266 err = NULL; /* traversed everything */
3270 parent_path = next_parent_path;
3271 next_parent_path = NULL;
3275 free(next_parent_path);
3279 static const struct got_error *
3280 worktree_status(struct got_worktree *worktree, const char *path,
3281 struct got_fileindex *fileindex, struct got_repository *repo,
3282 got_worktree_status_cb status_cb, void *status_arg,
3283 got_cancel_cb cancel_cb, void *cancel_arg, int no_ignores,
3284 int report_unchanged)
3286 const struct got_error *err = NULL;
3288 struct got_fileindex_diff_dir_cb fdiff_cb;
3289 struct diff_dir_cb_arg arg;
3290 char *ondisk_path = NULL;
3292 TAILQ_INIT(&arg.ignores);
3294 if (asprintf(&ondisk_path, "%s%s%s",
3295 worktree->root_path, path[0] ? "/" : "", path) == -1)
3296 return got_error_from_errno("asprintf");
3298 fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW | O_DIRECTORY);
3300 if (errno != ENOTDIR && errno != ENOENT && errno != EACCES &&
3302 err = got_error_from_errno2("open", ondisk_path);
3304 err = report_single_file_status(path, ondisk_path,
3305 fileindex, status_cb, status_arg, repo,
3308 fdiff_cb.diff_old_new = status_old_new;
3309 fdiff_cb.diff_old = status_old;
3310 fdiff_cb.diff_new = status_new;
3311 fdiff_cb.diff_traverse = status_traverse;
3312 arg.fileindex = fileindex;
3313 arg.worktree = worktree;
3314 arg.status_path = path;
3315 arg.status_path_len = strlen(path);
3317 arg.status_cb = status_cb;
3318 arg.status_arg = status_arg;
3319 arg.cancel_cb = cancel_cb;
3320 arg.cancel_arg = cancel_arg;
3321 arg.report_unchanged = report_unchanged;
3322 arg.no_ignores = no_ignores;
3324 err = add_ignores_from_parent_paths(&arg.ignores,
3325 worktree->root_path, path);
3329 err = got_fileindex_diff_dir(fileindex, fd,
3330 worktree->root_path, path, repo, &fdiff_cb, &arg);
3333 free_ignores(&arg.ignores);
3334 if (fd != -1 && close(fd) != 0 && err == NULL)
3335 err = got_error_from_errno("close");
3340 const struct got_error *
3341 got_worktree_status(struct got_worktree *worktree,
3342 struct got_pathlist_head *paths, struct got_repository *repo,
3343 got_worktree_status_cb status_cb, void *status_arg,
3344 got_cancel_cb cancel_cb, void *cancel_arg)
3346 const struct got_error *err = NULL;
3347 char *fileindex_path = NULL;
3348 struct got_fileindex *fileindex = NULL;
3349 struct got_pathlist_entry *pe;
3351 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3355 TAILQ_FOREACH(pe, paths, entry) {
3356 err = worktree_status(worktree, pe->path, fileindex, repo,
3357 status_cb, status_arg, cancel_cb, cancel_arg, 0, 0);
3361 free(fileindex_path);
3362 got_fileindex_free(fileindex);
3366 const struct got_error *
3367 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
3370 const struct got_error *err = NULL;
3371 char *resolved = NULL, *cwd = NULL, *path = NULL;
3377 cwd = getcwd(NULL, 0);
3379 return got_error_from_errno("getcwd");
3381 if (lstat(arg, &sb) == -1) {
3382 if (errno != ENOENT) {
3383 err = got_error_from_errno2("lstat", arg);
3387 if (S_ISLNK(sb.st_mode)) {
3389 * We cannot use realpath(3) with symlinks since we want to
3390 * operate on the symlink itself.
3391 * But we can make the path absolute, assuming it is relative
3392 * to the current working directory, and then canonicalize it.
3394 char *abspath = NULL;
3395 char canonpath[PATH_MAX];
3396 if (!got_path_is_absolute(arg)) {
3397 if (asprintf(&abspath, "%s/%s", cwd, arg) == -1) {
3398 err = got_error_from_errno("asprintf");
3403 err = got_canonpath(abspath ? abspath : arg, canonpath,
3407 resolved = strdup(canonpath);
3408 if (resolved == NULL) {
3409 err = got_error_from_errno("strdup");
3413 resolved = realpath(arg, NULL);
3414 if (resolved == NULL) {
3415 if (errno != ENOENT) {
3416 err = got_error_from_errno2("realpath", arg);
3419 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
3420 err = got_error_from_errno("asprintf");
3426 if (strncmp(got_worktree_get_root_path(worktree), resolved,
3427 strlen(got_worktree_get_root_path(worktree)))) {
3428 err = got_error_path(resolved, GOT_ERR_BAD_PATH);
3432 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
3433 err = got_path_skip_common_ancestor(&path,
3434 got_worktree_get_root_path(worktree), resolved);
3440 err = got_error_from_errno("strdup");
3445 /* XXX status walk can't deal with trailing slash! */
3447 while (len > 0 && path[len - 1] == '/') {
3448 path[len - 1] = '\0';
3461 struct schedule_addition_args {
3462 struct got_worktree *worktree;
3463 struct got_fileindex *fileindex;
3464 got_worktree_checkout_cb progress_cb;
3466 struct got_repository *repo;
3469 static const struct got_error *
3470 schedule_addition(void *arg, unsigned char status, unsigned char staged_status,
3471 const char *relpath, struct got_object_id *blob_id,
3472 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3473 int dirfd, const char *de_name)
3475 struct schedule_addition_args *a = arg;
3476 const struct got_error *err = NULL;
3477 struct got_fileindex_entry *ie;
3481 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3483 return got_error_from_errno("asprintf");
3485 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3487 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd,
3491 /* Re-adding an existing entry is a no-op. */
3492 if (status == GOT_STATUS_ADD)
3494 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3499 if (status != GOT_STATUS_UNVERSIONED) {
3500 err = got_error_path(ondisk_path, GOT_ERR_FILE_STATUS);
3504 err = got_fileindex_entry_alloc(&ie, relpath);
3507 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL, 1);
3509 got_fileindex_entry_free(ie);
3512 err = got_fileindex_entry_add(a->fileindex, ie);
3514 got_fileindex_entry_free(ie);
3521 if (status == GOT_STATUS_ADD)
3523 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_ADD, relpath);
3526 const struct got_error *
3527 got_worktree_schedule_add(struct got_worktree *worktree,
3528 struct got_pathlist_head *paths,
3529 got_worktree_checkout_cb progress_cb, void *progress_arg,
3530 struct got_repository *repo, int no_ignores)
3532 struct got_fileindex *fileindex = NULL;
3533 char *fileindex_path = NULL;
3534 const struct got_error *err = NULL, *sync_err, *unlockerr;
3535 struct got_pathlist_entry *pe;
3536 struct schedule_addition_args saa;
3538 err = lock_worktree(worktree, LOCK_EX);
3542 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3546 saa.worktree = worktree;
3547 saa.fileindex = fileindex;
3548 saa.progress_cb = progress_cb;
3549 saa.progress_arg = progress_arg;
3552 TAILQ_FOREACH(pe, paths, entry) {
3553 err = worktree_status(worktree, pe->path, fileindex, repo,
3554 schedule_addition, &saa, NULL, NULL, no_ignores, 0);
3558 sync_err = sync_fileindex(fileindex, fileindex_path);
3559 if (sync_err && err == NULL)
3562 free(fileindex_path);
3564 got_fileindex_free(fileindex);
3565 unlockerr = lock_worktree(worktree, LOCK_SH);
3566 if (unlockerr && err == NULL)
3571 struct schedule_deletion_args {
3572 struct got_worktree *worktree;
3573 struct got_fileindex *fileindex;
3574 got_worktree_delete_cb progress_cb;
3576 struct got_repository *repo;
3577 int delete_local_mods;
3581 static const struct got_error *
3582 schedule_for_deletion(void *arg, unsigned char status,
3583 unsigned char staged_status, const char *relpath,
3584 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
3585 struct got_object_id *commit_id, int dirfd, const char *de_name)
3587 struct schedule_deletion_args *a = arg;
3588 const struct got_error *err = NULL;
3589 struct got_fileindex_entry *ie = NULL;
3591 char *ondisk_path, *parent = NULL;
3593 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
3595 return got_error_path(relpath, GOT_ERR_BAD_PATH);
3597 staged_status = get_staged_status(ie);
3598 if (staged_status != GOT_STATUS_NO_CHANGE) {
3599 if (staged_status == GOT_STATUS_DELETE)
3601 return got_error_path(relpath, GOT_ERR_FILE_STAGED);
3604 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
3606 return got_error_from_errno("asprintf");
3608 err = get_file_status(&status, &sb, ie, ondisk_path, dirfd, de_name,
3613 if (status != GOT_STATUS_NO_CHANGE) {
3614 if (status == GOT_STATUS_DELETE)
3616 if (status == GOT_STATUS_MODIFY && !a->delete_local_mods) {
3617 err = got_error_path(relpath, GOT_ERR_FILE_MODIFIED);
3620 if (status != GOT_STATUS_MODIFY &&
3621 status != GOT_STATUS_MISSING) {
3622 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
3627 if (!a->keep_on_disk && status != GOT_STATUS_MISSING) {
3629 if (unlinkat(dirfd, de_name, 0) != 0) {
3630 err = got_error_from_errno2("unlinkat",
3634 } else if (unlink(ondisk_path) != 0) {
3635 err = got_error_from_errno2("unlink", ondisk_path);
3639 parent = dirname(ondisk_path);
3641 if (parent == NULL) {
3642 err = got_error_from_errno2("dirname", ondisk_path);
3645 while (parent && strcmp(parent, a->worktree->root_path) != 0) {
3646 if (rmdir(parent) == -1) {
3647 if (errno != ENOTEMPTY)
3648 err = got_error_from_errno2("rmdir",
3652 parent = dirname(parent);
3653 if (parent == NULL) {
3654 err = got_error_from_errno2("dirname", parent);
3660 got_fileindex_entry_mark_deleted_from_disk(ie);
3665 if (status == GOT_STATUS_DELETE)
3667 return (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
3668 staged_status, relpath);
3671 const struct got_error *
3672 got_worktree_schedule_delete(struct got_worktree *worktree,
3673 struct got_pathlist_head *paths, int delete_local_mods,
3674 got_worktree_delete_cb progress_cb, void *progress_arg,
3675 struct got_repository *repo, int keep_on_disk)
3677 struct got_fileindex *fileindex = NULL;
3678 char *fileindex_path = NULL;
3679 const struct got_error *err = NULL, *sync_err, *unlockerr;
3680 struct got_pathlist_entry *pe;
3681 struct schedule_deletion_args sda;
3683 err = lock_worktree(worktree, LOCK_EX);
3687 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3691 sda.worktree = worktree;
3692 sda.fileindex = fileindex;
3693 sda.progress_cb = progress_cb;
3694 sda.progress_arg = progress_arg;
3696 sda.delete_local_mods = delete_local_mods;
3697 sda.keep_on_disk = keep_on_disk;
3699 TAILQ_FOREACH(pe, paths, entry) {
3700 err = worktree_status(worktree, pe->path, fileindex, repo,
3701 schedule_for_deletion, &sda, NULL, NULL, 0, 1);
3705 sync_err = sync_fileindex(fileindex, fileindex_path);
3706 if (sync_err && err == NULL)
3709 free(fileindex_path);
3711 got_fileindex_free(fileindex);
3712 unlockerr = lock_worktree(worktree, LOCK_SH);
3713 if (unlockerr && err == NULL)
3718 static const struct got_error *
3719 copy_one_line(FILE *infile, FILE *outfile, FILE *rejectfile)
3721 const struct got_error *err = NULL;
3723 size_t linesize = 0, n;
3726 linelen = getline(&line, &linesize, infile);
3727 if (linelen == -1) {
3728 if (ferror(infile)) {
3729 err = got_error_from_errno("getline");
3735 n = fwrite(line, 1, linelen, outfile);
3737 err = got_ferror(outfile, GOT_ERR_IO);
3742 n = fwrite(line, 1, linelen, rejectfile);
3744 err = got_ferror(outfile, GOT_ERR_IO);
3751 static const struct got_error *
3752 skip_one_line(FILE *f)
3755 size_t linesize = 0;
3758 linelen = getline(&line, &linesize, f);
3759 if (linelen == -1) {
3761 return got_error_from_errno("getline");
3768 static const struct got_error *
3769 copy_change(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3770 int start_old, int end_old, int start_new, int end_new,
3771 FILE *outfile, FILE *rejectfile)
3773 const struct got_error *err;
3775 /* Copy old file's lines leading up to patch. */
3776 while (!feof(f1) && *line_cur1 < start_old) {
3777 err = copy_one_line(f1, outfile, NULL);
3782 /* Skip new file's lines leading up to patch. */
3783 while (!feof(f2) && *line_cur2 < start_new) {
3785 err = copy_one_line(f2, NULL, rejectfile);
3787 err = skip_one_line(f2);
3792 /* Copy patched lines. */
3793 while (!feof(f2) && *line_cur2 <= end_new) {
3794 err = copy_one_line(f2, outfile, NULL);
3799 /* Skip over old file's replaced lines. */
3800 while (!feof(f1) && *line_cur1 <= end_old) {
3802 err = copy_one_line(f1, NULL, rejectfile);
3804 err = skip_one_line(f1);
3813 static const struct got_error *
3814 copy_remaining_content(FILE *f1, FILE *f2, int *line_cur1, int *line_cur2,
3815 FILE *outfile, FILE *rejectfile)
3817 const struct got_error *err;
3820 /* Copy old file's lines until EOF. */
3822 err = copy_one_line(f1, outfile, NULL);
3829 /* Copy new file's lines until EOF. */
3831 err = copy_one_line(f2, NULL, rejectfile);
3841 static const struct got_error *
3842 apply_or_reject_change(int *choice, struct got_diff_change *change, int n,
3843 int nchanges, struct got_diff_state *ds, struct got_diff_args *args,
3844 int diff_flags, const char *relpath, FILE *f1, FILE *f2, int *line_cur1,
3845 int *line_cur2, FILE *outfile, FILE *rejectfile,
3846 got_worktree_patch_cb patch_cb, void *patch_arg)
3848 const struct got_error *err = NULL;
3849 int start_old = change->cv.a;
3850 int end_old = change->cv.b;
3851 int start_new = change->cv.c;
3852 int end_new = change->cv.d;
3856 *choice = GOT_PATCH_CHOICE_NONE;
3858 hunkfile = got_opentemp();
3859 if (hunkfile == NULL)
3860 return got_error_from_errno("got_opentemp");
3865 /* XXX TODO needs error checking */
3866 got_diff_dump_change(hunkfile, change, ds, args, f1, f2, diff_flags);
3868 if (fseek(f1, pos1, SEEK_SET) == -1) {
3869 err = got_ferror(f1, GOT_ERR_IO);
3872 if (fseek(f2, pos2, SEEK_SET) == -1) {
3873 err = got_ferror(f1, GOT_ERR_IO);
3876 if (fseek(hunkfile, 0L, SEEK_SET) == -1) {
3877 err = got_ferror(hunkfile, GOT_ERR_IO);
3881 err = (*patch_cb)(choice, patch_arg, GOT_STATUS_MODIFY, relpath,
3882 hunkfile, n, nchanges);
3887 case GOT_PATCH_CHOICE_YES:
3888 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3889 end_old, start_new, end_new, outfile, rejectfile);
3891 case GOT_PATCH_CHOICE_NO:
3892 err = copy_change(f1, f2, line_cur1, line_cur2, start_old,
3893 end_old, start_new, end_new, rejectfile, outfile);
3895 case GOT_PATCH_CHOICE_QUIT:
3898 err = got_error(GOT_ERR_PATCH_CHOICE);
3902 if (hunkfile && fclose(hunkfile) == EOF && err == NULL)
3903 err = got_error_from_errno("fclose");
3907 struct revert_file_args {
3908 struct got_worktree *worktree;
3909 struct got_fileindex *fileindex;
3910 got_worktree_checkout_cb progress_cb;
3912 got_worktree_patch_cb patch_cb;
3914 struct got_repository *repo;
3917 static const struct got_error *
3918 create_patched_content(char **path_outfile, int reverse_patch,
3919 struct got_object_id *blob_id, const char *path2,
3920 int dirfd2, const char *de_name2,
3921 const char *relpath, struct got_repository *repo,
3922 got_worktree_patch_cb patch_cb, void *patch_arg)
3924 const struct got_error *err;
3925 struct got_blob_object *blob = NULL;
3926 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
3928 char *path1 = NULL, *id_str = NULL;
3929 struct stat sb1, sb2;
3930 struct got_diff_changes *changes = NULL;
3931 struct got_diff_state *ds = NULL;
3932 struct got_diff_args *args = NULL;
3933 struct got_diff_change *change;
3934 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, have_content = 0;
3937 *path_outfile = NULL;
3939 err = got_object_id_str(&id_str, blob_id);
3944 fd2 = openat(dirfd2, de_name2, O_RDONLY | O_NOFOLLOW);
3946 err = got_error_from_errno2("openat", path2);
3950 fd2 = open(path2, O_RDONLY | O_NOFOLLOW);
3952 err = got_error_from_errno2("open", path2);
3956 if (fstat(fd2, &sb2) == -1) {
3957 err = got_error_from_errno2("fstat", path2);
3961 f2 = fdopen(fd2, "r");
3963 err = got_error_from_errno2("fdopen", path2);
3968 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
3972 err = got_opentemp_named(&path1, &f1, "got-patched-blob");
3976 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
3980 if (stat(path1, &sb1) == -1) {
3981 err = got_error_from_errno2("stat", path1);
3985 err = got_diff_files(&changes, &ds, &args, &diff_flags,
3986 f1, sb1.st_size, id_str, f2, sb2.st_size, path2, 3, NULL);
3990 err = got_opentemp_named(path_outfile, &outfile, "got-patched-content");
3994 if (fseek(f1, 0L, SEEK_SET) == -1)
3995 return got_ferror(f1, GOT_ERR_IO);
3996 if (fseek(f2, 0L, SEEK_SET) == -1)
3997 return got_ferror(f2, GOT_ERR_IO);
3998 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
4000 err = apply_or_reject_change(&choice, change, ++n,
4001 changes->nchanges, ds, args, diff_flags, relpath,
4002 f1, f2, &line_cur1, &line_cur2,
4003 reverse_patch ? NULL : outfile,
4004 reverse_patch ? outfile : NULL,
4005 patch_cb, patch_arg);
4008 if (choice == GOT_PATCH_CHOICE_YES)
4010 else if (choice == GOT_PATCH_CHOICE_QUIT)
4014 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
4015 reverse_patch ? NULL : outfile,
4016 reverse_patch ? outfile : NULL);
4020 if (chmod(*path_outfile, sb2.st_mode) == -1) {
4021 err = got_error_from_errno2("chmod", path2);
4028 got_object_blob_close(blob);
4029 if (f1 && fclose(f1) == EOF && err == NULL)
4030 err = got_error_from_errno2("fclose", path1);
4031 if (f2 && fclose(f2) == EOF && err == NULL)
4032 err = got_error_from_errno2("fclose", path2);
4033 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
4034 err = got_error_from_errno2("close", path2);
4035 if (outfile && fclose(outfile) == EOF && err == NULL)
4036 err = got_error_from_errno2("fclose", *path_outfile);
4037 if (path1 && unlink(path1) == -1 && err == NULL)
4038 err = got_error_from_errno2("unlink", path1);
4039 if (err || !have_content) {
4040 if (*path_outfile && unlink(*path_outfile) == -1 && err == NULL)
4041 err = got_error_from_errno2("unlink", *path_outfile);
4042 free(*path_outfile);
4043 *path_outfile = NULL;
4047 got_diff_state_free(ds);
4051 got_diff_free_changes(changes);
4056 static const struct got_error *
4057 revert_file(void *arg, unsigned char status, unsigned char staged_status,
4058 const char *relpath, struct got_object_id *blob_id,
4059 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4060 int dirfd, const char *de_name)
4062 struct revert_file_args *a = arg;
4063 const struct got_error *err = NULL;
4064 char *parent_path = NULL;
4065 struct got_fileindex_entry *ie;
4066 struct got_tree_object *tree = NULL;
4067 struct got_object_id *tree_id = NULL;
4068 const struct got_tree_entry *te = NULL;
4069 char *tree_path = NULL, *te_name;
4070 char *ondisk_path = NULL, *path_content = NULL;
4071 struct got_blob_object *blob = NULL;
4073 /* Reverting a staged deletion is a no-op. */
4074 if (status == GOT_STATUS_DELETE &&
4075 staged_status != GOT_STATUS_NO_CHANGE)
4078 if (status == GOT_STATUS_UNVERSIONED)
4079 return (*a->progress_cb)(a->progress_arg,
4080 GOT_STATUS_UNVERSIONED, relpath);
4082 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
4084 return got_error_path(relpath, GOT_ERR_BAD_PATH);
4086 /* Construct in-repository path of tree which contains this blob. */
4087 err = got_path_dirname(&parent_path, ie->path);
4089 if (err->code != GOT_ERR_BAD_PATH)
4091 parent_path = strdup("/");
4092 if (parent_path == NULL) {
4093 err = got_error_from_errno("strdup");
4097 if (got_path_is_root_dir(a->worktree->path_prefix)) {
4098 tree_path = strdup(parent_path);
4099 if (tree_path == NULL) {
4100 err = got_error_from_errno("strdup");
4104 if (got_path_is_root_dir(parent_path)) {
4105 tree_path = strdup(a->worktree->path_prefix);
4106 if (tree_path == NULL) {
4107 err = got_error_from_errno("strdup");
4111 if (asprintf(&tree_path, "%s/%s",
4112 a->worktree->path_prefix, parent_path) == -1) {
4113 err = got_error_from_errno("asprintf");
4119 err = got_object_id_by_path(&tree_id, a->repo,
4120 a->worktree->base_commit_id, tree_path);
4122 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
4123 (status == GOT_STATUS_ADD ||
4124 staged_status == GOT_STATUS_ADD)))
4127 err = got_object_open_as_tree(&tree, a->repo, tree_id);
4131 te_name = basename(ie->path);
4132 if (te_name == NULL) {
4133 err = got_error_from_errno2("basename", ie->path);
4137 te = got_object_tree_find_entry(tree, te_name);
4138 if (te == NULL && status != GOT_STATUS_ADD &&
4139 staged_status != GOT_STATUS_ADD) {
4140 err = got_error(GOT_ERR_NO_TREE_ENTRY);
4146 case GOT_STATUS_ADD:
4148 int choice = GOT_PATCH_CHOICE_NONE;
4149 err = (*a->patch_cb)(&choice, a->patch_arg,
4150 status, ie->path, NULL, 1, 1);
4153 if (choice != GOT_PATCH_CHOICE_YES)
4156 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_REVERT,
4160 got_fileindex_entry_remove(a->fileindex, ie);
4162 case GOT_STATUS_DELETE:
4164 int choice = GOT_PATCH_CHOICE_NONE;
4165 err = (*a->patch_cb)(&choice, a->patch_arg,
4166 status, ie->path, NULL, 1, 1);
4169 if (choice != GOT_PATCH_CHOICE_YES)
4173 case GOT_STATUS_MODIFY:
4174 case GOT_STATUS_MODE_CHANGE:
4175 case GOT_STATUS_CONFLICT:
4176 case GOT_STATUS_MISSING: {
4177 struct got_object_id id;
4178 if (staged_status == GOT_STATUS_ADD ||
4179 staged_status == GOT_STATUS_MODIFY) {
4180 memcpy(id.sha1, ie->staged_blob_sha1,
4181 SHA1_DIGEST_LENGTH);
4183 memcpy(id.sha1, ie->blob_sha1,
4184 SHA1_DIGEST_LENGTH);
4185 err = got_object_open_as_blob(&blob, a->repo, &id, 8192);
4189 if (asprintf(&ondisk_path, "%s/%s",
4190 got_worktree_get_root_path(a->worktree), relpath) == -1) {
4191 err = got_error_from_errno("asprintf");
4195 if (a->patch_cb && (status == GOT_STATUS_MODIFY ||
4196 status == GOT_STATUS_CONFLICT)) {
4197 err = create_patched_content(&path_content, 1, &id,
4198 ondisk_path, dirfd, de_name, ie->path, a->repo,
4199 a->patch_cb, a->patch_arg);
4200 if (err || path_content == NULL)
4202 if (rename(path_content, ondisk_path) == -1) {
4203 err = got_error_from_errno3("rename",
4204 path_content, ondisk_path);
4208 err = install_blob(a->worktree, ondisk_path, ie->path,
4209 te ? te->mode : GOT_DEFAULT_FILE_MODE,
4210 got_fileindex_perms_to_st(ie), blob, 0, 1, 0,
4211 a->repo, a->progress_cb, a->progress_arg);
4214 if (status == GOT_STATUS_DELETE ||
4215 status == GOT_STATUS_MODE_CHANGE) {
4216 err = got_fileindex_entry_update(ie,
4217 ondisk_path, blob->id.sha1,
4218 a->worktree->base_commit_id->sha1, 1);
4234 got_object_blob_close(blob);
4236 got_object_tree_close(tree);
4241 const struct got_error *
4242 got_worktree_revert(struct got_worktree *worktree,
4243 struct got_pathlist_head *paths,
4244 got_worktree_checkout_cb progress_cb, void *progress_arg,
4245 got_worktree_patch_cb patch_cb, void *patch_arg,
4246 struct got_repository *repo)
4248 struct got_fileindex *fileindex = NULL;
4249 char *fileindex_path = NULL;
4250 const struct got_error *err = NULL, *unlockerr = NULL;
4251 const struct got_error *sync_err = NULL;
4252 struct got_pathlist_entry *pe;
4253 struct revert_file_args rfa;
4255 err = lock_worktree(worktree, LOCK_EX);
4259 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4263 rfa.worktree = worktree;
4264 rfa.fileindex = fileindex;
4265 rfa.progress_cb = progress_cb;
4266 rfa.progress_arg = progress_arg;
4267 rfa.patch_cb = patch_cb;
4268 rfa.patch_arg = patch_arg;
4270 TAILQ_FOREACH(pe, paths, entry) {
4271 err = worktree_status(worktree, pe->path, fileindex, repo,
4272 revert_file, &rfa, NULL, NULL, 0, 0);
4276 sync_err = sync_fileindex(fileindex, fileindex_path);
4277 if (sync_err && err == NULL)
4280 free(fileindex_path);
4282 got_fileindex_free(fileindex);
4283 unlockerr = lock_worktree(worktree, LOCK_SH);
4284 if (unlockerr && err == NULL)
4290 free_commitable(struct got_commitable *ct)
4293 free(ct->in_repo_path);
4294 free(ct->ondisk_path);
4296 free(ct->base_blob_id);
4297 free(ct->staged_blob_id);
4298 free(ct->base_commit_id);
4302 struct collect_commitables_arg {
4303 struct got_pathlist_head *commitable_paths;
4304 struct got_repository *repo;
4305 struct got_worktree *worktree;
4306 int have_staged_files;
4309 static const struct got_error *
4310 collect_commitables(void *arg, unsigned char status,
4311 unsigned char staged_status, const char *relpath,
4312 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4313 struct got_object_id *commit_id, int dirfd, const char *de_name)
4315 struct collect_commitables_arg *a = arg;
4316 const struct got_error *err = NULL;
4317 struct got_commitable *ct = NULL;
4318 struct got_pathlist_entry *new = NULL;
4319 char *parent_path = NULL, *path = NULL;
4322 if (a->have_staged_files) {
4323 if (staged_status != GOT_STATUS_MODIFY &&
4324 staged_status != GOT_STATUS_ADD &&
4325 staged_status != GOT_STATUS_DELETE)
4328 if (status == GOT_STATUS_CONFLICT)
4329 return got_error(GOT_ERR_COMMIT_CONFLICT);
4331 if (status != GOT_STATUS_MODIFY &&
4332 status != GOT_STATUS_MODE_CHANGE &&
4333 status != GOT_STATUS_ADD &&
4334 status != GOT_STATUS_DELETE)
4338 if (asprintf(&path, "/%s", relpath) == -1) {
4339 err = got_error_from_errno("asprintf");
4342 if (strcmp(path, "/") == 0) {
4343 parent_path = strdup("");
4344 if (parent_path == NULL)
4345 return got_error_from_errno("strdup");
4347 err = got_path_dirname(&parent_path, path);
4352 ct = calloc(1, sizeof(*ct));
4354 err = got_error_from_errno("calloc");
4358 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
4360 err = got_error_from_errno("asprintf");
4363 if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
4364 sb.st_mode = GOT_DEFAULT_FILE_MODE;
4367 if (fstatat(dirfd, de_name, &sb,
4368 AT_SYMLINK_NOFOLLOW) == -1) {
4369 err = got_error_from_errno2("fstatat",
4373 } else if (lstat(ct->ondisk_path, &sb) == -1) {
4374 err = got_error_from_errno2("lstat", ct->ondisk_path);
4377 ct->mode = sb.st_mode;
4380 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
4381 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
4383 err = got_error_from_errno("asprintf");
4387 ct->status = status;
4388 ct->staged_status = staged_status;
4389 ct->blob_id = NULL; /* will be filled in when blob gets created */
4390 if (ct->status != GOT_STATUS_ADD &&
4391 ct->staged_status != GOT_STATUS_ADD) {
4392 ct->base_blob_id = got_object_id_dup(blob_id);
4393 if (ct->base_blob_id == NULL) {
4394 err = got_error_from_errno("got_object_id_dup");
4397 ct->base_commit_id = got_object_id_dup(commit_id);
4398 if (ct->base_commit_id == NULL) {
4399 err = got_error_from_errno("got_object_id_dup");
4403 if (ct->staged_status == GOT_STATUS_ADD ||
4404 ct->staged_status == GOT_STATUS_MODIFY) {
4405 ct->staged_blob_id = got_object_id_dup(staged_blob_id);
4406 if (ct->staged_blob_id == NULL) {
4407 err = got_error_from_errno("got_object_id_dup");
4411 ct->path = strdup(path);
4412 if (ct->path == NULL) {
4413 err = got_error_from_errno("strdup");
4416 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
4418 if (ct && (err || new == NULL))
4419 free_commitable(ct);
4425 static const struct got_error *write_tree(struct got_object_id **, int *,
4426 struct got_tree_object *, const char *, struct got_pathlist_head *,
4427 got_worktree_status_cb status_cb, void *status_arg,
4428 struct got_repository *);
4430 static const struct got_error *
4431 write_subtree(struct got_object_id **new_subtree_id, int *nentries,
4432 struct got_tree_entry *te, const char *parent_path,
4433 struct got_pathlist_head *commitable_paths,
4434 got_worktree_status_cb status_cb, void *status_arg,
4435 struct got_repository *repo)
4437 const struct got_error *err = NULL;
4438 struct got_tree_object *subtree;
4441 if (asprintf(&subpath, "%s%s%s", parent_path,
4442 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
4443 return got_error_from_errno("asprintf");
4445 err = got_object_open_as_tree(&subtree, repo, &te->id);
4449 err = write_tree(new_subtree_id, nentries, subtree, subpath,
4450 commitable_paths, status_cb, status_arg, repo);
4451 got_object_tree_close(subtree);
4456 static const struct got_error *
4457 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
4459 const struct got_error *err = NULL;
4460 char *ct_parent_path = NULL;
4464 if (strchr(ct->in_repo_path, '/') == NULL) {
4465 *match = got_path_is_root_dir(path);
4469 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
4472 *match = (strcmp(path, ct_parent_path) == 0);
4473 free(ct_parent_path);
4478 get_ct_file_mode(struct got_commitable *ct)
4480 if (S_ISLNK(ct->mode))
4483 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
4486 static const struct got_error *
4487 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
4488 struct got_tree_entry *te, struct got_commitable *ct)
4490 const struct got_error *err = NULL;
4494 err = got_object_tree_entry_dup(new_te, te);
4498 (*new_te)->mode = get_ct_file_mode(ct);
4500 if (ct->staged_status == GOT_STATUS_MODIFY)
4501 memcpy(&(*new_te)->id, ct->staged_blob_id,
4502 sizeof((*new_te)->id));
4504 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4506 if (err && *new_te) {
4513 static const struct got_error *
4514 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
4515 struct got_commitable *ct)
4517 const struct got_error *err = NULL;
4522 *new_te = calloc(1, sizeof(**new_te));
4523 if (*new_te == NULL)
4524 return got_error_from_errno("calloc");
4526 ct_name = basename(ct->path);
4527 if (ct_name == NULL) {
4528 err = got_error_from_errno2("basename", ct->path);
4531 if (strlcpy((*new_te)->name, ct_name, sizeof((*new_te)->name)) >=
4532 sizeof((*new_te)->name)) {
4533 err = got_error(GOT_ERR_NO_SPACE);
4537 (*new_te)->mode = get_ct_file_mode(ct);
4539 if (ct->staged_status == GOT_STATUS_ADD)
4540 memcpy(&(*new_te)->id, ct->staged_blob_id,
4541 sizeof((*new_te)->id));
4543 memcpy(&(*new_te)->id, ct->blob_id, sizeof((*new_te)->id));
4545 if (err && *new_te) {
4552 static const struct got_error *
4553 insert_tree_entry(struct got_tree_entry *new_te,
4554 struct got_pathlist_head *paths)
4556 const struct got_error *err = NULL;
4557 struct got_pathlist_entry *new_pe;
4559 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
4563 return got_error(GOT_ERR_TREE_DUP_ENTRY);
4567 static const struct got_error *
4568 report_ct_status(struct got_commitable *ct,
4569 got_worktree_status_cb status_cb, void *status_arg)
4571 const char *ct_path = ct->path;
4572 unsigned char status;
4574 while (ct_path[0] == '/')
4577 if (ct->staged_status != GOT_STATUS_NO_CHANGE)
4578 status = ct->staged_status;
4580 status = ct->status;
4582 return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
4583 ct_path, ct->blob_id, NULL, NULL, -1, NULL);
4586 static const struct got_error *
4587 match_modified_subtree(int *modified, struct got_tree_entry *te,
4588 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
4590 const struct got_error *err = NULL;
4591 struct got_pathlist_entry *pe;
4596 if (asprintf(&te_path, "%s%s%s", base_tree_path,
4597 got_path_is_root_dir(base_tree_path) ? "" : "/",
4599 return got_error_from_errno("asprintf");
4601 TAILQ_FOREACH(pe, commitable_paths, entry) {
4602 struct got_commitable *ct = pe->data;
4603 *modified = got_path_is_child(ct->in_repo_path, te_path,
4613 static const struct got_error *
4614 match_deleted_or_modified_ct(struct got_commitable **ctp,
4615 struct got_tree_entry *te, const char *base_tree_path,
4616 struct got_pathlist_head *commitable_paths)
4618 const struct got_error *err = NULL;
4619 struct got_pathlist_entry *pe;
4623 TAILQ_FOREACH(pe, commitable_paths, entry) {
4624 struct got_commitable *ct = pe->data;
4625 char *ct_name = NULL;
4628 if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
4629 if (ct->status != GOT_STATUS_MODIFY &&
4630 ct->status != GOT_STATUS_MODE_CHANGE &&
4631 ct->status != GOT_STATUS_DELETE)
4634 if (ct->staged_status != GOT_STATUS_MODIFY &&
4635 ct->staged_status != GOT_STATUS_DELETE)
4639 if (got_object_id_cmp(ct->base_blob_id, &te->id) != 0)
4642 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
4648 ct_name = basename(pe->path);
4649 if (ct_name == NULL)
4650 return got_error_from_errno2("basename", pe->path);
4652 if (strcmp(te->name, ct_name) != 0)
4662 static const struct got_error *
4663 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
4664 const char *child_path, const char *path_base_tree,
4665 struct got_pathlist_head *commitable_paths,
4666 got_worktree_status_cb status_cb, void *status_arg,
4667 struct got_repository *repo)
4669 const struct got_error *err = NULL;
4670 struct got_tree_entry *new_te;
4672 struct got_object_id *id = NULL;
4677 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
4678 got_path_is_root_dir(path_base_tree) ? "" : "/",
4680 return got_error_from_errno("asprintf");
4682 new_te = calloc(1, sizeof(*new_te));
4684 return got_error_from_errno("calloc");
4685 new_te->mode = S_IFDIR;
4687 if (strlcpy(new_te->name, child_path, sizeof(new_te->name)) >=
4688 sizeof(new_te->name)) {
4689 err = got_error(GOT_ERR_NO_SPACE);
4692 err = write_tree(&id, &nentries, NULL, subtree_path,
4693 commitable_paths, status_cb, status_arg, repo);
4698 memcpy(&new_te->id, id, sizeof(new_te->id));
4707 static const struct got_error *
4708 write_tree(struct got_object_id **new_tree_id, int *nentries,
4709 struct got_tree_object *base_tree, const char *path_base_tree,
4710 struct got_pathlist_head *commitable_paths,
4711 got_worktree_status_cb status_cb, void *status_arg,
4712 struct got_repository *repo)
4714 const struct got_error *err = NULL;
4715 struct got_pathlist_head paths;
4716 struct got_tree_entry *te, *new_te = NULL;
4717 struct got_pathlist_entry *pe;
4722 /* Insert, and recurse into, newly added entries first. */
4723 TAILQ_FOREACH(pe, commitable_paths, entry) {
4724 struct got_commitable *ct = pe->data;
4725 char *child_path = NULL, *slash;
4727 if ((ct->status != GOT_STATUS_ADD &&
4728 ct->staged_status != GOT_STATUS_ADD) ||
4729 (ct->flags & GOT_COMMITABLE_ADDED))
4732 if (!got_path_is_child(pe->path, path_base_tree,
4733 strlen(path_base_tree)))
4736 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
4741 slash = strchr(child_path, '/');
4742 if (slash == NULL) {
4743 err = alloc_added_blob_tree_entry(&new_te, ct);
4746 err = report_ct_status(ct, status_cb, status_arg);
4749 ct->flags |= GOT_COMMITABLE_ADDED;
4750 err = insert_tree_entry(new_te, &paths);
4755 *slash = '\0'; /* trim trailing path components */
4756 if (base_tree == NULL ||
4757 got_object_tree_find_entry(base_tree, child_path)
4759 err = make_subtree_for_added_blob(&new_te,
4760 child_path, path_base_tree,
4761 commitable_paths, status_cb, status_arg,
4765 err = insert_tree_entry(new_te, &paths);
4774 int i, nbase_entries;
4775 /* Handle modified and deleted entries. */
4776 nbase_entries = got_object_tree_get_nentries(base_tree);
4777 for (i = 0; i < nbase_entries; i++) {
4778 struct got_commitable *ct = NULL;
4780 te = got_object_tree_get_entry(base_tree, i);
4781 if (got_object_tree_entry_is_submodule(te)) {
4782 /* Entry is a submodule; just copy it. */
4783 err = got_object_tree_entry_dup(&new_te, te);
4786 err = insert_tree_entry(new_te, &paths);
4793 if (S_ISDIR(te->mode)) {
4795 err = got_object_tree_entry_dup(&new_te, te);
4798 err = match_modified_subtree(&modified, te,
4799 path_base_tree, commitable_paths);
4802 /* Avoid recursion into unmodified subtrees. */
4804 struct got_object_id *new_id;
4806 err = write_subtree(&new_id,
4808 path_base_tree, commitable_paths,
4809 status_cb, status_arg, repo);
4812 if (nsubentries == 0) {
4813 /* All entries were deleted. */
4817 memcpy(&new_te->id, new_id,
4818 sizeof(new_te->id));
4821 err = insert_tree_entry(new_te, &paths);
4828 err = match_deleted_or_modified_ct(&ct, te,
4829 path_base_tree, commitable_paths);
4833 /* NB: Deleted entries get dropped here. */
4834 if (ct->status == GOT_STATUS_MODIFY ||
4835 ct->status == GOT_STATUS_MODE_CHANGE ||
4836 ct->staged_status == GOT_STATUS_MODIFY) {
4837 err = alloc_modified_blob_tree_entry(
4841 err = insert_tree_entry(new_te, &paths);
4846 err = report_ct_status(ct, status_cb,
4851 /* Entry is unchanged; just copy it. */
4852 err = got_object_tree_entry_dup(&new_te, te);
4855 err = insert_tree_entry(new_te, &paths);
4863 /* Write new list of entries; deleted entries have been dropped. */
4864 err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
4866 got_pathlist_free(&paths);
4870 static const struct got_error *
4871 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
4872 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
4873 int have_staged_files)
4875 const struct got_error *err = NULL;
4876 struct got_pathlist_entry *pe;
4878 TAILQ_FOREACH(pe, commitable_paths, entry) {
4879 struct got_fileindex_entry *ie;
4880 struct got_commitable *ct = pe->data;
4882 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
4884 if (ct->status == GOT_STATUS_DELETE ||
4885 ct->staged_status == GOT_STATUS_DELETE) {
4886 got_fileindex_entry_remove(fileindex, ie);
4887 } else if (ct->staged_status == GOT_STATUS_ADD ||
4888 ct->staged_status == GOT_STATUS_MODIFY) {
4889 got_fileindex_entry_stage_set(ie,
4890 GOT_FILEIDX_STAGE_NONE);
4891 err = got_fileindex_entry_update(ie,
4892 ct->ondisk_path, ct->staged_blob_id->sha1,
4893 new_base_commit_id->sha1,
4894 !have_staged_files);
4896 err = got_fileindex_entry_update(ie,
4897 ct->ondisk_path, ct->blob_id->sha1,
4898 new_base_commit_id->sha1,
4899 !have_staged_files);
4901 err = got_fileindex_entry_alloc(&ie, pe->path);
4904 err = got_fileindex_entry_update(ie, ct->ondisk_path,
4905 ct->blob_id->sha1, new_base_commit_id->sha1, 1);
4907 got_fileindex_entry_free(ie);
4910 err = got_fileindex_entry_add(fileindex, ie);
4912 got_fileindex_entry_free(ie);
4921 static const struct got_error *
4922 check_out_of_date(const char *in_repo_path, unsigned char status,
4923 unsigned char staged_status, struct got_object_id *base_blob_id,
4924 struct got_object_id *base_commit_id,
4925 struct got_object_id *head_commit_id, struct got_repository *repo,
4928 const struct got_error *err = NULL;
4929 struct got_object_id *id = NULL;
4931 if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
4932 /* Trivial case: base commit == head commit */
4933 if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
4936 * Ensure file content which local changes were based
4937 * on matches file content in the branch head.
4939 err = got_object_id_by_path(&id, repo, head_commit_id,
4942 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4943 err = got_error(ood_errcode);
4945 } else if (got_object_id_cmp(id, base_blob_id) != 0)
4946 err = got_error(ood_errcode);
4948 /* Require that added files don't exist in the branch head. */
4949 err = got_object_id_by_path(&id, repo, head_commit_id,
4951 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
4953 err = id ? got_error(ood_errcode) : NULL;
4960 const struct got_error *
4961 commit_worktree(struct got_object_id **new_commit_id,
4962 struct got_pathlist_head *commitable_paths,
4963 struct got_object_id *head_commit_id, struct got_worktree *worktree,
4964 const char *author, const char *committer,
4965 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
4966 got_worktree_status_cb status_cb, void *status_arg,
4967 struct got_repository *repo)
4969 const struct got_error *err = NULL, *unlockerr = NULL;
4970 struct got_pathlist_entry *pe;
4971 const char *head_ref_name = NULL;
4972 struct got_commit_object *head_commit = NULL;
4973 struct got_reference *head_ref2 = NULL;
4974 struct got_object_id *head_commit_id2 = NULL;
4975 struct got_tree_object *head_tree = NULL;
4976 struct got_object_id *new_tree_id = NULL;
4978 struct got_object_id_queue parent_ids;
4979 struct got_object_qid *pid = NULL;
4980 char *logmsg = NULL;
4982 *new_commit_id = NULL;
4984 SIMPLEQ_INIT(&parent_ids);
4986 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
4990 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
4994 if (commit_msg_cb != NULL) {
4995 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
5000 if (logmsg == NULL || strlen(logmsg) == 0) {
5001 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
5005 /* Create blobs from added and modified files and record their IDs. */
5006 TAILQ_FOREACH(pe, commitable_paths, entry) {
5007 struct got_commitable *ct = pe->data;
5010 /* Blobs for staged files already exist. */
5011 if (ct->staged_status == GOT_STATUS_ADD ||
5012 ct->staged_status == GOT_STATUS_MODIFY)
5015 if (ct->status != GOT_STATUS_ADD &&
5016 ct->status != GOT_STATUS_MODIFY &&
5017 ct->status != GOT_STATUS_MODE_CHANGE)
5020 if (asprintf(&ondisk_path, "%s/%s",
5021 worktree->root_path, pe->path) == -1) {
5022 err = got_error_from_errno("asprintf");
5025 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
5032 * When comitting a symlink we convert "bad" symlinks (those
5033 * which point outside the work tree or into .got) to regular
5034 * files. This way, the post-commit work tree state matches
5035 * a fresh checkout of the tree which was committed.
5037 if (S_ISLNK(get_ct_file_mode(ct))) {
5038 struct got_blob_object *blob;
5039 err = got_object_open_as_blob(&blob, repo, ct->blob_id,
5045 err = install_symlink(worktree, ondisk_path, ct->path,
5046 get_ct_file_mode(ct), GOT_DEFAULT_FILE_MODE, blob,
5047 0, 0, repo, NULL, NULL);
5048 got_object_blob_close(blob);
5057 /* Recursively write new tree objects. */
5058 err = write_tree(&new_tree_id, &nentries, head_tree, "/",
5059 commitable_paths, status_cb, status_arg, repo);
5063 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
5066 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
5067 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
5068 1, author, time(NULL), committer, time(NULL), logmsg, repo);
5069 got_object_qid_free(pid);
5075 /* Check if a concurrent commit to our branch has occurred. */
5076 head_ref_name = got_worktree_get_head_ref_name(worktree);
5077 if (head_ref_name == NULL) {
5078 err = got_error_from_errno("got_worktree_get_head_ref_name");
5081 /* Lock the reference here to prevent concurrent modification. */
5082 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
5085 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
5088 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
5089 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
5092 /* Update branch head in repository. */
5093 err = got_ref_change_ref(head_ref2, *new_commit_id);
5096 err = got_ref_write(head_ref2, repo);
5100 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
5104 err = ref_base_commit(worktree, repo);
5109 got_object_tree_close(head_tree);
5111 got_object_commit_close(head_commit);
5112 free(head_commit_id2);
5114 unlockerr = got_ref_unlock(head_ref2);
5115 if (unlockerr && err == NULL)
5117 got_ref_close(head_ref2);
5122 static const struct got_error *
5123 check_path_is_commitable(const char *path,
5124 struct got_pathlist_head *commitable_paths)
5126 struct got_pathlist_entry *cpe = NULL;
5127 size_t path_len = strlen(path);
5129 TAILQ_FOREACH(cpe, commitable_paths, entry) {
5130 struct got_commitable *ct = cpe->data;
5131 const char *ct_path = ct->path;
5133 while (ct_path[0] == '/')
5136 if (strcmp(path, ct_path) == 0 ||
5137 got_path_is_child(ct_path, path, path_len))
5142 return got_error_path(path, GOT_ERR_BAD_PATH);
5147 static const struct got_error *
5148 check_staged_file(void *arg, struct got_fileindex_entry *ie)
5150 int *have_staged_files = arg;
5152 if (got_fileindex_entry_stage_get(ie) != GOT_FILEIDX_STAGE_NONE) {
5153 *have_staged_files = 1;
5154 return got_error(GOT_ERR_CANCELLED);
5160 static const struct got_error *
5161 check_non_staged_files(struct got_fileindex *fileindex,
5162 struct got_pathlist_head *paths)
5164 struct got_pathlist_entry *pe;
5165 struct got_fileindex_entry *ie;
5167 TAILQ_FOREACH(pe, paths, entry) {
5168 if (pe->path[0] == '\0')
5170 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
5172 return got_error_path(pe->path, GOT_ERR_BAD_PATH);
5173 if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
5174 return got_error_path(pe->path,
5175 GOT_ERR_FILE_NOT_STAGED);
5181 const struct got_error *
5182 got_worktree_commit(struct got_object_id **new_commit_id,
5183 struct got_worktree *worktree, struct got_pathlist_head *paths,
5184 const char *author, const char *committer,
5185 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
5186 got_worktree_status_cb status_cb, void *status_arg,
5187 struct got_repository *repo)
5189 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
5190 struct got_fileindex *fileindex = NULL;
5191 char *fileindex_path = NULL;
5192 struct got_pathlist_head commitable_paths;
5193 struct collect_commitables_arg cc_arg;
5194 struct got_pathlist_entry *pe;
5195 struct got_reference *head_ref = NULL;
5196 struct got_object_id *head_commit_id = NULL;
5197 int have_staged_files = 0;
5199 *new_commit_id = NULL;
5201 TAILQ_INIT(&commitable_paths);
5203 err = lock_worktree(worktree, LOCK_EX);
5207 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5211 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5215 err = open_fileindex(&fileindex, &fileindex_path, worktree);
5219 err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
5220 &have_staged_files);
5221 if (err && err->code != GOT_ERR_CANCELLED)
5223 if (have_staged_files) {
5224 err = check_non_staged_files(fileindex, paths);
5229 cc_arg.commitable_paths = &commitable_paths;
5230 cc_arg.worktree = worktree;
5232 cc_arg.have_staged_files = have_staged_files;
5233 TAILQ_FOREACH(pe, paths, entry) {
5234 err = worktree_status(worktree, pe->path, fileindex, repo,
5235 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5240 if (TAILQ_EMPTY(&commitable_paths)) {
5241 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5245 TAILQ_FOREACH(pe, paths, entry) {
5246 err = check_path_is_commitable(pe->path, &commitable_paths);
5251 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5252 struct got_commitable *ct = pe->data;
5253 const char *ct_path = ct->in_repo_path;
5255 while (ct_path[0] == '/')
5257 err = check_out_of_date(ct_path, ct->status,
5258 ct->staged_status, ct->base_blob_id, ct->base_commit_id,
5259 head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
5265 err = commit_worktree(new_commit_id, &commitable_paths,
5266 head_commit_id, worktree, author, committer,
5267 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
5271 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5272 fileindex, have_staged_files);
5273 sync_err = sync_fileindex(fileindex, fileindex_path);
5274 if (sync_err && err == NULL)
5278 got_fileindex_free(fileindex);
5279 free(fileindex_path);
5280 unlockerr = lock_worktree(worktree, LOCK_SH);
5281 if (unlockerr && err == NULL)
5283 TAILQ_FOREACH(pe, &commitable_paths, entry) {
5284 struct got_commitable *ct = pe->data;
5285 free_commitable(ct);
5287 got_pathlist_free(&commitable_paths);
5292 got_commitable_get_path(struct got_commitable *ct)
5298 got_commitable_get_status(struct got_commitable *ct)
5303 struct check_rebase_ok_arg {
5304 struct got_worktree *worktree;
5305 struct got_repository *repo;
5308 static const struct got_error *
5309 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
5311 const struct got_error *err = NULL;
5312 struct check_rebase_ok_arg *a = arg;
5313 unsigned char status;
5317 /* Reject rebase of a work tree with mixed base commits. */
5318 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
5319 SHA1_DIGEST_LENGTH))
5320 return got_error(GOT_ERR_MIXED_COMMITS);
5322 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
5324 return got_error_from_errno("asprintf");
5326 /* Reject rebase of a work tree with modified or staged files. */
5327 err = get_file_status(&status, &sb, ie, ondisk_path, -1, NULL, a->repo);
5332 if (status != GOT_STATUS_NO_CHANGE)
5333 return got_error(GOT_ERR_MODIFIED);
5334 if (get_staged_status(ie) != GOT_STATUS_NO_CHANGE)
5335 return got_error_path(ie->path, GOT_ERR_FILE_STAGED);
5340 const struct got_error *
5341 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
5342 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
5343 struct got_worktree *worktree, struct got_reference *branch,
5344 struct got_repository *repo)
5346 const struct got_error *err = NULL;
5347 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5348 char *branch_ref_name = NULL;
5349 char *fileindex_path = NULL;
5350 struct check_rebase_ok_arg ok_arg;
5351 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
5352 struct got_object_id *wt_branch_tip = NULL;
5354 *new_base_branch_ref = NULL;
5358 err = lock_worktree(worktree, LOCK_EX);
5362 err = open_fileindex(fileindex, &fileindex_path, worktree);
5366 ok_arg.worktree = worktree;
5368 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
5373 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5377 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5381 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5385 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
5390 err = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
5393 if (got_object_id_cmp(worktree->base_commit_id, wt_branch_tip) != 0) {
5394 err = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
5398 err = got_ref_alloc_symref(new_base_branch_ref,
5399 new_base_branch_ref_name, wt_branch);
5402 err = got_ref_write(*new_base_branch_ref, repo);
5406 /* TODO Lock original branch's ref while rebasing? */
5408 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
5412 err = got_ref_write(branch_ref, repo);
5416 err = got_ref_alloc(tmp_branch, tmp_branch_name,
5417 worktree->base_commit_id);
5420 err = got_ref_write(*tmp_branch, repo);
5424 err = got_worktree_set_head_ref(worktree, *tmp_branch);
5428 free(fileindex_path);
5429 free(tmp_branch_name);
5430 free(new_base_branch_ref_name);
5431 free(branch_ref_name);
5433 got_ref_close(branch_ref);
5435 got_ref_close(wt_branch);
5436 free(wt_branch_tip);
5438 if (*new_base_branch_ref) {
5439 got_ref_close(*new_base_branch_ref);
5440 *new_base_branch_ref = NULL;
5443 got_ref_close(*tmp_branch);
5447 got_fileindex_free(*fileindex);
5450 lock_worktree(worktree, LOCK_SH);
5455 const struct got_error *
5456 got_worktree_rebase_continue(struct got_object_id **commit_id,
5457 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
5458 struct got_reference **branch, struct got_fileindex **fileindex,
5459 struct got_worktree *worktree, struct got_repository *repo)
5461 const struct got_error *err;
5462 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
5463 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
5464 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
5465 char *fileindex_path = NULL;
5466 int have_staged_files = 0;
5469 *new_base_branch = NULL;
5474 err = lock_worktree(worktree, LOCK_EX);
5478 err = open_fileindex(fileindex, &fileindex_path, worktree);
5482 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
5483 &have_staged_files);
5484 if (err && err->code != GOT_ERR_CANCELLED)
5486 if (have_staged_files) {
5487 err = got_error(GOT_ERR_STAGED_PATHS);
5491 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5495 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5499 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5503 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5507 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
5511 err = got_ref_open(branch, repo,
5512 got_ref_get_symref_target(branch_ref), 0);
5516 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5520 err = got_ref_resolve(commit_id, repo, commit_ref);
5524 err = got_ref_open(new_base_branch, repo,
5525 new_base_branch_ref_name, 0);
5529 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
5533 free(commit_ref_name);
5534 free(branch_ref_name);
5535 free(fileindex_path);
5537 got_ref_close(commit_ref);
5539 got_ref_close(branch_ref);
5544 got_ref_close(*tmp_branch);
5547 if (*new_base_branch) {
5548 got_ref_close(*new_base_branch);
5549 *new_base_branch = NULL;
5552 got_ref_close(*branch);
5556 got_fileindex_free(*fileindex);
5559 lock_worktree(worktree, LOCK_SH);
5564 const struct got_error *
5565 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
5567 const struct got_error *err;
5568 char *tmp_branch_name = NULL;
5570 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5574 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
5575 free(tmp_branch_name);
5579 static const struct got_error *
5580 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
5581 char **logmsg, void *arg)
5587 static const struct got_error *
5588 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
5589 const char *path, struct got_object_id *blob_id,
5590 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5591 int dirfd, const char *de_name)
5596 struct collect_merged_paths_arg {
5597 got_worktree_checkout_cb progress_cb;
5599 struct got_pathlist_head *merged_paths;
5602 static const struct got_error *
5603 collect_merged_paths(void *arg, unsigned char status, const char *path)
5605 const struct got_error *err;
5606 struct collect_merged_paths_arg *a = arg;
5608 struct got_pathlist_entry *new;
5610 err = (*a->progress_cb)(a->progress_arg, status, path);
5614 if (status != GOT_STATUS_MERGE &&
5615 status != GOT_STATUS_ADD &&
5616 status != GOT_STATUS_DELETE &&
5617 status != GOT_STATUS_CONFLICT)
5622 return got_error_from_errno("strdup");
5624 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
5625 if (err || new == NULL)
5631 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
5633 struct got_pathlist_entry *pe;
5635 TAILQ_FOREACH(pe, merged_paths, entry)
5636 free((char *)pe->path);
5638 got_pathlist_free(merged_paths);
5641 static const struct got_error *
5642 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
5643 int is_rebase, struct got_repository *repo)
5645 const struct got_error *err;
5646 struct got_reference *commit_ref = NULL;
5648 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5650 if (err->code != GOT_ERR_NOT_REF)
5652 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
5655 err = got_ref_write(commit_ref, repo);
5658 } else if (is_rebase) {
5659 struct got_object_id *stored_id;
5662 err = got_ref_resolve(&stored_id, repo, commit_ref);
5665 cmp = got_object_id_cmp(commit_id, stored_id);
5668 err = got_error(GOT_ERR_REBASE_COMMITID);
5674 got_ref_close(commit_ref);
5678 static const struct got_error *
5679 rebase_merge_files(struct got_pathlist_head *merged_paths,
5680 const char *commit_ref_name, struct got_worktree *worktree,
5681 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
5682 struct got_object_id *commit_id, struct got_repository *repo,
5683 got_worktree_checkout_cb progress_cb, void *progress_arg,
5684 got_cancel_cb cancel_cb, void *cancel_arg)
5686 const struct got_error *err;
5687 struct got_reference *commit_ref = NULL;
5688 struct collect_merged_paths_arg cmp_arg;
5689 char *fileindex_path;
5691 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5693 err = get_fileindex_path(&fileindex_path, worktree);
5697 cmp_arg.progress_cb = progress_cb;
5698 cmp_arg.progress_arg = progress_arg;
5699 cmp_arg.merged_paths = merged_paths;
5700 err = merge_files(worktree, fileindex, fileindex_path,
5701 parent_commit_id, commit_id, repo, collect_merged_paths,
5702 &cmp_arg, cancel_cb, cancel_arg);
5704 got_ref_close(commit_ref);
5708 const struct got_error *
5709 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
5710 struct got_worktree *worktree, struct got_fileindex *fileindex,
5711 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5712 struct got_repository *repo,
5713 got_worktree_checkout_cb progress_cb, void *progress_arg,
5714 got_cancel_cb cancel_cb, void *cancel_arg)
5716 const struct got_error *err;
5717 char *commit_ref_name;
5719 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5723 err = store_commit_id(commit_ref_name, commit_id, 1, repo);
5727 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5728 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5729 progress_arg, cancel_cb, cancel_arg);
5731 free(commit_ref_name);
5735 const struct got_error *
5736 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
5737 struct got_worktree *worktree, struct got_fileindex *fileindex,
5738 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
5739 struct got_repository *repo,
5740 got_worktree_checkout_cb progress_cb, void *progress_arg,
5741 got_cancel_cb cancel_cb, void *cancel_arg)
5743 const struct got_error *err;
5744 char *commit_ref_name;
5746 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5750 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
5754 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
5755 fileindex, parent_commit_id, commit_id, repo, progress_cb,
5756 progress_arg, cancel_cb, cancel_arg);
5758 free(commit_ref_name);
5762 static const struct got_error *
5763 rebase_commit(struct got_object_id **new_commit_id,
5764 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
5765 struct got_worktree *worktree, struct got_fileindex *fileindex,
5766 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
5767 const char *new_logmsg, struct got_repository *repo)
5769 const struct got_error *err, *sync_err;
5770 struct got_pathlist_head commitable_paths;
5771 struct collect_commitables_arg cc_arg;
5772 char *fileindex_path = NULL;
5773 struct got_reference *head_ref = NULL;
5774 struct got_object_id *head_commit_id = NULL;
5775 char *logmsg = NULL;
5777 TAILQ_INIT(&commitable_paths);
5778 *new_commit_id = NULL;
5780 /* Work tree is locked/unlocked during rebase preparation/teardown. */
5782 err = get_fileindex_path(&fileindex_path, worktree);
5786 cc_arg.commitable_paths = &commitable_paths;
5787 cc_arg.worktree = worktree;
5789 cc_arg.have_staged_files = 0;
5791 * If possible get the status of individual files directly to
5792 * avoid crawling the entire work tree once per rebased commit.
5793 * TODO: Ideally, merged_paths would contain a list of commitables
5794 * we could use so we could skip worktree_status() entirely.
5797 struct got_pathlist_entry *pe;
5798 TAILQ_FOREACH(pe, merged_paths, entry) {
5799 err = worktree_status(worktree, pe->path, fileindex,
5800 repo, collect_commitables, &cc_arg, NULL, NULL, 0,
5806 err = worktree_status(worktree, "", fileindex, repo,
5807 collect_commitables, &cc_arg, NULL, NULL, 0, 0);
5812 if (TAILQ_EMPTY(&commitable_paths)) {
5813 /* No-op change; commit will be elided. */
5814 err = got_ref_delete(commit_ref, repo);
5817 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
5821 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
5825 err = got_ref_resolve(&head_commit_id, repo, head_ref);
5830 logmsg = strdup(new_logmsg);
5831 if (logmsg == NULL) {
5832 err = got_error_from_errno("strdup");
5836 err = got_object_commit_get_logmsg(&logmsg, orig_commit);
5841 /* NB: commit_worktree will call free(logmsg) */
5842 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
5843 worktree, got_object_commit_get_author(orig_commit),
5844 got_object_commit_get_committer(orig_commit),
5845 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
5849 err = got_ref_change_ref(tmp_branch, *new_commit_id);
5853 err = got_ref_delete(commit_ref, repo);
5857 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
5859 sync_err = sync_fileindex(fileindex, fileindex_path);
5860 if (sync_err && err == NULL)
5863 free(fileindex_path);
5864 free(head_commit_id);
5866 got_ref_close(head_ref);
5868 free(*new_commit_id);
5869 *new_commit_id = NULL;
5874 const struct got_error *
5875 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
5876 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5877 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5878 struct got_commit_object *orig_commit,
5879 struct got_object_id *orig_commit_id, struct got_repository *repo)
5881 const struct got_error *err;
5882 char *commit_ref_name;
5883 struct got_reference *commit_ref = NULL;
5884 struct got_object_id *commit_id = NULL;
5886 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5890 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5893 err = got_ref_resolve(&commit_id, repo, commit_ref);
5896 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
5897 err = got_error(GOT_ERR_REBASE_COMMITID);
5901 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5902 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
5905 got_ref_close(commit_ref);
5906 free(commit_ref_name);
5911 const struct got_error *
5912 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
5913 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
5914 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5915 struct got_commit_object *orig_commit,
5916 struct got_object_id *orig_commit_id, const char *new_logmsg,
5917 struct got_repository *repo)
5919 const struct got_error *err;
5920 char *commit_ref_name;
5921 struct got_reference *commit_ref = NULL;
5923 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
5927 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
5931 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
5932 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
5935 got_ref_close(commit_ref);
5936 free(commit_ref_name);
5940 const struct got_error *
5941 got_worktree_rebase_postpone(struct got_worktree *worktree,
5942 struct got_fileindex *fileindex)
5945 got_fileindex_free(fileindex);
5946 return lock_worktree(worktree, LOCK_SH);
5949 static const struct got_error *
5950 delete_ref(const char *name, struct got_repository *repo)
5952 const struct got_error *err;
5953 struct got_reference *ref;
5955 err = got_ref_open(&ref, repo, name, 0);
5957 if (err->code == GOT_ERR_NOT_REF)
5962 err = got_ref_delete(ref, repo);
5967 static const struct got_error *
5968 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
5970 const struct got_error *err;
5971 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
5972 char *branch_ref_name = NULL, *commit_ref_name = NULL;
5974 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
5977 err = delete_ref(tmp_branch_name, repo);
5981 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
5984 err = delete_ref(new_base_branch_ref_name, repo);
5988 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
5991 err = delete_ref(branch_ref_name, repo);
5995 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
5998 err = delete_ref(commit_ref_name, repo);
6003 free(tmp_branch_name);
6004 free(new_base_branch_ref_name);
6005 free(branch_ref_name);
6006 free(commit_ref_name);
6010 const struct got_error *
6011 got_worktree_rebase_complete(struct got_worktree *worktree,
6012 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
6013 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
6014 struct got_repository *repo)
6016 const struct got_error *err, *unlockerr;
6017 struct got_object_id *new_head_commit_id = NULL;
6019 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6023 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
6027 err = got_ref_write(rebased_branch, repo);
6031 err = got_worktree_set_head_ref(worktree, rebased_branch);
6035 err = delete_rebase_refs(worktree, repo);
6038 got_fileindex_free(fileindex);
6039 free(new_head_commit_id);
6040 unlockerr = lock_worktree(worktree, LOCK_SH);
6041 if (unlockerr && err == NULL)
6046 const struct got_error *
6047 got_worktree_rebase_abort(struct got_worktree *worktree,
6048 struct got_fileindex *fileindex, struct got_repository *repo,
6049 struct got_reference *new_base_branch,
6050 got_worktree_checkout_cb progress_cb, void *progress_arg)
6052 const struct got_error *err, *unlockerr, *sync_err;
6053 struct got_reference *resolved = NULL;
6054 struct got_object_id *commit_id = NULL;
6055 char *fileindex_path = NULL;
6056 struct revert_file_args rfa;
6057 struct got_object_id *tree_id = NULL;
6059 err = lock_worktree(worktree, LOCK_EX);
6063 err = got_ref_open(&resolved, repo,
6064 got_ref_get_symref_target(new_base_branch), 0);
6068 err = got_worktree_set_head_ref(worktree, resolved);
6073 * XXX commits to the base branch could have happened while
6074 * we were busy rebasing; should we store the original commit ID
6075 * when rebase begins and read it back here?
6077 err = got_ref_resolve(&commit_id, repo, resolved);
6081 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6085 err = got_object_id_by_path(&tree_id, repo,
6086 worktree->base_commit_id, worktree->path_prefix);
6090 err = delete_rebase_refs(worktree, repo);
6094 err = get_fileindex_path(&fileindex_path, worktree);
6098 rfa.worktree = worktree;
6099 rfa.fileindex = fileindex;
6100 rfa.progress_cb = progress_cb;
6101 rfa.progress_arg = progress_arg;
6102 rfa.patch_cb = NULL;
6103 rfa.patch_arg = NULL;
6105 err = worktree_status(worktree, "", fileindex, repo,
6106 revert_file, &rfa, NULL, NULL, 0, 0);
6110 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6111 repo, progress_cb, progress_arg, NULL, NULL);
6113 sync_err = sync_fileindex(fileindex, fileindex_path);
6114 if (sync_err && err == NULL)
6117 got_ref_close(resolved);
6121 got_fileindex_free(fileindex);
6122 free(fileindex_path);
6124 unlockerr = lock_worktree(worktree, LOCK_SH);
6125 if (unlockerr && err == NULL)
6130 const struct got_error *
6131 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
6132 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
6133 struct got_fileindex **fileindex, struct got_worktree *worktree,
6134 struct got_repository *repo)
6136 const struct got_error *err = NULL;
6137 char *tmp_branch_name = NULL;
6138 char *branch_ref_name = NULL;
6139 char *base_commit_ref_name = NULL;
6140 char *fileindex_path = NULL;
6141 struct check_rebase_ok_arg ok_arg;
6142 struct got_reference *wt_branch = NULL;
6143 struct got_reference *base_commit_ref = NULL;
6147 *base_commit_id = NULL;
6150 err = lock_worktree(worktree, LOCK_EX);
6154 err = open_fileindex(fileindex, &fileindex_path, worktree);
6158 ok_arg.worktree = worktree;
6160 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6165 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6169 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6173 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6178 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
6183 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
6187 err = got_ref_write(*branch_ref, repo);
6191 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
6192 worktree->base_commit_id);
6195 err = got_ref_write(base_commit_ref, repo);
6198 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
6199 if (*base_commit_id == NULL) {
6200 err = got_error_from_errno("got_object_id_dup");
6204 err = got_ref_alloc(tmp_branch, tmp_branch_name,
6205 worktree->base_commit_id);
6208 err = got_ref_write(*tmp_branch, repo);
6212 err = got_worktree_set_head_ref(worktree, *tmp_branch);
6216 free(fileindex_path);
6217 free(tmp_branch_name);
6218 free(branch_ref_name);
6219 free(base_commit_ref_name);
6221 got_ref_close(wt_branch);
6224 got_ref_close(*branch_ref);
6228 got_ref_close(*tmp_branch);
6231 free(*base_commit_id);
6233 got_fileindex_free(*fileindex);
6236 lock_worktree(worktree, LOCK_SH);
6241 const struct got_error *
6242 got_worktree_histedit_postpone(struct got_worktree *worktree,
6243 struct got_fileindex *fileindex)
6246 got_fileindex_free(fileindex);
6247 return lock_worktree(worktree, LOCK_SH);
6250 const struct got_error *
6251 got_worktree_histedit_in_progress(int *in_progress,
6252 struct got_worktree *worktree)
6254 const struct got_error *err;
6255 char *tmp_branch_name = NULL;
6257 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6261 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
6262 free(tmp_branch_name);
6266 const struct got_error *
6267 got_worktree_histedit_continue(struct got_object_id **commit_id,
6268 struct got_reference **tmp_branch, struct got_reference **branch_ref,
6269 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
6270 struct got_worktree *worktree, struct got_repository *repo)
6272 const struct got_error *err;
6273 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
6274 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
6275 struct got_reference *commit_ref = NULL;
6276 struct got_reference *base_commit_ref = NULL;
6277 char *fileindex_path = NULL;
6278 int have_staged_files = 0;
6282 *base_commit_id = NULL;
6285 err = lock_worktree(worktree, LOCK_EX);
6289 err = open_fileindex(fileindex, &fileindex_path, worktree);
6293 err = got_fileindex_for_each_entry_safe(*fileindex, check_staged_file,
6294 &have_staged_files);
6295 if (err && err->code != GOT_ERR_CANCELLED)
6297 if (have_staged_files) {
6298 err = got_error(GOT_ERR_STAGED_PATHS);
6302 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6306 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6310 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6314 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6319 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
6323 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
6326 err = got_ref_resolve(commit_id, repo, commit_ref);
6330 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
6333 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
6337 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
6341 free(commit_ref_name);
6342 free(branch_ref_name);
6343 free(fileindex_path);
6345 got_ref_close(commit_ref);
6346 if (base_commit_ref)
6347 got_ref_close(base_commit_ref);
6351 free(*base_commit_id);
6352 *base_commit_id = NULL;
6354 got_ref_close(*tmp_branch);
6358 got_fileindex_free(*fileindex);
6361 lock_worktree(worktree, LOCK_EX);
6366 static const struct got_error *
6367 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
6369 const struct got_error *err;
6370 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
6371 char *branch_ref_name = NULL, *commit_ref_name = NULL;
6373 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
6376 err = delete_ref(tmp_branch_name, repo);
6380 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
6384 err = delete_ref(base_commit_ref_name, repo);
6388 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
6391 err = delete_ref(branch_ref_name, repo);
6395 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6398 err = delete_ref(commit_ref_name, repo);
6402 free(tmp_branch_name);
6403 free(base_commit_ref_name);
6404 free(branch_ref_name);
6405 free(commit_ref_name);
6409 const struct got_error *
6410 got_worktree_histedit_abort(struct got_worktree *worktree,
6411 struct got_fileindex *fileindex, struct got_repository *repo,
6412 struct got_reference *branch, struct got_object_id *base_commit_id,
6413 got_worktree_checkout_cb progress_cb, void *progress_arg)
6415 const struct got_error *err, *unlockerr, *sync_err;
6416 struct got_reference *resolved = NULL;
6417 char *fileindex_path = NULL;
6418 struct got_object_id *tree_id = NULL;
6419 struct revert_file_args rfa;
6421 err = lock_worktree(worktree, LOCK_EX);
6425 err = got_ref_open(&resolved, repo,
6426 got_ref_get_symref_target(branch), 0);
6430 err = got_worktree_set_head_ref(worktree, resolved);
6434 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
6438 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
6439 worktree->path_prefix);
6443 err = delete_histedit_refs(worktree, repo);
6447 err = get_fileindex_path(&fileindex_path, worktree);
6451 rfa.worktree = worktree;
6452 rfa.fileindex = fileindex;
6453 rfa.progress_cb = progress_cb;
6454 rfa.progress_arg = progress_arg;
6455 rfa.patch_cb = NULL;
6456 rfa.patch_arg = NULL;
6458 err = worktree_status(worktree, "", fileindex, repo,
6459 revert_file, &rfa, NULL, NULL, 0, 0);
6463 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
6464 repo, progress_cb, progress_arg, NULL, NULL);
6466 sync_err = sync_fileindex(fileindex, fileindex_path);
6467 if (sync_err && err == NULL)
6470 got_ref_close(resolved);
6472 free(fileindex_path);
6474 unlockerr = lock_worktree(worktree, LOCK_SH);
6475 if (unlockerr && err == NULL)
6480 const struct got_error *
6481 got_worktree_histedit_complete(struct got_worktree *worktree,
6482 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6483 struct got_reference *edited_branch, struct got_repository *repo)
6485 const struct got_error *err, *unlockerr;
6486 struct got_object_id *new_head_commit_id = NULL;
6487 struct got_reference *resolved = NULL;
6489 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
6493 err = got_ref_open(&resolved, repo,
6494 got_ref_get_symref_target(edited_branch), 0);
6498 err = got_ref_change_ref(resolved, new_head_commit_id);
6502 err = got_ref_write(resolved, repo);
6506 err = got_worktree_set_head_ref(worktree, resolved);
6510 err = delete_histedit_refs(worktree, repo);
6513 got_fileindex_free(fileindex);
6514 free(new_head_commit_id);
6515 unlockerr = lock_worktree(worktree, LOCK_SH);
6516 if (unlockerr && err == NULL)
6521 const struct got_error *
6522 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
6523 struct got_object_id *commit_id, struct got_repository *repo)
6525 const struct got_error *err;
6526 char *commit_ref_name;
6528 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
6532 err = store_commit_id(commit_ref_name, commit_id, 0, repo);
6536 err = delete_ref(commit_ref_name, repo);
6538 free(commit_ref_name);
6542 const struct got_error *
6543 got_worktree_integrate_prepare(struct got_fileindex **fileindex,
6544 struct got_reference **branch_ref, struct got_reference **base_branch_ref,
6545 struct got_worktree *worktree, const char *refname,
6546 struct got_repository *repo)
6548 const struct got_error *err = NULL;
6549 char *fileindex_path = NULL;
6550 struct check_rebase_ok_arg ok_arg;
6554 *base_branch_ref = NULL;
6556 err = lock_worktree(worktree, LOCK_EX);
6560 if (strcmp(refname, got_worktree_get_head_ref_name(worktree)) == 0) {
6561 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6562 "cannot integrate a branch into itself; "
6563 "update -b or different branch name required");
6567 err = open_fileindex(fileindex, &fileindex_path, worktree);
6571 /* Preconditions are the same as for rebase. */
6572 ok_arg.worktree = worktree;
6574 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
6579 err = got_ref_open(branch_ref, repo, refname, 1);
6583 err = got_ref_open(base_branch_ref, repo,
6584 got_worktree_get_head_ref_name(worktree), 1);
6588 got_ref_close(*branch_ref);
6591 if (*base_branch_ref) {
6592 got_ref_close(*base_branch_ref);
6593 *base_branch_ref = NULL;
6596 got_fileindex_free(*fileindex);
6599 lock_worktree(worktree, LOCK_SH);
6604 const struct got_error *
6605 got_worktree_integrate_continue(struct got_worktree *worktree,
6606 struct got_fileindex *fileindex, struct got_repository *repo,
6607 struct got_reference *branch_ref, struct got_reference *base_branch_ref,
6608 got_worktree_checkout_cb progress_cb, void *progress_arg,
6609 got_cancel_cb cancel_cb, void *cancel_arg)
6611 const struct got_error *err = NULL, *sync_err, *unlockerr;
6612 char *fileindex_path = NULL;
6613 struct got_object_id *tree_id = NULL, *commit_id = NULL;
6615 err = get_fileindex_path(&fileindex_path, worktree);
6619 err = got_ref_resolve(&commit_id, repo, branch_ref);
6623 err = got_object_id_by_path(&tree_id, repo, commit_id,
6624 worktree->path_prefix);
6628 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
6632 err = checkout_files(worktree, fileindex, "", tree_id, NULL, repo,
6633 progress_cb, progress_arg, cancel_cb, cancel_arg);
6637 err = got_ref_change_ref(base_branch_ref, commit_id);
6641 err = got_ref_write(base_branch_ref, repo);
6643 sync_err = sync_fileindex(fileindex, fileindex_path);
6644 if (sync_err && err == NULL)
6648 unlockerr = got_ref_unlock(branch_ref);
6649 if (unlockerr && err == NULL)
6651 got_ref_close(branch_ref);
6653 unlockerr = got_ref_unlock(base_branch_ref);
6654 if (unlockerr && err == NULL)
6656 got_ref_close(base_branch_ref);
6658 got_fileindex_free(fileindex);
6659 free(fileindex_path);
6662 unlockerr = lock_worktree(worktree, LOCK_SH);
6663 if (unlockerr && err == NULL)
6668 const struct got_error *
6669 got_worktree_integrate_abort(struct got_worktree *worktree,
6670 struct got_fileindex *fileindex, struct got_repository *repo,
6671 struct got_reference *branch_ref, struct got_reference *base_branch_ref)
6673 const struct got_error *err = NULL, *unlockerr = NULL;
6675 got_fileindex_free(fileindex);
6677 err = lock_worktree(worktree, LOCK_SH);
6679 unlockerr = got_ref_unlock(branch_ref);
6680 if (unlockerr && err == NULL)
6682 got_ref_close(branch_ref);
6684 unlockerr = got_ref_unlock(base_branch_ref);
6685 if (unlockerr && err == NULL)
6687 got_ref_close(base_branch_ref);
6692 struct check_stage_ok_arg {
6693 struct got_object_id *head_commit_id;
6694 struct got_worktree *worktree;
6695 struct got_fileindex *fileindex;
6696 struct got_repository *repo;
6700 const struct got_error *
6701 check_stage_ok(void *arg, unsigned char status,
6702 unsigned char staged_status, const char *relpath,
6703 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6704 struct got_object_id *commit_id, int dirfd, const char *de_name)
6706 struct check_stage_ok_arg *a = arg;
6707 const struct got_error *err = NULL;
6708 struct got_fileindex_entry *ie;
6709 struct got_object_id base_commit_id;
6710 struct got_object_id *base_commit_idp = NULL;
6711 char *in_repo_path = NULL, *p;
6713 if (status == GOT_STATUS_UNVERSIONED ||
6714 status == GOT_STATUS_NO_CHANGE)
6716 if (status == GOT_STATUS_NONEXISTENT)
6717 return got_error_set_errno(ENOENT, relpath);
6719 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6721 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6723 if (asprintf(&in_repo_path, "%s%s%s", a->worktree->path_prefix,
6724 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
6726 return got_error_from_errno("asprintf");
6728 if (got_fileindex_entry_has_commit(ie)) {
6729 memcpy(base_commit_id.sha1, ie->commit_sha1,
6730 SHA1_DIGEST_LENGTH);
6731 base_commit_idp = &base_commit_id;
6734 if (status == GOT_STATUS_CONFLICT) {
6735 err = got_error_path(ie->path, GOT_ERR_STAGE_CONFLICT);
6737 } else if (status != GOT_STATUS_ADD &&
6738 status != GOT_STATUS_MODIFY &&
6739 status != GOT_STATUS_DELETE) {
6740 err = got_error_path(ie->path, GOT_ERR_FILE_STATUS);
6744 a->have_changes = 1;
6749 err = check_out_of_date(p, status, staged_status,
6750 blob_id, base_commit_idp, a->head_commit_id, a->repo,
6751 GOT_ERR_STAGE_OUT_OF_DATE);
6757 struct stage_path_arg {
6758 struct got_worktree *worktree;
6759 struct got_fileindex *fileindex;
6760 struct got_repository *repo;
6761 got_worktree_status_cb status_cb;
6763 got_worktree_patch_cb patch_cb;
6765 int staged_something;
6768 static const struct got_error *
6769 stage_path(void *arg, unsigned char status,
6770 unsigned char staged_status, const char *relpath,
6771 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6772 struct got_object_id *commit_id, int dirfd, const char *de_name)
6774 struct stage_path_arg *a = arg;
6775 const struct got_error *err = NULL;
6776 struct got_fileindex_entry *ie;
6777 char *ondisk_path = NULL, *path_content = NULL;
6779 struct got_object_id *new_staged_blob_id = NULL;
6781 if (status == GOT_STATUS_UNVERSIONED)
6784 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
6786 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
6788 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
6790 return got_error_from_errno("asprintf");
6793 case GOT_STATUS_ADD:
6794 case GOT_STATUS_MODIFY:
6796 if (status == GOT_STATUS_ADD) {
6797 int choice = GOT_PATCH_CHOICE_NONE;
6798 err = (*a->patch_cb)(&choice, a->patch_arg,
6799 status, ie->path, NULL, 1, 1);
6802 if (choice != GOT_PATCH_CHOICE_YES)
6805 err = create_patched_content(&path_content, 0,
6806 staged_blob_id ? staged_blob_id : blob_id,
6807 ondisk_path, dirfd, de_name, ie->path,
6808 a->repo, a->patch_cb, a->patch_arg);
6809 if (err || path_content == NULL)
6813 err = got_object_blob_create(&new_staged_blob_id,
6814 path_content ? path_content : ondisk_path, a->repo);
6817 memcpy(ie->staged_blob_sha1, new_staged_blob_id->sha1,
6818 SHA1_DIGEST_LENGTH);
6819 if (status == GOT_STATUS_ADD || staged_status == GOT_STATUS_ADD)
6820 stage = GOT_FILEIDX_STAGE_ADD;
6822 stage = GOT_FILEIDX_STAGE_MODIFY;
6823 got_fileindex_entry_stage_set(ie, stage);
6824 a->staged_something = 1;
6825 if (a->status_cb == NULL)
6827 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6828 get_staged_status(ie), relpath, blob_id,
6829 new_staged_blob_id, NULL, dirfd, de_name);
6831 case GOT_STATUS_DELETE:
6832 if (staged_status == GOT_STATUS_DELETE)
6835 int choice = GOT_PATCH_CHOICE_NONE;
6836 err = (*a->patch_cb)(&choice, a->patch_arg, status,
6837 ie->path, NULL, 1, 1);
6840 if (choice == GOT_PATCH_CHOICE_NO)
6842 if (choice != GOT_PATCH_CHOICE_YES) {
6843 err = got_error(GOT_ERR_PATCH_CHOICE);
6847 stage = GOT_FILEIDX_STAGE_DELETE;
6848 got_fileindex_entry_stage_set(ie, stage);
6849 a->staged_something = 1;
6850 if (a->status_cb == NULL)
6852 err = (*a->status_cb)(a->status_arg, GOT_STATUS_NO_CHANGE,
6853 get_staged_status(ie), relpath, NULL, NULL, NULL, dirfd,
6856 case GOT_STATUS_NO_CHANGE:
6858 case GOT_STATUS_CONFLICT:
6859 err = got_error_path(relpath, GOT_ERR_STAGE_CONFLICT);
6861 case GOT_STATUS_NONEXISTENT:
6862 err = got_error_set_errno(ENOENT, relpath);
6865 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
6869 if (path_content && unlink(path_content) == -1 && err == NULL)
6870 err = got_error_from_errno2("unlink", path_content);
6873 free(new_staged_blob_id);
6877 const struct got_error *
6878 got_worktree_stage(struct got_worktree *worktree,
6879 struct got_pathlist_head *paths,
6880 got_worktree_status_cb status_cb, void *status_arg,
6881 got_worktree_patch_cb patch_cb, void *patch_arg,
6882 struct got_repository *repo)
6884 const struct got_error *err = NULL, *sync_err, *unlockerr;
6885 struct got_pathlist_entry *pe;
6886 struct got_fileindex *fileindex = NULL;
6887 char *fileindex_path = NULL;
6888 struct got_reference *head_ref = NULL;
6889 struct got_object_id *head_commit_id = NULL;
6890 struct check_stage_ok_arg oka;
6891 struct stage_path_arg spa;
6893 err = lock_worktree(worktree, LOCK_EX);
6897 err = got_ref_open(&head_ref, repo,
6898 got_worktree_get_head_ref_name(worktree), 0);
6901 err = got_ref_resolve(&head_commit_id, repo, head_ref);
6904 err = open_fileindex(&fileindex, &fileindex_path, worktree);
6908 /* Check pre-conditions before staging anything. */
6909 oka.head_commit_id = head_commit_id;
6910 oka.worktree = worktree;
6911 oka.fileindex = fileindex;
6913 oka.have_changes = 0;
6914 TAILQ_FOREACH(pe, paths, entry) {
6915 err = worktree_status(worktree, pe->path, fileindex, repo,
6916 check_stage_ok, &oka, NULL, NULL, 0, 0);
6920 if (!oka.have_changes) {
6921 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6925 spa.worktree = worktree;
6926 spa.fileindex = fileindex;
6928 spa.patch_cb = patch_cb;
6929 spa.patch_arg = patch_arg;
6930 spa.status_cb = status_cb;
6931 spa.status_arg = status_arg;
6932 spa.staged_something = 0;
6933 TAILQ_FOREACH(pe, paths, entry) {
6934 err = worktree_status(worktree, pe->path, fileindex, repo,
6935 stage_path, &spa, NULL, NULL, 0, 0);
6939 if (!spa.staged_something) {
6940 err = got_error(GOT_ERR_STAGE_NO_CHANGE);
6944 sync_err = sync_fileindex(fileindex, fileindex_path);
6945 if (sync_err && err == NULL)
6949 got_ref_close(head_ref);
6950 free(head_commit_id);
6951 free(fileindex_path);
6953 got_fileindex_free(fileindex);
6954 unlockerr = lock_worktree(worktree, LOCK_SH);
6955 if (unlockerr && err == NULL)
6960 struct unstage_path_arg {
6961 struct got_worktree *worktree;
6962 struct got_fileindex *fileindex;
6963 struct got_repository *repo;
6964 got_worktree_checkout_cb progress_cb;
6966 got_worktree_patch_cb patch_cb;
6970 static const struct got_error *
6971 create_unstaged_content(char **path_unstaged_content,
6972 char **path_new_staged_content, struct got_object_id *blob_id,
6973 struct got_object_id *staged_blob_id, const char *relpath,
6974 struct got_repository *repo,
6975 got_worktree_patch_cb patch_cb, void *patch_arg)
6977 const struct got_error *err;
6978 struct got_blob_object *blob = NULL, *staged_blob = NULL;
6979 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL, *rejectfile = NULL;
6980 char *path1 = NULL, *path2 = NULL, *label1 = NULL;
6981 struct stat sb1, sb2;
6982 struct got_diff_changes *changes = NULL;
6983 struct got_diff_state *ds = NULL;
6984 struct got_diff_args *args = NULL;
6985 struct got_diff_change *change;
6986 int diff_flags = 0, line_cur1 = 1, line_cur2 = 1, n = 0;
6987 int have_content = 0, have_rejected_content = 0;
6989 *path_unstaged_content = NULL;
6990 *path_new_staged_content = NULL;
6992 err = got_object_id_str(&label1, blob_id);
6995 err = got_object_open_as_blob(&blob, repo, blob_id, 8192);
6999 err = got_opentemp_named(&path1, &f1, "got-unstage-blob-base");
7003 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f1, blob);
7007 err = got_object_open_as_blob(&staged_blob, repo, staged_blob_id, 8192);
7011 err = got_opentemp_named(&path2, &f2, "got-unstage-blob-staged");
7015 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f2, staged_blob);
7019 if (stat(path1, &sb1) == -1) {
7020 err = got_error_from_errno2("stat", path1);
7024 if (stat(path2, &sb2) == -1) {
7025 err = got_error_from_errno2("stat", path2);
7029 err = got_diff_files(&changes, &ds, &args, &diff_flags,
7030 f1, sb1.st_size, label1, f2, sb2.st_size, path2, 3, NULL);
7034 err = got_opentemp_named(path_unstaged_content, &outfile,
7035 "got-unstaged-content");
7038 err = got_opentemp_named(path_new_staged_content, &rejectfile,
7039 "got-new-staged-content");
7043 if (fseek(f1, 0L, SEEK_SET) == -1) {
7044 err = got_ferror(f1, GOT_ERR_IO);
7047 if (fseek(f2, 0L, SEEK_SET) == -1) {
7048 err = got_ferror(f2, GOT_ERR_IO);
7051 SIMPLEQ_FOREACH(change, &changes->entries, entry) {
7053 err = apply_or_reject_change(&choice, change, ++n,
7054 changes->nchanges, ds, args, diff_flags, relpath,
7055 f1, f2, &line_cur1, &line_cur2,
7056 outfile, rejectfile, patch_cb, patch_arg);
7059 if (choice == GOT_PATCH_CHOICE_YES)
7062 have_rejected_content = 1;
7063 if (choice == GOT_PATCH_CHOICE_QUIT)
7066 if (have_content || have_rejected_content)
7067 err = copy_remaining_content(f1, f2, &line_cur1, &line_cur2,
7068 outfile, rejectfile);
7072 got_object_blob_close(blob);
7074 got_object_blob_close(staged_blob);
7075 if (f1 && fclose(f1) == EOF && err == NULL)
7076 err = got_error_from_errno2("fclose", path1);
7077 if (f2 && fclose(f2) == EOF && err == NULL)
7078 err = got_error_from_errno2("fclose", path2);
7079 if (outfile && fclose(outfile) == EOF && err == NULL)
7080 err = got_error_from_errno2("fclose", *path_unstaged_content);
7081 if (rejectfile && fclose(rejectfile) == EOF && err == NULL)
7082 err = got_error_from_errno2("fclose", *path_new_staged_content);
7083 if (path1 && unlink(path1) == -1 && err == NULL)
7084 err = got_error_from_errno2("unlink", path1);
7085 if (path2 && unlink(path2) == -1 && err == NULL)
7086 err = got_error_from_errno2("unlink", path2);
7087 if (err || !have_content) {
7088 if (*path_unstaged_content &&
7089 unlink(*path_unstaged_content) == -1 && err == NULL)
7090 err = got_error_from_errno2("unlink",
7091 *path_unstaged_content);
7092 free(*path_unstaged_content);
7093 *path_unstaged_content = NULL;
7095 if (err || !have_rejected_content) {
7096 if (*path_new_staged_content &&
7097 unlink(*path_new_staged_content) == -1 && err == NULL)
7098 err = got_error_from_errno2("unlink",
7099 *path_new_staged_content);
7100 free(*path_new_staged_content);
7101 *path_new_staged_content = NULL;
7105 got_diff_state_free(ds);
7109 got_diff_free_changes(changes);
7115 static const struct got_error *
7116 unstage_path(void *arg, unsigned char status,
7117 unsigned char staged_status, const char *relpath,
7118 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7119 struct got_object_id *commit_id, int dirfd, const char *de_name)
7121 const struct got_error *err = NULL;
7122 struct unstage_path_arg *a = arg;
7123 struct got_fileindex_entry *ie;
7124 struct got_blob_object *blob_base = NULL, *blob_staged = NULL;
7125 char *ondisk_path = NULL, *path_unstaged_content = NULL;
7126 char *path_new_staged_content = NULL;
7127 char *id_str = NULL, *label_orig = NULL;
7128 int local_changes_subsumed;
7131 if (staged_status != GOT_STATUS_ADD &&
7132 staged_status != GOT_STATUS_MODIFY &&
7133 staged_status != GOT_STATUS_DELETE)
7136 ie = got_fileindex_entry_get(a->fileindex, relpath, strlen(relpath));
7138 return got_error_path(relpath, GOT_ERR_FILE_STATUS);
7140 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, relpath)
7142 return got_error_from_errno("asprintf");
7144 err = got_object_id_str(&id_str,
7145 commit_id ? commit_id : a->worktree->base_commit_id);
7148 if (asprintf(&label_orig, "%s: commit %s", GOT_MERGE_LABEL_BASE,
7150 err = got_error_from_errno("asprintf");
7154 switch (staged_status) {
7155 case GOT_STATUS_MODIFY:
7156 err = got_object_open_as_blob(&blob_base, a->repo,
7161 case GOT_STATUS_ADD:
7163 if (staged_status == GOT_STATUS_ADD) {
7164 int choice = GOT_PATCH_CHOICE_NONE;
7165 err = (*a->patch_cb)(&choice, a->patch_arg,
7166 staged_status, ie->path, NULL, 1, 1);
7169 if (choice != GOT_PATCH_CHOICE_YES)
7172 err = create_unstaged_content(
7173 &path_unstaged_content,
7174 &path_new_staged_content, blob_id,
7175 staged_blob_id, ie->path, a->repo,
7176 a->patch_cb, a->patch_arg);
7177 if (err || path_unstaged_content == NULL)
7179 if (path_new_staged_content) {
7180 err = got_object_blob_create(
7182 path_new_staged_content,
7186 memcpy(ie->staged_blob_sha1,
7187 staged_blob_id->sha1,
7188 SHA1_DIGEST_LENGTH);
7190 err = merge_file(&local_changes_subsumed,
7191 a->worktree, blob_base, ondisk_path,
7192 relpath, got_fileindex_perms_to_st(ie),
7193 path_unstaged_content, label_orig,
7194 "unstaged", a->repo, a->progress_cb,
7197 path_new_staged_content == NULL)
7198 got_fileindex_entry_stage_set(ie,
7199 GOT_FILEIDX_STAGE_NONE);
7200 break; /* Done with this file. */
7203 err = got_object_open_as_blob(&blob_staged, a->repo,
7204 staged_blob_id, 8192);
7207 err = merge_blob(&local_changes_subsumed, a->worktree,
7208 blob_base, ondisk_path, relpath,
7209 got_fileindex_perms_to_st(ie), label_orig, blob_staged,
7210 commit_id ? commit_id : a->worktree->base_commit_id,
7211 a->repo, a->progress_cb, a->progress_arg);
7213 got_fileindex_entry_stage_set(ie,
7214 GOT_FILEIDX_STAGE_NONE);
7216 case GOT_STATUS_DELETE:
7218 int choice = GOT_PATCH_CHOICE_NONE;
7219 err = (*a->patch_cb)(&choice, a->patch_arg,
7220 staged_status, ie->path, NULL, 1, 1);
7223 if (choice == GOT_PATCH_CHOICE_NO)
7225 if (choice != GOT_PATCH_CHOICE_YES) {
7226 err = got_error(GOT_ERR_PATCH_CHOICE);
7230 got_fileindex_entry_stage_set(ie, GOT_FILEIDX_STAGE_NONE);
7231 err = get_file_status(&status, &sb, ie, ondisk_path,
7232 dirfd, de_name, a->repo);
7235 err = (*a->progress_cb)(a->progress_arg, status, relpath);
7240 if (path_unstaged_content &&
7241 unlink(path_unstaged_content) == -1 && err == NULL)
7242 err = got_error_from_errno2("unlink", path_unstaged_content);
7243 if (path_new_staged_content &&
7244 unlink(path_new_staged_content) == -1 && err == NULL)
7245 err = got_error_from_errno2("unlink", path_new_staged_content);
7246 free(path_unstaged_content);
7247 free(path_new_staged_content);
7249 got_object_blob_close(blob_base);
7251 got_object_blob_close(blob_staged);
7257 const struct got_error *
7258 got_worktree_unstage(struct got_worktree *worktree,
7259 struct got_pathlist_head *paths,
7260 got_worktree_checkout_cb progress_cb, void *progress_arg,
7261 got_worktree_patch_cb patch_cb, void *patch_arg,
7262 struct got_repository *repo)
7264 const struct got_error *err = NULL, *sync_err, *unlockerr;
7265 struct got_pathlist_entry *pe;
7266 struct got_fileindex *fileindex = NULL;
7267 char *fileindex_path = NULL;
7268 struct unstage_path_arg upa;
7270 err = lock_worktree(worktree, LOCK_EX);
7274 err = open_fileindex(&fileindex, &fileindex_path, worktree);
7278 upa.worktree = worktree;
7279 upa.fileindex = fileindex;
7281 upa.progress_cb = progress_cb;
7282 upa.progress_arg = progress_arg;
7283 upa.patch_cb = patch_cb;
7284 upa.patch_arg = patch_arg;
7285 TAILQ_FOREACH(pe, paths, entry) {
7286 err = worktree_status(worktree, pe->path, fileindex, repo,
7287 unstage_path, &upa, NULL, NULL, 0, 0);
7292 sync_err = sync_fileindex(fileindex, fileindex_path);
7293 if (sync_err && err == NULL)
7296 free(fileindex_path);
7298 got_fileindex_free(fileindex);
7299 unlockerr = lock_worktree(worktree, LOCK_SH);
7300 if (unlockerr && err == NULL)