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_path(path, 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 (fstat(fd, &sb) != 0) {
146 err = got_error_from_errno2("fstat", 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_path(path, GOT_ERR_WORKTREE_META));
159 goto done;
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error_path(path, 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_msg(GOT_ERR_WORKTREE_META,
359 "could not parse work tree format version number");
360 goto done;
362 if (version != GOT_WORKTREE_FORMAT_VERSION) {
363 err = got_error(GOT_ERR_WORKTREE_VERS);
364 goto done;
367 *worktree = calloc(1, sizeof(**worktree));
368 if (*worktree == NULL) {
369 err = got_error_from_errno("calloc");
370 goto done;
372 (*worktree)->lockfd = -1;
374 (*worktree)->root_path = strdup(path);
375 if ((*worktree)->root_path == NULL) {
376 err = got_error_from_errno("strdup");
377 goto done;
379 err = read_meta_file(&(*worktree)->repo_path, path_got,
380 GOT_WORKTREE_REPOSITORY);
381 if (err)
382 goto done;
384 err = read_meta_file(&(*worktree)->path_prefix, path_got,
385 GOT_WORKTREE_PATH_PREFIX);
386 if (err)
387 goto done;
389 err = read_meta_file(&base_commit_id_str, path_got,
390 GOT_WORKTREE_BASE_COMMIT);
391 if (err)
392 goto done;
394 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
395 if (err)
396 goto done;
397 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
398 if (uuid_status != uuid_s_ok) {
399 err = got_error_uuid(uuid_status);
400 goto done;
403 err = got_repo_open(&repo, (*worktree)->repo_path);
404 if (err)
405 goto done;
407 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
408 base_commit_id_str);
409 if (err)
410 goto done;
412 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
413 GOT_WORKTREE_HEAD_REF);
414 done:
415 if (repo)
416 got_repo_close(repo);
417 free(path_got);
418 free(path_lock);
419 free(base_commit_id_str);
420 free(uuidstr);
421 free(formatstr);
422 if (err) {
423 if (fd != -1)
424 close(fd);
425 if (*worktree != NULL)
426 got_worktree_close(*worktree);
427 *worktree = NULL;
428 } else
429 (*worktree)->lockfd = fd;
431 return err;
434 const struct got_error *
435 got_worktree_open(struct got_worktree **worktree, const char *path)
437 const struct got_error *err = NULL;
439 do {
440 err = open_worktree(worktree, path);
441 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
442 return err;
443 if (*worktree)
444 return NULL;
445 path = dirname(path);
446 if (path == NULL)
447 return got_error_from_errno2("dirname", path);
448 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
450 return got_error(GOT_ERR_NOT_WORKTREE);
453 const struct got_error *
454 got_worktree_close(struct got_worktree *worktree)
456 const struct got_error *err = NULL;
457 free(worktree->root_path);
458 free(worktree->repo_path);
459 free(worktree->path_prefix);
460 free(worktree->base_commit_id);
461 free(worktree->head_ref_name);
462 if (worktree->lockfd != -1)
463 if (close(worktree->lockfd) != 0)
464 err = got_error_from_errno2("close",
465 got_worktree_get_root_path(worktree));
466 free(worktree);
467 return err;
470 const char *
471 got_worktree_get_root_path(struct got_worktree *worktree)
473 return worktree->root_path;
476 const char *
477 got_worktree_get_repo_path(struct got_worktree *worktree)
479 return worktree->repo_path;
482 const char *
483 got_worktree_get_path_prefix(struct got_worktree *worktree)
485 return worktree->path_prefix;
488 const struct got_error *
489 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
490 const char *path_prefix)
492 char *absprefix = NULL;
494 if (!got_path_is_absolute(path_prefix)) {
495 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
496 return got_error_from_errno("asprintf");
498 *match = (strcmp(absprefix ? absprefix : path_prefix,
499 worktree->path_prefix) == 0);
500 free(absprefix);
501 return NULL;
504 const char *
505 got_worktree_get_head_ref_name(struct got_worktree *worktree)
507 return worktree->head_ref_name;
510 const struct got_error *
511 got_worktree_set_head_ref(struct got_worktree *worktree,
512 struct got_reference *head_ref)
514 const struct got_error *err = NULL;
515 char *path_got = NULL, *head_ref_name = NULL;
517 if (asprintf(&path_got, "%s/%s", worktree->root_path,
518 GOT_WORKTREE_GOT_DIR) == -1) {
519 err = got_error_from_errno("asprintf");
520 path_got = NULL;
521 goto done;
524 head_ref_name = strdup(got_ref_get_name(head_ref));
525 if (head_ref_name == NULL) {
526 err = got_error_from_errno("strdup");
527 goto done;
530 err = write_head_ref(path_got, head_ref);
531 if (err)
532 goto done;
534 free(worktree->head_ref_name);
535 worktree->head_ref_name = head_ref_name;
536 done:
537 free(path_got);
538 if (err)
539 free(head_ref_name);
540 return err;
543 struct got_object_id *
544 got_worktree_get_base_commit_id(struct got_worktree *worktree)
546 return worktree->base_commit_id;
549 const struct got_error *
550 got_worktree_set_base_commit_id(struct got_worktree *worktree,
551 struct got_repository *repo, struct got_object_id *commit_id)
553 const struct got_error *err;
554 struct got_object *obj = NULL;
555 char *id_str = NULL;
556 char *path_got = NULL;
558 if (asprintf(&path_got, "%s/%s", worktree->root_path,
559 GOT_WORKTREE_GOT_DIR) == -1) {
560 err = got_error_from_errno("asprintf");
561 path_got = NULL;
562 goto done;
565 err = got_object_open(&obj, repo, commit_id);
566 if (err)
567 return err;
569 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
570 err = got_error(GOT_ERR_OBJ_TYPE);
571 goto done;
574 /* Record our base commit. */
575 err = got_object_id_str(&id_str, commit_id);
576 if (err)
577 goto done;
578 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
579 if (err)
580 goto done;
582 free(worktree->base_commit_id);
583 worktree->base_commit_id = got_object_id_dup(commit_id);
584 if (worktree->base_commit_id == NULL) {
585 err = got_error_from_errno("got_object_id_dup");
586 goto done;
588 done:
589 if (obj)
590 got_object_close(obj);
591 free(id_str);
592 free(path_got);
593 return err;
596 static const struct got_error *
597 lock_worktree(struct got_worktree *worktree, int operation)
599 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
600 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
601 : got_error_from_errno2("flock",
602 got_worktree_get_root_path(worktree)));
603 return NULL;
606 static const struct got_error *
607 add_dir_on_disk(struct got_worktree *worktree, const char *path)
609 const struct got_error *err = NULL;
610 char *abspath;
612 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
613 return got_error_from_errno("asprintf");
615 err = got_path_mkdir(abspath);
616 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
617 struct stat sb;
618 err = NULL;
619 if (lstat(abspath, &sb) == -1) {
620 err = got_error_from_errno2("lstat", abspath);
621 } else if (!S_ISDIR(sb.st_mode)) {
622 /* TODO directory is obstructed; do something */
623 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
626 free(abspath);
627 return err;
630 static const struct got_error *
631 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
633 const struct got_error *err = NULL;
634 uint8_t fbuf1[8192];
635 uint8_t fbuf2[8192];
636 size_t flen1 = 0, flen2 = 0;
638 *same = 1;
640 for (;;) {
641 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
642 if (flen1 == 0 && ferror(f1)) {
643 err = got_error_from_errno("fread");
644 break;
646 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
647 if (flen2 == 0 && ferror(f2)) {
648 err = got_error_from_errno("fread");
649 break;
651 if (flen1 == 0) {
652 if (flen2 != 0)
653 *same = 0;
654 break;
655 } else if (flen2 == 0) {
656 if (flen1 != 0)
657 *same = 0;
658 break;
659 } else if (flen1 == flen2) {
660 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
661 *same = 0;
662 break;
664 } else {
665 *same = 0;
666 break;
670 return err;
673 static const struct got_error *
674 check_files_equal(int *same, const char *f1_path, const char *f2_path)
676 const struct got_error *err = NULL;
677 struct stat sb;
678 size_t size1, size2;
679 FILE *f1 = NULL, *f2 = NULL;
681 *same = 1;
683 if (lstat(f1_path, &sb) != 0) {
684 err = got_error_from_errno2("lstat", f1_path);
685 goto done;
687 size1 = sb.st_size;
689 if (lstat(f2_path, &sb) != 0) {
690 err = got_error_from_errno2("lstat", f2_path);
691 goto done;
693 size2 = sb.st_size;
695 if (size1 != size2) {
696 *same = 0;
697 return NULL;
700 f1 = fopen(f1_path, "r");
701 if (f1 == NULL)
702 return got_error_from_errno2("open", f1_path);
704 f2 = fopen(f2_path, "r");
705 if (f2 == NULL) {
706 err = got_error_from_errno2("open", f2_path);
707 goto done;
710 err = check_file_contents_equal(same, f1, f2);
711 done:
712 if (f1 && fclose(f1) != 0 && err == NULL)
713 err = got_error_from_errno("fclose");
714 if (f2 && fclose(f2) != 0 && err == NULL)
715 err = got_error_from_errno("fclose");
717 return err;
720 /*
721 * Perform a 3-way merge where blob_orig acts as the common ancestor,
722 * blob_deriv acts as the first derived version, and the file on disk
723 * acts as the second derived version.
724 */
725 static const struct got_error *
726 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
727 struct got_blob_object *blob_orig, const char *ondisk_path,
728 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
729 struct got_object_id *deriv_base_commit_id,
730 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
731 void *progress_arg)
733 const struct got_error *err = NULL;
734 int merged_fd = -1;
735 FILE *f_deriv = NULL, *f_orig = NULL;
736 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
737 char *merged_path = NULL, *base_path = NULL;
738 char *id_str = NULL;
739 char *label_deriv = NULL;
740 int overlapcnt = 0;
741 char *parent;
743 *local_changes_subsumed = 0;
745 parent = dirname(ondisk_path);
746 if (parent == NULL)
747 return got_error_from_errno2("dirname", ondisk_path);
749 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
750 return got_error_from_errno("asprintf");
752 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
753 if (err)
754 goto done;
756 free(base_path);
757 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
758 err = got_error_from_errno("asprintf");
759 base_path = NULL;
760 goto done;
763 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
764 if (err)
765 goto done;
766 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
767 blob_deriv);
768 if (err)
769 goto done;
771 free(base_path);
772 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
773 err = got_error_from_errno("asprintf");
774 base_path = NULL;
775 goto done;
778 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
779 if (err)
780 goto done;
781 if (blob_orig) {
782 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
783 blob_orig);
784 if (err)
785 goto done;
786 } else {
787 /*
788 * If the file has no blob, this is an "add vs add" conflict,
789 * and we simply use an empty ancestor file to make both files
790 * appear in the merged result in their entirety.
791 */
794 err = got_object_id_str(&id_str, deriv_base_commit_id);
795 if (err)
796 goto done;
797 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
798 err = got_error_from_errno("asprintf");
799 goto done;
802 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
803 blob_orig_path, ondisk_path, label_deriv, path);
804 if (err)
805 goto done;
807 err = (*progress_cb)(progress_arg,
808 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
809 if (err)
810 goto done;
812 if (fsync(merged_fd) != 0) {
813 err = got_error_from_errno("fsync");
814 goto done;
817 /* Check if a clean merge has subsumed all local changes. */
818 if (overlapcnt == 0) {
819 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
820 merged_path);
821 if (err)
822 goto done;
825 if (chmod(merged_path, st_mode) != 0) {
826 err = got_error_from_errno2("chmod", merged_path);
827 goto done;
830 if (rename(merged_path, ondisk_path) != 0) {
831 err = got_error_from_errno3("rename", merged_path,
832 ondisk_path);
833 unlink(merged_path);
834 goto done;
837 done:
838 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
839 err = got_error_from_errno("close");
840 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
841 err = got_error_from_errno("fclose");
842 if (f_orig && fclose(f_orig) != 0 && err == NULL)
843 err = got_error_from_errno("fclose");
844 free(merged_path);
845 free(base_path);
846 if (blob_deriv_path) {
847 unlink(blob_deriv_path);
848 free(blob_deriv_path);
850 if (blob_orig_path) {
851 unlink(blob_orig_path);
852 free(blob_orig_path);
854 free(id_str);
855 free(label_deriv);
856 return err;
859 static const struct got_error *
860 update_blob_fileindex_entry(struct got_worktree *worktree,
861 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
862 const char *ondisk_path, const char *path, struct got_blob_object *blob,
863 int update_timestamps)
865 const struct got_error *err = NULL;
867 if (ie == NULL)
868 ie = got_fileindex_entry_get(fileindex, path);
869 if (ie)
870 err = got_fileindex_entry_update(ie, ondisk_path,
871 blob->id.sha1, worktree->base_commit_id->sha1,
872 update_timestamps);
873 else {
874 struct got_fileindex_entry *new_ie;
875 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
876 path, blob->id.sha1, worktree->base_commit_id->sha1);
877 if (!err)
878 err = got_fileindex_entry_add(fileindex, new_ie);
880 return err;
883 static const struct got_error *
884 install_blob(struct got_worktree *worktree, const char *ondisk_path,
885 const char *path, uint16_t te_mode, uint16_t st_mode,
886 struct got_blob_object *blob, int restoring_missing_file,
887 int reverting_versioned_file, struct got_repository *repo,
888 got_worktree_checkout_cb progress_cb, void *progress_arg)
890 const struct got_error *err = NULL;
891 int fd = -1;
892 size_t len, hdrlen;
893 int update = 0;
894 char *tmppath = NULL;
896 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
897 GOT_DEFAULT_FILE_MODE);
898 if (fd == -1) {
899 if (errno == ENOENT) {
900 char *parent = dirname(path);
901 if (parent == NULL)
902 return got_error_from_errno2("dirname", path);
903 err = add_dir_on_disk(worktree, parent);
904 if (err)
905 return err;
906 fd = open(ondisk_path,
907 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
908 GOT_DEFAULT_FILE_MODE);
909 if (fd == -1)
910 return got_error_from_errno2("open",
911 ondisk_path);
912 } else if (errno == EEXIST) {
913 if (!S_ISREG(st_mode)) {
914 /* TODO file is obstructed; do something */
915 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
916 goto done;
917 } else {
918 err = got_opentemp_named_fd(&tmppath, &fd,
919 ondisk_path);
920 if (err)
921 goto done;
922 update = 1;
924 } else
925 return got_error_from_errno2("open", ondisk_path);
928 if (restoring_missing_file)
929 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
930 else if (reverting_versioned_file)
931 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
932 else
933 err = (*progress_cb)(progress_arg,
934 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
935 if (err)
936 goto done;
938 hdrlen = got_object_blob_get_hdrlen(blob);
939 do {
940 const uint8_t *buf = got_object_blob_get_read_buf(blob);
941 err = got_object_blob_read_block(&len, blob);
942 if (err)
943 break;
944 if (len > 0) {
945 /* Skip blob object header first time around. */
946 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
947 if (outlen == -1) {
948 err = got_error_from_errno("write");
949 goto done;
950 } else if (outlen != len - hdrlen) {
951 err = got_error(GOT_ERR_IO);
952 goto done;
954 hdrlen = 0;
956 } while (len != 0);
958 if (fsync(fd) != 0) {
959 err = got_error_from_errno("fsync");
960 goto done;
963 if (update) {
964 if (rename(tmppath, ondisk_path) != 0) {
965 err = got_error_from_errno3("rename", tmppath,
966 ondisk_path);
967 unlink(tmppath);
968 goto done;
972 if (te_mode & S_IXUSR) {
973 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
974 err = got_error_from_errno2("chmod", ondisk_path);
975 goto done;
977 } else {
978 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
979 err = got_error_from_errno2("chmod", ondisk_path);
980 goto done;
984 done:
985 if (fd != -1 && close(fd) != 0 && err == NULL)
986 err = got_error_from_errno("close");
987 free(tmppath);
988 return err;
991 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
992 static const struct got_error *
993 get_modified_file_content_status(unsigned char *status, FILE *f)
995 const struct got_error *err = NULL;
996 const char *markers[3] = {
997 GOT_DIFF_CONFLICT_MARKER_BEGIN,
998 GOT_DIFF_CONFLICT_MARKER_SEP,
999 GOT_DIFF_CONFLICT_MARKER_END
1001 int i = 0;
1002 char *line;
1003 size_t len;
1004 const char delim[3] = {'\0', '\0', '\0'};
1006 while (*status == GOT_STATUS_MODIFY) {
1007 line = fparseln(f, &len, NULL, delim, 0);
1008 if (line == NULL) {
1009 if (feof(f))
1010 break;
1011 err = got_ferror(f, GOT_ERR_IO);
1012 break;
1015 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1016 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1017 == 0)
1018 *status = GOT_STATUS_CONFLICT;
1019 else
1020 i++;
1024 return err;
1027 static int
1028 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1030 return !(ie->ctime_sec == sb->st_ctime &&
1031 ie->ctime_nsec == sb->st_ctimensec &&
1032 ie->mtime_sec == sb->st_mtime &&
1033 ie->mtime_nsec == sb->st_mtimensec &&
1034 ie->size == (sb->st_size & 0xffffffff));
1037 static const struct got_error *
1038 get_file_status(unsigned char *status, struct stat *sb,
1039 struct got_fileindex_entry *ie, const char *abspath,
1040 struct got_repository *repo)
1042 const struct got_error *err = NULL;
1043 struct got_object_id id;
1044 size_t hdrlen;
1045 FILE *f = NULL;
1046 uint8_t fbuf[8192];
1047 struct got_blob_object *blob = NULL;
1048 size_t flen, blen;
1050 *status = GOT_STATUS_NO_CHANGE;
1052 if (lstat(abspath, sb) == -1) {
1053 if (errno == ENOENT) {
1054 if (got_fileindex_entry_has_file_on_disk(ie))
1055 *status = GOT_STATUS_MISSING;
1056 else
1057 *status = GOT_STATUS_DELETE;
1058 return NULL;
1060 return got_error_from_errno2("lstat", abspath);
1063 if (!S_ISREG(sb->st_mode)) {
1064 *status = GOT_STATUS_OBSTRUCTED;
1065 return NULL;
1068 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1069 *status = GOT_STATUS_DELETE;
1070 return NULL;
1071 } else if (!got_fileindex_entry_has_blob(ie)) {
1072 *status = GOT_STATUS_ADD;
1073 return NULL;
1076 if (!stat_info_differs(ie, sb))
1077 return NULL;
1079 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1080 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1081 if (err)
1082 return err;
1084 f = fopen(abspath, "r");
1085 if (f == NULL) {
1086 err = got_error_from_errno2("fopen", abspath);
1087 goto done;
1089 hdrlen = got_object_blob_get_hdrlen(blob);
1090 for (;;) {
1091 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1092 err = got_object_blob_read_block(&blen, blob);
1093 if (err)
1094 goto done;
1095 /* Skip length of blob object header first time around. */
1096 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1097 if (flen == 0 && ferror(f)) {
1098 err = got_error_from_errno("fread");
1099 goto done;
1101 if (blen == 0) {
1102 if (flen != 0)
1103 *status = GOT_STATUS_MODIFY;
1104 break;
1105 } else if (flen == 0) {
1106 if (blen != 0)
1107 *status = GOT_STATUS_MODIFY;
1108 break;
1109 } else if (blen - hdrlen == flen) {
1110 /* Skip blob object header first time around. */
1111 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1112 *status = GOT_STATUS_MODIFY;
1113 break;
1115 } else {
1116 *status = GOT_STATUS_MODIFY;
1117 break;
1119 hdrlen = 0;
1122 if (*status == GOT_STATUS_MODIFY) {
1123 rewind(f);
1124 err = get_modified_file_content_status(status, f);
1126 done:
1127 if (blob)
1128 got_object_blob_close(blob);
1129 if (f)
1130 fclose(f);
1131 return err;
1135 * Update timestamps in the file index if a file is unmodified and
1136 * we had to run a full content comparison to find out.
1138 static const struct got_error *
1139 sync_timestamps(char *ondisk_path, unsigned char status,
1140 struct got_fileindex_entry *ie, struct stat *sb)
1142 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1143 return got_fileindex_entry_update(ie, ondisk_path,
1144 ie->blob_sha1, ie->commit_sha1, 1);
1146 return NULL;
1149 static const struct got_error *
1150 update_blob(struct got_worktree *worktree,
1151 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1152 struct got_tree_entry *te, const char *path,
1153 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1154 void *progress_arg)
1156 const struct got_error *err = NULL;
1157 struct got_blob_object *blob = NULL;
1158 char *ondisk_path;
1159 unsigned char status = GOT_STATUS_NO_CHANGE;
1160 struct stat sb;
1162 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1163 return got_error_from_errno("asprintf");
1165 if (ie) {
1166 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1167 if (err)
1168 goto done;
1169 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1170 sb.st_mode = got_fileindex_perms_to_st(ie);
1171 } else
1172 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1174 if (status == GOT_STATUS_OBSTRUCTED) {
1175 err = (*progress_cb)(progress_arg, status, path);
1176 goto done;
1179 if (ie && status != GOT_STATUS_MISSING) {
1180 if (got_fileindex_entry_has_commit(ie) &&
1181 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1182 SHA1_DIGEST_LENGTH) == 0) {
1183 err = sync_timestamps(ondisk_path, status, ie, &sb);
1184 if (err)
1185 goto done;
1186 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1187 path);
1188 goto done;
1190 if (got_fileindex_entry_has_blob(ie) &&
1191 memcmp(ie->blob_sha1, te->id->sha1,
1192 SHA1_DIGEST_LENGTH) == 0) {
1193 err = sync_timestamps(ondisk_path, status, ie, &sb);
1194 goto done;
1198 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1199 if (err)
1200 goto done;
1202 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1203 int update_timestamps;
1204 struct got_blob_object *blob2 = NULL;
1205 if (got_fileindex_entry_has_blob(ie)) {
1206 struct got_object_id id2;
1207 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1208 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1209 if (err)
1210 goto done;
1212 err = merge_blob(&update_timestamps, worktree, blob2,
1213 ondisk_path, path, sb.st_mode, blob,
1214 worktree->base_commit_id, repo,
1215 progress_cb, progress_arg);
1216 if (blob2)
1217 got_object_blob_close(blob2);
1219 * Do not update timestamps of files with local changes.
1220 * Otherwise, a future status walk would treat them as
1221 * unmodified files again.
1223 err = got_fileindex_entry_update(ie, ondisk_path,
1224 blob->id.sha1, worktree->base_commit_id->sha1,
1225 update_timestamps);
1226 } else if (status == GOT_STATUS_DELETE) {
1227 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1228 if (err)
1229 goto done;
1230 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1231 ondisk_path, path, blob, 0);
1232 if (err)
1233 goto done;
1234 } else {
1235 err = install_blob(worktree, ondisk_path, path, te->mode,
1236 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1237 repo, progress_cb, progress_arg);
1238 if (err)
1239 goto done;
1240 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1241 ondisk_path, path, blob, 1);
1242 if (err)
1243 goto done;
1245 got_object_blob_close(blob);
1246 done:
1247 free(ondisk_path);
1248 return err;
1251 static const struct got_error *
1252 remove_ondisk_file(const char *root_path, const char *path)
1254 const struct got_error *err = NULL;
1255 char *ondisk_path = NULL;
1257 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1258 return got_error_from_errno("asprintf");
1260 if (unlink(ondisk_path) == -1) {
1261 if (errno != ENOENT)
1262 err = got_error_from_errno2("unlink", ondisk_path);
1263 } else {
1264 char *parent = dirname(ondisk_path);
1265 while (parent && strcmp(parent, root_path) != 0) {
1266 if (rmdir(parent) == -1) {
1267 if (errno != ENOTEMPTY)
1268 err = got_error_from_errno2("rmdir",
1269 parent);
1270 break;
1272 parent = dirname(parent);
1275 free(ondisk_path);
1276 return err;
1279 static const struct got_error *
1280 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1281 struct got_fileindex_entry *ie, struct got_repository *repo,
1282 got_worktree_checkout_cb progress_cb, void *progress_arg)
1284 const struct got_error *err = NULL;
1285 unsigned char status;
1286 struct stat sb;
1287 char *ondisk_path;
1289 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1290 == -1)
1291 return got_error_from_errno("asprintf");
1293 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1294 if (err)
1295 return err;
1297 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1298 status == GOT_STATUS_ADD) {
1299 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1300 if (err)
1301 return err;
1303 * Preserve the working file and change the deleted blob's
1304 * entry into a schedule-add entry.
1306 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1307 0);
1308 if (err)
1309 return err;
1310 } else {
1311 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1312 if (err)
1313 return err;
1314 if (status == GOT_STATUS_NO_CHANGE) {
1315 err = remove_ondisk_file(worktree->root_path, ie->path);
1316 if (err)
1317 return err;
1319 got_fileindex_entry_remove(fileindex, ie);
1322 return err;
1325 struct diff_cb_arg {
1326 struct got_fileindex *fileindex;
1327 struct got_worktree *worktree;
1328 struct got_repository *repo;
1329 got_worktree_checkout_cb progress_cb;
1330 void *progress_arg;
1331 got_worktree_cancel_cb cancel_cb;
1332 void *cancel_arg;
1335 static const struct got_error *
1336 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1337 struct got_tree_entry *te, const char *parent_path)
1339 struct diff_cb_arg *a = arg;
1341 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1342 return got_error(GOT_ERR_CANCELLED);
1344 return update_blob(a->worktree, a->fileindex, ie, te,
1345 ie->path, a->repo, a->progress_cb, a->progress_arg);
1348 static const struct got_error *
1349 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1351 struct diff_cb_arg *a = arg;
1353 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1354 return got_error(GOT_ERR_CANCELLED);
1356 return delete_blob(a->worktree, a->fileindex, ie,
1357 a->repo, a->progress_cb, a->progress_arg);
1360 static const struct got_error *
1361 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1363 struct diff_cb_arg *a = arg;
1364 const struct got_error *err;
1365 char *path;
1367 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1368 return got_error(GOT_ERR_CANCELLED);
1370 if (asprintf(&path, "%s%s%s", parent_path,
1371 parent_path[0] ? "/" : "", te->name)
1372 == -1)
1373 return got_error_from_errno("asprintf");
1375 if (S_ISDIR(te->mode))
1376 err = add_dir_on_disk(a->worktree, path);
1377 else
1378 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1379 a->repo, a->progress_cb, a->progress_arg);
1381 free(path);
1382 return err;
1385 static const struct got_error *
1386 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1388 const struct got_error *err = NULL;
1389 char *uuidstr = NULL;
1390 uint32_t uuid_status;
1392 *refname = NULL;
1394 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1395 if (uuid_status != uuid_s_ok)
1396 return got_error_uuid(uuid_status);
1398 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1399 == -1) {
1400 err = got_error_from_errno("asprintf");
1401 *refname = NULL;
1403 free(uuidstr);
1404 return err;
1407 const struct got_error *
1408 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1410 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1413 static const struct got_error *
1414 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1416 return get_ref_name(refname, worktree,
1417 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1420 static const struct got_error *
1421 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1423 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1426 static const struct got_error *
1427 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1429 return get_ref_name(refname, worktree,
1430 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1433 static const struct got_error *
1434 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1436 return get_ref_name(refname, worktree,
1437 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1440 static const struct got_error *
1441 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1443 return get_ref_name(refname, worktree,
1444 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1447 static const struct got_error *
1448 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1450 return get_ref_name(refname, worktree,
1451 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1454 static const struct got_error *
1455 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1457 return get_ref_name(refname, worktree,
1458 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1461 static const struct got_error *
1462 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1464 return get_ref_name(refname, worktree,
1465 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1468 const struct got_error *
1469 got_worktree_get_histedit_script_path(char **path,
1470 struct got_worktree *worktree)
1472 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1473 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1474 *path = NULL;
1475 return got_error_from_errno("asprintf");
1477 return NULL;
1481 * Prevent Git's garbage collector from deleting our base commit by
1482 * setting a reference to our base commit's ID.
1484 static const struct got_error *
1485 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_reference *ref = NULL;
1489 char *refname;
1491 err = got_worktree_get_base_ref_name(&refname, worktree);
1492 if (err)
1493 return err;
1495 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1496 if (err)
1497 goto done;
1499 err = got_ref_write(ref, repo);
1500 done:
1501 free(refname);
1502 if (ref)
1503 got_ref_close(ref);
1504 return err;
1507 static const struct got_error *
1508 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1510 const struct got_error *err = NULL;
1512 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1513 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1514 err = got_error_from_errno("asprintf");
1515 *fileindex_path = NULL;
1517 return err;
1521 static const struct got_error *
1522 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1523 struct got_worktree *worktree)
1525 const struct got_error *err = NULL;
1526 FILE *index = NULL;
1528 *fileindex_path = NULL;
1529 *fileindex = got_fileindex_alloc();
1530 if (*fileindex == NULL)
1531 return got_error_from_errno("got_fileindex_alloc");
1533 err = get_fileindex_path(fileindex_path, worktree);
1534 if (err)
1535 goto done;
1537 index = fopen(*fileindex_path, "rb");
1538 if (index == NULL) {
1539 if (errno != ENOENT)
1540 err = got_error_from_errno2("fopen", *fileindex_path);
1541 } else {
1542 err = got_fileindex_read(*fileindex, index);
1543 if (fclose(index) != 0 && err == NULL)
1544 err = got_error_from_errno("fclose");
1546 done:
1547 if (err) {
1548 free(*fileindex_path);
1549 *fileindex_path = NULL;
1550 got_fileindex_free(*fileindex);
1551 *fileindex = NULL;
1553 return err;
1556 struct bump_base_commit_id_arg {
1557 struct got_object_id *base_commit_id;
1558 const char *path;
1559 size_t path_len;
1560 const char *entry_name;
1561 got_worktree_checkout_cb progress_cb;
1562 void *progress_arg;
1565 /* Bump base commit ID of all files within an updated part of the work tree. */
1566 static const struct got_error *
1567 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1569 const struct got_error *err;
1570 struct bump_base_commit_id_arg *a = arg;
1572 if (a->entry_name) {
1573 if (strcmp(ie->path, a->path) != 0)
1574 return NULL;
1575 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1576 return NULL;
1578 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1579 SHA1_DIGEST_LENGTH) == 0)
1580 return NULL;
1582 if (a->progress_cb) {
1583 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1584 ie->path);
1585 if (err)
1586 return err;
1588 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1589 return NULL;
1592 static const struct got_error *
1593 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1595 const struct got_error *err = NULL;
1596 char *new_fileindex_path = NULL;
1597 FILE *new_index = NULL;
1599 err = got_opentemp_named(&new_fileindex_path, &new_index,
1600 fileindex_path);
1601 if (err)
1602 goto done;
1604 err = got_fileindex_write(fileindex, new_index);
1605 if (err)
1606 goto done;
1608 if (rename(new_fileindex_path, fileindex_path) != 0) {
1609 err = got_error_from_errno3("rename", new_fileindex_path,
1610 fileindex_path);
1611 unlink(new_fileindex_path);
1613 done:
1614 if (new_index)
1615 fclose(new_index);
1616 free(new_fileindex_path);
1617 return err;
1620 static const struct got_error *
1621 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1622 struct got_object_id **tree_id, const char *wt_relpath,
1623 struct got_worktree *worktree, struct got_repository *repo)
1625 const struct got_error *err = NULL;
1626 struct got_object_id *id = NULL;
1627 char *in_repo_path = NULL;
1628 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1630 *entry_type = GOT_OBJ_TYPE_ANY;
1631 *tree_relpath = NULL;
1632 *tree_id = NULL;
1634 if (wt_relpath[0] == '\0') {
1635 /* Check out all files within the work tree. */
1636 *entry_type = GOT_OBJ_TYPE_TREE;
1637 *tree_relpath = strdup("");
1638 if (*tree_relpath == NULL) {
1639 err = got_error_from_errno("strdup");
1640 goto done;
1642 err = got_object_id_by_path(tree_id, repo,
1643 worktree->base_commit_id, worktree->path_prefix);
1644 if (err)
1645 goto done;
1646 return NULL;
1649 /* Check out a subset of files in the work tree. */
1651 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1652 is_root_wt ? "" : "/", wt_relpath) == -1) {
1653 err = got_error_from_errno("asprintf");
1654 goto done;
1657 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1658 in_repo_path);
1659 if (err)
1660 goto done;
1662 free(in_repo_path);
1663 in_repo_path = NULL;
1665 err = got_object_get_type(entry_type, repo, id);
1666 if (err)
1667 goto done;
1669 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1670 /* Check out a single file. */
1671 if (strchr(wt_relpath, '/') == NULL) {
1672 /* Check out a single file in work tree's root dir. */
1673 in_repo_path = strdup(worktree->path_prefix);
1674 if (in_repo_path == NULL) {
1675 err = got_error_from_errno("strdup");
1676 goto done;
1678 *tree_relpath = strdup("");
1679 if (*tree_relpath == NULL) {
1680 err = got_error_from_errno("strdup");
1681 goto done;
1683 } else {
1684 /* Check out a single file in a subdirectory. */
1685 err = got_path_dirname(tree_relpath, wt_relpath);
1686 if (err)
1687 return err;
1688 if (asprintf(&in_repo_path, "%s%s%s",
1689 worktree->path_prefix, is_root_wt ? "" : "/",
1690 *tree_relpath) == -1) {
1691 err = got_error_from_errno("asprintf");
1692 goto done;
1695 err = got_object_id_by_path(tree_id, repo,
1696 worktree->base_commit_id, in_repo_path);
1697 } else {
1698 /* Check out all files within a subdirectory. */
1699 *tree_id = got_object_id_dup(id);
1700 if (*tree_id == NULL) {
1701 err = got_error_from_errno("got_object_id_dup");
1702 goto done;
1704 *tree_relpath = strdup(wt_relpath);
1705 if (*tree_relpath == NULL) {
1706 err = got_error_from_errno("strdup");
1707 goto done;
1710 done:
1711 free(id);
1712 free(in_repo_path);
1713 if (err) {
1714 *entry_type = GOT_OBJ_TYPE_ANY;
1715 free(*tree_relpath);
1716 *tree_relpath = NULL;
1717 free(*tree_id);
1718 *tree_id = NULL;
1720 return err;
1723 static const struct got_error *
1724 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1725 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1726 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1727 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1729 const struct got_error *err = NULL;
1730 struct got_commit_object *commit = NULL;
1731 struct got_tree_object *tree = NULL;
1732 struct got_fileindex_diff_tree_cb diff_cb;
1733 struct diff_cb_arg arg;
1735 err = ref_base_commit(worktree, repo);
1736 if (err)
1737 goto done;
1739 err = got_object_open_as_commit(&commit, repo,
1740 worktree->base_commit_id);
1741 if (err)
1742 goto done;
1744 err = got_object_open_as_tree(&tree, repo, tree_id);
1745 if (err)
1746 goto done;
1748 if (entry_name &&
1749 got_object_tree_find_entry(tree, entry_name) == NULL) {
1750 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1751 goto done;
1754 diff_cb.diff_old_new = diff_old_new;
1755 diff_cb.diff_old = diff_old;
1756 diff_cb.diff_new = diff_new;
1757 arg.fileindex = fileindex;
1758 arg.worktree = worktree;
1759 arg.repo = repo;
1760 arg.progress_cb = progress_cb;
1761 arg.progress_arg = progress_arg;
1762 arg.cancel_cb = cancel_cb;
1763 arg.cancel_arg = cancel_arg;
1764 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1765 entry_name, repo, &diff_cb, &arg);
1766 done:
1767 if (tree)
1768 got_object_tree_close(tree);
1769 if (commit)
1770 got_object_commit_close(commit);
1771 return err;
1774 const struct got_error *
1775 got_worktree_checkout_files(struct got_worktree *worktree,
1776 struct got_pathlist_head *paths, struct got_repository *repo,
1777 got_worktree_checkout_cb progress_cb, void *progress_arg,
1778 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1780 const struct got_error *err = NULL, *sync_err, *unlockerr;
1781 struct got_commit_object *commit = NULL;
1782 struct got_tree_object *tree = NULL;
1783 struct got_fileindex *fileindex = NULL;
1784 char *fileindex_path = NULL;
1785 struct got_pathlist_entry *pe;
1786 struct tree_path_data {
1787 SIMPLEQ_ENTRY(tree_path_data) entry;
1788 struct got_object_id *tree_id;
1789 int entry_type;
1790 char *relpath;
1791 char *entry_name;
1792 } *tpd = NULL;
1793 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1795 SIMPLEQ_INIT(&tree_paths);
1797 err = lock_worktree(worktree, LOCK_EX);
1798 if (err)
1799 return err;
1801 /* Map all specified paths to in-repository trees. */
1802 TAILQ_FOREACH(pe, paths, entry) {
1803 tpd = malloc(sizeof(*tpd));
1804 if (tpd == NULL) {
1805 err = got_error_from_errno("malloc");
1806 goto done;
1809 err = find_tree_entry_for_checkout(&tpd->entry_type,
1810 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1811 if (err) {
1812 free(tpd);
1813 goto done;
1816 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1817 err = got_path_basename(&tpd->entry_name, pe->path);
1818 if (err) {
1819 free(tpd->relpath);
1820 free(tpd->tree_id);
1821 free(tpd);
1822 goto done;
1824 } else
1825 tpd->entry_name = NULL;
1827 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1831 * Read the file index.
1832 * Checking out files is supposed to be an idempotent operation.
1833 * If the on-disk file index is incomplete we will try to complete it.
1835 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1836 if (err)
1837 goto done;
1839 tpd = SIMPLEQ_FIRST(&tree_paths);
1840 TAILQ_FOREACH(pe, paths, entry) {
1841 struct bump_base_commit_id_arg bbc_arg;
1843 err = checkout_files(worktree, fileindex, tpd->relpath,
1844 tpd->tree_id, tpd->entry_name, repo,
1845 progress_cb, progress_arg, cancel_cb, cancel_arg);
1846 if (err)
1847 break;
1849 bbc_arg.base_commit_id = worktree->base_commit_id;
1850 bbc_arg.entry_name = tpd->entry_name;
1851 bbc_arg.path = pe->path;
1852 bbc_arg.path_len = strlen(pe->path);
1853 bbc_arg.progress_cb = progress_cb;
1854 bbc_arg.progress_arg = progress_arg;
1855 err = got_fileindex_for_each_entry_safe(fileindex,
1856 bump_base_commit_id, &bbc_arg);
1857 if (err)
1858 break;
1860 tpd = SIMPLEQ_NEXT(tpd, entry);
1862 sync_err = sync_fileindex(fileindex, fileindex_path);
1863 if (sync_err && err == NULL)
1864 err = sync_err;
1865 done:
1866 free(fileindex_path);
1867 if (tree)
1868 got_object_tree_close(tree);
1869 if (commit)
1870 got_object_commit_close(commit);
1871 if (fileindex)
1872 got_fileindex_free(fileindex);
1873 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1874 tpd = SIMPLEQ_FIRST(&tree_paths);
1875 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1876 free(tpd->relpath);
1877 free(tpd->tree_id);
1878 free(tpd);
1880 unlockerr = lock_worktree(worktree, LOCK_SH);
1881 if (unlockerr && err == NULL)
1882 err = unlockerr;
1883 return err;
1886 struct merge_file_cb_arg {
1887 struct got_worktree *worktree;
1888 struct got_fileindex *fileindex;
1889 got_worktree_checkout_cb progress_cb;
1890 void *progress_arg;
1891 got_worktree_cancel_cb cancel_cb;
1892 void *cancel_arg;
1893 struct got_object_id *commit_id2;
1896 static const struct got_error *
1897 merge_file_cb(void *arg, struct got_blob_object *blob1,
1898 struct got_blob_object *blob2, struct got_object_id *id1,
1899 struct got_object_id *id2, const char *path1, const char *path2,
1900 struct got_repository *repo)
1902 static const struct got_error *err = NULL;
1903 struct merge_file_cb_arg *a = arg;
1904 struct got_fileindex_entry *ie;
1905 char *ondisk_path = NULL;
1906 struct stat sb;
1907 unsigned char status;
1908 int local_changes_subsumed;
1910 if (blob1 && blob2) {
1911 ie = got_fileindex_entry_get(a->fileindex, path2);
1912 if (ie == NULL)
1913 return (*a->progress_cb)(a->progress_arg,
1914 GOT_STATUS_MISSING, path2);
1916 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1917 path2) == -1)
1918 return got_error_from_errno("asprintf");
1920 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1921 if (err)
1922 goto done;
1924 if (status == GOT_STATUS_DELETE) {
1925 err = (*a->progress_cb)(a->progress_arg,
1926 GOT_STATUS_MERGE, path2);
1927 goto done;
1929 if (status != GOT_STATUS_NO_CHANGE &&
1930 status != GOT_STATUS_MODIFY &&
1931 status != GOT_STATUS_CONFLICT &&
1932 status != GOT_STATUS_ADD) {
1933 err = (*a->progress_cb)(a->progress_arg, status, path2);
1934 goto done;
1937 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1938 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1939 a->progress_cb, a->progress_arg);
1940 } else if (blob1) {
1941 ie = got_fileindex_entry_get(a->fileindex, path1);
1942 if (ie == NULL)
1943 return (*a->progress_cb)(a->progress_arg,
1944 GOT_STATUS_MISSING, path2);
1946 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1947 path1) == -1)
1948 return got_error_from_errno("asprintf");
1950 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1951 if (err)
1952 goto done;
1954 switch (status) {
1955 case GOT_STATUS_NO_CHANGE:
1956 err = (*a->progress_cb)(a->progress_arg,
1957 GOT_STATUS_DELETE, path1);
1958 if (err)
1959 goto done;
1960 err = remove_ondisk_file(a->worktree->root_path, path1);
1961 if (err)
1962 goto done;
1963 if (ie)
1964 got_fileindex_entry_mark_deleted_from_disk(ie);
1965 break;
1966 case GOT_STATUS_DELETE:
1967 case GOT_STATUS_MISSING:
1968 err = (*a->progress_cb)(a->progress_arg,
1969 GOT_STATUS_DELETE, path1);
1970 if (err)
1971 goto done;
1972 if (ie)
1973 got_fileindex_entry_mark_deleted_from_disk(ie);
1974 break;
1975 case GOT_STATUS_ADD:
1976 case GOT_STATUS_MODIFY:
1977 case GOT_STATUS_CONFLICT:
1978 err = (*a->progress_cb)(a->progress_arg,
1979 GOT_STATUS_CANNOT_DELETE, path1);
1980 if (err)
1981 goto done;
1982 break;
1983 case GOT_STATUS_OBSTRUCTED:
1984 err = (*a->progress_cb)(a->progress_arg, status, path1);
1985 if (err)
1986 goto done;
1987 break;
1988 default:
1989 break;
1991 } else if (blob2) {
1992 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1993 path2) == -1)
1994 return got_error_from_errno("asprintf");
1995 ie = got_fileindex_entry_get(a->fileindex, path2);
1996 if (ie) {
1997 err = get_file_status(&status, &sb, ie, ondisk_path,
1998 repo);
1999 if (err)
2000 goto done;
2001 if (status != GOT_STATUS_NO_CHANGE &&
2002 status != GOT_STATUS_MODIFY &&
2003 status != GOT_STATUS_CONFLICT &&
2004 status != GOT_STATUS_ADD) {
2005 err = (*a->progress_cb)(a->progress_arg,
2006 status, path2);
2007 goto done;
2009 err = merge_blob(&local_changes_subsumed, a->worktree,
2010 NULL, ondisk_path, path2, sb.st_mode, blob2,
2011 a->commit_id2, repo,
2012 a->progress_cb, a->progress_arg);
2013 if (status == GOT_STATUS_DELETE) {
2014 err = update_blob_fileindex_entry(a->worktree,
2015 a->fileindex, ie, ondisk_path, ie->path,
2016 blob2, 0);
2017 if (err)
2018 goto done;
2020 } else {
2021 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2022 err = install_blob(a->worktree, ondisk_path, path2,
2023 /* XXX get this from parent tree! */
2024 GOT_DEFAULT_FILE_MODE,
2025 sb.st_mode, blob2, 0, 0, repo,
2026 a->progress_cb, a->progress_arg);
2027 if (err)
2028 goto done;
2029 err = got_fileindex_entry_alloc(&ie,
2030 ondisk_path, path2, NULL, NULL);
2031 if (err)
2032 goto done;
2033 err = got_fileindex_entry_add(a->fileindex, ie);
2034 if (err) {
2035 got_fileindex_entry_free(ie);
2036 goto done;
2040 done:
2041 free(ondisk_path);
2042 return err;
2045 struct check_merge_ok_arg {
2046 struct got_worktree *worktree;
2047 struct got_repository *repo;
2050 static const struct got_error *
2051 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2053 const struct got_error *err = NULL;
2054 struct check_merge_ok_arg *a = arg;
2055 unsigned char status;
2056 struct stat sb;
2057 char *ondisk_path;
2059 /* Reject merges into a work tree with mixed base commits. */
2060 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2061 SHA1_DIGEST_LENGTH))
2062 return got_error(GOT_ERR_MIXED_COMMITS);
2064 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2065 == -1)
2066 return got_error_from_errno("asprintf");
2068 /* Reject merges into a work tree with conflicted files. */
2069 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2070 if (err)
2071 return err;
2072 if (status == GOT_STATUS_CONFLICT)
2073 return got_error(GOT_ERR_CONFLICTS);
2075 return NULL;
2078 static const struct got_error *
2079 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2080 const char *fileindex_path, struct got_object_id *commit_id1,
2081 struct got_object_id *commit_id2, struct got_repository *repo,
2082 got_worktree_checkout_cb progress_cb, void *progress_arg,
2083 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2085 const struct got_error *err = NULL, *sync_err;
2086 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2087 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2088 struct merge_file_cb_arg arg;
2090 if (commit_id1) {
2091 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2092 worktree->path_prefix);
2093 if (err)
2094 goto done;
2096 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2097 if (err)
2098 goto done;
2101 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2102 worktree->path_prefix);
2103 if (err)
2104 goto done;
2106 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2107 if (err)
2108 goto done;
2110 arg.worktree = worktree;
2111 arg.fileindex = fileindex;
2112 arg.progress_cb = progress_cb;
2113 arg.progress_arg = progress_arg;
2114 arg.cancel_cb = cancel_cb;
2115 arg.cancel_arg = cancel_arg;
2116 arg.commit_id2 = commit_id2;
2117 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2118 sync_err = sync_fileindex(fileindex, fileindex_path);
2119 if (sync_err && err == NULL)
2120 err = sync_err;
2121 done:
2122 if (tree1)
2123 got_object_tree_close(tree1);
2124 if (tree2)
2125 got_object_tree_close(tree2);
2126 return err;
2129 const struct got_error *
2130 got_worktree_merge_files(struct got_worktree *worktree,
2131 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2132 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2133 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2135 const struct got_error *err, *unlockerr;
2136 char *fileindex_path = NULL;
2137 struct got_fileindex *fileindex = NULL;
2138 struct check_merge_ok_arg mok_arg;
2140 err = lock_worktree(worktree, LOCK_EX);
2141 if (err)
2142 return err;
2144 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2145 if (err)
2146 goto done;
2148 mok_arg.worktree = worktree;
2149 mok_arg.repo = repo;
2150 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2151 &mok_arg);
2152 if (err)
2153 goto done;
2155 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2156 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2157 done:
2158 if (fileindex)
2159 got_fileindex_free(fileindex);
2160 free(fileindex_path);
2161 unlockerr = lock_worktree(worktree, LOCK_SH);
2162 if (unlockerr && err == NULL)
2163 err = unlockerr;
2164 return err;
2167 struct diff_dir_cb_arg {
2168 struct got_fileindex *fileindex;
2169 struct got_worktree *worktree;
2170 const char *status_path;
2171 size_t status_path_len;
2172 struct got_repository *repo;
2173 got_worktree_status_cb status_cb;
2174 void *status_arg;
2175 got_worktree_cancel_cb cancel_cb;
2176 void *cancel_arg;
2179 static const struct got_error *
2180 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2181 got_worktree_status_cb status_cb, void *status_arg,
2182 struct got_repository *repo)
2184 const struct got_error *err = NULL;
2185 unsigned char status = GOT_STATUS_NO_CHANGE;
2186 struct stat sb;
2187 struct got_object_id blob_id, commit_id;
2189 err = get_file_status(&status, &sb, ie, abspath, repo);
2190 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2191 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2192 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2193 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2194 &commit_id);
2196 return err;
2199 static const struct got_error *
2200 status_old_new(void *arg, struct got_fileindex_entry *ie,
2201 struct dirent *de, const char *parent_path)
2203 const struct got_error *err = NULL;
2204 struct diff_dir_cb_arg *a = arg;
2205 char *abspath;
2207 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2208 return got_error(GOT_ERR_CANCELLED);
2210 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2211 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2212 return NULL;
2214 if (parent_path[0]) {
2215 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2216 parent_path, de->d_name) == -1)
2217 return got_error_from_errno("asprintf");
2218 } else {
2219 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2220 de->d_name) == -1)
2221 return got_error_from_errno("asprintf");
2224 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2225 a->repo);
2226 free(abspath);
2227 return err;
2230 static const struct got_error *
2231 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2233 struct diff_dir_cb_arg *a = arg;
2234 struct got_object_id blob_id, commit_id;
2235 unsigned char status;
2237 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2238 return got_error(GOT_ERR_CANCELLED);
2240 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2241 return NULL;
2243 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2244 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2245 if (got_fileindex_entry_has_file_on_disk(ie))
2246 status = GOT_STATUS_MISSING;
2247 else
2248 status = GOT_STATUS_DELETE;
2249 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2250 &commit_id);
2253 static const struct got_error *
2254 status_new(void *arg, struct dirent *de, const char *parent_path)
2256 const struct got_error *err = NULL;
2257 struct diff_dir_cb_arg *a = arg;
2258 char *path = NULL;
2260 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2261 return got_error(GOT_ERR_CANCELLED);
2263 if (de->d_type == DT_DIR)
2264 return NULL;
2266 /* XXX ignore symlinks for now */
2267 if (de->d_type == DT_LNK)
2268 return NULL;
2270 if (parent_path[0]) {
2271 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2272 return got_error_from_errno("asprintf");
2273 } else {
2274 path = de->d_name;
2277 if (got_path_is_child(path, a->status_path, a->status_path_len))
2278 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2279 path, NULL, NULL);
2280 if (parent_path[0])
2281 free(path);
2282 return err;
2285 static const struct got_error *
2286 report_single_file_status(const char *path, const char *ondisk_path,
2287 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2288 void *status_arg, struct got_repository *repo)
2290 struct got_fileindex_entry *ie;
2291 struct stat sb;
2293 ie = got_fileindex_entry_get(fileindex, path);
2294 if (ie)
2295 return report_file_status(ie, ondisk_path, status_cb,
2296 status_arg, repo);
2298 if (lstat(ondisk_path, &sb) == -1) {
2299 if (errno != ENOENT)
2300 return got_error_from_errno2("lstat", ondisk_path);
2301 return NULL;
2304 if (S_ISREG(sb.st_mode))
2305 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED, path,
2306 NULL, NULL);
2308 return NULL;
2311 static const struct got_error *
2312 worktree_status(struct got_worktree *worktree, const char *path,
2313 struct got_fileindex *fileindex, struct got_repository *repo,
2314 got_worktree_status_cb status_cb, void *status_arg,
2315 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2317 const struct got_error *err = NULL;
2318 DIR *workdir = NULL;
2319 struct got_fileindex_diff_dir_cb fdiff_cb;
2320 struct diff_dir_cb_arg arg;
2321 char *ondisk_path = NULL;
2323 if (asprintf(&ondisk_path, "%s%s%s",
2324 worktree->root_path, path[0] ? "/" : "", path) == -1)
2325 return got_error_from_errno("asprintf");
2327 workdir = opendir(ondisk_path);
2328 if (workdir == NULL) {
2329 if (errno != ENOTDIR && errno != ENOENT)
2330 err = got_error_from_errno2("opendir", ondisk_path);
2331 else
2332 err = report_single_file_status(path, ondisk_path,
2333 fileindex, status_cb, status_arg, repo);
2334 } else {
2335 fdiff_cb.diff_old_new = status_old_new;
2336 fdiff_cb.diff_old = status_old;
2337 fdiff_cb.diff_new = status_new;
2338 arg.fileindex = fileindex;
2339 arg.worktree = worktree;
2340 arg.status_path = path;
2341 arg.status_path_len = strlen(path);
2342 arg.repo = repo;
2343 arg.status_cb = status_cb;
2344 arg.status_arg = status_arg;
2345 arg.cancel_cb = cancel_cb;
2346 arg.cancel_arg = cancel_arg;
2347 err = got_fileindex_diff_dir(fileindex, workdir,
2348 worktree->root_path, path, repo, &fdiff_cb, &arg);
2351 if (workdir)
2352 closedir(workdir);
2353 free(ondisk_path);
2354 return err;
2357 const struct got_error *
2358 got_worktree_status(struct got_worktree *worktree,
2359 struct got_pathlist_head *paths, struct got_repository *repo,
2360 got_worktree_status_cb status_cb, void *status_arg,
2361 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2363 const struct got_error *err = NULL;
2364 char *fileindex_path = NULL;
2365 struct got_fileindex *fileindex = NULL;
2366 struct got_pathlist_entry *pe;
2368 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2369 if (err)
2370 return err;
2372 TAILQ_FOREACH(pe, paths, entry) {
2373 err = worktree_status(worktree, pe->path, fileindex, repo,
2374 status_cb, status_arg, cancel_cb, cancel_arg);
2375 if (err)
2376 break;
2378 free(fileindex_path);
2379 got_fileindex_free(fileindex);
2380 return err;
2383 const struct got_error *
2384 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2385 const char *arg)
2387 const struct got_error *err = NULL;
2388 char *resolved, *cwd = NULL, *path = NULL;
2389 size_t len;
2391 *wt_path = NULL;
2393 resolved = realpath(arg, NULL);
2394 if (resolved == NULL) {
2395 if (errno != ENOENT)
2396 return got_error_from_errno2("realpath", arg);
2397 cwd = getcwd(NULL, 0);
2398 if (cwd == NULL)
2399 return got_error_from_errno("getcwd");
2400 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2401 err = got_error_from_errno("asprintf");
2402 goto done;
2406 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2407 strlen(got_worktree_get_root_path(worktree)))) {
2408 err = got_error(GOT_ERR_BAD_PATH);
2409 goto done;
2412 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2413 err = got_path_skip_common_ancestor(&path,
2414 got_worktree_get_root_path(worktree), resolved);
2415 if (err)
2416 goto done;
2417 } else {
2418 path = strdup("");
2419 if (path == NULL) {
2420 err = got_error_from_errno("strdup");
2421 goto done;
2425 /* XXX status walk can't deal with trailing slash! */
2426 len = strlen(path);
2427 while (len > 0 && path[len - 1] == '/') {
2428 path[len - 1] = '\0';
2429 len--;
2431 done:
2432 free(resolved);
2433 free(cwd);
2434 if (err == NULL)
2435 *wt_path = path;
2436 else
2437 free(path);
2438 return err;
2441 static const struct got_error *
2442 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2443 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2444 struct got_repository *repo)
2446 const struct got_error *err = NULL;
2447 struct got_fileindex_entry *ie;
2449 /* Re-adding an existing entry is a no-op. */
2450 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2451 return NULL;
2453 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2454 if (err)
2455 return err;
2457 err = got_fileindex_entry_add(fileindex, ie);
2458 if (err) {
2459 got_fileindex_entry_free(ie);
2460 return err;
2463 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2466 const struct got_error *
2467 got_worktree_schedule_add(struct got_worktree *worktree,
2468 struct got_pathlist_head *ondisk_paths,
2469 got_worktree_status_cb status_cb, void *status_arg,
2470 struct got_repository *repo)
2472 struct got_fileindex *fileindex = NULL;
2473 char *fileindex_path = NULL;
2474 const struct got_error *err = NULL, *sync_err, *unlockerr;
2475 struct got_pathlist_entry *pe;
2477 err = lock_worktree(worktree, LOCK_EX);
2478 if (err)
2479 return err;
2481 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2482 if (err)
2483 goto done;
2485 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2486 char *relpath;
2487 err = got_path_skip_common_ancestor(&relpath,
2488 got_worktree_get_root_path(worktree), pe->path);
2489 if (err)
2490 break;
2491 err = schedule_addition(pe->path, fileindex, relpath,
2492 status_cb, status_arg, repo);
2493 free(relpath);
2494 if (err)
2495 break;
2497 sync_err = sync_fileindex(fileindex, fileindex_path);
2498 if (sync_err && err == NULL)
2499 err = sync_err;
2500 done:
2501 free(fileindex_path);
2502 if (fileindex)
2503 got_fileindex_free(fileindex);
2504 unlockerr = lock_worktree(worktree, LOCK_SH);
2505 if (unlockerr && err == NULL)
2506 err = unlockerr;
2507 return err;
2510 static const struct got_error *
2511 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2512 const char *relpath, int delete_local_mods,
2513 got_worktree_status_cb status_cb, void *status_arg,
2514 struct got_repository *repo)
2516 const struct got_error *err = NULL;
2517 struct got_fileindex_entry *ie = NULL;
2518 unsigned char status;
2519 struct stat sb;
2521 ie = got_fileindex_entry_get(fileindex, relpath);
2522 if (ie == NULL)
2523 return got_error(GOT_ERR_BAD_PATH);
2525 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2526 if (err)
2527 return err;
2529 if (status != GOT_STATUS_NO_CHANGE) {
2530 if (status == GOT_STATUS_DELETE)
2531 return got_error_set_errno(ENOENT, ondisk_path);
2532 if (status != GOT_STATUS_MODIFY)
2533 return got_error(GOT_ERR_FILE_STATUS);
2534 if (!delete_local_mods)
2535 return got_error(GOT_ERR_FILE_MODIFIED);
2538 if (unlink(ondisk_path) != 0)
2539 return got_error_from_errno2("unlink", ondisk_path);
2541 got_fileindex_entry_mark_deleted_from_disk(ie);
2542 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2545 const struct got_error *
2546 got_worktree_schedule_delete(struct got_worktree *worktree,
2547 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2548 got_worktree_status_cb status_cb, void *status_arg,
2549 struct got_repository *repo)
2551 struct got_fileindex *fileindex = NULL;
2552 char *fileindex_path = NULL;
2553 const struct got_error *err = NULL, *sync_err, *unlockerr;
2554 struct got_pathlist_entry *pe;
2556 err = lock_worktree(worktree, LOCK_EX);
2557 if (err)
2558 return err;
2560 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2561 if (err)
2562 goto done;
2564 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2565 char *relpath;
2566 err = got_path_skip_common_ancestor(&relpath,
2567 got_worktree_get_root_path(worktree), pe->path);
2568 if (err)
2569 break;
2570 err = schedule_for_deletion(pe->path, fileindex, relpath,
2571 delete_local_mods, status_cb, status_arg, repo);
2572 free(relpath);
2573 if (err)
2574 break;
2576 sync_err = sync_fileindex(fileindex, fileindex_path);
2577 if (sync_err && err == NULL)
2578 err = sync_err;
2579 done:
2580 free(fileindex_path);
2581 if (fileindex)
2582 got_fileindex_free(fileindex);
2583 unlockerr = lock_worktree(worktree, LOCK_SH);
2584 if (unlockerr && err == NULL)
2585 err = unlockerr;
2586 return err;
2589 static const struct got_error *
2590 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2591 const char *ondisk_path,
2592 got_worktree_checkout_cb progress_cb, void *progress_arg,
2593 struct got_repository *repo)
2595 const struct got_error *err = NULL;
2596 char *relpath = NULL, *parent_path = NULL;
2597 struct got_fileindex_entry *ie;
2598 struct got_tree_object *tree = NULL;
2599 struct got_object_id *tree_id = NULL;
2600 const struct got_tree_entry *te;
2601 char *tree_path = NULL, *te_name;
2602 struct got_blob_object *blob = NULL;
2603 unsigned char status;
2604 struct stat sb;
2606 err = got_path_skip_common_ancestor(&relpath,
2607 got_worktree_get_root_path(worktree), ondisk_path);
2608 if (err)
2609 goto done;
2611 ie = got_fileindex_entry_get(fileindex, relpath);
2612 if (ie == NULL) {
2613 err = got_error(GOT_ERR_BAD_PATH);
2614 goto done;
2617 /* Construct in-repository path of tree which contains this blob. */
2618 err = got_path_dirname(&parent_path, ie->path);
2619 if (err) {
2620 if (err->code != GOT_ERR_BAD_PATH)
2621 goto done;
2622 parent_path = strdup("/");
2623 if (parent_path == NULL) {
2624 err = got_error_from_errno("strdup");
2625 goto done;
2628 if (got_path_is_root_dir(worktree->path_prefix)) {
2629 tree_path = strdup(parent_path);
2630 if (tree_path == NULL) {
2631 err = got_error_from_errno("strdup");
2632 goto done;
2634 } else {
2635 if (got_path_is_root_dir(parent_path)) {
2636 tree_path = strdup(worktree->path_prefix);
2637 if (tree_path == NULL) {
2638 err = got_error_from_errno("strdup");
2639 goto done;
2641 } else {
2642 if (asprintf(&tree_path, "%s/%s",
2643 worktree->path_prefix, parent_path) == -1) {
2644 err = got_error_from_errno("asprintf");
2645 goto done;
2650 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2651 if (err)
2652 goto done;
2654 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2655 tree_path);
2656 if (err) {
2657 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2658 status == GOT_STATUS_ADD))
2659 goto done;
2660 } else {
2661 err = got_object_open_as_tree(&tree, repo, tree_id);
2662 if (err)
2663 goto done;
2665 te_name = basename(ie->path);
2666 if (te_name == NULL) {
2667 err = got_error_from_errno2("basename", ie->path);
2668 goto done;
2671 te = got_object_tree_find_entry(tree, te_name);
2672 if (te == NULL && status != GOT_STATUS_ADD) {
2673 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2674 goto done;
2678 switch (status) {
2679 case GOT_STATUS_ADD:
2680 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2681 if (err)
2682 goto done;
2683 got_fileindex_entry_remove(fileindex, ie);
2684 break;
2685 case GOT_STATUS_DELETE:
2686 case GOT_STATUS_MODIFY:
2687 case GOT_STATUS_CONFLICT:
2688 case GOT_STATUS_MISSING: {
2689 struct got_object_id id;
2690 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2691 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2692 if (err)
2693 goto done;
2694 err = install_blob(worktree, ondisk_path, ie->path,
2695 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2696 progress_arg);
2697 if (err)
2698 goto done;
2699 if (status == GOT_STATUS_DELETE) {
2700 err = update_blob_fileindex_entry(worktree,
2701 fileindex, ie, ondisk_path, ie->path, blob, 1);
2702 if (err)
2703 goto done;
2705 break;
2707 default:
2708 goto done;
2710 done:
2711 free(relpath);
2712 free(parent_path);
2713 free(tree_path);
2714 if (blob)
2715 got_object_blob_close(blob);
2716 if (tree)
2717 got_object_tree_close(tree);
2718 free(tree_id);
2719 return err;
2722 const struct got_error *
2723 got_worktree_revert(struct got_worktree *worktree,
2724 struct got_pathlist_head *ondisk_paths,
2725 got_worktree_checkout_cb progress_cb, void *progress_arg,
2726 struct got_repository *repo)
2728 struct got_fileindex *fileindex = NULL;
2729 char *fileindex_path = NULL;
2730 const struct got_error *err = NULL, *unlockerr = NULL;
2731 const struct got_error *sync_err = NULL;
2732 struct got_pathlist_entry *pe;
2734 err = lock_worktree(worktree, LOCK_EX);
2735 if (err)
2736 return err;
2738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2739 if (err)
2740 goto done;
2742 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2743 err = revert_file(worktree, fileindex, pe->path,
2744 progress_cb, progress_arg, repo);
2745 if (err)
2746 break;
2748 sync_err = sync_fileindex(fileindex, fileindex_path);
2749 if (sync_err && err == NULL)
2750 err = sync_err;
2751 done:
2752 free(fileindex_path);
2753 if (fileindex)
2754 got_fileindex_free(fileindex);
2755 unlockerr = lock_worktree(worktree, LOCK_SH);
2756 if (unlockerr && err == NULL)
2757 err = unlockerr;
2758 return err;
2761 static void
2762 free_commitable(struct got_commitable *ct)
2764 free(ct->path);
2765 free(ct->in_repo_path);
2766 free(ct->ondisk_path);
2767 free(ct->blob_id);
2768 free(ct->base_blob_id);
2769 free(ct->base_commit_id);
2770 free(ct);
2773 struct collect_commitables_arg {
2774 struct got_pathlist_head *commitable_paths;
2775 struct got_repository *repo;
2776 struct got_worktree *worktree;
2779 static const struct got_error *
2780 collect_commitables(void *arg, unsigned char status, const char *relpath,
2781 struct got_object_id *blob_id, struct got_object_id *commit_id)
2783 struct collect_commitables_arg *a = arg;
2784 const struct got_error *err = NULL;
2785 struct got_commitable *ct = NULL;
2786 struct got_pathlist_entry *new = NULL;
2787 char *parent_path = NULL, *path = NULL;
2788 struct stat sb;
2790 if (status == GOT_STATUS_CONFLICT)
2791 return got_error(GOT_ERR_COMMIT_CONFLICT);
2793 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2794 status != GOT_STATUS_DELETE)
2795 return NULL;
2797 if (asprintf(&path, "/%s", relpath) == -1) {
2798 err = got_error_from_errno("asprintf");
2799 goto done;
2801 if (strcmp(path, "/") == 0) {
2802 parent_path = strdup("");
2803 if (parent_path == NULL)
2804 return got_error_from_errno("strdup");
2805 } else {
2806 err = got_path_dirname(&parent_path, path);
2807 if (err)
2808 return err;
2811 ct = calloc(1, sizeof(*ct));
2812 if (ct == NULL) {
2813 err = got_error_from_errno("calloc");
2814 goto done;
2817 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2818 relpath) == -1) {
2819 err = got_error_from_errno("asprintf");
2820 goto done;
2822 if (status == GOT_STATUS_DELETE) {
2823 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2824 } else {
2825 if (lstat(ct->ondisk_path, &sb) != 0) {
2826 err = got_error_from_errno2("lstat", ct->ondisk_path);
2827 goto done;
2829 ct->mode = sb.st_mode;
2832 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2833 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2834 relpath) == -1) {
2835 err = got_error_from_errno("asprintf");
2836 goto done;
2839 ct->status = status;
2840 ct->blob_id = NULL; /* will be filled in when blob gets created */
2841 if (ct->status != GOT_STATUS_ADD) {
2842 ct->base_blob_id = got_object_id_dup(blob_id);
2843 if (ct->base_blob_id == NULL) {
2844 err = got_error_from_errno("got_object_id_dup");
2845 goto done;
2847 ct->base_commit_id = got_object_id_dup(commit_id);
2848 if (ct->base_commit_id == NULL) {
2849 err = got_error_from_errno("got_object_id_dup");
2850 goto done;
2853 ct->path = strdup(path);
2854 if (ct->path == NULL) {
2855 err = got_error_from_errno("strdup");
2856 goto done;
2858 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2859 done:
2860 if (ct && (err || new == NULL))
2861 free_commitable(ct);
2862 free(parent_path);
2863 free(path);
2864 return err;
2867 static const struct got_error *write_tree(struct got_object_id **,
2868 struct got_tree_object *, const char *, struct got_pathlist_head *,
2869 got_worktree_status_cb status_cb, void *status_arg,
2870 struct got_repository *);
2872 static const struct got_error *
2873 write_subtree(struct got_object_id **new_subtree_id,
2874 struct got_tree_entry *te, const char *parent_path,
2875 struct got_pathlist_head *commitable_paths,
2876 got_worktree_status_cb status_cb, void *status_arg,
2877 struct got_repository *repo)
2879 const struct got_error *err = NULL;
2880 struct got_tree_object *subtree;
2881 char *subpath;
2883 if (asprintf(&subpath, "%s%s%s", parent_path,
2884 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2885 return got_error_from_errno("asprintf");
2887 err = got_object_open_as_tree(&subtree, repo, te->id);
2888 if (err)
2889 return err;
2891 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2892 status_cb, status_arg, repo);
2893 got_object_tree_close(subtree);
2894 free(subpath);
2895 return err;
2898 static const struct got_error *
2899 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2901 const struct got_error *err = NULL;
2902 char *ct_parent_path = NULL;
2904 *match = 0;
2906 if (strchr(ct->in_repo_path, '/') == NULL) {
2907 *match = got_path_is_root_dir(path);
2908 return NULL;
2911 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2912 if (err)
2913 return err;
2914 *match = (strcmp(path, ct_parent_path) == 0);
2915 free(ct_parent_path);
2916 return err;
2919 static mode_t
2920 get_ct_file_mode(struct got_commitable *ct)
2922 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2925 static const struct got_error *
2926 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2927 struct got_tree_entry *te, struct got_commitable *ct)
2929 const struct got_error *err = NULL;
2931 *new_te = NULL;
2933 err = got_object_tree_entry_dup(new_te, te);
2934 if (err)
2935 goto done;
2937 (*new_te)->mode = get_ct_file_mode(ct);
2939 free((*new_te)->id);
2940 (*new_te)->id = got_object_id_dup(ct->blob_id);
2941 if ((*new_te)->id == NULL) {
2942 err = got_error_from_errno("got_object_id_dup");
2943 goto done;
2945 done:
2946 if (err && *new_te) {
2947 got_object_tree_entry_close(*new_te);
2948 *new_te = NULL;
2950 return err;
2953 static const struct got_error *
2954 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2955 struct got_commitable *ct)
2957 const struct got_error *err = NULL;
2958 char *ct_name;
2960 *new_te = NULL;
2962 *new_te = calloc(1, sizeof(**new_te));
2963 if (*new_te == NULL)
2964 return got_error_from_errno("calloc");
2966 ct_name = basename(ct->path);
2967 if (ct_name == NULL) {
2968 err = got_error_from_errno2("basename", ct->path);
2969 goto done;
2971 (*new_te)->name = strdup(ct_name);
2972 if ((*new_te)->name == NULL) {
2973 err = got_error_from_errno("strdup");
2974 goto done;
2977 (*new_te)->mode = get_ct_file_mode(ct);
2979 (*new_te)->id = got_object_id_dup(ct->blob_id);
2980 if ((*new_te)->id == NULL) {
2981 err = got_error_from_errno("got_object_id_dup");
2982 goto done;
2984 done:
2985 if (err && *new_te) {
2986 got_object_tree_entry_close(*new_te);
2987 *new_te = NULL;
2989 return err;
2992 static const struct got_error *
2993 insert_tree_entry(struct got_tree_entry *new_te,
2994 struct got_pathlist_head *paths)
2996 const struct got_error *err = NULL;
2997 struct got_pathlist_entry *new_pe;
2999 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3000 if (err)
3001 return err;
3002 if (new_pe == NULL)
3003 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3004 return NULL;
3007 static const struct got_error *
3008 report_ct_status(struct got_commitable *ct,
3009 got_worktree_status_cb status_cb, void *status_arg)
3011 const char *ct_path = ct->path;
3012 while (ct_path[0] == '/')
3013 ct_path++;
3014 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
3017 static const struct got_error *
3018 match_modified_subtree(int *modified, struct got_tree_entry *te,
3019 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3021 const struct got_error *err = NULL;
3022 struct got_pathlist_entry *pe;
3023 char *te_path;
3025 *modified = 0;
3027 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3028 got_path_is_root_dir(base_tree_path) ? "" : "/",
3029 te->name) == -1)
3030 return got_error_from_errno("asprintf");
3032 TAILQ_FOREACH(pe, commitable_paths, entry) {
3033 struct got_commitable *ct = pe->data;
3034 *modified = got_path_is_child(ct->in_repo_path, te_path,
3035 strlen(te_path));
3036 if (*modified)
3037 break;
3040 free(te_path);
3041 return err;
3044 static const struct got_error *
3045 match_deleted_or_modified_ct(struct got_commitable **ctp,
3046 struct got_tree_entry *te, const char *base_tree_path,
3047 struct got_pathlist_head *commitable_paths)
3049 const struct got_error *err = NULL;
3050 struct got_pathlist_entry *pe;
3052 *ctp = NULL;
3054 TAILQ_FOREACH(pe, commitable_paths, entry) {
3055 struct got_commitable *ct = pe->data;
3056 char *ct_name = NULL;
3057 int path_matches;
3059 if (ct->status != GOT_STATUS_MODIFY &&
3060 ct->status != GOT_STATUS_DELETE)
3061 continue;
3063 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3064 continue;
3066 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3067 if (err)
3068 return err;
3069 if (!path_matches)
3070 continue;
3072 ct_name = basename(pe->path);
3073 if (ct_name == NULL)
3074 return got_error_from_errno2("basename", pe->path);
3076 if (strcmp(te->name, ct_name) != 0)
3077 continue;
3079 *ctp = ct;
3080 break;
3083 return err;
3086 static const struct got_error *
3087 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3088 const char *child_path, const char *path_base_tree,
3089 struct got_pathlist_head *commitable_paths,
3090 got_worktree_status_cb status_cb, void *status_arg,
3091 struct got_repository *repo)
3093 const struct got_error *err = NULL;
3094 struct got_tree_entry *new_te;
3095 char *subtree_path;
3097 *new_tep = NULL;
3099 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3100 got_path_is_root_dir(path_base_tree) ? "" : "/",
3101 child_path) == -1)
3102 return got_error_from_errno("asprintf");
3104 new_te = calloc(1, sizeof(*new_te));
3105 new_te->mode = S_IFDIR;
3106 new_te->name = strdup(child_path);
3107 if (new_te->name == NULL) {
3108 err = got_error_from_errno("strdup");
3109 got_object_tree_entry_close(new_te);
3110 goto done;
3112 err = write_tree(&new_te->id, NULL, subtree_path,
3113 commitable_paths, status_cb, status_arg, repo);
3114 if (err) {
3115 got_object_tree_entry_close(new_te);
3116 goto done;
3118 done:
3119 free(subtree_path);
3120 if (err == NULL)
3121 *new_tep = new_te;
3122 return err;
3125 static const struct got_error *
3126 write_tree(struct got_object_id **new_tree_id,
3127 struct got_tree_object *base_tree, const char *path_base_tree,
3128 struct got_pathlist_head *commitable_paths,
3129 got_worktree_status_cb status_cb, void *status_arg,
3130 struct got_repository *repo)
3132 const struct got_error *err = NULL;
3133 const struct got_tree_entries *base_entries = NULL;
3134 struct got_pathlist_head paths;
3135 struct got_tree_entries new_tree_entries;
3136 struct got_tree_entry *te, *new_te = NULL;
3137 struct got_pathlist_entry *pe;
3139 TAILQ_INIT(&paths);
3140 new_tree_entries.nentries = 0;
3141 SIMPLEQ_INIT(&new_tree_entries.head);
3143 /* Insert, and recurse into, newly added entries first. */
3144 TAILQ_FOREACH(pe, commitable_paths, entry) {
3145 struct got_commitable *ct = pe->data;
3146 char *child_path = NULL, *slash;
3148 if (ct->status != GOT_STATUS_ADD ||
3149 (ct->flags & GOT_COMMITABLE_ADDED))
3150 continue;
3152 if (!got_path_is_child(pe->path, path_base_tree,
3153 strlen(path_base_tree)))
3154 continue;
3156 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3157 pe->path);
3158 if (err)
3159 goto done;
3161 slash = strchr(child_path, '/');
3162 if (slash == NULL) {
3163 err = alloc_added_blob_tree_entry(&new_te, ct);
3164 if (err)
3165 goto done;
3166 err = report_ct_status(ct, status_cb, status_arg);
3167 if (err)
3168 goto done;
3169 ct->flags |= GOT_COMMITABLE_ADDED;
3170 err = insert_tree_entry(new_te, &paths);
3171 if (err)
3172 goto done;
3173 } else {
3174 *slash = '\0'; /* trim trailing path components */
3175 if (base_tree == NULL ||
3176 got_object_tree_find_entry(base_tree, child_path)
3177 == NULL) {
3178 err = make_subtree_for_added_blob(&new_te,
3179 child_path, path_base_tree,
3180 commitable_paths, status_cb, status_arg,
3181 repo);
3182 if (err)
3183 goto done;
3184 err = insert_tree_entry(new_te, &paths);
3185 if (err)
3186 goto done;
3191 if (base_tree) {
3192 /* Handle modified and deleted entries. */
3193 base_entries = got_object_tree_get_entries(base_tree);
3194 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3195 struct got_commitable *ct = NULL;
3197 if (S_ISDIR(te->mode)) {
3198 int modified;
3199 err = got_object_tree_entry_dup(&new_te, te);
3200 if (err)
3201 goto done;
3202 err = match_modified_subtree(&modified, te,
3203 path_base_tree, commitable_paths);
3204 if (err)
3205 goto done;
3206 /* Avoid recursion into unmodified subtrees. */
3207 if (modified) {
3208 free(new_te->id);
3209 err = write_subtree(&new_te->id, te,
3210 path_base_tree, commitable_paths,
3211 status_cb, status_arg, repo);
3212 if (err)
3213 goto done;
3215 err = insert_tree_entry(new_te, &paths);
3216 if (err)
3217 goto done;
3218 continue;
3221 err = match_deleted_or_modified_ct(&ct, te,
3222 path_base_tree, commitable_paths);
3223 if (ct) {
3224 /* NB: Deleted entries get dropped here. */
3225 if (ct->status == GOT_STATUS_MODIFY) {
3226 err = alloc_modified_blob_tree_entry(
3227 &new_te, te, ct);
3228 if (err)
3229 goto done;
3230 err = insert_tree_entry(new_te, &paths);
3231 if (err)
3232 goto done;
3234 err = report_ct_status(ct, status_cb,
3235 status_arg);
3236 if (err)
3237 goto done;
3238 } else {
3239 /* Entry is unchanged; just copy it. */
3240 err = got_object_tree_entry_dup(&new_te, te);
3241 if (err)
3242 goto done;
3243 err = insert_tree_entry(new_te, &paths);
3244 if (err)
3245 goto done;
3250 /* Write new list of entries; deleted entries have been dropped. */
3251 TAILQ_FOREACH(pe, &paths, entry) {
3252 struct got_tree_entry *te = pe->data;
3253 new_tree_entries.nentries++;
3254 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3256 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3257 done:
3258 got_object_tree_entries_close(&new_tree_entries);
3259 got_pathlist_free(&paths);
3260 return err;
3263 static const struct got_error *
3264 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3265 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3267 const struct got_error *err = NULL;
3268 struct got_pathlist_entry *pe;
3270 TAILQ_FOREACH(pe, commitable_paths, entry) {
3271 struct got_fileindex_entry *ie;
3272 struct got_commitable *ct = pe->data;
3274 ie = got_fileindex_entry_get(fileindex, pe->path);
3275 if (ie) {
3276 if (ct->status == GOT_STATUS_DELETE) {
3277 got_fileindex_entry_remove(fileindex, ie);
3278 got_fileindex_entry_free(ie);
3279 } else
3280 err = got_fileindex_entry_update(ie,
3281 ct->ondisk_path, ct->blob_id->sha1,
3282 new_base_commit_id->sha1, 1);
3283 } else {
3284 err = got_fileindex_entry_alloc(&ie,
3285 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3286 new_base_commit_id->sha1);
3287 if (err)
3288 break;
3289 err = got_fileindex_entry_add(fileindex, ie);
3290 if (err)
3291 break;
3294 return err;
3297 static const struct got_error *
3298 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3299 struct got_object_id *head_commit_id)
3301 const struct got_error *err = NULL;
3302 struct got_object_id *id = NULL;
3303 struct got_commit_object *commit = NULL;
3304 const char *ct_path = ct->in_repo_path;
3306 while (ct_path[0] == '/')
3307 ct_path++;
3309 if (ct->status != GOT_STATUS_ADD) {
3310 /* Trivial case: base commit == head commit */
3311 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3312 return NULL;
3314 * Ensure file content which local changes were based
3315 * on matches file content in the branch head.
3317 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3318 if (err) {
3319 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3320 goto done;
3321 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3322 goto done;
3323 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3324 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3325 } else {
3326 /* Require that added files don't exist in the branch head. */
3327 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3328 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3329 goto done;
3330 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3332 done:
3333 if (commit)
3334 got_object_commit_close(commit);
3335 free(id);
3336 return err;
3339 const struct got_error *
3340 commit_worktree(struct got_object_id **new_commit_id,
3341 struct got_pathlist_head *commitable_paths,
3342 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3343 const char *author, const char *committer,
3344 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3345 got_worktree_status_cb status_cb, void *status_arg,
3346 struct got_repository *repo)
3348 const struct got_error *err = NULL, *unlockerr = NULL;
3349 struct got_pathlist_entry *pe;
3350 const char *head_ref_name = NULL;
3351 struct got_commit_object *head_commit = NULL;
3352 struct got_reference *head_ref2 = NULL;
3353 struct got_object_id *head_commit_id2 = NULL;
3354 struct got_tree_object *head_tree = NULL;
3355 struct got_object_id *new_tree_id = NULL;
3356 struct got_object_id_queue parent_ids;
3357 struct got_object_qid *pid = NULL;
3358 char *logmsg = NULL;
3360 *new_commit_id = NULL;
3362 SIMPLEQ_INIT(&parent_ids);
3364 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3365 if (err)
3366 goto done;
3368 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3369 if (err)
3370 goto done;
3372 if (commit_msg_cb != NULL) {
3373 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3374 if (err)
3375 goto done;
3378 if (logmsg == NULL || strlen(logmsg) == 0) {
3379 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3380 goto done;
3383 /* Create blobs from added and modified files and record their IDs. */
3384 TAILQ_FOREACH(pe, commitable_paths, entry) {
3385 struct got_commitable *ct = pe->data;
3386 char *ondisk_path;
3388 if (ct->status != GOT_STATUS_ADD &&
3389 ct->status != GOT_STATUS_MODIFY)
3390 continue;
3392 if (asprintf(&ondisk_path, "%s/%s",
3393 worktree->root_path, pe->path) == -1) {
3394 err = got_error_from_errno("asprintf");
3395 goto done;
3397 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3398 free(ondisk_path);
3399 if (err)
3400 goto done;
3403 /* Recursively write new tree objects. */
3404 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3405 status_cb, status_arg, repo);
3406 if (err)
3407 goto done;
3409 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3410 if (err)
3411 goto done;
3412 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3413 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3414 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3415 got_object_qid_free(pid);
3416 if (logmsg != NULL)
3417 free(logmsg);
3418 if (err)
3419 goto done;
3421 /* Check if a concurrent commit to our branch has occurred. */
3422 head_ref_name = got_worktree_get_head_ref_name(worktree);
3423 if (head_ref_name == NULL) {
3424 err = got_error_from_errno("got_worktree_get_head_ref_name");
3425 goto done;
3427 /* Lock the reference here to prevent concurrent modification. */
3428 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3429 if (err)
3430 goto done;
3431 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3432 if (err)
3433 goto done;
3434 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3435 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3436 goto done;
3438 /* Update branch head in repository. */
3439 err = got_ref_change_ref(head_ref2, *new_commit_id);
3440 if (err)
3441 goto done;
3442 err = got_ref_write(head_ref2, repo);
3443 if (err)
3444 goto done;
3446 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3447 if (err)
3448 goto done;
3450 err = ref_base_commit(worktree, repo);
3451 if (err)
3452 goto done;
3453 done:
3454 if (head_tree)
3455 got_object_tree_close(head_tree);
3456 if (head_commit)
3457 got_object_commit_close(head_commit);
3458 free(head_commit_id2);
3459 if (head_ref2) {
3460 unlockerr = got_ref_unlock(head_ref2);
3461 if (unlockerr && err == NULL)
3462 err = unlockerr;
3463 got_ref_close(head_ref2);
3465 return err;
3468 static const struct got_error *
3469 check_path_is_commitable(const char *path,
3470 struct got_pathlist_head *commitable_paths)
3472 struct got_pathlist_entry *cpe = NULL;
3473 size_t path_len = strlen(path);
3475 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3476 struct got_commitable *ct = cpe->data;
3477 const char *ct_path = ct->path;
3479 while (ct_path[0] == '/')
3480 ct_path++;
3482 if (strcmp(path, ct_path) == 0 ||
3483 got_path_is_child(ct_path, path, path_len))
3484 break;
3487 if (cpe == NULL)
3488 return got_error_path(path, GOT_ERR_BAD_PATH);
3490 return NULL;
3493 const struct got_error *
3494 got_worktree_commit(struct got_object_id **new_commit_id,
3495 struct got_worktree *worktree, struct got_pathlist_head *paths,
3496 const char *author, const char *committer,
3497 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3498 got_worktree_status_cb status_cb, void *status_arg,
3499 struct got_repository *repo)
3501 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3502 struct got_fileindex *fileindex = NULL;
3503 char *fileindex_path = NULL;
3504 struct got_pathlist_head commitable_paths;
3505 struct collect_commitables_arg cc_arg;
3506 struct got_pathlist_entry *pe;
3507 struct got_reference *head_ref = NULL;
3508 struct got_object_id *head_commit_id = NULL;
3510 *new_commit_id = NULL;
3512 TAILQ_INIT(&commitable_paths);
3514 err = lock_worktree(worktree, LOCK_EX);
3515 if (err)
3516 goto done;
3518 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3519 if (err)
3520 goto done;
3522 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3523 if (err)
3524 goto done;
3526 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3527 if (err)
3528 goto done;
3530 cc_arg.commitable_paths = &commitable_paths;
3531 cc_arg.worktree = worktree;
3532 cc_arg.repo = repo;
3533 TAILQ_FOREACH(pe, paths, entry) {
3534 err = worktree_status(worktree, pe->path, fileindex, repo,
3535 collect_commitables, &cc_arg, NULL, NULL);
3536 if (err)
3537 goto done;
3540 if (TAILQ_EMPTY(&commitable_paths)) {
3541 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3542 goto done;
3545 TAILQ_FOREACH(pe, paths, entry) {
3546 err = check_path_is_commitable(pe->path, &commitable_paths);
3547 if (err)
3548 goto done;
3551 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3552 struct got_commitable *ct = pe->data;
3553 err = check_ct_out_of_date(ct, repo, head_commit_id);
3554 if (err)
3555 goto done;
3558 err = commit_worktree(new_commit_id, &commitable_paths,
3559 head_commit_id, worktree, author, committer,
3560 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3561 if (err)
3562 goto done;
3564 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3565 fileindex);
3566 sync_err = sync_fileindex(fileindex, fileindex_path);
3567 if (sync_err && err == NULL)
3568 err = sync_err;
3569 done:
3570 if (fileindex)
3571 got_fileindex_free(fileindex);
3572 free(fileindex_path);
3573 unlockerr = lock_worktree(worktree, LOCK_SH);
3574 if (unlockerr && err == NULL)
3575 err = unlockerr;
3576 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3577 struct got_commitable *ct = pe->data;
3578 free_commitable(ct);
3580 got_pathlist_free(&commitable_paths);
3581 return err;
3584 const char *
3585 got_commitable_get_path(struct got_commitable *ct)
3587 return ct->path;
3590 unsigned int
3591 got_commitable_get_status(struct got_commitable *ct)
3593 return ct->status;
3596 struct check_rebase_ok_arg {
3597 struct got_worktree *worktree;
3598 struct got_repository *repo;
3601 static const struct got_error *
3602 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3604 const struct got_error *err = NULL;
3605 struct check_rebase_ok_arg *a = arg;
3606 unsigned char status;
3607 struct stat sb;
3608 char *ondisk_path;
3610 /* Reject rebase of a work tree with mixed base commits. */
3611 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3612 SHA1_DIGEST_LENGTH))
3613 return got_error(GOT_ERR_MIXED_COMMITS);
3615 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3616 == -1)
3617 return got_error_from_errno("asprintf");
3619 /* Reject rebase of a work tree with modified or conflicted files. */
3620 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3621 free(ondisk_path);
3622 if (err)
3623 return err;
3625 if (status != GOT_STATUS_NO_CHANGE)
3626 return got_error(GOT_ERR_MODIFIED);
3628 return NULL;
3631 const struct got_error *
3632 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3633 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3634 struct got_worktree *worktree, struct got_reference *branch,
3635 struct got_repository *repo)
3637 const struct got_error *err = NULL;
3638 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3639 char *branch_ref_name = NULL;
3640 char *fileindex_path = NULL;
3641 struct check_rebase_ok_arg ok_arg;
3642 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3644 *new_base_branch_ref = NULL;
3645 *tmp_branch = NULL;
3646 *fileindex = NULL;
3648 err = lock_worktree(worktree, LOCK_EX);
3649 if (err)
3650 return err;
3652 err = open_fileindex(fileindex, &fileindex_path, worktree);
3653 if (err)
3654 goto done;
3656 ok_arg.worktree = worktree;
3657 ok_arg.repo = repo;
3658 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3659 &ok_arg);
3660 if (err)
3661 goto done;
3663 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3664 if (err)
3665 goto done;
3667 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3668 if (err)
3669 goto done;
3671 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3672 if (err)
3673 goto done;
3675 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3676 0);
3677 if (err)
3678 goto done;
3680 err = got_ref_alloc_symref(new_base_branch_ref,
3681 new_base_branch_ref_name, wt_branch);
3682 if (err)
3683 goto done;
3684 err = got_ref_write(*new_base_branch_ref, repo);
3685 if (err)
3686 goto done;
3688 /* TODO Lock original branch's ref while rebasing? */
3690 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3691 if (err)
3692 goto done;
3694 err = got_ref_write(branch_ref, repo);
3695 if (err)
3696 goto done;
3698 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3699 worktree->base_commit_id);
3700 if (err)
3701 goto done;
3702 err = got_ref_write(*tmp_branch, repo);
3703 if (err)
3704 goto done;
3706 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3707 if (err)
3708 goto done;
3709 done:
3710 free(fileindex_path);
3711 free(tmp_branch_name);
3712 free(new_base_branch_ref_name);
3713 free(branch_ref_name);
3714 if (branch_ref)
3715 got_ref_close(branch_ref);
3716 if (wt_branch)
3717 got_ref_close(wt_branch);
3718 if (err) {
3719 if (*new_base_branch_ref) {
3720 got_ref_close(*new_base_branch_ref);
3721 *new_base_branch_ref = NULL;
3723 if (*tmp_branch) {
3724 got_ref_close(*tmp_branch);
3725 *tmp_branch = NULL;
3727 if (*fileindex) {
3728 got_fileindex_free(*fileindex);
3729 *fileindex = NULL;
3731 lock_worktree(worktree, LOCK_SH);
3733 return err;
3736 const struct got_error *
3737 got_worktree_rebase_continue(struct got_object_id **commit_id,
3738 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3739 struct got_reference **branch, struct got_fileindex **fileindex,
3740 struct got_worktree *worktree, struct got_repository *repo)
3742 const struct got_error *err;
3743 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3744 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3745 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3746 char *fileindex_path = NULL;
3748 *commit_id = NULL;
3749 *new_base_branch = NULL;
3750 *tmp_branch = NULL;
3751 *branch = NULL;
3752 *fileindex = NULL;
3754 err = lock_worktree(worktree, LOCK_EX);
3755 if (err)
3756 return err;
3758 err = open_fileindex(fileindex, &fileindex_path, worktree);
3759 if (err)
3760 goto done;
3762 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3763 if (err)
3764 return err;
3766 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3767 if (err)
3768 goto done;
3770 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3771 if (err)
3772 goto done;
3774 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3775 if (err)
3776 goto done;
3778 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3779 if (err)
3780 goto done;
3782 err = got_ref_open(branch, repo,
3783 got_ref_get_symref_target(branch_ref), 0);
3784 if (err)
3785 goto done;
3787 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3788 if (err)
3789 goto done;
3791 err = got_ref_resolve(commit_id, repo, commit_ref);
3792 if (err)
3793 goto done;
3795 err = got_ref_open(new_base_branch, repo,
3796 new_base_branch_ref_name, 0);
3797 if (err)
3798 goto done;
3800 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3801 if (err)
3802 goto done;
3803 done:
3804 free(commit_ref_name);
3805 free(branch_ref_name);
3806 free(fileindex_path);
3807 if (commit_ref)
3808 got_ref_close(commit_ref);
3809 if (branch_ref)
3810 got_ref_close(branch_ref);
3811 if (err) {
3812 free(*commit_id);
3813 *commit_id = NULL;
3814 if (*tmp_branch) {
3815 got_ref_close(*tmp_branch);
3816 *tmp_branch = NULL;
3818 if (*new_base_branch) {
3819 got_ref_close(*new_base_branch);
3820 *new_base_branch = NULL;
3822 if (*branch) {
3823 got_ref_close(*branch);
3824 *branch = NULL;
3826 if (*fileindex) {
3827 got_fileindex_free(*fileindex);
3828 *fileindex = NULL;
3830 lock_worktree(worktree, LOCK_SH);
3832 return err;
3835 const struct got_error *
3836 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3838 const struct got_error *err;
3839 char *tmp_branch_name = NULL;
3841 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3842 if (err)
3843 return err;
3845 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3846 free(tmp_branch_name);
3847 return NULL;
3850 static const struct got_error *
3851 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3852 char **logmsg, void *arg)
3854 *logmsg = arg;
3855 return NULL;
3858 static const struct got_error *
3859 rebase_status(void *arg, unsigned char status, const char *path,
3860 struct got_object_id *blob_id, struct got_object_id *commit_id)
3862 return NULL;
3865 struct collect_merged_paths_arg {
3866 got_worktree_checkout_cb progress_cb;
3867 void *progress_arg;
3868 struct got_pathlist_head *merged_paths;
3871 static const struct got_error *
3872 collect_merged_paths(void *arg, unsigned char status, const char *path)
3874 const struct got_error *err;
3875 struct collect_merged_paths_arg *a = arg;
3876 char *p;
3877 struct got_pathlist_entry *new;
3879 err = (*a->progress_cb)(a->progress_arg, status, path);
3880 if (err)
3881 return err;
3883 if (status != GOT_STATUS_MERGE &&
3884 status != GOT_STATUS_ADD &&
3885 status != GOT_STATUS_DELETE &&
3886 status != GOT_STATUS_CONFLICT)
3887 return NULL;
3889 p = strdup(path);
3890 if (p == NULL)
3891 return got_error_from_errno("strdup");
3893 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3894 if (err || new == NULL)
3895 free(p);
3896 return err;
3899 void
3900 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3902 struct got_pathlist_entry *pe;
3904 TAILQ_FOREACH(pe, merged_paths, entry)
3905 free((char *)pe->path);
3907 got_pathlist_free(merged_paths);
3910 static const struct got_error *
3911 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3912 struct got_repository *repo)
3914 const struct got_error *err;
3915 struct got_reference *commit_ref = NULL;
3917 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3918 if (err) {
3919 if (err->code != GOT_ERR_NOT_REF)
3920 goto done;
3921 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3922 if (err)
3923 goto done;
3924 err = got_ref_write(commit_ref, repo);
3925 if (err)
3926 goto done;
3927 } else {
3928 struct got_object_id *stored_id;
3929 int cmp;
3931 err = got_ref_resolve(&stored_id, repo, commit_ref);
3932 if (err)
3933 goto done;
3934 cmp = got_object_id_cmp(commit_id, stored_id);
3935 free(stored_id);
3936 if (cmp != 0) {
3937 err = got_error(GOT_ERR_REBASE_COMMITID);
3938 goto done;
3941 done:
3942 if (commit_ref)
3943 got_ref_close(commit_ref);
3944 return err;
3947 static const struct got_error *
3948 rebase_merge_files(struct got_pathlist_head *merged_paths,
3949 const char *commit_ref_name, struct got_worktree *worktree,
3950 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3951 struct got_object_id *commit_id, struct got_repository *repo,
3952 got_worktree_checkout_cb progress_cb, void *progress_arg,
3953 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3955 const struct got_error *err;
3956 struct got_reference *commit_ref = NULL;
3957 struct collect_merged_paths_arg cmp_arg;
3958 char *fileindex_path;
3960 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3962 err = get_fileindex_path(&fileindex_path, worktree);
3963 if (err)
3964 return err;
3966 cmp_arg.progress_cb = progress_cb;
3967 cmp_arg.progress_arg = progress_arg;
3968 cmp_arg.merged_paths = merged_paths;
3969 err = merge_files(worktree, fileindex, fileindex_path,
3970 parent_commit_id, commit_id, repo, collect_merged_paths,
3971 &cmp_arg, cancel_cb, cancel_arg);
3972 if (commit_ref)
3973 got_ref_close(commit_ref);
3974 return err;
3977 const struct got_error *
3978 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3979 struct got_worktree *worktree, struct got_fileindex *fileindex,
3980 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3981 struct got_repository *repo,
3982 got_worktree_checkout_cb progress_cb, void *progress_arg,
3983 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3985 const struct got_error *err;
3986 char *commit_ref_name;
3988 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3989 if (err)
3990 return err;
3992 err = store_commit_id(commit_ref_name, commit_id, repo);
3993 if (err)
3994 goto done;
3996 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
3997 fileindex, parent_commit_id, commit_id, repo, progress_cb,
3998 progress_arg, cancel_cb, cancel_arg);
3999 done:
4000 free(commit_ref_name);
4001 return err;
4004 const struct got_error *
4005 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4006 struct got_worktree *worktree, struct got_fileindex *fileindex,
4007 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4008 struct got_repository *repo,
4009 got_worktree_checkout_cb progress_cb, void *progress_arg,
4010 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4012 const struct got_error *err;
4013 char *commit_ref_name;
4015 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4016 if (err)
4017 return err;
4019 err = store_commit_id(commit_ref_name, commit_id, repo);
4020 if (err)
4021 goto done;
4023 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4024 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4025 progress_arg, cancel_cb, cancel_arg);
4026 done:
4027 free(commit_ref_name);
4028 return err;
4031 static const struct got_error *
4032 rebase_commit(struct got_object_id **new_commit_id,
4033 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4034 struct got_worktree *worktree, struct got_fileindex *fileindex,
4035 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4036 const char *new_logmsg, struct got_repository *repo)
4038 const struct got_error *err, *sync_err;
4039 struct got_pathlist_head commitable_paths;
4040 struct collect_commitables_arg cc_arg;
4041 char *fileindex_path = NULL;
4042 struct got_reference *head_ref = NULL;
4043 struct got_object_id *head_commit_id = NULL;
4044 char *logmsg = NULL;
4046 TAILQ_INIT(&commitable_paths);
4047 *new_commit_id = NULL;
4049 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4051 err = get_fileindex_path(&fileindex_path, worktree);
4052 if (err)
4053 return err;
4055 cc_arg.commitable_paths = &commitable_paths;
4056 cc_arg.worktree = worktree;
4057 cc_arg.repo = repo;
4059 * If possible get the status of individual files directly to
4060 * avoid crawling the entire work tree once per rebased commit.
4061 * TODO: Ideally, merged_paths would contain a list of commitables
4062 * we could use so we could skip worktree_status() entirely.
4064 if (merged_paths) {
4065 struct got_pathlist_entry *pe;
4066 if (TAILQ_EMPTY(merged_paths)) {
4067 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4068 goto done;
4070 TAILQ_FOREACH(pe, merged_paths, entry) {
4071 err = worktree_status(worktree, pe->path, fileindex,
4072 repo, collect_commitables, &cc_arg, NULL, NULL);
4073 if (err)
4074 goto done;
4076 } else {
4077 err = worktree_status(worktree, "", fileindex, repo,
4078 collect_commitables, &cc_arg, NULL, NULL);
4079 if (err)
4080 goto done;
4083 if (TAILQ_EMPTY(&commitable_paths)) {
4084 /* No-op change; commit will be elided. */
4085 err = got_ref_delete(commit_ref, repo);
4086 if (err)
4087 goto done;
4088 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4089 goto done;
4092 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4093 if (err)
4094 goto done;
4096 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4097 if (err)
4098 goto done;
4100 if (new_logmsg)
4101 logmsg = strdup(new_logmsg);
4102 else
4103 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4104 if (logmsg == NULL)
4105 return got_error_from_errno("strdup");
4107 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4108 worktree, got_object_commit_get_author(orig_commit),
4109 got_object_commit_get_committer(orig_commit),
4110 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4111 if (err)
4112 goto done;
4114 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4115 if (err)
4116 goto done;
4118 err = got_ref_delete(commit_ref, repo);
4119 if (err)
4120 goto done;
4122 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4123 fileindex);
4124 sync_err = sync_fileindex(fileindex, fileindex_path);
4125 if (sync_err && err == NULL)
4126 err = sync_err;
4127 done:
4128 free(fileindex_path);
4129 free(head_commit_id);
4130 if (head_ref)
4131 got_ref_close(head_ref);
4132 if (err) {
4133 free(*new_commit_id);
4134 *new_commit_id = NULL;
4136 return err;
4139 const struct got_error *
4140 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4141 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4142 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4143 struct got_commit_object *orig_commit,
4144 struct got_object_id *orig_commit_id, struct got_repository *repo)
4146 const struct got_error *err;
4147 char *commit_ref_name;
4148 struct got_reference *commit_ref = NULL;
4149 struct got_object_id *commit_id = NULL;
4151 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4152 if (err)
4153 return err;
4155 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4156 if (err)
4157 goto done;
4158 err = got_ref_resolve(&commit_id, repo, commit_ref);
4159 if (err)
4160 goto done;
4161 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4162 err = got_error(GOT_ERR_REBASE_COMMITID);
4163 goto done;
4166 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4167 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4168 done:
4169 if (commit_ref)
4170 got_ref_close(commit_ref);
4171 free(commit_ref_name);
4172 free(commit_id);
4173 return err;
4176 const struct got_error *
4177 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4178 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4179 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4180 struct got_commit_object *orig_commit,
4181 struct got_object_id *orig_commit_id, const char *new_logmsg,
4182 struct got_repository *repo)
4184 const struct got_error *err;
4185 char *commit_ref_name;
4186 struct got_reference *commit_ref = NULL;
4187 struct got_object_id *commit_id = NULL;
4189 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4190 if (err)
4191 return err;
4193 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4194 if (err)
4195 goto done;
4196 err = got_ref_resolve(&commit_id, repo, commit_ref);
4197 if (err)
4198 goto done;
4199 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4200 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4201 goto done;
4204 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4205 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4206 done:
4207 if (commit_ref)
4208 got_ref_close(commit_ref);
4209 free(commit_ref_name);
4210 free(commit_id);
4211 return err;
4214 const struct got_error *
4215 got_worktree_rebase_postpone(struct got_worktree *worktree,
4216 struct got_fileindex *fileindex)
4218 if (fileindex)
4219 got_fileindex_free(fileindex);
4220 return lock_worktree(worktree, LOCK_SH);
4223 static const struct got_error *
4224 delete_ref(const char *name, struct got_repository *repo)
4226 const struct got_error *err;
4227 struct got_reference *ref;
4229 err = got_ref_open(&ref, repo, name, 0);
4230 if (err) {
4231 if (err->code == GOT_ERR_NOT_REF)
4232 return NULL;
4233 return err;
4236 err = got_ref_delete(ref, repo);
4237 got_ref_close(ref);
4238 return err;
4241 static const struct got_error *
4242 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4244 const struct got_error *err;
4245 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4246 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4248 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4249 if (err)
4250 goto done;
4251 err = delete_ref(tmp_branch_name, repo);
4252 if (err)
4253 goto done;
4255 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4256 if (err)
4257 goto done;
4258 err = delete_ref(new_base_branch_ref_name, repo);
4259 if (err)
4260 goto done;
4262 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4263 if (err)
4264 goto done;
4265 err = delete_ref(branch_ref_name, repo);
4266 if (err)
4267 goto done;
4269 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4270 if (err)
4271 goto done;
4272 err = delete_ref(commit_ref_name, repo);
4273 if (err)
4274 goto done;
4276 done:
4277 free(tmp_branch_name);
4278 free(new_base_branch_ref_name);
4279 free(branch_ref_name);
4280 free(commit_ref_name);
4281 return err;
4284 const struct got_error *
4285 got_worktree_rebase_complete(struct got_worktree *worktree,
4286 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4287 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4288 struct got_repository *repo)
4290 const struct got_error *err, *unlockerr;
4291 struct got_object_id *new_head_commit_id = NULL;
4293 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4294 if (err)
4295 return err;
4297 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4298 if (err)
4299 goto done;
4301 err = got_ref_write(rebased_branch, repo);
4302 if (err)
4303 goto done;
4305 err = got_worktree_set_head_ref(worktree, rebased_branch);
4306 if (err)
4307 goto done;
4309 err = delete_rebase_refs(worktree, repo);
4310 done:
4311 if (fileindex)
4312 got_fileindex_free(fileindex);
4313 free(new_head_commit_id);
4314 unlockerr = lock_worktree(worktree, LOCK_SH);
4315 if (unlockerr && err == NULL)
4316 err = unlockerr;
4317 return err;
4320 struct collect_revertible_paths_arg {
4321 struct got_pathlist_head *revertible_paths;
4322 struct got_worktree *worktree;
4325 static const struct got_error *
4326 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4327 struct got_object_id *blob_id, struct got_object_id *commit_id)
4329 struct collect_revertible_paths_arg *a = arg;
4330 const struct got_error *err = NULL;
4331 struct got_pathlist_entry *new = NULL;
4332 char *path = NULL;
4334 if (status != GOT_STATUS_ADD &&
4335 status != GOT_STATUS_DELETE &&
4336 status != GOT_STATUS_MODIFY &&
4337 status != GOT_STATUS_CONFLICT &&
4338 status != GOT_STATUS_MISSING)
4339 return NULL;
4341 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4342 return got_error_from_errno("asprintf");
4344 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4345 if (err || new == NULL)
4346 free(path);
4347 return err;
4350 const struct got_error *
4351 got_worktree_rebase_abort(struct got_worktree *worktree,
4352 struct got_fileindex *fileindex, struct got_repository *repo,
4353 struct got_reference *new_base_branch,
4354 got_worktree_checkout_cb progress_cb, void *progress_arg)
4356 const struct got_error *err, *unlockerr, *sync_err;
4357 struct got_reference *resolved = NULL;
4358 struct got_object_id *commit_id = NULL;
4359 char *fileindex_path = NULL;
4360 struct got_pathlist_head revertible_paths;
4361 struct got_pathlist_entry *pe;
4362 struct collect_revertible_paths_arg crp_arg;
4363 struct got_object_id *tree_id = NULL;
4365 TAILQ_INIT(&revertible_paths);
4367 err = lock_worktree(worktree, LOCK_EX);
4368 if (err)
4369 return err;
4371 err = got_ref_open(&resolved, repo,
4372 got_ref_get_symref_target(new_base_branch), 0);
4373 if (err)
4374 goto done;
4376 err = got_worktree_set_head_ref(worktree, resolved);
4377 if (err)
4378 goto done;
4381 * XXX commits to the base branch could have happened while
4382 * we were busy rebasing; should we store the original commit ID
4383 * when rebase begins and read it back here?
4385 err = got_ref_resolve(&commit_id, repo, resolved);
4386 if (err)
4387 goto done;
4389 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4390 if (err)
4391 goto done;
4393 err = got_object_id_by_path(&tree_id, repo,
4394 worktree->base_commit_id, worktree->path_prefix);
4395 if (err)
4396 goto done;
4398 err = delete_rebase_refs(worktree, repo);
4399 if (err)
4400 goto done;
4402 err = get_fileindex_path(&fileindex_path, worktree);
4403 if (err)
4404 goto done;
4406 crp_arg.revertible_paths = &revertible_paths;
4407 crp_arg.worktree = worktree;
4408 err = worktree_status(worktree, "", fileindex, repo,
4409 collect_revertible_paths, &crp_arg, NULL, NULL);
4410 if (err)
4411 goto done;
4413 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4414 err = revert_file(worktree, fileindex, pe->path,
4415 progress_cb, progress_arg, repo);
4416 if (err)
4417 goto sync;
4420 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4421 repo, progress_cb, progress_arg, NULL, NULL);
4422 sync:
4423 sync_err = sync_fileindex(fileindex, fileindex_path);
4424 if (sync_err && err == NULL)
4425 err = sync_err;
4426 done:
4427 got_ref_close(resolved);
4428 free(tree_id);
4429 free(commit_id);
4430 if (fileindex)
4431 got_fileindex_free(fileindex);
4432 free(fileindex_path);
4433 TAILQ_FOREACH(pe, &revertible_paths, entry)
4434 free((char *)pe->path);
4435 got_pathlist_free(&revertible_paths);
4437 unlockerr = lock_worktree(worktree, LOCK_SH);
4438 if (unlockerr && err == NULL)
4439 err = unlockerr;
4440 return err;
4443 const struct got_error *
4444 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4445 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4446 struct got_fileindex **fileindex, struct got_worktree *worktree,
4447 struct got_repository *repo)
4449 const struct got_error *err = NULL;
4450 char *tmp_branch_name = NULL;
4451 char *branch_ref_name = NULL;
4452 char *base_commit_ref_name = NULL;
4453 char *fileindex_path = NULL;
4454 struct check_rebase_ok_arg ok_arg;
4455 struct got_reference *wt_branch = NULL;
4456 struct got_reference *base_commit_ref = NULL;
4458 *tmp_branch = NULL;
4459 *branch_ref = NULL;
4460 *base_commit_id = NULL;
4461 *fileindex = NULL;
4463 err = lock_worktree(worktree, LOCK_EX);
4464 if (err)
4465 return err;
4467 err = open_fileindex(fileindex, &fileindex_path, worktree);
4468 if (err)
4469 goto done;
4471 ok_arg.worktree = worktree;
4472 ok_arg.repo = repo;
4473 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4474 &ok_arg);
4475 if (err)
4476 goto done;
4478 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4479 if (err)
4480 goto done;
4482 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4483 if (err)
4484 goto done;
4486 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4487 worktree);
4488 if (err)
4489 goto done;
4491 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4492 0);
4493 if (err)
4494 goto done;
4496 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4497 if (err)
4498 goto done;
4500 err = got_ref_write(*branch_ref, repo);
4501 if (err)
4502 goto done;
4504 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4505 worktree->base_commit_id);
4506 if (err)
4507 goto done;
4508 err = got_ref_write(base_commit_ref, repo);
4509 if (err)
4510 goto done;
4511 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4512 if (*base_commit_id == NULL) {
4513 err = got_error_from_errno("got_object_id_dup");
4514 goto done;
4517 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4518 worktree->base_commit_id);
4519 if (err)
4520 goto done;
4521 err = got_ref_write(*tmp_branch, repo);
4522 if (err)
4523 goto done;
4525 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4526 if (err)
4527 goto done;
4528 done:
4529 free(fileindex_path);
4530 free(tmp_branch_name);
4531 free(branch_ref_name);
4532 free(base_commit_ref_name);
4533 if (wt_branch)
4534 got_ref_close(wt_branch);
4535 if (err) {
4536 if (*branch_ref) {
4537 got_ref_close(*branch_ref);
4538 *branch_ref = NULL;
4540 if (*tmp_branch) {
4541 got_ref_close(*tmp_branch);
4542 *tmp_branch = NULL;
4544 free(*base_commit_id);
4545 if (*fileindex) {
4546 got_fileindex_free(*fileindex);
4547 *fileindex = NULL;
4549 lock_worktree(worktree, LOCK_SH);
4551 return err;
4554 const struct got_error *
4555 got_worktree_histedit_postpone(struct got_worktree *worktree,
4556 struct got_fileindex *fileindex)
4558 if (fileindex)
4559 got_fileindex_free(fileindex);
4560 return lock_worktree(worktree, LOCK_SH);
4563 const struct got_error *
4564 got_worktree_histedit_in_progress(int *in_progress,
4565 struct got_worktree *worktree)
4567 const struct got_error *err;
4568 char *tmp_branch_name = NULL;
4570 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4571 if (err)
4572 return err;
4574 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4575 free(tmp_branch_name);
4576 return NULL;
4579 const struct got_error *
4580 got_worktree_histedit_continue(struct got_object_id **commit_id,
4581 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4582 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4583 struct got_worktree *worktree, struct got_repository *repo)
4585 const struct got_error *err;
4586 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4587 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4588 struct got_reference *commit_ref = NULL;
4589 struct got_reference *base_commit_ref = NULL;
4590 char *fileindex_path = NULL;
4592 *commit_id = NULL;
4593 *tmp_branch = NULL;
4594 *base_commit_id = NULL;
4595 *fileindex = NULL;
4597 err = lock_worktree(worktree, LOCK_EX);
4598 if (err)
4599 return err;
4601 err = open_fileindex(fileindex, &fileindex_path, worktree);
4602 if (err)
4603 goto done;
4605 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4606 if (err)
4607 return err;
4609 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4610 if (err)
4611 goto done;
4613 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4614 if (err)
4615 goto done;
4617 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4618 worktree);
4619 if (err)
4620 goto done;
4622 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4623 if (err)
4624 goto done;
4626 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4627 if (err)
4628 goto done;
4629 err = got_ref_resolve(commit_id, repo, commit_ref);
4630 if (err)
4631 goto done;
4633 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4634 if (err)
4635 goto done;
4636 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4637 if (err)
4638 goto done;
4640 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4641 if (err)
4642 goto done;
4643 done:
4644 free(commit_ref_name);
4645 free(branch_ref_name);
4646 free(fileindex_path);
4647 if (commit_ref)
4648 got_ref_close(commit_ref);
4649 if (base_commit_ref)
4650 got_ref_close(base_commit_ref);
4651 if (err) {
4652 free(*commit_id);
4653 *commit_id = NULL;
4654 free(*base_commit_id);
4655 *base_commit_id = NULL;
4656 if (*tmp_branch) {
4657 got_ref_close(*tmp_branch);
4658 *tmp_branch = NULL;
4660 if (*fileindex) {
4661 got_fileindex_free(*fileindex);
4662 *fileindex = NULL;
4664 lock_worktree(worktree, LOCK_EX);
4666 return err;
4669 static const struct got_error *
4670 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4672 const struct got_error *err;
4673 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4674 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4676 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4677 if (err)
4678 goto done;
4679 err = delete_ref(tmp_branch_name, repo);
4680 if (err)
4681 goto done;
4683 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4684 worktree);
4685 if (err)
4686 goto done;
4687 err = delete_ref(base_commit_ref_name, repo);
4688 if (err)
4689 goto done;
4691 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4692 if (err)
4693 goto done;
4694 err = delete_ref(branch_ref_name, repo);
4695 if (err)
4696 goto done;
4698 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4699 if (err)
4700 goto done;
4701 err = delete_ref(commit_ref_name, repo);
4702 if (err)
4703 goto done;
4704 done:
4705 free(tmp_branch_name);
4706 free(base_commit_ref_name);
4707 free(branch_ref_name);
4708 free(commit_ref_name);
4709 return err;
4712 const struct got_error *
4713 got_worktree_histedit_abort(struct got_worktree *worktree,
4714 struct got_fileindex *fileindex, struct got_repository *repo,
4715 struct got_reference *branch, struct got_object_id *base_commit_id,
4716 got_worktree_checkout_cb progress_cb, void *progress_arg)
4718 const struct got_error *err, *unlockerr, *sync_err;
4719 struct got_reference *resolved = NULL;
4720 char *fileindex_path = NULL;
4721 struct got_pathlist_head revertible_paths;
4722 struct got_pathlist_entry *pe;
4723 struct collect_revertible_paths_arg crp_arg;
4724 struct got_object_id *tree_id = NULL;
4726 TAILQ_INIT(&revertible_paths);
4728 err = lock_worktree(worktree, LOCK_EX);
4729 if (err)
4730 return err;
4732 err = got_ref_open(&resolved, repo,
4733 got_ref_get_symref_target(branch), 0);
4734 if (err)
4735 goto done;
4737 err = got_worktree_set_head_ref(worktree, resolved);
4738 if (err)
4739 goto done;
4741 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4742 if (err)
4743 goto done;
4745 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4746 worktree->path_prefix);
4747 if (err)
4748 goto done;
4750 err = delete_histedit_refs(worktree, repo);
4751 if (err)
4752 goto done;
4754 err = get_fileindex_path(&fileindex_path, worktree);
4755 if (err)
4756 goto done;
4758 crp_arg.revertible_paths = &revertible_paths;
4759 crp_arg.worktree = worktree;
4760 err = worktree_status(worktree, "", fileindex, repo,
4761 collect_revertible_paths, &crp_arg, NULL, NULL);
4762 if (err)
4763 goto done;
4765 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4766 err = revert_file(worktree, fileindex, pe->path,
4767 progress_cb, progress_arg, repo);
4768 if (err)
4769 goto sync;
4772 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4773 repo, progress_cb, progress_arg, NULL, NULL);
4774 sync:
4775 sync_err = sync_fileindex(fileindex, fileindex_path);
4776 if (sync_err && err == NULL)
4777 err = sync_err;
4778 done:
4779 got_ref_close(resolved);
4780 free(tree_id);
4781 free(fileindex_path);
4782 TAILQ_FOREACH(pe, &revertible_paths, entry)
4783 free((char *)pe->path);
4784 got_pathlist_free(&revertible_paths);
4786 unlockerr = lock_worktree(worktree, LOCK_SH);
4787 if (unlockerr && err == NULL)
4788 err = unlockerr;
4789 return err;
4792 const struct got_error *
4793 got_worktree_histedit_complete(struct got_worktree *worktree,
4794 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4795 struct got_reference *edited_branch, struct got_repository *repo)
4797 const struct got_error *err, *unlockerr;
4798 struct got_object_id *new_head_commit_id = NULL;
4799 struct got_reference *resolved = NULL;
4801 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4802 if (err)
4803 return err;
4805 err = got_ref_open(&resolved, repo,
4806 got_ref_get_symref_target(edited_branch), 0);
4807 if (err)
4808 goto done;
4810 err = got_ref_change_ref(resolved, new_head_commit_id);
4811 if (err)
4812 goto done;
4814 err = got_ref_write(resolved, repo);
4815 if (err)
4816 goto done;
4818 err = got_worktree_set_head_ref(worktree, resolved);
4819 if (err)
4820 goto done;
4822 err = delete_histedit_refs(worktree, repo);
4823 done:
4824 if (fileindex)
4825 got_fileindex_free(fileindex);
4826 free(new_head_commit_id);
4827 unlockerr = lock_worktree(worktree, LOCK_SH);
4828 if (unlockerr && err == NULL)
4829 err = unlockerr;
4830 return err;
4833 const struct got_error *
4834 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4835 struct got_object_id *commit_id, struct got_repository *repo)
4837 const struct got_error *err;
4838 char *commit_ref_name;
4840 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4841 if (err)
4842 return err;
4844 err = store_commit_id(commit_ref_name, commit_id, repo);
4845 if (err)
4846 goto done;
4848 err = delete_ref(commit_ref_name, repo);
4849 done:
4850 free(commit_ref_name);
4851 return err;