Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/limits.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
22 #include <dirent.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sha1.h>
31 #include <zlib.h>
32 #include <fnmatch.h>
33 #include <libgen.h>
34 #include <uuid.h>
35 #include <util.h>
37 #include "got_error.h"
38 #include "got_repository.h"
39 #include "got_reference.h"
40 #include "got_object.h"
41 #include "got_path.h"
42 #include "got_worktree.h"
43 #include "got_opentemp.h"
44 #include "got_diff.h"
46 #include "got_lib_worktree.h"
47 #include "got_lib_sha1.h"
48 #include "got_lib_fileindex.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_delta.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_object_idset.h"
55 #include "got_lib_diff.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 static const struct got_error *
62 create_meta_file(const char *path_got, const char *name, const char *content)
63 {
64 const struct got_error *err = NULL;
65 char *path;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1)
68 return got_error_from_errno("asprintf");
70 err = got_path_create_file(path, content);
71 free(path);
72 return err;
73 }
75 static const struct got_error *
76 update_meta_file(const char *path_got, const char *name, const char *content)
77 {
78 const struct got_error *err = NULL;
79 FILE *tmpfile = NULL;
80 char *tmppath = NULL;
81 char *path = NULL;
83 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
84 err = got_error_from_errno("asprintf");
85 path = NULL;
86 goto done;
87 }
89 err = got_opentemp_named(&tmppath, &tmpfile, path);
90 if (err)
91 goto done;
93 if (content) {
94 int len = fprintf(tmpfile, "%s\n", content);
95 if (len != strlen(content) + 1) {
96 err = got_error_from_errno2("fprintf", tmppath);
97 goto done;
98 }
99 }
101 if (rename(tmppath, path) != 0) {
102 err = got_error_from_errno3("rename", tmppath, path);
103 unlink(tmppath);
104 goto done;
107 done:
108 if (fclose(tmpfile) != 0 && err == NULL)
109 err = got_error_from_errno2("fclose", tmppath);
110 free(tmppath);
111 return err;
114 static const struct got_error *
115 read_meta_file(char **content, const char *path_got, const char *name)
117 const struct got_error *err = NULL;
118 char *path;
119 int fd = -1;
120 ssize_t n;
121 struct stat sb;
123 *content = NULL;
125 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
126 err = got_error_from_errno("asprintf");
127 path = NULL;
128 goto done;
131 fd = open(path, O_RDONLY | O_NOFOLLOW);
132 if (fd == -1) {
133 if (errno == ENOENT)
134 err = got_error(GOT_ERR_WORKTREE_META);
135 else
136 err = got_error_from_errno2("open", path);
137 goto done;
139 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
140 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
141 : got_error_from_errno2("flock", path));
142 goto done;
145 if (lstat(path, &sb) != 0) {
146 err = got_error_from_errno2("lstat", path);
147 goto done;
149 *content = calloc(1, sb.st_size);
150 if (*content == NULL) {
151 err = got_error_from_errno("calloc");
152 goto done;
155 n = read(fd, *content, sb.st_size);
156 if (n != sb.st_size) {
157 err = (n == -1 ? got_error_from_errno2("read", path) :
158 got_error(GOT_ERR_WORKTREE_META));
159 goto done;
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error(GOT_ERR_WORKTREE_META);
163 goto done;
165 (*content)[sb.st_size - 1] = '\0';
167 done:
168 if (fd != -1 && close(fd) == -1 && err == NULL)
169 err = got_error_from_errno2("close", path_got);
170 free(path);
171 if (err) {
172 free(*content);
173 *content = NULL;
175 return err;
178 static const struct got_error *
179 write_head_ref(const char *path_got, struct got_reference *head_ref)
181 const struct got_error *err = NULL;
182 char *refstr = NULL;
184 if (got_ref_is_symbolic(head_ref)) {
185 refstr = got_ref_to_str(head_ref);
186 if (refstr == NULL)
187 return got_error_from_errno("got_ref_to_str");
188 } else {
189 refstr = strdup(got_ref_get_name(head_ref));
190 if (refstr == NULL)
191 return got_error_from_errno("strdup");
193 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
194 free(refstr);
195 return err;
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
204 uuid_t uuid;
205 uint32_t uuid_status;
206 int obj_type;
207 char *path_got = NULL;
208 char *formatstr = NULL;
209 char *absprefix = NULL;
210 char *basestr = NULL;
211 char *uuidstr = NULL;
213 if (strcmp(path, got_repo_get_path(repo)) == 0) {
214 err = got_error(GOT_ERR_WORKTREE_REPO);
215 goto done;
218 err = got_ref_resolve(&commit_id, repo, head_ref);
219 if (err)
220 return err;
221 err = got_object_get_type(&obj_type, repo, commit_id);
222 if (err)
223 return err;
224 if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 return got_error(GOT_ERR_OBJ_TYPE);
227 if (!got_path_is_absolute(prefix)) {
228 if (asprintf(&absprefix, "/%s", prefix) == -1)
229 return got_error_from_errno("asprintf");
232 /* Create top-level directory (may already exist). */
233 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
234 err = got_error_from_errno2("mkdir", path);
235 goto done;
238 /* Create .got directory (may already exist). */
239 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 err = got_error_from_errno("asprintf");
241 goto done;
243 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
244 err = got_error_from_errno2("mkdir", path_got);
245 goto done;
248 /* Create an empty lock file. */
249 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
250 if (err)
251 goto done;
253 /* Create an empty file index. */
254 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
255 if (err)
256 goto done;
258 /* Write the HEAD reference. */
259 err = write_head_ref(path_got, head_ref);
260 if (err)
261 goto done;
263 /* Record our base commit. */
264 err = got_object_id_str(&basestr, commit_id);
265 if (err)
266 goto done;
267 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
268 if (err)
269 goto done;
271 /* Store path to repository. */
272 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
273 got_repo_get_path(repo));
274 if (err)
275 goto done;
277 /* Store in-repository path prefix. */
278 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
279 absprefix ? absprefix : prefix);
280 if (err)
281 goto done;
283 /* Generate UUID. */
284 uuid_create(&uuid, &uuid_status);
285 if (uuid_status != uuid_s_ok) {
286 err = got_error_uuid(uuid_status);
287 goto done;
289 uuid_to_string(&uuid, &uuidstr, &uuid_status);
290 if (uuid_status != uuid_s_ok) {
291 err = got_error_uuid(uuid_status);
292 goto done;
294 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
295 if (err)
296 goto done;
298 /* Stamp work tree with format file. */
299 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
300 err = got_error_from_errno("asprintf");
301 goto done;
303 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
304 if (err)
305 goto done;
307 done:
308 free(commit_id);
309 free(path_got);
310 free(formatstr);
311 free(absprefix);
312 free(basestr);
313 free(uuidstr);
314 return err;
317 static const struct got_error *
318 open_worktree(struct got_worktree **worktree, const char *path)
320 const struct got_error *err = NULL;
321 char *path_got;
322 char *formatstr = NULL;
323 char *uuidstr = NULL;
324 char *path_lock = NULL;
325 char *base_commit_id_str = NULL;
326 int version, fd = -1;
327 const char *errstr;
328 struct got_repository *repo = NULL;
329 uint32_t uuid_status;
331 *worktree = NULL;
333 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
334 err = got_error_from_errno("asprintf");
335 path_got = NULL;
336 goto done;
339 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
340 err = got_error_from_errno("asprintf");
341 path_lock = NULL;
342 goto done;
345 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
346 if (fd == -1) {
347 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
348 : got_error_from_errno2("open", path_lock));
349 goto done;
352 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
353 if (err)
354 goto done;
356 version = strtonum(formatstr, 1, INT_MAX, &errstr);
357 if (errstr) {
358 err = got_error(GOT_ERR_WORKTREE_META);
359 goto done;
361 if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 err = got_error(GOT_ERR_WORKTREE_VERS);
363 goto done;
366 *worktree = calloc(1, sizeof(**worktree));
367 if (*worktree == NULL) {
368 err = got_error_from_errno("calloc");
369 goto done;
371 (*worktree)->lockfd = -1;
373 (*worktree)->root_path = strdup(path);
374 if ((*worktree)->root_path == NULL) {
375 err = got_error_from_errno("strdup");
376 goto done;
378 err = read_meta_file(&(*worktree)->repo_path, path_got,
379 GOT_WORKTREE_REPOSITORY);
380 if (err)
381 goto done;
383 err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 GOT_WORKTREE_PATH_PREFIX);
385 if (err)
386 goto done;
388 err = read_meta_file(&base_commit_id_str, path_got,
389 GOT_WORKTREE_BASE_COMMIT);
390 if (err)
391 goto done;
393 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
394 if (err)
395 goto done;
396 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 if (uuid_status != uuid_s_ok) {
398 err = got_error_uuid(uuid_status);
399 goto done;
402 err = got_repo_open(&repo, (*worktree)->repo_path);
403 if (err)
404 goto done;
406 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
407 base_commit_id_str);
408 if (err)
409 goto done;
411 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 GOT_WORKTREE_HEAD_REF);
413 done:
414 if (repo)
415 got_repo_close(repo);
416 free(path_got);
417 free(path_lock);
418 free(base_commit_id_str);
419 free(uuidstr);
420 free(formatstr);
421 if (err) {
422 if (fd != -1)
423 close(fd);
424 if (*worktree != NULL)
425 got_worktree_close(*worktree);
426 *worktree = NULL;
427 } else
428 (*worktree)->lockfd = fd;
430 return err;
433 const struct got_error *
434 got_worktree_open(struct got_worktree **worktree, const char *path)
436 const struct got_error *err = NULL;
438 do {
439 err = open_worktree(worktree, path);
440 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
441 return err;
442 if (*worktree)
443 return NULL;
444 path = dirname(path);
445 if (path == NULL)
446 return got_error_from_errno2("dirname", path);
447 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 return got_error(GOT_ERR_NOT_WORKTREE);
452 const struct got_error *
453 got_worktree_close(struct got_worktree *worktree)
455 const struct got_error *err = NULL;
456 free(worktree->root_path);
457 free(worktree->repo_path);
458 free(worktree->path_prefix);
459 free(worktree->base_commit_id);
460 free(worktree->head_ref_name);
461 if (worktree->lockfd != -1)
462 if (close(worktree->lockfd) != 0)
463 err = got_error_from_errno2("close",
464 got_worktree_get_root_path(worktree));
465 free(worktree);
466 return err;
469 const char *
470 got_worktree_get_root_path(struct got_worktree *worktree)
472 return worktree->root_path;
475 const char *
476 got_worktree_get_repo_path(struct got_worktree *worktree)
478 return worktree->repo_path;
481 const char *
482 got_worktree_get_path_prefix(struct got_worktree *worktree)
484 return worktree->path_prefix;
487 const struct got_error *
488 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 const char *path_prefix)
491 char *absprefix = NULL;
493 if (!got_path_is_absolute(path_prefix)) {
494 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 return got_error_from_errno("asprintf");
497 *match = (strcmp(absprefix ? absprefix : path_prefix,
498 worktree->path_prefix) == 0);
499 free(absprefix);
500 return NULL;
503 const char *
504 got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 return worktree->head_ref_name;
509 const struct got_error *
510 got_worktree_set_head_ref(struct got_worktree *worktree,
511 struct got_reference *head_ref)
513 const struct got_error *err = NULL;
514 char *path_got = NULL, *head_ref_name = NULL;
516 if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 GOT_WORKTREE_GOT_DIR) == -1) {
518 err = got_error_from_errno("asprintf");
519 path_got = NULL;
520 goto done;
523 head_ref_name = strdup(got_ref_get_name(head_ref));
524 if (head_ref_name == NULL) {
525 err = got_error_from_errno("strdup");
526 goto done;
529 err = write_head_ref(path_got, head_ref);
530 if (err)
531 goto done;
533 free(worktree->head_ref_name);
534 worktree->head_ref_name = head_ref_name;
535 done:
536 free(path_got);
537 if (err)
538 free(head_ref_name);
539 return err;
542 struct got_object_id *
543 got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 return worktree->base_commit_id;
548 const struct got_error *
549 got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 struct got_repository *repo, struct got_object_id *commit_id)
552 const struct got_error *err;
553 struct got_object *obj = NULL;
554 char *id_str = NULL;
555 char *path_got = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
560 path_got = NULL;
561 goto done;
564 err = got_object_open(&obj, repo, commit_id);
565 if (err)
566 return err;
568 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 err = got_error(GOT_ERR_OBJ_TYPE);
570 goto done;
573 /* Record our base commit. */
574 err = got_object_id_str(&id_str, commit_id);
575 if (err)
576 goto done;
577 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
578 if (err)
579 goto done;
581 free(worktree->base_commit_id);
582 worktree->base_commit_id = got_object_id_dup(commit_id);
583 if (worktree->base_commit_id == NULL) {
584 err = got_error_from_errno("got_object_id_dup");
585 goto done;
587 done:
588 if (obj)
589 got_object_close(obj);
590 free(id_str);
591 free(path_got);
592 return err;
595 static const struct got_error *
596 lock_worktree(struct got_worktree *worktree, int operation)
598 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 : got_error_from_errno2("flock",
601 got_worktree_get_root_path(worktree)));
602 return NULL;
605 static const struct got_error *
606 add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 const struct got_error *err = NULL;
609 char *abspath;
611 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 return got_error_from_errno("asprintf");
614 err = got_path_mkdir(abspath);
615 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
616 struct stat sb;
617 err = NULL;
618 if (lstat(abspath, &sb) == -1) {
619 err = got_error_from_errno2("lstat", abspath);
620 } else if (!S_ISDIR(sb.st_mode)) {
621 /* TODO directory is obstructed; do something */
622 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
625 free(abspath);
626 return err;
629 static const struct got_error *
630 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 const struct got_error *err = NULL;
633 uint8_t fbuf1[8192];
634 uint8_t fbuf2[8192];
635 size_t flen1 = 0, flen2 = 0;
637 *same = 1;
639 for (;;) {
640 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 if (flen1 == 0 && ferror(f1)) {
642 err = got_error_from_errno("fread");
643 break;
645 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 if (flen2 == 0 && ferror(f2)) {
647 err = got_error_from_errno("fread");
648 break;
650 if (flen1 == 0) {
651 if (flen2 != 0)
652 *same = 0;
653 break;
654 } else if (flen2 == 0) {
655 if (flen1 != 0)
656 *same = 0;
657 break;
658 } else if (flen1 == flen2) {
659 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
660 *same = 0;
661 break;
663 } else {
664 *same = 0;
665 break;
669 return err;
672 static const struct got_error *
673 check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 const struct got_error *err = NULL;
676 struct stat sb;
677 size_t size1, size2;
678 FILE *f1 = NULL, *f2 = NULL;
680 *same = 1;
682 if (lstat(f1_path, &sb) != 0) {
683 err = got_error_from_errno2("lstat", f1_path);
684 goto done;
686 size1 = sb.st_size;
688 if (lstat(f2_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f2_path);
690 goto done;
692 size2 = sb.st_size;
694 if (size1 != size2) {
695 *same = 0;
696 return NULL;
699 f1 = fopen(f1_path, "r");
700 if (f1 == NULL)
701 return got_error_from_errno2("open", f1_path);
703 f2 = fopen(f2_path, "r");
704 if (f2 == NULL) {
705 err = got_error_from_errno2("open", f2_path);
706 goto done;
709 err = check_file_contents_equal(same, f1, f2);
710 done:
711 if (f1 && fclose(f1) != 0 && err == NULL)
712 err = got_error_from_errno("fclose");
713 if (f2 && fclose(f2) != 0 && err == NULL)
714 err = got_error_from_errno("fclose");
716 return err;
719 /*
720 * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 * blob_deriv acts as the first derived version, and the file on disk
722 * acts as the second derived version.
723 */
724 static const struct got_error *
725 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 struct got_blob_object *blob_orig, const char *ondisk_path,
727 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 struct got_object_id *deriv_base_commit_id,
729 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
730 void *progress_arg)
732 const struct got_error *err = NULL;
733 int merged_fd = -1;
734 FILE *f_deriv = NULL, *f_orig = NULL;
735 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
736 char *merged_path = NULL, *base_path = NULL;
737 char *id_str = NULL;
738 char *label_deriv = NULL;
739 int overlapcnt = 0;
740 char *parent;
742 *local_changes_subsumed = 0;
744 parent = dirname(ondisk_path);
745 if (parent == NULL)
746 return got_error_from_errno2("dirname", ondisk_path);
748 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 return got_error_from_errno("asprintf");
751 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
752 if (err)
753 goto done;
755 free(base_path);
756 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
757 err = got_error_from_errno("asprintf");
758 base_path = NULL;
759 goto done;
762 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
763 if (err)
764 goto done;
765 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
766 blob_deriv);
767 if (err)
768 goto done;
770 free(base_path);
771 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
772 err = got_error_from_errno("asprintf");
773 base_path = NULL;
774 goto done;
777 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
778 if (err)
779 goto done;
780 if (blob_orig) {
781 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
782 blob_orig);
783 if (err)
784 goto done;
785 } else {
786 /*
787 * If the file has no blob, this is an "add vs add" conflict,
788 * and we simply use an empty ancestor file to make both files
789 * appear in the merged result in their entirety.
790 */
793 err = got_object_id_str(&id_str, deriv_base_commit_id);
794 if (err)
795 goto done;
796 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
797 err = got_error_from_errno("asprintf");
798 goto done;
801 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
802 blob_orig_path, ondisk_path, label_deriv, path);
803 if (err)
804 goto done;
806 err = (*progress_cb)(progress_arg,
807 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
808 if (err)
809 goto done;
811 if (fsync(merged_fd) != 0) {
812 err = got_error_from_errno("fsync");
813 goto done;
816 /* Check if a clean merge has subsumed all local changes. */
817 if (overlapcnt == 0) {
818 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
819 merged_path);
820 if (err)
821 goto done;
824 if (chmod(merged_path, st_mode) != 0) {
825 err = got_error_from_errno2("chmod", merged_path);
826 goto done;
829 if (rename(merged_path, ondisk_path) != 0) {
830 err = got_error_from_errno3("rename", merged_path,
831 ondisk_path);
832 unlink(merged_path);
833 goto done;
836 done:
837 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
838 err = got_error_from_errno("close");
839 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
840 err = got_error_from_errno("fclose");
841 if (f_orig && fclose(f_orig) != 0 && err == NULL)
842 err = got_error_from_errno("fclose");
843 free(merged_path);
844 free(base_path);
845 if (blob_deriv_path) {
846 unlink(blob_deriv_path);
847 free(blob_deriv_path);
849 if (blob_orig_path) {
850 unlink(blob_orig_path);
851 free(blob_orig_path);
853 free(id_str);
854 free(label_deriv);
855 return err;
858 static const struct got_error *
859 update_blob_fileindex_entry(struct got_worktree *worktree,
860 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
861 const char *ondisk_path, const char *path, struct got_blob_object *blob,
862 int update_timestamps)
864 const struct got_error *err = NULL;
866 if (ie == NULL)
867 ie = got_fileindex_entry_get(fileindex, path);
868 if (ie)
869 err = got_fileindex_entry_update(ie, ondisk_path,
870 blob->id.sha1, worktree->base_commit_id->sha1,
871 update_timestamps);
872 else {
873 struct got_fileindex_entry *new_ie;
874 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
875 path, blob->id.sha1, worktree->base_commit_id->sha1);
876 if (!err)
877 err = got_fileindex_entry_add(fileindex, new_ie);
879 return err;
882 static const struct got_error *
883 install_blob(struct got_worktree *worktree, const char *ondisk_path,
884 const char *path, uint16_t te_mode, uint16_t st_mode,
885 struct got_blob_object *blob, int restoring_missing_file,
886 int reverting_versioned_file, struct got_repository *repo,
887 got_worktree_checkout_cb progress_cb, void *progress_arg)
889 const struct got_error *err = NULL;
890 int fd = -1;
891 size_t len, hdrlen;
892 int update = 0;
893 char *tmppath = NULL;
895 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
896 GOT_DEFAULT_FILE_MODE);
897 if (fd == -1) {
898 if (errno == ENOENT) {
899 char *parent = dirname(path);
900 if (parent == NULL)
901 return got_error_from_errno2("dirname", path);
902 err = add_dir_on_disk(worktree, parent);
903 if (err)
904 return err;
905 fd = open(ondisk_path,
906 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
907 GOT_DEFAULT_FILE_MODE);
908 if (fd == -1)
909 return got_error_from_errno2("open",
910 ondisk_path);
911 } else if (errno == EEXIST) {
912 if (!S_ISREG(st_mode)) {
913 /* TODO file is obstructed; do something */
914 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
915 goto done;
916 } else {
917 err = got_opentemp_named_fd(&tmppath, &fd,
918 ondisk_path);
919 if (err)
920 goto done;
921 update = 1;
923 } else
924 return got_error_from_errno2("open", ondisk_path);
927 if (restoring_missing_file)
928 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
929 else if (reverting_versioned_file)
930 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
931 else
932 err = (*progress_cb)(progress_arg,
933 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
934 if (err)
935 goto done;
937 hdrlen = got_object_blob_get_hdrlen(blob);
938 do {
939 const uint8_t *buf = got_object_blob_get_read_buf(blob);
940 err = got_object_blob_read_block(&len, blob);
941 if (err)
942 break;
943 if (len > 0) {
944 /* Skip blob object header first time around. */
945 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
946 if (outlen == -1) {
947 err = got_error_from_errno("write");
948 goto done;
949 } else if (outlen != len - hdrlen) {
950 err = got_error(GOT_ERR_IO);
951 goto done;
953 hdrlen = 0;
955 } while (len != 0);
957 if (fsync(fd) != 0) {
958 err = got_error_from_errno("fsync");
959 goto done;
962 if (update) {
963 if (rename(tmppath, ondisk_path) != 0) {
964 err = got_error_from_errno3("rename", tmppath,
965 ondisk_path);
966 unlink(tmppath);
967 goto done;
971 if (te_mode & S_IXUSR) {
972 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
973 err = got_error_from_errno2("chmod", ondisk_path);
974 goto done;
976 } else {
977 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
978 err = got_error_from_errno2("chmod", ondisk_path);
979 goto done;
983 done:
984 if (fd != -1 && close(fd) != 0 && err == NULL)
985 err = got_error_from_errno("close");
986 free(tmppath);
987 return err;
990 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
991 static const struct got_error *
992 get_modified_file_content_status(unsigned char *status, FILE *f)
994 const struct got_error *err = NULL;
995 const char *markers[3] = {
996 GOT_DIFF_CONFLICT_MARKER_BEGIN,
997 GOT_DIFF_CONFLICT_MARKER_SEP,
998 GOT_DIFF_CONFLICT_MARKER_END
999 };
1000 int i = 0;
1001 char *line;
1002 size_t len;
1003 const char delim[3] = {'\0', '\0', '\0'};
1005 while (*status == GOT_STATUS_MODIFY) {
1006 line = fparseln(f, &len, NULL, delim, 0);
1007 if (line == NULL) {
1008 if (feof(f))
1009 break;
1010 err = got_ferror(f, GOT_ERR_IO);
1011 break;
1014 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1015 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1016 == 0)
1017 *status = GOT_STATUS_CONFLICT;
1018 else
1019 i++;
1023 return err;
1026 static int
1027 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1029 return !(ie->ctime_sec == sb->st_ctime &&
1030 ie->ctime_nsec == sb->st_ctimensec &&
1031 ie->mtime_sec == sb->st_mtime &&
1032 ie->mtime_nsec == sb->st_mtimensec &&
1033 ie->size == (sb->st_size & 0xffffffff));
1036 static const struct got_error *
1037 get_file_status(unsigned char *status, struct stat *sb,
1038 struct got_fileindex_entry *ie, const char *abspath,
1039 struct got_repository *repo)
1041 const struct got_error *err = NULL;
1042 struct got_object_id id;
1043 size_t hdrlen;
1044 FILE *f = NULL;
1045 uint8_t fbuf[8192];
1046 struct got_blob_object *blob = NULL;
1047 size_t flen, blen;
1049 *status = GOT_STATUS_NO_CHANGE;
1051 if (lstat(abspath, sb) == -1) {
1052 if (errno == ENOENT) {
1053 if (got_fileindex_entry_has_file_on_disk(ie))
1054 *status = GOT_STATUS_MISSING;
1055 else
1056 *status = GOT_STATUS_DELETE;
1057 return NULL;
1059 return got_error_from_errno2("lstat", abspath);
1062 if (!S_ISREG(sb->st_mode)) {
1063 *status = GOT_STATUS_OBSTRUCTED;
1064 return NULL;
1067 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1068 *status = GOT_STATUS_DELETE;
1069 return NULL;
1070 } else if (!got_fileindex_entry_has_blob(ie)) {
1071 *status = GOT_STATUS_ADD;
1072 return NULL;
1075 if (!stat_info_differs(ie, sb))
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;
1134 * Update timestamps in the file index if a file is unmodified and
1135 * we had to run a full content comparison to find out.
1137 static const struct got_error *
1138 sync_timestamps(char *ondisk_path, unsigned char status,
1139 struct got_fileindex_entry *ie, struct stat *sb)
1141 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1142 return got_fileindex_entry_update(ie, ondisk_path,
1143 ie->blob_sha1, ie->commit_sha1, 1);
1145 return NULL;
1148 static const struct got_error *
1149 update_blob(struct got_worktree *worktree,
1150 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1151 struct got_tree_entry *te, const char *path,
1152 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1153 void *progress_arg)
1155 const struct got_error *err = NULL;
1156 struct got_blob_object *blob = NULL;
1157 char *ondisk_path;
1158 unsigned char status = GOT_STATUS_NO_CHANGE;
1159 struct stat sb;
1161 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1162 return got_error_from_errno("asprintf");
1164 if (ie) {
1165 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1166 if (err)
1167 goto done;
1168 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1169 sb.st_mode = got_fileindex_perms_to_st(ie);
1170 } else
1171 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1173 if (status == GOT_STATUS_OBSTRUCTED) {
1174 err = (*progress_cb)(progress_arg, status, path);
1175 goto done;
1178 if (ie && status != GOT_STATUS_MISSING) {
1179 if (got_fileindex_entry_has_commit(ie) &&
1180 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1181 SHA1_DIGEST_LENGTH) == 0) {
1182 err = sync_timestamps(ondisk_path, status, ie, &sb);
1183 if (err)
1184 goto done;
1185 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1186 path);
1187 goto done;
1189 if (got_fileindex_entry_has_blob(ie) &&
1190 memcmp(ie->blob_sha1, te->id->sha1,
1191 SHA1_DIGEST_LENGTH) == 0) {
1192 err = sync_timestamps(ondisk_path, status, ie, &sb);
1193 goto done;
1197 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1198 if (err)
1199 goto done;
1201 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1202 int update_timestamps;
1203 struct got_blob_object *blob2 = NULL;
1204 if (got_fileindex_entry_has_blob(ie)) {
1205 struct got_object_id id2;
1206 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1207 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1208 if (err)
1209 goto done;
1211 err = merge_blob(&update_timestamps, worktree, blob2,
1212 ondisk_path, path, sb.st_mode, blob,
1213 worktree->base_commit_id, repo,
1214 progress_cb, progress_arg);
1215 if (blob2)
1216 got_object_blob_close(blob2);
1218 * Do not update timestamps of files with local changes.
1219 * Otherwise, a future status walk would treat them as
1220 * unmodified files again.
1222 err = got_fileindex_entry_update(ie, ondisk_path,
1223 blob->id.sha1, worktree->base_commit_id->sha1,
1224 update_timestamps);
1225 } else if (status == GOT_STATUS_DELETE) {
1226 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1227 if (err)
1228 goto done;
1229 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1230 ondisk_path, path, blob, 0);
1231 if (err)
1232 goto done;
1233 } else {
1234 err = install_blob(worktree, ondisk_path, path, te->mode,
1235 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1236 repo, progress_cb, progress_arg);
1237 if (err)
1238 goto done;
1239 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1240 ondisk_path, path, blob, 1);
1241 if (err)
1242 goto done;
1244 got_object_blob_close(blob);
1245 done:
1246 free(ondisk_path);
1247 return err;
1250 static const struct got_error *
1251 remove_ondisk_file(const char *root_path, const char *path)
1253 const struct got_error *err = NULL;
1254 char *ondisk_path = NULL;
1256 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1257 return got_error_from_errno("asprintf");
1259 if (unlink(ondisk_path) == -1) {
1260 if (errno != ENOENT)
1261 err = got_error_from_errno2("unlink", ondisk_path);
1262 } else {
1263 char *parent = dirname(ondisk_path);
1264 while (parent && strcmp(parent, root_path) != 0) {
1265 if (rmdir(parent) == -1) {
1266 if (errno != ENOTEMPTY)
1267 err = got_error_from_errno2("rmdir",
1268 parent);
1269 break;
1271 parent = dirname(parent);
1274 free(ondisk_path);
1275 return err;
1278 static const struct got_error *
1279 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1280 struct got_fileindex_entry *ie, struct got_repository *repo,
1281 got_worktree_checkout_cb progress_cb, void *progress_arg)
1283 const struct got_error *err = NULL;
1284 unsigned char status;
1285 struct stat sb;
1286 char *ondisk_path;
1288 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1289 == -1)
1290 return got_error_from_errno("asprintf");
1292 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1293 if (err)
1294 return err;
1296 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1297 status == GOT_STATUS_ADD) {
1298 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1299 if (err)
1300 return err;
1302 * Preserve the working file and change the deleted blob's
1303 * entry into a schedule-add entry.
1305 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1306 0);
1307 if (err)
1308 return err;
1309 } else {
1310 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1311 if (err)
1312 return err;
1313 if (status == GOT_STATUS_NO_CHANGE) {
1314 err = remove_ondisk_file(worktree->root_path, ie->path);
1315 if (err)
1316 return err;
1318 got_fileindex_entry_remove(fileindex, ie);
1321 return err;
1324 struct diff_cb_arg {
1325 struct got_fileindex *fileindex;
1326 struct got_worktree *worktree;
1327 struct got_repository *repo;
1328 got_worktree_checkout_cb progress_cb;
1329 void *progress_arg;
1330 got_worktree_cancel_cb cancel_cb;
1331 void *cancel_arg;
1334 static const struct got_error *
1335 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1336 struct got_tree_entry *te, const char *parent_path)
1338 struct diff_cb_arg *a = arg;
1340 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1341 return got_error(GOT_ERR_CANCELLED);
1343 return update_blob(a->worktree, a->fileindex, ie, te,
1344 ie->path, a->repo, a->progress_cb, a->progress_arg);
1347 static const struct got_error *
1348 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1350 struct diff_cb_arg *a = arg;
1352 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1353 return got_error(GOT_ERR_CANCELLED);
1355 return delete_blob(a->worktree, a->fileindex, ie,
1356 a->repo, a->progress_cb, a->progress_arg);
1359 static const struct got_error *
1360 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1362 struct diff_cb_arg *a = arg;
1363 const struct got_error *err;
1364 char *path;
1366 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1367 return got_error(GOT_ERR_CANCELLED);
1369 if (asprintf(&path, "%s%s%s", parent_path,
1370 parent_path[0] ? "/" : "", te->name)
1371 == -1)
1372 return got_error_from_errno("asprintf");
1374 if (S_ISDIR(te->mode))
1375 err = add_dir_on_disk(a->worktree, path);
1376 else
1377 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1378 a->repo, a->progress_cb, a->progress_arg);
1380 free(path);
1381 return err;
1384 static const struct got_error *
1385 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1387 const struct got_error *err = NULL;
1388 char *uuidstr = NULL;
1389 uint32_t uuid_status;
1391 *refname = NULL;
1393 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1394 if (uuid_status != uuid_s_ok)
1395 return got_error_uuid(uuid_status);
1397 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1398 == -1) {
1399 err = got_error_from_errno("asprintf");
1400 *refname = NULL;
1402 free(uuidstr);
1403 return err;
1406 const struct got_error *
1407 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1409 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1412 static const struct got_error *
1413 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1415 return get_ref_name(refname, worktree,
1416 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1419 static const struct got_error *
1420 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1422 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1425 static const struct got_error *
1426 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1428 return get_ref_name(refname, worktree,
1429 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1432 static const struct got_error *
1433 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1435 return get_ref_name(refname, worktree,
1436 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1439 static const struct got_error *
1440 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1442 return get_ref_name(refname, worktree,
1443 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1446 static const struct got_error *
1447 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1449 return get_ref_name(refname, worktree,
1450 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1453 static const struct got_error *
1454 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1456 return get_ref_name(refname, worktree,
1457 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1460 static const struct got_error *
1461 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1463 return get_ref_name(refname, worktree,
1464 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1467 const struct got_error *
1468 got_worktree_get_histedit_list_path(char **path, struct got_worktree *worktree)
1470 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1471 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_LIST) == -1) {
1472 *path = NULL;
1473 return got_error_from_errno("asprintf");
1475 return NULL;
1479 * Prevent Git's garbage collector from deleting our base commit by
1480 * setting a reference to our base commit's ID.
1482 static const struct got_error *
1483 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1485 const struct got_error *err = NULL;
1486 struct got_reference *ref = NULL;
1487 char *refname;
1489 err = got_worktree_get_base_ref_name(&refname, worktree);
1490 if (err)
1491 return err;
1493 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1494 if (err)
1495 goto done;
1497 err = got_ref_write(ref, repo);
1498 done:
1499 free(refname);
1500 if (ref)
1501 got_ref_close(ref);
1502 return err;
1505 static const struct got_error *
1506 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1508 const struct got_error *err = NULL;
1510 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1511 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1512 err = got_error_from_errno("asprintf");
1513 *fileindex_path = NULL;
1515 return err;
1519 static const struct got_error *
1520 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1521 struct got_worktree *worktree)
1523 const struct got_error *err = NULL;
1524 FILE *index = NULL;
1526 *fileindex_path = NULL;
1527 *fileindex = got_fileindex_alloc();
1528 if (*fileindex == NULL)
1529 return got_error_from_errno("got_fileindex_alloc");
1531 err = get_fileindex_path(fileindex_path, worktree);
1532 if (err)
1533 goto done;
1535 index = fopen(*fileindex_path, "rb");
1536 if (index == NULL) {
1537 if (errno != ENOENT)
1538 err = got_error_from_errno2("fopen", *fileindex_path);
1539 } else {
1540 err = got_fileindex_read(*fileindex, index);
1541 if (fclose(index) != 0 && err == NULL)
1542 err = got_error_from_errno("fclose");
1544 done:
1545 if (err) {
1546 free(*fileindex_path);
1547 *fileindex_path = NULL;
1548 got_fileindex_free(*fileindex);
1549 *fileindex = NULL;
1551 return err;
1554 struct bump_base_commit_id_arg {
1555 struct got_object_id *base_commit_id;
1556 const char *path;
1557 size_t path_len;
1558 const char *entry_name;
1559 got_worktree_checkout_cb progress_cb;
1560 void *progress_arg;
1563 /* Bump base commit ID of all files within an updated part of the work tree. */
1564 static const struct got_error *
1565 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1567 const struct got_error *err;
1568 struct bump_base_commit_id_arg *a = arg;
1570 if (a->entry_name) {
1571 if (strcmp(ie->path, a->path) != 0)
1572 return NULL;
1573 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1574 return NULL;
1576 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1577 SHA1_DIGEST_LENGTH) == 0)
1578 return NULL;
1580 if (a->progress_cb) {
1581 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1582 ie->path);
1583 if (err)
1584 return err;
1586 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1587 return NULL;
1590 static const struct got_error *
1591 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1593 const struct got_error *err = NULL;
1594 char *new_fileindex_path = NULL;
1595 FILE *new_index = NULL;
1597 err = got_opentemp_named(&new_fileindex_path, &new_index,
1598 fileindex_path);
1599 if (err)
1600 goto done;
1602 err = got_fileindex_write(fileindex, new_index);
1603 if (err)
1604 goto done;
1606 if (rename(new_fileindex_path, fileindex_path) != 0) {
1607 err = got_error_from_errno3("rename", new_fileindex_path,
1608 fileindex_path);
1609 unlink(new_fileindex_path);
1611 done:
1612 if (new_index)
1613 fclose(new_index);
1614 free(new_fileindex_path);
1615 return err;
1618 static const struct got_error *
1619 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1620 struct got_object_id **tree_id, const char *wt_relpath,
1621 struct got_worktree *worktree, struct got_repository *repo)
1623 const struct got_error *err = NULL;
1624 struct got_object_id *id = NULL;
1625 char *in_repo_path = NULL;
1626 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1628 *entry_type = GOT_OBJ_TYPE_ANY;
1629 *tree_relpath = NULL;
1630 *tree_id = NULL;
1632 if (wt_relpath[0] == '\0') {
1633 /* Check out all files within the work tree. */
1634 *entry_type = GOT_OBJ_TYPE_TREE;
1635 *tree_relpath = strdup("");
1636 if (*tree_relpath == NULL) {
1637 err = got_error_from_errno("strdup");
1638 goto done;
1640 err = got_object_id_by_path(tree_id, repo,
1641 worktree->base_commit_id, worktree->path_prefix);
1642 if (err)
1643 goto done;
1644 return NULL;
1647 /* Check out a subset of files in the work tree. */
1649 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1650 is_root_wt ? "" : "/", wt_relpath) == -1) {
1651 err = got_error_from_errno("asprintf");
1652 goto done;
1655 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1656 in_repo_path);
1657 if (err)
1658 goto done;
1660 free(in_repo_path);
1661 in_repo_path = NULL;
1663 err = got_object_get_type(entry_type, repo, id);
1664 if (err)
1665 goto done;
1667 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1668 /* Check out a single file. */
1669 if (strchr(wt_relpath, '/') == NULL) {
1670 /* Check out a single file in work tree's root dir. */
1671 in_repo_path = strdup(worktree->path_prefix);
1672 if (in_repo_path == NULL) {
1673 err = got_error_from_errno("strdup");
1674 goto done;
1676 *tree_relpath = strdup("");
1677 if (*tree_relpath == NULL) {
1678 err = got_error_from_errno("strdup");
1679 goto done;
1681 } else {
1682 /* Check out a single file in a subdirectory. */
1683 err = got_path_dirname(tree_relpath, wt_relpath);
1684 if (err)
1685 return err;
1686 if (asprintf(&in_repo_path, "%s%s%s",
1687 worktree->path_prefix, is_root_wt ? "" : "/",
1688 *tree_relpath) == -1) {
1689 err = got_error_from_errno("asprintf");
1690 goto done;
1693 err = got_object_id_by_path(tree_id, repo,
1694 worktree->base_commit_id, in_repo_path);
1695 } else {
1696 /* Check out all files within a subdirectory. */
1697 *tree_id = got_object_id_dup(id);
1698 if (*tree_id == NULL) {
1699 err = got_error_from_errno("got_object_id_dup");
1700 goto done;
1702 *tree_relpath = strdup(wt_relpath);
1703 if (*tree_relpath == NULL) {
1704 err = got_error_from_errno("strdup");
1705 goto done;
1708 done:
1709 free(id);
1710 free(in_repo_path);
1711 if (err) {
1712 *entry_type = GOT_OBJ_TYPE_ANY;
1713 free(*tree_relpath);
1714 *tree_relpath = NULL;
1715 free(*tree_id);
1716 *tree_id = NULL;
1718 return err;
1721 static const struct got_error *
1722 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1723 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1724 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1725 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1727 const struct got_error *err = NULL;
1728 struct got_commit_object *commit = NULL;
1729 struct got_tree_object *tree = NULL;
1730 struct got_fileindex_diff_tree_cb diff_cb;
1731 struct diff_cb_arg arg;
1733 err = ref_base_commit(worktree, repo);
1734 if (err)
1735 goto done;
1737 err = got_object_open_as_commit(&commit, repo,
1738 worktree->base_commit_id);
1739 if (err)
1740 goto done;
1742 err = got_object_open_as_tree(&tree, repo, tree_id);
1743 if (err)
1744 goto done;
1746 if (entry_name &&
1747 got_object_tree_find_entry(tree, entry_name) == NULL) {
1748 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1749 goto done;
1752 diff_cb.diff_old_new = diff_old_new;
1753 diff_cb.diff_old = diff_old;
1754 diff_cb.diff_new = diff_new;
1755 arg.fileindex = fileindex;
1756 arg.worktree = worktree;
1757 arg.repo = repo;
1758 arg.progress_cb = progress_cb;
1759 arg.progress_arg = progress_arg;
1760 arg.cancel_cb = cancel_cb;
1761 arg.cancel_arg = cancel_arg;
1762 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1763 entry_name, repo, &diff_cb, &arg);
1764 done:
1765 if (tree)
1766 got_object_tree_close(tree);
1767 if (commit)
1768 got_object_commit_close(commit);
1769 return err;
1772 const struct got_error *
1773 got_worktree_checkout_files(struct got_worktree *worktree,
1774 struct got_pathlist_head *paths, struct got_repository *repo,
1775 got_worktree_checkout_cb progress_cb, void *progress_arg,
1776 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1778 const struct got_error *err = NULL, *sync_err, *unlockerr;
1779 struct got_commit_object *commit = NULL;
1780 struct got_tree_object *tree = NULL;
1781 struct got_fileindex *fileindex = NULL;
1782 char *fileindex_path = NULL;
1783 struct got_pathlist_entry *pe;
1784 struct tree_path_data {
1785 SIMPLEQ_ENTRY(tree_path_data) entry;
1786 struct got_object_id *tree_id;
1787 int entry_type;
1788 char *relpath;
1789 char *entry_name;
1790 } *tpd = NULL;
1791 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1793 SIMPLEQ_INIT(&tree_paths);
1795 err = lock_worktree(worktree, LOCK_EX);
1796 if (err)
1797 return err;
1799 /* Map all specified paths to in-repository trees. */
1800 TAILQ_FOREACH(pe, paths, entry) {
1801 tpd = malloc(sizeof(*tpd));
1802 if (tpd == NULL) {
1803 err = got_error_from_errno("malloc");
1804 goto done;
1807 err = find_tree_entry_for_checkout(&tpd->entry_type,
1808 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1809 if (err) {
1810 free(tpd);
1811 goto done;
1814 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1815 err = got_path_basename(&tpd->entry_name, pe->path);
1816 if (err) {
1817 free(tpd->relpath);
1818 free(tpd->tree_id);
1819 free(tpd);
1820 goto done;
1822 } else
1823 tpd->entry_name = NULL;
1825 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1829 * Read the file index.
1830 * Checking out files is supposed to be an idempotent operation.
1831 * If the on-disk file index is incomplete we will try to complete it.
1833 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1834 if (err)
1835 goto done;
1837 tpd = SIMPLEQ_FIRST(&tree_paths);
1838 TAILQ_FOREACH(pe, paths, entry) {
1839 struct bump_base_commit_id_arg bbc_arg;
1841 err = checkout_files(worktree, fileindex, tpd->relpath,
1842 tpd->tree_id, tpd->entry_name, repo,
1843 progress_cb, progress_arg, cancel_cb, cancel_arg);
1844 if (err)
1845 break;
1847 bbc_arg.base_commit_id = worktree->base_commit_id;
1848 bbc_arg.entry_name = tpd->entry_name;
1849 bbc_arg.path = pe->path;
1850 bbc_arg.path_len = strlen(pe->path);
1851 bbc_arg.progress_cb = progress_cb;
1852 bbc_arg.progress_arg = progress_arg;
1853 err = got_fileindex_for_each_entry_safe(fileindex,
1854 bump_base_commit_id, &bbc_arg);
1855 if (err)
1856 break;
1858 tpd = SIMPLEQ_NEXT(tpd, entry);
1860 sync_err = sync_fileindex(fileindex, fileindex_path);
1861 if (sync_err && err == NULL)
1862 err = sync_err;
1863 done:
1864 free(fileindex_path);
1865 if (tree)
1866 got_object_tree_close(tree);
1867 if (commit)
1868 got_object_commit_close(commit);
1869 if (fileindex)
1870 got_fileindex_free(fileindex);
1871 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1872 tpd = SIMPLEQ_FIRST(&tree_paths);
1873 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1874 free(tpd->relpath);
1875 free(tpd->tree_id);
1876 free(tpd);
1878 unlockerr = lock_worktree(worktree, LOCK_SH);
1879 if (unlockerr && err == NULL)
1880 err = unlockerr;
1881 return err;
1884 struct merge_file_cb_arg {
1885 struct got_worktree *worktree;
1886 struct got_fileindex *fileindex;
1887 got_worktree_checkout_cb progress_cb;
1888 void *progress_arg;
1889 got_worktree_cancel_cb cancel_cb;
1890 void *cancel_arg;
1891 struct got_object_id *commit_id2;
1894 static const struct got_error *
1895 merge_file_cb(void *arg, struct got_blob_object *blob1,
1896 struct got_blob_object *blob2, struct got_object_id *id1,
1897 struct got_object_id *id2, const char *path1, const char *path2,
1898 struct got_repository *repo)
1900 static const struct got_error *err = NULL;
1901 struct merge_file_cb_arg *a = arg;
1902 struct got_fileindex_entry *ie;
1903 char *ondisk_path = NULL;
1904 struct stat sb;
1905 unsigned char status;
1906 int local_changes_subsumed;
1908 if (blob1 && blob2) {
1909 ie = got_fileindex_entry_get(a->fileindex, path2);
1910 if (ie == NULL)
1911 return (*a->progress_cb)(a->progress_arg,
1912 GOT_STATUS_MISSING, path2);
1914 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1915 path2) == -1)
1916 return got_error_from_errno("asprintf");
1918 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1919 if (err)
1920 goto done;
1922 if (status == GOT_STATUS_DELETE) {
1923 err = (*a->progress_cb)(a->progress_arg,
1924 GOT_STATUS_MERGE, path2);
1925 goto done;
1927 if (status != GOT_STATUS_NO_CHANGE &&
1928 status != GOT_STATUS_MODIFY &&
1929 status != GOT_STATUS_CONFLICT &&
1930 status != GOT_STATUS_ADD) {
1931 err = (*a->progress_cb)(a->progress_arg, status, path2);
1932 goto done;
1935 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1936 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1937 a->progress_cb, a->progress_arg);
1938 } else if (blob1) {
1939 ie = got_fileindex_entry_get(a->fileindex, path1);
1940 if (ie == NULL)
1941 return (*a->progress_cb)(a->progress_arg,
1942 GOT_STATUS_MISSING, path2);
1944 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1945 path1) == -1)
1946 return got_error_from_errno("asprintf");
1948 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1949 if (err)
1950 goto done;
1952 switch (status) {
1953 case GOT_STATUS_NO_CHANGE:
1954 err = (*a->progress_cb)(a->progress_arg,
1955 GOT_STATUS_DELETE, path1);
1956 if (err)
1957 goto done;
1958 err = remove_ondisk_file(a->worktree->root_path, path1);
1959 if (err)
1960 goto done;
1961 if (ie)
1962 got_fileindex_entry_mark_deleted_from_disk(ie);
1963 break;
1964 case GOT_STATUS_DELETE:
1965 case GOT_STATUS_MISSING:
1966 err = (*a->progress_cb)(a->progress_arg,
1967 GOT_STATUS_DELETE, path1);
1968 if (err)
1969 goto done;
1970 if (ie)
1971 got_fileindex_entry_mark_deleted_from_disk(ie);
1972 break;
1973 case GOT_STATUS_ADD:
1974 case GOT_STATUS_MODIFY:
1975 case GOT_STATUS_CONFLICT:
1976 err = (*a->progress_cb)(a->progress_arg,
1977 GOT_STATUS_CANNOT_DELETE, path1);
1978 if (err)
1979 goto done;
1980 break;
1981 case GOT_STATUS_OBSTRUCTED:
1982 err = (*a->progress_cb)(a->progress_arg, status, path1);
1983 if (err)
1984 goto done;
1985 break;
1986 default:
1987 break;
1989 } else if (blob2) {
1990 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1991 path2) == -1)
1992 return got_error_from_errno("asprintf");
1993 ie = got_fileindex_entry_get(a->fileindex, path2);
1994 if (ie) {
1995 err = get_file_status(&status, &sb, ie, ondisk_path,
1996 repo);
1997 if (err)
1998 goto done;
1999 if (status != GOT_STATUS_NO_CHANGE &&
2000 status != GOT_STATUS_MODIFY &&
2001 status != GOT_STATUS_CONFLICT &&
2002 status != GOT_STATUS_ADD) {
2003 err = (*a->progress_cb)(a->progress_arg,
2004 status, path2);
2005 goto done;
2007 err = merge_blob(&local_changes_subsumed, a->worktree,
2008 NULL, ondisk_path, path2, sb.st_mode, blob2,
2009 a->commit_id2, repo,
2010 a->progress_cb, a->progress_arg);
2011 if (status == GOT_STATUS_DELETE) {
2012 err = update_blob_fileindex_entry(a->worktree,
2013 a->fileindex, ie, ondisk_path, ie->path,
2014 blob2, 0);
2015 if (err)
2016 goto done;
2018 } else {
2019 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2020 err = install_blob(a->worktree, ondisk_path, path2,
2021 /* XXX get this from parent tree! */
2022 GOT_DEFAULT_FILE_MODE,
2023 sb.st_mode, blob2, 0, 0, repo,
2024 a->progress_cb, a->progress_arg);
2025 if (err)
2026 goto done;
2027 err = got_fileindex_entry_alloc(&ie,
2028 ondisk_path, path2, NULL, NULL);
2029 if (err)
2030 goto done;
2031 err = got_fileindex_entry_add(a->fileindex, ie);
2032 if (err) {
2033 got_fileindex_entry_free(ie);
2034 goto done;
2038 done:
2039 free(ondisk_path);
2040 return err;
2043 struct check_merge_ok_arg {
2044 struct got_worktree *worktree;
2045 struct got_repository *repo;
2048 static const struct got_error *
2049 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2051 const struct got_error *err = NULL;
2052 struct check_merge_ok_arg *a = arg;
2053 unsigned char status;
2054 struct stat sb;
2055 char *ondisk_path;
2057 /* Reject merges into a work tree with mixed base commits. */
2058 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2059 SHA1_DIGEST_LENGTH))
2060 return got_error(GOT_ERR_MIXED_COMMITS);
2062 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2063 == -1)
2064 return got_error_from_errno("asprintf");
2066 /* Reject merges into a work tree with conflicted files. */
2067 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2068 if (err)
2069 return err;
2070 if (status == GOT_STATUS_CONFLICT)
2071 return got_error(GOT_ERR_CONFLICTS);
2073 return NULL;
2076 static const struct got_error *
2077 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2078 const char *fileindex_path, struct got_object_id *commit_id1,
2079 struct got_object_id *commit_id2, struct got_repository *repo,
2080 got_worktree_checkout_cb progress_cb, void *progress_arg,
2081 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2083 const struct got_error *err = NULL, *sync_err;
2084 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2085 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2086 struct merge_file_cb_arg arg;
2088 if (commit_id1) {
2089 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2090 worktree->path_prefix);
2091 if (err)
2092 goto done;
2094 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2095 if (err)
2096 goto done;
2099 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2100 worktree->path_prefix);
2101 if (err)
2102 goto done;
2104 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2105 if (err)
2106 goto done;
2108 arg.worktree = worktree;
2109 arg.fileindex = fileindex;
2110 arg.progress_cb = progress_cb;
2111 arg.progress_arg = progress_arg;
2112 arg.cancel_cb = cancel_cb;
2113 arg.cancel_arg = cancel_arg;
2114 arg.commit_id2 = commit_id2;
2115 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2116 sync_err = sync_fileindex(fileindex, fileindex_path);
2117 if (sync_err && err == NULL)
2118 err = sync_err;
2119 done:
2120 if (tree1)
2121 got_object_tree_close(tree1);
2122 if (tree2)
2123 got_object_tree_close(tree2);
2124 return err;
2127 const struct got_error *
2128 got_worktree_merge_files(struct got_worktree *worktree,
2129 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2130 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2131 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2133 const struct got_error *err, *unlockerr;
2134 char *fileindex_path = NULL;
2135 struct got_fileindex *fileindex = NULL;
2136 struct check_merge_ok_arg mok_arg;
2138 err = lock_worktree(worktree, LOCK_EX);
2139 if (err)
2140 return err;
2142 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2143 if (err)
2144 goto done;
2146 mok_arg.worktree = worktree;
2147 mok_arg.repo = repo;
2148 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2149 &mok_arg);
2150 if (err)
2151 goto done;
2153 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2154 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2155 done:
2156 if (fileindex)
2157 got_fileindex_free(fileindex);
2158 free(fileindex_path);
2159 unlockerr = lock_worktree(worktree, LOCK_SH);
2160 if (unlockerr && err == NULL)
2161 err = unlockerr;
2162 return err;
2165 struct diff_dir_cb_arg {
2166 struct got_fileindex *fileindex;
2167 struct got_worktree *worktree;
2168 const char *status_path;
2169 size_t status_path_len;
2170 struct got_repository *repo;
2171 got_worktree_status_cb status_cb;
2172 void *status_arg;
2173 got_worktree_cancel_cb cancel_cb;
2174 void *cancel_arg;
2177 static const struct got_error *
2178 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2179 got_worktree_status_cb status_cb, void *status_arg,
2180 struct got_repository *repo)
2182 const struct got_error *err = NULL;
2183 unsigned char status = GOT_STATUS_NO_CHANGE;
2184 struct stat sb;
2185 struct got_object_id blob_id, commit_id;
2187 err = get_file_status(&status, &sb, ie, abspath, repo);
2188 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2189 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2190 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2191 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2192 &commit_id);
2194 return err;
2197 static const struct got_error *
2198 status_old_new(void *arg, struct got_fileindex_entry *ie,
2199 struct dirent *de, const char *parent_path)
2201 const struct got_error *err = NULL;
2202 struct diff_dir_cb_arg *a = arg;
2203 char *abspath;
2205 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2206 return got_error(GOT_ERR_CANCELLED);
2208 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2209 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2210 return NULL;
2212 if (parent_path[0]) {
2213 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2214 parent_path, de->d_name) == -1)
2215 return got_error_from_errno("asprintf");
2216 } else {
2217 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2218 de->d_name) == -1)
2219 return got_error_from_errno("asprintf");
2222 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2223 a->repo);
2224 free(abspath);
2225 return err;
2228 static const struct got_error *
2229 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2231 struct diff_dir_cb_arg *a = arg;
2232 struct got_object_id blob_id, commit_id;
2233 unsigned char status;
2235 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2236 return got_error(GOT_ERR_CANCELLED);
2238 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2239 return NULL;
2241 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2242 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2243 if (got_fileindex_entry_has_file_on_disk(ie))
2244 status = GOT_STATUS_MISSING;
2245 else
2246 status = GOT_STATUS_DELETE;
2247 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2248 &commit_id);
2251 static const struct got_error *
2252 status_new(void *arg, struct dirent *de, const char *parent_path)
2254 const struct got_error *err = NULL;
2255 struct diff_dir_cb_arg *a = arg;
2256 char *path = NULL;
2258 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2259 return got_error(GOT_ERR_CANCELLED);
2261 if (de->d_type == DT_DIR)
2262 return NULL;
2264 /* XXX ignore symlinks for now */
2265 if (de->d_type == DT_LNK)
2266 return NULL;
2268 if (parent_path[0]) {
2269 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2270 return got_error_from_errno("asprintf");
2271 } else {
2272 path = de->d_name;
2275 if (got_path_is_child(path, a->status_path, a->status_path_len))
2276 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2277 path, NULL, NULL);
2278 if (parent_path[0])
2279 free(path);
2280 return err;
2283 static const struct got_error *
2284 report_single_file_status(const char *path, const char *ondisk_path,
2285 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2286 void *status_arg, struct got_repository *repo)
2288 struct got_fileindex_entry *ie;
2289 struct stat sb;
2291 ie = got_fileindex_entry_get(fileindex, path);
2292 if (ie)
2293 return report_file_status(ie, ondisk_path, status_cb,
2294 status_arg, repo);
2296 if (lstat(ondisk_path, &sb) == -1) {
2297 if (errno != ENOENT)
2298 return got_error_from_errno2("lstat", ondisk_path);
2299 return NULL;
2302 if (S_ISREG(sb.st_mode))
2303 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED, path,
2304 NULL, NULL);
2306 return NULL;
2309 static const struct got_error *
2310 worktree_status(struct got_worktree *worktree, const char *path,
2311 struct got_fileindex *fileindex, struct got_repository *repo,
2312 got_worktree_status_cb status_cb, void *status_arg,
2313 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2315 const struct got_error *err = NULL;
2316 DIR *workdir = NULL;
2317 struct got_fileindex_diff_dir_cb fdiff_cb;
2318 struct diff_dir_cb_arg arg;
2319 char *ondisk_path = NULL;
2321 if (asprintf(&ondisk_path, "%s%s%s",
2322 worktree->root_path, path[0] ? "/" : "", path) == -1)
2323 return got_error_from_errno("asprintf");
2325 workdir = opendir(ondisk_path);
2326 if (workdir == NULL) {
2327 if (errno != ENOTDIR && errno != ENOENT)
2328 err = got_error_from_errno2("opendir", ondisk_path);
2329 else
2330 err = report_single_file_status(path, ondisk_path,
2331 fileindex, status_cb, status_arg, repo);
2332 } else {
2333 fdiff_cb.diff_old_new = status_old_new;
2334 fdiff_cb.diff_old = status_old;
2335 fdiff_cb.diff_new = status_new;
2336 arg.fileindex = fileindex;
2337 arg.worktree = worktree;
2338 arg.status_path = path;
2339 arg.status_path_len = strlen(path);
2340 arg.repo = repo;
2341 arg.status_cb = status_cb;
2342 arg.status_arg = status_arg;
2343 arg.cancel_cb = cancel_cb;
2344 arg.cancel_arg = cancel_arg;
2345 err = got_fileindex_diff_dir(fileindex, workdir,
2346 worktree->root_path, path, repo, &fdiff_cb, &arg);
2349 if (workdir)
2350 closedir(workdir);
2351 free(ondisk_path);
2352 return err;
2355 const struct got_error *
2356 got_worktree_status(struct got_worktree *worktree,
2357 struct got_pathlist_head *paths, struct got_repository *repo,
2358 got_worktree_status_cb status_cb, void *status_arg,
2359 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2361 const struct got_error *err = NULL;
2362 char *fileindex_path = NULL;
2363 struct got_fileindex *fileindex = NULL;
2364 struct got_pathlist_entry *pe;
2366 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2367 if (err)
2368 return err;
2370 TAILQ_FOREACH(pe, paths, entry) {
2371 err = worktree_status(worktree, pe->path, fileindex, repo,
2372 status_cb, status_arg, cancel_cb, cancel_arg);
2373 if (err)
2374 break;
2376 free(fileindex_path);
2377 got_fileindex_free(fileindex);
2378 return err;
2381 const struct got_error *
2382 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2383 const char *arg)
2385 const struct got_error *err = NULL;
2386 char *resolved, *cwd = NULL, *path = NULL;
2387 size_t len;
2389 *wt_path = NULL;
2391 resolved = realpath(arg, NULL);
2392 if (resolved == NULL) {
2393 if (errno != ENOENT)
2394 return got_error_from_errno2("realpath", arg);
2395 cwd = getcwd(NULL, 0);
2396 if (cwd == NULL)
2397 return got_error_from_errno("getcwd");
2398 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2399 err = got_error_from_errno("asprintf");
2400 goto done;
2404 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2405 strlen(got_worktree_get_root_path(worktree)))) {
2406 err = got_error(GOT_ERR_BAD_PATH);
2407 goto done;
2410 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2411 err = got_path_skip_common_ancestor(&path,
2412 got_worktree_get_root_path(worktree), resolved);
2413 if (err)
2414 goto done;
2415 } else {
2416 path = strdup("");
2417 if (path == NULL) {
2418 err = got_error_from_errno("strdup");
2419 goto done;
2423 /* XXX status walk can't deal with trailing slash! */
2424 len = strlen(path);
2425 while (len > 0 && path[len - 1] == '/') {
2426 path[len - 1] = '\0';
2427 len--;
2429 done:
2430 free(resolved);
2431 free(cwd);
2432 if (err == NULL)
2433 *wt_path = path;
2434 else
2435 free(path);
2436 return err;
2439 static const struct got_error *
2440 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2441 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2442 struct got_repository *repo)
2444 const struct got_error *err = NULL;
2445 struct got_fileindex_entry *ie;
2447 /* Re-adding an existing entry is a no-op. */
2448 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2449 return NULL;
2451 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2452 if (err)
2453 return err;
2455 err = got_fileindex_entry_add(fileindex, ie);
2456 if (err) {
2457 got_fileindex_entry_free(ie);
2458 return err;
2461 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2464 const struct got_error *
2465 got_worktree_schedule_add(struct got_worktree *worktree,
2466 struct got_pathlist_head *ondisk_paths,
2467 got_worktree_status_cb status_cb, void *status_arg,
2468 struct got_repository *repo)
2470 struct got_fileindex *fileindex = NULL;
2471 char *fileindex_path = NULL;
2472 const struct got_error *err = NULL, *sync_err, *unlockerr;
2473 struct got_pathlist_entry *pe;
2475 err = lock_worktree(worktree, LOCK_EX);
2476 if (err)
2477 return err;
2479 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2480 if (err)
2481 goto done;
2483 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2484 char *relpath;
2485 err = got_path_skip_common_ancestor(&relpath,
2486 got_worktree_get_root_path(worktree), pe->path);
2487 if (err)
2488 break;
2489 err = schedule_addition(pe->path, fileindex, relpath,
2490 status_cb, status_arg, repo);
2491 free(relpath);
2492 if (err)
2493 break;
2495 sync_err = sync_fileindex(fileindex, fileindex_path);
2496 if (sync_err && err == NULL)
2497 err = sync_err;
2498 done:
2499 free(fileindex_path);
2500 if (fileindex)
2501 got_fileindex_free(fileindex);
2502 unlockerr = lock_worktree(worktree, LOCK_SH);
2503 if (unlockerr && err == NULL)
2504 err = unlockerr;
2505 return err;
2508 static const struct got_error *
2509 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2510 const char *relpath, int delete_local_mods,
2511 got_worktree_status_cb status_cb, void *status_arg,
2512 struct got_repository *repo)
2514 const struct got_error *err = NULL;
2515 struct got_fileindex_entry *ie = NULL;
2516 unsigned char status;
2517 struct stat sb;
2519 ie = got_fileindex_entry_get(fileindex, relpath);
2520 if (ie == NULL)
2521 return got_error(GOT_ERR_BAD_PATH);
2523 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2524 if (err)
2525 return err;
2527 if (status != GOT_STATUS_NO_CHANGE) {
2528 if (status == GOT_STATUS_DELETE)
2529 return got_error_set_errno(ENOENT, ondisk_path);
2530 if (status != GOT_STATUS_MODIFY)
2531 return got_error(GOT_ERR_FILE_STATUS);
2532 if (!delete_local_mods)
2533 return got_error(GOT_ERR_FILE_MODIFIED);
2536 if (unlink(ondisk_path) != 0)
2537 return got_error_from_errno2("unlink", ondisk_path);
2539 got_fileindex_entry_mark_deleted_from_disk(ie);
2540 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2543 const struct got_error *
2544 got_worktree_schedule_delete(struct got_worktree *worktree,
2545 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2546 got_worktree_status_cb status_cb, void *status_arg,
2547 struct got_repository *repo)
2549 struct got_fileindex *fileindex = NULL;
2550 char *fileindex_path = NULL;
2551 const struct got_error *err = NULL, *sync_err, *unlockerr;
2552 struct got_pathlist_entry *pe;
2554 err = lock_worktree(worktree, LOCK_EX);
2555 if (err)
2556 return err;
2558 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2559 if (err)
2560 goto done;
2562 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2563 char *relpath;
2564 err = got_path_skip_common_ancestor(&relpath,
2565 got_worktree_get_root_path(worktree), pe->path);
2566 if (err)
2567 break;
2568 err = schedule_for_deletion(pe->path, fileindex, relpath,
2569 delete_local_mods, status_cb, status_arg, repo);
2570 free(relpath);
2571 if (err)
2572 break;
2574 sync_err = sync_fileindex(fileindex, fileindex_path);
2575 if (sync_err && err == NULL)
2576 err = sync_err;
2577 done:
2578 free(fileindex_path);
2579 if (fileindex)
2580 got_fileindex_free(fileindex);
2581 unlockerr = lock_worktree(worktree, LOCK_SH);
2582 if (unlockerr && err == NULL)
2583 err = unlockerr;
2584 return err;
2587 static const struct got_error *
2588 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2589 const char *ondisk_path,
2590 got_worktree_checkout_cb progress_cb, void *progress_arg,
2591 struct got_repository *repo)
2593 const struct got_error *err = NULL;
2594 char *relpath = NULL, *parent_path = NULL;
2595 struct got_fileindex_entry *ie;
2596 struct got_tree_object *tree = NULL;
2597 struct got_object_id *tree_id = NULL;
2598 const struct got_tree_entry *te;
2599 char *tree_path = NULL, *te_name;
2600 struct got_blob_object *blob = NULL;
2601 unsigned char status;
2602 struct stat sb;
2604 err = got_path_skip_common_ancestor(&relpath,
2605 got_worktree_get_root_path(worktree), ondisk_path);
2606 if (err)
2607 goto done;
2609 ie = got_fileindex_entry_get(fileindex, relpath);
2610 if (ie == NULL) {
2611 err = got_error(GOT_ERR_BAD_PATH);
2612 goto done;
2615 /* Construct in-repository path of tree which contains this blob. */
2616 err = got_path_dirname(&parent_path, ie->path);
2617 if (err) {
2618 if (err->code != GOT_ERR_BAD_PATH)
2619 goto done;
2620 parent_path = strdup("/");
2621 if (parent_path == NULL) {
2622 err = got_error_from_errno("strdup");
2623 goto done;
2626 if (got_path_is_root_dir(worktree->path_prefix)) {
2627 tree_path = strdup(parent_path);
2628 if (tree_path == NULL) {
2629 err = got_error_from_errno("strdup");
2630 goto done;
2632 } else {
2633 if (got_path_is_root_dir(parent_path)) {
2634 tree_path = strdup(worktree->path_prefix);
2635 if (tree_path == NULL) {
2636 err = got_error_from_errno("strdup");
2637 goto done;
2639 } else {
2640 if (asprintf(&tree_path, "%s/%s",
2641 worktree->path_prefix, parent_path) == -1) {
2642 err = got_error_from_errno("asprintf");
2643 goto done;
2648 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2649 if (err)
2650 goto done;
2652 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2653 tree_path);
2654 if (err) {
2655 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2656 status == GOT_STATUS_ADD))
2657 goto done;
2658 } else {
2659 err = got_object_open_as_tree(&tree, repo, tree_id);
2660 if (err)
2661 goto done;
2663 te_name = basename(ie->path);
2664 if (te_name == NULL) {
2665 err = got_error_from_errno2("basename", ie->path);
2666 goto done;
2669 te = got_object_tree_find_entry(tree, te_name);
2670 if (te == NULL && status != GOT_STATUS_ADD) {
2671 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2672 goto done;
2676 switch (status) {
2677 case GOT_STATUS_ADD:
2678 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2679 if (err)
2680 goto done;
2681 got_fileindex_entry_remove(fileindex, ie);
2682 break;
2683 case GOT_STATUS_DELETE:
2684 case GOT_STATUS_MODIFY:
2685 case GOT_STATUS_CONFLICT:
2686 case GOT_STATUS_MISSING: {
2687 struct got_object_id id;
2688 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2689 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2690 if (err)
2691 goto done;
2692 err = install_blob(worktree, ondisk_path, ie->path,
2693 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2694 progress_arg);
2695 if (err)
2696 goto done;
2697 if (status == GOT_STATUS_DELETE) {
2698 err = update_blob_fileindex_entry(worktree,
2699 fileindex, ie, ondisk_path, ie->path, blob, 1);
2700 if (err)
2701 goto done;
2703 break;
2705 default:
2706 goto done;
2708 done:
2709 free(relpath);
2710 free(parent_path);
2711 free(tree_path);
2712 if (blob)
2713 got_object_blob_close(blob);
2714 if (tree)
2715 got_object_tree_close(tree);
2716 free(tree_id);
2717 return err;
2720 const struct got_error *
2721 got_worktree_revert(struct got_worktree *worktree,
2722 struct got_pathlist_head *ondisk_paths,
2723 got_worktree_checkout_cb progress_cb, void *progress_arg,
2724 struct got_repository *repo)
2726 struct got_fileindex *fileindex = NULL;
2727 char *fileindex_path = NULL;
2728 const struct got_error *err = NULL, *unlockerr = NULL;
2729 const struct got_error *sync_err = NULL;
2730 struct got_pathlist_entry *pe;
2732 err = lock_worktree(worktree, LOCK_EX);
2733 if (err)
2734 return err;
2736 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2737 if (err)
2738 goto done;
2740 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2741 err = revert_file(worktree, fileindex, pe->path,
2742 progress_cb, progress_arg, repo);
2743 if (err)
2744 break;
2746 sync_err = sync_fileindex(fileindex, fileindex_path);
2747 if (sync_err && err == NULL)
2748 err = sync_err;
2749 done:
2750 free(fileindex_path);
2751 if (fileindex)
2752 got_fileindex_free(fileindex);
2753 unlockerr = lock_worktree(worktree, LOCK_SH);
2754 if (unlockerr && err == NULL)
2755 err = unlockerr;
2756 return err;
2759 static void
2760 free_commitable(struct got_commitable *ct)
2762 free(ct->path);
2763 free(ct->in_repo_path);
2764 free(ct->ondisk_path);
2765 free(ct->blob_id);
2766 free(ct->base_blob_id);
2767 free(ct->base_commit_id);
2768 free(ct);
2771 struct collect_commitables_arg {
2772 struct got_pathlist_head *commitable_paths;
2773 struct got_repository *repo;
2774 struct got_worktree *worktree;
2777 static const struct got_error *
2778 collect_commitables(void *arg, unsigned char status, const char *relpath,
2779 struct got_object_id *blob_id, struct got_object_id *commit_id)
2781 struct collect_commitables_arg *a = arg;
2782 const struct got_error *err = NULL;
2783 struct got_commitable *ct = NULL;
2784 struct got_pathlist_entry *new = NULL;
2785 char *parent_path = NULL, *path = NULL;
2786 struct stat sb;
2788 if (status == GOT_STATUS_CONFLICT)
2789 return got_error(GOT_ERR_COMMIT_CONFLICT);
2791 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2792 status != GOT_STATUS_DELETE)
2793 return NULL;
2795 if (asprintf(&path, "/%s", relpath) == -1) {
2796 err = got_error_from_errno("asprintf");
2797 goto done;
2799 if (strcmp(path, "/") == 0) {
2800 parent_path = strdup("");
2801 if (parent_path == NULL)
2802 return got_error_from_errno("strdup");
2803 } else {
2804 err = got_path_dirname(&parent_path, path);
2805 if (err)
2806 return err;
2809 ct = calloc(1, sizeof(*ct));
2810 if (ct == NULL) {
2811 err = got_error_from_errno("calloc");
2812 goto done;
2815 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2816 relpath) == -1) {
2817 err = got_error_from_errno("asprintf");
2818 goto done;
2820 if (status == GOT_STATUS_DELETE) {
2821 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2822 } else {
2823 if (lstat(ct->ondisk_path, &sb) != 0) {
2824 err = got_error_from_errno2("lstat", ct->ondisk_path);
2825 goto done;
2827 ct->mode = sb.st_mode;
2830 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2831 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2832 relpath) == -1) {
2833 err = got_error_from_errno("asprintf");
2834 goto done;
2837 ct->status = status;
2838 ct->blob_id = NULL; /* will be filled in when blob gets created */
2839 if (ct->status != GOT_STATUS_ADD) {
2840 ct->base_blob_id = got_object_id_dup(blob_id);
2841 if (ct->base_blob_id == NULL) {
2842 err = got_error_from_errno("got_object_id_dup");
2843 goto done;
2845 ct->base_commit_id = got_object_id_dup(commit_id);
2846 if (ct->base_commit_id == NULL) {
2847 err = got_error_from_errno("got_object_id_dup");
2848 goto done;
2851 ct->path = strdup(path);
2852 if (ct->path == NULL) {
2853 err = got_error_from_errno("strdup");
2854 goto done;
2856 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2857 done:
2858 if (ct && (err || new == NULL))
2859 free_commitable(ct);
2860 free(parent_path);
2861 free(path);
2862 return err;
2865 static const struct got_error *write_tree(struct got_object_id **,
2866 struct got_tree_object *, const char *, struct got_pathlist_head *,
2867 got_worktree_status_cb status_cb, void *status_arg,
2868 struct got_repository *);
2870 static const struct got_error *
2871 write_subtree(struct got_object_id **new_subtree_id,
2872 struct got_tree_entry *te, const char *parent_path,
2873 struct got_pathlist_head *commitable_paths,
2874 got_worktree_status_cb status_cb, void *status_arg,
2875 struct got_repository *repo)
2877 const struct got_error *err = NULL;
2878 struct got_tree_object *subtree;
2879 char *subpath;
2881 if (asprintf(&subpath, "%s%s%s", parent_path,
2882 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2883 return got_error_from_errno("asprintf");
2885 err = got_object_open_as_tree(&subtree, repo, te->id);
2886 if (err)
2887 return err;
2889 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2890 status_cb, status_arg, repo);
2891 got_object_tree_close(subtree);
2892 free(subpath);
2893 return err;
2896 static const struct got_error *
2897 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2899 const struct got_error *err = NULL;
2900 char *ct_parent_path = NULL;
2902 *match = 0;
2904 if (strchr(ct->in_repo_path, '/') == NULL) {
2905 *match = got_path_is_root_dir(path);
2906 return NULL;
2909 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2910 if (err)
2911 return err;
2912 *match = (strcmp(path, ct_parent_path) == 0);
2913 free(ct_parent_path);
2914 return err;
2917 static mode_t
2918 get_ct_file_mode(struct got_commitable *ct)
2920 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2923 static const struct got_error *
2924 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2925 struct got_tree_entry *te, struct got_commitable *ct)
2927 const struct got_error *err = NULL;
2929 *new_te = NULL;
2931 err = got_object_tree_entry_dup(new_te, te);
2932 if (err)
2933 goto done;
2935 (*new_te)->mode = get_ct_file_mode(ct);
2937 free((*new_te)->id);
2938 (*new_te)->id = got_object_id_dup(ct->blob_id);
2939 if ((*new_te)->id == NULL) {
2940 err = got_error_from_errno("got_object_id_dup");
2941 goto done;
2943 done:
2944 if (err && *new_te) {
2945 got_object_tree_entry_close(*new_te);
2946 *new_te = NULL;
2948 return err;
2951 static const struct got_error *
2952 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2953 struct got_commitable *ct)
2955 const struct got_error *err = NULL;
2956 char *ct_name;
2958 *new_te = NULL;
2960 *new_te = calloc(1, sizeof(**new_te));
2961 if (*new_te == NULL)
2962 return got_error_from_errno("calloc");
2964 ct_name = basename(ct->path);
2965 if (ct_name == NULL) {
2966 err = got_error_from_errno2("basename", ct->path);
2967 goto done;
2969 (*new_te)->name = strdup(ct_name);
2970 if ((*new_te)->name == NULL) {
2971 err = got_error_from_errno("strdup");
2972 goto done;
2975 (*new_te)->mode = get_ct_file_mode(ct);
2977 (*new_te)->id = got_object_id_dup(ct->blob_id);
2978 if ((*new_te)->id == NULL) {
2979 err = got_error_from_errno("got_object_id_dup");
2980 goto done;
2982 done:
2983 if (err && *new_te) {
2984 got_object_tree_entry_close(*new_te);
2985 *new_te = NULL;
2987 return err;
2990 static const struct got_error *
2991 insert_tree_entry(struct got_tree_entry *new_te,
2992 struct got_pathlist_head *paths)
2994 const struct got_error *err = NULL;
2995 struct got_pathlist_entry *new_pe;
2997 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2998 if (err)
2999 return err;
3000 if (new_pe == NULL)
3001 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3002 return NULL;
3005 static const struct got_error *
3006 report_ct_status(struct got_commitable *ct,
3007 got_worktree_status_cb status_cb, void *status_arg)
3009 const char *ct_path = ct->path;
3010 while (ct_path[0] == '/')
3011 ct_path++;
3012 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
3015 static const struct got_error *
3016 match_modified_subtree(int *modified, struct got_tree_entry *te,
3017 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3019 const struct got_error *err = NULL;
3020 struct got_pathlist_entry *pe;
3021 char *te_path;
3023 *modified = 0;
3025 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3026 got_path_is_root_dir(base_tree_path) ? "" : "/",
3027 te->name) == -1)
3028 return got_error_from_errno("asprintf");
3030 TAILQ_FOREACH(pe, commitable_paths, entry) {
3031 struct got_commitable *ct = pe->data;
3032 *modified = got_path_is_child(ct->in_repo_path, te_path,
3033 strlen(te_path));
3034 if (*modified)
3035 break;
3038 free(te_path);
3039 return err;
3042 static const struct got_error *
3043 match_deleted_or_modified_ct(struct got_commitable **ctp,
3044 struct got_tree_entry *te, const char *base_tree_path,
3045 struct got_pathlist_head *commitable_paths)
3047 const struct got_error *err = NULL;
3048 struct got_pathlist_entry *pe;
3050 *ctp = NULL;
3052 TAILQ_FOREACH(pe, commitable_paths, entry) {
3053 struct got_commitable *ct = pe->data;
3054 char *ct_name = NULL;
3055 int path_matches;
3057 if (ct->status != GOT_STATUS_MODIFY &&
3058 ct->status != GOT_STATUS_DELETE)
3059 continue;
3061 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3062 continue;
3064 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3065 if (err)
3066 return err;
3067 if (!path_matches)
3068 continue;
3070 ct_name = basename(pe->path);
3071 if (ct_name == NULL)
3072 return got_error_from_errno2("basename", pe->path);
3074 if (strcmp(te->name, ct_name) != 0)
3075 continue;
3077 *ctp = ct;
3078 break;
3081 return err;
3084 static const struct got_error *
3085 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3086 const char *child_path, const char *path_base_tree,
3087 struct got_pathlist_head *commitable_paths,
3088 got_worktree_status_cb status_cb, void *status_arg,
3089 struct got_repository *repo)
3091 const struct got_error *err = NULL;
3092 struct got_tree_entry *new_te;
3093 char *subtree_path;
3095 *new_tep = NULL;
3097 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3098 got_path_is_root_dir(path_base_tree) ? "" : "/",
3099 child_path) == -1)
3100 return got_error_from_errno("asprintf");
3102 new_te = calloc(1, sizeof(*new_te));
3103 new_te->mode = S_IFDIR;
3104 new_te->name = strdup(child_path);
3105 if (new_te->name == NULL) {
3106 err = got_error_from_errno("strdup");
3107 got_object_tree_entry_close(new_te);
3108 goto done;
3110 err = write_tree(&new_te->id, NULL, subtree_path,
3111 commitable_paths, status_cb, status_arg, repo);
3112 if (err) {
3113 got_object_tree_entry_close(new_te);
3114 goto done;
3116 done:
3117 free(subtree_path);
3118 if (err == NULL)
3119 *new_tep = new_te;
3120 return err;
3123 static const struct got_error *
3124 write_tree(struct got_object_id **new_tree_id,
3125 struct got_tree_object *base_tree, const char *path_base_tree,
3126 struct got_pathlist_head *commitable_paths,
3127 got_worktree_status_cb status_cb, void *status_arg,
3128 struct got_repository *repo)
3130 const struct got_error *err = NULL;
3131 const struct got_tree_entries *base_entries = NULL;
3132 struct got_pathlist_head paths;
3133 struct got_tree_entries new_tree_entries;
3134 struct got_tree_entry *te, *new_te = NULL;
3135 struct got_pathlist_entry *pe;
3137 TAILQ_INIT(&paths);
3138 new_tree_entries.nentries = 0;
3139 SIMPLEQ_INIT(&new_tree_entries.head);
3141 /* Insert, and recurse into, newly added entries first. */
3142 TAILQ_FOREACH(pe, commitable_paths, entry) {
3143 struct got_commitable *ct = pe->data;
3144 char *child_path = NULL, *slash;
3146 if (ct->status != GOT_STATUS_ADD ||
3147 (ct->flags & GOT_COMMITABLE_ADDED))
3148 continue;
3150 if (!got_path_is_child(pe->path, path_base_tree,
3151 strlen(path_base_tree)))
3152 continue;
3154 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3155 pe->path);
3156 if (err)
3157 goto done;
3159 slash = strchr(child_path, '/');
3160 if (slash == NULL) {
3161 err = alloc_added_blob_tree_entry(&new_te, ct);
3162 if (err)
3163 goto done;
3164 err = report_ct_status(ct, status_cb, status_arg);
3165 if (err)
3166 goto done;
3167 ct->flags |= GOT_COMMITABLE_ADDED;
3168 err = insert_tree_entry(new_te, &paths);
3169 if (err)
3170 goto done;
3171 } else {
3172 *slash = '\0'; /* trim trailing path components */
3173 if (base_tree == NULL ||
3174 got_object_tree_find_entry(base_tree, child_path)
3175 == NULL) {
3176 err = make_subtree_for_added_blob(&new_te,
3177 child_path, path_base_tree,
3178 commitable_paths, status_cb, status_arg,
3179 repo);
3180 if (err)
3181 goto done;
3182 err = insert_tree_entry(new_te, &paths);
3183 if (err)
3184 goto done;
3189 if (base_tree) {
3190 /* Handle modified and deleted entries. */
3191 base_entries = got_object_tree_get_entries(base_tree);
3192 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3193 struct got_commitable *ct = NULL;
3195 if (S_ISDIR(te->mode)) {
3196 int modified;
3197 err = got_object_tree_entry_dup(&new_te, te);
3198 if (err)
3199 goto done;
3200 err = match_modified_subtree(&modified, te,
3201 path_base_tree, commitable_paths);
3202 if (err)
3203 goto done;
3204 /* Avoid recursion into unmodified subtrees. */
3205 if (modified) {
3206 free(new_te->id);
3207 err = write_subtree(&new_te->id, te,
3208 path_base_tree, commitable_paths,
3209 status_cb, status_arg, repo);
3210 if (err)
3211 goto done;
3213 err = insert_tree_entry(new_te, &paths);
3214 if (err)
3215 goto done;
3216 continue;
3219 err = match_deleted_or_modified_ct(&ct, te,
3220 path_base_tree, commitable_paths);
3221 if (ct) {
3222 /* NB: Deleted entries get dropped here. */
3223 if (ct->status == GOT_STATUS_MODIFY) {
3224 err = alloc_modified_blob_tree_entry(
3225 &new_te, te, ct);
3226 if (err)
3227 goto done;
3228 err = insert_tree_entry(new_te, &paths);
3229 if (err)
3230 goto done;
3232 err = report_ct_status(ct, status_cb,
3233 status_arg);
3234 if (err)
3235 goto done;
3236 } else {
3237 /* Entry is unchanged; just copy it. */
3238 err = got_object_tree_entry_dup(&new_te, te);
3239 if (err)
3240 goto done;
3241 err = insert_tree_entry(new_te, &paths);
3242 if (err)
3243 goto done;
3248 /* Write new list of entries; deleted entries have been dropped. */
3249 TAILQ_FOREACH(pe, &paths, entry) {
3250 struct got_tree_entry *te = pe->data;
3251 new_tree_entries.nentries++;
3252 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3254 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3255 done:
3256 got_object_tree_entries_close(&new_tree_entries);
3257 got_pathlist_free(&paths);
3258 return err;
3261 static const struct got_error *
3262 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3263 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3265 const struct got_error *err = NULL;
3266 struct got_pathlist_entry *pe;
3268 TAILQ_FOREACH(pe, commitable_paths, entry) {
3269 struct got_fileindex_entry *ie;
3270 struct got_commitable *ct = pe->data;
3272 ie = got_fileindex_entry_get(fileindex, pe->path);
3273 if (ie) {
3274 if (ct->status == GOT_STATUS_DELETE) {
3275 got_fileindex_entry_remove(fileindex, ie);
3276 got_fileindex_entry_free(ie);
3277 } else
3278 err = got_fileindex_entry_update(ie,
3279 ct->ondisk_path, ct->blob_id->sha1,
3280 new_base_commit_id->sha1, 1);
3281 } else {
3282 err = got_fileindex_entry_alloc(&ie,
3283 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3284 new_base_commit_id->sha1);
3285 if (err)
3286 break;
3287 err = got_fileindex_entry_add(fileindex, ie);
3288 if (err)
3289 break;
3292 return err;
3295 static const struct got_error *
3296 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3297 struct got_object_id *head_commit_id)
3299 const struct got_error *err = NULL;
3300 struct got_object_id *id_in_head = NULL, *id = NULL;
3301 struct got_commit_object *commit = NULL;
3302 char *path = NULL;
3303 const char *ct_path = ct->in_repo_path;
3305 while (ct_path[0] == '/')
3306 ct_path++;
3309 * Ensure that no modifications were made to files *and their parents*
3310 * in commits between the file's base commit and the branch head.
3312 * Checking the parents is important for detecting conflicting tree
3313 * configurations (files or parent folders might have been moved,
3314 * deleted, added again, etc.). Such changes need to be merged with
3315 * local changes before a commit can occur.
3317 * The implication is that the file's (parent) entry in the root
3318 * directory must have the same ID in all relevant commits.
3320 if (ct->status != GOT_STATUS_ADD) {
3321 struct got_object_qid *pid;
3322 char *slash;
3323 struct got_object_id *root_entry_id = NULL;
3325 /* Trivial case: base commit == head commit */
3326 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3327 return NULL;
3329 /* Compute the path to the root directory's entry. */
3330 path = strdup(ct_path);
3331 if (path == NULL) {
3332 err = got_error_from_errno("strdup");
3333 goto done;
3335 slash = strchr(path, '/');
3336 if (slash)
3337 *slash = '\0';
3339 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3340 if (err)
3341 goto done;
3343 err = got_object_id_by_path(&root_entry_id, repo,
3344 head_commit_id, path);
3345 if (err)
3346 goto done;
3348 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3349 while (pid) {
3350 struct got_commit_object *pcommit;
3352 err = got_object_id_by_path(&id, repo, pid->id, path);
3353 if (err) {
3354 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3355 goto done;
3356 err = NULL;
3357 break;
3360 err = got_object_id_by_path(&id, repo, pid->id, path);
3361 if (err)
3362 goto done;
3364 if (got_object_id_cmp(id, root_entry_id) != 0) {
3365 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3366 break;
3369 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3370 break; /* all relevant commits scanned */
3372 err = got_object_open_as_commit(&pcommit, repo,
3373 pid->id);
3374 if (err)
3375 goto done;
3377 got_object_commit_close(commit);
3378 commit = pcommit;
3379 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3380 commit));
3382 } else {
3383 /* Require that added files don't exist in the branch head. */
3384 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3385 ct_path);
3386 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3387 goto done;
3388 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3390 done:
3391 if (commit)
3392 got_object_commit_close(commit);
3393 free(id_in_head);
3394 free(id);
3395 free(path);
3396 return err;
3399 const struct got_error *
3400 commit_worktree(struct got_object_id **new_commit_id,
3401 struct got_pathlist_head *commitable_paths,
3402 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3403 const char *ondisk_path, const char *author, const char *committer,
3404 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3405 got_worktree_status_cb status_cb, void *status_arg,
3406 struct got_repository *repo)
3408 const struct got_error *err = NULL, *unlockerr = NULL;
3409 struct got_pathlist_entry *pe;
3410 const char *head_ref_name = NULL;
3411 struct got_commit_object *head_commit = NULL;
3412 struct got_reference *head_ref2 = NULL;
3413 struct got_object_id *head_commit_id2 = NULL;
3414 struct got_tree_object *head_tree = NULL;
3415 struct got_object_id *new_tree_id = NULL;
3416 struct got_object_id_queue parent_ids;
3417 struct got_object_qid *pid = NULL;
3418 char *logmsg = NULL;
3420 *new_commit_id = NULL;
3422 SIMPLEQ_INIT(&parent_ids);
3424 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3425 if (err)
3426 goto done;
3428 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3429 if (err)
3430 goto done;
3432 if (commit_msg_cb != NULL) {
3433 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3434 if (err)
3435 goto done;
3438 if (logmsg == NULL || strlen(logmsg) == 0) {
3439 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3440 goto done;
3443 /* Create blobs from added and modified files and record their IDs. */
3444 TAILQ_FOREACH(pe, commitable_paths, entry) {
3445 struct got_commitable *ct = pe->data;
3446 char *ondisk_path;
3448 if (ct->status != GOT_STATUS_ADD &&
3449 ct->status != GOT_STATUS_MODIFY)
3450 continue;
3452 if (asprintf(&ondisk_path, "%s/%s",
3453 worktree->root_path, pe->path) == -1) {
3454 err = got_error_from_errno("asprintf");
3455 goto done;
3457 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3458 free(ondisk_path);
3459 if (err)
3460 goto done;
3463 /* Recursively write new tree objects. */
3464 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3465 status_cb, status_arg, repo);
3466 if (err)
3467 goto done;
3469 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3470 if (err)
3471 goto done;
3472 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3473 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3474 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3475 got_object_qid_free(pid);
3476 if (logmsg != NULL)
3477 free(logmsg);
3478 if (err)
3479 goto done;
3481 /* Check if a concurrent commit to our branch has occurred. */
3482 head_ref_name = got_worktree_get_head_ref_name(worktree);
3483 if (head_ref_name == NULL) {
3484 err = got_error_from_errno("got_worktree_get_head_ref_name");
3485 goto done;
3487 /* Lock the reference here to prevent concurrent modification. */
3488 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3489 if (err)
3490 goto done;
3491 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3492 if (err)
3493 goto done;
3494 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3495 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3496 goto done;
3498 /* Update branch head in repository. */
3499 err = got_ref_change_ref(head_ref2, *new_commit_id);
3500 if (err)
3501 goto done;
3502 err = got_ref_write(head_ref2, repo);
3503 if (err)
3504 goto done;
3506 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3507 if (err)
3508 goto done;
3510 err = ref_base_commit(worktree, repo);
3511 if (err)
3512 goto done;
3513 done:
3514 if (head_tree)
3515 got_object_tree_close(head_tree);
3516 if (head_commit)
3517 got_object_commit_close(head_commit);
3518 free(head_commit_id2);
3519 if (head_ref2) {
3520 unlockerr = got_ref_unlock(head_ref2);
3521 if (unlockerr && err == NULL)
3522 err = unlockerr;
3523 got_ref_close(head_ref2);
3525 return err;
3528 const struct got_error *
3529 got_worktree_commit(struct got_object_id **new_commit_id,
3530 struct got_worktree *worktree, const char *ondisk_path,
3531 const char *author, const char *committer,
3532 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3533 got_worktree_status_cb status_cb, void *status_arg,
3534 struct got_repository *repo)
3536 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3537 struct got_fileindex *fileindex = NULL;
3538 char *fileindex_path = NULL, *relpath = NULL;
3539 struct got_pathlist_head commitable_paths;
3540 struct collect_commitables_arg cc_arg;
3541 struct got_pathlist_entry *pe;
3542 struct got_reference *head_ref = NULL;
3543 struct got_object_id *head_commit_id = NULL;
3545 *new_commit_id = NULL;
3547 TAILQ_INIT(&commitable_paths);
3549 err = lock_worktree(worktree, LOCK_EX);
3550 if (err)
3551 goto done;
3553 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3554 if (err)
3555 goto done;
3557 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3558 if (err)
3559 goto done;
3561 if (ondisk_path) {
3562 if (strcmp(ondisk_path, worktree->root_path) == 0) {
3563 relpath = strdup("");
3564 if (relpath == NULL) {
3565 err = got_error_from_errno("strdup");
3566 goto done;
3568 } else {
3569 err = got_path_skip_common_ancestor(&relpath,
3570 worktree->root_path, ondisk_path);
3571 if (err)
3572 return err;
3576 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3577 if (err)
3578 goto done;
3580 cc_arg.commitable_paths = &commitable_paths;
3581 cc_arg.worktree = worktree;
3582 cc_arg.repo = repo;
3583 err = worktree_status(worktree, relpath ? relpath : "",
3584 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3585 if (err)
3586 goto done;
3588 if (TAILQ_EMPTY(&commitable_paths)) {
3589 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3590 goto done;
3593 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3594 struct got_commitable *ct = pe->data;
3595 err = check_ct_out_of_date(ct, repo, head_commit_id);
3596 if (err)
3597 goto done;
3600 err = commit_worktree(new_commit_id, &commitable_paths,
3601 head_commit_id, worktree, ondisk_path, author, committer,
3602 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3603 if (err)
3604 goto done;
3606 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3607 fileindex);
3608 sync_err = sync_fileindex(fileindex, fileindex_path);
3609 if (sync_err && err == NULL)
3610 err = sync_err;
3611 done:
3612 if (fileindex)
3613 got_fileindex_free(fileindex);
3614 free(fileindex_path);
3615 free(relpath);
3616 unlockerr = lock_worktree(worktree, LOCK_SH);
3617 if (unlockerr && err == NULL)
3618 err = unlockerr;
3619 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3620 struct got_commitable *ct = pe->data;
3621 free_commitable(ct);
3623 got_pathlist_free(&commitable_paths);
3624 return err;
3627 const char *
3628 got_commitable_get_path(struct got_commitable *ct)
3630 return ct->path;
3633 unsigned int
3634 got_commitable_get_status(struct got_commitable *ct)
3636 return ct->status;
3639 struct check_rebase_ok_arg {
3640 struct got_worktree *worktree;
3641 struct got_repository *repo;
3644 static const struct got_error *
3645 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3647 const struct got_error *err = NULL;
3648 struct check_rebase_ok_arg *a = arg;
3649 unsigned char status;
3650 struct stat sb;
3651 char *ondisk_path;
3653 /* Reject rebase of a work tree with mixed base commits. */
3654 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3655 SHA1_DIGEST_LENGTH))
3656 return got_error(GOT_ERR_MIXED_COMMITS);
3658 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3659 == -1)
3660 return got_error_from_errno("asprintf");
3662 /* Reject rebase of a work tree with modified or conflicted files. */
3663 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3664 free(ondisk_path);
3665 if (err)
3666 return err;
3668 if (status != GOT_STATUS_NO_CHANGE)
3669 return got_error(GOT_ERR_MODIFIED);
3671 return NULL;
3674 const struct got_error *
3675 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3676 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3677 struct got_worktree *worktree, struct got_reference *branch,
3678 struct got_repository *repo)
3680 const struct got_error *err = NULL;
3681 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3682 char *branch_ref_name = NULL;
3683 char *fileindex_path = NULL;
3684 struct check_rebase_ok_arg ok_arg;
3685 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3687 *new_base_branch_ref = NULL;
3688 *tmp_branch = NULL;
3689 *fileindex = NULL;
3691 err = lock_worktree(worktree, LOCK_EX);
3692 if (err)
3693 return err;
3695 err = open_fileindex(fileindex, &fileindex_path, worktree);
3696 if (err)
3697 goto done;
3699 ok_arg.worktree = worktree;
3700 ok_arg.repo = repo;
3701 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3702 &ok_arg);
3703 if (err)
3704 goto done;
3706 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3707 if (err)
3708 goto done;
3710 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3711 if (err)
3712 goto done;
3714 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3715 if (err)
3716 goto done;
3718 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3719 0);
3720 if (err)
3721 goto done;
3723 err = got_ref_alloc_symref(new_base_branch_ref,
3724 new_base_branch_ref_name, wt_branch);
3725 if (err)
3726 goto done;
3727 err = got_ref_write(*new_base_branch_ref, repo);
3728 if (err)
3729 goto done;
3731 /* TODO Lock original branch's ref while rebasing? */
3733 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3734 if (err)
3735 goto done;
3737 err = got_ref_write(branch_ref, repo);
3738 if (err)
3739 goto done;
3741 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3742 worktree->base_commit_id);
3743 if (err)
3744 goto done;
3745 err = got_ref_write(*tmp_branch, repo);
3746 if (err)
3747 goto done;
3749 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3750 if (err)
3751 goto done;
3752 done:
3753 free(fileindex_path);
3754 free(tmp_branch_name);
3755 free(new_base_branch_ref_name);
3756 free(branch_ref_name);
3757 if (branch_ref)
3758 got_ref_close(branch_ref);
3759 if (wt_branch)
3760 got_ref_close(wt_branch);
3761 if (err) {
3762 if (*new_base_branch_ref) {
3763 got_ref_close(*new_base_branch_ref);
3764 *new_base_branch_ref = NULL;
3766 if (*tmp_branch) {
3767 got_ref_close(*tmp_branch);
3768 *tmp_branch = NULL;
3770 if (*fileindex) {
3771 got_fileindex_free(*fileindex);
3772 *fileindex = NULL;
3774 lock_worktree(worktree, LOCK_SH);
3776 return err;
3779 const struct got_error *
3780 got_worktree_rebase_continue(struct got_object_id **commit_id,
3781 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3782 struct got_reference **branch, struct got_fileindex **fileindex,
3783 struct got_worktree *worktree, struct got_repository *repo)
3785 const struct got_error *err;
3786 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3787 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3788 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3789 char *fileindex_path = NULL;
3791 *commit_id = NULL;
3792 *new_base_branch = NULL;
3793 *tmp_branch = NULL;
3794 *branch = NULL;
3795 *fileindex = NULL;
3797 err = lock_worktree(worktree, LOCK_EX);
3798 if (err)
3799 return err;
3801 err = open_fileindex(fileindex, &fileindex_path, worktree);
3802 if (err)
3803 goto done;
3805 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3806 if (err)
3807 return err;
3809 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3810 if (err)
3811 goto done;
3813 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3814 if (err)
3815 goto done;
3817 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3818 if (err)
3819 goto done;
3821 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3822 if (err)
3823 goto done;
3825 err = got_ref_open(branch, repo,
3826 got_ref_get_symref_target(branch_ref), 0);
3827 if (err)
3828 goto done;
3830 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3831 if (err)
3832 goto done;
3834 err = got_ref_resolve(commit_id, repo, commit_ref);
3835 if (err)
3836 goto done;
3838 err = got_ref_open(new_base_branch, repo,
3839 new_base_branch_ref_name, 0);
3840 if (err)
3841 goto done;
3843 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3844 if (err)
3845 goto done;
3846 done:
3847 free(commit_ref_name);
3848 free(branch_ref_name);
3849 free(fileindex_path);
3850 if (commit_ref)
3851 got_ref_close(commit_ref);
3852 if (branch_ref)
3853 got_ref_close(branch_ref);
3854 if (err) {
3855 free(*commit_id);
3856 *commit_id = NULL;
3857 if (*tmp_branch) {
3858 got_ref_close(*tmp_branch);
3859 *tmp_branch = NULL;
3861 if (*new_base_branch) {
3862 got_ref_close(*new_base_branch);
3863 *new_base_branch = NULL;
3865 if (*branch) {
3866 got_ref_close(*branch);
3867 *branch = NULL;
3869 if (*fileindex) {
3870 got_fileindex_free(*fileindex);
3871 *fileindex = NULL;
3873 lock_worktree(worktree, LOCK_SH);
3875 return err;
3878 const struct got_error *
3879 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3881 const struct got_error *err;
3882 char *tmp_branch_name = NULL;
3884 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3885 if (err)
3886 return err;
3888 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3889 free(tmp_branch_name);
3890 return NULL;
3893 static const struct got_error *
3894 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3895 char **logmsg, void *arg)
3897 *logmsg = arg;
3898 return NULL;
3901 static const struct got_error *
3902 rebase_status(void *arg, unsigned char status, const char *path,
3903 struct got_object_id *blob_id, struct got_object_id *commit_id)
3905 return NULL;
3908 struct collect_merged_paths_arg {
3909 got_worktree_checkout_cb progress_cb;
3910 void *progress_arg;
3911 struct got_pathlist_head *merged_paths;
3914 static const struct got_error *
3915 collect_merged_paths(void *arg, unsigned char status, const char *path)
3917 const struct got_error *err;
3918 struct collect_merged_paths_arg *a = arg;
3919 char *p;
3920 struct got_pathlist_entry *new;
3922 err = (*a->progress_cb)(a->progress_arg, status, path);
3923 if (err)
3924 return err;
3926 if (status != GOT_STATUS_MERGE &&
3927 status != GOT_STATUS_ADD &&
3928 status != GOT_STATUS_DELETE &&
3929 status != GOT_STATUS_CONFLICT)
3930 return NULL;
3932 p = strdup(path);
3933 if (p == NULL)
3934 return got_error_from_errno("strdup");
3936 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3937 if (err || new == NULL)
3938 free(p);
3939 return err;
3942 void
3943 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3945 struct got_pathlist_entry *pe;
3947 TAILQ_FOREACH(pe, merged_paths, entry)
3948 free((char *)pe->path);
3950 got_pathlist_free(merged_paths);
3953 static const struct got_error *
3954 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3955 struct got_repository *repo)
3957 const struct got_error *err;
3958 struct got_reference *commit_ref = NULL;
3960 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3961 if (err) {
3962 if (err->code != GOT_ERR_NOT_REF)
3963 goto done;
3964 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3965 if (err)
3966 goto done;
3967 err = got_ref_write(commit_ref, repo);
3968 if (err)
3969 goto done;
3970 } else {
3971 struct got_object_id *stored_id;
3972 int cmp;
3974 err = got_ref_resolve(&stored_id, repo, commit_ref);
3975 if (err)
3976 goto done;
3977 cmp = got_object_id_cmp(commit_id, stored_id);
3978 free(stored_id);
3979 if (cmp != 0) {
3980 err = got_error(GOT_ERR_REBASE_COMMITID);
3981 goto done;
3984 done:
3985 if (commit_ref)
3986 got_ref_close(commit_ref);
3987 return err;
3990 static const struct got_error *
3991 rebase_merge_files(struct got_pathlist_head *merged_paths,
3992 const char *commit_ref_name, struct got_worktree *worktree,
3993 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3994 struct got_object_id *commit_id, struct got_repository *repo,
3995 got_worktree_checkout_cb progress_cb, void *progress_arg,
3996 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3998 const struct got_error *err;
3999 struct got_reference *commit_ref = NULL;
4000 struct collect_merged_paths_arg cmp_arg;
4001 char *fileindex_path;
4003 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4005 err = get_fileindex_path(&fileindex_path, worktree);
4006 if (err)
4007 return err;
4009 cmp_arg.progress_cb = progress_cb;
4010 cmp_arg.progress_arg = progress_arg;
4011 cmp_arg.merged_paths = merged_paths;
4012 err = merge_files(worktree, fileindex, fileindex_path,
4013 parent_commit_id, commit_id, repo, collect_merged_paths,
4014 &cmp_arg, cancel_cb, cancel_arg);
4015 if (commit_ref)
4016 got_ref_close(commit_ref);
4017 return err;
4020 const struct got_error *
4021 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4022 struct got_worktree *worktree, struct got_fileindex *fileindex,
4023 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4024 struct got_repository *repo,
4025 got_worktree_checkout_cb progress_cb, void *progress_arg,
4026 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4028 const struct got_error *err;
4029 char *commit_ref_name;
4031 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4032 if (err)
4033 return err;
4035 err = store_commit_id(commit_ref_name, commit_id, repo);
4036 if (err)
4037 goto done;
4039 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4040 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4041 progress_arg, cancel_cb, cancel_arg);
4042 done:
4043 free(commit_ref_name);
4044 return err;
4047 const struct got_error *
4048 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4049 struct got_worktree *worktree, struct got_fileindex *fileindex,
4050 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4051 struct got_repository *repo,
4052 got_worktree_checkout_cb progress_cb, void *progress_arg,
4053 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4055 const struct got_error *err;
4056 char *commit_ref_name;
4058 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4059 if (err)
4060 return err;
4062 err = store_commit_id(commit_ref_name, commit_id, repo);
4063 if (err)
4064 goto done;
4066 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4067 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4068 progress_arg, cancel_cb, cancel_arg);
4069 done:
4070 free(commit_ref_name);
4071 return err;
4074 static const struct got_error *
4075 rebase_commit(struct got_object_id **new_commit_id,
4076 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4077 struct got_worktree *worktree, struct got_fileindex *fileindex,
4078 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4079 const char *new_logmsg, struct got_repository *repo)
4081 const struct got_error *err, *sync_err;
4082 struct got_pathlist_head commitable_paths;
4083 struct collect_commitables_arg cc_arg;
4084 char *fileindex_path = NULL;
4085 struct got_reference *head_ref = NULL;
4086 struct got_object_id *head_commit_id = NULL;
4087 char *logmsg = NULL;
4089 TAILQ_INIT(&commitable_paths);
4090 *new_commit_id = NULL;
4092 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4094 err = get_fileindex_path(&fileindex_path, worktree);
4095 if (err)
4096 return err;
4098 cc_arg.commitable_paths = &commitable_paths;
4099 cc_arg.worktree = worktree;
4100 cc_arg.repo = repo;
4102 * If possible get the status of individual files directly to
4103 * avoid crawling the entire work tree once per rebased commit.
4104 * TODO: Ideally, merged_paths would contain a list of commitables
4105 * we could use so we could skip worktree_status() entirely.
4107 if (merged_paths) {
4108 struct got_pathlist_entry *pe;
4109 if (TAILQ_EMPTY(merged_paths)) {
4110 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4111 goto done;
4113 TAILQ_FOREACH(pe, merged_paths, entry) {
4114 err = worktree_status(worktree, pe->path, fileindex,
4115 repo, collect_commitables, &cc_arg, NULL, NULL);
4116 if (err)
4117 goto done;
4119 } else {
4120 err = worktree_status(worktree, "", fileindex, repo,
4121 collect_commitables, &cc_arg, NULL, NULL);
4122 if (err)
4123 goto done;
4126 if (TAILQ_EMPTY(&commitable_paths)) {
4127 /* No-op change; commit will be elided. */
4128 err = got_ref_delete(commit_ref, repo);
4129 if (err)
4130 goto done;
4131 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4132 goto done;
4135 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4136 if (err)
4137 goto done;
4139 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4140 if (err)
4141 goto done;
4143 if (new_logmsg)
4144 logmsg = strdup(new_logmsg);
4145 else
4146 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4147 if (logmsg == NULL)
4148 return got_error_from_errno("strdup");
4150 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4151 worktree, NULL, got_object_commit_get_author(orig_commit),
4152 got_object_commit_get_committer(orig_commit),
4153 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4154 if (err)
4155 goto done;
4157 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4158 if (err)
4159 goto done;
4161 err = got_ref_delete(commit_ref, repo);
4162 if (err)
4163 goto done;
4165 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4166 fileindex);
4167 sync_err = sync_fileindex(fileindex, fileindex_path);
4168 if (sync_err && err == NULL)
4169 err = sync_err;
4170 done:
4171 free(fileindex_path);
4172 free(head_commit_id);
4173 if (head_ref)
4174 got_ref_close(head_ref);
4175 if (err) {
4176 free(*new_commit_id);
4177 *new_commit_id = NULL;
4179 return err;
4182 const struct got_error *
4183 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4184 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4185 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4186 struct got_commit_object *orig_commit,
4187 struct got_object_id *orig_commit_id, struct got_repository *repo)
4189 const struct got_error *err;
4190 char *commit_ref_name;
4191 struct got_reference *commit_ref = NULL;
4192 struct got_object_id *commit_id = NULL;
4194 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4195 if (err)
4196 return err;
4198 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4199 if (err)
4200 goto done;
4201 err = got_ref_resolve(&commit_id, repo, commit_ref);
4202 if (err)
4203 goto done;
4204 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4205 err = got_error(GOT_ERR_REBASE_COMMITID);
4206 goto done;
4209 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4210 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4211 done:
4212 if (commit_ref)
4213 got_ref_close(commit_ref);
4214 free(commit_ref_name);
4215 free(commit_id);
4216 return err;
4219 const struct got_error *
4220 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4221 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4222 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4223 struct got_commit_object *orig_commit,
4224 struct got_object_id *orig_commit_id, const char *new_logmsg,
4225 struct got_repository *repo)
4227 const struct got_error *err;
4228 char *commit_ref_name;
4229 struct got_reference *commit_ref = NULL;
4230 struct got_object_id *commit_id = NULL;
4232 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4233 if (err)
4234 return err;
4236 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4237 if (err)
4238 goto done;
4239 err = got_ref_resolve(&commit_id, repo, commit_ref);
4240 if (err)
4241 goto done;
4242 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4243 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4244 goto done;
4247 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4248 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4249 done:
4250 if (commit_ref)
4251 got_ref_close(commit_ref);
4252 free(commit_ref_name);
4253 free(commit_id);
4254 return err;
4257 const struct got_error *
4258 got_worktree_rebase_postpone(struct got_worktree *worktree,
4259 struct got_fileindex *fileindex)
4261 if (fileindex)
4262 got_fileindex_free(fileindex);
4263 return lock_worktree(worktree, LOCK_SH);
4266 static const struct got_error *
4267 delete_ref(const char *name, struct got_repository *repo)
4269 const struct got_error *err;
4270 struct got_reference *ref;
4272 err = got_ref_open(&ref, repo, name, 0);
4273 if (err) {
4274 if (err->code == GOT_ERR_NOT_REF)
4275 return NULL;
4276 return err;
4279 err = got_ref_delete(ref, repo);
4280 got_ref_close(ref);
4281 return err;
4284 static const struct got_error *
4285 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4287 const struct got_error *err;
4288 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4289 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4291 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4292 if (err)
4293 goto done;
4294 err = delete_ref(tmp_branch_name, repo);
4295 if (err)
4296 goto done;
4298 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4299 if (err)
4300 goto done;
4301 err = delete_ref(new_base_branch_ref_name, repo);
4302 if (err)
4303 goto done;
4305 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4306 if (err)
4307 goto done;
4308 err = delete_ref(branch_ref_name, repo);
4309 if (err)
4310 goto done;
4312 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4313 if (err)
4314 goto done;
4315 err = delete_ref(commit_ref_name, repo);
4316 if (err)
4317 goto done;
4319 done:
4320 free(tmp_branch_name);
4321 free(new_base_branch_ref_name);
4322 free(branch_ref_name);
4323 free(commit_ref_name);
4324 return err;
4327 const struct got_error *
4328 got_worktree_rebase_complete(struct got_worktree *worktree,
4329 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4330 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4331 struct got_repository *repo)
4333 const struct got_error *err, *unlockerr;
4334 struct got_object_id *new_head_commit_id = NULL;
4336 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4337 if (err)
4338 return err;
4340 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4341 if (err)
4342 goto done;
4344 err = got_ref_write(rebased_branch, repo);
4345 if (err)
4346 goto done;
4348 err = got_worktree_set_head_ref(worktree, rebased_branch);
4349 if (err)
4350 goto done;
4352 err = delete_rebase_refs(worktree, repo);
4353 done:
4354 if (fileindex)
4355 got_fileindex_free(fileindex);
4356 free(new_head_commit_id);
4357 unlockerr = lock_worktree(worktree, LOCK_SH);
4358 if (unlockerr && err == NULL)
4359 err = unlockerr;
4360 return err;
4363 struct collect_revertible_paths_arg {
4364 struct got_pathlist_head *revertible_paths;
4365 struct got_worktree *worktree;
4368 static const struct got_error *
4369 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4370 struct got_object_id *blob_id, struct got_object_id *commit_id)
4372 struct collect_revertible_paths_arg *a = arg;
4373 const struct got_error *err = NULL;
4374 struct got_pathlist_entry *new = NULL;
4375 char *path = NULL;
4377 if (status != GOT_STATUS_ADD &&
4378 status != GOT_STATUS_DELETE &&
4379 status != GOT_STATUS_MODIFY &&
4380 status != GOT_STATUS_CONFLICT &&
4381 status != GOT_STATUS_MISSING)
4382 return NULL;
4384 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4385 return got_error_from_errno("asprintf");
4387 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4388 if (err || new == NULL)
4389 free(path);
4390 return err;
4393 const struct got_error *
4394 got_worktree_rebase_abort(struct got_worktree *worktree,
4395 struct got_fileindex *fileindex, struct got_repository *repo,
4396 struct got_reference *new_base_branch,
4397 got_worktree_checkout_cb progress_cb, void *progress_arg)
4399 const struct got_error *err, *unlockerr, *sync_err;
4400 struct got_reference *resolved = NULL;
4401 struct got_object_id *commit_id = NULL;
4402 char *fileindex_path = NULL;
4403 struct got_pathlist_head revertible_paths;
4404 struct got_pathlist_entry *pe;
4405 struct collect_revertible_paths_arg crp_arg;
4406 struct got_object_id *tree_id = NULL;
4408 TAILQ_INIT(&revertible_paths);
4410 err = lock_worktree(worktree, LOCK_EX);
4411 if (err)
4412 return err;
4414 err = got_ref_open(&resolved, repo,
4415 got_ref_get_symref_target(new_base_branch), 0);
4416 if (err)
4417 goto done;
4419 err = got_worktree_set_head_ref(worktree, resolved);
4420 if (err)
4421 goto done;
4424 * XXX commits to the base branch could have happened while
4425 * we were busy rebasing; should we store the original commit ID
4426 * when rebase begins and read it back here?
4428 err = got_ref_resolve(&commit_id, repo, resolved);
4429 if (err)
4430 goto done;
4432 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4433 if (err)
4434 goto done;
4436 err = got_object_id_by_path(&tree_id, repo,
4437 worktree->base_commit_id, worktree->path_prefix);
4438 if (err)
4439 goto done;
4441 err = delete_rebase_refs(worktree, repo);
4442 if (err)
4443 goto done;
4445 err = get_fileindex_path(&fileindex_path, worktree);
4446 if (err)
4447 goto done;
4449 crp_arg.revertible_paths = &revertible_paths;
4450 crp_arg.worktree = worktree;
4451 err = worktree_status(worktree, "", fileindex, repo,
4452 collect_revertible_paths, &crp_arg, NULL, NULL);
4453 if (err)
4454 goto done;
4456 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4457 err = revert_file(worktree, fileindex, pe->path,
4458 progress_cb, progress_arg, repo);
4459 if (err)
4460 goto sync;
4463 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4464 repo, progress_cb, progress_arg, NULL, NULL);
4465 sync:
4466 sync_err = sync_fileindex(fileindex, fileindex_path);
4467 if (sync_err && err == NULL)
4468 err = sync_err;
4469 done:
4470 got_ref_close(resolved);
4471 free(tree_id);
4472 free(commit_id);
4473 if (fileindex)
4474 got_fileindex_free(fileindex);
4475 free(fileindex_path);
4476 TAILQ_FOREACH(pe, &revertible_paths, entry)
4477 free((char *)pe->path);
4478 got_pathlist_free(&revertible_paths);
4480 unlockerr = lock_worktree(worktree, LOCK_SH);
4481 if (unlockerr && err == NULL)
4482 err = unlockerr;
4483 return err;
4486 const struct got_error *
4487 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4488 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4489 struct got_fileindex **fileindex, struct got_worktree *worktree,
4490 struct got_repository *repo)
4492 const struct got_error *err = NULL;
4493 char *tmp_branch_name = NULL;
4494 char *branch_ref_name = NULL;
4495 char *base_commit_ref_name = NULL;
4496 char *fileindex_path = NULL;
4497 struct check_rebase_ok_arg ok_arg;
4498 struct got_reference *wt_branch = NULL;
4499 struct got_reference *base_commit_ref = NULL;
4501 *tmp_branch = NULL;
4502 *branch_ref = NULL;
4503 *base_commit_id = NULL;
4504 *fileindex = NULL;
4506 err = lock_worktree(worktree, LOCK_EX);
4507 if (err)
4508 return err;
4510 err = open_fileindex(fileindex, &fileindex_path, worktree);
4511 if (err)
4512 goto done;
4514 ok_arg.worktree = worktree;
4515 ok_arg.repo = repo;
4516 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4517 &ok_arg);
4518 if (err)
4519 goto done;
4521 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4522 if (err)
4523 goto done;
4525 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4526 if (err)
4527 goto done;
4529 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4530 worktree);
4531 if (err)
4532 goto done;
4534 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4535 0);
4536 if (err)
4537 goto done;
4539 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4540 if (err)
4541 goto done;
4543 err = got_ref_write(*branch_ref, repo);
4544 if (err)
4545 goto done;
4547 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4548 worktree->base_commit_id);
4549 if (err)
4550 goto done;
4551 err = got_ref_write(base_commit_ref, repo);
4552 if (err)
4553 goto done;
4554 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4555 if (*base_commit_id == NULL) {
4556 err = got_error_from_errno("got_object_id_dup");
4557 goto done;
4560 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4561 worktree->base_commit_id);
4562 if (err)
4563 goto done;
4564 err = got_ref_write(*tmp_branch, repo);
4565 if (err)
4566 goto done;
4568 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4569 if (err)
4570 goto done;
4571 done:
4572 free(fileindex_path);
4573 free(tmp_branch_name);
4574 free(branch_ref_name);
4575 free(base_commit_ref_name);
4576 if (wt_branch)
4577 got_ref_close(wt_branch);
4578 if (err) {
4579 if (*branch_ref) {
4580 got_ref_close(*branch_ref);
4581 *branch_ref = NULL;
4583 if (*tmp_branch) {
4584 got_ref_close(*tmp_branch);
4585 *tmp_branch = NULL;
4587 free(*base_commit_id);
4588 if (*fileindex) {
4589 got_fileindex_free(*fileindex);
4590 *fileindex = NULL;
4592 lock_worktree(worktree, LOCK_SH);
4594 return err;
4597 const struct got_error *
4598 got_worktree_histedit_postpone(struct got_worktree *worktree,
4599 struct got_fileindex *fileindex)
4601 if (fileindex)
4602 got_fileindex_free(fileindex);
4603 return lock_worktree(worktree, LOCK_SH);
4606 const struct got_error *
4607 got_worktree_histedit_in_progress(int *in_progress,
4608 struct got_worktree *worktree)
4610 const struct got_error *err;
4611 char *tmp_branch_name = NULL;
4613 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4614 if (err)
4615 return err;
4617 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4618 free(tmp_branch_name);
4619 return NULL;
4622 const struct got_error *
4623 got_worktree_histedit_continue(struct got_object_id **commit_id,
4624 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4625 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4626 struct got_worktree *worktree, struct got_repository *repo)
4628 const struct got_error *err;
4629 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4630 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4631 struct got_reference *commit_ref = NULL;
4632 struct got_reference *base_commit_ref = NULL;
4633 char *fileindex_path = NULL;
4635 *commit_id = NULL;
4636 *tmp_branch = NULL;
4637 *base_commit_id = NULL;
4638 *fileindex = NULL;
4640 err = lock_worktree(worktree, LOCK_EX);
4641 if (err)
4642 return err;
4644 err = open_fileindex(fileindex, &fileindex_path, worktree);
4645 if (err)
4646 goto done;
4648 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4649 if (err)
4650 return err;
4652 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4653 if (err)
4654 goto done;
4656 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4657 if (err)
4658 goto done;
4660 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4661 worktree);
4662 if (err)
4663 goto done;
4665 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4666 if (err)
4667 goto done;
4669 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4670 if (err)
4671 goto done;
4672 err = got_ref_resolve(commit_id, repo, commit_ref);
4673 if (err)
4674 goto done;
4676 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4677 if (err)
4678 goto done;
4679 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4680 if (err)
4681 goto done;
4683 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4684 if (err)
4685 goto done;
4686 done:
4687 free(commit_ref_name);
4688 free(branch_ref_name);
4689 free(fileindex_path);
4690 if (commit_ref)
4691 got_ref_close(commit_ref);
4692 if (base_commit_ref)
4693 got_ref_close(base_commit_ref);
4694 if (err) {
4695 free(*commit_id);
4696 *commit_id = NULL;
4697 free(*base_commit_id);
4698 *base_commit_id = NULL;
4699 if (*tmp_branch) {
4700 got_ref_close(*tmp_branch);
4701 *tmp_branch = NULL;
4703 if (*fileindex) {
4704 got_fileindex_free(*fileindex);
4705 *fileindex = NULL;
4707 lock_worktree(worktree, LOCK_EX);
4709 return err;
4712 static const struct got_error *
4713 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4715 const struct got_error *err;
4716 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4717 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4719 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4720 if (err)
4721 goto done;
4722 err = delete_ref(tmp_branch_name, repo);
4723 if (err)
4724 goto done;
4726 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4727 worktree);
4728 if (err)
4729 goto done;
4730 err = delete_ref(base_commit_ref_name, repo);
4731 if (err)
4732 goto done;
4734 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4735 if (err)
4736 goto done;
4737 err = delete_ref(branch_ref_name, repo);
4738 if (err)
4739 goto done;
4741 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4742 if (err)
4743 goto done;
4744 err = delete_ref(commit_ref_name, repo);
4745 if (err)
4746 goto done;
4747 done:
4748 free(tmp_branch_name);
4749 free(base_commit_ref_name);
4750 free(branch_ref_name);
4751 free(commit_ref_name);
4752 return err;
4755 const struct got_error *
4756 got_worktree_histedit_abort(struct got_worktree *worktree,
4757 struct got_fileindex *fileindex, struct got_repository *repo,
4758 struct got_reference *branch, struct got_object_id *base_commit_id,
4759 got_worktree_checkout_cb progress_cb, void *progress_arg)
4761 const struct got_error *err, *unlockerr, *sync_err;
4762 struct got_reference *resolved = NULL;
4763 char *fileindex_path = NULL;
4764 struct got_pathlist_head revertible_paths;
4765 struct got_pathlist_entry *pe;
4766 struct collect_revertible_paths_arg crp_arg;
4767 struct got_object_id *tree_id = NULL;
4769 TAILQ_INIT(&revertible_paths);
4771 err = lock_worktree(worktree, LOCK_EX);
4772 if (err)
4773 return err;
4775 err = got_ref_open(&resolved, repo,
4776 got_ref_get_symref_target(branch), 0);
4777 if (err)
4778 goto done;
4780 err = got_worktree_set_head_ref(worktree, resolved);
4781 if (err)
4782 goto done;
4784 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4785 if (err)
4786 goto done;
4788 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4789 worktree->path_prefix);
4790 if (err)
4791 goto done;
4793 err = delete_histedit_refs(worktree, repo);
4794 if (err)
4795 goto done;
4797 err = get_fileindex_path(&fileindex_path, worktree);
4798 if (err)
4799 goto done;
4801 crp_arg.revertible_paths = &revertible_paths;
4802 crp_arg.worktree = worktree;
4803 err = worktree_status(worktree, "", fileindex, repo,
4804 collect_revertible_paths, &crp_arg, NULL, NULL);
4805 if (err)
4806 goto done;
4808 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4809 err = revert_file(worktree, fileindex, pe->path,
4810 progress_cb, progress_arg, repo);
4811 if (err)
4812 goto sync;
4815 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4816 repo, progress_cb, progress_arg, NULL, NULL);
4817 sync:
4818 sync_err = sync_fileindex(fileindex, fileindex_path);
4819 if (sync_err && err == NULL)
4820 err = sync_err;
4821 done:
4822 got_ref_close(resolved);
4823 free(tree_id);
4824 free(fileindex_path);
4825 TAILQ_FOREACH(pe, &revertible_paths, entry)
4826 free((char *)pe->path);
4827 got_pathlist_free(&revertible_paths);
4829 unlockerr = lock_worktree(worktree, LOCK_SH);
4830 if (unlockerr && err == NULL)
4831 err = unlockerr;
4832 return err;
4835 const struct got_error *
4836 got_worktree_histedit_complete(struct got_worktree *worktree,
4837 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4838 struct got_reference *edited_branch, struct got_repository *repo)
4840 const struct got_error *err, *unlockerr;
4841 struct got_object_id *new_head_commit_id = NULL;
4842 struct got_reference *resolved = NULL;
4844 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4845 if (err)
4846 return err;
4848 err = got_ref_open(&resolved, repo,
4849 got_ref_get_symref_target(edited_branch), 0);
4850 if (err)
4851 goto done;
4853 err = got_ref_change_ref(resolved, new_head_commit_id);
4854 if (err)
4855 goto done;
4857 err = got_ref_write(resolved, repo);
4858 if (err)
4859 goto done;
4861 err = got_worktree_set_head_ref(worktree, resolved);
4862 if (err)
4863 goto done;
4865 err = delete_histedit_refs(worktree, repo);
4866 done:
4867 if (fileindex)
4868 got_fileindex_free(fileindex);
4869 free(new_head_commit_id);
4870 unlockerr = lock_worktree(worktree, LOCK_SH);
4871 if (unlockerr && err == NULL)
4872 err = unlockerr;
4873 return err;
4876 const struct got_error *
4877 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4878 struct got_object_id *commit_id, struct got_repository *repo)
4880 const struct got_error *err;
4881 char *commit_ref_name;
4883 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4884 if (err)
4885 return err;
4887 err = store_commit_id(commit_ref_name, commit_id, repo);
4888 if (err)
4889 goto done;
4891 err = delete_ref(commit_ref_name, repo);
4892 done:
4893 free(commit_ref_name);
4894 return err;