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 if (a->progress_cb)
1500 (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1501 ie->path);
1502 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1503 return NULL;
1506 static const struct got_error *
1507 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1509 const struct got_error *err = NULL;
1510 char *new_fileindex_path = NULL;
1511 FILE *new_index = NULL;
1513 err = got_opentemp_named(&new_fileindex_path, &new_index,
1514 fileindex_path);
1515 if (err)
1516 goto done;
1518 err = got_fileindex_write(fileindex, new_index);
1519 if (err)
1520 goto done;
1522 if (rename(new_fileindex_path, fileindex_path) != 0) {
1523 err = got_error_from_errno3("rename", new_fileindex_path,
1524 fileindex_path);
1525 unlink(new_fileindex_path);
1527 done:
1528 if (new_index)
1529 fclose(new_index);
1530 free(new_fileindex_path);
1531 return err;
1534 const struct got_error *
1535 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1536 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1537 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1539 const struct got_error *err = NULL, *sync_err, *unlockerr;
1540 struct got_commit_object *commit = NULL;
1541 struct got_object_id *tree_id = NULL;
1542 struct got_tree_object *tree = NULL;
1543 struct got_fileindex *fileindex = NULL;
1544 char *fileindex_path = NULL;
1545 struct got_fileindex_diff_tree_cb diff_cb;
1546 struct diff_cb_arg arg;
1547 char *relpath = NULL, *entry_name = NULL;
1548 struct bump_base_commit_id_arg bbc_arg;
1550 err = lock_worktree(worktree, LOCK_EX);
1551 if (err)
1552 return err;
1555 * Read the file index.
1556 * Checking out files is supposed to be an idempotent operation.
1557 * If the on-disk file index is incomplete we will try to complete it.
1559 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1560 if (err)
1561 goto done;
1563 err = ref_base_commit(worktree, repo);
1564 if (err)
1565 goto done;
1567 err = got_object_open_as_commit(&commit, repo,
1568 worktree->base_commit_id);
1569 if (err)
1570 goto done;
1572 if (path[0]) {
1573 char *tree_path;
1574 int obj_type;
1575 relpath = strdup(path);
1576 if (relpath == NULL) {
1577 err = got_error_from_errno("strdup");
1578 goto done;
1580 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1581 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1582 path) == -1) {
1583 err = got_error_from_errno("asprintf");
1584 goto done;
1586 err = got_object_id_by_path(&tree_id, repo,
1587 worktree->base_commit_id, tree_path);
1588 free(tree_path);
1589 if (err)
1590 goto done;
1591 err = got_object_get_type(&obj_type, repo, tree_id);
1592 if (err)
1593 goto done;
1594 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1595 /* Split provided path into parent dir + entry name. */
1596 if (strchr(path, '/') == NULL) {
1597 relpath = strdup("");
1598 if (relpath == NULL) {
1599 err = got_error_from_errno("strdup");
1600 goto done;
1602 tree_path = strdup(worktree->path_prefix);
1603 if (tree_path == NULL) {
1604 err = got_error_from_errno("strdup");
1605 goto done;
1607 } else {
1608 err = got_path_dirname(&relpath, path);
1609 if (err)
1610 goto done;
1611 if (asprintf(&tree_path, "%s%s%s",
1612 worktree->path_prefix,
1613 got_path_is_root_dir(
1614 worktree->path_prefix) ? "" : "/",
1615 relpath) == -1) {
1616 err = got_error_from_errno("asprintf");
1617 goto done;
1620 err = got_object_id_by_path(&tree_id, repo,
1621 worktree->base_commit_id, tree_path);
1622 free(tree_path);
1623 if (err)
1624 goto done;
1625 entry_name = basename(path);
1626 if (entry_name == NULL) {
1627 err = got_error_from_errno2("basename", path);
1628 goto done;
1631 } else {
1632 relpath = strdup("");
1633 if (relpath == NULL) {
1634 err = got_error_from_errno("strdup");
1635 goto done;
1637 err = got_object_id_by_path(&tree_id, repo,
1638 worktree->base_commit_id, worktree->path_prefix);
1639 if (err)
1640 goto done;
1643 err = got_object_open_as_tree(&tree, repo, tree_id);
1644 if (err)
1645 goto done;
1647 if (entry_name &&
1648 got_object_tree_find_entry(tree, entry_name) == NULL) {
1649 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1650 goto done;
1653 diff_cb.diff_old_new = diff_old_new;
1654 diff_cb.diff_old = diff_old;
1655 diff_cb.diff_new = diff_new;
1656 arg.fileindex = fileindex;
1657 arg.worktree = worktree;
1658 arg.repo = repo;
1659 arg.progress_cb = progress_cb;
1660 arg.progress_arg = progress_arg;
1661 arg.cancel_cb = cancel_cb;
1662 arg.cancel_arg = cancel_arg;
1663 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1664 entry_name, repo, &diff_cb, &arg);
1665 if (err)
1666 goto sync;
1668 bbc_arg.base_commit_id = worktree->base_commit_id;
1669 bbc_arg.entry_name = entry_name;
1670 bbc_arg.path = path;
1671 bbc_arg.path_len = strlen(path);
1672 bbc_arg.progress_cb = progress_cb;
1673 bbc_arg.progress_arg = progress_arg;
1674 err = got_fileindex_for_each_entry_safe(fileindex,
1675 bump_base_commit_id, &bbc_arg);
1676 sync:
1677 sync_err = sync_fileindex(fileindex, fileindex_path);
1678 if (sync_err && err == NULL)
1679 err = sync_err;
1680 done:
1681 free(fileindex_path);
1682 free(relpath);
1683 if (tree)
1684 got_object_tree_close(tree);
1685 if (commit)
1686 got_object_commit_close(commit);
1687 got_fileindex_free(fileindex);
1688 unlockerr = lock_worktree(worktree, LOCK_SH);
1689 if (unlockerr && err == NULL)
1690 err = unlockerr;
1691 return err;
1694 struct merge_file_cb_arg {
1695 struct got_worktree *worktree;
1696 struct got_fileindex *fileindex;
1697 got_worktree_checkout_cb progress_cb;
1698 void *progress_arg;
1699 got_worktree_cancel_cb cancel_cb;
1700 void *cancel_arg;
1701 struct got_object_id *commit_id2;
1704 static const struct got_error *
1705 merge_file_cb(void *arg, struct got_blob_object *blob1,
1706 struct got_blob_object *blob2, struct got_object_id *id1,
1707 struct got_object_id *id2, const char *path1, const char *path2,
1708 struct got_repository *repo)
1710 static const struct got_error *err = NULL;
1711 struct merge_file_cb_arg *a = arg;
1712 struct got_fileindex_entry *ie;
1713 char *ondisk_path = NULL;
1714 struct stat sb;
1715 unsigned char status;
1716 int local_changes_subsumed;
1718 if (blob1 && blob2) {
1719 ie = got_fileindex_entry_get(a->fileindex, path2);
1720 if (ie == NULL) {
1721 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1722 path2);
1723 return NULL;
1726 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1727 path2) == -1)
1728 return got_error_from_errno("asprintf");
1730 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1731 if (err)
1732 goto done;
1734 if (status == GOT_STATUS_DELETE) {
1735 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1736 path2);
1737 goto done;
1739 if (status != GOT_STATUS_NO_CHANGE &&
1740 status != GOT_STATUS_MODIFY &&
1741 status != GOT_STATUS_CONFLICT &&
1742 status != GOT_STATUS_ADD) {
1743 (*a->progress_cb)(a->progress_arg, status, path2);
1744 goto done;
1747 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1748 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1749 a->progress_cb, a->progress_arg);
1750 } else if (blob1) {
1751 ie = got_fileindex_entry_get(a->fileindex, path1);
1752 if (ie == NULL) {
1753 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1754 path2);
1755 return NULL;
1758 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1759 path1) == -1)
1760 return got_error_from_errno("asprintf");
1762 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1763 if (err)
1764 goto done;
1766 switch (status) {
1767 case GOT_STATUS_NO_CHANGE:
1768 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1769 path1);
1770 err = remove_ondisk_file(a->worktree->root_path, path1);
1771 if (err)
1772 goto done;
1773 if (ie)
1774 got_fileindex_entry_mark_deleted_from_disk(ie);
1775 break;
1776 case GOT_STATUS_DELETE:
1777 case GOT_STATUS_MISSING:
1778 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1779 path1);
1780 if (ie)
1781 got_fileindex_entry_mark_deleted_from_disk(ie);
1782 break;
1783 case GOT_STATUS_ADD:
1784 case GOT_STATUS_MODIFY:
1785 case GOT_STATUS_CONFLICT:
1786 (*a->progress_cb)(a->progress_arg,
1787 GOT_STATUS_CANNOT_DELETE, path1);
1788 break;
1789 case GOT_STATUS_OBSTRUCTED:
1790 (*a->progress_cb)(a->progress_arg, status, path1);
1791 break;
1792 default:
1793 break;
1795 } else if (blob2) {
1796 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1797 path2) == -1)
1798 return got_error_from_errno("asprintf");
1799 ie = got_fileindex_entry_get(a->fileindex, path2);
1800 if (ie) {
1801 err = get_file_status(&status, &sb, ie, ondisk_path,
1802 repo);
1803 if (err)
1804 goto done;
1805 if (status != GOT_STATUS_NO_CHANGE &&
1806 status != GOT_STATUS_MODIFY &&
1807 status != GOT_STATUS_CONFLICT &&
1808 status != GOT_STATUS_ADD) {
1809 (*a->progress_cb)(a->progress_arg, status,
1810 path2);
1811 goto done;
1813 err = merge_blob(&local_changes_subsumed, a->worktree,
1814 NULL, ondisk_path, path2, sb.st_mode, blob2,
1815 a->commit_id2, repo,
1816 a->progress_cb, a->progress_arg);
1817 if (status == GOT_STATUS_DELETE) {
1818 err = update_blob_fileindex_entry(a->worktree,
1819 a->fileindex, ie, ondisk_path, ie->path,
1820 blob2, 0);
1821 if (err)
1822 goto done;
1824 } else {
1825 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1826 err = install_blob(a->worktree, ondisk_path, path2,
1827 /* XXX get this from parent tree! */
1828 GOT_DEFAULT_FILE_MODE,
1829 sb.st_mode, blob2, 0, 0, repo,
1830 a->progress_cb, a->progress_arg);
1831 if (err)
1832 goto done;
1833 err = got_fileindex_entry_alloc(&ie,
1834 ondisk_path, path2, NULL, NULL);
1835 if (err)
1836 goto done;
1837 err = got_fileindex_entry_add(a->fileindex, ie);
1838 if (err) {
1839 got_fileindex_entry_free(ie);
1840 goto done;
1844 done:
1845 free(ondisk_path);
1846 return err;
1849 struct check_merge_ok_arg {
1850 struct got_worktree *worktree;
1851 struct got_repository *repo;
1854 static const struct got_error *
1855 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1857 const struct got_error *err = NULL;
1858 struct check_merge_ok_arg *a = arg;
1859 unsigned char status;
1860 struct stat sb;
1861 char *ondisk_path;
1863 /* Reject merges into a work tree with mixed base commits. */
1864 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1865 SHA1_DIGEST_LENGTH))
1866 return got_error(GOT_ERR_MIXED_COMMITS);
1868 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1869 == -1)
1870 return got_error_from_errno("asprintf");
1872 /* Reject merges into a work tree with conflicted files. */
1873 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1874 if (err)
1875 return err;
1876 if (status == GOT_STATUS_CONFLICT)
1877 return got_error(GOT_ERR_CONFLICTS);
1879 return NULL;
1882 static const struct got_error *
1883 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1884 const char *fileindex_path, struct got_object_id *commit_id1,
1885 struct got_object_id *commit_id2, struct got_repository *repo,
1886 got_worktree_checkout_cb progress_cb, void *progress_arg,
1887 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1889 const struct got_error *err = NULL, *sync_err;
1890 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1891 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1892 struct merge_file_cb_arg arg;
1894 if (commit_id1) {
1895 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1896 worktree->path_prefix);
1897 if (err)
1898 goto done;
1900 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1901 if (err)
1902 goto done;
1905 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1906 worktree->path_prefix);
1907 if (err)
1908 goto done;
1910 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1911 if (err)
1912 goto done;
1914 arg.worktree = worktree;
1915 arg.fileindex = fileindex;
1916 arg.progress_cb = progress_cb;
1917 arg.progress_arg = progress_arg;
1918 arg.cancel_cb = cancel_cb;
1919 arg.cancel_arg = cancel_arg;
1920 arg.commit_id2 = commit_id2;
1921 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1922 sync_err = sync_fileindex(fileindex, fileindex_path);
1923 if (sync_err && err == NULL)
1924 err = sync_err;
1925 done:
1926 if (tree1)
1927 got_object_tree_close(tree1);
1928 if (tree2)
1929 got_object_tree_close(tree2);
1930 return err;
1933 const struct got_error *
1934 got_worktree_merge_files(struct got_worktree *worktree,
1935 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1936 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1937 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1939 const struct got_error *err, *unlockerr;
1940 char *fileindex_path = NULL;
1941 struct got_fileindex *fileindex = NULL;
1942 struct check_merge_ok_arg mok_arg;
1944 err = lock_worktree(worktree, LOCK_EX);
1945 if (err)
1946 return err;
1948 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1949 if (err)
1950 goto done;
1952 mok_arg.worktree = worktree;
1953 mok_arg.repo = repo;
1954 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1955 &mok_arg);
1956 if (err)
1957 goto done;
1959 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
1960 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1961 done:
1962 if (fileindex)
1963 got_fileindex_free(fileindex);
1964 free(fileindex_path);
1965 unlockerr = lock_worktree(worktree, LOCK_SH);
1966 if (unlockerr && err == NULL)
1967 err = unlockerr;
1968 return err;
1971 struct diff_dir_cb_arg {
1972 struct got_fileindex *fileindex;
1973 struct got_worktree *worktree;
1974 const char *status_path;
1975 size_t status_path_len;
1976 struct got_repository *repo;
1977 got_worktree_status_cb status_cb;
1978 void *status_arg;
1979 got_worktree_cancel_cb cancel_cb;
1980 void *cancel_arg;
1983 static const struct got_error *
1984 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1985 got_worktree_status_cb status_cb, void *status_arg,
1986 struct got_repository *repo)
1988 const struct got_error *err = NULL;
1989 unsigned char status = GOT_STATUS_NO_CHANGE;
1990 struct stat sb;
1991 struct got_object_id blob_id, commit_id;
1993 err = get_file_status(&status, &sb, ie, abspath, repo);
1994 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1995 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1996 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1997 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1998 &commit_id);
2000 return err;
2003 static const struct got_error *
2004 status_old_new(void *arg, struct got_fileindex_entry *ie,
2005 struct dirent *de, const char *parent_path)
2007 const struct got_error *err = NULL;
2008 struct diff_dir_cb_arg *a = arg;
2009 char *abspath;
2011 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2012 return got_error(GOT_ERR_CANCELLED);
2014 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2015 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2016 return NULL;
2018 if (parent_path[0]) {
2019 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2020 parent_path, de->d_name) == -1)
2021 return got_error_from_errno("asprintf");
2022 } else {
2023 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2024 de->d_name) == -1)
2025 return got_error_from_errno("asprintf");
2028 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2029 a->repo);
2030 free(abspath);
2031 return err;
2034 static const struct got_error *
2035 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2037 struct diff_dir_cb_arg *a = arg;
2038 struct got_object_id blob_id, commit_id;
2039 unsigned char status;
2041 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2042 return got_error(GOT_ERR_CANCELLED);
2044 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2045 return NULL;
2047 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2048 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2049 if (got_fileindex_entry_has_file_on_disk(ie))
2050 status = GOT_STATUS_MISSING;
2051 else
2052 status = GOT_STATUS_DELETE;
2053 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2054 &commit_id);
2057 static const struct got_error *
2058 status_new(void *arg, struct dirent *de, const char *parent_path)
2060 const struct got_error *err = NULL;
2061 struct diff_dir_cb_arg *a = arg;
2062 char *path = NULL;
2064 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2065 return got_error(GOT_ERR_CANCELLED);
2067 if (de->d_type == DT_DIR)
2068 return NULL;
2070 /* XXX ignore symlinks for now */
2071 if (de->d_type == DT_LNK)
2072 return NULL;
2074 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2075 return NULL;
2077 if (parent_path[0]) {
2078 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2079 return got_error_from_errno("asprintf");
2080 } else {
2081 path = de->d_name;
2084 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2085 NULL, NULL);
2086 if (parent_path[0])
2087 free(path);
2088 return err;
2091 const struct got_error *
2092 got_worktree_status(struct got_worktree *worktree, const char *path,
2093 struct got_repository *repo, got_worktree_status_cb status_cb,
2094 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2096 const struct got_error *err = NULL;
2097 DIR *workdir = NULL;
2098 char *fileindex_path = NULL;
2099 struct got_fileindex *fileindex = NULL;
2100 FILE *index = NULL;
2101 struct got_fileindex_diff_dir_cb fdiff_cb;
2102 struct diff_dir_cb_arg arg;
2103 char *ondisk_path = NULL;
2105 fileindex = got_fileindex_alloc();
2106 if (fileindex == NULL) {
2107 err = got_error_from_errno("got_fileindex_alloc");
2108 goto done;
2111 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2112 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2113 err = got_error_from_errno("asprintf");
2114 fileindex_path = NULL;
2115 goto done;
2118 index = fopen(fileindex_path, "rb");
2119 if (index == NULL) {
2120 if (errno != ENOENT) {
2121 err = got_error_from_errno2("fopen", fileindex_path);
2122 goto done;
2124 } else {
2125 err = got_fileindex_read(fileindex, index);
2126 fclose(index);
2127 if (err)
2128 goto done;
2131 if (asprintf(&ondisk_path, "%s%s%s",
2132 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2133 err = got_error_from_errno("asprintf");
2134 goto done;
2136 workdir = opendir(ondisk_path);
2137 if (workdir == NULL) {
2138 if (errno == ENOTDIR || errno == ENOENT) {
2139 struct got_fileindex_entry *ie;
2140 ie = got_fileindex_entry_get(fileindex, path);
2141 if (ie == NULL) {
2142 err = got_error(GOT_ERR_BAD_PATH);
2143 goto done;
2145 err = report_file_status(ie, ondisk_path,
2146 status_cb, status_arg, repo);
2147 goto done;
2148 } else {
2149 err = got_error_from_errno2("opendir", ondisk_path);
2150 goto done;
2153 fdiff_cb.diff_old_new = status_old_new;
2154 fdiff_cb.diff_old = status_old;
2155 fdiff_cb.diff_new = status_new;
2156 arg.fileindex = fileindex;
2157 arg.worktree = worktree;
2158 arg.status_path = path;
2159 arg.status_path_len = strlen(path);
2160 arg.repo = repo;
2161 arg.status_cb = status_cb;
2162 arg.status_arg = status_arg;
2163 arg.cancel_cb = cancel_cb;
2164 arg.cancel_arg = cancel_arg;
2165 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2166 path, repo, &fdiff_cb, &arg);
2167 done:
2168 if (workdir)
2169 closedir(workdir);
2170 free(ondisk_path);
2171 free(fileindex_path);
2172 got_fileindex_free(fileindex);
2173 return err;
2176 const struct got_error *
2177 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2178 const char *arg)
2180 const struct got_error *err = NULL;
2181 char *resolved, *path = NULL;
2182 size_t len;
2184 *wt_path = NULL;
2186 resolved = realpath(arg, NULL);
2187 if (resolved == NULL)
2188 return got_error_from_errno2("realpath", arg);
2190 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2191 strlen(got_worktree_get_root_path(worktree)))) {
2192 err = got_error(GOT_ERR_BAD_PATH);
2193 goto done;
2196 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2197 err = got_path_skip_common_ancestor(&path,
2198 got_worktree_get_root_path(worktree), resolved);
2199 if (err)
2200 goto done;
2201 } else {
2202 path = strdup("");
2203 if (path == NULL) {
2204 err = got_error_from_errno("strdup");
2205 goto done;
2209 /* XXX status walk can't deal with trailing slash! */
2210 len = strlen(path);
2211 while (path[len - 1] == '/') {
2212 path[len - 1] = '\0';
2213 len--;
2215 done:
2216 free(resolved);
2217 if (err == NULL)
2218 *wt_path = path;
2219 else
2220 free(path);
2221 return err;
2224 static const struct got_error *
2225 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2226 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2227 struct got_repository *repo)
2229 const struct got_error *err = NULL;
2230 struct got_fileindex_entry *ie;
2232 /* Re-adding an existing entry is a no-op. */
2233 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2234 return NULL;
2236 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2237 if (err)
2238 return err;
2240 err = got_fileindex_entry_add(fileindex, ie);
2241 if (err) {
2242 got_fileindex_entry_free(ie);
2243 return err;
2246 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2249 const struct got_error *
2250 got_worktree_schedule_add(struct got_worktree *worktree,
2251 struct got_pathlist_head *ondisk_paths,
2252 got_worktree_status_cb status_cb, void *status_arg,
2253 struct got_repository *repo)
2255 struct got_fileindex *fileindex = NULL;
2256 char *fileindex_path = NULL;
2257 FILE *index = NULL;
2258 const struct got_error *err = NULL, *sync_err, *unlockerr;
2259 struct got_pathlist_entry *pe;
2261 err = lock_worktree(worktree, LOCK_EX);
2262 if (err)
2263 return err;
2266 fileindex = got_fileindex_alloc();
2267 if (fileindex == NULL) {
2268 err = got_error_from_errno("got_fileindex_alloc");
2269 goto done;
2272 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2273 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2274 err = got_error_from_errno("asprintf");
2275 fileindex_path = NULL;
2276 goto done;
2279 index = fopen(fileindex_path, "rb");
2280 if (index == NULL) {
2281 err = got_error_from_errno2("fopen", fileindex_path);
2282 goto done;
2285 err = got_fileindex_read(fileindex, index);
2286 if (err)
2287 goto done;
2289 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2290 char *relpath;
2291 err = got_path_skip_common_ancestor(&relpath,
2292 got_worktree_get_root_path(worktree), pe->path);
2293 if (err)
2294 break;
2295 err = schedule_addition(pe->path, fileindex, relpath,
2296 status_cb, status_arg, repo);
2297 free(relpath);
2298 if (err)
2299 break;
2301 sync_err = sync_fileindex(fileindex, fileindex_path);
2302 if (sync_err && err == NULL)
2303 err = sync_err;
2304 done:
2305 if (index) {
2306 if (fclose(index) != 0 && err == NULL)
2307 err = got_error_from_errno("fclose");
2309 if (fileindex)
2310 got_fileindex_free(fileindex);
2311 unlockerr = lock_worktree(worktree, LOCK_SH);
2312 if (unlockerr && err == NULL)
2313 err = unlockerr;
2314 return err;
2317 static const struct got_error *
2318 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2319 const char *relpath, int delete_local_mods,
2320 got_worktree_status_cb status_cb, void *status_arg,
2321 struct got_repository *repo)
2323 const struct got_error *err = NULL;
2324 struct got_fileindex_entry *ie = NULL;
2325 unsigned char status;
2326 struct stat sb;
2328 ie = got_fileindex_entry_get(fileindex, relpath);
2329 if (ie == NULL)
2330 return got_error(GOT_ERR_BAD_PATH);
2332 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2333 if (err)
2334 return err;
2336 if (status != GOT_STATUS_NO_CHANGE) {
2337 if (status == GOT_STATUS_DELETE)
2338 return got_error_set_errno(ENOENT, ondisk_path);
2339 if (status != GOT_STATUS_MODIFY)
2340 return got_error(GOT_ERR_FILE_STATUS);
2341 if (!delete_local_mods)
2342 return got_error(GOT_ERR_FILE_MODIFIED);
2345 if (unlink(ondisk_path) != 0)
2346 return got_error_from_errno2("unlink", ondisk_path);
2348 got_fileindex_entry_mark_deleted_from_disk(ie);
2349 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2352 const struct got_error *
2353 got_worktree_schedule_delete(struct got_worktree *worktree,
2354 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2355 got_worktree_status_cb status_cb, void *status_arg,
2356 struct got_repository *repo)
2358 struct got_fileindex *fileindex = NULL;
2359 char *fileindex_path = NULL;
2360 FILE *index = NULL;
2361 const struct got_error *err = NULL, *sync_err, *unlockerr;
2362 struct got_pathlist_entry *pe;
2364 err = lock_worktree(worktree, LOCK_EX);
2365 if (err)
2366 return err;
2368 fileindex = got_fileindex_alloc();
2369 if (fileindex == NULL) {
2370 err = got_error_from_errno("got_fileindex_alloc");
2371 goto done;
2374 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2375 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2376 err = got_error_from_errno("asprintf");
2377 fileindex_path = NULL;
2378 goto done;
2381 index = fopen(fileindex_path, "rb");
2382 if (index == NULL) {
2383 err = got_error_from_errno2("fopen", fileindex_path);
2384 goto done;
2387 err = got_fileindex_read(fileindex, index);
2388 if (err)
2389 goto done;
2391 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2392 char *relpath;
2393 err = got_path_skip_common_ancestor(&relpath,
2394 got_worktree_get_root_path(worktree), pe->path);
2395 if (err)
2396 break;
2397 err = schedule_for_deletion(pe->path, fileindex, relpath,
2398 delete_local_mods, status_cb, status_arg, repo);
2399 free(relpath);
2400 if (err)
2401 break;
2403 sync_err = sync_fileindex(fileindex, fileindex_path);
2404 if (sync_err && err == NULL)
2405 err = sync_err;
2406 done:
2407 if (index) {
2408 if (fclose(index) != 0 && err == NULL)
2409 err = got_error_from_errno("fclose");
2411 if (fileindex)
2412 got_fileindex_free(fileindex);
2413 unlockerr = lock_worktree(worktree, LOCK_SH);
2414 if (unlockerr && err == NULL)
2415 err = unlockerr;
2416 return err;
2419 static const struct got_error *
2420 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2421 const char *ondisk_path,
2422 got_worktree_checkout_cb progress_cb, void *progress_arg,
2423 struct got_repository *repo)
2425 const struct got_error *err = NULL;
2426 char *relpath = NULL, *parent_path = NULL;
2427 struct got_fileindex_entry *ie;
2428 struct got_tree_object *tree = NULL;
2429 struct got_object_id *tree_id = NULL;
2430 const struct got_tree_entry *te;
2431 char *tree_path = NULL, *te_name;
2432 struct got_blob_object *blob = NULL;
2433 unsigned char status;
2434 struct stat sb;
2436 err = got_path_skip_common_ancestor(&relpath,
2437 got_worktree_get_root_path(worktree), ondisk_path);
2438 if (err)
2439 goto done;
2441 ie = got_fileindex_entry_get(fileindex, relpath);
2442 if (ie == NULL) {
2443 err = got_error(GOT_ERR_BAD_PATH);
2444 goto done;
2447 /* Construct in-repository path of tree which contains this blob. */
2448 err = got_path_dirname(&parent_path, ie->path);
2449 if (err) {
2450 if (err->code != GOT_ERR_BAD_PATH)
2451 goto done;
2452 parent_path = strdup("/");
2453 if (parent_path == NULL) {
2454 err = got_error_from_errno("strdup");
2455 goto done;
2458 if (got_path_is_root_dir(worktree->path_prefix)) {
2459 tree_path = strdup(parent_path);
2460 if (tree_path == NULL) {
2461 err = got_error_from_errno("strdup");
2462 goto done;
2464 } else {
2465 if (got_path_is_root_dir(parent_path)) {
2466 tree_path = strdup(worktree->path_prefix);
2467 if (tree_path == NULL) {
2468 err = got_error_from_errno("strdup");
2469 goto done;
2471 } else {
2472 if (asprintf(&tree_path, "%s/%s",
2473 worktree->path_prefix, parent_path) == -1) {
2474 err = got_error_from_errno("asprintf");
2475 goto done;
2480 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2481 tree_path);
2482 if (err)
2483 goto done;
2485 err = got_object_open_as_tree(&tree, repo, tree_id);
2486 if (err)
2487 goto done;
2489 te_name = basename(ie->path);
2490 if (te_name == NULL) {
2491 err = got_error_from_errno2("basename", ie->path);
2492 goto done;
2495 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2496 if (err)
2497 goto done;
2499 te = got_object_tree_find_entry(tree, te_name);
2500 if (te == NULL && status != GOT_STATUS_ADD) {
2501 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2502 goto done;
2505 switch (status) {
2506 case GOT_STATUS_ADD:
2507 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2508 got_fileindex_entry_remove(fileindex, ie);
2509 break;
2510 case GOT_STATUS_DELETE:
2511 case GOT_STATUS_MODIFY:
2512 case GOT_STATUS_CONFLICT:
2513 case GOT_STATUS_MISSING: {
2514 struct got_object_id id;
2515 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2516 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2517 if (err)
2518 goto done;
2519 err = install_blob(worktree, ondisk_path, ie->path,
2520 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2521 progress_arg);
2522 if (err)
2523 goto done;
2524 if (status == GOT_STATUS_DELETE) {
2525 err = update_blob_fileindex_entry(worktree,
2526 fileindex, ie, ondisk_path, ie->path, blob, 1);
2527 if (err)
2528 goto done;
2530 break;
2532 default:
2533 goto done;
2535 done:
2536 free(relpath);
2537 free(parent_path);
2538 free(tree_path);
2539 if (blob)
2540 got_object_blob_close(blob);
2541 if (tree)
2542 got_object_tree_close(tree);
2543 free(tree_id);
2544 return err;
2547 const struct got_error *
2548 got_worktree_revert(struct got_worktree *worktree,
2549 struct got_pathlist_head *ondisk_paths,
2550 got_worktree_checkout_cb progress_cb, void *progress_arg,
2551 struct got_repository *repo)
2553 struct got_fileindex *fileindex = NULL;
2554 char *fileindex_path = NULL;
2555 FILE *index = NULL;
2556 const struct got_error *err = NULL, *unlockerr = NULL;
2557 const struct got_error *sync_err = NULL;
2558 struct got_pathlist_entry *pe;
2560 err = lock_worktree(worktree, LOCK_EX);
2561 if (err)
2562 return err;
2564 fileindex = got_fileindex_alloc();
2565 if (fileindex == NULL) {
2566 err = got_error_from_errno("got_fileindex_alloc");
2567 goto done;
2570 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2571 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2572 err = got_error_from_errno("asprintf");
2573 fileindex_path = NULL;
2574 goto done;
2577 index = fopen(fileindex_path, "rb");
2578 if (index == NULL) {
2579 err = got_error_from_errno2("fopen", fileindex_path);
2580 goto done;
2583 err = got_fileindex_read(fileindex, index);
2584 if (err)
2585 goto done;
2587 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2588 err = revert_file(worktree, fileindex, pe->path,
2589 progress_cb, progress_arg, repo);
2590 if (err)
2591 break;
2593 sync_err = sync_fileindex(fileindex, fileindex_path);
2594 if (sync_err && err == NULL)
2595 err = sync_err;
2596 done:
2597 if (index) {
2598 if (fclose(index) != 0 && err == NULL)
2599 err = got_error_from_errno("fclose");
2601 if (fileindex)
2602 got_fileindex_free(fileindex);
2603 unlockerr = lock_worktree(worktree, LOCK_SH);
2604 if (unlockerr && err == NULL)
2605 err = unlockerr;
2606 return err;
2609 static void
2610 free_commitable(struct got_commitable *ct)
2612 free(ct->path);
2613 free(ct->in_repo_path);
2614 free(ct->ondisk_path);
2615 free(ct->blob_id);
2616 free(ct->base_blob_id);
2617 free(ct->base_commit_id);
2618 free(ct);
2621 struct collect_commitables_arg {
2622 struct got_pathlist_head *commitable_paths;
2623 struct got_repository *repo;
2624 struct got_worktree *worktree;
2627 static const struct got_error *
2628 collect_commitables(void *arg, unsigned char status, const char *relpath,
2629 struct got_object_id *blob_id, struct got_object_id *commit_id)
2631 struct collect_commitables_arg *a = arg;
2632 const struct got_error *err = NULL;
2633 struct got_commitable *ct = NULL;
2634 struct got_pathlist_entry *new = NULL;
2635 char *parent_path = NULL, *path = NULL;
2636 struct stat sb;
2638 if (status == GOT_STATUS_CONFLICT)
2639 return got_error(GOT_ERR_COMMIT_CONFLICT);
2641 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2642 status != GOT_STATUS_DELETE)
2643 return NULL;
2645 if (asprintf(&path, "/%s", relpath) == -1) {
2646 err = got_error_from_errno("asprintf");
2647 goto done;
2649 if (strcmp(path, "/") == 0) {
2650 parent_path = strdup("");
2651 if (parent_path == NULL)
2652 return got_error_from_errno("strdup");
2653 } else {
2654 err = got_path_dirname(&parent_path, path);
2655 if (err)
2656 return err;
2659 ct = calloc(1, sizeof(*ct));
2660 if (ct == NULL) {
2661 err = got_error_from_errno("calloc");
2662 goto done;
2665 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2666 relpath) == -1) {
2667 err = got_error_from_errno("asprintf");
2668 goto done;
2670 if (status == GOT_STATUS_DELETE) {
2671 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2672 } else {
2673 if (lstat(ct->ondisk_path, &sb) != 0) {
2674 err = got_error_from_errno2("lstat", ct->ondisk_path);
2675 goto done;
2677 ct->mode = sb.st_mode;
2680 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2681 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2682 relpath) == -1) {
2683 err = got_error_from_errno("asprintf");
2684 goto done;
2687 ct->status = status;
2688 ct->blob_id = NULL; /* will be filled in when blob gets created */
2689 if (ct->status != GOT_STATUS_ADD) {
2690 ct->base_blob_id = got_object_id_dup(blob_id);
2691 if (ct->base_blob_id == NULL) {
2692 err = got_error_from_errno("got_object_id_dup");
2693 goto done;
2695 ct->base_commit_id = got_object_id_dup(commit_id);
2696 if (ct->base_commit_id == NULL) {
2697 err = got_error_from_errno("got_object_id_dup");
2698 goto done;
2701 ct->path = strdup(path);
2702 if (ct->path == NULL) {
2703 err = got_error_from_errno("strdup");
2704 goto done;
2706 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2707 done:
2708 if (ct && (err || new == NULL))
2709 free_commitable(ct);
2710 free(parent_path);
2711 free(path);
2712 return err;
2715 static const struct got_error *write_tree(struct got_object_id **,
2716 struct got_tree_object *, const char *, struct got_pathlist_head *,
2717 got_worktree_status_cb status_cb, void *status_arg,
2718 struct got_repository *);
2720 static const struct got_error *
2721 write_subtree(struct got_object_id **new_subtree_id,
2722 struct got_tree_entry *te, const char *parent_path,
2723 struct got_pathlist_head *commitable_paths,
2724 got_worktree_status_cb status_cb, void *status_arg,
2725 struct got_repository *repo)
2727 const struct got_error *err = NULL;
2728 struct got_tree_object *subtree;
2729 char *subpath;
2731 if (asprintf(&subpath, "%s%s%s", parent_path,
2732 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2733 return got_error_from_errno("asprintf");
2735 err = got_object_open_as_tree(&subtree, repo, te->id);
2736 if (err)
2737 return err;
2739 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2740 status_cb, status_arg, repo);
2741 got_object_tree_close(subtree);
2742 free(subpath);
2743 return err;
2746 static const struct got_error *
2747 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2749 const struct got_error *err = NULL;
2750 char *ct_parent_path = NULL;
2752 *match = 0;
2754 if (strchr(ct->path, '/') == NULL) {
2755 *match = got_path_is_root_dir(path);
2756 return NULL;
2759 err = got_path_dirname(&ct_parent_path, ct->path);
2760 if (err)
2761 return err;
2762 *match = (strcmp(path, ct_parent_path) == 0);
2763 free(ct_parent_path);
2764 return err;
2767 static mode_t
2768 get_ct_file_mode(struct got_commitable *ct)
2770 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2773 static const struct got_error *
2774 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2775 struct got_tree_entry *te, struct got_commitable *ct)
2777 const struct got_error *err = NULL;
2779 *new_te = NULL;
2781 err = got_object_tree_entry_dup(new_te, te);
2782 if (err)
2783 goto done;
2785 (*new_te)->mode = get_ct_file_mode(ct);
2787 free((*new_te)->id);
2788 (*new_te)->id = got_object_id_dup(ct->blob_id);
2789 if ((*new_te)->id == NULL) {
2790 err = got_error_from_errno("got_object_id_dup");
2791 goto done;
2793 done:
2794 if (err && *new_te) {
2795 got_object_tree_entry_close(*new_te);
2796 *new_te = NULL;
2798 return err;
2801 static const struct got_error *
2802 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2803 struct got_commitable *ct)
2805 const struct got_error *err = NULL;
2806 char *ct_name;
2808 *new_te = NULL;
2810 *new_te = calloc(1, sizeof(**new_te));
2811 if (*new_te == NULL)
2812 return got_error_from_errno("calloc");
2814 ct_name = basename(ct->path);
2815 if (ct_name == NULL) {
2816 err = got_error_from_errno2("basename", ct->path);
2817 goto done;
2819 (*new_te)->name = strdup(ct_name);
2820 if ((*new_te)->name == NULL) {
2821 err = got_error_from_errno("strdup");
2822 goto done;
2825 (*new_te)->mode = get_ct_file_mode(ct);
2827 (*new_te)->id = got_object_id_dup(ct->blob_id);
2828 if ((*new_te)->id == NULL) {
2829 err = got_error_from_errno("got_object_id_dup");
2830 goto done;
2832 done:
2833 if (err && *new_te) {
2834 got_object_tree_entry_close(*new_te);
2835 *new_te = NULL;
2837 return err;
2840 static const struct got_error *
2841 insert_tree_entry(struct got_tree_entry *new_te,
2842 struct got_pathlist_head *paths)
2844 const struct got_error *err = NULL;
2845 struct got_pathlist_entry *new_pe;
2847 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2848 if (err)
2849 return err;
2850 if (new_pe == NULL)
2851 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2852 return NULL;
2855 static const struct got_error *
2856 report_ct_status(struct got_commitable *ct,
2857 got_worktree_status_cb status_cb, void *status_arg)
2859 const char *ct_path = ct->path;
2860 while (ct_path[0] == '/')
2861 ct_path++;
2862 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2865 static const struct got_error *
2866 match_modified_subtree(int *modified, struct got_tree_entry *te,
2867 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2869 const struct got_error *err = NULL;
2870 struct got_pathlist_entry *pe;
2871 char *te_path;
2873 *modified = 0;
2875 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2876 got_path_is_root_dir(base_tree_path) ? "" : "/",
2877 te->name) == -1)
2878 return got_error_from_errno("asprintf");
2880 TAILQ_FOREACH(pe, commitable_paths, entry) {
2881 struct got_commitable *ct = pe->data;
2882 *modified = got_path_is_child(ct->in_repo_path, te_path,
2883 strlen(te_path));
2884 if (*modified)
2885 break;
2888 free(te_path);
2889 return err;
2892 static const struct got_error *
2893 match_deleted_or_modified_ct(struct got_commitable **ctp,
2894 struct got_tree_entry *te, const char *base_tree_path,
2895 struct got_pathlist_head *commitable_paths)
2897 const struct got_error *err = NULL;
2898 struct got_pathlist_entry *pe;
2900 *ctp = NULL;
2902 TAILQ_FOREACH(pe, commitable_paths, entry) {
2903 struct got_commitable *ct = pe->data;
2904 char *ct_name = NULL;
2905 int path_matches;
2907 if (ct->status != GOT_STATUS_MODIFY &&
2908 ct->status != GOT_STATUS_DELETE)
2909 continue;
2911 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2912 continue;
2914 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2915 if (err)
2916 return err;
2917 if (!path_matches)
2918 continue;
2920 ct_name = basename(pe->path);
2921 if (ct_name == NULL)
2922 return got_error_from_errno2("basename", pe->path);
2924 if (strcmp(te->name, ct_name) != 0)
2925 continue;
2927 *ctp = ct;
2928 break;
2931 return err;
2934 static const struct got_error *
2935 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2936 const char *child_path, const char *path_base_tree,
2937 struct got_pathlist_head *commitable_paths,
2938 got_worktree_status_cb status_cb, void *status_arg,
2939 struct got_repository *repo)
2941 const struct got_error *err = NULL;
2942 struct got_tree_entry *new_te;
2943 char *subtree_path;
2945 *new_tep = NULL;
2947 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2948 got_path_is_root_dir(path_base_tree) ? "" : "/",
2949 child_path) == -1)
2950 return got_error_from_errno("asprintf");
2952 new_te = calloc(1, sizeof(*new_te));
2953 new_te->mode = S_IFDIR;
2954 new_te->name = strdup(child_path);
2955 if (new_te->name == NULL) {
2956 err = got_error_from_errno("strdup");
2957 got_object_tree_entry_close(new_te);
2958 goto done;
2960 err = write_tree(&new_te->id, NULL, subtree_path,
2961 commitable_paths, status_cb, status_arg, repo);
2962 if (err) {
2963 got_object_tree_entry_close(new_te);
2964 goto done;
2966 done:
2967 free(subtree_path);
2968 if (err == NULL)
2969 *new_tep = new_te;
2970 return err;
2973 static const struct got_error *
2974 write_tree(struct got_object_id **new_tree_id,
2975 struct got_tree_object *base_tree, const char *path_base_tree,
2976 struct got_pathlist_head *commitable_paths,
2977 got_worktree_status_cb status_cb, void *status_arg,
2978 struct got_repository *repo)
2980 const struct got_error *err = NULL;
2981 const struct got_tree_entries *base_entries = NULL;
2982 struct got_pathlist_head paths;
2983 struct got_tree_entries new_tree_entries;
2984 struct got_tree_entry *te, *new_te = NULL;
2985 struct got_pathlist_entry *pe;
2987 TAILQ_INIT(&paths);
2988 new_tree_entries.nentries = 0;
2989 SIMPLEQ_INIT(&new_tree_entries.head);
2991 /* Insert, and recurse into, newly added entries first. */
2992 TAILQ_FOREACH(pe, commitable_paths, entry) {
2993 struct got_commitable *ct = pe->data;
2994 char *child_path = NULL, *slash;
2996 if (ct->status != GOT_STATUS_ADD ||
2997 (ct->flags & GOT_COMMITABLE_ADDED))
2998 continue;
3000 if (!got_path_is_child(pe->path, path_base_tree,
3001 strlen(path_base_tree)))
3002 continue;
3004 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3005 pe->path);
3006 if (err)
3007 goto done;
3009 slash = strchr(child_path, '/');
3010 if (slash == NULL) {
3011 err = alloc_added_blob_tree_entry(&new_te, ct);
3012 if (err)
3013 goto done;
3014 err = report_ct_status(ct, status_cb, status_arg);
3015 if (err)
3016 goto done;
3017 ct->flags |= GOT_COMMITABLE_ADDED;
3018 err = insert_tree_entry(new_te, &paths);
3019 if (err)
3020 goto done;
3021 } else {
3022 *slash = '\0'; /* trim trailing path components */
3023 if (base_tree == NULL ||
3024 got_object_tree_find_entry(base_tree, child_path)
3025 == NULL) {
3026 err = make_subtree_for_added_blob(&new_te,
3027 child_path, path_base_tree,
3028 commitable_paths, status_cb, status_arg,
3029 repo);
3030 if (err)
3031 goto done;
3032 err = insert_tree_entry(new_te, &paths);
3033 if (err)
3034 goto done;
3039 if (base_tree) {
3040 /* Handle modified and deleted entries. */
3041 base_entries = got_object_tree_get_entries(base_tree);
3042 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3043 struct got_commitable *ct = NULL;
3045 if (S_ISDIR(te->mode)) {
3046 int modified;
3047 err = got_object_tree_entry_dup(&new_te, te);
3048 if (err)
3049 goto done;
3050 err = match_modified_subtree(&modified, te,
3051 path_base_tree, commitable_paths);
3052 if (err)
3053 goto done;
3054 /* Avoid recursion into unmodified subtrees. */
3055 if (modified) {
3056 free(new_te->id);
3057 err = write_subtree(&new_te->id, te,
3058 path_base_tree, commitable_paths,
3059 status_cb, status_arg, repo);
3060 if (err)
3061 goto done;
3063 err = insert_tree_entry(new_te, &paths);
3064 if (err)
3065 goto done;
3066 continue;
3069 err = match_deleted_or_modified_ct(&ct, te,
3070 path_base_tree, commitable_paths);
3071 if (ct) {
3072 /* NB: Deleted entries get dropped here. */
3073 if (ct->status == GOT_STATUS_MODIFY) {
3074 err = alloc_modified_blob_tree_entry(
3075 &new_te, te, ct);
3076 if (err)
3077 goto done;
3078 err = insert_tree_entry(new_te, &paths);
3079 if (err)
3080 goto done;
3082 err = report_ct_status(ct, status_cb,
3083 status_arg);
3084 if (err)
3085 goto done;
3086 } else {
3087 /* Entry is unchanged; just copy it. */
3088 err = got_object_tree_entry_dup(&new_te, te);
3089 if (err)
3090 goto done;
3091 err = insert_tree_entry(new_te, &paths);
3092 if (err)
3093 goto done;
3098 /* Write new list of entries; deleted entries have been dropped. */
3099 TAILQ_FOREACH(pe, &paths, entry) {
3100 struct got_tree_entry *te = pe->data;
3101 new_tree_entries.nentries++;
3102 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3104 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3105 done:
3106 got_object_tree_entries_close(&new_tree_entries);
3107 got_pathlist_free(&paths);
3108 return err;
3111 static const struct got_error *
3112 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3113 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3115 const struct got_error *err = NULL, *sync_err;
3116 char *fileindex_path = NULL;
3117 struct got_fileindex *fileindex = NULL;
3118 struct got_pathlist_entry *pe;
3120 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3121 if (err)
3122 return err;
3124 TAILQ_FOREACH(pe, commitable_paths, entry) {
3125 struct got_fileindex_entry *ie;
3126 struct got_commitable *ct = pe->data;
3128 ie = got_fileindex_entry_get(fileindex, pe->path);
3129 if (ie) {
3130 if (ct->status == GOT_STATUS_DELETE) {
3131 got_fileindex_entry_remove(fileindex, ie);
3132 got_fileindex_entry_free(ie);
3133 } else
3134 err = got_fileindex_entry_update(ie,
3135 ct->ondisk_path, ct->blob_id->sha1,
3136 new_base_commit_id->sha1, 1);
3137 } else {
3138 err = got_fileindex_entry_alloc(&ie,
3139 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3140 new_base_commit_id->sha1);
3141 if (err)
3142 break;
3143 err = got_fileindex_entry_add(fileindex, ie);
3144 if (err)
3145 break;
3148 sync_err = sync_fileindex(fileindex, fileindex_path);
3149 if (sync_err && err == NULL)
3150 err = sync_err;
3151 free(fileindex_path);
3152 got_fileindex_free(fileindex);
3153 return err;
3156 static const struct got_error *
3157 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3158 struct got_object_id *head_commit_id)
3160 const struct got_error *err = NULL;
3161 struct got_object_id *id_in_head = NULL, *id = NULL;
3162 struct got_commit_object *commit = NULL;
3163 char *path = NULL;
3164 const char *ct_path = ct->in_repo_path;
3166 while (ct_path[0] == '/')
3167 ct_path++;
3170 * Ensure that no modifications were made to files *and their parents*
3171 * in commits between the file's base commit and the branch head.
3173 * Checking the parents is important for detecting conflicting tree
3174 * configurations (files or parent folders might have been moved,
3175 * deleted, added again, etc.). Such changes need to be merged with
3176 * local changes before a commit can occur.
3178 * The implication is that the file's (parent) entry in the root
3179 * directory must have the same ID in all relevant commits.
3181 if (ct->status != GOT_STATUS_ADD) {
3182 struct got_object_qid *pid;
3183 char *slash;
3184 struct got_object_id *root_entry_id = NULL;
3186 /* Trivial case: base commit == head commit */
3187 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3188 return NULL;
3190 /* Compute the path to the root directory's entry. */
3191 path = strdup(ct_path);
3192 if (path == NULL) {
3193 err = got_error_from_errno("strdup");
3194 goto done;
3196 slash = strchr(path, '/');
3197 if (slash)
3198 *slash = '\0';
3200 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3201 if (err)
3202 goto done;
3204 err = got_object_id_by_path(&root_entry_id, repo,
3205 head_commit_id, path);
3206 if (err)
3207 goto done;
3209 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3210 while (pid) {
3211 struct got_commit_object *pcommit;
3213 err = got_object_id_by_path(&id, repo, pid->id, path);
3214 if (err) {
3215 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3216 goto done;
3217 err = NULL;
3218 break;
3221 err = got_object_id_by_path(&id, repo, pid->id, path);
3222 if (err)
3223 goto done;
3225 if (got_object_id_cmp(id, root_entry_id) != 0) {
3226 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3227 break;
3230 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3231 break; /* all relevant commits scanned */
3233 err = got_object_open_as_commit(&pcommit, repo,
3234 pid->id);
3235 if (err)
3236 goto done;
3238 got_object_commit_close(commit);
3239 commit = pcommit;
3240 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3241 commit));
3243 } else {
3244 /* Require that added files don't exist in the branch head. */
3245 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3246 ct_path);
3247 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3248 goto done;
3249 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3251 done:
3252 if (commit)
3253 got_object_commit_close(commit);
3254 free(id_in_head);
3255 free(id);
3256 free(path);
3257 return err;
3260 const struct got_error *
3261 got_worktree_commit(struct got_object_id **new_commit_id,
3262 struct got_worktree *worktree, const char *ondisk_path,
3263 const char *author, const char *committer,
3264 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3265 got_worktree_status_cb status_cb, void *status_arg,
3266 struct got_repository *repo)
3268 const struct got_error *err = NULL, *unlockerr = NULL;
3269 struct collect_commitables_arg cc_arg;
3270 struct got_pathlist_head commitable_paths;
3271 struct got_pathlist_entry *pe;
3272 char *relpath = NULL;
3273 const char *head_ref_name = NULL;
3274 struct got_reference *head_ref = NULL;
3275 struct got_commit_object *head_commit = NULL;
3276 struct got_object_id *head_commit_id = NULL;
3277 struct got_reference *head_ref2 = NULL;
3278 struct got_object_id *head_commit_id2 = NULL;
3279 struct got_tree_object *head_tree = NULL;
3280 struct got_object_id *new_tree_id = NULL;
3281 struct got_object_id_queue parent_ids;
3282 struct got_object_qid *pid = NULL;
3283 char *logmsg = NULL;
3285 *new_commit_id = NULL;
3287 TAILQ_INIT(&commitable_paths);
3288 SIMPLEQ_INIT(&parent_ids);
3290 if (ondisk_path) {
3291 err = got_path_skip_common_ancestor(&relpath,
3292 worktree->root_path, ondisk_path);
3293 if (err)
3294 return err;
3297 err = lock_worktree(worktree, LOCK_EX);
3298 if (err)
3299 goto done;
3301 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3302 if (err)
3303 goto done;
3304 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3305 if (err)
3306 goto done;
3308 cc_arg.commitable_paths = &commitable_paths;
3309 cc_arg.worktree = worktree;
3310 cc_arg.repo = repo;
3311 err = got_worktree_status(worktree, relpath ? relpath : "",
3312 repo, collect_commitables, &cc_arg, NULL, NULL);
3313 if (err)
3314 goto done;
3316 if (TAILQ_EMPTY(&commitable_paths)) {
3317 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3318 goto done;
3321 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3322 if (err)
3323 goto done;
3325 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3326 struct got_commitable *ct = pe->data;
3327 err = check_ct_out_of_date(ct, repo, head_commit_id);
3328 if (err)
3329 goto done;
3332 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3333 if (err)
3334 goto done;
3336 if (commit_msg_cb != NULL) {
3337 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3338 if (err)
3339 goto done;
3342 if (logmsg == NULL || strlen(logmsg) == 0) {
3343 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3344 goto done;
3347 /* Create blobs from added and modified files and record their IDs. */
3348 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3349 struct got_commitable *ct = pe->data;
3350 char *ondisk_path;
3352 if (ct->status != GOT_STATUS_ADD &&
3353 ct->status != GOT_STATUS_MODIFY)
3354 continue;
3356 if (asprintf(&ondisk_path, "%s/%s",
3357 worktree->root_path, pe->path) == -1) {
3358 err = got_error_from_errno("asprintf");
3359 goto done;
3361 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3362 free(ondisk_path);
3363 if (err)
3364 goto done;
3367 /* Recursively write new tree objects. */
3368 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3369 status_cb, status_arg, repo);
3370 if (err)
3371 goto done;
3373 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3374 if (err)
3375 goto done;
3376 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3377 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3378 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3379 got_object_qid_free(pid);
3380 if (logmsg != NULL)
3381 free(logmsg);
3382 if (err)
3383 goto done;
3385 /* Check if a concurrent commit to our branch has occurred. */
3386 head_ref_name = got_worktree_get_head_ref_name(worktree);
3387 if (head_ref_name == NULL) {
3388 err = got_error_from_errno("got_worktree_get_head_ref_name");
3389 goto done;
3391 /* Lock the reference here to prevent concurrent modification. */
3392 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3393 if (err)
3394 goto done;
3395 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3396 if (err)
3397 goto done;
3398 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3399 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3400 goto done;
3402 /* Update branch head in repository. */
3403 err = got_ref_change_ref(head_ref2, *new_commit_id);
3404 if (err)
3405 goto done;
3406 err = got_ref_write(head_ref2, repo);
3407 if (err)
3408 goto done;
3410 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3411 if (err)
3412 goto done;
3414 err = ref_base_commit(worktree, repo);
3415 if (err)
3416 goto done;
3418 err = update_fileindex_after_commit(&commitable_paths,
3419 *new_commit_id, worktree);
3420 if (err)
3421 goto done;
3422 done:
3423 unlockerr = lock_worktree(worktree, LOCK_SH);
3424 if (unlockerr && err == NULL)
3425 err = unlockerr;
3426 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3427 struct got_commitable *ct = pe->data;
3428 free_commitable(ct);
3430 got_pathlist_free(&commitable_paths);
3431 if (head_tree)
3432 got_object_tree_close(head_tree);
3433 if (head_commit)
3434 got_object_commit_close(head_commit);
3435 free(relpath);
3436 free(head_commit_id);
3437 free(head_commit_id2);
3438 if (head_ref)
3439 got_ref_close(head_ref);
3440 if (head_ref2) {
3441 unlockerr = got_ref_unlock(head_ref2);
3442 if (unlockerr && err == NULL)
3443 err = unlockerr;
3444 got_ref_close(head_ref2);
3446 return err;
3449 const char *
3450 got_commitable_get_path(struct got_commitable *ct)
3452 return ct->path;
3455 unsigned int
3456 got_commitable_get_status(struct got_commitable *ct)
3458 return ct->status;
3461 struct check_rebase_ok_arg {
3462 struct got_worktree *worktree;
3463 struct got_repository *repo;
3464 int rebase_in_progress;
3467 static const struct got_error *
3468 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3470 const struct got_error *err = NULL;
3471 struct check_rebase_ok_arg *a = arg;
3472 unsigned char status;
3473 struct stat sb;
3474 char *ondisk_path;
3476 if (!a->rebase_in_progress) {
3477 /* Reject rebase of a work tree with mixed base commits. */
3478 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3479 SHA1_DIGEST_LENGTH))
3480 return got_error(GOT_ERR_MIXED_COMMITS);
3483 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3484 == -1)
3485 return got_error_from_errno("asprintf");
3487 /* Reject rebase of a work tree with modified or conflicted files. */
3488 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3489 free(ondisk_path);
3490 if (err)
3491 return err;
3493 if (a->rebase_in_progress) {
3494 if (status == GOT_STATUS_CONFLICT)
3495 return got_error(GOT_ERR_CONFLICTS);
3496 } else if (status != GOT_STATUS_NO_CHANGE)
3497 return got_error(GOT_ERR_MODIFIED);
3499 return NULL;
3502 const struct got_error *
3503 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3504 struct got_reference **tmp_branch, struct got_worktree *worktree,
3505 struct got_reference *branch, struct got_repository *repo)
3507 const struct got_error *err = NULL;
3508 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3509 char *branch_ref_name = NULL;
3510 struct got_fileindex *fileindex = NULL;
3511 char *fileindex_path = NULL;
3512 struct check_rebase_ok_arg ok_arg;
3513 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3515 *new_base_branch_ref = NULL;
3516 *tmp_branch = NULL;
3518 err = lock_worktree(worktree, LOCK_EX);
3519 if (err)
3520 return err;
3522 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3523 if (err)
3524 goto done;
3526 ok_arg.worktree = worktree;
3527 ok_arg.repo = repo;
3528 ok_arg.rebase_in_progress = 0;
3529 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3530 &ok_arg);
3531 if (err)
3532 goto done;
3534 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3535 if (err)
3536 goto done;
3538 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3539 if (err)
3540 goto done;
3542 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3543 if (err)
3544 goto done;
3546 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3547 0);
3548 if (err)
3549 goto done;
3551 err = got_ref_alloc_symref(new_base_branch_ref,
3552 new_base_branch_ref_name, wt_branch);
3553 if (err)
3554 goto done;
3555 err = got_ref_write(*new_base_branch_ref, repo);
3556 if (err)
3557 goto done;
3559 /* TODO Lock original branch's ref while rebasing? */
3561 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3562 if (err)
3563 goto done;
3565 err = got_ref_write(branch_ref, repo);
3566 if (err)
3567 goto done;
3569 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3570 worktree->base_commit_id);
3571 if (err)
3572 goto done;
3573 err = got_ref_write(*tmp_branch, repo);
3574 if (err)
3575 goto done;
3577 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3578 if (err)
3579 goto done;
3580 done:
3581 free(fileindex_path);
3582 if (fileindex)
3583 got_fileindex_free(fileindex);
3584 free(tmp_branch_name);
3585 free(new_base_branch_ref_name);
3586 free(branch_ref_name);
3587 if (branch_ref)
3588 got_ref_close(branch_ref);
3589 if (wt_branch)
3590 got_ref_close(wt_branch);
3591 if (err) {
3592 if (*new_base_branch_ref) {
3593 got_ref_close(*new_base_branch_ref);
3594 *new_base_branch_ref = NULL;
3596 if (*tmp_branch) {
3597 got_ref_close(*tmp_branch);
3598 *tmp_branch = NULL;
3600 lock_worktree(worktree, LOCK_SH);
3602 return err;
3605 const struct got_error *
3606 got_worktree_rebase_continue(struct got_object_id **commit_id,
3607 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3608 struct got_reference **branch, struct got_worktree *worktree,
3609 struct got_repository *repo)
3611 const struct got_error *err;
3612 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3613 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3614 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3616 *commit_id = NULL;
3618 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3619 if (err)
3620 return err;
3622 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3623 if (err)
3624 goto done;
3626 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3627 if (err)
3628 goto done;
3630 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3631 if (err)
3632 goto done;
3634 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3635 if (err)
3636 goto done;
3638 err = got_ref_open(branch, repo,
3639 got_ref_get_symref_target(branch_ref), 0);
3640 if (err)
3641 goto done;
3643 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3644 if (err)
3645 goto done;
3647 err = got_ref_resolve(commit_id, repo, commit_ref);
3648 if (err)
3649 goto done;
3651 err = got_ref_open(new_base_branch, repo,
3652 new_base_branch_ref_name, 0);
3653 if (err)
3654 goto done;
3656 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3657 if (err)
3658 goto done;
3659 done:
3660 free(commit_ref_name);
3661 free(branch_ref_name);
3662 if (commit_ref)
3663 got_ref_close(commit_ref);
3664 if (branch_ref)
3665 got_ref_close(branch_ref);
3666 if (err) {
3667 free(*commit_id);
3668 *commit_id = NULL;
3669 if (*tmp_branch) {
3670 got_ref_close(*tmp_branch);
3671 *tmp_branch = NULL;
3673 if (*new_base_branch) {
3674 got_ref_close(*new_base_branch);
3675 *new_base_branch = NULL;
3677 if (*branch) {
3678 got_ref_close(*branch);
3679 *branch = NULL;
3682 return err;
3685 const struct got_error *
3686 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3688 const struct got_error *err;
3689 char *tmp_branch_name = NULL;
3691 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3692 if (err)
3693 return err;
3695 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3696 free(tmp_branch_name);
3697 return NULL;
3700 static const struct got_error *
3701 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3702 char **logmsg, void *arg)
3704 struct got_commit_object *commit = arg;
3706 *logmsg = strdup(got_object_commit_get_logmsg(commit));
3707 if (*logmsg == NULL)
3708 return got_error_from_errno("strdup");
3710 return NULL;
3713 static const struct got_error *
3714 rebase_status(void *arg, unsigned char status, const char *path,
3715 struct got_object_id *blob_id, struct got_object_id *commit_id)
3717 return NULL;
3720 const struct got_error *
3721 got_worktree_rebase_merge_files(struct got_worktree *worktree,
3722 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3723 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3724 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3726 const struct got_error *err;
3727 struct got_fileindex *fileindex;
3728 char *fileindex_path, *commit_ref_name = NULL;
3729 struct got_reference *commit_ref = NULL;
3731 /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3733 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3734 if (err)
3735 return err;
3737 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3738 if (err)
3739 goto done;
3740 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3741 if (err) {
3742 if (err->code != GOT_ERR_NOT_REF)
3743 goto done;
3744 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3745 if (err)
3746 goto done;
3747 err = got_ref_write(commit_ref, repo);
3748 if (err)
3749 goto done;
3750 } else {
3751 struct got_object_id *stored_id;
3752 int cmp;
3754 err = got_ref_resolve(&stored_id, repo, commit_ref);
3755 if (err)
3756 goto done;
3757 cmp = got_object_id_cmp(commit_id, stored_id);
3758 free(stored_id);
3759 if (cmp != 0) {
3760 err = got_error(GOT_ERR_REBASE_COMMITID);
3761 goto done;
3765 err = merge_files(worktree, fileindex, fileindex_path,
3766 parent_commit_id, commit_id, repo, progress_cb, progress_arg,
3767 cancel_cb, cancel_arg);
3768 done:
3769 got_fileindex_free(fileindex);
3770 free(fileindex_path);
3771 if (commit_ref)
3772 got_ref_close(commit_ref);
3773 return err;
3776 static const struct got_error *
3777 bump_all_base_commit_ids(struct got_worktree *worktree,
3778 struct got_object_id *base_commit_id)
3780 const struct got_error *err = NULL, *sync_err;
3781 struct got_fileindex *fileindex;
3782 char *fileindex_path;
3783 struct bump_base_commit_id_arg bbc_arg;
3785 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3786 if (err)
3787 return err;
3789 bbc_arg.base_commit_id = base_commit_id;
3790 bbc_arg.entry_name = NULL;
3791 bbc_arg.path = "";
3792 bbc_arg.path_len = 0;
3793 bbc_arg.progress_cb = NULL;
3794 bbc_arg.progress_arg = NULL;
3795 err = got_fileindex_for_each_entry_safe(fileindex,
3796 bump_base_commit_id, &bbc_arg);
3798 sync_err = sync_fileindex(fileindex, fileindex_path);
3799 if (sync_err && err == NULL)
3800 err = sync_err;
3802 got_fileindex_free(fileindex);
3803 free(fileindex_path);
3804 return err;
3807 const struct got_error *
3808 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3809 struct got_worktree *worktree, struct got_reference *tmp_branch,
3810 struct got_commit_object *orig_commit,
3811 struct got_object_id *orig_commit_id, struct got_repository *repo)
3813 const struct got_error *err;
3814 char *commit_ref_name = NULL;
3815 struct got_reference *commit_ref = NULL;
3816 struct got_object_id *commit_id = NULL;
3818 /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3820 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3821 if (err)
3822 goto done;
3823 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3824 if (err)
3825 goto done;
3826 err = got_ref_resolve(&commit_id, repo, commit_ref);
3827 if (err)
3828 goto done;
3829 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3830 err = got_error(GOT_ERR_REBASE_COMMITID);
3831 goto done;
3834 err = got_worktree_commit(new_commit_id, worktree, NULL,
3835 got_object_commit_get_author(orig_commit),
3836 got_object_commit_get_committer(orig_commit),
3837 collect_rebase_commit_msg, orig_commit,
3838 rebase_status, NULL, repo);
3839 if (err) {
3840 if (err->code == GOT_ERR_COMMIT_NO_CHANGES) {
3841 /* No-op change; commit will be elided. */
3842 err = got_ref_delete(commit_ref, repo);
3843 if (err)
3844 goto done;
3845 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3847 goto done;
3850 /* Prevent out-of-date errors during rebase of subsequent commits. */
3851 err = bump_all_base_commit_ids(worktree, *new_commit_id);
3852 if (err)
3853 goto done;
3855 err = got_ref_delete(commit_ref, repo);
3856 if (err)
3857 goto done;
3859 err = got_ref_change_ref(tmp_branch, *new_commit_id);
3860 done:
3861 free(commit_ref_name);
3862 if (commit_ref)
3863 got_ref_close(commit_ref);
3864 if (err) {
3865 free(*new_commit_id);
3866 *new_commit_id = NULL;
3868 return err;
3871 const struct got_error *
3872 got_worktree_rebase_postpone(struct got_worktree *worktree)
3874 return lock_worktree(worktree, LOCK_SH);
3877 static const struct got_error *
3878 delete_ref(const char *name, struct got_repository *repo)
3880 const struct got_error *err;
3881 struct got_reference *ref;
3883 err = got_ref_open(&ref, repo, name, 0);
3884 if (err) {
3885 if (err->code == GOT_ERR_NOT_REF)
3886 return NULL;
3887 return err;
3890 err = got_ref_delete(ref, repo);
3891 got_ref_close(ref);
3892 return err;
3895 static const struct got_error *
3896 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
3898 const struct got_error *err;
3899 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3900 char *branch_ref_name = NULL, *commit_ref_name = NULL;
3902 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3903 if (err)
3904 goto done;
3905 err = delete_ref(tmp_branch_name, repo);
3906 if (err)
3907 goto done;
3909 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3910 if (err)
3911 goto done;
3912 err = delete_ref(new_base_branch_ref_name, repo);
3913 if (err)
3914 goto done;
3916 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3917 if (err)
3918 goto done;
3919 err = delete_ref(branch_ref_name, repo);
3920 if (err)
3921 goto done;
3923 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3924 if (err)
3925 goto done;
3926 err = delete_ref(commit_ref_name, repo);
3927 if (err)
3928 goto done;
3930 done:
3931 free(tmp_branch_name);
3932 free(new_base_branch_ref_name);
3933 free(branch_ref_name);
3934 free(commit_ref_name);
3935 return err;
3938 const struct got_error *
3939 got_worktree_rebase_complete(struct got_worktree *worktree,
3940 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3941 struct got_reference *rebased_branch,
3942 struct got_repository *repo)
3944 const struct got_error *err, *unlockerr;
3945 struct got_object_id *new_head_commit_id = NULL;
3947 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
3948 if (err)
3949 return err;
3951 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
3952 if (err)
3953 goto done;
3955 err = got_ref_write(rebased_branch, repo);
3956 if (err)
3957 goto done;
3959 err = got_worktree_set_head_ref(worktree, rebased_branch);
3960 if (err)
3961 goto done;
3963 err = delete_rebase_refs(worktree, repo);
3964 done:
3965 free(new_head_commit_id);
3966 unlockerr = lock_worktree(worktree, LOCK_SH);
3967 if (unlockerr && err == NULL)
3968 err = unlockerr;
3969 return err;
3972 struct collect_revertible_paths_arg {
3973 struct got_pathlist_head *revertible_paths;
3974 struct got_worktree *worktree;
3977 static const struct got_error *
3978 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
3979 struct got_object_id *blob_id, struct got_object_id *commit_id)
3981 struct collect_revertible_paths_arg *a = arg;
3982 const struct got_error *err = NULL;
3983 struct got_pathlist_entry *new = NULL;
3984 char *path = NULL;
3986 if (status != GOT_STATUS_ADD &&
3987 status != GOT_STATUS_DELETE &&
3988 status != GOT_STATUS_MODIFY &&
3989 status != GOT_STATUS_CONFLICT &&
3990 status != GOT_STATUS_MISSING)
3991 return NULL;
3993 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
3994 return got_error_from_errno("asprintf");
3996 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
3997 if (err || new == NULL)
3998 free(path);
3999 return err;
4002 const struct got_error *
4003 got_worktree_rebase_abort(struct got_worktree *worktree,
4004 struct got_repository *repo, struct got_reference *new_base_branch,
4005 got_worktree_checkout_cb progress_cb, void *progress_arg)
4007 const struct got_error *err, *unlockerr;
4008 struct got_reference *resolved = NULL;
4009 struct got_object_id *commit_id = NULL;
4010 struct got_fileindex *fileindex = NULL;
4011 char *fileindex_path = NULL;
4012 struct got_pathlist_head revertible_paths;
4013 struct got_pathlist_entry *pe;
4014 struct collect_revertible_paths_arg crp_arg;
4016 TAILQ_INIT(&revertible_paths);
4018 err = lock_worktree(worktree, LOCK_EX);
4019 if (err)
4020 return err;
4022 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4023 if (err)
4024 goto done;
4026 err = got_ref_open(&resolved, repo,
4027 got_ref_get_symref_target(new_base_branch), 0);
4028 if (err)
4029 goto done;
4031 err = got_worktree_set_head_ref(worktree, resolved);
4032 if (err)
4033 goto done;
4036 * XXX commits to the base branch could have happened while
4037 * we were busy rebasing; should we store the original commit ID
4038 * when rebase begins and read it back here?
4040 err = got_ref_resolve(&commit_id, repo, resolved);
4041 if (err)
4042 goto done;
4044 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4045 if (err)
4046 goto done;
4048 err = got_worktree_checkout_files(worktree, "", repo, progress_cb,
4049 progress_arg, NULL, NULL);
4050 if (err)
4051 goto done;
4053 crp_arg.revertible_paths = &revertible_paths;
4054 crp_arg.worktree = worktree;
4055 err = got_worktree_status(worktree, "", repo,
4056 collect_revertible_paths, &crp_arg, NULL, NULL);
4057 if (err)
4058 goto done;
4060 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4061 err = revert_file(worktree, fileindex, pe->path,
4062 progress_cb, progress_arg, repo);
4063 if (err)
4064 goto done;
4067 err = delete_rebase_refs(worktree, repo);
4068 done:
4069 got_ref_close(resolved);
4070 free(commit_id);
4071 got_fileindex_free(fileindex);
4072 free(fileindex_path);
4073 TAILQ_FOREACH(pe, &revertible_paths, entry)
4074 free((char *)pe->path);
4075 got_pathlist_free(&revertible_paths);
4077 unlockerr = lock_worktree(worktree, LOCK_SH);
4078 if (unlockerr && err == NULL)
4079 err = unlockerr;
4080 return err;