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 (*progress_cb)(progress_arg,
807 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 if (fsync(merged_fd) != 0) {
810 err = got_error_from_errno("fsync");
811 goto done;
814 /* Check if a clean merge has subsumed all local changes. */
815 if (overlapcnt == 0) {
816 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
817 merged_path);
818 if (err)
819 goto done;
822 if (chmod(merged_path, st_mode) != 0) {
823 err = got_error_from_errno2("chmod", merged_path);
824 goto done;
827 if (rename(merged_path, ondisk_path) != 0) {
828 err = got_error_from_errno3("rename", merged_path,
829 ondisk_path);
830 unlink(merged_path);
831 goto done;
834 done:
835 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
836 err = got_error_from_errno("close");
837 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
838 err = got_error_from_errno("fclose");
839 if (f_orig && fclose(f_orig) != 0 && err == NULL)
840 err = got_error_from_errno("fclose");
841 free(merged_path);
842 free(base_path);
843 if (blob_deriv_path) {
844 unlink(blob_deriv_path);
845 free(blob_deriv_path);
847 if (blob_orig_path) {
848 unlink(blob_orig_path);
849 free(blob_orig_path);
851 free(id_str);
852 free(label_deriv);
853 return err;
856 static const struct got_error *
857 update_blob_fileindex_entry(struct got_worktree *worktree,
858 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
859 const char *ondisk_path, const char *path, struct got_blob_object *blob,
860 int update_timestamps)
862 const struct got_error *err = NULL;
864 if (ie == NULL)
865 ie = got_fileindex_entry_get(fileindex, path);
866 if (ie)
867 err = got_fileindex_entry_update(ie, ondisk_path,
868 blob->id.sha1, worktree->base_commit_id->sha1,
869 update_timestamps);
870 else {
871 struct got_fileindex_entry *new_ie;
872 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
873 path, blob->id.sha1, worktree->base_commit_id->sha1);
874 if (!err)
875 err = got_fileindex_entry_add(fileindex, new_ie);
877 return err;
880 static const struct got_error *
881 install_blob(struct got_worktree *worktree, const char *ondisk_path,
882 const char *path, uint16_t te_mode, uint16_t st_mode,
883 struct got_blob_object *blob, int restoring_missing_file,
884 int reverting_versioned_file, struct got_repository *repo,
885 got_worktree_checkout_cb progress_cb, void *progress_arg)
887 const struct got_error *err = NULL;
888 int fd = -1;
889 size_t len, hdrlen;
890 int update = 0;
891 char *tmppath = NULL;
893 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
894 GOT_DEFAULT_FILE_MODE);
895 if (fd == -1) {
896 if (errno == ENOENT) {
897 char *parent = dirname(path);
898 if (parent == NULL)
899 return got_error_from_errno2("dirname", path);
900 err = add_dir_on_disk(worktree, parent);
901 if (err)
902 return err;
903 fd = open(ondisk_path,
904 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
905 GOT_DEFAULT_FILE_MODE);
906 if (fd == -1)
907 return got_error_from_errno2("open",
908 ondisk_path);
909 } else if (errno == EEXIST) {
910 if (!S_ISREG(st_mode)) {
911 /* TODO file is obstructed; do something */
912 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
913 goto done;
914 } else {
915 err = got_opentemp_named_fd(&tmppath, &fd,
916 ondisk_path);
917 if (err)
918 goto done;
919 update = 1;
921 } else
922 return got_error_from_errno2("open", ondisk_path);
925 if (restoring_missing_file)
926 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
927 else if (reverting_versioned_file)
928 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
929 else
930 (*progress_cb)(progress_arg,
931 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
933 hdrlen = got_object_blob_get_hdrlen(blob);
934 do {
935 const uint8_t *buf = got_object_blob_get_read_buf(blob);
936 err = got_object_blob_read_block(&len, blob);
937 if (err)
938 break;
939 if (len > 0) {
940 /* Skip blob object header first time around. */
941 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
942 if (outlen == -1) {
943 err = got_error_from_errno("write");
944 goto done;
945 } else if (outlen != len - hdrlen) {
946 err = got_error(GOT_ERR_IO);
947 goto done;
949 hdrlen = 0;
951 } while (len != 0);
953 if (fsync(fd) != 0) {
954 err = got_error_from_errno("fsync");
955 goto done;
958 if (update) {
959 if (rename(tmppath, ondisk_path) != 0) {
960 err = got_error_from_errno3("rename", tmppath,
961 ondisk_path);
962 unlink(tmppath);
963 goto done;
967 if (te_mode & S_IXUSR) {
968 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
969 err = got_error_from_errno2("chmod", ondisk_path);
970 goto done;
972 } else {
973 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
974 err = got_error_from_errno2("chmod", ondisk_path);
975 goto done;
979 done:
980 if (fd != -1 && close(fd) != 0 && err == NULL)
981 err = got_error_from_errno("close");
982 free(tmppath);
983 return err;
986 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
987 static const struct got_error *
988 get_modified_file_content_status(unsigned char *status, FILE *f)
990 const struct got_error *err = NULL;
991 const char *markers[3] = {
992 GOT_DIFF_CONFLICT_MARKER_BEGIN,
993 GOT_DIFF_CONFLICT_MARKER_SEP,
994 GOT_DIFF_CONFLICT_MARKER_END
995 };
996 int i = 0;
997 char *line;
998 size_t len;
999 const char delim[3] = {'\0', '\0', '\0'};
1001 while (*status == GOT_STATUS_MODIFY) {
1002 line = fparseln(f, &len, NULL, delim, 0);
1003 if (line == NULL) {
1004 if (feof(f))
1005 break;
1006 err = got_ferror(f, GOT_ERR_IO);
1007 break;
1010 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1011 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1012 == 0)
1013 *status = GOT_STATUS_CONFLICT;
1014 else
1015 i++;
1019 return err;
1022 static const struct got_error *
1023 get_file_status(unsigned char *status, struct stat *sb,
1024 struct got_fileindex_entry *ie, const char *abspath,
1025 struct got_repository *repo)
1027 const struct got_error *err = NULL;
1028 struct got_object_id id;
1029 size_t hdrlen;
1030 FILE *f = NULL;
1031 uint8_t fbuf[8192];
1032 struct got_blob_object *blob = NULL;
1033 size_t flen, blen;
1035 *status = GOT_STATUS_NO_CHANGE;
1037 if (lstat(abspath, sb) == -1) {
1038 if (errno == ENOENT) {
1039 if (ie) {
1040 if (got_fileindex_entry_has_file_on_disk(ie))
1041 *status = GOT_STATUS_MISSING;
1042 else
1043 *status = GOT_STATUS_DELETE;
1044 sb->st_mode =
1045 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1046 & (S_IRWXU | S_IRWXG | S_IRWXO));
1047 } else
1048 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1049 return NULL;
1051 return got_error_from_errno2("lstat", abspath);
1054 if (!S_ISREG(sb->st_mode)) {
1055 *status = GOT_STATUS_OBSTRUCTED;
1056 return NULL;
1059 if (ie == NULL)
1060 return NULL;
1062 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1063 *status = GOT_STATUS_DELETE;
1064 return NULL;
1065 } else if (!got_fileindex_entry_has_blob(ie)) {
1066 *status = GOT_STATUS_ADD;
1067 return NULL;
1070 if (ie->ctime_sec == sb->st_ctime &&
1071 ie->ctime_nsec == sb->st_ctimensec &&
1072 ie->mtime_sec == sb->st_mtime &&
1073 ie->mtime_sec == sb->st_mtime &&
1074 ie->mtime_nsec == sb->st_mtimensec &&
1075 ie->size == (sb->st_size & 0xffffffff))
1076 return NULL;
1078 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1079 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1080 if (err)
1081 return err;
1083 f = fopen(abspath, "r");
1084 if (f == NULL) {
1085 err = got_error_from_errno2("fopen", abspath);
1086 goto done;
1088 hdrlen = got_object_blob_get_hdrlen(blob);
1089 for (;;) {
1090 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1091 err = got_object_blob_read_block(&blen, blob);
1092 if (err)
1093 goto done;
1094 /* Skip length of blob object header first time around. */
1095 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1096 if (flen == 0 && ferror(f)) {
1097 err = got_error_from_errno("fread");
1098 goto done;
1100 if (blen == 0) {
1101 if (flen != 0)
1102 *status = GOT_STATUS_MODIFY;
1103 break;
1104 } else if (flen == 0) {
1105 if (blen != 0)
1106 *status = GOT_STATUS_MODIFY;
1107 break;
1108 } else if (blen - hdrlen == flen) {
1109 /* Skip blob object header first time around. */
1110 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1111 *status = GOT_STATUS_MODIFY;
1112 break;
1114 } else {
1115 *status = GOT_STATUS_MODIFY;
1116 break;
1118 hdrlen = 0;
1121 if (*status == GOT_STATUS_MODIFY) {
1122 rewind(f);
1123 err = get_modified_file_content_status(status, f);
1125 done:
1126 if (blob)
1127 got_object_blob_close(blob);
1128 if (f)
1129 fclose(f);
1130 return err;
1133 static const struct got_error *
1134 update_blob(struct got_worktree *worktree,
1135 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1136 struct got_tree_entry *te, const char *path,
1137 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1138 void *progress_arg)
1140 const struct got_error *err = NULL;
1141 struct got_blob_object *blob = NULL;
1142 char *ondisk_path;
1143 unsigned char status = GOT_STATUS_NO_CHANGE;
1144 struct stat sb;
1146 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1147 return got_error_from_errno("asprintf");
1149 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1150 if (err)
1151 goto done;
1153 if (status == GOT_STATUS_OBSTRUCTED) {
1154 (*progress_cb)(progress_arg, status, path);
1155 goto done;
1158 if (ie && status != GOT_STATUS_MISSING) {
1159 if (got_fileindex_entry_has_commit(ie) &&
1160 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1161 SHA1_DIGEST_LENGTH) == 0) {
1162 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1163 path);
1164 goto done;
1166 if (got_fileindex_entry_has_blob(ie) &&
1167 memcmp(ie->blob_sha1, te->id->sha1,
1168 SHA1_DIGEST_LENGTH) == 0)
1169 goto done;
1172 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1173 if (err)
1174 goto done;
1176 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1177 int update_timestamps;
1178 struct got_blob_object *blob2 = NULL;
1179 if (got_fileindex_entry_has_blob(ie)) {
1180 struct got_object_id id2;
1181 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1182 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1183 if (err)
1184 goto done;
1186 err = merge_blob(&update_timestamps, worktree, blob2,
1187 ondisk_path, path, sb.st_mode, blob,
1188 worktree->base_commit_id, repo,
1189 progress_cb, progress_arg);
1190 if (blob2)
1191 got_object_blob_close(blob2);
1193 * Do not update timestamps of files with local changes.
1194 * Otherwise, a future status walk would treat them as
1195 * unmodified files again.
1197 err = got_fileindex_entry_update(ie, ondisk_path,
1198 blob->id.sha1, worktree->base_commit_id->sha1,
1199 update_timestamps);
1200 } else if (status == GOT_STATUS_DELETE) {
1201 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1202 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1203 ondisk_path, path, blob, 0);
1204 if (err)
1205 goto done;
1206 } else {
1207 err = install_blob(worktree, ondisk_path, path, te->mode,
1208 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1209 repo, progress_cb, progress_arg);
1210 if (err)
1211 goto done;
1212 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1213 ondisk_path, path, blob, 1);
1214 if (err)
1215 goto done;
1217 got_object_blob_close(blob);
1218 done:
1219 free(ondisk_path);
1220 return err;
1223 static const struct got_error *
1224 remove_ondisk_file(const char *root_path, const char *path)
1226 const struct got_error *err = NULL;
1227 char *ondisk_path = NULL;
1229 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1230 return got_error_from_errno("asprintf");
1232 if (unlink(ondisk_path) == -1) {
1233 if (errno != ENOENT)
1234 err = got_error_from_errno2("unlink", ondisk_path);
1235 } else {
1236 char *parent = dirname(ondisk_path);
1237 while (parent && strcmp(parent, root_path) != 0) {
1238 if (rmdir(parent) == -1) {
1239 if (errno != ENOTEMPTY)
1240 err = got_error_from_errno2("rmdir",
1241 parent);
1242 break;
1244 parent = dirname(parent);
1247 free(ondisk_path);
1248 return err;
1251 static const struct got_error *
1252 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1253 struct got_fileindex_entry *ie, struct got_repository *repo,
1254 got_worktree_checkout_cb progress_cb, void *progress_arg)
1256 const struct got_error *err = NULL;
1257 unsigned char status;
1258 struct stat sb;
1259 char *ondisk_path;
1261 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1262 == -1)
1263 return got_error_from_errno("asprintf");
1265 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1266 if (err)
1267 return err;
1269 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1270 status == GOT_STATUS_ADD) {
1271 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1273 * Preserve the working file and change the deleted blob's
1274 * entry into a schedule-add entry.
1276 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1277 0);
1278 if (err)
1279 return err;
1280 } else {
1281 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1282 if (status == GOT_STATUS_NO_CHANGE) {
1283 err = remove_ondisk_file(worktree->root_path, ie->path);
1284 if (err)
1285 return err;
1287 got_fileindex_entry_remove(fileindex, ie);
1290 return err;
1293 struct diff_cb_arg {
1294 struct got_fileindex *fileindex;
1295 struct got_worktree *worktree;
1296 struct got_repository *repo;
1297 got_worktree_checkout_cb progress_cb;
1298 void *progress_arg;
1299 got_worktree_cancel_cb cancel_cb;
1300 void *cancel_arg;
1303 static const struct got_error *
1304 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1305 struct got_tree_entry *te, const char *parent_path)
1307 struct diff_cb_arg *a = arg;
1309 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1310 return got_error(GOT_ERR_CANCELLED);
1312 return update_blob(a->worktree, a->fileindex, ie, te,
1313 ie->path, a->repo, a->progress_cb, a->progress_arg);
1316 static const struct got_error *
1317 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1319 struct diff_cb_arg *a = arg;
1321 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1322 return got_error(GOT_ERR_CANCELLED);
1324 return delete_blob(a->worktree, a->fileindex, ie,
1325 a->repo, a->progress_cb, a->progress_arg);
1328 static const struct got_error *
1329 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1331 struct diff_cb_arg *a = arg;
1332 const struct got_error *err;
1333 char *path;
1335 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1336 return got_error(GOT_ERR_CANCELLED);
1338 if (asprintf(&path, "%s%s%s", parent_path,
1339 parent_path[0] ? "/" : "", te->name)
1340 == -1)
1341 return got_error_from_errno("asprintf");
1343 if (S_ISDIR(te->mode))
1344 err = add_dir_on_disk(a->worktree, path);
1345 else
1346 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1347 a->repo, a->progress_cb, a->progress_arg);
1349 free(path);
1350 return err;
1353 static const struct got_error *
1354 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1356 const struct got_error *err = NULL;
1357 char *uuidstr = NULL;
1358 uint32_t uuid_status;
1360 *refname = NULL;
1362 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1363 if (uuid_status != uuid_s_ok)
1364 return got_error_uuid(uuid_status);
1366 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1367 == -1) {
1368 err = got_error_from_errno("asprintf");
1369 *refname = NULL;
1371 free(uuidstr);
1372 return err;
1375 const struct got_error *
1376 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1378 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1381 static const struct got_error *
1382 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1384 return get_ref_name(refname, worktree,
1385 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1388 static const struct got_error *
1389 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1391 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1394 static const struct got_error *
1395 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1397 return get_ref_name(refname, worktree,
1398 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1401 static const struct got_error *
1402 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1404 return get_ref_name(refname, worktree,
1405 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1410 * Prevent Git's garbage collector from deleting our base commit by
1411 * setting a reference to our base commit's ID.
1413 static const struct got_error *
1414 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1416 const struct got_error *err = NULL;
1417 struct got_reference *ref = NULL;
1418 char *refname;
1420 err = got_worktree_get_base_ref_name(&refname, worktree);
1421 if (err)
1422 return err;
1424 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1425 if (err)
1426 goto done;
1428 err = got_ref_write(ref, repo);
1429 done:
1430 free(refname);
1431 if (ref)
1432 got_ref_close(ref);
1433 return err;
1436 static const struct got_error *
1437 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1438 struct got_worktree *worktree)
1440 const struct got_error *err = NULL;
1441 FILE *index = NULL;
1443 *fileindex_path = NULL;
1444 *fileindex = got_fileindex_alloc();
1445 if (*fileindex == NULL)
1446 return got_error_from_errno("got_fileindex_alloc");
1448 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1449 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1450 err = got_error_from_errno("asprintf");
1451 *fileindex_path = NULL;
1452 goto done;
1455 index = fopen(*fileindex_path, "rb");
1456 if (index == NULL) {
1457 if (errno != ENOENT)
1458 err = got_error_from_errno2("fopen", *fileindex_path);
1459 } else {
1460 err = got_fileindex_read(*fileindex, index);
1461 if (fclose(index) != 0 && err == NULL)
1462 err = got_error_from_errno("fclose");
1464 done:
1465 if (err) {
1466 free(*fileindex_path);
1467 *fileindex_path = NULL;
1468 got_fileindex_free(*fileindex);
1469 *fileindex = NULL;
1471 return err;
1474 struct bump_base_commit_id_arg {
1475 struct got_object_id *base_commit_id;
1476 const char *path;
1477 size_t path_len;
1478 const char *entry_name;
1479 got_worktree_checkout_cb progress_cb;
1480 void *progress_arg;
1483 /* Bump base commit ID of all files within an updated part of the work tree. */
1484 static const struct got_error *
1485 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1487 struct bump_base_commit_id_arg *a = arg;
1489 if (a->entry_name) {
1490 if (strcmp(ie->path, a->path) != 0)
1491 return NULL;
1492 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1493 return NULL;
1495 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1496 SHA1_DIGEST_LENGTH) == 0)
1497 return NULL;
1499 (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1500 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1501 return NULL;
1504 static const struct got_error *
1505 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1507 const struct got_error *err = NULL;
1508 char *new_fileindex_path = NULL;
1509 FILE *new_index = NULL;
1511 err = got_opentemp_named(&new_fileindex_path, &new_index,
1512 fileindex_path);
1513 if (err)
1514 goto done;
1516 err = got_fileindex_write(fileindex, new_index);
1517 if (err)
1518 goto done;
1520 if (rename(new_fileindex_path, fileindex_path) != 0) {
1521 err = got_error_from_errno3("rename", new_fileindex_path,
1522 fileindex_path);
1523 unlink(new_fileindex_path);
1525 done:
1526 if (new_index)
1527 fclose(new_index);
1528 free(new_fileindex_path);
1529 return err;
1532 const struct got_error *
1533 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1534 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1535 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1537 const struct got_error *err = NULL, *sync_err, *unlockerr;
1538 struct got_commit_object *commit = NULL;
1539 struct got_object_id *tree_id = NULL;
1540 struct got_tree_object *tree = NULL;
1541 struct got_fileindex *fileindex = NULL;
1542 char *fileindex_path = NULL;
1543 struct got_fileindex_diff_tree_cb diff_cb;
1544 struct diff_cb_arg arg;
1545 char *relpath = NULL, *entry_name = NULL;
1546 struct bump_base_commit_id_arg bbc_arg;
1548 err = lock_worktree(worktree, LOCK_EX);
1549 if (err)
1550 return err;
1553 * Read the file index.
1554 * Checking out files is supposed to be an idempotent operation.
1555 * If the on-disk file index is incomplete we will try to complete it.
1557 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1558 if (err)
1559 goto done;
1561 err = ref_base_commit(worktree, repo);
1562 if (err)
1563 goto done;
1565 err = got_object_open_as_commit(&commit, repo,
1566 worktree->base_commit_id);
1567 if (err)
1568 goto done;
1570 if (path[0]) {
1571 char *tree_path;
1572 int obj_type;
1573 relpath = strdup(path);
1574 if (relpath == NULL) {
1575 err = got_error_from_errno("strdup");
1576 goto done;
1578 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1579 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1580 path) == -1) {
1581 err = got_error_from_errno("asprintf");
1582 goto done;
1584 err = got_object_id_by_path(&tree_id, repo,
1585 worktree->base_commit_id, tree_path);
1586 free(tree_path);
1587 if (err)
1588 goto done;
1589 err = got_object_get_type(&obj_type, repo, tree_id);
1590 if (err)
1591 goto done;
1592 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1593 /* Split provided path into parent dir + entry name. */
1594 if (strchr(path, '/') == NULL) {
1595 relpath = strdup("");
1596 if (relpath == NULL) {
1597 err = got_error_from_errno("strdup");
1598 goto done;
1600 tree_path = strdup(worktree->path_prefix);
1601 if (tree_path == NULL) {
1602 err = got_error_from_errno("strdup");
1603 goto done;
1605 } else {
1606 err = got_path_dirname(&relpath, path);
1607 if (err)
1608 goto done;
1609 if (asprintf(&tree_path, "%s%s%s",
1610 worktree->path_prefix,
1611 got_path_is_root_dir(
1612 worktree->path_prefix) ? "" : "/",
1613 relpath) == -1) {
1614 err = got_error_from_errno("asprintf");
1615 goto done;
1618 err = got_object_id_by_path(&tree_id, repo,
1619 worktree->base_commit_id, tree_path);
1620 free(tree_path);
1621 if (err)
1622 goto done;
1623 entry_name = basename(path);
1624 if (entry_name == NULL) {
1625 err = got_error_from_errno2("basename", path);
1626 goto done;
1629 } else {
1630 relpath = strdup("");
1631 if (relpath == NULL) {
1632 err = got_error_from_errno("strdup");
1633 goto done;
1635 err = got_object_id_by_path(&tree_id, repo,
1636 worktree->base_commit_id, worktree->path_prefix);
1637 if (err)
1638 goto done;
1641 err = got_object_open_as_tree(&tree, repo, tree_id);
1642 if (err)
1643 goto done;
1645 if (entry_name &&
1646 got_object_tree_find_entry(tree, entry_name) == NULL) {
1647 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1648 goto done;
1651 diff_cb.diff_old_new = diff_old_new;
1652 diff_cb.diff_old = diff_old;
1653 diff_cb.diff_new = diff_new;
1654 arg.fileindex = fileindex;
1655 arg.worktree = worktree;
1656 arg.repo = repo;
1657 arg.progress_cb = progress_cb;
1658 arg.progress_arg = progress_arg;
1659 arg.cancel_cb = cancel_cb;
1660 arg.cancel_arg = cancel_arg;
1661 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1662 entry_name, repo, &diff_cb, &arg);
1663 if (err)
1664 goto sync;
1666 bbc_arg.base_commit_id = worktree->base_commit_id;
1667 bbc_arg.entry_name = entry_name;
1668 bbc_arg.path = path;
1669 bbc_arg.path_len = strlen(path);
1670 bbc_arg.progress_cb = progress_cb;
1671 bbc_arg.progress_arg = progress_arg;
1672 err = got_fileindex_for_each_entry_safe(fileindex,
1673 bump_base_commit_id, &bbc_arg);
1674 sync:
1675 sync_err = sync_fileindex(fileindex, fileindex_path);
1676 if (sync_err && err == NULL)
1677 err = sync_err;
1678 done:
1679 free(fileindex_path);
1680 free(relpath);
1681 if (tree)
1682 got_object_tree_close(tree);
1683 if (commit)
1684 got_object_commit_close(commit);
1685 got_fileindex_free(fileindex);
1686 unlockerr = lock_worktree(worktree, LOCK_SH);
1687 if (unlockerr && err == NULL)
1688 err = unlockerr;
1689 return err;
1692 struct merge_file_cb_arg {
1693 struct got_worktree *worktree;
1694 struct got_fileindex *fileindex;
1695 got_worktree_checkout_cb progress_cb;
1696 void *progress_arg;
1697 got_worktree_cancel_cb cancel_cb;
1698 void *cancel_arg;
1699 struct got_object_id *commit_id2;
1702 static const struct got_error *
1703 merge_file_cb(void *arg, struct got_blob_object *blob1,
1704 struct got_blob_object *blob2, struct got_object_id *id1,
1705 struct got_object_id *id2, const char *path1, const char *path2,
1706 struct got_repository *repo)
1708 static const struct got_error *err = NULL;
1709 struct merge_file_cb_arg *a = arg;
1710 struct got_fileindex_entry *ie;
1711 char *ondisk_path = NULL;
1712 struct stat sb;
1713 unsigned char status;
1714 int local_changes_subsumed;
1716 if (blob1 && blob2) {
1717 ie = got_fileindex_entry_get(a->fileindex, path2);
1718 if (ie == NULL) {
1719 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1720 path2);
1721 return NULL;
1724 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1725 path2) == -1)
1726 return got_error_from_errno("asprintf");
1728 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1729 if (err)
1730 goto done;
1732 if (status == GOT_STATUS_DELETE) {
1733 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1734 path2);
1735 goto done;
1737 if (status != GOT_STATUS_NO_CHANGE &&
1738 status != GOT_STATUS_MODIFY &&
1739 status != GOT_STATUS_CONFLICT &&
1740 status != GOT_STATUS_ADD) {
1741 (*a->progress_cb)(a->progress_arg, status, path2);
1742 goto done;
1745 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1746 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1747 a->progress_cb, a->progress_arg);
1748 } else if (blob1) {
1749 ie = got_fileindex_entry_get(a->fileindex, path1);
1750 if (ie == NULL) {
1751 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1752 path2);
1753 return NULL;
1756 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1757 path1) == -1)
1758 return got_error_from_errno("asprintf");
1760 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1761 if (err)
1762 goto done;
1764 switch (status) {
1765 case GOT_STATUS_NO_CHANGE:
1766 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1767 path1);
1768 err = remove_ondisk_file(a->worktree->root_path, path1);
1769 if (err)
1770 goto done;
1771 if (ie)
1772 got_fileindex_entry_mark_deleted_from_disk(ie);
1773 break;
1774 case GOT_STATUS_DELETE:
1775 case GOT_STATUS_MISSING:
1776 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1777 path1);
1778 if (ie)
1779 got_fileindex_entry_mark_deleted_from_disk(ie);
1780 break;
1781 case GOT_STATUS_ADD:
1782 case GOT_STATUS_MODIFY:
1783 case GOT_STATUS_CONFLICT:
1784 (*a->progress_cb)(a->progress_arg,
1785 GOT_STATUS_CANNOT_DELETE, path1);
1786 break;
1787 case GOT_STATUS_OBSTRUCTED:
1788 (*a->progress_cb)(a->progress_arg, status, path1);
1789 break;
1790 default:
1791 break;
1793 } else if (blob2) {
1794 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1795 path2) == -1)
1796 return got_error_from_errno("asprintf");
1797 ie = got_fileindex_entry_get(a->fileindex, path2);
1798 if (ie) {
1799 err = get_file_status(&status, &sb, ie, ondisk_path,
1800 repo);
1801 if (err)
1802 goto done;
1803 if (status != GOT_STATUS_NO_CHANGE &&
1804 status != GOT_STATUS_MODIFY &&
1805 status != GOT_STATUS_CONFLICT &&
1806 status != GOT_STATUS_ADD) {
1807 (*a->progress_cb)(a->progress_arg, status,
1808 path2);
1809 goto done;
1811 err = merge_blob(&local_changes_subsumed, a->worktree,
1812 NULL, ondisk_path, path2, sb.st_mode, blob2,
1813 a->commit_id2, repo,
1814 a->progress_cb, a->progress_arg);
1815 if (status == GOT_STATUS_DELETE) {
1816 err = update_blob_fileindex_entry(a->worktree,
1817 a->fileindex, ie, ondisk_path, ie->path,
1818 blob2, 0);
1819 if (err)
1820 goto done;
1822 } else {
1823 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1824 err = install_blob(a->worktree, ondisk_path, path2,
1825 /* XXX get this from parent tree! */
1826 GOT_DEFAULT_FILE_MODE,
1827 sb.st_mode, blob2, 0, 0, repo,
1828 a->progress_cb, a->progress_arg);
1829 if (err)
1830 goto done;
1831 err = got_fileindex_entry_alloc(&ie,
1832 ondisk_path, path2, NULL, NULL);
1833 if (err)
1834 goto done;
1835 err = got_fileindex_entry_add(a->fileindex, ie);
1836 if (err) {
1837 got_fileindex_entry_free(ie);
1838 goto done;
1842 done:
1843 free(ondisk_path);
1844 return err;
1847 struct check_merge_ok_arg {
1848 struct got_worktree *worktree;
1849 struct got_repository *repo;
1852 static const struct got_error *
1853 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1855 const struct got_error *err = NULL;
1856 struct check_merge_ok_arg *a = arg;
1857 unsigned char status;
1858 struct stat sb;
1859 char *ondisk_path;
1861 /* Reject merges into a work tree with mixed base commits. */
1862 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1863 SHA1_DIGEST_LENGTH))
1864 return got_error(GOT_ERR_MIXED_COMMITS);
1866 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1867 == -1)
1868 return got_error_from_errno("asprintf");
1870 /* Reject merges into a work tree with conflicted files. */
1871 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1872 if (err)
1873 return err;
1874 if (status == GOT_STATUS_CONFLICT)
1875 return got_error(GOT_ERR_CONFLICTS);
1877 return NULL;
1880 static const struct got_error *
1881 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1882 const char *fileindex_path, struct got_object_id *commit_id1,
1883 struct got_object_id *commit_id2, struct got_repository *repo,
1884 got_worktree_checkout_cb progress_cb, void *progress_arg,
1885 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1887 const struct got_error *err = NULL, *sync_err;
1888 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1889 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1890 struct merge_file_cb_arg arg;
1892 if (commit_id1) {
1893 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1894 worktree->path_prefix);
1895 if (err)
1896 goto done;
1898 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1899 if (err)
1900 goto done;
1903 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1904 worktree->path_prefix);
1905 if (err)
1906 goto done;
1908 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1909 if (err)
1910 goto done;
1912 arg.worktree = worktree;
1913 arg.fileindex = fileindex;
1914 arg.progress_cb = progress_cb;
1915 arg.progress_arg = progress_arg;
1916 arg.cancel_cb = cancel_cb;
1917 arg.cancel_arg = cancel_arg;
1918 arg.commit_id2 = commit_id2;
1919 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1920 sync_err = sync_fileindex(fileindex, fileindex_path);
1921 if (sync_err && err == NULL)
1922 err = sync_err;
1923 done:
1924 if (tree1)
1925 got_object_tree_close(tree1);
1926 if (tree2)
1927 got_object_tree_close(tree2);
1928 return err;
1931 const struct got_error *
1932 got_worktree_merge_files(struct got_worktree *worktree,
1933 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1934 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1935 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1937 const struct got_error *err, *unlockerr;
1938 char *fileindex_path = NULL;
1939 struct got_fileindex *fileindex = NULL;
1940 struct check_merge_ok_arg mok_arg;
1942 err = lock_worktree(worktree, LOCK_EX);
1943 if (err)
1944 return err;
1946 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1947 if (err)
1948 goto done;
1950 mok_arg.worktree = worktree;
1951 mok_arg.repo = repo;
1952 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1953 &mok_arg);
1954 if (err)
1955 goto done;
1957 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
1958 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1959 done:
1960 if (fileindex)
1961 got_fileindex_free(fileindex);
1962 free(fileindex_path);
1963 unlockerr = lock_worktree(worktree, LOCK_SH);
1964 if (unlockerr && err == NULL)
1965 err = unlockerr;
1966 return err;
1969 struct diff_dir_cb_arg {
1970 struct got_fileindex *fileindex;
1971 struct got_worktree *worktree;
1972 const char *status_path;
1973 size_t status_path_len;
1974 struct got_repository *repo;
1975 got_worktree_status_cb status_cb;
1976 void *status_arg;
1977 got_worktree_cancel_cb cancel_cb;
1978 void *cancel_arg;
1981 static const struct got_error *
1982 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1983 got_worktree_status_cb status_cb, void *status_arg,
1984 struct got_repository *repo)
1986 const struct got_error *err = NULL;
1987 unsigned char status = GOT_STATUS_NO_CHANGE;
1988 struct stat sb;
1989 struct got_object_id blob_id, commit_id;
1991 err = get_file_status(&status, &sb, ie, abspath, repo);
1992 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1993 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1994 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1995 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1996 &commit_id);
1998 return err;
2001 static const struct got_error *
2002 status_old_new(void *arg, struct got_fileindex_entry *ie,
2003 struct dirent *de, const char *parent_path)
2005 const struct got_error *err = NULL;
2006 struct diff_dir_cb_arg *a = arg;
2007 char *abspath;
2009 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2010 return got_error(GOT_ERR_CANCELLED);
2012 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2013 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2014 return NULL;
2016 if (parent_path[0]) {
2017 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2018 parent_path, de->d_name) == -1)
2019 return got_error_from_errno("asprintf");
2020 } else {
2021 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2022 de->d_name) == -1)
2023 return got_error_from_errno("asprintf");
2026 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2027 a->repo);
2028 free(abspath);
2029 return err;
2032 static const struct got_error *
2033 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2035 struct diff_dir_cb_arg *a = arg;
2036 struct got_object_id blob_id, commit_id;
2037 unsigned char status;
2039 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2040 return got_error(GOT_ERR_CANCELLED);
2042 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2043 return NULL;
2045 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2046 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2047 if (got_fileindex_entry_has_file_on_disk(ie))
2048 status = GOT_STATUS_MISSING;
2049 else
2050 status = GOT_STATUS_DELETE;
2051 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2052 &commit_id);
2055 static const struct got_error *
2056 status_new(void *arg, struct dirent *de, const char *parent_path)
2058 const struct got_error *err = NULL;
2059 struct diff_dir_cb_arg *a = arg;
2060 char *path = NULL;
2062 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2063 return got_error(GOT_ERR_CANCELLED);
2065 if (de->d_type == DT_DIR)
2066 return NULL;
2068 /* XXX ignore symlinks for now */
2069 if (de->d_type == DT_LNK)
2070 return NULL;
2072 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2073 return NULL;
2075 if (parent_path[0]) {
2076 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2077 return got_error_from_errno("asprintf");
2078 } else {
2079 path = de->d_name;
2082 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2083 NULL, NULL);
2084 if (parent_path[0])
2085 free(path);
2086 return err;
2089 const struct got_error *
2090 got_worktree_status(struct got_worktree *worktree, const char *path,
2091 struct got_repository *repo, got_worktree_status_cb status_cb,
2092 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2094 const struct got_error *err = NULL;
2095 DIR *workdir = NULL;
2096 char *fileindex_path = NULL;
2097 struct got_fileindex *fileindex = NULL;
2098 FILE *index = NULL;
2099 struct got_fileindex_diff_dir_cb fdiff_cb;
2100 struct diff_dir_cb_arg arg;
2101 char *ondisk_path = NULL;
2103 fileindex = got_fileindex_alloc();
2104 if (fileindex == NULL) {
2105 err = got_error_from_errno("got_fileindex_alloc");
2106 goto done;
2109 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2110 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2111 err = got_error_from_errno("asprintf");
2112 fileindex_path = NULL;
2113 goto done;
2116 index = fopen(fileindex_path, "rb");
2117 if (index == NULL) {
2118 if (errno != ENOENT) {
2119 err = got_error_from_errno2("fopen", fileindex_path);
2120 goto done;
2122 } else {
2123 err = got_fileindex_read(fileindex, index);
2124 fclose(index);
2125 if (err)
2126 goto done;
2129 if (asprintf(&ondisk_path, "%s%s%s",
2130 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2131 err = got_error_from_errno("asprintf");
2132 goto done;
2134 workdir = opendir(ondisk_path);
2135 if (workdir == NULL) {
2136 if (errno == ENOTDIR || errno == ENOENT) {
2137 struct got_fileindex_entry *ie;
2138 ie = got_fileindex_entry_get(fileindex, path);
2139 if (ie == NULL) {
2140 err = got_error(GOT_ERR_BAD_PATH);
2141 goto done;
2143 err = report_file_status(ie, ondisk_path,
2144 status_cb, status_arg, repo);
2145 goto done;
2146 } else {
2147 err = got_error_from_errno2("opendir", ondisk_path);
2148 goto done;
2151 fdiff_cb.diff_old_new = status_old_new;
2152 fdiff_cb.diff_old = status_old;
2153 fdiff_cb.diff_new = status_new;
2154 arg.fileindex = fileindex;
2155 arg.worktree = worktree;
2156 arg.status_path = path;
2157 arg.status_path_len = strlen(path);
2158 arg.repo = repo;
2159 arg.status_cb = status_cb;
2160 arg.status_arg = status_arg;
2161 arg.cancel_cb = cancel_cb;
2162 arg.cancel_arg = cancel_arg;
2163 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2164 path, repo, &fdiff_cb, &arg);
2165 done:
2166 if (workdir)
2167 closedir(workdir);
2168 free(ondisk_path);
2169 free(fileindex_path);
2170 got_fileindex_free(fileindex);
2171 return err;
2174 const struct got_error *
2175 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2176 const char *arg)
2178 const struct got_error *err = NULL;
2179 char *resolved, *path = NULL;
2180 size_t len;
2182 *wt_path = NULL;
2184 resolved = realpath(arg, NULL);
2185 if (resolved == NULL)
2186 return got_error_from_errno2("realpath", arg);
2188 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2189 strlen(got_worktree_get_root_path(worktree)))) {
2190 err = got_error(GOT_ERR_BAD_PATH);
2191 goto done;
2194 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2195 err = got_path_skip_common_ancestor(&path,
2196 got_worktree_get_root_path(worktree), resolved);
2197 if (err)
2198 goto done;
2199 } else {
2200 path = strdup("");
2201 if (path == NULL) {
2202 err = got_error_from_errno("strdup");
2203 goto done;
2207 /* XXX status walk can't deal with trailing slash! */
2208 len = strlen(path);
2209 while (path[len - 1] == '/') {
2210 path[len - 1] = '\0';
2211 len--;
2213 done:
2214 free(resolved);
2215 if (err == NULL)
2216 *wt_path = path;
2217 else
2218 free(path);
2219 return err;
2222 static const struct got_error *
2223 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2224 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2225 struct got_repository *repo)
2227 const struct got_error *err = NULL;
2228 struct got_fileindex_entry *ie;
2230 /* Re-adding an existing entry is a no-op. */
2231 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2232 return NULL;
2234 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2235 if (err)
2236 return err;
2238 err = got_fileindex_entry_add(fileindex, ie);
2239 if (err) {
2240 got_fileindex_entry_free(ie);
2241 return err;
2244 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2247 const struct got_error *
2248 got_worktree_schedule_add(struct got_worktree *worktree,
2249 struct got_pathlist_head *ondisk_paths,
2250 got_worktree_status_cb status_cb, void *status_arg,
2251 struct got_repository *repo)
2253 struct got_fileindex *fileindex = NULL;
2254 char *fileindex_path = NULL;
2255 FILE *index = NULL;
2256 const struct got_error *err = NULL, *sync_err, *unlockerr;
2257 struct got_pathlist_entry *pe;
2259 err = lock_worktree(worktree, LOCK_EX);
2260 if (err)
2261 return err;
2264 fileindex = got_fileindex_alloc();
2265 if (fileindex == NULL) {
2266 err = got_error_from_errno("got_fileindex_alloc");
2267 goto done;
2270 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2271 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2272 err = got_error_from_errno("asprintf");
2273 fileindex_path = NULL;
2274 goto done;
2277 index = fopen(fileindex_path, "rb");
2278 if (index == NULL) {
2279 err = got_error_from_errno2("fopen", fileindex_path);
2280 goto done;
2283 err = got_fileindex_read(fileindex, index);
2284 if (err)
2285 goto done;
2287 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2288 char *relpath;
2289 err = got_path_skip_common_ancestor(&relpath,
2290 got_worktree_get_root_path(worktree), pe->path);
2291 if (err)
2292 break;
2293 err = schedule_addition(pe->path, fileindex, relpath,
2294 status_cb, status_arg, repo);
2295 free(relpath);
2296 if (err)
2297 break;
2299 sync_err = sync_fileindex(fileindex, fileindex_path);
2300 if (sync_err && err == NULL)
2301 err = sync_err;
2302 done:
2303 if (index) {
2304 if (fclose(index) != 0 && err == NULL)
2305 err = got_error_from_errno("fclose");
2307 if (fileindex)
2308 got_fileindex_free(fileindex);
2309 unlockerr = lock_worktree(worktree, LOCK_SH);
2310 if (unlockerr && err == NULL)
2311 err = unlockerr;
2312 return err;
2315 static const struct got_error *
2316 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2317 const char *relpath, int delete_local_mods,
2318 got_worktree_status_cb status_cb, void *status_arg,
2319 struct got_repository *repo)
2321 const struct got_error *err = NULL;
2322 struct got_fileindex_entry *ie = NULL;
2323 unsigned char status;
2324 struct stat sb;
2326 ie = got_fileindex_entry_get(fileindex, relpath);
2327 if (ie == NULL)
2328 return got_error(GOT_ERR_BAD_PATH);
2330 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2331 if (err)
2332 return err;
2334 if (status != GOT_STATUS_NO_CHANGE) {
2335 if (status == GOT_STATUS_DELETE)
2336 return got_error_set_errno(ENOENT, ondisk_path);
2337 if (status != GOT_STATUS_MODIFY)
2338 return got_error(GOT_ERR_FILE_STATUS);
2339 if (!delete_local_mods)
2340 return got_error(GOT_ERR_FILE_MODIFIED);
2343 if (unlink(ondisk_path) != 0)
2344 return got_error_from_errno2("unlink", ondisk_path);
2346 got_fileindex_entry_mark_deleted_from_disk(ie);
2347 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2350 const struct got_error *
2351 got_worktree_schedule_delete(struct got_worktree *worktree,
2352 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2353 got_worktree_status_cb status_cb, void *status_arg,
2354 struct got_repository *repo)
2356 struct got_fileindex *fileindex = NULL;
2357 char *fileindex_path = NULL;
2358 FILE *index = NULL;
2359 const struct got_error *err = NULL, *sync_err, *unlockerr;
2360 struct got_pathlist_entry *pe;
2362 err = lock_worktree(worktree, LOCK_EX);
2363 if (err)
2364 return err;
2366 fileindex = got_fileindex_alloc();
2367 if (fileindex == NULL) {
2368 err = got_error_from_errno("got_fileindex_alloc");
2369 goto done;
2372 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2373 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2374 err = got_error_from_errno("asprintf");
2375 fileindex_path = NULL;
2376 goto done;
2379 index = fopen(fileindex_path, "rb");
2380 if (index == NULL) {
2381 err = got_error_from_errno2("fopen", fileindex_path);
2382 goto done;
2385 err = got_fileindex_read(fileindex, index);
2386 if (err)
2387 goto done;
2389 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2390 char *relpath;
2391 err = got_path_skip_common_ancestor(&relpath,
2392 got_worktree_get_root_path(worktree), pe->path);
2393 if (err)
2394 break;
2395 err = schedule_for_deletion(pe->path, fileindex, relpath,
2396 delete_local_mods, status_cb, status_arg, repo);
2397 free(relpath);
2398 if (err)
2399 break;
2401 sync_err = sync_fileindex(fileindex, fileindex_path);
2402 if (sync_err && err == NULL)
2403 err = sync_err;
2404 done:
2405 if (index) {
2406 if (fclose(index) != 0 && err == NULL)
2407 err = got_error_from_errno("fclose");
2409 if (fileindex)
2410 got_fileindex_free(fileindex);
2411 unlockerr = lock_worktree(worktree, LOCK_SH);
2412 if (unlockerr && err == NULL)
2413 err = unlockerr;
2414 return err;
2417 static const struct got_error *
2418 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2419 const char *ondisk_path,
2420 got_worktree_checkout_cb progress_cb, void *progress_arg,
2421 struct got_repository *repo)
2423 const struct got_error *err = NULL;
2424 char *relpath = NULL, *parent_path = NULL;
2425 struct got_fileindex_entry *ie;
2426 struct got_tree_object *tree = NULL;
2427 struct got_object_id *tree_id = NULL;
2428 const struct got_tree_entry *te;
2429 char *tree_path = NULL, *te_name;
2430 struct got_blob_object *blob = NULL;
2431 unsigned char status;
2432 struct stat sb;
2434 err = got_path_skip_common_ancestor(&relpath,
2435 got_worktree_get_root_path(worktree), ondisk_path);
2436 if (err)
2437 goto done;
2439 ie = got_fileindex_entry_get(fileindex, relpath);
2440 if (ie == NULL) {
2441 err = got_error(GOT_ERR_BAD_PATH);
2442 goto done;
2445 /* Construct in-repository path of tree which contains this blob. */
2446 err = got_path_dirname(&parent_path, ie->path);
2447 if (err) {
2448 if (err->code != GOT_ERR_BAD_PATH)
2449 goto done;
2450 parent_path = strdup("/");
2451 if (parent_path == NULL) {
2452 err = got_error_from_errno("strdup");
2453 goto done;
2456 if (got_path_is_root_dir(worktree->path_prefix)) {
2457 tree_path = strdup(parent_path);
2458 if (tree_path == NULL) {
2459 err = got_error_from_errno("strdup");
2460 goto done;
2462 } else {
2463 if (got_path_is_root_dir(parent_path)) {
2464 tree_path = strdup(worktree->path_prefix);
2465 if (tree_path == NULL) {
2466 err = got_error_from_errno("strdup");
2467 goto done;
2469 } else {
2470 if (asprintf(&tree_path, "%s/%s",
2471 worktree->path_prefix, parent_path) == -1) {
2472 err = got_error_from_errno("asprintf");
2473 goto done;
2478 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2479 tree_path);
2480 if (err)
2481 goto done;
2483 err = got_object_open_as_tree(&tree, repo, tree_id);
2484 if (err)
2485 goto done;
2487 te_name = basename(ie->path);
2488 if (te_name == NULL) {
2489 err = got_error_from_errno2("basename", ie->path);
2490 goto done;
2493 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2494 if (err)
2495 goto done;
2497 te = got_object_tree_find_entry(tree, te_name);
2498 if (te == NULL && status != GOT_STATUS_ADD) {
2499 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2500 goto done;
2503 switch (status) {
2504 case GOT_STATUS_ADD:
2505 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2506 got_fileindex_entry_remove(fileindex, ie);
2507 break;
2508 case GOT_STATUS_DELETE:
2509 case GOT_STATUS_MODIFY:
2510 case GOT_STATUS_CONFLICT:
2511 case GOT_STATUS_MISSING: {
2512 struct got_object_id id;
2513 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2514 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2515 if (err)
2516 goto done;
2517 err = install_blob(worktree, ondisk_path, ie->path,
2518 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2519 progress_arg);
2520 if (err)
2521 goto done;
2522 if (status == GOT_STATUS_DELETE) {
2523 err = update_blob_fileindex_entry(worktree,
2524 fileindex, ie, ondisk_path, ie->path, blob, 1);
2525 if (err)
2526 goto done;
2528 break;
2530 default:
2531 goto done;
2533 done:
2534 free(relpath);
2535 free(parent_path);
2536 free(tree_path);
2537 if (blob)
2538 got_object_blob_close(blob);
2539 if (tree)
2540 got_object_tree_close(tree);
2541 free(tree_id);
2542 return err;
2545 const struct got_error *
2546 got_worktree_revert(struct got_worktree *worktree,
2547 struct got_pathlist_head *ondisk_paths,
2548 got_worktree_checkout_cb progress_cb, void *progress_arg,
2549 struct got_repository *repo)
2551 struct got_fileindex *fileindex = NULL;
2552 char *fileindex_path = NULL;
2553 FILE *index = NULL;
2554 const struct got_error *err = NULL, *unlockerr = NULL;
2555 const struct got_error *sync_err = NULL;
2556 struct got_pathlist_entry *pe;
2558 err = lock_worktree(worktree, LOCK_EX);
2559 if (err)
2560 return err;
2562 fileindex = got_fileindex_alloc();
2563 if (fileindex == NULL) {
2564 err = got_error_from_errno("got_fileindex_alloc");
2565 goto done;
2568 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2569 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2570 err = got_error_from_errno("asprintf");
2571 fileindex_path = NULL;
2572 goto done;
2575 index = fopen(fileindex_path, "rb");
2576 if (index == NULL) {
2577 err = got_error_from_errno2("fopen", fileindex_path);
2578 goto done;
2581 err = got_fileindex_read(fileindex, index);
2582 if (err)
2583 goto done;
2585 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2586 err = revert_file(worktree, fileindex, pe->path,
2587 progress_cb, progress_arg, repo);
2588 if (err)
2589 break;
2591 sync_err = sync_fileindex(fileindex, fileindex_path);
2592 if (sync_err && err == NULL)
2593 err = sync_err;
2594 done:
2595 if (index) {
2596 if (fclose(index) != 0 && err == NULL)
2597 err = got_error_from_errno("fclose");
2599 if (fileindex)
2600 got_fileindex_free(fileindex);
2601 unlockerr = lock_worktree(worktree, LOCK_SH);
2602 if (unlockerr && err == NULL)
2603 err = unlockerr;
2604 return err;
2607 static void
2608 free_commitable(struct got_commitable *ct)
2610 free(ct->path);
2611 free(ct->in_repo_path);
2612 free(ct->ondisk_path);
2613 free(ct->blob_id);
2614 free(ct->base_blob_id);
2615 free(ct->base_commit_id);
2616 free(ct);
2619 struct collect_commitables_arg {
2620 struct got_pathlist_head *commitable_paths;
2621 struct got_repository *repo;
2622 struct got_worktree *worktree;
2625 static const struct got_error *
2626 collect_commitables(void *arg, unsigned char status, const char *relpath,
2627 struct got_object_id *blob_id, struct got_object_id *commit_id)
2629 struct collect_commitables_arg *a = arg;
2630 const struct got_error *err = NULL;
2631 struct got_commitable *ct = NULL;
2632 struct got_pathlist_entry *new = NULL;
2633 char *parent_path = NULL, *path = NULL;
2634 struct stat sb;
2636 if (status == GOT_STATUS_CONFLICT)
2637 return got_error(GOT_ERR_COMMIT_CONFLICT);
2639 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2640 status != GOT_STATUS_DELETE)
2641 return NULL;
2643 if (asprintf(&path, "/%s", relpath) == -1) {
2644 err = got_error_from_errno("asprintf");
2645 goto done;
2647 if (strcmp(path, "/") == 0) {
2648 parent_path = strdup("");
2649 if (parent_path == NULL)
2650 return got_error_from_errno("strdup");
2651 } else {
2652 err = got_path_dirname(&parent_path, path);
2653 if (err)
2654 return err;
2657 ct = calloc(1, sizeof(*ct));
2658 if (ct == NULL) {
2659 err = got_error_from_errno("calloc");
2660 goto done;
2663 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2664 relpath) == -1) {
2665 err = got_error_from_errno("asprintf");
2666 goto done;
2668 if (status == GOT_STATUS_DELETE) {
2669 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2670 } else {
2671 if (lstat(ct->ondisk_path, &sb) != 0) {
2672 err = got_error_from_errno2("lstat", ct->ondisk_path);
2673 goto done;
2675 ct->mode = sb.st_mode;
2678 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2679 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2680 relpath) == -1) {
2681 err = got_error_from_errno("asprintf");
2682 goto done;
2685 ct->status = status;
2686 ct->blob_id = NULL; /* will be filled in when blob gets created */
2687 if (ct->status != GOT_STATUS_ADD) {
2688 ct->base_blob_id = got_object_id_dup(blob_id);
2689 if (ct->base_blob_id == NULL) {
2690 err = got_error_from_errno("got_object_id_dup");
2691 goto done;
2693 ct->base_commit_id = got_object_id_dup(commit_id);
2694 if (ct->base_commit_id == NULL) {
2695 err = got_error_from_errno("got_object_id_dup");
2696 goto done;
2699 ct->path = strdup(path);
2700 if (ct->path == NULL) {
2701 err = got_error_from_errno("strdup");
2702 goto done;
2704 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2705 done:
2706 if (ct && (err || new == NULL))
2707 free_commitable(ct);
2708 free(parent_path);
2709 free(path);
2710 return err;
2713 static const struct got_error *write_tree(struct got_object_id **,
2714 struct got_tree_object *, const char *, struct got_pathlist_head *,
2715 got_worktree_status_cb status_cb, void *status_arg,
2716 struct got_repository *);
2718 static const struct got_error *
2719 write_subtree(struct got_object_id **new_subtree_id,
2720 struct got_tree_entry *te, const char *parent_path,
2721 struct got_pathlist_head *commitable_paths,
2722 got_worktree_status_cb status_cb, void *status_arg,
2723 struct got_repository *repo)
2725 const struct got_error *err = NULL;
2726 struct got_tree_object *subtree;
2727 char *subpath;
2729 if (asprintf(&subpath, "%s%s%s", parent_path,
2730 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2731 return got_error_from_errno("asprintf");
2733 err = got_object_open_as_tree(&subtree, repo, te->id);
2734 if (err)
2735 return err;
2737 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2738 status_cb, status_arg, repo);
2739 got_object_tree_close(subtree);
2740 free(subpath);
2741 return err;
2744 static const struct got_error *
2745 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2747 const struct got_error *err = NULL;
2748 char *ct_parent_path = NULL;
2750 *match = 0;
2752 if (strchr(ct->path, '/') == NULL) {
2753 *match = got_path_is_root_dir(path);
2754 return NULL;
2757 err = got_path_dirname(&ct_parent_path, ct->path);
2758 if (err)
2759 return err;
2760 *match = (strcmp(path, ct_parent_path) == 0);
2761 free(ct_parent_path);
2762 return err;
2765 static mode_t
2766 get_ct_file_mode(struct got_commitable *ct)
2768 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2771 static const struct got_error *
2772 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2773 struct got_tree_entry *te, struct got_commitable *ct)
2775 const struct got_error *err = NULL;
2777 *new_te = NULL;
2779 err = got_object_tree_entry_dup(new_te, te);
2780 if (err)
2781 goto done;
2783 (*new_te)->mode = get_ct_file_mode(ct);
2785 free((*new_te)->id);
2786 (*new_te)->id = got_object_id_dup(ct->blob_id);
2787 if ((*new_te)->id == NULL) {
2788 err = got_error_from_errno("got_object_id_dup");
2789 goto done;
2791 done:
2792 if (err && *new_te) {
2793 got_object_tree_entry_close(*new_te);
2794 *new_te = NULL;
2796 return err;
2799 static const struct got_error *
2800 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2801 struct got_commitable *ct)
2803 const struct got_error *err = NULL;
2804 char *ct_name;
2806 *new_te = NULL;
2808 *new_te = calloc(1, sizeof(**new_te));
2809 if (*new_te == NULL)
2810 return got_error_from_errno("calloc");
2812 ct_name = basename(ct->path);
2813 if (ct_name == NULL) {
2814 err = got_error_from_errno2("basename", ct->path);
2815 goto done;
2817 (*new_te)->name = strdup(ct_name);
2818 if ((*new_te)->name == NULL) {
2819 err = got_error_from_errno("strdup");
2820 goto done;
2823 (*new_te)->mode = get_ct_file_mode(ct);
2825 (*new_te)->id = got_object_id_dup(ct->blob_id);
2826 if ((*new_te)->id == NULL) {
2827 err = got_error_from_errno("got_object_id_dup");
2828 goto done;
2830 done:
2831 if (err && *new_te) {
2832 got_object_tree_entry_close(*new_te);
2833 *new_te = NULL;
2835 return err;
2838 static const struct got_error *
2839 insert_tree_entry(struct got_tree_entry *new_te,
2840 struct got_pathlist_head *paths)
2842 const struct got_error *err = NULL;
2843 struct got_pathlist_entry *new_pe;
2845 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2846 if (err)
2847 return err;
2848 if (new_pe == NULL)
2849 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2850 return NULL;
2853 static const struct got_error *
2854 report_ct_status(struct got_commitable *ct,
2855 got_worktree_status_cb status_cb, void *status_arg)
2857 const char *ct_path = ct->path;
2858 while (ct_path[0] == '/')
2859 ct_path++;
2860 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2863 static const struct got_error *
2864 match_modified_subtree(int *modified, struct got_tree_entry *te,
2865 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2867 const struct got_error *err = NULL;
2868 struct got_pathlist_entry *pe;
2869 char *te_path;
2871 *modified = 0;
2873 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2874 got_path_is_root_dir(base_tree_path) ? "" : "/",
2875 te->name) == -1)
2876 return got_error_from_errno("asprintf");
2878 TAILQ_FOREACH(pe, commitable_paths, entry) {
2879 struct got_commitable *ct = pe->data;
2880 *modified = got_path_is_child(ct->in_repo_path, te_path,
2881 strlen(te_path));
2882 if (*modified)
2883 break;
2886 free(te_path);
2887 return err;
2890 static const struct got_error *
2891 match_deleted_or_modified_ct(struct got_commitable **ctp,
2892 struct got_tree_entry *te, const char *base_tree_path,
2893 struct got_pathlist_head *commitable_paths)
2895 const struct got_error *err = NULL;
2896 struct got_pathlist_entry *pe;
2898 *ctp = NULL;
2900 TAILQ_FOREACH(pe, commitable_paths, entry) {
2901 struct got_commitable *ct = pe->data;
2902 char *ct_name = NULL;
2903 int path_matches;
2905 if (ct->status != GOT_STATUS_MODIFY &&
2906 ct->status != GOT_STATUS_DELETE)
2907 continue;
2909 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2910 continue;
2912 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2913 if (err)
2914 return err;
2915 if (!path_matches)
2916 continue;
2918 ct_name = basename(pe->path);
2919 if (ct_name == NULL)
2920 return got_error_from_errno2("basename", pe->path);
2922 if (strcmp(te->name, ct_name) != 0)
2923 continue;
2925 *ctp = ct;
2926 break;
2929 return err;
2932 static const struct got_error *
2933 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2934 const char *child_path, const char *path_base_tree,
2935 struct got_pathlist_head *commitable_paths,
2936 got_worktree_status_cb status_cb, void *status_arg,
2937 struct got_repository *repo)
2939 const struct got_error *err = NULL;
2940 struct got_tree_entry *new_te;
2941 char *subtree_path;
2943 *new_tep = NULL;
2945 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2946 got_path_is_root_dir(path_base_tree) ? "" : "/",
2947 child_path) == -1)
2948 return got_error_from_errno("asprintf");
2950 new_te = calloc(1, sizeof(*new_te));
2951 new_te->mode = S_IFDIR;
2952 new_te->name = strdup(child_path);
2953 if (new_te->name == NULL) {
2954 err = got_error_from_errno("strdup");
2955 got_object_tree_entry_close(new_te);
2956 goto done;
2958 err = write_tree(&new_te->id, NULL, subtree_path,
2959 commitable_paths, status_cb, status_arg, repo);
2960 if (err) {
2961 got_object_tree_entry_close(new_te);
2962 goto done;
2964 done:
2965 free(subtree_path);
2966 if (err == NULL)
2967 *new_tep = new_te;
2968 return err;
2971 static const struct got_error *
2972 write_tree(struct got_object_id **new_tree_id,
2973 struct got_tree_object *base_tree, const char *path_base_tree,
2974 struct got_pathlist_head *commitable_paths,
2975 got_worktree_status_cb status_cb, void *status_arg,
2976 struct got_repository *repo)
2978 const struct got_error *err = NULL;
2979 const struct got_tree_entries *base_entries = NULL;
2980 struct got_pathlist_head paths;
2981 struct got_tree_entries new_tree_entries;
2982 struct got_tree_entry *te, *new_te = NULL;
2983 struct got_pathlist_entry *pe;
2985 TAILQ_INIT(&paths);
2986 new_tree_entries.nentries = 0;
2987 SIMPLEQ_INIT(&new_tree_entries.head);
2989 /* Insert, and recurse into, newly added entries first. */
2990 TAILQ_FOREACH(pe, commitable_paths, entry) {
2991 struct got_commitable *ct = pe->data;
2992 char *child_path = NULL, *slash;
2994 if (ct->status != GOT_STATUS_ADD ||
2995 (ct->flags & GOT_COMMITABLE_ADDED))
2996 continue;
2998 if (!got_path_is_child(pe->path, path_base_tree,
2999 strlen(path_base_tree)))
3000 continue;
3002 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3003 pe->path);
3004 if (err)
3005 goto done;
3007 slash = strchr(child_path, '/');
3008 if (slash == NULL) {
3009 err = alloc_added_blob_tree_entry(&new_te, ct);
3010 if (err)
3011 goto done;
3012 err = report_ct_status(ct, status_cb, status_arg);
3013 if (err)
3014 goto done;
3015 ct->flags |= GOT_COMMITABLE_ADDED;
3016 err = insert_tree_entry(new_te, &paths);
3017 if (err)
3018 goto done;
3019 } else {
3020 *slash = '\0'; /* trim trailing path components */
3021 if (base_tree == NULL ||
3022 got_object_tree_find_entry(base_tree, child_path)
3023 == NULL) {
3024 err = make_subtree_for_added_blob(&new_te,
3025 child_path, path_base_tree,
3026 commitable_paths, status_cb, status_arg,
3027 repo);
3028 if (err)
3029 goto done;
3030 err = insert_tree_entry(new_te, &paths);
3031 if (err)
3032 goto done;
3037 if (base_tree) {
3038 /* Handle modified and deleted entries. */
3039 base_entries = got_object_tree_get_entries(base_tree);
3040 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3041 struct got_commitable *ct = NULL;
3043 if (S_ISDIR(te->mode)) {
3044 int modified;
3045 err = got_object_tree_entry_dup(&new_te, te);
3046 if (err)
3047 goto done;
3048 err = match_modified_subtree(&modified, te,
3049 path_base_tree, commitable_paths);
3050 if (err)
3051 goto done;
3052 /* Avoid recursion into unmodified subtrees. */
3053 if (modified) {
3054 free(new_te->id);
3055 err = write_subtree(&new_te->id, te,
3056 path_base_tree, commitable_paths,
3057 status_cb, status_arg, repo);
3058 if (err)
3059 goto done;
3061 err = insert_tree_entry(new_te, &paths);
3062 if (err)
3063 goto done;
3064 continue;
3067 err = match_deleted_or_modified_ct(&ct, te,
3068 path_base_tree, commitable_paths);
3069 if (ct) {
3070 /* NB: Deleted entries get dropped here. */
3071 if (ct->status == GOT_STATUS_MODIFY) {
3072 err = alloc_modified_blob_tree_entry(
3073 &new_te, te, ct);
3074 if (err)
3075 goto done;
3076 err = insert_tree_entry(new_te, &paths);
3077 if (err)
3078 goto done;
3080 err = report_ct_status(ct, status_cb,
3081 status_arg);
3082 if (err)
3083 goto done;
3084 } else {
3085 /* Entry is unchanged; just copy it. */
3086 err = got_object_tree_entry_dup(&new_te, te);
3087 if (err)
3088 goto done;
3089 err = insert_tree_entry(new_te, &paths);
3090 if (err)
3091 goto done;
3096 /* Write new list of entries; deleted entries have been dropped. */
3097 TAILQ_FOREACH(pe, &paths, entry) {
3098 struct got_tree_entry *te = pe->data;
3099 new_tree_entries.nentries++;
3100 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3102 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3103 done:
3104 got_object_tree_entries_close(&new_tree_entries);
3105 got_pathlist_free(&paths);
3106 return err;
3109 static const struct got_error *
3110 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3111 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3113 const struct got_error *err = NULL, *sync_err;
3114 char *fileindex_path = NULL;
3115 struct got_fileindex *fileindex = NULL;
3116 struct got_pathlist_entry *pe;
3118 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3119 if (err)
3120 return err;
3122 TAILQ_FOREACH(pe, commitable_paths, entry) {
3123 struct got_fileindex_entry *ie;
3124 struct got_commitable *ct = pe->data;
3126 ie = got_fileindex_entry_get(fileindex, pe->path);
3127 if (ie) {
3128 if (ct->status == GOT_STATUS_DELETE) {
3129 got_fileindex_entry_remove(fileindex, ie);
3130 got_fileindex_entry_free(ie);
3131 } else
3132 err = got_fileindex_entry_update(ie,
3133 ct->ondisk_path, ct->blob_id->sha1,
3134 new_base_commit_id->sha1, 1);
3135 } else {
3136 err = got_fileindex_entry_alloc(&ie,
3137 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3138 new_base_commit_id->sha1);
3139 if (err)
3140 break;
3141 err = got_fileindex_entry_add(fileindex, ie);
3142 if (err)
3143 break;
3146 sync_err = sync_fileindex(fileindex, fileindex_path);
3147 if (sync_err && err == NULL)
3148 err = sync_err;
3149 free(fileindex_path);
3150 got_fileindex_free(fileindex);
3151 return err;
3154 static const struct got_error *
3155 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3156 struct got_object_id *head_commit_id)
3158 const struct got_error *err = NULL;
3159 struct got_object_id *id_in_head = NULL, *id = NULL;
3160 struct got_commit_object *commit = NULL;
3161 char *path = NULL;
3162 const char *ct_path = ct->in_repo_path;
3164 while (ct_path[0] == '/')
3165 ct_path++;
3168 * Ensure that no modifications were made to files *and their parents*
3169 * in commits between the file's base commit and the branch head.
3171 * Checking the parents is important for detecting conflicting tree
3172 * configurations (files or parent folders might have been moved,
3173 * deleted, added again, etc.). Such changes need to be merged with
3174 * local changes before a commit can occur.
3176 * The implication is that the file's (parent) entry in the root
3177 * directory must have the same ID in all relevant commits.
3179 if (ct->status != GOT_STATUS_ADD) {
3180 struct got_object_qid *pid;
3181 char *slash;
3182 struct got_object_id *root_entry_id = NULL;
3184 /* Trivial case: base commit == head commit */
3185 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3186 return NULL;
3188 /* Compute the path to the root directory's entry. */
3189 path = strdup(ct_path);
3190 if (path == NULL) {
3191 err = got_error_from_errno("strdup");
3192 goto done;
3194 slash = strchr(path, '/');
3195 if (slash)
3196 *slash = '\0';
3198 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3199 if (err)
3200 goto done;
3202 err = got_object_id_by_path(&root_entry_id, repo,
3203 head_commit_id, path);
3204 if (err)
3205 goto done;
3207 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3208 while (pid) {
3209 struct got_commit_object *pcommit;
3211 err = got_object_id_by_path(&id, repo, pid->id, path);
3212 if (err) {
3213 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3214 goto done;
3215 err = NULL;
3216 break;
3219 err = got_object_id_by_path(&id, repo, pid->id, path);
3220 if (err)
3221 goto done;
3223 if (got_object_id_cmp(id, root_entry_id) != 0) {
3224 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3225 break;
3228 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3229 break; /* all relevant commits scanned */
3231 err = got_object_open_as_commit(&pcommit, repo,
3232 pid->id);
3233 if (err)
3234 goto done;
3236 got_object_commit_close(commit);
3237 commit = pcommit;
3238 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3239 commit));
3241 } else {
3242 /* Require that added files don't exist in the branch head. */
3243 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3244 ct_path);
3245 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3246 goto done;
3247 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3249 done:
3250 if (commit)
3251 got_object_commit_close(commit);
3252 free(id_in_head);
3253 free(id);
3254 free(path);
3255 return err;
3258 const struct got_error *
3259 got_worktree_commit(struct got_object_id **new_commit_id,
3260 struct got_worktree *worktree, const char *ondisk_path,
3261 const char *author, const char *committer,
3262 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3263 got_worktree_status_cb status_cb, void *status_arg,
3264 struct got_repository *repo)
3266 const struct got_error *err = NULL, *unlockerr = NULL;
3267 struct collect_commitables_arg cc_arg;
3268 struct got_pathlist_head commitable_paths;
3269 struct got_pathlist_entry *pe;
3270 char *relpath = NULL;
3271 const char *head_ref_name = NULL;
3272 struct got_reference *head_ref = NULL;
3273 struct got_commit_object *head_commit = NULL;
3274 struct got_object_id *head_commit_id = NULL;
3275 struct got_reference *head_ref2 = NULL;
3276 struct got_object_id *head_commit_id2 = NULL;
3277 struct got_tree_object *head_tree = NULL;
3278 struct got_object_id *new_tree_id = NULL;
3279 struct got_object_id_queue parent_ids;
3280 struct got_object_qid *pid = NULL;
3281 char *logmsg = NULL;
3283 *new_commit_id = NULL;
3285 TAILQ_INIT(&commitable_paths);
3286 SIMPLEQ_INIT(&parent_ids);
3288 if (ondisk_path) {
3289 err = got_path_skip_common_ancestor(&relpath,
3290 worktree->root_path, ondisk_path);
3291 if (err)
3292 return err;
3295 err = lock_worktree(worktree, LOCK_EX);
3296 if (err)
3297 goto done;
3299 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3300 if (err)
3301 goto done;
3302 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3303 if (err)
3304 goto done;
3306 cc_arg.commitable_paths = &commitable_paths;
3307 cc_arg.worktree = worktree;
3308 cc_arg.repo = repo;
3309 err = got_worktree_status(worktree, relpath ? relpath : "",
3310 repo, collect_commitables, &cc_arg, NULL, NULL);
3311 if (err)
3312 goto done;
3314 if (TAILQ_EMPTY(&commitable_paths)) {
3315 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3316 goto done;
3319 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3320 if (err)
3321 goto done;
3323 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3324 struct got_commitable *ct = pe->data;
3325 err = check_ct_out_of_date(ct, repo, head_commit_id);
3326 if (err)
3327 goto done;
3330 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3331 if (err)
3332 goto done;
3334 if (commit_msg_cb != NULL) {
3335 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3336 if (err)
3337 goto done;
3340 if (logmsg == NULL || strlen(logmsg) == 0) {
3341 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3342 goto done;
3345 /* Create blobs from added and modified files and record their IDs. */
3346 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3347 struct got_commitable *ct = pe->data;
3348 char *ondisk_path;
3350 if (ct->status != GOT_STATUS_ADD &&
3351 ct->status != GOT_STATUS_MODIFY)
3352 continue;
3354 if (asprintf(&ondisk_path, "%s/%s",
3355 worktree->root_path, pe->path) == -1) {
3356 err = got_error_from_errno("asprintf");
3357 goto done;
3359 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3360 free(ondisk_path);
3361 if (err)
3362 goto done;
3365 /* Recursively write new tree objects. */
3366 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3367 status_cb, status_arg, repo);
3368 if (err)
3369 goto done;
3371 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3372 if (err)
3373 goto done;
3374 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3375 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3376 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3377 got_object_qid_free(pid);
3378 if (logmsg != NULL)
3379 free(logmsg);
3380 if (err)
3381 goto done;
3383 /* Check if a concurrent commit to our branch has occurred. */
3384 head_ref_name = got_worktree_get_head_ref_name(worktree);
3385 if (head_ref_name == NULL) {
3386 err = got_error_from_errno("got_worktree_get_head_ref_name");
3387 goto done;
3389 /* Lock the reference here to prevent concurrent modification. */
3390 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3391 if (err)
3392 goto done;
3393 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3394 if (err)
3395 goto done;
3396 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3397 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3398 goto done;
3400 /* Update branch head in repository. */
3401 err = got_ref_change_ref(head_ref2, *new_commit_id);
3402 if (err)
3403 goto done;
3404 err = got_ref_write(head_ref2, repo);
3405 if (err)
3406 goto done;
3408 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3409 if (err)
3410 goto done;
3412 err = ref_base_commit(worktree, repo);
3413 if (err)
3414 goto done;
3416 err = update_fileindex_after_commit(&commitable_paths,
3417 *new_commit_id, worktree);
3418 if (err)
3419 goto done;
3420 done:
3421 unlockerr = lock_worktree(worktree, LOCK_SH);
3422 if (unlockerr && err == NULL)
3423 err = unlockerr;
3424 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3425 struct got_commitable *ct = pe->data;
3426 free_commitable(ct);
3428 got_pathlist_free(&commitable_paths);
3429 if (head_tree)
3430 got_object_tree_close(head_tree);
3431 if (head_commit)
3432 got_object_commit_close(head_commit);
3433 free(relpath);
3434 free(head_commit_id);
3435 free(head_commit_id2);
3436 if (head_ref)
3437 got_ref_close(head_ref);
3438 if (head_ref2) {
3439 unlockerr = got_ref_unlock(head_ref2);
3440 if (unlockerr && err == NULL)
3441 err = unlockerr;
3442 got_ref_close(head_ref2);
3444 return err;
3447 const char *
3448 got_commitable_get_path(struct got_commitable *ct)
3450 return ct->path;
3453 unsigned int
3454 got_commitable_get_status(struct got_commitable *ct)
3456 return ct->status;
3459 struct check_rebase_ok_arg {
3460 struct got_worktree *worktree;
3461 struct got_repository *repo;
3462 int rebase_in_progress;
3465 static const struct got_error *
3466 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3468 const struct got_error *err = NULL;
3469 struct check_rebase_ok_arg *a = arg;
3470 unsigned char status;
3471 struct stat sb;
3472 char *ondisk_path;
3474 if (!a->rebase_in_progress) {
3475 /* Reject rebase of a work tree with mixed base commits. */
3476 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3477 SHA1_DIGEST_LENGTH))
3478 return got_error(GOT_ERR_MIXED_COMMITS);
3481 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3482 == -1)
3483 return got_error_from_errno("asprintf");
3485 /* Reject rebase of a work tree with modified or conflicted files. */
3486 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3487 free(ondisk_path);
3488 if (err)
3489 return err;
3491 if (a->rebase_in_progress) {
3492 if (status == GOT_STATUS_CONFLICT)
3493 return got_error(GOT_ERR_CONFLICTS);
3494 } else if (status != GOT_STATUS_NO_CHANGE)
3495 return got_error(GOT_ERR_MODIFIED);
3497 return NULL;
3500 const struct got_error *
3501 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3502 struct got_reference **tmp_branch, struct got_worktree *worktree,
3503 struct got_reference *branch, struct got_repository *repo)
3505 const struct got_error *err = NULL;
3506 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3507 char *branch_ref_name = NULL;
3508 struct got_fileindex *fileindex = NULL;
3509 char *fileindex_path = NULL;
3510 struct check_rebase_ok_arg ok_arg;
3511 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3513 *new_base_branch_ref = NULL;
3514 *tmp_branch = NULL;
3516 err = lock_worktree(worktree, LOCK_EX);
3517 if (err)
3518 return err;
3520 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3521 if (err)
3522 goto done;
3524 ok_arg.worktree = worktree;
3525 ok_arg.repo = repo;
3526 ok_arg.rebase_in_progress = 0;
3527 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3528 &ok_arg);
3529 if (err)
3530 goto done;
3532 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3533 if (err)
3534 goto done;
3536 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3537 if (err)
3538 goto done;
3540 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3541 if (err)
3542 goto done;
3544 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3545 0);
3546 if (err)
3547 goto done;
3549 err = got_ref_alloc_symref(new_base_branch_ref,
3550 new_base_branch_ref_name, wt_branch);
3551 if (err)
3552 goto done;
3553 err = got_ref_write(*new_base_branch_ref, repo);
3554 if (err)
3555 goto done;
3557 /* TODO Lock original branch's ref while rebasing? */
3559 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3560 if (err)
3561 goto done;
3563 err = got_ref_write(branch_ref, repo);
3564 if (err)
3565 goto done;
3567 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3568 worktree->base_commit_id);
3569 if (err)
3570 goto done;
3571 err = got_ref_write(*tmp_branch, repo);
3572 if (err)
3573 goto done;
3575 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3576 if (err)
3577 goto done;
3578 done:
3579 free(fileindex_path);
3580 if (fileindex)
3581 got_fileindex_free(fileindex);
3582 free(tmp_branch_name);
3583 free(new_base_branch_ref_name);
3584 free(branch_ref_name);
3585 if (branch_ref)
3586 got_ref_close(branch_ref);
3587 if (wt_branch)
3588 got_ref_close(wt_branch);
3589 if (err) {
3590 if (*new_base_branch_ref) {
3591 got_ref_close(*new_base_branch_ref);
3592 *new_base_branch_ref = NULL;
3594 if (*tmp_branch) {
3595 got_ref_close(*tmp_branch);
3596 *tmp_branch = NULL;
3598 lock_worktree(worktree, LOCK_SH);
3600 return err;
3603 const struct got_error *
3604 got_worktree_rebase_continue(struct got_object_id **commit_id,
3605 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3606 struct got_reference **branch, struct got_worktree *worktree,
3607 struct got_repository *repo)
3609 const struct got_error *err;
3610 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3611 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3612 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3614 *commit_id = NULL;
3616 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3617 if (err)
3618 return err;
3620 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3621 if (err)
3622 goto done;
3624 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3625 if (err)
3626 goto done;
3628 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3629 if (err)
3630 goto done;
3632 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3633 if (err)
3634 goto done;
3636 err = got_ref_open(branch, repo,
3637 got_ref_get_symref_target(branch_ref), 0);
3638 if (err)
3639 goto done;
3641 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3642 if (err)
3643 goto done;
3645 err = got_ref_resolve(commit_id, repo, commit_ref);
3646 if (err)
3647 goto done;
3649 err = got_ref_open(new_base_branch, repo,
3650 new_base_branch_ref_name, 0);
3651 if (err)
3652 goto done;
3654 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3655 if (err)
3656 goto done;
3657 done:
3658 free(commit_ref_name);
3659 free(branch_ref_name);
3660 if (commit_ref)
3661 got_ref_close(commit_ref);
3662 if (branch_ref)
3663 got_ref_close(branch_ref);
3664 if (err) {
3665 free(*commit_id);
3666 *commit_id = NULL;
3667 if (*tmp_branch) {
3668 got_ref_close(*tmp_branch);
3669 *tmp_branch = NULL;
3671 if (*new_base_branch) {
3672 got_ref_close(*new_base_branch);
3673 *new_base_branch = NULL;
3675 if (*branch) {
3676 got_ref_close(*branch);
3677 *branch = NULL;
3680 return err;
3683 const struct got_error *
3684 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3686 const struct got_error *err;
3687 char *tmp_branch_name = NULL;
3689 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3690 if (err)
3691 return err;
3693 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3694 free(tmp_branch_name);
3695 return NULL;
3698 static const struct got_error *
3699 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3700 char **logmsg, void *arg)
3702 struct got_commit_object *commit = arg;
3704 *logmsg = strdup(got_object_commit_get_logmsg(commit));
3705 if (*logmsg == NULL)
3706 return got_error_from_errno("strdup");
3708 return NULL;
3711 static const struct got_error *
3712 rebase_status(void *arg, unsigned char status, const char *path,
3713 struct got_object_id *blob_id, struct got_object_id *commit_id)
3715 return NULL;
3718 const struct got_error *
3719 got_worktree_rebase_merge_files(struct got_worktree *worktree,
3720 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3721 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3722 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3724 const struct got_error *err;
3725 struct got_fileindex *fileindex;
3726 char *fileindex_path, *commit_ref_name = NULL;
3727 struct got_reference *commit_ref = NULL;
3729 /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3731 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3732 if (err)
3733 return err;
3735 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3736 if (err)
3737 goto done;
3738 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3739 if (err) {
3740 if (err->code != GOT_ERR_NOT_REF)
3741 goto done;
3742 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3743 if (err)
3744 goto done;
3745 err = got_ref_write(commit_ref, repo);
3746 if (err)
3747 goto done;
3748 } else {
3749 struct got_object_id *stored_id;
3750 int cmp;
3752 err = got_ref_resolve(&stored_id, repo, commit_ref);
3753 if (err)
3754 goto done;
3755 cmp = got_object_id_cmp(commit_id, stored_id);
3756 free(stored_id);
3757 if (cmp != 0) {
3758 err = got_error(GOT_ERR_REBASE_COMMITID);
3759 goto done;
3763 err = merge_files(worktree, fileindex, fileindex_path,
3764 parent_commit_id, commit_id, repo, progress_cb, progress_arg,
3765 cancel_cb, cancel_arg);
3766 done:
3767 got_fileindex_free(fileindex);
3768 free(fileindex_path);
3769 if (commit_ref)
3770 got_ref_close(commit_ref);
3771 return err;
3774 const struct got_error *
3775 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3776 struct got_worktree *worktree, struct got_reference *tmp_branch,
3777 struct got_commit_object *orig_commit,
3778 struct got_object_id *orig_commit_id, struct got_repository *repo)
3780 const struct got_error *err;
3781 char *commit_ref_name = NULL;
3782 struct got_reference *commit_ref = NULL;
3783 struct got_object_id *commit_id = NULL;
3785 /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3787 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3788 if (err)
3789 goto done;
3790 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3791 if (err)
3792 goto done;
3793 err = got_ref_resolve(&commit_id, repo, commit_ref);
3794 if (err)
3795 goto done;
3796 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3797 err = got_error(GOT_ERR_REBASE_COMMITID);
3798 goto done;
3801 err = got_worktree_commit(new_commit_id, worktree, NULL,
3802 got_object_commit_get_author(orig_commit),
3803 got_object_commit_get_committer(orig_commit),
3804 collect_rebase_commit_msg, orig_commit,
3805 rebase_status, NULL, repo);
3806 if (err)
3807 goto done;
3809 err = got_ref_delete(commit_ref, repo);
3810 if (err)
3811 goto done;
3813 err = got_ref_change_ref(tmp_branch, *new_commit_id);
3814 done:
3815 free(commit_ref_name);
3816 if (commit_ref)
3817 got_ref_close(commit_ref);
3818 if (err) {
3819 free(*new_commit_id);
3820 *new_commit_id = NULL;
3822 return err;
3825 const struct got_error *
3826 got_worktree_rebase_postpone(struct got_worktree *worktree)
3828 return lock_worktree(worktree, LOCK_SH);
3831 const struct got_error *
3832 got_worktree_rebase_complete(struct got_worktree *worktree,
3833 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3834 struct got_reference *rebased_branch,
3835 struct got_repository *repo)
3837 const struct got_error *err, *unlockerr;
3838 struct got_object_id *new_head_commit_id = NULL;
3840 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
3841 if (err)
3842 return err;
3844 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
3845 if (err)
3846 goto done;
3848 err = got_ref_write(rebased_branch, repo);
3849 if (err)
3850 goto done;
3852 err = got_worktree_set_head_ref(worktree, rebased_branch);
3853 if (err)
3854 goto done;
3856 err = got_ref_delete(tmp_branch, repo);
3857 if (err)
3858 goto done;
3860 err = got_ref_delete(new_base_branch, repo);
3861 if (err)
3862 goto done;
3863 done:
3864 free(new_head_commit_id);
3865 unlockerr = lock_worktree(worktree, LOCK_SH);
3866 if (unlockerr && err == NULL)
3867 err = unlockerr;
3868 return err;
3871 struct collect_revertible_paths_arg {
3872 struct got_pathlist_head *revertible_paths;
3873 struct got_worktree *worktree;
3876 static const struct got_error *
3877 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
3878 struct got_object_id *blob_id, struct got_object_id *commit_id)
3880 struct collect_revertible_paths_arg *a = arg;
3881 const struct got_error *err = NULL;
3882 struct got_pathlist_entry *new = NULL;
3883 char *path = NULL;
3885 if (status != GOT_STATUS_ADD &&
3886 status != GOT_STATUS_DELETE &&
3887 status != GOT_STATUS_MODIFY &&
3888 status != GOT_STATUS_CONFLICT &&
3889 status != GOT_STATUS_MISSING)
3890 return NULL;
3892 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
3893 return got_error_from_errno("asprintf");
3895 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
3896 if (err || new == NULL)
3897 free(path);
3898 return err;
3901 const struct got_error *
3902 got_worktree_rebase_abort(struct got_worktree *worktree,
3903 struct got_repository *repo, struct got_reference *new_base_branch,
3904 got_worktree_checkout_cb progress_cb, void *progress_arg)
3906 const struct got_error *err, *unlockerr;
3907 struct got_reference *resolved = NULL;
3908 struct got_object_id *commit_id = NULL;
3909 struct got_fileindex *fileindex = NULL;
3910 char *fileindex_path = NULL;
3911 struct got_pathlist_head revertible_paths;
3912 struct got_pathlist_entry *pe;
3913 struct collect_revertible_paths_arg crp_arg;
3915 TAILQ_INIT(&revertible_paths);
3917 err = lock_worktree(worktree, LOCK_EX);
3918 if (err)
3919 return err;
3921 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3922 if (err)
3923 goto done;
3925 err = got_ref_open(&resolved, repo,
3926 got_ref_get_symref_target(new_base_branch), 0);
3927 if (err)
3928 goto done;
3930 err = got_worktree_set_head_ref(worktree, resolved);
3931 if (err)
3932 goto done;
3935 * XXX commits to the base branch could have happened while
3936 * we were busy rebasing; should we store the original commit ID
3937 * when rebase begins and read it back here?
3939 err = got_ref_resolve(&commit_id, repo, resolved);
3940 if (err)
3941 goto done;
3943 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
3944 if (err)
3945 goto done;
3947 err = got_worktree_checkout_files(worktree, "", repo, progress_cb,
3948 progress_arg, NULL, NULL);
3949 if (err)
3950 goto done;
3952 crp_arg.revertible_paths = &revertible_paths;
3953 crp_arg.worktree = worktree;
3954 err = got_worktree_status(worktree, "", repo,
3955 collect_revertible_paths, &crp_arg, NULL, NULL);
3956 if (err)
3957 goto done;
3959 TAILQ_FOREACH(pe, &revertible_paths, entry) {
3960 err = revert_file(worktree, fileindex, pe->path,
3961 progress_cb, progress_arg, repo);
3962 if (err)
3963 break;
3965 done:
3966 got_ref_close(resolved);
3967 free(commit_id);
3968 got_fileindex_free(fileindex);
3969 free(fileindex_path);
3970 TAILQ_FOREACH(pe, &revertible_paths, entry)
3971 free((char *)pe->path);
3972 got_pathlist_free(&revertible_paths);
3974 unlockerr = lock_worktree(worktree, LOCK_SH);
3975 if (unlockerr && err == NULL)
3976 err = unlockerr;
3977 return err;