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 if (fileindex)
1688 got_fileindex_free(fileindex);
1689 unlockerr = lock_worktree(worktree, LOCK_SH);
1690 if (unlockerr && err == NULL)
1691 err = unlockerr;
1692 return err;
1695 struct merge_file_cb_arg {
1696 struct got_worktree *worktree;
1697 struct got_fileindex *fileindex;
1698 got_worktree_checkout_cb progress_cb;
1699 void *progress_arg;
1700 got_worktree_cancel_cb cancel_cb;
1701 void *cancel_arg;
1702 struct got_object_id *commit_id2;
1705 static const struct got_error *
1706 merge_file_cb(void *arg, struct got_blob_object *blob1,
1707 struct got_blob_object *blob2, struct got_object_id *id1,
1708 struct got_object_id *id2, const char *path1, const char *path2,
1709 struct got_repository *repo)
1711 static const struct got_error *err = NULL;
1712 struct merge_file_cb_arg *a = arg;
1713 struct got_fileindex_entry *ie;
1714 char *ondisk_path = NULL;
1715 struct stat sb;
1716 unsigned char status;
1717 int local_changes_subsumed;
1719 if (blob1 && blob2) {
1720 ie = got_fileindex_entry_get(a->fileindex, path2);
1721 if (ie == NULL) {
1722 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1723 path2);
1724 return NULL;
1727 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1728 path2) == -1)
1729 return got_error_from_errno("asprintf");
1731 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1732 if (err)
1733 goto done;
1735 if (status == GOT_STATUS_DELETE) {
1736 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1737 path2);
1738 goto done;
1740 if (status != GOT_STATUS_NO_CHANGE &&
1741 status != GOT_STATUS_MODIFY &&
1742 status != GOT_STATUS_CONFLICT &&
1743 status != GOT_STATUS_ADD) {
1744 (*a->progress_cb)(a->progress_arg, status, path2);
1745 goto done;
1748 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1749 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1750 a->progress_cb, a->progress_arg);
1751 } else if (blob1) {
1752 ie = got_fileindex_entry_get(a->fileindex, path1);
1753 if (ie == NULL) {
1754 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1755 path2);
1756 return NULL;
1759 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1760 path1) == -1)
1761 return got_error_from_errno("asprintf");
1763 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1764 if (err)
1765 goto done;
1767 switch (status) {
1768 case GOT_STATUS_NO_CHANGE:
1769 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1770 path1);
1771 err = remove_ondisk_file(a->worktree->root_path, path1);
1772 if (err)
1773 goto done;
1774 if (ie)
1775 got_fileindex_entry_mark_deleted_from_disk(ie);
1776 break;
1777 case GOT_STATUS_DELETE:
1778 case GOT_STATUS_MISSING:
1779 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1780 path1);
1781 if (ie)
1782 got_fileindex_entry_mark_deleted_from_disk(ie);
1783 break;
1784 case GOT_STATUS_ADD:
1785 case GOT_STATUS_MODIFY:
1786 case GOT_STATUS_CONFLICT:
1787 (*a->progress_cb)(a->progress_arg,
1788 GOT_STATUS_CANNOT_DELETE, path1);
1789 break;
1790 case GOT_STATUS_OBSTRUCTED:
1791 (*a->progress_cb)(a->progress_arg, status, path1);
1792 break;
1793 default:
1794 break;
1796 } else if (blob2) {
1797 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1798 path2) == -1)
1799 return got_error_from_errno("asprintf");
1800 ie = got_fileindex_entry_get(a->fileindex, path2);
1801 if (ie) {
1802 err = get_file_status(&status, &sb, ie, ondisk_path,
1803 repo);
1804 if (err)
1805 goto done;
1806 if (status != GOT_STATUS_NO_CHANGE &&
1807 status != GOT_STATUS_MODIFY &&
1808 status != GOT_STATUS_CONFLICT &&
1809 status != GOT_STATUS_ADD) {
1810 (*a->progress_cb)(a->progress_arg, status,
1811 path2);
1812 goto done;
1814 err = merge_blob(&local_changes_subsumed, a->worktree,
1815 NULL, ondisk_path, path2, sb.st_mode, blob2,
1816 a->commit_id2, repo,
1817 a->progress_cb, a->progress_arg);
1818 if (status == GOT_STATUS_DELETE) {
1819 err = update_blob_fileindex_entry(a->worktree,
1820 a->fileindex, ie, ondisk_path, ie->path,
1821 blob2, 0);
1822 if (err)
1823 goto done;
1825 } else {
1826 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1827 err = install_blob(a->worktree, ondisk_path, path2,
1828 /* XXX get this from parent tree! */
1829 GOT_DEFAULT_FILE_MODE,
1830 sb.st_mode, blob2, 0, 0, repo,
1831 a->progress_cb, a->progress_arg);
1832 if (err)
1833 goto done;
1834 err = got_fileindex_entry_alloc(&ie,
1835 ondisk_path, path2, NULL, NULL);
1836 if (err)
1837 goto done;
1838 err = got_fileindex_entry_add(a->fileindex, ie);
1839 if (err) {
1840 got_fileindex_entry_free(ie);
1841 goto done;
1845 done:
1846 free(ondisk_path);
1847 return err;
1850 struct check_merge_ok_arg {
1851 struct got_worktree *worktree;
1852 struct got_repository *repo;
1855 static const struct got_error *
1856 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1858 const struct got_error *err = NULL;
1859 struct check_merge_ok_arg *a = arg;
1860 unsigned char status;
1861 struct stat sb;
1862 char *ondisk_path;
1864 /* Reject merges into a work tree with mixed base commits. */
1865 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1866 SHA1_DIGEST_LENGTH))
1867 return got_error(GOT_ERR_MIXED_COMMITS);
1869 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1870 == -1)
1871 return got_error_from_errno("asprintf");
1873 /* Reject merges into a work tree with conflicted files. */
1874 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1875 if (err)
1876 return err;
1877 if (status == GOT_STATUS_CONFLICT)
1878 return got_error(GOT_ERR_CONFLICTS);
1880 return NULL;
1883 static const struct got_error *
1884 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1885 const char *fileindex_path, struct got_object_id *commit_id1,
1886 struct got_object_id *commit_id2, struct got_repository *repo,
1887 got_worktree_checkout_cb progress_cb, void *progress_arg,
1888 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1890 const struct got_error *err = NULL, *sync_err;
1891 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1892 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1893 struct merge_file_cb_arg arg;
1895 if (commit_id1) {
1896 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1897 worktree->path_prefix);
1898 if (err)
1899 goto done;
1901 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1902 if (err)
1903 goto done;
1906 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1907 worktree->path_prefix);
1908 if (err)
1909 goto done;
1911 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1912 if (err)
1913 goto done;
1915 arg.worktree = worktree;
1916 arg.fileindex = fileindex;
1917 arg.progress_cb = progress_cb;
1918 arg.progress_arg = progress_arg;
1919 arg.cancel_cb = cancel_cb;
1920 arg.cancel_arg = cancel_arg;
1921 arg.commit_id2 = commit_id2;
1922 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1923 sync_err = sync_fileindex(fileindex, fileindex_path);
1924 if (sync_err && err == NULL)
1925 err = sync_err;
1926 done:
1927 if (tree1)
1928 got_object_tree_close(tree1);
1929 if (tree2)
1930 got_object_tree_close(tree2);
1931 return err;
1934 const struct got_error *
1935 got_worktree_merge_files(struct got_worktree *worktree,
1936 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1937 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1938 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1940 const struct got_error *err, *unlockerr;
1941 char *fileindex_path = NULL;
1942 struct got_fileindex *fileindex = NULL;
1943 struct check_merge_ok_arg mok_arg;
1945 err = lock_worktree(worktree, LOCK_EX);
1946 if (err)
1947 return err;
1949 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1950 if (err)
1951 goto done;
1953 mok_arg.worktree = worktree;
1954 mok_arg.repo = repo;
1955 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1956 &mok_arg);
1957 if (err)
1958 goto done;
1960 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
1961 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1962 done:
1963 if (fileindex)
1964 got_fileindex_free(fileindex);
1965 free(fileindex_path);
1966 unlockerr = lock_worktree(worktree, LOCK_SH);
1967 if (unlockerr && err == NULL)
1968 err = unlockerr;
1969 return err;
1972 struct diff_dir_cb_arg {
1973 struct got_fileindex *fileindex;
1974 struct got_worktree *worktree;
1975 const char *status_path;
1976 size_t status_path_len;
1977 struct got_repository *repo;
1978 got_worktree_status_cb status_cb;
1979 void *status_arg;
1980 got_worktree_cancel_cb cancel_cb;
1981 void *cancel_arg;
1984 static const struct got_error *
1985 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1986 got_worktree_status_cb status_cb, void *status_arg,
1987 struct got_repository *repo)
1989 const struct got_error *err = NULL;
1990 unsigned char status = GOT_STATUS_NO_CHANGE;
1991 struct stat sb;
1992 struct got_object_id blob_id, commit_id;
1994 err = get_file_status(&status, &sb, ie, abspath, repo);
1995 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1996 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1997 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1998 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1999 &commit_id);
2001 return err;
2004 static const struct got_error *
2005 status_old_new(void *arg, struct got_fileindex_entry *ie,
2006 struct dirent *de, const char *parent_path)
2008 const struct got_error *err = NULL;
2009 struct diff_dir_cb_arg *a = arg;
2010 char *abspath;
2012 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2013 return got_error(GOT_ERR_CANCELLED);
2015 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2016 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2017 return NULL;
2019 if (parent_path[0]) {
2020 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2021 parent_path, de->d_name) == -1)
2022 return got_error_from_errno("asprintf");
2023 } else {
2024 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2025 de->d_name) == -1)
2026 return got_error_from_errno("asprintf");
2029 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2030 a->repo);
2031 free(abspath);
2032 return err;
2035 static const struct got_error *
2036 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2038 struct diff_dir_cb_arg *a = arg;
2039 struct got_object_id blob_id, commit_id;
2040 unsigned char status;
2042 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2043 return got_error(GOT_ERR_CANCELLED);
2045 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2046 return NULL;
2048 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2049 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2050 if (got_fileindex_entry_has_file_on_disk(ie))
2051 status = GOT_STATUS_MISSING;
2052 else
2053 status = GOT_STATUS_DELETE;
2054 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2055 &commit_id);
2058 static const struct got_error *
2059 status_new(void *arg, struct dirent *de, const char *parent_path)
2061 const struct got_error *err = NULL;
2062 struct diff_dir_cb_arg *a = arg;
2063 char *path = NULL;
2065 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2066 return got_error(GOT_ERR_CANCELLED);
2068 if (de->d_type == DT_DIR)
2069 return NULL;
2071 /* XXX ignore symlinks for now */
2072 if (de->d_type == DT_LNK)
2073 return NULL;
2075 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2076 return NULL;
2078 if (parent_path[0]) {
2079 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2080 return got_error_from_errno("asprintf");
2081 } else {
2082 path = de->d_name;
2085 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2086 NULL, NULL);
2087 if (parent_path[0])
2088 free(path);
2089 return err;
2092 const struct got_error *
2093 got_worktree_status(struct got_worktree *worktree, const char *path,
2094 struct got_repository *repo, got_worktree_status_cb status_cb,
2095 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2097 const struct got_error *err = NULL;
2098 DIR *workdir = NULL;
2099 char *fileindex_path = NULL;
2100 struct got_fileindex *fileindex = NULL;
2101 struct got_fileindex_diff_dir_cb fdiff_cb;
2102 struct diff_dir_cb_arg arg;
2103 char *ondisk_path = NULL;
2105 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2106 if (err)
2107 return err;
2109 if (asprintf(&ondisk_path, "%s%s%s",
2110 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2111 err = got_error_from_errno("asprintf");
2112 goto done;
2114 workdir = opendir(ondisk_path);
2115 if (workdir == NULL) {
2116 if (errno == ENOTDIR || errno == ENOENT) {
2117 struct got_fileindex_entry *ie;
2118 ie = got_fileindex_entry_get(fileindex, path);
2119 if (ie == NULL) {
2120 err = got_error(GOT_ERR_BAD_PATH);
2121 goto done;
2123 err = report_file_status(ie, ondisk_path,
2124 status_cb, status_arg, repo);
2125 goto done;
2126 } else {
2127 err = got_error_from_errno2("opendir", ondisk_path);
2128 goto done;
2131 fdiff_cb.diff_old_new = status_old_new;
2132 fdiff_cb.diff_old = status_old;
2133 fdiff_cb.diff_new = status_new;
2134 arg.fileindex = fileindex;
2135 arg.worktree = worktree;
2136 arg.status_path = path;
2137 arg.status_path_len = strlen(path);
2138 arg.repo = repo;
2139 arg.status_cb = status_cb;
2140 arg.status_arg = status_arg;
2141 arg.cancel_cb = cancel_cb;
2142 arg.cancel_arg = cancel_arg;
2143 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2144 path, repo, &fdiff_cb, &arg);
2145 done:
2146 if (workdir)
2147 closedir(workdir);
2148 free(ondisk_path);
2149 free(fileindex_path);
2150 got_fileindex_free(fileindex);
2151 return err;
2154 const struct got_error *
2155 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2156 const char *arg)
2158 const struct got_error *err = NULL;
2159 char *resolved, *path = NULL;
2160 size_t len;
2162 *wt_path = NULL;
2164 resolved = realpath(arg, NULL);
2165 if (resolved == NULL)
2166 return got_error_from_errno2("realpath", arg);
2168 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2169 strlen(got_worktree_get_root_path(worktree)))) {
2170 err = got_error(GOT_ERR_BAD_PATH);
2171 goto done;
2174 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2175 err = got_path_skip_common_ancestor(&path,
2176 got_worktree_get_root_path(worktree), resolved);
2177 if (err)
2178 goto done;
2179 } else {
2180 path = strdup("");
2181 if (path == NULL) {
2182 err = got_error_from_errno("strdup");
2183 goto done;
2187 /* XXX status walk can't deal with trailing slash! */
2188 len = strlen(path);
2189 while (path[len - 1] == '/') {
2190 path[len - 1] = '\0';
2191 len--;
2193 done:
2194 free(resolved);
2195 if (err == NULL)
2196 *wt_path = path;
2197 else
2198 free(path);
2199 return err;
2202 static const struct got_error *
2203 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2204 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2205 struct got_repository *repo)
2207 const struct got_error *err = NULL;
2208 struct got_fileindex_entry *ie;
2210 /* Re-adding an existing entry is a no-op. */
2211 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2212 return NULL;
2214 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2215 if (err)
2216 return err;
2218 err = got_fileindex_entry_add(fileindex, ie);
2219 if (err) {
2220 got_fileindex_entry_free(ie);
2221 return err;
2224 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2227 const struct got_error *
2228 got_worktree_schedule_add(struct got_worktree *worktree,
2229 struct got_pathlist_head *ondisk_paths,
2230 got_worktree_status_cb status_cb, void *status_arg,
2231 struct got_repository *repo)
2233 struct got_fileindex *fileindex = NULL;
2234 char *fileindex_path = NULL;
2235 const struct got_error *err = NULL, *sync_err, *unlockerr;
2236 struct got_pathlist_entry *pe;
2238 err = lock_worktree(worktree, LOCK_EX);
2239 if (err)
2240 return err;
2242 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2243 if (err)
2244 goto done;
2246 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2247 char *relpath;
2248 err = got_path_skip_common_ancestor(&relpath,
2249 got_worktree_get_root_path(worktree), pe->path);
2250 if (err)
2251 break;
2252 err = schedule_addition(pe->path, fileindex, relpath,
2253 status_cb, status_arg, repo);
2254 free(relpath);
2255 if (err)
2256 break;
2258 sync_err = sync_fileindex(fileindex, fileindex_path);
2259 if (sync_err && err == NULL)
2260 err = sync_err;
2261 done:
2262 if (fileindex)
2263 got_fileindex_free(fileindex);
2264 unlockerr = lock_worktree(worktree, LOCK_SH);
2265 if (unlockerr && err == NULL)
2266 err = unlockerr;
2267 return err;
2270 static const struct got_error *
2271 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2272 const char *relpath, int delete_local_mods,
2273 got_worktree_status_cb status_cb, void *status_arg,
2274 struct got_repository *repo)
2276 const struct got_error *err = NULL;
2277 struct got_fileindex_entry *ie = NULL;
2278 unsigned char status;
2279 struct stat sb;
2281 ie = got_fileindex_entry_get(fileindex, relpath);
2282 if (ie == NULL)
2283 return got_error(GOT_ERR_BAD_PATH);
2285 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2286 if (err)
2287 return err;
2289 if (status != GOT_STATUS_NO_CHANGE) {
2290 if (status == GOT_STATUS_DELETE)
2291 return got_error_set_errno(ENOENT, ondisk_path);
2292 if (status != GOT_STATUS_MODIFY)
2293 return got_error(GOT_ERR_FILE_STATUS);
2294 if (!delete_local_mods)
2295 return got_error(GOT_ERR_FILE_MODIFIED);
2298 if (unlink(ondisk_path) != 0)
2299 return got_error_from_errno2("unlink", ondisk_path);
2301 got_fileindex_entry_mark_deleted_from_disk(ie);
2302 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2305 const struct got_error *
2306 got_worktree_schedule_delete(struct got_worktree *worktree,
2307 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2308 got_worktree_status_cb status_cb, void *status_arg,
2309 struct got_repository *repo)
2311 struct got_fileindex *fileindex = NULL;
2312 char *fileindex_path = NULL;
2313 const struct got_error *err = NULL, *sync_err, *unlockerr;
2314 struct got_pathlist_entry *pe;
2316 err = lock_worktree(worktree, LOCK_EX);
2317 if (err)
2318 return err;
2320 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2321 if (err)
2322 goto done;
2324 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2325 char *relpath;
2326 err = got_path_skip_common_ancestor(&relpath,
2327 got_worktree_get_root_path(worktree), pe->path);
2328 if (err)
2329 break;
2330 err = schedule_for_deletion(pe->path, fileindex, relpath,
2331 delete_local_mods, status_cb, status_arg, repo);
2332 free(relpath);
2333 if (err)
2334 break;
2336 sync_err = sync_fileindex(fileindex, fileindex_path);
2337 if (sync_err && err == NULL)
2338 err = sync_err;
2339 done:
2340 if (fileindex)
2341 got_fileindex_free(fileindex);
2342 unlockerr = lock_worktree(worktree, LOCK_SH);
2343 if (unlockerr && err == NULL)
2344 err = unlockerr;
2345 return err;
2348 static const struct got_error *
2349 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2350 const char *ondisk_path,
2351 got_worktree_checkout_cb progress_cb, void *progress_arg,
2352 struct got_repository *repo)
2354 const struct got_error *err = NULL;
2355 char *relpath = NULL, *parent_path = NULL;
2356 struct got_fileindex_entry *ie;
2357 struct got_tree_object *tree = NULL;
2358 struct got_object_id *tree_id = NULL;
2359 const struct got_tree_entry *te;
2360 char *tree_path = NULL, *te_name;
2361 struct got_blob_object *blob = NULL;
2362 unsigned char status;
2363 struct stat sb;
2365 err = got_path_skip_common_ancestor(&relpath,
2366 got_worktree_get_root_path(worktree), ondisk_path);
2367 if (err)
2368 goto done;
2370 ie = got_fileindex_entry_get(fileindex, relpath);
2371 if (ie == NULL) {
2372 err = got_error(GOT_ERR_BAD_PATH);
2373 goto done;
2376 /* Construct in-repository path of tree which contains this blob. */
2377 err = got_path_dirname(&parent_path, ie->path);
2378 if (err) {
2379 if (err->code != GOT_ERR_BAD_PATH)
2380 goto done;
2381 parent_path = strdup("/");
2382 if (parent_path == NULL) {
2383 err = got_error_from_errno("strdup");
2384 goto done;
2387 if (got_path_is_root_dir(worktree->path_prefix)) {
2388 tree_path = strdup(parent_path);
2389 if (tree_path == NULL) {
2390 err = got_error_from_errno("strdup");
2391 goto done;
2393 } else {
2394 if (got_path_is_root_dir(parent_path)) {
2395 tree_path = strdup(worktree->path_prefix);
2396 if (tree_path == NULL) {
2397 err = got_error_from_errno("strdup");
2398 goto done;
2400 } else {
2401 if (asprintf(&tree_path, "%s/%s",
2402 worktree->path_prefix, parent_path) == -1) {
2403 err = got_error_from_errno("asprintf");
2404 goto done;
2409 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2410 tree_path);
2411 if (err)
2412 goto done;
2414 err = got_object_open_as_tree(&tree, repo, tree_id);
2415 if (err)
2416 goto done;
2418 te_name = basename(ie->path);
2419 if (te_name == NULL) {
2420 err = got_error_from_errno2("basename", ie->path);
2421 goto done;
2424 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2425 if (err)
2426 goto done;
2428 te = got_object_tree_find_entry(tree, te_name);
2429 if (te == NULL && status != GOT_STATUS_ADD) {
2430 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2431 goto done;
2434 switch (status) {
2435 case GOT_STATUS_ADD:
2436 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2437 got_fileindex_entry_remove(fileindex, ie);
2438 break;
2439 case GOT_STATUS_DELETE:
2440 case GOT_STATUS_MODIFY:
2441 case GOT_STATUS_CONFLICT:
2442 case GOT_STATUS_MISSING: {
2443 struct got_object_id id;
2444 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2445 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2446 if (err)
2447 goto done;
2448 err = install_blob(worktree, ondisk_path, ie->path,
2449 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2450 progress_arg);
2451 if (err)
2452 goto done;
2453 if (status == GOT_STATUS_DELETE) {
2454 err = update_blob_fileindex_entry(worktree,
2455 fileindex, ie, ondisk_path, ie->path, blob, 1);
2456 if (err)
2457 goto done;
2459 break;
2461 default:
2462 goto done;
2464 done:
2465 free(relpath);
2466 free(parent_path);
2467 free(tree_path);
2468 if (blob)
2469 got_object_blob_close(blob);
2470 if (tree)
2471 got_object_tree_close(tree);
2472 free(tree_id);
2473 return err;
2476 const struct got_error *
2477 got_worktree_revert(struct got_worktree *worktree,
2478 struct got_pathlist_head *ondisk_paths,
2479 got_worktree_checkout_cb progress_cb, void *progress_arg,
2480 struct got_repository *repo)
2482 struct got_fileindex *fileindex = NULL;
2483 char *fileindex_path = NULL;
2484 const struct got_error *err = NULL, *unlockerr = NULL;
2485 const struct got_error *sync_err = NULL;
2486 struct got_pathlist_entry *pe;
2488 err = lock_worktree(worktree, LOCK_EX);
2489 if (err)
2490 return err;
2492 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2493 if (err)
2494 goto done;
2496 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2497 err = revert_file(worktree, fileindex, pe->path,
2498 progress_cb, progress_arg, repo);
2499 if (err)
2500 break;
2502 sync_err = sync_fileindex(fileindex, fileindex_path);
2503 if (sync_err && err == NULL)
2504 err = sync_err;
2505 done:
2506 if (fileindex)
2507 got_fileindex_free(fileindex);
2508 unlockerr = lock_worktree(worktree, LOCK_SH);
2509 if (unlockerr && err == NULL)
2510 err = unlockerr;
2511 return err;
2514 static void
2515 free_commitable(struct got_commitable *ct)
2517 free(ct->path);
2518 free(ct->in_repo_path);
2519 free(ct->ondisk_path);
2520 free(ct->blob_id);
2521 free(ct->base_blob_id);
2522 free(ct->base_commit_id);
2523 free(ct);
2526 struct collect_commitables_arg {
2527 struct got_pathlist_head *commitable_paths;
2528 struct got_repository *repo;
2529 struct got_worktree *worktree;
2532 static const struct got_error *
2533 collect_commitables(void *arg, unsigned char status, const char *relpath,
2534 struct got_object_id *blob_id, struct got_object_id *commit_id)
2536 struct collect_commitables_arg *a = arg;
2537 const struct got_error *err = NULL;
2538 struct got_commitable *ct = NULL;
2539 struct got_pathlist_entry *new = NULL;
2540 char *parent_path = NULL, *path = NULL;
2541 struct stat sb;
2543 if (status == GOT_STATUS_CONFLICT)
2544 return got_error(GOT_ERR_COMMIT_CONFLICT);
2546 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2547 status != GOT_STATUS_DELETE)
2548 return NULL;
2550 if (asprintf(&path, "/%s", relpath) == -1) {
2551 err = got_error_from_errno("asprintf");
2552 goto done;
2554 if (strcmp(path, "/") == 0) {
2555 parent_path = strdup("");
2556 if (parent_path == NULL)
2557 return got_error_from_errno("strdup");
2558 } else {
2559 err = got_path_dirname(&parent_path, path);
2560 if (err)
2561 return err;
2564 ct = calloc(1, sizeof(*ct));
2565 if (ct == NULL) {
2566 err = got_error_from_errno("calloc");
2567 goto done;
2570 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2571 relpath) == -1) {
2572 err = got_error_from_errno("asprintf");
2573 goto done;
2575 if (status == GOT_STATUS_DELETE) {
2576 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2577 } else {
2578 if (lstat(ct->ondisk_path, &sb) != 0) {
2579 err = got_error_from_errno2("lstat", ct->ondisk_path);
2580 goto done;
2582 ct->mode = sb.st_mode;
2585 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2586 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2587 relpath) == -1) {
2588 err = got_error_from_errno("asprintf");
2589 goto done;
2592 ct->status = status;
2593 ct->blob_id = NULL; /* will be filled in when blob gets created */
2594 if (ct->status != GOT_STATUS_ADD) {
2595 ct->base_blob_id = got_object_id_dup(blob_id);
2596 if (ct->base_blob_id == NULL) {
2597 err = got_error_from_errno("got_object_id_dup");
2598 goto done;
2600 ct->base_commit_id = got_object_id_dup(commit_id);
2601 if (ct->base_commit_id == NULL) {
2602 err = got_error_from_errno("got_object_id_dup");
2603 goto done;
2606 ct->path = strdup(path);
2607 if (ct->path == NULL) {
2608 err = got_error_from_errno("strdup");
2609 goto done;
2611 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2612 done:
2613 if (ct && (err || new == NULL))
2614 free_commitable(ct);
2615 free(parent_path);
2616 free(path);
2617 return err;
2620 static const struct got_error *write_tree(struct got_object_id **,
2621 struct got_tree_object *, const char *, struct got_pathlist_head *,
2622 got_worktree_status_cb status_cb, void *status_arg,
2623 struct got_repository *);
2625 static const struct got_error *
2626 write_subtree(struct got_object_id **new_subtree_id,
2627 struct got_tree_entry *te, const char *parent_path,
2628 struct got_pathlist_head *commitable_paths,
2629 got_worktree_status_cb status_cb, void *status_arg,
2630 struct got_repository *repo)
2632 const struct got_error *err = NULL;
2633 struct got_tree_object *subtree;
2634 char *subpath;
2636 if (asprintf(&subpath, "%s%s%s", parent_path,
2637 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2638 return got_error_from_errno("asprintf");
2640 err = got_object_open_as_tree(&subtree, repo, te->id);
2641 if (err)
2642 return err;
2644 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2645 status_cb, status_arg, repo);
2646 got_object_tree_close(subtree);
2647 free(subpath);
2648 return err;
2651 static const struct got_error *
2652 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2654 const struct got_error *err = NULL;
2655 char *ct_parent_path = NULL;
2657 *match = 0;
2659 if (strchr(ct->path, '/') == NULL) {
2660 *match = got_path_is_root_dir(path);
2661 return NULL;
2664 err = got_path_dirname(&ct_parent_path, ct->path);
2665 if (err)
2666 return err;
2667 *match = (strcmp(path, ct_parent_path) == 0);
2668 free(ct_parent_path);
2669 return err;
2672 static mode_t
2673 get_ct_file_mode(struct got_commitable *ct)
2675 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2678 static const struct got_error *
2679 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2680 struct got_tree_entry *te, struct got_commitable *ct)
2682 const struct got_error *err = NULL;
2684 *new_te = NULL;
2686 err = got_object_tree_entry_dup(new_te, te);
2687 if (err)
2688 goto done;
2690 (*new_te)->mode = get_ct_file_mode(ct);
2692 free((*new_te)->id);
2693 (*new_te)->id = got_object_id_dup(ct->blob_id);
2694 if ((*new_te)->id == NULL) {
2695 err = got_error_from_errno("got_object_id_dup");
2696 goto done;
2698 done:
2699 if (err && *new_te) {
2700 got_object_tree_entry_close(*new_te);
2701 *new_te = NULL;
2703 return err;
2706 static const struct got_error *
2707 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2708 struct got_commitable *ct)
2710 const struct got_error *err = NULL;
2711 char *ct_name;
2713 *new_te = NULL;
2715 *new_te = calloc(1, sizeof(**new_te));
2716 if (*new_te == NULL)
2717 return got_error_from_errno("calloc");
2719 ct_name = basename(ct->path);
2720 if (ct_name == NULL) {
2721 err = got_error_from_errno2("basename", ct->path);
2722 goto done;
2724 (*new_te)->name = strdup(ct_name);
2725 if ((*new_te)->name == NULL) {
2726 err = got_error_from_errno("strdup");
2727 goto done;
2730 (*new_te)->mode = get_ct_file_mode(ct);
2732 (*new_te)->id = got_object_id_dup(ct->blob_id);
2733 if ((*new_te)->id == NULL) {
2734 err = got_error_from_errno("got_object_id_dup");
2735 goto done;
2737 done:
2738 if (err && *new_te) {
2739 got_object_tree_entry_close(*new_te);
2740 *new_te = NULL;
2742 return err;
2745 static const struct got_error *
2746 insert_tree_entry(struct got_tree_entry *new_te,
2747 struct got_pathlist_head *paths)
2749 const struct got_error *err = NULL;
2750 struct got_pathlist_entry *new_pe;
2752 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2753 if (err)
2754 return err;
2755 if (new_pe == NULL)
2756 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2757 return NULL;
2760 static const struct got_error *
2761 report_ct_status(struct got_commitable *ct,
2762 got_worktree_status_cb status_cb, void *status_arg)
2764 const char *ct_path = ct->path;
2765 while (ct_path[0] == '/')
2766 ct_path++;
2767 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2770 static const struct got_error *
2771 match_modified_subtree(int *modified, struct got_tree_entry *te,
2772 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2774 const struct got_error *err = NULL;
2775 struct got_pathlist_entry *pe;
2776 char *te_path;
2778 *modified = 0;
2780 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2781 got_path_is_root_dir(base_tree_path) ? "" : "/",
2782 te->name) == -1)
2783 return got_error_from_errno("asprintf");
2785 TAILQ_FOREACH(pe, commitable_paths, entry) {
2786 struct got_commitable *ct = pe->data;
2787 *modified = got_path_is_child(ct->in_repo_path, te_path,
2788 strlen(te_path));
2789 if (*modified)
2790 break;
2793 free(te_path);
2794 return err;
2797 static const struct got_error *
2798 match_deleted_or_modified_ct(struct got_commitable **ctp,
2799 struct got_tree_entry *te, const char *base_tree_path,
2800 struct got_pathlist_head *commitable_paths)
2802 const struct got_error *err = NULL;
2803 struct got_pathlist_entry *pe;
2805 *ctp = NULL;
2807 TAILQ_FOREACH(pe, commitable_paths, entry) {
2808 struct got_commitable *ct = pe->data;
2809 char *ct_name = NULL;
2810 int path_matches;
2812 if (ct->status != GOT_STATUS_MODIFY &&
2813 ct->status != GOT_STATUS_DELETE)
2814 continue;
2816 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2817 continue;
2819 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2820 if (err)
2821 return err;
2822 if (!path_matches)
2823 continue;
2825 ct_name = basename(pe->path);
2826 if (ct_name == NULL)
2827 return got_error_from_errno2("basename", pe->path);
2829 if (strcmp(te->name, ct_name) != 0)
2830 continue;
2832 *ctp = ct;
2833 break;
2836 return err;
2839 static const struct got_error *
2840 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2841 const char *child_path, const char *path_base_tree,
2842 struct got_pathlist_head *commitable_paths,
2843 got_worktree_status_cb status_cb, void *status_arg,
2844 struct got_repository *repo)
2846 const struct got_error *err = NULL;
2847 struct got_tree_entry *new_te;
2848 char *subtree_path;
2850 *new_tep = NULL;
2852 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2853 got_path_is_root_dir(path_base_tree) ? "" : "/",
2854 child_path) == -1)
2855 return got_error_from_errno("asprintf");
2857 new_te = calloc(1, sizeof(*new_te));
2858 new_te->mode = S_IFDIR;
2859 new_te->name = strdup(child_path);
2860 if (new_te->name == NULL) {
2861 err = got_error_from_errno("strdup");
2862 got_object_tree_entry_close(new_te);
2863 goto done;
2865 err = write_tree(&new_te->id, NULL, subtree_path,
2866 commitable_paths, status_cb, status_arg, repo);
2867 if (err) {
2868 got_object_tree_entry_close(new_te);
2869 goto done;
2871 done:
2872 free(subtree_path);
2873 if (err == NULL)
2874 *new_tep = new_te;
2875 return err;
2878 static const struct got_error *
2879 write_tree(struct got_object_id **new_tree_id,
2880 struct got_tree_object *base_tree, const char *path_base_tree,
2881 struct got_pathlist_head *commitable_paths,
2882 got_worktree_status_cb status_cb, void *status_arg,
2883 struct got_repository *repo)
2885 const struct got_error *err = NULL;
2886 const struct got_tree_entries *base_entries = NULL;
2887 struct got_pathlist_head paths;
2888 struct got_tree_entries new_tree_entries;
2889 struct got_tree_entry *te, *new_te = NULL;
2890 struct got_pathlist_entry *pe;
2892 TAILQ_INIT(&paths);
2893 new_tree_entries.nentries = 0;
2894 SIMPLEQ_INIT(&new_tree_entries.head);
2896 /* Insert, and recurse into, newly added entries first. */
2897 TAILQ_FOREACH(pe, commitable_paths, entry) {
2898 struct got_commitable *ct = pe->data;
2899 char *child_path = NULL, *slash;
2901 if (ct->status != GOT_STATUS_ADD ||
2902 (ct->flags & GOT_COMMITABLE_ADDED))
2903 continue;
2905 if (!got_path_is_child(pe->path, path_base_tree,
2906 strlen(path_base_tree)))
2907 continue;
2909 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2910 pe->path);
2911 if (err)
2912 goto done;
2914 slash = strchr(child_path, '/');
2915 if (slash == NULL) {
2916 err = alloc_added_blob_tree_entry(&new_te, ct);
2917 if (err)
2918 goto done;
2919 err = report_ct_status(ct, status_cb, status_arg);
2920 if (err)
2921 goto done;
2922 ct->flags |= GOT_COMMITABLE_ADDED;
2923 err = insert_tree_entry(new_te, &paths);
2924 if (err)
2925 goto done;
2926 } else {
2927 *slash = '\0'; /* trim trailing path components */
2928 if (base_tree == NULL ||
2929 got_object_tree_find_entry(base_tree, child_path)
2930 == NULL) {
2931 err = make_subtree_for_added_blob(&new_te,
2932 child_path, path_base_tree,
2933 commitable_paths, status_cb, status_arg,
2934 repo);
2935 if (err)
2936 goto done;
2937 err = insert_tree_entry(new_te, &paths);
2938 if (err)
2939 goto done;
2944 if (base_tree) {
2945 /* Handle modified and deleted entries. */
2946 base_entries = got_object_tree_get_entries(base_tree);
2947 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2948 struct got_commitable *ct = NULL;
2950 if (S_ISDIR(te->mode)) {
2951 int modified;
2952 err = got_object_tree_entry_dup(&new_te, te);
2953 if (err)
2954 goto done;
2955 err = match_modified_subtree(&modified, te,
2956 path_base_tree, commitable_paths);
2957 if (err)
2958 goto done;
2959 /* Avoid recursion into unmodified subtrees. */
2960 if (modified) {
2961 free(new_te->id);
2962 err = write_subtree(&new_te->id, te,
2963 path_base_tree, commitable_paths,
2964 status_cb, status_arg, repo);
2965 if (err)
2966 goto done;
2968 err = insert_tree_entry(new_te, &paths);
2969 if (err)
2970 goto done;
2971 continue;
2974 err = match_deleted_or_modified_ct(&ct, te,
2975 path_base_tree, commitable_paths);
2976 if (ct) {
2977 /* NB: Deleted entries get dropped here. */
2978 if (ct->status == GOT_STATUS_MODIFY) {
2979 err = alloc_modified_blob_tree_entry(
2980 &new_te, te, ct);
2981 if (err)
2982 goto done;
2983 err = insert_tree_entry(new_te, &paths);
2984 if (err)
2985 goto done;
2987 err = report_ct_status(ct, status_cb,
2988 status_arg);
2989 if (err)
2990 goto done;
2991 } else {
2992 /* Entry is unchanged; just copy it. */
2993 err = got_object_tree_entry_dup(&new_te, te);
2994 if (err)
2995 goto done;
2996 err = insert_tree_entry(new_te, &paths);
2997 if (err)
2998 goto done;
3003 /* Write new list of entries; deleted entries have been dropped. */
3004 TAILQ_FOREACH(pe, &paths, entry) {
3005 struct got_tree_entry *te = pe->data;
3006 new_tree_entries.nentries++;
3007 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3009 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3010 done:
3011 got_object_tree_entries_close(&new_tree_entries);
3012 got_pathlist_free(&paths);
3013 return err;
3016 static const struct got_error *
3017 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3018 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex,
3019 struct got_worktree *worktree)
3021 const struct got_error *err = NULL;
3022 struct got_pathlist_entry *pe;
3024 TAILQ_FOREACH(pe, commitable_paths, entry) {
3025 struct got_fileindex_entry *ie;
3026 struct got_commitable *ct = pe->data;
3028 ie = got_fileindex_entry_get(fileindex, pe->path);
3029 if (ie) {
3030 if (ct->status == GOT_STATUS_DELETE) {
3031 got_fileindex_entry_remove(fileindex, ie);
3032 got_fileindex_entry_free(ie);
3033 } else
3034 err = got_fileindex_entry_update(ie,
3035 ct->ondisk_path, ct->blob_id->sha1,
3036 new_base_commit_id->sha1, 1);
3037 } else {
3038 err = got_fileindex_entry_alloc(&ie,
3039 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3040 new_base_commit_id->sha1);
3041 if (err)
3042 break;
3043 err = got_fileindex_entry_add(fileindex, ie);
3044 if (err)
3045 break;
3048 return err;
3051 static const struct got_error *
3052 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3053 struct got_object_id *head_commit_id)
3055 const struct got_error *err = NULL;
3056 struct got_object_id *id_in_head = NULL, *id = NULL;
3057 struct got_commit_object *commit = NULL;
3058 char *path = NULL;
3059 const char *ct_path = ct->in_repo_path;
3061 while (ct_path[0] == '/')
3062 ct_path++;
3065 * Ensure that no modifications were made to files *and their parents*
3066 * in commits between the file's base commit and the branch head.
3068 * Checking the parents is important for detecting conflicting tree
3069 * configurations (files or parent folders might have been moved,
3070 * deleted, added again, etc.). Such changes need to be merged with
3071 * local changes before a commit can occur.
3073 * The implication is that the file's (parent) entry in the root
3074 * directory must have the same ID in all relevant commits.
3076 if (ct->status != GOT_STATUS_ADD) {
3077 struct got_object_qid *pid;
3078 char *slash;
3079 struct got_object_id *root_entry_id = NULL;
3081 /* Trivial case: base commit == head commit */
3082 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3083 return NULL;
3085 /* Compute the path to the root directory's entry. */
3086 path = strdup(ct_path);
3087 if (path == NULL) {
3088 err = got_error_from_errno("strdup");
3089 goto done;
3091 slash = strchr(path, '/');
3092 if (slash)
3093 *slash = '\0';
3095 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3096 if (err)
3097 goto done;
3099 err = got_object_id_by_path(&root_entry_id, repo,
3100 head_commit_id, path);
3101 if (err)
3102 goto done;
3104 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3105 while (pid) {
3106 struct got_commit_object *pcommit;
3108 err = got_object_id_by_path(&id, repo, pid->id, path);
3109 if (err) {
3110 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3111 goto done;
3112 err = NULL;
3113 break;
3116 err = got_object_id_by_path(&id, repo, pid->id, path);
3117 if (err)
3118 goto done;
3120 if (got_object_id_cmp(id, root_entry_id) != 0) {
3121 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3122 break;
3125 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3126 break; /* all relevant commits scanned */
3128 err = got_object_open_as_commit(&pcommit, repo,
3129 pid->id);
3130 if (err)
3131 goto done;
3133 got_object_commit_close(commit);
3134 commit = pcommit;
3135 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3136 commit));
3138 } else {
3139 /* Require that added files don't exist in the branch head. */
3140 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3141 ct_path);
3142 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3143 goto done;
3144 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3146 done:
3147 if (commit)
3148 got_object_commit_close(commit);
3149 free(id_in_head);
3150 free(id);
3151 free(path);
3152 return err;
3155 const struct got_error *
3156 got_worktree_commit(struct got_object_id **new_commit_id,
3157 struct got_worktree *worktree, const char *ondisk_path,
3158 const char *author, const char *committer,
3159 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3160 got_worktree_status_cb status_cb, void *status_arg,
3161 struct got_repository *repo)
3163 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3164 struct got_fileindex *fileindex = NULL;
3165 char *fileindex_path = NULL;
3166 struct collect_commitables_arg cc_arg;
3167 struct got_pathlist_head commitable_paths;
3168 struct got_pathlist_entry *pe;
3169 char *relpath = NULL;
3170 const char *head_ref_name = NULL;
3171 struct got_reference *head_ref = NULL;
3172 struct got_commit_object *head_commit = NULL;
3173 struct got_object_id *head_commit_id = NULL;
3174 struct got_reference *head_ref2 = NULL;
3175 struct got_object_id *head_commit_id2 = NULL;
3176 struct got_tree_object *head_tree = NULL;
3177 struct got_object_id *new_tree_id = NULL;
3178 struct got_object_id_queue parent_ids;
3179 struct got_object_qid *pid = NULL;
3180 char *logmsg = NULL;
3182 *new_commit_id = NULL;
3184 TAILQ_INIT(&commitable_paths);
3185 SIMPLEQ_INIT(&parent_ids);
3187 if (ondisk_path) {
3188 err = got_path_skip_common_ancestor(&relpath,
3189 worktree->root_path, ondisk_path);
3190 if (err)
3191 return err;
3194 err = lock_worktree(worktree, LOCK_EX);
3195 if (err)
3196 goto done;
3198 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3199 if (err)
3200 goto done;
3201 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3202 if (err)
3203 goto done;
3205 cc_arg.commitable_paths = &commitable_paths;
3206 cc_arg.worktree = worktree;
3207 cc_arg.repo = repo;
3208 err = got_worktree_status(worktree, relpath ? relpath : "",
3209 repo, collect_commitables, &cc_arg, NULL, NULL);
3210 if (err)
3211 goto done;
3213 if (TAILQ_EMPTY(&commitable_paths)) {
3214 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3215 goto done;
3218 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3219 if (err)
3220 goto done;
3222 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3223 struct got_commitable *ct = pe->data;
3224 err = check_ct_out_of_date(ct, repo, head_commit_id);
3225 if (err)
3226 goto done;
3229 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3230 if (err)
3231 goto done;
3233 if (commit_msg_cb != NULL) {
3234 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3235 if (err)
3236 goto done;
3239 if (logmsg == NULL || strlen(logmsg) == 0) {
3240 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3241 goto done;
3244 /* Create blobs from added and modified files and record their IDs. */
3245 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3246 struct got_commitable *ct = pe->data;
3247 char *ondisk_path;
3249 if (ct->status != GOT_STATUS_ADD &&
3250 ct->status != GOT_STATUS_MODIFY)
3251 continue;
3253 if (asprintf(&ondisk_path, "%s/%s",
3254 worktree->root_path, pe->path) == -1) {
3255 err = got_error_from_errno("asprintf");
3256 goto done;
3258 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3259 free(ondisk_path);
3260 if (err)
3261 goto done;
3264 /* Recursively write new tree objects. */
3265 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3266 status_cb, status_arg, repo);
3267 if (err)
3268 goto done;
3270 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3271 if (err)
3272 goto done;
3273 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3274 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3275 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3276 got_object_qid_free(pid);
3277 if (logmsg != NULL)
3278 free(logmsg);
3279 if (err)
3280 goto done;
3282 /* Check if a concurrent commit to our branch has occurred. */
3283 head_ref_name = got_worktree_get_head_ref_name(worktree);
3284 if (head_ref_name == NULL) {
3285 err = got_error_from_errno("got_worktree_get_head_ref_name");
3286 goto done;
3288 /* Lock the reference here to prevent concurrent modification. */
3289 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3290 if (err)
3291 goto done;
3292 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3293 if (err)
3294 goto done;
3295 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3296 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3297 goto done;
3299 /* Update branch head in repository. */
3300 err = got_ref_change_ref(head_ref2, *new_commit_id);
3301 if (err)
3302 goto done;
3303 err = got_ref_write(head_ref2, repo);
3304 if (err)
3305 goto done;
3307 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3308 if (err)
3309 goto done;
3311 err = ref_base_commit(worktree, repo);
3312 if (err)
3313 goto done;
3315 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3316 if (err)
3317 goto done;
3319 err = update_fileindex_after_commit(&commitable_paths,
3320 *new_commit_id, fileindex, worktree);
3321 if (err)
3322 goto done;
3323 done:
3324 if (fileindex) {
3325 sync_err = sync_fileindex(fileindex, fileindex_path);
3326 if (sync_err && err == NULL)
3327 err = sync_err;
3328 got_fileindex_free(fileindex);
3330 free(fileindex_path);
3331 unlockerr = lock_worktree(worktree, LOCK_SH);
3332 if (unlockerr && err == NULL)
3333 err = unlockerr;
3334 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3335 struct got_commitable *ct = pe->data;
3336 free_commitable(ct);
3338 got_pathlist_free(&commitable_paths);
3339 if (head_tree)
3340 got_object_tree_close(head_tree);
3341 if (head_commit)
3342 got_object_commit_close(head_commit);
3343 free(relpath);
3344 free(head_commit_id);
3345 free(head_commit_id2);
3346 if (head_ref)
3347 got_ref_close(head_ref);
3348 if (head_ref2) {
3349 unlockerr = got_ref_unlock(head_ref2);
3350 if (unlockerr && err == NULL)
3351 err = unlockerr;
3352 got_ref_close(head_ref2);
3354 return err;
3357 const char *
3358 got_commitable_get_path(struct got_commitable *ct)
3360 return ct->path;
3363 unsigned int
3364 got_commitable_get_status(struct got_commitable *ct)
3366 return ct->status;
3369 struct check_rebase_ok_arg {
3370 struct got_worktree *worktree;
3371 struct got_repository *repo;
3372 int rebase_in_progress;
3375 static const struct got_error *
3376 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3378 const struct got_error *err = NULL;
3379 struct check_rebase_ok_arg *a = arg;
3380 unsigned char status;
3381 struct stat sb;
3382 char *ondisk_path;
3384 if (!a->rebase_in_progress) {
3385 /* Reject rebase of a work tree with mixed base commits. */
3386 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3387 SHA1_DIGEST_LENGTH))
3388 return got_error(GOT_ERR_MIXED_COMMITS);
3391 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3392 == -1)
3393 return got_error_from_errno("asprintf");
3395 /* Reject rebase of a work tree with modified or conflicted files. */
3396 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3397 free(ondisk_path);
3398 if (err)
3399 return err;
3401 if (a->rebase_in_progress) {
3402 if (status == GOT_STATUS_CONFLICT)
3403 return got_error(GOT_ERR_CONFLICTS);
3404 } else if (status != GOT_STATUS_NO_CHANGE)
3405 return got_error(GOT_ERR_MODIFIED);
3407 return NULL;
3410 const struct got_error *
3411 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3412 struct got_reference **tmp_branch, struct got_worktree *worktree,
3413 struct got_reference *branch, struct got_repository *repo)
3415 const struct got_error *err = NULL;
3416 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3417 char *branch_ref_name = NULL;
3418 struct got_fileindex *fileindex = NULL;
3419 char *fileindex_path = NULL;
3420 struct check_rebase_ok_arg ok_arg;
3421 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3423 *new_base_branch_ref = NULL;
3424 *tmp_branch = NULL;
3426 err = lock_worktree(worktree, LOCK_EX);
3427 if (err)
3428 return err;
3430 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3431 if (err)
3432 goto done;
3434 ok_arg.worktree = worktree;
3435 ok_arg.repo = repo;
3436 ok_arg.rebase_in_progress = 0;
3437 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3438 &ok_arg);
3439 if (err)
3440 goto done;
3442 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3443 if (err)
3444 goto done;
3446 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3447 if (err)
3448 goto done;
3450 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3451 if (err)
3452 goto done;
3454 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3455 0);
3456 if (err)
3457 goto done;
3459 err = got_ref_alloc_symref(new_base_branch_ref,
3460 new_base_branch_ref_name, wt_branch);
3461 if (err)
3462 goto done;
3463 err = got_ref_write(*new_base_branch_ref, repo);
3464 if (err)
3465 goto done;
3467 /* TODO Lock original branch's ref while rebasing? */
3469 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3470 if (err)
3471 goto done;
3473 err = got_ref_write(branch_ref, repo);
3474 if (err)
3475 goto done;
3477 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3478 worktree->base_commit_id);
3479 if (err)
3480 goto done;
3481 err = got_ref_write(*tmp_branch, repo);
3482 if (err)
3483 goto done;
3485 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3486 if (err)
3487 goto done;
3488 done:
3489 free(fileindex_path);
3490 if (fileindex)
3491 got_fileindex_free(fileindex);
3492 free(tmp_branch_name);
3493 free(new_base_branch_ref_name);
3494 free(branch_ref_name);
3495 if (branch_ref)
3496 got_ref_close(branch_ref);
3497 if (wt_branch)
3498 got_ref_close(wt_branch);
3499 if (err) {
3500 if (*new_base_branch_ref) {
3501 got_ref_close(*new_base_branch_ref);
3502 *new_base_branch_ref = NULL;
3504 if (*tmp_branch) {
3505 got_ref_close(*tmp_branch);
3506 *tmp_branch = NULL;
3508 lock_worktree(worktree, LOCK_SH);
3510 return err;
3513 const struct got_error *
3514 got_worktree_rebase_continue(struct got_object_id **commit_id,
3515 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3516 struct got_reference **branch, struct got_worktree *worktree,
3517 struct got_repository *repo)
3519 const struct got_error *err;
3520 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3521 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3522 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3524 *commit_id = NULL;
3526 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3527 if (err)
3528 return err;
3530 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3531 if (err)
3532 goto done;
3534 err = get_rebase_commit_ref_name(&commit_ref_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 = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3543 if (err)
3544 goto done;
3546 err = got_ref_open(branch, repo,
3547 got_ref_get_symref_target(branch_ref), 0);
3548 if (err)
3549 goto done;
3551 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3552 if (err)
3553 goto done;
3555 err = got_ref_resolve(commit_id, repo, commit_ref);
3556 if (err)
3557 goto done;
3559 err = got_ref_open(new_base_branch, repo,
3560 new_base_branch_ref_name, 0);
3561 if (err)
3562 goto done;
3564 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3565 if (err)
3566 goto done;
3567 done:
3568 free(commit_ref_name);
3569 free(branch_ref_name);
3570 if (commit_ref)
3571 got_ref_close(commit_ref);
3572 if (branch_ref)
3573 got_ref_close(branch_ref);
3574 if (err) {
3575 free(*commit_id);
3576 *commit_id = NULL;
3577 if (*tmp_branch) {
3578 got_ref_close(*tmp_branch);
3579 *tmp_branch = NULL;
3581 if (*new_base_branch) {
3582 got_ref_close(*new_base_branch);
3583 *new_base_branch = NULL;
3585 if (*branch) {
3586 got_ref_close(*branch);
3587 *branch = NULL;
3590 return err;
3593 const struct got_error *
3594 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3596 const struct got_error *err;
3597 char *tmp_branch_name = NULL;
3599 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3600 if (err)
3601 return err;
3603 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3604 free(tmp_branch_name);
3605 return NULL;
3608 static const struct got_error *
3609 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3610 char **logmsg, void *arg)
3612 struct got_commit_object *commit = arg;
3614 *logmsg = strdup(got_object_commit_get_logmsg(commit));
3615 if (*logmsg == NULL)
3616 return got_error_from_errno("strdup");
3618 return NULL;
3621 static const struct got_error *
3622 rebase_status(void *arg, unsigned char status, const char *path,
3623 struct got_object_id *blob_id, struct got_object_id *commit_id)
3625 return NULL;
3628 const struct got_error *
3629 got_worktree_rebase_merge_files(struct got_worktree *worktree,
3630 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3631 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3632 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3634 const struct got_error *err;
3635 struct got_fileindex *fileindex;
3636 char *fileindex_path, *commit_ref_name = NULL;
3637 struct got_reference *commit_ref = NULL;
3639 /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3641 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3642 if (err)
3643 return err;
3645 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3646 if (err)
3647 goto done;
3648 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3649 if (err) {
3650 if (err->code != GOT_ERR_NOT_REF)
3651 goto done;
3652 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3653 if (err)
3654 goto done;
3655 err = got_ref_write(commit_ref, repo);
3656 if (err)
3657 goto done;
3658 } else {
3659 struct got_object_id *stored_id;
3660 int cmp;
3662 err = got_ref_resolve(&stored_id, repo, commit_ref);
3663 if (err)
3664 goto done;
3665 cmp = got_object_id_cmp(commit_id, stored_id);
3666 free(stored_id);
3667 if (cmp != 0) {
3668 err = got_error(GOT_ERR_REBASE_COMMITID);
3669 goto done;
3673 err = merge_files(worktree, fileindex, fileindex_path,
3674 parent_commit_id, commit_id, repo, progress_cb, progress_arg,
3675 cancel_cb, cancel_arg);
3676 done:
3677 got_fileindex_free(fileindex);
3678 free(fileindex_path);
3679 if (commit_ref)
3680 got_ref_close(commit_ref);
3681 return err;
3684 static const struct got_error *
3685 bump_all_base_commit_ids(struct got_worktree *worktree,
3686 struct got_object_id *base_commit_id)
3688 const struct got_error *err = NULL, *sync_err;
3689 struct got_fileindex *fileindex;
3690 char *fileindex_path;
3691 struct bump_base_commit_id_arg bbc_arg;
3693 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3694 if (err)
3695 return err;
3697 bbc_arg.base_commit_id = base_commit_id;
3698 bbc_arg.entry_name = NULL;
3699 bbc_arg.path = "";
3700 bbc_arg.path_len = 0;
3701 bbc_arg.progress_cb = NULL;
3702 bbc_arg.progress_arg = NULL;
3703 err = got_fileindex_for_each_entry_safe(fileindex,
3704 bump_base_commit_id, &bbc_arg);
3706 sync_err = sync_fileindex(fileindex, fileindex_path);
3707 if (sync_err && err == NULL)
3708 err = sync_err;
3710 got_fileindex_free(fileindex);
3711 free(fileindex_path);
3712 return err;
3715 const struct got_error *
3716 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3717 struct got_worktree *worktree, struct got_reference *tmp_branch,
3718 struct got_commit_object *orig_commit,
3719 struct got_object_id *orig_commit_id, struct got_repository *repo)
3721 const struct got_error *err;
3722 char *commit_ref_name = NULL;
3723 struct got_reference *commit_ref = NULL;
3724 struct got_object_id *commit_id = NULL;
3726 /* Work tree is locked/unlocked during rebase prepartion/teardown. */
3728 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3729 if (err)
3730 goto done;
3731 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3732 if (err)
3733 goto done;
3734 err = got_ref_resolve(&commit_id, repo, commit_ref);
3735 if (err)
3736 goto done;
3737 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3738 err = got_error(GOT_ERR_REBASE_COMMITID);
3739 goto done;
3742 err = got_worktree_commit(new_commit_id, worktree, NULL,
3743 got_object_commit_get_author(orig_commit),
3744 got_object_commit_get_committer(orig_commit),
3745 collect_rebase_commit_msg, orig_commit,
3746 rebase_status, NULL, repo);
3747 if (err) {
3748 if (err->code == GOT_ERR_COMMIT_NO_CHANGES) {
3749 /* No-op change; commit will be elided. */
3750 err = got_ref_delete(commit_ref, repo);
3751 if (err)
3752 goto done;
3753 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3755 goto done;
3758 /* Prevent out-of-date errors during rebase of subsequent commits. */
3759 err = bump_all_base_commit_ids(worktree, *new_commit_id);
3760 if (err)
3761 goto done;
3763 err = got_ref_delete(commit_ref, repo);
3764 if (err)
3765 goto done;
3767 err = got_ref_change_ref(tmp_branch, *new_commit_id);
3768 done:
3769 free(commit_ref_name);
3770 if (commit_ref)
3771 got_ref_close(commit_ref);
3772 if (err) {
3773 free(*new_commit_id);
3774 *new_commit_id = NULL;
3776 return err;
3779 const struct got_error *
3780 got_worktree_rebase_postpone(struct got_worktree *worktree)
3782 return lock_worktree(worktree, LOCK_SH);
3785 static const struct got_error *
3786 delete_ref(const char *name, struct got_repository *repo)
3788 const struct got_error *err;
3789 struct got_reference *ref;
3791 err = got_ref_open(&ref, repo, name, 0);
3792 if (err) {
3793 if (err->code == GOT_ERR_NOT_REF)
3794 return NULL;
3795 return err;
3798 err = got_ref_delete(ref, repo);
3799 got_ref_close(ref);
3800 return err;
3803 static const struct got_error *
3804 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
3806 const struct got_error *err;
3807 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3808 char *branch_ref_name = NULL, *commit_ref_name = NULL;
3810 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3811 if (err)
3812 goto done;
3813 err = delete_ref(tmp_branch_name, repo);
3814 if (err)
3815 goto done;
3817 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3818 if (err)
3819 goto done;
3820 err = delete_ref(new_base_branch_ref_name, repo);
3821 if (err)
3822 goto done;
3824 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3825 if (err)
3826 goto done;
3827 err = delete_ref(branch_ref_name, repo);
3828 if (err)
3829 goto done;
3831 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3832 if (err)
3833 goto done;
3834 err = delete_ref(commit_ref_name, repo);
3835 if (err)
3836 goto done;
3838 done:
3839 free(tmp_branch_name);
3840 free(new_base_branch_ref_name);
3841 free(branch_ref_name);
3842 free(commit_ref_name);
3843 return err;
3846 const struct got_error *
3847 got_worktree_rebase_complete(struct got_worktree *worktree,
3848 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3849 struct got_reference *rebased_branch,
3850 struct got_repository *repo)
3852 const struct got_error *err, *unlockerr;
3853 struct got_object_id *new_head_commit_id = NULL;
3855 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
3856 if (err)
3857 return err;
3859 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
3860 if (err)
3861 goto done;
3863 err = got_ref_write(rebased_branch, repo);
3864 if (err)
3865 goto done;
3867 err = got_worktree_set_head_ref(worktree, rebased_branch);
3868 if (err)
3869 goto done;
3871 err = delete_rebase_refs(worktree, repo);
3872 done:
3873 free(new_head_commit_id);
3874 unlockerr = lock_worktree(worktree, LOCK_SH);
3875 if (unlockerr && err == NULL)
3876 err = unlockerr;
3877 return err;
3880 struct collect_revertible_paths_arg {
3881 struct got_pathlist_head *revertible_paths;
3882 struct got_worktree *worktree;
3885 static const struct got_error *
3886 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
3887 struct got_object_id *blob_id, struct got_object_id *commit_id)
3889 struct collect_revertible_paths_arg *a = arg;
3890 const struct got_error *err = NULL;
3891 struct got_pathlist_entry *new = NULL;
3892 char *path = NULL;
3894 if (status != GOT_STATUS_ADD &&
3895 status != GOT_STATUS_DELETE &&
3896 status != GOT_STATUS_MODIFY &&
3897 status != GOT_STATUS_CONFLICT &&
3898 status != GOT_STATUS_MISSING)
3899 return NULL;
3901 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
3902 return got_error_from_errno("asprintf");
3904 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
3905 if (err || new == NULL)
3906 free(path);
3907 return err;
3910 const struct got_error *
3911 got_worktree_rebase_abort(struct got_worktree *worktree,
3912 struct got_repository *repo, struct got_reference *new_base_branch,
3913 got_worktree_checkout_cb progress_cb, void *progress_arg)
3915 const struct got_error *err, *unlockerr;
3916 struct got_reference *resolved = NULL;
3917 struct got_object_id *commit_id = NULL;
3918 struct got_fileindex *fileindex = NULL;
3919 char *fileindex_path = NULL;
3920 struct got_pathlist_head revertible_paths;
3921 struct got_pathlist_entry *pe;
3922 struct collect_revertible_paths_arg crp_arg;
3924 TAILQ_INIT(&revertible_paths);
3926 err = lock_worktree(worktree, LOCK_EX);
3927 if (err)
3928 return err;
3930 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3931 if (err)
3932 goto done;
3934 err = got_ref_open(&resolved, repo,
3935 got_ref_get_symref_target(new_base_branch), 0);
3936 if (err)
3937 goto done;
3939 err = got_worktree_set_head_ref(worktree, resolved);
3940 if (err)
3941 goto done;
3944 * XXX commits to the base branch could have happened while
3945 * we were busy rebasing; should we store the original commit ID
3946 * when rebase begins and read it back here?
3948 err = got_ref_resolve(&commit_id, repo, resolved);
3949 if (err)
3950 goto done;
3952 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
3953 if (err)
3954 goto done;
3956 err = got_worktree_checkout_files(worktree, "", repo, progress_cb,
3957 progress_arg, NULL, NULL);
3958 if (err)
3959 goto done;
3961 crp_arg.revertible_paths = &revertible_paths;
3962 crp_arg.worktree = worktree;
3963 err = got_worktree_status(worktree, "", repo,
3964 collect_revertible_paths, &crp_arg, NULL, NULL);
3965 if (err)
3966 goto done;
3968 TAILQ_FOREACH(pe, &revertible_paths, entry) {
3969 err = revert_file(worktree, fileindex, pe->path,
3970 progress_cb, progress_arg, repo);
3971 if (err)
3972 goto done;
3975 err = delete_rebase_refs(worktree, repo);
3976 done:
3977 got_ref_close(resolved);
3978 free(commit_id);
3979 if (fileindex)
3980 got_fileindex_free(fileindex);
3981 free(fileindex_path);
3982 TAILQ_FOREACH(pe, &revertible_paths, entry)
3983 free((char *)pe->path);
3984 got_pathlist_free(&revertible_paths);
3986 unlockerr = lock_worktree(worktree, LOCK_SH);
3987 if (unlockerr && err == NULL)
3988 err = unlockerr;
3989 return err;