Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/stat.h>
18 #include <sys/limits.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
22 #include <dirent.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
34 #include <uuid.h>
35 #include <util.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 static const struct got_error *
62 create_meta_file(const char *path_got, const char *name, const char *content)
63 {
64 const struct got_error *err = NULL;
65 char *path;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 return got_error_from_errno("asprintf");
70 err = got_path_create_file(path, content);
71 free(path);
72 return err;
73 }
75 static const struct got_error *
76 update_meta_file(const char *path_got, const char *name, const char *content)
77 {
78 const struct got_error *err = NULL;
79 FILE *tmpfile = NULL;
80 char *tmppath = NULL;
81 char *path = NULL;
83 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 err = got_error_from_errno("asprintf");
85 path = NULL;
86 goto done;
87 }
89 err = got_opentemp_named(&tmppath, &tmpfile, path);
90 if (err)
91 goto done;
93 if (content) {
94 int len = fprintf(tmpfile, "%s\n", content);
95 if (len != strlen(content) + 1) {
96 err = got_error_from_errno2("fprintf", tmppath);
97 goto done;
98 }
99 }
101 if (rename(tmppath, path) != 0) {
102 err = got_error_from_errno3("rename", tmppath, path);
103 unlink(tmppath);
104 goto done;
107 done:
108 if (fclose(tmpfile) != 0 && err == NULL)
109 err = got_error_from_errno2("fclose", tmppath);
110 free(tmppath);
111 return err;
114 static const struct got_error *
115 read_meta_file(char **content, const char *path_got, const char *name)
117 const struct got_error *err = NULL;
118 char *path;
119 int fd = -1;
120 ssize_t n;
121 struct stat sb;
123 *content = NULL;
125 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 err = got_error_from_errno("asprintf");
127 path = NULL;
128 goto done;
131 fd = open(path, O_RDONLY | O_NOFOLLOW);
132 if (fd == -1) {
133 if (errno == ENOENT)
134 err = got_error_path(path, GOT_ERR_WORKTREE_META);
135 else
136 err = got_error_from_errno2("open", path);
137 goto done;
139 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 : got_error_from_errno2("flock", path));
142 goto done;
145 if (fstat(fd, &sb) != 0) {
146 err = got_error_from_errno2("fstat", path);
147 goto done;
149 *content = calloc(1, sb.st_size);
150 if (*content == NULL) {
151 err = got_error_from_errno("calloc");
152 goto done;
155 n = read(fd, *content, sb.st_size);
156 if (n != sb.st_size) {
157 err = (n == -1 ? got_error_from_errno2("read", path) :
158 got_error_path(path, GOT_ERR_WORKTREE_META));
159 goto done;
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error_path(path, GOT_ERR_WORKTREE_META);
163 goto done;
165 (*content)[sb.st_size - 1] = '\0';
167 done:
168 if (fd != -1 && close(fd) == -1 && err == NULL)
169 err = got_error_from_errno2("close", path_got);
170 free(path);
171 if (err) {
172 free(*content);
173 *content = NULL;
175 return err;
178 static const struct got_error *
179 write_head_ref(const char *path_got, struct got_reference *head_ref)
181 const struct got_error *err = NULL;
182 char *refstr = NULL;
184 if (got_ref_is_symbolic(head_ref)) {
185 refstr = got_ref_to_str(head_ref);
186 if (refstr == NULL)
187 return got_error_from_errno("got_ref_to_str");
188 } else {
189 refstr = strdup(got_ref_get_name(head_ref));
190 if (refstr == NULL)
191 return got_error_from_errno("strdup");
193 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 free(refstr);
195 return err;
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
204 uuid_t uuid;
205 uint32_t uuid_status;
206 int obj_type;
207 char *path_got = NULL;
208 char *formatstr = NULL;
209 char *absprefix = NULL;
210 char *basestr = NULL;
211 char *uuidstr = NULL;
213 if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 err = got_error(GOT_ERR_WORKTREE_REPO);
215 goto done;
218 err = got_ref_resolve(&commit_id, repo, head_ref);
219 if (err)
220 return err;
221 err = got_object_get_type(&obj_type, repo, commit_id);
222 if (err)
223 return err;
224 if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 return got_error(GOT_ERR_OBJ_TYPE);
227 if (!got_path_is_absolute(prefix)) {
228 if (asprintf(&absprefix, "/%s", prefix) == -1)
229 return got_error_from_errno("asprintf");
232 /* Create top-level directory (may already exist). */
233 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 err = got_error_from_errno2("mkdir", path);
235 goto done;
238 /* Create .got directory (may already exist). */
239 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 err = got_error_from_errno("asprintf");
241 goto done;
243 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 err = got_error_from_errno2("mkdir", path_got);
245 goto done;
248 /* Create an empty lock file. */
249 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
250 if (err)
251 goto done;
253 /* Create an empty file index. */
254 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
255 if (err)
256 goto done;
258 /* Write the HEAD reference. */
259 err = write_head_ref(path_got, head_ref);
260 if (err)
261 goto done;
263 /* Record our base commit. */
264 err = got_object_id_str(&basestr, commit_id);
265 if (err)
266 goto done;
267 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
268 if (err)
269 goto done;
271 /* Store path to repository. */
272 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 got_repo_get_path(repo));
274 if (err)
275 goto done;
277 /* Store in-repository path prefix. */
278 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 absprefix ? absprefix : prefix);
280 if (err)
281 goto done;
283 /* Generate UUID. */
284 uuid_create(&uuid, &uuid_status);
285 if (uuid_status != uuid_s_ok) {
286 err = got_error_uuid(uuid_status);
287 goto done;
289 uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status);
292 goto done;
294 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
295 if (err)
296 goto done;
298 /* Stamp work tree with format file. */
299 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 err = got_error_from_errno("asprintf");
301 goto done;
303 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
304 if (err)
305 goto done;
307 done:
308 free(commit_id);
309 free(path_got);
310 free(formatstr);
311 free(absprefix);
312 free(basestr);
313 free(uuidstr);
314 return err;
317 static const struct got_error *
318 open_worktree(struct got_worktree **worktree, const char *path)
320 const struct got_error *err = NULL;
321 char *path_got;
322 char *formatstr = NULL;
323 char *uuidstr = NULL;
324 char *path_lock = NULL;
325 char *base_commit_id_str = NULL;
326 int version, fd = -1;
327 const char *errstr;
328 struct got_repository *repo = NULL;
329 uint32_t uuid_status;
331 *worktree = NULL;
333 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
335 path_got = NULL;
336 goto done;
339 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_lock = NULL;
342 goto done;
345 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 if (fd == -1) {
347 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 : got_error_from_errno2("open", path_lock));
349 goto done;
352 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
353 if (err)
354 goto done;
356 version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 if (errstr) {
358 err = got_error_msg(GOT_ERR_WORKTREE_META,
359 "could not parse work tree format version number");
360 goto done;
362 if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 err = got_error(GOT_ERR_WORKTREE_VERS);
364 goto done;
367 *worktree = calloc(1, sizeof(**worktree));
368 if (*worktree == NULL) {
369 err = got_error_from_errno("calloc");
370 goto done;
372 (*worktree)->lockfd = -1;
374 (*worktree)->root_path = strdup(path);
375 if ((*worktree)->root_path == NULL) {
376 err = got_error_from_errno("strdup");
377 goto done;
379 err = read_meta_file(&(*worktree)->repo_path, path_got,
380 GOT_WORKTREE_REPOSITORY);
381 if (err)
382 goto done;
384 err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 GOT_WORKTREE_PATH_PREFIX);
386 if (err)
387 goto done;
389 err = read_meta_file(&base_commit_id_str, path_got,
390 GOT_WORKTREE_BASE_COMMIT);
391 if (err)
392 goto done;
394 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
395 if (err)
396 goto done;
397 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 if (uuid_status != uuid_s_ok) {
399 err = got_error_uuid(uuid_status);
400 goto done;
403 err = got_repo_open(&repo, (*worktree)->repo_path);
404 if (err)
405 goto done;
407 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
408 base_commit_id_str);
409 if (err)
410 goto done;
412 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 GOT_WORKTREE_HEAD_REF);
414 done:
415 if (repo)
416 got_repo_close(repo);
417 free(path_got);
418 free(path_lock);
419 free(base_commit_id_str);
420 free(uuidstr);
421 free(formatstr);
422 if (err) {
423 if (fd != -1)
424 close(fd);
425 if (*worktree != NULL)
426 got_worktree_close(*worktree);
427 *worktree = NULL;
428 } else
429 (*worktree)->lockfd = fd;
431 return err;
434 const struct got_error *
435 got_worktree_open(struct got_worktree **worktree, const char *path)
437 const struct got_error *err = NULL;
439 do {
440 err = open_worktree(worktree, path);
441 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
442 return err;
443 if (*worktree)
444 return NULL;
445 path = dirname(path);
446 if (path == NULL)
447 return got_error_from_errno2("dirname", path);
448 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
450 return got_error(GOT_ERR_NOT_WORKTREE);
453 const struct got_error *
454 got_worktree_close(struct got_worktree *worktree)
456 const struct got_error *err = NULL;
457 free(worktree->root_path);
458 free(worktree->repo_path);
459 free(worktree->path_prefix);
460 free(worktree->base_commit_id);
461 free(worktree->head_ref_name);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno2("close",
465 got_worktree_get_root_path(worktree));
466 free(worktree);
467 return err;
470 const char *
471 got_worktree_get_root_path(struct got_worktree *worktree)
473 return worktree->root_path;
476 const char *
477 got_worktree_get_repo_path(struct got_worktree *worktree)
479 return worktree->repo_path;
482 const char *
483 got_worktree_get_path_prefix(struct got_worktree *worktree)
485 return worktree->path_prefix;
488 const struct got_error *
489 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 const char *path_prefix)
492 char *absprefix = NULL;
494 if (!got_path_is_absolute(path_prefix)) {
495 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 return got_error_from_errno("asprintf");
498 *match = (strcmp(absprefix ? absprefix : path_prefix,
499 worktree->path_prefix) == 0);
500 free(absprefix);
501 return NULL;
504 const char *
505 got_worktree_get_head_ref_name(struct got_worktree *worktree)
507 return worktree->head_ref_name;
510 const struct got_error *
511 got_worktree_set_head_ref(struct got_worktree *worktree,
512 struct got_reference *head_ref)
514 const struct got_error *err = NULL;
515 char *path_got = NULL, *head_ref_name = NULL;
517 if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 GOT_WORKTREE_GOT_DIR) == -1) {
519 err = got_error_from_errno("asprintf");
520 path_got = NULL;
521 goto done;
524 head_ref_name = strdup(got_ref_get_name(head_ref));
525 if (head_ref_name == NULL) {
526 err = got_error_from_errno("strdup");
527 goto done;
530 err = write_head_ref(path_got, head_ref);
531 if (err)
532 goto done;
534 free(worktree->head_ref_name);
535 worktree->head_ref_name = head_ref_name;
536 done:
537 free(path_got);
538 if (err)
539 free(head_ref_name);
540 return err;
543 struct got_object_id *
544 got_worktree_get_base_commit_id(struct got_worktree *worktree)
546 return worktree->base_commit_id;
549 const struct got_error *
550 got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 struct got_repository *repo, struct got_object_id *commit_id)
553 const struct got_error *err;
554 struct got_object *obj = NULL;
555 char *id_str = NULL;
556 char *path_got = NULL;
558 if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 GOT_WORKTREE_GOT_DIR) == -1) {
560 err = got_error_from_errno("asprintf");
561 path_got = NULL;
562 goto done;
565 err = got_object_open(&obj, repo, commit_id);
566 if (err)
567 return err;
569 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 err = got_error(GOT_ERR_OBJ_TYPE);
571 goto done;
574 /* Record our base commit. */
575 err = got_object_id_str(&id_str, commit_id);
576 if (err)
577 goto done;
578 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
579 if (err)
580 goto done;
582 free(worktree->base_commit_id);
583 worktree->base_commit_id = got_object_id_dup(commit_id);
584 if (worktree->base_commit_id == NULL) {
585 err = got_error_from_errno("got_object_id_dup");
586 goto done;
588 done:
589 if (obj)
590 got_object_close(obj);
591 free(id_str);
592 free(path_got);
593 return err;
596 static const struct got_error *
597 lock_worktree(struct got_worktree *worktree, int operation)
599 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 : got_error_from_errno2("flock",
602 got_worktree_get_root_path(worktree)));
603 return NULL;
606 static const struct got_error *
607 add_dir_on_disk(struct got_worktree *worktree, const char *path)
609 const struct got_error *err = NULL;
610 char *abspath;
612 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 return got_error_from_errno("asprintf");
615 err = got_path_mkdir(abspath);
616 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
617 struct stat sb;
618 err = NULL;
619 if (lstat(abspath, &sb) == -1) {
620 err = got_error_from_errno2("lstat", abspath);
621 } else if (!S_ISDIR(sb.st_mode)) {
622 /* TODO directory is obstructed; do something */
623 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
626 free(abspath);
627 return err;
630 static const struct got_error *
631 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
633 const struct got_error *err = NULL;
634 uint8_t fbuf1[8192];
635 uint8_t fbuf2[8192];
636 size_t flen1 = 0, flen2 = 0;
638 *same = 1;
640 for (;;) {
641 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 if (flen1 == 0 && ferror(f1)) {
643 err = got_error_from_errno("fread");
644 break;
646 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 if (flen2 == 0 && ferror(f2)) {
648 err = got_error_from_errno("fread");
649 break;
651 if (flen1 == 0) {
652 if (flen2 != 0)
653 *same = 0;
654 break;
655 } else if (flen2 == 0) {
656 if (flen1 != 0)
657 *same = 0;
658 break;
659 } else if (flen1 == flen2) {
660 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
661 *same = 0;
662 break;
664 } else {
665 *same = 0;
666 break;
670 return err;
673 static const struct got_error *
674 check_files_equal(int *same, const char *f1_path, const char *f2_path)
676 const struct got_error *err = NULL;
677 struct stat sb;
678 size_t size1, size2;
679 FILE *f1 = NULL, *f2 = NULL;
681 *same = 1;
683 if (lstat(f1_path, &sb) != 0) {
684 err = got_error_from_errno2("lstat", f1_path);
685 goto done;
687 size1 = sb.st_size;
689 if (lstat(f2_path, &sb) != 0) {
690 err = got_error_from_errno2("lstat", f2_path);
691 goto done;
693 size2 = sb.st_size;
695 if (size1 != size2) {
696 *same = 0;
697 return NULL;
700 f1 = fopen(f1_path, "r");
701 if (f1 == NULL)
702 return got_error_from_errno2("open", f1_path);
704 f2 = fopen(f2_path, "r");
705 if (f2 == NULL) {
706 err = got_error_from_errno2("open", f2_path);
707 goto done;
710 err = check_file_contents_equal(same, f1, f2);
711 done:
712 if (f1 && fclose(f1) != 0 && err == NULL)
713 err = got_error_from_errno("fclose");
714 if (f2 && fclose(f2) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
717 return err;
720 /*
721 * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 * blob_deriv acts as the first derived version, and the file on disk
723 * acts as the second derived version.
724 */
725 static const struct got_error *
726 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
727 struct got_blob_object *blob_orig, const char *ondisk_path,
728 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
729 struct got_object_id *deriv_base_commit_id,
730 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
731 void *progress_arg)
733 const struct got_error *err = NULL;
734 int merged_fd = -1;
735 FILE *f_deriv = NULL, *f_orig = NULL;
736 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
738 char *id_str = NULL;
739 char *label_deriv = NULL;
740 int overlapcnt = 0;
741 char *parent;
743 *local_changes_subsumed = 0;
745 parent = dirname(ondisk_path);
746 if (parent == NULL)
747 return got_error_from_errno2("dirname", ondisk_path);
749 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
750 return got_error_from_errno("asprintf");
752 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
753 if (err)
754 goto done;
756 free(base_path);
757 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
758 err = got_error_from_errno("asprintf");
759 base_path = NULL;
760 goto done;
763 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
764 if (err)
765 goto done;
766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
767 blob_deriv);
768 if (err)
769 goto done;
771 free(base_path);
772 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
773 err = got_error_from_errno("asprintf");
774 base_path = NULL;
775 goto done;
778 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
779 if (err)
780 goto done;
781 if (blob_orig) {
782 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
783 blob_orig);
784 if (err)
785 goto done;
786 } else {
787 /*
788 * If the file has no blob, this is an "add vs add" conflict,
789 * and we simply use an empty ancestor file to make both files
790 * appear in the merged result in their entirety.
791 */
794 err = got_object_id_str(&id_str, deriv_base_commit_id);
795 if (err)
796 goto done;
797 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
798 err = got_error_from_errno("asprintf");
799 goto done;
802 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
803 blob_orig_path, ondisk_path, label_deriv, path);
804 if (err)
805 goto done;
807 err = (*progress_cb)(progress_arg,
808 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 if (err)
810 goto done;
812 if (fsync(merged_fd) != 0) {
813 err = got_error_from_errno("fsync");
814 goto done;
817 /* Check if a clean merge has subsumed all local changes. */
818 if (overlapcnt == 0) {
819 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
820 merged_path);
821 if (err)
822 goto done;
825 if (chmod(merged_path, st_mode) != 0) {
826 err = got_error_from_errno2("chmod", merged_path);
827 goto done;
830 if (rename(merged_path, ondisk_path) != 0) {
831 err = got_error_from_errno3("rename", merged_path,
832 ondisk_path);
833 unlink(merged_path);
834 goto done;
837 done:
838 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
839 err = got_error_from_errno("close");
840 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
841 err = got_error_from_errno("fclose");
842 if (f_orig && fclose(f_orig) != 0 && err == NULL)
843 err = got_error_from_errno("fclose");
844 free(merged_path);
845 free(base_path);
846 if (blob_deriv_path) {
847 unlink(blob_deriv_path);
848 free(blob_deriv_path);
850 if (blob_orig_path) {
851 unlink(blob_orig_path);
852 free(blob_orig_path);
854 free(id_str);
855 free(label_deriv);
856 return err;
859 static const struct got_error *
860 update_blob_fileindex_entry(struct got_worktree *worktree,
861 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
862 const char *ondisk_path, const char *path, struct got_blob_object *blob,
863 int update_timestamps)
865 const struct got_error *err = NULL;
867 if (ie == NULL)
868 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
869 if (ie)
870 err = got_fileindex_entry_update(ie, ondisk_path,
871 blob->id.sha1, worktree->base_commit_id->sha1,
872 update_timestamps);
873 else {
874 struct got_fileindex_entry *new_ie;
875 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
876 path, blob->id.sha1, worktree->base_commit_id->sha1);
877 if (!err)
878 err = got_fileindex_entry_add(fileindex, new_ie);
880 return err;
883 static const struct got_error *
884 install_blob(struct got_worktree *worktree, const char *ondisk_path,
885 const char *path, uint16_t te_mode, uint16_t st_mode,
886 struct got_blob_object *blob, int restoring_missing_file,
887 int reverting_versioned_file, struct got_repository *repo,
888 got_worktree_checkout_cb progress_cb, void *progress_arg)
890 const struct got_error *err = NULL;
891 int fd = -1;
892 size_t len, hdrlen;
893 int update = 0;
894 char *tmppath = NULL;
896 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
897 GOT_DEFAULT_FILE_MODE);
898 if (fd == -1) {
899 if (errno == ENOENT) {
900 char *parent = dirname(path);
901 if (parent == NULL)
902 return got_error_from_errno2("dirname", path);
903 err = add_dir_on_disk(worktree, parent);
904 if (err)
905 return err;
906 fd = open(ondisk_path,
907 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
908 GOT_DEFAULT_FILE_MODE);
909 if (fd == -1)
910 return got_error_from_errno2("open",
911 ondisk_path);
912 } else if (errno == EEXIST) {
913 if (!S_ISREG(st_mode)) {
914 /* TODO file is obstructed; do something */
915 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
916 goto done;
917 } else {
918 err = got_opentemp_named_fd(&tmppath, &fd,
919 ondisk_path);
920 if (err)
921 goto done;
922 update = 1;
924 } else
925 return got_error_from_errno2("open", ondisk_path);
928 if (restoring_missing_file)
929 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
930 else if (reverting_versioned_file)
931 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
932 else
933 err = (*progress_cb)(progress_arg,
934 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
935 if (err)
936 goto done;
938 hdrlen = got_object_blob_get_hdrlen(blob);
939 do {
940 const uint8_t *buf = got_object_blob_get_read_buf(blob);
941 err = got_object_blob_read_block(&len, blob);
942 if (err)
943 break;
944 if (len > 0) {
945 /* Skip blob object header first time around. */
946 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
947 if (outlen == -1) {
948 err = got_error_from_errno("write");
949 goto done;
950 } else if (outlen != len - hdrlen) {
951 err = got_error(GOT_ERR_IO);
952 goto done;
954 hdrlen = 0;
956 } while (len != 0);
958 if (fsync(fd) != 0) {
959 err = got_error_from_errno("fsync");
960 goto done;
963 if (update) {
964 if (rename(tmppath, ondisk_path) != 0) {
965 err = got_error_from_errno3("rename", tmppath,
966 ondisk_path);
967 unlink(tmppath);
968 goto done;
972 if (te_mode & S_IXUSR) {
973 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
974 err = got_error_from_errno2("chmod", ondisk_path);
975 goto done;
977 } else {
978 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
979 err = got_error_from_errno2("chmod", ondisk_path);
980 goto done;
984 done:
985 if (fd != -1 && close(fd) != 0 && err == NULL)
986 err = got_error_from_errno("close");
987 free(tmppath);
988 return err;
991 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
992 static const struct got_error *
993 get_modified_file_content_status(unsigned char *status, FILE *f)
995 const struct got_error *err = NULL;
996 const char *markers[3] = {
997 GOT_DIFF_CONFLICT_MARKER_BEGIN,
998 GOT_DIFF_CONFLICT_MARKER_SEP,
999 GOT_DIFF_CONFLICT_MARKER_END
1001 int i = 0;
1002 char *line;
1003 size_t len;
1004 const char delim[3] = {'\0', '\0', '\0'};
1006 while (*status == GOT_STATUS_MODIFY) {
1007 line = fparseln(f, &len, NULL, delim, 0);
1008 if (line == NULL) {
1009 if (feof(f))
1010 break;
1011 err = got_ferror(f, GOT_ERR_IO);
1012 break;
1015 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1016 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1017 == 0)
1018 *status = GOT_STATUS_CONFLICT;
1019 else
1020 i++;
1024 return err;
1027 static int
1028 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1030 return !(ie->ctime_sec == sb->st_ctime &&
1031 ie->ctime_nsec == sb->st_ctimensec &&
1032 ie->mtime_sec == sb->st_mtime &&
1033 ie->mtime_nsec == sb->st_mtimensec &&
1034 ie->size == (sb->st_size & 0xffffffff));
1037 static const struct got_error *
1038 get_file_status(unsigned char *status, struct stat *sb,
1039 struct got_fileindex_entry *ie, const char *abspath,
1040 struct got_repository *repo)
1042 const struct got_error *err = NULL;
1043 struct got_object_id id;
1044 size_t hdrlen;
1045 FILE *f = NULL;
1046 uint8_t fbuf[8192];
1047 struct got_blob_object *blob = NULL;
1048 size_t flen, blen;
1050 *status = GOT_STATUS_NO_CHANGE;
1052 if (lstat(abspath, sb) == -1) {
1053 if (errno == ENOENT) {
1054 if (got_fileindex_entry_has_file_on_disk(ie))
1055 *status = GOT_STATUS_MISSING;
1056 else
1057 *status = GOT_STATUS_DELETE;
1058 return NULL;
1060 return got_error_from_errno2("lstat", abspath);
1063 if (!S_ISREG(sb->st_mode)) {
1064 *status = GOT_STATUS_OBSTRUCTED;
1065 return NULL;
1068 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1069 *status = GOT_STATUS_DELETE;
1070 return NULL;
1071 } else if (!got_fileindex_entry_has_blob(ie)) {
1072 *status = GOT_STATUS_ADD;
1073 return NULL;
1076 if (!stat_info_differs(ie, sb))
1077 return NULL;
1079 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1080 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1081 if (err)
1082 return err;
1084 f = fopen(abspath, "r");
1085 if (f == NULL) {
1086 err = got_error_from_errno2("fopen", abspath);
1087 goto done;
1089 hdrlen = got_object_blob_get_hdrlen(blob);
1090 for (;;) {
1091 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1092 err = got_object_blob_read_block(&blen, blob);
1093 if (err)
1094 goto done;
1095 /* Skip length of blob object header first time around. */
1096 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1097 if (flen == 0 && ferror(f)) {
1098 err = got_error_from_errno("fread");
1099 goto done;
1101 if (blen == 0) {
1102 if (flen != 0)
1103 *status = GOT_STATUS_MODIFY;
1104 break;
1105 } else if (flen == 0) {
1106 if (blen != 0)
1107 *status = GOT_STATUS_MODIFY;
1108 break;
1109 } else if (blen - hdrlen == flen) {
1110 /* Skip blob object header first time around. */
1111 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1112 *status = GOT_STATUS_MODIFY;
1113 break;
1115 } else {
1116 *status = GOT_STATUS_MODIFY;
1117 break;
1119 hdrlen = 0;
1122 if (*status == GOT_STATUS_MODIFY) {
1123 rewind(f);
1124 err = get_modified_file_content_status(status, f);
1126 done:
1127 if (blob)
1128 got_object_blob_close(blob);
1129 if (f)
1130 fclose(f);
1131 return err;
1135 * Update timestamps in the file index if a file is unmodified and
1136 * we had to run a full content comparison to find out.
1138 static const struct got_error *
1139 sync_timestamps(char *ondisk_path, unsigned char status,
1140 struct got_fileindex_entry *ie, struct stat *sb)
1142 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1143 return got_fileindex_entry_update(ie, ondisk_path,
1144 ie->blob_sha1, ie->commit_sha1, 1);
1146 return NULL;
1149 static const struct got_error *
1150 update_blob(struct got_worktree *worktree,
1151 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1152 struct got_tree_entry *te, const char *path,
1153 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1154 void *progress_arg)
1156 const struct got_error *err = NULL;
1157 struct got_blob_object *blob = NULL;
1158 char *ondisk_path;
1159 unsigned char status = GOT_STATUS_NO_CHANGE;
1160 struct stat sb;
1162 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1163 return got_error_from_errno("asprintf");
1165 if (ie) {
1166 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1167 if (err)
1168 goto done;
1169 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1170 sb.st_mode = got_fileindex_perms_to_st(ie);
1171 } else
1172 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1174 if (status == GOT_STATUS_OBSTRUCTED) {
1175 err = (*progress_cb)(progress_arg, status, path);
1176 goto done;
1179 if (ie && status != GOT_STATUS_MISSING) {
1180 if (got_fileindex_entry_has_commit(ie) &&
1181 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1182 SHA1_DIGEST_LENGTH) == 0) {
1183 err = sync_timestamps(ondisk_path, status, ie, &sb);
1184 if (err)
1185 goto done;
1186 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1187 path);
1188 goto done;
1190 if (got_fileindex_entry_has_blob(ie) &&
1191 memcmp(ie->blob_sha1, te->id->sha1,
1192 SHA1_DIGEST_LENGTH) == 0) {
1193 err = sync_timestamps(ondisk_path, status, ie, &sb);
1194 goto done;
1198 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1199 if (err)
1200 goto done;
1202 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1203 int update_timestamps;
1204 struct got_blob_object *blob2 = NULL;
1205 if (got_fileindex_entry_has_blob(ie)) {
1206 struct got_object_id id2;
1207 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1208 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1209 if (err)
1210 goto done;
1212 err = merge_blob(&update_timestamps, worktree, blob2,
1213 ondisk_path, path, sb.st_mode, blob,
1214 worktree->base_commit_id, repo,
1215 progress_cb, progress_arg);
1216 if (blob2)
1217 got_object_blob_close(blob2);
1219 * Do not update timestamps of files with local changes.
1220 * Otherwise, a future status walk would treat them as
1221 * unmodified files again.
1223 err = got_fileindex_entry_update(ie, ondisk_path,
1224 blob->id.sha1, worktree->base_commit_id->sha1,
1225 update_timestamps);
1226 } else if (status == GOT_STATUS_DELETE) {
1227 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1228 if (err)
1229 goto done;
1230 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1231 ondisk_path, path, blob, 0);
1232 if (err)
1233 goto done;
1234 } else {
1235 err = install_blob(worktree, ondisk_path, path, te->mode,
1236 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1237 repo, progress_cb, progress_arg);
1238 if (err)
1239 goto done;
1240 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1241 ondisk_path, path, blob, 1);
1242 if (err)
1243 goto done;
1245 got_object_blob_close(blob);
1246 done:
1247 free(ondisk_path);
1248 return err;
1251 static const struct got_error *
1252 remove_ondisk_file(const char *root_path, const char *path)
1254 const struct got_error *err = NULL;
1255 char *ondisk_path = NULL;
1257 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1258 return got_error_from_errno("asprintf");
1260 if (unlink(ondisk_path) == -1) {
1261 if (errno != ENOENT)
1262 err = got_error_from_errno2("unlink", ondisk_path);
1263 } else {
1264 char *parent = dirname(ondisk_path);
1265 while (parent && strcmp(parent, root_path) != 0) {
1266 if (rmdir(parent) == -1) {
1267 if (errno != ENOTEMPTY)
1268 err = got_error_from_errno2("rmdir",
1269 parent);
1270 break;
1272 parent = dirname(parent);
1275 free(ondisk_path);
1276 return err;
1279 static const struct got_error *
1280 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1281 struct got_fileindex_entry *ie, struct got_repository *repo,
1282 got_worktree_checkout_cb progress_cb, void *progress_arg)
1284 const struct got_error *err = NULL;
1285 unsigned char status;
1286 struct stat sb;
1287 char *ondisk_path;
1289 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1290 == -1)
1291 return got_error_from_errno("asprintf");
1293 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1294 if (err)
1295 return err;
1297 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1298 status == GOT_STATUS_ADD) {
1299 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1300 if (err)
1301 return err;
1303 * Preserve the working file and change the deleted blob's
1304 * entry into a schedule-add entry.
1306 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1307 0);
1308 if (err)
1309 return err;
1310 } else {
1311 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1312 if (err)
1313 return err;
1314 if (status == GOT_STATUS_NO_CHANGE) {
1315 err = remove_ondisk_file(worktree->root_path, ie->path);
1316 if (err)
1317 return err;
1319 got_fileindex_entry_remove(fileindex, ie);
1322 return err;
1325 struct diff_cb_arg {
1326 struct got_fileindex *fileindex;
1327 struct got_worktree *worktree;
1328 struct got_repository *repo;
1329 got_worktree_checkout_cb progress_cb;
1330 void *progress_arg;
1331 got_worktree_cancel_cb cancel_cb;
1332 void *cancel_arg;
1335 static const struct got_error *
1336 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1337 struct got_tree_entry *te, const char *parent_path)
1339 struct diff_cb_arg *a = arg;
1341 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1342 return got_error(GOT_ERR_CANCELLED);
1344 return update_blob(a->worktree, a->fileindex, ie, te,
1345 ie->path, a->repo, a->progress_cb, a->progress_arg);
1348 static const struct got_error *
1349 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1351 struct diff_cb_arg *a = arg;
1353 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1354 return got_error(GOT_ERR_CANCELLED);
1356 return delete_blob(a->worktree, a->fileindex, ie,
1357 a->repo, a->progress_cb, a->progress_arg);
1360 static const struct got_error *
1361 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1363 struct diff_cb_arg *a = arg;
1364 const struct got_error *err;
1365 char *path;
1367 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1368 return got_error(GOT_ERR_CANCELLED);
1370 if (asprintf(&path, "%s%s%s", parent_path,
1371 parent_path[0] ? "/" : "", te->name)
1372 == -1)
1373 return got_error_from_errno("asprintf");
1375 if (S_ISDIR(te->mode))
1376 err = add_dir_on_disk(a->worktree, path);
1377 else
1378 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1379 a->repo, a->progress_cb, a->progress_arg);
1381 free(path);
1382 return err;
1385 static const struct got_error *
1386 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1388 const struct got_error *err = NULL;
1389 char *uuidstr = NULL;
1390 uint32_t uuid_status;
1392 *refname = NULL;
1394 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1395 if (uuid_status != uuid_s_ok)
1396 return got_error_uuid(uuid_status);
1398 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1399 == -1) {
1400 err = got_error_from_errno("asprintf");
1401 *refname = NULL;
1403 free(uuidstr);
1404 return err;
1407 const struct got_error *
1408 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1410 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1413 static const struct got_error *
1414 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1416 return get_ref_name(refname, worktree,
1417 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1420 static const struct got_error *
1421 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1423 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1426 static const struct got_error *
1427 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1429 return get_ref_name(refname, worktree,
1430 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1433 static const struct got_error *
1434 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1436 return get_ref_name(refname, worktree,
1437 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1440 static const struct got_error *
1441 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1443 return get_ref_name(refname, worktree,
1444 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1447 static const struct got_error *
1448 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1450 return get_ref_name(refname, worktree,
1451 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1454 static const struct got_error *
1455 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1457 return get_ref_name(refname, worktree,
1458 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1461 static const struct got_error *
1462 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1464 return get_ref_name(refname, worktree,
1465 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1468 const struct got_error *
1469 got_worktree_get_histedit_script_path(char **path,
1470 struct got_worktree *worktree)
1472 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1473 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1474 *path = NULL;
1475 return got_error_from_errno("asprintf");
1477 return NULL;
1481 * Prevent Git's garbage collector from deleting our base commit by
1482 * setting a reference to our base commit's ID.
1484 static const struct got_error *
1485 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_reference *ref = NULL;
1489 char *refname;
1491 err = got_worktree_get_base_ref_name(&refname, worktree);
1492 if (err)
1493 return err;
1495 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1496 if (err)
1497 goto done;
1499 err = got_ref_write(ref, repo);
1500 done:
1501 free(refname);
1502 if (ref)
1503 got_ref_close(ref);
1504 return err;
1507 static const struct got_error *
1508 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1510 const struct got_error *err = NULL;
1512 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1513 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1514 err = got_error_from_errno("asprintf");
1515 *fileindex_path = NULL;
1517 return err;
1521 static const struct got_error *
1522 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1523 struct got_worktree *worktree)
1525 const struct got_error *err = NULL;
1526 FILE *index = NULL;
1528 *fileindex_path = NULL;
1529 *fileindex = got_fileindex_alloc();
1530 if (*fileindex == NULL)
1531 return got_error_from_errno("got_fileindex_alloc");
1533 err = get_fileindex_path(fileindex_path, worktree);
1534 if (err)
1535 goto done;
1537 index = fopen(*fileindex_path, "rb");
1538 if (index == NULL) {
1539 if (errno != ENOENT)
1540 err = got_error_from_errno2("fopen", *fileindex_path);
1541 } else {
1542 err = got_fileindex_read(*fileindex, index);
1543 if (fclose(index) != 0 && err == NULL)
1544 err = got_error_from_errno("fclose");
1546 done:
1547 if (err) {
1548 free(*fileindex_path);
1549 *fileindex_path = NULL;
1550 got_fileindex_free(*fileindex);
1551 *fileindex = NULL;
1553 return err;
1556 struct bump_base_commit_id_arg {
1557 struct got_object_id *base_commit_id;
1558 const char *path;
1559 size_t path_len;
1560 const char *entry_name;
1561 got_worktree_checkout_cb progress_cb;
1562 void *progress_arg;
1565 /* Bump base commit ID of all files within an updated part of the work tree. */
1566 static const struct got_error *
1567 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1569 const struct got_error *err;
1570 struct bump_base_commit_id_arg *a = arg;
1572 if (a->entry_name) {
1573 if (strcmp(ie->path, a->path) != 0)
1574 return NULL;
1575 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1576 return NULL;
1578 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1579 SHA1_DIGEST_LENGTH) == 0)
1580 return NULL;
1582 if (a->progress_cb) {
1583 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1584 ie->path);
1585 if (err)
1586 return err;
1588 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1589 return NULL;
1592 static const struct got_error *
1593 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1595 const struct got_error *err = NULL;
1596 char *new_fileindex_path = NULL;
1597 FILE *new_index = NULL;
1599 err = got_opentemp_named(&new_fileindex_path, &new_index,
1600 fileindex_path);
1601 if (err)
1602 goto done;
1604 err = got_fileindex_write(fileindex, new_index);
1605 if (err)
1606 goto done;
1608 if (rename(new_fileindex_path, fileindex_path) != 0) {
1609 err = got_error_from_errno3("rename", new_fileindex_path,
1610 fileindex_path);
1611 unlink(new_fileindex_path);
1613 done:
1614 if (new_index)
1615 fclose(new_index);
1616 free(new_fileindex_path);
1617 return err;
1620 static const struct got_error *
1621 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1622 struct got_object_id **tree_id, const char *wt_relpath,
1623 struct got_worktree *worktree, struct got_repository *repo)
1625 const struct got_error *err = NULL;
1626 struct got_object_id *id = NULL;
1627 char *in_repo_path = NULL;
1628 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1630 *entry_type = GOT_OBJ_TYPE_ANY;
1631 *tree_relpath = NULL;
1632 *tree_id = NULL;
1634 if (wt_relpath[0] == '\0') {
1635 /* Check out all files within the work tree. */
1636 *entry_type = GOT_OBJ_TYPE_TREE;
1637 *tree_relpath = strdup("");
1638 if (*tree_relpath == NULL) {
1639 err = got_error_from_errno("strdup");
1640 goto done;
1642 err = got_object_id_by_path(tree_id, repo,
1643 worktree->base_commit_id, worktree->path_prefix);
1644 if (err)
1645 goto done;
1646 return NULL;
1649 /* Check out a subset of files in the work tree. */
1651 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1652 is_root_wt ? "" : "/", wt_relpath) == -1) {
1653 err = got_error_from_errno("asprintf");
1654 goto done;
1657 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1658 in_repo_path);
1659 if (err)
1660 goto done;
1662 free(in_repo_path);
1663 in_repo_path = NULL;
1665 err = got_object_get_type(entry_type, repo, id);
1666 if (err)
1667 goto done;
1669 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1670 /* Check out a single file. */
1671 if (strchr(wt_relpath, '/') == NULL) {
1672 /* Check out a single file in work tree's root dir. */
1673 in_repo_path = strdup(worktree->path_prefix);
1674 if (in_repo_path == NULL) {
1675 err = got_error_from_errno("strdup");
1676 goto done;
1678 *tree_relpath = strdup("");
1679 if (*tree_relpath == NULL) {
1680 err = got_error_from_errno("strdup");
1681 goto done;
1683 } else {
1684 /* Check out a single file in a subdirectory. */
1685 err = got_path_dirname(tree_relpath, wt_relpath);
1686 if (err)
1687 return err;
1688 if (asprintf(&in_repo_path, "%s%s%s",
1689 worktree->path_prefix, is_root_wt ? "" : "/",
1690 *tree_relpath) == -1) {
1691 err = got_error_from_errno("asprintf");
1692 goto done;
1695 err = got_object_id_by_path(tree_id, repo,
1696 worktree->base_commit_id, in_repo_path);
1697 } else {
1698 /* Check out all files within a subdirectory. */
1699 *tree_id = got_object_id_dup(id);
1700 if (*tree_id == NULL) {
1701 err = got_error_from_errno("got_object_id_dup");
1702 goto done;
1704 *tree_relpath = strdup(wt_relpath);
1705 if (*tree_relpath == NULL) {
1706 err = got_error_from_errno("strdup");
1707 goto done;
1710 done:
1711 free(id);
1712 free(in_repo_path);
1713 if (err) {
1714 *entry_type = GOT_OBJ_TYPE_ANY;
1715 free(*tree_relpath);
1716 *tree_relpath = NULL;
1717 free(*tree_id);
1718 *tree_id = NULL;
1720 return err;
1723 static const struct got_error *
1724 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1725 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1726 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1727 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1729 const struct got_error *err = NULL;
1730 struct got_commit_object *commit = NULL;
1731 struct got_tree_object *tree = NULL;
1732 struct got_fileindex_diff_tree_cb diff_cb;
1733 struct diff_cb_arg arg;
1735 err = ref_base_commit(worktree, repo);
1736 if (err)
1737 goto done;
1739 err = got_object_open_as_commit(&commit, repo,
1740 worktree->base_commit_id);
1741 if (err)
1742 goto done;
1744 err = got_object_open_as_tree(&tree, repo, tree_id);
1745 if (err)
1746 goto done;
1748 if (entry_name &&
1749 got_object_tree_find_entry(tree, entry_name) == NULL) {
1750 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1751 goto done;
1754 diff_cb.diff_old_new = diff_old_new;
1755 diff_cb.diff_old = diff_old;
1756 diff_cb.diff_new = diff_new;
1757 arg.fileindex = fileindex;
1758 arg.worktree = worktree;
1759 arg.repo = repo;
1760 arg.progress_cb = progress_cb;
1761 arg.progress_arg = progress_arg;
1762 arg.cancel_cb = cancel_cb;
1763 arg.cancel_arg = cancel_arg;
1764 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1765 entry_name, repo, &diff_cb, &arg);
1766 done:
1767 if (tree)
1768 got_object_tree_close(tree);
1769 if (commit)
1770 got_object_commit_close(commit);
1771 return err;
1774 const struct got_error *
1775 got_worktree_checkout_files(struct got_worktree *worktree,
1776 struct got_pathlist_head *paths, struct got_repository *repo,
1777 got_worktree_checkout_cb progress_cb, void *progress_arg,
1778 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1780 const struct got_error *err = NULL, *sync_err, *unlockerr;
1781 struct got_commit_object *commit = NULL;
1782 struct got_tree_object *tree = NULL;
1783 struct got_fileindex *fileindex = NULL;
1784 char *fileindex_path = NULL;
1785 struct got_pathlist_entry *pe;
1786 struct tree_path_data {
1787 SIMPLEQ_ENTRY(tree_path_data) entry;
1788 struct got_object_id *tree_id;
1789 int entry_type;
1790 char *relpath;
1791 char *entry_name;
1792 } *tpd = NULL;
1793 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1795 SIMPLEQ_INIT(&tree_paths);
1797 err = lock_worktree(worktree, LOCK_EX);
1798 if (err)
1799 return err;
1801 /* Map all specified paths to in-repository trees. */
1802 TAILQ_FOREACH(pe, paths, entry) {
1803 tpd = malloc(sizeof(*tpd));
1804 if (tpd == NULL) {
1805 err = got_error_from_errno("malloc");
1806 goto done;
1809 err = find_tree_entry_for_checkout(&tpd->entry_type,
1810 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1811 if (err) {
1812 free(tpd);
1813 goto done;
1816 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1817 err = got_path_basename(&tpd->entry_name, pe->path);
1818 if (err) {
1819 free(tpd->relpath);
1820 free(tpd->tree_id);
1821 free(tpd);
1822 goto done;
1824 } else
1825 tpd->entry_name = NULL;
1827 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1831 * Read the file index.
1832 * Checking out files is supposed to be an idempotent operation.
1833 * If the on-disk file index is incomplete we will try to complete it.
1835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1836 if (err)
1837 goto done;
1839 tpd = SIMPLEQ_FIRST(&tree_paths);
1840 TAILQ_FOREACH(pe, paths, entry) {
1841 struct bump_base_commit_id_arg bbc_arg;
1843 err = checkout_files(worktree, fileindex, tpd->relpath,
1844 tpd->tree_id, tpd->entry_name, repo,
1845 progress_cb, progress_arg, cancel_cb, cancel_arg);
1846 if (err)
1847 break;
1849 bbc_arg.base_commit_id = worktree->base_commit_id;
1850 bbc_arg.entry_name = tpd->entry_name;
1851 bbc_arg.path = pe->path;
1852 bbc_arg.path_len = pe->path_len;
1853 bbc_arg.progress_cb = progress_cb;
1854 bbc_arg.progress_arg = progress_arg;
1855 err = got_fileindex_for_each_entry_safe(fileindex,
1856 bump_base_commit_id, &bbc_arg);
1857 if (err)
1858 break;
1860 tpd = SIMPLEQ_NEXT(tpd, entry);
1862 sync_err = sync_fileindex(fileindex, fileindex_path);
1863 if (sync_err && err == NULL)
1864 err = sync_err;
1865 done:
1866 free(fileindex_path);
1867 if (tree)
1868 got_object_tree_close(tree);
1869 if (commit)
1870 got_object_commit_close(commit);
1871 if (fileindex)
1872 got_fileindex_free(fileindex);
1873 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1874 tpd = SIMPLEQ_FIRST(&tree_paths);
1875 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1876 free(tpd->relpath);
1877 free(tpd->tree_id);
1878 free(tpd);
1880 unlockerr = lock_worktree(worktree, LOCK_SH);
1881 if (unlockerr && err == NULL)
1882 err = unlockerr;
1883 return err;
1886 struct merge_file_cb_arg {
1887 struct got_worktree *worktree;
1888 struct got_fileindex *fileindex;
1889 got_worktree_checkout_cb progress_cb;
1890 void *progress_arg;
1891 got_worktree_cancel_cb cancel_cb;
1892 void *cancel_arg;
1893 struct got_object_id *commit_id2;
1896 static const struct got_error *
1897 merge_file_cb(void *arg, struct got_blob_object *blob1,
1898 struct got_blob_object *blob2, struct got_object_id *id1,
1899 struct got_object_id *id2, const char *path1, const char *path2,
1900 struct got_repository *repo)
1902 static const struct got_error *err = NULL;
1903 struct merge_file_cb_arg *a = arg;
1904 struct got_fileindex_entry *ie;
1905 char *ondisk_path = NULL;
1906 struct stat sb;
1907 unsigned char status;
1908 int local_changes_subsumed;
1910 if (blob1 && blob2) {
1911 ie = got_fileindex_entry_get(a->fileindex, path2,
1912 strlen(path2));
1913 if (ie == NULL)
1914 return (*a->progress_cb)(a->progress_arg,
1915 GOT_STATUS_MISSING, path2);
1917 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1918 path2) == -1)
1919 return got_error_from_errno("asprintf");
1921 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1922 if (err)
1923 goto done;
1925 if (status == GOT_STATUS_DELETE) {
1926 err = (*a->progress_cb)(a->progress_arg,
1927 GOT_STATUS_MERGE, path2);
1928 goto done;
1930 if (status != GOT_STATUS_NO_CHANGE &&
1931 status != GOT_STATUS_MODIFY &&
1932 status != GOT_STATUS_CONFLICT &&
1933 status != GOT_STATUS_ADD) {
1934 err = (*a->progress_cb)(a->progress_arg, status, path2);
1935 goto done;
1938 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1939 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1940 a->progress_cb, a->progress_arg);
1941 } else if (blob1) {
1942 ie = got_fileindex_entry_get(a->fileindex, path1,
1943 strlen(path1));
1944 if (ie == NULL)
1945 return (*a->progress_cb)(a->progress_arg,
1946 GOT_STATUS_MISSING, path2);
1948 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1949 path1) == -1)
1950 return got_error_from_errno("asprintf");
1952 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1953 if (err)
1954 goto done;
1956 switch (status) {
1957 case GOT_STATUS_NO_CHANGE:
1958 err = (*a->progress_cb)(a->progress_arg,
1959 GOT_STATUS_DELETE, path1);
1960 if (err)
1961 goto done;
1962 err = remove_ondisk_file(a->worktree->root_path, path1);
1963 if (err)
1964 goto done;
1965 if (ie)
1966 got_fileindex_entry_mark_deleted_from_disk(ie);
1967 break;
1968 case GOT_STATUS_DELETE:
1969 case GOT_STATUS_MISSING:
1970 err = (*a->progress_cb)(a->progress_arg,
1971 GOT_STATUS_DELETE, path1);
1972 if (err)
1973 goto done;
1974 if (ie)
1975 got_fileindex_entry_mark_deleted_from_disk(ie);
1976 break;
1977 case GOT_STATUS_ADD:
1978 case GOT_STATUS_MODIFY:
1979 case GOT_STATUS_CONFLICT:
1980 err = (*a->progress_cb)(a->progress_arg,
1981 GOT_STATUS_CANNOT_DELETE, path1);
1982 if (err)
1983 goto done;
1984 break;
1985 case GOT_STATUS_OBSTRUCTED:
1986 err = (*a->progress_cb)(a->progress_arg, status, path1);
1987 if (err)
1988 goto done;
1989 break;
1990 default:
1991 break;
1993 } else if (blob2) {
1994 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1995 path2) == -1)
1996 return got_error_from_errno("asprintf");
1997 ie = got_fileindex_entry_get(a->fileindex, path2,
1998 strlen(path2));
1999 if (ie) {
2000 err = get_file_status(&status, &sb, ie, ondisk_path,
2001 repo);
2002 if (err)
2003 goto done;
2004 if (status != GOT_STATUS_NO_CHANGE &&
2005 status != GOT_STATUS_MODIFY &&
2006 status != GOT_STATUS_CONFLICT &&
2007 status != GOT_STATUS_ADD) {
2008 err = (*a->progress_cb)(a->progress_arg,
2009 status, path2);
2010 goto done;
2012 err = merge_blob(&local_changes_subsumed, a->worktree,
2013 NULL, ondisk_path, path2, sb.st_mode, blob2,
2014 a->commit_id2, repo,
2015 a->progress_cb, a->progress_arg);
2016 if (status == GOT_STATUS_DELETE) {
2017 err = update_blob_fileindex_entry(a->worktree,
2018 a->fileindex, ie, ondisk_path, ie->path,
2019 blob2, 0);
2020 if (err)
2021 goto done;
2023 } else {
2024 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2025 err = install_blob(a->worktree, ondisk_path, path2,
2026 /* XXX get this from parent tree! */
2027 GOT_DEFAULT_FILE_MODE,
2028 sb.st_mode, blob2, 0, 0, repo,
2029 a->progress_cb, a->progress_arg);
2030 if (err)
2031 goto done;
2032 err = got_fileindex_entry_alloc(&ie,
2033 ondisk_path, path2, NULL, NULL);
2034 if (err)
2035 goto done;
2036 err = got_fileindex_entry_add(a->fileindex, ie);
2037 if (err) {
2038 got_fileindex_entry_free(ie);
2039 goto done;
2043 done:
2044 free(ondisk_path);
2045 return err;
2048 struct check_merge_ok_arg {
2049 struct got_worktree *worktree;
2050 struct got_repository *repo;
2053 static const struct got_error *
2054 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2056 const struct got_error *err = NULL;
2057 struct check_merge_ok_arg *a = arg;
2058 unsigned char status;
2059 struct stat sb;
2060 char *ondisk_path;
2062 /* Reject merges into a work tree with mixed base commits. */
2063 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2064 SHA1_DIGEST_LENGTH))
2065 return got_error(GOT_ERR_MIXED_COMMITS);
2067 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2068 == -1)
2069 return got_error_from_errno("asprintf");
2071 /* Reject merges into a work tree with conflicted files. */
2072 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2073 if (err)
2074 return err;
2075 if (status == GOT_STATUS_CONFLICT)
2076 return got_error(GOT_ERR_CONFLICTS);
2078 return NULL;
2081 static const struct got_error *
2082 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 const char *fileindex_path, struct got_object_id *commit_id1,
2084 struct got_object_id *commit_id2, struct got_repository *repo,
2085 got_worktree_checkout_cb progress_cb, void *progress_arg,
2086 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2088 const struct got_error *err = NULL, *sync_err;
2089 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2090 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2091 struct merge_file_cb_arg arg;
2093 if (commit_id1) {
2094 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2095 worktree->path_prefix);
2096 if (err)
2097 goto done;
2099 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2100 if (err)
2101 goto done;
2104 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2105 worktree->path_prefix);
2106 if (err)
2107 goto done;
2109 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2110 if (err)
2111 goto done;
2113 arg.worktree = worktree;
2114 arg.fileindex = fileindex;
2115 arg.progress_cb = progress_cb;
2116 arg.progress_arg = progress_arg;
2117 arg.cancel_cb = cancel_cb;
2118 arg.cancel_arg = cancel_arg;
2119 arg.commit_id2 = commit_id2;
2120 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2121 sync_err = sync_fileindex(fileindex, fileindex_path);
2122 if (sync_err && err == NULL)
2123 err = sync_err;
2124 done:
2125 if (tree1)
2126 got_object_tree_close(tree1);
2127 if (tree2)
2128 got_object_tree_close(tree2);
2129 return err;
2132 const struct got_error *
2133 got_worktree_merge_files(struct got_worktree *worktree,
2134 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2135 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2136 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2138 const struct got_error *err, *unlockerr;
2139 char *fileindex_path = NULL;
2140 struct got_fileindex *fileindex = NULL;
2141 struct check_merge_ok_arg mok_arg;
2143 err = lock_worktree(worktree, LOCK_EX);
2144 if (err)
2145 return err;
2147 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2148 if (err)
2149 goto done;
2151 mok_arg.worktree = worktree;
2152 mok_arg.repo = repo;
2153 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2154 &mok_arg);
2155 if (err)
2156 goto done;
2158 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2159 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2160 done:
2161 if (fileindex)
2162 got_fileindex_free(fileindex);
2163 free(fileindex_path);
2164 unlockerr = lock_worktree(worktree, LOCK_SH);
2165 if (unlockerr && err == NULL)
2166 err = unlockerr;
2167 return err;
2170 struct diff_dir_cb_arg {
2171 struct got_fileindex *fileindex;
2172 struct got_worktree *worktree;
2173 const char *status_path;
2174 size_t status_path_len;
2175 struct got_repository *repo;
2176 got_worktree_status_cb status_cb;
2177 void *status_arg;
2178 got_worktree_cancel_cb cancel_cb;
2179 void *cancel_arg;
2182 static unsigned char
2183 get_staged_status(struct got_fileindex_entry *ie)
2185 switch (got_fileindex_entry_stage_get(ie)) {
2186 case GOT_FILEIDX_STAGE_ADD:
2187 return GOT_STATUS_ADD;
2188 case GOT_FILEIDX_STAGE_DELETE:
2189 return GOT_STATUS_DELETE;
2190 case GOT_FILEIDX_STAGE_MODIFY:
2191 return GOT_STATUS_MODIFY;
2192 default:
2193 return GOT_STATUS_NO_CHANGE;
2197 static const struct got_error *
2198 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2199 got_worktree_status_cb status_cb, void *status_arg,
2200 struct got_repository *repo)
2202 const struct got_error *err = NULL;
2203 unsigned char status = GOT_STATUS_NO_CHANGE;
2204 struct stat sb;
2205 struct got_object_id blob_id, commit_id;
2207 err = get_file_status(&status, &sb, ie, abspath, repo);
2208 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2209 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2210 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2211 err = (*status_cb)(status_arg, status, get_staged_status(ie),
2212 ie->path, &blob_id, &commit_id);
2214 return err;
2217 static const struct got_error *
2218 status_old_new(void *arg, struct got_fileindex_entry *ie,
2219 struct dirent *de, const char *parent_path)
2221 const struct got_error *err = NULL;
2222 struct diff_dir_cb_arg *a = arg;
2223 char *abspath;
2225 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2226 return got_error(GOT_ERR_CANCELLED);
2228 if (got_path_cmp(parent_path, a->status_path,
2229 strlen(parent_path), a->status_path_len) != 0 &&
2230 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2231 return NULL;
2233 if (parent_path[0]) {
2234 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2235 parent_path, de->d_name) == -1)
2236 return got_error_from_errno("asprintf");
2237 } else {
2238 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2239 de->d_name) == -1)
2240 return got_error_from_errno("asprintf");
2243 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2244 a->repo);
2245 free(abspath);
2246 return err;
2249 static const struct got_error *
2250 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2252 struct diff_dir_cb_arg *a = arg;
2253 struct got_object_id blob_id, commit_id;
2254 unsigned char status;
2256 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2257 return got_error(GOT_ERR_CANCELLED);
2259 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2260 return NULL;
2262 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2263 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2264 if (got_fileindex_entry_has_file_on_disk(ie))
2265 status = GOT_STATUS_MISSING;
2266 else
2267 status = GOT_STATUS_DELETE;
2268 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2269 ie->path, &blob_id, &commit_id);
2272 static const struct got_error *
2273 status_new(void *arg, struct dirent *de, const char *parent_path)
2275 const struct got_error *err = NULL;
2276 struct diff_dir_cb_arg *a = arg;
2277 char *path = NULL;
2279 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2280 return got_error(GOT_ERR_CANCELLED);
2282 if (de->d_type == DT_DIR)
2283 return NULL;
2285 /* XXX ignore symlinks for now */
2286 if (de->d_type == DT_LNK)
2287 return NULL;
2289 if (parent_path[0]) {
2290 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2291 return got_error_from_errno("asprintf");
2292 } else {
2293 path = de->d_name;
2296 if (got_path_is_child(path, a->status_path, a->status_path_len))
2297 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2298 GOT_STATUS_NO_CHANGE, path, NULL, NULL);
2299 if (parent_path[0])
2300 free(path);
2301 return err;
2304 static const struct got_error *
2305 report_single_file_status(const char *path, const char *ondisk_path,
2306 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2307 void *status_arg, struct got_repository *repo)
2309 struct got_fileindex_entry *ie;
2310 struct stat sb;
2312 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2313 if (ie)
2314 return report_file_status(ie, ondisk_path, status_cb,
2315 status_arg, repo);
2317 if (lstat(ondisk_path, &sb) == -1) {
2318 if (errno != ENOENT)
2319 return got_error_from_errno2("lstat", ondisk_path);
2320 return NULL;
2323 if (S_ISREG(sb.st_mode))
2324 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2325 GOT_STATUS_NO_CHANGE, path, NULL, NULL);
2327 return NULL;
2330 static const struct got_error *
2331 worktree_status(struct got_worktree *worktree, const char *path,
2332 struct got_fileindex *fileindex, struct got_repository *repo,
2333 got_worktree_status_cb status_cb, void *status_arg,
2334 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2336 const struct got_error *err = NULL;
2337 DIR *workdir = NULL;
2338 struct got_fileindex_diff_dir_cb fdiff_cb;
2339 struct diff_dir_cb_arg arg;
2340 char *ondisk_path = NULL;
2342 if (asprintf(&ondisk_path, "%s%s%s",
2343 worktree->root_path, path[0] ? "/" : "", path) == -1)
2344 return got_error_from_errno("asprintf");
2346 workdir = opendir(ondisk_path);
2347 if (workdir == NULL) {
2348 if (errno != ENOTDIR && errno != ENOENT)
2349 err = got_error_from_errno2("opendir", ondisk_path);
2350 else
2351 err = report_single_file_status(path, ondisk_path,
2352 fileindex, status_cb, status_arg, repo);
2353 } else {
2354 fdiff_cb.diff_old_new = status_old_new;
2355 fdiff_cb.diff_old = status_old;
2356 fdiff_cb.diff_new = status_new;
2357 arg.fileindex = fileindex;
2358 arg.worktree = worktree;
2359 arg.status_path = path;
2360 arg.status_path_len = strlen(path);
2361 arg.repo = repo;
2362 arg.status_cb = status_cb;
2363 arg.status_arg = status_arg;
2364 arg.cancel_cb = cancel_cb;
2365 arg.cancel_arg = cancel_arg;
2366 err = got_fileindex_diff_dir(fileindex, workdir,
2367 worktree->root_path, path, repo, &fdiff_cb, &arg);
2370 if (workdir)
2371 closedir(workdir);
2372 free(ondisk_path);
2373 return err;
2376 const struct got_error *
2377 got_worktree_status(struct got_worktree *worktree,
2378 struct got_pathlist_head *paths, struct got_repository *repo,
2379 got_worktree_status_cb status_cb, void *status_arg,
2380 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2382 const struct got_error *err = NULL;
2383 char *fileindex_path = NULL;
2384 struct got_fileindex *fileindex = NULL;
2385 struct got_pathlist_entry *pe;
2387 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2388 if (err)
2389 return err;
2391 TAILQ_FOREACH(pe, paths, entry) {
2392 err = worktree_status(worktree, pe->path, fileindex, repo,
2393 status_cb, status_arg, cancel_cb, cancel_arg);
2394 if (err)
2395 break;
2397 free(fileindex_path);
2398 got_fileindex_free(fileindex);
2399 return err;
2402 const struct got_error *
2403 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2404 const char *arg)
2406 const struct got_error *err = NULL;
2407 char *resolved, *cwd = NULL, *path = NULL;
2408 size_t len;
2410 *wt_path = NULL;
2412 resolved = realpath(arg, NULL);
2413 if (resolved == NULL) {
2414 if (errno != ENOENT)
2415 return got_error_from_errno2("realpath", arg);
2416 cwd = getcwd(NULL, 0);
2417 if (cwd == NULL)
2418 return got_error_from_errno("getcwd");
2419 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2420 err = got_error_from_errno("asprintf");
2421 goto done;
2425 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2426 strlen(got_worktree_get_root_path(worktree)))) {
2427 err = got_error(GOT_ERR_BAD_PATH);
2428 goto done;
2431 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2432 err = got_path_skip_common_ancestor(&path,
2433 got_worktree_get_root_path(worktree), resolved);
2434 if (err)
2435 goto done;
2436 } else {
2437 path = strdup("");
2438 if (path == NULL) {
2439 err = got_error_from_errno("strdup");
2440 goto done;
2444 /* XXX status walk can't deal with trailing slash! */
2445 len = strlen(path);
2446 while (len > 0 && path[len - 1] == '/') {
2447 path[len - 1] = '\0';
2448 len--;
2450 done:
2451 free(resolved);
2452 free(cwd);
2453 if (err == NULL)
2454 *wt_path = path;
2455 else
2456 free(path);
2457 return err;
2460 static const struct got_error *
2461 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2462 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2463 struct got_repository *repo)
2465 const struct got_error *err = NULL;
2466 struct got_fileindex_entry *ie;
2468 /* Re-adding an existing entry is a no-op. */
2469 if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2470 return NULL;
2472 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2473 if (err)
2474 return err;
2476 err = got_fileindex_entry_add(fileindex, ie);
2477 if (err) {
2478 got_fileindex_entry_free(ie);
2479 return err;
2482 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2485 const struct got_error *
2486 got_worktree_schedule_add(struct got_worktree *worktree,
2487 struct got_pathlist_head *ondisk_paths,
2488 got_worktree_status_cb status_cb, void *status_arg,
2489 struct got_repository *repo)
2491 struct got_fileindex *fileindex = NULL;
2492 char *fileindex_path = NULL;
2493 const struct got_error *err = NULL, *sync_err, *unlockerr;
2494 struct got_pathlist_entry *pe;
2496 err = lock_worktree(worktree, LOCK_EX);
2497 if (err)
2498 return err;
2500 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2501 if (err)
2502 goto done;
2504 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2505 char *relpath;
2506 err = got_path_skip_common_ancestor(&relpath,
2507 got_worktree_get_root_path(worktree), pe->path);
2508 if (err)
2509 break;
2510 err = schedule_addition(pe->path, fileindex, relpath,
2511 status_cb, status_arg, repo);
2512 free(relpath);
2513 if (err)
2514 break;
2516 sync_err = sync_fileindex(fileindex, fileindex_path);
2517 if (sync_err && err == NULL)
2518 err = sync_err;
2519 done:
2520 free(fileindex_path);
2521 if (fileindex)
2522 got_fileindex_free(fileindex);
2523 unlockerr = lock_worktree(worktree, LOCK_SH);
2524 if (unlockerr && err == NULL)
2525 err = unlockerr;
2526 return err;
2529 static const struct got_error *
2530 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2531 const char *relpath, int delete_local_mods,
2532 got_worktree_status_cb status_cb, void *status_arg,
2533 struct got_repository *repo)
2535 const struct got_error *err = NULL;
2536 struct got_fileindex_entry *ie = NULL;
2537 unsigned char status;
2538 struct stat sb;
2540 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2541 if (ie == NULL)
2542 return got_error(GOT_ERR_BAD_PATH);
2544 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2545 if (err)
2546 return err;
2548 if (status != GOT_STATUS_NO_CHANGE) {
2549 if (status == GOT_STATUS_DELETE)
2550 return NULL;
2551 if (status != GOT_STATUS_MODIFY)
2552 return got_error(GOT_ERR_FILE_STATUS);
2553 if (!delete_local_mods)
2554 return got_error(GOT_ERR_FILE_MODIFIED);
2557 if (unlink(ondisk_path) != 0)
2558 return got_error_from_errno2("unlink", ondisk_path);
2560 got_fileindex_entry_mark_deleted_from_disk(ie);
2561 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2564 const struct got_error *
2565 got_worktree_schedule_delete(struct got_worktree *worktree,
2566 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2567 got_worktree_status_cb status_cb, void *status_arg,
2568 struct got_repository *repo)
2570 struct got_fileindex *fileindex = NULL;
2571 char *fileindex_path = NULL;
2572 const struct got_error *err = NULL, *sync_err, *unlockerr;
2573 struct got_pathlist_entry *pe;
2575 err = lock_worktree(worktree, LOCK_EX);
2576 if (err)
2577 return err;
2579 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2580 if (err)
2581 goto done;
2583 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2584 char *relpath;
2585 err = got_path_skip_common_ancestor(&relpath,
2586 got_worktree_get_root_path(worktree), pe->path);
2587 if (err)
2588 break;
2589 err = schedule_for_deletion(pe->path, fileindex, relpath,
2590 delete_local_mods, status_cb, status_arg, repo);
2591 free(relpath);
2592 if (err)
2593 break;
2595 sync_err = sync_fileindex(fileindex, fileindex_path);
2596 if (sync_err && err == NULL)
2597 err = sync_err;
2598 done:
2599 free(fileindex_path);
2600 if (fileindex)
2601 got_fileindex_free(fileindex);
2602 unlockerr = lock_worktree(worktree, LOCK_SH);
2603 if (unlockerr && err == NULL)
2604 err = unlockerr;
2605 return err;
2608 static const struct got_error *
2609 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2610 const char *ondisk_path,
2611 got_worktree_checkout_cb progress_cb, void *progress_arg,
2612 struct got_repository *repo)
2614 const struct got_error *err = NULL;
2615 char *relpath = NULL, *parent_path = NULL;
2616 struct got_fileindex_entry *ie;
2617 struct got_tree_object *tree = NULL;
2618 struct got_object_id *tree_id = NULL;
2619 const struct got_tree_entry *te;
2620 char *tree_path = NULL, *te_name;
2621 struct got_blob_object *blob = NULL;
2622 unsigned char status;
2623 struct stat sb;
2625 err = got_path_skip_common_ancestor(&relpath,
2626 got_worktree_get_root_path(worktree), ondisk_path);
2627 if (err)
2628 goto done;
2630 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2631 if (ie == NULL) {
2632 err = got_error(GOT_ERR_BAD_PATH);
2633 goto done;
2636 /* Construct in-repository path of tree which contains this blob. */
2637 err = got_path_dirname(&parent_path, ie->path);
2638 if (err) {
2639 if (err->code != GOT_ERR_BAD_PATH)
2640 goto done;
2641 parent_path = strdup("/");
2642 if (parent_path == NULL) {
2643 err = got_error_from_errno("strdup");
2644 goto done;
2647 if (got_path_is_root_dir(worktree->path_prefix)) {
2648 tree_path = strdup(parent_path);
2649 if (tree_path == NULL) {
2650 err = got_error_from_errno("strdup");
2651 goto done;
2653 } else {
2654 if (got_path_is_root_dir(parent_path)) {
2655 tree_path = strdup(worktree->path_prefix);
2656 if (tree_path == NULL) {
2657 err = got_error_from_errno("strdup");
2658 goto done;
2660 } else {
2661 if (asprintf(&tree_path, "%s/%s",
2662 worktree->path_prefix, parent_path) == -1) {
2663 err = got_error_from_errno("asprintf");
2664 goto done;
2669 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2670 if (err)
2671 goto done;
2672 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2673 sb.st_mode = got_fileindex_perms_to_st(ie);
2675 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2676 tree_path);
2677 if (err) {
2678 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2679 status == GOT_STATUS_ADD))
2680 goto done;
2681 } else {
2682 err = got_object_open_as_tree(&tree, repo, tree_id);
2683 if (err)
2684 goto done;
2686 te_name = basename(ie->path);
2687 if (te_name == NULL) {
2688 err = got_error_from_errno2("basename", ie->path);
2689 goto done;
2692 te = got_object_tree_find_entry(tree, te_name);
2693 if (te == NULL && status != GOT_STATUS_ADD) {
2694 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2695 goto done;
2699 switch (status) {
2700 case GOT_STATUS_ADD:
2701 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2702 if (err)
2703 goto done;
2704 got_fileindex_entry_remove(fileindex, ie);
2705 break;
2706 case GOT_STATUS_DELETE:
2707 case GOT_STATUS_MODIFY:
2708 case GOT_STATUS_CONFLICT:
2709 case GOT_STATUS_MISSING: {
2710 struct got_object_id id;
2711 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2712 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2713 if (err)
2714 goto done;
2715 err = install_blob(worktree, ondisk_path, ie->path,
2716 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2717 progress_arg);
2718 if (err)
2719 goto done;
2720 if (status == GOT_STATUS_DELETE) {
2721 err = update_blob_fileindex_entry(worktree,
2722 fileindex, ie, ondisk_path, ie->path, blob, 1);
2723 if (err)
2724 goto done;
2726 break;
2728 default:
2729 goto done;
2731 done:
2732 free(relpath);
2733 free(parent_path);
2734 free(tree_path);
2735 if (blob)
2736 got_object_blob_close(blob);
2737 if (tree)
2738 got_object_tree_close(tree);
2739 free(tree_id);
2740 return err;
2743 const struct got_error *
2744 got_worktree_revert(struct got_worktree *worktree,
2745 struct got_pathlist_head *ondisk_paths,
2746 got_worktree_checkout_cb progress_cb, void *progress_arg,
2747 struct got_repository *repo)
2749 struct got_fileindex *fileindex = NULL;
2750 char *fileindex_path = NULL;
2751 const struct got_error *err = NULL, *unlockerr = NULL;
2752 const struct got_error *sync_err = NULL;
2753 struct got_pathlist_entry *pe;
2755 err = lock_worktree(worktree, LOCK_EX);
2756 if (err)
2757 return err;
2759 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2760 if (err)
2761 goto done;
2763 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2764 err = revert_file(worktree, fileindex, pe->path,
2765 progress_cb, progress_arg, repo);
2766 if (err)
2767 break;
2769 sync_err = sync_fileindex(fileindex, fileindex_path);
2770 if (sync_err && err == NULL)
2771 err = sync_err;
2772 done:
2773 free(fileindex_path);
2774 if (fileindex)
2775 got_fileindex_free(fileindex);
2776 unlockerr = lock_worktree(worktree, LOCK_SH);
2777 if (unlockerr && err == NULL)
2778 err = unlockerr;
2779 return err;
2782 static void
2783 free_commitable(struct got_commitable *ct)
2785 free(ct->path);
2786 free(ct->in_repo_path);
2787 free(ct->ondisk_path);
2788 free(ct->blob_id);
2789 free(ct->base_blob_id);
2790 free(ct->base_commit_id);
2791 free(ct);
2794 struct collect_commitables_arg {
2795 struct got_pathlist_head *commitable_paths;
2796 struct got_repository *repo;
2797 struct got_worktree *worktree;
2800 static const struct got_error *
2801 collect_commitables(void *arg, unsigned char status,
2802 unsigned char staged_status, const char *relpath,
2803 struct got_object_id *blob_id, struct got_object_id *commit_id)
2805 struct collect_commitables_arg *a = arg;
2806 const struct got_error *err = NULL;
2807 struct got_commitable *ct = NULL;
2808 struct got_pathlist_entry *new = NULL;
2809 char *parent_path = NULL, *path = NULL;
2810 struct stat sb;
2812 if (status == GOT_STATUS_CONFLICT)
2813 return got_error(GOT_ERR_COMMIT_CONFLICT);
2815 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2816 status != GOT_STATUS_DELETE)
2817 return NULL;
2819 if (asprintf(&path, "/%s", relpath) == -1) {
2820 err = got_error_from_errno("asprintf");
2821 goto done;
2823 if (strcmp(path, "/") == 0) {
2824 parent_path = strdup("");
2825 if (parent_path == NULL)
2826 return got_error_from_errno("strdup");
2827 } else {
2828 err = got_path_dirname(&parent_path, path);
2829 if (err)
2830 return err;
2833 ct = calloc(1, sizeof(*ct));
2834 if (ct == NULL) {
2835 err = got_error_from_errno("calloc");
2836 goto done;
2839 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2840 relpath) == -1) {
2841 err = got_error_from_errno("asprintf");
2842 goto done;
2844 if (status == GOT_STATUS_DELETE) {
2845 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2846 } else {
2847 if (lstat(ct->ondisk_path, &sb) != 0) {
2848 err = got_error_from_errno2("lstat", ct->ondisk_path);
2849 goto done;
2851 ct->mode = sb.st_mode;
2854 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2855 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2856 relpath) == -1) {
2857 err = got_error_from_errno("asprintf");
2858 goto done;
2861 ct->status = status;
2862 ct->blob_id = NULL; /* will be filled in when blob gets created */
2863 if (ct->status != GOT_STATUS_ADD) {
2864 ct->base_blob_id = got_object_id_dup(blob_id);
2865 if (ct->base_blob_id == NULL) {
2866 err = got_error_from_errno("got_object_id_dup");
2867 goto done;
2869 ct->base_commit_id = got_object_id_dup(commit_id);
2870 if (ct->base_commit_id == NULL) {
2871 err = got_error_from_errno("got_object_id_dup");
2872 goto done;
2875 ct->path = strdup(path);
2876 if (ct->path == NULL) {
2877 err = got_error_from_errno("strdup");
2878 goto done;
2880 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2881 done:
2882 if (ct && (err || new == NULL))
2883 free_commitable(ct);
2884 free(parent_path);
2885 free(path);
2886 return err;
2889 static const struct got_error *write_tree(struct got_object_id **,
2890 struct got_tree_object *, const char *, struct got_pathlist_head *,
2891 got_worktree_status_cb status_cb, void *status_arg,
2892 struct got_repository *);
2894 static const struct got_error *
2895 write_subtree(struct got_object_id **new_subtree_id,
2896 struct got_tree_entry *te, const char *parent_path,
2897 struct got_pathlist_head *commitable_paths,
2898 got_worktree_status_cb status_cb, void *status_arg,
2899 struct got_repository *repo)
2901 const struct got_error *err = NULL;
2902 struct got_tree_object *subtree;
2903 char *subpath;
2905 if (asprintf(&subpath, "%s%s%s", parent_path,
2906 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2907 return got_error_from_errno("asprintf");
2909 err = got_object_open_as_tree(&subtree, repo, te->id);
2910 if (err)
2911 return err;
2913 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2914 status_cb, status_arg, repo);
2915 got_object_tree_close(subtree);
2916 free(subpath);
2917 return err;
2920 static const struct got_error *
2921 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2923 const struct got_error *err = NULL;
2924 char *ct_parent_path = NULL;
2926 *match = 0;
2928 if (strchr(ct->in_repo_path, '/') == NULL) {
2929 *match = got_path_is_root_dir(path);
2930 return NULL;
2933 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2934 if (err)
2935 return err;
2936 *match = (strcmp(path, ct_parent_path) == 0);
2937 free(ct_parent_path);
2938 return err;
2941 static mode_t
2942 get_ct_file_mode(struct got_commitable *ct)
2944 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2947 static const struct got_error *
2948 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2949 struct got_tree_entry *te, struct got_commitable *ct)
2951 const struct got_error *err = NULL;
2953 *new_te = NULL;
2955 err = got_object_tree_entry_dup(new_te, te);
2956 if (err)
2957 goto done;
2959 (*new_te)->mode = get_ct_file_mode(ct);
2961 free((*new_te)->id);
2962 (*new_te)->id = got_object_id_dup(ct->blob_id);
2963 if ((*new_te)->id == NULL) {
2964 err = got_error_from_errno("got_object_id_dup");
2965 goto done;
2967 done:
2968 if (err && *new_te) {
2969 got_object_tree_entry_close(*new_te);
2970 *new_te = NULL;
2972 return err;
2975 static const struct got_error *
2976 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2977 struct got_commitable *ct)
2979 const struct got_error *err = NULL;
2980 char *ct_name;
2982 *new_te = NULL;
2984 *new_te = calloc(1, sizeof(**new_te));
2985 if (*new_te == NULL)
2986 return got_error_from_errno("calloc");
2988 ct_name = basename(ct->path);
2989 if (ct_name == NULL) {
2990 err = got_error_from_errno2("basename", ct->path);
2991 goto done;
2993 (*new_te)->name = strdup(ct_name);
2994 if ((*new_te)->name == NULL) {
2995 err = got_error_from_errno("strdup");
2996 goto done;
2999 (*new_te)->mode = get_ct_file_mode(ct);
3001 (*new_te)->id = got_object_id_dup(ct->blob_id);
3002 if ((*new_te)->id == NULL) {
3003 err = got_error_from_errno("got_object_id_dup");
3004 goto done;
3006 done:
3007 if (err && *new_te) {
3008 got_object_tree_entry_close(*new_te);
3009 *new_te = NULL;
3011 return err;
3014 static const struct got_error *
3015 insert_tree_entry(struct got_tree_entry *new_te,
3016 struct got_pathlist_head *paths)
3018 const struct got_error *err = NULL;
3019 struct got_pathlist_entry *new_pe;
3021 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3022 if (err)
3023 return err;
3024 if (new_pe == NULL)
3025 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3026 return NULL;
3029 static const struct got_error *
3030 report_ct_status(struct got_commitable *ct,
3031 got_worktree_status_cb status_cb, void *status_arg)
3033 const char *ct_path = ct->path;
3034 while (ct_path[0] == '/')
3035 ct_path++;
3036 return (*status_cb)(status_arg, ct->status, GOT_STATUS_NO_CHANGE,
3037 ct_path, ct->blob_id, NULL);
3040 static const struct got_error *
3041 match_modified_subtree(int *modified, struct got_tree_entry *te,
3042 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3044 const struct got_error *err = NULL;
3045 struct got_pathlist_entry *pe;
3046 char *te_path;
3048 *modified = 0;
3050 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3051 got_path_is_root_dir(base_tree_path) ? "" : "/",
3052 te->name) == -1)
3053 return got_error_from_errno("asprintf");
3055 TAILQ_FOREACH(pe, commitable_paths, entry) {
3056 struct got_commitable *ct = pe->data;
3057 *modified = got_path_is_child(ct->in_repo_path, te_path,
3058 strlen(te_path));
3059 if (*modified)
3060 break;
3063 free(te_path);
3064 return err;
3067 static const struct got_error *
3068 match_deleted_or_modified_ct(struct got_commitable **ctp,
3069 struct got_tree_entry *te, const char *base_tree_path,
3070 struct got_pathlist_head *commitable_paths)
3072 const struct got_error *err = NULL;
3073 struct got_pathlist_entry *pe;
3075 *ctp = NULL;
3077 TAILQ_FOREACH(pe, commitable_paths, entry) {
3078 struct got_commitable *ct = pe->data;
3079 char *ct_name = NULL;
3080 int path_matches;
3082 if (ct->status != GOT_STATUS_MODIFY &&
3083 ct->status != GOT_STATUS_DELETE)
3084 continue;
3086 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3087 continue;
3089 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3090 if (err)
3091 return err;
3092 if (!path_matches)
3093 continue;
3095 ct_name = basename(pe->path);
3096 if (ct_name == NULL)
3097 return got_error_from_errno2("basename", pe->path);
3099 if (strcmp(te->name, ct_name) != 0)
3100 continue;
3102 *ctp = ct;
3103 break;
3106 return err;
3109 static const struct got_error *
3110 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3111 const char *child_path, const char *path_base_tree,
3112 struct got_pathlist_head *commitable_paths,
3113 got_worktree_status_cb status_cb, void *status_arg,
3114 struct got_repository *repo)
3116 const struct got_error *err = NULL;
3117 struct got_tree_entry *new_te;
3118 char *subtree_path;
3120 *new_tep = NULL;
3122 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3123 got_path_is_root_dir(path_base_tree) ? "" : "/",
3124 child_path) == -1)
3125 return got_error_from_errno("asprintf");
3127 new_te = calloc(1, sizeof(*new_te));
3128 new_te->mode = S_IFDIR;
3129 new_te->name = strdup(child_path);
3130 if (new_te->name == NULL) {
3131 err = got_error_from_errno("strdup");
3132 got_object_tree_entry_close(new_te);
3133 goto done;
3135 err = write_tree(&new_te->id, NULL, subtree_path,
3136 commitable_paths, status_cb, status_arg, repo);
3137 if (err) {
3138 got_object_tree_entry_close(new_te);
3139 goto done;
3141 done:
3142 free(subtree_path);
3143 if (err == NULL)
3144 *new_tep = new_te;
3145 return err;
3148 static const struct got_error *
3149 write_tree(struct got_object_id **new_tree_id,
3150 struct got_tree_object *base_tree, const char *path_base_tree,
3151 struct got_pathlist_head *commitable_paths,
3152 got_worktree_status_cb status_cb, void *status_arg,
3153 struct got_repository *repo)
3155 const struct got_error *err = NULL;
3156 const struct got_tree_entries *base_entries = NULL;
3157 struct got_pathlist_head paths;
3158 struct got_tree_entries new_tree_entries;
3159 struct got_tree_entry *te, *new_te = NULL;
3160 struct got_pathlist_entry *pe;
3162 TAILQ_INIT(&paths);
3163 new_tree_entries.nentries = 0;
3164 SIMPLEQ_INIT(&new_tree_entries.head);
3166 /* Insert, and recurse into, newly added entries first. */
3167 TAILQ_FOREACH(pe, commitable_paths, entry) {
3168 struct got_commitable *ct = pe->data;
3169 char *child_path = NULL, *slash;
3171 if (ct->status != GOT_STATUS_ADD ||
3172 (ct->flags & GOT_COMMITABLE_ADDED))
3173 continue;
3175 if (!got_path_is_child(pe->path, path_base_tree,
3176 strlen(path_base_tree)))
3177 continue;
3179 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3180 pe->path);
3181 if (err)
3182 goto done;
3184 slash = strchr(child_path, '/');
3185 if (slash == NULL) {
3186 err = alloc_added_blob_tree_entry(&new_te, ct);
3187 if (err)
3188 goto done;
3189 err = report_ct_status(ct, status_cb, status_arg);
3190 if (err)
3191 goto done;
3192 ct->flags |= GOT_COMMITABLE_ADDED;
3193 err = insert_tree_entry(new_te, &paths);
3194 if (err)
3195 goto done;
3196 } else {
3197 *slash = '\0'; /* trim trailing path components */
3198 if (base_tree == NULL ||
3199 got_object_tree_find_entry(base_tree, child_path)
3200 == NULL) {
3201 err = make_subtree_for_added_blob(&new_te,
3202 child_path, path_base_tree,
3203 commitable_paths, status_cb, status_arg,
3204 repo);
3205 if (err)
3206 goto done;
3207 err = insert_tree_entry(new_te, &paths);
3208 if (err)
3209 goto done;
3214 if (base_tree) {
3215 /* Handle modified and deleted entries. */
3216 base_entries = got_object_tree_get_entries(base_tree);
3217 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3218 struct got_commitable *ct = NULL;
3220 if (S_ISDIR(te->mode)) {
3221 int modified;
3222 err = got_object_tree_entry_dup(&new_te, te);
3223 if (err)
3224 goto done;
3225 err = match_modified_subtree(&modified, te,
3226 path_base_tree, commitable_paths);
3227 if (err)
3228 goto done;
3229 /* Avoid recursion into unmodified subtrees. */
3230 if (modified) {
3231 free(new_te->id);
3232 err = write_subtree(&new_te->id, te,
3233 path_base_tree, commitable_paths,
3234 status_cb, status_arg, repo);
3235 if (err)
3236 goto done;
3238 err = insert_tree_entry(new_te, &paths);
3239 if (err)
3240 goto done;
3241 continue;
3244 err = match_deleted_or_modified_ct(&ct, te,
3245 path_base_tree, commitable_paths);
3246 if (ct) {
3247 /* NB: Deleted entries get dropped here. */
3248 if (ct->status == GOT_STATUS_MODIFY) {
3249 err = alloc_modified_blob_tree_entry(
3250 &new_te, te, ct);
3251 if (err)
3252 goto done;
3253 err = insert_tree_entry(new_te, &paths);
3254 if (err)
3255 goto done;
3257 err = report_ct_status(ct, status_cb,
3258 status_arg);
3259 if (err)
3260 goto done;
3261 } else {
3262 /* Entry is unchanged; just copy it. */
3263 err = got_object_tree_entry_dup(&new_te, te);
3264 if (err)
3265 goto done;
3266 err = insert_tree_entry(new_te, &paths);
3267 if (err)
3268 goto done;
3273 /* Write new list of entries; deleted entries have been dropped. */
3274 TAILQ_FOREACH(pe, &paths, entry) {
3275 struct got_tree_entry *te = pe->data;
3276 new_tree_entries.nentries++;
3277 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3279 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3280 done:
3281 got_object_tree_entries_close(&new_tree_entries);
3282 got_pathlist_free(&paths);
3283 return err;
3286 static const struct got_error *
3287 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3288 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3290 const struct got_error *err = NULL;
3291 struct got_pathlist_entry *pe;
3293 TAILQ_FOREACH(pe, commitable_paths, entry) {
3294 struct got_fileindex_entry *ie;
3295 struct got_commitable *ct = pe->data;
3297 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3298 if (ie) {
3299 if (ct->status == GOT_STATUS_DELETE) {
3300 got_fileindex_entry_remove(fileindex, ie);
3301 got_fileindex_entry_free(ie);
3302 } else
3303 err = got_fileindex_entry_update(ie,
3304 ct->ondisk_path, ct->blob_id->sha1,
3305 new_base_commit_id->sha1, 1);
3306 } else {
3307 err = got_fileindex_entry_alloc(&ie,
3308 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3309 new_base_commit_id->sha1);
3310 if (err)
3311 break;
3312 err = got_fileindex_entry_add(fileindex, ie);
3313 if (err)
3314 break;
3317 return err;
3320 static const struct got_error *
3321 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3322 struct got_object_id *head_commit_id)
3324 const struct got_error *err = NULL;
3325 struct got_object_id *id = NULL;
3326 struct got_commit_object *commit = NULL;
3327 const char *ct_path = ct->in_repo_path;
3329 while (ct_path[0] == '/')
3330 ct_path++;
3332 if (ct->status != GOT_STATUS_ADD) {
3333 /* Trivial case: base commit == head commit */
3334 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3335 return NULL;
3337 * Ensure file content which local changes were based
3338 * on matches file content in the branch head.
3340 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3341 if (err) {
3342 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3343 goto done;
3344 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3345 goto done;
3346 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3347 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3348 } else {
3349 /* Require that added files don't exist in the branch head. */
3350 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3351 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3352 goto done;
3353 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3355 done:
3356 if (commit)
3357 got_object_commit_close(commit);
3358 free(id);
3359 return err;
3362 const struct got_error *
3363 commit_worktree(struct got_object_id **new_commit_id,
3364 struct got_pathlist_head *commitable_paths,
3365 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3366 const char *author, const char *committer,
3367 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3368 got_worktree_status_cb status_cb, void *status_arg,
3369 struct got_repository *repo)
3371 const struct got_error *err = NULL, *unlockerr = NULL;
3372 struct got_pathlist_entry *pe;
3373 const char *head_ref_name = NULL;
3374 struct got_commit_object *head_commit = NULL;
3375 struct got_reference *head_ref2 = NULL;
3376 struct got_object_id *head_commit_id2 = NULL;
3377 struct got_tree_object *head_tree = NULL;
3378 struct got_object_id *new_tree_id = NULL;
3379 struct got_object_id_queue parent_ids;
3380 struct got_object_qid *pid = NULL;
3381 char *logmsg = NULL;
3383 *new_commit_id = NULL;
3385 SIMPLEQ_INIT(&parent_ids);
3387 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3388 if (err)
3389 goto done;
3391 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3392 if (err)
3393 goto done;
3395 if (commit_msg_cb != NULL) {
3396 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3397 if (err)
3398 goto done;
3401 if (logmsg == NULL || strlen(logmsg) == 0) {
3402 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3403 goto done;
3406 /* Create blobs from added and modified files and record their IDs. */
3407 TAILQ_FOREACH(pe, commitable_paths, entry) {
3408 struct got_commitable *ct = pe->data;
3409 char *ondisk_path;
3411 if (ct->status != GOT_STATUS_ADD &&
3412 ct->status != GOT_STATUS_MODIFY)
3413 continue;
3415 if (asprintf(&ondisk_path, "%s/%s",
3416 worktree->root_path, pe->path) == -1) {
3417 err = got_error_from_errno("asprintf");
3418 goto done;
3420 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3421 free(ondisk_path);
3422 if (err)
3423 goto done;
3426 /* Recursively write new tree objects. */
3427 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3428 status_cb, status_arg, repo);
3429 if (err)
3430 goto done;
3432 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3433 if (err)
3434 goto done;
3435 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3436 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3437 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3438 got_object_qid_free(pid);
3439 if (logmsg != NULL)
3440 free(logmsg);
3441 if (err)
3442 goto done;
3444 /* Check if a concurrent commit to our branch has occurred. */
3445 head_ref_name = got_worktree_get_head_ref_name(worktree);
3446 if (head_ref_name == NULL) {
3447 err = got_error_from_errno("got_worktree_get_head_ref_name");
3448 goto done;
3450 /* Lock the reference here to prevent concurrent modification. */
3451 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3452 if (err)
3453 goto done;
3454 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3455 if (err)
3456 goto done;
3457 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3458 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3459 goto done;
3461 /* Update branch head in repository. */
3462 err = got_ref_change_ref(head_ref2, *new_commit_id);
3463 if (err)
3464 goto done;
3465 err = got_ref_write(head_ref2, repo);
3466 if (err)
3467 goto done;
3469 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3470 if (err)
3471 goto done;
3473 err = ref_base_commit(worktree, repo);
3474 if (err)
3475 goto done;
3476 done:
3477 if (head_tree)
3478 got_object_tree_close(head_tree);
3479 if (head_commit)
3480 got_object_commit_close(head_commit);
3481 free(head_commit_id2);
3482 if (head_ref2) {
3483 unlockerr = got_ref_unlock(head_ref2);
3484 if (unlockerr && err == NULL)
3485 err = unlockerr;
3486 got_ref_close(head_ref2);
3488 return err;
3491 static const struct got_error *
3492 check_path_is_commitable(const char *path,
3493 struct got_pathlist_head *commitable_paths)
3495 struct got_pathlist_entry *cpe = NULL;
3496 size_t path_len = strlen(path);
3498 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3499 struct got_commitable *ct = cpe->data;
3500 const char *ct_path = ct->path;
3502 while (ct_path[0] == '/')
3503 ct_path++;
3505 if (strcmp(path, ct_path) == 0 ||
3506 got_path_is_child(ct_path, path, path_len))
3507 break;
3510 if (cpe == NULL)
3511 return got_error_path(path, GOT_ERR_BAD_PATH);
3513 return NULL;
3516 const struct got_error *
3517 got_worktree_commit(struct got_object_id **new_commit_id,
3518 struct got_worktree *worktree, struct got_pathlist_head *paths,
3519 const char *author, const char *committer,
3520 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3521 got_worktree_status_cb status_cb, void *status_arg,
3522 struct got_repository *repo)
3524 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3525 struct got_fileindex *fileindex = NULL;
3526 char *fileindex_path = NULL;
3527 struct got_pathlist_head commitable_paths;
3528 struct collect_commitables_arg cc_arg;
3529 struct got_pathlist_entry *pe;
3530 struct got_reference *head_ref = NULL;
3531 struct got_object_id *head_commit_id = NULL;
3533 *new_commit_id = NULL;
3535 TAILQ_INIT(&commitable_paths);
3537 err = lock_worktree(worktree, LOCK_EX);
3538 if (err)
3539 goto done;
3541 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3542 if (err)
3543 goto done;
3545 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3546 if (err)
3547 goto done;
3549 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3550 if (err)
3551 goto done;
3553 cc_arg.commitable_paths = &commitable_paths;
3554 cc_arg.worktree = worktree;
3555 cc_arg.repo = repo;
3556 TAILQ_FOREACH(pe, paths, entry) {
3557 err = worktree_status(worktree, pe->path, fileindex, repo,
3558 collect_commitables, &cc_arg, NULL, NULL);
3559 if (err)
3560 goto done;
3563 if (TAILQ_EMPTY(&commitable_paths)) {
3564 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3565 goto done;
3568 TAILQ_FOREACH(pe, paths, entry) {
3569 err = check_path_is_commitable(pe->path, &commitable_paths);
3570 if (err)
3571 goto done;
3574 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3575 struct got_commitable *ct = pe->data;
3576 err = check_ct_out_of_date(ct, repo, head_commit_id);
3577 if (err)
3578 goto done;
3581 err = commit_worktree(new_commit_id, &commitable_paths,
3582 head_commit_id, worktree, author, committer,
3583 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3584 if (err)
3585 goto done;
3587 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3588 fileindex);
3589 sync_err = sync_fileindex(fileindex, fileindex_path);
3590 if (sync_err && err == NULL)
3591 err = sync_err;
3592 done:
3593 if (fileindex)
3594 got_fileindex_free(fileindex);
3595 free(fileindex_path);
3596 unlockerr = lock_worktree(worktree, LOCK_SH);
3597 if (unlockerr && err == NULL)
3598 err = unlockerr;
3599 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3600 struct got_commitable *ct = pe->data;
3601 free_commitable(ct);
3603 got_pathlist_free(&commitable_paths);
3604 return err;
3607 const char *
3608 got_commitable_get_path(struct got_commitable *ct)
3610 return ct->path;
3613 unsigned int
3614 got_commitable_get_status(struct got_commitable *ct)
3616 return ct->status;
3619 struct check_rebase_ok_arg {
3620 struct got_worktree *worktree;
3621 struct got_repository *repo;
3624 static const struct got_error *
3625 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3627 const struct got_error *err = NULL;
3628 struct check_rebase_ok_arg *a = arg;
3629 unsigned char status;
3630 struct stat sb;
3631 char *ondisk_path;
3633 /* Reject rebase of a work tree with mixed base commits. */
3634 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3635 SHA1_DIGEST_LENGTH))
3636 return got_error(GOT_ERR_MIXED_COMMITS);
3638 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3639 == -1)
3640 return got_error_from_errno("asprintf");
3642 /* Reject rebase of a work tree with modified or conflicted files. */
3643 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3644 free(ondisk_path);
3645 if (err)
3646 return err;
3648 if (status != GOT_STATUS_NO_CHANGE)
3649 return got_error(GOT_ERR_MODIFIED);
3651 return NULL;
3654 const struct got_error *
3655 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3656 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3657 struct got_worktree *worktree, struct got_reference *branch,
3658 struct got_repository *repo)
3660 const struct got_error *err = NULL;
3661 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3662 char *branch_ref_name = NULL;
3663 char *fileindex_path = NULL;
3664 struct check_rebase_ok_arg ok_arg;
3665 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3667 *new_base_branch_ref = NULL;
3668 *tmp_branch = NULL;
3669 *fileindex = NULL;
3671 err = lock_worktree(worktree, LOCK_EX);
3672 if (err)
3673 return err;
3675 err = open_fileindex(fileindex, &fileindex_path, worktree);
3676 if (err)
3677 goto done;
3679 ok_arg.worktree = worktree;
3680 ok_arg.repo = repo;
3681 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3682 &ok_arg);
3683 if (err)
3684 goto done;
3686 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3687 if (err)
3688 goto done;
3690 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3691 if (err)
3692 goto done;
3694 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3695 if (err)
3696 goto done;
3698 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3699 0);
3700 if (err)
3701 goto done;
3703 err = got_ref_alloc_symref(new_base_branch_ref,
3704 new_base_branch_ref_name, wt_branch);
3705 if (err)
3706 goto done;
3707 err = got_ref_write(*new_base_branch_ref, repo);
3708 if (err)
3709 goto done;
3711 /* TODO Lock original branch's ref while rebasing? */
3713 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3714 if (err)
3715 goto done;
3717 err = got_ref_write(branch_ref, repo);
3718 if (err)
3719 goto done;
3721 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3722 worktree->base_commit_id);
3723 if (err)
3724 goto done;
3725 err = got_ref_write(*tmp_branch, repo);
3726 if (err)
3727 goto done;
3729 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3730 if (err)
3731 goto done;
3732 done:
3733 free(fileindex_path);
3734 free(tmp_branch_name);
3735 free(new_base_branch_ref_name);
3736 free(branch_ref_name);
3737 if (branch_ref)
3738 got_ref_close(branch_ref);
3739 if (wt_branch)
3740 got_ref_close(wt_branch);
3741 if (err) {
3742 if (*new_base_branch_ref) {
3743 got_ref_close(*new_base_branch_ref);
3744 *new_base_branch_ref = NULL;
3746 if (*tmp_branch) {
3747 got_ref_close(*tmp_branch);
3748 *tmp_branch = NULL;
3750 if (*fileindex) {
3751 got_fileindex_free(*fileindex);
3752 *fileindex = NULL;
3754 lock_worktree(worktree, LOCK_SH);
3756 return err;
3759 const struct got_error *
3760 got_worktree_rebase_continue(struct got_object_id **commit_id,
3761 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3762 struct got_reference **branch, struct got_fileindex **fileindex,
3763 struct got_worktree *worktree, struct got_repository *repo)
3765 const struct got_error *err;
3766 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3767 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3768 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3769 char *fileindex_path = NULL;
3771 *commit_id = NULL;
3772 *new_base_branch = NULL;
3773 *tmp_branch = NULL;
3774 *branch = NULL;
3775 *fileindex = NULL;
3777 err = lock_worktree(worktree, LOCK_EX);
3778 if (err)
3779 return err;
3781 err = open_fileindex(fileindex, &fileindex_path, worktree);
3782 if (err)
3783 goto done;
3785 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3786 if (err)
3787 return err;
3789 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3790 if (err)
3791 goto done;
3793 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3794 if (err)
3795 goto done;
3797 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3798 if (err)
3799 goto done;
3801 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3802 if (err)
3803 goto done;
3805 err = got_ref_open(branch, repo,
3806 got_ref_get_symref_target(branch_ref), 0);
3807 if (err)
3808 goto done;
3810 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3811 if (err)
3812 goto done;
3814 err = got_ref_resolve(commit_id, repo, commit_ref);
3815 if (err)
3816 goto done;
3818 err = got_ref_open(new_base_branch, repo,
3819 new_base_branch_ref_name, 0);
3820 if (err)
3821 goto done;
3823 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3824 if (err)
3825 goto done;
3826 done:
3827 free(commit_ref_name);
3828 free(branch_ref_name);
3829 free(fileindex_path);
3830 if (commit_ref)
3831 got_ref_close(commit_ref);
3832 if (branch_ref)
3833 got_ref_close(branch_ref);
3834 if (err) {
3835 free(*commit_id);
3836 *commit_id = NULL;
3837 if (*tmp_branch) {
3838 got_ref_close(*tmp_branch);
3839 *tmp_branch = NULL;
3841 if (*new_base_branch) {
3842 got_ref_close(*new_base_branch);
3843 *new_base_branch = NULL;
3845 if (*branch) {
3846 got_ref_close(*branch);
3847 *branch = NULL;
3849 if (*fileindex) {
3850 got_fileindex_free(*fileindex);
3851 *fileindex = NULL;
3853 lock_worktree(worktree, LOCK_SH);
3855 return err;
3858 const struct got_error *
3859 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3861 const struct got_error *err;
3862 char *tmp_branch_name = NULL;
3864 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3865 if (err)
3866 return err;
3868 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3869 free(tmp_branch_name);
3870 return NULL;
3873 static const struct got_error *
3874 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3875 char **logmsg, void *arg)
3877 *logmsg = arg;
3878 return NULL;
3881 static const struct got_error *
3882 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
3883 const char *path, struct got_object_id *blob_id,
3884 struct got_object_id *commit_id)
3886 return NULL;
3889 struct collect_merged_paths_arg {
3890 got_worktree_checkout_cb progress_cb;
3891 void *progress_arg;
3892 struct got_pathlist_head *merged_paths;
3895 static const struct got_error *
3896 collect_merged_paths(void *arg, unsigned char status, const char *path)
3898 const struct got_error *err;
3899 struct collect_merged_paths_arg *a = arg;
3900 char *p;
3901 struct got_pathlist_entry *new;
3903 err = (*a->progress_cb)(a->progress_arg, status, path);
3904 if (err)
3905 return err;
3907 if (status != GOT_STATUS_MERGE &&
3908 status != GOT_STATUS_ADD &&
3909 status != GOT_STATUS_DELETE &&
3910 status != GOT_STATUS_CONFLICT)
3911 return NULL;
3913 p = strdup(path);
3914 if (p == NULL)
3915 return got_error_from_errno("strdup");
3917 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3918 if (err || new == NULL)
3919 free(p);
3920 return err;
3923 void
3924 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3926 struct got_pathlist_entry *pe;
3928 TAILQ_FOREACH(pe, merged_paths, entry)
3929 free((char *)pe->path);
3931 got_pathlist_free(merged_paths);
3934 static const struct got_error *
3935 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3936 struct got_repository *repo)
3938 const struct got_error *err;
3939 struct got_reference *commit_ref = NULL;
3941 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3942 if (err) {
3943 if (err->code != GOT_ERR_NOT_REF)
3944 goto done;
3945 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3946 if (err)
3947 goto done;
3948 err = got_ref_write(commit_ref, repo);
3949 if (err)
3950 goto done;
3951 } else {
3952 struct got_object_id *stored_id;
3953 int cmp;
3955 err = got_ref_resolve(&stored_id, repo, commit_ref);
3956 if (err)
3957 goto done;
3958 cmp = got_object_id_cmp(commit_id, stored_id);
3959 free(stored_id);
3960 if (cmp != 0) {
3961 err = got_error(GOT_ERR_REBASE_COMMITID);
3962 goto done;
3965 done:
3966 if (commit_ref)
3967 got_ref_close(commit_ref);
3968 return err;
3971 static const struct got_error *
3972 rebase_merge_files(struct got_pathlist_head *merged_paths,
3973 const char *commit_ref_name, struct got_worktree *worktree,
3974 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3975 struct got_object_id *commit_id, struct got_repository *repo,
3976 got_worktree_checkout_cb progress_cb, void *progress_arg,
3977 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3979 const struct got_error *err;
3980 struct got_reference *commit_ref = NULL;
3981 struct collect_merged_paths_arg cmp_arg;
3982 char *fileindex_path;
3984 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3986 err = get_fileindex_path(&fileindex_path, worktree);
3987 if (err)
3988 return err;
3990 cmp_arg.progress_cb = progress_cb;
3991 cmp_arg.progress_arg = progress_arg;
3992 cmp_arg.merged_paths = merged_paths;
3993 err = merge_files(worktree, fileindex, fileindex_path,
3994 parent_commit_id, commit_id, repo, collect_merged_paths,
3995 &cmp_arg, cancel_cb, cancel_arg);
3996 if (commit_ref)
3997 got_ref_close(commit_ref);
3998 return err;
4001 const struct got_error *
4002 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4003 struct got_worktree *worktree, struct got_fileindex *fileindex,
4004 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4005 struct got_repository *repo,
4006 got_worktree_checkout_cb progress_cb, void *progress_arg,
4007 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4009 const struct got_error *err;
4010 char *commit_ref_name;
4012 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4013 if (err)
4014 return err;
4016 err = store_commit_id(commit_ref_name, commit_id, repo);
4017 if (err)
4018 goto done;
4020 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4021 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4022 progress_arg, cancel_cb, cancel_arg);
4023 done:
4024 free(commit_ref_name);
4025 return err;
4028 const struct got_error *
4029 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4030 struct got_worktree *worktree, struct got_fileindex *fileindex,
4031 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4032 struct got_repository *repo,
4033 got_worktree_checkout_cb progress_cb, void *progress_arg,
4034 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4036 const struct got_error *err;
4037 char *commit_ref_name;
4039 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4040 if (err)
4041 return err;
4043 err = store_commit_id(commit_ref_name, commit_id, repo);
4044 if (err)
4045 goto done;
4047 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4048 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4049 progress_arg, cancel_cb, cancel_arg);
4050 done:
4051 free(commit_ref_name);
4052 return err;
4055 static const struct got_error *
4056 rebase_commit(struct got_object_id **new_commit_id,
4057 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4058 struct got_worktree *worktree, struct got_fileindex *fileindex,
4059 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4060 const char *new_logmsg, struct got_repository *repo)
4062 const struct got_error *err, *sync_err;
4063 struct got_pathlist_head commitable_paths;
4064 struct collect_commitables_arg cc_arg;
4065 char *fileindex_path = NULL;
4066 struct got_reference *head_ref = NULL;
4067 struct got_object_id *head_commit_id = NULL;
4068 char *logmsg = NULL;
4070 TAILQ_INIT(&commitable_paths);
4071 *new_commit_id = NULL;
4073 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4075 err = get_fileindex_path(&fileindex_path, worktree);
4076 if (err)
4077 return err;
4079 cc_arg.commitable_paths = &commitable_paths;
4080 cc_arg.worktree = worktree;
4081 cc_arg.repo = repo;
4083 * If possible get the status of individual files directly to
4084 * avoid crawling the entire work tree once per rebased commit.
4085 * TODO: Ideally, merged_paths would contain a list of commitables
4086 * we could use so we could skip worktree_status() entirely.
4088 if (merged_paths) {
4089 struct got_pathlist_entry *pe;
4090 if (TAILQ_EMPTY(merged_paths)) {
4091 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4092 goto done;
4094 TAILQ_FOREACH(pe, merged_paths, entry) {
4095 err = worktree_status(worktree, pe->path, fileindex,
4096 repo, collect_commitables, &cc_arg, NULL, NULL);
4097 if (err)
4098 goto done;
4100 } else {
4101 err = worktree_status(worktree, "", fileindex, repo,
4102 collect_commitables, &cc_arg, NULL, NULL);
4103 if (err)
4104 goto done;
4107 if (TAILQ_EMPTY(&commitable_paths)) {
4108 /* No-op change; commit will be elided. */
4109 err = got_ref_delete(commit_ref, repo);
4110 if (err)
4111 goto done;
4112 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4113 goto done;
4116 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4117 if (err)
4118 goto done;
4120 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4121 if (err)
4122 goto done;
4124 if (new_logmsg)
4125 logmsg = strdup(new_logmsg);
4126 else
4127 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4128 if (logmsg == NULL)
4129 return got_error_from_errno("strdup");
4131 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4132 worktree, got_object_commit_get_author(orig_commit),
4133 got_object_commit_get_committer(orig_commit),
4134 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4135 if (err)
4136 goto done;
4138 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4139 if (err)
4140 goto done;
4142 err = got_ref_delete(commit_ref, repo);
4143 if (err)
4144 goto done;
4146 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4147 fileindex);
4148 sync_err = sync_fileindex(fileindex, fileindex_path);
4149 if (sync_err && err == NULL)
4150 err = sync_err;
4151 done:
4152 free(fileindex_path);
4153 free(head_commit_id);
4154 if (head_ref)
4155 got_ref_close(head_ref);
4156 if (err) {
4157 free(*new_commit_id);
4158 *new_commit_id = NULL;
4160 return err;
4163 const struct got_error *
4164 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4165 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4166 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4167 struct got_commit_object *orig_commit,
4168 struct got_object_id *orig_commit_id, struct got_repository *repo)
4170 const struct got_error *err;
4171 char *commit_ref_name;
4172 struct got_reference *commit_ref = NULL;
4173 struct got_object_id *commit_id = NULL;
4175 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4176 if (err)
4177 return err;
4179 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4180 if (err)
4181 goto done;
4182 err = got_ref_resolve(&commit_id, repo, commit_ref);
4183 if (err)
4184 goto done;
4185 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4186 err = got_error(GOT_ERR_REBASE_COMMITID);
4187 goto done;
4190 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4191 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4192 done:
4193 if (commit_ref)
4194 got_ref_close(commit_ref);
4195 free(commit_ref_name);
4196 free(commit_id);
4197 return err;
4200 const struct got_error *
4201 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4202 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4203 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4204 struct got_commit_object *orig_commit,
4205 struct got_object_id *orig_commit_id, const char *new_logmsg,
4206 struct got_repository *repo)
4208 const struct got_error *err;
4209 char *commit_ref_name;
4210 struct got_reference *commit_ref = NULL;
4211 struct got_object_id *commit_id = NULL;
4213 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4214 if (err)
4215 return err;
4217 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4218 if (err)
4219 goto done;
4220 err = got_ref_resolve(&commit_id, repo, commit_ref);
4221 if (err)
4222 goto done;
4223 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4224 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4225 goto done;
4228 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4229 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4230 done:
4231 if (commit_ref)
4232 got_ref_close(commit_ref);
4233 free(commit_ref_name);
4234 free(commit_id);
4235 return err;
4238 const struct got_error *
4239 got_worktree_rebase_postpone(struct got_worktree *worktree,
4240 struct got_fileindex *fileindex)
4242 if (fileindex)
4243 got_fileindex_free(fileindex);
4244 return lock_worktree(worktree, LOCK_SH);
4247 static const struct got_error *
4248 delete_ref(const char *name, struct got_repository *repo)
4250 const struct got_error *err;
4251 struct got_reference *ref;
4253 err = got_ref_open(&ref, repo, name, 0);
4254 if (err) {
4255 if (err->code == GOT_ERR_NOT_REF)
4256 return NULL;
4257 return err;
4260 err = got_ref_delete(ref, repo);
4261 got_ref_close(ref);
4262 return err;
4265 static const struct got_error *
4266 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4268 const struct got_error *err;
4269 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4270 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4272 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4273 if (err)
4274 goto done;
4275 err = delete_ref(tmp_branch_name, repo);
4276 if (err)
4277 goto done;
4279 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4280 if (err)
4281 goto done;
4282 err = delete_ref(new_base_branch_ref_name, repo);
4283 if (err)
4284 goto done;
4286 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4287 if (err)
4288 goto done;
4289 err = delete_ref(branch_ref_name, repo);
4290 if (err)
4291 goto done;
4293 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4294 if (err)
4295 goto done;
4296 err = delete_ref(commit_ref_name, repo);
4297 if (err)
4298 goto done;
4300 done:
4301 free(tmp_branch_name);
4302 free(new_base_branch_ref_name);
4303 free(branch_ref_name);
4304 free(commit_ref_name);
4305 return err;
4308 const struct got_error *
4309 got_worktree_rebase_complete(struct got_worktree *worktree,
4310 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4311 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4312 struct got_repository *repo)
4314 const struct got_error *err, *unlockerr;
4315 struct got_object_id *new_head_commit_id = NULL;
4317 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4318 if (err)
4319 return err;
4321 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4322 if (err)
4323 goto done;
4325 err = got_ref_write(rebased_branch, repo);
4326 if (err)
4327 goto done;
4329 err = got_worktree_set_head_ref(worktree, rebased_branch);
4330 if (err)
4331 goto done;
4333 err = delete_rebase_refs(worktree, repo);
4334 done:
4335 if (fileindex)
4336 got_fileindex_free(fileindex);
4337 free(new_head_commit_id);
4338 unlockerr = lock_worktree(worktree, LOCK_SH);
4339 if (unlockerr && err == NULL)
4340 err = unlockerr;
4341 return err;
4344 struct collect_revertible_paths_arg {
4345 struct got_pathlist_head *revertible_paths;
4346 struct got_worktree *worktree;
4349 static const struct got_error *
4350 collect_revertible_paths(void *arg, unsigned char status,
4351 unsigned char staged_status, const char *relpath,
4352 struct got_object_id *blob_id, struct got_object_id *commit_id)
4354 struct collect_revertible_paths_arg *a = arg;
4355 const struct got_error *err = NULL;
4356 struct got_pathlist_entry *new = NULL;
4357 char *path = NULL;
4359 if (status != GOT_STATUS_ADD &&
4360 status != GOT_STATUS_DELETE &&
4361 status != GOT_STATUS_MODIFY &&
4362 status != GOT_STATUS_CONFLICT &&
4363 status != GOT_STATUS_MISSING)
4364 return NULL;
4366 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4367 return got_error_from_errno("asprintf");
4369 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4370 if (err || new == NULL)
4371 free(path);
4372 return err;
4375 const struct got_error *
4376 got_worktree_rebase_abort(struct got_worktree *worktree,
4377 struct got_fileindex *fileindex, struct got_repository *repo,
4378 struct got_reference *new_base_branch,
4379 got_worktree_checkout_cb progress_cb, void *progress_arg)
4381 const struct got_error *err, *unlockerr, *sync_err;
4382 struct got_reference *resolved = NULL;
4383 struct got_object_id *commit_id = NULL;
4384 char *fileindex_path = NULL;
4385 struct got_pathlist_head revertible_paths;
4386 struct got_pathlist_entry *pe;
4387 struct collect_revertible_paths_arg crp_arg;
4388 struct got_object_id *tree_id = NULL;
4390 TAILQ_INIT(&revertible_paths);
4392 err = lock_worktree(worktree, LOCK_EX);
4393 if (err)
4394 return err;
4396 err = got_ref_open(&resolved, repo,
4397 got_ref_get_symref_target(new_base_branch), 0);
4398 if (err)
4399 goto done;
4401 err = got_worktree_set_head_ref(worktree, resolved);
4402 if (err)
4403 goto done;
4406 * XXX commits to the base branch could have happened while
4407 * we were busy rebasing; should we store the original commit ID
4408 * when rebase begins and read it back here?
4410 err = got_ref_resolve(&commit_id, repo, resolved);
4411 if (err)
4412 goto done;
4414 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4415 if (err)
4416 goto done;
4418 err = got_object_id_by_path(&tree_id, repo,
4419 worktree->base_commit_id, worktree->path_prefix);
4420 if (err)
4421 goto done;
4423 err = delete_rebase_refs(worktree, repo);
4424 if (err)
4425 goto done;
4427 err = get_fileindex_path(&fileindex_path, worktree);
4428 if (err)
4429 goto done;
4431 crp_arg.revertible_paths = &revertible_paths;
4432 crp_arg.worktree = worktree;
4433 err = worktree_status(worktree, "", fileindex, repo,
4434 collect_revertible_paths, &crp_arg, NULL, NULL);
4435 if (err)
4436 goto done;
4438 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4439 err = revert_file(worktree, fileindex, pe->path,
4440 progress_cb, progress_arg, repo);
4441 if (err)
4442 goto sync;
4445 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4446 repo, progress_cb, progress_arg, NULL, NULL);
4447 sync:
4448 sync_err = sync_fileindex(fileindex, fileindex_path);
4449 if (sync_err && err == NULL)
4450 err = sync_err;
4451 done:
4452 got_ref_close(resolved);
4453 free(tree_id);
4454 free(commit_id);
4455 if (fileindex)
4456 got_fileindex_free(fileindex);
4457 free(fileindex_path);
4458 TAILQ_FOREACH(pe, &revertible_paths, entry)
4459 free((char *)pe->path);
4460 got_pathlist_free(&revertible_paths);
4462 unlockerr = lock_worktree(worktree, LOCK_SH);
4463 if (unlockerr && err == NULL)
4464 err = unlockerr;
4465 return err;
4468 const struct got_error *
4469 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4470 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4471 struct got_fileindex **fileindex, struct got_worktree *worktree,
4472 struct got_repository *repo)
4474 const struct got_error *err = NULL;
4475 char *tmp_branch_name = NULL;
4476 char *branch_ref_name = NULL;
4477 char *base_commit_ref_name = NULL;
4478 char *fileindex_path = NULL;
4479 struct check_rebase_ok_arg ok_arg;
4480 struct got_reference *wt_branch = NULL;
4481 struct got_reference *base_commit_ref = NULL;
4483 *tmp_branch = NULL;
4484 *branch_ref = NULL;
4485 *base_commit_id = NULL;
4486 *fileindex = NULL;
4488 err = lock_worktree(worktree, LOCK_EX);
4489 if (err)
4490 return err;
4492 err = open_fileindex(fileindex, &fileindex_path, worktree);
4493 if (err)
4494 goto done;
4496 ok_arg.worktree = worktree;
4497 ok_arg.repo = repo;
4498 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4499 &ok_arg);
4500 if (err)
4501 goto done;
4503 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4504 if (err)
4505 goto done;
4507 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4508 if (err)
4509 goto done;
4511 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4512 worktree);
4513 if (err)
4514 goto done;
4516 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4517 0);
4518 if (err)
4519 goto done;
4521 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4522 if (err)
4523 goto done;
4525 err = got_ref_write(*branch_ref, repo);
4526 if (err)
4527 goto done;
4529 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4530 worktree->base_commit_id);
4531 if (err)
4532 goto done;
4533 err = got_ref_write(base_commit_ref, repo);
4534 if (err)
4535 goto done;
4536 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4537 if (*base_commit_id == NULL) {
4538 err = got_error_from_errno("got_object_id_dup");
4539 goto done;
4542 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4543 worktree->base_commit_id);
4544 if (err)
4545 goto done;
4546 err = got_ref_write(*tmp_branch, repo);
4547 if (err)
4548 goto done;
4550 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4551 if (err)
4552 goto done;
4553 done:
4554 free(fileindex_path);
4555 free(tmp_branch_name);
4556 free(branch_ref_name);
4557 free(base_commit_ref_name);
4558 if (wt_branch)
4559 got_ref_close(wt_branch);
4560 if (err) {
4561 if (*branch_ref) {
4562 got_ref_close(*branch_ref);
4563 *branch_ref = NULL;
4565 if (*tmp_branch) {
4566 got_ref_close(*tmp_branch);
4567 *tmp_branch = NULL;
4569 free(*base_commit_id);
4570 if (*fileindex) {
4571 got_fileindex_free(*fileindex);
4572 *fileindex = NULL;
4574 lock_worktree(worktree, LOCK_SH);
4576 return err;
4579 const struct got_error *
4580 got_worktree_histedit_postpone(struct got_worktree *worktree,
4581 struct got_fileindex *fileindex)
4583 if (fileindex)
4584 got_fileindex_free(fileindex);
4585 return lock_worktree(worktree, LOCK_SH);
4588 const struct got_error *
4589 got_worktree_histedit_in_progress(int *in_progress,
4590 struct got_worktree *worktree)
4592 const struct got_error *err;
4593 char *tmp_branch_name = NULL;
4595 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4596 if (err)
4597 return err;
4599 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4600 free(tmp_branch_name);
4601 return NULL;
4604 const struct got_error *
4605 got_worktree_histedit_continue(struct got_object_id **commit_id,
4606 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4607 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4608 struct got_worktree *worktree, struct got_repository *repo)
4610 const struct got_error *err;
4611 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4612 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4613 struct got_reference *commit_ref = NULL;
4614 struct got_reference *base_commit_ref = NULL;
4615 char *fileindex_path = NULL;
4617 *commit_id = NULL;
4618 *tmp_branch = NULL;
4619 *base_commit_id = NULL;
4620 *fileindex = NULL;
4622 err = lock_worktree(worktree, LOCK_EX);
4623 if (err)
4624 return err;
4626 err = open_fileindex(fileindex, &fileindex_path, worktree);
4627 if (err)
4628 goto done;
4630 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4631 if (err)
4632 return err;
4634 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4635 if (err)
4636 goto done;
4638 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4639 if (err)
4640 goto done;
4642 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4643 worktree);
4644 if (err)
4645 goto done;
4647 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4648 if (err)
4649 goto done;
4651 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4652 if (err)
4653 goto done;
4654 err = got_ref_resolve(commit_id, repo, commit_ref);
4655 if (err)
4656 goto done;
4658 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4659 if (err)
4660 goto done;
4661 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4662 if (err)
4663 goto done;
4665 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4666 if (err)
4667 goto done;
4668 done:
4669 free(commit_ref_name);
4670 free(branch_ref_name);
4671 free(fileindex_path);
4672 if (commit_ref)
4673 got_ref_close(commit_ref);
4674 if (base_commit_ref)
4675 got_ref_close(base_commit_ref);
4676 if (err) {
4677 free(*commit_id);
4678 *commit_id = NULL;
4679 free(*base_commit_id);
4680 *base_commit_id = NULL;
4681 if (*tmp_branch) {
4682 got_ref_close(*tmp_branch);
4683 *tmp_branch = NULL;
4685 if (*fileindex) {
4686 got_fileindex_free(*fileindex);
4687 *fileindex = NULL;
4689 lock_worktree(worktree, LOCK_EX);
4691 return err;
4694 static const struct got_error *
4695 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4697 const struct got_error *err;
4698 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4699 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4701 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4702 if (err)
4703 goto done;
4704 err = delete_ref(tmp_branch_name, repo);
4705 if (err)
4706 goto done;
4708 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4709 worktree);
4710 if (err)
4711 goto done;
4712 err = delete_ref(base_commit_ref_name, repo);
4713 if (err)
4714 goto done;
4716 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4717 if (err)
4718 goto done;
4719 err = delete_ref(branch_ref_name, repo);
4720 if (err)
4721 goto done;
4723 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4724 if (err)
4725 goto done;
4726 err = delete_ref(commit_ref_name, repo);
4727 if (err)
4728 goto done;
4729 done:
4730 free(tmp_branch_name);
4731 free(base_commit_ref_name);
4732 free(branch_ref_name);
4733 free(commit_ref_name);
4734 return err;
4737 const struct got_error *
4738 got_worktree_histedit_abort(struct got_worktree *worktree,
4739 struct got_fileindex *fileindex, struct got_repository *repo,
4740 struct got_reference *branch, struct got_object_id *base_commit_id,
4741 got_worktree_checkout_cb progress_cb, void *progress_arg)
4743 const struct got_error *err, *unlockerr, *sync_err;
4744 struct got_reference *resolved = NULL;
4745 char *fileindex_path = NULL;
4746 struct got_pathlist_head revertible_paths;
4747 struct got_pathlist_entry *pe;
4748 struct collect_revertible_paths_arg crp_arg;
4749 struct got_object_id *tree_id = NULL;
4751 TAILQ_INIT(&revertible_paths);
4753 err = lock_worktree(worktree, LOCK_EX);
4754 if (err)
4755 return err;
4757 err = got_ref_open(&resolved, repo,
4758 got_ref_get_symref_target(branch), 0);
4759 if (err)
4760 goto done;
4762 err = got_worktree_set_head_ref(worktree, resolved);
4763 if (err)
4764 goto done;
4766 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4767 if (err)
4768 goto done;
4770 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4771 worktree->path_prefix);
4772 if (err)
4773 goto done;
4775 err = delete_histedit_refs(worktree, repo);
4776 if (err)
4777 goto done;
4779 err = get_fileindex_path(&fileindex_path, worktree);
4780 if (err)
4781 goto done;
4783 crp_arg.revertible_paths = &revertible_paths;
4784 crp_arg.worktree = worktree;
4785 err = worktree_status(worktree, "", fileindex, repo,
4786 collect_revertible_paths, &crp_arg, NULL, NULL);
4787 if (err)
4788 goto done;
4790 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4791 err = revert_file(worktree, fileindex, pe->path,
4792 progress_cb, progress_arg, repo);
4793 if (err)
4794 goto sync;
4797 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4798 repo, progress_cb, progress_arg, NULL, NULL);
4799 sync:
4800 sync_err = sync_fileindex(fileindex, fileindex_path);
4801 if (sync_err && err == NULL)
4802 err = sync_err;
4803 done:
4804 got_ref_close(resolved);
4805 free(tree_id);
4806 free(fileindex_path);
4807 TAILQ_FOREACH(pe, &revertible_paths, entry)
4808 free((char *)pe->path);
4809 got_pathlist_free(&revertible_paths);
4811 unlockerr = lock_worktree(worktree, LOCK_SH);
4812 if (unlockerr && err == NULL)
4813 err = unlockerr;
4814 return err;
4817 const struct got_error *
4818 got_worktree_histedit_complete(struct got_worktree *worktree,
4819 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4820 struct got_reference *edited_branch, struct got_repository *repo)
4822 const struct got_error *err, *unlockerr;
4823 struct got_object_id *new_head_commit_id = NULL;
4824 struct got_reference *resolved = NULL;
4826 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4827 if (err)
4828 return err;
4830 err = got_ref_open(&resolved, repo,
4831 got_ref_get_symref_target(edited_branch), 0);
4832 if (err)
4833 goto done;
4835 err = got_ref_change_ref(resolved, new_head_commit_id);
4836 if (err)
4837 goto done;
4839 err = got_ref_write(resolved, repo);
4840 if (err)
4841 goto done;
4843 err = got_worktree_set_head_ref(worktree, resolved);
4844 if (err)
4845 goto done;
4847 err = delete_histedit_refs(worktree, repo);
4848 done:
4849 if (fileindex)
4850 got_fileindex_free(fileindex);
4851 free(new_head_commit_id);
4852 unlockerr = lock_worktree(worktree, LOCK_SH);
4853 if (unlockerr && err == NULL)
4854 err = unlockerr;
4855 return err;
4858 const struct got_error *
4859 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4860 struct got_object_id *commit_id, struct got_repository *repo)
4862 const struct got_error *err;
4863 char *commit_ref_name;
4865 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4866 if (err)
4867 return err;
4869 err = store_commit_id(commit_ref_name, commit_id, repo);
4870 if (err)
4871 goto done;
4873 err = delete_ref(commit_ref_name, repo);
4874 done:
4875 free(commit_ref_name);
4876 return err;
4879 static const struct got_error *
4880 stage_path(const char *relpath, const char *ondisk_path,
4881 const char *path_content, struct got_worktree *worktree,
4882 struct got_fileindex *fileindex, struct got_repository *repo,
4883 got_worktree_status_cb status_cb, void *status_arg)
4885 const struct got_error *err = NULL;
4886 struct got_fileindex_entry *ie;
4887 unsigned char status;
4888 struct stat sb;
4889 struct got_object_id *blob_id = NULL;
4890 uint32_t stage;
4892 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
4893 if (ie == NULL) {
4894 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4895 goto done;
4898 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4899 if (err)
4900 goto done;
4902 switch (status) {
4903 case GOT_STATUS_ADD:
4904 case GOT_STATUS_MODIFY:
4905 err = got_object_blob_create(&blob_id,
4906 path_content ? path_content : ondisk_path, repo);
4907 if (err)
4908 goto done;
4909 memcpy(ie->staged_blob_sha1, blob_id->sha1,
4910 SHA1_DIGEST_LENGTH);
4911 if (status == GOT_STATUS_ADD)
4912 stage = GOT_FILEIDX_STAGE_ADD;
4913 else
4914 stage = GOT_FILEIDX_STAGE_MODIFY;
4915 break;
4916 case GOT_STATUS_DELETE:
4917 stage = GOT_FILEIDX_STAGE_DELETE;
4918 break;
4919 default:
4920 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4921 goto done;
4924 got_fileindex_entry_stage_set(ie, stage);
4925 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4926 get_staged_status(ie), relpath, blob_id, NULL);
4927 done:
4928 free(blob_id);
4929 return err;
4932 const struct got_error *
4933 got_worktree_stage(struct got_worktree *worktree,
4934 struct got_pathlist_head *paths,
4935 got_worktree_status_cb status_cb, void *status_arg,
4936 struct got_repository *repo)
4938 const struct got_error *err = NULL, *sync_err, *unlockerr;
4939 struct got_pathlist_entry *pe;
4940 struct got_fileindex *fileindex = NULL;
4941 char *fileindex_path = NULL;
4943 err = lock_worktree(worktree, LOCK_EX);
4944 if (err)
4945 return err;
4947 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4948 if (err)
4949 goto done;
4951 TAILQ_FOREACH(pe, paths, entry) {
4952 char *relpath;
4953 err = got_path_skip_common_ancestor(&relpath,
4954 got_worktree_get_root_path(worktree), pe->path);
4955 if (err)
4956 break;
4957 err = stage_path(relpath, pe->path,
4958 (const char *)pe->data, worktree, fileindex, repo,
4959 status_cb, status_arg);
4960 free(relpath);
4961 if (err)
4962 break;
4965 sync_err = sync_fileindex(fileindex, fileindex_path);
4966 if (sync_err && err == NULL)
4967 err = sync_err;
4968 done:
4969 free(fileindex_path);
4970 if (fileindex)
4971 got_fileindex_free(fileindex);
4972 unlockerr = lock_worktree(worktree, LOCK_SH);
4973 if (unlockerr && err == NULL)
4974 err = unlockerr;
4975 return err;