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 (ie) {
1054 if (got_fileindex_entry_has_file_on_disk(ie))
1055 *status = GOT_STATUS_MISSING;
1056 else
1057 *status = GOT_STATUS_DELETE;
1058 sb->st_mode = got_fileindex_perms_to_st(ie);
1059 } else
1060 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1061 return NULL;
1063 return got_error_from_errno2("lstat", abspath);
1066 if (!S_ISREG(sb->st_mode)) {
1067 *status = GOT_STATUS_OBSTRUCTED;
1068 return NULL;
1071 if (ie == NULL)
1072 return NULL;
1074 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1075 *status = GOT_STATUS_DELETE;
1076 return NULL;
1077 } else if (!got_fileindex_entry_has_blob(ie)) {
1078 *status = GOT_STATUS_ADD;
1079 return NULL;
1082 if (!stat_info_differs(ie, sb))
1083 return NULL;
1085 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1086 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1087 if (err)
1088 return err;
1090 f = fopen(abspath, "r");
1091 if (f == NULL) {
1092 err = got_error_from_errno2("fopen", abspath);
1093 goto done;
1095 hdrlen = got_object_blob_get_hdrlen(blob);
1096 for (;;) {
1097 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1098 err = got_object_blob_read_block(&blen, blob);
1099 if (err)
1100 goto done;
1101 /* Skip length of blob object header first time around. */
1102 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1103 if (flen == 0 && ferror(f)) {
1104 err = got_error_from_errno("fread");
1105 goto done;
1107 if (blen == 0) {
1108 if (flen != 0)
1109 *status = GOT_STATUS_MODIFY;
1110 break;
1111 } else if (flen == 0) {
1112 if (blen != 0)
1113 *status = GOT_STATUS_MODIFY;
1114 break;
1115 } else if (blen - hdrlen == flen) {
1116 /* Skip blob object header first time around. */
1117 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1118 *status = GOT_STATUS_MODIFY;
1119 break;
1121 } else {
1122 *status = GOT_STATUS_MODIFY;
1123 break;
1125 hdrlen = 0;
1128 if (*status == GOT_STATUS_MODIFY) {
1129 rewind(f);
1130 err = get_modified_file_content_status(status, f);
1132 done:
1133 if (blob)
1134 got_object_blob_close(blob);
1135 if (f)
1136 fclose(f);
1137 return err;
1141 * Update timestamps in the file index if a file is unmodified and
1142 * we had to run a full content comparison to find out.
1144 static const struct got_error *
1145 sync_timestamps(char *ondisk_path, unsigned char status,
1146 struct got_fileindex_entry *ie, struct stat *sb)
1148 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1149 return got_fileindex_entry_update(ie, ondisk_path,
1150 ie->blob_sha1, ie->commit_sha1, 1);
1152 return NULL;
1155 static const struct got_error *
1156 update_blob(struct got_worktree *worktree,
1157 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1158 struct got_tree_entry *te, const char *path,
1159 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1160 void *progress_arg)
1162 const struct got_error *err = NULL;
1163 struct got_blob_object *blob = NULL;
1164 char *ondisk_path;
1165 unsigned char status = GOT_STATUS_NO_CHANGE;
1166 struct stat sb;
1168 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1169 return got_error_from_errno("asprintf");
1171 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1172 if (err)
1173 goto done;
1175 if (status == GOT_STATUS_OBSTRUCTED) {
1176 err = (*progress_cb)(progress_arg, status, path);
1177 goto done;
1180 if (ie && status != GOT_STATUS_MISSING) {
1181 if (got_fileindex_entry_has_commit(ie) &&
1182 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1183 SHA1_DIGEST_LENGTH) == 0) {
1184 err = sync_timestamps(ondisk_path, status, ie, &sb);
1185 if (err)
1186 goto done;
1187 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1188 path);
1189 goto done;
1191 if (got_fileindex_entry_has_blob(ie) &&
1192 memcmp(ie->blob_sha1, te->id->sha1,
1193 SHA1_DIGEST_LENGTH) == 0) {
1194 err = sync_timestamps(ondisk_path, status, ie, &sb);
1195 goto done;
1199 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1200 if (err)
1201 goto done;
1203 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1204 int update_timestamps;
1205 struct got_blob_object *blob2 = NULL;
1206 if (got_fileindex_entry_has_blob(ie)) {
1207 struct got_object_id id2;
1208 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1209 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1210 if (err)
1211 goto done;
1213 err = merge_blob(&update_timestamps, worktree, blob2,
1214 ondisk_path, path, sb.st_mode, blob,
1215 worktree->base_commit_id, repo,
1216 progress_cb, progress_arg);
1217 if (blob2)
1218 got_object_blob_close(blob2);
1220 * Do not update timestamps of files with local changes.
1221 * Otherwise, a future status walk would treat them as
1222 * unmodified files again.
1224 err = got_fileindex_entry_update(ie, ondisk_path,
1225 blob->id.sha1, worktree->base_commit_id->sha1,
1226 update_timestamps);
1227 } else if (status == GOT_STATUS_DELETE) {
1228 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1229 if (err)
1230 goto done;
1231 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1232 ondisk_path, path, blob, 0);
1233 if (err)
1234 goto done;
1235 } else {
1236 err = install_blob(worktree, ondisk_path, path, te->mode,
1237 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1238 repo, progress_cb, progress_arg);
1239 if (err)
1240 goto done;
1241 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1242 ondisk_path, path, blob, 1);
1243 if (err)
1244 goto done;
1246 got_object_blob_close(blob);
1247 done:
1248 free(ondisk_path);
1249 return err;
1252 static const struct got_error *
1253 remove_ondisk_file(const char *root_path, const char *path)
1255 const struct got_error *err = NULL;
1256 char *ondisk_path = NULL;
1258 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1259 return got_error_from_errno("asprintf");
1261 if (unlink(ondisk_path) == -1) {
1262 if (errno != ENOENT)
1263 err = got_error_from_errno2("unlink", ondisk_path);
1264 } else {
1265 char *parent = dirname(ondisk_path);
1266 while (parent && strcmp(parent, root_path) != 0) {
1267 if (rmdir(parent) == -1) {
1268 if (errno != ENOTEMPTY)
1269 err = got_error_from_errno2("rmdir",
1270 parent);
1271 break;
1273 parent = dirname(parent);
1276 free(ondisk_path);
1277 return err;
1280 static const struct got_error *
1281 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1282 struct got_fileindex_entry *ie, struct got_repository *repo,
1283 got_worktree_checkout_cb progress_cb, void *progress_arg)
1285 const struct got_error *err = NULL;
1286 unsigned char status;
1287 struct stat sb;
1288 char *ondisk_path;
1290 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1291 == -1)
1292 return got_error_from_errno("asprintf");
1294 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1295 if (err)
1296 return err;
1298 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1299 status == GOT_STATUS_ADD) {
1300 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1301 if (err)
1302 return err;
1304 * Preserve the working file and change the deleted blob's
1305 * entry into a schedule-add entry.
1307 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1308 0);
1309 if (err)
1310 return err;
1311 } else {
1312 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1313 if (err)
1314 return err;
1315 if (status == GOT_STATUS_NO_CHANGE) {
1316 err = remove_ondisk_file(worktree->root_path, ie->path);
1317 if (err)
1318 return err;
1320 got_fileindex_entry_remove(fileindex, ie);
1323 return err;
1326 struct diff_cb_arg {
1327 struct got_fileindex *fileindex;
1328 struct got_worktree *worktree;
1329 struct got_repository *repo;
1330 got_worktree_checkout_cb progress_cb;
1331 void *progress_arg;
1332 got_worktree_cancel_cb cancel_cb;
1333 void *cancel_arg;
1336 static const struct got_error *
1337 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1338 struct got_tree_entry *te, const char *parent_path)
1340 struct diff_cb_arg *a = arg;
1342 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1343 return got_error(GOT_ERR_CANCELLED);
1345 return update_blob(a->worktree, a->fileindex, ie, te,
1346 ie->path, a->repo, a->progress_cb, a->progress_arg);
1349 static const struct got_error *
1350 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1352 struct diff_cb_arg *a = arg;
1354 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1355 return got_error(GOT_ERR_CANCELLED);
1357 return delete_blob(a->worktree, a->fileindex, ie,
1358 a->repo, a->progress_cb, a->progress_arg);
1361 static const struct got_error *
1362 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1364 struct diff_cb_arg *a = arg;
1365 const struct got_error *err;
1366 char *path;
1368 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1369 return got_error(GOT_ERR_CANCELLED);
1371 if (asprintf(&path, "%s%s%s", parent_path,
1372 parent_path[0] ? "/" : "", te->name)
1373 == -1)
1374 return got_error_from_errno("asprintf");
1376 if (S_ISDIR(te->mode))
1377 err = add_dir_on_disk(a->worktree, path);
1378 else
1379 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1380 a->repo, a->progress_cb, a->progress_arg);
1382 free(path);
1383 return err;
1386 static const struct got_error *
1387 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1389 const struct got_error *err = NULL;
1390 char *uuidstr = NULL;
1391 uint32_t uuid_status;
1393 *refname = NULL;
1395 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1396 if (uuid_status != uuid_s_ok)
1397 return got_error_uuid(uuid_status);
1399 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1400 == -1) {
1401 err = got_error_from_errno("asprintf");
1402 *refname = NULL;
1404 free(uuidstr);
1405 return err;
1408 const struct got_error *
1409 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1411 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1414 static const struct got_error *
1415 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1417 return get_ref_name(refname, worktree,
1418 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1421 static const struct got_error *
1422 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1424 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1427 static const struct got_error *
1428 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1430 return get_ref_name(refname, worktree,
1431 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1434 static const struct got_error *
1435 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1437 return get_ref_name(refname, worktree,
1438 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1441 static const struct got_error *
1442 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1444 return get_ref_name(refname, worktree,
1445 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1448 static const struct got_error *
1449 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1451 return get_ref_name(refname, worktree,
1452 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1455 static const struct got_error *
1456 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1458 return get_ref_name(refname, worktree,
1459 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1462 static const struct got_error *
1463 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1465 return get_ref_name(refname, worktree,
1466 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1469 const struct got_error *
1470 got_worktree_get_histedit_list_path(char **path, struct got_worktree *worktree)
1472 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1473 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_LIST) == -1) {
1474 *path = NULL;
1475 return got_error_from_errno("asprintf");
1477 return NULL;
1481 * Prevent Git's garbage collector from deleting our base commit by
1482 * setting a reference to our base commit's ID.
1484 static const struct got_error *
1485 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_reference *ref = NULL;
1489 char *refname;
1491 err = got_worktree_get_base_ref_name(&refname, worktree);
1492 if (err)
1493 return err;
1495 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1496 if (err)
1497 goto done;
1499 err = got_ref_write(ref, repo);
1500 done:
1501 free(refname);
1502 if (ref)
1503 got_ref_close(ref);
1504 return err;
1507 static const struct got_error *
1508 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1510 const struct got_error *err = NULL;
1512 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1513 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1514 err = got_error_from_errno("asprintf");
1515 *fileindex_path = NULL;
1517 return err;
1521 static const struct got_error *
1522 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1523 struct got_worktree *worktree)
1525 const struct got_error *err = NULL;
1526 FILE *index = NULL;
1528 *fileindex_path = NULL;
1529 *fileindex = got_fileindex_alloc();
1530 if (*fileindex == NULL)
1531 return got_error_from_errno("got_fileindex_alloc");
1533 err = get_fileindex_path(fileindex_path, worktree);
1534 if (err)
1535 goto done;
1537 index = fopen(*fileindex_path, "rb");
1538 if (index == NULL) {
1539 if (errno != ENOENT)
1540 err = got_error_from_errno2("fopen", *fileindex_path);
1541 } else {
1542 err = got_fileindex_read(*fileindex, index);
1543 if (fclose(index) != 0 && err == NULL)
1544 err = got_error_from_errno("fclose");
1546 done:
1547 if (err) {
1548 free(*fileindex_path);
1549 *fileindex_path = NULL;
1550 got_fileindex_free(*fileindex);
1551 *fileindex = NULL;
1553 return err;
1556 struct bump_base_commit_id_arg {
1557 struct got_object_id *base_commit_id;
1558 const char *path;
1559 size_t path_len;
1560 const char *entry_name;
1561 got_worktree_checkout_cb progress_cb;
1562 void *progress_arg;
1565 /* Bump base commit ID of all files within an updated part of the work tree. */
1566 static const struct got_error *
1567 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1569 const struct got_error *err;
1570 struct bump_base_commit_id_arg *a = arg;
1572 if (a->entry_name) {
1573 if (strcmp(ie->path, a->path) != 0)
1574 return NULL;
1575 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1576 return NULL;
1578 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1579 SHA1_DIGEST_LENGTH) == 0)
1580 return NULL;
1582 if (a->progress_cb) {
1583 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1584 ie->path);
1585 if (err)
1586 return err;
1588 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1589 return NULL;
1592 static const struct got_error *
1593 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1595 const struct got_error *err = NULL;
1596 char *new_fileindex_path = NULL;
1597 FILE *new_index = NULL;
1599 err = got_opentemp_named(&new_fileindex_path, &new_index,
1600 fileindex_path);
1601 if (err)
1602 goto done;
1604 err = got_fileindex_write(fileindex, new_index);
1605 if (err)
1606 goto done;
1608 if (rename(new_fileindex_path, fileindex_path) != 0) {
1609 err = got_error_from_errno3("rename", new_fileindex_path,
1610 fileindex_path);
1611 unlink(new_fileindex_path);
1613 done:
1614 if (new_index)
1615 fclose(new_index);
1616 free(new_fileindex_path);
1617 return err;
1620 static const struct got_error *
1621 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1622 struct got_object_id **tree_id, const char *wt_relpath,
1623 struct got_worktree *worktree, struct got_repository *repo)
1625 const struct got_error *err = NULL;
1626 struct got_object_id *id = NULL;
1627 char *in_repo_path = NULL;
1628 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1630 *entry_type = GOT_OBJ_TYPE_ANY;
1631 *tree_relpath = NULL;
1632 *tree_id = NULL;
1634 if (wt_relpath[0] == '\0') {
1635 /* Check out all files within the work tree. */
1636 *entry_type = GOT_OBJ_TYPE_TREE;
1637 *tree_relpath = strdup("");
1638 if (*tree_relpath == NULL) {
1639 err = got_error_from_errno("strdup");
1640 goto done;
1642 err = got_object_id_by_path(tree_id, repo,
1643 worktree->base_commit_id, worktree->path_prefix);
1644 if (err)
1645 goto done;
1646 return NULL;
1649 /* Check out a subset of files in the work tree. */
1651 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1652 is_root_wt ? "" : "/", wt_relpath) == -1) {
1653 err = got_error_from_errno("asprintf");
1654 goto done;
1657 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1658 in_repo_path);
1659 if (err)
1660 goto done;
1662 free(in_repo_path);
1663 in_repo_path = NULL;
1665 err = got_object_get_type(entry_type, repo, id);
1666 if (err)
1667 goto done;
1669 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1670 /* Check out a single file. */
1671 if (strchr(wt_relpath, '/') == NULL) {
1672 /* Check out a single file in work tree's root dir. */
1673 in_repo_path = strdup(worktree->path_prefix);
1674 if (in_repo_path == NULL) {
1675 err = got_error_from_errno("strdup");
1676 goto done;
1678 *tree_relpath = strdup("");
1679 if (*tree_relpath == NULL) {
1680 err = got_error_from_errno("strdup");
1681 goto done;
1683 } else {
1684 /* Check out a single file in a subdirectory. */
1685 err = got_path_dirname(tree_relpath, wt_relpath);
1686 if (err)
1687 return err;
1688 if (asprintf(&in_repo_path, "%s%s%s",
1689 worktree->path_prefix, is_root_wt ? "" : "/",
1690 *tree_relpath) == -1) {
1691 err = got_error_from_errno("asprintf");
1692 goto done;
1695 err = got_object_id_by_path(tree_id, repo,
1696 worktree->base_commit_id, in_repo_path);
1697 } else {
1698 /* Check out all files within a subdirectory. */
1699 *tree_id = got_object_id_dup(id);
1700 if (*tree_id == NULL) {
1701 err = got_error_from_errno("got_object_id_dup");
1702 goto done;
1704 *tree_relpath = strdup(wt_relpath);
1705 if (*tree_relpath == NULL) {
1706 err = got_error_from_errno("strdup");
1707 goto done;
1710 done:
1711 free(id);
1712 free(in_repo_path);
1713 if (err) {
1714 *entry_type = GOT_OBJ_TYPE_ANY;
1715 free(*tree_relpath);
1716 *tree_relpath = NULL;
1717 free(*tree_id);
1718 *tree_id = NULL;
1720 return err;
1723 static const struct got_error *
1724 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1725 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1726 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1727 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1729 const struct got_error *err = NULL;
1730 struct got_commit_object *commit = NULL;
1731 struct got_tree_object *tree = NULL;
1732 struct got_fileindex_diff_tree_cb diff_cb;
1733 struct diff_cb_arg arg;
1735 err = ref_base_commit(worktree, repo);
1736 if (err)
1737 goto done;
1739 err = got_object_open_as_commit(&commit, repo,
1740 worktree->base_commit_id);
1741 if (err)
1742 goto done;
1744 err = got_object_open_as_tree(&tree, repo, tree_id);
1745 if (err)
1746 goto done;
1748 if (entry_name &&
1749 got_object_tree_find_entry(tree, entry_name) == NULL) {
1750 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1751 goto done;
1754 diff_cb.diff_old_new = diff_old_new;
1755 diff_cb.diff_old = diff_old;
1756 diff_cb.diff_new = diff_new;
1757 arg.fileindex = fileindex;
1758 arg.worktree = worktree;
1759 arg.repo = repo;
1760 arg.progress_cb = progress_cb;
1761 arg.progress_arg = progress_arg;
1762 arg.cancel_cb = cancel_cb;
1763 arg.cancel_arg = cancel_arg;
1764 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1765 entry_name, repo, &diff_cb, &arg);
1766 done:
1767 if (tree)
1768 got_object_tree_close(tree);
1769 if (commit)
1770 got_object_commit_close(commit);
1771 return err;
1774 const struct got_error *
1775 got_worktree_checkout_files(struct got_worktree *worktree,
1776 struct got_pathlist_head *paths, struct got_repository *repo,
1777 got_worktree_checkout_cb progress_cb, void *progress_arg,
1778 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1780 const struct got_error *err = NULL, *sync_err, *unlockerr;
1781 struct got_commit_object *commit = NULL;
1782 struct got_tree_object *tree = NULL;
1783 struct got_fileindex *fileindex = NULL;
1784 char *fileindex_path = NULL;
1785 struct got_pathlist_entry *pe;
1786 struct tree_path_data {
1787 SIMPLEQ_ENTRY(tree_path_data) entry;
1788 struct got_object_id *tree_id;
1789 int entry_type;
1790 char *relpath;
1791 char *entry_name;
1792 } *tpd = NULL;
1793 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1795 SIMPLEQ_INIT(&tree_paths);
1797 err = lock_worktree(worktree, LOCK_EX);
1798 if (err)
1799 return err;
1801 /* Map all specified paths to in-repository trees. */
1802 TAILQ_FOREACH(pe, paths, entry) {
1803 tpd = malloc(sizeof(*tpd));
1804 if (tpd == NULL) {
1805 err = got_error_from_errno("malloc");
1806 goto done;
1809 err = find_tree_entry_for_checkout(&tpd->entry_type,
1810 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1811 if (err) {
1812 free(tpd);
1813 goto done;
1816 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1817 err = got_path_basename(&tpd->entry_name, pe->path);
1818 if (err) {
1819 free(tpd->relpath);
1820 free(tpd->tree_id);
1821 free(tpd);
1822 goto done;
1824 } else
1825 tpd->entry_name = NULL;
1827 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1831 * Read the file index.
1832 * Checking out files is supposed to be an idempotent operation.
1833 * If the on-disk file index is incomplete we will try to complete it.
1835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1836 if (err)
1837 goto done;
1839 tpd = SIMPLEQ_FIRST(&tree_paths);
1840 TAILQ_FOREACH(pe, paths, entry) {
1841 struct bump_base_commit_id_arg bbc_arg;
1843 err = checkout_files(worktree, fileindex, tpd->relpath,
1844 tpd->tree_id, tpd->entry_name, repo,
1845 progress_cb, progress_arg, cancel_cb, cancel_arg);
1846 if (err)
1847 break;
1849 bbc_arg.base_commit_id = worktree->base_commit_id;
1850 bbc_arg.entry_name = tpd->entry_name;
1851 bbc_arg.path = pe->path;
1852 bbc_arg.path_len = strlen(pe->path);
1853 bbc_arg.progress_cb = progress_cb;
1854 bbc_arg.progress_arg = progress_arg;
1855 err = got_fileindex_for_each_entry_safe(fileindex,
1856 bump_base_commit_id, &bbc_arg);
1857 if (err)
1858 break;
1860 tpd = SIMPLEQ_NEXT(tpd, entry);
1862 sync_err = sync_fileindex(fileindex, fileindex_path);
1863 if (sync_err && err == NULL)
1864 err = sync_err;
1865 done:
1866 free(fileindex_path);
1867 if (tree)
1868 got_object_tree_close(tree);
1869 if (commit)
1870 got_object_commit_close(commit);
1871 if (fileindex)
1872 got_fileindex_free(fileindex);
1873 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1874 tpd = SIMPLEQ_FIRST(&tree_paths);
1875 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1876 free(tpd->relpath);
1877 free(tpd->tree_id);
1878 free(tpd);
1880 unlockerr = lock_worktree(worktree, LOCK_SH);
1881 if (unlockerr && err == NULL)
1882 err = unlockerr;
1883 return err;
1886 struct merge_file_cb_arg {
1887 struct got_worktree *worktree;
1888 struct got_fileindex *fileindex;
1889 got_worktree_checkout_cb progress_cb;
1890 void *progress_arg;
1891 got_worktree_cancel_cb cancel_cb;
1892 void *cancel_arg;
1893 struct got_object_id *commit_id2;
1896 static const struct got_error *
1897 merge_file_cb(void *arg, struct got_blob_object *blob1,
1898 struct got_blob_object *blob2, struct got_object_id *id1,
1899 struct got_object_id *id2, const char *path1, const char *path2,
1900 struct got_repository *repo)
1902 static const struct got_error *err = NULL;
1903 struct merge_file_cb_arg *a = arg;
1904 struct got_fileindex_entry *ie;
1905 char *ondisk_path = NULL;
1906 struct stat sb;
1907 unsigned char status;
1908 int local_changes_subsumed;
1910 if (blob1 && blob2) {
1911 ie = got_fileindex_entry_get(a->fileindex, path2);
1912 if (ie == NULL)
1913 return (*a->progress_cb)(a->progress_arg,
1914 GOT_STATUS_MISSING, path2);
1916 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1917 path2) == -1)
1918 return got_error_from_errno("asprintf");
1920 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1921 if (err)
1922 goto done;
1924 if (status == GOT_STATUS_DELETE) {
1925 err = (*a->progress_cb)(a->progress_arg,
1926 GOT_STATUS_MERGE, path2);
1927 goto done;
1929 if (status != GOT_STATUS_NO_CHANGE &&
1930 status != GOT_STATUS_MODIFY &&
1931 status != GOT_STATUS_CONFLICT &&
1932 status != GOT_STATUS_ADD) {
1933 err = (*a->progress_cb)(a->progress_arg, status, path2);
1934 goto done;
1937 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1938 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1939 a->progress_cb, a->progress_arg);
1940 } else if (blob1) {
1941 ie = got_fileindex_entry_get(a->fileindex, path1);
1942 if (ie == NULL)
1943 return (*a->progress_cb)(a->progress_arg,
1944 GOT_STATUS_MISSING, path2);
1946 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1947 path1) == -1)
1948 return got_error_from_errno("asprintf");
1950 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1951 if (err)
1952 goto done;
1954 switch (status) {
1955 case GOT_STATUS_NO_CHANGE:
1956 err = (*a->progress_cb)(a->progress_arg,
1957 GOT_STATUS_DELETE, path1);
1958 if (err)
1959 goto done;
1960 err = remove_ondisk_file(a->worktree->root_path, path1);
1961 if (err)
1962 goto done;
1963 if (ie)
1964 got_fileindex_entry_mark_deleted_from_disk(ie);
1965 break;
1966 case GOT_STATUS_DELETE:
1967 case GOT_STATUS_MISSING:
1968 err = (*a->progress_cb)(a->progress_arg,
1969 GOT_STATUS_DELETE, path1);
1970 if (err)
1971 goto done;
1972 if (ie)
1973 got_fileindex_entry_mark_deleted_from_disk(ie);
1974 break;
1975 case GOT_STATUS_ADD:
1976 case GOT_STATUS_MODIFY:
1977 case GOT_STATUS_CONFLICT:
1978 err = (*a->progress_cb)(a->progress_arg,
1979 GOT_STATUS_CANNOT_DELETE, path1);
1980 if (err)
1981 goto done;
1982 break;
1983 case GOT_STATUS_OBSTRUCTED:
1984 err = (*a->progress_cb)(a->progress_arg, status, path1);
1985 if (err)
1986 goto done;
1987 break;
1988 default:
1989 break;
1991 } else if (blob2) {
1992 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1993 path2) == -1)
1994 return got_error_from_errno("asprintf");
1995 ie = got_fileindex_entry_get(a->fileindex, path2);
1996 if (ie) {
1997 err = get_file_status(&status, &sb, ie, ondisk_path,
1998 repo);
1999 if (err)
2000 goto done;
2001 if (status != GOT_STATUS_NO_CHANGE &&
2002 status != GOT_STATUS_MODIFY &&
2003 status != GOT_STATUS_CONFLICT &&
2004 status != GOT_STATUS_ADD) {
2005 err = (*a->progress_cb)(a->progress_arg,
2006 status, path2);
2007 goto done;
2009 err = merge_blob(&local_changes_subsumed, a->worktree,
2010 NULL, ondisk_path, path2, sb.st_mode, blob2,
2011 a->commit_id2, repo,
2012 a->progress_cb, a->progress_arg);
2013 if (status == GOT_STATUS_DELETE) {
2014 err = update_blob_fileindex_entry(a->worktree,
2015 a->fileindex, ie, ondisk_path, ie->path,
2016 blob2, 0);
2017 if (err)
2018 goto done;
2020 } else {
2021 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2022 err = install_blob(a->worktree, ondisk_path, path2,
2023 /* XXX get this from parent tree! */
2024 GOT_DEFAULT_FILE_MODE,
2025 sb.st_mode, blob2, 0, 0, repo,
2026 a->progress_cb, a->progress_arg);
2027 if (err)
2028 goto done;
2029 err = got_fileindex_entry_alloc(&ie,
2030 ondisk_path, path2, NULL, NULL);
2031 if (err)
2032 goto done;
2033 err = got_fileindex_entry_add(a->fileindex, ie);
2034 if (err) {
2035 got_fileindex_entry_free(ie);
2036 goto done;
2040 done:
2041 free(ondisk_path);
2042 return err;
2045 struct check_merge_ok_arg {
2046 struct got_worktree *worktree;
2047 struct got_repository *repo;
2050 static const struct got_error *
2051 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2053 const struct got_error *err = NULL;
2054 struct check_merge_ok_arg *a = arg;
2055 unsigned char status;
2056 struct stat sb;
2057 char *ondisk_path;
2059 /* Reject merges into a work tree with mixed base commits. */
2060 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2061 SHA1_DIGEST_LENGTH))
2062 return got_error(GOT_ERR_MIXED_COMMITS);
2064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2065 == -1)
2066 return got_error_from_errno("asprintf");
2068 /* Reject merges into a work tree with conflicted files. */
2069 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2070 if (err)
2071 return err;
2072 if (status == GOT_STATUS_CONFLICT)
2073 return got_error(GOT_ERR_CONFLICTS);
2075 return NULL;
2078 static const struct got_error *
2079 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2080 const char *fileindex_path, struct got_object_id *commit_id1,
2081 struct got_object_id *commit_id2, struct got_repository *repo,
2082 got_worktree_checkout_cb progress_cb, void *progress_arg,
2083 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2085 const struct got_error *err = NULL, *sync_err;
2086 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2087 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2088 struct merge_file_cb_arg arg;
2090 if (commit_id1) {
2091 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2092 worktree->path_prefix);
2093 if (err)
2094 goto done;
2096 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2097 if (err)
2098 goto done;
2101 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2102 worktree->path_prefix);
2103 if (err)
2104 goto done;
2106 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2107 if (err)
2108 goto done;
2110 arg.worktree = worktree;
2111 arg.fileindex = fileindex;
2112 arg.progress_cb = progress_cb;
2113 arg.progress_arg = progress_arg;
2114 arg.cancel_cb = cancel_cb;
2115 arg.cancel_arg = cancel_arg;
2116 arg.commit_id2 = commit_id2;
2117 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2118 sync_err = sync_fileindex(fileindex, fileindex_path);
2119 if (sync_err && err == NULL)
2120 err = sync_err;
2121 done:
2122 if (tree1)
2123 got_object_tree_close(tree1);
2124 if (tree2)
2125 got_object_tree_close(tree2);
2126 return err;
2129 const struct got_error *
2130 got_worktree_merge_files(struct got_worktree *worktree,
2131 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2132 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2133 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2135 const struct got_error *err, *unlockerr;
2136 char *fileindex_path = NULL;
2137 struct got_fileindex *fileindex = NULL;
2138 struct check_merge_ok_arg mok_arg;
2140 err = lock_worktree(worktree, LOCK_EX);
2141 if (err)
2142 return err;
2144 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2145 if (err)
2146 goto done;
2148 mok_arg.worktree = worktree;
2149 mok_arg.repo = repo;
2150 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2151 &mok_arg);
2152 if (err)
2153 goto done;
2155 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2156 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2157 done:
2158 if (fileindex)
2159 got_fileindex_free(fileindex);
2160 free(fileindex_path);
2161 unlockerr = lock_worktree(worktree, LOCK_SH);
2162 if (unlockerr && err == NULL)
2163 err = unlockerr;
2164 return err;
2167 struct diff_dir_cb_arg {
2168 struct got_fileindex *fileindex;
2169 struct got_worktree *worktree;
2170 const char *status_path;
2171 size_t status_path_len;
2172 struct got_repository *repo;
2173 got_worktree_status_cb status_cb;
2174 void *status_arg;
2175 got_worktree_cancel_cb cancel_cb;
2176 void *cancel_arg;
2179 static const struct got_error *
2180 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2181 got_worktree_status_cb status_cb, void *status_arg,
2182 struct got_repository *repo)
2184 const struct got_error *err = NULL;
2185 unsigned char status = GOT_STATUS_NO_CHANGE;
2186 struct stat sb;
2187 struct got_object_id blob_id, commit_id;
2189 err = get_file_status(&status, &sb, ie, abspath, repo);
2190 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2191 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2192 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2193 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2194 &commit_id);
2196 return err;
2199 static const struct got_error *
2200 status_old_new(void *arg, struct got_fileindex_entry *ie,
2201 struct dirent *de, const char *parent_path)
2203 const struct got_error *err = NULL;
2204 struct diff_dir_cb_arg *a = arg;
2205 char *abspath;
2207 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2208 return got_error(GOT_ERR_CANCELLED);
2210 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2211 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2212 return NULL;
2214 if (parent_path[0]) {
2215 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2216 parent_path, de->d_name) == -1)
2217 return got_error_from_errno("asprintf");
2218 } else {
2219 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2220 de->d_name) == -1)
2221 return got_error_from_errno("asprintf");
2224 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2225 a->repo);
2226 free(abspath);
2227 return err;
2230 static const struct got_error *
2231 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2233 struct diff_dir_cb_arg *a = arg;
2234 struct got_object_id blob_id, commit_id;
2235 unsigned char status;
2237 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2238 return got_error(GOT_ERR_CANCELLED);
2240 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2241 return NULL;
2243 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2244 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2245 if (got_fileindex_entry_has_file_on_disk(ie))
2246 status = GOT_STATUS_MISSING;
2247 else
2248 status = GOT_STATUS_DELETE;
2249 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2250 &commit_id);
2253 static const struct got_error *
2254 status_new(void *arg, struct dirent *de, const char *parent_path)
2256 const struct got_error *err = NULL;
2257 struct diff_dir_cb_arg *a = arg;
2258 char *path = NULL;
2260 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2261 return got_error(GOT_ERR_CANCELLED);
2263 if (de->d_type == DT_DIR)
2264 return NULL;
2266 /* XXX ignore symlinks for now */
2267 if (de->d_type == DT_LNK)
2268 return NULL;
2270 if (parent_path[0]) {
2271 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2272 return got_error_from_errno("asprintf");
2273 } else {
2274 path = de->d_name;
2277 if (got_path_is_child(path, a->status_path, a->status_path_len))
2278 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2279 path, NULL, NULL);
2280 if (parent_path[0])
2281 free(path);
2282 return err;
2285 static const struct got_error *
2286 worktree_status(struct got_worktree *worktree, const char *path,
2287 struct got_fileindex *fileindex, struct got_repository *repo,
2288 got_worktree_status_cb status_cb, void *status_arg,
2289 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2291 const struct got_error *err = NULL;
2292 DIR *workdir = NULL;
2293 struct got_fileindex_diff_dir_cb fdiff_cb;
2294 struct diff_dir_cb_arg arg;
2295 char *ondisk_path = NULL;
2297 if (asprintf(&ondisk_path, "%s%s%s",
2298 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2299 err = got_error_from_errno("asprintf");
2300 goto done;
2302 workdir = opendir(ondisk_path);
2303 if (workdir == NULL) {
2304 if (errno == ENOTDIR || errno == ENOENT) {
2305 err = report_file_status(
2306 got_fileindex_entry_get(fileindex, path),
2307 ondisk_path, status_cb, status_arg, repo);
2308 goto done;
2309 } else {
2310 err = got_error_from_errno2("opendir", ondisk_path);
2311 goto done;
2314 fdiff_cb.diff_old_new = status_old_new;
2315 fdiff_cb.diff_old = status_old;
2316 fdiff_cb.diff_new = status_new;
2317 arg.fileindex = fileindex;
2318 arg.worktree = worktree;
2319 arg.status_path = path;
2320 arg.status_path_len = strlen(path);
2321 arg.repo = repo;
2322 arg.status_cb = status_cb;
2323 arg.status_arg = status_arg;
2324 arg.cancel_cb = cancel_cb;
2325 arg.cancel_arg = cancel_arg;
2326 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2327 path, repo, &fdiff_cb, &arg);
2328 done:
2329 if (workdir)
2330 closedir(workdir);
2331 free(ondisk_path);
2332 return err;
2335 const struct got_error *
2336 got_worktree_status(struct got_worktree *worktree,
2337 struct got_pathlist_head *paths, struct got_repository *repo,
2338 got_worktree_status_cb status_cb, void *status_arg,
2339 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2341 const struct got_error *err = NULL;
2342 char *fileindex_path = NULL;
2343 struct got_fileindex *fileindex = NULL;
2344 struct got_pathlist_entry *pe;
2346 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2347 if (err)
2348 return err;
2350 TAILQ_FOREACH(pe, paths, entry) {
2351 err = worktree_status(worktree, pe->path, fileindex, repo,
2352 status_cb, status_arg, cancel_cb, cancel_arg);
2353 if (err)
2354 break;
2356 free(fileindex_path);
2357 got_fileindex_free(fileindex);
2358 return err;
2361 const struct got_error *
2362 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2363 const char *arg)
2365 const struct got_error *err = NULL;
2366 char *resolved, *cwd = NULL, *path = NULL;
2367 size_t len;
2369 *wt_path = NULL;
2371 resolved = realpath(arg, NULL);
2372 if (resolved == NULL) {
2373 if (errno != ENOENT)
2374 return got_error_from_errno2("realpath", arg);
2375 cwd = getcwd(NULL, 0);
2376 if (cwd == NULL)
2377 return got_error_from_errno("getcwd");
2378 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2379 err = got_error_from_errno("asprintf");
2380 goto done;
2384 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2385 strlen(got_worktree_get_root_path(worktree)))) {
2386 err = got_error(GOT_ERR_BAD_PATH);
2387 goto done;
2390 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2391 err = got_path_skip_common_ancestor(&path,
2392 got_worktree_get_root_path(worktree), resolved);
2393 if (err)
2394 goto done;
2395 } else {
2396 path = strdup("");
2397 if (path == NULL) {
2398 err = got_error_from_errno("strdup");
2399 goto done;
2403 /* XXX status walk can't deal with trailing slash! */
2404 len = strlen(path);
2405 while (len > 0 && path[len - 1] == '/') {
2406 path[len - 1] = '\0';
2407 len--;
2409 done:
2410 free(resolved);
2411 free(cwd);
2412 if (err == NULL)
2413 *wt_path = path;
2414 else
2415 free(path);
2416 return err;
2419 static const struct got_error *
2420 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2421 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2422 struct got_repository *repo)
2424 const struct got_error *err = NULL;
2425 struct got_fileindex_entry *ie;
2427 /* Re-adding an existing entry is a no-op. */
2428 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2429 return NULL;
2431 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2432 if (err)
2433 return err;
2435 err = got_fileindex_entry_add(fileindex, ie);
2436 if (err) {
2437 got_fileindex_entry_free(ie);
2438 return err;
2441 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2444 const struct got_error *
2445 got_worktree_schedule_add(struct got_worktree *worktree,
2446 struct got_pathlist_head *ondisk_paths,
2447 got_worktree_status_cb status_cb, void *status_arg,
2448 struct got_repository *repo)
2450 struct got_fileindex *fileindex = NULL;
2451 char *fileindex_path = NULL;
2452 const struct got_error *err = NULL, *sync_err, *unlockerr;
2453 struct got_pathlist_entry *pe;
2455 err = lock_worktree(worktree, LOCK_EX);
2456 if (err)
2457 return err;
2459 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2460 if (err)
2461 goto done;
2463 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2464 char *relpath;
2465 err = got_path_skip_common_ancestor(&relpath,
2466 got_worktree_get_root_path(worktree), pe->path);
2467 if (err)
2468 break;
2469 err = schedule_addition(pe->path, fileindex, relpath,
2470 status_cb, status_arg, repo);
2471 free(relpath);
2472 if (err)
2473 break;
2475 sync_err = sync_fileindex(fileindex, fileindex_path);
2476 if (sync_err && err == NULL)
2477 err = sync_err;
2478 done:
2479 free(fileindex_path);
2480 if (fileindex)
2481 got_fileindex_free(fileindex);
2482 unlockerr = lock_worktree(worktree, LOCK_SH);
2483 if (unlockerr && err == NULL)
2484 err = unlockerr;
2485 return err;
2488 static const struct got_error *
2489 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2490 const char *relpath, int delete_local_mods,
2491 got_worktree_status_cb status_cb, void *status_arg,
2492 struct got_repository *repo)
2494 const struct got_error *err = NULL;
2495 struct got_fileindex_entry *ie = NULL;
2496 unsigned char status;
2497 struct stat sb;
2499 ie = got_fileindex_entry_get(fileindex, relpath);
2500 if (ie == NULL)
2501 return got_error(GOT_ERR_BAD_PATH);
2503 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2504 if (err)
2505 return err;
2507 if (status != GOT_STATUS_NO_CHANGE) {
2508 if (status == GOT_STATUS_DELETE)
2509 return got_error_set_errno(ENOENT, ondisk_path);
2510 if (status != GOT_STATUS_MODIFY)
2511 return got_error(GOT_ERR_FILE_STATUS);
2512 if (!delete_local_mods)
2513 return got_error(GOT_ERR_FILE_MODIFIED);
2516 if (unlink(ondisk_path) != 0)
2517 return got_error_from_errno2("unlink", ondisk_path);
2519 got_fileindex_entry_mark_deleted_from_disk(ie);
2520 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2523 const struct got_error *
2524 got_worktree_schedule_delete(struct got_worktree *worktree,
2525 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2526 got_worktree_status_cb status_cb, void *status_arg,
2527 struct got_repository *repo)
2529 struct got_fileindex *fileindex = NULL;
2530 char *fileindex_path = NULL;
2531 const struct got_error *err = NULL, *sync_err, *unlockerr;
2532 struct got_pathlist_entry *pe;
2534 err = lock_worktree(worktree, LOCK_EX);
2535 if (err)
2536 return err;
2538 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2539 if (err)
2540 goto done;
2542 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2543 char *relpath;
2544 err = got_path_skip_common_ancestor(&relpath,
2545 got_worktree_get_root_path(worktree), pe->path);
2546 if (err)
2547 break;
2548 err = schedule_for_deletion(pe->path, fileindex, relpath,
2549 delete_local_mods, status_cb, status_arg, repo);
2550 free(relpath);
2551 if (err)
2552 break;
2554 sync_err = sync_fileindex(fileindex, fileindex_path);
2555 if (sync_err && err == NULL)
2556 err = sync_err;
2557 done:
2558 free(fileindex_path);
2559 if (fileindex)
2560 got_fileindex_free(fileindex);
2561 unlockerr = lock_worktree(worktree, LOCK_SH);
2562 if (unlockerr && err == NULL)
2563 err = unlockerr;
2564 return err;
2567 static const struct got_error *
2568 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2569 const char *ondisk_path,
2570 got_worktree_checkout_cb progress_cb, void *progress_arg,
2571 struct got_repository *repo)
2573 const struct got_error *err = NULL;
2574 char *relpath = NULL, *parent_path = NULL;
2575 struct got_fileindex_entry *ie;
2576 struct got_tree_object *tree = NULL;
2577 struct got_object_id *tree_id = NULL;
2578 const struct got_tree_entry *te;
2579 char *tree_path = NULL, *te_name;
2580 struct got_blob_object *blob = NULL;
2581 unsigned char status;
2582 struct stat sb;
2584 err = got_path_skip_common_ancestor(&relpath,
2585 got_worktree_get_root_path(worktree), ondisk_path);
2586 if (err)
2587 goto done;
2589 ie = got_fileindex_entry_get(fileindex, relpath);
2590 if (ie == NULL) {
2591 err = got_error(GOT_ERR_BAD_PATH);
2592 goto done;
2595 /* Construct in-repository path of tree which contains this blob. */
2596 err = got_path_dirname(&parent_path, ie->path);
2597 if (err) {
2598 if (err->code != GOT_ERR_BAD_PATH)
2599 goto done;
2600 parent_path = strdup("/");
2601 if (parent_path == NULL) {
2602 err = got_error_from_errno("strdup");
2603 goto done;
2606 if (got_path_is_root_dir(worktree->path_prefix)) {
2607 tree_path = strdup(parent_path);
2608 if (tree_path == NULL) {
2609 err = got_error_from_errno("strdup");
2610 goto done;
2612 } else {
2613 if (got_path_is_root_dir(parent_path)) {
2614 tree_path = strdup(worktree->path_prefix);
2615 if (tree_path == NULL) {
2616 err = got_error_from_errno("strdup");
2617 goto done;
2619 } else {
2620 if (asprintf(&tree_path, "%s/%s",
2621 worktree->path_prefix, parent_path) == -1) {
2622 err = got_error_from_errno("asprintf");
2623 goto done;
2628 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2629 tree_path);
2630 if (err)
2631 goto done;
2633 err = got_object_open_as_tree(&tree, repo, tree_id);
2634 if (err)
2635 goto done;
2637 te_name = basename(ie->path);
2638 if (te_name == NULL) {
2639 err = got_error_from_errno2("basename", ie->path);
2640 goto done;
2643 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2644 if (err)
2645 goto done;
2647 te = got_object_tree_find_entry(tree, te_name);
2648 if (te == NULL && status != GOT_STATUS_ADD) {
2649 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2650 goto done;
2653 switch (status) {
2654 case GOT_STATUS_ADD:
2655 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2656 if (err)
2657 goto done;
2658 got_fileindex_entry_remove(fileindex, ie);
2659 break;
2660 case GOT_STATUS_DELETE:
2661 case GOT_STATUS_MODIFY:
2662 case GOT_STATUS_CONFLICT:
2663 case GOT_STATUS_MISSING: {
2664 struct got_object_id id;
2665 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2666 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2667 if (err)
2668 goto done;
2669 err = install_blob(worktree, ondisk_path, ie->path,
2670 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2671 progress_arg);
2672 if (err)
2673 goto done;
2674 if (status == GOT_STATUS_DELETE) {
2675 err = update_blob_fileindex_entry(worktree,
2676 fileindex, ie, ondisk_path, ie->path, blob, 1);
2677 if (err)
2678 goto done;
2680 break;
2682 default:
2683 goto done;
2685 done:
2686 free(relpath);
2687 free(parent_path);
2688 free(tree_path);
2689 if (blob)
2690 got_object_blob_close(blob);
2691 if (tree)
2692 got_object_tree_close(tree);
2693 free(tree_id);
2694 return err;
2697 const struct got_error *
2698 got_worktree_revert(struct got_worktree *worktree,
2699 struct got_pathlist_head *ondisk_paths,
2700 got_worktree_checkout_cb progress_cb, void *progress_arg,
2701 struct got_repository *repo)
2703 struct got_fileindex *fileindex = NULL;
2704 char *fileindex_path = NULL;
2705 const struct got_error *err = NULL, *unlockerr = NULL;
2706 const struct got_error *sync_err = NULL;
2707 struct got_pathlist_entry *pe;
2709 err = lock_worktree(worktree, LOCK_EX);
2710 if (err)
2711 return err;
2713 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2714 if (err)
2715 goto done;
2717 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2718 err = revert_file(worktree, fileindex, pe->path,
2719 progress_cb, progress_arg, repo);
2720 if (err)
2721 break;
2723 sync_err = sync_fileindex(fileindex, fileindex_path);
2724 if (sync_err && err == NULL)
2725 err = sync_err;
2726 done:
2727 free(fileindex_path);
2728 if (fileindex)
2729 got_fileindex_free(fileindex);
2730 unlockerr = lock_worktree(worktree, LOCK_SH);
2731 if (unlockerr && err == NULL)
2732 err = unlockerr;
2733 return err;
2736 static void
2737 free_commitable(struct got_commitable *ct)
2739 free(ct->path);
2740 free(ct->in_repo_path);
2741 free(ct->ondisk_path);
2742 free(ct->blob_id);
2743 free(ct->base_blob_id);
2744 free(ct->base_commit_id);
2745 free(ct);
2748 struct collect_commitables_arg {
2749 struct got_pathlist_head *commitable_paths;
2750 struct got_repository *repo;
2751 struct got_worktree *worktree;
2754 static const struct got_error *
2755 collect_commitables(void *arg, unsigned char status, const char *relpath,
2756 struct got_object_id *blob_id, struct got_object_id *commit_id)
2758 struct collect_commitables_arg *a = arg;
2759 const struct got_error *err = NULL;
2760 struct got_commitable *ct = NULL;
2761 struct got_pathlist_entry *new = NULL;
2762 char *parent_path = NULL, *path = NULL;
2763 struct stat sb;
2765 if (status == GOT_STATUS_CONFLICT)
2766 return got_error(GOT_ERR_COMMIT_CONFLICT);
2768 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2769 status != GOT_STATUS_DELETE)
2770 return NULL;
2772 if (asprintf(&path, "/%s", relpath) == -1) {
2773 err = got_error_from_errno("asprintf");
2774 goto done;
2776 if (strcmp(path, "/") == 0) {
2777 parent_path = strdup("");
2778 if (parent_path == NULL)
2779 return got_error_from_errno("strdup");
2780 } else {
2781 err = got_path_dirname(&parent_path, path);
2782 if (err)
2783 return err;
2786 ct = calloc(1, sizeof(*ct));
2787 if (ct == NULL) {
2788 err = got_error_from_errno("calloc");
2789 goto done;
2792 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2793 relpath) == -1) {
2794 err = got_error_from_errno("asprintf");
2795 goto done;
2797 if (status == GOT_STATUS_DELETE) {
2798 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2799 } else {
2800 if (lstat(ct->ondisk_path, &sb) != 0) {
2801 err = got_error_from_errno2("lstat", ct->ondisk_path);
2802 goto done;
2804 ct->mode = sb.st_mode;
2807 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2808 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2809 relpath) == -1) {
2810 err = got_error_from_errno("asprintf");
2811 goto done;
2814 ct->status = status;
2815 ct->blob_id = NULL; /* will be filled in when blob gets created */
2816 if (ct->status != GOT_STATUS_ADD) {
2817 ct->base_blob_id = got_object_id_dup(blob_id);
2818 if (ct->base_blob_id == NULL) {
2819 err = got_error_from_errno("got_object_id_dup");
2820 goto done;
2822 ct->base_commit_id = got_object_id_dup(commit_id);
2823 if (ct->base_commit_id == NULL) {
2824 err = got_error_from_errno("got_object_id_dup");
2825 goto done;
2828 ct->path = strdup(path);
2829 if (ct->path == NULL) {
2830 err = got_error_from_errno("strdup");
2831 goto done;
2833 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2834 done:
2835 if (ct && (err || new == NULL))
2836 free_commitable(ct);
2837 free(parent_path);
2838 free(path);
2839 return err;
2842 static const struct got_error *write_tree(struct got_object_id **,
2843 struct got_tree_object *, const char *, struct got_pathlist_head *,
2844 got_worktree_status_cb status_cb, void *status_arg,
2845 struct got_repository *);
2847 static const struct got_error *
2848 write_subtree(struct got_object_id **new_subtree_id,
2849 struct got_tree_entry *te, const char *parent_path,
2850 struct got_pathlist_head *commitable_paths,
2851 got_worktree_status_cb status_cb, void *status_arg,
2852 struct got_repository *repo)
2854 const struct got_error *err = NULL;
2855 struct got_tree_object *subtree;
2856 char *subpath;
2858 if (asprintf(&subpath, "%s%s%s", parent_path,
2859 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2860 return got_error_from_errno("asprintf");
2862 err = got_object_open_as_tree(&subtree, repo, te->id);
2863 if (err)
2864 return err;
2866 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2867 status_cb, status_arg, repo);
2868 got_object_tree_close(subtree);
2869 free(subpath);
2870 return err;
2873 static const struct got_error *
2874 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2876 const struct got_error *err = NULL;
2877 char *ct_parent_path = NULL;
2879 *match = 0;
2881 if (strchr(ct->in_repo_path, '/') == NULL) {
2882 *match = got_path_is_root_dir(path);
2883 return NULL;
2886 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2887 if (err)
2888 return err;
2889 *match = (strcmp(path, ct_parent_path) == 0);
2890 free(ct_parent_path);
2891 return err;
2894 static mode_t
2895 get_ct_file_mode(struct got_commitable *ct)
2897 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2900 static const struct got_error *
2901 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2902 struct got_tree_entry *te, struct got_commitable *ct)
2904 const struct got_error *err = NULL;
2906 *new_te = NULL;
2908 err = got_object_tree_entry_dup(new_te, te);
2909 if (err)
2910 goto done;
2912 (*new_te)->mode = get_ct_file_mode(ct);
2914 free((*new_te)->id);
2915 (*new_te)->id = got_object_id_dup(ct->blob_id);
2916 if ((*new_te)->id == NULL) {
2917 err = got_error_from_errno("got_object_id_dup");
2918 goto done;
2920 done:
2921 if (err && *new_te) {
2922 got_object_tree_entry_close(*new_te);
2923 *new_te = NULL;
2925 return err;
2928 static const struct got_error *
2929 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2930 struct got_commitable *ct)
2932 const struct got_error *err = NULL;
2933 char *ct_name;
2935 *new_te = NULL;
2937 *new_te = calloc(1, sizeof(**new_te));
2938 if (*new_te == NULL)
2939 return got_error_from_errno("calloc");
2941 ct_name = basename(ct->path);
2942 if (ct_name == NULL) {
2943 err = got_error_from_errno2("basename", ct->path);
2944 goto done;
2946 (*new_te)->name = strdup(ct_name);
2947 if ((*new_te)->name == NULL) {
2948 err = got_error_from_errno("strdup");
2949 goto done;
2952 (*new_te)->mode = get_ct_file_mode(ct);
2954 (*new_te)->id = got_object_id_dup(ct->blob_id);
2955 if ((*new_te)->id == NULL) {
2956 err = got_error_from_errno("got_object_id_dup");
2957 goto done;
2959 done:
2960 if (err && *new_te) {
2961 got_object_tree_entry_close(*new_te);
2962 *new_te = NULL;
2964 return err;
2967 static const struct got_error *
2968 insert_tree_entry(struct got_tree_entry *new_te,
2969 struct got_pathlist_head *paths)
2971 const struct got_error *err = NULL;
2972 struct got_pathlist_entry *new_pe;
2974 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2975 if (err)
2976 return err;
2977 if (new_pe == NULL)
2978 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2979 return NULL;
2982 static const struct got_error *
2983 report_ct_status(struct got_commitable *ct,
2984 got_worktree_status_cb status_cb, void *status_arg)
2986 const char *ct_path = ct->path;
2987 while (ct_path[0] == '/')
2988 ct_path++;
2989 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2992 static const struct got_error *
2993 match_modified_subtree(int *modified, struct got_tree_entry *te,
2994 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2996 const struct got_error *err = NULL;
2997 struct got_pathlist_entry *pe;
2998 char *te_path;
3000 *modified = 0;
3002 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3003 got_path_is_root_dir(base_tree_path) ? "" : "/",
3004 te->name) == -1)
3005 return got_error_from_errno("asprintf");
3007 TAILQ_FOREACH(pe, commitable_paths, entry) {
3008 struct got_commitable *ct = pe->data;
3009 *modified = got_path_is_child(ct->in_repo_path, te_path,
3010 strlen(te_path));
3011 if (*modified)
3012 break;
3015 free(te_path);
3016 return err;
3019 static const struct got_error *
3020 match_deleted_or_modified_ct(struct got_commitable **ctp,
3021 struct got_tree_entry *te, const char *base_tree_path,
3022 struct got_pathlist_head *commitable_paths)
3024 const struct got_error *err = NULL;
3025 struct got_pathlist_entry *pe;
3027 *ctp = NULL;
3029 TAILQ_FOREACH(pe, commitable_paths, entry) {
3030 struct got_commitable *ct = pe->data;
3031 char *ct_name = NULL;
3032 int path_matches;
3034 if (ct->status != GOT_STATUS_MODIFY &&
3035 ct->status != GOT_STATUS_DELETE)
3036 continue;
3038 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3039 continue;
3041 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3042 if (err)
3043 return err;
3044 if (!path_matches)
3045 continue;
3047 ct_name = basename(pe->path);
3048 if (ct_name == NULL)
3049 return got_error_from_errno2("basename", pe->path);
3051 if (strcmp(te->name, ct_name) != 0)
3052 continue;
3054 *ctp = ct;
3055 break;
3058 return err;
3061 static const struct got_error *
3062 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3063 const char *child_path, const char *path_base_tree,
3064 struct got_pathlist_head *commitable_paths,
3065 got_worktree_status_cb status_cb, void *status_arg,
3066 struct got_repository *repo)
3068 const struct got_error *err = NULL;
3069 struct got_tree_entry *new_te;
3070 char *subtree_path;
3072 *new_tep = NULL;
3074 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3075 got_path_is_root_dir(path_base_tree) ? "" : "/",
3076 child_path) == -1)
3077 return got_error_from_errno("asprintf");
3079 new_te = calloc(1, sizeof(*new_te));
3080 new_te->mode = S_IFDIR;
3081 new_te->name = strdup(child_path);
3082 if (new_te->name == NULL) {
3083 err = got_error_from_errno("strdup");
3084 got_object_tree_entry_close(new_te);
3085 goto done;
3087 err = write_tree(&new_te->id, NULL, subtree_path,
3088 commitable_paths, status_cb, status_arg, repo);
3089 if (err) {
3090 got_object_tree_entry_close(new_te);
3091 goto done;
3093 done:
3094 free(subtree_path);
3095 if (err == NULL)
3096 *new_tep = new_te;
3097 return err;
3100 static const struct got_error *
3101 write_tree(struct got_object_id **new_tree_id,
3102 struct got_tree_object *base_tree, const char *path_base_tree,
3103 struct got_pathlist_head *commitable_paths,
3104 got_worktree_status_cb status_cb, void *status_arg,
3105 struct got_repository *repo)
3107 const struct got_error *err = NULL;
3108 const struct got_tree_entries *base_entries = NULL;
3109 struct got_pathlist_head paths;
3110 struct got_tree_entries new_tree_entries;
3111 struct got_tree_entry *te, *new_te = NULL;
3112 struct got_pathlist_entry *pe;
3114 TAILQ_INIT(&paths);
3115 new_tree_entries.nentries = 0;
3116 SIMPLEQ_INIT(&new_tree_entries.head);
3118 /* Insert, and recurse into, newly added entries first. */
3119 TAILQ_FOREACH(pe, commitable_paths, entry) {
3120 struct got_commitable *ct = pe->data;
3121 char *child_path = NULL, *slash;
3123 if (ct->status != GOT_STATUS_ADD ||
3124 (ct->flags & GOT_COMMITABLE_ADDED))
3125 continue;
3127 if (!got_path_is_child(pe->path, path_base_tree,
3128 strlen(path_base_tree)))
3129 continue;
3131 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3132 pe->path);
3133 if (err)
3134 goto done;
3136 slash = strchr(child_path, '/');
3137 if (slash == NULL) {
3138 err = alloc_added_blob_tree_entry(&new_te, ct);
3139 if (err)
3140 goto done;
3141 err = report_ct_status(ct, status_cb, status_arg);
3142 if (err)
3143 goto done;
3144 ct->flags |= GOT_COMMITABLE_ADDED;
3145 err = insert_tree_entry(new_te, &paths);
3146 if (err)
3147 goto done;
3148 } else {
3149 *slash = '\0'; /* trim trailing path components */
3150 if (base_tree == NULL ||
3151 got_object_tree_find_entry(base_tree, child_path)
3152 == NULL) {
3153 err = make_subtree_for_added_blob(&new_te,
3154 child_path, path_base_tree,
3155 commitable_paths, status_cb, status_arg,
3156 repo);
3157 if (err)
3158 goto done;
3159 err = insert_tree_entry(new_te, &paths);
3160 if (err)
3161 goto done;
3166 if (base_tree) {
3167 /* Handle modified and deleted entries. */
3168 base_entries = got_object_tree_get_entries(base_tree);
3169 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3170 struct got_commitable *ct = NULL;
3172 if (S_ISDIR(te->mode)) {
3173 int modified;
3174 err = got_object_tree_entry_dup(&new_te, te);
3175 if (err)
3176 goto done;
3177 err = match_modified_subtree(&modified, te,
3178 path_base_tree, commitable_paths);
3179 if (err)
3180 goto done;
3181 /* Avoid recursion into unmodified subtrees. */
3182 if (modified) {
3183 free(new_te->id);
3184 err = write_subtree(&new_te->id, te,
3185 path_base_tree, commitable_paths,
3186 status_cb, status_arg, repo);
3187 if (err)
3188 goto done;
3190 err = insert_tree_entry(new_te, &paths);
3191 if (err)
3192 goto done;
3193 continue;
3196 err = match_deleted_or_modified_ct(&ct, te,
3197 path_base_tree, commitable_paths);
3198 if (ct) {
3199 /* NB: Deleted entries get dropped here. */
3200 if (ct->status == GOT_STATUS_MODIFY) {
3201 err = alloc_modified_blob_tree_entry(
3202 &new_te, te, ct);
3203 if (err)
3204 goto done;
3205 err = insert_tree_entry(new_te, &paths);
3206 if (err)
3207 goto done;
3209 err = report_ct_status(ct, status_cb,
3210 status_arg);
3211 if (err)
3212 goto done;
3213 } else {
3214 /* Entry is unchanged; just copy it. */
3215 err = got_object_tree_entry_dup(&new_te, te);
3216 if (err)
3217 goto done;
3218 err = insert_tree_entry(new_te, &paths);
3219 if (err)
3220 goto done;
3225 /* Write new list of entries; deleted entries have been dropped. */
3226 TAILQ_FOREACH(pe, &paths, entry) {
3227 struct got_tree_entry *te = pe->data;
3228 new_tree_entries.nentries++;
3229 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3231 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3232 done:
3233 got_object_tree_entries_close(&new_tree_entries);
3234 got_pathlist_free(&paths);
3235 return err;
3238 static const struct got_error *
3239 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3240 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3242 const struct got_error *err = NULL;
3243 struct got_pathlist_entry *pe;
3245 TAILQ_FOREACH(pe, commitable_paths, entry) {
3246 struct got_fileindex_entry *ie;
3247 struct got_commitable *ct = pe->data;
3249 ie = got_fileindex_entry_get(fileindex, pe->path);
3250 if (ie) {
3251 if (ct->status == GOT_STATUS_DELETE) {
3252 got_fileindex_entry_remove(fileindex, ie);
3253 got_fileindex_entry_free(ie);
3254 } else
3255 err = got_fileindex_entry_update(ie,
3256 ct->ondisk_path, ct->blob_id->sha1,
3257 new_base_commit_id->sha1, 1);
3258 } else {
3259 err = got_fileindex_entry_alloc(&ie,
3260 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3261 new_base_commit_id->sha1);
3262 if (err)
3263 break;
3264 err = got_fileindex_entry_add(fileindex, ie);
3265 if (err)
3266 break;
3269 return err;
3272 static const struct got_error *
3273 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3274 struct got_object_id *head_commit_id)
3276 const struct got_error *err = NULL;
3277 struct got_object_id *id_in_head = NULL, *id = NULL;
3278 struct got_commit_object *commit = NULL;
3279 char *path = NULL;
3280 const char *ct_path = ct->in_repo_path;
3282 while (ct_path[0] == '/')
3283 ct_path++;
3286 * Ensure that no modifications were made to files *and their parents*
3287 * in commits between the file's base commit and the branch head.
3289 * Checking the parents is important for detecting conflicting tree
3290 * configurations (files or parent folders might have been moved,
3291 * deleted, added again, etc.). Such changes need to be merged with
3292 * local changes before a commit can occur.
3294 * The implication is that the file's (parent) entry in the root
3295 * directory must have the same ID in all relevant commits.
3297 if (ct->status != GOT_STATUS_ADD) {
3298 struct got_object_qid *pid;
3299 char *slash;
3300 struct got_object_id *root_entry_id = NULL;
3302 /* Trivial case: base commit == head commit */
3303 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3304 return NULL;
3306 /* Compute the path to the root directory's entry. */
3307 path = strdup(ct_path);
3308 if (path == NULL) {
3309 err = got_error_from_errno("strdup");
3310 goto done;
3312 slash = strchr(path, '/');
3313 if (slash)
3314 *slash = '\0';
3316 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3317 if (err)
3318 goto done;
3320 err = got_object_id_by_path(&root_entry_id, repo,
3321 head_commit_id, path);
3322 if (err)
3323 goto done;
3325 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3326 while (pid) {
3327 struct got_commit_object *pcommit;
3329 err = got_object_id_by_path(&id, repo, pid->id, path);
3330 if (err) {
3331 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3332 goto done;
3333 err = NULL;
3334 break;
3337 err = got_object_id_by_path(&id, repo, pid->id, path);
3338 if (err)
3339 goto done;
3341 if (got_object_id_cmp(id, root_entry_id) != 0) {
3342 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3343 break;
3346 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3347 break; /* all relevant commits scanned */
3349 err = got_object_open_as_commit(&pcommit, repo,
3350 pid->id);
3351 if (err)
3352 goto done;
3354 got_object_commit_close(commit);
3355 commit = pcommit;
3356 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3357 commit));
3359 } else {
3360 /* Require that added files don't exist in the branch head. */
3361 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3362 ct_path);
3363 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3364 goto done;
3365 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3367 done:
3368 if (commit)
3369 got_object_commit_close(commit);
3370 free(id_in_head);
3371 free(id);
3372 free(path);
3373 return err;
3376 const struct got_error *
3377 commit_worktree(struct got_object_id **new_commit_id,
3378 struct got_pathlist_head *commitable_paths,
3379 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3380 const char *ondisk_path, const char *author, const char *committer,
3381 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3382 got_worktree_status_cb status_cb, void *status_arg,
3383 struct got_repository *repo)
3385 const struct got_error *err = NULL, *unlockerr = NULL;
3386 struct got_pathlist_entry *pe;
3387 const char *head_ref_name = NULL;
3388 struct got_commit_object *head_commit = NULL;
3389 struct got_reference *head_ref2 = NULL;
3390 struct got_object_id *head_commit_id2 = NULL;
3391 struct got_tree_object *head_tree = NULL;
3392 struct got_object_id *new_tree_id = NULL;
3393 struct got_object_id_queue parent_ids;
3394 struct got_object_qid *pid = NULL;
3395 char *logmsg = NULL;
3397 *new_commit_id = NULL;
3399 SIMPLEQ_INIT(&parent_ids);
3401 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3402 if (err)
3403 goto done;
3405 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3406 if (err)
3407 goto done;
3409 if (commit_msg_cb != NULL) {
3410 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3411 if (err)
3412 goto done;
3415 if (logmsg == NULL || strlen(logmsg) == 0) {
3416 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3417 goto done;
3420 /* Create blobs from added and modified files and record their IDs. */
3421 TAILQ_FOREACH(pe, commitable_paths, entry) {
3422 struct got_commitable *ct = pe->data;
3423 char *ondisk_path;
3425 if (ct->status != GOT_STATUS_ADD &&
3426 ct->status != GOT_STATUS_MODIFY)
3427 continue;
3429 if (asprintf(&ondisk_path, "%s/%s",
3430 worktree->root_path, pe->path) == -1) {
3431 err = got_error_from_errno("asprintf");
3432 goto done;
3434 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3435 free(ondisk_path);
3436 if (err)
3437 goto done;
3440 /* Recursively write new tree objects. */
3441 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3442 status_cb, status_arg, repo);
3443 if (err)
3444 goto done;
3446 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3447 if (err)
3448 goto done;
3449 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3450 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3451 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3452 got_object_qid_free(pid);
3453 if (logmsg != NULL)
3454 free(logmsg);
3455 if (err)
3456 goto done;
3458 /* Check if a concurrent commit to our branch has occurred. */
3459 head_ref_name = got_worktree_get_head_ref_name(worktree);
3460 if (head_ref_name == NULL) {
3461 err = got_error_from_errno("got_worktree_get_head_ref_name");
3462 goto done;
3464 /* Lock the reference here to prevent concurrent modification. */
3465 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3466 if (err)
3467 goto done;
3468 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3469 if (err)
3470 goto done;
3471 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3472 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3473 goto done;
3475 /* Update branch head in repository. */
3476 err = got_ref_change_ref(head_ref2, *new_commit_id);
3477 if (err)
3478 goto done;
3479 err = got_ref_write(head_ref2, repo);
3480 if (err)
3481 goto done;
3483 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3484 if (err)
3485 goto done;
3487 err = ref_base_commit(worktree, repo);
3488 if (err)
3489 goto done;
3490 done:
3491 if (head_tree)
3492 got_object_tree_close(head_tree);
3493 if (head_commit)
3494 got_object_commit_close(head_commit);
3495 free(head_commit_id2);
3496 if (head_ref2) {
3497 unlockerr = got_ref_unlock(head_ref2);
3498 if (unlockerr && err == NULL)
3499 err = unlockerr;
3500 got_ref_close(head_ref2);
3502 return err;
3505 const struct got_error *
3506 got_worktree_commit(struct got_object_id **new_commit_id,
3507 struct got_worktree *worktree, const char *ondisk_path,
3508 const char *author, const char *committer,
3509 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3510 got_worktree_status_cb status_cb, void *status_arg,
3511 struct got_repository *repo)
3513 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3514 struct got_fileindex *fileindex = NULL;
3515 char *fileindex_path = NULL, *relpath = NULL;
3516 struct got_pathlist_head commitable_paths;
3517 struct collect_commitables_arg cc_arg;
3518 struct got_pathlist_entry *pe;
3519 struct got_reference *head_ref = NULL;
3520 struct got_object_id *head_commit_id = NULL;
3522 *new_commit_id = NULL;
3524 TAILQ_INIT(&commitable_paths);
3526 err = lock_worktree(worktree, LOCK_EX);
3527 if (err)
3528 goto done;
3530 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3531 if (err)
3532 goto done;
3534 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3535 if (err)
3536 goto done;
3538 if (ondisk_path) {
3539 if (strcmp(ondisk_path, worktree->root_path) == 0) {
3540 relpath = strdup("");
3541 if (relpath == NULL) {
3542 err = got_error_from_errno("strdup");
3543 goto done;
3545 } else {
3546 err = got_path_skip_common_ancestor(&relpath,
3547 worktree->root_path, ondisk_path);
3548 if (err)
3549 return err;
3553 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3554 if (err)
3555 goto done;
3557 cc_arg.commitable_paths = &commitable_paths;
3558 cc_arg.worktree = worktree;
3559 cc_arg.repo = repo;
3560 err = worktree_status(worktree, relpath ? relpath : "",
3561 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3562 if (err)
3563 goto done;
3565 if (TAILQ_EMPTY(&commitable_paths)) {
3566 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3567 goto done;
3570 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3571 struct got_commitable *ct = pe->data;
3572 err = check_ct_out_of_date(ct, repo, head_commit_id);
3573 if (err)
3574 goto done;
3577 err = commit_worktree(new_commit_id, &commitable_paths,
3578 head_commit_id, worktree, ondisk_path, author, committer,
3579 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3580 if (err)
3581 goto done;
3583 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3584 fileindex);
3585 sync_err = sync_fileindex(fileindex, fileindex_path);
3586 if (sync_err && err == NULL)
3587 err = sync_err;
3588 done:
3589 if (fileindex)
3590 got_fileindex_free(fileindex);
3591 free(fileindex_path);
3592 free(relpath);
3593 unlockerr = lock_worktree(worktree, LOCK_SH);
3594 if (unlockerr && err == NULL)
3595 err = unlockerr;
3596 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3597 struct got_commitable *ct = pe->data;
3598 free_commitable(ct);
3600 got_pathlist_free(&commitable_paths);
3601 return err;
3604 const char *
3605 got_commitable_get_path(struct got_commitable *ct)
3607 return ct->path;
3610 unsigned int
3611 got_commitable_get_status(struct got_commitable *ct)
3613 return ct->status;
3616 struct check_rebase_ok_arg {
3617 struct got_worktree *worktree;
3618 struct got_repository *repo;
3621 static const struct got_error *
3622 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3624 const struct got_error *err = NULL;
3625 struct check_rebase_ok_arg *a = arg;
3626 unsigned char status;
3627 struct stat sb;
3628 char *ondisk_path;
3630 /* Reject rebase of a work tree with mixed base commits. */
3631 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3632 SHA1_DIGEST_LENGTH))
3633 return got_error(GOT_ERR_MIXED_COMMITS);
3635 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3636 == -1)
3637 return got_error_from_errno("asprintf");
3639 /* Reject rebase of a work tree with modified or conflicted files. */
3640 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3641 free(ondisk_path);
3642 if (err)
3643 return err;
3645 if (status != GOT_STATUS_NO_CHANGE)
3646 return got_error(GOT_ERR_MODIFIED);
3648 return NULL;
3651 const struct got_error *
3652 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3653 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3654 struct got_worktree *worktree, struct got_reference *branch,
3655 struct got_repository *repo)
3657 const struct got_error *err = NULL;
3658 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3659 char *branch_ref_name = NULL;
3660 char *fileindex_path = NULL;
3661 struct check_rebase_ok_arg ok_arg;
3662 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3664 *new_base_branch_ref = NULL;
3665 *tmp_branch = NULL;
3666 *fileindex = NULL;
3668 err = lock_worktree(worktree, LOCK_EX);
3669 if (err)
3670 return err;
3672 err = open_fileindex(fileindex, &fileindex_path, worktree);
3673 if (err)
3674 goto done;
3676 ok_arg.worktree = worktree;
3677 ok_arg.repo = repo;
3678 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3679 &ok_arg);
3680 if (err)
3681 goto done;
3683 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3684 if (err)
3685 goto done;
3687 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3688 if (err)
3689 goto done;
3691 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3692 if (err)
3693 goto done;
3695 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3696 0);
3697 if (err)
3698 goto done;
3700 err = got_ref_alloc_symref(new_base_branch_ref,
3701 new_base_branch_ref_name, wt_branch);
3702 if (err)
3703 goto done;
3704 err = got_ref_write(*new_base_branch_ref, repo);
3705 if (err)
3706 goto done;
3708 /* TODO Lock original branch's ref while rebasing? */
3710 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3711 if (err)
3712 goto done;
3714 err = got_ref_write(branch_ref, repo);
3715 if (err)
3716 goto done;
3718 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3719 worktree->base_commit_id);
3720 if (err)
3721 goto done;
3722 err = got_ref_write(*tmp_branch, repo);
3723 if (err)
3724 goto done;
3726 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3727 if (err)
3728 goto done;
3729 done:
3730 free(fileindex_path);
3731 free(tmp_branch_name);
3732 free(new_base_branch_ref_name);
3733 free(branch_ref_name);
3734 if (branch_ref)
3735 got_ref_close(branch_ref);
3736 if (wt_branch)
3737 got_ref_close(wt_branch);
3738 if (err) {
3739 if (*new_base_branch_ref) {
3740 got_ref_close(*new_base_branch_ref);
3741 *new_base_branch_ref = NULL;
3743 if (*tmp_branch) {
3744 got_ref_close(*tmp_branch);
3745 *tmp_branch = NULL;
3747 if (*fileindex) {
3748 got_fileindex_free(*fileindex);
3749 *fileindex = NULL;
3751 lock_worktree(worktree, LOCK_SH);
3753 return err;
3756 const struct got_error *
3757 got_worktree_rebase_continue(struct got_object_id **commit_id,
3758 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3759 struct got_reference **branch, struct got_fileindex **fileindex,
3760 struct got_worktree *worktree, struct got_repository *repo)
3762 const struct got_error *err;
3763 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3764 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3765 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3766 char *fileindex_path = NULL;
3768 *commit_id = NULL;
3769 *new_base_branch = NULL;
3770 *tmp_branch = NULL;
3771 *branch = NULL;
3772 *fileindex = NULL;
3774 err = lock_worktree(worktree, LOCK_EX);
3775 if (err)
3776 return err;
3778 err = open_fileindex(fileindex, &fileindex_path, worktree);
3779 if (err)
3780 goto done;
3782 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3783 if (err)
3784 return err;
3786 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3787 if (err)
3788 goto done;
3790 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3791 if (err)
3792 goto done;
3794 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3795 if (err)
3796 goto done;
3798 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3799 if (err)
3800 goto done;
3802 err = got_ref_open(branch, repo,
3803 got_ref_get_symref_target(branch_ref), 0);
3804 if (err)
3805 goto done;
3807 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3808 if (err)
3809 goto done;
3811 err = got_ref_resolve(commit_id, repo, commit_ref);
3812 if (err)
3813 goto done;
3815 err = got_ref_open(new_base_branch, repo,
3816 new_base_branch_ref_name, 0);
3817 if (err)
3818 goto done;
3820 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3821 if (err)
3822 goto done;
3823 done:
3824 free(commit_ref_name);
3825 free(branch_ref_name);
3826 free(fileindex_path);
3827 if (commit_ref)
3828 got_ref_close(commit_ref);
3829 if (branch_ref)
3830 got_ref_close(branch_ref);
3831 if (err) {
3832 free(*commit_id);
3833 *commit_id = NULL;
3834 if (*tmp_branch) {
3835 got_ref_close(*tmp_branch);
3836 *tmp_branch = NULL;
3838 if (*new_base_branch) {
3839 got_ref_close(*new_base_branch);
3840 *new_base_branch = NULL;
3842 if (*branch) {
3843 got_ref_close(*branch);
3844 *branch = NULL;
3846 if (*fileindex) {
3847 got_fileindex_free(*fileindex);
3848 *fileindex = NULL;
3850 lock_worktree(worktree, LOCK_SH);
3852 return err;
3855 const struct got_error *
3856 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3858 const struct got_error *err;
3859 char *tmp_branch_name = NULL;
3861 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3862 if (err)
3863 return err;
3865 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3866 free(tmp_branch_name);
3867 return NULL;
3870 static const struct got_error *
3871 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3872 char **logmsg, void *arg)
3874 *logmsg = arg;
3875 return NULL;
3878 static const struct got_error *
3879 rebase_status(void *arg, unsigned char status, const char *path,
3880 struct got_object_id *blob_id, struct got_object_id *commit_id)
3882 return NULL;
3885 struct collect_merged_paths_arg {
3886 got_worktree_checkout_cb progress_cb;
3887 void *progress_arg;
3888 struct got_pathlist_head *merged_paths;
3891 static const struct got_error *
3892 collect_merged_paths(void *arg, unsigned char status, const char *path)
3894 const struct got_error *err;
3895 struct collect_merged_paths_arg *a = arg;
3896 char *p;
3897 struct got_pathlist_entry *new;
3899 err = (*a->progress_cb)(a->progress_arg, status, path);
3900 if (err)
3901 return err;
3903 if (status != GOT_STATUS_MERGE &&
3904 status != GOT_STATUS_ADD &&
3905 status != GOT_STATUS_DELETE &&
3906 status != GOT_STATUS_CONFLICT)
3907 return NULL;
3909 p = strdup(path);
3910 if (p == NULL)
3911 return got_error_from_errno("strdup");
3913 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3914 if (err || new == NULL)
3915 free(p);
3916 return err;
3919 void
3920 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3922 struct got_pathlist_entry *pe;
3924 TAILQ_FOREACH(pe, merged_paths, entry)
3925 free((char *)pe->path);
3927 got_pathlist_free(merged_paths);
3930 static const struct got_error *
3931 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3932 struct got_repository *repo)
3934 const struct got_error *err;
3935 struct got_reference *commit_ref = NULL;
3937 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3938 if (err) {
3939 if (err->code != GOT_ERR_NOT_REF)
3940 goto done;
3941 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3942 if (err)
3943 goto done;
3944 err = got_ref_write(commit_ref, repo);
3945 if (err)
3946 goto done;
3947 } else {
3948 struct got_object_id *stored_id;
3949 int cmp;
3951 err = got_ref_resolve(&stored_id, repo, commit_ref);
3952 if (err)
3953 goto done;
3954 cmp = got_object_id_cmp(commit_id, stored_id);
3955 free(stored_id);
3956 if (cmp != 0) {
3957 err = got_error(GOT_ERR_REBASE_COMMITID);
3958 goto done;
3961 done:
3962 if (commit_ref)
3963 got_ref_close(commit_ref);
3964 return err;
3967 static const struct got_error *
3968 rebase_merge_files(struct got_pathlist_head *merged_paths,
3969 const char *commit_ref_name, struct got_worktree *worktree,
3970 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3971 struct got_object_id *commit_id, struct got_repository *repo,
3972 got_worktree_checkout_cb progress_cb, void *progress_arg,
3973 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3975 const struct got_error *err;
3976 struct got_reference *commit_ref = NULL;
3977 struct collect_merged_paths_arg cmp_arg;
3978 char *fileindex_path;
3980 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3982 err = get_fileindex_path(&fileindex_path, worktree);
3983 if (err)
3984 return err;
3986 cmp_arg.progress_cb = progress_cb;
3987 cmp_arg.progress_arg = progress_arg;
3988 cmp_arg.merged_paths = merged_paths;
3989 err = merge_files(worktree, fileindex, fileindex_path,
3990 parent_commit_id, commit_id, repo, collect_merged_paths,
3991 &cmp_arg, cancel_cb, cancel_arg);
3992 if (commit_ref)
3993 got_ref_close(commit_ref);
3994 return err;
3997 const struct got_error *
3998 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3999 struct got_worktree *worktree, struct got_fileindex *fileindex,
4000 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4001 struct got_repository *repo,
4002 got_worktree_checkout_cb progress_cb, void *progress_arg,
4003 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4005 const struct got_error *err;
4006 char *commit_ref_name;
4008 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4009 if (err)
4010 return err;
4012 err = store_commit_id(commit_ref_name, commit_id, repo);
4013 if (err)
4014 goto done;
4016 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4017 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4018 progress_arg, cancel_cb, cancel_arg);
4019 done:
4020 free(commit_ref_name);
4021 return err;
4024 const struct got_error *
4025 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4026 struct got_worktree *worktree, struct got_fileindex *fileindex,
4027 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4028 struct got_repository *repo,
4029 got_worktree_checkout_cb progress_cb, void *progress_arg,
4030 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4032 const struct got_error *err;
4033 char *commit_ref_name;
4035 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4036 if (err)
4037 return err;
4039 err = store_commit_id(commit_ref_name, commit_id, repo);
4040 if (err)
4041 goto done;
4043 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4044 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4045 progress_arg, cancel_cb, cancel_arg);
4046 done:
4047 free(commit_ref_name);
4048 return err;
4051 static const struct got_error *
4052 rebase_commit(struct got_object_id **new_commit_id,
4053 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4054 struct got_worktree *worktree, struct got_fileindex *fileindex,
4055 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4056 const char *new_logmsg, struct got_repository *repo)
4058 const struct got_error *err, *sync_err;
4059 struct got_pathlist_head commitable_paths;
4060 struct collect_commitables_arg cc_arg;
4061 char *fileindex_path = NULL;
4062 struct got_reference *head_ref = NULL;
4063 struct got_object_id *head_commit_id = NULL;
4064 char *logmsg = NULL;
4066 TAILQ_INIT(&commitable_paths);
4067 *new_commit_id = NULL;
4069 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4071 err = get_fileindex_path(&fileindex_path, worktree);
4072 if (err)
4073 return err;
4075 cc_arg.commitable_paths = &commitable_paths;
4076 cc_arg.worktree = worktree;
4077 cc_arg.repo = repo;
4079 * If possible get the status of individual files directly to
4080 * avoid crawling the entire work tree once per rebased commit.
4081 * TODO: Ideally, merged_paths would contain a list of commitables
4082 * we could use so we could skip worktree_status() entirely.
4084 if (merged_paths) {
4085 struct got_pathlist_entry *pe;
4086 if (TAILQ_EMPTY(merged_paths)) {
4087 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4088 goto done;
4090 TAILQ_FOREACH(pe, merged_paths, entry) {
4091 err = worktree_status(worktree, pe->path, fileindex,
4092 repo, collect_commitables, &cc_arg, NULL, NULL);
4093 if (err)
4094 goto done;
4096 } else {
4097 err = worktree_status(worktree, "", fileindex, repo,
4098 collect_commitables, &cc_arg, NULL, NULL);
4099 if (err)
4100 goto done;
4103 if (TAILQ_EMPTY(&commitable_paths)) {
4104 /* No-op change; commit will be elided. */
4105 err = got_ref_delete(commit_ref, repo);
4106 if (err)
4107 goto done;
4108 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4109 goto done;
4112 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4113 if (err)
4114 goto done;
4116 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4117 if (err)
4118 goto done;
4120 if (new_logmsg)
4121 logmsg = strdup(new_logmsg);
4122 else
4123 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4124 if (logmsg == NULL)
4125 return got_error_from_errno("strdup");
4127 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4128 worktree, NULL, got_object_commit_get_author(orig_commit),
4129 got_object_commit_get_committer(orig_commit),
4130 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4131 if (err)
4132 goto done;
4134 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4135 if (err)
4136 goto done;
4138 err = got_ref_delete(commit_ref, repo);
4139 if (err)
4140 goto done;
4142 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4143 fileindex);
4144 sync_err = sync_fileindex(fileindex, fileindex_path);
4145 if (sync_err && err == NULL)
4146 err = sync_err;
4147 done:
4148 free(fileindex_path);
4149 free(head_commit_id);
4150 if (head_ref)
4151 got_ref_close(head_ref);
4152 if (err) {
4153 free(*new_commit_id);
4154 *new_commit_id = NULL;
4156 return err;
4159 const struct got_error *
4160 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4161 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4162 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4163 struct got_commit_object *orig_commit,
4164 struct got_object_id *orig_commit_id, struct got_repository *repo)
4166 const struct got_error *err;
4167 char *commit_ref_name;
4168 struct got_reference *commit_ref = NULL;
4169 struct got_object_id *commit_id = NULL;
4171 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4172 if (err)
4173 return err;
4175 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4176 if (err)
4177 goto done;
4178 err = got_ref_resolve(&commit_id, repo, commit_ref);
4179 if (err)
4180 goto done;
4181 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4182 err = got_error(GOT_ERR_REBASE_COMMITID);
4183 goto done;
4186 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4187 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4188 done:
4189 if (commit_ref)
4190 got_ref_close(commit_ref);
4191 free(commit_ref_name);
4192 free(commit_id);
4193 return err;
4196 const struct got_error *
4197 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4198 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4199 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4200 struct got_commit_object *orig_commit,
4201 struct got_object_id *orig_commit_id, const char *new_logmsg,
4202 struct got_repository *repo)
4204 const struct got_error *err;
4205 char *commit_ref_name;
4206 struct got_reference *commit_ref = NULL;
4207 struct got_object_id *commit_id = NULL;
4209 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4210 if (err)
4211 return err;
4213 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4214 if (err)
4215 goto done;
4216 err = got_ref_resolve(&commit_id, repo, commit_ref);
4217 if (err)
4218 goto done;
4219 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4220 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4221 goto done;
4224 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4225 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4226 done:
4227 if (commit_ref)
4228 got_ref_close(commit_ref);
4229 free(commit_ref_name);
4230 free(commit_id);
4231 return err;
4234 const struct got_error *
4235 got_worktree_rebase_postpone(struct got_worktree *worktree,
4236 struct got_fileindex *fileindex)
4238 if (fileindex)
4239 got_fileindex_free(fileindex);
4240 return lock_worktree(worktree, LOCK_SH);
4243 static const struct got_error *
4244 delete_ref(const char *name, struct got_repository *repo)
4246 const struct got_error *err;
4247 struct got_reference *ref;
4249 err = got_ref_open(&ref, repo, name, 0);
4250 if (err) {
4251 if (err->code == GOT_ERR_NOT_REF)
4252 return NULL;
4253 return err;
4256 err = got_ref_delete(ref, repo);
4257 got_ref_close(ref);
4258 return err;
4261 static const struct got_error *
4262 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4264 const struct got_error *err;
4265 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4266 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4268 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4269 if (err)
4270 goto done;
4271 err = delete_ref(tmp_branch_name, repo);
4272 if (err)
4273 goto done;
4275 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4276 if (err)
4277 goto done;
4278 err = delete_ref(new_base_branch_ref_name, repo);
4279 if (err)
4280 goto done;
4282 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4283 if (err)
4284 goto done;
4285 err = delete_ref(branch_ref_name, repo);
4286 if (err)
4287 goto done;
4289 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4290 if (err)
4291 goto done;
4292 err = delete_ref(commit_ref_name, repo);
4293 if (err)
4294 goto done;
4296 done:
4297 free(tmp_branch_name);
4298 free(new_base_branch_ref_name);
4299 free(branch_ref_name);
4300 free(commit_ref_name);
4301 return err;
4304 const struct got_error *
4305 got_worktree_rebase_complete(struct got_worktree *worktree,
4306 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4307 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4308 struct got_repository *repo)
4310 const struct got_error *err, *unlockerr;
4311 struct got_object_id *new_head_commit_id = NULL;
4313 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4314 if (err)
4315 return err;
4317 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4318 if (err)
4319 goto done;
4321 err = got_ref_write(rebased_branch, repo);
4322 if (err)
4323 goto done;
4325 err = got_worktree_set_head_ref(worktree, rebased_branch);
4326 if (err)
4327 goto done;
4329 err = delete_rebase_refs(worktree, repo);
4330 done:
4331 if (fileindex)
4332 got_fileindex_free(fileindex);
4333 free(new_head_commit_id);
4334 unlockerr = lock_worktree(worktree, LOCK_SH);
4335 if (unlockerr && err == NULL)
4336 err = unlockerr;
4337 return err;
4340 struct collect_revertible_paths_arg {
4341 struct got_pathlist_head *revertible_paths;
4342 struct got_worktree *worktree;
4345 static const struct got_error *
4346 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4347 struct got_object_id *blob_id, struct got_object_id *commit_id)
4349 struct collect_revertible_paths_arg *a = arg;
4350 const struct got_error *err = NULL;
4351 struct got_pathlist_entry *new = NULL;
4352 char *path = NULL;
4354 if (status != GOT_STATUS_ADD &&
4355 status != GOT_STATUS_DELETE &&
4356 status != GOT_STATUS_MODIFY &&
4357 status != GOT_STATUS_CONFLICT &&
4358 status != GOT_STATUS_MISSING)
4359 return NULL;
4361 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4362 return got_error_from_errno("asprintf");
4364 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4365 if (err || new == NULL)
4366 free(path);
4367 return err;
4370 const struct got_error *
4371 got_worktree_rebase_abort(struct got_worktree *worktree,
4372 struct got_fileindex *fileindex, struct got_repository *repo,
4373 struct got_reference *new_base_branch,
4374 got_worktree_checkout_cb progress_cb, void *progress_arg)
4376 const struct got_error *err, *unlockerr, *sync_err;
4377 struct got_reference *resolved = NULL;
4378 struct got_object_id *commit_id = NULL;
4379 char *fileindex_path = NULL;
4380 struct got_pathlist_head revertible_paths;
4381 struct got_pathlist_entry *pe;
4382 struct collect_revertible_paths_arg crp_arg;
4383 struct got_object_id *tree_id = NULL;
4385 TAILQ_INIT(&revertible_paths);
4387 err = lock_worktree(worktree, LOCK_EX);
4388 if (err)
4389 return err;
4391 err = got_ref_open(&resolved, repo,
4392 got_ref_get_symref_target(new_base_branch), 0);
4393 if (err)
4394 goto done;
4396 err = got_worktree_set_head_ref(worktree, resolved);
4397 if (err)
4398 goto done;
4401 * XXX commits to the base branch could have happened while
4402 * we were busy rebasing; should we store the original commit ID
4403 * when rebase begins and read it back here?
4405 err = got_ref_resolve(&commit_id, repo, resolved);
4406 if (err)
4407 goto done;
4409 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4410 if (err)
4411 goto done;
4413 err = got_object_id_by_path(&tree_id, repo,
4414 worktree->base_commit_id, worktree->path_prefix);
4415 if (err)
4416 goto done;
4418 err = delete_rebase_refs(worktree, repo);
4419 if (err)
4420 goto done;
4422 err = get_fileindex_path(&fileindex_path, worktree);
4423 if (err)
4424 goto done;
4426 crp_arg.revertible_paths = &revertible_paths;
4427 crp_arg.worktree = worktree;
4428 err = worktree_status(worktree, "", fileindex, repo,
4429 collect_revertible_paths, &crp_arg, NULL, NULL);
4430 if (err)
4431 goto done;
4433 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4434 err = revert_file(worktree, fileindex, pe->path,
4435 progress_cb, progress_arg, repo);
4436 if (err)
4437 goto sync;
4440 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4441 repo, progress_cb, progress_arg, NULL, NULL);
4442 sync:
4443 sync_err = sync_fileindex(fileindex, fileindex_path);
4444 if (sync_err && err == NULL)
4445 err = sync_err;
4446 done:
4447 got_ref_close(resolved);
4448 free(tree_id);
4449 free(commit_id);
4450 if (fileindex)
4451 got_fileindex_free(fileindex);
4452 free(fileindex_path);
4453 TAILQ_FOREACH(pe, &revertible_paths, entry)
4454 free((char *)pe->path);
4455 got_pathlist_free(&revertible_paths);
4457 unlockerr = lock_worktree(worktree, LOCK_SH);
4458 if (unlockerr && err == NULL)
4459 err = unlockerr;
4460 return err;
4463 const struct got_error *
4464 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4465 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4466 struct got_fileindex **fileindex, struct got_worktree *worktree,
4467 struct got_repository *repo)
4469 const struct got_error *err = NULL;
4470 char *tmp_branch_name = NULL;
4471 char *branch_ref_name = NULL;
4472 char *base_commit_ref_name = NULL;
4473 char *fileindex_path = NULL;
4474 struct check_rebase_ok_arg ok_arg;
4475 struct got_reference *wt_branch = NULL;
4476 struct got_reference *base_commit_ref = NULL;
4478 *tmp_branch = NULL;
4479 *branch_ref = NULL;
4480 *base_commit_id = NULL;
4481 *fileindex = NULL;
4483 err = lock_worktree(worktree, LOCK_EX);
4484 if (err)
4485 return err;
4487 err = open_fileindex(fileindex, &fileindex_path, worktree);
4488 if (err)
4489 goto done;
4491 ok_arg.worktree = worktree;
4492 ok_arg.repo = repo;
4493 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4494 &ok_arg);
4495 if (err)
4496 goto done;
4498 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4499 if (err)
4500 goto done;
4502 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4503 if (err)
4504 goto done;
4506 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4507 worktree);
4508 if (err)
4509 goto done;
4511 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4512 0);
4513 if (err)
4514 goto done;
4516 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4517 if (err)
4518 goto done;
4520 err = got_ref_write(*branch_ref, repo);
4521 if (err)
4522 goto done;
4524 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4525 worktree->base_commit_id);
4526 if (err)
4527 goto done;
4528 err = got_ref_write(base_commit_ref, repo);
4529 if (err)
4530 goto done;
4531 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4532 if (*base_commit_id == NULL) {
4533 err = got_error_from_errno("got_object_id_dup");
4534 goto done;
4537 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4538 worktree->base_commit_id);
4539 if (err)
4540 goto done;
4541 err = got_ref_write(*tmp_branch, repo);
4542 if (err)
4543 goto done;
4545 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4546 if (err)
4547 goto done;
4548 done:
4549 free(fileindex_path);
4550 free(tmp_branch_name);
4551 free(branch_ref_name);
4552 free(base_commit_ref_name);
4553 if (wt_branch)
4554 got_ref_close(wt_branch);
4555 if (err) {
4556 if (*branch_ref) {
4557 got_ref_close(*branch_ref);
4558 *branch_ref = NULL;
4560 if (*tmp_branch) {
4561 got_ref_close(*tmp_branch);
4562 *tmp_branch = NULL;
4564 free(*base_commit_id);
4565 if (*fileindex) {
4566 got_fileindex_free(*fileindex);
4567 *fileindex = NULL;
4569 lock_worktree(worktree, LOCK_SH);
4571 return err;
4574 const struct got_error *
4575 got_worktree_histedit_postpone(struct got_worktree *worktree,
4576 struct got_fileindex *fileindex)
4578 if (fileindex)
4579 got_fileindex_free(fileindex);
4580 return lock_worktree(worktree, LOCK_SH);
4583 const struct got_error *
4584 got_worktree_histedit_in_progress(int *in_progress,
4585 struct got_worktree *worktree)
4587 const struct got_error *err;
4588 char *tmp_branch_name = NULL;
4590 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4591 if (err)
4592 return err;
4594 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4595 free(tmp_branch_name);
4596 return NULL;
4599 const struct got_error *
4600 got_worktree_histedit_continue(struct got_object_id **commit_id,
4601 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4602 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4603 struct got_worktree *worktree, struct got_repository *repo)
4605 const struct got_error *err;
4606 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4607 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4608 struct got_reference *commit_ref = NULL;
4609 struct got_reference *base_commit_ref = NULL;
4610 char *fileindex_path = NULL;
4612 *commit_id = NULL;
4613 *tmp_branch = NULL;
4614 *base_commit_id = NULL;
4615 *fileindex = NULL;
4617 err = lock_worktree(worktree, LOCK_EX);
4618 if (err)
4619 return err;
4621 err = open_fileindex(fileindex, &fileindex_path, worktree);
4622 if (err)
4623 goto done;
4625 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4626 if (err)
4627 return err;
4629 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4630 if (err)
4631 goto done;
4633 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4634 if (err)
4635 goto done;
4637 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4638 worktree);
4639 if (err)
4640 goto done;
4642 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4643 if (err)
4644 goto done;
4646 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4647 if (err)
4648 goto done;
4649 err = got_ref_resolve(commit_id, repo, commit_ref);
4650 if (err)
4651 goto done;
4653 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4654 if (err)
4655 goto done;
4656 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4657 if (err)
4658 goto done;
4660 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4661 if (err)
4662 goto done;
4663 done:
4664 free(commit_ref_name);
4665 free(branch_ref_name);
4666 free(fileindex_path);
4667 if (commit_ref)
4668 got_ref_close(commit_ref);
4669 if (base_commit_ref)
4670 got_ref_close(base_commit_ref);
4671 if (err) {
4672 free(*commit_id);
4673 *commit_id = NULL;
4674 free(*base_commit_id);
4675 *base_commit_id = NULL;
4676 if (*tmp_branch) {
4677 got_ref_close(*tmp_branch);
4678 *tmp_branch = NULL;
4680 if (*fileindex) {
4681 got_fileindex_free(*fileindex);
4682 *fileindex = NULL;
4684 lock_worktree(worktree, LOCK_EX);
4686 return err;
4689 static const struct got_error *
4690 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4692 const struct got_error *err;
4693 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4694 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4696 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4697 if (err)
4698 goto done;
4699 err = delete_ref(tmp_branch_name, repo);
4700 if (err)
4701 goto done;
4703 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4704 worktree);
4705 if (err)
4706 goto done;
4707 err = delete_ref(base_commit_ref_name, repo);
4708 if (err)
4709 goto done;
4711 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4712 if (err)
4713 goto done;
4714 err = delete_ref(branch_ref_name, repo);
4715 if (err)
4716 goto done;
4718 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4719 if (err)
4720 goto done;
4721 err = delete_ref(commit_ref_name, repo);
4722 if (err)
4723 goto done;
4724 done:
4725 free(tmp_branch_name);
4726 free(base_commit_ref_name);
4727 free(branch_ref_name);
4728 free(commit_ref_name);
4729 return err;
4732 const struct got_error *
4733 got_worktree_histedit_abort(struct got_worktree *worktree,
4734 struct got_fileindex *fileindex, struct got_repository *repo,
4735 struct got_reference *branch, struct got_object_id *base_commit_id,
4736 got_worktree_checkout_cb progress_cb, void *progress_arg)
4738 const struct got_error *err, *unlockerr, *sync_err;
4739 struct got_reference *resolved = NULL;
4740 char *fileindex_path = NULL;
4741 struct got_pathlist_head revertible_paths;
4742 struct got_pathlist_entry *pe;
4743 struct collect_revertible_paths_arg crp_arg;
4744 struct got_object_id *tree_id = NULL;
4746 TAILQ_INIT(&revertible_paths);
4748 err = lock_worktree(worktree, LOCK_EX);
4749 if (err)
4750 return err;
4752 err = got_ref_open(&resolved, repo,
4753 got_ref_get_symref_target(branch), 0);
4754 if (err)
4755 goto done;
4757 err = got_worktree_set_head_ref(worktree, resolved);
4758 if (err)
4759 goto done;
4761 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4762 if (err)
4763 goto done;
4765 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4766 worktree->path_prefix);
4767 if (err)
4768 goto done;
4770 err = delete_histedit_refs(worktree, repo);
4771 if (err)
4772 goto done;
4774 err = get_fileindex_path(&fileindex_path, worktree);
4775 if (err)
4776 goto done;
4778 crp_arg.revertible_paths = &revertible_paths;
4779 crp_arg.worktree = worktree;
4780 err = worktree_status(worktree, "", fileindex, repo,
4781 collect_revertible_paths, &crp_arg, NULL, NULL);
4782 if (err)
4783 goto done;
4785 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4786 err = revert_file(worktree, fileindex, pe->path,
4787 progress_cb, progress_arg, repo);
4788 if (err)
4789 goto sync;
4792 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4793 repo, progress_cb, progress_arg, NULL, NULL);
4794 sync:
4795 sync_err = sync_fileindex(fileindex, fileindex_path);
4796 if (sync_err && err == NULL)
4797 err = sync_err;
4798 done:
4799 got_ref_close(resolved);
4800 free(tree_id);
4801 free(fileindex_path);
4802 TAILQ_FOREACH(pe, &revertible_paths, entry)
4803 free((char *)pe->path);
4804 got_pathlist_free(&revertible_paths);
4806 unlockerr = lock_worktree(worktree, LOCK_SH);
4807 if (unlockerr && err == NULL)
4808 err = unlockerr;
4809 return err;
4812 const struct got_error *
4813 got_worktree_histedit_complete(struct got_worktree *worktree,
4814 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4815 struct got_reference *edited_branch, struct got_repository *repo)
4817 const struct got_error *err, *unlockerr;
4818 struct got_object_id *new_head_commit_id = NULL;
4819 struct got_reference *resolved = NULL;
4821 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4822 if (err)
4823 return err;
4825 err = got_ref_open(&resolved, repo,
4826 got_ref_get_symref_target(edited_branch), 0);
4827 if (err)
4828 goto done;
4830 err = got_ref_change_ref(resolved, new_head_commit_id);
4831 if (err)
4832 goto done;
4834 err = got_ref_write(resolved, repo);
4835 if (err)
4836 goto done;
4838 err = got_worktree_set_head_ref(worktree, resolved);
4839 if (err)
4840 goto done;
4842 err = delete_histedit_refs(worktree, repo);
4843 done:
4844 if (fileindex)
4845 got_fileindex_free(fileindex);
4846 free(new_head_commit_id);
4847 unlockerr = lock_worktree(worktree, LOCK_SH);
4848 if (unlockerr && err == NULL)
4849 err = unlockerr;
4850 return err;
4853 const struct got_error *
4854 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4855 struct got_object_id *commit_id, struct got_repository *repo)
4857 const struct got_error *err;
4858 char *commit_ref_name;
4860 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4861 if (err)
4862 return err;
4864 err = store_commit_id(commit_ref_name, commit_id, repo);
4865 if (err)
4866 goto done;
4868 err = delete_ref(commit_ref_name, repo);
4869 done:
4870 free(commit_ref_name);
4871 return err;