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 err = got_error_from_errno("asprintf");
2324 goto done;
2326 workdir = opendir(ondisk_path);
2327 if (workdir == NULL) {
2328 if (errno != ENOTDIR && errno != ENOENT)
2329 err = got_error_from_errno2("opendir", ondisk_path);
2330 else
2331 err = report_single_file_status(path, ondisk_path,
2332 fileindex, status_cb, status_arg, repo);
2333 } else {
2334 fdiff_cb.diff_old_new = status_old_new;
2335 fdiff_cb.diff_old = status_old;
2336 fdiff_cb.diff_new = status_new;
2337 arg.fileindex = fileindex;
2338 arg.worktree = worktree;
2339 arg.status_path = path;
2340 arg.status_path_len = strlen(path);
2341 arg.repo = repo;
2342 arg.status_cb = status_cb;
2343 arg.status_arg = status_arg;
2344 arg.cancel_cb = cancel_cb;
2345 arg.cancel_arg = cancel_arg;
2346 err = got_fileindex_diff_dir(fileindex, workdir,
2347 worktree->root_path, path, repo, &fdiff_cb, &arg);
2349 done:
2350 if (workdir)
2351 closedir(workdir);
2352 free(ondisk_path);
2353 return err;
2356 const struct got_error *
2357 got_worktree_status(struct got_worktree *worktree,
2358 struct got_pathlist_head *paths, struct got_repository *repo,
2359 got_worktree_status_cb status_cb, void *status_arg,
2360 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2362 const struct got_error *err = NULL;
2363 char *fileindex_path = NULL;
2364 struct got_fileindex *fileindex = NULL;
2365 struct got_pathlist_entry *pe;
2367 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2368 if (err)
2369 return err;
2371 TAILQ_FOREACH(pe, paths, entry) {
2372 err = worktree_status(worktree, pe->path, fileindex, repo,
2373 status_cb, status_arg, cancel_cb, cancel_arg);
2374 if (err)
2375 break;
2377 free(fileindex_path);
2378 got_fileindex_free(fileindex);
2379 return err;
2382 const struct got_error *
2383 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2384 const char *arg)
2386 const struct got_error *err = NULL;
2387 char *resolved, *cwd = NULL, *path = NULL;
2388 size_t len;
2390 *wt_path = NULL;
2392 resolved = realpath(arg, NULL);
2393 if (resolved == NULL) {
2394 if (errno != ENOENT)
2395 return got_error_from_errno2("realpath", arg);
2396 cwd = getcwd(NULL, 0);
2397 if (cwd == NULL)
2398 return got_error_from_errno("getcwd");
2399 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2400 err = got_error_from_errno("asprintf");
2401 goto done;
2405 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2406 strlen(got_worktree_get_root_path(worktree)))) {
2407 err = got_error(GOT_ERR_BAD_PATH);
2408 goto done;
2411 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2412 err = got_path_skip_common_ancestor(&path,
2413 got_worktree_get_root_path(worktree), resolved);
2414 if (err)
2415 goto done;
2416 } else {
2417 path = strdup("");
2418 if (path == NULL) {
2419 err = got_error_from_errno("strdup");
2420 goto done;
2424 /* XXX status walk can't deal with trailing slash! */
2425 len = strlen(path);
2426 while (len > 0 && path[len - 1] == '/') {
2427 path[len - 1] = '\0';
2428 len--;
2430 done:
2431 free(resolved);
2432 free(cwd);
2433 if (err == NULL)
2434 *wt_path = path;
2435 else
2436 free(path);
2437 return err;
2440 static const struct got_error *
2441 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2442 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2443 struct got_repository *repo)
2445 const struct got_error *err = NULL;
2446 struct got_fileindex_entry *ie;
2448 /* Re-adding an existing entry is a no-op. */
2449 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2450 return NULL;
2452 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2453 if (err)
2454 return err;
2456 err = got_fileindex_entry_add(fileindex, ie);
2457 if (err) {
2458 got_fileindex_entry_free(ie);
2459 return err;
2462 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2465 const struct got_error *
2466 got_worktree_schedule_add(struct got_worktree *worktree,
2467 struct got_pathlist_head *ondisk_paths,
2468 got_worktree_status_cb status_cb, void *status_arg,
2469 struct got_repository *repo)
2471 struct got_fileindex *fileindex = NULL;
2472 char *fileindex_path = NULL;
2473 const struct got_error *err = NULL, *sync_err, *unlockerr;
2474 struct got_pathlist_entry *pe;
2476 err = lock_worktree(worktree, LOCK_EX);
2477 if (err)
2478 return err;
2480 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2481 if (err)
2482 goto done;
2484 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2485 char *relpath;
2486 err = got_path_skip_common_ancestor(&relpath,
2487 got_worktree_get_root_path(worktree), pe->path);
2488 if (err)
2489 break;
2490 err = schedule_addition(pe->path, fileindex, relpath,
2491 status_cb, status_arg, repo);
2492 free(relpath);
2493 if (err)
2494 break;
2496 sync_err = sync_fileindex(fileindex, fileindex_path);
2497 if (sync_err && err == NULL)
2498 err = sync_err;
2499 done:
2500 free(fileindex_path);
2501 if (fileindex)
2502 got_fileindex_free(fileindex);
2503 unlockerr = lock_worktree(worktree, LOCK_SH);
2504 if (unlockerr && err == NULL)
2505 err = unlockerr;
2506 return err;
2509 static const struct got_error *
2510 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2511 const char *relpath, int delete_local_mods,
2512 got_worktree_status_cb status_cb, void *status_arg,
2513 struct got_repository *repo)
2515 const struct got_error *err = NULL;
2516 struct got_fileindex_entry *ie = NULL;
2517 unsigned char status;
2518 struct stat sb;
2520 ie = got_fileindex_entry_get(fileindex, relpath);
2521 if (ie == NULL)
2522 return got_error(GOT_ERR_BAD_PATH);
2524 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2525 if (err)
2526 return err;
2528 if (status != GOT_STATUS_NO_CHANGE) {
2529 if (status == GOT_STATUS_DELETE)
2530 return got_error_set_errno(ENOENT, ondisk_path);
2531 if (status != GOT_STATUS_MODIFY)
2532 return got_error(GOT_ERR_FILE_STATUS);
2533 if (!delete_local_mods)
2534 return got_error(GOT_ERR_FILE_MODIFIED);
2537 if (unlink(ondisk_path) != 0)
2538 return got_error_from_errno2("unlink", ondisk_path);
2540 got_fileindex_entry_mark_deleted_from_disk(ie);
2541 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2544 const struct got_error *
2545 got_worktree_schedule_delete(struct got_worktree *worktree,
2546 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2547 got_worktree_status_cb status_cb, void *status_arg,
2548 struct got_repository *repo)
2550 struct got_fileindex *fileindex = NULL;
2551 char *fileindex_path = NULL;
2552 const struct got_error *err = NULL, *sync_err, *unlockerr;
2553 struct got_pathlist_entry *pe;
2555 err = lock_worktree(worktree, LOCK_EX);
2556 if (err)
2557 return err;
2559 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2560 if (err)
2561 goto done;
2563 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2564 char *relpath;
2565 err = got_path_skip_common_ancestor(&relpath,
2566 got_worktree_get_root_path(worktree), pe->path);
2567 if (err)
2568 break;
2569 err = schedule_for_deletion(pe->path, fileindex, relpath,
2570 delete_local_mods, status_cb, status_arg, repo);
2571 free(relpath);
2572 if (err)
2573 break;
2575 sync_err = sync_fileindex(fileindex, fileindex_path);
2576 if (sync_err && err == NULL)
2577 err = sync_err;
2578 done:
2579 free(fileindex_path);
2580 if (fileindex)
2581 got_fileindex_free(fileindex);
2582 unlockerr = lock_worktree(worktree, LOCK_SH);
2583 if (unlockerr && err == NULL)
2584 err = unlockerr;
2585 return err;
2588 static const struct got_error *
2589 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2590 const char *ondisk_path,
2591 got_worktree_checkout_cb progress_cb, void *progress_arg,
2592 struct got_repository *repo)
2594 const struct got_error *err = NULL;
2595 char *relpath = NULL, *parent_path = NULL;
2596 struct got_fileindex_entry *ie;
2597 struct got_tree_object *tree = NULL;
2598 struct got_object_id *tree_id = NULL;
2599 const struct got_tree_entry *te;
2600 char *tree_path = NULL, *te_name;
2601 struct got_blob_object *blob = NULL;
2602 unsigned char status;
2603 struct stat sb;
2605 err = got_path_skip_common_ancestor(&relpath,
2606 got_worktree_get_root_path(worktree), ondisk_path);
2607 if (err)
2608 goto done;
2610 ie = got_fileindex_entry_get(fileindex, relpath);
2611 if (ie == NULL) {
2612 err = got_error(GOT_ERR_BAD_PATH);
2613 goto done;
2616 /* Construct in-repository path of tree which contains this blob. */
2617 err = got_path_dirname(&parent_path, ie->path);
2618 if (err) {
2619 if (err->code != GOT_ERR_BAD_PATH)
2620 goto done;
2621 parent_path = strdup("/");
2622 if (parent_path == NULL) {
2623 err = got_error_from_errno("strdup");
2624 goto done;
2627 if (got_path_is_root_dir(worktree->path_prefix)) {
2628 tree_path = strdup(parent_path);
2629 if (tree_path == NULL) {
2630 err = got_error_from_errno("strdup");
2631 goto done;
2633 } else {
2634 if (got_path_is_root_dir(parent_path)) {
2635 tree_path = strdup(worktree->path_prefix);
2636 if (tree_path == NULL) {
2637 err = got_error_from_errno("strdup");
2638 goto done;
2640 } else {
2641 if (asprintf(&tree_path, "%s/%s",
2642 worktree->path_prefix, parent_path) == -1) {
2643 err = got_error_from_errno("asprintf");
2644 goto done;
2649 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2650 tree_path);
2651 if (err)
2652 goto done;
2654 err = got_object_open_as_tree(&tree, repo, tree_id);
2655 if (err)
2656 goto done;
2658 te_name = basename(ie->path);
2659 if (te_name == NULL) {
2660 err = got_error_from_errno2("basename", ie->path);
2661 goto done;
2664 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2665 if (err)
2666 goto done;
2668 te = got_object_tree_find_entry(tree, te_name);
2669 if (te == NULL && status != GOT_STATUS_ADD) {
2670 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2671 goto done;
2674 switch (status) {
2675 case GOT_STATUS_ADD:
2676 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2677 if (err)
2678 goto done;
2679 got_fileindex_entry_remove(fileindex, ie);
2680 break;
2681 case GOT_STATUS_DELETE:
2682 case GOT_STATUS_MODIFY:
2683 case GOT_STATUS_CONFLICT:
2684 case GOT_STATUS_MISSING: {
2685 struct got_object_id id;
2686 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2687 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2688 if (err)
2689 goto done;
2690 err = install_blob(worktree, ondisk_path, ie->path,
2691 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2692 progress_arg);
2693 if (err)
2694 goto done;
2695 if (status == GOT_STATUS_DELETE) {
2696 err = update_blob_fileindex_entry(worktree,
2697 fileindex, ie, ondisk_path, ie->path, blob, 1);
2698 if (err)
2699 goto done;
2701 break;
2703 default:
2704 goto done;
2706 done:
2707 free(relpath);
2708 free(parent_path);
2709 free(tree_path);
2710 if (blob)
2711 got_object_blob_close(blob);
2712 if (tree)
2713 got_object_tree_close(tree);
2714 free(tree_id);
2715 return err;
2718 const struct got_error *
2719 got_worktree_revert(struct got_worktree *worktree,
2720 struct got_pathlist_head *ondisk_paths,
2721 got_worktree_checkout_cb progress_cb, void *progress_arg,
2722 struct got_repository *repo)
2724 struct got_fileindex *fileindex = NULL;
2725 char *fileindex_path = NULL;
2726 const struct got_error *err = NULL, *unlockerr = NULL;
2727 const struct got_error *sync_err = NULL;
2728 struct got_pathlist_entry *pe;
2730 err = lock_worktree(worktree, LOCK_EX);
2731 if (err)
2732 return err;
2734 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2735 if (err)
2736 goto done;
2738 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2739 err = revert_file(worktree, fileindex, pe->path,
2740 progress_cb, progress_arg, repo);
2741 if (err)
2742 break;
2744 sync_err = sync_fileindex(fileindex, fileindex_path);
2745 if (sync_err && err == NULL)
2746 err = sync_err;
2747 done:
2748 free(fileindex_path);
2749 if (fileindex)
2750 got_fileindex_free(fileindex);
2751 unlockerr = lock_worktree(worktree, LOCK_SH);
2752 if (unlockerr && err == NULL)
2753 err = unlockerr;
2754 return err;
2757 static void
2758 free_commitable(struct got_commitable *ct)
2760 free(ct->path);
2761 free(ct->in_repo_path);
2762 free(ct->ondisk_path);
2763 free(ct->blob_id);
2764 free(ct->base_blob_id);
2765 free(ct->base_commit_id);
2766 free(ct);
2769 struct collect_commitables_arg {
2770 struct got_pathlist_head *commitable_paths;
2771 struct got_repository *repo;
2772 struct got_worktree *worktree;
2775 static const struct got_error *
2776 collect_commitables(void *arg, unsigned char status, const char *relpath,
2777 struct got_object_id *blob_id, struct got_object_id *commit_id)
2779 struct collect_commitables_arg *a = arg;
2780 const struct got_error *err = NULL;
2781 struct got_commitable *ct = NULL;
2782 struct got_pathlist_entry *new = NULL;
2783 char *parent_path = NULL, *path = NULL;
2784 struct stat sb;
2786 if (status == GOT_STATUS_CONFLICT)
2787 return got_error(GOT_ERR_COMMIT_CONFLICT);
2789 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2790 status != GOT_STATUS_DELETE)
2791 return NULL;
2793 if (asprintf(&path, "/%s", relpath) == -1) {
2794 err = got_error_from_errno("asprintf");
2795 goto done;
2797 if (strcmp(path, "/") == 0) {
2798 parent_path = strdup("");
2799 if (parent_path == NULL)
2800 return got_error_from_errno("strdup");
2801 } else {
2802 err = got_path_dirname(&parent_path, path);
2803 if (err)
2804 return err;
2807 ct = calloc(1, sizeof(*ct));
2808 if (ct == NULL) {
2809 err = got_error_from_errno("calloc");
2810 goto done;
2813 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2814 relpath) == -1) {
2815 err = got_error_from_errno("asprintf");
2816 goto done;
2818 if (status == GOT_STATUS_DELETE) {
2819 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2820 } else {
2821 if (lstat(ct->ondisk_path, &sb) != 0) {
2822 err = got_error_from_errno2("lstat", ct->ondisk_path);
2823 goto done;
2825 ct->mode = sb.st_mode;
2828 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2829 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2830 relpath) == -1) {
2831 err = got_error_from_errno("asprintf");
2832 goto done;
2835 ct->status = status;
2836 ct->blob_id = NULL; /* will be filled in when blob gets created */
2837 if (ct->status != GOT_STATUS_ADD) {
2838 ct->base_blob_id = got_object_id_dup(blob_id);
2839 if (ct->base_blob_id == NULL) {
2840 err = got_error_from_errno("got_object_id_dup");
2841 goto done;
2843 ct->base_commit_id = got_object_id_dup(commit_id);
2844 if (ct->base_commit_id == NULL) {
2845 err = got_error_from_errno("got_object_id_dup");
2846 goto done;
2849 ct->path = strdup(path);
2850 if (ct->path == NULL) {
2851 err = got_error_from_errno("strdup");
2852 goto done;
2854 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2855 done:
2856 if (ct && (err || new == NULL))
2857 free_commitable(ct);
2858 free(parent_path);
2859 free(path);
2860 return err;
2863 static const struct got_error *write_tree(struct got_object_id **,
2864 struct got_tree_object *, const char *, struct got_pathlist_head *,
2865 got_worktree_status_cb status_cb, void *status_arg,
2866 struct got_repository *);
2868 static const struct got_error *
2869 write_subtree(struct got_object_id **new_subtree_id,
2870 struct got_tree_entry *te, const char *parent_path,
2871 struct got_pathlist_head *commitable_paths,
2872 got_worktree_status_cb status_cb, void *status_arg,
2873 struct got_repository *repo)
2875 const struct got_error *err = NULL;
2876 struct got_tree_object *subtree;
2877 char *subpath;
2879 if (asprintf(&subpath, "%s%s%s", parent_path,
2880 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2881 return got_error_from_errno("asprintf");
2883 err = got_object_open_as_tree(&subtree, repo, te->id);
2884 if (err)
2885 return err;
2887 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2888 status_cb, status_arg, repo);
2889 got_object_tree_close(subtree);
2890 free(subpath);
2891 return err;
2894 static const struct got_error *
2895 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2897 const struct got_error *err = NULL;
2898 char *ct_parent_path = NULL;
2900 *match = 0;
2902 if (strchr(ct->in_repo_path, '/') == NULL) {
2903 *match = got_path_is_root_dir(path);
2904 return NULL;
2907 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2908 if (err)
2909 return err;
2910 *match = (strcmp(path, ct_parent_path) == 0);
2911 free(ct_parent_path);
2912 return err;
2915 static mode_t
2916 get_ct_file_mode(struct got_commitable *ct)
2918 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2921 static const struct got_error *
2922 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2923 struct got_tree_entry *te, struct got_commitable *ct)
2925 const struct got_error *err = NULL;
2927 *new_te = NULL;
2929 err = got_object_tree_entry_dup(new_te, te);
2930 if (err)
2931 goto done;
2933 (*new_te)->mode = get_ct_file_mode(ct);
2935 free((*new_te)->id);
2936 (*new_te)->id = got_object_id_dup(ct->blob_id);
2937 if ((*new_te)->id == NULL) {
2938 err = got_error_from_errno("got_object_id_dup");
2939 goto done;
2941 done:
2942 if (err && *new_te) {
2943 got_object_tree_entry_close(*new_te);
2944 *new_te = NULL;
2946 return err;
2949 static const struct got_error *
2950 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2951 struct got_commitable *ct)
2953 const struct got_error *err = NULL;
2954 char *ct_name;
2956 *new_te = NULL;
2958 *new_te = calloc(1, sizeof(**new_te));
2959 if (*new_te == NULL)
2960 return got_error_from_errno("calloc");
2962 ct_name = basename(ct->path);
2963 if (ct_name == NULL) {
2964 err = got_error_from_errno2("basename", ct->path);
2965 goto done;
2967 (*new_te)->name = strdup(ct_name);
2968 if ((*new_te)->name == NULL) {
2969 err = got_error_from_errno("strdup");
2970 goto done;
2973 (*new_te)->mode = get_ct_file_mode(ct);
2975 (*new_te)->id = got_object_id_dup(ct->blob_id);
2976 if ((*new_te)->id == NULL) {
2977 err = got_error_from_errno("got_object_id_dup");
2978 goto done;
2980 done:
2981 if (err && *new_te) {
2982 got_object_tree_entry_close(*new_te);
2983 *new_te = NULL;
2985 return err;
2988 static const struct got_error *
2989 insert_tree_entry(struct got_tree_entry *new_te,
2990 struct got_pathlist_head *paths)
2992 const struct got_error *err = NULL;
2993 struct got_pathlist_entry *new_pe;
2995 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2996 if (err)
2997 return err;
2998 if (new_pe == NULL)
2999 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3000 return NULL;
3003 static const struct got_error *
3004 report_ct_status(struct got_commitable *ct,
3005 got_worktree_status_cb status_cb, void *status_arg)
3007 const char *ct_path = ct->path;
3008 while (ct_path[0] == '/')
3009 ct_path++;
3010 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
3013 static const struct got_error *
3014 match_modified_subtree(int *modified, struct got_tree_entry *te,
3015 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3017 const struct got_error *err = NULL;
3018 struct got_pathlist_entry *pe;
3019 char *te_path;
3021 *modified = 0;
3023 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3024 got_path_is_root_dir(base_tree_path) ? "" : "/",
3025 te->name) == -1)
3026 return got_error_from_errno("asprintf");
3028 TAILQ_FOREACH(pe, commitable_paths, entry) {
3029 struct got_commitable *ct = pe->data;
3030 *modified = got_path_is_child(ct->in_repo_path, te_path,
3031 strlen(te_path));
3032 if (*modified)
3033 break;
3036 free(te_path);
3037 return err;
3040 static const struct got_error *
3041 match_deleted_or_modified_ct(struct got_commitable **ctp,
3042 struct got_tree_entry *te, const char *base_tree_path,
3043 struct got_pathlist_head *commitable_paths)
3045 const struct got_error *err = NULL;
3046 struct got_pathlist_entry *pe;
3048 *ctp = NULL;
3050 TAILQ_FOREACH(pe, commitable_paths, entry) {
3051 struct got_commitable *ct = pe->data;
3052 char *ct_name = NULL;
3053 int path_matches;
3055 if (ct->status != GOT_STATUS_MODIFY &&
3056 ct->status != GOT_STATUS_DELETE)
3057 continue;
3059 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3060 continue;
3062 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3063 if (err)
3064 return err;
3065 if (!path_matches)
3066 continue;
3068 ct_name = basename(pe->path);
3069 if (ct_name == NULL)
3070 return got_error_from_errno2("basename", pe->path);
3072 if (strcmp(te->name, ct_name) != 0)
3073 continue;
3075 *ctp = ct;
3076 break;
3079 return err;
3082 static const struct got_error *
3083 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3084 const char *child_path, const char *path_base_tree,
3085 struct got_pathlist_head *commitable_paths,
3086 got_worktree_status_cb status_cb, void *status_arg,
3087 struct got_repository *repo)
3089 const struct got_error *err = NULL;
3090 struct got_tree_entry *new_te;
3091 char *subtree_path;
3093 *new_tep = NULL;
3095 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3096 got_path_is_root_dir(path_base_tree) ? "" : "/",
3097 child_path) == -1)
3098 return got_error_from_errno("asprintf");
3100 new_te = calloc(1, sizeof(*new_te));
3101 new_te->mode = S_IFDIR;
3102 new_te->name = strdup(child_path);
3103 if (new_te->name == NULL) {
3104 err = got_error_from_errno("strdup");
3105 got_object_tree_entry_close(new_te);
3106 goto done;
3108 err = write_tree(&new_te->id, NULL, subtree_path,
3109 commitable_paths, status_cb, status_arg, repo);
3110 if (err) {
3111 got_object_tree_entry_close(new_te);
3112 goto done;
3114 done:
3115 free(subtree_path);
3116 if (err == NULL)
3117 *new_tep = new_te;
3118 return err;
3121 static const struct got_error *
3122 write_tree(struct got_object_id **new_tree_id,
3123 struct got_tree_object *base_tree, const char *path_base_tree,
3124 struct got_pathlist_head *commitable_paths,
3125 got_worktree_status_cb status_cb, void *status_arg,
3126 struct got_repository *repo)
3128 const struct got_error *err = NULL;
3129 const struct got_tree_entries *base_entries = NULL;
3130 struct got_pathlist_head paths;
3131 struct got_tree_entries new_tree_entries;
3132 struct got_tree_entry *te, *new_te = NULL;
3133 struct got_pathlist_entry *pe;
3135 TAILQ_INIT(&paths);
3136 new_tree_entries.nentries = 0;
3137 SIMPLEQ_INIT(&new_tree_entries.head);
3139 /* Insert, and recurse into, newly added entries first. */
3140 TAILQ_FOREACH(pe, commitable_paths, entry) {
3141 struct got_commitable *ct = pe->data;
3142 char *child_path = NULL, *slash;
3144 if (ct->status != GOT_STATUS_ADD ||
3145 (ct->flags & GOT_COMMITABLE_ADDED))
3146 continue;
3148 if (!got_path_is_child(pe->path, path_base_tree,
3149 strlen(path_base_tree)))
3150 continue;
3152 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3153 pe->path);
3154 if (err)
3155 goto done;
3157 slash = strchr(child_path, '/');
3158 if (slash == NULL) {
3159 err = alloc_added_blob_tree_entry(&new_te, ct);
3160 if (err)
3161 goto done;
3162 err = report_ct_status(ct, status_cb, status_arg);
3163 if (err)
3164 goto done;
3165 ct->flags |= GOT_COMMITABLE_ADDED;
3166 err = insert_tree_entry(new_te, &paths);
3167 if (err)
3168 goto done;
3169 } else {
3170 *slash = '\0'; /* trim trailing path components */
3171 if (base_tree == NULL ||
3172 got_object_tree_find_entry(base_tree, child_path)
3173 == NULL) {
3174 err = make_subtree_for_added_blob(&new_te,
3175 child_path, path_base_tree,
3176 commitable_paths, status_cb, status_arg,
3177 repo);
3178 if (err)
3179 goto done;
3180 err = insert_tree_entry(new_te, &paths);
3181 if (err)
3182 goto done;
3187 if (base_tree) {
3188 /* Handle modified and deleted entries. */
3189 base_entries = got_object_tree_get_entries(base_tree);
3190 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3191 struct got_commitable *ct = NULL;
3193 if (S_ISDIR(te->mode)) {
3194 int modified;
3195 err = got_object_tree_entry_dup(&new_te, te);
3196 if (err)
3197 goto done;
3198 err = match_modified_subtree(&modified, te,
3199 path_base_tree, commitable_paths);
3200 if (err)
3201 goto done;
3202 /* Avoid recursion into unmodified subtrees. */
3203 if (modified) {
3204 free(new_te->id);
3205 err = write_subtree(&new_te->id, te,
3206 path_base_tree, commitable_paths,
3207 status_cb, status_arg, repo);
3208 if (err)
3209 goto done;
3211 err = insert_tree_entry(new_te, &paths);
3212 if (err)
3213 goto done;
3214 continue;
3217 err = match_deleted_or_modified_ct(&ct, te,
3218 path_base_tree, commitable_paths);
3219 if (ct) {
3220 /* NB: Deleted entries get dropped here. */
3221 if (ct->status == GOT_STATUS_MODIFY) {
3222 err = alloc_modified_blob_tree_entry(
3223 &new_te, te, ct);
3224 if (err)
3225 goto done;
3226 err = insert_tree_entry(new_te, &paths);
3227 if (err)
3228 goto done;
3230 err = report_ct_status(ct, status_cb,
3231 status_arg);
3232 if (err)
3233 goto done;
3234 } else {
3235 /* Entry is unchanged; just copy it. */
3236 err = got_object_tree_entry_dup(&new_te, te);
3237 if (err)
3238 goto done;
3239 err = insert_tree_entry(new_te, &paths);
3240 if (err)
3241 goto done;
3246 /* Write new list of entries; deleted entries have been dropped. */
3247 TAILQ_FOREACH(pe, &paths, entry) {
3248 struct got_tree_entry *te = pe->data;
3249 new_tree_entries.nentries++;
3250 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3252 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3253 done:
3254 got_object_tree_entries_close(&new_tree_entries);
3255 got_pathlist_free(&paths);
3256 return err;
3259 static const struct got_error *
3260 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3261 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3263 const struct got_error *err = NULL;
3264 struct got_pathlist_entry *pe;
3266 TAILQ_FOREACH(pe, commitable_paths, entry) {
3267 struct got_fileindex_entry *ie;
3268 struct got_commitable *ct = pe->data;
3270 ie = got_fileindex_entry_get(fileindex, pe->path);
3271 if (ie) {
3272 if (ct->status == GOT_STATUS_DELETE) {
3273 got_fileindex_entry_remove(fileindex, ie);
3274 got_fileindex_entry_free(ie);
3275 } else
3276 err = got_fileindex_entry_update(ie,
3277 ct->ondisk_path, ct->blob_id->sha1,
3278 new_base_commit_id->sha1, 1);
3279 } else {
3280 err = got_fileindex_entry_alloc(&ie,
3281 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3282 new_base_commit_id->sha1);
3283 if (err)
3284 break;
3285 err = got_fileindex_entry_add(fileindex, ie);
3286 if (err)
3287 break;
3290 return err;
3293 static const struct got_error *
3294 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3295 struct got_object_id *head_commit_id)
3297 const struct got_error *err = NULL;
3298 struct got_object_id *id_in_head = NULL, *id = NULL;
3299 struct got_commit_object *commit = NULL;
3300 char *path = NULL;
3301 const char *ct_path = ct->in_repo_path;
3303 while (ct_path[0] == '/')
3304 ct_path++;
3307 * Ensure that no modifications were made to files *and their parents*
3308 * in commits between the file's base commit and the branch head.
3310 * Checking the parents is important for detecting conflicting tree
3311 * configurations (files or parent folders might have been moved,
3312 * deleted, added again, etc.). Such changes need to be merged with
3313 * local changes before a commit can occur.
3315 * The implication is that the file's (parent) entry in the root
3316 * directory must have the same ID in all relevant commits.
3318 if (ct->status != GOT_STATUS_ADD) {
3319 struct got_object_qid *pid;
3320 char *slash;
3321 struct got_object_id *root_entry_id = NULL;
3323 /* Trivial case: base commit == head commit */
3324 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3325 return NULL;
3327 /* Compute the path to the root directory's entry. */
3328 path = strdup(ct_path);
3329 if (path == NULL) {
3330 err = got_error_from_errno("strdup");
3331 goto done;
3333 slash = strchr(path, '/');
3334 if (slash)
3335 *slash = '\0';
3337 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3338 if (err)
3339 goto done;
3341 err = got_object_id_by_path(&root_entry_id, repo,
3342 head_commit_id, path);
3343 if (err)
3344 goto done;
3346 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3347 while (pid) {
3348 struct got_commit_object *pcommit;
3350 err = got_object_id_by_path(&id, repo, pid->id, path);
3351 if (err) {
3352 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3353 goto done;
3354 err = NULL;
3355 break;
3358 err = got_object_id_by_path(&id, repo, pid->id, path);
3359 if (err)
3360 goto done;
3362 if (got_object_id_cmp(id, root_entry_id) != 0) {
3363 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3364 break;
3367 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3368 break; /* all relevant commits scanned */
3370 err = got_object_open_as_commit(&pcommit, repo,
3371 pid->id);
3372 if (err)
3373 goto done;
3375 got_object_commit_close(commit);
3376 commit = pcommit;
3377 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3378 commit));
3380 } else {
3381 /* Require that added files don't exist in the branch head. */
3382 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3383 ct_path);
3384 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3385 goto done;
3386 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3388 done:
3389 if (commit)
3390 got_object_commit_close(commit);
3391 free(id_in_head);
3392 free(id);
3393 free(path);
3394 return err;
3397 const struct got_error *
3398 commit_worktree(struct got_object_id **new_commit_id,
3399 struct got_pathlist_head *commitable_paths,
3400 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3401 const char *ondisk_path, const char *author, const char *committer,
3402 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3403 got_worktree_status_cb status_cb, void *status_arg,
3404 struct got_repository *repo)
3406 const struct got_error *err = NULL, *unlockerr = NULL;
3407 struct got_pathlist_entry *pe;
3408 const char *head_ref_name = NULL;
3409 struct got_commit_object *head_commit = NULL;
3410 struct got_reference *head_ref2 = NULL;
3411 struct got_object_id *head_commit_id2 = NULL;
3412 struct got_tree_object *head_tree = NULL;
3413 struct got_object_id *new_tree_id = NULL;
3414 struct got_object_id_queue parent_ids;
3415 struct got_object_qid *pid = NULL;
3416 char *logmsg = NULL;
3418 *new_commit_id = NULL;
3420 SIMPLEQ_INIT(&parent_ids);
3422 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3423 if (err)
3424 goto done;
3426 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3427 if (err)
3428 goto done;
3430 if (commit_msg_cb != NULL) {
3431 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3432 if (err)
3433 goto done;
3436 if (logmsg == NULL || strlen(logmsg) == 0) {
3437 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3438 goto done;
3441 /* Create blobs from added and modified files and record their IDs. */
3442 TAILQ_FOREACH(pe, commitable_paths, entry) {
3443 struct got_commitable *ct = pe->data;
3444 char *ondisk_path;
3446 if (ct->status != GOT_STATUS_ADD &&
3447 ct->status != GOT_STATUS_MODIFY)
3448 continue;
3450 if (asprintf(&ondisk_path, "%s/%s",
3451 worktree->root_path, pe->path) == -1) {
3452 err = got_error_from_errno("asprintf");
3453 goto done;
3455 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3456 free(ondisk_path);
3457 if (err)
3458 goto done;
3461 /* Recursively write new tree objects. */
3462 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3463 status_cb, status_arg, repo);
3464 if (err)
3465 goto done;
3467 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3468 if (err)
3469 goto done;
3470 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3471 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3472 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3473 got_object_qid_free(pid);
3474 if (logmsg != NULL)
3475 free(logmsg);
3476 if (err)
3477 goto done;
3479 /* Check if a concurrent commit to our branch has occurred. */
3480 head_ref_name = got_worktree_get_head_ref_name(worktree);
3481 if (head_ref_name == NULL) {
3482 err = got_error_from_errno("got_worktree_get_head_ref_name");
3483 goto done;
3485 /* Lock the reference here to prevent concurrent modification. */
3486 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3487 if (err)
3488 goto done;
3489 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3490 if (err)
3491 goto done;
3492 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3493 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3494 goto done;
3496 /* Update branch head in repository. */
3497 err = got_ref_change_ref(head_ref2, *new_commit_id);
3498 if (err)
3499 goto done;
3500 err = got_ref_write(head_ref2, repo);
3501 if (err)
3502 goto done;
3504 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3505 if (err)
3506 goto done;
3508 err = ref_base_commit(worktree, repo);
3509 if (err)
3510 goto done;
3511 done:
3512 if (head_tree)
3513 got_object_tree_close(head_tree);
3514 if (head_commit)
3515 got_object_commit_close(head_commit);
3516 free(head_commit_id2);
3517 if (head_ref2) {
3518 unlockerr = got_ref_unlock(head_ref2);
3519 if (unlockerr && err == NULL)
3520 err = unlockerr;
3521 got_ref_close(head_ref2);
3523 return err;
3526 const struct got_error *
3527 got_worktree_commit(struct got_object_id **new_commit_id,
3528 struct got_worktree *worktree, const char *ondisk_path,
3529 const char *author, const char *committer,
3530 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3531 got_worktree_status_cb status_cb, void *status_arg,
3532 struct got_repository *repo)
3534 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3535 struct got_fileindex *fileindex = NULL;
3536 char *fileindex_path = NULL, *relpath = NULL;
3537 struct got_pathlist_head commitable_paths;
3538 struct collect_commitables_arg cc_arg;
3539 struct got_pathlist_entry *pe;
3540 struct got_reference *head_ref = NULL;
3541 struct got_object_id *head_commit_id = NULL;
3543 *new_commit_id = NULL;
3545 TAILQ_INIT(&commitable_paths);
3547 err = lock_worktree(worktree, LOCK_EX);
3548 if (err)
3549 goto done;
3551 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3552 if (err)
3553 goto done;
3555 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3556 if (err)
3557 goto done;
3559 if (ondisk_path) {
3560 if (strcmp(ondisk_path, worktree->root_path) == 0) {
3561 relpath = strdup("");
3562 if (relpath == NULL) {
3563 err = got_error_from_errno("strdup");
3564 goto done;
3566 } else {
3567 err = got_path_skip_common_ancestor(&relpath,
3568 worktree->root_path, ondisk_path);
3569 if (err)
3570 return err;
3574 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3575 if (err)
3576 goto done;
3578 cc_arg.commitable_paths = &commitable_paths;
3579 cc_arg.worktree = worktree;
3580 cc_arg.repo = repo;
3581 err = worktree_status(worktree, relpath ? relpath : "",
3582 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3583 if (err)
3584 goto done;
3586 if (TAILQ_EMPTY(&commitable_paths)) {
3587 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3588 goto done;
3591 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3592 struct got_commitable *ct = pe->data;
3593 err = check_ct_out_of_date(ct, repo, head_commit_id);
3594 if (err)
3595 goto done;
3598 err = commit_worktree(new_commit_id, &commitable_paths,
3599 head_commit_id, worktree, ondisk_path, author, committer,
3600 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3601 if (err)
3602 goto done;
3604 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3605 fileindex);
3606 sync_err = sync_fileindex(fileindex, fileindex_path);
3607 if (sync_err && err == NULL)
3608 err = sync_err;
3609 done:
3610 if (fileindex)
3611 got_fileindex_free(fileindex);
3612 free(fileindex_path);
3613 free(relpath);
3614 unlockerr = lock_worktree(worktree, LOCK_SH);
3615 if (unlockerr && err == NULL)
3616 err = unlockerr;
3617 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3618 struct got_commitable *ct = pe->data;
3619 free_commitable(ct);
3621 got_pathlist_free(&commitable_paths);
3622 return err;
3625 const char *
3626 got_commitable_get_path(struct got_commitable *ct)
3628 return ct->path;
3631 unsigned int
3632 got_commitable_get_status(struct got_commitable *ct)
3634 return ct->status;
3637 struct check_rebase_ok_arg {
3638 struct got_worktree *worktree;
3639 struct got_repository *repo;
3642 static const struct got_error *
3643 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3645 const struct got_error *err = NULL;
3646 struct check_rebase_ok_arg *a = arg;
3647 unsigned char status;
3648 struct stat sb;
3649 char *ondisk_path;
3651 /* Reject rebase of a work tree with mixed base commits. */
3652 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3653 SHA1_DIGEST_LENGTH))
3654 return got_error(GOT_ERR_MIXED_COMMITS);
3656 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3657 == -1)
3658 return got_error_from_errno("asprintf");
3660 /* Reject rebase of a work tree with modified or conflicted files. */
3661 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3662 free(ondisk_path);
3663 if (err)
3664 return err;
3666 if (status != GOT_STATUS_NO_CHANGE)
3667 return got_error(GOT_ERR_MODIFIED);
3669 return NULL;
3672 const struct got_error *
3673 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3674 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3675 struct got_worktree *worktree, struct got_reference *branch,
3676 struct got_repository *repo)
3678 const struct got_error *err = NULL;
3679 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3680 char *branch_ref_name = NULL;
3681 char *fileindex_path = NULL;
3682 struct check_rebase_ok_arg ok_arg;
3683 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3685 *new_base_branch_ref = NULL;
3686 *tmp_branch = NULL;
3687 *fileindex = NULL;
3689 err = lock_worktree(worktree, LOCK_EX);
3690 if (err)
3691 return err;
3693 err = open_fileindex(fileindex, &fileindex_path, worktree);
3694 if (err)
3695 goto done;
3697 ok_arg.worktree = worktree;
3698 ok_arg.repo = repo;
3699 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3700 &ok_arg);
3701 if (err)
3702 goto done;
3704 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3705 if (err)
3706 goto done;
3708 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3709 if (err)
3710 goto done;
3712 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3713 if (err)
3714 goto done;
3716 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3717 0);
3718 if (err)
3719 goto done;
3721 err = got_ref_alloc_symref(new_base_branch_ref,
3722 new_base_branch_ref_name, wt_branch);
3723 if (err)
3724 goto done;
3725 err = got_ref_write(*new_base_branch_ref, repo);
3726 if (err)
3727 goto done;
3729 /* TODO Lock original branch's ref while rebasing? */
3731 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3732 if (err)
3733 goto done;
3735 err = got_ref_write(branch_ref, repo);
3736 if (err)
3737 goto done;
3739 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3740 worktree->base_commit_id);
3741 if (err)
3742 goto done;
3743 err = got_ref_write(*tmp_branch, repo);
3744 if (err)
3745 goto done;
3747 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3748 if (err)
3749 goto done;
3750 done:
3751 free(fileindex_path);
3752 free(tmp_branch_name);
3753 free(new_base_branch_ref_name);
3754 free(branch_ref_name);
3755 if (branch_ref)
3756 got_ref_close(branch_ref);
3757 if (wt_branch)
3758 got_ref_close(wt_branch);
3759 if (err) {
3760 if (*new_base_branch_ref) {
3761 got_ref_close(*new_base_branch_ref);
3762 *new_base_branch_ref = NULL;
3764 if (*tmp_branch) {
3765 got_ref_close(*tmp_branch);
3766 *tmp_branch = NULL;
3768 if (*fileindex) {
3769 got_fileindex_free(*fileindex);
3770 *fileindex = NULL;
3772 lock_worktree(worktree, LOCK_SH);
3774 return err;
3777 const struct got_error *
3778 got_worktree_rebase_continue(struct got_object_id **commit_id,
3779 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3780 struct got_reference **branch, struct got_fileindex **fileindex,
3781 struct got_worktree *worktree, struct got_repository *repo)
3783 const struct got_error *err;
3784 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3785 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3786 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3787 char *fileindex_path = NULL;
3789 *commit_id = NULL;
3790 *new_base_branch = NULL;
3791 *tmp_branch = NULL;
3792 *branch = NULL;
3793 *fileindex = NULL;
3795 err = lock_worktree(worktree, LOCK_EX);
3796 if (err)
3797 return err;
3799 err = open_fileindex(fileindex, &fileindex_path, worktree);
3800 if (err)
3801 goto done;
3803 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3804 if (err)
3805 return err;
3807 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3808 if (err)
3809 goto done;
3811 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3812 if (err)
3813 goto done;
3815 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3816 if (err)
3817 goto done;
3819 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3820 if (err)
3821 goto done;
3823 err = got_ref_open(branch, repo,
3824 got_ref_get_symref_target(branch_ref), 0);
3825 if (err)
3826 goto done;
3828 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3829 if (err)
3830 goto done;
3832 err = got_ref_resolve(commit_id, repo, commit_ref);
3833 if (err)
3834 goto done;
3836 err = got_ref_open(new_base_branch, repo,
3837 new_base_branch_ref_name, 0);
3838 if (err)
3839 goto done;
3841 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3842 if (err)
3843 goto done;
3844 done:
3845 free(commit_ref_name);
3846 free(branch_ref_name);
3847 free(fileindex_path);
3848 if (commit_ref)
3849 got_ref_close(commit_ref);
3850 if (branch_ref)
3851 got_ref_close(branch_ref);
3852 if (err) {
3853 free(*commit_id);
3854 *commit_id = NULL;
3855 if (*tmp_branch) {
3856 got_ref_close(*tmp_branch);
3857 *tmp_branch = NULL;
3859 if (*new_base_branch) {
3860 got_ref_close(*new_base_branch);
3861 *new_base_branch = NULL;
3863 if (*branch) {
3864 got_ref_close(*branch);
3865 *branch = NULL;
3867 if (*fileindex) {
3868 got_fileindex_free(*fileindex);
3869 *fileindex = NULL;
3871 lock_worktree(worktree, LOCK_SH);
3873 return err;
3876 const struct got_error *
3877 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3879 const struct got_error *err;
3880 char *tmp_branch_name = NULL;
3882 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3883 if (err)
3884 return err;
3886 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3887 free(tmp_branch_name);
3888 return NULL;
3891 static const struct got_error *
3892 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3893 char **logmsg, void *arg)
3895 *logmsg = arg;
3896 return NULL;
3899 static const struct got_error *
3900 rebase_status(void *arg, unsigned char status, const char *path,
3901 struct got_object_id *blob_id, struct got_object_id *commit_id)
3903 return NULL;
3906 struct collect_merged_paths_arg {
3907 got_worktree_checkout_cb progress_cb;
3908 void *progress_arg;
3909 struct got_pathlist_head *merged_paths;
3912 static const struct got_error *
3913 collect_merged_paths(void *arg, unsigned char status, const char *path)
3915 const struct got_error *err;
3916 struct collect_merged_paths_arg *a = arg;
3917 char *p;
3918 struct got_pathlist_entry *new;
3920 err = (*a->progress_cb)(a->progress_arg, status, path);
3921 if (err)
3922 return err;
3924 if (status != GOT_STATUS_MERGE &&
3925 status != GOT_STATUS_ADD &&
3926 status != GOT_STATUS_DELETE &&
3927 status != GOT_STATUS_CONFLICT)
3928 return NULL;
3930 p = strdup(path);
3931 if (p == NULL)
3932 return got_error_from_errno("strdup");
3934 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3935 if (err || new == NULL)
3936 free(p);
3937 return err;
3940 void
3941 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3943 struct got_pathlist_entry *pe;
3945 TAILQ_FOREACH(pe, merged_paths, entry)
3946 free((char *)pe->path);
3948 got_pathlist_free(merged_paths);
3951 static const struct got_error *
3952 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3953 struct got_repository *repo)
3955 const struct got_error *err;
3956 struct got_reference *commit_ref = NULL;
3958 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3959 if (err) {
3960 if (err->code != GOT_ERR_NOT_REF)
3961 goto done;
3962 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3963 if (err)
3964 goto done;
3965 err = got_ref_write(commit_ref, repo);
3966 if (err)
3967 goto done;
3968 } else {
3969 struct got_object_id *stored_id;
3970 int cmp;
3972 err = got_ref_resolve(&stored_id, repo, commit_ref);
3973 if (err)
3974 goto done;
3975 cmp = got_object_id_cmp(commit_id, stored_id);
3976 free(stored_id);
3977 if (cmp != 0) {
3978 err = got_error(GOT_ERR_REBASE_COMMITID);
3979 goto done;
3982 done:
3983 if (commit_ref)
3984 got_ref_close(commit_ref);
3985 return err;
3988 static const struct got_error *
3989 rebase_merge_files(struct got_pathlist_head *merged_paths,
3990 const char *commit_ref_name, struct got_worktree *worktree,
3991 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3992 struct got_object_id *commit_id, struct got_repository *repo,
3993 got_worktree_checkout_cb progress_cb, void *progress_arg,
3994 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3996 const struct got_error *err;
3997 struct got_reference *commit_ref = NULL;
3998 struct collect_merged_paths_arg cmp_arg;
3999 char *fileindex_path;
4001 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4003 err = get_fileindex_path(&fileindex_path, worktree);
4004 if (err)
4005 return err;
4007 cmp_arg.progress_cb = progress_cb;
4008 cmp_arg.progress_arg = progress_arg;
4009 cmp_arg.merged_paths = merged_paths;
4010 err = merge_files(worktree, fileindex, fileindex_path,
4011 parent_commit_id, commit_id, repo, collect_merged_paths,
4012 &cmp_arg, cancel_cb, cancel_arg);
4013 if (commit_ref)
4014 got_ref_close(commit_ref);
4015 return err;
4018 const struct got_error *
4019 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4020 struct got_worktree *worktree, struct got_fileindex *fileindex,
4021 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4022 struct got_repository *repo,
4023 got_worktree_checkout_cb progress_cb, void *progress_arg,
4024 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4026 const struct got_error *err;
4027 char *commit_ref_name;
4029 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4030 if (err)
4031 return err;
4033 err = store_commit_id(commit_ref_name, commit_id, repo);
4034 if (err)
4035 goto done;
4037 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4038 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4039 progress_arg, cancel_cb, cancel_arg);
4040 done:
4041 free(commit_ref_name);
4042 return err;
4045 const struct got_error *
4046 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4047 struct got_worktree *worktree, struct got_fileindex *fileindex,
4048 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4049 struct got_repository *repo,
4050 got_worktree_checkout_cb progress_cb, void *progress_arg,
4051 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4053 const struct got_error *err;
4054 char *commit_ref_name;
4056 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4057 if (err)
4058 return err;
4060 err = store_commit_id(commit_ref_name, commit_id, repo);
4061 if (err)
4062 goto done;
4064 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4065 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4066 progress_arg, cancel_cb, cancel_arg);
4067 done:
4068 free(commit_ref_name);
4069 return err;
4072 static const struct got_error *
4073 rebase_commit(struct got_object_id **new_commit_id,
4074 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4075 struct got_worktree *worktree, struct got_fileindex *fileindex,
4076 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4077 const char *new_logmsg, struct got_repository *repo)
4079 const struct got_error *err, *sync_err;
4080 struct got_pathlist_head commitable_paths;
4081 struct collect_commitables_arg cc_arg;
4082 char *fileindex_path = NULL;
4083 struct got_reference *head_ref = NULL;
4084 struct got_object_id *head_commit_id = NULL;
4085 char *logmsg = NULL;
4087 TAILQ_INIT(&commitable_paths);
4088 *new_commit_id = NULL;
4090 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4092 err = get_fileindex_path(&fileindex_path, worktree);
4093 if (err)
4094 return err;
4096 cc_arg.commitable_paths = &commitable_paths;
4097 cc_arg.worktree = worktree;
4098 cc_arg.repo = repo;
4100 * If possible get the status of individual files directly to
4101 * avoid crawling the entire work tree once per rebased commit.
4102 * TODO: Ideally, merged_paths would contain a list of commitables
4103 * we could use so we could skip worktree_status() entirely.
4105 if (merged_paths) {
4106 struct got_pathlist_entry *pe;
4107 if (TAILQ_EMPTY(merged_paths)) {
4108 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4109 goto done;
4111 TAILQ_FOREACH(pe, merged_paths, entry) {
4112 err = worktree_status(worktree, pe->path, fileindex,
4113 repo, collect_commitables, &cc_arg, NULL, NULL);
4114 if (err)
4115 goto done;
4117 } else {
4118 err = worktree_status(worktree, "", fileindex, repo,
4119 collect_commitables, &cc_arg, NULL, NULL);
4120 if (err)
4121 goto done;
4124 if (TAILQ_EMPTY(&commitable_paths)) {
4125 /* No-op change; commit will be elided. */
4126 err = got_ref_delete(commit_ref, repo);
4127 if (err)
4128 goto done;
4129 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4130 goto done;
4133 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4134 if (err)
4135 goto done;
4137 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4138 if (err)
4139 goto done;
4141 if (new_logmsg)
4142 logmsg = strdup(new_logmsg);
4143 else
4144 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4145 if (logmsg == NULL)
4146 return got_error_from_errno("strdup");
4148 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4149 worktree, NULL, got_object_commit_get_author(orig_commit),
4150 got_object_commit_get_committer(orig_commit),
4151 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4152 if (err)
4153 goto done;
4155 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4156 if (err)
4157 goto done;
4159 err = got_ref_delete(commit_ref, repo);
4160 if (err)
4161 goto done;
4163 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4164 fileindex);
4165 sync_err = sync_fileindex(fileindex, fileindex_path);
4166 if (sync_err && err == NULL)
4167 err = sync_err;
4168 done:
4169 free(fileindex_path);
4170 free(head_commit_id);
4171 if (head_ref)
4172 got_ref_close(head_ref);
4173 if (err) {
4174 free(*new_commit_id);
4175 *new_commit_id = NULL;
4177 return err;
4180 const struct got_error *
4181 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4182 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4183 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4184 struct got_commit_object *orig_commit,
4185 struct got_object_id *orig_commit_id, struct got_repository *repo)
4187 const struct got_error *err;
4188 char *commit_ref_name;
4189 struct got_reference *commit_ref = NULL;
4190 struct got_object_id *commit_id = NULL;
4192 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4193 if (err)
4194 return err;
4196 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4197 if (err)
4198 goto done;
4199 err = got_ref_resolve(&commit_id, repo, commit_ref);
4200 if (err)
4201 goto done;
4202 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4203 err = got_error(GOT_ERR_REBASE_COMMITID);
4204 goto done;
4207 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4208 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4209 done:
4210 if (commit_ref)
4211 got_ref_close(commit_ref);
4212 free(commit_ref_name);
4213 free(commit_id);
4214 return err;
4217 const struct got_error *
4218 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4219 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4220 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4221 struct got_commit_object *orig_commit,
4222 struct got_object_id *orig_commit_id, const char *new_logmsg,
4223 struct got_repository *repo)
4225 const struct got_error *err;
4226 char *commit_ref_name;
4227 struct got_reference *commit_ref = NULL;
4228 struct got_object_id *commit_id = NULL;
4230 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4231 if (err)
4232 return err;
4234 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4235 if (err)
4236 goto done;
4237 err = got_ref_resolve(&commit_id, repo, commit_ref);
4238 if (err)
4239 goto done;
4240 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4241 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4242 goto done;
4245 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4246 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4247 done:
4248 if (commit_ref)
4249 got_ref_close(commit_ref);
4250 free(commit_ref_name);
4251 free(commit_id);
4252 return err;
4255 const struct got_error *
4256 got_worktree_rebase_postpone(struct got_worktree *worktree,
4257 struct got_fileindex *fileindex)
4259 if (fileindex)
4260 got_fileindex_free(fileindex);
4261 return lock_worktree(worktree, LOCK_SH);
4264 static const struct got_error *
4265 delete_ref(const char *name, struct got_repository *repo)
4267 const struct got_error *err;
4268 struct got_reference *ref;
4270 err = got_ref_open(&ref, repo, name, 0);
4271 if (err) {
4272 if (err->code == GOT_ERR_NOT_REF)
4273 return NULL;
4274 return err;
4277 err = got_ref_delete(ref, repo);
4278 got_ref_close(ref);
4279 return err;
4282 static const struct got_error *
4283 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4285 const struct got_error *err;
4286 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4287 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4289 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4290 if (err)
4291 goto done;
4292 err = delete_ref(tmp_branch_name, repo);
4293 if (err)
4294 goto done;
4296 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4297 if (err)
4298 goto done;
4299 err = delete_ref(new_base_branch_ref_name, repo);
4300 if (err)
4301 goto done;
4303 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4304 if (err)
4305 goto done;
4306 err = delete_ref(branch_ref_name, repo);
4307 if (err)
4308 goto done;
4310 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4311 if (err)
4312 goto done;
4313 err = delete_ref(commit_ref_name, repo);
4314 if (err)
4315 goto done;
4317 done:
4318 free(tmp_branch_name);
4319 free(new_base_branch_ref_name);
4320 free(branch_ref_name);
4321 free(commit_ref_name);
4322 return err;
4325 const struct got_error *
4326 got_worktree_rebase_complete(struct got_worktree *worktree,
4327 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4328 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4329 struct got_repository *repo)
4331 const struct got_error *err, *unlockerr;
4332 struct got_object_id *new_head_commit_id = NULL;
4334 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4335 if (err)
4336 return err;
4338 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4339 if (err)
4340 goto done;
4342 err = got_ref_write(rebased_branch, repo);
4343 if (err)
4344 goto done;
4346 err = got_worktree_set_head_ref(worktree, rebased_branch);
4347 if (err)
4348 goto done;
4350 err = delete_rebase_refs(worktree, repo);
4351 done:
4352 if (fileindex)
4353 got_fileindex_free(fileindex);
4354 free(new_head_commit_id);
4355 unlockerr = lock_worktree(worktree, LOCK_SH);
4356 if (unlockerr && err == NULL)
4357 err = unlockerr;
4358 return err;
4361 struct collect_revertible_paths_arg {
4362 struct got_pathlist_head *revertible_paths;
4363 struct got_worktree *worktree;
4366 static const struct got_error *
4367 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4368 struct got_object_id *blob_id, struct got_object_id *commit_id)
4370 struct collect_revertible_paths_arg *a = arg;
4371 const struct got_error *err = NULL;
4372 struct got_pathlist_entry *new = NULL;
4373 char *path = NULL;
4375 if (status != GOT_STATUS_ADD &&
4376 status != GOT_STATUS_DELETE &&
4377 status != GOT_STATUS_MODIFY &&
4378 status != GOT_STATUS_CONFLICT &&
4379 status != GOT_STATUS_MISSING)
4380 return NULL;
4382 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4383 return got_error_from_errno("asprintf");
4385 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4386 if (err || new == NULL)
4387 free(path);
4388 return err;
4391 const struct got_error *
4392 got_worktree_rebase_abort(struct got_worktree *worktree,
4393 struct got_fileindex *fileindex, struct got_repository *repo,
4394 struct got_reference *new_base_branch,
4395 got_worktree_checkout_cb progress_cb, void *progress_arg)
4397 const struct got_error *err, *unlockerr, *sync_err;
4398 struct got_reference *resolved = NULL;
4399 struct got_object_id *commit_id = NULL;
4400 char *fileindex_path = NULL;
4401 struct got_pathlist_head revertible_paths;
4402 struct got_pathlist_entry *pe;
4403 struct collect_revertible_paths_arg crp_arg;
4404 struct got_object_id *tree_id = NULL;
4406 TAILQ_INIT(&revertible_paths);
4408 err = lock_worktree(worktree, LOCK_EX);
4409 if (err)
4410 return err;
4412 err = got_ref_open(&resolved, repo,
4413 got_ref_get_symref_target(new_base_branch), 0);
4414 if (err)
4415 goto done;
4417 err = got_worktree_set_head_ref(worktree, resolved);
4418 if (err)
4419 goto done;
4422 * XXX commits to the base branch could have happened while
4423 * we were busy rebasing; should we store the original commit ID
4424 * when rebase begins and read it back here?
4426 err = got_ref_resolve(&commit_id, repo, resolved);
4427 if (err)
4428 goto done;
4430 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4431 if (err)
4432 goto done;
4434 err = got_object_id_by_path(&tree_id, repo,
4435 worktree->base_commit_id, worktree->path_prefix);
4436 if (err)
4437 goto done;
4439 err = delete_rebase_refs(worktree, repo);
4440 if (err)
4441 goto done;
4443 err = get_fileindex_path(&fileindex_path, worktree);
4444 if (err)
4445 goto done;
4447 crp_arg.revertible_paths = &revertible_paths;
4448 crp_arg.worktree = worktree;
4449 err = worktree_status(worktree, "", fileindex, repo,
4450 collect_revertible_paths, &crp_arg, NULL, NULL);
4451 if (err)
4452 goto done;
4454 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4455 err = revert_file(worktree, fileindex, pe->path,
4456 progress_cb, progress_arg, repo);
4457 if (err)
4458 goto sync;
4461 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4462 repo, progress_cb, progress_arg, NULL, NULL);
4463 sync:
4464 sync_err = sync_fileindex(fileindex, fileindex_path);
4465 if (sync_err && err == NULL)
4466 err = sync_err;
4467 done:
4468 got_ref_close(resolved);
4469 free(tree_id);
4470 free(commit_id);
4471 if (fileindex)
4472 got_fileindex_free(fileindex);
4473 free(fileindex_path);
4474 TAILQ_FOREACH(pe, &revertible_paths, entry)
4475 free((char *)pe->path);
4476 got_pathlist_free(&revertible_paths);
4478 unlockerr = lock_worktree(worktree, LOCK_SH);
4479 if (unlockerr && err == NULL)
4480 err = unlockerr;
4481 return err;
4484 const struct got_error *
4485 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4486 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4487 struct got_fileindex **fileindex, struct got_worktree *worktree,
4488 struct got_repository *repo)
4490 const struct got_error *err = NULL;
4491 char *tmp_branch_name = NULL;
4492 char *branch_ref_name = NULL;
4493 char *base_commit_ref_name = NULL;
4494 char *fileindex_path = NULL;
4495 struct check_rebase_ok_arg ok_arg;
4496 struct got_reference *wt_branch = NULL;
4497 struct got_reference *base_commit_ref = NULL;
4499 *tmp_branch = NULL;
4500 *branch_ref = NULL;
4501 *base_commit_id = NULL;
4502 *fileindex = NULL;
4504 err = lock_worktree(worktree, LOCK_EX);
4505 if (err)
4506 return err;
4508 err = open_fileindex(fileindex, &fileindex_path, worktree);
4509 if (err)
4510 goto done;
4512 ok_arg.worktree = worktree;
4513 ok_arg.repo = repo;
4514 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4515 &ok_arg);
4516 if (err)
4517 goto done;
4519 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4520 if (err)
4521 goto done;
4523 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4524 if (err)
4525 goto done;
4527 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4528 worktree);
4529 if (err)
4530 goto done;
4532 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4533 0);
4534 if (err)
4535 goto done;
4537 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4538 if (err)
4539 goto done;
4541 err = got_ref_write(*branch_ref, repo);
4542 if (err)
4543 goto done;
4545 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4546 worktree->base_commit_id);
4547 if (err)
4548 goto done;
4549 err = got_ref_write(base_commit_ref, repo);
4550 if (err)
4551 goto done;
4552 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4553 if (*base_commit_id == NULL) {
4554 err = got_error_from_errno("got_object_id_dup");
4555 goto done;
4558 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4559 worktree->base_commit_id);
4560 if (err)
4561 goto done;
4562 err = got_ref_write(*tmp_branch, repo);
4563 if (err)
4564 goto done;
4566 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4567 if (err)
4568 goto done;
4569 done:
4570 free(fileindex_path);
4571 free(tmp_branch_name);
4572 free(branch_ref_name);
4573 free(base_commit_ref_name);
4574 if (wt_branch)
4575 got_ref_close(wt_branch);
4576 if (err) {
4577 if (*branch_ref) {
4578 got_ref_close(*branch_ref);
4579 *branch_ref = NULL;
4581 if (*tmp_branch) {
4582 got_ref_close(*tmp_branch);
4583 *tmp_branch = NULL;
4585 free(*base_commit_id);
4586 if (*fileindex) {
4587 got_fileindex_free(*fileindex);
4588 *fileindex = NULL;
4590 lock_worktree(worktree, LOCK_SH);
4592 return err;
4595 const struct got_error *
4596 got_worktree_histedit_postpone(struct got_worktree *worktree,
4597 struct got_fileindex *fileindex)
4599 if (fileindex)
4600 got_fileindex_free(fileindex);
4601 return lock_worktree(worktree, LOCK_SH);
4604 const struct got_error *
4605 got_worktree_histedit_in_progress(int *in_progress,
4606 struct got_worktree *worktree)
4608 const struct got_error *err;
4609 char *tmp_branch_name = NULL;
4611 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4612 if (err)
4613 return err;
4615 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4616 free(tmp_branch_name);
4617 return NULL;
4620 const struct got_error *
4621 got_worktree_histedit_continue(struct got_object_id **commit_id,
4622 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4623 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4624 struct got_worktree *worktree, struct got_repository *repo)
4626 const struct got_error *err;
4627 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4628 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4629 struct got_reference *commit_ref = NULL;
4630 struct got_reference *base_commit_ref = NULL;
4631 char *fileindex_path = NULL;
4633 *commit_id = NULL;
4634 *tmp_branch = NULL;
4635 *base_commit_id = NULL;
4636 *fileindex = NULL;
4638 err = lock_worktree(worktree, LOCK_EX);
4639 if (err)
4640 return err;
4642 err = open_fileindex(fileindex, &fileindex_path, worktree);
4643 if (err)
4644 goto done;
4646 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4647 if (err)
4648 return err;
4650 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4651 if (err)
4652 goto done;
4654 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4655 if (err)
4656 goto done;
4658 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4659 worktree);
4660 if (err)
4661 goto done;
4663 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4664 if (err)
4665 goto done;
4667 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4668 if (err)
4669 goto done;
4670 err = got_ref_resolve(commit_id, repo, commit_ref);
4671 if (err)
4672 goto done;
4674 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4675 if (err)
4676 goto done;
4677 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4678 if (err)
4679 goto done;
4681 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4682 if (err)
4683 goto done;
4684 done:
4685 free(commit_ref_name);
4686 free(branch_ref_name);
4687 free(fileindex_path);
4688 if (commit_ref)
4689 got_ref_close(commit_ref);
4690 if (base_commit_ref)
4691 got_ref_close(base_commit_ref);
4692 if (err) {
4693 free(*commit_id);
4694 *commit_id = NULL;
4695 free(*base_commit_id);
4696 *base_commit_id = NULL;
4697 if (*tmp_branch) {
4698 got_ref_close(*tmp_branch);
4699 *tmp_branch = NULL;
4701 if (*fileindex) {
4702 got_fileindex_free(*fileindex);
4703 *fileindex = NULL;
4705 lock_worktree(worktree, LOCK_EX);
4707 return err;
4710 static const struct got_error *
4711 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4713 const struct got_error *err;
4714 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4715 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4717 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4718 if (err)
4719 goto done;
4720 err = delete_ref(tmp_branch_name, repo);
4721 if (err)
4722 goto done;
4724 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4725 worktree);
4726 if (err)
4727 goto done;
4728 err = delete_ref(base_commit_ref_name, repo);
4729 if (err)
4730 goto done;
4732 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4733 if (err)
4734 goto done;
4735 err = delete_ref(branch_ref_name, repo);
4736 if (err)
4737 goto done;
4739 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4740 if (err)
4741 goto done;
4742 err = delete_ref(commit_ref_name, repo);
4743 if (err)
4744 goto done;
4745 done:
4746 free(tmp_branch_name);
4747 free(base_commit_ref_name);
4748 free(branch_ref_name);
4749 free(commit_ref_name);
4750 return err;
4753 const struct got_error *
4754 got_worktree_histedit_abort(struct got_worktree *worktree,
4755 struct got_fileindex *fileindex, struct got_repository *repo,
4756 struct got_reference *branch, struct got_object_id *base_commit_id,
4757 got_worktree_checkout_cb progress_cb, void *progress_arg)
4759 const struct got_error *err, *unlockerr, *sync_err;
4760 struct got_reference *resolved = NULL;
4761 char *fileindex_path = NULL;
4762 struct got_pathlist_head revertible_paths;
4763 struct got_pathlist_entry *pe;
4764 struct collect_revertible_paths_arg crp_arg;
4765 struct got_object_id *tree_id = NULL;
4767 TAILQ_INIT(&revertible_paths);
4769 err = lock_worktree(worktree, LOCK_EX);
4770 if (err)
4771 return err;
4773 err = got_ref_open(&resolved, repo,
4774 got_ref_get_symref_target(branch), 0);
4775 if (err)
4776 goto done;
4778 err = got_worktree_set_head_ref(worktree, resolved);
4779 if (err)
4780 goto done;
4782 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4783 if (err)
4784 goto done;
4786 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4787 worktree->path_prefix);
4788 if (err)
4789 goto done;
4791 err = delete_histedit_refs(worktree, repo);
4792 if (err)
4793 goto done;
4795 err = get_fileindex_path(&fileindex_path, worktree);
4796 if (err)
4797 goto done;
4799 crp_arg.revertible_paths = &revertible_paths;
4800 crp_arg.worktree = worktree;
4801 err = worktree_status(worktree, "", fileindex, repo,
4802 collect_revertible_paths, &crp_arg, NULL, NULL);
4803 if (err)
4804 goto done;
4806 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4807 err = revert_file(worktree, fileindex, pe->path,
4808 progress_cb, progress_arg, repo);
4809 if (err)
4810 goto sync;
4813 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4814 repo, progress_cb, progress_arg, NULL, NULL);
4815 sync:
4816 sync_err = sync_fileindex(fileindex, fileindex_path);
4817 if (sync_err && err == NULL)
4818 err = sync_err;
4819 done:
4820 got_ref_close(resolved);
4821 free(tree_id);
4822 free(fileindex_path);
4823 TAILQ_FOREACH(pe, &revertible_paths, entry)
4824 free((char *)pe->path);
4825 got_pathlist_free(&revertible_paths);
4827 unlockerr = lock_worktree(worktree, LOCK_SH);
4828 if (unlockerr && err == NULL)
4829 err = unlockerr;
4830 return err;
4833 const struct got_error *
4834 got_worktree_histedit_complete(struct got_worktree *worktree,
4835 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4836 struct got_reference *edited_branch, struct got_repository *repo)
4838 const struct got_error *err, *unlockerr;
4839 struct got_object_id *new_head_commit_id = NULL;
4840 struct got_reference *resolved = NULL;
4842 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4843 if (err)
4844 return err;
4846 err = got_ref_open(&resolved, repo,
4847 got_ref_get_symref_target(edited_branch), 0);
4848 if (err)
4849 goto done;
4851 err = got_ref_change_ref(resolved, new_head_commit_id);
4852 if (err)
4853 goto done;
4855 err = got_ref_write(resolved, repo);
4856 if (err)
4857 goto done;
4859 err = got_worktree_set_head_ref(worktree, resolved);
4860 if (err)
4861 goto done;
4863 err = delete_histedit_refs(worktree, repo);
4864 done:
4865 if (fileindex)
4866 got_fileindex_free(fileindex);
4867 free(new_head_commit_id);
4868 unlockerr = lock_worktree(worktree, LOCK_SH);
4869 if (unlockerr && err == NULL)
4870 err = unlockerr;
4871 return err;
4874 const struct got_error *
4875 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4876 struct got_object_id *commit_id, struct got_repository *repo)
4878 const struct got_error *err;
4879 char *commit_ref_name;
4881 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4882 if (err)
4883 return err;
4885 err = store_commit_id(commit_ref_name, commit_id, repo);
4886 if (err)
4887 goto done;
4889 err = delete_ref(commit_ref_name, repo);
4890 done:
4891 free(commit_ref_name);
4892 return err;