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, const char *path,
1778 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1779 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1781 const struct got_error *err = NULL, *sync_err, *unlockerr;
1782 struct got_commit_object *commit = NULL;
1783 struct got_object_id *tree_id = NULL;
1784 struct got_tree_object *tree = NULL;
1785 struct got_fileindex *fileindex = NULL;
1786 char *fileindex_path = NULL;
1787 char *relpath = NULL, *entry_name = NULL;
1788 int entry_type;
1790 err = lock_worktree(worktree, LOCK_EX);
1791 if (err)
1792 return err;
1794 err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
1795 path, worktree, repo);
1796 if (err)
1797 goto done;
1799 if (entry_type == GOT_OBJ_TYPE_BLOB) {
1800 entry_name = basename(path);
1801 if (entry_name == NULL) {
1802 err = got_error_from_errno2("basename", path);
1803 goto done;
1808 * Read the file index.
1809 * Checking out files is supposed to be an idempotent operation.
1810 * If the on-disk file index is incomplete we will try to complete it.
1812 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1813 if (err)
1814 goto done;
1816 err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
1817 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1818 if (err == NULL) {
1819 struct bump_base_commit_id_arg bbc_arg;
1820 bbc_arg.base_commit_id = worktree->base_commit_id;
1821 bbc_arg.entry_name = entry_name;
1822 bbc_arg.path = path;
1823 bbc_arg.path_len = strlen(path);
1824 bbc_arg.progress_cb = progress_cb;
1825 bbc_arg.progress_arg = progress_arg;
1826 err = got_fileindex_for_each_entry_safe(fileindex,
1827 bump_base_commit_id, &bbc_arg);
1829 sync_err = sync_fileindex(fileindex, fileindex_path);
1830 if (sync_err && err == NULL)
1831 err = sync_err;
1832 done:
1833 free(fileindex_path);
1834 free(relpath);
1835 if (tree)
1836 got_object_tree_close(tree);
1837 if (commit)
1838 got_object_commit_close(commit);
1839 if (fileindex)
1840 got_fileindex_free(fileindex);
1841 unlockerr = lock_worktree(worktree, LOCK_SH);
1842 if (unlockerr && err == NULL)
1843 err = unlockerr;
1844 return err;
1847 struct merge_file_cb_arg {
1848 struct got_worktree *worktree;
1849 struct got_fileindex *fileindex;
1850 got_worktree_checkout_cb progress_cb;
1851 void *progress_arg;
1852 got_worktree_cancel_cb cancel_cb;
1853 void *cancel_arg;
1854 struct got_object_id *commit_id2;
1857 static const struct got_error *
1858 merge_file_cb(void *arg, struct got_blob_object *blob1,
1859 struct got_blob_object *blob2, struct got_object_id *id1,
1860 struct got_object_id *id2, const char *path1, const char *path2,
1861 struct got_repository *repo)
1863 static const struct got_error *err = NULL;
1864 struct merge_file_cb_arg *a = arg;
1865 struct got_fileindex_entry *ie;
1866 char *ondisk_path = NULL;
1867 struct stat sb;
1868 unsigned char status;
1869 int local_changes_subsumed;
1871 if (blob1 && blob2) {
1872 ie = got_fileindex_entry_get(a->fileindex, path2);
1873 if (ie == NULL)
1874 return (*a->progress_cb)(a->progress_arg,
1875 GOT_STATUS_MISSING, path2);
1877 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1878 path2) == -1)
1879 return got_error_from_errno("asprintf");
1881 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1882 if (err)
1883 goto done;
1885 if (status == GOT_STATUS_DELETE) {
1886 err = (*a->progress_cb)(a->progress_arg,
1887 GOT_STATUS_MERGE, path2);
1888 goto done;
1890 if (status != GOT_STATUS_NO_CHANGE &&
1891 status != GOT_STATUS_MODIFY &&
1892 status != GOT_STATUS_CONFLICT &&
1893 status != GOT_STATUS_ADD) {
1894 err = (*a->progress_cb)(a->progress_arg, status, path2);
1895 goto done;
1898 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1899 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1900 a->progress_cb, a->progress_arg);
1901 } else if (blob1) {
1902 ie = got_fileindex_entry_get(a->fileindex, path1);
1903 if (ie == NULL)
1904 return (*a->progress_cb)(a->progress_arg,
1905 GOT_STATUS_MISSING, path2);
1907 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1908 path1) == -1)
1909 return got_error_from_errno("asprintf");
1911 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1912 if (err)
1913 goto done;
1915 switch (status) {
1916 case GOT_STATUS_NO_CHANGE:
1917 err = (*a->progress_cb)(a->progress_arg,
1918 GOT_STATUS_DELETE, path1);
1919 if (err)
1920 goto done;
1921 err = remove_ondisk_file(a->worktree->root_path, path1);
1922 if (err)
1923 goto done;
1924 if (ie)
1925 got_fileindex_entry_mark_deleted_from_disk(ie);
1926 break;
1927 case GOT_STATUS_DELETE:
1928 case GOT_STATUS_MISSING:
1929 err = (*a->progress_cb)(a->progress_arg,
1930 GOT_STATUS_DELETE, path1);
1931 if (err)
1932 goto done;
1933 if (ie)
1934 got_fileindex_entry_mark_deleted_from_disk(ie);
1935 break;
1936 case GOT_STATUS_ADD:
1937 case GOT_STATUS_MODIFY:
1938 case GOT_STATUS_CONFLICT:
1939 err = (*a->progress_cb)(a->progress_arg,
1940 GOT_STATUS_CANNOT_DELETE, path1);
1941 if (err)
1942 goto done;
1943 break;
1944 case GOT_STATUS_OBSTRUCTED:
1945 err = (*a->progress_cb)(a->progress_arg, status, path1);
1946 if (err)
1947 goto done;
1948 break;
1949 default:
1950 break;
1952 } else if (blob2) {
1953 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1954 path2) == -1)
1955 return got_error_from_errno("asprintf");
1956 ie = got_fileindex_entry_get(a->fileindex, path2);
1957 if (ie) {
1958 err = get_file_status(&status, &sb, ie, ondisk_path,
1959 repo);
1960 if (err)
1961 goto done;
1962 if (status != GOT_STATUS_NO_CHANGE &&
1963 status != GOT_STATUS_MODIFY &&
1964 status != GOT_STATUS_CONFLICT &&
1965 status != GOT_STATUS_ADD) {
1966 err = (*a->progress_cb)(a->progress_arg,
1967 status, path2);
1968 goto done;
1970 err = merge_blob(&local_changes_subsumed, a->worktree,
1971 NULL, ondisk_path, path2, sb.st_mode, blob2,
1972 a->commit_id2, repo,
1973 a->progress_cb, a->progress_arg);
1974 if (status == GOT_STATUS_DELETE) {
1975 err = update_blob_fileindex_entry(a->worktree,
1976 a->fileindex, ie, ondisk_path, ie->path,
1977 blob2, 0);
1978 if (err)
1979 goto done;
1981 } else {
1982 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1983 err = install_blob(a->worktree, ondisk_path, path2,
1984 /* XXX get this from parent tree! */
1985 GOT_DEFAULT_FILE_MODE,
1986 sb.st_mode, blob2, 0, 0, repo,
1987 a->progress_cb, a->progress_arg);
1988 if (err)
1989 goto done;
1990 err = got_fileindex_entry_alloc(&ie,
1991 ondisk_path, path2, NULL, NULL);
1992 if (err)
1993 goto done;
1994 err = got_fileindex_entry_add(a->fileindex, ie);
1995 if (err) {
1996 got_fileindex_entry_free(ie);
1997 goto done;
2001 done:
2002 free(ondisk_path);
2003 return err;
2006 struct check_merge_ok_arg {
2007 struct got_worktree *worktree;
2008 struct got_repository *repo;
2011 static const struct got_error *
2012 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2014 const struct got_error *err = NULL;
2015 struct check_merge_ok_arg *a = arg;
2016 unsigned char status;
2017 struct stat sb;
2018 char *ondisk_path;
2020 /* Reject merges into a work tree with mixed base commits. */
2021 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2022 SHA1_DIGEST_LENGTH))
2023 return got_error(GOT_ERR_MIXED_COMMITS);
2025 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2026 == -1)
2027 return got_error_from_errno("asprintf");
2029 /* Reject merges into a work tree with conflicted files. */
2030 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2031 if (err)
2032 return err;
2033 if (status == GOT_STATUS_CONFLICT)
2034 return got_error(GOT_ERR_CONFLICTS);
2036 return NULL;
2039 static const struct got_error *
2040 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2041 const char *fileindex_path, struct got_object_id *commit_id1,
2042 struct got_object_id *commit_id2, struct got_repository *repo,
2043 got_worktree_checkout_cb progress_cb, void *progress_arg,
2044 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2046 const struct got_error *err = NULL, *sync_err;
2047 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2049 struct merge_file_cb_arg arg;
2051 if (commit_id1) {
2052 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2053 worktree->path_prefix);
2054 if (err)
2055 goto done;
2057 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2058 if (err)
2059 goto done;
2062 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2063 worktree->path_prefix);
2064 if (err)
2065 goto done;
2067 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2068 if (err)
2069 goto done;
2071 arg.worktree = worktree;
2072 arg.fileindex = fileindex;
2073 arg.progress_cb = progress_cb;
2074 arg.progress_arg = progress_arg;
2075 arg.cancel_cb = cancel_cb;
2076 arg.cancel_arg = cancel_arg;
2077 arg.commit_id2 = commit_id2;
2078 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2079 sync_err = sync_fileindex(fileindex, fileindex_path);
2080 if (sync_err && err == NULL)
2081 err = sync_err;
2082 done:
2083 if (tree1)
2084 got_object_tree_close(tree1);
2085 if (tree2)
2086 got_object_tree_close(tree2);
2087 return err;
2090 const struct got_error *
2091 got_worktree_merge_files(struct got_worktree *worktree,
2092 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2093 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2094 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2096 const struct got_error *err, *unlockerr;
2097 char *fileindex_path = NULL;
2098 struct got_fileindex *fileindex = NULL;
2099 struct check_merge_ok_arg mok_arg;
2101 err = lock_worktree(worktree, LOCK_EX);
2102 if (err)
2103 return err;
2105 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2106 if (err)
2107 goto done;
2109 mok_arg.worktree = worktree;
2110 mok_arg.repo = repo;
2111 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2112 &mok_arg);
2113 if (err)
2114 goto done;
2116 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2117 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2118 done:
2119 if (fileindex)
2120 got_fileindex_free(fileindex);
2121 free(fileindex_path);
2122 unlockerr = lock_worktree(worktree, LOCK_SH);
2123 if (unlockerr && err == NULL)
2124 err = unlockerr;
2125 return err;
2128 struct diff_dir_cb_arg {
2129 struct got_fileindex *fileindex;
2130 struct got_worktree *worktree;
2131 const char *status_path;
2132 size_t status_path_len;
2133 struct got_repository *repo;
2134 got_worktree_status_cb status_cb;
2135 void *status_arg;
2136 got_worktree_cancel_cb cancel_cb;
2137 void *cancel_arg;
2140 static const struct got_error *
2141 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2142 got_worktree_status_cb status_cb, void *status_arg,
2143 struct got_repository *repo)
2145 const struct got_error *err = NULL;
2146 unsigned char status = GOT_STATUS_NO_CHANGE;
2147 struct stat sb;
2148 struct got_object_id blob_id, commit_id;
2150 err = get_file_status(&status, &sb, ie, abspath, repo);
2151 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2152 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2153 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2154 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2155 &commit_id);
2157 return err;
2160 static const struct got_error *
2161 status_old_new(void *arg, struct got_fileindex_entry *ie,
2162 struct dirent *de, const char *parent_path)
2164 const struct got_error *err = NULL;
2165 struct diff_dir_cb_arg *a = arg;
2166 char *abspath;
2168 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2169 return got_error(GOT_ERR_CANCELLED);
2171 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2172 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2173 return NULL;
2175 if (parent_path[0]) {
2176 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2177 parent_path, de->d_name) == -1)
2178 return got_error_from_errno("asprintf");
2179 } else {
2180 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2181 de->d_name) == -1)
2182 return got_error_from_errno("asprintf");
2185 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2186 a->repo);
2187 free(abspath);
2188 return err;
2191 static const struct got_error *
2192 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2194 struct diff_dir_cb_arg *a = arg;
2195 struct got_object_id blob_id, commit_id;
2196 unsigned char status;
2198 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2199 return got_error(GOT_ERR_CANCELLED);
2201 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2202 return NULL;
2204 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2205 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2206 if (got_fileindex_entry_has_file_on_disk(ie))
2207 status = GOT_STATUS_MISSING;
2208 else
2209 status = GOT_STATUS_DELETE;
2210 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2211 &commit_id);
2214 static const struct got_error *
2215 status_new(void *arg, struct dirent *de, const char *parent_path)
2217 const struct got_error *err = NULL;
2218 struct diff_dir_cb_arg *a = arg;
2219 char *path = NULL;
2221 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2222 return got_error(GOT_ERR_CANCELLED);
2224 if (de->d_type == DT_DIR)
2225 return NULL;
2227 /* XXX ignore symlinks for now */
2228 if (de->d_type == DT_LNK)
2229 return NULL;
2231 if (parent_path[0]) {
2232 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2233 return got_error_from_errno("asprintf");
2234 } else {
2235 path = de->d_name;
2238 if (got_path_is_child(path, a->status_path, a->status_path_len))
2239 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2240 path, NULL, NULL);
2241 if (parent_path[0])
2242 free(path);
2243 return err;
2246 static const struct got_error *
2247 worktree_status(struct got_worktree *worktree, const char *path,
2248 struct got_fileindex *fileindex, struct got_repository *repo,
2249 got_worktree_status_cb status_cb, void *status_arg,
2250 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2252 const struct got_error *err = NULL;
2253 DIR *workdir = NULL;
2254 struct got_fileindex_diff_dir_cb fdiff_cb;
2255 struct diff_dir_cb_arg arg;
2256 char *ondisk_path = NULL;
2258 if (asprintf(&ondisk_path, "%s%s%s",
2259 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2260 err = got_error_from_errno("asprintf");
2261 goto done;
2263 workdir = opendir(ondisk_path);
2264 if (workdir == NULL) {
2265 if (errno == ENOTDIR || errno == ENOENT) {
2266 struct got_fileindex_entry *ie;
2267 ie = got_fileindex_entry_get(fileindex, path);
2268 if (ie == NULL)
2269 err = (*status_cb)(status_arg,
2270 GOT_STATUS_UNVERSIONED, path, NULL, NULL);
2271 else
2272 err = report_file_status(ie, ondisk_path,
2273 status_cb, status_arg, repo);
2274 if (err)
2275 goto done;
2276 err = report_file_status(ie, ondisk_path,
2277 status_cb, status_arg, repo);
2278 goto done;
2279 } else {
2280 err = got_error_from_errno2("opendir", ondisk_path);
2281 goto done;
2284 fdiff_cb.diff_old_new = status_old_new;
2285 fdiff_cb.diff_old = status_old;
2286 fdiff_cb.diff_new = status_new;
2287 arg.fileindex = fileindex;
2288 arg.worktree = worktree;
2289 arg.status_path = path;
2290 arg.status_path_len = strlen(path);
2291 arg.repo = repo;
2292 arg.status_cb = status_cb;
2293 arg.status_arg = status_arg;
2294 arg.cancel_cb = cancel_cb;
2295 arg.cancel_arg = cancel_arg;
2296 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2297 path, repo, &fdiff_cb, &arg);
2298 done:
2299 if (workdir)
2300 closedir(workdir);
2301 free(ondisk_path);
2302 return err;
2305 const struct got_error *
2306 got_worktree_status(struct got_worktree *worktree,
2307 struct got_pathlist_head *paths, struct got_repository *repo,
2308 got_worktree_status_cb status_cb, void *status_arg,
2309 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2311 const struct got_error *err = NULL;
2312 char *fileindex_path = NULL;
2313 struct got_fileindex *fileindex = NULL;
2314 struct got_pathlist_entry *pe;
2316 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2317 if (err)
2318 return err;
2320 TAILQ_FOREACH(pe, paths, entry) {
2321 err = worktree_status(worktree, pe->path, fileindex, repo,
2322 status_cb, status_arg, cancel_cb, cancel_arg);
2323 if (err)
2324 break;
2326 free(fileindex_path);
2327 got_fileindex_free(fileindex);
2328 return err;
2331 const struct got_error *
2332 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2333 const char *arg)
2335 const struct got_error *err = NULL;
2336 char *resolved, *cwd = NULL, *path = NULL;
2337 size_t len;
2339 *wt_path = NULL;
2341 resolved = realpath(arg, NULL);
2342 if (resolved == NULL) {
2343 if (errno != ENOENT)
2344 return got_error_from_errno2("realpath", arg);
2345 cwd = getcwd(NULL, 0);
2346 if (cwd == NULL)
2347 return got_error_from_errno("getcwd");
2348 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2349 err = got_error_from_errno("asprintf");
2350 goto done;
2354 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2355 strlen(got_worktree_get_root_path(worktree)))) {
2356 err = got_error(GOT_ERR_BAD_PATH);
2357 goto done;
2360 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2361 err = got_path_skip_common_ancestor(&path,
2362 got_worktree_get_root_path(worktree), resolved);
2363 if (err)
2364 goto done;
2365 } else {
2366 path = strdup("");
2367 if (path == NULL) {
2368 err = got_error_from_errno("strdup");
2369 goto done;
2373 /* XXX status walk can't deal with trailing slash! */
2374 len = strlen(path);
2375 while (len > 0 && path[len - 1] == '/') {
2376 path[len - 1] = '\0';
2377 len--;
2379 done:
2380 free(resolved);
2381 free(cwd);
2382 if (err == NULL)
2383 *wt_path = path;
2384 else
2385 free(path);
2386 return err;
2389 static const struct got_error *
2390 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2391 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2392 struct got_repository *repo)
2394 const struct got_error *err = NULL;
2395 struct got_fileindex_entry *ie;
2397 /* Re-adding an existing entry is a no-op. */
2398 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2399 return NULL;
2401 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2402 if (err)
2403 return err;
2405 err = got_fileindex_entry_add(fileindex, ie);
2406 if (err) {
2407 got_fileindex_entry_free(ie);
2408 return err;
2411 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2414 const struct got_error *
2415 got_worktree_schedule_add(struct got_worktree *worktree,
2416 struct got_pathlist_head *ondisk_paths,
2417 got_worktree_status_cb status_cb, void *status_arg,
2418 struct got_repository *repo)
2420 struct got_fileindex *fileindex = NULL;
2421 char *fileindex_path = NULL;
2422 const struct got_error *err = NULL, *sync_err, *unlockerr;
2423 struct got_pathlist_entry *pe;
2425 err = lock_worktree(worktree, LOCK_EX);
2426 if (err)
2427 return err;
2429 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2430 if (err)
2431 goto done;
2433 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2434 char *relpath;
2435 err = got_path_skip_common_ancestor(&relpath,
2436 got_worktree_get_root_path(worktree), pe->path);
2437 if (err)
2438 break;
2439 err = schedule_addition(pe->path, fileindex, relpath,
2440 status_cb, status_arg, repo);
2441 free(relpath);
2442 if (err)
2443 break;
2445 sync_err = sync_fileindex(fileindex, fileindex_path);
2446 if (sync_err && err == NULL)
2447 err = sync_err;
2448 done:
2449 free(fileindex_path);
2450 if (fileindex)
2451 got_fileindex_free(fileindex);
2452 unlockerr = lock_worktree(worktree, LOCK_SH);
2453 if (unlockerr && err == NULL)
2454 err = unlockerr;
2455 return err;
2458 static const struct got_error *
2459 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2460 const char *relpath, int delete_local_mods,
2461 got_worktree_status_cb status_cb, void *status_arg,
2462 struct got_repository *repo)
2464 const struct got_error *err = NULL;
2465 struct got_fileindex_entry *ie = NULL;
2466 unsigned char status;
2467 struct stat sb;
2469 ie = got_fileindex_entry_get(fileindex, relpath);
2470 if (ie == NULL)
2471 return got_error(GOT_ERR_BAD_PATH);
2473 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2474 if (err)
2475 return err;
2477 if (status != GOT_STATUS_NO_CHANGE) {
2478 if (status == GOT_STATUS_DELETE)
2479 return got_error_set_errno(ENOENT, ondisk_path);
2480 if (status != GOT_STATUS_MODIFY)
2481 return got_error(GOT_ERR_FILE_STATUS);
2482 if (!delete_local_mods)
2483 return got_error(GOT_ERR_FILE_MODIFIED);
2486 if (unlink(ondisk_path) != 0)
2487 return got_error_from_errno2("unlink", ondisk_path);
2489 got_fileindex_entry_mark_deleted_from_disk(ie);
2490 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2493 const struct got_error *
2494 got_worktree_schedule_delete(struct got_worktree *worktree,
2495 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2496 got_worktree_status_cb status_cb, void *status_arg,
2497 struct got_repository *repo)
2499 struct got_fileindex *fileindex = NULL;
2500 char *fileindex_path = NULL;
2501 const struct got_error *err = NULL, *sync_err, *unlockerr;
2502 struct got_pathlist_entry *pe;
2504 err = lock_worktree(worktree, LOCK_EX);
2505 if (err)
2506 return err;
2508 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2509 if (err)
2510 goto done;
2512 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2513 char *relpath;
2514 err = got_path_skip_common_ancestor(&relpath,
2515 got_worktree_get_root_path(worktree), pe->path);
2516 if (err)
2517 break;
2518 err = schedule_for_deletion(pe->path, fileindex, relpath,
2519 delete_local_mods, status_cb, status_arg, repo);
2520 free(relpath);
2521 if (err)
2522 break;
2524 sync_err = sync_fileindex(fileindex, fileindex_path);
2525 if (sync_err && err == NULL)
2526 err = sync_err;
2527 done:
2528 free(fileindex_path);
2529 if (fileindex)
2530 got_fileindex_free(fileindex);
2531 unlockerr = lock_worktree(worktree, LOCK_SH);
2532 if (unlockerr && err == NULL)
2533 err = unlockerr;
2534 return err;
2537 static const struct got_error *
2538 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2539 const char *ondisk_path,
2540 got_worktree_checkout_cb progress_cb, void *progress_arg,
2541 struct got_repository *repo)
2543 const struct got_error *err = NULL;
2544 char *relpath = NULL, *parent_path = NULL;
2545 struct got_fileindex_entry *ie;
2546 struct got_tree_object *tree = NULL;
2547 struct got_object_id *tree_id = NULL;
2548 const struct got_tree_entry *te;
2549 char *tree_path = NULL, *te_name;
2550 struct got_blob_object *blob = NULL;
2551 unsigned char status;
2552 struct stat sb;
2554 err = got_path_skip_common_ancestor(&relpath,
2555 got_worktree_get_root_path(worktree), ondisk_path);
2556 if (err)
2557 goto done;
2559 ie = got_fileindex_entry_get(fileindex, relpath);
2560 if (ie == NULL) {
2561 err = got_error(GOT_ERR_BAD_PATH);
2562 goto done;
2565 /* Construct in-repository path of tree which contains this blob. */
2566 err = got_path_dirname(&parent_path, ie->path);
2567 if (err) {
2568 if (err->code != GOT_ERR_BAD_PATH)
2569 goto done;
2570 parent_path = strdup("/");
2571 if (parent_path == NULL) {
2572 err = got_error_from_errno("strdup");
2573 goto done;
2576 if (got_path_is_root_dir(worktree->path_prefix)) {
2577 tree_path = strdup(parent_path);
2578 if (tree_path == NULL) {
2579 err = got_error_from_errno("strdup");
2580 goto done;
2582 } else {
2583 if (got_path_is_root_dir(parent_path)) {
2584 tree_path = strdup(worktree->path_prefix);
2585 if (tree_path == NULL) {
2586 err = got_error_from_errno("strdup");
2587 goto done;
2589 } else {
2590 if (asprintf(&tree_path, "%s/%s",
2591 worktree->path_prefix, parent_path) == -1) {
2592 err = got_error_from_errno("asprintf");
2593 goto done;
2598 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2599 tree_path);
2600 if (err)
2601 goto done;
2603 err = got_object_open_as_tree(&tree, repo, tree_id);
2604 if (err)
2605 goto done;
2607 te_name = basename(ie->path);
2608 if (te_name == NULL) {
2609 err = got_error_from_errno2("basename", ie->path);
2610 goto done;
2613 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2614 if (err)
2615 goto done;
2617 te = got_object_tree_find_entry(tree, te_name);
2618 if (te == NULL && status != GOT_STATUS_ADD) {
2619 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2620 goto done;
2623 switch (status) {
2624 case GOT_STATUS_ADD:
2625 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2626 if (err)
2627 goto done;
2628 got_fileindex_entry_remove(fileindex, ie);
2629 break;
2630 case GOT_STATUS_DELETE:
2631 case GOT_STATUS_MODIFY:
2632 case GOT_STATUS_CONFLICT:
2633 case GOT_STATUS_MISSING: {
2634 struct got_object_id id;
2635 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2636 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2637 if (err)
2638 goto done;
2639 err = install_blob(worktree, ondisk_path, ie->path,
2640 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2641 progress_arg);
2642 if (err)
2643 goto done;
2644 if (status == GOT_STATUS_DELETE) {
2645 err = update_blob_fileindex_entry(worktree,
2646 fileindex, ie, ondisk_path, ie->path, blob, 1);
2647 if (err)
2648 goto done;
2650 break;
2652 default:
2653 goto done;
2655 done:
2656 free(relpath);
2657 free(parent_path);
2658 free(tree_path);
2659 if (blob)
2660 got_object_blob_close(blob);
2661 if (tree)
2662 got_object_tree_close(tree);
2663 free(tree_id);
2664 return err;
2667 const struct got_error *
2668 got_worktree_revert(struct got_worktree *worktree,
2669 struct got_pathlist_head *ondisk_paths,
2670 got_worktree_checkout_cb progress_cb, void *progress_arg,
2671 struct got_repository *repo)
2673 struct got_fileindex *fileindex = NULL;
2674 char *fileindex_path = NULL;
2675 const struct got_error *err = NULL, *unlockerr = NULL;
2676 const struct got_error *sync_err = NULL;
2677 struct got_pathlist_entry *pe;
2679 err = lock_worktree(worktree, LOCK_EX);
2680 if (err)
2681 return err;
2683 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2684 if (err)
2685 goto done;
2687 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2688 err = revert_file(worktree, fileindex, pe->path,
2689 progress_cb, progress_arg, repo);
2690 if (err)
2691 break;
2693 sync_err = sync_fileindex(fileindex, fileindex_path);
2694 if (sync_err && err == NULL)
2695 err = sync_err;
2696 done:
2697 free(fileindex_path);
2698 if (fileindex)
2699 got_fileindex_free(fileindex);
2700 unlockerr = lock_worktree(worktree, LOCK_SH);
2701 if (unlockerr && err == NULL)
2702 err = unlockerr;
2703 return err;
2706 static void
2707 free_commitable(struct got_commitable *ct)
2709 free(ct->path);
2710 free(ct->in_repo_path);
2711 free(ct->ondisk_path);
2712 free(ct->blob_id);
2713 free(ct->base_blob_id);
2714 free(ct->base_commit_id);
2715 free(ct);
2718 struct collect_commitables_arg {
2719 struct got_pathlist_head *commitable_paths;
2720 struct got_repository *repo;
2721 struct got_worktree *worktree;
2724 static const struct got_error *
2725 collect_commitables(void *arg, unsigned char status, const char *relpath,
2726 struct got_object_id *blob_id, struct got_object_id *commit_id)
2728 struct collect_commitables_arg *a = arg;
2729 const struct got_error *err = NULL;
2730 struct got_commitable *ct = NULL;
2731 struct got_pathlist_entry *new = NULL;
2732 char *parent_path = NULL, *path = NULL;
2733 struct stat sb;
2735 if (status == GOT_STATUS_CONFLICT)
2736 return got_error(GOT_ERR_COMMIT_CONFLICT);
2738 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2739 status != GOT_STATUS_DELETE)
2740 return NULL;
2742 if (asprintf(&path, "/%s", relpath) == -1) {
2743 err = got_error_from_errno("asprintf");
2744 goto done;
2746 if (strcmp(path, "/") == 0) {
2747 parent_path = strdup("");
2748 if (parent_path == NULL)
2749 return got_error_from_errno("strdup");
2750 } else {
2751 err = got_path_dirname(&parent_path, path);
2752 if (err)
2753 return err;
2756 ct = calloc(1, sizeof(*ct));
2757 if (ct == NULL) {
2758 err = got_error_from_errno("calloc");
2759 goto done;
2762 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2763 relpath) == -1) {
2764 err = got_error_from_errno("asprintf");
2765 goto done;
2767 if (status == GOT_STATUS_DELETE) {
2768 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2769 } else {
2770 if (lstat(ct->ondisk_path, &sb) != 0) {
2771 err = got_error_from_errno2("lstat", ct->ondisk_path);
2772 goto done;
2774 ct->mode = sb.st_mode;
2777 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2778 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2779 relpath) == -1) {
2780 err = got_error_from_errno("asprintf");
2781 goto done;
2784 ct->status = status;
2785 ct->blob_id = NULL; /* will be filled in when blob gets created */
2786 if (ct->status != GOT_STATUS_ADD) {
2787 ct->base_blob_id = got_object_id_dup(blob_id);
2788 if (ct->base_blob_id == NULL) {
2789 err = got_error_from_errno("got_object_id_dup");
2790 goto done;
2792 ct->base_commit_id = got_object_id_dup(commit_id);
2793 if (ct->base_commit_id == NULL) {
2794 err = got_error_from_errno("got_object_id_dup");
2795 goto done;
2798 ct->path = strdup(path);
2799 if (ct->path == NULL) {
2800 err = got_error_from_errno("strdup");
2801 goto done;
2803 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2804 done:
2805 if (ct && (err || new == NULL))
2806 free_commitable(ct);
2807 free(parent_path);
2808 free(path);
2809 return err;
2812 static const struct got_error *write_tree(struct got_object_id **,
2813 struct got_tree_object *, const char *, struct got_pathlist_head *,
2814 got_worktree_status_cb status_cb, void *status_arg,
2815 struct got_repository *);
2817 static const struct got_error *
2818 write_subtree(struct got_object_id **new_subtree_id,
2819 struct got_tree_entry *te, const char *parent_path,
2820 struct got_pathlist_head *commitable_paths,
2821 got_worktree_status_cb status_cb, void *status_arg,
2822 struct got_repository *repo)
2824 const struct got_error *err = NULL;
2825 struct got_tree_object *subtree;
2826 char *subpath;
2828 if (asprintf(&subpath, "%s%s%s", parent_path,
2829 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2830 return got_error_from_errno("asprintf");
2832 err = got_object_open_as_tree(&subtree, repo, te->id);
2833 if (err)
2834 return err;
2836 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2837 status_cb, status_arg, repo);
2838 got_object_tree_close(subtree);
2839 free(subpath);
2840 return err;
2843 static const struct got_error *
2844 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2846 const struct got_error *err = NULL;
2847 char *ct_parent_path = NULL;
2849 *match = 0;
2851 if (strchr(ct->in_repo_path, '/') == NULL) {
2852 *match = got_path_is_root_dir(path);
2853 return NULL;
2856 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2857 if (err)
2858 return err;
2859 *match = (strcmp(path, ct_parent_path) == 0);
2860 free(ct_parent_path);
2861 return err;
2864 static mode_t
2865 get_ct_file_mode(struct got_commitable *ct)
2867 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2870 static const struct got_error *
2871 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2872 struct got_tree_entry *te, struct got_commitable *ct)
2874 const struct got_error *err = NULL;
2876 *new_te = NULL;
2878 err = got_object_tree_entry_dup(new_te, te);
2879 if (err)
2880 goto done;
2882 (*new_te)->mode = get_ct_file_mode(ct);
2884 free((*new_te)->id);
2885 (*new_te)->id = got_object_id_dup(ct->blob_id);
2886 if ((*new_te)->id == NULL) {
2887 err = got_error_from_errno("got_object_id_dup");
2888 goto done;
2890 done:
2891 if (err && *new_te) {
2892 got_object_tree_entry_close(*new_te);
2893 *new_te = NULL;
2895 return err;
2898 static const struct got_error *
2899 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2900 struct got_commitable *ct)
2902 const struct got_error *err = NULL;
2903 char *ct_name;
2905 *new_te = NULL;
2907 *new_te = calloc(1, sizeof(**new_te));
2908 if (*new_te == NULL)
2909 return got_error_from_errno("calloc");
2911 ct_name = basename(ct->path);
2912 if (ct_name == NULL) {
2913 err = got_error_from_errno2("basename", ct->path);
2914 goto done;
2916 (*new_te)->name = strdup(ct_name);
2917 if ((*new_te)->name == NULL) {
2918 err = got_error_from_errno("strdup");
2919 goto done;
2922 (*new_te)->mode = get_ct_file_mode(ct);
2924 (*new_te)->id = got_object_id_dup(ct->blob_id);
2925 if ((*new_te)->id == NULL) {
2926 err = got_error_from_errno("got_object_id_dup");
2927 goto done;
2929 done:
2930 if (err && *new_te) {
2931 got_object_tree_entry_close(*new_te);
2932 *new_te = NULL;
2934 return err;
2937 static const struct got_error *
2938 insert_tree_entry(struct got_tree_entry *new_te,
2939 struct got_pathlist_head *paths)
2941 const struct got_error *err = NULL;
2942 struct got_pathlist_entry *new_pe;
2944 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2945 if (err)
2946 return err;
2947 if (new_pe == NULL)
2948 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2949 return NULL;
2952 static const struct got_error *
2953 report_ct_status(struct got_commitable *ct,
2954 got_worktree_status_cb status_cb, void *status_arg)
2956 const char *ct_path = ct->path;
2957 while (ct_path[0] == '/')
2958 ct_path++;
2959 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2962 static const struct got_error *
2963 match_modified_subtree(int *modified, struct got_tree_entry *te,
2964 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2966 const struct got_error *err = NULL;
2967 struct got_pathlist_entry *pe;
2968 char *te_path;
2970 *modified = 0;
2972 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2973 got_path_is_root_dir(base_tree_path) ? "" : "/",
2974 te->name) == -1)
2975 return got_error_from_errno("asprintf");
2977 TAILQ_FOREACH(pe, commitable_paths, entry) {
2978 struct got_commitable *ct = pe->data;
2979 *modified = got_path_is_child(ct->in_repo_path, te_path,
2980 strlen(te_path));
2981 if (*modified)
2982 break;
2985 free(te_path);
2986 return err;
2989 static const struct got_error *
2990 match_deleted_or_modified_ct(struct got_commitable **ctp,
2991 struct got_tree_entry *te, const char *base_tree_path,
2992 struct got_pathlist_head *commitable_paths)
2994 const struct got_error *err = NULL;
2995 struct got_pathlist_entry *pe;
2997 *ctp = NULL;
2999 TAILQ_FOREACH(pe, commitable_paths, entry) {
3000 struct got_commitable *ct = pe->data;
3001 char *ct_name = NULL;
3002 int path_matches;
3004 if (ct->status != GOT_STATUS_MODIFY &&
3005 ct->status != GOT_STATUS_DELETE)
3006 continue;
3008 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3009 continue;
3011 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3012 if (err)
3013 return err;
3014 if (!path_matches)
3015 continue;
3017 ct_name = basename(pe->path);
3018 if (ct_name == NULL)
3019 return got_error_from_errno2("basename", pe->path);
3021 if (strcmp(te->name, ct_name) != 0)
3022 continue;
3024 *ctp = ct;
3025 break;
3028 return err;
3031 static const struct got_error *
3032 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3033 const char *child_path, const char *path_base_tree,
3034 struct got_pathlist_head *commitable_paths,
3035 got_worktree_status_cb status_cb, void *status_arg,
3036 struct got_repository *repo)
3038 const struct got_error *err = NULL;
3039 struct got_tree_entry *new_te;
3040 char *subtree_path;
3042 *new_tep = NULL;
3044 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3045 got_path_is_root_dir(path_base_tree) ? "" : "/",
3046 child_path) == -1)
3047 return got_error_from_errno("asprintf");
3049 new_te = calloc(1, sizeof(*new_te));
3050 new_te->mode = S_IFDIR;
3051 new_te->name = strdup(child_path);
3052 if (new_te->name == NULL) {
3053 err = got_error_from_errno("strdup");
3054 got_object_tree_entry_close(new_te);
3055 goto done;
3057 err = write_tree(&new_te->id, NULL, subtree_path,
3058 commitable_paths, status_cb, status_arg, repo);
3059 if (err) {
3060 got_object_tree_entry_close(new_te);
3061 goto done;
3063 done:
3064 free(subtree_path);
3065 if (err == NULL)
3066 *new_tep = new_te;
3067 return err;
3070 static const struct got_error *
3071 write_tree(struct got_object_id **new_tree_id,
3072 struct got_tree_object *base_tree, const char *path_base_tree,
3073 struct got_pathlist_head *commitable_paths,
3074 got_worktree_status_cb status_cb, void *status_arg,
3075 struct got_repository *repo)
3077 const struct got_error *err = NULL;
3078 const struct got_tree_entries *base_entries = NULL;
3079 struct got_pathlist_head paths;
3080 struct got_tree_entries new_tree_entries;
3081 struct got_tree_entry *te, *new_te = NULL;
3082 struct got_pathlist_entry *pe;
3084 TAILQ_INIT(&paths);
3085 new_tree_entries.nentries = 0;
3086 SIMPLEQ_INIT(&new_tree_entries.head);
3088 /* Insert, and recurse into, newly added entries first. */
3089 TAILQ_FOREACH(pe, commitable_paths, entry) {
3090 struct got_commitable *ct = pe->data;
3091 char *child_path = NULL, *slash;
3093 if (ct->status != GOT_STATUS_ADD ||
3094 (ct->flags & GOT_COMMITABLE_ADDED))
3095 continue;
3097 if (!got_path_is_child(pe->path, path_base_tree,
3098 strlen(path_base_tree)))
3099 continue;
3101 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3102 pe->path);
3103 if (err)
3104 goto done;
3106 slash = strchr(child_path, '/');
3107 if (slash == NULL) {
3108 err = alloc_added_blob_tree_entry(&new_te, ct);
3109 if (err)
3110 goto done;
3111 err = report_ct_status(ct, status_cb, status_arg);
3112 if (err)
3113 goto done;
3114 ct->flags |= GOT_COMMITABLE_ADDED;
3115 err = insert_tree_entry(new_te, &paths);
3116 if (err)
3117 goto done;
3118 } else {
3119 *slash = '\0'; /* trim trailing path components */
3120 if (base_tree == NULL ||
3121 got_object_tree_find_entry(base_tree, child_path)
3122 == NULL) {
3123 err = make_subtree_for_added_blob(&new_te,
3124 child_path, path_base_tree,
3125 commitable_paths, status_cb, status_arg,
3126 repo);
3127 if (err)
3128 goto done;
3129 err = insert_tree_entry(new_te, &paths);
3130 if (err)
3131 goto done;
3136 if (base_tree) {
3137 /* Handle modified and deleted entries. */
3138 base_entries = got_object_tree_get_entries(base_tree);
3139 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3140 struct got_commitable *ct = NULL;
3142 if (S_ISDIR(te->mode)) {
3143 int modified;
3144 err = got_object_tree_entry_dup(&new_te, te);
3145 if (err)
3146 goto done;
3147 err = match_modified_subtree(&modified, te,
3148 path_base_tree, commitable_paths);
3149 if (err)
3150 goto done;
3151 /* Avoid recursion into unmodified subtrees. */
3152 if (modified) {
3153 free(new_te->id);
3154 err = write_subtree(&new_te->id, te,
3155 path_base_tree, commitable_paths,
3156 status_cb, status_arg, repo);
3157 if (err)
3158 goto done;
3160 err = insert_tree_entry(new_te, &paths);
3161 if (err)
3162 goto done;
3163 continue;
3166 err = match_deleted_or_modified_ct(&ct, te,
3167 path_base_tree, commitable_paths);
3168 if (ct) {
3169 /* NB: Deleted entries get dropped here. */
3170 if (ct->status == GOT_STATUS_MODIFY) {
3171 err = alloc_modified_blob_tree_entry(
3172 &new_te, te, ct);
3173 if (err)
3174 goto done;
3175 err = insert_tree_entry(new_te, &paths);
3176 if (err)
3177 goto done;
3179 err = report_ct_status(ct, status_cb,
3180 status_arg);
3181 if (err)
3182 goto done;
3183 } else {
3184 /* Entry is unchanged; just copy it. */
3185 err = got_object_tree_entry_dup(&new_te, te);
3186 if (err)
3187 goto done;
3188 err = insert_tree_entry(new_te, &paths);
3189 if (err)
3190 goto done;
3195 /* Write new list of entries; deleted entries have been dropped. */
3196 TAILQ_FOREACH(pe, &paths, entry) {
3197 struct got_tree_entry *te = pe->data;
3198 new_tree_entries.nentries++;
3199 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3201 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3202 done:
3203 got_object_tree_entries_close(&new_tree_entries);
3204 got_pathlist_free(&paths);
3205 return err;
3208 static const struct got_error *
3209 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3210 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3212 const struct got_error *err = NULL;
3213 struct got_pathlist_entry *pe;
3215 TAILQ_FOREACH(pe, commitable_paths, entry) {
3216 struct got_fileindex_entry *ie;
3217 struct got_commitable *ct = pe->data;
3219 ie = got_fileindex_entry_get(fileindex, pe->path);
3220 if (ie) {
3221 if (ct->status == GOT_STATUS_DELETE) {
3222 got_fileindex_entry_remove(fileindex, ie);
3223 got_fileindex_entry_free(ie);
3224 } else
3225 err = got_fileindex_entry_update(ie,
3226 ct->ondisk_path, ct->blob_id->sha1,
3227 new_base_commit_id->sha1, 1);
3228 } else {
3229 err = got_fileindex_entry_alloc(&ie,
3230 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3231 new_base_commit_id->sha1);
3232 if (err)
3233 break;
3234 err = got_fileindex_entry_add(fileindex, ie);
3235 if (err)
3236 break;
3239 return err;
3242 static const struct got_error *
3243 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3244 struct got_object_id *head_commit_id)
3246 const struct got_error *err = NULL;
3247 struct got_object_id *id_in_head = NULL, *id = NULL;
3248 struct got_commit_object *commit = NULL;
3249 char *path = NULL;
3250 const char *ct_path = ct->in_repo_path;
3252 while (ct_path[0] == '/')
3253 ct_path++;
3256 * Ensure that no modifications were made to files *and their parents*
3257 * in commits between the file's base commit and the branch head.
3259 * Checking the parents is important for detecting conflicting tree
3260 * configurations (files or parent folders might have been moved,
3261 * deleted, added again, etc.). Such changes need to be merged with
3262 * local changes before a commit can occur.
3264 * The implication is that the file's (parent) entry in the root
3265 * directory must have the same ID in all relevant commits.
3267 if (ct->status != GOT_STATUS_ADD) {
3268 struct got_object_qid *pid;
3269 char *slash;
3270 struct got_object_id *root_entry_id = NULL;
3272 /* Trivial case: base commit == head commit */
3273 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3274 return NULL;
3276 /* Compute the path to the root directory's entry. */
3277 path = strdup(ct_path);
3278 if (path == NULL) {
3279 err = got_error_from_errno("strdup");
3280 goto done;
3282 slash = strchr(path, '/');
3283 if (slash)
3284 *slash = '\0';
3286 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3287 if (err)
3288 goto done;
3290 err = got_object_id_by_path(&root_entry_id, repo,
3291 head_commit_id, path);
3292 if (err)
3293 goto done;
3295 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3296 while (pid) {
3297 struct got_commit_object *pcommit;
3299 err = got_object_id_by_path(&id, repo, pid->id, path);
3300 if (err) {
3301 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3302 goto done;
3303 err = NULL;
3304 break;
3307 err = got_object_id_by_path(&id, repo, pid->id, path);
3308 if (err)
3309 goto done;
3311 if (got_object_id_cmp(id, root_entry_id) != 0) {
3312 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3313 break;
3316 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3317 break; /* all relevant commits scanned */
3319 err = got_object_open_as_commit(&pcommit, repo,
3320 pid->id);
3321 if (err)
3322 goto done;
3324 got_object_commit_close(commit);
3325 commit = pcommit;
3326 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3327 commit));
3329 } else {
3330 /* Require that added files don't exist in the branch head. */
3331 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3332 ct_path);
3333 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3334 goto done;
3335 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3337 done:
3338 if (commit)
3339 got_object_commit_close(commit);
3340 free(id_in_head);
3341 free(id);
3342 free(path);
3343 return err;
3346 const struct got_error *
3347 commit_worktree(struct got_object_id **new_commit_id,
3348 struct got_pathlist_head *commitable_paths,
3349 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3350 const char *ondisk_path, const char *author, const char *committer,
3351 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3352 got_worktree_status_cb status_cb, void *status_arg,
3353 struct got_repository *repo)
3355 const struct got_error *err = NULL, *unlockerr = NULL;
3356 struct got_pathlist_entry *pe;
3357 const char *head_ref_name = NULL;
3358 struct got_commit_object *head_commit = NULL;
3359 struct got_reference *head_ref2 = NULL;
3360 struct got_object_id *head_commit_id2 = NULL;
3361 struct got_tree_object *head_tree = NULL;
3362 struct got_object_id *new_tree_id = NULL;
3363 struct got_object_id_queue parent_ids;
3364 struct got_object_qid *pid = NULL;
3365 char *logmsg = NULL;
3367 *new_commit_id = NULL;
3369 SIMPLEQ_INIT(&parent_ids);
3371 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3372 if (err)
3373 goto done;
3375 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3376 if (err)
3377 goto done;
3379 if (commit_msg_cb != NULL) {
3380 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3381 if (err)
3382 goto done;
3385 if (logmsg == NULL || strlen(logmsg) == 0) {
3386 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3387 goto done;
3390 /* Create blobs from added and modified files and record their IDs. */
3391 TAILQ_FOREACH(pe, commitable_paths, entry) {
3392 struct got_commitable *ct = pe->data;
3393 char *ondisk_path;
3395 if (ct->status != GOT_STATUS_ADD &&
3396 ct->status != GOT_STATUS_MODIFY)
3397 continue;
3399 if (asprintf(&ondisk_path, "%s/%s",
3400 worktree->root_path, pe->path) == -1) {
3401 err = got_error_from_errno("asprintf");
3402 goto done;
3404 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3405 free(ondisk_path);
3406 if (err)
3407 goto done;
3410 /* Recursively write new tree objects. */
3411 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3412 status_cb, status_arg, repo);
3413 if (err)
3414 goto done;
3416 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3417 if (err)
3418 goto done;
3419 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3420 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3421 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3422 got_object_qid_free(pid);
3423 if (logmsg != NULL)
3424 free(logmsg);
3425 if (err)
3426 goto done;
3428 /* Check if a concurrent commit to our branch has occurred. */
3429 head_ref_name = got_worktree_get_head_ref_name(worktree);
3430 if (head_ref_name == NULL) {
3431 err = got_error_from_errno("got_worktree_get_head_ref_name");
3432 goto done;
3434 /* Lock the reference here to prevent concurrent modification. */
3435 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3436 if (err)
3437 goto done;
3438 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3439 if (err)
3440 goto done;
3441 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3442 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3443 goto done;
3445 /* Update branch head in repository. */
3446 err = got_ref_change_ref(head_ref2, *new_commit_id);
3447 if (err)
3448 goto done;
3449 err = got_ref_write(head_ref2, repo);
3450 if (err)
3451 goto done;
3453 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3454 if (err)
3455 goto done;
3457 err = ref_base_commit(worktree, repo);
3458 if (err)
3459 goto done;
3460 done:
3461 if (head_tree)
3462 got_object_tree_close(head_tree);
3463 if (head_commit)
3464 got_object_commit_close(head_commit);
3465 free(head_commit_id2);
3466 if (head_ref2) {
3467 unlockerr = got_ref_unlock(head_ref2);
3468 if (unlockerr && err == NULL)
3469 err = unlockerr;
3470 got_ref_close(head_ref2);
3472 return err;
3475 const struct got_error *
3476 got_worktree_commit(struct got_object_id **new_commit_id,
3477 struct got_worktree *worktree, const char *ondisk_path,
3478 const char *author, const char *committer,
3479 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3480 got_worktree_status_cb status_cb, void *status_arg,
3481 struct got_repository *repo)
3483 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3484 struct got_fileindex *fileindex = NULL;
3485 char *fileindex_path = NULL, *relpath = NULL;
3486 struct got_pathlist_head commitable_paths;
3487 struct collect_commitables_arg cc_arg;
3488 struct got_pathlist_entry *pe;
3489 struct got_reference *head_ref = NULL;
3490 struct got_object_id *head_commit_id = NULL;
3492 *new_commit_id = NULL;
3494 TAILQ_INIT(&commitable_paths);
3496 err = lock_worktree(worktree, LOCK_EX);
3497 if (err)
3498 goto done;
3500 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3501 if (err)
3502 goto done;
3504 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3505 if (err)
3506 goto done;
3508 if (ondisk_path) {
3509 if (strcmp(ondisk_path, worktree->root_path) == 0) {
3510 relpath = strdup("");
3511 if (relpath == NULL) {
3512 err = got_error_from_errno("strdup");
3513 goto done;
3515 } else {
3516 err = got_path_skip_common_ancestor(&relpath,
3517 worktree->root_path, ondisk_path);
3518 if (err)
3519 return err;
3523 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3524 if (err)
3525 goto done;
3527 cc_arg.commitable_paths = &commitable_paths;
3528 cc_arg.worktree = worktree;
3529 cc_arg.repo = repo;
3530 err = worktree_status(worktree, relpath ? relpath : "",
3531 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3532 if (err)
3533 goto done;
3535 if (TAILQ_EMPTY(&commitable_paths)) {
3536 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3537 goto done;
3540 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3541 struct got_commitable *ct = pe->data;
3542 err = check_ct_out_of_date(ct, repo, head_commit_id);
3543 if (err)
3544 goto done;
3547 err = commit_worktree(new_commit_id, &commitable_paths,
3548 head_commit_id, worktree, ondisk_path, author, committer,
3549 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3550 if (err)
3551 goto done;
3553 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3554 fileindex);
3555 sync_err = sync_fileindex(fileindex, fileindex_path);
3556 if (sync_err && err == NULL)
3557 err = sync_err;
3558 done:
3559 if (fileindex)
3560 got_fileindex_free(fileindex);
3561 free(fileindex_path);
3562 free(relpath);
3563 unlockerr = lock_worktree(worktree, LOCK_SH);
3564 if (unlockerr && err == NULL)
3565 err = unlockerr;
3566 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3567 struct got_commitable *ct = pe->data;
3568 free_commitable(ct);
3570 got_pathlist_free(&commitable_paths);
3571 return err;
3574 const char *
3575 got_commitable_get_path(struct got_commitable *ct)
3577 return ct->path;
3580 unsigned int
3581 got_commitable_get_status(struct got_commitable *ct)
3583 return ct->status;
3586 struct check_rebase_ok_arg {
3587 struct got_worktree *worktree;
3588 struct got_repository *repo;
3589 int rebase_in_progress;
3592 static const struct got_error *
3593 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3595 const struct got_error *err = NULL;
3596 struct check_rebase_ok_arg *a = arg;
3597 unsigned char status;
3598 struct stat sb;
3599 char *ondisk_path;
3601 if (!a->rebase_in_progress) {
3602 /* Reject rebase of a work tree with mixed base commits. */
3603 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3604 SHA1_DIGEST_LENGTH))
3605 return got_error(GOT_ERR_MIXED_COMMITS);
3608 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3609 == -1)
3610 return got_error_from_errno("asprintf");
3612 /* Reject rebase of a work tree with modified or conflicted files. */
3613 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3614 free(ondisk_path);
3615 if (err)
3616 return err;
3618 if (a->rebase_in_progress) {
3619 if (status == GOT_STATUS_CONFLICT)
3620 return got_error(GOT_ERR_CONFLICTS);
3621 } else if (status != GOT_STATUS_NO_CHANGE)
3622 return got_error(GOT_ERR_MODIFIED);
3624 return NULL;
3627 const struct got_error *
3628 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3629 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3630 struct got_worktree *worktree, struct got_reference *branch,
3631 struct got_repository *repo)
3633 const struct got_error *err = NULL;
3634 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3635 char *branch_ref_name = NULL;
3636 char *fileindex_path = NULL;
3637 struct check_rebase_ok_arg ok_arg;
3638 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3640 *new_base_branch_ref = NULL;
3641 *tmp_branch = NULL;
3642 *fileindex = NULL;
3644 err = lock_worktree(worktree, LOCK_EX);
3645 if (err)
3646 return err;
3648 err = open_fileindex(fileindex, &fileindex_path, worktree);
3649 if (err)
3650 goto done;
3652 ok_arg.worktree = worktree;
3653 ok_arg.repo = repo;
3654 ok_arg.rebase_in_progress = 0;
3655 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3656 &ok_arg);
3657 if (err)
3658 goto done;
3660 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3661 if (err)
3662 goto done;
3664 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3665 if (err)
3666 goto done;
3668 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3669 if (err)
3670 goto done;
3672 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3673 0);
3674 if (err)
3675 goto done;
3677 err = got_ref_alloc_symref(new_base_branch_ref,
3678 new_base_branch_ref_name, wt_branch);
3679 if (err)
3680 goto done;
3681 err = got_ref_write(*new_base_branch_ref, repo);
3682 if (err)
3683 goto done;
3685 /* TODO Lock original branch's ref while rebasing? */
3687 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3688 if (err)
3689 goto done;
3691 err = got_ref_write(branch_ref, repo);
3692 if (err)
3693 goto done;
3695 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3696 worktree->base_commit_id);
3697 if (err)
3698 goto done;
3699 err = got_ref_write(*tmp_branch, repo);
3700 if (err)
3701 goto done;
3703 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3704 if (err)
3705 goto done;
3706 done:
3707 free(fileindex_path);
3708 free(tmp_branch_name);
3709 free(new_base_branch_ref_name);
3710 free(branch_ref_name);
3711 if (branch_ref)
3712 got_ref_close(branch_ref);
3713 if (wt_branch)
3714 got_ref_close(wt_branch);
3715 if (err) {
3716 if (*new_base_branch_ref) {
3717 got_ref_close(*new_base_branch_ref);
3718 *new_base_branch_ref = NULL;
3720 if (*tmp_branch) {
3721 got_ref_close(*tmp_branch);
3722 *tmp_branch = NULL;
3724 if (*fileindex) {
3725 got_fileindex_free(*fileindex);
3726 *fileindex = NULL;
3728 lock_worktree(worktree, LOCK_SH);
3730 return err;
3733 const struct got_error *
3734 got_worktree_rebase_continue(struct got_object_id **commit_id,
3735 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3736 struct got_reference **branch, struct got_fileindex **fileindex,
3737 struct got_worktree *worktree, struct got_repository *repo)
3739 const struct got_error *err;
3740 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3741 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3742 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3743 char *fileindex_path = NULL;
3745 *commit_id = NULL;
3746 *new_base_branch = NULL;
3747 *tmp_branch = NULL;
3748 *branch = NULL;
3749 *fileindex = NULL;
3751 err = lock_worktree(worktree, LOCK_EX);
3752 if (err)
3753 return err;
3755 err = open_fileindex(fileindex, &fileindex_path, worktree);
3756 if (err)
3757 goto done;
3759 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3760 if (err)
3761 return err;
3763 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3764 if (err)
3765 goto done;
3767 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3768 if (err)
3769 goto done;
3771 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3772 if (err)
3773 goto done;
3775 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3776 if (err)
3777 goto done;
3779 err = got_ref_open(branch, repo,
3780 got_ref_get_symref_target(branch_ref), 0);
3781 if (err)
3782 goto done;
3784 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3785 if (err)
3786 goto done;
3788 err = got_ref_resolve(commit_id, repo, commit_ref);
3789 if (err)
3790 goto done;
3792 err = got_ref_open(new_base_branch, repo,
3793 new_base_branch_ref_name, 0);
3794 if (err)
3795 goto done;
3797 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3798 if (err)
3799 goto done;
3800 done:
3801 free(commit_ref_name);
3802 free(branch_ref_name);
3803 free(fileindex_path);
3804 if (commit_ref)
3805 got_ref_close(commit_ref);
3806 if (branch_ref)
3807 got_ref_close(branch_ref);
3808 if (err) {
3809 free(*commit_id);
3810 *commit_id = NULL;
3811 if (*tmp_branch) {
3812 got_ref_close(*tmp_branch);
3813 *tmp_branch = NULL;
3815 if (*new_base_branch) {
3816 got_ref_close(*new_base_branch);
3817 *new_base_branch = NULL;
3819 if (*branch) {
3820 got_ref_close(*branch);
3821 *branch = NULL;
3823 if (*fileindex) {
3824 got_fileindex_free(*fileindex);
3825 *fileindex = NULL;
3827 lock_worktree(worktree, LOCK_SH);
3829 return err;
3832 const struct got_error *
3833 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3835 const struct got_error *err;
3836 char *tmp_branch_name = NULL;
3838 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3839 if (err)
3840 return err;
3842 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3843 free(tmp_branch_name);
3844 return NULL;
3847 static const struct got_error *
3848 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3849 char **logmsg, void *arg)
3851 *logmsg = arg;
3852 return NULL;
3855 static const struct got_error *
3856 rebase_status(void *arg, unsigned char status, const char *path,
3857 struct got_object_id *blob_id, struct got_object_id *commit_id)
3859 return NULL;
3862 struct collect_merged_paths_arg {
3863 got_worktree_checkout_cb progress_cb;
3864 void *progress_arg;
3865 struct got_pathlist_head *merged_paths;
3868 static const struct got_error *
3869 collect_merged_paths(void *arg, unsigned char status, const char *path)
3871 const struct got_error *err;
3872 struct collect_merged_paths_arg *a = arg;
3873 char *p;
3874 struct got_pathlist_entry *new;
3876 err = (*a->progress_cb)(a->progress_arg, status, path);
3877 if (err)
3878 return err;
3880 if (status != GOT_STATUS_MERGE &&
3881 status != GOT_STATUS_ADD &&
3882 status != GOT_STATUS_DELETE &&
3883 status != GOT_STATUS_CONFLICT)
3884 return NULL;
3886 p = strdup(path);
3887 if (p == NULL)
3888 return got_error_from_errno("strdup");
3890 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3891 if (err || new == NULL)
3892 free(p);
3893 return err;
3896 void
3897 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3899 struct got_pathlist_entry *pe;
3901 TAILQ_FOREACH(pe, merged_paths, entry)
3902 free((char *)pe->path);
3904 got_pathlist_free(merged_paths);
3907 static const struct got_error *
3908 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3909 struct got_repository *repo)
3911 const struct got_error *err;
3912 struct got_reference *commit_ref = NULL;
3914 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3915 if (err) {
3916 if (err->code != GOT_ERR_NOT_REF)
3917 goto done;
3918 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3919 if (err)
3920 goto done;
3921 err = got_ref_write(commit_ref, repo);
3922 if (err)
3923 goto done;
3924 } else {
3925 struct got_object_id *stored_id;
3926 int cmp;
3928 err = got_ref_resolve(&stored_id, repo, commit_ref);
3929 if (err)
3930 goto done;
3931 cmp = got_object_id_cmp(commit_id, stored_id);
3932 free(stored_id);
3933 if (cmp != 0) {
3934 err = got_error(GOT_ERR_REBASE_COMMITID);
3935 goto done;
3938 done:
3939 if (commit_ref)
3940 got_ref_close(commit_ref);
3941 return err;
3944 static const struct got_error *
3945 rebase_merge_files(struct got_pathlist_head *merged_paths,
3946 const char *commit_ref_name, struct got_worktree *worktree,
3947 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3948 struct got_object_id *commit_id, struct got_repository *repo,
3949 got_worktree_checkout_cb progress_cb, void *progress_arg,
3950 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3952 const struct got_error *err;
3953 struct got_reference *commit_ref = NULL;
3954 struct collect_merged_paths_arg cmp_arg;
3955 char *fileindex_path;
3957 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3959 err = get_fileindex_path(&fileindex_path, worktree);
3960 if (err)
3961 return err;
3963 cmp_arg.progress_cb = progress_cb;
3964 cmp_arg.progress_arg = progress_arg;
3965 cmp_arg.merged_paths = merged_paths;
3966 err = merge_files(worktree, fileindex, fileindex_path,
3967 parent_commit_id, commit_id, repo, collect_merged_paths,
3968 &cmp_arg, cancel_cb, cancel_arg);
3969 if (commit_ref)
3970 got_ref_close(commit_ref);
3971 return err;
3974 const struct got_error *
3975 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3976 struct got_worktree *worktree, struct got_fileindex *fileindex,
3977 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3978 struct got_repository *repo,
3979 got_worktree_checkout_cb progress_cb, void *progress_arg,
3980 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3982 const struct got_error *err;
3983 char *commit_ref_name;
3985 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3986 if (err)
3987 return err;
3989 err = store_commit_id(commit_ref_name, commit_id, repo);
3990 if (err)
3991 goto done;
3993 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
3994 fileindex, parent_commit_id, commit_id, repo, progress_cb,
3995 progress_arg, cancel_cb, cancel_arg);
3996 done:
3997 free(commit_ref_name);
3998 return err;
4001 const struct got_error *
4002 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4003 struct got_worktree *worktree, struct got_fileindex *fileindex,
4004 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4005 struct got_repository *repo,
4006 got_worktree_checkout_cb progress_cb, void *progress_arg,
4007 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4009 const struct got_error *err;
4010 char *commit_ref_name;
4012 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4013 if (err)
4014 return err;
4016 err = store_commit_id(commit_ref_name, commit_id, repo);
4017 if (err)
4018 goto done;
4020 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4021 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4022 progress_arg, cancel_cb, cancel_arg);
4023 done:
4024 free(commit_ref_name);
4025 return err;
4028 static const struct got_error *
4029 rebase_commit(struct got_object_id **new_commit_id,
4030 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4031 struct got_worktree *worktree, struct got_fileindex *fileindex,
4032 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4033 const char *new_logmsg, struct got_repository *repo)
4035 const struct got_error *err, *sync_err;
4036 struct got_pathlist_head commitable_paths;
4037 struct collect_commitables_arg cc_arg;
4038 char *fileindex_path = NULL;
4039 struct got_reference *head_ref = NULL;
4040 struct got_object_id *head_commit_id = NULL;
4041 char *logmsg = NULL;
4043 TAILQ_INIT(&commitable_paths);
4044 *new_commit_id = NULL;
4046 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4048 err = get_fileindex_path(&fileindex_path, worktree);
4049 if (err)
4050 return err;
4052 cc_arg.commitable_paths = &commitable_paths;
4053 cc_arg.worktree = worktree;
4054 cc_arg.repo = repo;
4056 * If possible get the status of individual files directly to
4057 * avoid crawling the entire work tree once per rebased commit.
4058 * TODO: Ideally, merged_paths would contain a list of commitables
4059 * we could use so we could skip worktree_status() entirely.
4061 if (merged_paths) {
4062 struct got_pathlist_entry *pe;
4063 if (TAILQ_EMPTY(merged_paths)) {
4064 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4065 goto done;
4067 TAILQ_FOREACH(pe, merged_paths, entry) {
4068 err = worktree_status(worktree, pe->path, fileindex,
4069 repo, collect_commitables, &cc_arg, NULL, NULL);
4070 if (err)
4071 goto done;
4073 } else {
4074 err = worktree_status(worktree, "", fileindex, repo,
4075 collect_commitables, &cc_arg, NULL, NULL);
4076 if (err)
4077 goto done;
4080 if (TAILQ_EMPTY(&commitable_paths)) {
4081 /* No-op change; commit will be elided. */
4082 err = got_ref_delete(commit_ref, repo);
4083 if (err)
4084 goto done;
4085 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4086 goto done;
4089 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4090 if (err)
4091 goto done;
4093 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4094 if (err)
4095 goto done;
4097 if (new_logmsg)
4098 logmsg = strdup(new_logmsg);
4099 else
4100 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4101 if (logmsg == NULL)
4102 return got_error_from_errno("strdup");
4104 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4105 worktree, NULL, got_object_commit_get_author(orig_commit),
4106 got_object_commit_get_committer(orig_commit),
4107 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4108 if (err)
4109 goto done;
4111 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4112 if (err)
4113 goto done;
4115 err = got_ref_delete(commit_ref, repo);
4116 if (err)
4117 goto done;
4119 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4120 fileindex);
4121 sync_err = sync_fileindex(fileindex, fileindex_path);
4122 if (sync_err && err == NULL)
4123 err = sync_err;
4124 done:
4125 free(fileindex_path);
4126 free(head_commit_id);
4127 if (head_ref)
4128 got_ref_close(head_ref);
4129 if (err) {
4130 free(*new_commit_id);
4131 *new_commit_id = NULL;
4133 return err;
4136 const struct got_error *
4137 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4138 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4139 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4140 struct got_commit_object *orig_commit,
4141 struct got_object_id *orig_commit_id, struct got_repository *repo)
4143 const struct got_error *err;
4144 char *commit_ref_name;
4145 struct got_reference *commit_ref = NULL;
4146 struct got_object_id *commit_id = NULL;
4148 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4149 if (err)
4150 return err;
4152 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4153 if (err)
4154 goto done;
4155 err = got_ref_resolve(&commit_id, repo, commit_ref);
4156 if (err)
4157 goto done;
4158 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4159 err = got_error(GOT_ERR_REBASE_COMMITID);
4160 goto done;
4163 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4164 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4165 done:
4166 if (commit_ref)
4167 got_ref_close(commit_ref);
4168 free(commit_ref_name);
4169 free(commit_id);
4170 return err;
4173 const struct got_error *
4174 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4175 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4176 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4177 struct got_commit_object *orig_commit,
4178 struct got_object_id *orig_commit_id, const char *new_logmsg,
4179 struct got_repository *repo)
4181 const struct got_error *err;
4182 char *commit_ref_name;
4183 struct got_reference *commit_ref = NULL;
4184 struct got_object_id *commit_id = NULL;
4186 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4187 if (err)
4188 return err;
4190 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4191 if (err)
4192 goto done;
4193 err = got_ref_resolve(&commit_id, repo, commit_ref);
4194 if (err)
4195 goto done;
4196 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4197 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4198 goto done;
4201 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4202 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4203 done:
4204 if (commit_ref)
4205 got_ref_close(commit_ref);
4206 free(commit_ref_name);
4207 free(commit_id);
4208 return err;
4211 const struct got_error *
4212 got_worktree_rebase_postpone(struct got_worktree *worktree,
4213 struct got_fileindex *fileindex)
4215 if (fileindex)
4216 got_fileindex_free(fileindex);
4217 return lock_worktree(worktree, LOCK_SH);
4220 static const struct got_error *
4221 delete_ref(const char *name, struct got_repository *repo)
4223 const struct got_error *err;
4224 struct got_reference *ref;
4226 err = got_ref_open(&ref, repo, name, 0);
4227 if (err) {
4228 if (err->code == GOT_ERR_NOT_REF)
4229 return NULL;
4230 return err;
4233 err = got_ref_delete(ref, repo);
4234 got_ref_close(ref);
4235 return err;
4238 static const struct got_error *
4239 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4241 const struct got_error *err;
4242 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4243 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4245 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4246 if (err)
4247 goto done;
4248 err = delete_ref(tmp_branch_name, repo);
4249 if (err)
4250 goto done;
4252 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4253 if (err)
4254 goto done;
4255 err = delete_ref(new_base_branch_ref_name, repo);
4256 if (err)
4257 goto done;
4259 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4260 if (err)
4261 goto done;
4262 err = delete_ref(branch_ref_name, repo);
4263 if (err)
4264 goto done;
4266 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4267 if (err)
4268 goto done;
4269 err = delete_ref(commit_ref_name, repo);
4270 if (err)
4271 goto done;
4273 done:
4274 free(tmp_branch_name);
4275 free(new_base_branch_ref_name);
4276 free(branch_ref_name);
4277 free(commit_ref_name);
4278 return err;
4281 const struct got_error *
4282 got_worktree_rebase_complete(struct got_worktree *worktree,
4283 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4284 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4285 struct got_repository *repo)
4287 const struct got_error *err, *unlockerr;
4288 struct got_object_id *new_head_commit_id = NULL;
4290 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4291 if (err)
4292 return err;
4294 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4295 if (err)
4296 goto done;
4298 err = got_ref_write(rebased_branch, repo);
4299 if (err)
4300 goto done;
4302 err = got_worktree_set_head_ref(worktree, rebased_branch);
4303 if (err)
4304 goto done;
4306 err = delete_rebase_refs(worktree, repo);
4307 done:
4308 if (fileindex)
4309 got_fileindex_free(fileindex);
4310 free(new_head_commit_id);
4311 unlockerr = lock_worktree(worktree, LOCK_SH);
4312 if (unlockerr && err == NULL)
4313 err = unlockerr;
4314 return err;
4317 struct collect_revertible_paths_arg {
4318 struct got_pathlist_head *revertible_paths;
4319 struct got_worktree *worktree;
4322 static const struct got_error *
4323 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4324 struct got_object_id *blob_id, struct got_object_id *commit_id)
4326 struct collect_revertible_paths_arg *a = arg;
4327 const struct got_error *err = NULL;
4328 struct got_pathlist_entry *new = NULL;
4329 char *path = NULL;
4331 if (status != GOT_STATUS_ADD &&
4332 status != GOT_STATUS_DELETE &&
4333 status != GOT_STATUS_MODIFY &&
4334 status != GOT_STATUS_CONFLICT &&
4335 status != GOT_STATUS_MISSING)
4336 return NULL;
4338 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4339 return got_error_from_errno("asprintf");
4341 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4342 if (err || new == NULL)
4343 free(path);
4344 return err;
4347 const struct got_error *
4348 got_worktree_rebase_abort(struct got_worktree *worktree,
4349 struct got_fileindex *fileindex, struct got_repository *repo,
4350 struct got_reference *new_base_branch,
4351 got_worktree_checkout_cb progress_cb, void *progress_arg)
4353 const struct got_error *err, *unlockerr, *sync_err;
4354 struct got_reference *resolved = NULL;
4355 struct got_object_id *commit_id = NULL;
4356 char *fileindex_path = NULL;
4357 struct got_pathlist_head revertible_paths;
4358 struct got_pathlist_entry *pe;
4359 struct collect_revertible_paths_arg crp_arg;
4360 struct got_object_id *tree_id = NULL;
4362 TAILQ_INIT(&revertible_paths);
4364 err = lock_worktree(worktree, LOCK_EX);
4365 if (err)
4366 return err;
4368 err = got_ref_open(&resolved, repo,
4369 got_ref_get_symref_target(new_base_branch), 0);
4370 if (err)
4371 goto done;
4373 err = got_worktree_set_head_ref(worktree, resolved);
4374 if (err)
4375 goto done;
4378 * XXX commits to the base branch could have happened while
4379 * we were busy rebasing; should we store the original commit ID
4380 * when rebase begins and read it back here?
4382 err = got_ref_resolve(&commit_id, repo, resolved);
4383 if (err)
4384 goto done;
4386 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4387 if (err)
4388 goto done;
4390 err = got_object_id_by_path(&tree_id, repo,
4391 worktree->base_commit_id, worktree->path_prefix);
4392 if (err)
4393 goto done;
4395 err = delete_rebase_refs(worktree, repo);
4396 if (err)
4397 goto done;
4399 err = get_fileindex_path(&fileindex_path, worktree);
4400 if (err)
4401 goto done;
4403 crp_arg.revertible_paths = &revertible_paths;
4404 crp_arg.worktree = worktree;
4405 err = worktree_status(worktree, "", fileindex, repo,
4406 collect_revertible_paths, &crp_arg, NULL, NULL);
4407 if (err)
4408 goto done;
4410 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4411 err = revert_file(worktree, fileindex, pe->path,
4412 progress_cb, progress_arg, repo);
4413 if (err)
4414 goto sync;
4417 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4418 repo, progress_cb, progress_arg, NULL, NULL);
4419 sync:
4420 sync_err = sync_fileindex(fileindex, fileindex_path);
4421 if (sync_err && err == NULL)
4422 err = sync_err;
4423 done:
4424 got_ref_close(resolved);
4425 free(tree_id);
4426 free(commit_id);
4427 if (fileindex)
4428 got_fileindex_free(fileindex);
4429 free(fileindex_path);
4430 TAILQ_FOREACH(pe, &revertible_paths, entry)
4431 free((char *)pe->path);
4432 got_pathlist_free(&revertible_paths);
4434 unlockerr = lock_worktree(worktree, LOCK_SH);
4435 if (unlockerr && err == NULL)
4436 err = unlockerr;
4437 return err;
4440 const struct got_error *
4441 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4442 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4443 struct got_fileindex **fileindex, struct got_worktree *worktree,
4444 struct got_repository *repo)
4446 const struct got_error *err = NULL;
4447 char *tmp_branch_name = NULL;
4448 char *branch_ref_name = NULL;
4449 char *base_commit_ref_name = NULL;
4450 char *fileindex_path = NULL;
4451 struct check_rebase_ok_arg ok_arg;
4452 struct got_reference *wt_branch = NULL;
4453 struct got_reference *base_commit_ref = NULL;
4455 *tmp_branch = NULL;
4456 *branch_ref = NULL;
4457 *base_commit_id = NULL;
4458 *fileindex = NULL;
4460 err = lock_worktree(worktree, LOCK_EX);
4461 if (err)
4462 return err;
4464 err = open_fileindex(fileindex, &fileindex_path, worktree);
4465 if (err)
4466 goto done;
4468 ok_arg.worktree = worktree;
4469 ok_arg.repo = repo;
4470 ok_arg.rebase_in_progress = 0;
4471 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4472 &ok_arg);
4473 if (err)
4474 goto done;
4476 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4477 if (err)
4478 goto done;
4480 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4481 if (err)
4482 goto done;
4484 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4485 worktree);
4486 if (err)
4487 goto done;
4489 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4490 0);
4491 if (err)
4492 goto done;
4494 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4495 if (err)
4496 goto done;
4498 err = got_ref_write(*branch_ref, repo);
4499 if (err)
4500 goto done;
4502 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4503 worktree->base_commit_id);
4504 if (err)
4505 goto done;
4506 err = got_ref_write(base_commit_ref, repo);
4507 if (err)
4508 goto done;
4509 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4510 if (*base_commit_id == NULL) {
4511 err = got_error_from_errno("got_object_id_dup");
4512 goto done;
4515 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4516 worktree->base_commit_id);
4517 if (err)
4518 goto done;
4519 err = got_ref_write(*tmp_branch, repo);
4520 if (err)
4521 goto done;
4523 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4524 if (err)
4525 goto done;
4526 done:
4527 free(fileindex_path);
4528 free(tmp_branch_name);
4529 free(branch_ref_name);
4530 free(base_commit_ref_name);
4531 if (wt_branch)
4532 got_ref_close(wt_branch);
4533 if (err) {
4534 if (*branch_ref) {
4535 got_ref_close(*branch_ref);
4536 *branch_ref = NULL;
4538 if (*tmp_branch) {
4539 got_ref_close(*tmp_branch);
4540 *tmp_branch = NULL;
4542 free(*base_commit_id);
4543 if (*fileindex) {
4544 got_fileindex_free(*fileindex);
4545 *fileindex = NULL;
4547 lock_worktree(worktree, LOCK_SH);
4549 return err;
4552 const struct got_error *
4553 got_worktree_histedit_postpone(struct got_worktree *worktree,
4554 struct got_fileindex *fileindex)
4556 if (fileindex)
4557 got_fileindex_free(fileindex);
4558 return lock_worktree(worktree, LOCK_SH);
4561 const struct got_error *
4562 got_worktree_histedit_in_progress(int *in_progress,
4563 struct got_worktree *worktree)
4565 const struct got_error *err;
4566 char *tmp_branch_name = NULL;
4568 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4569 if (err)
4570 return err;
4572 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4573 free(tmp_branch_name);
4574 return NULL;
4577 const struct got_error *
4578 got_worktree_histedit_continue(struct got_object_id **commit_id,
4579 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4580 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4581 struct got_worktree *worktree, struct got_repository *repo)
4583 const struct got_error *err;
4584 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4585 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4586 struct got_reference *commit_ref = NULL;
4587 struct got_reference *base_commit_ref = NULL;
4588 char *fileindex_path = NULL;
4590 *commit_id = NULL;
4591 *tmp_branch = NULL;
4592 *base_commit_id = NULL;
4593 *fileindex = NULL;
4595 err = lock_worktree(worktree, LOCK_EX);
4596 if (err)
4597 return err;
4599 err = open_fileindex(fileindex, &fileindex_path, worktree);
4600 if (err)
4601 goto done;
4603 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4604 if (err)
4605 return err;
4607 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4608 if (err)
4609 goto done;
4611 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4612 if (err)
4613 goto done;
4615 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4616 worktree);
4617 if (err)
4618 goto done;
4620 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4621 if (err)
4622 goto done;
4624 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4625 if (err)
4626 goto done;
4627 err = got_ref_resolve(commit_id, repo, commit_ref);
4628 if (err)
4629 goto done;
4631 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4632 if (err)
4633 goto done;
4634 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4635 if (err)
4636 goto done;
4638 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4639 if (err)
4640 goto done;
4641 done:
4642 free(commit_ref_name);
4643 free(branch_ref_name);
4644 free(fileindex_path);
4645 if (commit_ref)
4646 got_ref_close(commit_ref);
4647 if (base_commit_ref)
4648 got_ref_close(base_commit_ref);
4649 if (err) {
4650 free(*commit_id);
4651 *commit_id = NULL;
4652 free(*base_commit_id);
4653 *base_commit_id = NULL;
4654 if (*tmp_branch) {
4655 got_ref_close(*tmp_branch);
4656 *tmp_branch = NULL;
4658 if (*fileindex) {
4659 got_fileindex_free(*fileindex);
4660 *fileindex = NULL;
4662 lock_worktree(worktree, LOCK_EX);
4664 return err;
4667 static const struct got_error *
4668 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4670 const struct got_error *err;
4671 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4672 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4674 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4675 if (err)
4676 goto done;
4677 err = delete_ref(tmp_branch_name, repo);
4678 if (err)
4679 goto done;
4681 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4682 worktree);
4683 if (err)
4684 goto done;
4685 err = delete_ref(base_commit_ref_name, repo);
4686 if (err)
4687 goto done;
4689 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4690 if (err)
4691 goto done;
4692 err = delete_ref(branch_ref_name, repo);
4693 if (err)
4694 goto done;
4696 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4697 if (err)
4698 goto done;
4699 err = delete_ref(commit_ref_name, repo);
4700 if (err)
4701 goto done;
4702 done:
4703 free(tmp_branch_name);
4704 free(base_commit_ref_name);
4705 free(branch_ref_name);
4706 free(commit_ref_name);
4707 return err;
4710 const struct got_error *
4711 got_worktree_histedit_abort(struct got_worktree *worktree,
4712 struct got_fileindex *fileindex, struct got_repository *repo,
4713 struct got_reference *branch, struct got_object_id *base_commit_id,
4714 got_worktree_checkout_cb progress_cb, void *progress_arg)
4716 const struct got_error *err, *unlockerr, *sync_err;
4717 struct got_reference *resolved = NULL;
4718 char *fileindex_path = NULL;
4719 struct got_pathlist_head revertible_paths;
4720 struct got_pathlist_entry *pe;
4721 struct collect_revertible_paths_arg crp_arg;
4722 struct got_object_id *tree_id = NULL;
4724 TAILQ_INIT(&revertible_paths);
4726 err = lock_worktree(worktree, LOCK_EX);
4727 if (err)
4728 return err;
4730 err = got_ref_open(&resolved, repo,
4731 got_ref_get_symref_target(branch), 0);
4732 if (err)
4733 goto done;
4735 err = got_worktree_set_head_ref(worktree, resolved);
4736 if (err)
4737 goto done;
4739 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4740 if (err)
4741 goto done;
4743 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4744 worktree->path_prefix);
4745 if (err)
4746 goto done;
4748 err = delete_histedit_refs(worktree, repo);
4749 if (err)
4750 goto done;
4752 err = get_fileindex_path(&fileindex_path, worktree);
4753 if (err)
4754 goto done;
4756 crp_arg.revertible_paths = &revertible_paths;
4757 crp_arg.worktree = worktree;
4758 err = worktree_status(worktree, "", fileindex, repo,
4759 collect_revertible_paths, &crp_arg, NULL, NULL);
4760 if (err)
4761 goto done;
4763 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4764 err = revert_file(worktree, fileindex, pe->path,
4765 progress_cb, progress_arg, repo);
4766 if (err)
4767 goto sync;
4770 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4771 repo, progress_cb, progress_arg, NULL, NULL);
4772 sync:
4773 sync_err = sync_fileindex(fileindex, fileindex_path);
4774 if (sync_err && err == NULL)
4775 err = sync_err;
4776 done:
4777 got_ref_close(resolved);
4778 free(tree_id);
4779 free(fileindex_path);
4780 TAILQ_FOREACH(pe, &revertible_paths, entry)
4781 free((char *)pe->path);
4782 got_pathlist_free(&revertible_paths);
4784 unlockerr = lock_worktree(worktree, LOCK_SH);
4785 if (unlockerr && err == NULL)
4786 err = unlockerr;
4787 return err;
4790 const struct got_error *
4791 got_worktree_histedit_complete(struct got_worktree *worktree,
4792 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4793 struct got_reference *edited_branch, struct got_repository *repo)
4795 const struct got_error *err, *unlockerr;
4796 struct got_object_id *new_head_commit_id = NULL;
4797 struct got_reference *resolved = NULL;
4799 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4800 if (err)
4801 return err;
4803 err = got_ref_open(&resolved, repo,
4804 got_ref_get_symref_target(edited_branch), 0);
4805 if (err)
4806 goto done;
4808 err = got_ref_change_ref(resolved, new_head_commit_id);
4809 if (err)
4810 goto done;
4812 err = got_ref_write(resolved, repo);
4813 if (err)
4814 goto done;
4816 err = got_worktree_set_head_ref(worktree, resolved);
4817 if (err)
4818 goto done;
4820 err = delete_histedit_refs(worktree, repo);
4821 done:
4822 if (fileindex)
4823 got_fileindex_free(fileindex);
4824 free(new_head_commit_id);
4825 unlockerr = lock_worktree(worktree, LOCK_SH);
4826 if (unlockerr && err == NULL)
4827 err = unlockerr;
4828 return err;
4831 const struct got_error *
4832 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4833 struct got_object_id *commit_id, struct got_repository *repo)
4835 const struct got_error *err;
4836 char *commit_ref_name;
4838 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4839 if (err)
4840 return err;
4842 err = store_commit_id(commit_ref_name, commit_id, repo);
4843 if (err)
4844 goto done;
4846 err = delete_ref(commit_ref_name, repo);
4847 done:
4848 free(commit_ref_name);
4849 return err;