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 unsigned char
1038 get_staged_status(struct got_fileindex_entry *ie)
1040 switch (got_fileindex_entry_stage_get(ie)) {
1041 case GOT_FILEIDX_STAGE_ADD:
1042 return GOT_STATUS_ADD;
1043 case GOT_FILEIDX_STAGE_DELETE:
1044 return GOT_STATUS_DELETE;
1045 case GOT_FILEIDX_STAGE_MODIFY:
1046 return GOT_STATUS_MODIFY;
1047 default:
1048 return GOT_STATUS_NO_CHANGE;
1052 static const struct got_error *
1053 get_file_status(unsigned char *status, struct stat *sb,
1054 struct got_fileindex_entry *ie, const char *abspath,
1055 struct got_repository *repo)
1057 const struct got_error *err = NULL;
1058 struct got_object_id id;
1059 size_t hdrlen;
1060 FILE *f = NULL;
1061 uint8_t fbuf[8192];
1062 struct got_blob_object *blob = NULL;
1063 size_t flen, blen;
1064 unsigned char staged_status = get_staged_status(ie);
1066 *status = GOT_STATUS_NO_CHANGE;
1068 if (lstat(abspath, sb) == -1) {
1069 if (errno == ENOENT) {
1070 if (got_fileindex_entry_has_file_on_disk(ie))
1071 *status = GOT_STATUS_MISSING;
1072 else
1073 *status = GOT_STATUS_DELETE;
1074 return NULL;
1076 return got_error_from_errno2("lstat", abspath);
1079 if (!S_ISREG(sb->st_mode)) {
1080 *status = GOT_STATUS_OBSTRUCTED;
1081 return NULL;
1084 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1085 *status = GOT_STATUS_DELETE;
1086 return NULL;
1087 } else if (!got_fileindex_entry_has_blob(ie)) {
1088 *status = GOT_STATUS_ADD;
1089 return NULL;
1092 if (!stat_info_differs(ie, sb))
1093 return NULL;
1095 if (staged_status == GOT_STATUS_MODIFY ||
1096 staged_status == GOT_STATUS_ADD)
1097 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1098 else
1099 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1101 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1102 if (err)
1103 return err;
1105 f = fopen(abspath, "r");
1106 if (f == NULL) {
1107 err = got_error_from_errno2("fopen", abspath);
1108 goto done;
1110 hdrlen = got_object_blob_get_hdrlen(blob);
1111 for (;;) {
1112 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1113 err = got_object_blob_read_block(&blen, blob);
1114 if (err)
1115 goto done;
1116 /* Skip length of blob object header first time around. */
1117 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1118 if (flen == 0 && ferror(f)) {
1119 err = got_error_from_errno("fread");
1120 goto done;
1122 if (blen == 0) {
1123 if (flen != 0)
1124 *status = GOT_STATUS_MODIFY;
1125 break;
1126 } else if (flen == 0) {
1127 if (blen != 0)
1128 *status = GOT_STATUS_MODIFY;
1129 break;
1130 } else if (blen - hdrlen == flen) {
1131 /* Skip blob object header first time around. */
1132 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1133 *status = GOT_STATUS_MODIFY;
1134 break;
1136 } else {
1137 *status = GOT_STATUS_MODIFY;
1138 break;
1140 hdrlen = 0;
1143 if (*status == GOT_STATUS_MODIFY) {
1144 rewind(f);
1145 err = get_modified_file_content_status(status, f);
1147 done:
1148 if (blob)
1149 got_object_blob_close(blob);
1150 if (f)
1151 fclose(f);
1152 return err;
1156 * Update timestamps in the file index if a file is unmodified and
1157 * we had to run a full content comparison to find out.
1159 static const struct got_error *
1160 sync_timestamps(char *ondisk_path, unsigned char status,
1161 struct got_fileindex_entry *ie, struct stat *sb)
1163 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1164 return got_fileindex_entry_update(ie, ondisk_path,
1165 ie->blob_sha1, ie->commit_sha1, 1);
1167 return NULL;
1170 static const struct got_error *
1171 update_blob(struct got_worktree *worktree,
1172 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1173 struct got_tree_entry *te, const char *path,
1174 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1175 void *progress_arg)
1177 const struct got_error *err = NULL;
1178 struct got_blob_object *blob = NULL;
1179 char *ondisk_path;
1180 unsigned char status = GOT_STATUS_NO_CHANGE;
1181 struct stat sb;
1183 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1184 return got_error_from_errno("asprintf");
1186 if (ie) {
1187 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1188 if (err)
1189 goto done;
1190 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1191 sb.st_mode = got_fileindex_perms_to_st(ie);
1192 } else
1193 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1195 if (status == GOT_STATUS_OBSTRUCTED) {
1196 err = (*progress_cb)(progress_arg, status, path);
1197 goto done;
1200 if (ie && status != GOT_STATUS_MISSING) {
1201 if (got_fileindex_entry_has_commit(ie) &&
1202 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1203 SHA1_DIGEST_LENGTH) == 0) {
1204 err = sync_timestamps(ondisk_path, status, ie, &sb);
1205 if (err)
1206 goto done;
1207 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1208 path);
1209 goto done;
1211 if (got_fileindex_entry_has_blob(ie) &&
1212 memcmp(ie->blob_sha1, te->id->sha1,
1213 SHA1_DIGEST_LENGTH) == 0) {
1214 err = sync_timestamps(ondisk_path, status, ie, &sb);
1215 goto done;
1219 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1220 if (err)
1221 goto done;
1223 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1224 int update_timestamps;
1225 struct got_blob_object *blob2 = NULL;
1226 if (got_fileindex_entry_has_blob(ie)) {
1227 struct got_object_id id2;
1228 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1229 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1230 if (err)
1231 goto done;
1233 err = merge_blob(&update_timestamps, worktree, blob2,
1234 ondisk_path, path, sb.st_mode, blob,
1235 worktree->base_commit_id, repo,
1236 progress_cb, progress_arg);
1237 if (blob2)
1238 got_object_blob_close(blob2);
1240 * Do not update timestamps of files with local changes.
1241 * Otherwise, a future status walk would treat them as
1242 * unmodified files again.
1244 err = got_fileindex_entry_update(ie, ondisk_path,
1245 blob->id.sha1, worktree->base_commit_id->sha1,
1246 update_timestamps);
1247 } else if (status == GOT_STATUS_DELETE) {
1248 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1249 if (err)
1250 goto done;
1251 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1252 ondisk_path, path, blob, 0);
1253 if (err)
1254 goto done;
1255 } else {
1256 err = install_blob(worktree, ondisk_path, path, te->mode,
1257 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1258 repo, progress_cb, progress_arg);
1259 if (err)
1260 goto done;
1261 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1262 ondisk_path, path, blob, 1);
1263 if (err)
1264 goto done;
1266 got_object_blob_close(blob);
1267 done:
1268 free(ondisk_path);
1269 return err;
1272 static const struct got_error *
1273 remove_ondisk_file(const char *root_path, const char *path)
1275 const struct got_error *err = NULL;
1276 char *ondisk_path = NULL;
1278 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1279 return got_error_from_errno("asprintf");
1281 if (unlink(ondisk_path) == -1) {
1282 if (errno != ENOENT)
1283 err = got_error_from_errno2("unlink", ondisk_path);
1284 } else {
1285 char *parent = dirname(ondisk_path);
1286 while (parent && strcmp(parent, root_path) != 0) {
1287 if (rmdir(parent) == -1) {
1288 if (errno != ENOTEMPTY)
1289 err = got_error_from_errno2("rmdir",
1290 parent);
1291 break;
1293 parent = dirname(parent);
1296 free(ondisk_path);
1297 return err;
1300 static const struct got_error *
1301 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1302 struct got_fileindex_entry *ie, struct got_repository *repo,
1303 got_worktree_checkout_cb progress_cb, void *progress_arg)
1305 const struct got_error *err = NULL;
1306 unsigned char status;
1307 struct stat sb;
1308 char *ondisk_path;
1310 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1311 == -1)
1312 return got_error_from_errno("asprintf");
1314 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1315 if (err)
1316 return err;
1318 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1319 status == GOT_STATUS_ADD) {
1320 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1321 if (err)
1322 return err;
1324 * Preserve the working file and change the deleted blob's
1325 * entry into a schedule-add entry.
1327 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1328 0);
1329 if (err)
1330 return err;
1331 } else {
1332 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1333 if (err)
1334 return err;
1335 if (status == GOT_STATUS_NO_CHANGE) {
1336 err = remove_ondisk_file(worktree->root_path, ie->path);
1337 if (err)
1338 return err;
1340 got_fileindex_entry_remove(fileindex, ie);
1343 return err;
1346 struct diff_cb_arg {
1347 struct got_fileindex *fileindex;
1348 struct got_worktree *worktree;
1349 struct got_repository *repo;
1350 got_worktree_checkout_cb progress_cb;
1351 void *progress_arg;
1352 got_worktree_cancel_cb cancel_cb;
1353 void *cancel_arg;
1356 static const struct got_error *
1357 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1358 struct got_tree_entry *te, const char *parent_path)
1360 struct diff_cb_arg *a = arg;
1362 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1363 return got_error(GOT_ERR_CANCELLED);
1365 return update_blob(a->worktree, a->fileindex, ie, te,
1366 ie->path, a->repo, a->progress_cb, a->progress_arg);
1369 static const struct got_error *
1370 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1372 struct diff_cb_arg *a = arg;
1374 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1375 return got_error(GOT_ERR_CANCELLED);
1377 return delete_blob(a->worktree, a->fileindex, ie,
1378 a->repo, a->progress_cb, a->progress_arg);
1381 static const struct got_error *
1382 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1384 struct diff_cb_arg *a = arg;
1385 const struct got_error *err;
1386 char *path;
1388 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1389 return got_error(GOT_ERR_CANCELLED);
1391 if (asprintf(&path, "%s%s%s", parent_path,
1392 parent_path[0] ? "/" : "", te->name)
1393 == -1)
1394 return got_error_from_errno("asprintf");
1396 if (S_ISDIR(te->mode))
1397 err = add_dir_on_disk(a->worktree, path);
1398 else
1399 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1400 a->repo, a->progress_cb, a->progress_arg);
1402 free(path);
1403 return err;
1406 static const struct got_error *
1407 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1409 const struct got_error *err = NULL;
1410 char *uuidstr = NULL;
1411 uint32_t uuid_status;
1413 *refname = NULL;
1415 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1416 if (uuid_status != uuid_s_ok)
1417 return got_error_uuid(uuid_status);
1419 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1420 == -1) {
1421 err = got_error_from_errno("asprintf");
1422 *refname = NULL;
1424 free(uuidstr);
1425 return err;
1428 const struct got_error *
1429 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1431 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1434 static const struct got_error *
1435 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1437 return get_ref_name(refname, worktree,
1438 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1441 static const struct got_error *
1442 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1444 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1447 static const struct got_error *
1448 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1450 return get_ref_name(refname, worktree,
1451 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1454 static const struct got_error *
1455 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1457 return get_ref_name(refname, worktree,
1458 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1461 static const struct got_error *
1462 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1464 return get_ref_name(refname, worktree,
1465 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1468 static const struct got_error *
1469 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1471 return get_ref_name(refname, worktree,
1472 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1475 static const struct got_error *
1476 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1478 return get_ref_name(refname, worktree,
1479 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1482 static const struct got_error *
1483 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1485 return get_ref_name(refname, worktree,
1486 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1489 const struct got_error *
1490 got_worktree_get_histedit_script_path(char **path,
1491 struct got_worktree *worktree)
1493 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1494 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1495 *path = NULL;
1496 return got_error_from_errno("asprintf");
1498 return NULL;
1502 * Prevent Git's garbage collector from deleting our base commit by
1503 * setting a reference to our base commit's ID.
1505 static const struct got_error *
1506 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1508 const struct got_error *err = NULL;
1509 struct got_reference *ref = NULL;
1510 char *refname;
1512 err = got_worktree_get_base_ref_name(&refname, worktree);
1513 if (err)
1514 return err;
1516 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1517 if (err)
1518 goto done;
1520 err = got_ref_write(ref, repo);
1521 done:
1522 free(refname);
1523 if (ref)
1524 got_ref_close(ref);
1525 return err;
1528 static const struct got_error *
1529 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1531 const struct got_error *err = NULL;
1533 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1534 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1535 err = got_error_from_errno("asprintf");
1536 *fileindex_path = NULL;
1538 return err;
1542 static const struct got_error *
1543 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1544 struct got_worktree *worktree)
1546 const struct got_error *err = NULL;
1547 FILE *index = NULL;
1549 *fileindex_path = NULL;
1550 *fileindex = got_fileindex_alloc();
1551 if (*fileindex == NULL)
1552 return got_error_from_errno("got_fileindex_alloc");
1554 err = get_fileindex_path(fileindex_path, worktree);
1555 if (err)
1556 goto done;
1558 index = fopen(*fileindex_path, "rb");
1559 if (index == NULL) {
1560 if (errno != ENOENT)
1561 err = got_error_from_errno2("fopen", *fileindex_path);
1562 } else {
1563 err = got_fileindex_read(*fileindex, index);
1564 if (fclose(index) != 0 && err == NULL)
1565 err = got_error_from_errno("fclose");
1567 done:
1568 if (err) {
1569 free(*fileindex_path);
1570 *fileindex_path = NULL;
1571 got_fileindex_free(*fileindex);
1572 *fileindex = NULL;
1574 return err;
1577 struct bump_base_commit_id_arg {
1578 struct got_object_id *base_commit_id;
1579 const char *path;
1580 size_t path_len;
1581 const char *entry_name;
1582 got_worktree_checkout_cb progress_cb;
1583 void *progress_arg;
1586 /* Bump base commit ID of all files within an updated part of the work tree. */
1587 static const struct got_error *
1588 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1590 const struct got_error *err;
1591 struct bump_base_commit_id_arg *a = arg;
1593 if (a->entry_name) {
1594 if (strcmp(ie->path, a->path) != 0)
1595 return NULL;
1596 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1597 return NULL;
1599 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1600 SHA1_DIGEST_LENGTH) == 0)
1601 return NULL;
1603 if (a->progress_cb) {
1604 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1605 ie->path);
1606 if (err)
1607 return err;
1609 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1610 return NULL;
1613 static const struct got_error *
1614 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1616 const struct got_error *err = NULL;
1617 char *new_fileindex_path = NULL;
1618 FILE *new_index = NULL;
1620 err = got_opentemp_named(&new_fileindex_path, &new_index,
1621 fileindex_path);
1622 if (err)
1623 goto done;
1625 err = got_fileindex_write(fileindex, new_index);
1626 if (err)
1627 goto done;
1629 if (rename(new_fileindex_path, fileindex_path) != 0) {
1630 err = got_error_from_errno3("rename", new_fileindex_path,
1631 fileindex_path);
1632 unlink(new_fileindex_path);
1634 done:
1635 if (new_index)
1636 fclose(new_index);
1637 free(new_fileindex_path);
1638 return err;
1641 static const struct got_error *
1642 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1643 struct got_object_id **tree_id, const char *wt_relpath,
1644 struct got_worktree *worktree, struct got_repository *repo)
1646 const struct got_error *err = NULL;
1647 struct got_object_id *id = NULL;
1648 char *in_repo_path = NULL;
1649 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1651 *entry_type = GOT_OBJ_TYPE_ANY;
1652 *tree_relpath = NULL;
1653 *tree_id = NULL;
1655 if (wt_relpath[0] == '\0') {
1656 /* Check out all files within the work tree. */
1657 *entry_type = GOT_OBJ_TYPE_TREE;
1658 *tree_relpath = strdup("");
1659 if (*tree_relpath == NULL) {
1660 err = got_error_from_errno("strdup");
1661 goto done;
1663 err = got_object_id_by_path(tree_id, repo,
1664 worktree->base_commit_id, worktree->path_prefix);
1665 if (err)
1666 goto done;
1667 return NULL;
1670 /* Check out a subset of files in the work tree. */
1672 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1673 is_root_wt ? "" : "/", wt_relpath) == -1) {
1674 err = got_error_from_errno("asprintf");
1675 goto done;
1678 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1679 in_repo_path);
1680 if (err)
1681 goto done;
1683 free(in_repo_path);
1684 in_repo_path = NULL;
1686 err = got_object_get_type(entry_type, repo, id);
1687 if (err)
1688 goto done;
1690 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1691 /* Check out a single file. */
1692 if (strchr(wt_relpath, '/') == NULL) {
1693 /* Check out a single file in work tree's root dir. */
1694 in_repo_path = strdup(worktree->path_prefix);
1695 if (in_repo_path == NULL) {
1696 err = got_error_from_errno("strdup");
1697 goto done;
1699 *tree_relpath = strdup("");
1700 if (*tree_relpath == NULL) {
1701 err = got_error_from_errno("strdup");
1702 goto done;
1704 } else {
1705 /* Check out a single file in a subdirectory. */
1706 err = got_path_dirname(tree_relpath, wt_relpath);
1707 if (err)
1708 return err;
1709 if (asprintf(&in_repo_path, "%s%s%s",
1710 worktree->path_prefix, is_root_wt ? "" : "/",
1711 *tree_relpath) == -1) {
1712 err = got_error_from_errno("asprintf");
1713 goto done;
1716 err = got_object_id_by_path(tree_id, repo,
1717 worktree->base_commit_id, in_repo_path);
1718 } else {
1719 /* Check out all files within a subdirectory. */
1720 *tree_id = got_object_id_dup(id);
1721 if (*tree_id == NULL) {
1722 err = got_error_from_errno("got_object_id_dup");
1723 goto done;
1725 *tree_relpath = strdup(wt_relpath);
1726 if (*tree_relpath == NULL) {
1727 err = got_error_from_errno("strdup");
1728 goto done;
1731 done:
1732 free(id);
1733 free(in_repo_path);
1734 if (err) {
1735 *entry_type = GOT_OBJ_TYPE_ANY;
1736 free(*tree_relpath);
1737 *tree_relpath = NULL;
1738 free(*tree_id);
1739 *tree_id = NULL;
1741 return err;
1744 static const struct got_error *
1745 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1746 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1747 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1748 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1750 const struct got_error *err = NULL;
1751 struct got_commit_object *commit = NULL;
1752 struct got_tree_object *tree = NULL;
1753 struct got_fileindex_diff_tree_cb diff_cb;
1754 struct diff_cb_arg arg;
1756 err = ref_base_commit(worktree, repo);
1757 if (err)
1758 goto done;
1760 err = got_object_open_as_commit(&commit, repo,
1761 worktree->base_commit_id);
1762 if (err)
1763 goto done;
1765 err = got_object_open_as_tree(&tree, repo, tree_id);
1766 if (err)
1767 goto done;
1769 if (entry_name &&
1770 got_object_tree_find_entry(tree, entry_name) == NULL) {
1771 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1772 goto done;
1775 diff_cb.diff_old_new = diff_old_new;
1776 diff_cb.diff_old = diff_old;
1777 diff_cb.diff_new = diff_new;
1778 arg.fileindex = fileindex;
1779 arg.worktree = worktree;
1780 arg.repo = repo;
1781 arg.progress_cb = progress_cb;
1782 arg.progress_arg = progress_arg;
1783 arg.cancel_cb = cancel_cb;
1784 arg.cancel_arg = cancel_arg;
1785 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1786 entry_name, repo, &diff_cb, &arg);
1787 done:
1788 if (tree)
1789 got_object_tree_close(tree);
1790 if (commit)
1791 got_object_commit_close(commit);
1792 return err;
1795 const struct got_error *
1796 got_worktree_checkout_files(struct got_worktree *worktree,
1797 struct got_pathlist_head *paths, struct got_repository *repo,
1798 got_worktree_checkout_cb progress_cb, void *progress_arg,
1799 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1801 const struct got_error *err = NULL, *sync_err, *unlockerr;
1802 struct got_commit_object *commit = NULL;
1803 struct got_tree_object *tree = NULL;
1804 struct got_fileindex *fileindex = NULL;
1805 char *fileindex_path = NULL;
1806 struct got_pathlist_entry *pe;
1807 struct tree_path_data {
1808 SIMPLEQ_ENTRY(tree_path_data) entry;
1809 struct got_object_id *tree_id;
1810 int entry_type;
1811 char *relpath;
1812 char *entry_name;
1813 } *tpd = NULL;
1814 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1816 SIMPLEQ_INIT(&tree_paths);
1818 err = lock_worktree(worktree, LOCK_EX);
1819 if (err)
1820 return err;
1822 /* Map all specified paths to in-repository trees. */
1823 TAILQ_FOREACH(pe, paths, entry) {
1824 tpd = malloc(sizeof(*tpd));
1825 if (tpd == NULL) {
1826 err = got_error_from_errno("malloc");
1827 goto done;
1830 err = find_tree_entry_for_checkout(&tpd->entry_type,
1831 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1832 if (err) {
1833 free(tpd);
1834 goto done;
1837 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1838 err = got_path_basename(&tpd->entry_name, pe->path);
1839 if (err) {
1840 free(tpd->relpath);
1841 free(tpd->tree_id);
1842 free(tpd);
1843 goto done;
1845 } else
1846 tpd->entry_name = NULL;
1848 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1852 * Read the file index.
1853 * Checking out files is supposed to be an idempotent operation.
1854 * If the on-disk file index is incomplete we will try to complete it.
1856 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1857 if (err)
1858 goto done;
1860 tpd = SIMPLEQ_FIRST(&tree_paths);
1861 TAILQ_FOREACH(pe, paths, entry) {
1862 struct bump_base_commit_id_arg bbc_arg;
1864 err = checkout_files(worktree, fileindex, tpd->relpath,
1865 tpd->tree_id, tpd->entry_name, repo,
1866 progress_cb, progress_arg, cancel_cb, cancel_arg);
1867 if (err)
1868 break;
1870 bbc_arg.base_commit_id = worktree->base_commit_id;
1871 bbc_arg.entry_name = tpd->entry_name;
1872 bbc_arg.path = pe->path;
1873 bbc_arg.path_len = pe->path_len;
1874 bbc_arg.progress_cb = progress_cb;
1875 bbc_arg.progress_arg = progress_arg;
1876 err = got_fileindex_for_each_entry_safe(fileindex,
1877 bump_base_commit_id, &bbc_arg);
1878 if (err)
1879 break;
1881 tpd = SIMPLEQ_NEXT(tpd, entry);
1883 sync_err = sync_fileindex(fileindex, fileindex_path);
1884 if (sync_err && err == NULL)
1885 err = sync_err;
1886 done:
1887 free(fileindex_path);
1888 if (tree)
1889 got_object_tree_close(tree);
1890 if (commit)
1891 got_object_commit_close(commit);
1892 if (fileindex)
1893 got_fileindex_free(fileindex);
1894 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1895 tpd = SIMPLEQ_FIRST(&tree_paths);
1896 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1897 free(tpd->relpath);
1898 free(tpd->tree_id);
1899 free(tpd);
1901 unlockerr = lock_worktree(worktree, LOCK_SH);
1902 if (unlockerr && err == NULL)
1903 err = unlockerr;
1904 return err;
1907 struct merge_file_cb_arg {
1908 struct got_worktree *worktree;
1909 struct got_fileindex *fileindex;
1910 got_worktree_checkout_cb progress_cb;
1911 void *progress_arg;
1912 got_worktree_cancel_cb cancel_cb;
1913 void *cancel_arg;
1914 struct got_object_id *commit_id2;
1917 static const struct got_error *
1918 merge_file_cb(void *arg, struct got_blob_object *blob1,
1919 struct got_blob_object *blob2, struct got_object_id *id1,
1920 struct got_object_id *id2, const char *path1, const char *path2,
1921 struct got_repository *repo)
1923 static const struct got_error *err = NULL;
1924 struct merge_file_cb_arg *a = arg;
1925 struct got_fileindex_entry *ie;
1926 char *ondisk_path = NULL;
1927 struct stat sb;
1928 unsigned char status;
1929 int local_changes_subsumed;
1931 if (blob1 && blob2) {
1932 ie = got_fileindex_entry_get(a->fileindex, path2,
1933 strlen(path2));
1934 if (ie == NULL)
1935 return (*a->progress_cb)(a->progress_arg,
1936 GOT_STATUS_MISSING, path2);
1938 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1939 path2) == -1)
1940 return got_error_from_errno("asprintf");
1942 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1943 if (err)
1944 goto done;
1946 if (status == GOT_STATUS_DELETE) {
1947 err = (*a->progress_cb)(a->progress_arg,
1948 GOT_STATUS_MERGE, path2);
1949 goto done;
1951 if (status != GOT_STATUS_NO_CHANGE &&
1952 status != GOT_STATUS_MODIFY &&
1953 status != GOT_STATUS_CONFLICT &&
1954 status != GOT_STATUS_ADD) {
1955 err = (*a->progress_cb)(a->progress_arg, status, path2);
1956 goto done;
1959 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1960 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1961 a->progress_cb, a->progress_arg);
1962 } else if (blob1) {
1963 ie = got_fileindex_entry_get(a->fileindex, path1,
1964 strlen(path1));
1965 if (ie == NULL)
1966 return (*a->progress_cb)(a->progress_arg,
1967 GOT_STATUS_MISSING, path2);
1969 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1970 path1) == -1)
1971 return got_error_from_errno("asprintf");
1973 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1974 if (err)
1975 goto done;
1977 switch (status) {
1978 case GOT_STATUS_NO_CHANGE:
1979 err = (*a->progress_cb)(a->progress_arg,
1980 GOT_STATUS_DELETE, path1);
1981 if (err)
1982 goto done;
1983 err = remove_ondisk_file(a->worktree->root_path, path1);
1984 if (err)
1985 goto done;
1986 if (ie)
1987 got_fileindex_entry_mark_deleted_from_disk(ie);
1988 break;
1989 case GOT_STATUS_DELETE:
1990 case GOT_STATUS_MISSING:
1991 err = (*a->progress_cb)(a->progress_arg,
1992 GOT_STATUS_DELETE, path1);
1993 if (err)
1994 goto done;
1995 if (ie)
1996 got_fileindex_entry_mark_deleted_from_disk(ie);
1997 break;
1998 case GOT_STATUS_ADD:
1999 case GOT_STATUS_MODIFY:
2000 case GOT_STATUS_CONFLICT:
2001 err = (*a->progress_cb)(a->progress_arg,
2002 GOT_STATUS_CANNOT_DELETE, path1);
2003 if (err)
2004 goto done;
2005 break;
2006 case GOT_STATUS_OBSTRUCTED:
2007 err = (*a->progress_cb)(a->progress_arg, status, path1);
2008 if (err)
2009 goto done;
2010 break;
2011 default:
2012 break;
2014 } else if (blob2) {
2015 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2016 path2) == -1)
2017 return got_error_from_errno("asprintf");
2018 ie = got_fileindex_entry_get(a->fileindex, path2,
2019 strlen(path2));
2020 if (ie) {
2021 err = get_file_status(&status, &sb, ie, ondisk_path,
2022 repo);
2023 if (err)
2024 goto done;
2025 if (status != GOT_STATUS_NO_CHANGE &&
2026 status != GOT_STATUS_MODIFY &&
2027 status != GOT_STATUS_CONFLICT &&
2028 status != GOT_STATUS_ADD) {
2029 err = (*a->progress_cb)(a->progress_arg,
2030 status, path2);
2031 goto done;
2033 err = merge_blob(&local_changes_subsumed, a->worktree,
2034 NULL, ondisk_path, path2, sb.st_mode, blob2,
2035 a->commit_id2, repo,
2036 a->progress_cb, a->progress_arg);
2037 if (status == GOT_STATUS_DELETE) {
2038 err = update_blob_fileindex_entry(a->worktree,
2039 a->fileindex, ie, ondisk_path, ie->path,
2040 blob2, 0);
2041 if (err)
2042 goto done;
2044 } else {
2045 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2046 err = install_blob(a->worktree, ondisk_path, path2,
2047 /* XXX get this from parent tree! */
2048 GOT_DEFAULT_FILE_MODE,
2049 sb.st_mode, blob2, 0, 0, repo,
2050 a->progress_cb, a->progress_arg);
2051 if (err)
2052 goto done;
2053 err = got_fileindex_entry_alloc(&ie,
2054 ondisk_path, path2, NULL, NULL);
2055 if (err)
2056 goto done;
2057 err = got_fileindex_entry_add(a->fileindex, ie);
2058 if (err) {
2059 got_fileindex_entry_free(ie);
2060 goto done;
2064 done:
2065 free(ondisk_path);
2066 return err;
2069 struct check_merge_ok_arg {
2070 struct got_worktree *worktree;
2071 struct got_repository *repo;
2074 static const struct got_error *
2075 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2077 const struct got_error *err = NULL;
2078 struct check_merge_ok_arg *a = arg;
2079 unsigned char status;
2080 struct stat sb;
2081 char *ondisk_path;
2083 /* Reject merges into a work tree with mixed base commits. */
2084 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2085 SHA1_DIGEST_LENGTH))
2086 return got_error(GOT_ERR_MIXED_COMMITS);
2088 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2089 == -1)
2090 return got_error_from_errno("asprintf");
2092 /* Reject merges into a work tree with conflicted files. */
2093 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2094 if (err)
2095 return err;
2096 if (status == GOT_STATUS_CONFLICT)
2097 return got_error(GOT_ERR_CONFLICTS);
2099 return NULL;
2102 static const struct got_error *
2103 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2104 const char *fileindex_path, struct got_object_id *commit_id1,
2105 struct got_object_id *commit_id2, struct got_repository *repo,
2106 got_worktree_checkout_cb progress_cb, void *progress_arg,
2107 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2109 const struct got_error *err = NULL, *sync_err;
2110 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2111 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2112 struct merge_file_cb_arg arg;
2114 if (commit_id1) {
2115 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2116 worktree->path_prefix);
2117 if (err)
2118 goto done;
2120 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2121 if (err)
2122 goto done;
2125 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2126 worktree->path_prefix);
2127 if (err)
2128 goto done;
2130 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2131 if (err)
2132 goto done;
2134 arg.worktree = worktree;
2135 arg.fileindex = fileindex;
2136 arg.progress_cb = progress_cb;
2137 arg.progress_arg = progress_arg;
2138 arg.cancel_cb = cancel_cb;
2139 arg.cancel_arg = cancel_arg;
2140 arg.commit_id2 = commit_id2;
2141 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2142 sync_err = sync_fileindex(fileindex, fileindex_path);
2143 if (sync_err && err == NULL)
2144 err = sync_err;
2145 done:
2146 if (tree1)
2147 got_object_tree_close(tree1);
2148 if (tree2)
2149 got_object_tree_close(tree2);
2150 return err;
2153 const struct got_error *
2154 got_worktree_merge_files(struct got_worktree *worktree,
2155 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2156 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2157 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2159 const struct got_error *err, *unlockerr;
2160 char *fileindex_path = NULL;
2161 struct got_fileindex *fileindex = NULL;
2162 struct check_merge_ok_arg mok_arg;
2164 err = lock_worktree(worktree, LOCK_EX);
2165 if (err)
2166 return err;
2168 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2169 if (err)
2170 goto done;
2172 mok_arg.worktree = worktree;
2173 mok_arg.repo = repo;
2174 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2175 &mok_arg);
2176 if (err)
2177 goto done;
2179 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2180 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2181 done:
2182 if (fileindex)
2183 got_fileindex_free(fileindex);
2184 free(fileindex_path);
2185 unlockerr = lock_worktree(worktree, LOCK_SH);
2186 if (unlockerr && err == NULL)
2187 err = unlockerr;
2188 return err;
2191 struct diff_dir_cb_arg {
2192 struct got_fileindex *fileindex;
2193 struct got_worktree *worktree;
2194 const char *status_path;
2195 size_t status_path_len;
2196 struct got_repository *repo;
2197 got_worktree_status_cb status_cb;
2198 void *status_arg;
2199 got_worktree_cancel_cb cancel_cb;
2200 void *cancel_arg;
2203 static const struct got_error *
2204 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2205 got_worktree_status_cb status_cb, void *status_arg,
2206 struct got_repository *repo)
2208 const struct got_error *err = NULL;
2209 unsigned char status = GOT_STATUS_NO_CHANGE;
2210 unsigned char staged_status = get_staged_status(ie);
2211 struct stat sb;
2212 struct got_object_id blob_id, commit_id;
2214 err = get_file_status(&status, &sb, ie, abspath, repo);
2215 if (err == NULL && (status != GOT_STATUS_NO_CHANGE ||
2216 staged_status != GOT_STATUS_NO_CHANGE)) {
2217 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2218 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2219 err = (*status_cb)(status_arg, status, staged_status,
2220 ie->path, &blob_id, &commit_id);
2222 return err;
2225 static const struct got_error *
2226 status_old_new(void *arg, struct got_fileindex_entry *ie,
2227 struct dirent *de, const char *parent_path)
2229 const struct got_error *err = NULL;
2230 struct diff_dir_cb_arg *a = arg;
2231 char *abspath;
2233 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2234 return got_error(GOT_ERR_CANCELLED);
2236 if (got_path_cmp(parent_path, a->status_path,
2237 strlen(parent_path), a->status_path_len) != 0 &&
2238 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2239 return NULL;
2241 if (parent_path[0]) {
2242 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2243 parent_path, de->d_name) == -1)
2244 return got_error_from_errno("asprintf");
2245 } else {
2246 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2247 de->d_name) == -1)
2248 return got_error_from_errno("asprintf");
2251 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2252 a->repo);
2253 free(abspath);
2254 return err;
2257 static const struct got_error *
2258 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2260 struct diff_dir_cb_arg *a = arg;
2261 struct got_object_id blob_id, commit_id;
2262 unsigned char status;
2264 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2265 return got_error(GOT_ERR_CANCELLED);
2267 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2268 return NULL;
2270 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2271 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2272 if (got_fileindex_entry_has_file_on_disk(ie))
2273 status = GOT_STATUS_MISSING;
2274 else
2275 status = GOT_STATUS_DELETE;
2276 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2277 ie->path, &blob_id, &commit_id);
2280 static const struct got_error *
2281 status_new(void *arg, struct dirent *de, const char *parent_path)
2283 const struct got_error *err = NULL;
2284 struct diff_dir_cb_arg *a = arg;
2285 char *path = NULL;
2287 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2288 return got_error(GOT_ERR_CANCELLED);
2290 if (de->d_type == DT_DIR)
2291 return NULL;
2293 /* XXX ignore symlinks for now */
2294 if (de->d_type == DT_LNK)
2295 return NULL;
2297 if (parent_path[0]) {
2298 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2299 return got_error_from_errno("asprintf");
2300 } else {
2301 path = de->d_name;
2304 if (got_path_is_child(path, a->status_path, a->status_path_len))
2305 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2306 GOT_STATUS_NO_CHANGE, path, NULL, NULL);
2307 if (parent_path[0])
2308 free(path);
2309 return err;
2312 static const struct got_error *
2313 report_single_file_status(const char *path, const char *ondisk_path,
2314 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2315 void *status_arg, struct got_repository *repo)
2317 struct got_fileindex_entry *ie;
2318 struct stat sb;
2320 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2321 if (ie)
2322 return report_file_status(ie, ondisk_path, status_cb,
2323 status_arg, repo);
2325 if (lstat(ondisk_path, &sb) == -1) {
2326 if (errno != ENOENT)
2327 return got_error_from_errno2("lstat", ondisk_path);
2328 return NULL;
2331 if (S_ISREG(sb.st_mode))
2332 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2333 GOT_STATUS_NO_CHANGE, path, NULL, NULL);
2335 return NULL;
2338 static const struct got_error *
2339 worktree_status(struct got_worktree *worktree, const char *path,
2340 struct got_fileindex *fileindex, struct got_repository *repo,
2341 got_worktree_status_cb status_cb, void *status_arg,
2342 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2344 const struct got_error *err = NULL;
2345 DIR *workdir = NULL;
2346 struct got_fileindex_diff_dir_cb fdiff_cb;
2347 struct diff_dir_cb_arg arg;
2348 char *ondisk_path = NULL;
2350 if (asprintf(&ondisk_path, "%s%s%s",
2351 worktree->root_path, path[0] ? "/" : "", path) == -1)
2352 return got_error_from_errno("asprintf");
2354 workdir = opendir(ondisk_path);
2355 if (workdir == NULL) {
2356 if (errno != ENOTDIR && errno != ENOENT)
2357 err = got_error_from_errno2("opendir", ondisk_path);
2358 else
2359 err = report_single_file_status(path, ondisk_path,
2360 fileindex, status_cb, status_arg, repo);
2361 } else {
2362 fdiff_cb.diff_old_new = status_old_new;
2363 fdiff_cb.diff_old = status_old;
2364 fdiff_cb.diff_new = status_new;
2365 arg.fileindex = fileindex;
2366 arg.worktree = worktree;
2367 arg.status_path = path;
2368 arg.status_path_len = strlen(path);
2369 arg.repo = repo;
2370 arg.status_cb = status_cb;
2371 arg.status_arg = status_arg;
2372 arg.cancel_cb = cancel_cb;
2373 arg.cancel_arg = cancel_arg;
2374 err = got_fileindex_diff_dir(fileindex, workdir,
2375 worktree->root_path, path, repo, &fdiff_cb, &arg);
2378 if (workdir)
2379 closedir(workdir);
2380 free(ondisk_path);
2381 return err;
2384 const struct got_error *
2385 got_worktree_status(struct got_worktree *worktree,
2386 struct got_pathlist_head *paths, struct got_repository *repo,
2387 got_worktree_status_cb status_cb, void *status_arg,
2388 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2390 const struct got_error *err = NULL;
2391 char *fileindex_path = NULL;
2392 struct got_fileindex *fileindex = NULL;
2393 struct got_pathlist_entry *pe;
2395 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2396 if (err)
2397 return err;
2399 TAILQ_FOREACH(pe, paths, entry) {
2400 err = worktree_status(worktree, pe->path, fileindex, repo,
2401 status_cb, status_arg, cancel_cb, cancel_arg);
2402 if (err)
2403 break;
2405 free(fileindex_path);
2406 got_fileindex_free(fileindex);
2407 return err;
2410 const struct got_error *
2411 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2412 const char *arg)
2414 const struct got_error *err = NULL;
2415 char *resolved, *cwd = NULL, *path = NULL;
2416 size_t len;
2418 *wt_path = NULL;
2420 resolved = realpath(arg, NULL);
2421 if (resolved == NULL) {
2422 if (errno != ENOENT)
2423 return got_error_from_errno2("realpath", arg);
2424 cwd = getcwd(NULL, 0);
2425 if (cwd == NULL)
2426 return got_error_from_errno("getcwd");
2427 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2428 err = got_error_from_errno("asprintf");
2429 goto done;
2433 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2434 strlen(got_worktree_get_root_path(worktree)))) {
2435 err = got_error(GOT_ERR_BAD_PATH);
2436 goto done;
2439 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2440 err = got_path_skip_common_ancestor(&path,
2441 got_worktree_get_root_path(worktree), resolved);
2442 if (err)
2443 goto done;
2444 } else {
2445 path = strdup("");
2446 if (path == NULL) {
2447 err = got_error_from_errno("strdup");
2448 goto done;
2452 /* XXX status walk can't deal with trailing slash! */
2453 len = strlen(path);
2454 while (len > 0 && path[len - 1] == '/') {
2455 path[len - 1] = '\0';
2456 len--;
2458 done:
2459 free(resolved);
2460 free(cwd);
2461 if (err == NULL)
2462 *wt_path = path;
2463 else
2464 free(path);
2465 return err;
2468 static const struct got_error *
2469 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2470 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2471 struct got_repository *repo)
2473 const struct got_error *err = NULL;
2474 struct got_fileindex_entry *ie;
2476 /* Re-adding an existing entry is a no-op. */
2477 if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2478 return NULL;
2480 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2481 if (err)
2482 return err;
2484 err = got_fileindex_entry_add(fileindex, ie);
2485 if (err) {
2486 got_fileindex_entry_free(ie);
2487 return err;
2490 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2493 const struct got_error *
2494 got_worktree_schedule_add(struct got_worktree *worktree,
2495 struct got_pathlist_head *ondisk_paths,
2496 got_worktree_status_cb status_cb, void *status_arg,
2497 struct got_repository *repo)
2499 struct got_fileindex *fileindex = NULL;
2500 char *fileindex_path = NULL;
2501 const struct got_error *err = NULL, *sync_err, *unlockerr;
2502 struct got_pathlist_entry *pe;
2504 err = lock_worktree(worktree, LOCK_EX);
2505 if (err)
2506 return err;
2508 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2509 if (err)
2510 goto done;
2512 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2513 char *relpath;
2514 err = got_path_skip_common_ancestor(&relpath,
2515 got_worktree_get_root_path(worktree), pe->path);
2516 if (err)
2517 break;
2518 err = schedule_addition(pe->path, fileindex, relpath,
2519 status_cb, status_arg, repo);
2520 free(relpath);
2521 if (err)
2522 break;
2524 sync_err = sync_fileindex(fileindex, fileindex_path);
2525 if (sync_err && err == NULL)
2526 err = sync_err;
2527 done:
2528 free(fileindex_path);
2529 if (fileindex)
2530 got_fileindex_free(fileindex);
2531 unlockerr = lock_worktree(worktree, LOCK_SH);
2532 if (unlockerr && err == NULL)
2533 err = unlockerr;
2534 return err;
2537 static const struct got_error *
2538 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2539 const char *relpath, int delete_local_mods,
2540 got_worktree_status_cb status_cb, void *status_arg,
2541 struct got_repository *repo)
2543 const struct got_error *err = NULL;
2544 struct got_fileindex_entry *ie = NULL;
2545 unsigned char status;
2546 struct stat sb;
2548 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2549 if (ie == NULL)
2550 return got_error(GOT_ERR_BAD_PATH);
2552 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2553 if (err)
2554 return err;
2556 if (status != GOT_STATUS_NO_CHANGE) {
2557 if (status == GOT_STATUS_DELETE)
2558 return NULL;
2559 if (status != GOT_STATUS_MODIFY)
2560 return got_error(GOT_ERR_FILE_STATUS);
2561 if (!delete_local_mods)
2562 return got_error(GOT_ERR_FILE_MODIFIED);
2565 if (unlink(ondisk_path) != 0)
2566 return got_error_from_errno2("unlink", ondisk_path);
2568 got_fileindex_entry_mark_deleted_from_disk(ie);
2569 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2572 const struct got_error *
2573 got_worktree_schedule_delete(struct got_worktree *worktree,
2574 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2575 got_worktree_status_cb status_cb, void *status_arg,
2576 struct got_repository *repo)
2578 struct got_fileindex *fileindex = NULL;
2579 char *fileindex_path = NULL;
2580 const struct got_error *err = NULL, *sync_err, *unlockerr;
2581 struct got_pathlist_entry *pe;
2583 err = lock_worktree(worktree, LOCK_EX);
2584 if (err)
2585 return err;
2587 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2588 if (err)
2589 goto done;
2591 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2592 char *relpath;
2593 err = got_path_skip_common_ancestor(&relpath,
2594 got_worktree_get_root_path(worktree), pe->path);
2595 if (err)
2596 break;
2597 err = schedule_for_deletion(pe->path, fileindex, relpath,
2598 delete_local_mods, status_cb, status_arg, repo);
2599 free(relpath);
2600 if (err)
2601 break;
2603 sync_err = sync_fileindex(fileindex, fileindex_path);
2604 if (sync_err && err == NULL)
2605 err = sync_err;
2606 done:
2607 free(fileindex_path);
2608 if (fileindex)
2609 got_fileindex_free(fileindex);
2610 unlockerr = lock_worktree(worktree, LOCK_SH);
2611 if (unlockerr && err == NULL)
2612 err = unlockerr;
2613 return err;
2616 static const struct got_error *
2617 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2618 const char *ondisk_path,
2619 got_worktree_checkout_cb progress_cb, void *progress_arg,
2620 struct got_repository *repo)
2622 const struct got_error *err = NULL;
2623 char *relpath = NULL, *parent_path = NULL;
2624 struct got_fileindex_entry *ie;
2625 struct got_tree_object *tree = NULL;
2626 struct got_object_id *tree_id = NULL;
2627 const struct got_tree_entry *te;
2628 char *tree_path = NULL, *te_name;
2629 struct got_blob_object *blob = NULL;
2630 unsigned char status;
2631 struct stat sb;
2633 err = got_path_skip_common_ancestor(&relpath,
2634 got_worktree_get_root_path(worktree), ondisk_path);
2635 if (err)
2636 goto done;
2638 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2639 if (ie == NULL) {
2640 err = got_error(GOT_ERR_BAD_PATH);
2641 goto done;
2644 /* Construct in-repository path of tree which contains this blob. */
2645 err = got_path_dirname(&parent_path, ie->path);
2646 if (err) {
2647 if (err->code != GOT_ERR_BAD_PATH)
2648 goto done;
2649 parent_path = strdup("/");
2650 if (parent_path == NULL) {
2651 err = got_error_from_errno("strdup");
2652 goto done;
2655 if (got_path_is_root_dir(worktree->path_prefix)) {
2656 tree_path = strdup(parent_path);
2657 if (tree_path == NULL) {
2658 err = got_error_from_errno("strdup");
2659 goto done;
2661 } else {
2662 if (got_path_is_root_dir(parent_path)) {
2663 tree_path = strdup(worktree->path_prefix);
2664 if (tree_path == NULL) {
2665 err = got_error_from_errno("strdup");
2666 goto done;
2668 } else {
2669 if (asprintf(&tree_path, "%s/%s",
2670 worktree->path_prefix, parent_path) == -1) {
2671 err = got_error_from_errno("asprintf");
2672 goto done;
2677 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2678 if (err)
2679 goto done;
2680 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2681 sb.st_mode = got_fileindex_perms_to_st(ie);
2683 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2684 tree_path);
2685 if (err) {
2686 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2687 status == GOT_STATUS_ADD))
2688 goto done;
2689 } else {
2690 err = got_object_open_as_tree(&tree, repo, tree_id);
2691 if (err)
2692 goto done;
2694 te_name = basename(ie->path);
2695 if (te_name == NULL) {
2696 err = got_error_from_errno2("basename", ie->path);
2697 goto done;
2700 te = got_object_tree_find_entry(tree, te_name);
2701 if (te == NULL && status != GOT_STATUS_ADD) {
2702 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2703 goto done;
2707 switch (status) {
2708 case GOT_STATUS_ADD:
2709 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2710 if (err)
2711 goto done;
2712 got_fileindex_entry_remove(fileindex, ie);
2713 break;
2714 case GOT_STATUS_DELETE:
2715 case GOT_STATUS_MODIFY:
2716 case GOT_STATUS_CONFLICT:
2717 case GOT_STATUS_MISSING: {
2718 struct got_object_id id;
2719 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2720 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2721 if (err)
2722 goto done;
2723 err = install_blob(worktree, ondisk_path, ie->path,
2724 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2725 progress_arg);
2726 if (err)
2727 goto done;
2728 if (status == GOT_STATUS_DELETE) {
2729 err = update_blob_fileindex_entry(worktree,
2730 fileindex, ie, ondisk_path, ie->path, blob, 1);
2731 if (err)
2732 goto done;
2734 break;
2736 default:
2737 goto done;
2739 done:
2740 free(relpath);
2741 free(parent_path);
2742 free(tree_path);
2743 if (blob)
2744 got_object_blob_close(blob);
2745 if (tree)
2746 got_object_tree_close(tree);
2747 free(tree_id);
2748 return err;
2751 const struct got_error *
2752 got_worktree_revert(struct got_worktree *worktree,
2753 struct got_pathlist_head *ondisk_paths,
2754 got_worktree_checkout_cb progress_cb, void *progress_arg,
2755 struct got_repository *repo)
2757 struct got_fileindex *fileindex = NULL;
2758 char *fileindex_path = NULL;
2759 const struct got_error *err = NULL, *unlockerr = NULL;
2760 const struct got_error *sync_err = NULL;
2761 struct got_pathlist_entry *pe;
2763 err = lock_worktree(worktree, LOCK_EX);
2764 if (err)
2765 return err;
2767 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2768 if (err)
2769 goto done;
2771 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2772 err = revert_file(worktree, fileindex, pe->path,
2773 progress_cb, progress_arg, repo);
2774 if (err)
2775 break;
2777 sync_err = sync_fileindex(fileindex, fileindex_path);
2778 if (sync_err && err == NULL)
2779 err = sync_err;
2780 done:
2781 free(fileindex_path);
2782 if (fileindex)
2783 got_fileindex_free(fileindex);
2784 unlockerr = lock_worktree(worktree, LOCK_SH);
2785 if (unlockerr && err == NULL)
2786 err = unlockerr;
2787 return err;
2790 static void
2791 free_commitable(struct got_commitable *ct)
2793 free(ct->path);
2794 free(ct->in_repo_path);
2795 free(ct->ondisk_path);
2796 free(ct->blob_id);
2797 free(ct->base_blob_id);
2798 free(ct->base_commit_id);
2799 free(ct);
2802 struct collect_commitables_arg {
2803 struct got_pathlist_head *commitable_paths;
2804 struct got_repository *repo;
2805 struct got_worktree *worktree;
2808 static const struct got_error *
2809 collect_commitables(void *arg, unsigned char status,
2810 unsigned char staged_status, const char *relpath,
2811 struct got_object_id *blob_id, struct got_object_id *commit_id)
2813 struct collect_commitables_arg *a = arg;
2814 const struct got_error *err = NULL;
2815 struct got_commitable *ct = NULL;
2816 struct got_pathlist_entry *new = NULL;
2817 char *parent_path = NULL, *path = NULL;
2818 struct stat sb;
2820 if (status == GOT_STATUS_CONFLICT)
2821 return got_error(GOT_ERR_COMMIT_CONFLICT);
2823 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2824 status != GOT_STATUS_DELETE)
2825 return NULL;
2827 if (asprintf(&path, "/%s", relpath) == -1) {
2828 err = got_error_from_errno("asprintf");
2829 goto done;
2831 if (strcmp(path, "/") == 0) {
2832 parent_path = strdup("");
2833 if (parent_path == NULL)
2834 return got_error_from_errno("strdup");
2835 } else {
2836 err = got_path_dirname(&parent_path, path);
2837 if (err)
2838 return err;
2841 ct = calloc(1, sizeof(*ct));
2842 if (ct == NULL) {
2843 err = got_error_from_errno("calloc");
2844 goto done;
2847 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2848 relpath) == -1) {
2849 err = got_error_from_errno("asprintf");
2850 goto done;
2852 if (status == GOT_STATUS_DELETE) {
2853 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2854 } else {
2855 if (lstat(ct->ondisk_path, &sb) != 0) {
2856 err = got_error_from_errno2("lstat", ct->ondisk_path);
2857 goto done;
2859 ct->mode = sb.st_mode;
2862 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2863 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2864 relpath) == -1) {
2865 err = got_error_from_errno("asprintf");
2866 goto done;
2869 ct->status = status;
2870 ct->blob_id = NULL; /* will be filled in when blob gets created */
2871 if (ct->status != GOT_STATUS_ADD) {
2872 ct->base_blob_id = got_object_id_dup(blob_id);
2873 if (ct->base_blob_id == NULL) {
2874 err = got_error_from_errno("got_object_id_dup");
2875 goto done;
2877 ct->base_commit_id = got_object_id_dup(commit_id);
2878 if (ct->base_commit_id == NULL) {
2879 err = got_error_from_errno("got_object_id_dup");
2880 goto done;
2883 ct->path = strdup(path);
2884 if (ct->path == NULL) {
2885 err = got_error_from_errno("strdup");
2886 goto done;
2888 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2889 done:
2890 if (ct && (err || new == NULL))
2891 free_commitable(ct);
2892 free(parent_path);
2893 free(path);
2894 return err;
2897 static const struct got_error *write_tree(struct got_object_id **,
2898 struct got_tree_object *, const char *, struct got_pathlist_head *,
2899 got_worktree_status_cb status_cb, void *status_arg,
2900 struct got_repository *);
2902 static const struct got_error *
2903 write_subtree(struct got_object_id **new_subtree_id,
2904 struct got_tree_entry *te, const char *parent_path,
2905 struct got_pathlist_head *commitable_paths,
2906 got_worktree_status_cb status_cb, void *status_arg,
2907 struct got_repository *repo)
2909 const struct got_error *err = NULL;
2910 struct got_tree_object *subtree;
2911 char *subpath;
2913 if (asprintf(&subpath, "%s%s%s", parent_path,
2914 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2915 return got_error_from_errno("asprintf");
2917 err = got_object_open_as_tree(&subtree, repo, te->id);
2918 if (err)
2919 return err;
2921 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2922 status_cb, status_arg, repo);
2923 got_object_tree_close(subtree);
2924 free(subpath);
2925 return err;
2928 static const struct got_error *
2929 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2931 const struct got_error *err = NULL;
2932 char *ct_parent_path = NULL;
2934 *match = 0;
2936 if (strchr(ct->in_repo_path, '/') == NULL) {
2937 *match = got_path_is_root_dir(path);
2938 return NULL;
2941 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2942 if (err)
2943 return err;
2944 *match = (strcmp(path, ct_parent_path) == 0);
2945 free(ct_parent_path);
2946 return err;
2949 static mode_t
2950 get_ct_file_mode(struct got_commitable *ct)
2952 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2955 static const struct got_error *
2956 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2957 struct got_tree_entry *te, struct got_commitable *ct)
2959 const struct got_error *err = NULL;
2961 *new_te = NULL;
2963 err = got_object_tree_entry_dup(new_te, te);
2964 if (err)
2965 goto done;
2967 (*new_te)->mode = get_ct_file_mode(ct);
2969 free((*new_te)->id);
2970 (*new_te)->id = got_object_id_dup(ct->blob_id);
2971 if ((*new_te)->id == NULL) {
2972 err = got_error_from_errno("got_object_id_dup");
2973 goto done;
2975 done:
2976 if (err && *new_te) {
2977 got_object_tree_entry_close(*new_te);
2978 *new_te = NULL;
2980 return err;
2983 static const struct got_error *
2984 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2985 struct got_commitable *ct)
2987 const struct got_error *err = NULL;
2988 char *ct_name;
2990 *new_te = NULL;
2992 *new_te = calloc(1, sizeof(**new_te));
2993 if (*new_te == NULL)
2994 return got_error_from_errno("calloc");
2996 ct_name = basename(ct->path);
2997 if (ct_name == NULL) {
2998 err = got_error_from_errno2("basename", ct->path);
2999 goto done;
3001 (*new_te)->name = strdup(ct_name);
3002 if ((*new_te)->name == NULL) {
3003 err = got_error_from_errno("strdup");
3004 goto done;
3007 (*new_te)->mode = get_ct_file_mode(ct);
3009 (*new_te)->id = got_object_id_dup(ct->blob_id);
3010 if ((*new_te)->id == NULL) {
3011 err = got_error_from_errno("got_object_id_dup");
3012 goto done;
3014 done:
3015 if (err && *new_te) {
3016 got_object_tree_entry_close(*new_te);
3017 *new_te = NULL;
3019 return err;
3022 static const struct got_error *
3023 insert_tree_entry(struct got_tree_entry *new_te,
3024 struct got_pathlist_head *paths)
3026 const struct got_error *err = NULL;
3027 struct got_pathlist_entry *new_pe;
3029 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3030 if (err)
3031 return err;
3032 if (new_pe == NULL)
3033 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3034 return NULL;
3037 static const struct got_error *
3038 report_ct_status(struct got_commitable *ct,
3039 got_worktree_status_cb status_cb, void *status_arg)
3041 const char *ct_path = ct->path;
3042 while (ct_path[0] == '/')
3043 ct_path++;
3044 return (*status_cb)(status_arg, ct->status, GOT_STATUS_NO_CHANGE,
3045 ct_path, ct->blob_id, NULL);
3048 static const struct got_error *
3049 match_modified_subtree(int *modified, struct got_tree_entry *te,
3050 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3052 const struct got_error *err = NULL;
3053 struct got_pathlist_entry *pe;
3054 char *te_path;
3056 *modified = 0;
3058 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3059 got_path_is_root_dir(base_tree_path) ? "" : "/",
3060 te->name) == -1)
3061 return got_error_from_errno("asprintf");
3063 TAILQ_FOREACH(pe, commitable_paths, entry) {
3064 struct got_commitable *ct = pe->data;
3065 *modified = got_path_is_child(ct->in_repo_path, te_path,
3066 strlen(te_path));
3067 if (*modified)
3068 break;
3071 free(te_path);
3072 return err;
3075 static const struct got_error *
3076 match_deleted_or_modified_ct(struct got_commitable **ctp,
3077 struct got_tree_entry *te, const char *base_tree_path,
3078 struct got_pathlist_head *commitable_paths)
3080 const struct got_error *err = NULL;
3081 struct got_pathlist_entry *pe;
3083 *ctp = NULL;
3085 TAILQ_FOREACH(pe, commitable_paths, entry) {
3086 struct got_commitable *ct = pe->data;
3087 char *ct_name = NULL;
3088 int path_matches;
3090 if (ct->status != GOT_STATUS_MODIFY &&
3091 ct->status != GOT_STATUS_DELETE)
3092 continue;
3094 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3095 continue;
3097 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3098 if (err)
3099 return err;
3100 if (!path_matches)
3101 continue;
3103 ct_name = basename(pe->path);
3104 if (ct_name == NULL)
3105 return got_error_from_errno2("basename", pe->path);
3107 if (strcmp(te->name, ct_name) != 0)
3108 continue;
3110 *ctp = ct;
3111 break;
3114 return err;
3117 static const struct got_error *
3118 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3119 const char *child_path, const char *path_base_tree,
3120 struct got_pathlist_head *commitable_paths,
3121 got_worktree_status_cb status_cb, void *status_arg,
3122 struct got_repository *repo)
3124 const struct got_error *err = NULL;
3125 struct got_tree_entry *new_te;
3126 char *subtree_path;
3128 *new_tep = NULL;
3130 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3131 got_path_is_root_dir(path_base_tree) ? "" : "/",
3132 child_path) == -1)
3133 return got_error_from_errno("asprintf");
3135 new_te = calloc(1, sizeof(*new_te));
3136 new_te->mode = S_IFDIR;
3137 new_te->name = strdup(child_path);
3138 if (new_te->name == NULL) {
3139 err = got_error_from_errno("strdup");
3140 got_object_tree_entry_close(new_te);
3141 goto done;
3143 err = write_tree(&new_te->id, NULL, subtree_path,
3144 commitable_paths, status_cb, status_arg, repo);
3145 if (err) {
3146 got_object_tree_entry_close(new_te);
3147 goto done;
3149 done:
3150 free(subtree_path);
3151 if (err == NULL)
3152 *new_tep = new_te;
3153 return err;
3156 static const struct got_error *
3157 write_tree(struct got_object_id **new_tree_id,
3158 struct got_tree_object *base_tree, const char *path_base_tree,
3159 struct got_pathlist_head *commitable_paths,
3160 got_worktree_status_cb status_cb, void *status_arg,
3161 struct got_repository *repo)
3163 const struct got_error *err = NULL;
3164 const struct got_tree_entries *base_entries = NULL;
3165 struct got_pathlist_head paths;
3166 struct got_tree_entries new_tree_entries;
3167 struct got_tree_entry *te, *new_te = NULL;
3168 struct got_pathlist_entry *pe;
3170 TAILQ_INIT(&paths);
3171 new_tree_entries.nentries = 0;
3172 SIMPLEQ_INIT(&new_tree_entries.head);
3174 /* Insert, and recurse into, newly added entries first. */
3175 TAILQ_FOREACH(pe, commitable_paths, entry) {
3176 struct got_commitable *ct = pe->data;
3177 char *child_path = NULL, *slash;
3179 if (ct->status != GOT_STATUS_ADD ||
3180 (ct->flags & GOT_COMMITABLE_ADDED))
3181 continue;
3183 if (!got_path_is_child(pe->path, path_base_tree,
3184 strlen(path_base_tree)))
3185 continue;
3187 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3188 pe->path);
3189 if (err)
3190 goto done;
3192 slash = strchr(child_path, '/');
3193 if (slash == NULL) {
3194 err = alloc_added_blob_tree_entry(&new_te, ct);
3195 if (err)
3196 goto done;
3197 err = report_ct_status(ct, status_cb, status_arg);
3198 if (err)
3199 goto done;
3200 ct->flags |= GOT_COMMITABLE_ADDED;
3201 err = insert_tree_entry(new_te, &paths);
3202 if (err)
3203 goto done;
3204 } else {
3205 *slash = '\0'; /* trim trailing path components */
3206 if (base_tree == NULL ||
3207 got_object_tree_find_entry(base_tree, child_path)
3208 == NULL) {
3209 err = make_subtree_for_added_blob(&new_te,
3210 child_path, path_base_tree,
3211 commitable_paths, status_cb, status_arg,
3212 repo);
3213 if (err)
3214 goto done;
3215 err = insert_tree_entry(new_te, &paths);
3216 if (err)
3217 goto done;
3222 if (base_tree) {
3223 /* Handle modified and deleted entries. */
3224 base_entries = got_object_tree_get_entries(base_tree);
3225 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3226 struct got_commitable *ct = NULL;
3228 if (S_ISDIR(te->mode)) {
3229 int modified;
3230 err = got_object_tree_entry_dup(&new_te, te);
3231 if (err)
3232 goto done;
3233 err = match_modified_subtree(&modified, te,
3234 path_base_tree, commitable_paths);
3235 if (err)
3236 goto done;
3237 /* Avoid recursion into unmodified subtrees. */
3238 if (modified) {
3239 free(new_te->id);
3240 err = write_subtree(&new_te->id, te,
3241 path_base_tree, commitable_paths,
3242 status_cb, status_arg, repo);
3243 if (err)
3244 goto done;
3246 err = insert_tree_entry(new_te, &paths);
3247 if (err)
3248 goto done;
3249 continue;
3252 err = match_deleted_or_modified_ct(&ct, te,
3253 path_base_tree, commitable_paths);
3254 if (ct) {
3255 /* NB: Deleted entries get dropped here. */
3256 if (ct->status == GOT_STATUS_MODIFY) {
3257 err = alloc_modified_blob_tree_entry(
3258 &new_te, te, ct);
3259 if (err)
3260 goto done;
3261 err = insert_tree_entry(new_te, &paths);
3262 if (err)
3263 goto done;
3265 err = report_ct_status(ct, status_cb,
3266 status_arg);
3267 if (err)
3268 goto done;
3269 } else {
3270 /* Entry is unchanged; just copy it. */
3271 err = got_object_tree_entry_dup(&new_te, te);
3272 if (err)
3273 goto done;
3274 err = insert_tree_entry(new_te, &paths);
3275 if (err)
3276 goto done;
3281 /* Write new list of entries; deleted entries have been dropped. */
3282 TAILQ_FOREACH(pe, &paths, entry) {
3283 struct got_tree_entry *te = pe->data;
3284 new_tree_entries.nentries++;
3285 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3287 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3288 done:
3289 got_object_tree_entries_close(&new_tree_entries);
3290 got_pathlist_free(&paths);
3291 return err;
3294 static const struct got_error *
3295 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3296 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3298 const struct got_error *err = NULL;
3299 struct got_pathlist_entry *pe;
3301 TAILQ_FOREACH(pe, commitable_paths, entry) {
3302 struct got_fileindex_entry *ie;
3303 struct got_commitable *ct = pe->data;
3305 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3306 if (ie) {
3307 if (ct->status == GOT_STATUS_DELETE) {
3308 got_fileindex_entry_remove(fileindex, ie);
3309 got_fileindex_entry_free(ie);
3310 } else
3311 err = got_fileindex_entry_update(ie,
3312 ct->ondisk_path, ct->blob_id->sha1,
3313 new_base_commit_id->sha1, 1);
3314 } else {
3315 err = got_fileindex_entry_alloc(&ie,
3316 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3317 new_base_commit_id->sha1);
3318 if (err)
3319 break;
3320 err = got_fileindex_entry_add(fileindex, ie);
3321 if (err)
3322 break;
3325 return err;
3328 static const struct got_error *
3329 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3330 struct got_object_id *head_commit_id)
3332 const struct got_error *err = NULL;
3333 struct got_object_id *id = NULL;
3334 struct got_commit_object *commit = NULL;
3335 const char *ct_path = ct->in_repo_path;
3337 while (ct_path[0] == '/')
3338 ct_path++;
3340 if (ct->status != GOT_STATUS_ADD) {
3341 /* Trivial case: base commit == head commit */
3342 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3343 return NULL;
3345 * Ensure file content which local changes were based
3346 * on matches file content in the branch head.
3348 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3349 if (err) {
3350 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3351 goto done;
3352 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3353 goto done;
3354 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3355 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3356 } else {
3357 /* Require that added files don't exist in the branch head. */
3358 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3359 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3360 goto done;
3361 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3363 done:
3364 if (commit)
3365 got_object_commit_close(commit);
3366 free(id);
3367 return err;
3370 const struct got_error *
3371 commit_worktree(struct got_object_id **new_commit_id,
3372 struct got_pathlist_head *commitable_paths,
3373 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3374 const char *author, const char *committer,
3375 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3376 got_worktree_status_cb status_cb, void *status_arg,
3377 struct got_repository *repo)
3379 const struct got_error *err = NULL, *unlockerr = NULL;
3380 struct got_pathlist_entry *pe;
3381 const char *head_ref_name = NULL;
3382 struct got_commit_object *head_commit = NULL;
3383 struct got_reference *head_ref2 = NULL;
3384 struct got_object_id *head_commit_id2 = NULL;
3385 struct got_tree_object *head_tree = NULL;
3386 struct got_object_id *new_tree_id = NULL;
3387 struct got_object_id_queue parent_ids;
3388 struct got_object_qid *pid = NULL;
3389 char *logmsg = NULL;
3391 *new_commit_id = NULL;
3393 SIMPLEQ_INIT(&parent_ids);
3395 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3396 if (err)
3397 goto done;
3399 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3400 if (err)
3401 goto done;
3403 if (commit_msg_cb != NULL) {
3404 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3405 if (err)
3406 goto done;
3409 if (logmsg == NULL || strlen(logmsg) == 0) {
3410 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3411 goto done;
3414 /* Create blobs from added and modified files and record their IDs. */
3415 TAILQ_FOREACH(pe, commitable_paths, entry) {
3416 struct got_commitable *ct = pe->data;
3417 char *ondisk_path;
3419 if (ct->status != GOT_STATUS_ADD &&
3420 ct->status != GOT_STATUS_MODIFY)
3421 continue;
3423 if (asprintf(&ondisk_path, "%s/%s",
3424 worktree->root_path, pe->path) == -1) {
3425 err = got_error_from_errno("asprintf");
3426 goto done;
3428 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3429 free(ondisk_path);
3430 if (err)
3431 goto done;
3434 /* Recursively write new tree objects. */
3435 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3436 status_cb, status_arg, repo);
3437 if (err)
3438 goto done;
3440 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3441 if (err)
3442 goto done;
3443 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3444 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3445 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3446 got_object_qid_free(pid);
3447 if (logmsg != NULL)
3448 free(logmsg);
3449 if (err)
3450 goto done;
3452 /* Check if a concurrent commit to our branch has occurred. */
3453 head_ref_name = got_worktree_get_head_ref_name(worktree);
3454 if (head_ref_name == NULL) {
3455 err = got_error_from_errno("got_worktree_get_head_ref_name");
3456 goto done;
3458 /* Lock the reference here to prevent concurrent modification. */
3459 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3460 if (err)
3461 goto done;
3462 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3463 if (err)
3464 goto done;
3465 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3466 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3467 goto done;
3469 /* Update branch head in repository. */
3470 err = got_ref_change_ref(head_ref2, *new_commit_id);
3471 if (err)
3472 goto done;
3473 err = got_ref_write(head_ref2, repo);
3474 if (err)
3475 goto done;
3477 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3478 if (err)
3479 goto done;
3481 err = ref_base_commit(worktree, repo);
3482 if (err)
3483 goto done;
3484 done:
3485 if (head_tree)
3486 got_object_tree_close(head_tree);
3487 if (head_commit)
3488 got_object_commit_close(head_commit);
3489 free(head_commit_id2);
3490 if (head_ref2) {
3491 unlockerr = got_ref_unlock(head_ref2);
3492 if (unlockerr && err == NULL)
3493 err = unlockerr;
3494 got_ref_close(head_ref2);
3496 return err;
3499 static const struct got_error *
3500 check_path_is_commitable(const char *path,
3501 struct got_pathlist_head *commitable_paths)
3503 struct got_pathlist_entry *cpe = NULL;
3504 size_t path_len = strlen(path);
3506 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3507 struct got_commitable *ct = cpe->data;
3508 const char *ct_path = ct->path;
3510 while (ct_path[0] == '/')
3511 ct_path++;
3513 if (strcmp(path, ct_path) == 0 ||
3514 got_path_is_child(ct_path, path, path_len))
3515 break;
3518 if (cpe == NULL)
3519 return got_error_path(path, GOT_ERR_BAD_PATH);
3521 return NULL;
3524 const struct got_error *
3525 got_worktree_commit(struct got_object_id **new_commit_id,
3526 struct got_worktree *worktree, struct got_pathlist_head *paths,
3527 const char *author, const char *committer,
3528 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3529 got_worktree_status_cb status_cb, void *status_arg,
3530 struct got_repository *repo)
3532 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3533 struct got_fileindex *fileindex = NULL;
3534 char *fileindex_path = NULL;
3535 struct got_pathlist_head commitable_paths;
3536 struct collect_commitables_arg cc_arg;
3537 struct got_pathlist_entry *pe;
3538 struct got_reference *head_ref = NULL;
3539 struct got_object_id *head_commit_id = NULL;
3541 *new_commit_id = NULL;
3543 TAILQ_INIT(&commitable_paths);
3545 err = lock_worktree(worktree, LOCK_EX);
3546 if (err)
3547 goto done;
3549 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3550 if (err)
3551 goto done;
3553 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3554 if (err)
3555 goto done;
3557 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3558 if (err)
3559 goto done;
3561 cc_arg.commitable_paths = &commitable_paths;
3562 cc_arg.worktree = worktree;
3563 cc_arg.repo = repo;
3564 TAILQ_FOREACH(pe, paths, entry) {
3565 err = worktree_status(worktree, pe->path, fileindex, repo,
3566 collect_commitables, &cc_arg, NULL, NULL);
3567 if (err)
3568 goto done;
3571 if (TAILQ_EMPTY(&commitable_paths)) {
3572 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3573 goto done;
3576 TAILQ_FOREACH(pe, paths, entry) {
3577 err = check_path_is_commitable(pe->path, &commitable_paths);
3578 if (err)
3579 goto done;
3582 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3583 struct got_commitable *ct = pe->data;
3584 err = check_ct_out_of_date(ct, repo, head_commit_id);
3585 if (err)
3586 goto done;
3589 err = commit_worktree(new_commit_id, &commitable_paths,
3590 head_commit_id, worktree, author, committer,
3591 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3592 if (err)
3593 goto done;
3595 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3596 fileindex);
3597 sync_err = sync_fileindex(fileindex, fileindex_path);
3598 if (sync_err && err == NULL)
3599 err = sync_err;
3600 done:
3601 if (fileindex)
3602 got_fileindex_free(fileindex);
3603 free(fileindex_path);
3604 unlockerr = lock_worktree(worktree, LOCK_SH);
3605 if (unlockerr && err == NULL)
3606 err = unlockerr;
3607 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3608 struct got_commitable *ct = pe->data;
3609 free_commitable(ct);
3611 got_pathlist_free(&commitable_paths);
3612 return err;
3615 const char *
3616 got_commitable_get_path(struct got_commitable *ct)
3618 return ct->path;
3621 unsigned int
3622 got_commitable_get_status(struct got_commitable *ct)
3624 return ct->status;
3627 struct check_rebase_ok_arg {
3628 struct got_worktree *worktree;
3629 struct got_repository *repo;
3632 static const struct got_error *
3633 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3635 const struct got_error *err = NULL;
3636 struct check_rebase_ok_arg *a = arg;
3637 unsigned char status;
3638 struct stat sb;
3639 char *ondisk_path;
3641 /* Reject rebase of a work tree with mixed base commits. */
3642 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3643 SHA1_DIGEST_LENGTH))
3644 return got_error(GOT_ERR_MIXED_COMMITS);
3646 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3647 == -1)
3648 return got_error_from_errno("asprintf");
3650 /* Reject rebase of a work tree with modified or conflicted files. */
3651 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3652 free(ondisk_path);
3653 if (err)
3654 return err;
3656 if (status != GOT_STATUS_NO_CHANGE)
3657 return got_error(GOT_ERR_MODIFIED);
3659 return NULL;
3662 const struct got_error *
3663 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3664 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3665 struct got_worktree *worktree, struct got_reference *branch,
3666 struct got_repository *repo)
3668 const struct got_error *err = NULL;
3669 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3670 char *branch_ref_name = NULL;
3671 char *fileindex_path = NULL;
3672 struct check_rebase_ok_arg ok_arg;
3673 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3675 *new_base_branch_ref = NULL;
3676 *tmp_branch = NULL;
3677 *fileindex = NULL;
3679 err = lock_worktree(worktree, LOCK_EX);
3680 if (err)
3681 return err;
3683 err = open_fileindex(fileindex, &fileindex_path, worktree);
3684 if (err)
3685 goto done;
3687 ok_arg.worktree = worktree;
3688 ok_arg.repo = repo;
3689 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3690 &ok_arg);
3691 if (err)
3692 goto done;
3694 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3695 if (err)
3696 goto done;
3698 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3699 if (err)
3700 goto done;
3702 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3703 if (err)
3704 goto done;
3706 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3707 0);
3708 if (err)
3709 goto done;
3711 err = got_ref_alloc_symref(new_base_branch_ref,
3712 new_base_branch_ref_name, wt_branch);
3713 if (err)
3714 goto done;
3715 err = got_ref_write(*new_base_branch_ref, repo);
3716 if (err)
3717 goto done;
3719 /* TODO Lock original branch's ref while rebasing? */
3721 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3722 if (err)
3723 goto done;
3725 err = got_ref_write(branch_ref, repo);
3726 if (err)
3727 goto done;
3729 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3730 worktree->base_commit_id);
3731 if (err)
3732 goto done;
3733 err = got_ref_write(*tmp_branch, repo);
3734 if (err)
3735 goto done;
3737 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3738 if (err)
3739 goto done;
3740 done:
3741 free(fileindex_path);
3742 free(tmp_branch_name);
3743 free(new_base_branch_ref_name);
3744 free(branch_ref_name);
3745 if (branch_ref)
3746 got_ref_close(branch_ref);
3747 if (wt_branch)
3748 got_ref_close(wt_branch);
3749 if (err) {
3750 if (*new_base_branch_ref) {
3751 got_ref_close(*new_base_branch_ref);
3752 *new_base_branch_ref = NULL;
3754 if (*tmp_branch) {
3755 got_ref_close(*tmp_branch);
3756 *tmp_branch = NULL;
3758 if (*fileindex) {
3759 got_fileindex_free(*fileindex);
3760 *fileindex = NULL;
3762 lock_worktree(worktree, LOCK_SH);
3764 return err;
3767 const struct got_error *
3768 got_worktree_rebase_continue(struct got_object_id **commit_id,
3769 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3770 struct got_reference **branch, struct got_fileindex **fileindex,
3771 struct got_worktree *worktree, struct got_repository *repo)
3773 const struct got_error *err;
3774 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3775 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3776 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3777 char *fileindex_path = NULL;
3779 *commit_id = NULL;
3780 *new_base_branch = NULL;
3781 *tmp_branch = NULL;
3782 *branch = NULL;
3783 *fileindex = NULL;
3785 err = lock_worktree(worktree, LOCK_EX);
3786 if (err)
3787 return err;
3789 err = open_fileindex(fileindex, &fileindex_path, worktree);
3790 if (err)
3791 goto done;
3793 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3794 if (err)
3795 return err;
3797 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3798 if (err)
3799 goto done;
3801 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3802 if (err)
3803 goto done;
3805 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3806 if (err)
3807 goto done;
3809 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3810 if (err)
3811 goto done;
3813 err = got_ref_open(branch, repo,
3814 got_ref_get_symref_target(branch_ref), 0);
3815 if (err)
3816 goto done;
3818 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3819 if (err)
3820 goto done;
3822 err = got_ref_resolve(commit_id, repo, commit_ref);
3823 if (err)
3824 goto done;
3826 err = got_ref_open(new_base_branch, repo,
3827 new_base_branch_ref_name, 0);
3828 if (err)
3829 goto done;
3831 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3832 if (err)
3833 goto done;
3834 done:
3835 free(commit_ref_name);
3836 free(branch_ref_name);
3837 free(fileindex_path);
3838 if (commit_ref)
3839 got_ref_close(commit_ref);
3840 if (branch_ref)
3841 got_ref_close(branch_ref);
3842 if (err) {
3843 free(*commit_id);
3844 *commit_id = NULL;
3845 if (*tmp_branch) {
3846 got_ref_close(*tmp_branch);
3847 *tmp_branch = NULL;
3849 if (*new_base_branch) {
3850 got_ref_close(*new_base_branch);
3851 *new_base_branch = NULL;
3853 if (*branch) {
3854 got_ref_close(*branch);
3855 *branch = NULL;
3857 if (*fileindex) {
3858 got_fileindex_free(*fileindex);
3859 *fileindex = NULL;
3861 lock_worktree(worktree, LOCK_SH);
3863 return err;
3866 const struct got_error *
3867 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3869 const struct got_error *err;
3870 char *tmp_branch_name = NULL;
3872 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3873 if (err)
3874 return err;
3876 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3877 free(tmp_branch_name);
3878 return NULL;
3881 static const struct got_error *
3882 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3883 char **logmsg, void *arg)
3885 *logmsg = arg;
3886 return NULL;
3889 static const struct got_error *
3890 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
3891 const char *path, struct got_object_id *blob_id,
3892 struct got_object_id *commit_id)
3894 return NULL;
3897 struct collect_merged_paths_arg {
3898 got_worktree_checkout_cb progress_cb;
3899 void *progress_arg;
3900 struct got_pathlist_head *merged_paths;
3903 static const struct got_error *
3904 collect_merged_paths(void *arg, unsigned char status, const char *path)
3906 const struct got_error *err;
3907 struct collect_merged_paths_arg *a = arg;
3908 char *p;
3909 struct got_pathlist_entry *new;
3911 err = (*a->progress_cb)(a->progress_arg, status, path);
3912 if (err)
3913 return err;
3915 if (status != GOT_STATUS_MERGE &&
3916 status != GOT_STATUS_ADD &&
3917 status != GOT_STATUS_DELETE &&
3918 status != GOT_STATUS_CONFLICT)
3919 return NULL;
3921 p = strdup(path);
3922 if (p == NULL)
3923 return got_error_from_errno("strdup");
3925 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3926 if (err || new == NULL)
3927 free(p);
3928 return err;
3931 void
3932 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3934 struct got_pathlist_entry *pe;
3936 TAILQ_FOREACH(pe, merged_paths, entry)
3937 free((char *)pe->path);
3939 got_pathlist_free(merged_paths);
3942 static const struct got_error *
3943 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3944 struct got_repository *repo)
3946 const struct got_error *err;
3947 struct got_reference *commit_ref = NULL;
3949 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3950 if (err) {
3951 if (err->code != GOT_ERR_NOT_REF)
3952 goto done;
3953 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3954 if (err)
3955 goto done;
3956 err = got_ref_write(commit_ref, repo);
3957 if (err)
3958 goto done;
3959 } else {
3960 struct got_object_id *stored_id;
3961 int cmp;
3963 err = got_ref_resolve(&stored_id, repo, commit_ref);
3964 if (err)
3965 goto done;
3966 cmp = got_object_id_cmp(commit_id, stored_id);
3967 free(stored_id);
3968 if (cmp != 0) {
3969 err = got_error(GOT_ERR_REBASE_COMMITID);
3970 goto done;
3973 done:
3974 if (commit_ref)
3975 got_ref_close(commit_ref);
3976 return err;
3979 static const struct got_error *
3980 rebase_merge_files(struct got_pathlist_head *merged_paths,
3981 const char *commit_ref_name, struct got_worktree *worktree,
3982 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3983 struct got_object_id *commit_id, struct got_repository *repo,
3984 got_worktree_checkout_cb progress_cb, void *progress_arg,
3985 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3987 const struct got_error *err;
3988 struct got_reference *commit_ref = NULL;
3989 struct collect_merged_paths_arg cmp_arg;
3990 char *fileindex_path;
3992 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3994 err = get_fileindex_path(&fileindex_path, worktree);
3995 if (err)
3996 return err;
3998 cmp_arg.progress_cb = progress_cb;
3999 cmp_arg.progress_arg = progress_arg;
4000 cmp_arg.merged_paths = merged_paths;
4001 err = merge_files(worktree, fileindex, fileindex_path,
4002 parent_commit_id, commit_id, repo, collect_merged_paths,
4003 &cmp_arg, cancel_cb, cancel_arg);
4004 if (commit_ref)
4005 got_ref_close(commit_ref);
4006 return err;
4009 const struct got_error *
4010 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4011 struct got_worktree *worktree, struct got_fileindex *fileindex,
4012 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4013 struct got_repository *repo,
4014 got_worktree_checkout_cb progress_cb, void *progress_arg,
4015 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4017 const struct got_error *err;
4018 char *commit_ref_name;
4020 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4021 if (err)
4022 return err;
4024 err = store_commit_id(commit_ref_name, commit_id, repo);
4025 if (err)
4026 goto done;
4028 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4029 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4030 progress_arg, cancel_cb, cancel_arg);
4031 done:
4032 free(commit_ref_name);
4033 return err;
4036 const struct got_error *
4037 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4038 struct got_worktree *worktree, struct got_fileindex *fileindex,
4039 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4040 struct got_repository *repo,
4041 got_worktree_checkout_cb progress_cb, void *progress_arg,
4042 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4044 const struct got_error *err;
4045 char *commit_ref_name;
4047 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4048 if (err)
4049 return err;
4051 err = store_commit_id(commit_ref_name, commit_id, repo);
4052 if (err)
4053 goto done;
4055 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4056 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4057 progress_arg, cancel_cb, cancel_arg);
4058 done:
4059 free(commit_ref_name);
4060 return err;
4063 static const struct got_error *
4064 rebase_commit(struct got_object_id **new_commit_id,
4065 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4066 struct got_worktree *worktree, struct got_fileindex *fileindex,
4067 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4068 const char *new_logmsg, struct got_repository *repo)
4070 const struct got_error *err, *sync_err;
4071 struct got_pathlist_head commitable_paths;
4072 struct collect_commitables_arg cc_arg;
4073 char *fileindex_path = NULL;
4074 struct got_reference *head_ref = NULL;
4075 struct got_object_id *head_commit_id = NULL;
4076 char *logmsg = NULL;
4078 TAILQ_INIT(&commitable_paths);
4079 *new_commit_id = NULL;
4081 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4083 err = get_fileindex_path(&fileindex_path, worktree);
4084 if (err)
4085 return err;
4087 cc_arg.commitable_paths = &commitable_paths;
4088 cc_arg.worktree = worktree;
4089 cc_arg.repo = repo;
4091 * If possible get the status of individual files directly to
4092 * avoid crawling the entire work tree once per rebased commit.
4093 * TODO: Ideally, merged_paths would contain a list of commitables
4094 * we could use so we could skip worktree_status() entirely.
4096 if (merged_paths) {
4097 struct got_pathlist_entry *pe;
4098 if (TAILQ_EMPTY(merged_paths)) {
4099 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4100 goto done;
4102 TAILQ_FOREACH(pe, merged_paths, entry) {
4103 err = worktree_status(worktree, pe->path, fileindex,
4104 repo, collect_commitables, &cc_arg, NULL, NULL);
4105 if (err)
4106 goto done;
4108 } else {
4109 err = worktree_status(worktree, "", fileindex, repo,
4110 collect_commitables, &cc_arg, NULL, NULL);
4111 if (err)
4112 goto done;
4115 if (TAILQ_EMPTY(&commitable_paths)) {
4116 /* No-op change; commit will be elided. */
4117 err = got_ref_delete(commit_ref, repo);
4118 if (err)
4119 goto done;
4120 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4121 goto done;
4124 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4125 if (err)
4126 goto done;
4128 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4129 if (err)
4130 goto done;
4132 if (new_logmsg)
4133 logmsg = strdup(new_logmsg);
4134 else
4135 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4136 if (logmsg == NULL)
4137 return got_error_from_errno("strdup");
4139 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4140 worktree, got_object_commit_get_author(orig_commit),
4141 got_object_commit_get_committer(orig_commit),
4142 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4143 if (err)
4144 goto done;
4146 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4147 if (err)
4148 goto done;
4150 err = got_ref_delete(commit_ref, repo);
4151 if (err)
4152 goto done;
4154 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4155 fileindex);
4156 sync_err = sync_fileindex(fileindex, fileindex_path);
4157 if (sync_err && err == NULL)
4158 err = sync_err;
4159 done:
4160 free(fileindex_path);
4161 free(head_commit_id);
4162 if (head_ref)
4163 got_ref_close(head_ref);
4164 if (err) {
4165 free(*new_commit_id);
4166 *new_commit_id = NULL;
4168 return err;
4171 const struct got_error *
4172 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4173 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4174 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4175 struct got_commit_object *orig_commit,
4176 struct got_object_id *orig_commit_id, struct got_repository *repo)
4178 const struct got_error *err;
4179 char *commit_ref_name;
4180 struct got_reference *commit_ref = NULL;
4181 struct got_object_id *commit_id = NULL;
4183 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4184 if (err)
4185 return err;
4187 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4188 if (err)
4189 goto done;
4190 err = got_ref_resolve(&commit_id, repo, commit_ref);
4191 if (err)
4192 goto done;
4193 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4194 err = got_error(GOT_ERR_REBASE_COMMITID);
4195 goto done;
4198 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4199 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4200 done:
4201 if (commit_ref)
4202 got_ref_close(commit_ref);
4203 free(commit_ref_name);
4204 free(commit_id);
4205 return err;
4208 const struct got_error *
4209 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4210 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4211 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4212 struct got_commit_object *orig_commit,
4213 struct got_object_id *orig_commit_id, const char *new_logmsg,
4214 struct got_repository *repo)
4216 const struct got_error *err;
4217 char *commit_ref_name;
4218 struct got_reference *commit_ref = NULL;
4219 struct got_object_id *commit_id = NULL;
4221 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4222 if (err)
4223 return err;
4225 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4226 if (err)
4227 goto done;
4228 err = got_ref_resolve(&commit_id, repo, commit_ref);
4229 if (err)
4230 goto done;
4231 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4232 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4233 goto done;
4236 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4237 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4238 done:
4239 if (commit_ref)
4240 got_ref_close(commit_ref);
4241 free(commit_ref_name);
4242 free(commit_id);
4243 return err;
4246 const struct got_error *
4247 got_worktree_rebase_postpone(struct got_worktree *worktree,
4248 struct got_fileindex *fileindex)
4250 if (fileindex)
4251 got_fileindex_free(fileindex);
4252 return lock_worktree(worktree, LOCK_SH);
4255 static const struct got_error *
4256 delete_ref(const char *name, struct got_repository *repo)
4258 const struct got_error *err;
4259 struct got_reference *ref;
4261 err = got_ref_open(&ref, repo, name, 0);
4262 if (err) {
4263 if (err->code == GOT_ERR_NOT_REF)
4264 return NULL;
4265 return err;
4268 err = got_ref_delete(ref, repo);
4269 got_ref_close(ref);
4270 return err;
4273 static const struct got_error *
4274 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4276 const struct got_error *err;
4277 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4278 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4280 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4281 if (err)
4282 goto done;
4283 err = delete_ref(tmp_branch_name, repo);
4284 if (err)
4285 goto done;
4287 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4288 if (err)
4289 goto done;
4290 err = delete_ref(new_base_branch_ref_name, repo);
4291 if (err)
4292 goto done;
4294 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4295 if (err)
4296 goto done;
4297 err = delete_ref(branch_ref_name, repo);
4298 if (err)
4299 goto done;
4301 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4302 if (err)
4303 goto done;
4304 err = delete_ref(commit_ref_name, repo);
4305 if (err)
4306 goto done;
4308 done:
4309 free(tmp_branch_name);
4310 free(new_base_branch_ref_name);
4311 free(branch_ref_name);
4312 free(commit_ref_name);
4313 return err;
4316 const struct got_error *
4317 got_worktree_rebase_complete(struct got_worktree *worktree,
4318 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4319 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4320 struct got_repository *repo)
4322 const struct got_error *err, *unlockerr;
4323 struct got_object_id *new_head_commit_id = NULL;
4325 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4326 if (err)
4327 return err;
4329 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4330 if (err)
4331 goto done;
4333 err = got_ref_write(rebased_branch, repo);
4334 if (err)
4335 goto done;
4337 err = got_worktree_set_head_ref(worktree, rebased_branch);
4338 if (err)
4339 goto done;
4341 err = delete_rebase_refs(worktree, repo);
4342 done:
4343 if (fileindex)
4344 got_fileindex_free(fileindex);
4345 free(new_head_commit_id);
4346 unlockerr = lock_worktree(worktree, LOCK_SH);
4347 if (unlockerr && err == NULL)
4348 err = unlockerr;
4349 return err;
4352 struct collect_revertible_paths_arg {
4353 struct got_pathlist_head *revertible_paths;
4354 struct got_worktree *worktree;
4357 static const struct got_error *
4358 collect_revertible_paths(void *arg, unsigned char status,
4359 unsigned char staged_status, const char *relpath,
4360 struct got_object_id *blob_id, struct got_object_id *commit_id)
4362 struct collect_revertible_paths_arg *a = arg;
4363 const struct got_error *err = NULL;
4364 struct got_pathlist_entry *new = NULL;
4365 char *path = NULL;
4367 if (status != GOT_STATUS_ADD &&
4368 status != GOT_STATUS_DELETE &&
4369 status != GOT_STATUS_MODIFY &&
4370 status != GOT_STATUS_CONFLICT &&
4371 status != GOT_STATUS_MISSING)
4372 return NULL;
4374 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4375 return got_error_from_errno("asprintf");
4377 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4378 if (err || new == NULL)
4379 free(path);
4380 return err;
4383 const struct got_error *
4384 got_worktree_rebase_abort(struct got_worktree *worktree,
4385 struct got_fileindex *fileindex, struct got_repository *repo,
4386 struct got_reference *new_base_branch,
4387 got_worktree_checkout_cb progress_cb, void *progress_arg)
4389 const struct got_error *err, *unlockerr, *sync_err;
4390 struct got_reference *resolved = NULL;
4391 struct got_object_id *commit_id = NULL;
4392 char *fileindex_path = NULL;
4393 struct got_pathlist_head revertible_paths;
4394 struct got_pathlist_entry *pe;
4395 struct collect_revertible_paths_arg crp_arg;
4396 struct got_object_id *tree_id = NULL;
4398 TAILQ_INIT(&revertible_paths);
4400 err = lock_worktree(worktree, LOCK_EX);
4401 if (err)
4402 return err;
4404 err = got_ref_open(&resolved, repo,
4405 got_ref_get_symref_target(new_base_branch), 0);
4406 if (err)
4407 goto done;
4409 err = got_worktree_set_head_ref(worktree, resolved);
4410 if (err)
4411 goto done;
4414 * XXX commits to the base branch could have happened while
4415 * we were busy rebasing; should we store the original commit ID
4416 * when rebase begins and read it back here?
4418 err = got_ref_resolve(&commit_id, repo, resolved);
4419 if (err)
4420 goto done;
4422 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4423 if (err)
4424 goto done;
4426 err = got_object_id_by_path(&tree_id, repo,
4427 worktree->base_commit_id, worktree->path_prefix);
4428 if (err)
4429 goto done;
4431 err = delete_rebase_refs(worktree, repo);
4432 if (err)
4433 goto done;
4435 err = get_fileindex_path(&fileindex_path, worktree);
4436 if (err)
4437 goto done;
4439 crp_arg.revertible_paths = &revertible_paths;
4440 crp_arg.worktree = worktree;
4441 err = worktree_status(worktree, "", fileindex, repo,
4442 collect_revertible_paths, &crp_arg, NULL, NULL);
4443 if (err)
4444 goto done;
4446 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4447 err = revert_file(worktree, fileindex, pe->path,
4448 progress_cb, progress_arg, repo);
4449 if (err)
4450 goto sync;
4453 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4454 repo, progress_cb, progress_arg, NULL, NULL);
4455 sync:
4456 sync_err = sync_fileindex(fileindex, fileindex_path);
4457 if (sync_err && err == NULL)
4458 err = sync_err;
4459 done:
4460 got_ref_close(resolved);
4461 free(tree_id);
4462 free(commit_id);
4463 if (fileindex)
4464 got_fileindex_free(fileindex);
4465 free(fileindex_path);
4466 TAILQ_FOREACH(pe, &revertible_paths, entry)
4467 free((char *)pe->path);
4468 got_pathlist_free(&revertible_paths);
4470 unlockerr = lock_worktree(worktree, LOCK_SH);
4471 if (unlockerr && err == NULL)
4472 err = unlockerr;
4473 return err;
4476 const struct got_error *
4477 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4478 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4479 struct got_fileindex **fileindex, struct got_worktree *worktree,
4480 struct got_repository *repo)
4482 const struct got_error *err = NULL;
4483 char *tmp_branch_name = NULL;
4484 char *branch_ref_name = NULL;
4485 char *base_commit_ref_name = NULL;
4486 char *fileindex_path = NULL;
4487 struct check_rebase_ok_arg ok_arg;
4488 struct got_reference *wt_branch = NULL;
4489 struct got_reference *base_commit_ref = NULL;
4491 *tmp_branch = NULL;
4492 *branch_ref = NULL;
4493 *base_commit_id = NULL;
4494 *fileindex = NULL;
4496 err = lock_worktree(worktree, LOCK_EX);
4497 if (err)
4498 return err;
4500 err = open_fileindex(fileindex, &fileindex_path, worktree);
4501 if (err)
4502 goto done;
4504 ok_arg.worktree = worktree;
4505 ok_arg.repo = repo;
4506 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4507 &ok_arg);
4508 if (err)
4509 goto done;
4511 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4512 if (err)
4513 goto done;
4515 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4516 if (err)
4517 goto done;
4519 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4520 worktree);
4521 if (err)
4522 goto done;
4524 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4525 0);
4526 if (err)
4527 goto done;
4529 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4530 if (err)
4531 goto done;
4533 err = got_ref_write(*branch_ref, repo);
4534 if (err)
4535 goto done;
4537 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4538 worktree->base_commit_id);
4539 if (err)
4540 goto done;
4541 err = got_ref_write(base_commit_ref, repo);
4542 if (err)
4543 goto done;
4544 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4545 if (*base_commit_id == NULL) {
4546 err = got_error_from_errno("got_object_id_dup");
4547 goto done;
4550 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4551 worktree->base_commit_id);
4552 if (err)
4553 goto done;
4554 err = got_ref_write(*tmp_branch, repo);
4555 if (err)
4556 goto done;
4558 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4559 if (err)
4560 goto done;
4561 done:
4562 free(fileindex_path);
4563 free(tmp_branch_name);
4564 free(branch_ref_name);
4565 free(base_commit_ref_name);
4566 if (wt_branch)
4567 got_ref_close(wt_branch);
4568 if (err) {
4569 if (*branch_ref) {
4570 got_ref_close(*branch_ref);
4571 *branch_ref = NULL;
4573 if (*tmp_branch) {
4574 got_ref_close(*tmp_branch);
4575 *tmp_branch = NULL;
4577 free(*base_commit_id);
4578 if (*fileindex) {
4579 got_fileindex_free(*fileindex);
4580 *fileindex = NULL;
4582 lock_worktree(worktree, LOCK_SH);
4584 return err;
4587 const struct got_error *
4588 got_worktree_histedit_postpone(struct got_worktree *worktree,
4589 struct got_fileindex *fileindex)
4591 if (fileindex)
4592 got_fileindex_free(fileindex);
4593 return lock_worktree(worktree, LOCK_SH);
4596 const struct got_error *
4597 got_worktree_histedit_in_progress(int *in_progress,
4598 struct got_worktree *worktree)
4600 const struct got_error *err;
4601 char *tmp_branch_name = NULL;
4603 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4604 if (err)
4605 return err;
4607 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4608 free(tmp_branch_name);
4609 return NULL;
4612 const struct got_error *
4613 got_worktree_histedit_continue(struct got_object_id **commit_id,
4614 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4615 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4616 struct got_worktree *worktree, struct got_repository *repo)
4618 const struct got_error *err;
4619 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4620 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4621 struct got_reference *commit_ref = NULL;
4622 struct got_reference *base_commit_ref = NULL;
4623 char *fileindex_path = NULL;
4625 *commit_id = NULL;
4626 *tmp_branch = NULL;
4627 *base_commit_id = NULL;
4628 *fileindex = NULL;
4630 err = lock_worktree(worktree, LOCK_EX);
4631 if (err)
4632 return err;
4634 err = open_fileindex(fileindex, &fileindex_path, worktree);
4635 if (err)
4636 goto done;
4638 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4639 if (err)
4640 return err;
4642 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4643 if (err)
4644 goto done;
4646 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4647 if (err)
4648 goto done;
4650 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4651 worktree);
4652 if (err)
4653 goto done;
4655 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4656 if (err)
4657 goto done;
4659 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4660 if (err)
4661 goto done;
4662 err = got_ref_resolve(commit_id, repo, commit_ref);
4663 if (err)
4664 goto done;
4666 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4667 if (err)
4668 goto done;
4669 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4670 if (err)
4671 goto done;
4673 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4674 if (err)
4675 goto done;
4676 done:
4677 free(commit_ref_name);
4678 free(branch_ref_name);
4679 free(fileindex_path);
4680 if (commit_ref)
4681 got_ref_close(commit_ref);
4682 if (base_commit_ref)
4683 got_ref_close(base_commit_ref);
4684 if (err) {
4685 free(*commit_id);
4686 *commit_id = NULL;
4687 free(*base_commit_id);
4688 *base_commit_id = NULL;
4689 if (*tmp_branch) {
4690 got_ref_close(*tmp_branch);
4691 *tmp_branch = NULL;
4693 if (*fileindex) {
4694 got_fileindex_free(*fileindex);
4695 *fileindex = NULL;
4697 lock_worktree(worktree, LOCK_EX);
4699 return err;
4702 static const struct got_error *
4703 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4705 const struct got_error *err;
4706 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4707 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4709 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4710 if (err)
4711 goto done;
4712 err = delete_ref(tmp_branch_name, repo);
4713 if (err)
4714 goto done;
4716 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4717 worktree);
4718 if (err)
4719 goto done;
4720 err = delete_ref(base_commit_ref_name, repo);
4721 if (err)
4722 goto done;
4724 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4725 if (err)
4726 goto done;
4727 err = delete_ref(branch_ref_name, repo);
4728 if (err)
4729 goto done;
4731 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4732 if (err)
4733 goto done;
4734 err = delete_ref(commit_ref_name, repo);
4735 if (err)
4736 goto done;
4737 done:
4738 free(tmp_branch_name);
4739 free(base_commit_ref_name);
4740 free(branch_ref_name);
4741 free(commit_ref_name);
4742 return err;
4745 const struct got_error *
4746 got_worktree_histedit_abort(struct got_worktree *worktree,
4747 struct got_fileindex *fileindex, struct got_repository *repo,
4748 struct got_reference *branch, struct got_object_id *base_commit_id,
4749 got_worktree_checkout_cb progress_cb, void *progress_arg)
4751 const struct got_error *err, *unlockerr, *sync_err;
4752 struct got_reference *resolved = NULL;
4753 char *fileindex_path = NULL;
4754 struct got_pathlist_head revertible_paths;
4755 struct got_pathlist_entry *pe;
4756 struct collect_revertible_paths_arg crp_arg;
4757 struct got_object_id *tree_id = NULL;
4759 TAILQ_INIT(&revertible_paths);
4761 err = lock_worktree(worktree, LOCK_EX);
4762 if (err)
4763 return err;
4765 err = got_ref_open(&resolved, repo,
4766 got_ref_get_symref_target(branch), 0);
4767 if (err)
4768 goto done;
4770 err = got_worktree_set_head_ref(worktree, resolved);
4771 if (err)
4772 goto done;
4774 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4775 if (err)
4776 goto done;
4778 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4779 worktree->path_prefix);
4780 if (err)
4781 goto done;
4783 err = delete_histedit_refs(worktree, repo);
4784 if (err)
4785 goto done;
4787 err = get_fileindex_path(&fileindex_path, worktree);
4788 if (err)
4789 goto done;
4791 crp_arg.revertible_paths = &revertible_paths;
4792 crp_arg.worktree = worktree;
4793 err = worktree_status(worktree, "", fileindex, repo,
4794 collect_revertible_paths, &crp_arg, NULL, NULL);
4795 if (err)
4796 goto done;
4798 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4799 err = revert_file(worktree, fileindex, pe->path,
4800 progress_cb, progress_arg, repo);
4801 if (err)
4802 goto sync;
4805 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4806 repo, progress_cb, progress_arg, NULL, NULL);
4807 sync:
4808 sync_err = sync_fileindex(fileindex, fileindex_path);
4809 if (sync_err && err == NULL)
4810 err = sync_err;
4811 done:
4812 got_ref_close(resolved);
4813 free(tree_id);
4814 free(fileindex_path);
4815 TAILQ_FOREACH(pe, &revertible_paths, entry)
4816 free((char *)pe->path);
4817 got_pathlist_free(&revertible_paths);
4819 unlockerr = lock_worktree(worktree, LOCK_SH);
4820 if (unlockerr && err == NULL)
4821 err = unlockerr;
4822 return err;
4825 const struct got_error *
4826 got_worktree_histedit_complete(struct got_worktree *worktree,
4827 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4828 struct got_reference *edited_branch, struct got_repository *repo)
4830 const struct got_error *err, *unlockerr;
4831 struct got_object_id *new_head_commit_id = NULL;
4832 struct got_reference *resolved = NULL;
4834 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4835 if (err)
4836 return err;
4838 err = got_ref_open(&resolved, repo,
4839 got_ref_get_symref_target(edited_branch), 0);
4840 if (err)
4841 goto done;
4843 err = got_ref_change_ref(resolved, new_head_commit_id);
4844 if (err)
4845 goto done;
4847 err = got_ref_write(resolved, repo);
4848 if (err)
4849 goto done;
4851 err = got_worktree_set_head_ref(worktree, resolved);
4852 if (err)
4853 goto done;
4855 err = delete_histedit_refs(worktree, repo);
4856 done:
4857 if (fileindex)
4858 got_fileindex_free(fileindex);
4859 free(new_head_commit_id);
4860 unlockerr = lock_worktree(worktree, LOCK_SH);
4861 if (unlockerr && err == NULL)
4862 err = unlockerr;
4863 return err;
4866 const struct got_error *
4867 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4868 struct got_object_id *commit_id, struct got_repository *repo)
4870 const struct got_error *err;
4871 char *commit_ref_name;
4873 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4874 if (err)
4875 return err;
4877 err = store_commit_id(commit_ref_name, commit_id, repo);
4878 if (err)
4879 goto done;
4881 err = delete_ref(commit_ref_name, repo);
4882 done:
4883 free(commit_ref_name);
4884 return err;
4887 static const struct got_error *
4888 stage_path(const char *relpath, const char *ondisk_path,
4889 const char *path_content, struct got_worktree *worktree,
4890 struct got_fileindex *fileindex, struct got_repository *repo,
4891 got_worktree_status_cb status_cb, void *status_arg)
4893 const struct got_error *err = NULL;
4894 struct got_fileindex_entry *ie;
4895 unsigned char status;
4896 struct stat sb;
4897 struct got_object_id *blob_id = NULL;
4898 uint32_t stage;
4900 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
4901 if (ie == NULL) {
4902 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4903 goto done;
4906 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4907 if (err)
4908 goto done;
4910 switch (status) {
4911 case GOT_STATUS_ADD:
4912 case GOT_STATUS_MODIFY:
4913 err = got_object_blob_create(&blob_id,
4914 path_content ? path_content : ondisk_path, repo);
4915 if (err)
4916 goto done;
4917 memcpy(ie->staged_blob_sha1, blob_id->sha1,
4918 SHA1_DIGEST_LENGTH);
4919 if (status == GOT_STATUS_ADD)
4920 stage = GOT_FILEIDX_STAGE_ADD;
4921 else
4922 stage = GOT_FILEIDX_STAGE_MODIFY;
4923 break;
4924 case GOT_STATUS_DELETE:
4925 stage = GOT_FILEIDX_STAGE_DELETE;
4926 break;
4927 default:
4928 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4929 goto done;
4932 got_fileindex_entry_stage_set(ie, stage);
4933 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4934 get_staged_status(ie), relpath, blob_id, NULL);
4935 done:
4936 free(blob_id);
4937 return err;
4940 const struct got_error *
4941 got_worktree_stage(struct got_worktree *worktree,
4942 struct got_pathlist_head *paths,
4943 got_worktree_status_cb status_cb, void *status_arg,
4944 struct got_repository *repo)
4946 const struct got_error *err = NULL, *sync_err, *unlockerr;
4947 struct got_pathlist_entry *pe;
4948 struct got_fileindex *fileindex = NULL;
4949 char *fileindex_path = NULL;
4951 err = lock_worktree(worktree, LOCK_EX);
4952 if (err)
4953 return err;
4955 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4956 if (err)
4957 goto done;
4959 TAILQ_FOREACH(pe, paths, entry) {
4960 char *relpath;
4961 err = got_path_skip_common_ancestor(&relpath,
4962 got_worktree_get_root_path(worktree), pe->path);
4963 if (err)
4964 break;
4965 err = stage_path(relpath, pe->path,
4966 (const char *)pe->data, worktree, fileindex, repo,
4967 status_cb, status_arg);
4968 free(relpath);
4969 if (err)
4970 break;
4973 sync_err = sync_fileindex(fileindex, fileindex_path);
4974 if (sync_err && err == NULL)
4975 err = sync_err;
4976 done:
4977 free(fileindex_path);
4978 if (fileindex)
4979 got_fileindex_free(fileindex);
4980 unlockerr = lock_worktree(worktree, LOCK_SH);
4981 if (unlockerr && err == NULL)
4982 err = unlockerr;
4983 return err;