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 =
1059 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1060 & (S_IRWXU | S_IRWXG | S_IRWXO));
1061 } else
1062 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1063 return NULL;
1065 return got_error_from_errno2("lstat", abspath);
1068 if (!S_ISREG(sb->st_mode)) {
1069 *status = GOT_STATUS_OBSTRUCTED;
1070 return NULL;
1073 if (ie == NULL)
1074 return NULL;
1076 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1077 *status = GOT_STATUS_DELETE;
1078 return NULL;
1079 } else if (!got_fileindex_entry_has_blob(ie)) {
1080 *status = GOT_STATUS_ADD;
1081 return NULL;
1084 if (!stat_info_differs(ie, sb))
1085 return NULL;
1087 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1088 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1089 if (err)
1090 return err;
1092 f = fopen(abspath, "r");
1093 if (f == NULL) {
1094 err = got_error_from_errno2("fopen", abspath);
1095 goto done;
1097 hdrlen = got_object_blob_get_hdrlen(blob);
1098 for (;;) {
1099 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1100 err = got_object_blob_read_block(&blen, blob);
1101 if (err)
1102 goto done;
1103 /* Skip length of blob object header first time around. */
1104 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1105 if (flen == 0 && ferror(f)) {
1106 err = got_error_from_errno("fread");
1107 goto done;
1109 if (blen == 0) {
1110 if (flen != 0)
1111 *status = GOT_STATUS_MODIFY;
1112 break;
1113 } else if (flen == 0) {
1114 if (blen != 0)
1115 *status = GOT_STATUS_MODIFY;
1116 break;
1117 } else if (blen - hdrlen == flen) {
1118 /* Skip blob object header first time around. */
1119 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1120 *status = GOT_STATUS_MODIFY;
1121 break;
1123 } else {
1124 *status = GOT_STATUS_MODIFY;
1125 break;
1127 hdrlen = 0;
1130 if (*status == GOT_STATUS_MODIFY) {
1131 rewind(f);
1132 err = get_modified_file_content_status(status, f);
1134 done:
1135 if (blob)
1136 got_object_blob_close(blob);
1137 if (f)
1138 fclose(f);
1139 return err;
1143 * Update timestamps in the file index if a file is unmodified and
1144 * we had to run a full content comparison to find out.
1146 static const struct got_error *
1147 sync_timestamps(char *ondisk_path, unsigned char status,
1148 struct got_fileindex_entry *ie, struct stat *sb)
1150 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1151 return got_fileindex_entry_update(ie, ondisk_path,
1152 ie->blob_sha1, ie->commit_sha1, 1);
1154 return NULL;
1157 static const struct got_error *
1158 update_blob(struct got_worktree *worktree,
1159 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1160 struct got_tree_entry *te, const char *path,
1161 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1162 void *progress_arg)
1164 const struct got_error *err = NULL;
1165 struct got_blob_object *blob = NULL;
1166 char *ondisk_path;
1167 unsigned char status = GOT_STATUS_NO_CHANGE;
1168 struct stat sb;
1170 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1171 return got_error_from_errno("asprintf");
1173 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1174 if (err)
1175 goto done;
1177 if (status == GOT_STATUS_OBSTRUCTED) {
1178 err = (*progress_cb)(progress_arg, status, path);
1179 goto done;
1182 if (ie && status != GOT_STATUS_MISSING) {
1183 if (got_fileindex_entry_has_commit(ie) &&
1184 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1185 SHA1_DIGEST_LENGTH) == 0) {
1186 err = sync_timestamps(ondisk_path, status, ie, &sb);
1187 if (err)
1188 goto done;
1189 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1190 path);
1191 goto done;
1193 if (got_fileindex_entry_has_blob(ie) &&
1194 memcmp(ie->blob_sha1, te->id->sha1,
1195 SHA1_DIGEST_LENGTH) == 0) {
1196 err = sync_timestamps(ondisk_path, status, ie, &sb);
1197 goto done;
1201 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1202 if (err)
1203 goto done;
1205 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1206 int update_timestamps;
1207 struct got_blob_object *blob2 = NULL;
1208 if (got_fileindex_entry_has_blob(ie)) {
1209 struct got_object_id id2;
1210 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1211 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1212 if (err)
1213 goto done;
1215 err = merge_blob(&update_timestamps, worktree, blob2,
1216 ondisk_path, path, sb.st_mode, blob,
1217 worktree->base_commit_id, repo,
1218 progress_cb, progress_arg);
1219 if (blob2)
1220 got_object_blob_close(blob2);
1222 * Do not update timestamps of files with local changes.
1223 * Otherwise, a future status walk would treat them as
1224 * unmodified files again.
1226 err = got_fileindex_entry_update(ie, ondisk_path,
1227 blob->id.sha1, worktree->base_commit_id->sha1,
1228 update_timestamps);
1229 } else if (status == GOT_STATUS_DELETE) {
1230 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1231 if (err)
1232 goto done;
1233 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1234 ondisk_path, path, blob, 0);
1235 if (err)
1236 goto done;
1237 } else {
1238 err = install_blob(worktree, ondisk_path, path, te->mode,
1239 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1240 repo, progress_cb, progress_arg);
1241 if (err)
1242 goto done;
1243 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1244 ondisk_path, path, blob, 1);
1245 if (err)
1246 goto done;
1248 got_object_blob_close(blob);
1249 done:
1250 free(ondisk_path);
1251 return err;
1254 static const struct got_error *
1255 remove_ondisk_file(const char *root_path, const char *path)
1257 const struct got_error *err = NULL;
1258 char *ondisk_path = NULL;
1260 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1261 return got_error_from_errno("asprintf");
1263 if (unlink(ondisk_path) == -1) {
1264 if (errno != ENOENT)
1265 err = got_error_from_errno2("unlink", ondisk_path);
1266 } else {
1267 char *parent = dirname(ondisk_path);
1268 while (parent && strcmp(parent, root_path) != 0) {
1269 if (rmdir(parent) == -1) {
1270 if (errno != ENOTEMPTY)
1271 err = got_error_from_errno2("rmdir",
1272 parent);
1273 break;
1275 parent = dirname(parent);
1278 free(ondisk_path);
1279 return err;
1282 static const struct got_error *
1283 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1284 struct got_fileindex_entry *ie, struct got_repository *repo,
1285 got_worktree_checkout_cb progress_cb, void *progress_arg)
1287 const struct got_error *err = NULL;
1288 unsigned char status;
1289 struct stat sb;
1290 char *ondisk_path;
1292 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1293 == -1)
1294 return got_error_from_errno("asprintf");
1296 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1297 if (err)
1298 return err;
1300 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1301 status == GOT_STATUS_ADD) {
1302 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1303 if (err)
1304 return err;
1306 * Preserve the working file and change the deleted blob's
1307 * entry into a schedule-add entry.
1309 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1310 0);
1311 if (err)
1312 return err;
1313 } else {
1314 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1315 if (err)
1316 return err;
1317 if (status == GOT_STATUS_NO_CHANGE) {
1318 err = remove_ondisk_file(worktree->root_path, ie->path);
1319 if (err)
1320 return err;
1322 got_fileindex_entry_remove(fileindex, ie);
1325 return err;
1328 struct diff_cb_arg {
1329 struct got_fileindex *fileindex;
1330 struct got_worktree *worktree;
1331 struct got_repository *repo;
1332 got_worktree_checkout_cb progress_cb;
1333 void *progress_arg;
1334 got_worktree_cancel_cb cancel_cb;
1335 void *cancel_arg;
1338 static const struct got_error *
1339 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1340 struct got_tree_entry *te, const char *parent_path)
1342 struct diff_cb_arg *a = arg;
1344 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1345 return got_error(GOT_ERR_CANCELLED);
1347 return update_blob(a->worktree, a->fileindex, ie, te,
1348 ie->path, a->repo, a->progress_cb, a->progress_arg);
1351 static const struct got_error *
1352 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1354 struct diff_cb_arg *a = arg;
1356 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1357 return got_error(GOT_ERR_CANCELLED);
1359 return delete_blob(a->worktree, a->fileindex, ie,
1360 a->repo, a->progress_cb, a->progress_arg);
1363 static const struct got_error *
1364 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1366 struct diff_cb_arg *a = arg;
1367 const struct got_error *err;
1368 char *path;
1370 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1371 return got_error(GOT_ERR_CANCELLED);
1373 if (asprintf(&path, "%s%s%s", parent_path,
1374 parent_path[0] ? "/" : "", te->name)
1375 == -1)
1376 return got_error_from_errno("asprintf");
1378 if (S_ISDIR(te->mode))
1379 err = add_dir_on_disk(a->worktree, path);
1380 else
1381 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1382 a->repo, a->progress_cb, a->progress_arg);
1384 free(path);
1385 return err;
1388 static const struct got_error *
1389 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1391 const struct got_error *err = NULL;
1392 char *uuidstr = NULL;
1393 uint32_t uuid_status;
1395 *refname = NULL;
1397 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1398 if (uuid_status != uuid_s_ok)
1399 return got_error_uuid(uuid_status);
1401 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1402 == -1) {
1403 err = got_error_from_errno("asprintf");
1404 *refname = NULL;
1406 free(uuidstr);
1407 return err;
1410 const struct got_error *
1411 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1413 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1416 static const struct got_error *
1417 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1419 return get_ref_name(refname, worktree,
1420 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1423 static const struct got_error *
1424 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1426 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1429 static const struct got_error *
1430 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1432 return get_ref_name(refname, worktree,
1433 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1436 static const struct got_error *
1437 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1439 return get_ref_name(refname, worktree,
1440 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1443 static const struct got_error *
1444 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1446 return get_ref_name(refname, worktree,
1447 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1450 static const struct got_error *
1451 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1453 return get_ref_name(refname, worktree,
1454 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1457 static const struct got_error *
1458 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1460 return get_ref_name(refname, worktree,
1461 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1464 static const struct got_error *
1465 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1467 return get_ref_name(refname, worktree,
1468 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1471 const struct got_error *
1472 got_worktree_get_histedit_list_path(char **path, struct got_worktree *worktree)
1474 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1475 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_LIST) == -1) {
1476 *path = NULL;
1477 return got_error_from_errno("asprintf");
1479 return NULL;
1483 * Prevent Git's garbage collector from deleting our base commit by
1484 * setting a reference to our base commit's ID.
1486 static const struct got_error *
1487 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1489 const struct got_error *err = NULL;
1490 struct got_reference *ref = NULL;
1491 char *refname;
1493 err = got_worktree_get_base_ref_name(&refname, worktree);
1494 if (err)
1495 return err;
1497 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1498 if (err)
1499 goto done;
1501 err = got_ref_write(ref, repo);
1502 done:
1503 free(refname);
1504 if (ref)
1505 got_ref_close(ref);
1506 return err;
1509 static const struct got_error *
1510 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1511 struct got_worktree *worktree)
1513 const struct got_error *err = NULL;
1514 FILE *index = NULL;
1516 *fileindex_path = NULL;
1517 *fileindex = got_fileindex_alloc();
1518 if (*fileindex == NULL)
1519 return got_error_from_errno("got_fileindex_alloc");
1521 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1522 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1523 err = got_error_from_errno("asprintf");
1524 *fileindex_path = NULL;
1525 goto done;
1528 index = fopen(*fileindex_path, "rb");
1529 if (index == NULL) {
1530 if (errno != ENOENT)
1531 err = got_error_from_errno2("fopen", *fileindex_path);
1532 } else {
1533 err = got_fileindex_read(*fileindex, index);
1534 if (fclose(index) != 0 && err == NULL)
1535 err = got_error_from_errno("fclose");
1537 done:
1538 if (err) {
1539 free(*fileindex_path);
1540 *fileindex_path = NULL;
1541 got_fileindex_free(*fileindex);
1542 *fileindex = NULL;
1544 return err;
1547 struct bump_base_commit_id_arg {
1548 struct got_object_id *base_commit_id;
1549 const char *path;
1550 size_t path_len;
1551 const char *entry_name;
1552 got_worktree_checkout_cb progress_cb;
1553 void *progress_arg;
1556 /* Bump base commit ID of all files within an updated part of the work tree. */
1557 static const struct got_error *
1558 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1560 const struct got_error *err;
1561 struct bump_base_commit_id_arg *a = arg;
1563 if (a->entry_name) {
1564 if (strcmp(ie->path, a->path) != 0)
1565 return NULL;
1566 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1567 return NULL;
1569 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1570 SHA1_DIGEST_LENGTH) == 0)
1571 return NULL;
1573 if (a->progress_cb) {
1574 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1575 ie->path);
1576 if (err)
1577 return err;
1579 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1580 return NULL;
1583 static const struct got_error *
1584 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1586 const struct got_error *err = NULL;
1587 char *new_fileindex_path = NULL;
1588 FILE *new_index = NULL;
1590 err = got_opentemp_named(&new_fileindex_path, &new_index,
1591 fileindex_path);
1592 if (err)
1593 goto done;
1595 err = got_fileindex_write(fileindex, new_index);
1596 if (err)
1597 goto done;
1599 if (rename(new_fileindex_path, fileindex_path) != 0) {
1600 err = got_error_from_errno3("rename", new_fileindex_path,
1601 fileindex_path);
1602 unlink(new_fileindex_path);
1604 done:
1605 if (new_index)
1606 fclose(new_index);
1607 free(new_fileindex_path);
1608 return err;
1611 static const struct got_error *
1612 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1613 struct got_object_id **tree_id, const char *wt_relpath,
1614 struct got_worktree *worktree, struct got_repository *repo)
1616 const struct got_error *err = NULL;
1617 struct got_object_id *id = NULL;
1618 char *in_repo_path = NULL;
1619 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1621 *entry_type = GOT_OBJ_TYPE_ANY;
1622 *tree_relpath = NULL;
1623 *tree_id = NULL;
1625 if (wt_relpath[0] == '\0') {
1626 /* Check out all files within the work tree. */
1627 *entry_type = GOT_OBJ_TYPE_TREE;
1628 *tree_relpath = strdup("");
1629 if (*tree_relpath == NULL) {
1630 err = got_error_from_errno("strdup");
1631 goto done;
1633 err = got_object_id_by_path(tree_id, repo,
1634 worktree->base_commit_id, worktree->path_prefix);
1635 if (err)
1636 goto done;
1637 return NULL;
1640 /* Check out a subset of files in the work tree. */
1642 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1643 is_root_wt ? "" : "/", wt_relpath) == -1) {
1644 err = got_error_from_errno("asprintf");
1645 goto done;
1648 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1649 in_repo_path);
1650 if (err)
1651 goto done;
1653 free(in_repo_path);
1654 in_repo_path = NULL;
1656 err = got_object_get_type(entry_type, repo, id);
1657 if (err)
1658 goto done;
1660 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1661 /* Check out a single file. */
1662 if (strchr(wt_relpath, '/') == NULL) {
1663 /* Check out a single file in work tree's root dir. */
1664 in_repo_path = strdup(worktree->path_prefix);
1665 if (in_repo_path == NULL) {
1666 err = got_error_from_errno("strdup");
1667 goto done;
1669 *tree_relpath = strdup("");
1670 if (*tree_relpath == NULL) {
1671 err = got_error_from_errno("strdup");
1672 goto done;
1674 } else {
1675 /* Check out a single file in a subdirectory. */
1676 err = got_path_dirname(tree_relpath, wt_relpath);
1677 if (err)
1678 return err;
1679 if (asprintf(&in_repo_path, "%s%s%s",
1680 worktree->path_prefix, is_root_wt ? "" : "/",
1681 *tree_relpath) == -1) {
1682 err = got_error_from_errno("asprintf");
1683 goto done;
1686 err = got_object_id_by_path(tree_id, repo,
1687 worktree->base_commit_id, in_repo_path);
1688 } else {
1689 /* Check out all files within a subdirectory. */
1690 *tree_id = got_object_id_dup(id);
1691 if (*tree_id == NULL) {
1692 err = got_error_from_errno("got_object_id_dup");
1693 goto done;
1695 *tree_relpath = strdup(wt_relpath);
1696 if (*tree_relpath == NULL) {
1697 err = got_error_from_errno("strdup");
1698 goto done;
1701 done:
1702 free(id);
1703 free(in_repo_path);
1704 if (err) {
1705 *entry_type = GOT_OBJ_TYPE_ANY;
1706 free(*tree_relpath);
1707 *tree_relpath = NULL;
1708 free(*tree_id);
1709 *tree_id = NULL;
1711 return err;
1714 static const struct got_error *
1715 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1716 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1717 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1718 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1720 const struct got_error *err = NULL;
1721 struct got_commit_object *commit = NULL;
1722 struct got_tree_object *tree = NULL;
1723 struct got_fileindex_diff_tree_cb diff_cb;
1724 struct diff_cb_arg arg;
1726 err = ref_base_commit(worktree, repo);
1727 if (err)
1728 goto done;
1730 err = got_object_open_as_commit(&commit, repo,
1731 worktree->base_commit_id);
1732 if (err)
1733 goto done;
1735 err = got_object_open_as_tree(&tree, repo, tree_id);
1736 if (err)
1737 goto done;
1739 if (entry_name &&
1740 got_object_tree_find_entry(tree, entry_name) == NULL) {
1741 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1742 goto done;
1745 diff_cb.diff_old_new = diff_old_new;
1746 diff_cb.diff_old = diff_old;
1747 diff_cb.diff_new = diff_new;
1748 arg.fileindex = fileindex;
1749 arg.worktree = worktree;
1750 arg.repo = repo;
1751 arg.progress_cb = progress_cb;
1752 arg.progress_arg = progress_arg;
1753 arg.cancel_cb = cancel_cb;
1754 arg.cancel_arg = cancel_arg;
1755 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1756 entry_name, repo, &diff_cb, &arg);
1757 done:
1758 if (tree)
1759 got_object_tree_close(tree);
1760 if (commit)
1761 got_object_commit_close(commit);
1762 return err;
1765 const struct got_error *
1766 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1767 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1768 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1770 const struct got_error *err = NULL, *sync_err, *unlockerr;
1771 struct got_commit_object *commit = NULL;
1772 struct got_object_id *tree_id = NULL;
1773 struct got_tree_object *tree = NULL;
1774 struct got_fileindex *fileindex = NULL;
1775 char *fileindex_path = NULL;
1776 char *relpath = NULL, *entry_name = NULL;
1777 int entry_type;
1779 err = lock_worktree(worktree, LOCK_EX);
1780 if (err)
1781 return err;
1783 err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
1784 path, worktree, repo);
1785 if (err)
1786 goto done;
1788 if (entry_type == GOT_OBJ_TYPE_BLOB) {
1789 entry_name = basename(path);
1790 if (entry_name == NULL) {
1791 err = got_error_from_errno2("basename", path);
1792 goto done;
1797 * Read the file index.
1798 * Checking out files is supposed to be an idempotent operation.
1799 * If the on-disk file index is incomplete we will try to complete it.
1801 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1802 if (err)
1803 goto done;
1805 err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
1806 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1807 if (err == NULL) {
1808 struct bump_base_commit_id_arg bbc_arg;
1809 bbc_arg.base_commit_id = worktree->base_commit_id;
1810 bbc_arg.entry_name = entry_name;
1811 bbc_arg.path = path;
1812 bbc_arg.path_len = strlen(path);
1813 bbc_arg.progress_cb = progress_cb;
1814 bbc_arg.progress_arg = progress_arg;
1815 err = got_fileindex_for_each_entry_safe(fileindex,
1816 bump_base_commit_id, &bbc_arg);
1818 sync_err = sync_fileindex(fileindex, fileindex_path);
1819 if (sync_err && err == NULL)
1820 err = sync_err;
1821 done:
1822 free(fileindex_path);
1823 free(relpath);
1824 if (tree)
1825 got_object_tree_close(tree);
1826 if (commit)
1827 got_object_commit_close(commit);
1828 if (fileindex)
1829 got_fileindex_free(fileindex);
1830 unlockerr = lock_worktree(worktree, LOCK_SH);
1831 if (unlockerr && err == NULL)
1832 err = unlockerr;
1833 return err;
1836 struct merge_file_cb_arg {
1837 struct got_worktree *worktree;
1838 struct got_fileindex *fileindex;
1839 got_worktree_checkout_cb progress_cb;
1840 void *progress_arg;
1841 got_worktree_cancel_cb cancel_cb;
1842 void *cancel_arg;
1843 struct got_object_id *commit_id2;
1846 static const struct got_error *
1847 merge_file_cb(void *arg, struct got_blob_object *blob1,
1848 struct got_blob_object *blob2, struct got_object_id *id1,
1849 struct got_object_id *id2, const char *path1, const char *path2,
1850 struct got_repository *repo)
1852 static const struct got_error *err = NULL;
1853 struct merge_file_cb_arg *a = arg;
1854 struct got_fileindex_entry *ie;
1855 char *ondisk_path = NULL;
1856 struct stat sb;
1857 unsigned char status;
1858 int local_changes_subsumed;
1860 if (blob1 && blob2) {
1861 ie = got_fileindex_entry_get(a->fileindex, path2);
1862 if (ie == NULL)
1863 return (*a->progress_cb)(a->progress_arg,
1864 GOT_STATUS_MISSING, path2);
1866 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1867 path2) == -1)
1868 return got_error_from_errno("asprintf");
1870 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1871 if (err)
1872 goto done;
1874 if (status == GOT_STATUS_DELETE) {
1875 err = (*a->progress_cb)(a->progress_arg,
1876 GOT_STATUS_MERGE, path2);
1877 goto done;
1879 if (status != GOT_STATUS_NO_CHANGE &&
1880 status != GOT_STATUS_MODIFY &&
1881 status != GOT_STATUS_CONFLICT &&
1882 status != GOT_STATUS_ADD) {
1883 err = (*a->progress_cb)(a->progress_arg, status, path2);
1884 goto done;
1887 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1888 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1889 a->progress_cb, a->progress_arg);
1890 } else if (blob1) {
1891 ie = got_fileindex_entry_get(a->fileindex, path1);
1892 if (ie == NULL)
1893 return (*a->progress_cb)(a->progress_arg,
1894 GOT_STATUS_MISSING, path2);
1896 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1897 path1) == -1)
1898 return got_error_from_errno("asprintf");
1900 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1901 if (err)
1902 goto done;
1904 switch (status) {
1905 case GOT_STATUS_NO_CHANGE:
1906 err = (*a->progress_cb)(a->progress_arg,
1907 GOT_STATUS_DELETE, path1);
1908 if (err)
1909 goto done;
1910 err = remove_ondisk_file(a->worktree->root_path, path1);
1911 if (err)
1912 goto done;
1913 if (ie)
1914 got_fileindex_entry_mark_deleted_from_disk(ie);
1915 break;
1916 case GOT_STATUS_DELETE:
1917 case GOT_STATUS_MISSING:
1918 err = (*a->progress_cb)(a->progress_arg,
1919 GOT_STATUS_DELETE, path1);
1920 if (err)
1921 goto done;
1922 if (ie)
1923 got_fileindex_entry_mark_deleted_from_disk(ie);
1924 break;
1925 case GOT_STATUS_ADD:
1926 case GOT_STATUS_MODIFY:
1927 case GOT_STATUS_CONFLICT:
1928 err = (*a->progress_cb)(a->progress_arg,
1929 GOT_STATUS_CANNOT_DELETE, path1);
1930 if (err)
1931 goto done;
1932 break;
1933 case GOT_STATUS_OBSTRUCTED:
1934 err = (*a->progress_cb)(a->progress_arg, status, path1);
1935 if (err)
1936 goto done;
1937 break;
1938 default:
1939 break;
1941 } else if (blob2) {
1942 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1943 path2) == -1)
1944 return got_error_from_errno("asprintf");
1945 ie = got_fileindex_entry_get(a->fileindex, path2);
1946 if (ie) {
1947 err = get_file_status(&status, &sb, ie, ondisk_path,
1948 repo);
1949 if (err)
1950 goto done;
1951 if (status != GOT_STATUS_NO_CHANGE &&
1952 status != GOT_STATUS_MODIFY &&
1953 status != GOT_STATUS_CONFLICT &&
1954 status != GOT_STATUS_ADD) {
1955 err = (*a->progress_cb)(a->progress_arg,
1956 status, path2);
1957 goto done;
1959 err = merge_blob(&local_changes_subsumed, a->worktree,
1960 NULL, ondisk_path, path2, sb.st_mode, blob2,
1961 a->commit_id2, repo,
1962 a->progress_cb, a->progress_arg);
1963 if (status == GOT_STATUS_DELETE) {
1964 err = update_blob_fileindex_entry(a->worktree,
1965 a->fileindex, ie, ondisk_path, ie->path,
1966 blob2, 0);
1967 if (err)
1968 goto done;
1970 } else {
1971 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1972 err = install_blob(a->worktree, ondisk_path, path2,
1973 /* XXX get this from parent tree! */
1974 GOT_DEFAULT_FILE_MODE,
1975 sb.st_mode, blob2, 0, 0, repo,
1976 a->progress_cb, a->progress_arg);
1977 if (err)
1978 goto done;
1979 err = got_fileindex_entry_alloc(&ie,
1980 ondisk_path, path2, NULL, NULL);
1981 if (err)
1982 goto done;
1983 err = got_fileindex_entry_add(a->fileindex, ie);
1984 if (err) {
1985 got_fileindex_entry_free(ie);
1986 goto done;
1990 done:
1991 free(ondisk_path);
1992 return err;
1995 struct check_merge_ok_arg {
1996 struct got_worktree *worktree;
1997 struct got_repository *repo;
2000 static const struct got_error *
2001 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2003 const struct got_error *err = NULL;
2004 struct check_merge_ok_arg *a = arg;
2005 unsigned char status;
2006 struct stat sb;
2007 char *ondisk_path;
2009 /* Reject merges into a work tree with mixed base commits. */
2010 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2011 SHA1_DIGEST_LENGTH))
2012 return got_error(GOT_ERR_MIXED_COMMITS);
2014 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2015 == -1)
2016 return got_error_from_errno("asprintf");
2018 /* Reject merges into a work tree with conflicted files. */
2019 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2020 if (err)
2021 return err;
2022 if (status == GOT_STATUS_CONFLICT)
2023 return got_error(GOT_ERR_CONFLICTS);
2025 return NULL;
2028 static const struct got_error *
2029 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2030 const char *fileindex_path, struct got_object_id *commit_id1,
2031 struct got_object_id *commit_id2, struct got_repository *repo,
2032 got_worktree_checkout_cb progress_cb, void *progress_arg,
2033 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2035 const struct got_error *err = NULL, *sync_err;
2036 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2037 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2038 struct merge_file_cb_arg arg;
2040 if (commit_id1) {
2041 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2042 worktree->path_prefix);
2043 if (err)
2044 goto done;
2046 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2047 if (err)
2048 goto done;
2051 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2052 worktree->path_prefix);
2053 if (err)
2054 goto done;
2056 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2057 if (err)
2058 goto done;
2060 arg.worktree = worktree;
2061 arg.fileindex = fileindex;
2062 arg.progress_cb = progress_cb;
2063 arg.progress_arg = progress_arg;
2064 arg.cancel_cb = cancel_cb;
2065 arg.cancel_arg = cancel_arg;
2066 arg.commit_id2 = commit_id2;
2067 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2068 sync_err = sync_fileindex(fileindex, fileindex_path);
2069 if (sync_err && err == NULL)
2070 err = sync_err;
2071 done:
2072 if (tree1)
2073 got_object_tree_close(tree1);
2074 if (tree2)
2075 got_object_tree_close(tree2);
2076 return err;
2079 const struct got_error *
2080 got_worktree_merge_files(struct got_worktree *worktree,
2081 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2082 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2083 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2085 const struct got_error *err, *unlockerr;
2086 char *fileindex_path = NULL;
2087 struct got_fileindex *fileindex = NULL;
2088 struct check_merge_ok_arg mok_arg;
2090 err = lock_worktree(worktree, LOCK_EX);
2091 if (err)
2092 return err;
2094 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2095 if (err)
2096 goto done;
2098 mok_arg.worktree = worktree;
2099 mok_arg.repo = repo;
2100 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2101 &mok_arg);
2102 if (err)
2103 goto done;
2105 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2106 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2107 done:
2108 if (fileindex)
2109 got_fileindex_free(fileindex);
2110 free(fileindex_path);
2111 unlockerr = lock_worktree(worktree, LOCK_SH);
2112 if (unlockerr && err == NULL)
2113 err = unlockerr;
2114 return err;
2117 struct diff_dir_cb_arg {
2118 struct got_fileindex *fileindex;
2119 struct got_worktree *worktree;
2120 const char *status_path;
2121 size_t status_path_len;
2122 struct got_repository *repo;
2123 got_worktree_status_cb status_cb;
2124 void *status_arg;
2125 got_worktree_cancel_cb cancel_cb;
2126 void *cancel_arg;
2129 static const struct got_error *
2130 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2131 got_worktree_status_cb status_cb, void *status_arg,
2132 struct got_repository *repo)
2134 const struct got_error *err = NULL;
2135 unsigned char status = GOT_STATUS_NO_CHANGE;
2136 struct stat sb;
2137 struct got_object_id blob_id, commit_id;
2139 err = get_file_status(&status, &sb, ie, abspath, repo);
2140 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2141 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2142 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2143 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2144 &commit_id);
2146 return err;
2149 static const struct got_error *
2150 status_old_new(void *arg, struct got_fileindex_entry *ie,
2151 struct dirent *de, const char *parent_path)
2153 const struct got_error *err = NULL;
2154 struct diff_dir_cb_arg *a = arg;
2155 char *abspath;
2157 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2158 return got_error(GOT_ERR_CANCELLED);
2160 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2161 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2162 return NULL;
2164 if (parent_path[0]) {
2165 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2166 parent_path, de->d_name) == -1)
2167 return got_error_from_errno("asprintf");
2168 } else {
2169 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2170 de->d_name) == -1)
2171 return got_error_from_errno("asprintf");
2174 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2175 a->repo);
2176 free(abspath);
2177 return err;
2180 static const struct got_error *
2181 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2183 struct diff_dir_cb_arg *a = arg;
2184 struct got_object_id blob_id, commit_id;
2185 unsigned char status;
2187 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2188 return got_error(GOT_ERR_CANCELLED);
2190 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2191 return NULL;
2193 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2194 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2195 if (got_fileindex_entry_has_file_on_disk(ie))
2196 status = GOT_STATUS_MISSING;
2197 else
2198 status = GOT_STATUS_DELETE;
2199 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2200 &commit_id);
2203 static const struct got_error *
2204 status_new(void *arg, struct dirent *de, const char *parent_path)
2206 const struct got_error *err = NULL;
2207 struct diff_dir_cb_arg *a = arg;
2208 char *path = NULL;
2210 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 return got_error(GOT_ERR_CANCELLED);
2213 if (de->d_type == DT_DIR)
2214 return NULL;
2216 /* XXX ignore symlinks for now */
2217 if (de->d_type == DT_LNK)
2218 return NULL;
2220 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2221 return NULL;
2223 if (parent_path[0]) {
2224 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2225 return got_error_from_errno("asprintf");
2226 } else {
2227 path = de->d_name;
2230 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2231 NULL, NULL);
2232 if (parent_path[0])
2233 free(path);
2234 return err;
2237 static const struct got_error *
2238 worktree_status(struct got_worktree *worktree, const char *path,
2239 struct got_fileindex *fileindex, struct got_repository *repo,
2240 got_worktree_status_cb status_cb, void *status_arg,
2241 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2243 const struct got_error *err = NULL;
2244 DIR *workdir = NULL;
2245 struct got_fileindex_diff_dir_cb fdiff_cb;
2246 struct diff_dir_cb_arg arg;
2247 char *ondisk_path = NULL;
2249 if (asprintf(&ondisk_path, "%s%s%s",
2250 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2251 err = got_error_from_errno("asprintf");
2252 goto done;
2254 workdir = opendir(ondisk_path);
2255 if (workdir == NULL) {
2256 if (errno == ENOTDIR || errno == ENOENT) {
2257 struct got_fileindex_entry *ie;
2258 ie = got_fileindex_entry_get(fileindex, path);
2259 if (ie == NULL) {
2260 err = got_error(GOT_ERR_BAD_PATH);
2261 goto done;
2263 err = report_file_status(ie, ondisk_path,
2264 status_cb, status_arg, repo);
2265 goto done;
2266 } else {
2267 err = got_error_from_errno2("opendir", ondisk_path);
2268 goto done;
2271 fdiff_cb.diff_old_new = status_old_new;
2272 fdiff_cb.diff_old = status_old;
2273 fdiff_cb.diff_new = status_new;
2274 arg.fileindex = fileindex;
2275 arg.worktree = worktree;
2276 arg.status_path = path;
2277 arg.status_path_len = strlen(path);
2278 arg.repo = repo;
2279 arg.status_cb = status_cb;
2280 arg.status_arg = status_arg;
2281 arg.cancel_cb = cancel_cb;
2282 arg.cancel_arg = cancel_arg;
2283 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2284 path, repo, &fdiff_cb, &arg);
2285 done:
2286 if (workdir)
2287 closedir(workdir);
2288 free(ondisk_path);
2289 return err;
2292 const struct got_error *
2293 got_worktree_status(struct got_worktree *worktree, const char *path,
2294 struct got_repository *repo, got_worktree_status_cb status_cb,
2295 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2297 const struct got_error *err = NULL;
2298 char *fileindex_path = NULL;
2299 struct got_fileindex *fileindex = NULL;
2301 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2302 if (err)
2303 return err;
2305 err = worktree_status(worktree, path, fileindex, repo,
2306 status_cb, status_arg, cancel_cb, cancel_arg);
2307 free(fileindex_path);
2308 got_fileindex_free(fileindex);
2309 return err;
2312 const struct got_error *
2313 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2314 const char *arg)
2316 const struct got_error *err = NULL;
2317 char *resolved, *cwd = NULL, *path = NULL;
2318 size_t len;
2320 *wt_path = NULL;
2322 resolved = realpath(arg, NULL);
2323 if (resolved == NULL) {
2324 if (errno != ENOENT)
2325 return got_error_from_errno2("realpath", arg);
2326 cwd = getcwd(NULL, 0);
2327 if (cwd == NULL)
2328 return got_error_from_errno("getcwd");
2329 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2330 err = got_error_from_errno("asprintf");
2331 goto done;
2335 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2336 strlen(got_worktree_get_root_path(worktree)))) {
2337 err = got_error(GOT_ERR_BAD_PATH);
2338 goto done;
2341 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2342 err = got_path_skip_common_ancestor(&path,
2343 got_worktree_get_root_path(worktree), resolved);
2344 if (err)
2345 goto done;
2346 } else {
2347 path = strdup("");
2348 if (path == NULL) {
2349 err = got_error_from_errno("strdup");
2350 goto done;
2354 /* XXX status walk can't deal with trailing slash! */
2355 len = strlen(path);
2356 while (path[len - 1] == '/') {
2357 path[len - 1] = '\0';
2358 len--;
2360 done:
2361 free(resolved);
2362 free(cwd);
2363 if (err == NULL)
2364 *wt_path = path;
2365 else
2366 free(path);
2367 return err;
2370 static const struct got_error *
2371 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2372 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2373 struct got_repository *repo)
2375 const struct got_error *err = NULL;
2376 struct got_fileindex_entry *ie;
2378 /* Re-adding an existing entry is a no-op. */
2379 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2380 return NULL;
2382 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2383 if (err)
2384 return err;
2386 err = got_fileindex_entry_add(fileindex, ie);
2387 if (err) {
2388 got_fileindex_entry_free(ie);
2389 return err;
2392 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2395 const struct got_error *
2396 got_worktree_schedule_add(struct got_worktree *worktree,
2397 struct got_pathlist_head *ondisk_paths,
2398 got_worktree_status_cb status_cb, void *status_arg,
2399 struct got_repository *repo)
2401 struct got_fileindex *fileindex = NULL;
2402 char *fileindex_path = NULL;
2403 const struct got_error *err = NULL, *sync_err, *unlockerr;
2404 struct got_pathlist_entry *pe;
2406 err = lock_worktree(worktree, LOCK_EX);
2407 if (err)
2408 return err;
2410 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2411 if (err)
2412 goto done;
2414 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2415 char *relpath;
2416 err = got_path_skip_common_ancestor(&relpath,
2417 got_worktree_get_root_path(worktree), pe->path);
2418 if (err)
2419 break;
2420 err = schedule_addition(pe->path, fileindex, relpath,
2421 status_cb, status_arg, repo);
2422 free(relpath);
2423 if (err)
2424 break;
2426 sync_err = sync_fileindex(fileindex, fileindex_path);
2427 if (sync_err && err == NULL)
2428 err = sync_err;
2429 done:
2430 free(fileindex_path);
2431 if (fileindex)
2432 got_fileindex_free(fileindex);
2433 unlockerr = lock_worktree(worktree, LOCK_SH);
2434 if (unlockerr && err == NULL)
2435 err = unlockerr;
2436 return err;
2439 static const struct got_error *
2440 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2441 const char *relpath, int delete_local_mods,
2442 got_worktree_status_cb status_cb, void *status_arg,
2443 struct got_repository *repo)
2445 const struct got_error *err = NULL;
2446 struct got_fileindex_entry *ie = NULL;
2447 unsigned char status;
2448 struct stat sb;
2450 ie = got_fileindex_entry_get(fileindex, relpath);
2451 if (ie == NULL)
2452 return got_error(GOT_ERR_BAD_PATH);
2454 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2455 if (err)
2456 return err;
2458 if (status != GOT_STATUS_NO_CHANGE) {
2459 if (status == GOT_STATUS_DELETE)
2460 return got_error_set_errno(ENOENT, ondisk_path);
2461 if (status != GOT_STATUS_MODIFY)
2462 return got_error(GOT_ERR_FILE_STATUS);
2463 if (!delete_local_mods)
2464 return got_error(GOT_ERR_FILE_MODIFIED);
2467 if (unlink(ondisk_path) != 0)
2468 return got_error_from_errno2("unlink", ondisk_path);
2470 got_fileindex_entry_mark_deleted_from_disk(ie);
2471 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2474 const struct got_error *
2475 got_worktree_schedule_delete(struct got_worktree *worktree,
2476 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2477 got_worktree_status_cb status_cb, void *status_arg,
2478 struct got_repository *repo)
2480 struct got_fileindex *fileindex = NULL;
2481 char *fileindex_path = NULL;
2482 const struct got_error *err = NULL, *sync_err, *unlockerr;
2483 struct got_pathlist_entry *pe;
2485 err = lock_worktree(worktree, LOCK_EX);
2486 if (err)
2487 return err;
2489 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2490 if (err)
2491 goto done;
2493 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2494 char *relpath;
2495 err = got_path_skip_common_ancestor(&relpath,
2496 got_worktree_get_root_path(worktree), pe->path);
2497 if (err)
2498 break;
2499 err = schedule_for_deletion(pe->path, fileindex, relpath,
2500 delete_local_mods, status_cb, status_arg, repo);
2501 free(relpath);
2502 if (err)
2503 break;
2505 sync_err = sync_fileindex(fileindex, fileindex_path);
2506 if (sync_err && err == NULL)
2507 err = sync_err;
2508 done:
2509 free(fileindex_path);
2510 if (fileindex)
2511 got_fileindex_free(fileindex);
2512 unlockerr = lock_worktree(worktree, LOCK_SH);
2513 if (unlockerr && err == NULL)
2514 err = unlockerr;
2515 return err;
2518 static const struct got_error *
2519 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2520 const char *ondisk_path,
2521 got_worktree_checkout_cb progress_cb, void *progress_arg,
2522 struct got_repository *repo)
2524 const struct got_error *err = NULL;
2525 char *relpath = NULL, *parent_path = NULL;
2526 struct got_fileindex_entry *ie;
2527 struct got_tree_object *tree = NULL;
2528 struct got_object_id *tree_id = NULL;
2529 const struct got_tree_entry *te;
2530 char *tree_path = NULL, *te_name;
2531 struct got_blob_object *blob = NULL;
2532 unsigned char status;
2533 struct stat sb;
2535 err = got_path_skip_common_ancestor(&relpath,
2536 got_worktree_get_root_path(worktree), ondisk_path);
2537 if (err)
2538 goto done;
2540 ie = got_fileindex_entry_get(fileindex, relpath);
2541 if (ie == NULL) {
2542 err = got_error(GOT_ERR_BAD_PATH);
2543 goto done;
2546 /* Construct in-repository path of tree which contains this blob. */
2547 err = got_path_dirname(&parent_path, ie->path);
2548 if (err) {
2549 if (err->code != GOT_ERR_BAD_PATH)
2550 goto done;
2551 parent_path = strdup("/");
2552 if (parent_path == NULL) {
2553 err = got_error_from_errno("strdup");
2554 goto done;
2557 if (got_path_is_root_dir(worktree->path_prefix)) {
2558 tree_path = strdup(parent_path);
2559 if (tree_path == NULL) {
2560 err = got_error_from_errno("strdup");
2561 goto done;
2563 } else {
2564 if (got_path_is_root_dir(parent_path)) {
2565 tree_path = strdup(worktree->path_prefix);
2566 if (tree_path == NULL) {
2567 err = got_error_from_errno("strdup");
2568 goto done;
2570 } else {
2571 if (asprintf(&tree_path, "%s/%s",
2572 worktree->path_prefix, parent_path) == -1) {
2573 err = got_error_from_errno("asprintf");
2574 goto done;
2579 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2580 tree_path);
2581 if (err)
2582 goto done;
2584 err = got_object_open_as_tree(&tree, repo, tree_id);
2585 if (err)
2586 goto done;
2588 te_name = basename(ie->path);
2589 if (te_name == NULL) {
2590 err = got_error_from_errno2("basename", ie->path);
2591 goto done;
2594 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2595 if (err)
2596 goto done;
2598 te = got_object_tree_find_entry(tree, te_name);
2599 if (te == NULL && status != GOT_STATUS_ADD) {
2600 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2601 goto done;
2604 switch (status) {
2605 case GOT_STATUS_ADD:
2606 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2607 if (err)
2608 goto done;
2609 got_fileindex_entry_remove(fileindex, ie);
2610 break;
2611 case GOT_STATUS_DELETE:
2612 case GOT_STATUS_MODIFY:
2613 case GOT_STATUS_CONFLICT:
2614 case GOT_STATUS_MISSING: {
2615 struct got_object_id id;
2616 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2617 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2618 if (err)
2619 goto done;
2620 err = install_blob(worktree, ondisk_path, ie->path,
2621 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2622 progress_arg);
2623 if (err)
2624 goto done;
2625 if (status == GOT_STATUS_DELETE) {
2626 err = update_blob_fileindex_entry(worktree,
2627 fileindex, ie, ondisk_path, ie->path, blob, 1);
2628 if (err)
2629 goto done;
2631 break;
2633 default:
2634 goto done;
2636 done:
2637 free(relpath);
2638 free(parent_path);
2639 free(tree_path);
2640 if (blob)
2641 got_object_blob_close(blob);
2642 if (tree)
2643 got_object_tree_close(tree);
2644 free(tree_id);
2645 return err;
2648 const struct got_error *
2649 got_worktree_revert(struct got_worktree *worktree,
2650 struct got_pathlist_head *ondisk_paths,
2651 got_worktree_checkout_cb progress_cb, void *progress_arg,
2652 struct got_repository *repo)
2654 struct got_fileindex *fileindex = NULL;
2655 char *fileindex_path = NULL;
2656 const struct got_error *err = NULL, *unlockerr = NULL;
2657 const struct got_error *sync_err = NULL;
2658 struct got_pathlist_entry *pe;
2660 err = lock_worktree(worktree, LOCK_EX);
2661 if (err)
2662 return err;
2664 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2665 if (err)
2666 goto done;
2668 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2669 err = revert_file(worktree, fileindex, pe->path,
2670 progress_cb, progress_arg, repo);
2671 if (err)
2672 break;
2674 sync_err = sync_fileindex(fileindex, fileindex_path);
2675 if (sync_err && err == NULL)
2676 err = sync_err;
2677 done:
2678 free(fileindex_path);
2679 if (fileindex)
2680 got_fileindex_free(fileindex);
2681 unlockerr = lock_worktree(worktree, LOCK_SH);
2682 if (unlockerr && err == NULL)
2683 err = unlockerr;
2684 return err;
2687 static void
2688 free_commitable(struct got_commitable *ct)
2690 free(ct->path);
2691 free(ct->in_repo_path);
2692 free(ct->ondisk_path);
2693 free(ct->blob_id);
2694 free(ct->base_blob_id);
2695 free(ct->base_commit_id);
2696 free(ct);
2699 struct collect_commitables_arg {
2700 struct got_pathlist_head *commitable_paths;
2701 struct got_repository *repo;
2702 struct got_worktree *worktree;
2705 static const struct got_error *
2706 collect_commitables(void *arg, unsigned char status, const char *relpath,
2707 struct got_object_id *blob_id, struct got_object_id *commit_id)
2709 struct collect_commitables_arg *a = arg;
2710 const struct got_error *err = NULL;
2711 struct got_commitable *ct = NULL;
2712 struct got_pathlist_entry *new = NULL;
2713 char *parent_path = NULL, *path = NULL;
2714 struct stat sb;
2716 if (status == GOT_STATUS_CONFLICT)
2717 return got_error(GOT_ERR_COMMIT_CONFLICT);
2719 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2720 status != GOT_STATUS_DELETE)
2721 return NULL;
2723 if (asprintf(&path, "/%s", relpath) == -1) {
2724 err = got_error_from_errno("asprintf");
2725 goto done;
2727 if (strcmp(path, "/") == 0) {
2728 parent_path = strdup("");
2729 if (parent_path == NULL)
2730 return got_error_from_errno("strdup");
2731 } else {
2732 err = got_path_dirname(&parent_path, path);
2733 if (err)
2734 return err;
2737 ct = calloc(1, sizeof(*ct));
2738 if (ct == NULL) {
2739 err = got_error_from_errno("calloc");
2740 goto done;
2743 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2744 relpath) == -1) {
2745 err = got_error_from_errno("asprintf");
2746 goto done;
2748 if (status == GOT_STATUS_DELETE) {
2749 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2750 } else {
2751 if (lstat(ct->ondisk_path, &sb) != 0) {
2752 err = got_error_from_errno2("lstat", ct->ondisk_path);
2753 goto done;
2755 ct->mode = sb.st_mode;
2758 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2759 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2760 relpath) == -1) {
2761 err = got_error_from_errno("asprintf");
2762 goto done;
2765 ct->status = status;
2766 ct->blob_id = NULL; /* will be filled in when blob gets created */
2767 if (ct->status != GOT_STATUS_ADD) {
2768 ct->base_blob_id = got_object_id_dup(blob_id);
2769 if (ct->base_blob_id == NULL) {
2770 err = got_error_from_errno("got_object_id_dup");
2771 goto done;
2773 ct->base_commit_id = got_object_id_dup(commit_id);
2774 if (ct->base_commit_id == NULL) {
2775 err = got_error_from_errno("got_object_id_dup");
2776 goto done;
2779 ct->path = strdup(path);
2780 if (ct->path == NULL) {
2781 err = got_error_from_errno("strdup");
2782 goto done;
2784 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2785 done:
2786 if (ct && (err || new == NULL))
2787 free_commitable(ct);
2788 free(parent_path);
2789 free(path);
2790 return err;
2793 static const struct got_error *write_tree(struct got_object_id **,
2794 struct got_tree_object *, const char *, struct got_pathlist_head *,
2795 got_worktree_status_cb status_cb, void *status_arg,
2796 struct got_repository *);
2798 static const struct got_error *
2799 write_subtree(struct got_object_id **new_subtree_id,
2800 struct got_tree_entry *te, const char *parent_path,
2801 struct got_pathlist_head *commitable_paths,
2802 got_worktree_status_cb status_cb, void *status_arg,
2803 struct got_repository *repo)
2805 const struct got_error *err = NULL;
2806 struct got_tree_object *subtree;
2807 char *subpath;
2809 if (asprintf(&subpath, "%s%s%s", parent_path,
2810 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2811 return got_error_from_errno("asprintf");
2813 err = got_object_open_as_tree(&subtree, repo, te->id);
2814 if (err)
2815 return err;
2817 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2818 status_cb, status_arg, repo);
2819 got_object_tree_close(subtree);
2820 free(subpath);
2821 return err;
2824 static const struct got_error *
2825 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2827 const struct got_error *err = NULL;
2828 char *ct_parent_path = NULL;
2830 *match = 0;
2832 if (strchr(ct->path, '/') == NULL) {
2833 *match = got_path_is_root_dir(path);
2834 return NULL;
2837 err = got_path_dirname(&ct_parent_path, ct->path);
2838 if (err)
2839 return err;
2840 *match = (strcmp(path, ct_parent_path) == 0);
2841 free(ct_parent_path);
2842 return err;
2845 static mode_t
2846 get_ct_file_mode(struct got_commitable *ct)
2848 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2851 static const struct got_error *
2852 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2853 struct got_tree_entry *te, struct got_commitable *ct)
2855 const struct got_error *err = NULL;
2857 *new_te = NULL;
2859 err = got_object_tree_entry_dup(new_te, te);
2860 if (err)
2861 goto done;
2863 (*new_te)->mode = get_ct_file_mode(ct);
2865 free((*new_te)->id);
2866 (*new_te)->id = got_object_id_dup(ct->blob_id);
2867 if ((*new_te)->id == NULL) {
2868 err = got_error_from_errno("got_object_id_dup");
2869 goto done;
2871 done:
2872 if (err && *new_te) {
2873 got_object_tree_entry_close(*new_te);
2874 *new_te = NULL;
2876 return err;
2879 static const struct got_error *
2880 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2881 struct got_commitable *ct)
2883 const struct got_error *err = NULL;
2884 char *ct_name;
2886 *new_te = NULL;
2888 *new_te = calloc(1, sizeof(**new_te));
2889 if (*new_te == NULL)
2890 return got_error_from_errno("calloc");
2892 ct_name = basename(ct->path);
2893 if (ct_name == NULL) {
2894 err = got_error_from_errno2("basename", ct->path);
2895 goto done;
2897 (*new_te)->name = strdup(ct_name);
2898 if ((*new_te)->name == NULL) {
2899 err = got_error_from_errno("strdup");
2900 goto done;
2903 (*new_te)->mode = get_ct_file_mode(ct);
2905 (*new_te)->id = got_object_id_dup(ct->blob_id);
2906 if ((*new_te)->id == NULL) {
2907 err = got_error_from_errno("got_object_id_dup");
2908 goto done;
2910 done:
2911 if (err && *new_te) {
2912 got_object_tree_entry_close(*new_te);
2913 *new_te = NULL;
2915 return err;
2918 static const struct got_error *
2919 insert_tree_entry(struct got_tree_entry *new_te,
2920 struct got_pathlist_head *paths)
2922 const struct got_error *err = NULL;
2923 struct got_pathlist_entry *new_pe;
2925 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2926 if (err)
2927 return err;
2928 if (new_pe == NULL)
2929 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2930 return NULL;
2933 static const struct got_error *
2934 report_ct_status(struct got_commitable *ct,
2935 got_worktree_status_cb status_cb, void *status_arg)
2937 const char *ct_path = ct->path;
2938 while (ct_path[0] == '/')
2939 ct_path++;
2940 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2943 static const struct got_error *
2944 match_modified_subtree(int *modified, struct got_tree_entry *te,
2945 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2947 const struct got_error *err = NULL;
2948 struct got_pathlist_entry *pe;
2949 char *te_path;
2951 *modified = 0;
2953 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2954 got_path_is_root_dir(base_tree_path) ? "" : "/",
2955 te->name) == -1)
2956 return got_error_from_errno("asprintf");
2958 TAILQ_FOREACH(pe, commitable_paths, entry) {
2959 struct got_commitable *ct = pe->data;
2960 *modified = got_path_is_child(ct->in_repo_path, te_path,
2961 strlen(te_path));
2962 if (*modified)
2963 break;
2966 free(te_path);
2967 return err;
2970 static const struct got_error *
2971 match_deleted_or_modified_ct(struct got_commitable **ctp,
2972 struct got_tree_entry *te, const char *base_tree_path,
2973 struct got_pathlist_head *commitable_paths)
2975 const struct got_error *err = NULL;
2976 struct got_pathlist_entry *pe;
2978 *ctp = NULL;
2980 TAILQ_FOREACH(pe, commitable_paths, entry) {
2981 struct got_commitable *ct = pe->data;
2982 char *ct_name = NULL;
2983 int path_matches;
2985 if (ct->status != GOT_STATUS_MODIFY &&
2986 ct->status != GOT_STATUS_DELETE)
2987 continue;
2989 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2990 continue;
2992 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2993 if (err)
2994 return err;
2995 if (!path_matches)
2996 continue;
2998 ct_name = basename(pe->path);
2999 if (ct_name == NULL)
3000 return got_error_from_errno2("basename", pe->path);
3002 if (strcmp(te->name, ct_name) != 0)
3003 continue;
3005 *ctp = ct;
3006 break;
3009 return err;
3012 static const struct got_error *
3013 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3014 const char *child_path, const char *path_base_tree,
3015 struct got_pathlist_head *commitable_paths,
3016 got_worktree_status_cb status_cb, void *status_arg,
3017 struct got_repository *repo)
3019 const struct got_error *err = NULL;
3020 struct got_tree_entry *new_te;
3021 char *subtree_path;
3023 *new_tep = NULL;
3025 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3026 got_path_is_root_dir(path_base_tree) ? "" : "/",
3027 child_path) == -1)
3028 return got_error_from_errno("asprintf");
3030 new_te = calloc(1, sizeof(*new_te));
3031 new_te->mode = S_IFDIR;
3032 new_te->name = strdup(child_path);
3033 if (new_te->name == NULL) {
3034 err = got_error_from_errno("strdup");
3035 got_object_tree_entry_close(new_te);
3036 goto done;
3038 err = write_tree(&new_te->id, NULL, subtree_path,
3039 commitable_paths, status_cb, status_arg, repo);
3040 if (err) {
3041 got_object_tree_entry_close(new_te);
3042 goto done;
3044 done:
3045 free(subtree_path);
3046 if (err == NULL)
3047 *new_tep = new_te;
3048 return err;
3051 static const struct got_error *
3052 write_tree(struct got_object_id **new_tree_id,
3053 struct got_tree_object *base_tree, const char *path_base_tree,
3054 struct got_pathlist_head *commitable_paths,
3055 got_worktree_status_cb status_cb, void *status_arg,
3056 struct got_repository *repo)
3058 const struct got_error *err = NULL;
3059 const struct got_tree_entries *base_entries = NULL;
3060 struct got_pathlist_head paths;
3061 struct got_tree_entries new_tree_entries;
3062 struct got_tree_entry *te, *new_te = NULL;
3063 struct got_pathlist_entry *pe;
3065 TAILQ_INIT(&paths);
3066 new_tree_entries.nentries = 0;
3067 SIMPLEQ_INIT(&new_tree_entries.head);
3069 /* Insert, and recurse into, newly added entries first. */
3070 TAILQ_FOREACH(pe, commitable_paths, entry) {
3071 struct got_commitable *ct = pe->data;
3072 char *child_path = NULL, *slash;
3074 if (ct->status != GOT_STATUS_ADD ||
3075 (ct->flags & GOT_COMMITABLE_ADDED))
3076 continue;
3078 if (!got_path_is_child(pe->path, path_base_tree,
3079 strlen(path_base_tree)))
3080 continue;
3082 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3083 pe->path);
3084 if (err)
3085 goto done;
3087 slash = strchr(child_path, '/');
3088 if (slash == NULL) {
3089 err = alloc_added_blob_tree_entry(&new_te, ct);
3090 if (err)
3091 goto done;
3092 err = report_ct_status(ct, status_cb, status_arg);
3093 if (err)
3094 goto done;
3095 ct->flags |= GOT_COMMITABLE_ADDED;
3096 err = insert_tree_entry(new_te, &paths);
3097 if (err)
3098 goto done;
3099 } else {
3100 *slash = '\0'; /* trim trailing path components */
3101 if (base_tree == NULL ||
3102 got_object_tree_find_entry(base_tree, child_path)
3103 == NULL) {
3104 err = make_subtree_for_added_blob(&new_te,
3105 child_path, path_base_tree,
3106 commitable_paths, status_cb, status_arg,
3107 repo);
3108 if (err)
3109 goto done;
3110 err = insert_tree_entry(new_te, &paths);
3111 if (err)
3112 goto done;
3117 if (base_tree) {
3118 /* Handle modified and deleted entries. */
3119 base_entries = got_object_tree_get_entries(base_tree);
3120 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3121 struct got_commitable *ct = NULL;
3123 if (S_ISDIR(te->mode)) {
3124 int modified;
3125 err = got_object_tree_entry_dup(&new_te, te);
3126 if (err)
3127 goto done;
3128 err = match_modified_subtree(&modified, te,
3129 path_base_tree, commitable_paths);
3130 if (err)
3131 goto done;
3132 /* Avoid recursion into unmodified subtrees. */
3133 if (modified) {
3134 free(new_te->id);
3135 err = write_subtree(&new_te->id, te,
3136 path_base_tree, commitable_paths,
3137 status_cb, status_arg, repo);
3138 if (err)
3139 goto done;
3141 err = insert_tree_entry(new_te, &paths);
3142 if (err)
3143 goto done;
3144 continue;
3147 err = match_deleted_or_modified_ct(&ct, te,
3148 path_base_tree, commitable_paths);
3149 if (ct) {
3150 /* NB: Deleted entries get dropped here. */
3151 if (ct->status == GOT_STATUS_MODIFY) {
3152 err = alloc_modified_blob_tree_entry(
3153 &new_te, te, ct);
3154 if (err)
3155 goto done;
3156 err = insert_tree_entry(new_te, &paths);
3157 if (err)
3158 goto done;
3160 err = report_ct_status(ct, status_cb,
3161 status_arg);
3162 if (err)
3163 goto done;
3164 } else {
3165 /* Entry is unchanged; just copy it. */
3166 err = got_object_tree_entry_dup(&new_te, te);
3167 if (err)
3168 goto done;
3169 err = insert_tree_entry(new_te, &paths);
3170 if (err)
3171 goto done;
3176 /* Write new list of entries; deleted entries have been dropped. */
3177 TAILQ_FOREACH(pe, &paths, entry) {
3178 struct got_tree_entry *te = pe->data;
3179 new_tree_entries.nentries++;
3180 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3182 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3183 done:
3184 got_object_tree_entries_close(&new_tree_entries);
3185 got_pathlist_free(&paths);
3186 return err;
3189 static const struct got_error *
3190 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3191 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3193 const struct got_error *err = NULL;
3194 struct got_pathlist_entry *pe;
3196 TAILQ_FOREACH(pe, commitable_paths, entry) {
3197 struct got_fileindex_entry *ie;
3198 struct got_commitable *ct = pe->data;
3200 ie = got_fileindex_entry_get(fileindex, pe->path);
3201 if (ie) {
3202 if (ct->status == GOT_STATUS_DELETE) {
3203 got_fileindex_entry_remove(fileindex, ie);
3204 got_fileindex_entry_free(ie);
3205 } else
3206 err = got_fileindex_entry_update(ie,
3207 ct->ondisk_path, ct->blob_id->sha1,
3208 new_base_commit_id->sha1, 1);
3209 } else {
3210 err = got_fileindex_entry_alloc(&ie,
3211 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3212 new_base_commit_id->sha1);
3213 if (err)
3214 break;
3215 err = got_fileindex_entry_add(fileindex, ie);
3216 if (err)
3217 break;
3220 return err;
3223 static const struct got_error *
3224 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3225 struct got_object_id *head_commit_id)
3227 const struct got_error *err = NULL;
3228 struct got_object_id *id_in_head = NULL, *id = NULL;
3229 struct got_commit_object *commit = NULL;
3230 char *path = NULL;
3231 const char *ct_path = ct->in_repo_path;
3233 while (ct_path[0] == '/')
3234 ct_path++;
3237 * Ensure that no modifications were made to files *and their parents*
3238 * in commits between the file's base commit and the branch head.
3240 * Checking the parents is important for detecting conflicting tree
3241 * configurations (files or parent folders might have been moved,
3242 * deleted, added again, etc.). Such changes need to be merged with
3243 * local changes before a commit can occur.
3245 * The implication is that the file's (parent) entry in the root
3246 * directory must have the same ID in all relevant commits.
3248 if (ct->status != GOT_STATUS_ADD) {
3249 struct got_object_qid *pid;
3250 char *slash;
3251 struct got_object_id *root_entry_id = NULL;
3253 /* Trivial case: base commit == head commit */
3254 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3255 return NULL;
3257 /* Compute the path to the root directory's entry. */
3258 path = strdup(ct_path);
3259 if (path == NULL) {
3260 err = got_error_from_errno("strdup");
3261 goto done;
3263 slash = strchr(path, '/');
3264 if (slash)
3265 *slash = '\0';
3267 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3268 if (err)
3269 goto done;
3271 err = got_object_id_by_path(&root_entry_id, repo,
3272 head_commit_id, path);
3273 if (err)
3274 goto done;
3276 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3277 while (pid) {
3278 struct got_commit_object *pcommit;
3280 err = got_object_id_by_path(&id, repo, pid->id, path);
3281 if (err) {
3282 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3283 goto done;
3284 err = NULL;
3285 break;
3288 err = got_object_id_by_path(&id, repo, pid->id, path);
3289 if (err)
3290 goto done;
3292 if (got_object_id_cmp(id, root_entry_id) != 0) {
3293 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3294 break;
3297 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3298 break; /* all relevant commits scanned */
3300 err = got_object_open_as_commit(&pcommit, repo,
3301 pid->id);
3302 if (err)
3303 goto done;
3305 got_object_commit_close(commit);
3306 commit = pcommit;
3307 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3308 commit));
3310 } else {
3311 /* Require that added files don't exist in the branch head. */
3312 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3313 ct_path);
3314 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3315 goto done;
3316 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3318 done:
3319 if (commit)
3320 got_object_commit_close(commit);
3321 free(id_in_head);
3322 free(id);
3323 free(path);
3324 return err;
3327 const struct got_error *
3328 commit_worktree(struct got_object_id **new_commit_id,
3329 struct got_pathlist_head *commitable_paths,
3330 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3331 const char *ondisk_path, const char *author, const char *committer,
3332 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3333 got_worktree_status_cb status_cb, void *status_arg,
3334 struct got_repository *repo)
3336 const struct got_error *err = NULL, *unlockerr = NULL;
3337 struct got_pathlist_entry *pe;
3338 const char *head_ref_name = NULL;
3339 struct got_commit_object *head_commit = NULL;
3340 struct got_reference *head_ref2 = NULL;
3341 struct got_object_id *head_commit_id2 = NULL;
3342 struct got_tree_object *head_tree = NULL;
3343 struct got_object_id *new_tree_id = NULL;
3344 struct got_object_id_queue parent_ids;
3345 struct got_object_qid *pid = NULL;
3346 char *logmsg = NULL;
3348 *new_commit_id = NULL;
3350 SIMPLEQ_INIT(&parent_ids);
3352 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3353 if (err)
3354 goto done;
3356 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3357 if (err)
3358 goto done;
3360 if (commit_msg_cb != NULL) {
3361 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3362 if (err)
3363 goto done;
3366 if (logmsg == NULL || strlen(logmsg) == 0) {
3367 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3368 goto done;
3371 /* Create blobs from added and modified files and record their IDs. */
3372 TAILQ_FOREACH(pe, commitable_paths, entry) {
3373 struct got_commitable *ct = pe->data;
3374 char *ondisk_path;
3376 if (ct->status != GOT_STATUS_ADD &&
3377 ct->status != GOT_STATUS_MODIFY)
3378 continue;
3380 if (asprintf(&ondisk_path, "%s/%s",
3381 worktree->root_path, pe->path) == -1) {
3382 err = got_error_from_errno("asprintf");
3383 goto done;
3385 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3386 free(ondisk_path);
3387 if (err)
3388 goto done;
3391 /* Recursively write new tree objects. */
3392 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3393 status_cb, status_arg, repo);
3394 if (err)
3395 goto done;
3397 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3398 if (err)
3399 goto done;
3400 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3401 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3402 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3403 got_object_qid_free(pid);
3404 if (logmsg != NULL)
3405 free(logmsg);
3406 if (err)
3407 goto done;
3409 /* Check if a concurrent commit to our branch has occurred. */
3410 head_ref_name = got_worktree_get_head_ref_name(worktree);
3411 if (head_ref_name == NULL) {
3412 err = got_error_from_errno("got_worktree_get_head_ref_name");
3413 goto done;
3415 /* Lock the reference here to prevent concurrent modification. */
3416 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3417 if (err)
3418 goto done;
3419 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3420 if (err)
3421 goto done;
3422 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3423 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3424 goto done;
3426 /* Update branch head in repository. */
3427 err = got_ref_change_ref(head_ref2, *new_commit_id);
3428 if (err)
3429 goto done;
3430 err = got_ref_write(head_ref2, repo);
3431 if (err)
3432 goto done;
3434 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3435 if (err)
3436 goto done;
3438 err = ref_base_commit(worktree, repo);
3439 if (err)
3440 goto done;
3441 done:
3442 if (head_tree)
3443 got_object_tree_close(head_tree);
3444 if (head_commit)
3445 got_object_commit_close(head_commit);
3446 free(head_commit_id2);
3447 if (head_ref2) {
3448 unlockerr = got_ref_unlock(head_ref2);
3449 if (unlockerr && err == NULL)
3450 err = unlockerr;
3451 got_ref_close(head_ref2);
3453 return err;
3456 const struct got_error *
3457 got_worktree_commit(struct got_object_id **new_commit_id,
3458 struct got_worktree *worktree, const char *ondisk_path,
3459 const char *author, const char *committer,
3460 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3461 got_worktree_status_cb status_cb, void *status_arg,
3462 struct got_repository *repo)
3464 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3465 struct got_fileindex *fileindex = NULL;
3466 char *fileindex_path = NULL, *relpath = NULL;
3467 struct got_pathlist_head commitable_paths;
3468 struct collect_commitables_arg cc_arg;
3469 struct got_pathlist_entry *pe;
3470 struct got_reference *head_ref = NULL;
3471 struct got_object_id *head_commit_id = NULL;
3473 *new_commit_id = NULL;
3475 TAILQ_INIT(&commitable_paths);
3477 err = lock_worktree(worktree, LOCK_EX);
3478 if (err)
3479 goto done;
3481 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3482 if (err)
3483 goto done;
3485 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3486 if (err)
3487 goto done;
3489 if (ondisk_path) {
3490 err = got_path_skip_common_ancestor(&relpath,
3491 worktree->root_path, ondisk_path);
3492 if (err)
3493 return err;
3496 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3497 if (err)
3498 goto done;
3500 cc_arg.commitable_paths = &commitable_paths;
3501 cc_arg.worktree = worktree;
3502 cc_arg.repo = repo;
3503 err = worktree_status(worktree, relpath ? relpath : "",
3504 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3505 if (err)
3506 goto done;
3508 if (TAILQ_EMPTY(&commitable_paths)) {
3509 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3510 goto done;
3513 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3514 struct got_commitable *ct = pe->data;
3515 err = check_ct_out_of_date(ct, repo, head_commit_id);
3516 if (err)
3517 goto done;
3520 err = commit_worktree(new_commit_id, &commitable_paths,
3521 head_commit_id, worktree, ondisk_path, author, committer,
3522 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3523 if (err)
3524 goto done;
3526 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3527 fileindex);
3528 sync_err = sync_fileindex(fileindex, fileindex_path);
3529 if (sync_err && err == NULL)
3530 err = sync_err;
3531 done:
3532 if (fileindex)
3533 got_fileindex_free(fileindex);
3534 free(fileindex_path);
3535 free(relpath);
3536 unlockerr = lock_worktree(worktree, LOCK_SH);
3537 if (unlockerr && err == NULL)
3538 err = unlockerr;
3539 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3540 struct got_commitable *ct = pe->data;
3541 free_commitable(ct);
3543 got_pathlist_free(&commitable_paths);
3544 return err;
3547 const char *
3548 got_commitable_get_path(struct got_commitable *ct)
3550 return ct->path;
3553 unsigned int
3554 got_commitable_get_status(struct got_commitable *ct)
3556 return ct->status;
3559 struct check_rebase_ok_arg {
3560 struct got_worktree *worktree;
3561 struct got_repository *repo;
3562 int rebase_in_progress;
3565 static const struct got_error *
3566 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3568 const struct got_error *err = NULL;
3569 struct check_rebase_ok_arg *a = arg;
3570 unsigned char status;
3571 struct stat sb;
3572 char *ondisk_path;
3574 if (!a->rebase_in_progress) {
3575 /* Reject rebase of a work tree with mixed base commits. */
3576 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3577 SHA1_DIGEST_LENGTH))
3578 return got_error(GOT_ERR_MIXED_COMMITS);
3581 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3582 == -1)
3583 return got_error_from_errno("asprintf");
3585 /* Reject rebase of a work tree with modified or conflicted files. */
3586 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3587 free(ondisk_path);
3588 if (err)
3589 return err;
3591 if (a->rebase_in_progress) {
3592 if (status == GOT_STATUS_CONFLICT)
3593 return got_error(GOT_ERR_CONFLICTS);
3594 } else if (status != GOT_STATUS_NO_CHANGE)
3595 return got_error(GOT_ERR_MODIFIED);
3597 return NULL;
3600 const struct got_error *
3601 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3602 struct got_reference **tmp_branch, struct got_worktree *worktree,
3603 struct got_reference *branch, struct got_repository *repo)
3605 const struct got_error *err = NULL;
3606 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3607 char *branch_ref_name = NULL;
3608 struct got_fileindex *fileindex = NULL;
3609 char *fileindex_path = NULL;
3610 struct check_rebase_ok_arg ok_arg;
3611 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3613 *new_base_branch_ref = NULL;
3614 *tmp_branch = NULL;
3616 err = lock_worktree(worktree, LOCK_EX);
3617 if (err)
3618 return err;
3620 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3621 if (err)
3622 goto done;
3624 ok_arg.worktree = worktree;
3625 ok_arg.repo = repo;
3626 ok_arg.rebase_in_progress = 0;
3627 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3628 &ok_arg);
3629 if (err)
3630 goto done;
3632 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3633 if (err)
3634 goto done;
3636 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3637 if (err)
3638 goto done;
3640 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3641 if (err)
3642 goto done;
3644 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3645 0);
3646 if (err)
3647 goto done;
3649 err = got_ref_alloc_symref(new_base_branch_ref,
3650 new_base_branch_ref_name, wt_branch);
3651 if (err)
3652 goto done;
3653 err = got_ref_write(*new_base_branch_ref, repo);
3654 if (err)
3655 goto done;
3657 /* TODO Lock original branch's ref while rebasing? */
3659 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3660 if (err)
3661 goto done;
3663 err = got_ref_write(branch_ref, repo);
3664 if (err)
3665 goto done;
3667 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3668 worktree->base_commit_id);
3669 if (err)
3670 goto done;
3671 err = got_ref_write(*tmp_branch, repo);
3672 if (err)
3673 goto done;
3675 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3676 if (err)
3677 goto done;
3678 done:
3679 free(fileindex_path);
3680 if (fileindex)
3681 got_fileindex_free(fileindex);
3682 free(tmp_branch_name);
3683 free(new_base_branch_ref_name);
3684 free(branch_ref_name);
3685 if (branch_ref)
3686 got_ref_close(branch_ref);
3687 if (wt_branch)
3688 got_ref_close(wt_branch);
3689 if (err) {
3690 if (*new_base_branch_ref) {
3691 got_ref_close(*new_base_branch_ref);
3692 *new_base_branch_ref = NULL;
3694 if (*tmp_branch) {
3695 got_ref_close(*tmp_branch);
3696 *tmp_branch = NULL;
3698 lock_worktree(worktree, LOCK_SH);
3700 return err;
3703 const struct got_error *
3704 got_worktree_rebase_continue(struct got_object_id **commit_id,
3705 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3706 struct got_reference **branch, struct got_worktree *worktree,
3707 struct got_repository *repo)
3709 const struct got_error *err;
3710 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3711 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3712 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3714 *commit_id = NULL;
3716 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3717 if (err)
3718 return err;
3720 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3721 if (err)
3722 goto done;
3724 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3725 if (err)
3726 goto done;
3728 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3729 if (err)
3730 goto done;
3732 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3733 if (err)
3734 goto done;
3736 err = got_ref_open(branch, repo,
3737 got_ref_get_symref_target(branch_ref), 0);
3738 if (err)
3739 goto done;
3741 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3742 if (err)
3743 goto done;
3745 err = got_ref_resolve(commit_id, repo, commit_ref);
3746 if (err)
3747 goto done;
3749 err = got_ref_open(new_base_branch, repo,
3750 new_base_branch_ref_name, 0);
3751 if (err)
3752 goto done;
3754 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3755 if (err)
3756 goto done;
3757 done:
3758 free(commit_ref_name);
3759 free(branch_ref_name);
3760 if (commit_ref)
3761 got_ref_close(commit_ref);
3762 if (branch_ref)
3763 got_ref_close(branch_ref);
3764 if (err) {
3765 free(*commit_id);
3766 *commit_id = NULL;
3767 if (*tmp_branch) {
3768 got_ref_close(*tmp_branch);
3769 *tmp_branch = NULL;
3771 if (*new_base_branch) {
3772 got_ref_close(*new_base_branch);
3773 *new_base_branch = NULL;
3775 if (*branch) {
3776 got_ref_close(*branch);
3777 *branch = NULL;
3780 return err;
3783 const struct got_error *
3784 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3786 const struct got_error *err;
3787 char *tmp_branch_name = NULL;
3789 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3790 if (err)
3791 return err;
3793 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3794 free(tmp_branch_name);
3795 return NULL;
3798 static const struct got_error *
3799 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3800 char **logmsg, void *arg)
3802 *logmsg = arg;
3803 return NULL;
3806 static const struct got_error *
3807 rebase_status(void *arg, unsigned char status, const char *path,
3808 struct got_object_id *blob_id, struct got_object_id *commit_id)
3810 return NULL;
3813 struct collect_merged_paths_arg {
3814 got_worktree_checkout_cb progress_cb;
3815 void *progress_arg;
3816 struct got_pathlist_head *merged_paths;
3819 static const struct got_error *
3820 collect_merged_paths(void *arg, unsigned char status, const char *path)
3822 const struct got_error *err;
3823 struct collect_merged_paths_arg *a = arg;
3824 char *p;
3825 struct got_pathlist_entry *new;
3827 err = (*a->progress_cb)(a->progress_arg, status, path);
3828 if (err)
3829 return err;
3831 if (status != GOT_STATUS_MERGE &&
3832 status != GOT_STATUS_ADD &&
3833 status != GOT_STATUS_DELETE &&
3834 status != GOT_STATUS_CONFLICT)
3835 return NULL;
3837 p = strdup(path);
3838 if (p == NULL)
3839 return got_error_from_errno("strdup");
3841 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3842 if (err || new == NULL)
3843 free(p);
3844 return err;
3847 void
3848 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3850 struct got_pathlist_entry *pe;
3852 TAILQ_FOREACH(pe, merged_paths, entry)
3853 free((char *)pe->path);
3855 got_pathlist_free(merged_paths);
3858 static const struct got_error *
3859 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3860 struct got_repository *repo)
3862 const struct got_error *err;
3863 struct got_reference *commit_ref = NULL;
3865 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3866 if (err) {
3867 if (err->code != GOT_ERR_NOT_REF)
3868 goto done;
3869 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3870 if (err)
3871 goto done;
3872 err = got_ref_write(commit_ref, repo);
3873 if (err)
3874 goto done;
3875 } else {
3876 struct got_object_id *stored_id;
3877 int cmp;
3879 err = got_ref_resolve(&stored_id, repo, commit_ref);
3880 if (err)
3881 goto done;
3882 cmp = got_object_id_cmp(commit_id, stored_id);
3883 free(stored_id);
3884 if (cmp != 0) {
3885 err = got_error(GOT_ERR_REBASE_COMMITID);
3886 goto done;
3889 done:
3890 if (commit_ref)
3891 got_ref_close(commit_ref);
3892 return err;
3895 static const struct got_error *
3896 rebase_merge_files(struct got_pathlist_head *merged_paths,
3897 const char *commit_ref_name, struct got_worktree *worktree,
3898 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3899 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3900 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3902 const struct got_error *err;
3903 struct got_fileindex *fileindex;
3904 char *fileindex_path;
3905 struct got_reference *commit_ref = NULL;
3906 struct collect_merged_paths_arg cmp_arg;
3908 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3910 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3911 if (err)
3912 return err;
3914 cmp_arg.progress_cb = progress_cb;
3915 cmp_arg.progress_arg = progress_arg;
3916 cmp_arg.merged_paths = merged_paths;
3917 err = merge_files(worktree, fileindex, fileindex_path,
3918 parent_commit_id, commit_id, repo, collect_merged_paths,
3919 &cmp_arg, cancel_cb, cancel_arg);
3920 got_fileindex_free(fileindex);
3921 free(fileindex_path);
3922 if (commit_ref)
3923 got_ref_close(commit_ref);
3924 return err;
3927 const struct got_error *
3928 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3929 struct got_worktree *worktree, struct got_object_id *parent_commit_id,
3930 struct got_object_id *commit_id, struct got_repository *repo,
3931 got_worktree_checkout_cb progress_cb, void *progress_arg,
3932 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3934 const struct got_error *err;
3935 char *commit_ref_name;
3937 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3938 if (err)
3939 return err;
3941 err = store_commit_id(commit_ref_name, commit_id, repo);
3942 if (err)
3943 goto done;
3945 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
3946 parent_commit_id, commit_id, repo, progress_cb, progress_arg,
3947 cancel_cb, cancel_arg);
3948 done:
3949 free(commit_ref_name);
3950 return err;
3953 const struct got_error *
3954 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
3955 struct got_worktree *worktree, struct got_object_id *parent_commit_id,
3956 struct got_object_id *commit_id, struct got_repository *repo,
3957 got_worktree_checkout_cb progress_cb, void *progress_arg,
3958 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3960 const struct got_error *err;
3961 char *commit_ref_name;
3963 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
3964 if (err)
3965 return err;
3967 err = store_commit_id(commit_ref_name, commit_id, repo);
3968 if (err)
3969 goto done;
3971 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
3972 parent_commit_id, commit_id, repo, progress_cb, progress_arg,
3973 cancel_cb, cancel_arg);
3974 done:
3975 free(commit_ref_name);
3976 return err;
3979 static const struct got_error *
3980 rebase_commit(struct got_object_id **new_commit_id,
3981 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
3982 struct got_worktree *worktree, struct got_reference *tmp_branch,
3983 struct got_commit_object *orig_commit, const char *new_logmsg,
3984 struct got_repository *repo)
3986 const struct got_error *err, *sync_err;
3987 struct got_pathlist_head commitable_paths;
3988 struct collect_commitables_arg cc_arg;
3989 struct got_fileindex *fileindex = NULL;
3990 char *fileindex_path = NULL;
3991 struct got_reference *head_ref = NULL;
3992 struct got_object_id *head_commit_id = NULL;
3993 char *logmsg = NULL;
3995 TAILQ_INIT(&commitable_paths);
3996 *new_commit_id = NULL;
3998 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4000 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4001 if (err)
4002 goto done;
4004 cc_arg.commitable_paths = &commitable_paths;
4005 cc_arg.worktree = worktree;
4006 cc_arg.repo = repo;
4008 * If possible get the status of individual files directly to
4009 * avoid crawling the entire work tree once per rebased commit.
4010 * TODO: Ideally, merged_paths would contain a list of commitables
4011 * we could use so we could skip worktree_status() entirely.
4013 if (merged_paths) {
4014 struct got_pathlist_entry *pe;
4015 TAILQ_FOREACH(pe, merged_paths, entry) {
4016 err = worktree_status(worktree, pe->path, fileindex,
4017 repo, collect_commitables, &cc_arg, NULL, NULL);
4018 if (err)
4019 goto done;
4021 } else {
4022 err = worktree_status(worktree, "", fileindex, repo,
4023 collect_commitables, &cc_arg, NULL, NULL);
4024 if (err)
4025 goto done;
4028 if (TAILQ_EMPTY(&commitable_paths)) {
4029 /* No-op change; commit will be elided. */
4030 err = got_ref_delete(commit_ref, repo);
4031 if (err)
4032 goto done;
4033 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4034 goto done;
4037 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4038 if (err)
4039 goto done;
4041 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4042 if (err)
4043 goto done;
4045 if (new_logmsg)
4046 logmsg = strdup(new_logmsg);
4047 else
4048 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4049 if (logmsg == NULL)
4050 return got_error_from_errno("strdup");
4052 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4053 worktree, NULL, got_object_commit_get_author(orig_commit),
4054 got_object_commit_get_committer(orig_commit),
4055 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4056 if (err)
4057 goto done;
4059 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4060 if (err)
4061 goto done;
4063 err = got_ref_delete(commit_ref, repo);
4064 if (err)
4065 goto done;
4067 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4068 fileindex);
4069 sync_err = sync_fileindex(fileindex, fileindex_path);
4070 if (sync_err && err == NULL)
4071 err = sync_err;
4072 done:
4073 if (fileindex)
4074 got_fileindex_free(fileindex);
4075 free(fileindex_path);
4076 free(head_commit_id);
4077 if (head_ref)
4078 got_ref_close(head_ref);
4079 if (err) {
4080 free(*new_commit_id);
4081 *new_commit_id = NULL;
4083 return err;
4086 const struct got_error *
4087 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4088 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4089 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4090 struct got_object_id *orig_commit_id, struct got_repository *repo)
4092 const struct got_error *err;
4093 char *commit_ref_name;
4094 struct got_reference *commit_ref = NULL;
4095 struct got_object_id *commit_id = NULL;
4097 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4098 if (err)
4099 return err;
4101 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4102 if (err)
4103 goto done;
4104 err = got_ref_resolve(&commit_id, repo, commit_ref);
4105 if (err)
4106 goto done;
4107 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4108 err = got_error(GOT_ERR_REBASE_COMMITID);
4109 goto done;
4112 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4113 worktree, tmp_branch, orig_commit, NULL, repo);
4114 done:
4115 if (commit_ref)
4116 got_ref_close(commit_ref);
4117 free(commit_ref_name);
4118 free(commit_id);
4119 return err;
4122 const struct got_error *
4123 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4124 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4125 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4126 struct got_object_id *orig_commit_id, const char *new_logmsg,
4127 struct got_repository *repo)
4129 const struct got_error *err;
4130 char *commit_ref_name;
4131 struct got_reference *commit_ref = NULL;
4132 struct got_object_id *commit_id = NULL;
4134 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4135 if (err)
4136 return err;
4138 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4139 if (err)
4140 goto done;
4141 err = got_ref_resolve(&commit_id, repo, commit_ref);
4142 if (err)
4143 goto done;
4144 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4145 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4146 goto done;
4149 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4150 worktree, tmp_branch, orig_commit, new_logmsg, repo);
4151 done:
4152 if (commit_ref)
4153 got_ref_close(commit_ref);
4154 free(commit_ref_name);
4155 free(commit_id);
4156 return err;
4159 const struct got_error *
4160 got_worktree_rebase_postpone(struct got_worktree *worktree)
4162 return lock_worktree(worktree, LOCK_SH);
4165 static const struct got_error *
4166 delete_ref(const char *name, struct got_repository *repo)
4168 const struct got_error *err;
4169 struct got_reference *ref;
4171 err = got_ref_open(&ref, repo, name, 0);
4172 if (err) {
4173 if (err->code == GOT_ERR_NOT_REF)
4174 return NULL;
4175 return err;
4178 err = got_ref_delete(ref, repo);
4179 got_ref_close(ref);
4180 return err;
4183 static const struct got_error *
4184 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4186 const struct got_error *err;
4187 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4188 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4190 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4191 if (err)
4192 goto done;
4193 err = delete_ref(tmp_branch_name, repo);
4194 if (err)
4195 goto done;
4197 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4198 if (err)
4199 goto done;
4200 err = delete_ref(new_base_branch_ref_name, repo);
4201 if (err)
4202 goto done;
4204 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4205 if (err)
4206 goto done;
4207 err = delete_ref(branch_ref_name, repo);
4208 if (err)
4209 goto done;
4211 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4212 if (err)
4213 goto done;
4214 err = delete_ref(commit_ref_name, repo);
4215 if (err)
4216 goto done;
4218 done:
4219 free(tmp_branch_name);
4220 free(new_base_branch_ref_name);
4221 free(branch_ref_name);
4222 free(commit_ref_name);
4223 return err;
4226 const struct got_error *
4227 got_worktree_rebase_complete(struct got_worktree *worktree,
4228 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
4229 struct got_reference *rebased_branch,
4230 struct got_repository *repo)
4232 const struct got_error *err, *unlockerr;
4233 struct got_object_id *new_head_commit_id = NULL;
4235 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4236 if (err)
4237 return err;
4239 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4240 if (err)
4241 goto done;
4243 err = got_ref_write(rebased_branch, repo);
4244 if (err)
4245 goto done;
4247 err = got_worktree_set_head_ref(worktree, rebased_branch);
4248 if (err)
4249 goto done;
4251 err = delete_rebase_refs(worktree, repo);
4252 done:
4253 free(new_head_commit_id);
4254 unlockerr = lock_worktree(worktree, LOCK_SH);
4255 if (unlockerr && err == NULL)
4256 err = unlockerr;
4257 return err;
4260 struct collect_revertible_paths_arg {
4261 struct got_pathlist_head *revertible_paths;
4262 struct got_worktree *worktree;
4265 static const struct got_error *
4266 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4267 struct got_object_id *blob_id, struct got_object_id *commit_id)
4269 struct collect_revertible_paths_arg *a = arg;
4270 const struct got_error *err = NULL;
4271 struct got_pathlist_entry *new = NULL;
4272 char *path = NULL;
4274 if (status != GOT_STATUS_ADD &&
4275 status != GOT_STATUS_DELETE &&
4276 status != GOT_STATUS_MODIFY &&
4277 status != GOT_STATUS_CONFLICT &&
4278 status != GOT_STATUS_MISSING)
4279 return NULL;
4281 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4282 return got_error_from_errno("asprintf");
4284 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4285 if (err || new == NULL)
4286 free(path);
4287 return err;
4290 const struct got_error *
4291 got_worktree_rebase_abort(struct got_worktree *worktree,
4292 struct got_repository *repo, struct got_reference *new_base_branch,
4293 got_worktree_checkout_cb progress_cb, void *progress_arg)
4295 const struct got_error *err, *unlockerr, *sync_err;
4296 struct got_reference *resolved = NULL;
4297 struct got_object_id *commit_id = NULL;
4298 struct got_fileindex *fileindex = NULL;
4299 char *fileindex_path = NULL;
4300 struct got_pathlist_head revertible_paths;
4301 struct got_pathlist_entry *pe;
4302 struct collect_revertible_paths_arg crp_arg;
4303 struct got_object_id *tree_id = NULL;
4305 TAILQ_INIT(&revertible_paths);
4307 err = lock_worktree(worktree, LOCK_EX);
4308 if (err)
4309 return err;
4311 err = got_ref_open(&resolved, repo,
4312 got_ref_get_symref_target(new_base_branch), 0);
4313 if (err)
4314 goto done;
4316 err = got_worktree_set_head_ref(worktree, resolved);
4317 if (err)
4318 goto done;
4321 * XXX commits to the base branch could have happened while
4322 * we were busy rebasing; should we store the original commit ID
4323 * when rebase begins and read it back here?
4325 err = got_ref_resolve(&commit_id, repo, resolved);
4326 if (err)
4327 goto done;
4329 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4330 if (err)
4331 goto done;
4333 err = got_object_id_by_path(&tree_id, repo,
4334 worktree->base_commit_id, worktree->path_prefix);
4335 if (err)
4336 goto done;
4338 err = delete_rebase_refs(worktree, repo);
4339 if (err)
4340 goto done;
4342 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4343 if (err)
4344 goto done;
4346 crp_arg.revertible_paths = &revertible_paths;
4347 crp_arg.worktree = worktree;
4348 err = worktree_status(worktree, "", fileindex, repo,
4349 collect_revertible_paths, &crp_arg, NULL, NULL);
4350 if (err)
4351 goto done;
4353 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4354 err = revert_file(worktree, fileindex, pe->path,
4355 progress_cb, progress_arg, repo);
4356 if (err)
4357 goto sync;
4360 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4361 repo, progress_cb, progress_arg, NULL, NULL);
4362 sync:
4363 sync_err = sync_fileindex(fileindex, fileindex_path);
4364 if (sync_err && err == NULL)
4365 err = sync_err;
4366 done:
4367 got_ref_close(resolved);
4368 free(tree_id);
4369 free(commit_id);
4370 if (fileindex)
4371 got_fileindex_free(fileindex);
4372 free(fileindex_path);
4373 TAILQ_FOREACH(pe, &revertible_paths, entry)
4374 free((char *)pe->path);
4375 got_pathlist_free(&revertible_paths);
4377 unlockerr = lock_worktree(worktree, LOCK_SH);
4378 if (unlockerr && err == NULL)
4379 err = unlockerr;
4380 return err;
4383 const struct got_error *
4384 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4385 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4386 struct got_worktree *worktree, struct got_repository *repo)
4388 const struct got_error *err = NULL;
4389 char *tmp_branch_name = NULL;
4390 char *branch_ref_name = NULL;
4391 char *base_commit_ref_name = NULL;
4392 struct got_fileindex *fileindex = NULL;
4393 char *fileindex_path = NULL;
4394 struct check_rebase_ok_arg ok_arg;
4395 struct got_reference *wt_branch = NULL;
4396 struct got_reference *base_commit_ref = NULL;
4398 *tmp_branch = NULL;
4399 *branch_ref = NULL;
4400 *base_commit_id = NULL;
4402 err = lock_worktree(worktree, LOCK_EX);
4403 if (err)
4404 return err;
4406 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4407 if (err)
4408 goto done;
4410 ok_arg.worktree = worktree;
4411 ok_arg.repo = repo;
4412 ok_arg.rebase_in_progress = 0;
4413 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
4414 &ok_arg);
4415 if (err)
4416 goto done;
4418 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4419 if (err)
4420 goto done;
4422 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4423 if (err)
4424 goto done;
4426 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4427 worktree);
4428 if (err)
4429 goto done;
4431 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4432 0);
4433 if (err)
4434 goto done;
4436 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4437 if (err)
4438 goto done;
4440 err = got_ref_write(*branch_ref, repo);
4441 if (err)
4442 goto done;
4444 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4445 worktree->base_commit_id);
4446 if (err)
4447 goto done;
4448 err = got_ref_write(base_commit_ref, repo);
4449 if (err)
4450 goto done;
4451 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4452 if (*base_commit_id == NULL) {
4453 err = got_error_from_errno("got_object_id_dup");
4454 goto done;
4457 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4458 worktree->base_commit_id);
4459 if (err)
4460 goto done;
4461 err = got_ref_write(*tmp_branch, repo);
4462 if (err)
4463 goto done;
4465 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4466 if (err)
4467 goto done;
4468 done:
4469 free(fileindex_path);
4470 if (fileindex)
4471 got_fileindex_free(fileindex);
4472 free(tmp_branch_name);
4473 free(branch_ref_name);
4474 free(base_commit_ref_name);
4475 if (wt_branch)
4476 got_ref_close(wt_branch);
4477 if (err) {
4478 if (*branch_ref) {
4479 got_ref_close(*branch_ref);
4480 *branch_ref = NULL;
4482 if (*tmp_branch) {
4483 got_ref_close(*tmp_branch);
4484 *tmp_branch = NULL;
4486 free(*base_commit_id);
4487 lock_worktree(worktree, LOCK_SH);
4489 return err;
4492 const struct got_error *
4493 got_worktree_histedit_postpone(struct got_worktree *worktree)
4495 return lock_worktree(worktree, LOCK_SH);
4498 const struct got_error *
4499 got_worktree_histedit_in_progress(int *in_progress,
4500 struct got_worktree *worktree)
4502 const struct got_error *err;
4503 char *tmp_branch_name = NULL;
4505 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4506 if (err)
4507 return err;
4509 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4510 free(tmp_branch_name);
4511 return NULL;
4514 const struct got_error *
4515 got_worktree_histedit_continue(struct got_object_id **commit_id,
4516 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4517 struct got_object_id **base_commit_id,
4518 struct got_worktree *worktree, struct got_repository *repo)
4520 const struct got_error *err;
4521 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4522 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4523 struct got_reference *commit_ref = NULL;
4524 struct got_reference *base_commit_ref = NULL;
4526 *commit_id = NULL;
4527 *tmp_branch = NULL;
4528 *base_commit_id = NULL;
4530 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4531 if (err)
4532 return err;
4534 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4535 if (err)
4536 goto done;
4538 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4539 if (err)
4540 goto done;
4542 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4543 worktree);
4544 if (err)
4545 goto done;
4547 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4548 if (err)
4549 goto done;
4551 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4552 if (err)
4553 goto done;
4554 err = got_ref_resolve(commit_id, repo, commit_ref);
4555 if (err)
4556 goto done;
4558 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4559 if (err)
4560 goto done;
4561 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4562 if (err)
4563 goto done;
4565 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4566 if (err)
4567 goto done;
4568 done:
4569 free(commit_ref_name);
4570 free(branch_ref_name);
4571 if (commit_ref)
4572 got_ref_close(commit_ref);
4573 if (base_commit_ref)
4574 got_ref_close(base_commit_ref);
4575 if (err) {
4576 free(*commit_id);
4577 *commit_id = NULL;
4578 free(*base_commit_id);
4579 *base_commit_id = NULL;
4580 if (*tmp_branch) {
4581 got_ref_close(*tmp_branch);
4582 *tmp_branch = NULL;
4585 return err;
4588 static const struct got_error *
4589 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4591 const struct got_error *err;
4592 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4593 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4595 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4596 if (err)
4597 goto done;
4598 err = delete_ref(tmp_branch_name, repo);
4599 if (err)
4600 goto done;
4602 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4603 worktree);
4604 if (err)
4605 goto done;
4606 err = delete_ref(base_commit_ref_name, repo);
4607 if (err)
4608 goto done;
4610 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4611 if (err)
4612 goto done;
4613 err = delete_ref(branch_ref_name, repo);
4614 if (err)
4615 goto done;
4617 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4618 if (err)
4619 goto done;
4620 err = delete_ref(commit_ref_name, repo);
4621 if (err)
4622 goto done;
4623 done:
4624 free(tmp_branch_name);
4625 free(base_commit_ref_name);
4626 free(branch_ref_name);
4627 free(commit_ref_name);
4628 return err;
4631 const struct got_error *
4632 got_worktree_histedit_abort(struct got_worktree *worktree,
4633 struct got_repository *repo, struct got_reference *branch,
4634 struct got_object_id *base_commit_id,
4635 got_worktree_checkout_cb progress_cb, void *progress_arg)
4637 const struct got_error *err, *unlockerr, *sync_err;
4638 struct got_reference *resolved = NULL;
4639 struct got_fileindex *fileindex = NULL;
4640 char *fileindex_path = NULL;
4641 struct got_pathlist_head revertible_paths;
4642 struct got_pathlist_entry *pe;
4643 struct collect_revertible_paths_arg crp_arg;
4644 struct got_object_id *tree_id = NULL;
4646 TAILQ_INIT(&revertible_paths);
4648 err = lock_worktree(worktree, LOCK_EX);
4649 if (err)
4650 return err;
4652 err = got_ref_open(&resolved, repo,
4653 got_ref_get_symref_target(branch), 0);
4654 if (err)
4655 goto done;
4657 err = got_worktree_set_head_ref(worktree, resolved);
4658 if (err)
4659 goto done;
4661 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4662 if (err)
4663 goto done;
4665 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4666 worktree->path_prefix);
4667 if (err)
4668 goto done;
4670 err = delete_histedit_refs(worktree, repo);
4671 if (err)
4672 goto done;
4674 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4675 if (err)
4676 goto done;
4678 crp_arg.revertible_paths = &revertible_paths;
4679 crp_arg.worktree = worktree;
4680 err = worktree_status(worktree, "", fileindex, repo,
4681 collect_revertible_paths, &crp_arg, NULL, NULL);
4682 if (err)
4683 goto done;
4685 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4686 err = revert_file(worktree, fileindex, pe->path,
4687 progress_cb, progress_arg, repo);
4688 if (err)
4689 goto sync;
4692 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4693 repo, progress_cb, progress_arg, NULL, NULL);
4694 sync:
4695 sync_err = sync_fileindex(fileindex, fileindex_path);
4696 if (sync_err && err == NULL)
4697 err = sync_err;
4698 done:
4699 got_ref_close(resolved);
4700 free(tree_id);
4701 if (fileindex)
4702 got_fileindex_free(fileindex);
4703 free(fileindex_path);
4704 TAILQ_FOREACH(pe, &revertible_paths, entry)
4705 free((char *)pe->path);
4706 got_pathlist_free(&revertible_paths);
4708 unlockerr = lock_worktree(worktree, LOCK_SH);
4709 if (unlockerr && err == NULL)
4710 err = unlockerr;
4711 return err;
4714 const struct got_error *
4715 got_worktree_histedit_complete(struct got_worktree *worktree,
4716 struct got_reference *tmp_branch, struct got_reference *edited_branch,
4717 struct got_repository *repo)
4719 const struct got_error *err, *unlockerr;
4720 struct got_object_id *new_head_commit_id = NULL;
4721 struct got_reference *resolved = NULL;
4723 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4724 if (err)
4725 return err;
4727 err = got_ref_open(&resolved, repo,
4728 got_ref_get_symref_target(edited_branch), 0);
4729 if (err)
4730 goto done;
4732 err = got_ref_change_ref(resolved, new_head_commit_id);
4733 if (err)
4734 goto done;
4736 err = got_ref_write(resolved, repo);
4737 if (err)
4738 goto done;
4740 err = got_worktree_set_head_ref(worktree, resolved);
4741 if (err)
4742 goto done;
4744 err = delete_histedit_refs(worktree, repo);
4745 done:
4746 free(new_head_commit_id);
4747 unlockerr = lock_worktree(worktree, LOCK_SH);
4748 if (unlockerr && err == NULL)
4749 err = unlockerr;
4750 return err;
4753 const struct got_error *
4754 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4755 struct got_object_id *commit_id, struct got_repository *repo)
4757 const struct got_error *err;
4758 char *commit_ref_name;
4760 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4761 if (err)
4762 return err;
4764 err = store_commit_id(commit_ref_name, commit_id, repo);
4765 if (err)
4766 goto done;
4768 err = delete_ref(commit_ref_name, repo);
4769 done:
4770 free(commit_ref_name);
4771 return err;