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(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 (lstat(path, &sb) != 0) {
146 err = got_error_from_errno2("lstat", 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(GOT_ERR_WORKTREE_META));
159 goto done;
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error(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(GOT_ERR_WORKTREE_META);
359 goto done;
361 if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 err = got_error(GOT_ERR_WORKTREE_VERS);
363 goto done;
366 *worktree = calloc(1, sizeof(**worktree));
367 if (*worktree == NULL) {
368 err = got_error_from_errno("calloc");
369 goto done;
371 (*worktree)->lockfd = -1;
373 (*worktree)->root_path = strdup(path);
374 if ((*worktree)->root_path == NULL) {
375 err = got_error_from_errno("strdup");
376 goto done;
378 err = read_meta_file(&(*worktree)->repo_path, path_got,
379 GOT_WORKTREE_REPOSITORY);
380 if (err)
381 goto done;
383 err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 GOT_WORKTREE_PATH_PREFIX);
385 if (err)
386 goto done;
388 err = read_meta_file(&base_commit_id_str, path_got,
389 GOT_WORKTREE_BASE_COMMIT);
390 if (err)
391 goto done;
393 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
394 if (err)
395 goto done;
396 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 if (uuid_status != uuid_s_ok) {
398 err = got_error_uuid(uuid_status);
399 goto done;
402 err = got_repo_open(&repo, (*worktree)->repo_path);
403 if (err)
404 goto done;
406 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
407 base_commit_id_str);
408 if (err)
409 goto done;
411 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 GOT_WORKTREE_HEAD_REF);
413 done:
414 if (repo)
415 got_repo_close(repo);
416 free(path_got);
417 free(path_lock);
418 free(base_commit_id_str);
419 free(uuidstr);
420 free(formatstr);
421 if (err) {
422 if (fd != -1)
423 close(fd);
424 if (*worktree != NULL)
425 got_worktree_close(*worktree);
426 *worktree = NULL;
427 } else
428 (*worktree)->lockfd = fd;
430 return err;
433 const struct got_error *
434 got_worktree_open(struct got_worktree **worktree, const char *path)
436 const struct got_error *err = NULL;
438 do {
439 err = open_worktree(worktree, path);
440 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
441 return err;
442 if (*worktree)
443 return NULL;
444 path = dirname(path);
445 if (path == NULL)
446 return got_error_from_errno2("dirname", path);
447 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 return got_error(GOT_ERR_NOT_WORKTREE);
452 const struct got_error *
453 got_worktree_close(struct got_worktree *worktree)
455 const struct got_error *err = NULL;
456 free(worktree->root_path);
457 free(worktree->repo_path);
458 free(worktree->path_prefix);
459 free(worktree->base_commit_id);
460 free(worktree->head_ref_name);
461 if (worktree->lockfd != -1)
462 if (close(worktree->lockfd) != 0)
463 err = got_error_from_errno2("close",
464 got_worktree_get_root_path(worktree));
465 free(worktree);
466 return err;
469 const char *
470 got_worktree_get_root_path(struct got_worktree *worktree)
472 return worktree->root_path;
475 const char *
476 got_worktree_get_repo_path(struct got_worktree *worktree)
478 return worktree->repo_path;
481 const char *
482 got_worktree_get_path_prefix(struct got_worktree *worktree)
484 return worktree->path_prefix;
487 const struct got_error *
488 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 const char *path_prefix)
491 char *absprefix = NULL;
493 if (!got_path_is_absolute(path_prefix)) {
494 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 return got_error_from_errno("asprintf");
497 *match = (strcmp(absprefix ? absprefix : path_prefix,
498 worktree->path_prefix) == 0);
499 free(absprefix);
500 return NULL;
503 const char *
504 got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 return worktree->head_ref_name;
509 const struct got_error *
510 got_worktree_set_head_ref(struct got_worktree *worktree,
511 struct got_reference *head_ref)
513 const struct got_error *err = NULL;
514 char *path_got = NULL, *head_ref_name = NULL;
516 if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 GOT_WORKTREE_GOT_DIR) == -1) {
518 err = got_error_from_errno("asprintf");
519 path_got = NULL;
520 goto done;
523 head_ref_name = strdup(got_ref_get_name(head_ref));
524 if (head_ref_name == NULL) {
525 err = got_error_from_errno("strdup");
526 goto done;
529 err = write_head_ref(path_got, head_ref);
530 if (err)
531 goto done;
533 free(worktree->head_ref_name);
534 worktree->head_ref_name = head_ref_name;
535 done:
536 free(path_got);
537 if (err)
538 free(head_ref_name);
539 return err;
542 struct got_object_id *
543 got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 return worktree->base_commit_id;
548 const struct got_error *
549 got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 struct got_repository *repo, struct got_object_id *commit_id)
552 const struct got_error *err;
553 struct got_object *obj = NULL;
554 char *id_str = NULL;
555 char *path_got = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
560 path_got = NULL;
561 goto done;
564 err = got_object_open(&obj, repo, commit_id);
565 if (err)
566 return err;
568 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 err = got_error(GOT_ERR_OBJ_TYPE);
570 goto done;
573 /* Record our base commit. */
574 err = got_object_id_str(&id_str, commit_id);
575 if (err)
576 goto done;
577 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
578 if (err)
579 goto done;
581 free(worktree->base_commit_id);
582 worktree->base_commit_id = got_object_id_dup(commit_id);
583 if (worktree->base_commit_id == NULL) {
584 err = got_error_from_errno("got_object_id_dup");
585 goto done;
587 done:
588 if (obj)
589 got_object_close(obj);
590 free(id_str);
591 free(path_got);
592 return err;
595 static const struct got_error *
596 lock_worktree(struct got_worktree *worktree, int operation)
598 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 : got_error_from_errno2("flock",
601 got_worktree_get_root_path(worktree)));
602 return NULL;
605 static const struct got_error *
606 add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 const struct got_error *err = NULL;
609 char *abspath;
611 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 return got_error_from_errno("asprintf");
614 err = got_path_mkdir(abspath);
615 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
616 struct stat sb;
617 err = NULL;
618 if (lstat(abspath, &sb) == -1) {
619 err = got_error_from_errno2("lstat", abspath);
620 } else if (!S_ISDIR(sb.st_mode)) {
621 /* TODO directory is obstructed; do something */
622 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
625 free(abspath);
626 return err;
629 static const struct got_error *
630 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 const struct got_error *err = NULL;
633 uint8_t fbuf1[8192];
634 uint8_t fbuf2[8192];
635 size_t flen1 = 0, flen2 = 0;
637 *same = 1;
639 for (;;) {
640 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 if (flen1 == 0 && ferror(f1)) {
642 err = got_error_from_errno("fread");
643 break;
645 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 if (flen2 == 0 && ferror(f2)) {
647 err = got_error_from_errno("fread");
648 break;
650 if (flen1 == 0) {
651 if (flen2 != 0)
652 *same = 0;
653 break;
654 } else if (flen2 == 0) {
655 if (flen1 != 0)
656 *same = 0;
657 break;
658 } else if (flen1 == flen2) {
659 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
660 *same = 0;
661 break;
663 } else {
664 *same = 0;
665 break;
669 return err;
672 static const struct got_error *
673 check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 const struct got_error *err = NULL;
676 struct stat sb;
677 size_t size1, size2;
678 FILE *f1 = NULL, *f2 = NULL;
680 *same = 1;
682 if (lstat(f1_path, &sb) != 0) {
683 err = got_error_from_errno2("lstat", f1_path);
684 goto done;
686 size1 = sb.st_size;
688 if (lstat(f2_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f2_path);
690 goto done;
692 size2 = sb.st_size;
694 if (size1 != size2) {
695 *same = 0;
696 return NULL;
699 f1 = fopen(f1_path, "r");
700 if (f1 == NULL)
701 return got_error_from_errno2("open", f1_path);
703 f2 = fopen(f2_path, "r");
704 if (f2 == NULL) {
705 err = got_error_from_errno2("open", f2_path);
706 goto done;
709 err = check_file_contents_equal(same, f1, f2);
710 done:
711 if (f1 && fclose(f1) != 0 && err == NULL)
712 err = got_error_from_errno("fclose");
713 if (f2 && fclose(f2) != 0 && err == NULL)
714 err = got_error_from_errno("fclose");
716 return err;
719 /*
720 * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 * blob_deriv acts as the first derived version, and the file on disk
722 * acts as the second derived version.
723 */
724 static const struct got_error *
725 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 struct got_blob_object *blob_orig, const char *ondisk_path,
727 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 struct got_object_id *deriv_base_commit_id,
729 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
730 void *progress_arg)
732 const struct got_error *err = NULL;
733 int merged_fd = -1;
734 FILE *f_deriv = NULL, *f_orig = NULL;
735 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
736 char *merged_path = NULL, *base_path = NULL;
737 char *id_str = NULL;
738 char *label_deriv = NULL;
739 int overlapcnt = 0;
740 char *parent;
742 *local_changes_subsumed = 0;
744 parent = dirname(ondisk_path);
745 if (parent == NULL)
746 return got_error_from_errno2("dirname", ondisk_path);
748 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 return got_error_from_errno("asprintf");
751 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
752 if (err)
753 goto done;
755 free(base_path);
756 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
757 err = got_error_from_errno("asprintf");
758 base_path = NULL;
759 goto done;
762 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
763 if (err)
764 goto done;
765 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
766 blob_deriv);
767 if (err)
768 goto done;
770 free(base_path);
771 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
772 err = got_error_from_errno("asprintf");
773 base_path = NULL;
774 goto done;
777 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
778 if (err)
779 goto done;
780 if (blob_orig) {
781 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
782 blob_orig);
783 if (err)
784 goto done;
785 } else {
786 /*
787 * If the file has no blob, this is an "add vs add" conflict,
788 * and we simply use an empty ancestor file to make both files
789 * appear in the merged result in their entirety.
790 */
793 err = got_object_id_str(&id_str, deriv_base_commit_id);
794 if (err)
795 goto done;
796 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
797 err = got_error_from_errno("asprintf");
798 goto done;
801 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
802 blob_orig_path, ondisk_path, label_deriv, path);
803 if (err)
804 goto done;
806 err = (*progress_cb)(progress_arg,
807 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
808 if (err)
809 goto done;
811 if (fsync(merged_fd) != 0) {
812 err = got_error_from_errno("fsync");
813 goto done;
816 /* Check if a clean merge has subsumed all local changes. */
817 if (overlapcnt == 0) {
818 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
819 merged_path);
820 if (err)
821 goto done;
824 if (chmod(merged_path, st_mode) != 0) {
825 err = got_error_from_errno2("chmod", merged_path);
826 goto done;
829 if (rename(merged_path, ondisk_path) != 0) {
830 err = got_error_from_errno3("rename", merged_path,
831 ondisk_path);
832 unlink(merged_path);
833 goto done;
836 done:
837 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
838 err = got_error_from_errno("close");
839 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
840 err = got_error_from_errno("fclose");
841 if (f_orig && fclose(f_orig) != 0 && err == NULL)
842 err = got_error_from_errno("fclose");
843 free(merged_path);
844 free(base_path);
845 if (blob_deriv_path) {
846 unlink(blob_deriv_path);
847 free(blob_deriv_path);
849 if (blob_orig_path) {
850 unlink(blob_orig_path);
851 free(blob_orig_path);
853 free(id_str);
854 free(label_deriv);
855 return err;
858 static const struct got_error *
859 update_blob_fileindex_entry(struct got_worktree *worktree,
860 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
861 const char *ondisk_path, const char *path, struct got_blob_object *blob,
862 int update_timestamps)
864 const struct got_error *err = NULL;
866 if (ie == NULL)
867 ie = got_fileindex_entry_get(fileindex, path);
868 if (ie)
869 err = got_fileindex_entry_update(ie, ondisk_path,
870 blob->id.sha1, worktree->base_commit_id->sha1,
871 update_timestamps);
872 else {
873 struct got_fileindex_entry *new_ie;
874 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
875 path, blob->id.sha1, worktree->base_commit_id->sha1);
876 if (!err)
877 err = got_fileindex_entry_add(fileindex, new_ie);
879 return err;
882 static const struct got_error *
883 install_blob(struct got_worktree *worktree, const char *ondisk_path,
884 const char *path, uint16_t te_mode, uint16_t st_mode,
885 struct got_blob_object *blob, int restoring_missing_file,
886 int reverting_versioned_file, struct got_repository *repo,
887 got_worktree_checkout_cb progress_cb, void *progress_arg)
889 const struct got_error *err = NULL;
890 int fd = -1;
891 size_t len, hdrlen;
892 int update = 0;
893 char *tmppath = NULL;
895 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
896 GOT_DEFAULT_FILE_MODE);
897 if (fd == -1) {
898 if (errno == ENOENT) {
899 char *parent = dirname(path);
900 if (parent == NULL)
901 return got_error_from_errno2("dirname", path);
902 err = add_dir_on_disk(worktree, parent);
903 if (err)
904 return err;
905 fd = open(ondisk_path,
906 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
907 GOT_DEFAULT_FILE_MODE);
908 if (fd == -1)
909 return got_error_from_errno2("open",
910 ondisk_path);
911 } else if (errno == EEXIST) {
912 if (!S_ISREG(st_mode)) {
913 /* TODO file is obstructed; do something */
914 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
915 goto done;
916 } else {
917 err = got_opentemp_named_fd(&tmppath, &fd,
918 ondisk_path);
919 if (err)
920 goto done;
921 update = 1;
923 } else
924 return got_error_from_errno2("open", ondisk_path);
927 if (restoring_missing_file)
928 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
929 else if (reverting_versioned_file)
930 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
931 else
932 err = (*progress_cb)(progress_arg,
933 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
934 if (err)
935 goto done;
937 hdrlen = got_object_blob_get_hdrlen(blob);
938 do {
939 const uint8_t *buf = got_object_blob_get_read_buf(blob);
940 err = got_object_blob_read_block(&len, blob);
941 if (err)
942 break;
943 if (len > 0) {
944 /* Skip blob object header first time around. */
945 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
946 if (outlen == -1) {
947 err = got_error_from_errno("write");
948 goto done;
949 } else if (outlen != len - hdrlen) {
950 err = got_error(GOT_ERR_IO);
951 goto done;
953 hdrlen = 0;
955 } while (len != 0);
957 if (fsync(fd) != 0) {
958 err = got_error_from_errno("fsync");
959 goto done;
962 if (update) {
963 if (rename(tmppath, ondisk_path) != 0) {
964 err = got_error_from_errno3("rename", tmppath,
965 ondisk_path);
966 unlink(tmppath);
967 goto done;
971 if (te_mode & S_IXUSR) {
972 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
973 err = got_error_from_errno2("chmod", ondisk_path);
974 goto done;
976 } else {
977 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
978 err = got_error_from_errno2("chmod", ondisk_path);
979 goto done;
983 done:
984 if (fd != -1 && close(fd) != 0 && err == NULL)
985 err = got_error_from_errno("close");
986 free(tmppath);
987 return err;
990 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
991 static const struct got_error *
992 get_modified_file_content_status(unsigned char *status, FILE *f)
994 const struct got_error *err = NULL;
995 const char *markers[3] = {
996 GOT_DIFF_CONFLICT_MARKER_BEGIN,
997 GOT_DIFF_CONFLICT_MARKER_SEP,
998 GOT_DIFF_CONFLICT_MARKER_END
999 };
1000 int i = 0;
1001 char *line;
1002 size_t len;
1003 const char delim[3] = {'\0', '\0', '\0'};
1005 while (*status == GOT_STATUS_MODIFY) {
1006 line = fparseln(f, &len, NULL, delim, 0);
1007 if (line == NULL) {
1008 if (feof(f))
1009 break;
1010 err = got_ferror(f, GOT_ERR_IO);
1011 break;
1014 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1015 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1016 == 0)
1017 *status = GOT_STATUS_CONFLICT;
1018 else
1019 i++;
1023 return err;
1026 static int
1027 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1029 return !(ie->ctime_sec == sb->st_ctime &&
1030 ie->ctime_nsec == sb->st_ctimensec &&
1031 ie->mtime_sec == sb->st_mtime &&
1032 ie->mtime_nsec == sb->st_mtimensec &&
1033 ie->size == (sb->st_size & 0xffffffff));
1036 static const struct got_error *
1037 get_file_status(unsigned char *status, struct stat *sb,
1038 struct got_fileindex_entry *ie, const char *abspath,
1039 struct got_repository *repo)
1041 const struct got_error *err = NULL;
1042 struct got_object_id id;
1043 size_t hdrlen;
1044 FILE *f = NULL;
1045 uint8_t fbuf[8192];
1046 struct got_blob_object *blob = NULL;
1047 size_t flen, blen;
1049 *status = GOT_STATUS_NO_CHANGE;
1051 if (lstat(abspath, sb) == -1) {
1052 if (errno == ENOENT) {
1053 if (ie) {
1054 if (got_fileindex_entry_has_file_on_disk(ie))
1055 *status = GOT_STATUS_MISSING;
1056 else
1057 *status = GOT_STATUS_DELETE;
1058 sb->st_mode =
1059 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1060 & (S_IRWXU | S_IRWXG | S_IRWXO));
1061 } else
1062 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1063 return NULL;
1065 return got_error_from_errno2("lstat", abspath);
1068 if (!S_ISREG(sb->st_mode)) {
1069 *status = GOT_STATUS_OBSTRUCTED;
1070 return NULL;
1073 if (ie == NULL)
1074 return NULL;
1076 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1077 *status = GOT_STATUS_DELETE;
1078 return NULL;
1079 } else if (!got_fileindex_entry_has_blob(ie)) {
1080 *status = GOT_STATUS_ADD;
1081 return NULL;
1084 if (!stat_info_differs(ie, sb))
1085 return NULL;
1087 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1088 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1089 if (err)
1090 return err;
1092 f = fopen(abspath, "r");
1093 if (f == NULL) {
1094 err = got_error_from_errno2("fopen", abspath);
1095 goto done;
1097 hdrlen = got_object_blob_get_hdrlen(blob);
1098 for (;;) {
1099 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1100 err = got_object_blob_read_block(&blen, blob);
1101 if (err)
1102 goto done;
1103 /* Skip length of blob object header first time around. */
1104 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1105 if (flen == 0 && ferror(f)) {
1106 err = got_error_from_errno("fread");
1107 goto done;
1109 if (blen == 0) {
1110 if (flen != 0)
1111 *status = GOT_STATUS_MODIFY;
1112 break;
1113 } else if (flen == 0) {
1114 if (blen != 0)
1115 *status = GOT_STATUS_MODIFY;
1116 break;
1117 } else if (blen - hdrlen == flen) {
1118 /* Skip blob object header first time around. */
1119 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1120 *status = GOT_STATUS_MODIFY;
1121 break;
1123 } else {
1124 *status = GOT_STATUS_MODIFY;
1125 break;
1127 hdrlen = 0;
1130 if (*status == GOT_STATUS_MODIFY) {
1131 rewind(f);
1132 err = get_modified_file_content_status(status, f);
1134 done:
1135 if (blob)
1136 got_object_blob_close(blob);
1137 if (f)
1138 fclose(f);
1139 return err;
1143 * Update timestamps in the file index if a file is unmodified and
1144 * we had to run a full content comparison to find out.
1146 static const struct got_error *
1147 sync_timestamps(char *ondisk_path, unsigned char status,
1148 struct got_fileindex_entry *ie, struct stat *sb)
1150 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1151 return got_fileindex_entry_update(ie, ondisk_path,
1152 ie->blob_sha1, ie->commit_sha1, 1);
1154 return NULL;
1157 static const struct got_error *
1158 update_blob(struct got_worktree *worktree,
1159 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1160 struct got_tree_entry *te, const char *path,
1161 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1162 void *progress_arg)
1164 const struct got_error *err = NULL;
1165 struct got_blob_object *blob = NULL;
1166 char *ondisk_path;
1167 unsigned char status = GOT_STATUS_NO_CHANGE;
1168 struct stat sb;
1170 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1171 return got_error_from_errno("asprintf");
1173 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1174 if (err)
1175 goto done;
1177 if (status == GOT_STATUS_OBSTRUCTED) {
1178 err = (*progress_cb)(progress_arg, status, path);
1179 goto done;
1182 if (ie && status != GOT_STATUS_MISSING) {
1183 if (got_fileindex_entry_has_commit(ie) &&
1184 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1185 SHA1_DIGEST_LENGTH) == 0) {
1186 err = sync_timestamps(ondisk_path, status, ie, &sb);
1187 if (err)
1188 goto done;
1189 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1190 path);
1191 goto done;
1193 if (got_fileindex_entry_has_blob(ie) &&
1194 memcmp(ie->blob_sha1, te->id->sha1,
1195 SHA1_DIGEST_LENGTH) == 0) {
1196 err = sync_timestamps(ondisk_path, status, ie, &sb);
1197 goto done;
1201 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1202 if (err)
1203 goto done;
1205 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1206 int update_timestamps;
1207 struct got_blob_object *blob2 = NULL;
1208 if (got_fileindex_entry_has_blob(ie)) {
1209 struct got_object_id id2;
1210 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1211 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1212 if (err)
1213 goto done;
1215 err = merge_blob(&update_timestamps, worktree, blob2,
1216 ondisk_path, path, sb.st_mode, blob,
1217 worktree->base_commit_id, repo,
1218 progress_cb, progress_arg);
1219 if (blob2)
1220 got_object_blob_close(blob2);
1222 * Do not update timestamps of files with local changes.
1223 * Otherwise, a future status walk would treat them as
1224 * unmodified files again.
1226 err = got_fileindex_entry_update(ie, ondisk_path,
1227 blob->id.sha1, worktree->base_commit_id->sha1,
1228 update_timestamps);
1229 } else if (status == GOT_STATUS_DELETE) {
1230 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1231 if (err)
1232 goto done;
1233 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1234 ondisk_path, path, blob, 0);
1235 if (err)
1236 goto done;
1237 } else {
1238 err = install_blob(worktree, ondisk_path, path, te->mode,
1239 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1240 repo, progress_cb, progress_arg);
1241 if (err)
1242 goto done;
1243 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1244 ondisk_path, path, blob, 1);
1245 if (err)
1246 goto done;
1248 got_object_blob_close(blob);
1249 done:
1250 free(ondisk_path);
1251 return err;
1254 static const struct got_error *
1255 remove_ondisk_file(const char *root_path, const char *path)
1257 const struct got_error *err = NULL;
1258 char *ondisk_path = NULL;
1260 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1261 return got_error_from_errno("asprintf");
1263 if (unlink(ondisk_path) == -1) {
1264 if (errno != ENOENT)
1265 err = got_error_from_errno2("unlink", ondisk_path);
1266 } else {
1267 char *parent = dirname(ondisk_path);
1268 while (parent && strcmp(parent, root_path) != 0) {
1269 if (rmdir(parent) == -1) {
1270 if (errno != ENOTEMPTY)
1271 err = got_error_from_errno2("rmdir",
1272 parent);
1273 break;
1275 parent = dirname(parent);
1278 free(ondisk_path);
1279 return err;
1282 static const struct got_error *
1283 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1284 struct got_fileindex_entry *ie, struct got_repository *repo,
1285 got_worktree_checkout_cb progress_cb, void *progress_arg)
1287 const struct got_error *err = NULL;
1288 unsigned char status;
1289 struct stat sb;
1290 char *ondisk_path;
1292 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1293 == -1)
1294 return got_error_from_errno("asprintf");
1296 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1297 if (err)
1298 return err;
1300 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1301 status == GOT_STATUS_ADD) {
1302 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1303 if (err)
1304 return err;
1306 * Preserve the working file and change the deleted blob's
1307 * entry into a schedule-add entry.
1309 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1310 0);
1311 if (err)
1312 return err;
1313 } else {
1314 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1315 if (err)
1316 return err;
1317 if (status == GOT_STATUS_NO_CHANGE) {
1318 err = remove_ondisk_file(worktree->root_path, ie->path);
1319 if (err)
1320 return err;
1322 got_fileindex_entry_remove(fileindex, ie);
1325 return err;
1328 struct diff_cb_arg {
1329 struct got_fileindex *fileindex;
1330 struct got_worktree *worktree;
1331 struct got_repository *repo;
1332 got_worktree_checkout_cb progress_cb;
1333 void *progress_arg;
1334 got_worktree_cancel_cb cancel_cb;
1335 void *cancel_arg;
1338 static const struct got_error *
1339 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1340 struct got_tree_entry *te, const char *parent_path)
1342 struct diff_cb_arg *a = arg;
1344 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1345 return got_error(GOT_ERR_CANCELLED);
1347 return update_blob(a->worktree, a->fileindex, ie, te,
1348 ie->path, a->repo, a->progress_cb, a->progress_arg);
1351 static const struct got_error *
1352 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1354 struct diff_cb_arg *a = arg;
1356 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1357 return got_error(GOT_ERR_CANCELLED);
1359 return delete_blob(a->worktree, a->fileindex, ie,
1360 a->repo, a->progress_cb, a->progress_arg);
1363 static const struct got_error *
1364 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1366 struct diff_cb_arg *a = arg;
1367 const struct got_error *err;
1368 char *path;
1370 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1371 return got_error(GOT_ERR_CANCELLED);
1373 if (asprintf(&path, "%s%s%s", parent_path,
1374 parent_path[0] ? "/" : "", te->name)
1375 == -1)
1376 return got_error_from_errno("asprintf");
1378 if (S_ISDIR(te->mode))
1379 err = add_dir_on_disk(a->worktree, path);
1380 else
1381 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1382 a->repo, a->progress_cb, a->progress_arg);
1384 free(path);
1385 return err;
1388 static const struct got_error *
1389 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1391 const struct got_error *err = NULL;
1392 char *uuidstr = NULL;
1393 uint32_t uuid_status;
1395 *refname = NULL;
1397 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1398 if (uuid_status != uuid_s_ok)
1399 return got_error_uuid(uuid_status);
1401 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1402 == -1) {
1403 err = got_error_from_errno("asprintf");
1404 *refname = NULL;
1406 free(uuidstr);
1407 return err;
1410 const struct got_error *
1411 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1413 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1416 static const struct got_error *
1417 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1419 return get_ref_name(refname, worktree,
1420 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1423 static const struct got_error *
1424 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1426 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1429 static const struct got_error *
1430 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1432 return get_ref_name(refname, worktree,
1433 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1436 static const struct got_error *
1437 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1439 return get_ref_name(refname, worktree,
1440 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1443 static const struct got_error *
1444 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1446 return get_ref_name(refname, worktree,
1447 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1450 static const struct got_error *
1451 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1453 return get_ref_name(refname, worktree,
1454 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1457 static const struct got_error *
1458 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1460 return get_ref_name(refname, worktree,
1461 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1464 static const struct got_error *
1465 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1467 return get_ref_name(refname, worktree,
1468 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1471 const struct got_error *
1472 got_worktree_get_histedit_list_path(char **path, struct got_worktree *worktree)
1474 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1475 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_LIST) == -1) {
1476 *path = NULL;
1477 return got_error_from_errno("asprintf");
1479 return NULL;
1483 * Prevent Git's garbage collector from deleting our base commit by
1484 * setting a reference to our base commit's ID.
1486 static const struct got_error *
1487 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1489 const struct got_error *err = NULL;
1490 struct got_reference *ref = NULL;
1491 char *refname;
1493 err = got_worktree_get_base_ref_name(&refname, worktree);
1494 if (err)
1495 return err;
1497 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1498 if (err)
1499 goto done;
1501 err = got_ref_write(ref, repo);
1502 done:
1503 free(refname);
1504 if (ref)
1505 got_ref_close(ref);
1506 return err;
1509 static const struct got_error *
1510 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1512 const struct got_error *err = NULL;
1514 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1515 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1516 err = got_error_from_errno("asprintf");
1517 *fileindex_path = NULL;
1519 return err;
1523 static const struct got_error *
1524 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1525 struct got_worktree *worktree)
1527 const struct got_error *err = NULL;
1528 FILE *index = NULL;
1530 *fileindex_path = NULL;
1531 *fileindex = got_fileindex_alloc();
1532 if (*fileindex == NULL)
1533 return got_error_from_errno("got_fileindex_alloc");
1535 err = get_fileindex_path(fileindex_path, worktree);
1536 if (err)
1537 goto done;
1539 index = fopen(*fileindex_path, "rb");
1540 if (index == NULL) {
1541 if (errno != ENOENT)
1542 err = got_error_from_errno2("fopen", *fileindex_path);
1543 } else {
1544 err = got_fileindex_read(*fileindex, index);
1545 if (fclose(index) != 0 && err == NULL)
1546 err = got_error_from_errno("fclose");
1548 done:
1549 if (err) {
1550 free(*fileindex_path);
1551 *fileindex_path = NULL;
1552 got_fileindex_free(*fileindex);
1553 *fileindex = NULL;
1555 return err;
1558 struct bump_base_commit_id_arg {
1559 struct got_object_id *base_commit_id;
1560 const char *path;
1561 size_t path_len;
1562 const char *entry_name;
1563 got_worktree_checkout_cb progress_cb;
1564 void *progress_arg;
1567 /* Bump base commit ID of all files within an updated part of the work tree. */
1568 static const struct got_error *
1569 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1571 const struct got_error *err;
1572 struct bump_base_commit_id_arg *a = arg;
1574 if (a->entry_name) {
1575 if (strcmp(ie->path, a->path) != 0)
1576 return NULL;
1577 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1578 return NULL;
1580 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1581 SHA1_DIGEST_LENGTH) == 0)
1582 return NULL;
1584 if (a->progress_cb) {
1585 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1586 ie->path);
1587 if (err)
1588 return err;
1590 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1591 return NULL;
1594 static const struct got_error *
1595 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1597 const struct got_error *err = NULL;
1598 char *new_fileindex_path = NULL;
1599 FILE *new_index = NULL;
1601 err = got_opentemp_named(&new_fileindex_path, &new_index,
1602 fileindex_path);
1603 if (err)
1604 goto done;
1606 err = got_fileindex_write(fileindex, new_index);
1607 if (err)
1608 goto done;
1610 if (rename(new_fileindex_path, fileindex_path) != 0) {
1611 err = got_error_from_errno3("rename", new_fileindex_path,
1612 fileindex_path);
1613 unlink(new_fileindex_path);
1615 done:
1616 if (new_index)
1617 fclose(new_index);
1618 free(new_fileindex_path);
1619 return err;
1622 static const struct got_error *
1623 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1624 struct got_object_id **tree_id, const char *wt_relpath,
1625 struct got_worktree *worktree, struct got_repository *repo)
1627 const struct got_error *err = NULL;
1628 struct got_object_id *id = NULL;
1629 char *in_repo_path = NULL;
1630 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1632 *entry_type = GOT_OBJ_TYPE_ANY;
1633 *tree_relpath = NULL;
1634 *tree_id = NULL;
1636 if (wt_relpath[0] == '\0') {
1637 /* Check out all files within the work tree. */
1638 *entry_type = GOT_OBJ_TYPE_TREE;
1639 *tree_relpath = strdup("");
1640 if (*tree_relpath == NULL) {
1641 err = got_error_from_errno("strdup");
1642 goto done;
1644 err = got_object_id_by_path(tree_id, repo,
1645 worktree->base_commit_id, worktree->path_prefix);
1646 if (err)
1647 goto done;
1648 return NULL;
1651 /* Check out a subset of files in the work tree. */
1653 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1654 is_root_wt ? "" : "/", wt_relpath) == -1) {
1655 err = got_error_from_errno("asprintf");
1656 goto done;
1659 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1660 in_repo_path);
1661 if (err)
1662 goto done;
1664 free(in_repo_path);
1665 in_repo_path = NULL;
1667 err = got_object_get_type(entry_type, repo, id);
1668 if (err)
1669 goto done;
1671 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1672 /* Check out a single file. */
1673 if (strchr(wt_relpath, '/') == NULL) {
1674 /* Check out a single file in work tree's root dir. */
1675 in_repo_path = strdup(worktree->path_prefix);
1676 if (in_repo_path == NULL) {
1677 err = got_error_from_errno("strdup");
1678 goto done;
1680 *tree_relpath = strdup("");
1681 if (*tree_relpath == NULL) {
1682 err = got_error_from_errno("strdup");
1683 goto done;
1685 } else {
1686 /* Check out a single file in a subdirectory. */
1687 err = got_path_dirname(tree_relpath, wt_relpath);
1688 if (err)
1689 return err;
1690 if (asprintf(&in_repo_path, "%s%s%s",
1691 worktree->path_prefix, is_root_wt ? "" : "/",
1692 *tree_relpath) == -1) {
1693 err = got_error_from_errno("asprintf");
1694 goto done;
1697 err = got_object_id_by_path(tree_id, repo,
1698 worktree->base_commit_id, in_repo_path);
1699 } else {
1700 /* Check out all files within a subdirectory. */
1701 *tree_id = got_object_id_dup(id);
1702 if (*tree_id == NULL) {
1703 err = got_error_from_errno("got_object_id_dup");
1704 goto done;
1706 *tree_relpath = strdup(wt_relpath);
1707 if (*tree_relpath == NULL) {
1708 err = got_error_from_errno("strdup");
1709 goto done;
1712 done:
1713 free(id);
1714 free(in_repo_path);
1715 if (err) {
1716 *entry_type = GOT_OBJ_TYPE_ANY;
1717 free(*tree_relpath);
1718 *tree_relpath = NULL;
1719 free(*tree_id);
1720 *tree_id = NULL;
1722 return err;
1725 static const struct got_error *
1726 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1727 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1728 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1729 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1731 const struct got_error *err = NULL;
1732 struct got_commit_object *commit = NULL;
1733 struct got_tree_object *tree = NULL;
1734 struct got_fileindex_diff_tree_cb diff_cb;
1735 struct diff_cb_arg arg;
1737 err = ref_base_commit(worktree, repo);
1738 if (err)
1739 goto done;
1741 err = got_object_open_as_commit(&commit, repo,
1742 worktree->base_commit_id);
1743 if (err)
1744 goto done;
1746 err = got_object_open_as_tree(&tree, repo, tree_id);
1747 if (err)
1748 goto done;
1750 if (entry_name &&
1751 got_object_tree_find_entry(tree, entry_name) == NULL) {
1752 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1753 goto done;
1756 diff_cb.diff_old_new = diff_old_new;
1757 diff_cb.diff_old = diff_old;
1758 diff_cb.diff_new = diff_new;
1759 arg.fileindex = fileindex;
1760 arg.worktree = worktree;
1761 arg.repo = repo;
1762 arg.progress_cb = progress_cb;
1763 arg.progress_arg = progress_arg;
1764 arg.cancel_cb = cancel_cb;
1765 arg.cancel_arg = cancel_arg;
1766 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1767 entry_name, repo, &diff_cb, &arg);
1768 done:
1769 if (tree)
1770 got_object_tree_close(tree);
1771 if (commit)
1772 got_object_commit_close(commit);
1773 return err;
1776 const struct got_error *
1777 got_worktree_checkout_files(struct got_worktree *worktree,
1778 struct got_pathlist_head *paths, struct got_repository *repo,
1779 got_worktree_checkout_cb progress_cb, void *progress_arg,
1780 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1782 const struct got_error *err = NULL, *sync_err, *unlockerr;
1783 struct got_commit_object *commit = NULL;
1784 struct got_tree_object *tree = NULL;
1785 struct got_fileindex *fileindex = NULL;
1786 char *fileindex_path = NULL;
1787 struct got_pathlist_entry *pe;
1788 struct tree_path_data {
1789 SIMPLEQ_ENTRY(tree_path_data) entry;
1790 struct got_object_id *tree_id;
1791 int entry_type;
1792 char *relpath;
1793 char *entry_name;
1794 } *tpd = NULL;
1795 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1797 SIMPLEQ_INIT(&tree_paths);
1799 err = lock_worktree(worktree, LOCK_EX);
1800 if (err)
1801 return err;
1803 /* Map all specified paths to in-repository trees. */
1804 TAILQ_FOREACH(pe, paths, entry) {
1805 tpd = malloc(sizeof(*tpd));
1806 if (tpd == NULL) {
1807 err = got_error_from_errno("malloc");
1808 goto done;
1811 err = find_tree_entry_for_checkout(&tpd->entry_type,
1812 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1813 if (err) {
1814 free(tpd);
1815 goto done;
1818 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1819 err = got_path_basename(&tpd->entry_name, pe->path);
1820 if (err) {
1821 free(tpd->relpath);
1822 free(tpd->tree_id);
1823 free(tpd);
1824 goto done;
1826 } else
1827 tpd->entry_name = NULL;
1829 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1833 * Read the file index.
1834 * Checking out files is supposed to be an idempotent operation.
1835 * If the on-disk file index is incomplete we will try to complete it.
1837 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1838 if (err)
1839 goto done;
1841 tpd = SIMPLEQ_FIRST(&tree_paths);
1842 TAILQ_FOREACH(pe, paths, entry) {
1843 struct bump_base_commit_id_arg bbc_arg;
1845 err = checkout_files(worktree, fileindex, tpd->relpath,
1846 tpd->tree_id, tpd->entry_name, repo,
1847 progress_cb, progress_arg, cancel_cb, cancel_arg);
1848 if (err)
1849 break;
1851 bbc_arg.base_commit_id = worktree->base_commit_id;
1852 bbc_arg.entry_name = tpd->entry_name;
1853 bbc_arg.path = pe->path;
1854 bbc_arg.path_len = strlen(pe->path);
1855 bbc_arg.progress_cb = progress_cb;
1856 bbc_arg.progress_arg = progress_arg;
1857 err = got_fileindex_for_each_entry_safe(fileindex,
1858 bump_base_commit_id, &bbc_arg);
1859 if (err)
1860 break;
1862 tpd = SIMPLEQ_NEXT(tpd, entry);
1864 sync_err = sync_fileindex(fileindex, fileindex_path);
1865 if (sync_err && err == NULL)
1866 err = sync_err;
1867 done:
1868 free(fileindex_path);
1869 if (tree)
1870 got_object_tree_close(tree);
1871 if (commit)
1872 got_object_commit_close(commit);
1873 if (fileindex)
1874 got_fileindex_free(fileindex);
1875 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1876 tpd = SIMPLEQ_FIRST(&tree_paths);
1877 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1878 free(tpd->relpath);
1879 free(tpd->tree_id);
1880 free(tpd);
1882 unlockerr = lock_worktree(worktree, LOCK_SH);
1883 if (unlockerr && err == NULL)
1884 err = unlockerr;
1885 return err;
1888 struct merge_file_cb_arg {
1889 struct got_worktree *worktree;
1890 struct got_fileindex *fileindex;
1891 got_worktree_checkout_cb progress_cb;
1892 void *progress_arg;
1893 got_worktree_cancel_cb cancel_cb;
1894 void *cancel_arg;
1895 struct got_object_id *commit_id2;
1898 static const struct got_error *
1899 merge_file_cb(void *arg, struct got_blob_object *blob1,
1900 struct got_blob_object *blob2, struct got_object_id *id1,
1901 struct got_object_id *id2, const char *path1, const char *path2,
1902 struct got_repository *repo)
1904 static const struct got_error *err = NULL;
1905 struct merge_file_cb_arg *a = arg;
1906 struct got_fileindex_entry *ie;
1907 char *ondisk_path = NULL;
1908 struct stat sb;
1909 unsigned char status;
1910 int local_changes_subsumed;
1912 if (blob1 && blob2) {
1913 ie = got_fileindex_entry_get(a->fileindex, path2);
1914 if (ie == NULL)
1915 return (*a->progress_cb)(a->progress_arg,
1916 GOT_STATUS_MISSING, path2);
1918 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1919 path2) == -1)
1920 return got_error_from_errno("asprintf");
1922 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1923 if (err)
1924 goto done;
1926 if (status == GOT_STATUS_DELETE) {
1927 err = (*a->progress_cb)(a->progress_arg,
1928 GOT_STATUS_MERGE, path2);
1929 goto done;
1931 if (status != GOT_STATUS_NO_CHANGE &&
1932 status != GOT_STATUS_MODIFY &&
1933 status != GOT_STATUS_CONFLICT &&
1934 status != GOT_STATUS_ADD) {
1935 err = (*a->progress_cb)(a->progress_arg, status, path2);
1936 goto done;
1939 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1940 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1941 a->progress_cb, a->progress_arg);
1942 } else if (blob1) {
1943 ie = got_fileindex_entry_get(a->fileindex, 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 if (ie) {
1999 err = get_file_status(&status, &sb, ie, ondisk_path,
2000 repo);
2001 if (err)
2002 goto done;
2003 if (status != GOT_STATUS_NO_CHANGE &&
2004 status != GOT_STATUS_MODIFY &&
2005 status != GOT_STATUS_CONFLICT &&
2006 status != GOT_STATUS_ADD) {
2007 err = (*a->progress_cb)(a->progress_arg,
2008 status, path2);
2009 goto done;
2011 err = merge_blob(&local_changes_subsumed, a->worktree,
2012 NULL, ondisk_path, path2, sb.st_mode, blob2,
2013 a->commit_id2, repo,
2014 a->progress_cb, a->progress_arg);
2015 if (status == GOT_STATUS_DELETE) {
2016 err = update_blob_fileindex_entry(a->worktree,
2017 a->fileindex, ie, ondisk_path, ie->path,
2018 blob2, 0);
2019 if (err)
2020 goto done;
2022 } else {
2023 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2024 err = install_blob(a->worktree, ondisk_path, path2,
2025 /* XXX get this from parent tree! */
2026 GOT_DEFAULT_FILE_MODE,
2027 sb.st_mode, blob2, 0, 0, repo,
2028 a->progress_cb, a->progress_arg);
2029 if (err)
2030 goto done;
2031 err = got_fileindex_entry_alloc(&ie,
2032 ondisk_path, path2, NULL, NULL);
2033 if (err)
2034 goto done;
2035 err = got_fileindex_entry_add(a->fileindex, ie);
2036 if (err) {
2037 got_fileindex_entry_free(ie);
2038 goto done;
2042 done:
2043 free(ondisk_path);
2044 return err;
2047 struct check_merge_ok_arg {
2048 struct got_worktree *worktree;
2049 struct got_repository *repo;
2052 static const struct got_error *
2053 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2055 const struct got_error *err = NULL;
2056 struct check_merge_ok_arg *a = arg;
2057 unsigned char status;
2058 struct stat sb;
2059 char *ondisk_path;
2061 /* Reject merges into a work tree with mixed base commits. */
2062 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2063 SHA1_DIGEST_LENGTH))
2064 return got_error(GOT_ERR_MIXED_COMMITS);
2066 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2067 == -1)
2068 return got_error_from_errno("asprintf");
2070 /* Reject merges into a work tree with conflicted files. */
2071 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2072 if (err)
2073 return err;
2074 if (status == GOT_STATUS_CONFLICT)
2075 return got_error(GOT_ERR_CONFLICTS);
2077 return NULL;
2080 static const struct got_error *
2081 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2082 const char *fileindex_path, struct got_object_id *commit_id1,
2083 struct got_object_id *commit_id2, struct got_repository *repo,
2084 got_worktree_checkout_cb progress_cb, void *progress_arg,
2085 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2087 const struct got_error *err = NULL, *sync_err;
2088 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2089 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2090 struct merge_file_cb_arg arg;
2092 if (commit_id1) {
2093 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2094 worktree->path_prefix);
2095 if (err)
2096 goto done;
2098 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2099 if (err)
2100 goto done;
2103 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2104 worktree->path_prefix);
2105 if (err)
2106 goto done;
2108 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2109 if (err)
2110 goto done;
2112 arg.worktree = worktree;
2113 arg.fileindex = fileindex;
2114 arg.progress_cb = progress_cb;
2115 arg.progress_arg = progress_arg;
2116 arg.cancel_cb = cancel_cb;
2117 arg.cancel_arg = cancel_arg;
2118 arg.commit_id2 = commit_id2;
2119 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2120 sync_err = sync_fileindex(fileindex, fileindex_path);
2121 if (sync_err && err == NULL)
2122 err = sync_err;
2123 done:
2124 if (tree1)
2125 got_object_tree_close(tree1);
2126 if (tree2)
2127 got_object_tree_close(tree2);
2128 return err;
2131 const struct got_error *
2132 got_worktree_merge_files(struct got_worktree *worktree,
2133 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2134 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2135 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2137 const struct got_error *err, *unlockerr;
2138 char *fileindex_path = NULL;
2139 struct got_fileindex *fileindex = NULL;
2140 struct check_merge_ok_arg mok_arg;
2142 err = lock_worktree(worktree, LOCK_EX);
2143 if (err)
2144 return err;
2146 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2147 if (err)
2148 goto done;
2150 mok_arg.worktree = worktree;
2151 mok_arg.repo = repo;
2152 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2153 &mok_arg);
2154 if (err)
2155 goto done;
2157 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2158 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2159 done:
2160 if (fileindex)
2161 got_fileindex_free(fileindex);
2162 free(fileindex_path);
2163 unlockerr = lock_worktree(worktree, LOCK_SH);
2164 if (unlockerr && err == NULL)
2165 err = unlockerr;
2166 return err;
2169 struct diff_dir_cb_arg {
2170 struct got_fileindex *fileindex;
2171 struct got_worktree *worktree;
2172 const char *status_path;
2173 size_t status_path_len;
2174 struct got_repository *repo;
2175 got_worktree_status_cb status_cb;
2176 void *status_arg;
2177 got_worktree_cancel_cb cancel_cb;
2178 void *cancel_arg;
2181 static const struct got_error *
2182 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2183 got_worktree_status_cb status_cb, void *status_arg,
2184 struct got_repository *repo)
2186 const struct got_error *err = NULL;
2187 unsigned char status = GOT_STATUS_NO_CHANGE;
2188 struct stat sb;
2189 struct got_object_id blob_id, commit_id;
2191 err = get_file_status(&status, &sb, ie, abspath, repo);
2192 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2193 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2194 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2195 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2196 &commit_id);
2198 return err;
2201 static const struct got_error *
2202 status_old_new(void *arg, struct got_fileindex_entry *ie,
2203 struct dirent *de, const char *parent_path)
2205 const struct got_error *err = NULL;
2206 struct diff_dir_cb_arg *a = arg;
2207 char *abspath;
2209 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2210 return got_error(GOT_ERR_CANCELLED);
2212 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2213 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2214 return NULL;
2216 if (parent_path[0]) {
2217 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2218 parent_path, de->d_name) == -1)
2219 return got_error_from_errno("asprintf");
2220 } else {
2221 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2222 de->d_name) == -1)
2223 return got_error_from_errno("asprintf");
2226 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2227 a->repo);
2228 free(abspath);
2229 return err;
2232 static const struct got_error *
2233 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2235 struct diff_dir_cb_arg *a = arg;
2236 struct got_object_id blob_id, commit_id;
2237 unsigned char status;
2239 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2240 return got_error(GOT_ERR_CANCELLED);
2242 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2243 return NULL;
2245 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2246 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2247 if (got_fileindex_entry_has_file_on_disk(ie))
2248 status = GOT_STATUS_MISSING;
2249 else
2250 status = GOT_STATUS_DELETE;
2251 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2252 &commit_id);
2255 static const struct got_error *
2256 status_new(void *arg, struct dirent *de, const char *parent_path)
2258 const struct got_error *err = NULL;
2259 struct diff_dir_cb_arg *a = arg;
2260 char *path = NULL;
2262 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2263 return got_error(GOT_ERR_CANCELLED);
2265 if (de->d_type == DT_DIR)
2266 return NULL;
2268 /* XXX ignore symlinks for now */
2269 if (de->d_type == DT_LNK)
2270 return NULL;
2272 if (parent_path[0]) {
2273 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2274 return got_error_from_errno("asprintf");
2275 } else {
2276 path = de->d_name;
2279 if (got_path_is_child(path, a->status_path, a->status_path_len))
2280 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2281 path, NULL, NULL);
2282 if (parent_path[0])
2283 free(path);
2284 return err;
2287 static const struct got_error *
2288 worktree_status(struct got_worktree *worktree, const char *path,
2289 struct got_fileindex *fileindex, struct got_repository *repo,
2290 got_worktree_status_cb status_cb, void *status_arg,
2291 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2293 const struct got_error *err = NULL;
2294 DIR *workdir = NULL;
2295 struct got_fileindex_diff_dir_cb fdiff_cb;
2296 struct diff_dir_cb_arg arg;
2297 char *ondisk_path = NULL;
2299 if (asprintf(&ondisk_path, "%s%s%s",
2300 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2301 err = got_error_from_errno("asprintf");
2302 goto done;
2304 workdir = opendir(ondisk_path);
2305 if (workdir == NULL) {
2306 if (errno == ENOTDIR || errno == ENOENT) {
2307 err = report_file_status(
2308 got_fileindex_entry_get(fileindex, path),
2309 ondisk_path, status_cb, status_arg, repo);
2310 goto done;
2311 } else {
2312 err = got_error_from_errno2("opendir", ondisk_path);
2313 goto done;
2316 fdiff_cb.diff_old_new = status_old_new;
2317 fdiff_cb.diff_old = status_old;
2318 fdiff_cb.diff_new = status_new;
2319 arg.fileindex = fileindex;
2320 arg.worktree = worktree;
2321 arg.status_path = path;
2322 arg.status_path_len = strlen(path);
2323 arg.repo = repo;
2324 arg.status_cb = status_cb;
2325 arg.status_arg = status_arg;
2326 arg.cancel_cb = cancel_cb;
2327 arg.cancel_arg = cancel_arg;
2328 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2329 path, repo, &fdiff_cb, &arg);
2330 done:
2331 if (workdir)
2332 closedir(workdir);
2333 free(ondisk_path);
2334 return err;
2337 const struct got_error *
2338 got_worktree_status(struct got_worktree *worktree,
2339 struct got_pathlist_head *paths, struct got_repository *repo,
2340 got_worktree_status_cb status_cb, void *status_arg,
2341 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2343 const struct got_error *err = NULL;
2344 char *fileindex_path = NULL;
2345 struct got_fileindex *fileindex = NULL;
2346 struct got_pathlist_entry *pe;
2348 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2349 if (err)
2350 return err;
2352 TAILQ_FOREACH(pe, paths, entry) {
2353 err = worktree_status(worktree, pe->path, fileindex, repo,
2354 status_cb, status_arg, cancel_cb, cancel_arg);
2355 if (err)
2356 break;
2358 free(fileindex_path);
2359 got_fileindex_free(fileindex);
2360 return err;
2363 const struct got_error *
2364 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2365 const char *arg)
2367 const struct got_error *err = NULL;
2368 char *resolved, *cwd = NULL, *path = NULL;
2369 size_t len;
2371 *wt_path = NULL;
2373 resolved = realpath(arg, NULL);
2374 if (resolved == NULL) {
2375 if (errno != ENOENT)
2376 return got_error_from_errno2("realpath", arg);
2377 cwd = getcwd(NULL, 0);
2378 if (cwd == NULL)
2379 return got_error_from_errno("getcwd");
2380 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2381 err = got_error_from_errno("asprintf");
2382 goto done;
2386 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2387 strlen(got_worktree_get_root_path(worktree)))) {
2388 err = got_error(GOT_ERR_BAD_PATH);
2389 goto done;
2392 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2393 err = got_path_skip_common_ancestor(&path,
2394 got_worktree_get_root_path(worktree), resolved);
2395 if (err)
2396 goto done;
2397 } else {
2398 path = strdup("");
2399 if (path == NULL) {
2400 err = got_error_from_errno("strdup");
2401 goto done;
2405 /* XXX status walk can't deal with trailing slash! */
2406 len = strlen(path);
2407 while (len > 0 && path[len - 1] == '/') {
2408 path[len - 1] = '\0';
2409 len--;
2411 done:
2412 free(resolved);
2413 free(cwd);
2414 if (err == NULL)
2415 *wt_path = path;
2416 else
2417 free(path);
2418 return err;
2421 static const struct got_error *
2422 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2423 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2424 struct got_repository *repo)
2426 const struct got_error *err = NULL;
2427 struct got_fileindex_entry *ie;
2429 /* Re-adding an existing entry is a no-op. */
2430 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2431 return NULL;
2433 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2434 if (err)
2435 return err;
2437 err = got_fileindex_entry_add(fileindex, ie);
2438 if (err) {
2439 got_fileindex_entry_free(ie);
2440 return err;
2443 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2446 const struct got_error *
2447 got_worktree_schedule_add(struct got_worktree *worktree,
2448 struct got_pathlist_head *ondisk_paths,
2449 got_worktree_status_cb status_cb, void *status_arg,
2450 struct got_repository *repo)
2452 struct got_fileindex *fileindex = NULL;
2453 char *fileindex_path = NULL;
2454 const struct got_error *err = NULL, *sync_err, *unlockerr;
2455 struct got_pathlist_entry *pe;
2457 err = lock_worktree(worktree, LOCK_EX);
2458 if (err)
2459 return err;
2461 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2462 if (err)
2463 goto done;
2465 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2466 char *relpath;
2467 err = got_path_skip_common_ancestor(&relpath,
2468 got_worktree_get_root_path(worktree), pe->path);
2469 if (err)
2470 break;
2471 err = schedule_addition(pe->path, fileindex, relpath,
2472 status_cb, status_arg, repo);
2473 free(relpath);
2474 if (err)
2475 break;
2477 sync_err = sync_fileindex(fileindex, fileindex_path);
2478 if (sync_err && err == NULL)
2479 err = sync_err;
2480 done:
2481 free(fileindex_path);
2482 if (fileindex)
2483 got_fileindex_free(fileindex);
2484 unlockerr = lock_worktree(worktree, LOCK_SH);
2485 if (unlockerr && err == NULL)
2486 err = unlockerr;
2487 return err;
2490 static const struct got_error *
2491 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2492 const char *relpath, int delete_local_mods,
2493 got_worktree_status_cb status_cb, void *status_arg,
2494 struct got_repository *repo)
2496 const struct got_error *err = NULL;
2497 struct got_fileindex_entry *ie = NULL;
2498 unsigned char status;
2499 struct stat sb;
2501 ie = got_fileindex_entry_get(fileindex, relpath);
2502 if (ie == NULL)
2503 return got_error(GOT_ERR_BAD_PATH);
2505 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2506 if (err)
2507 return err;
2509 if (status != GOT_STATUS_NO_CHANGE) {
2510 if (status == GOT_STATUS_DELETE)
2511 return got_error_set_errno(ENOENT, ondisk_path);
2512 if (status != GOT_STATUS_MODIFY)
2513 return got_error(GOT_ERR_FILE_STATUS);
2514 if (!delete_local_mods)
2515 return got_error(GOT_ERR_FILE_MODIFIED);
2518 if (unlink(ondisk_path) != 0)
2519 return got_error_from_errno2("unlink", ondisk_path);
2521 got_fileindex_entry_mark_deleted_from_disk(ie);
2522 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2525 const struct got_error *
2526 got_worktree_schedule_delete(struct got_worktree *worktree,
2527 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2528 got_worktree_status_cb status_cb, void *status_arg,
2529 struct got_repository *repo)
2531 struct got_fileindex *fileindex = NULL;
2532 char *fileindex_path = NULL;
2533 const struct got_error *err = NULL, *sync_err, *unlockerr;
2534 struct got_pathlist_entry *pe;
2536 err = lock_worktree(worktree, LOCK_EX);
2537 if (err)
2538 return err;
2540 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2541 if (err)
2542 goto done;
2544 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2545 char *relpath;
2546 err = got_path_skip_common_ancestor(&relpath,
2547 got_worktree_get_root_path(worktree), pe->path);
2548 if (err)
2549 break;
2550 err = schedule_for_deletion(pe->path, fileindex, relpath,
2551 delete_local_mods, status_cb, status_arg, repo);
2552 free(relpath);
2553 if (err)
2554 break;
2556 sync_err = sync_fileindex(fileindex, fileindex_path);
2557 if (sync_err && err == NULL)
2558 err = sync_err;
2559 done:
2560 free(fileindex_path);
2561 if (fileindex)
2562 got_fileindex_free(fileindex);
2563 unlockerr = lock_worktree(worktree, LOCK_SH);
2564 if (unlockerr && err == NULL)
2565 err = unlockerr;
2566 return err;
2569 static const struct got_error *
2570 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2571 const char *ondisk_path,
2572 got_worktree_checkout_cb progress_cb, void *progress_arg,
2573 struct got_repository *repo)
2575 const struct got_error *err = NULL;
2576 char *relpath = NULL, *parent_path = NULL;
2577 struct got_fileindex_entry *ie;
2578 struct got_tree_object *tree = NULL;
2579 struct got_object_id *tree_id = NULL;
2580 const struct got_tree_entry *te;
2581 char *tree_path = NULL, *te_name;
2582 struct got_blob_object *blob = NULL;
2583 unsigned char status;
2584 struct stat sb;
2586 err = got_path_skip_common_ancestor(&relpath,
2587 got_worktree_get_root_path(worktree), ondisk_path);
2588 if (err)
2589 goto done;
2591 ie = got_fileindex_entry_get(fileindex, relpath);
2592 if (ie == NULL) {
2593 err = got_error(GOT_ERR_BAD_PATH);
2594 goto done;
2597 /* Construct in-repository path of tree which contains this blob. */
2598 err = got_path_dirname(&parent_path, ie->path);
2599 if (err) {
2600 if (err->code != GOT_ERR_BAD_PATH)
2601 goto done;
2602 parent_path = strdup("/");
2603 if (parent_path == NULL) {
2604 err = got_error_from_errno("strdup");
2605 goto done;
2608 if (got_path_is_root_dir(worktree->path_prefix)) {
2609 tree_path = strdup(parent_path);
2610 if (tree_path == NULL) {
2611 err = got_error_from_errno("strdup");
2612 goto done;
2614 } else {
2615 if (got_path_is_root_dir(parent_path)) {
2616 tree_path = strdup(worktree->path_prefix);
2617 if (tree_path == NULL) {
2618 err = got_error_from_errno("strdup");
2619 goto done;
2621 } else {
2622 if (asprintf(&tree_path, "%s/%s",
2623 worktree->path_prefix, parent_path) == -1) {
2624 err = got_error_from_errno("asprintf");
2625 goto done;
2630 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2631 tree_path);
2632 if (err)
2633 goto done;
2635 err = got_object_open_as_tree(&tree, repo, tree_id);
2636 if (err)
2637 goto done;
2639 te_name = basename(ie->path);
2640 if (te_name == NULL) {
2641 err = got_error_from_errno2("basename", ie->path);
2642 goto done;
2645 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2646 if (err)
2647 goto done;
2649 te = got_object_tree_find_entry(tree, te_name);
2650 if (te == NULL && status != GOT_STATUS_ADD) {
2651 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2652 goto done;
2655 switch (status) {
2656 case GOT_STATUS_ADD:
2657 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2658 if (err)
2659 goto done;
2660 got_fileindex_entry_remove(fileindex, ie);
2661 break;
2662 case GOT_STATUS_DELETE:
2663 case GOT_STATUS_MODIFY:
2664 case GOT_STATUS_CONFLICT:
2665 case GOT_STATUS_MISSING: {
2666 struct got_object_id id;
2667 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2668 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2669 if (err)
2670 goto done;
2671 err = install_blob(worktree, ondisk_path, ie->path,
2672 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2673 progress_arg);
2674 if (err)
2675 goto done;
2676 if (status == GOT_STATUS_DELETE) {
2677 err = update_blob_fileindex_entry(worktree,
2678 fileindex, ie, ondisk_path, ie->path, blob, 1);
2679 if (err)
2680 goto done;
2682 break;
2684 default:
2685 goto done;
2687 done:
2688 free(relpath);
2689 free(parent_path);
2690 free(tree_path);
2691 if (blob)
2692 got_object_blob_close(blob);
2693 if (tree)
2694 got_object_tree_close(tree);
2695 free(tree_id);
2696 return err;
2699 const struct got_error *
2700 got_worktree_revert(struct got_worktree *worktree,
2701 struct got_pathlist_head *ondisk_paths,
2702 got_worktree_checkout_cb progress_cb, void *progress_arg,
2703 struct got_repository *repo)
2705 struct got_fileindex *fileindex = NULL;
2706 char *fileindex_path = NULL;
2707 const struct got_error *err = NULL, *unlockerr = NULL;
2708 const struct got_error *sync_err = NULL;
2709 struct got_pathlist_entry *pe;
2711 err = lock_worktree(worktree, LOCK_EX);
2712 if (err)
2713 return err;
2715 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2716 if (err)
2717 goto done;
2719 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2720 err = revert_file(worktree, fileindex, pe->path,
2721 progress_cb, progress_arg, repo);
2722 if (err)
2723 break;
2725 sync_err = sync_fileindex(fileindex, fileindex_path);
2726 if (sync_err && err == NULL)
2727 err = sync_err;
2728 done:
2729 free(fileindex_path);
2730 if (fileindex)
2731 got_fileindex_free(fileindex);
2732 unlockerr = lock_worktree(worktree, LOCK_SH);
2733 if (unlockerr && err == NULL)
2734 err = unlockerr;
2735 return err;
2738 static void
2739 free_commitable(struct got_commitable *ct)
2741 free(ct->path);
2742 free(ct->in_repo_path);
2743 free(ct->ondisk_path);
2744 free(ct->blob_id);
2745 free(ct->base_blob_id);
2746 free(ct->base_commit_id);
2747 free(ct);
2750 struct collect_commitables_arg {
2751 struct got_pathlist_head *commitable_paths;
2752 struct got_repository *repo;
2753 struct got_worktree *worktree;
2756 static const struct got_error *
2757 collect_commitables(void *arg, unsigned char status, const char *relpath,
2758 struct got_object_id *blob_id, struct got_object_id *commit_id)
2760 struct collect_commitables_arg *a = arg;
2761 const struct got_error *err = NULL;
2762 struct got_commitable *ct = NULL;
2763 struct got_pathlist_entry *new = NULL;
2764 char *parent_path = NULL, *path = NULL;
2765 struct stat sb;
2767 if (status == GOT_STATUS_CONFLICT)
2768 return got_error(GOT_ERR_COMMIT_CONFLICT);
2770 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2771 status != GOT_STATUS_DELETE)
2772 return NULL;
2774 if (asprintf(&path, "/%s", relpath) == -1) {
2775 err = got_error_from_errno("asprintf");
2776 goto done;
2778 if (strcmp(path, "/") == 0) {
2779 parent_path = strdup("");
2780 if (parent_path == NULL)
2781 return got_error_from_errno("strdup");
2782 } else {
2783 err = got_path_dirname(&parent_path, path);
2784 if (err)
2785 return err;
2788 ct = calloc(1, sizeof(*ct));
2789 if (ct == NULL) {
2790 err = got_error_from_errno("calloc");
2791 goto done;
2794 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2795 relpath) == -1) {
2796 err = got_error_from_errno("asprintf");
2797 goto done;
2799 if (status == GOT_STATUS_DELETE) {
2800 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2801 } else {
2802 if (lstat(ct->ondisk_path, &sb) != 0) {
2803 err = got_error_from_errno2("lstat", ct->ondisk_path);
2804 goto done;
2806 ct->mode = sb.st_mode;
2809 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2810 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2811 relpath) == -1) {
2812 err = got_error_from_errno("asprintf");
2813 goto done;
2816 ct->status = status;
2817 ct->blob_id = NULL; /* will be filled in when blob gets created */
2818 if (ct->status != GOT_STATUS_ADD) {
2819 ct->base_blob_id = got_object_id_dup(blob_id);
2820 if (ct->base_blob_id == NULL) {
2821 err = got_error_from_errno("got_object_id_dup");
2822 goto done;
2824 ct->base_commit_id = got_object_id_dup(commit_id);
2825 if (ct->base_commit_id == NULL) {
2826 err = got_error_from_errno("got_object_id_dup");
2827 goto done;
2830 ct->path = strdup(path);
2831 if (ct->path == NULL) {
2832 err = got_error_from_errno("strdup");
2833 goto done;
2835 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2836 done:
2837 if (ct && (err || new == NULL))
2838 free_commitable(ct);
2839 free(parent_path);
2840 free(path);
2841 return err;
2844 static const struct got_error *write_tree(struct got_object_id **,
2845 struct got_tree_object *, const char *, struct got_pathlist_head *,
2846 got_worktree_status_cb status_cb, void *status_arg,
2847 struct got_repository *);
2849 static const struct got_error *
2850 write_subtree(struct got_object_id **new_subtree_id,
2851 struct got_tree_entry *te, const char *parent_path,
2852 struct got_pathlist_head *commitable_paths,
2853 got_worktree_status_cb status_cb, void *status_arg,
2854 struct got_repository *repo)
2856 const struct got_error *err = NULL;
2857 struct got_tree_object *subtree;
2858 char *subpath;
2860 if (asprintf(&subpath, "%s%s%s", parent_path,
2861 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2862 return got_error_from_errno("asprintf");
2864 err = got_object_open_as_tree(&subtree, repo, te->id);
2865 if (err)
2866 return err;
2868 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2869 status_cb, status_arg, repo);
2870 got_object_tree_close(subtree);
2871 free(subpath);
2872 return err;
2875 static const struct got_error *
2876 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2878 const struct got_error *err = NULL;
2879 char *ct_parent_path = NULL;
2881 *match = 0;
2883 if (strchr(ct->in_repo_path, '/') == NULL) {
2884 *match = got_path_is_root_dir(path);
2885 return NULL;
2888 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2889 if (err)
2890 return err;
2891 *match = (strcmp(path, ct_parent_path) == 0);
2892 free(ct_parent_path);
2893 return err;
2896 static mode_t
2897 get_ct_file_mode(struct got_commitable *ct)
2899 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2902 static const struct got_error *
2903 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2904 struct got_tree_entry *te, struct got_commitable *ct)
2906 const struct got_error *err = NULL;
2908 *new_te = NULL;
2910 err = got_object_tree_entry_dup(new_te, te);
2911 if (err)
2912 goto done;
2914 (*new_te)->mode = get_ct_file_mode(ct);
2916 free((*new_te)->id);
2917 (*new_te)->id = got_object_id_dup(ct->blob_id);
2918 if ((*new_te)->id == NULL) {
2919 err = got_error_from_errno("got_object_id_dup");
2920 goto done;
2922 done:
2923 if (err && *new_te) {
2924 got_object_tree_entry_close(*new_te);
2925 *new_te = NULL;
2927 return err;
2930 static const struct got_error *
2931 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2932 struct got_commitable *ct)
2934 const struct got_error *err = NULL;
2935 char *ct_name;
2937 *new_te = NULL;
2939 *new_te = calloc(1, sizeof(**new_te));
2940 if (*new_te == NULL)
2941 return got_error_from_errno("calloc");
2943 ct_name = basename(ct->path);
2944 if (ct_name == NULL) {
2945 err = got_error_from_errno2("basename", ct->path);
2946 goto done;
2948 (*new_te)->name = strdup(ct_name);
2949 if ((*new_te)->name == NULL) {
2950 err = got_error_from_errno("strdup");
2951 goto done;
2954 (*new_te)->mode = get_ct_file_mode(ct);
2956 (*new_te)->id = got_object_id_dup(ct->blob_id);
2957 if ((*new_te)->id == NULL) {
2958 err = got_error_from_errno("got_object_id_dup");
2959 goto done;
2961 done:
2962 if (err && *new_te) {
2963 got_object_tree_entry_close(*new_te);
2964 *new_te = NULL;
2966 return err;
2969 static const struct got_error *
2970 insert_tree_entry(struct got_tree_entry *new_te,
2971 struct got_pathlist_head *paths)
2973 const struct got_error *err = NULL;
2974 struct got_pathlist_entry *new_pe;
2976 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2977 if (err)
2978 return err;
2979 if (new_pe == NULL)
2980 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2981 return NULL;
2984 static const struct got_error *
2985 report_ct_status(struct got_commitable *ct,
2986 got_worktree_status_cb status_cb, void *status_arg)
2988 const char *ct_path = ct->path;
2989 while (ct_path[0] == '/')
2990 ct_path++;
2991 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2994 static const struct got_error *
2995 match_modified_subtree(int *modified, struct got_tree_entry *te,
2996 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2998 const struct got_error *err = NULL;
2999 struct got_pathlist_entry *pe;
3000 char *te_path;
3002 *modified = 0;
3004 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3005 got_path_is_root_dir(base_tree_path) ? "" : "/",
3006 te->name) == -1)
3007 return got_error_from_errno("asprintf");
3009 TAILQ_FOREACH(pe, commitable_paths, entry) {
3010 struct got_commitable *ct = pe->data;
3011 *modified = got_path_is_child(ct->in_repo_path, te_path,
3012 strlen(te_path));
3013 if (*modified)
3014 break;
3017 free(te_path);
3018 return err;
3021 static const struct got_error *
3022 match_deleted_or_modified_ct(struct got_commitable **ctp,
3023 struct got_tree_entry *te, const char *base_tree_path,
3024 struct got_pathlist_head *commitable_paths)
3026 const struct got_error *err = NULL;
3027 struct got_pathlist_entry *pe;
3029 *ctp = NULL;
3031 TAILQ_FOREACH(pe, commitable_paths, entry) {
3032 struct got_commitable *ct = pe->data;
3033 char *ct_name = NULL;
3034 int path_matches;
3036 if (ct->status != GOT_STATUS_MODIFY &&
3037 ct->status != GOT_STATUS_DELETE)
3038 continue;
3040 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3041 continue;
3043 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3044 if (err)
3045 return err;
3046 if (!path_matches)
3047 continue;
3049 ct_name = basename(pe->path);
3050 if (ct_name == NULL)
3051 return got_error_from_errno2("basename", pe->path);
3053 if (strcmp(te->name, ct_name) != 0)
3054 continue;
3056 *ctp = ct;
3057 break;
3060 return err;
3063 static const struct got_error *
3064 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3065 const char *child_path, const char *path_base_tree,
3066 struct got_pathlist_head *commitable_paths,
3067 got_worktree_status_cb status_cb, void *status_arg,
3068 struct got_repository *repo)
3070 const struct got_error *err = NULL;
3071 struct got_tree_entry *new_te;
3072 char *subtree_path;
3074 *new_tep = NULL;
3076 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3077 got_path_is_root_dir(path_base_tree) ? "" : "/",
3078 child_path) == -1)
3079 return got_error_from_errno("asprintf");
3081 new_te = calloc(1, sizeof(*new_te));
3082 new_te->mode = S_IFDIR;
3083 new_te->name = strdup(child_path);
3084 if (new_te->name == NULL) {
3085 err = got_error_from_errno("strdup");
3086 got_object_tree_entry_close(new_te);
3087 goto done;
3089 err = write_tree(&new_te->id, NULL, subtree_path,
3090 commitable_paths, status_cb, status_arg, repo);
3091 if (err) {
3092 got_object_tree_entry_close(new_te);
3093 goto done;
3095 done:
3096 free(subtree_path);
3097 if (err == NULL)
3098 *new_tep = new_te;
3099 return err;
3102 static const struct got_error *
3103 write_tree(struct got_object_id **new_tree_id,
3104 struct got_tree_object *base_tree, const char *path_base_tree,
3105 struct got_pathlist_head *commitable_paths,
3106 got_worktree_status_cb status_cb, void *status_arg,
3107 struct got_repository *repo)
3109 const struct got_error *err = NULL;
3110 const struct got_tree_entries *base_entries = NULL;
3111 struct got_pathlist_head paths;
3112 struct got_tree_entries new_tree_entries;
3113 struct got_tree_entry *te, *new_te = NULL;
3114 struct got_pathlist_entry *pe;
3116 TAILQ_INIT(&paths);
3117 new_tree_entries.nentries = 0;
3118 SIMPLEQ_INIT(&new_tree_entries.head);
3120 /* Insert, and recurse into, newly added entries first. */
3121 TAILQ_FOREACH(pe, commitable_paths, entry) {
3122 struct got_commitable *ct = pe->data;
3123 char *child_path = NULL, *slash;
3125 if (ct->status != GOT_STATUS_ADD ||
3126 (ct->flags & GOT_COMMITABLE_ADDED))
3127 continue;
3129 if (!got_path_is_child(pe->path, path_base_tree,
3130 strlen(path_base_tree)))
3131 continue;
3133 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3134 pe->path);
3135 if (err)
3136 goto done;
3138 slash = strchr(child_path, '/');
3139 if (slash == NULL) {
3140 err = alloc_added_blob_tree_entry(&new_te, ct);
3141 if (err)
3142 goto done;
3143 err = report_ct_status(ct, status_cb, status_arg);
3144 if (err)
3145 goto done;
3146 ct->flags |= GOT_COMMITABLE_ADDED;
3147 err = insert_tree_entry(new_te, &paths);
3148 if (err)
3149 goto done;
3150 } else {
3151 *slash = '\0'; /* trim trailing path components */
3152 if (base_tree == NULL ||
3153 got_object_tree_find_entry(base_tree, child_path)
3154 == NULL) {
3155 err = make_subtree_for_added_blob(&new_te,
3156 child_path, path_base_tree,
3157 commitable_paths, status_cb, status_arg,
3158 repo);
3159 if (err)
3160 goto done;
3161 err = insert_tree_entry(new_te, &paths);
3162 if (err)
3163 goto done;
3168 if (base_tree) {
3169 /* Handle modified and deleted entries. */
3170 base_entries = got_object_tree_get_entries(base_tree);
3171 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3172 struct got_commitable *ct = NULL;
3174 if (S_ISDIR(te->mode)) {
3175 int modified;
3176 err = got_object_tree_entry_dup(&new_te, te);
3177 if (err)
3178 goto done;
3179 err = match_modified_subtree(&modified, te,
3180 path_base_tree, commitable_paths);
3181 if (err)
3182 goto done;
3183 /* Avoid recursion into unmodified subtrees. */
3184 if (modified) {
3185 free(new_te->id);
3186 err = write_subtree(&new_te->id, te,
3187 path_base_tree, commitable_paths,
3188 status_cb, status_arg, repo);
3189 if (err)
3190 goto done;
3192 err = insert_tree_entry(new_te, &paths);
3193 if (err)
3194 goto done;
3195 continue;
3198 err = match_deleted_or_modified_ct(&ct, te,
3199 path_base_tree, commitable_paths);
3200 if (ct) {
3201 /* NB: Deleted entries get dropped here. */
3202 if (ct->status == GOT_STATUS_MODIFY) {
3203 err = alloc_modified_blob_tree_entry(
3204 &new_te, te, ct);
3205 if (err)
3206 goto done;
3207 err = insert_tree_entry(new_te, &paths);
3208 if (err)
3209 goto done;
3211 err = report_ct_status(ct, status_cb,
3212 status_arg);
3213 if (err)
3214 goto done;
3215 } else {
3216 /* Entry is unchanged; just copy it. */
3217 err = got_object_tree_entry_dup(&new_te, te);
3218 if (err)
3219 goto done;
3220 err = insert_tree_entry(new_te, &paths);
3221 if (err)
3222 goto done;
3227 /* Write new list of entries; deleted entries have been dropped. */
3228 TAILQ_FOREACH(pe, &paths, entry) {
3229 struct got_tree_entry *te = pe->data;
3230 new_tree_entries.nentries++;
3231 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3233 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3234 done:
3235 got_object_tree_entries_close(&new_tree_entries);
3236 got_pathlist_free(&paths);
3237 return err;
3240 static const struct got_error *
3241 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3242 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3244 const struct got_error *err = NULL;
3245 struct got_pathlist_entry *pe;
3247 TAILQ_FOREACH(pe, commitable_paths, entry) {
3248 struct got_fileindex_entry *ie;
3249 struct got_commitable *ct = pe->data;
3251 ie = got_fileindex_entry_get(fileindex, pe->path);
3252 if (ie) {
3253 if (ct->status == GOT_STATUS_DELETE) {
3254 got_fileindex_entry_remove(fileindex, ie);
3255 got_fileindex_entry_free(ie);
3256 } else
3257 err = got_fileindex_entry_update(ie,
3258 ct->ondisk_path, ct->blob_id->sha1,
3259 new_base_commit_id->sha1, 1);
3260 } else {
3261 err = got_fileindex_entry_alloc(&ie,
3262 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3263 new_base_commit_id->sha1);
3264 if (err)
3265 break;
3266 err = got_fileindex_entry_add(fileindex, ie);
3267 if (err)
3268 break;
3271 return err;
3274 static const struct got_error *
3275 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3276 struct got_object_id *head_commit_id)
3278 const struct got_error *err = NULL;
3279 struct got_object_id *id_in_head = NULL, *id = NULL;
3280 struct got_commit_object *commit = NULL;
3281 char *path = NULL;
3282 const char *ct_path = ct->in_repo_path;
3284 while (ct_path[0] == '/')
3285 ct_path++;
3288 * Ensure that no modifications were made to files *and their parents*
3289 * in commits between the file's base commit and the branch head.
3291 * Checking the parents is important for detecting conflicting tree
3292 * configurations (files or parent folders might have been moved,
3293 * deleted, added again, etc.). Such changes need to be merged with
3294 * local changes before a commit can occur.
3296 * The implication is that the file's (parent) entry in the root
3297 * directory must have the same ID in all relevant commits.
3299 if (ct->status != GOT_STATUS_ADD) {
3300 struct got_object_qid *pid;
3301 char *slash;
3302 struct got_object_id *root_entry_id = NULL;
3304 /* Trivial case: base commit == head commit */
3305 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3306 return NULL;
3308 /* Compute the path to the root directory's entry. */
3309 path = strdup(ct_path);
3310 if (path == NULL) {
3311 err = got_error_from_errno("strdup");
3312 goto done;
3314 slash = strchr(path, '/');
3315 if (slash)
3316 *slash = '\0';
3318 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3319 if (err)
3320 goto done;
3322 err = got_object_id_by_path(&root_entry_id, repo,
3323 head_commit_id, path);
3324 if (err)
3325 goto done;
3327 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3328 while (pid) {
3329 struct got_commit_object *pcommit;
3331 err = got_object_id_by_path(&id, repo, pid->id, path);
3332 if (err) {
3333 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3334 goto done;
3335 err = NULL;
3336 break;
3339 err = got_object_id_by_path(&id, repo, pid->id, path);
3340 if (err)
3341 goto done;
3343 if (got_object_id_cmp(id, root_entry_id) != 0) {
3344 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3345 break;
3348 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3349 break; /* all relevant commits scanned */
3351 err = got_object_open_as_commit(&pcommit, repo,
3352 pid->id);
3353 if (err)
3354 goto done;
3356 got_object_commit_close(commit);
3357 commit = pcommit;
3358 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3359 commit));
3361 } else {
3362 /* Require that added files don't exist in the branch head. */
3363 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3364 ct_path);
3365 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3366 goto done;
3367 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3369 done:
3370 if (commit)
3371 got_object_commit_close(commit);
3372 free(id_in_head);
3373 free(id);
3374 free(path);
3375 return err;
3378 const struct got_error *
3379 commit_worktree(struct got_object_id **new_commit_id,
3380 struct got_pathlist_head *commitable_paths,
3381 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3382 const char *ondisk_path, const char *author, const char *committer,
3383 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3384 got_worktree_status_cb status_cb, void *status_arg,
3385 struct got_repository *repo)
3387 const struct got_error *err = NULL, *unlockerr = NULL;
3388 struct got_pathlist_entry *pe;
3389 const char *head_ref_name = NULL;
3390 struct got_commit_object *head_commit = NULL;
3391 struct got_reference *head_ref2 = NULL;
3392 struct got_object_id *head_commit_id2 = NULL;
3393 struct got_tree_object *head_tree = NULL;
3394 struct got_object_id *new_tree_id = NULL;
3395 struct got_object_id_queue parent_ids;
3396 struct got_object_qid *pid = NULL;
3397 char *logmsg = NULL;
3399 *new_commit_id = NULL;
3401 SIMPLEQ_INIT(&parent_ids);
3403 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3404 if (err)
3405 goto done;
3407 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3408 if (err)
3409 goto done;
3411 if (commit_msg_cb != NULL) {
3412 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3413 if (err)
3414 goto done;
3417 if (logmsg == NULL || strlen(logmsg) == 0) {
3418 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3419 goto done;
3422 /* Create blobs from added and modified files and record their IDs. */
3423 TAILQ_FOREACH(pe, commitable_paths, entry) {
3424 struct got_commitable *ct = pe->data;
3425 char *ondisk_path;
3427 if (ct->status != GOT_STATUS_ADD &&
3428 ct->status != GOT_STATUS_MODIFY)
3429 continue;
3431 if (asprintf(&ondisk_path, "%s/%s",
3432 worktree->root_path, pe->path) == -1) {
3433 err = got_error_from_errno("asprintf");
3434 goto done;
3436 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3437 free(ondisk_path);
3438 if (err)
3439 goto done;
3442 /* Recursively write new tree objects. */
3443 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3444 status_cb, status_arg, repo);
3445 if (err)
3446 goto done;
3448 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3449 if (err)
3450 goto done;
3451 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3452 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3453 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3454 got_object_qid_free(pid);
3455 if (logmsg != NULL)
3456 free(logmsg);
3457 if (err)
3458 goto done;
3460 /* Check if a concurrent commit to our branch has occurred. */
3461 head_ref_name = got_worktree_get_head_ref_name(worktree);
3462 if (head_ref_name == NULL) {
3463 err = got_error_from_errno("got_worktree_get_head_ref_name");
3464 goto done;
3466 /* Lock the reference here to prevent concurrent modification. */
3467 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3468 if (err)
3469 goto done;
3470 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3471 if (err)
3472 goto done;
3473 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3474 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3475 goto done;
3477 /* Update branch head in repository. */
3478 err = got_ref_change_ref(head_ref2, *new_commit_id);
3479 if (err)
3480 goto done;
3481 err = got_ref_write(head_ref2, repo);
3482 if (err)
3483 goto done;
3485 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3486 if (err)
3487 goto done;
3489 err = ref_base_commit(worktree, repo);
3490 if (err)
3491 goto done;
3492 done:
3493 if (head_tree)
3494 got_object_tree_close(head_tree);
3495 if (head_commit)
3496 got_object_commit_close(head_commit);
3497 free(head_commit_id2);
3498 if (head_ref2) {
3499 unlockerr = got_ref_unlock(head_ref2);
3500 if (unlockerr && err == NULL)
3501 err = unlockerr;
3502 got_ref_close(head_ref2);
3504 return err;
3507 const struct got_error *
3508 got_worktree_commit(struct got_object_id **new_commit_id,
3509 struct got_worktree *worktree, const char *ondisk_path,
3510 const char *author, const char *committer,
3511 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3512 got_worktree_status_cb status_cb, void *status_arg,
3513 struct got_repository *repo)
3515 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3516 struct got_fileindex *fileindex = NULL;
3517 char *fileindex_path = NULL, *relpath = NULL;
3518 struct got_pathlist_head commitable_paths;
3519 struct collect_commitables_arg cc_arg;
3520 struct got_pathlist_entry *pe;
3521 struct got_reference *head_ref = NULL;
3522 struct got_object_id *head_commit_id = NULL;
3524 *new_commit_id = NULL;
3526 TAILQ_INIT(&commitable_paths);
3528 err = lock_worktree(worktree, LOCK_EX);
3529 if (err)
3530 goto done;
3532 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3533 if (err)
3534 goto done;
3536 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3537 if (err)
3538 goto done;
3540 if (ondisk_path) {
3541 if (strcmp(ondisk_path, worktree->root_path) == 0) {
3542 relpath = strdup("");
3543 if (relpath == NULL) {
3544 err = got_error_from_errno("strdup");
3545 goto done;
3547 } else {
3548 err = got_path_skip_common_ancestor(&relpath,
3549 worktree->root_path, ondisk_path);
3550 if (err)
3551 return err;
3555 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3556 if (err)
3557 goto done;
3559 cc_arg.commitable_paths = &commitable_paths;
3560 cc_arg.worktree = worktree;
3561 cc_arg.repo = repo;
3562 err = worktree_status(worktree, relpath ? relpath : "",
3563 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3564 if (err)
3565 goto done;
3567 if (TAILQ_EMPTY(&commitable_paths)) {
3568 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3569 goto done;
3572 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3573 struct got_commitable *ct = pe->data;
3574 err = check_ct_out_of_date(ct, repo, head_commit_id);
3575 if (err)
3576 goto done;
3579 err = commit_worktree(new_commit_id, &commitable_paths,
3580 head_commit_id, worktree, ondisk_path, author, committer,
3581 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3582 if (err)
3583 goto done;
3585 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3586 fileindex);
3587 sync_err = sync_fileindex(fileindex, fileindex_path);
3588 if (sync_err && err == NULL)
3589 err = sync_err;
3590 done:
3591 if (fileindex)
3592 got_fileindex_free(fileindex);
3593 free(fileindex_path);
3594 free(relpath);
3595 unlockerr = lock_worktree(worktree, LOCK_SH);
3596 if (unlockerr && err == NULL)
3597 err = unlockerr;
3598 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3599 struct got_commitable *ct = pe->data;
3600 free_commitable(ct);
3602 got_pathlist_free(&commitable_paths);
3603 return err;
3606 const char *
3607 got_commitable_get_path(struct got_commitable *ct)
3609 return ct->path;
3612 unsigned int
3613 got_commitable_get_status(struct got_commitable *ct)
3615 return ct->status;
3618 struct check_rebase_ok_arg {
3619 struct got_worktree *worktree;
3620 struct got_repository *repo;
3621 int rebase_in_progress;
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 if (!a->rebase_in_progress) {
3634 /* Reject rebase of a work tree with mixed base commits. */
3635 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3636 SHA1_DIGEST_LENGTH))
3637 return got_error(GOT_ERR_MIXED_COMMITS);
3640 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3641 == -1)
3642 return got_error_from_errno("asprintf");
3644 /* Reject rebase of a work tree with modified or conflicted files. */
3645 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3646 free(ondisk_path);
3647 if (err)
3648 return err;
3650 if (a->rebase_in_progress) {
3651 if (status == GOT_STATUS_CONFLICT)
3652 return got_error(GOT_ERR_CONFLICTS);
3653 } else if (status != GOT_STATUS_NO_CHANGE)
3654 return got_error(GOT_ERR_MODIFIED);
3656 return NULL;
3659 const struct got_error *
3660 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3661 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3662 struct got_worktree *worktree, struct got_reference *branch,
3663 struct got_repository *repo)
3665 const struct got_error *err = NULL;
3666 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3667 char *branch_ref_name = NULL;
3668 char *fileindex_path = NULL;
3669 struct check_rebase_ok_arg ok_arg;
3670 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3672 *new_base_branch_ref = NULL;
3673 *tmp_branch = NULL;
3674 *fileindex = NULL;
3676 err = lock_worktree(worktree, LOCK_EX);
3677 if (err)
3678 return err;
3680 err = open_fileindex(fileindex, &fileindex_path, worktree);
3681 if (err)
3682 goto done;
3684 ok_arg.worktree = worktree;
3685 ok_arg.repo = repo;
3686 ok_arg.rebase_in_progress = 0;
3687 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3688 &ok_arg);
3689 if (err)
3690 goto done;
3692 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3693 if (err)
3694 goto done;
3696 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3697 if (err)
3698 goto done;
3700 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3701 if (err)
3702 goto done;
3704 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3705 0);
3706 if (err)
3707 goto done;
3709 err = got_ref_alloc_symref(new_base_branch_ref,
3710 new_base_branch_ref_name, wt_branch);
3711 if (err)
3712 goto done;
3713 err = got_ref_write(*new_base_branch_ref, repo);
3714 if (err)
3715 goto done;
3717 /* TODO Lock original branch's ref while rebasing? */
3719 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3720 if (err)
3721 goto done;
3723 err = got_ref_write(branch_ref, repo);
3724 if (err)
3725 goto done;
3727 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3728 worktree->base_commit_id);
3729 if (err)
3730 goto done;
3731 err = got_ref_write(*tmp_branch, repo);
3732 if (err)
3733 goto done;
3735 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3736 if (err)
3737 goto done;
3738 done:
3739 free(fileindex_path);
3740 free(tmp_branch_name);
3741 free(new_base_branch_ref_name);
3742 free(branch_ref_name);
3743 if (branch_ref)
3744 got_ref_close(branch_ref);
3745 if (wt_branch)
3746 got_ref_close(wt_branch);
3747 if (err) {
3748 if (*new_base_branch_ref) {
3749 got_ref_close(*new_base_branch_ref);
3750 *new_base_branch_ref = NULL;
3752 if (*tmp_branch) {
3753 got_ref_close(*tmp_branch);
3754 *tmp_branch = NULL;
3756 if (*fileindex) {
3757 got_fileindex_free(*fileindex);
3758 *fileindex = NULL;
3760 lock_worktree(worktree, LOCK_SH);
3762 return err;
3765 const struct got_error *
3766 got_worktree_rebase_continue(struct got_object_id **commit_id,
3767 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3768 struct got_reference **branch, struct got_fileindex **fileindex,
3769 struct got_worktree *worktree, struct got_repository *repo)
3771 const struct got_error *err;
3772 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3773 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3774 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3775 char *fileindex_path = NULL;
3777 *commit_id = NULL;
3778 *new_base_branch = NULL;
3779 *tmp_branch = NULL;
3780 *branch = NULL;
3781 *fileindex = NULL;
3783 err = lock_worktree(worktree, LOCK_EX);
3784 if (err)
3785 return err;
3787 err = open_fileindex(fileindex, &fileindex_path, worktree);
3788 if (err)
3789 goto done;
3791 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3792 if (err)
3793 return err;
3795 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3796 if (err)
3797 goto done;
3799 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3800 if (err)
3801 goto done;
3803 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3804 if (err)
3805 goto done;
3807 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3808 if (err)
3809 goto done;
3811 err = got_ref_open(branch, repo,
3812 got_ref_get_symref_target(branch_ref), 0);
3813 if (err)
3814 goto done;
3816 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3817 if (err)
3818 goto done;
3820 err = got_ref_resolve(commit_id, repo, commit_ref);
3821 if (err)
3822 goto done;
3824 err = got_ref_open(new_base_branch, repo,
3825 new_base_branch_ref_name, 0);
3826 if (err)
3827 goto done;
3829 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3830 if (err)
3831 goto done;
3832 done:
3833 free(commit_ref_name);
3834 free(branch_ref_name);
3835 free(fileindex_path);
3836 if (commit_ref)
3837 got_ref_close(commit_ref);
3838 if (branch_ref)
3839 got_ref_close(branch_ref);
3840 if (err) {
3841 free(*commit_id);
3842 *commit_id = NULL;
3843 if (*tmp_branch) {
3844 got_ref_close(*tmp_branch);
3845 *tmp_branch = NULL;
3847 if (*new_base_branch) {
3848 got_ref_close(*new_base_branch);
3849 *new_base_branch = NULL;
3851 if (*branch) {
3852 got_ref_close(*branch);
3853 *branch = NULL;
3855 if (*fileindex) {
3856 got_fileindex_free(*fileindex);
3857 *fileindex = NULL;
3859 lock_worktree(worktree, LOCK_SH);
3861 return err;
3864 const struct got_error *
3865 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3867 const struct got_error *err;
3868 char *tmp_branch_name = NULL;
3870 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3871 if (err)
3872 return err;
3874 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3875 free(tmp_branch_name);
3876 return NULL;
3879 static const struct got_error *
3880 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3881 char **logmsg, void *arg)
3883 *logmsg = arg;
3884 return NULL;
3887 static const struct got_error *
3888 rebase_status(void *arg, unsigned char status, const char *path,
3889 struct got_object_id *blob_id, struct got_object_id *commit_id)
3891 return NULL;
3894 struct collect_merged_paths_arg {
3895 got_worktree_checkout_cb progress_cb;
3896 void *progress_arg;
3897 struct got_pathlist_head *merged_paths;
3900 static const struct got_error *
3901 collect_merged_paths(void *arg, unsigned char status, const char *path)
3903 const struct got_error *err;
3904 struct collect_merged_paths_arg *a = arg;
3905 char *p;
3906 struct got_pathlist_entry *new;
3908 err = (*a->progress_cb)(a->progress_arg, status, path);
3909 if (err)
3910 return err;
3912 if (status != GOT_STATUS_MERGE &&
3913 status != GOT_STATUS_ADD &&
3914 status != GOT_STATUS_DELETE &&
3915 status != GOT_STATUS_CONFLICT)
3916 return NULL;
3918 p = strdup(path);
3919 if (p == NULL)
3920 return got_error_from_errno("strdup");
3922 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3923 if (err || new == NULL)
3924 free(p);
3925 return err;
3928 void
3929 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3931 struct got_pathlist_entry *pe;
3933 TAILQ_FOREACH(pe, merged_paths, entry)
3934 free((char *)pe->path);
3936 got_pathlist_free(merged_paths);
3939 static const struct got_error *
3940 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3941 struct got_repository *repo)
3943 const struct got_error *err;
3944 struct got_reference *commit_ref = NULL;
3946 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3947 if (err) {
3948 if (err->code != GOT_ERR_NOT_REF)
3949 goto done;
3950 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3951 if (err)
3952 goto done;
3953 err = got_ref_write(commit_ref, repo);
3954 if (err)
3955 goto done;
3956 } else {
3957 struct got_object_id *stored_id;
3958 int cmp;
3960 err = got_ref_resolve(&stored_id, repo, commit_ref);
3961 if (err)
3962 goto done;
3963 cmp = got_object_id_cmp(commit_id, stored_id);
3964 free(stored_id);
3965 if (cmp != 0) {
3966 err = got_error(GOT_ERR_REBASE_COMMITID);
3967 goto done;
3970 done:
3971 if (commit_ref)
3972 got_ref_close(commit_ref);
3973 return err;
3976 static const struct got_error *
3977 rebase_merge_files(struct got_pathlist_head *merged_paths,
3978 const char *commit_ref_name, struct got_worktree *worktree,
3979 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3980 struct got_object_id *commit_id, struct got_repository *repo,
3981 got_worktree_checkout_cb progress_cb, void *progress_arg,
3982 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3984 const struct got_error *err;
3985 struct got_reference *commit_ref = NULL;
3986 struct collect_merged_paths_arg cmp_arg;
3987 char *fileindex_path;
3989 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3991 err = get_fileindex_path(&fileindex_path, worktree);
3992 if (err)
3993 return err;
3995 cmp_arg.progress_cb = progress_cb;
3996 cmp_arg.progress_arg = progress_arg;
3997 cmp_arg.merged_paths = merged_paths;
3998 err = merge_files(worktree, fileindex, fileindex_path,
3999 parent_commit_id, commit_id, repo, collect_merged_paths,
4000 &cmp_arg, cancel_cb, cancel_arg);
4001 if (commit_ref)
4002 got_ref_close(commit_ref);
4003 return err;
4006 const struct got_error *
4007 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4008 struct got_worktree *worktree, struct got_fileindex *fileindex,
4009 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4010 struct got_repository *repo,
4011 got_worktree_checkout_cb progress_cb, void *progress_arg,
4012 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4014 const struct got_error *err;
4015 char *commit_ref_name;
4017 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4018 if (err)
4019 return err;
4021 err = store_commit_id(commit_ref_name, commit_id, repo);
4022 if (err)
4023 goto done;
4025 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4026 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4027 progress_arg, cancel_cb, cancel_arg);
4028 done:
4029 free(commit_ref_name);
4030 return err;
4033 const struct got_error *
4034 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4035 struct got_worktree *worktree, struct got_fileindex *fileindex,
4036 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4037 struct got_repository *repo,
4038 got_worktree_checkout_cb progress_cb, void *progress_arg,
4039 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4041 const struct got_error *err;
4042 char *commit_ref_name;
4044 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4045 if (err)
4046 return err;
4048 err = store_commit_id(commit_ref_name, commit_id, repo);
4049 if (err)
4050 goto done;
4052 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4053 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4054 progress_arg, cancel_cb, cancel_arg);
4055 done:
4056 free(commit_ref_name);
4057 return err;
4060 static const struct got_error *
4061 rebase_commit(struct got_object_id **new_commit_id,
4062 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4063 struct got_worktree *worktree, struct got_fileindex *fileindex,
4064 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4065 const char *new_logmsg, struct got_repository *repo)
4067 const struct got_error *err, *sync_err;
4068 struct got_pathlist_head commitable_paths;
4069 struct collect_commitables_arg cc_arg;
4070 char *fileindex_path = NULL;
4071 struct got_reference *head_ref = NULL;
4072 struct got_object_id *head_commit_id = NULL;
4073 char *logmsg = NULL;
4075 TAILQ_INIT(&commitable_paths);
4076 *new_commit_id = NULL;
4078 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4080 err = get_fileindex_path(&fileindex_path, worktree);
4081 if (err)
4082 return err;
4084 cc_arg.commitable_paths = &commitable_paths;
4085 cc_arg.worktree = worktree;
4086 cc_arg.repo = repo;
4088 * If possible get the status of individual files directly to
4089 * avoid crawling the entire work tree once per rebased commit.
4090 * TODO: Ideally, merged_paths would contain a list of commitables
4091 * we could use so we could skip worktree_status() entirely.
4093 if (merged_paths) {
4094 struct got_pathlist_entry *pe;
4095 if (TAILQ_EMPTY(merged_paths)) {
4096 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4097 goto done;
4099 TAILQ_FOREACH(pe, merged_paths, entry) {
4100 err = worktree_status(worktree, pe->path, fileindex,
4101 repo, collect_commitables, &cc_arg, NULL, NULL);
4102 if (err)
4103 goto done;
4105 } else {
4106 err = worktree_status(worktree, "", fileindex, repo,
4107 collect_commitables, &cc_arg, NULL, NULL);
4108 if (err)
4109 goto done;
4112 if (TAILQ_EMPTY(&commitable_paths)) {
4113 /* No-op change; commit will be elided. */
4114 err = got_ref_delete(commit_ref, repo);
4115 if (err)
4116 goto done;
4117 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4118 goto done;
4121 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4122 if (err)
4123 goto done;
4125 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4126 if (err)
4127 goto done;
4129 if (new_logmsg)
4130 logmsg = strdup(new_logmsg);
4131 else
4132 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4133 if (logmsg == NULL)
4134 return got_error_from_errno("strdup");
4136 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4137 worktree, NULL, got_object_commit_get_author(orig_commit),
4138 got_object_commit_get_committer(orig_commit),
4139 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4140 if (err)
4141 goto done;
4143 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4144 if (err)
4145 goto done;
4147 err = got_ref_delete(commit_ref, repo);
4148 if (err)
4149 goto done;
4151 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4152 fileindex);
4153 sync_err = sync_fileindex(fileindex, fileindex_path);
4154 if (sync_err && err == NULL)
4155 err = sync_err;
4156 done:
4157 free(fileindex_path);
4158 free(head_commit_id);
4159 if (head_ref)
4160 got_ref_close(head_ref);
4161 if (err) {
4162 free(*new_commit_id);
4163 *new_commit_id = NULL;
4165 return err;
4168 const struct got_error *
4169 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4170 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4171 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4172 struct got_commit_object *orig_commit,
4173 struct got_object_id *orig_commit_id, struct got_repository *repo)
4175 const struct got_error *err;
4176 char *commit_ref_name;
4177 struct got_reference *commit_ref = NULL;
4178 struct got_object_id *commit_id = NULL;
4180 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4181 if (err)
4182 return err;
4184 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4185 if (err)
4186 goto done;
4187 err = got_ref_resolve(&commit_id, repo, commit_ref);
4188 if (err)
4189 goto done;
4190 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4191 err = got_error(GOT_ERR_REBASE_COMMITID);
4192 goto done;
4195 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4196 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4197 done:
4198 if (commit_ref)
4199 got_ref_close(commit_ref);
4200 free(commit_ref_name);
4201 free(commit_id);
4202 return err;
4205 const struct got_error *
4206 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4207 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4208 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4209 struct got_commit_object *orig_commit,
4210 struct got_object_id *orig_commit_id, const char *new_logmsg,
4211 struct got_repository *repo)
4213 const struct got_error *err;
4214 char *commit_ref_name;
4215 struct got_reference *commit_ref = NULL;
4216 struct got_object_id *commit_id = NULL;
4218 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4219 if (err)
4220 return err;
4222 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4223 if (err)
4224 goto done;
4225 err = got_ref_resolve(&commit_id, repo, commit_ref);
4226 if (err)
4227 goto done;
4228 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4229 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4230 goto done;
4233 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4234 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4235 done:
4236 if (commit_ref)
4237 got_ref_close(commit_ref);
4238 free(commit_ref_name);
4239 free(commit_id);
4240 return err;
4243 const struct got_error *
4244 got_worktree_rebase_postpone(struct got_worktree *worktree,
4245 struct got_fileindex *fileindex)
4247 if (fileindex)
4248 got_fileindex_free(fileindex);
4249 return lock_worktree(worktree, LOCK_SH);
4252 static const struct got_error *
4253 delete_ref(const char *name, struct got_repository *repo)
4255 const struct got_error *err;
4256 struct got_reference *ref;
4258 err = got_ref_open(&ref, repo, name, 0);
4259 if (err) {
4260 if (err->code == GOT_ERR_NOT_REF)
4261 return NULL;
4262 return err;
4265 err = got_ref_delete(ref, repo);
4266 got_ref_close(ref);
4267 return err;
4270 static const struct got_error *
4271 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4273 const struct got_error *err;
4274 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4275 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4277 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4278 if (err)
4279 goto done;
4280 err = delete_ref(tmp_branch_name, repo);
4281 if (err)
4282 goto done;
4284 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4285 if (err)
4286 goto done;
4287 err = delete_ref(new_base_branch_ref_name, repo);
4288 if (err)
4289 goto done;
4291 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4292 if (err)
4293 goto done;
4294 err = delete_ref(branch_ref_name, repo);
4295 if (err)
4296 goto done;
4298 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4299 if (err)
4300 goto done;
4301 err = delete_ref(commit_ref_name, repo);
4302 if (err)
4303 goto done;
4305 done:
4306 free(tmp_branch_name);
4307 free(new_base_branch_ref_name);
4308 free(branch_ref_name);
4309 free(commit_ref_name);
4310 return err;
4313 const struct got_error *
4314 got_worktree_rebase_complete(struct got_worktree *worktree,
4315 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4316 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4317 struct got_repository *repo)
4319 const struct got_error *err, *unlockerr;
4320 struct got_object_id *new_head_commit_id = NULL;
4322 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4323 if (err)
4324 return err;
4326 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4327 if (err)
4328 goto done;
4330 err = got_ref_write(rebased_branch, repo);
4331 if (err)
4332 goto done;
4334 err = got_worktree_set_head_ref(worktree, rebased_branch);
4335 if (err)
4336 goto done;
4338 err = delete_rebase_refs(worktree, repo);
4339 done:
4340 if (fileindex)
4341 got_fileindex_free(fileindex);
4342 free(new_head_commit_id);
4343 unlockerr = lock_worktree(worktree, LOCK_SH);
4344 if (unlockerr && err == NULL)
4345 err = unlockerr;
4346 return err;
4349 struct collect_revertible_paths_arg {
4350 struct got_pathlist_head *revertible_paths;
4351 struct got_worktree *worktree;
4354 static const struct got_error *
4355 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4356 struct got_object_id *blob_id, struct got_object_id *commit_id)
4358 struct collect_revertible_paths_arg *a = arg;
4359 const struct got_error *err = NULL;
4360 struct got_pathlist_entry *new = NULL;
4361 char *path = NULL;
4363 if (status != GOT_STATUS_ADD &&
4364 status != GOT_STATUS_DELETE &&
4365 status != GOT_STATUS_MODIFY &&
4366 status != GOT_STATUS_CONFLICT &&
4367 status != GOT_STATUS_MISSING)
4368 return NULL;
4370 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4371 return got_error_from_errno("asprintf");
4373 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4374 if (err || new == NULL)
4375 free(path);
4376 return err;
4379 const struct got_error *
4380 got_worktree_rebase_abort(struct got_worktree *worktree,
4381 struct got_fileindex *fileindex, struct got_repository *repo,
4382 struct got_reference *new_base_branch,
4383 got_worktree_checkout_cb progress_cb, void *progress_arg)
4385 const struct got_error *err, *unlockerr, *sync_err;
4386 struct got_reference *resolved = NULL;
4387 struct got_object_id *commit_id = NULL;
4388 char *fileindex_path = NULL;
4389 struct got_pathlist_head revertible_paths;
4390 struct got_pathlist_entry *pe;
4391 struct collect_revertible_paths_arg crp_arg;
4392 struct got_object_id *tree_id = NULL;
4394 TAILQ_INIT(&revertible_paths);
4396 err = lock_worktree(worktree, LOCK_EX);
4397 if (err)
4398 return err;
4400 err = got_ref_open(&resolved, repo,
4401 got_ref_get_symref_target(new_base_branch), 0);
4402 if (err)
4403 goto done;
4405 err = got_worktree_set_head_ref(worktree, resolved);
4406 if (err)
4407 goto done;
4410 * XXX commits to the base branch could have happened while
4411 * we were busy rebasing; should we store the original commit ID
4412 * when rebase begins and read it back here?
4414 err = got_ref_resolve(&commit_id, repo, resolved);
4415 if (err)
4416 goto done;
4418 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4419 if (err)
4420 goto done;
4422 err = got_object_id_by_path(&tree_id, repo,
4423 worktree->base_commit_id, worktree->path_prefix);
4424 if (err)
4425 goto done;
4427 err = delete_rebase_refs(worktree, repo);
4428 if (err)
4429 goto done;
4431 err = get_fileindex_path(&fileindex_path, worktree);
4432 if (err)
4433 goto done;
4435 crp_arg.revertible_paths = &revertible_paths;
4436 crp_arg.worktree = worktree;
4437 err = worktree_status(worktree, "", fileindex, repo,
4438 collect_revertible_paths, &crp_arg, NULL, NULL);
4439 if (err)
4440 goto done;
4442 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4443 err = revert_file(worktree, fileindex, pe->path,
4444 progress_cb, progress_arg, repo);
4445 if (err)
4446 goto sync;
4449 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4450 repo, progress_cb, progress_arg, NULL, NULL);
4451 sync:
4452 sync_err = sync_fileindex(fileindex, fileindex_path);
4453 if (sync_err && err == NULL)
4454 err = sync_err;
4455 done:
4456 got_ref_close(resolved);
4457 free(tree_id);
4458 free(commit_id);
4459 if (fileindex)
4460 got_fileindex_free(fileindex);
4461 free(fileindex_path);
4462 TAILQ_FOREACH(pe, &revertible_paths, entry)
4463 free((char *)pe->path);
4464 got_pathlist_free(&revertible_paths);
4466 unlockerr = lock_worktree(worktree, LOCK_SH);
4467 if (unlockerr && err == NULL)
4468 err = unlockerr;
4469 return err;
4472 const struct got_error *
4473 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4474 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4475 struct got_fileindex **fileindex, struct got_worktree *worktree,
4476 struct got_repository *repo)
4478 const struct got_error *err = NULL;
4479 char *tmp_branch_name = NULL;
4480 char *branch_ref_name = NULL;
4481 char *base_commit_ref_name = NULL;
4482 char *fileindex_path = NULL;
4483 struct check_rebase_ok_arg ok_arg;
4484 struct got_reference *wt_branch = NULL;
4485 struct got_reference *base_commit_ref = NULL;
4487 *tmp_branch = NULL;
4488 *branch_ref = NULL;
4489 *base_commit_id = NULL;
4490 *fileindex = NULL;
4492 err = lock_worktree(worktree, LOCK_EX);
4493 if (err)
4494 return err;
4496 err = open_fileindex(fileindex, &fileindex_path, worktree);
4497 if (err)
4498 goto done;
4500 ok_arg.worktree = worktree;
4501 ok_arg.repo = repo;
4502 ok_arg.rebase_in_progress = 0;
4503 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4504 &ok_arg);
4505 if (err)
4506 goto done;
4508 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4509 if (err)
4510 goto done;
4512 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4513 if (err)
4514 goto done;
4516 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4517 worktree);
4518 if (err)
4519 goto done;
4521 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4522 0);
4523 if (err)
4524 goto done;
4526 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4527 if (err)
4528 goto done;
4530 err = got_ref_write(*branch_ref, repo);
4531 if (err)
4532 goto done;
4534 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4535 worktree->base_commit_id);
4536 if (err)
4537 goto done;
4538 err = got_ref_write(base_commit_ref, repo);
4539 if (err)
4540 goto done;
4541 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4542 if (*base_commit_id == NULL) {
4543 err = got_error_from_errno("got_object_id_dup");
4544 goto done;
4547 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4548 worktree->base_commit_id);
4549 if (err)
4550 goto done;
4551 err = got_ref_write(*tmp_branch, repo);
4552 if (err)
4553 goto done;
4555 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4556 if (err)
4557 goto done;
4558 done:
4559 free(fileindex_path);
4560 free(tmp_branch_name);
4561 free(branch_ref_name);
4562 free(base_commit_ref_name);
4563 if (wt_branch)
4564 got_ref_close(wt_branch);
4565 if (err) {
4566 if (*branch_ref) {
4567 got_ref_close(*branch_ref);
4568 *branch_ref = NULL;
4570 if (*tmp_branch) {
4571 got_ref_close(*tmp_branch);
4572 *tmp_branch = NULL;
4574 free(*base_commit_id);
4575 if (*fileindex) {
4576 got_fileindex_free(*fileindex);
4577 *fileindex = NULL;
4579 lock_worktree(worktree, LOCK_SH);
4581 return err;
4584 const struct got_error *
4585 got_worktree_histedit_postpone(struct got_worktree *worktree,
4586 struct got_fileindex *fileindex)
4588 if (fileindex)
4589 got_fileindex_free(fileindex);
4590 return lock_worktree(worktree, LOCK_SH);
4593 const struct got_error *
4594 got_worktree_histedit_in_progress(int *in_progress,
4595 struct got_worktree *worktree)
4597 const struct got_error *err;
4598 char *tmp_branch_name = NULL;
4600 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4601 if (err)
4602 return err;
4604 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4605 free(tmp_branch_name);
4606 return NULL;
4609 const struct got_error *
4610 got_worktree_histedit_continue(struct got_object_id **commit_id,
4611 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4612 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4613 struct got_worktree *worktree, struct got_repository *repo)
4615 const struct got_error *err;
4616 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4617 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4618 struct got_reference *commit_ref = NULL;
4619 struct got_reference *base_commit_ref = NULL;
4620 char *fileindex_path = NULL;
4622 *commit_id = NULL;
4623 *tmp_branch = NULL;
4624 *base_commit_id = NULL;
4625 *fileindex = NULL;
4627 err = lock_worktree(worktree, LOCK_EX);
4628 if (err)
4629 return err;
4631 err = open_fileindex(fileindex, &fileindex_path, worktree);
4632 if (err)
4633 goto done;
4635 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4636 if (err)
4637 return err;
4639 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4640 if (err)
4641 goto done;
4643 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4644 if (err)
4645 goto done;
4647 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4648 worktree);
4649 if (err)
4650 goto done;
4652 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4653 if (err)
4654 goto done;
4656 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4657 if (err)
4658 goto done;
4659 err = got_ref_resolve(commit_id, repo, commit_ref);
4660 if (err)
4661 goto done;
4663 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4664 if (err)
4665 goto done;
4666 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4667 if (err)
4668 goto done;
4670 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4671 if (err)
4672 goto done;
4673 done:
4674 free(commit_ref_name);
4675 free(branch_ref_name);
4676 free(fileindex_path);
4677 if (commit_ref)
4678 got_ref_close(commit_ref);
4679 if (base_commit_ref)
4680 got_ref_close(base_commit_ref);
4681 if (err) {
4682 free(*commit_id);
4683 *commit_id = NULL;
4684 free(*base_commit_id);
4685 *base_commit_id = NULL;
4686 if (*tmp_branch) {
4687 got_ref_close(*tmp_branch);
4688 *tmp_branch = NULL;
4690 if (*fileindex) {
4691 got_fileindex_free(*fileindex);
4692 *fileindex = NULL;
4694 lock_worktree(worktree, LOCK_EX);
4696 return err;
4699 static const struct got_error *
4700 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4702 const struct got_error *err;
4703 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4704 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4706 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4707 if (err)
4708 goto done;
4709 err = delete_ref(tmp_branch_name, repo);
4710 if (err)
4711 goto done;
4713 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4714 worktree);
4715 if (err)
4716 goto done;
4717 err = delete_ref(base_commit_ref_name, repo);
4718 if (err)
4719 goto done;
4721 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4722 if (err)
4723 goto done;
4724 err = delete_ref(branch_ref_name, repo);
4725 if (err)
4726 goto done;
4728 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4729 if (err)
4730 goto done;
4731 err = delete_ref(commit_ref_name, repo);
4732 if (err)
4733 goto done;
4734 done:
4735 free(tmp_branch_name);
4736 free(base_commit_ref_name);
4737 free(branch_ref_name);
4738 free(commit_ref_name);
4739 return err;
4742 const struct got_error *
4743 got_worktree_histedit_abort(struct got_worktree *worktree,
4744 struct got_fileindex *fileindex, struct got_repository *repo,
4745 struct got_reference *branch, struct got_object_id *base_commit_id,
4746 got_worktree_checkout_cb progress_cb, void *progress_arg)
4748 const struct got_error *err, *unlockerr, *sync_err;
4749 struct got_reference *resolved = NULL;
4750 char *fileindex_path = NULL;
4751 struct got_pathlist_head revertible_paths;
4752 struct got_pathlist_entry *pe;
4753 struct collect_revertible_paths_arg crp_arg;
4754 struct got_object_id *tree_id = NULL;
4756 TAILQ_INIT(&revertible_paths);
4758 err = lock_worktree(worktree, LOCK_EX);
4759 if (err)
4760 return err;
4762 err = got_ref_open(&resolved, repo,
4763 got_ref_get_symref_target(branch), 0);
4764 if (err)
4765 goto done;
4767 err = got_worktree_set_head_ref(worktree, resolved);
4768 if (err)
4769 goto done;
4771 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4772 if (err)
4773 goto done;
4775 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4776 worktree->path_prefix);
4777 if (err)
4778 goto done;
4780 err = delete_histedit_refs(worktree, repo);
4781 if (err)
4782 goto done;
4784 err = get_fileindex_path(&fileindex_path, worktree);
4785 if (err)
4786 goto done;
4788 crp_arg.revertible_paths = &revertible_paths;
4789 crp_arg.worktree = worktree;
4790 err = worktree_status(worktree, "", fileindex, repo,
4791 collect_revertible_paths, &crp_arg, NULL, NULL);
4792 if (err)
4793 goto done;
4795 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4796 err = revert_file(worktree, fileindex, pe->path,
4797 progress_cb, progress_arg, repo);
4798 if (err)
4799 goto sync;
4802 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4803 repo, progress_cb, progress_arg, NULL, NULL);
4804 sync:
4805 sync_err = sync_fileindex(fileindex, fileindex_path);
4806 if (sync_err && err == NULL)
4807 err = sync_err;
4808 done:
4809 got_ref_close(resolved);
4810 free(tree_id);
4811 free(fileindex_path);
4812 TAILQ_FOREACH(pe, &revertible_paths, entry)
4813 free((char *)pe->path);
4814 got_pathlist_free(&revertible_paths);
4816 unlockerr = lock_worktree(worktree, LOCK_SH);
4817 if (unlockerr && err == NULL)
4818 err = unlockerr;
4819 return err;
4822 const struct got_error *
4823 got_worktree_histedit_complete(struct got_worktree *worktree,
4824 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4825 struct got_reference *edited_branch, struct got_repository *repo)
4827 const struct got_error *err, *unlockerr;
4828 struct got_object_id *new_head_commit_id = NULL;
4829 struct got_reference *resolved = NULL;
4831 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4832 if (err)
4833 return err;
4835 err = got_ref_open(&resolved, repo,
4836 got_ref_get_symref_target(edited_branch), 0);
4837 if (err)
4838 goto done;
4840 err = got_ref_change_ref(resolved, new_head_commit_id);
4841 if (err)
4842 goto done;
4844 err = got_ref_write(resolved, repo);
4845 if (err)
4846 goto done;
4848 err = got_worktree_set_head_ref(worktree, resolved);
4849 if (err)
4850 goto done;
4852 err = delete_histedit_refs(worktree, repo);
4853 done:
4854 if (fileindex)
4855 got_fileindex_free(fileindex);
4856 free(new_head_commit_id);
4857 unlockerr = lock_worktree(worktree, LOCK_SH);
4858 if (unlockerr && err == NULL)
4859 err = unlockerr;
4860 return err;
4863 const struct got_error *
4864 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4865 struct got_object_id *commit_id, struct got_repository *repo)
4867 const struct got_error *err;
4868 char *commit_ref_name;
4870 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4871 if (err)
4872 return err;
4874 err = store_commit_id(commit_ref_name, commit_id, repo);
4875 if (err)
4876 goto done;
4878 err = delete_ref(commit_ref_name, repo);
4879 done:
4880 free(commit_ref_name);
4881 return err;