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, strlen(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 = pe->path_len;
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 strlen(path2));
1913 if (ie == NULL)
1914 return (*a->progress_cb)(a->progress_arg,
1915 GOT_STATUS_MISSING, path2);
1917 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1918 path2) == -1)
1919 return got_error_from_errno("asprintf");
1921 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1922 if (err)
1923 goto done;
1925 if (status == GOT_STATUS_DELETE) {
1926 err = (*a->progress_cb)(a->progress_arg,
1927 GOT_STATUS_MERGE, path2);
1928 goto done;
1930 if (status != GOT_STATUS_NO_CHANGE &&
1931 status != GOT_STATUS_MODIFY &&
1932 status != GOT_STATUS_CONFLICT &&
1933 status != GOT_STATUS_ADD) {
1934 err = (*a->progress_cb)(a->progress_arg, status, path2);
1935 goto done;
1938 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1939 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1940 a->progress_cb, a->progress_arg);
1941 } else if (blob1) {
1942 ie = got_fileindex_entry_get(a->fileindex, path1,
1943 strlen(path1));
1944 if (ie == NULL)
1945 return (*a->progress_cb)(a->progress_arg,
1946 GOT_STATUS_MISSING, path2);
1948 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1949 path1) == -1)
1950 return got_error_from_errno("asprintf");
1952 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1953 if (err)
1954 goto done;
1956 switch (status) {
1957 case GOT_STATUS_NO_CHANGE:
1958 err = (*a->progress_cb)(a->progress_arg,
1959 GOT_STATUS_DELETE, path1);
1960 if (err)
1961 goto done;
1962 err = remove_ondisk_file(a->worktree->root_path, path1);
1963 if (err)
1964 goto done;
1965 if (ie)
1966 got_fileindex_entry_mark_deleted_from_disk(ie);
1967 break;
1968 case GOT_STATUS_DELETE:
1969 case GOT_STATUS_MISSING:
1970 err = (*a->progress_cb)(a->progress_arg,
1971 GOT_STATUS_DELETE, path1);
1972 if (err)
1973 goto done;
1974 if (ie)
1975 got_fileindex_entry_mark_deleted_from_disk(ie);
1976 break;
1977 case GOT_STATUS_ADD:
1978 case GOT_STATUS_MODIFY:
1979 case GOT_STATUS_CONFLICT:
1980 err = (*a->progress_cb)(a->progress_arg,
1981 GOT_STATUS_CANNOT_DELETE, path1);
1982 if (err)
1983 goto done;
1984 break;
1985 case GOT_STATUS_OBSTRUCTED:
1986 err = (*a->progress_cb)(a->progress_arg, status, path1);
1987 if (err)
1988 goto done;
1989 break;
1990 default:
1991 break;
1993 } else if (blob2) {
1994 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1995 path2) == -1)
1996 return got_error_from_errno("asprintf");
1997 ie = got_fileindex_entry_get(a->fileindex, path2,
1998 strlen(path2));
1999 if (ie) {
2000 err = get_file_status(&status, &sb, ie, ondisk_path,
2001 repo);
2002 if (err)
2003 goto done;
2004 if (status != GOT_STATUS_NO_CHANGE &&
2005 status != GOT_STATUS_MODIFY &&
2006 status != GOT_STATUS_CONFLICT &&
2007 status != GOT_STATUS_ADD) {
2008 err = (*a->progress_cb)(a->progress_arg,
2009 status, path2);
2010 goto done;
2012 err = merge_blob(&local_changes_subsumed, a->worktree,
2013 NULL, ondisk_path, path2, sb.st_mode, blob2,
2014 a->commit_id2, repo,
2015 a->progress_cb, a->progress_arg);
2016 if (status == GOT_STATUS_DELETE) {
2017 err = update_blob_fileindex_entry(a->worktree,
2018 a->fileindex, ie, ondisk_path, ie->path,
2019 blob2, 0);
2020 if (err)
2021 goto done;
2023 } else {
2024 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2025 err = install_blob(a->worktree, ondisk_path, path2,
2026 /* XXX get this from parent tree! */
2027 GOT_DEFAULT_FILE_MODE,
2028 sb.st_mode, blob2, 0, 0, repo,
2029 a->progress_cb, a->progress_arg);
2030 if (err)
2031 goto done;
2032 err = got_fileindex_entry_alloc(&ie,
2033 ondisk_path, path2, NULL, NULL);
2034 if (err)
2035 goto done;
2036 err = got_fileindex_entry_add(a->fileindex, ie);
2037 if (err) {
2038 got_fileindex_entry_free(ie);
2039 goto done;
2043 done:
2044 free(ondisk_path);
2045 return err;
2048 struct check_merge_ok_arg {
2049 struct got_worktree *worktree;
2050 struct got_repository *repo;
2053 static const struct got_error *
2054 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2056 const struct got_error *err = NULL;
2057 struct check_merge_ok_arg *a = arg;
2058 unsigned char status;
2059 struct stat sb;
2060 char *ondisk_path;
2062 /* Reject merges into a work tree with mixed base commits. */
2063 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2064 SHA1_DIGEST_LENGTH))
2065 return got_error(GOT_ERR_MIXED_COMMITS);
2067 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2068 == -1)
2069 return got_error_from_errno("asprintf");
2071 /* Reject merges into a work tree with conflicted files. */
2072 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2073 if (err)
2074 return err;
2075 if (status == GOT_STATUS_CONFLICT)
2076 return got_error(GOT_ERR_CONFLICTS);
2078 return NULL;
2081 static const struct got_error *
2082 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2083 const char *fileindex_path, struct got_object_id *commit_id1,
2084 struct got_object_id *commit_id2, struct got_repository *repo,
2085 got_worktree_checkout_cb progress_cb, void *progress_arg,
2086 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2088 const struct got_error *err = NULL, *sync_err;
2089 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2090 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2091 struct merge_file_cb_arg arg;
2093 if (commit_id1) {
2094 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2095 worktree->path_prefix);
2096 if (err)
2097 goto done;
2099 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2100 if (err)
2101 goto done;
2104 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2105 worktree->path_prefix);
2106 if (err)
2107 goto done;
2109 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2110 if (err)
2111 goto done;
2113 arg.worktree = worktree;
2114 arg.fileindex = fileindex;
2115 arg.progress_cb = progress_cb;
2116 arg.progress_arg = progress_arg;
2117 arg.cancel_cb = cancel_cb;
2118 arg.cancel_arg = cancel_arg;
2119 arg.commit_id2 = commit_id2;
2120 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2121 sync_err = sync_fileindex(fileindex, fileindex_path);
2122 if (sync_err && err == NULL)
2123 err = sync_err;
2124 done:
2125 if (tree1)
2126 got_object_tree_close(tree1);
2127 if (tree2)
2128 got_object_tree_close(tree2);
2129 return err;
2132 const struct got_error *
2133 got_worktree_merge_files(struct got_worktree *worktree,
2134 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2135 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2136 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2138 const struct got_error *err, *unlockerr;
2139 char *fileindex_path = NULL;
2140 struct got_fileindex *fileindex = NULL;
2141 struct check_merge_ok_arg mok_arg;
2143 err = lock_worktree(worktree, LOCK_EX);
2144 if (err)
2145 return err;
2147 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2148 if (err)
2149 goto done;
2151 mok_arg.worktree = worktree;
2152 mok_arg.repo = repo;
2153 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2154 &mok_arg);
2155 if (err)
2156 goto done;
2158 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2159 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2160 done:
2161 if (fileindex)
2162 got_fileindex_free(fileindex);
2163 free(fileindex_path);
2164 unlockerr = lock_worktree(worktree, LOCK_SH);
2165 if (unlockerr && err == NULL)
2166 err = unlockerr;
2167 return err;
2170 struct diff_dir_cb_arg {
2171 struct got_fileindex *fileindex;
2172 struct got_worktree *worktree;
2173 const char *status_path;
2174 size_t status_path_len;
2175 struct got_repository *repo;
2176 got_worktree_status_cb status_cb;
2177 void *status_arg;
2178 got_worktree_cancel_cb cancel_cb;
2179 void *cancel_arg;
2182 static const struct got_error *
2183 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2184 got_worktree_status_cb status_cb, void *status_arg,
2185 struct got_repository *repo)
2187 const struct got_error *err = NULL;
2188 unsigned char status = GOT_STATUS_NO_CHANGE;
2189 struct stat sb;
2190 struct got_object_id blob_id, commit_id;
2192 err = get_file_status(&status, &sb, ie, abspath, repo);
2193 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2194 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2195 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2196 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2197 &commit_id);
2199 return err;
2202 static const struct got_error *
2203 status_old_new(void *arg, struct got_fileindex_entry *ie,
2204 struct dirent *de, const char *parent_path)
2206 const struct got_error *err = NULL;
2207 struct diff_dir_cb_arg *a = arg;
2208 char *abspath;
2210 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2211 return got_error(GOT_ERR_CANCELLED);
2213 if (got_path_cmp(parent_path, a->status_path,
2214 strlen(parent_path), a->status_path_len) != 0 &&
2215 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2216 return NULL;
2218 if (parent_path[0]) {
2219 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2220 parent_path, de->d_name) == -1)
2221 return got_error_from_errno("asprintf");
2222 } else {
2223 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2224 de->d_name) == -1)
2225 return got_error_from_errno("asprintf");
2228 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2229 a->repo);
2230 free(abspath);
2231 return err;
2234 static const struct got_error *
2235 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2237 struct diff_dir_cb_arg *a = arg;
2238 struct got_object_id blob_id, commit_id;
2239 unsigned char status;
2241 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 return got_error(GOT_ERR_CANCELLED);
2244 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2245 return NULL;
2247 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2248 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2249 if (got_fileindex_entry_has_file_on_disk(ie))
2250 status = GOT_STATUS_MISSING;
2251 else
2252 status = GOT_STATUS_DELETE;
2253 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2254 &commit_id);
2257 static const struct got_error *
2258 status_new(void *arg, struct dirent *de, const char *parent_path)
2260 const struct got_error *err = NULL;
2261 struct diff_dir_cb_arg *a = arg;
2262 char *path = NULL;
2264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 return got_error(GOT_ERR_CANCELLED);
2267 if (de->d_type == DT_DIR)
2268 return NULL;
2270 /* XXX ignore symlinks for now */
2271 if (de->d_type == DT_LNK)
2272 return NULL;
2274 if (parent_path[0]) {
2275 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2276 return got_error_from_errno("asprintf");
2277 } else {
2278 path = de->d_name;
2281 if (got_path_is_child(path, a->status_path, a->status_path_len))
2282 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2283 path, NULL, NULL);
2284 if (parent_path[0])
2285 free(path);
2286 return err;
2289 static const struct got_error *
2290 report_single_file_status(const char *path, const char *ondisk_path,
2291 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2292 void *status_arg, struct got_repository *repo)
2294 struct got_fileindex_entry *ie;
2295 struct stat sb;
2297 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2298 if (ie)
2299 return report_file_status(ie, ondisk_path, status_cb,
2300 status_arg, repo);
2302 if (lstat(ondisk_path, &sb) == -1) {
2303 if (errno != ENOENT)
2304 return got_error_from_errno2("lstat", ondisk_path);
2305 return NULL;
2308 if (S_ISREG(sb.st_mode))
2309 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED, path,
2310 NULL, NULL);
2312 return NULL;
2315 static const struct got_error *
2316 worktree_status(struct got_worktree *worktree, const char *path,
2317 struct got_fileindex *fileindex, struct got_repository *repo,
2318 got_worktree_status_cb status_cb, void *status_arg,
2319 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2321 const struct got_error *err = NULL;
2322 DIR *workdir = NULL;
2323 struct got_fileindex_diff_dir_cb fdiff_cb;
2324 struct diff_dir_cb_arg arg;
2325 char *ondisk_path = NULL;
2327 if (asprintf(&ondisk_path, "%s%s%s",
2328 worktree->root_path, path[0] ? "/" : "", path) == -1)
2329 return got_error_from_errno("asprintf");
2331 workdir = opendir(ondisk_path);
2332 if (workdir == NULL) {
2333 if (errno != ENOTDIR && errno != ENOENT)
2334 err = got_error_from_errno2("opendir", ondisk_path);
2335 else
2336 err = report_single_file_status(path, ondisk_path,
2337 fileindex, status_cb, status_arg, repo);
2338 } else {
2339 fdiff_cb.diff_old_new = status_old_new;
2340 fdiff_cb.diff_old = status_old;
2341 fdiff_cb.diff_new = status_new;
2342 arg.fileindex = fileindex;
2343 arg.worktree = worktree;
2344 arg.status_path = path;
2345 arg.status_path_len = strlen(path);
2346 arg.repo = repo;
2347 arg.status_cb = status_cb;
2348 arg.status_arg = status_arg;
2349 arg.cancel_cb = cancel_cb;
2350 arg.cancel_arg = cancel_arg;
2351 err = got_fileindex_diff_dir(fileindex, workdir,
2352 worktree->root_path, path, repo, &fdiff_cb, &arg);
2355 if (workdir)
2356 closedir(workdir);
2357 free(ondisk_path);
2358 return err;
2361 const struct got_error *
2362 got_worktree_status(struct got_worktree *worktree,
2363 struct got_pathlist_head *paths, struct got_repository *repo,
2364 got_worktree_status_cb status_cb, void *status_arg,
2365 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2367 const struct got_error *err = NULL;
2368 char *fileindex_path = NULL;
2369 struct got_fileindex *fileindex = NULL;
2370 struct got_pathlist_entry *pe;
2372 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2373 if (err)
2374 return err;
2376 TAILQ_FOREACH(pe, paths, entry) {
2377 err = worktree_status(worktree, pe->path, fileindex, repo,
2378 status_cb, status_arg, cancel_cb, cancel_arg);
2379 if (err)
2380 break;
2382 free(fileindex_path);
2383 got_fileindex_free(fileindex);
2384 return err;
2387 const struct got_error *
2388 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2389 const char *arg)
2391 const struct got_error *err = NULL;
2392 char *resolved, *cwd = NULL, *path = NULL;
2393 size_t len;
2395 *wt_path = NULL;
2397 resolved = realpath(arg, NULL);
2398 if (resolved == NULL) {
2399 if (errno != ENOENT)
2400 return got_error_from_errno2("realpath", arg);
2401 cwd = getcwd(NULL, 0);
2402 if (cwd == NULL)
2403 return got_error_from_errno("getcwd");
2404 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2405 err = got_error_from_errno("asprintf");
2406 goto done;
2410 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2411 strlen(got_worktree_get_root_path(worktree)))) {
2412 err = got_error(GOT_ERR_BAD_PATH);
2413 goto done;
2416 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2417 err = got_path_skip_common_ancestor(&path,
2418 got_worktree_get_root_path(worktree), resolved);
2419 if (err)
2420 goto done;
2421 } else {
2422 path = strdup("");
2423 if (path == NULL) {
2424 err = got_error_from_errno("strdup");
2425 goto done;
2429 /* XXX status walk can't deal with trailing slash! */
2430 len = strlen(path);
2431 while (len > 0 && path[len - 1] == '/') {
2432 path[len - 1] = '\0';
2433 len--;
2435 done:
2436 free(resolved);
2437 free(cwd);
2438 if (err == NULL)
2439 *wt_path = path;
2440 else
2441 free(path);
2442 return err;
2445 static const struct got_error *
2446 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2447 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2448 struct got_repository *repo)
2450 const struct got_error *err = NULL;
2451 struct got_fileindex_entry *ie;
2453 /* Re-adding an existing entry is a no-op. */
2454 if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2455 return NULL;
2457 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2458 if (err)
2459 return err;
2461 err = got_fileindex_entry_add(fileindex, ie);
2462 if (err) {
2463 got_fileindex_entry_free(ie);
2464 return err;
2467 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2470 const struct got_error *
2471 got_worktree_schedule_add(struct got_worktree *worktree,
2472 struct got_pathlist_head *ondisk_paths,
2473 got_worktree_status_cb status_cb, void *status_arg,
2474 struct got_repository *repo)
2476 struct got_fileindex *fileindex = NULL;
2477 char *fileindex_path = NULL;
2478 const struct got_error *err = NULL, *sync_err, *unlockerr;
2479 struct got_pathlist_entry *pe;
2481 err = lock_worktree(worktree, LOCK_EX);
2482 if (err)
2483 return err;
2485 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2486 if (err)
2487 goto done;
2489 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2490 char *relpath;
2491 err = got_path_skip_common_ancestor(&relpath,
2492 got_worktree_get_root_path(worktree), pe->path);
2493 if (err)
2494 break;
2495 err = schedule_addition(pe->path, fileindex, relpath,
2496 status_cb, status_arg, repo);
2497 free(relpath);
2498 if (err)
2499 break;
2501 sync_err = sync_fileindex(fileindex, fileindex_path);
2502 if (sync_err && err == NULL)
2503 err = sync_err;
2504 done:
2505 free(fileindex_path);
2506 if (fileindex)
2507 got_fileindex_free(fileindex);
2508 unlockerr = lock_worktree(worktree, LOCK_SH);
2509 if (unlockerr && err == NULL)
2510 err = unlockerr;
2511 return err;
2514 static const struct got_error *
2515 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2516 const char *relpath, int delete_local_mods,
2517 got_worktree_status_cb status_cb, void *status_arg,
2518 struct got_repository *repo)
2520 const struct got_error *err = NULL;
2521 struct got_fileindex_entry *ie = NULL;
2522 unsigned char status;
2523 struct stat sb;
2525 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2526 if (ie == NULL)
2527 return got_error(GOT_ERR_BAD_PATH);
2529 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2530 if (err)
2531 return err;
2533 if (status != GOT_STATUS_NO_CHANGE) {
2534 if (status == GOT_STATUS_DELETE)
2535 return NULL;
2536 if (status != GOT_STATUS_MODIFY)
2537 return got_error(GOT_ERR_FILE_STATUS);
2538 if (!delete_local_mods)
2539 return got_error(GOT_ERR_FILE_MODIFIED);
2542 if (unlink(ondisk_path) != 0)
2543 return got_error_from_errno2("unlink", ondisk_path);
2545 got_fileindex_entry_mark_deleted_from_disk(ie);
2546 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2549 const struct got_error *
2550 got_worktree_schedule_delete(struct got_worktree *worktree,
2551 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2552 got_worktree_status_cb status_cb, void *status_arg,
2553 struct got_repository *repo)
2555 struct got_fileindex *fileindex = NULL;
2556 char *fileindex_path = NULL;
2557 const struct got_error *err = NULL, *sync_err, *unlockerr;
2558 struct got_pathlist_entry *pe;
2560 err = lock_worktree(worktree, LOCK_EX);
2561 if (err)
2562 return err;
2564 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2565 if (err)
2566 goto done;
2568 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2569 char *relpath;
2570 err = got_path_skip_common_ancestor(&relpath,
2571 got_worktree_get_root_path(worktree), pe->path);
2572 if (err)
2573 break;
2574 err = schedule_for_deletion(pe->path, fileindex, relpath,
2575 delete_local_mods, status_cb, status_arg, repo);
2576 free(relpath);
2577 if (err)
2578 break;
2580 sync_err = sync_fileindex(fileindex, fileindex_path);
2581 if (sync_err && err == NULL)
2582 err = sync_err;
2583 done:
2584 free(fileindex_path);
2585 if (fileindex)
2586 got_fileindex_free(fileindex);
2587 unlockerr = lock_worktree(worktree, LOCK_SH);
2588 if (unlockerr && err == NULL)
2589 err = unlockerr;
2590 return err;
2593 static const struct got_error *
2594 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2595 const char *ondisk_path,
2596 got_worktree_checkout_cb progress_cb, void *progress_arg,
2597 struct got_repository *repo)
2599 const struct got_error *err = NULL;
2600 char *relpath = NULL, *parent_path = NULL;
2601 struct got_fileindex_entry *ie;
2602 struct got_tree_object *tree = NULL;
2603 struct got_object_id *tree_id = NULL;
2604 const struct got_tree_entry *te;
2605 char *tree_path = NULL, *te_name;
2606 struct got_blob_object *blob = NULL;
2607 unsigned char status;
2608 struct stat sb;
2610 err = got_path_skip_common_ancestor(&relpath,
2611 got_worktree_get_root_path(worktree), ondisk_path);
2612 if (err)
2613 goto done;
2615 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2616 if (ie == NULL) {
2617 err = got_error(GOT_ERR_BAD_PATH);
2618 goto done;
2621 /* Construct in-repository path of tree which contains this blob. */
2622 err = got_path_dirname(&parent_path, ie->path);
2623 if (err) {
2624 if (err->code != GOT_ERR_BAD_PATH)
2625 goto done;
2626 parent_path = strdup("/");
2627 if (parent_path == NULL) {
2628 err = got_error_from_errno("strdup");
2629 goto done;
2632 if (got_path_is_root_dir(worktree->path_prefix)) {
2633 tree_path = strdup(parent_path);
2634 if (tree_path == NULL) {
2635 err = got_error_from_errno("strdup");
2636 goto done;
2638 } else {
2639 if (got_path_is_root_dir(parent_path)) {
2640 tree_path = strdup(worktree->path_prefix);
2641 if (tree_path == NULL) {
2642 err = got_error_from_errno("strdup");
2643 goto done;
2645 } else {
2646 if (asprintf(&tree_path, "%s/%s",
2647 worktree->path_prefix, parent_path) == -1) {
2648 err = got_error_from_errno("asprintf");
2649 goto done;
2654 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2655 if (err)
2656 goto done;
2657 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2658 sb.st_mode = got_fileindex_perms_to_st(ie);
2660 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2661 tree_path);
2662 if (err) {
2663 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2664 status == GOT_STATUS_ADD))
2665 goto done;
2666 } else {
2667 err = got_object_open_as_tree(&tree, repo, tree_id);
2668 if (err)
2669 goto done;
2671 te_name = basename(ie->path);
2672 if (te_name == NULL) {
2673 err = got_error_from_errno2("basename", ie->path);
2674 goto done;
2677 te = got_object_tree_find_entry(tree, te_name);
2678 if (te == NULL && status != GOT_STATUS_ADD) {
2679 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2680 goto done;
2684 switch (status) {
2685 case GOT_STATUS_ADD:
2686 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2687 if (err)
2688 goto done;
2689 got_fileindex_entry_remove(fileindex, ie);
2690 break;
2691 case GOT_STATUS_DELETE:
2692 case GOT_STATUS_MODIFY:
2693 case GOT_STATUS_CONFLICT:
2694 case GOT_STATUS_MISSING: {
2695 struct got_object_id id;
2696 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2697 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2698 if (err)
2699 goto done;
2700 err = install_blob(worktree, ondisk_path, ie->path,
2701 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2702 progress_arg);
2703 if (err)
2704 goto done;
2705 if (status == GOT_STATUS_DELETE) {
2706 err = update_blob_fileindex_entry(worktree,
2707 fileindex, ie, ondisk_path, ie->path, blob, 1);
2708 if (err)
2709 goto done;
2711 break;
2713 default:
2714 goto done;
2716 done:
2717 free(relpath);
2718 free(parent_path);
2719 free(tree_path);
2720 if (blob)
2721 got_object_blob_close(blob);
2722 if (tree)
2723 got_object_tree_close(tree);
2724 free(tree_id);
2725 return err;
2728 const struct got_error *
2729 got_worktree_revert(struct got_worktree *worktree,
2730 struct got_pathlist_head *ondisk_paths,
2731 got_worktree_checkout_cb progress_cb, void *progress_arg,
2732 struct got_repository *repo)
2734 struct got_fileindex *fileindex = NULL;
2735 char *fileindex_path = NULL;
2736 const struct got_error *err = NULL, *unlockerr = NULL;
2737 const struct got_error *sync_err = NULL;
2738 struct got_pathlist_entry *pe;
2740 err = lock_worktree(worktree, LOCK_EX);
2741 if (err)
2742 return err;
2744 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2745 if (err)
2746 goto done;
2748 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2749 err = revert_file(worktree, fileindex, pe->path,
2750 progress_cb, progress_arg, repo);
2751 if (err)
2752 break;
2754 sync_err = sync_fileindex(fileindex, fileindex_path);
2755 if (sync_err && err == NULL)
2756 err = sync_err;
2757 done:
2758 free(fileindex_path);
2759 if (fileindex)
2760 got_fileindex_free(fileindex);
2761 unlockerr = lock_worktree(worktree, LOCK_SH);
2762 if (unlockerr && err == NULL)
2763 err = unlockerr;
2764 return err;
2767 static void
2768 free_commitable(struct got_commitable *ct)
2770 free(ct->path);
2771 free(ct->in_repo_path);
2772 free(ct->ondisk_path);
2773 free(ct->blob_id);
2774 free(ct->base_blob_id);
2775 free(ct->base_commit_id);
2776 free(ct);
2779 struct collect_commitables_arg {
2780 struct got_pathlist_head *commitable_paths;
2781 struct got_repository *repo;
2782 struct got_worktree *worktree;
2785 static const struct got_error *
2786 collect_commitables(void *arg, unsigned char status, const char *relpath,
2787 struct got_object_id *blob_id, struct got_object_id *commit_id)
2789 struct collect_commitables_arg *a = arg;
2790 const struct got_error *err = NULL;
2791 struct got_commitable *ct = NULL;
2792 struct got_pathlist_entry *new = NULL;
2793 char *parent_path = NULL, *path = NULL;
2794 struct stat sb;
2796 if (status == GOT_STATUS_CONFLICT)
2797 return got_error(GOT_ERR_COMMIT_CONFLICT);
2799 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2800 status != GOT_STATUS_DELETE)
2801 return NULL;
2803 if (asprintf(&path, "/%s", relpath) == -1) {
2804 err = got_error_from_errno("asprintf");
2805 goto done;
2807 if (strcmp(path, "/") == 0) {
2808 parent_path = strdup("");
2809 if (parent_path == NULL)
2810 return got_error_from_errno("strdup");
2811 } else {
2812 err = got_path_dirname(&parent_path, path);
2813 if (err)
2814 return err;
2817 ct = calloc(1, sizeof(*ct));
2818 if (ct == NULL) {
2819 err = got_error_from_errno("calloc");
2820 goto done;
2823 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2824 relpath) == -1) {
2825 err = got_error_from_errno("asprintf");
2826 goto done;
2828 if (status == GOT_STATUS_DELETE) {
2829 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2830 } else {
2831 if (lstat(ct->ondisk_path, &sb) != 0) {
2832 err = got_error_from_errno2("lstat", ct->ondisk_path);
2833 goto done;
2835 ct->mode = sb.st_mode;
2838 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2839 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2840 relpath) == -1) {
2841 err = got_error_from_errno("asprintf");
2842 goto done;
2845 ct->status = status;
2846 ct->blob_id = NULL; /* will be filled in when blob gets created */
2847 if (ct->status != GOT_STATUS_ADD) {
2848 ct->base_blob_id = got_object_id_dup(blob_id);
2849 if (ct->base_blob_id == NULL) {
2850 err = got_error_from_errno("got_object_id_dup");
2851 goto done;
2853 ct->base_commit_id = got_object_id_dup(commit_id);
2854 if (ct->base_commit_id == NULL) {
2855 err = got_error_from_errno("got_object_id_dup");
2856 goto done;
2859 ct->path = strdup(path);
2860 if (ct->path == NULL) {
2861 err = got_error_from_errno("strdup");
2862 goto done;
2864 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2865 done:
2866 if (ct && (err || new == NULL))
2867 free_commitable(ct);
2868 free(parent_path);
2869 free(path);
2870 return err;
2873 static const struct got_error *write_tree(struct got_object_id **,
2874 struct got_tree_object *, const char *, struct got_pathlist_head *,
2875 got_worktree_status_cb status_cb, void *status_arg,
2876 struct got_repository *);
2878 static const struct got_error *
2879 write_subtree(struct got_object_id **new_subtree_id,
2880 struct got_tree_entry *te, const char *parent_path,
2881 struct got_pathlist_head *commitable_paths,
2882 got_worktree_status_cb status_cb, void *status_arg,
2883 struct got_repository *repo)
2885 const struct got_error *err = NULL;
2886 struct got_tree_object *subtree;
2887 char *subpath;
2889 if (asprintf(&subpath, "%s%s%s", parent_path,
2890 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2891 return got_error_from_errno("asprintf");
2893 err = got_object_open_as_tree(&subtree, repo, te->id);
2894 if (err)
2895 return err;
2897 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2898 status_cb, status_arg, repo);
2899 got_object_tree_close(subtree);
2900 free(subpath);
2901 return err;
2904 static const struct got_error *
2905 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2907 const struct got_error *err = NULL;
2908 char *ct_parent_path = NULL;
2910 *match = 0;
2912 if (strchr(ct->in_repo_path, '/') == NULL) {
2913 *match = got_path_is_root_dir(path);
2914 return NULL;
2917 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2918 if (err)
2919 return err;
2920 *match = (strcmp(path, ct_parent_path) == 0);
2921 free(ct_parent_path);
2922 return err;
2925 static mode_t
2926 get_ct_file_mode(struct got_commitable *ct)
2928 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2931 static const struct got_error *
2932 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2933 struct got_tree_entry *te, struct got_commitable *ct)
2935 const struct got_error *err = NULL;
2937 *new_te = NULL;
2939 err = got_object_tree_entry_dup(new_te, te);
2940 if (err)
2941 goto done;
2943 (*new_te)->mode = get_ct_file_mode(ct);
2945 free((*new_te)->id);
2946 (*new_te)->id = got_object_id_dup(ct->blob_id);
2947 if ((*new_te)->id == NULL) {
2948 err = got_error_from_errno("got_object_id_dup");
2949 goto done;
2951 done:
2952 if (err && *new_te) {
2953 got_object_tree_entry_close(*new_te);
2954 *new_te = NULL;
2956 return err;
2959 static const struct got_error *
2960 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2961 struct got_commitable *ct)
2963 const struct got_error *err = NULL;
2964 char *ct_name;
2966 *new_te = NULL;
2968 *new_te = calloc(1, sizeof(**new_te));
2969 if (*new_te == NULL)
2970 return got_error_from_errno("calloc");
2972 ct_name = basename(ct->path);
2973 if (ct_name == NULL) {
2974 err = got_error_from_errno2("basename", ct->path);
2975 goto done;
2977 (*new_te)->name = strdup(ct_name);
2978 if ((*new_te)->name == NULL) {
2979 err = got_error_from_errno("strdup");
2980 goto done;
2983 (*new_te)->mode = get_ct_file_mode(ct);
2985 (*new_te)->id = got_object_id_dup(ct->blob_id);
2986 if ((*new_te)->id == NULL) {
2987 err = got_error_from_errno("got_object_id_dup");
2988 goto done;
2990 done:
2991 if (err && *new_te) {
2992 got_object_tree_entry_close(*new_te);
2993 *new_te = NULL;
2995 return err;
2998 static const struct got_error *
2999 insert_tree_entry(struct got_tree_entry *new_te,
3000 struct got_pathlist_head *paths)
3002 const struct got_error *err = NULL;
3003 struct got_pathlist_entry *new_pe;
3005 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3006 if (err)
3007 return err;
3008 if (new_pe == NULL)
3009 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3010 return NULL;
3013 static const struct got_error *
3014 report_ct_status(struct got_commitable *ct,
3015 got_worktree_status_cb status_cb, void *status_arg)
3017 const char *ct_path = ct->path;
3018 while (ct_path[0] == '/')
3019 ct_path++;
3020 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
3023 static const struct got_error *
3024 match_modified_subtree(int *modified, struct got_tree_entry *te,
3025 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3027 const struct got_error *err = NULL;
3028 struct got_pathlist_entry *pe;
3029 char *te_path;
3031 *modified = 0;
3033 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3034 got_path_is_root_dir(base_tree_path) ? "" : "/",
3035 te->name) == -1)
3036 return got_error_from_errno("asprintf");
3038 TAILQ_FOREACH(pe, commitable_paths, entry) {
3039 struct got_commitable *ct = pe->data;
3040 *modified = got_path_is_child(ct->in_repo_path, te_path,
3041 strlen(te_path));
3042 if (*modified)
3043 break;
3046 free(te_path);
3047 return err;
3050 static const struct got_error *
3051 match_deleted_or_modified_ct(struct got_commitable **ctp,
3052 struct got_tree_entry *te, const char *base_tree_path,
3053 struct got_pathlist_head *commitable_paths)
3055 const struct got_error *err = NULL;
3056 struct got_pathlist_entry *pe;
3058 *ctp = NULL;
3060 TAILQ_FOREACH(pe, commitable_paths, entry) {
3061 struct got_commitable *ct = pe->data;
3062 char *ct_name = NULL;
3063 int path_matches;
3065 if (ct->status != GOT_STATUS_MODIFY &&
3066 ct->status != GOT_STATUS_DELETE)
3067 continue;
3069 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3070 continue;
3072 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3073 if (err)
3074 return err;
3075 if (!path_matches)
3076 continue;
3078 ct_name = basename(pe->path);
3079 if (ct_name == NULL)
3080 return got_error_from_errno2("basename", pe->path);
3082 if (strcmp(te->name, ct_name) != 0)
3083 continue;
3085 *ctp = ct;
3086 break;
3089 return err;
3092 static const struct got_error *
3093 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3094 const char *child_path, const char *path_base_tree,
3095 struct got_pathlist_head *commitable_paths,
3096 got_worktree_status_cb status_cb, void *status_arg,
3097 struct got_repository *repo)
3099 const struct got_error *err = NULL;
3100 struct got_tree_entry *new_te;
3101 char *subtree_path;
3103 *new_tep = NULL;
3105 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3106 got_path_is_root_dir(path_base_tree) ? "" : "/",
3107 child_path) == -1)
3108 return got_error_from_errno("asprintf");
3110 new_te = calloc(1, sizeof(*new_te));
3111 new_te->mode = S_IFDIR;
3112 new_te->name = strdup(child_path);
3113 if (new_te->name == NULL) {
3114 err = got_error_from_errno("strdup");
3115 got_object_tree_entry_close(new_te);
3116 goto done;
3118 err = write_tree(&new_te->id, NULL, subtree_path,
3119 commitable_paths, status_cb, status_arg, repo);
3120 if (err) {
3121 got_object_tree_entry_close(new_te);
3122 goto done;
3124 done:
3125 free(subtree_path);
3126 if (err == NULL)
3127 *new_tep = new_te;
3128 return err;
3131 static const struct got_error *
3132 write_tree(struct got_object_id **new_tree_id,
3133 struct got_tree_object *base_tree, const char *path_base_tree,
3134 struct got_pathlist_head *commitable_paths,
3135 got_worktree_status_cb status_cb, void *status_arg,
3136 struct got_repository *repo)
3138 const struct got_error *err = NULL;
3139 const struct got_tree_entries *base_entries = NULL;
3140 struct got_pathlist_head paths;
3141 struct got_tree_entries new_tree_entries;
3142 struct got_tree_entry *te, *new_te = NULL;
3143 struct got_pathlist_entry *pe;
3145 TAILQ_INIT(&paths);
3146 new_tree_entries.nentries = 0;
3147 SIMPLEQ_INIT(&new_tree_entries.head);
3149 /* Insert, and recurse into, newly added entries first. */
3150 TAILQ_FOREACH(pe, commitable_paths, entry) {
3151 struct got_commitable *ct = pe->data;
3152 char *child_path = NULL, *slash;
3154 if (ct->status != GOT_STATUS_ADD ||
3155 (ct->flags & GOT_COMMITABLE_ADDED))
3156 continue;
3158 if (!got_path_is_child(pe->path, path_base_tree,
3159 strlen(path_base_tree)))
3160 continue;
3162 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3163 pe->path);
3164 if (err)
3165 goto done;
3167 slash = strchr(child_path, '/');
3168 if (slash == NULL) {
3169 err = alloc_added_blob_tree_entry(&new_te, ct);
3170 if (err)
3171 goto done;
3172 err = report_ct_status(ct, status_cb, status_arg);
3173 if (err)
3174 goto done;
3175 ct->flags |= GOT_COMMITABLE_ADDED;
3176 err = insert_tree_entry(new_te, &paths);
3177 if (err)
3178 goto done;
3179 } else {
3180 *slash = '\0'; /* trim trailing path components */
3181 if (base_tree == NULL ||
3182 got_object_tree_find_entry(base_tree, child_path)
3183 == NULL) {
3184 err = make_subtree_for_added_blob(&new_te,
3185 child_path, path_base_tree,
3186 commitable_paths, status_cb, status_arg,
3187 repo);
3188 if (err)
3189 goto done;
3190 err = insert_tree_entry(new_te, &paths);
3191 if (err)
3192 goto done;
3197 if (base_tree) {
3198 /* Handle modified and deleted entries. */
3199 base_entries = got_object_tree_get_entries(base_tree);
3200 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3201 struct got_commitable *ct = NULL;
3203 if (S_ISDIR(te->mode)) {
3204 int modified;
3205 err = got_object_tree_entry_dup(&new_te, te);
3206 if (err)
3207 goto done;
3208 err = match_modified_subtree(&modified, te,
3209 path_base_tree, commitable_paths);
3210 if (err)
3211 goto done;
3212 /* Avoid recursion into unmodified subtrees. */
3213 if (modified) {
3214 free(new_te->id);
3215 err = write_subtree(&new_te->id, te,
3216 path_base_tree, commitable_paths,
3217 status_cb, status_arg, repo);
3218 if (err)
3219 goto done;
3221 err = insert_tree_entry(new_te, &paths);
3222 if (err)
3223 goto done;
3224 continue;
3227 err = match_deleted_or_modified_ct(&ct, te,
3228 path_base_tree, commitable_paths);
3229 if (ct) {
3230 /* NB: Deleted entries get dropped here. */
3231 if (ct->status == GOT_STATUS_MODIFY) {
3232 err = alloc_modified_blob_tree_entry(
3233 &new_te, te, ct);
3234 if (err)
3235 goto done;
3236 err = insert_tree_entry(new_te, &paths);
3237 if (err)
3238 goto done;
3240 err = report_ct_status(ct, status_cb,
3241 status_arg);
3242 if (err)
3243 goto done;
3244 } else {
3245 /* Entry is unchanged; just copy it. */
3246 err = got_object_tree_entry_dup(&new_te, te);
3247 if (err)
3248 goto done;
3249 err = insert_tree_entry(new_te, &paths);
3250 if (err)
3251 goto done;
3256 /* Write new list of entries; deleted entries have been dropped. */
3257 TAILQ_FOREACH(pe, &paths, entry) {
3258 struct got_tree_entry *te = pe->data;
3259 new_tree_entries.nentries++;
3260 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3262 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3263 done:
3264 got_object_tree_entries_close(&new_tree_entries);
3265 got_pathlist_free(&paths);
3266 return err;
3269 static const struct got_error *
3270 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3271 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3273 const struct got_error *err = NULL;
3274 struct got_pathlist_entry *pe;
3276 TAILQ_FOREACH(pe, commitable_paths, entry) {
3277 struct got_fileindex_entry *ie;
3278 struct got_commitable *ct = pe->data;
3280 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3281 if (ie) {
3282 if (ct->status == GOT_STATUS_DELETE) {
3283 got_fileindex_entry_remove(fileindex, ie);
3284 got_fileindex_entry_free(ie);
3285 } else
3286 err = got_fileindex_entry_update(ie,
3287 ct->ondisk_path, ct->blob_id->sha1,
3288 new_base_commit_id->sha1, 1);
3289 } else {
3290 err = got_fileindex_entry_alloc(&ie,
3291 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3292 new_base_commit_id->sha1);
3293 if (err)
3294 break;
3295 err = got_fileindex_entry_add(fileindex, ie);
3296 if (err)
3297 break;
3300 return err;
3303 static const struct got_error *
3304 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3305 struct got_object_id *head_commit_id)
3307 const struct got_error *err = NULL;
3308 struct got_object_id *id = NULL;
3309 struct got_commit_object *commit = NULL;
3310 const char *ct_path = ct->in_repo_path;
3312 while (ct_path[0] == '/')
3313 ct_path++;
3315 if (ct->status != GOT_STATUS_ADD) {
3316 /* Trivial case: base commit == head commit */
3317 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3318 return NULL;
3320 * Ensure file content which local changes were based
3321 * on matches file content in the branch head.
3323 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3324 if (err) {
3325 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3326 goto done;
3327 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3328 goto done;
3329 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3330 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3331 } else {
3332 /* Require that added files don't exist in the branch head. */
3333 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3334 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3335 goto done;
3336 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3338 done:
3339 if (commit)
3340 got_object_commit_close(commit);
3341 free(id);
3342 return err;
3345 const struct got_error *
3346 commit_worktree(struct got_object_id **new_commit_id,
3347 struct got_pathlist_head *commitable_paths,
3348 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3349 const char *author, const char *committer,
3350 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3351 got_worktree_status_cb status_cb, void *status_arg,
3352 struct got_repository *repo)
3354 const struct got_error *err = NULL, *unlockerr = NULL;
3355 struct got_pathlist_entry *pe;
3356 const char *head_ref_name = NULL;
3357 struct got_commit_object *head_commit = NULL;
3358 struct got_reference *head_ref2 = NULL;
3359 struct got_object_id *head_commit_id2 = NULL;
3360 struct got_tree_object *head_tree = NULL;
3361 struct got_object_id *new_tree_id = NULL;
3362 struct got_object_id_queue parent_ids;
3363 struct got_object_qid *pid = NULL;
3364 char *logmsg = NULL;
3366 *new_commit_id = NULL;
3368 SIMPLEQ_INIT(&parent_ids);
3370 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3371 if (err)
3372 goto done;
3374 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3375 if (err)
3376 goto done;
3378 if (commit_msg_cb != NULL) {
3379 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3380 if (err)
3381 goto done;
3384 if (logmsg == NULL || strlen(logmsg) == 0) {
3385 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3386 goto done;
3389 /* Create blobs from added and modified files and record their IDs. */
3390 TAILQ_FOREACH(pe, commitable_paths, entry) {
3391 struct got_commitable *ct = pe->data;
3392 char *ondisk_path;
3394 if (ct->status != GOT_STATUS_ADD &&
3395 ct->status != GOT_STATUS_MODIFY)
3396 continue;
3398 if (asprintf(&ondisk_path, "%s/%s",
3399 worktree->root_path, pe->path) == -1) {
3400 err = got_error_from_errno("asprintf");
3401 goto done;
3403 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3404 free(ondisk_path);
3405 if (err)
3406 goto done;
3409 /* Recursively write new tree objects. */
3410 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3411 status_cb, status_arg, repo);
3412 if (err)
3413 goto done;
3415 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3416 if (err)
3417 goto done;
3418 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3419 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3420 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3421 got_object_qid_free(pid);
3422 if (logmsg != NULL)
3423 free(logmsg);
3424 if (err)
3425 goto done;
3427 /* Check if a concurrent commit to our branch has occurred. */
3428 head_ref_name = got_worktree_get_head_ref_name(worktree);
3429 if (head_ref_name == NULL) {
3430 err = got_error_from_errno("got_worktree_get_head_ref_name");
3431 goto done;
3433 /* Lock the reference here to prevent concurrent modification. */
3434 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3435 if (err)
3436 goto done;
3437 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3438 if (err)
3439 goto done;
3440 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3441 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3442 goto done;
3444 /* Update branch head in repository. */
3445 err = got_ref_change_ref(head_ref2, *new_commit_id);
3446 if (err)
3447 goto done;
3448 err = got_ref_write(head_ref2, repo);
3449 if (err)
3450 goto done;
3452 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3453 if (err)
3454 goto done;
3456 err = ref_base_commit(worktree, repo);
3457 if (err)
3458 goto done;
3459 done:
3460 if (head_tree)
3461 got_object_tree_close(head_tree);
3462 if (head_commit)
3463 got_object_commit_close(head_commit);
3464 free(head_commit_id2);
3465 if (head_ref2) {
3466 unlockerr = got_ref_unlock(head_ref2);
3467 if (unlockerr && err == NULL)
3468 err = unlockerr;
3469 got_ref_close(head_ref2);
3471 return err;
3474 static const struct got_error *
3475 check_path_is_commitable(const char *path,
3476 struct got_pathlist_head *commitable_paths)
3478 struct got_pathlist_entry *cpe = NULL;
3479 size_t path_len = strlen(path);
3481 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3482 struct got_commitable *ct = cpe->data;
3483 const char *ct_path = ct->path;
3485 while (ct_path[0] == '/')
3486 ct_path++;
3488 if (strcmp(path, ct_path) == 0 ||
3489 got_path_is_child(ct_path, path, path_len))
3490 break;
3493 if (cpe == NULL)
3494 return got_error_path(path, GOT_ERR_BAD_PATH);
3496 return NULL;
3499 const struct got_error *
3500 got_worktree_commit(struct got_object_id **new_commit_id,
3501 struct got_worktree *worktree, struct got_pathlist_head *paths,
3502 const char *author, const char *committer,
3503 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3504 got_worktree_status_cb status_cb, void *status_arg,
3505 struct got_repository *repo)
3507 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3508 struct got_fileindex *fileindex = NULL;
3509 char *fileindex_path = NULL;
3510 struct got_pathlist_head commitable_paths;
3511 struct collect_commitables_arg cc_arg;
3512 struct got_pathlist_entry *pe;
3513 struct got_reference *head_ref = NULL;
3514 struct got_object_id *head_commit_id = NULL;
3516 *new_commit_id = NULL;
3518 TAILQ_INIT(&commitable_paths);
3520 err = lock_worktree(worktree, LOCK_EX);
3521 if (err)
3522 goto done;
3524 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3525 if (err)
3526 goto done;
3528 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3529 if (err)
3530 goto done;
3532 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3533 if (err)
3534 goto done;
3536 cc_arg.commitable_paths = &commitable_paths;
3537 cc_arg.worktree = worktree;
3538 cc_arg.repo = repo;
3539 TAILQ_FOREACH(pe, paths, entry) {
3540 err = worktree_status(worktree, pe->path, fileindex, repo,
3541 collect_commitables, &cc_arg, NULL, NULL);
3542 if (err)
3543 goto done;
3546 if (TAILQ_EMPTY(&commitable_paths)) {
3547 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3548 goto done;
3551 TAILQ_FOREACH(pe, paths, entry) {
3552 err = check_path_is_commitable(pe->path, &commitable_paths);
3553 if (err)
3554 goto done;
3557 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3558 struct got_commitable *ct = pe->data;
3559 err = check_ct_out_of_date(ct, repo, head_commit_id);
3560 if (err)
3561 goto done;
3564 err = commit_worktree(new_commit_id, &commitable_paths,
3565 head_commit_id, worktree, author, committer,
3566 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3567 if (err)
3568 goto done;
3570 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3571 fileindex);
3572 sync_err = sync_fileindex(fileindex, fileindex_path);
3573 if (sync_err && err == NULL)
3574 err = sync_err;
3575 done:
3576 if (fileindex)
3577 got_fileindex_free(fileindex);
3578 free(fileindex_path);
3579 unlockerr = lock_worktree(worktree, LOCK_SH);
3580 if (unlockerr && err == NULL)
3581 err = unlockerr;
3582 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3583 struct got_commitable *ct = pe->data;
3584 free_commitable(ct);
3586 got_pathlist_free(&commitable_paths);
3587 return err;
3590 const char *
3591 got_commitable_get_path(struct got_commitable *ct)
3593 return ct->path;
3596 unsigned int
3597 got_commitable_get_status(struct got_commitable *ct)
3599 return ct->status;
3602 struct check_rebase_ok_arg {
3603 struct got_worktree *worktree;
3604 struct got_repository *repo;
3607 static const struct got_error *
3608 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3610 const struct got_error *err = NULL;
3611 struct check_rebase_ok_arg *a = arg;
3612 unsigned char status;
3613 struct stat sb;
3614 char *ondisk_path;
3616 /* Reject rebase of a work tree with mixed base commits. */
3617 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3618 SHA1_DIGEST_LENGTH))
3619 return got_error(GOT_ERR_MIXED_COMMITS);
3621 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3622 == -1)
3623 return got_error_from_errno("asprintf");
3625 /* Reject rebase of a work tree with modified or conflicted files. */
3626 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3627 free(ondisk_path);
3628 if (err)
3629 return err;
3631 if (status != GOT_STATUS_NO_CHANGE)
3632 return got_error(GOT_ERR_MODIFIED);
3634 return NULL;
3637 const struct got_error *
3638 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3639 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3640 struct got_worktree *worktree, struct got_reference *branch,
3641 struct got_repository *repo)
3643 const struct got_error *err = NULL;
3644 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3645 char *branch_ref_name = NULL;
3646 char *fileindex_path = NULL;
3647 struct check_rebase_ok_arg ok_arg;
3648 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3650 *new_base_branch_ref = NULL;
3651 *tmp_branch = NULL;
3652 *fileindex = NULL;
3654 err = lock_worktree(worktree, LOCK_EX);
3655 if (err)
3656 return err;
3658 err = open_fileindex(fileindex, &fileindex_path, worktree);
3659 if (err)
3660 goto done;
3662 ok_arg.worktree = worktree;
3663 ok_arg.repo = repo;
3664 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3665 &ok_arg);
3666 if (err)
3667 goto done;
3669 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3670 if (err)
3671 goto done;
3673 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3674 if (err)
3675 goto done;
3677 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3678 if (err)
3679 goto done;
3681 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3682 0);
3683 if (err)
3684 goto done;
3686 err = got_ref_alloc_symref(new_base_branch_ref,
3687 new_base_branch_ref_name, wt_branch);
3688 if (err)
3689 goto done;
3690 err = got_ref_write(*new_base_branch_ref, repo);
3691 if (err)
3692 goto done;
3694 /* TODO Lock original branch's ref while rebasing? */
3696 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3697 if (err)
3698 goto done;
3700 err = got_ref_write(branch_ref, repo);
3701 if (err)
3702 goto done;
3704 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3705 worktree->base_commit_id);
3706 if (err)
3707 goto done;
3708 err = got_ref_write(*tmp_branch, repo);
3709 if (err)
3710 goto done;
3712 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3713 if (err)
3714 goto done;
3715 done:
3716 free(fileindex_path);
3717 free(tmp_branch_name);
3718 free(new_base_branch_ref_name);
3719 free(branch_ref_name);
3720 if (branch_ref)
3721 got_ref_close(branch_ref);
3722 if (wt_branch)
3723 got_ref_close(wt_branch);
3724 if (err) {
3725 if (*new_base_branch_ref) {
3726 got_ref_close(*new_base_branch_ref);
3727 *new_base_branch_ref = NULL;
3729 if (*tmp_branch) {
3730 got_ref_close(*tmp_branch);
3731 *tmp_branch = NULL;
3733 if (*fileindex) {
3734 got_fileindex_free(*fileindex);
3735 *fileindex = NULL;
3737 lock_worktree(worktree, LOCK_SH);
3739 return err;
3742 const struct got_error *
3743 got_worktree_rebase_continue(struct got_object_id **commit_id,
3744 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3745 struct got_reference **branch, struct got_fileindex **fileindex,
3746 struct got_worktree *worktree, struct got_repository *repo)
3748 const struct got_error *err;
3749 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3750 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3751 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3752 char *fileindex_path = NULL;
3754 *commit_id = NULL;
3755 *new_base_branch = NULL;
3756 *tmp_branch = NULL;
3757 *branch = NULL;
3758 *fileindex = NULL;
3760 err = lock_worktree(worktree, LOCK_EX);
3761 if (err)
3762 return err;
3764 err = open_fileindex(fileindex, &fileindex_path, worktree);
3765 if (err)
3766 goto done;
3768 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3769 if (err)
3770 return err;
3772 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3773 if (err)
3774 goto done;
3776 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3777 if (err)
3778 goto done;
3780 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3781 if (err)
3782 goto done;
3784 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3785 if (err)
3786 goto done;
3788 err = got_ref_open(branch, repo,
3789 got_ref_get_symref_target(branch_ref), 0);
3790 if (err)
3791 goto done;
3793 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3794 if (err)
3795 goto done;
3797 err = got_ref_resolve(commit_id, repo, commit_ref);
3798 if (err)
3799 goto done;
3801 err = got_ref_open(new_base_branch, repo,
3802 new_base_branch_ref_name, 0);
3803 if (err)
3804 goto done;
3806 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3807 if (err)
3808 goto done;
3809 done:
3810 free(commit_ref_name);
3811 free(branch_ref_name);
3812 free(fileindex_path);
3813 if (commit_ref)
3814 got_ref_close(commit_ref);
3815 if (branch_ref)
3816 got_ref_close(branch_ref);
3817 if (err) {
3818 free(*commit_id);
3819 *commit_id = NULL;
3820 if (*tmp_branch) {
3821 got_ref_close(*tmp_branch);
3822 *tmp_branch = NULL;
3824 if (*new_base_branch) {
3825 got_ref_close(*new_base_branch);
3826 *new_base_branch = NULL;
3828 if (*branch) {
3829 got_ref_close(*branch);
3830 *branch = NULL;
3832 if (*fileindex) {
3833 got_fileindex_free(*fileindex);
3834 *fileindex = NULL;
3836 lock_worktree(worktree, LOCK_SH);
3838 return err;
3841 const struct got_error *
3842 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3844 const struct got_error *err;
3845 char *tmp_branch_name = NULL;
3847 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3848 if (err)
3849 return err;
3851 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3852 free(tmp_branch_name);
3853 return NULL;
3856 static const struct got_error *
3857 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3858 char **logmsg, void *arg)
3860 *logmsg = arg;
3861 return NULL;
3864 static const struct got_error *
3865 rebase_status(void *arg, unsigned char status, const char *path,
3866 struct got_object_id *blob_id, struct got_object_id *commit_id)
3868 return NULL;
3871 struct collect_merged_paths_arg {
3872 got_worktree_checkout_cb progress_cb;
3873 void *progress_arg;
3874 struct got_pathlist_head *merged_paths;
3877 static const struct got_error *
3878 collect_merged_paths(void *arg, unsigned char status, const char *path)
3880 const struct got_error *err;
3881 struct collect_merged_paths_arg *a = arg;
3882 char *p;
3883 struct got_pathlist_entry *new;
3885 err = (*a->progress_cb)(a->progress_arg, status, path);
3886 if (err)
3887 return err;
3889 if (status != GOT_STATUS_MERGE &&
3890 status != GOT_STATUS_ADD &&
3891 status != GOT_STATUS_DELETE &&
3892 status != GOT_STATUS_CONFLICT)
3893 return NULL;
3895 p = strdup(path);
3896 if (p == NULL)
3897 return got_error_from_errno("strdup");
3899 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3900 if (err || new == NULL)
3901 free(p);
3902 return err;
3905 void
3906 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3908 struct got_pathlist_entry *pe;
3910 TAILQ_FOREACH(pe, merged_paths, entry)
3911 free((char *)pe->path);
3913 got_pathlist_free(merged_paths);
3916 static const struct got_error *
3917 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3918 struct got_repository *repo)
3920 const struct got_error *err;
3921 struct got_reference *commit_ref = NULL;
3923 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3924 if (err) {
3925 if (err->code != GOT_ERR_NOT_REF)
3926 goto done;
3927 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3928 if (err)
3929 goto done;
3930 err = got_ref_write(commit_ref, repo);
3931 if (err)
3932 goto done;
3933 } else {
3934 struct got_object_id *stored_id;
3935 int cmp;
3937 err = got_ref_resolve(&stored_id, repo, commit_ref);
3938 if (err)
3939 goto done;
3940 cmp = got_object_id_cmp(commit_id, stored_id);
3941 free(stored_id);
3942 if (cmp != 0) {
3943 err = got_error(GOT_ERR_REBASE_COMMITID);
3944 goto done;
3947 done:
3948 if (commit_ref)
3949 got_ref_close(commit_ref);
3950 return err;
3953 static const struct got_error *
3954 rebase_merge_files(struct got_pathlist_head *merged_paths,
3955 const char *commit_ref_name, struct got_worktree *worktree,
3956 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3957 struct got_object_id *commit_id, struct got_repository *repo,
3958 got_worktree_checkout_cb progress_cb, void *progress_arg,
3959 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3961 const struct got_error *err;
3962 struct got_reference *commit_ref = NULL;
3963 struct collect_merged_paths_arg cmp_arg;
3964 char *fileindex_path;
3966 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3968 err = get_fileindex_path(&fileindex_path, worktree);
3969 if (err)
3970 return err;
3972 cmp_arg.progress_cb = progress_cb;
3973 cmp_arg.progress_arg = progress_arg;
3974 cmp_arg.merged_paths = merged_paths;
3975 err = merge_files(worktree, fileindex, fileindex_path,
3976 parent_commit_id, commit_id, repo, collect_merged_paths,
3977 &cmp_arg, cancel_cb, cancel_arg);
3978 if (commit_ref)
3979 got_ref_close(commit_ref);
3980 return err;
3983 const struct got_error *
3984 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3985 struct got_worktree *worktree, struct got_fileindex *fileindex,
3986 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3987 struct got_repository *repo,
3988 got_worktree_checkout_cb progress_cb, void *progress_arg,
3989 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3991 const struct got_error *err;
3992 char *commit_ref_name;
3994 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3995 if (err)
3996 return err;
3998 err = store_commit_id(commit_ref_name, commit_id, repo);
3999 if (err)
4000 goto done;
4002 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4003 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4004 progress_arg, cancel_cb, cancel_arg);
4005 done:
4006 free(commit_ref_name);
4007 return err;
4010 const struct got_error *
4011 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4012 struct got_worktree *worktree, struct got_fileindex *fileindex,
4013 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4014 struct got_repository *repo,
4015 got_worktree_checkout_cb progress_cb, void *progress_arg,
4016 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4018 const struct got_error *err;
4019 char *commit_ref_name;
4021 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4022 if (err)
4023 return err;
4025 err = store_commit_id(commit_ref_name, commit_id, repo);
4026 if (err)
4027 goto done;
4029 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4030 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4031 progress_arg, cancel_cb, cancel_arg);
4032 done:
4033 free(commit_ref_name);
4034 return err;
4037 static const struct got_error *
4038 rebase_commit(struct got_object_id **new_commit_id,
4039 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4040 struct got_worktree *worktree, struct got_fileindex *fileindex,
4041 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4042 const char *new_logmsg, struct got_repository *repo)
4044 const struct got_error *err, *sync_err;
4045 struct got_pathlist_head commitable_paths;
4046 struct collect_commitables_arg cc_arg;
4047 char *fileindex_path = NULL;
4048 struct got_reference *head_ref = NULL;
4049 struct got_object_id *head_commit_id = NULL;
4050 char *logmsg = NULL;
4052 TAILQ_INIT(&commitable_paths);
4053 *new_commit_id = NULL;
4055 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4057 err = get_fileindex_path(&fileindex_path, worktree);
4058 if (err)
4059 return err;
4061 cc_arg.commitable_paths = &commitable_paths;
4062 cc_arg.worktree = worktree;
4063 cc_arg.repo = repo;
4065 * If possible get the status of individual files directly to
4066 * avoid crawling the entire work tree once per rebased commit.
4067 * TODO: Ideally, merged_paths would contain a list of commitables
4068 * we could use so we could skip worktree_status() entirely.
4070 if (merged_paths) {
4071 struct got_pathlist_entry *pe;
4072 if (TAILQ_EMPTY(merged_paths)) {
4073 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4074 goto done;
4076 TAILQ_FOREACH(pe, merged_paths, entry) {
4077 err = worktree_status(worktree, pe->path, fileindex,
4078 repo, collect_commitables, &cc_arg, NULL, NULL);
4079 if (err)
4080 goto done;
4082 } else {
4083 err = worktree_status(worktree, "", fileindex, repo,
4084 collect_commitables, &cc_arg, NULL, NULL);
4085 if (err)
4086 goto done;
4089 if (TAILQ_EMPTY(&commitable_paths)) {
4090 /* No-op change; commit will be elided. */
4091 err = got_ref_delete(commit_ref, repo);
4092 if (err)
4093 goto done;
4094 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4095 goto done;
4098 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4099 if (err)
4100 goto done;
4102 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4103 if (err)
4104 goto done;
4106 if (new_logmsg)
4107 logmsg = strdup(new_logmsg);
4108 else
4109 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4110 if (logmsg == NULL)
4111 return got_error_from_errno("strdup");
4113 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4114 worktree, got_object_commit_get_author(orig_commit),
4115 got_object_commit_get_committer(orig_commit),
4116 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4117 if (err)
4118 goto done;
4120 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4121 if (err)
4122 goto done;
4124 err = got_ref_delete(commit_ref, repo);
4125 if (err)
4126 goto done;
4128 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4129 fileindex);
4130 sync_err = sync_fileindex(fileindex, fileindex_path);
4131 if (sync_err && err == NULL)
4132 err = sync_err;
4133 done:
4134 free(fileindex_path);
4135 free(head_commit_id);
4136 if (head_ref)
4137 got_ref_close(head_ref);
4138 if (err) {
4139 free(*new_commit_id);
4140 *new_commit_id = NULL;
4142 return err;
4145 const struct got_error *
4146 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4147 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4148 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4149 struct got_commit_object *orig_commit,
4150 struct got_object_id *orig_commit_id, struct got_repository *repo)
4152 const struct got_error *err;
4153 char *commit_ref_name;
4154 struct got_reference *commit_ref = NULL;
4155 struct got_object_id *commit_id = NULL;
4157 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4158 if (err)
4159 return err;
4161 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4162 if (err)
4163 goto done;
4164 err = got_ref_resolve(&commit_id, repo, commit_ref);
4165 if (err)
4166 goto done;
4167 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4168 err = got_error(GOT_ERR_REBASE_COMMITID);
4169 goto done;
4172 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4173 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4174 done:
4175 if (commit_ref)
4176 got_ref_close(commit_ref);
4177 free(commit_ref_name);
4178 free(commit_id);
4179 return err;
4182 const struct got_error *
4183 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4184 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4185 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4186 struct got_commit_object *orig_commit,
4187 struct got_object_id *orig_commit_id, const char *new_logmsg,
4188 struct got_repository *repo)
4190 const struct got_error *err;
4191 char *commit_ref_name;
4192 struct got_reference *commit_ref = NULL;
4193 struct got_object_id *commit_id = NULL;
4195 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4196 if (err)
4197 return err;
4199 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4200 if (err)
4201 goto done;
4202 err = got_ref_resolve(&commit_id, repo, commit_ref);
4203 if (err)
4204 goto done;
4205 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4206 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4207 goto done;
4210 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4211 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4212 done:
4213 if (commit_ref)
4214 got_ref_close(commit_ref);
4215 free(commit_ref_name);
4216 free(commit_id);
4217 return err;
4220 const struct got_error *
4221 got_worktree_rebase_postpone(struct got_worktree *worktree,
4222 struct got_fileindex *fileindex)
4224 if (fileindex)
4225 got_fileindex_free(fileindex);
4226 return lock_worktree(worktree, LOCK_SH);
4229 static const struct got_error *
4230 delete_ref(const char *name, struct got_repository *repo)
4232 const struct got_error *err;
4233 struct got_reference *ref;
4235 err = got_ref_open(&ref, repo, name, 0);
4236 if (err) {
4237 if (err->code == GOT_ERR_NOT_REF)
4238 return NULL;
4239 return err;
4242 err = got_ref_delete(ref, repo);
4243 got_ref_close(ref);
4244 return err;
4247 static const struct got_error *
4248 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4250 const struct got_error *err;
4251 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4252 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4254 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4255 if (err)
4256 goto done;
4257 err = delete_ref(tmp_branch_name, repo);
4258 if (err)
4259 goto done;
4261 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4262 if (err)
4263 goto done;
4264 err = delete_ref(new_base_branch_ref_name, repo);
4265 if (err)
4266 goto done;
4268 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4269 if (err)
4270 goto done;
4271 err = delete_ref(branch_ref_name, repo);
4272 if (err)
4273 goto done;
4275 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4276 if (err)
4277 goto done;
4278 err = delete_ref(commit_ref_name, repo);
4279 if (err)
4280 goto done;
4282 done:
4283 free(tmp_branch_name);
4284 free(new_base_branch_ref_name);
4285 free(branch_ref_name);
4286 free(commit_ref_name);
4287 return err;
4290 const struct got_error *
4291 got_worktree_rebase_complete(struct got_worktree *worktree,
4292 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4293 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4294 struct got_repository *repo)
4296 const struct got_error *err, *unlockerr;
4297 struct got_object_id *new_head_commit_id = NULL;
4299 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4300 if (err)
4301 return err;
4303 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4304 if (err)
4305 goto done;
4307 err = got_ref_write(rebased_branch, repo);
4308 if (err)
4309 goto done;
4311 err = got_worktree_set_head_ref(worktree, rebased_branch);
4312 if (err)
4313 goto done;
4315 err = delete_rebase_refs(worktree, repo);
4316 done:
4317 if (fileindex)
4318 got_fileindex_free(fileindex);
4319 free(new_head_commit_id);
4320 unlockerr = lock_worktree(worktree, LOCK_SH);
4321 if (unlockerr && err == NULL)
4322 err = unlockerr;
4323 return err;
4326 struct collect_revertible_paths_arg {
4327 struct got_pathlist_head *revertible_paths;
4328 struct got_worktree *worktree;
4331 static const struct got_error *
4332 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4333 struct got_object_id *blob_id, struct got_object_id *commit_id)
4335 struct collect_revertible_paths_arg *a = arg;
4336 const struct got_error *err = NULL;
4337 struct got_pathlist_entry *new = NULL;
4338 char *path = NULL;
4340 if (status != GOT_STATUS_ADD &&
4341 status != GOT_STATUS_DELETE &&
4342 status != GOT_STATUS_MODIFY &&
4343 status != GOT_STATUS_CONFLICT &&
4344 status != GOT_STATUS_MISSING)
4345 return NULL;
4347 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4348 return got_error_from_errno("asprintf");
4350 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4351 if (err || new == NULL)
4352 free(path);
4353 return err;
4356 const struct got_error *
4357 got_worktree_rebase_abort(struct got_worktree *worktree,
4358 struct got_fileindex *fileindex, struct got_repository *repo,
4359 struct got_reference *new_base_branch,
4360 got_worktree_checkout_cb progress_cb, void *progress_arg)
4362 const struct got_error *err, *unlockerr, *sync_err;
4363 struct got_reference *resolved = NULL;
4364 struct got_object_id *commit_id = NULL;
4365 char *fileindex_path = NULL;
4366 struct got_pathlist_head revertible_paths;
4367 struct got_pathlist_entry *pe;
4368 struct collect_revertible_paths_arg crp_arg;
4369 struct got_object_id *tree_id = NULL;
4371 TAILQ_INIT(&revertible_paths);
4373 err = lock_worktree(worktree, LOCK_EX);
4374 if (err)
4375 return err;
4377 err = got_ref_open(&resolved, repo,
4378 got_ref_get_symref_target(new_base_branch), 0);
4379 if (err)
4380 goto done;
4382 err = got_worktree_set_head_ref(worktree, resolved);
4383 if (err)
4384 goto done;
4387 * XXX commits to the base branch could have happened while
4388 * we were busy rebasing; should we store the original commit ID
4389 * when rebase begins and read it back here?
4391 err = got_ref_resolve(&commit_id, repo, resolved);
4392 if (err)
4393 goto done;
4395 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4396 if (err)
4397 goto done;
4399 err = got_object_id_by_path(&tree_id, repo,
4400 worktree->base_commit_id, worktree->path_prefix);
4401 if (err)
4402 goto done;
4404 err = delete_rebase_refs(worktree, repo);
4405 if (err)
4406 goto done;
4408 err = get_fileindex_path(&fileindex_path, worktree);
4409 if (err)
4410 goto done;
4412 crp_arg.revertible_paths = &revertible_paths;
4413 crp_arg.worktree = worktree;
4414 err = worktree_status(worktree, "", fileindex, repo,
4415 collect_revertible_paths, &crp_arg, NULL, NULL);
4416 if (err)
4417 goto done;
4419 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4420 err = revert_file(worktree, fileindex, pe->path,
4421 progress_cb, progress_arg, repo);
4422 if (err)
4423 goto sync;
4426 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4427 repo, progress_cb, progress_arg, NULL, NULL);
4428 sync:
4429 sync_err = sync_fileindex(fileindex, fileindex_path);
4430 if (sync_err && err == NULL)
4431 err = sync_err;
4432 done:
4433 got_ref_close(resolved);
4434 free(tree_id);
4435 free(commit_id);
4436 if (fileindex)
4437 got_fileindex_free(fileindex);
4438 free(fileindex_path);
4439 TAILQ_FOREACH(pe, &revertible_paths, entry)
4440 free((char *)pe->path);
4441 got_pathlist_free(&revertible_paths);
4443 unlockerr = lock_worktree(worktree, LOCK_SH);
4444 if (unlockerr && err == NULL)
4445 err = unlockerr;
4446 return err;
4449 const struct got_error *
4450 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4451 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4452 struct got_fileindex **fileindex, struct got_worktree *worktree,
4453 struct got_repository *repo)
4455 const struct got_error *err = NULL;
4456 char *tmp_branch_name = NULL;
4457 char *branch_ref_name = NULL;
4458 char *base_commit_ref_name = NULL;
4459 char *fileindex_path = NULL;
4460 struct check_rebase_ok_arg ok_arg;
4461 struct got_reference *wt_branch = NULL;
4462 struct got_reference *base_commit_ref = NULL;
4464 *tmp_branch = NULL;
4465 *branch_ref = NULL;
4466 *base_commit_id = NULL;
4467 *fileindex = NULL;
4469 err = lock_worktree(worktree, LOCK_EX);
4470 if (err)
4471 return err;
4473 err = open_fileindex(fileindex, &fileindex_path, worktree);
4474 if (err)
4475 goto done;
4477 ok_arg.worktree = worktree;
4478 ok_arg.repo = repo;
4479 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4480 &ok_arg);
4481 if (err)
4482 goto done;
4484 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4485 if (err)
4486 goto done;
4488 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4489 if (err)
4490 goto done;
4492 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4493 worktree);
4494 if (err)
4495 goto done;
4497 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4498 0);
4499 if (err)
4500 goto done;
4502 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4503 if (err)
4504 goto done;
4506 err = got_ref_write(*branch_ref, repo);
4507 if (err)
4508 goto done;
4510 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4511 worktree->base_commit_id);
4512 if (err)
4513 goto done;
4514 err = got_ref_write(base_commit_ref, repo);
4515 if (err)
4516 goto done;
4517 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4518 if (*base_commit_id == NULL) {
4519 err = got_error_from_errno("got_object_id_dup");
4520 goto done;
4523 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4524 worktree->base_commit_id);
4525 if (err)
4526 goto done;
4527 err = got_ref_write(*tmp_branch, repo);
4528 if (err)
4529 goto done;
4531 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4532 if (err)
4533 goto done;
4534 done:
4535 free(fileindex_path);
4536 free(tmp_branch_name);
4537 free(branch_ref_name);
4538 free(base_commit_ref_name);
4539 if (wt_branch)
4540 got_ref_close(wt_branch);
4541 if (err) {
4542 if (*branch_ref) {
4543 got_ref_close(*branch_ref);
4544 *branch_ref = NULL;
4546 if (*tmp_branch) {
4547 got_ref_close(*tmp_branch);
4548 *tmp_branch = NULL;
4550 free(*base_commit_id);
4551 if (*fileindex) {
4552 got_fileindex_free(*fileindex);
4553 *fileindex = NULL;
4555 lock_worktree(worktree, LOCK_SH);
4557 return err;
4560 const struct got_error *
4561 got_worktree_histedit_postpone(struct got_worktree *worktree,
4562 struct got_fileindex *fileindex)
4564 if (fileindex)
4565 got_fileindex_free(fileindex);
4566 return lock_worktree(worktree, LOCK_SH);
4569 const struct got_error *
4570 got_worktree_histedit_in_progress(int *in_progress,
4571 struct got_worktree *worktree)
4573 const struct got_error *err;
4574 char *tmp_branch_name = NULL;
4576 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4577 if (err)
4578 return err;
4580 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4581 free(tmp_branch_name);
4582 return NULL;
4585 const struct got_error *
4586 got_worktree_histedit_continue(struct got_object_id **commit_id,
4587 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4588 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4589 struct got_worktree *worktree, struct got_repository *repo)
4591 const struct got_error *err;
4592 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4593 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4594 struct got_reference *commit_ref = NULL;
4595 struct got_reference *base_commit_ref = NULL;
4596 char *fileindex_path = NULL;
4598 *commit_id = NULL;
4599 *tmp_branch = NULL;
4600 *base_commit_id = NULL;
4601 *fileindex = NULL;
4603 err = lock_worktree(worktree, LOCK_EX);
4604 if (err)
4605 return err;
4607 err = open_fileindex(fileindex, &fileindex_path, worktree);
4608 if (err)
4609 goto done;
4611 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4612 if (err)
4613 return err;
4615 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4616 if (err)
4617 goto done;
4619 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4620 if (err)
4621 goto done;
4623 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4624 worktree);
4625 if (err)
4626 goto done;
4628 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4629 if (err)
4630 goto done;
4632 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4633 if (err)
4634 goto done;
4635 err = got_ref_resolve(commit_id, repo, commit_ref);
4636 if (err)
4637 goto done;
4639 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4640 if (err)
4641 goto done;
4642 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4643 if (err)
4644 goto done;
4646 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4647 if (err)
4648 goto done;
4649 done:
4650 free(commit_ref_name);
4651 free(branch_ref_name);
4652 free(fileindex_path);
4653 if (commit_ref)
4654 got_ref_close(commit_ref);
4655 if (base_commit_ref)
4656 got_ref_close(base_commit_ref);
4657 if (err) {
4658 free(*commit_id);
4659 *commit_id = NULL;
4660 free(*base_commit_id);
4661 *base_commit_id = NULL;
4662 if (*tmp_branch) {
4663 got_ref_close(*tmp_branch);
4664 *tmp_branch = NULL;
4666 if (*fileindex) {
4667 got_fileindex_free(*fileindex);
4668 *fileindex = NULL;
4670 lock_worktree(worktree, LOCK_EX);
4672 return err;
4675 static const struct got_error *
4676 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4678 const struct got_error *err;
4679 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4680 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4682 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4683 if (err)
4684 goto done;
4685 err = delete_ref(tmp_branch_name, repo);
4686 if (err)
4687 goto done;
4689 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4690 worktree);
4691 if (err)
4692 goto done;
4693 err = delete_ref(base_commit_ref_name, repo);
4694 if (err)
4695 goto done;
4697 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4698 if (err)
4699 goto done;
4700 err = delete_ref(branch_ref_name, repo);
4701 if (err)
4702 goto done;
4704 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4705 if (err)
4706 goto done;
4707 err = delete_ref(commit_ref_name, repo);
4708 if (err)
4709 goto done;
4710 done:
4711 free(tmp_branch_name);
4712 free(base_commit_ref_name);
4713 free(branch_ref_name);
4714 free(commit_ref_name);
4715 return err;
4718 const struct got_error *
4719 got_worktree_histedit_abort(struct got_worktree *worktree,
4720 struct got_fileindex *fileindex, struct got_repository *repo,
4721 struct got_reference *branch, struct got_object_id *base_commit_id,
4722 got_worktree_checkout_cb progress_cb, void *progress_arg)
4724 const struct got_error *err, *unlockerr, *sync_err;
4725 struct got_reference *resolved = NULL;
4726 char *fileindex_path = NULL;
4727 struct got_pathlist_head revertible_paths;
4728 struct got_pathlist_entry *pe;
4729 struct collect_revertible_paths_arg crp_arg;
4730 struct got_object_id *tree_id = NULL;
4732 TAILQ_INIT(&revertible_paths);
4734 err = lock_worktree(worktree, LOCK_EX);
4735 if (err)
4736 return err;
4738 err = got_ref_open(&resolved, repo,
4739 got_ref_get_symref_target(branch), 0);
4740 if (err)
4741 goto done;
4743 err = got_worktree_set_head_ref(worktree, resolved);
4744 if (err)
4745 goto done;
4747 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4748 if (err)
4749 goto done;
4751 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4752 worktree->path_prefix);
4753 if (err)
4754 goto done;
4756 err = delete_histedit_refs(worktree, repo);
4757 if (err)
4758 goto done;
4760 err = get_fileindex_path(&fileindex_path, worktree);
4761 if (err)
4762 goto done;
4764 crp_arg.revertible_paths = &revertible_paths;
4765 crp_arg.worktree = worktree;
4766 err = worktree_status(worktree, "", fileindex, repo,
4767 collect_revertible_paths, &crp_arg, NULL, NULL);
4768 if (err)
4769 goto done;
4771 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4772 err = revert_file(worktree, fileindex, pe->path,
4773 progress_cb, progress_arg, repo);
4774 if (err)
4775 goto sync;
4778 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4779 repo, progress_cb, progress_arg, NULL, NULL);
4780 sync:
4781 sync_err = sync_fileindex(fileindex, fileindex_path);
4782 if (sync_err && err == NULL)
4783 err = sync_err;
4784 done:
4785 got_ref_close(resolved);
4786 free(tree_id);
4787 free(fileindex_path);
4788 TAILQ_FOREACH(pe, &revertible_paths, entry)
4789 free((char *)pe->path);
4790 got_pathlist_free(&revertible_paths);
4792 unlockerr = lock_worktree(worktree, LOCK_SH);
4793 if (unlockerr && err == NULL)
4794 err = unlockerr;
4795 return err;
4798 const struct got_error *
4799 got_worktree_histedit_complete(struct got_worktree *worktree,
4800 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4801 struct got_reference *edited_branch, struct got_repository *repo)
4803 const struct got_error *err, *unlockerr;
4804 struct got_object_id *new_head_commit_id = NULL;
4805 struct got_reference *resolved = NULL;
4807 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4808 if (err)
4809 return err;
4811 err = got_ref_open(&resolved, repo,
4812 got_ref_get_symref_target(edited_branch), 0);
4813 if (err)
4814 goto done;
4816 err = got_ref_change_ref(resolved, new_head_commit_id);
4817 if (err)
4818 goto done;
4820 err = got_ref_write(resolved, repo);
4821 if (err)
4822 goto done;
4824 err = got_worktree_set_head_ref(worktree, resolved);
4825 if (err)
4826 goto done;
4828 err = delete_histedit_refs(worktree, repo);
4829 done:
4830 if (fileindex)
4831 got_fileindex_free(fileindex);
4832 free(new_head_commit_id);
4833 unlockerr = lock_worktree(worktree, LOCK_SH);
4834 if (unlockerr && err == NULL)
4835 err = unlockerr;
4836 return err;
4839 const struct got_error *
4840 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4841 struct got_object_id *commit_id, struct got_repository *repo)
4843 const struct got_error *err;
4844 char *commit_ref_name;
4846 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4847 if (err)
4848 return err;
4850 err = store_commit_id(commit_ref_name, commit_id, repo);
4851 if (err)
4852 goto done;
4854 err = delete_ref(commit_ref_name, repo);
4855 done:
4856 free(commit_ref_name);
4857 return err;
4860 static const struct got_error *
4861 stage_path(const char *path, size_t path_len, struct got_worktree *worktree,
4862 struct got_fileindex *fileindex, struct got_repository *repo,
4863 got_worktree_status_cb status_cb, void *status_arg)
4865 const struct got_error *err = NULL;
4866 char *ondisk_path;
4867 struct got_fileindex_entry *ie;
4868 unsigned char status;
4869 struct stat sb;
4870 struct got_object_id *blob_id = NULL;
4871 uint32_t stage;
4873 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
4874 return got_error_from_errno("asprintf");
4876 ie = got_fileindex_entry_get(fileindex, path, path_len);
4877 if (ie == NULL) {
4878 err = got_error_path(path, GOT_ERR_FILE_STATUS);
4879 goto done;
4882 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4883 if (err)
4884 goto done;
4886 switch (status) {
4887 case GOT_STATUS_ADD:
4888 case GOT_STATUS_MODIFY:
4889 err = got_object_blob_create(&blob_id, ondisk_path,
4890 repo);
4891 if (err)
4892 goto done;
4893 memcpy(ie->staged_blob_sha1, blob_id->sha1,
4894 SHA1_DIGEST_LENGTH);
4895 if (status == GOT_STATUS_ADD)
4896 stage = GOT_FILEIDX_STAGE_ADD;
4897 else
4898 stage = GOT_FILEIDX_STAGE_MODIFY;
4899 break;
4900 case GOT_STATUS_DELETE:
4901 stage = GOT_FILEIDX_STAGE_DELETE;
4902 break;
4903 default:
4904 err = got_error_path(path, GOT_ERR_FILE_STATUS);
4905 goto done;
4908 got_fileindex_entry_stage_set(ie, stage);
4910 /* XXX TODO pass 'staged' status separately */
4911 err = (*status_cb)(status_arg, status, path, blob_id, NULL);
4912 done:
4913 free(blob_id);
4914 free(ondisk_path);
4915 return err;
4918 const struct got_error *
4919 got_worktree_stage_paths(struct got_worktree *worktree,
4920 struct got_pathlist_head *paths, struct got_repository *repo,
4921 got_worktree_status_cb status_cb, void *status_arg,
4922 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4924 const struct got_error *err = NULL, *sync_err, *unlockerr;
4925 struct got_pathlist_entry *pe;
4926 struct got_fileindex *fileindex = NULL;
4927 char *fileindex_path = NULL;
4929 err = lock_worktree(worktree, LOCK_EX);
4930 if (err)
4931 return err;
4933 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4934 if (err)
4935 goto done;
4937 TAILQ_FOREACH(pe, paths, entry) {
4938 if (cancel_cb) {
4939 err = (*cancel_cb)(cancel_arg);
4940 if (err)
4941 break;
4943 err = stage_path(pe->path, pe->path_len, worktree, fileindex,
4944 repo, status_cb, status_arg);
4945 if (err)
4946 break;
4949 sync_err = sync_fileindex(fileindex, fileindex_path);
4950 if (sync_err && err == NULL)
4951 err = sync_err;
4952 done:
4953 free(fileindex_path);
4954 if (fileindex)
4955 got_fileindex_free(fileindex);
4956 unlockerr = lock_worktree(worktree, LOCK_SH);
4957 if (unlockerr && err == NULL)
4958 err = unlockerr;
4959 return err;