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, staged_blob_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 if (staged_status == GOT_STATUS_ADD ||
2220 staged_status == GOT_STATUS_MODIFY) {
2221 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2222 SHA1_DIGEST_LENGTH);
2223 err = (*status_cb)(status_arg, status, staged_status,
2224 ie->path, &blob_id, &staged_blob_id, &commit_id);
2225 } else
2226 err = (*status_cb)(status_arg, status, staged_status,
2227 ie->path, &blob_id, NULL, &commit_id);
2229 return err;
2232 static const struct got_error *
2233 status_old_new(void *arg, struct got_fileindex_entry *ie,
2234 struct dirent *de, const char *parent_path)
2236 const struct got_error *err = NULL;
2237 struct diff_dir_cb_arg *a = arg;
2238 char *abspath;
2240 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2241 return got_error(GOT_ERR_CANCELLED);
2243 if (got_path_cmp(parent_path, a->status_path,
2244 strlen(parent_path), a->status_path_len) != 0 &&
2245 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2246 return NULL;
2248 if (parent_path[0]) {
2249 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2250 parent_path, de->d_name) == -1)
2251 return got_error_from_errno("asprintf");
2252 } else {
2253 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2254 de->d_name) == -1)
2255 return got_error_from_errno("asprintf");
2258 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2259 a->repo);
2260 free(abspath);
2261 return err;
2264 static const struct got_error *
2265 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2267 struct diff_dir_cb_arg *a = arg;
2268 struct got_object_id blob_id, commit_id;
2269 unsigned char status;
2271 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2272 return got_error(GOT_ERR_CANCELLED);
2274 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2275 return NULL;
2277 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2278 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2279 if (got_fileindex_entry_has_file_on_disk(ie))
2280 status = GOT_STATUS_MISSING;
2281 else
2282 status = GOT_STATUS_DELETE;
2283 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2284 ie->path, &blob_id, NULL, &commit_id);
2287 static const struct got_error *
2288 status_new(void *arg, struct dirent *de, const char *parent_path)
2290 const struct got_error *err = NULL;
2291 struct diff_dir_cb_arg *a = arg;
2292 char *path = NULL;
2294 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2295 return got_error(GOT_ERR_CANCELLED);
2297 if (de->d_type == DT_DIR)
2298 return NULL;
2300 /* XXX ignore symlinks for now */
2301 if (de->d_type == DT_LNK)
2302 return NULL;
2304 if (parent_path[0]) {
2305 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2306 return got_error_from_errno("asprintf");
2307 } else {
2308 path = de->d_name;
2311 if (got_path_is_child(path, a->status_path, a->status_path_len))
2312 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2313 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2314 if (parent_path[0])
2315 free(path);
2316 return err;
2319 static const struct got_error *
2320 report_single_file_status(const char *path, const char *ondisk_path,
2321 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2322 void *status_arg, struct got_repository *repo)
2324 struct got_fileindex_entry *ie;
2325 struct stat sb;
2327 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2328 if (ie)
2329 return report_file_status(ie, ondisk_path, status_cb,
2330 status_arg, repo);
2332 if (lstat(ondisk_path, &sb) == -1) {
2333 if (errno != ENOENT)
2334 return got_error_from_errno2("lstat", ondisk_path);
2335 return NULL;
2338 if (S_ISREG(sb.st_mode))
2339 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2340 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2342 return NULL;
2345 static const struct got_error *
2346 worktree_status(struct got_worktree *worktree, const char *path,
2347 struct got_fileindex *fileindex, struct got_repository *repo,
2348 got_worktree_status_cb status_cb, void *status_arg,
2349 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2351 const struct got_error *err = NULL;
2352 DIR *workdir = NULL;
2353 struct got_fileindex_diff_dir_cb fdiff_cb;
2354 struct diff_dir_cb_arg arg;
2355 char *ondisk_path = NULL;
2357 if (asprintf(&ondisk_path, "%s%s%s",
2358 worktree->root_path, path[0] ? "/" : "", path) == -1)
2359 return got_error_from_errno("asprintf");
2361 workdir = opendir(ondisk_path);
2362 if (workdir == NULL) {
2363 if (errno != ENOTDIR && errno != ENOENT)
2364 err = got_error_from_errno2("opendir", ondisk_path);
2365 else
2366 err = report_single_file_status(path, ondisk_path,
2367 fileindex, status_cb, status_arg, repo);
2368 } else {
2369 fdiff_cb.diff_old_new = status_old_new;
2370 fdiff_cb.diff_old = status_old;
2371 fdiff_cb.diff_new = status_new;
2372 arg.fileindex = fileindex;
2373 arg.worktree = worktree;
2374 arg.status_path = path;
2375 arg.status_path_len = strlen(path);
2376 arg.repo = repo;
2377 arg.status_cb = status_cb;
2378 arg.status_arg = status_arg;
2379 arg.cancel_cb = cancel_cb;
2380 arg.cancel_arg = cancel_arg;
2381 err = got_fileindex_diff_dir(fileindex, workdir,
2382 worktree->root_path, path, repo, &fdiff_cb, &arg);
2385 if (workdir)
2386 closedir(workdir);
2387 free(ondisk_path);
2388 return err;
2391 const struct got_error *
2392 got_worktree_status(struct got_worktree *worktree,
2393 struct got_pathlist_head *paths, struct got_repository *repo,
2394 got_worktree_status_cb status_cb, void *status_arg,
2395 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2397 const struct got_error *err = NULL;
2398 char *fileindex_path = NULL;
2399 struct got_fileindex *fileindex = NULL;
2400 struct got_pathlist_entry *pe;
2402 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2403 if (err)
2404 return err;
2406 TAILQ_FOREACH(pe, paths, entry) {
2407 err = worktree_status(worktree, pe->path, fileindex, repo,
2408 status_cb, status_arg, cancel_cb, cancel_arg);
2409 if (err)
2410 break;
2412 free(fileindex_path);
2413 got_fileindex_free(fileindex);
2414 return err;
2417 const struct got_error *
2418 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2419 const char *arg)
2421 const struct got_error *err = NULL;
2422 char *resolved, *cwd = NULL, *path = NULL;
2423 size_t len;
2425 *wt_path = NULL;
2427 resolved = realpath(arg, NULL);
2428 if (resolved == NULL) {
2429 if (errno != ENOENT)
2430 return got_error_from_errno2("realpath", arg);
2431 cwd = getcwd(NULL, 0);
2432 if (cwd == NULL)
2433 return got_error_from_errno("getcwd");
2434 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2435 err = got_error_from_errno("asprintf");
2436 goto done;
2440 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2441 strlen(got_worktree_get_root_path(worktree)))) {
2442 err = got_error(GOT_ERR_BAD_PATH);
2443 goto done;
2446 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2447 err = got_path_skip_common_ancestor(&path,
2448 got_worktree_get_root_path(worktree), resolved);
2449 if (err)
2450 goto done;
2451 } else {
2452 path = strdup("");
2453 if (path == NULL) {
2454 err = got_error_from_errno("strdup");
2455 goto done;
2459 /* XXX status walk can't deal with trailing slash! */
2460 len = strlen(path);
2461 while (len > 0 && path[len - 1] == '/') {
2462 path[len - 1] = '\0';
2463 len--;
2465 done:
2466 free(resolved);
2467 free(cwd);
2468 if (err == NULL)
2469 *wt_path = path;
2470 else
2471 free(path);
2472 return err;
2475 static const struct got_error *
2476 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2477 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2478 struct got_repository *repo)
2480 const struct got_error *err = NULL;
2481 struct got_fileindex_entry *ie;
2483 /* Re-adding an existing entry is a no-op. */
2484 if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2485 return NULL;
2487 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2488 if (err)
2489 return err;
2491 err = got_fileindex_entry_add(fileindex, ie);
2492 if (err) {
2493 got_fileindex_entry_free(ie);
2494 return err;
2497 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2500 const struct got_error *
2501 got_worktree_schedule_add(struct got_worktree *worktree,
2502 struct got_pathlist_head *ondisk_paths,
2503 got_worktree_status_cb status_cb, void *status_arg,
2504 struct got_repository *repo)
2506 struct got_fileindex *fileindex = NULL;
2507 char *fileindex_path = NULL;
2508 const struct got_error *err = NULL, *sync_err, *unlockerr;
2509 struct got_pathlist_entry *pe;
2511 err = lock_worktree(worktree, LOCK_EX);
2512 if (err)
2513 return err;
2515 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2516 if (err)
2517 goto done;
2519 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2520 char *relpath;
2521 err = got_path_skip_common_ancestor(&relpath,
2522 got_worktree_get_root_path(worktree), pe->path);
2523 if (err)
2524 break;
2525 err = schedule_addition(pe->path, fileindex, relpath,
2526 status_cb, status_arg, repo);
2527 free(relpath);
2528 if (err)
2529 break;
2531 sync_err = sync_fileindex(fileindex, fileindex_path);
2532 if (sync_err && err == NULL)
2533 err = sync_err;
2534 done:
2535 free(fileindex_path);
2536 if (fileindex)
2537 got_fileindex_free(fileindex);
2538 unlockerr = lock_worktree(worktree, LOCK_SH);
2539 if (unlockerr && err == NULL)
2540 err = unlockerr;
2541 return err;
2544 static const struct got_error *
2545 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2546 const char *relpath, int delete_local_mods,
2547 got_worktree_status_cb status_cb, void *status_arg,
2548 struct got_repository *repo)
2550 const struct got_error *err = NULL;
2551 struct got_fileindex_entry *ie = NULL;
2552 unsigned char status;
2553 struct stat sb;
2555 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2556 if (ie == NULL)
2557 return got_error(GOT_ERR_BAD_PATH);
2559 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2560 if (err)
2561 return err;
2563 if (status != GOT_STATUS_NO_CHANGE) {
2564 if (status == GOT_STATUS_DELETE)
2565 return NULL;
2566 if (status != GOT_STATUS_MODIFY)
2567 return got_error(GOT_ERR_FILE_STATUS);
2568 if (!delete_local_mods)
2569 return got_error(GOT_ERR_FILE_MODIFIED);
2572 if (unlink(ondisk_path) != 0)
2573 return got_error_from_errno2("unlink", ondisk_path);
2575 got_fileindex_entry_mark_deleted_from_disk(ie);
2576 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2579 const struct got_error *
2580 got_worktree_schedule_delete(struct got_worktree *worktree,
2581 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2582 got_worktree_status_cb status_cb, void *status_arg,
2583 struct got_repository *repo)
2585 struct got_fileindex *fileindex = NULL;
2586 char *fileindex_path = NULL;
2587 const struct got_error *err = NULL, *sync_err, *unlockerr;
2588 struct got_pathlist_entry *pe;
2590 err = lock_worktree(worktree, LOCK_EX);
2591 if (err)
2592 return err;
2594 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2595 if (err)
2596 goto done;
2598 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2599 char *relpath;
2600 err = got_path_skip_common_ancestor(&relpath,
2601 got_worktree_get_root_path(worktree), pe->path);
2602 if (err)
2603 break;
2604 err = schedule_for_deletion(pe->path, fileindex, relpath,
2605 delete_local_mods, status_cb, status_arg, repo);
2606 free(relpath);
2607 if (err)
2608 break;
2610 sync_err = sync_fileindex(fileindex, fileindex_path);
2611 if (sync_err && err == NULL)
2612 err = sync_err;
2613 done:
2614 free(fileindex_path);
2615 if (fileindex)
2616 got_fileindex_free(fileindex);
2617 unlockerr = lock_worktree(worktree, LOCK_SH);
2618 if (unlockerr && err == NULL)
2619 err = unlockerr;
2620 return err;
2623 static const struct got_error *
2624 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2625 const char *ondisk_path,
2626 got_worktree_checkout_cb progress_cb, void *progress_arg,
2627 struct got_repository *repo)
2629 const struct got_error *err = NULL;
2630 char *relpath = NULL, *parent_path = NULL;
2631 struct got_fileindex_entry *ie;
2632 struct got_tree_object *tree = NULL;
2633 struct got_object_id *tree_id = NULL;
2634 const struct got_tree_entry *te;
2635 char *tree_path = NULL, *te_name;
2636 struct got_blob_object *blob = NULL;
2637 unsigned char status;
2638 struct stat sb;
2640 err = got_path_skip_common_ancestor(&relpath,
2641 got_worktree_get_root_path(worktree), ondisk_path);
2642 if (err)
2643 goto done;
2645 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2646 if (ie == NULL) {
2647 err = got_error(GOT_ERR_BAD_PATH);
2648 goto done;
2651 /* Construct in-repository path of tree which contains this blob. */
2652 err = got_path_dirname(&parent_path, ie->path);
2653 if (err) {
2654 if (err->code != GOT_ERR_BAD_PATH)
2655 goto done;
2656 parent_path = strdup("/");
2657 if (parent_path == NULL) {
2658 err = got_error_from_errno("strdup");
2659 goto done;
2662 if (got_path_is_root_dir(worktree->path_prefix)) {
2663 tree_path = strdup(parent_path);
2664 if (tree_path == NULL) {
2665 err = got_error_from_errno("strdup");
2666 goto done;
2668 } else {
2669 if (got_path_is_root_dir(parent_path)) {
2670 tree_path = strdup(worktree->path_prefix);
2671 if (tree_path == NULL) {
2672 err = got_error_from_errno("strdup");
2673 goto done;
2675 } else {
2676 if (asprintf(&tree_path, "%s/%s",
2677 worktree->path_prefix, parent_path) == -1) {
2678 err = got_error_from_errno("asprintf");
2679 goto done;
2684 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2685 if (err)
2686 goto done;
2687 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2688 sb.st_mode = got_fileindex_perms_to_st(ie);
2690 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2691 tree_path);
2692 if (err) {
2693 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2694 status == GOT_STATUS_ADD))
2695 goto done;
2696 } else {
2697 err = got_object_open_as_tree(&tree, repo, tree_id);
2698 if (err)
2699 goto done;
2701 te_name = basename(ie->path);
2702 if (te_name == NULL) {
2703 err = got_error_from_errno2("basename", ie->path);
2704 goto done;
2707 te = got_object_tree_find_entry(tree, te_name);
2708 if (te == NULL && status != GOT_STATUS_ADD) {
2709 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2710 goto done;
2714 switch (status) {
2715 case GOT_STATUS_ADD:
2716 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2717 if (err)
2718 goto done;
2719 got_fileindex_entry_remove(fileindex, ie);
2720 break;
2721 case GOT_STATUS_DELETE:
2722 case GOT_STATUS_MODIFY:
2723 case GOT_STATUS_CONFLICT:
2724 case GOT_STATUS_MISSING: {
2725 struct got_object_id id;
2726 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2727 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2728 if (err)
2729 goto done;
2730 err = install_blob(worktree, ondisk_path, ie->path,
2731 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2732 progress_arg);
2733 if (err)
2734 goto done;
2735 if (status == GOT_STATUS_DELETE) {
2736 err = update_blob_fileindex_entry(worktree,
2737 fileindex, ie, ondisk_path, ie->path, blob, 1);
2738 if (err)
2739 goto done;
2741 break;
2743 default:
2744 goto done;
2746 done:
2747 free(relpath);
2748 free(parent_path);
2749 free(tree_path);
2750 if (blob)
2751 got_object_blob_close(blob);
2752 if (tree)
2753 got_object_tree_close(tree);
2754 free(tree_id);
2755 return err;
2758 const struct got_error *
2759 got_worktree_revert(struct got_worktree *worktree,
2760 struct got_pathlist_head *ondisk_paths,
2761 got_worktree_checkout_cb progress_cb, void *progress_arg,
2762 struct got_repository *repo)
2764 struct got_fileindex *fileindex = NULL;
2765 char *fileindex_path = NULL;
2766 const struct got_error *err = NULL, *unlockerr = NULL;
2767 const struct got_error *sync_err = NULL;
2768 struct got_pathlist_entry *pe;
2770 err = lock_worktree(worktree, LOCK_EX);
2771 if (err)
2772 return err;
2774 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2775 if (err)
2776 goto done;
2778 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2779 err = revert_file(worktree, fileindex, pe->path,
2780 progress_cb, progress_arg, repo);
2781 if (err)
2782 break;
2784 sync_err = sync_fileindex(fileindex, fileindex_path);
2785 if (sync_err && err == NULL)
2786 err = sync_err;
2787 done:
2788 free(fileindex_path);
2789 if (fileindex)
2790 got_fileindex_free(fileindex);
2791 unlockerr = lock_worktree(worktree, LOCK_SH);
2792 if (unlockerr && err == NULL)
2793 err = unlockerr;
2794 return err;
2797 static void
2798 free_commitable(struct got_commitable *ct)
2800 free(ct->path);
2801 free(ct->in_repo_path);
2802 free(ct->ondisk_path);
2803 free(ct->blob_id);
2804 free(ct->base_blob_id);
2805 free(ct->base_commit_id);
2806 free(ct);
2809 struct collect_commitables_arg {
2810 struct got_pathlist_head *commitable_paths;
2811 struct got_repository *repo;
2812 struct got_worktree *worktree;
2815 static const struct got_error *
2816 collect_commitables(void *arg, unsigned char status,
2817 unsigned char staged_status, const char *relpath,
2818 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2819 struct got_object_id *commit_id)
2821 struct collect_commitables_arg *a = arg;
2822 const struct got_error *err = NULL;
2823 struct got_commitable *ct = NULL;
2824 struct got_pathlist_entry *new = NULL;
2825 char *parent_path = NULL, *path = NULL;
2826 struct stat sb;
2828 if (status == GOT_STATUS_CONFLICT)
2829 return got_error(GOT_ERR_COMMIT_CONFLICT);
2831 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2832 status != GOT_STATUS_DELETE)
2833 return NULL;
2835 if (asprintf(&path, "/%s", relpath) == -1) {
2836 err = got_error_from_errno("asprintf");
2837 goto done;
2839 if (strcmp(path, "/") == 0) {
2840 parent_path = strdup("");
2841 if (parent_path == NULL)
2842 return got_error_from_errno("strdup");
2843 } else {
2844 err = got_path_dirname(&parent_path, path);
2845 if (err)
2846 return err;
2849 ct = calloc(1, sizeof(*ct));
2850 if (ct == NULL) {
2851 err = got_error_from_errno("calloc");
2852 goto done;
2855 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2856 relpath) == -1) {
2857 err = got_error_from_errno("asprintf");
2858 goto done;
2860 if (status == GOT_STATUS_DELETE) {
2861 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2862 } else {
2863 if (lstat(ct->ondisk_path, &sb) != 0) {
2864 err = got_error_from_errno2("lstat", ct->ondisk_path);
2865 goto done;
2867 ct->mode = sb.st_mode;
2870 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2871 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2872 relpath) == -1) {
2873 err = got_error_from_errno("asprintf");
2874 goto done;
2877 ct->status = status;
2878 ct->blob_id = NULL; /* will be filled in when blob gets created */
2879 if (ct->status != GOT_STATUS_ADD) {
2880 ct->base_blob_id = got_object_id_dup(blob_id);
2881 if (ct->base_blob_id == NULL) {
2882 err = got_error_from_errno("got_object_id_dup");
2883 goto done;
2885 ct->base_commit_id = got_object_id_dup(commit_id);
2886 if (ct->base_commit_id == NULL) {
2887 err = got_error_from_errno("got_object_id_dup");
2888 goto done;
2891 ct->path = strdup(path);
2892 if (ct->path == NULL) {
2893 err = got_error_from_errno("strdup");
2894 goto done;
2896 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2897 done:
2898 if (ct && (err || new == NULL))
2899 free_commitable(ct);
2900 free(parent_path);
2901 free(path);
2902 return err;
2905 static const struct got_error *write_tree(struct got_object_id **,
2906 struct got_tree_object *, const char *, struct got_pathlist_head *,
2907 got_worktree_status_cb status_cb, void *status_arg,
2908 struct got_repository *);
2910 static const struct got_error *
2911 write_subtree(struct got_object_id **new_subtree_id,
2912 struct got_tree_entry *te, const char *parent_path,
2913 struct got_pathlist_head *commitable_paths,
2914 got_worktree_status_cb status_cb, void *status_arg,
2915 struct got_repository *repo)
2917 const struct got_error *err = NULL;
2918 struct got_tree_object *subtree;
2919 char *subpath;
2921 if (asprintf(&subpath, "%s%s%s", parent_path,
2922 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2923 return got_error_from_errno("asprintf");
2925 err = got_object_open_as_tree(&subtree, repo, te->id);
2926 if (err)
2927 return err;
2929 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2930 status_cb, status_arg, repo);
2931 got_object_tree_close(subtree);
2932 free(subpath);
2933 return err;
2936 static const struct got_error *
2937 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2939 const struct got_error *err = NULL;
2940 char *ct_parent_path = NULL;
2942 *match = 0;
2944 if (strchr(ct->in_repo_path, '/') == NULL) {
2945 *match = got_path_is_root_dir(path);
2946 return NULL;
2949 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2950 if (err)
2951 return err;
2952 *match = (strcmp(path, ct_parent_path) == 0);
2953 free(ct_parent_path);
2954 return err;
2957 static mode_t
2958 get_ct_file_mode(struct got_commitable *ct)
2960 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2963 static const struct got_error *
2964 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2965 struct got_tree_entry *te, struct got_commitable *ct)
2967 const struct got_error *err = NULL;
2969 *new_te = NULL;
2971 err = got_object_tree_entry_dup(new_te, te);
2972 if (err)
2973 goto done;
2975 (*new_te)->mode = get_ct_file_mode(ct);
2977 free((*new_te)->id);
2978 (*new_te)->id = got_object_id_dup(ct->blob_id);
2979 if ((*new_te)->id == NULL) {
2980 err = got_error_from_errno("got_object_id_dup");
2981 goto done;
2983 done:
2984 if (err && *new_te) {
2985 got_object_tree_entry_close(*new_te);
2986 *new_te = NULL;
2988 return err;
2991 static const struct got_error *
2992 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2993 struct got_commitable *ct)
2995 const struct got_error *err = NULL;
2996 char *ct_name;
2998 *new_te = NULL;
3000 *new_te = calloc(1, sizeof(**new_te));
3001 if (*new_te == NULL)
3002 return got_error_from_errno("calloc");
3004 ct_name = basename(ct->path);
3005 if (ct_name == NULL) {
3006 err = got_error_from_errno2("basename", ct->path);
3007 goto done;
3009 (*new_te)->name = strdup(ct_name);
3010 if ((*new_te)->name == NULL) {
3011 err = got_error_from_errno("strdup");
3012 goto done;
3015 (*new_te)->mode = get_ct_file_mode(ct);
3017 (*new_te)->id = got_object_id_dup(ct->blob_id);
3018 if ((*new_te)->id == NULL) {
3019 err = got_error_from_errno("got_object_id_dup");
3020 goto done;
3022 done:
3023 if (err && *new_te) {
3024 got_object_tree_entry_close(*new_te);
3025 *new_te = NULL;
3027 return err;
3030 static const struct got_error *
3031 insert_tree_entry(struct got_tree_entry *new_te,
3032 struct got_pathlist_head *paths)
3034 const struct got_error *err = NULL;
3035 struct got_pathlist_entry *new_pe;
3037 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3038 if (err)
3039 return err;
3040 if (new_pe == NULL)
3041 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3042 return NULL;
3045 static const struct got_error *
3046 report_ct_status(struct got_commitable *ct,
3047 got_worktree_status_cb status_cb, void *status_arg)
3049 const char *ct_path = ct->path;
3050 while (ct_path[0] == '/')
3051 ct_path++;
3052 return (*status_cb)(status_arg, ct->status, GOT_STATUS_NO_CHANGE,
3053 ct_path, ct->blob_id, NULL, NULL);
3056 static const struct got_error *
3057 match_modified_subtree(int *modified, struct got_tree_entry *te,
3058 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3060 const struct got_error *err = NULL;
3061 struct got_pathlist_entry *pe;
3062 char *te_path;
3064 *modified = 0;
3066 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3067 got_path_is_root_dir(base_tree_path) ? "" : "/",
3068 te->name) == -1)
3069 return got_error_from_errno("asprintf");
3071 TAILQ_FOREACH(pe, commitable_paths, entry) {
3072 struct got_commitable *ct = pe->data;
3073 *modified = got_path_is_child(ct->in_repo_path, te_path,
3074 strlen(te_path));
3075 if (*modified)
3076 break;
3079 free(te_path);
3080 return err;
3083 static const struct got_error *
3084 match_deleted_or_modified_ct(struct got_commitable **ctp,
3085 struct got_tree_entry *te, const char *base_tree_path,
3086 struct got_pathlist_head *commitable_paths)
3088 const struct got_error *err = NULL;
3089 struct got_pathlist_entry *pe;
3091 *ctp = NULL;
3093 TAILQ_FOREACH(pe, commitable_paths, entry) {
3094 struct got_commitable *ct = pe->data;
3095 char *ct_name = NULL;
3096 int path_matches;
3098 if (ct->status != GOT_STATUS_MODIFY &&
3099 ct->status != GOT_STATUS_DELETE)
3100 continue;
3102 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3103 continue;
3105 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3106 if (err)
3107 return err;
3108 if (!path_matches)
3109 continue;
3111 ct_name = basename(pe->path);
3112 if (ct_name == NULL)
3113 return got_error_from_errno2("basename", pe->path);
3115 if (strcmp(te->name, ct_name) != 0)
3116 continue;
3118 *ctp = ct;
3119 break;
3122 return err;
3125 static const struct got_error *
3126 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3127 const char *child_path, const char *path_base_tree,
3128 struct got_pathlist_head *commitable_paths,
3129 got_worktree_status_cb status_cb, void *status_arg,
3130 struct got_repository *repo)
3132 const struct got_error *err = NULL;
3133 struct got_tree_entry *new_te;
3134 char *subtree_path;
3136 *new_tep = NULL;
3138 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3139 got_path_is_root_dir(path_base_tree) ? "" : "/",
3140 child_path) == -1)
3141 return got_error_from_errno("asprintf");
3143 new_te = calloc(1, sizeof(*new_te));
3144 new_te->mode = S_IFDIR;
3145 new_te->name = strdup(child_path);
3146 if (new_te->name == NULL) {
3147 err = got_error_from_errno("strdup");
3148 got_object_tree_entry_close(new_te);
3149 goto done;
3151 err = write_tree(&new_te->id, NULL, subtree_path,
3152 commitable_paths, status_cb, status_arg, repo);
3153 if (err) {
3154 got_object_tree_entry_close(new_te);
3155 goto done;
3157 done:
3158 free(subtree_path);
3159 if (err == NULL)
3160 *new_tep = new_te;
3161 return err;
3164 static const struct got_error *
3165 write_tree(struct got_object_id **new_tree_id,
3166 struct got_tree_object *base_tree, const char *path_base_tree,
3167 struct got_pathlist_head *commitable_paths,
3168 got_worktree_status_cb status_cb, void *status_arg,
3169 struct got_repository *repo)
3171 const struct got_error *err = NULL;
3172 const struct got_tree_entries *base_entries = NULL;
3173 struct got_pathlist_head paths;
3174 struct got_tree_entries new_tree_entries;
3175 struct got_tree_entry *te, *new_te = NULL;
3176 struct got_pathlist_entry *pe;
3178 TAILQ_INIT(&paths);
3179 new_tree_entries.nentries = 0;
3180 SIMPLEQ_INIT(&new_tree_entries.head);
3182 /* Insert, and recurse into, newly added entries first. */
3183 TAILQ_FOREACH(pe, commitable_paths, entry) {
3184 struct got_commitable *ct = pe->data;
3185 char *child_path = NULL, *slash;
3187 if (ct->status != GOT_STATUS_ADD ||
3188 (ct->flags & GOT_COMMITABLE_ADDED))
3189 continue;
3191 if (!got_path_is_child(pe->path, path_base_tree,
3192 strlen(path_base_tree)))
3193 continue;
3195 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3196 pe->path);
3197 if (err)
3198 goto done;
3200 slash = strchr(child_path, '/');
3201 if (slash == NULL) {
3202 err = alloc_added_blob_tree_entry(&new_te, ct);
3203 if (err)
3204 goto done;
3205 err = report_ct_status(ct, status_cb, status_arg);
3206 if (err)
3207 goto done;
3208 ct->flags |= GOT_COMMITABLE_ADDED;
3209 err = insert_tree_entry(new_te, &paths);
3210 if (err)
3211 goto done;
3212 } else {
3213 *slash = '\0'; /* trim trailing path components */
3214 if (base_tree == NULL ||
3215 got_object_tree_find_entry(base_tree, child_path)
3216 == NULL) {
3217 err = make_subtree_for_added_blob(&new_te,
3218 child_path, path_base_tree,
3219 commitable_paths, status_cb, status_arg,
3220 repo);
3221 if (err)
3222 goto done;
3223 err = insert_tree_entry(new_te, &paths);
3224 if (err)
3225 goto done;
3230 if (base_tree) {
3231 /* Handle modified and deleted entries. */
3232 base_entries = got_object_tree_get_entries(base_tree);
3233 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3234 struct got_commitable *ct = NULL;
3236 if (S_ISDIR(te->mode)) {
3237 int modified;
3238 err = got_object_tree_entry_dup(&new_te, te);
3239 if (err)
3240 goto done;
3241 err = match_modified_subtree(&modified, te,
3242 path_base_tree, commitable_paths);
3243 if (err)
3244 goto done;
3245 /* Avoid recursion into unmodified subtrees. */
3246 if (modified) {
3247 free(new_te->id);
3248 err = write_subtree(&new_te->id, te,
3249 path_base_tree, commitable_paths,
3250 status_cb, status_arg, repo);
3251 if (err)
3252 goto done;
3254 err = insert_tree_entry(new_te, &paths);
3255 if (err)
3256 goto done;
3257 continue;
3260 err = match_deleted_or_modified_ct(&ct, te,
3261 path_base_tree, commitable_paths);
3262 if (ct) {
3263 /* NB: Deleted entries get dropped here. */
3264 if (ct->status == GOT_STATUS_MODIFY) {
3265 err = alloc_modified_blob_tree_entry(
3266 &new_te, te, ct);
3267 if (err)
3268 goto done;
3269 err = insert_tree_entry(new_te, &paths);
3270 if (err)
3271 goto done;
3273 err = report_ct_status(ct, status_cb,
3274 status_arg);
3275 if (err)
3276 goto done;
3277 } else {
3278 /* Entry is unchanged; just copy it. */
3279 err = got_object_tree_entry_dup(&new_te, te);
3280 if (err)
3281 goto done;
3282 err = insert_tree_entry(new_te, &paths);
3283 if (err)
3284 goto done;
3289 /* Write new list of entries; deleted entries have been dropped. */
3290 TAILQ_FOREACH(pe, &paths, entry) {
3291 struct got_tree_entry *te = pe->data;
3292 new_tree_entries.nentries++;
3293 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3295 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3296 done:
3297 got_object_tree_entries_close(&new_tree_entries);
3298 got_pathlist_free(&paths);
3299 return err;
3302 static const struct got_error *
3303 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3304 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3306 const struct got_error *err = NULL;
3307 struct got_pathlist_entry *pe;
3309 TAILQ_FOREACH(pe, commitable_paths, entry) {
3310 struct got_fileindex_entry *ie;
3311 struct got_commitable *ct = pe->data;
3313 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3314 if (ie) {
3315 if (ct->status == GOT_STATUS_DELETE) {
3316 got_fileindex_entry_remove(fileindex, ie);
3317 got_fileindex_entry_free(ie);
3318 } else
3319 err = got_fileindex_entry_update(ie,
3320 ct->ondisk_path, ct->blob_id->sha1,
3321 new_base_commit_id->sha1, 1);
3322 } else {
3323 err = got_fileindex_entry_alloc(&ie,
3324 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3325 new_base_commit_id->sha1);
3326 if (err)
3327 break;
3328 err = got_fileindex_entry_add(fileindex, ie);
3329 if (err)
3330 break;
3333 return err;
3336 static const struct got_error *
3337 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3338 struct got_object_id *head_commit_id)
3340 const struct got_error *err = NULL;
3341 struct got_object_id *id = NULL;
3342 struct got_commit_object *commit = NULL;
3343 const char *ct_path = ct->in_repo_path;
3345 while (ct_path[0] == '/')
3346 ct_path++;
3348 if (ct->status != GOT_STATUS_ADD) {
3349 /* Trivial case: base commit == head commit */
3350 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3351 return NULL;
3353 * Ensure file content which local changes were based
3354 * on matches file content in the branch head.
3356 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3357 if (err) {
3358 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3359 goto done;
3360 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3361 goto done;
3362 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3363 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3364 } else {
3365 /* Require that added files don't exist in the branch head. */
3366 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3367 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3368 goto done;
3369 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3371 done:
3372 if (commit)
3373 got_object_commit_close(commit);
3374 free(id);
3375 return err;
3378 const struct got_error *
3379 commit_worktree(struct got_object_id **new_commit_id,
3380 struct got_pathlist_head *commitable_paths,
3381 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3382 const char *author, const char *committer,
3383 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3384 got_worktree_status_cb status_cb, void *status_arg,
3385 struct got_repository *repo)
3387 const struct got_error *err = NULL, *unlockerr = NULL;
3388 struct got_pathlist_entry *pe;
3389 const char *head_ref_name = NULL;
3390 struct got_commit_object *head_commit = NULL;
3391 struct got_reference *head_ref2 = NULL;
3392 struct got_object_id *head_commit_id2 = NULL;
3393 struct got_tree_object *head_tree = NULL;
3394 struct got_object_id *new_tree_id = NULL;
3395 struct got_object_id_queue parent_ids;
3396 struct got_object_qid *pid = NULL;
3397 char *logmsg = NULL;
3399 *new_commit_id = NULL;
3401 SIMPLEQ_INIT(&parent_ids);
3403 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3404 if (err)
3405 goto done;
3407 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3408 if (err)
3409 goto done;
3411 if (commit_msg_cb != NULL) {
3412 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3413 if (err)
3414 goto done;
3417 if (logmsg == NULL || strlen(logmsg) == 0) {
3418 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3419 goto done;
3422 /* Create blobs from added and modified files and record their IDs. */
3423 TAILQ_FOREACH(pe, commitable_paths, entry) {
3424 struct got_commitable *ct = pe->data;
3425 char *ondisk_path;
3427 if (ct->status != GOT_STATUS_ADD &&
3428 ct->status != GOT_STATUS_MODIFY)
3429 continue;
3431 if (asprintf(&ondisk_path, "%s/%s",
3432 worktree->root_path, pe->path) == -1) {
3433 err = got_error_from_errno("asprintf");
3434 goto done;
3436 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3437 free(ondisk_path);
3438 if (err)
3439 goto done;
3442 /* Recursively write new tree objects. */
3443 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3444 status_cb, status_arg, repo);
3445 if (err)
3446 goto done;
3448 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3449 if (err)
3450 goto done;
3451 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3452 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3453 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3454 got_object_qid_free(pid);
3455 if (logmsg != NULL)
3456 free(logmsg);
3457 if (err)
3458 goto done;
3460 /* Check if a concurrent commit to our branch has occurred. */
3461 head_ref_name = got_worktree_get_head_ref_name(worktree);
3462 if (head_ref_name == NULL) {
3463 err = got_error_from_errno("got_worktree_get_head_ref_name");
3464 goto done;
3466 /* Lock the reference here to prevent concurrent modification. */
3467 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3468 if (err)
3469 goto done;
3470 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3471 if (err)
3472 goto done;
3473 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3474 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3475 goto done;
3477 /* Update branch head in repository. */
3478 err = got_ref_change_ref(head_ref2, *new_commit_id);
3479 if (err)
3480 goto done;
3481 err = got_ref_write(head_ref2, repo);
3482 if (err)
3483 goto done;
3485 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3486 if (err)
3487 goto done;
3489 err = ref_base_commit(worktree, repo);
3490 if (err)
3491 goto done;
3492 done:
3493 if (head_tree)
3494 got_object_tree_close(head_tree);
3495 if (head_commit)
3496 got_object_commit_close(head_commit);
3497 free(head_commit_id2);
3498 if (head_ref2) {
3499 unlockerr = got_ref_unlock(head_ref2);
3500 if (unlockerr && err == NULL)
3501 err = unlockerr;
3502 got_ref_close(head_ref2);
3504 return err;
3507 static const struct got_error *
3508 check_path_is_commitable(const char *path,
3509 struct got_pathlist_head *commitable_paths)
3511 struct got_pathlist_entry *cpe = NULL;
3512 size_t path_len = strlen(path);
3514 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3515 struct got_commitable *ct = cpe->data;
3516 const char *ct_path = ct->path;
3518 while (ct_path[0] == '/')
3519 ct_path++;
3521 if (strcmp(path, ct_path) == 0 ||
3522 got_path_is_child(ct_path, path, path_len))
3523 break;
3526 if (cpe == NULL)
3527 return got_error_path(path, GOT_ERR_BAD_PATH);
3529 return NULL;
3532 const struct got_error *
3533 got_worktree_commit(struct got_object_id **new_commit_id,
3534 struct got_worktree *worktree, struct got_pathlist_head *paths,
3535 const char *author, const char *committer,
3536 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3537 got_worktree_status_cb status_cb, void *status_arg,
3538 struct got_repository *repo)
3540 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3541 struct got_fileindex *fileindex = NULL;
3542 char *fileindex_path = NULL;
3543 struct got_pathlist_head commitable_paths;
3544 struct collect_commitables_arg cc_arg;
3545 struct got_pathlist_entry *pe;
3546 struct got_reference *head_ref = NULL;
3547 struct got_object_id *head_commit_id = NULL;
3549 *new_commit_id = NULL;
3551 TAILQ_INIT(&commitable_paths);
3553 err = lock_worktree(worktree, LOCK_EX);
3554 if (err)
3555 goto done;
3557 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3558 if (err)
3559 goto done;
3561 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3562 if (err)
3563 goto done;
3565 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3566 if (err)
3567 goto done;
3569 cc_arg.commitable_paths = &commitable_paths;
3570 cc_arg.worktree = worktree;
3571 cc_arg.repo = repo;
3572 TAILQ_FOREACH(pe, paths, entry) {
3573 err = worktree_status(worktree, pe->path, fileindex, repo,
3574 collect_commitables, &cc_arg, NULL, NULL);
3575 if (err)
3576 goto done;
3579 if (TAILQ_EMPTY(&commitable_paths)) {
3580 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3581 goto done;
3584 TAILQ_FOREACH(pe, paths, entry) {
3585 err = check_path_is_commitable(pe->path, &commitable_paths);
3586 if (err)
3587 goto done;
3590 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3591 struct got_commitable *ct = pe->data;
3592 err = check_ct_out_of_date(ct, repo, head_commit_id);
3593 if (err)
3594 goto done;
3597 err = commit_worktree(new_commit_id, &commitable_paths,
3598 head_commit_id, worktree, author, committer,
3599 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3600 if (err)
3601 goto done;
3603 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3604 fileindex);
3605 sync_err = sync_fileindex(fileindex, fileindex_path);
3606 if (sync_err && err == NULL)
3607 err = sync_err;
3608 done:
3609 if (fileindex)
3610 got_fileindex_free(fileindex);
3611 free(fileindex_path);
3612 unlockerr = lock_worktree(worktree, LOCK_SH);
3613 if (unlockerr && err == NULL)
3614 err = unlockerr;
3615 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3616 struct got_commitable *ct = pe->data;
3617 free_commitable(ct);
3619 got_pathlist_free(&commitable_paths);
3620 return err;
3623 const char *
3624 got_commitable_get_path(struct got_commitable *ct)
3626 return ct->path;
3629 unsigned int
3630 got_commitable_get_status(struct got_commitable *ct)
3632 return ct->status;
3635 struct check_rebase_ok_arg {
3636 struct got_worktree *worktree;
3637 struct got_repository *repo;
3640 static const struct got_error *
3641 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3643 const struct got_error *err = NULL;
3644 struct check_rebase_ok_arg *a = arg;
3645 unsigned char status;
3646 struct stat sb;
3647 char *ondisk_path;
3649 /* Reject rebase of a work tree with mixed base commits. */
3650 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3651 SHA1_DIGEST_LENGTH))
3652 return got_error(GOT_ERR_MIXED_COMMITS);
3654 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3655 == -1)
3656 return got_error_from_errno("asprintf");
3658 /* Reject rebase of a work tree with modified or conflicted files. */
3659 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3660 free(ondisk_path);
3661 if (err)
3662 return err;
3664 if (status != GOT_STATUS_NO_CHANGE)
3665 return got_error(GOT_ERR_MODIFIED);
3667 return NULL;
3670 const struct got_error *
3671 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3672 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3673 struct got_worktree *worktree, struct got_reference *branch,
3674 struct got_repository *repo)
3676 const struct got_error *err = NULL;
3677 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3678 char *branch_ref_name = NULL;
3679 char *fileindex_path = NULL;
3680 struct check_rebase_ok_arg ok_arg;
3681 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3683 *new_base_branch_ref = NULL;
3684 *tmp_branch = NULL;
3685 *fileindex = NULL;
3687 err = lock_worktree(worktree, LOCK_EX);
3688 if (err)
3689 return err;
3691 err = open_fileindex(fileindex, &fileindex_path, worktree);
3692 if (err)
3693 goto done;
3695 ok_arg.worktree = worktree;
3696 ok_arg.repo = repo;
3697 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3698 &ok_arg);
3699 if (err)
3700 goto done;
3702 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3703 if (err)
3704 goto done;
3706 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3707 if (err)
3708 goto done;
3710 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3711 if (err)
3712 goto done;
3714 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3715 0);
3716 if (err)
3717 goto done;
3719 err = got_ref_alloc_symref(new_base_branch_ref,
3720 new_base_branch_ref_name, wt_branch);
3721 if (err)
3722 goto done;
3723 err = got_ref_write(*new_base_branch_ref, repo);
3724 if (err)
3725 goto done;
3727 /* TODO Lock original branch's ref while rebasing? */
3729 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3730 if (err)
3731 goto done;
3733 err = got_ref_write(branch_ref, repo);
3734 if (err)
3735 goto done;
3737 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3738 worktree->base_commit_id);
3739 if (err)
3740 goto done;
3741 err = got_ref_write(*tmp_branch, repo);
3742 if (err)
3743 goto done;
3745 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3746 if (err)
3747 goto done;
3748 done:
3749 free(fileindex_path);
3750 free(tmp_branch_name);
3751 free(new_base_branch_ref_name);
3752 free(branch_ref_name);
3753 if (branch_ref)
3754 got_ref_close(branch_ref);
3755 if (wt_branch)
3756 got_ref_close(wt_branch);
3757 if (err) {
3758 if (*new_base_branch_ref) {
3759 got_ref_close(*new_base_branch_ref);
3760 *new_base_branch_ref = NULL;
3762 if (*tmp_branch) {
3763 got_ref_close(*tmp_branch);
3764 *tmp_branch = NULL;
3766 if (*fileindex) {
3767 got_fileindex_free(*fileindex);
3768 *fileindex = NULL;
3770 lock_worktree(worktree, LOCK_SH);
3772 return err;
3775 const struct got_error *
3776 got_worktree_rebase_continue(struct got_object_id **commit_id,
3777 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3778 struct got_reference **branch, struct got_fileindex **fileindex,
3779 struct got_worktree *worktree, struct got_repository *repo)
3781 const struct got_error *err;
3782 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3783 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3784 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3785 char *fileindex_path = NULL;
3787 *commit_id = NULL;
3788 *new_base_branch = NULL;
3789 *tmp_branch = NULL;
3790 *branch = NULL;
3791 *fileindex = NULL;
3793 err = lock_worktree(worktree, LOCK_EX);
3794 if (err)
3795 return err;
3797 err = open_fileindex(fileindex, &fileindex_path, worktree);
3798 if (err)
3799 goto done;
3801 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3802 if (err)
3803 return err;
3805 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3806 if (err)
3807 goto done;
3809 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3810 if (err)
3811 goto done;
3813 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3814 if (err)
3815 goto done;
3817 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3818 if (err)
3819 goto done;
3821 err = got_ref_open(branch, repo,
3822 got_ref_get_symref_target(branch_ref), 0);
3823 if (err)
3824 goto done;
3826 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3827 if (err)
3828 goto done;
3830 err = got_ref_resolve(commit_id, repo, commit_ref);
3831 if (err)
3832 goto done;
3834 err = got_ref_open(new_base_branch, repo,
3835 new_base_branch_ref_name, 0);
3836 if (err)
3837 goto done;
3839 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3840 if (err)
3841 goto done;
3842 done:
3843 free(commit_ref_name);
3844 free(branch_ref_name);
3845 free(fileindex_path);
3846 if (commit_ref)
3847 got_ref_close(commit_ref);
3848 if (branch_ref)
3849 got_ref_close(branch_ref);
3850 if (err) {
3851 free(*commit_id);
3852 *commit_id = NULL;
3853 if (*tmp_branch) {
3854 got_ref_close(*tmp_branch);
3855 *tmp_branch = NULL;
3857 if (*new_base_branch) {
3858 got_ref_close(*new_base_branch);
3859 *new_base_branch = NULL;
3861 if (*branch) {
3862 got_ref_close(*branch);
3863 *branch = NULL;
3865 if (*fileindex) {
3866 got_fileindex_free(*fileindex);
3867 *fileindex = NULL;
3869 lock_worktree(worktree, LOCK_SH);
3871 return err;
3874 const struct got_error *
3875 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3877 const struct got_error *err;
3878 char *tmp_branch_name = NULL;
3880 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3881 if (err)
3882 return err;
3884 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3885 free(tmp_branch_name);
3886 return NULL;
3889 static const struct got_error *
3890 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3891 char **logmsg, void *arg)
3893 *logmsg = arg;
3894 return NULL;
3897 static const struct got_error *
3898 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
3899 const char *path, struct got_object_id *blob_id,
3900 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3902 return NULL;
3905 struct collect_merged_paths_arg {
3906 got_worktree_checkout_cb progress_cb;
3907 void *progress_arg;
3908 struct got_pathlist_head *merged_paths;
3911 static const struct got_error *
3912 collect_merged_paths(void *arg, unsigned char status, const char *path)
3914 const struct got_error *err;
3915 struct collect_merged_paths_arg *a = arg;
3916 char *p;
3917 struct got_pathlist_entry *new;
3919 err = (*a->progress_cb)(a->progress_arg, status, path);
3920 if (err)
3921 return err;
3923 if (status != GOT_STATUS_MERGE &&
3924 status != GOT_STATUS_ADD &&
3925 status != GOT_STATUS_DELETE &&
3926 status != GOT_STATUS_CONFLICT)
3927 return NULL;
3929 p = strdup(path);
3930 if (p == NULL)
3931 return got_error_from_errno("strdup");
3933 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3934 if (err || new == NULL)
3935 free(p);
3936 return err;
3939 void
3940 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3942 struct got_pathlist_entry *pe;
3944 TAILQ_FOREACH(pe, merged_paths, entry)
3945 free((char *)pe->path);
3947 got_pathlist_free(merged_paths);
3950 static const struct got_error *
3951 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3952 struct got_repository *repo)
3954 const struct got_error *err;
3955 struct got_reference *commit_ref = NULL;
3957 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3958 if (err) {
3959 if (err->code != GOT_ERR_NOT_REF)
3960 goto done;
3961 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3962 if (err)
3963 goto done;
3964 err = got_ref_write(commit_ref, repo);
3965 if (err)
3966 goto done;
3967 } else {
3968 struct got_object_id *stored_id;
3969 int cmp;
3971 err = got_ref_resolve(&stored_id, repo, commit_ref);
3972 if (err)
3973 goto done;
3974 cmp = got_object_id_cmp(commit_id, stored_id);
3975 free(stored_id);
3976 if (cmp != 0) {
3977 err = got_error(GOT_ERR_REBASE_COMMITID);
3978 goto done;
3981 done:
3982 if (commit_ref)
3983 got_ref_close(commit_ref);
3984 return err;
3987 static const struct got_error *
3988 rebase_merge_files(struct got_pathlist_head *merged_paths,
3989 const char *commit_ref_name, struct got_worktree *worktree,
3990 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3991 struct got_object_id *commit_id, struct got_repository *repo,
3992 got_worktree_checkout_cb progress_cb, void *progress_arg,
3993 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3995 const struct got_error *err;
3996 struct got_reference *commit_ref = NULL;
3997 struct collect_merged_paths_arg cmp_arg;
3998 char *fileindex_path;
4000 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4002 err = get_fileindex_path(&fileindex_path, worktree);
4003 if (err)
4004 return err;
4006 cmp_arg.progress_cb = progress_cb;
4007 cmp_arg.progress_arg = progress_arg;
4008 cmp_arg.merged_paths = merged_paths;
4009 err = merge_files(worktree, fileindex, fileindex_path,
4010 parent_commit_id, commit_id, repo, collect_merged_paths,
4011 &cmp_arg, cancel_cb, cancel_arg);
4012 if (commit_ref)
4013 got_ref_close(commit_ref);
4014 return err;
4017 const struct got_error *
4018 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4019 struct got_worktree *worktree, struct got_fileindex *fileindex,
4020 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4021 struct got_repository *repo,
4022 got_worktree_checkout_cb progress_cb, void *progress_arg,
4023 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4025 const struct got_error *err;
4026 char *commit_ref_name;
4028 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4029 if (err)
4030 return err;
4032 err = store_commit_id(commit_ref_name, commit_id, repo);
4033 if (err)
4034 goto done;
4036 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4037 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4038 progress_arg, cancel_cb, cancel_arg);
4039 done:
4040 free(commit_ref_name);
4041 return err;
4044 const struct got_error *
4045 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4046 struct got_worktree *worktree, struct got_fileindex *fileindex,
4047 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4048 struct got_repository *repo,
4049 got_worktree_checkout_cb progress_cb, void *progress_arg,
4050 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4052 const struct got_error *err;
4053 char *commit_ref_name;
4055 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4056 if (err)
4057 return err;
4059 err = store_commit_id(commit_ref_name, commit_id, repo);
4060 if (err)
4061 goto done;
4063 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4064 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4065 progress_arg, cancel_cb, cancel_arg);
4066 done:
4067 free(commit_ref_name);
4068 return err;
4071 static const struct got_error *
4072 rebase_commit(struct got_object_id **new_commit_id,
4073 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4074 struct got_worktree *worktree, struct got_fileindex *fileindex,
4075 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4076 const char *new_logmsg, struct got_repository *repo)
4078 const struct got_error *err, *sync_err;
4079 struct got_pathlist_head commitable_paths;
4080 struct collect_commitables_arg cc_arg;
4081 char *fileindex_path = NULL;
4082 struct got_reference *head_ref = NULL;
4083 struct got_object_id *head_commit_id = NULL;
4084 char *logmsg = NULL;
4086 TAILQ_INIT(&commitable_paths);
4087 *new_commit_id = NULL;
4089 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4091 err = get_fileindex_path(&fileindex_path, worktree);
4092 if (err)
4093 return err;
4095 cc_arg.commitable_paths = &commitable_paths;
4096 cc_arg.worktree = worktree;
4097 cc_arg.repo = repo;
4099 * If possible get the status of individual files directly to
4100 * avoid crawling the entire work tree once per rebased commit.
4101 * TODO: Ideally, merged_paths would contain a list of commitables
4102 * we could use so we could skip worktree_status() entirely.
4104 if (merged_paths) {
4105 struct got_pathlist_entry *pe;
4106 if (TAILQ_EMPTY(merged_paths)) {
4107 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4108 goto done;
4110 TAILQ_FOREACH(pe, merged_paths, entry) {
4111 err = worktree_status(worktree, pe->path, fileindex,
4112 repo, collect_commitables, &cc_arg, NULL, NULL);
4113 if (err)
4114 goto done;
4116 } else {
4117 err = worktree_status(worktree, "", fileindex, repo,
4118 collect_commitables, &cc_arg, NULL, NULL);
4119 if (err)
4120 goto done;
4123 if (TAILQ_EMPTY(&commitable_paths)) {
4124 /* No-op change; commit will be elided. */
4125 err = got_ref_delete(commit_ref, repo);
4126 if (err)
4127 goto done;
4128 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4129 goto done;
4132 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4133 if (err)
4134 goto done;
4136 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4137 if (err)
4138 goto done;
4140 if (new_logmsg)
4141 logmsg = strdup(new_logmsg);
4142 else
4143 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4144 if (logmsg == NULL)
4145 return got_error_from_errno("strdup");
4147 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4148 worktree, got_object_commit_get_author(orig_commit),
4149 got_object_commit_get_committer(orig_commit),
4150 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4151 if (err)
4152 goto done;
4154 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4155 if (err)
4156 goto done;
4158 err = got_ref_delete(commit_ref, repo);
4159 if (err)
4160 goto done;
4162 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4163 fileindex);
4164 sync_err = sync_fileindex(fileindex, fileindex_path);
4165 if (sync_err && err == NULL)
4166 err = sync_err;
4167 done:
4168 free(fileindex_path);
4169 free(head_commit_id);
4170 if (head_ref)
4171 got_ref_close(head_ref);
4172 if (err) {
4173 free(*new_commit_id);
4174 *new_commit_id = NULL;
4176 return err;
4179 const struct got_error *
4180 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4181 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4182 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4183 struct got_commit_object *orig_commit,
4184 struct got_object_id *orig_commit_id, struct got_repository *repo)
4186 const struct got_error *err;
4187 char *commit_ref_name;
4188 struct got_reference *commit_ref = NULL;
4189 struct got_object_id *commit_id = NULL;
4191 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4192 if (err)
4193 return err;
4195 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4196 if (err)
4197 goto done;
4198 err = got_ref_resolve(&commit_id, repo, commit_ref);
4199 if (err)
4200 goto done;
4201 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4202 err = got_error(GOT_ERR_REBASE_COMMITID);
4203 goto done;
4206 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4207 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4208 done:
4209 if (commit_ref)
4210 got_ref_close(commit_ref);
4211 free(commit_ref_name);
4212 free(commit_id);
4213 return err;
4216 const struct got_error *
4217 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4218 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4219 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4220 struct got_commit_object *orig_commit,
4221 struct got_object_id *orig_commit_id, const char *new_logmsg,
4222 struct got_repository *repo)
4224 const struct got_error *err;
4225 char *commit_ref_name;
4226 struct got_reference *commit_ref = NULL;
4227 struct got_object_id *commit_id = NULL;
4229 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4230 if (err)
4231 return err;
4233 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4234 if (err)
4235 goto done;
4236 err = got_ref_resolve(&commit_id, repo, commit_ref);
4237 if (err)
4238 goto done;
4239 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4240 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4241 goto done;
4244 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4245 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4246 done:
4247 if (commit_ref)
4248 got_ref_close(commit_ref);
4249 free(commit_ref_name);
4250 free(commit_id);
4251 return err;
4254 const struct got_error *
4255 got_worktree_rebase_postpone(struct got_worktree *worktree,
4256 struct got_fileindex *fileindex)
4258 if (fileindex)
4259 got_fileindex_free(fileindex);
4260 return lock_worktree(worktree, LOCK_SH);
4263 static const struct got_error *
4264 delete_ref(const char *name, struct got_repository *repo)
4266 const struct got_error *err;
4267 struct got_reference *ref;
4269 err = got_ref_open(&ref, repo, name, 0);
4270 if (err) {
4271 if (err->code == GOT_ERR_NOT_REF)
4272 return NULL;
4273 return err;
4276 err = got_ref_delete(ref, repo);
4277 got_ref_close(ref);
4278 return err;
4281 static const struct got_error *
4282 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4284 const struct got_error *err;
4285 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4286 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4288 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4289 if (err)
4290 goto done;
4291 err = delete_ref(tmp_branch_name, repo);
4292 if (err)
4293 goto done;
4295 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4296 if (err)
4297 goto done;
4298 err = delete_ref(new_base_branch_ref_name, repo);
4299 if (err)
4300 goto done;
4302 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4303 if (err)
4304 goto done;
4305 err = delete_ref(branch_ref_name, repo);
4306 if (err)
4307 goto done;
4309 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4310 if (err)
4311 goto done;
4312 err = delete_ref(commit_ref_name, repo);
4313 if (err)
4314 goto done;
4316 done:
4317 free(tmp_branch_name);
4318 free(new_base_branch_ref_name);
4319 free(branch_ref_name);
4320 free(commit_ref_name);
4321 return err;
4324 const struct got_error *
4325 got_worktree_rebase_complete(struct got_worktree *worktree,
4326 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4327 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4328 struct got_repository *repo)
4330 const struct got_error *err, *unlockerr;
4331 struct got_object_id *new_head_commit_id = NULL;
4333 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4334 if (err)
4335 return err;
4337 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4338 if (err)
4339 goto done;
4341 err = got_ref_write(rebased_branch, repo);
4342 if (err)
4343 goto done;
4345 err = got_worktree_set_head_ref(worktree, rebased_branch);
4346 if (err)
4347 goto done;
4349 err = delete_rebase_refs(worktree, repo);
4350 done:
4351 if (fileindex)
4352 got_fileindex_free(fileindex);
4353 free(new_head_commit_id);
4354 unlockerr = lock_worktree(worktree, LOCK_SH);
4355 if (unlockerr && err == NULL)
4356 err = unlockerr;
4357 return err;
4360 struct collect_revertible_paths_arg {
4361 struct got_pathlist_head *revertible_paths;
4362 struct got_worktree *worktree;
4365 static const struct got_error *
4366 collect_revertible_paths(void *arg, unsigned char status,
4367 unsigned char staged_status, const char *relpath,
4368 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4369 struct got_object_id *commit_id)
4371 struct collect_revertible_paths_arg *a = arg;
4372 const struct got_error *err = NULL;
4373 struct got_pathlist_entry *new = NULL;
4374 char *path = NULL;
4376 if (status != GOT_STATUS_ADD &&
4377 status != GOT_STATUS_DELETE &&
4378 status != GOT_STATUS_MODIFY &&
4379 status != GOT_STATUS_CONFLICT &&
4380 status != GOT_STATUS_MISSING)
4381 return NULL;
4383 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4384 return got_error_from_errno("asprintf");
4386 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4387 if (err || new == NULL)
4388 free(path);
4389 return err;
4392 const struct got_error *
4393 got_worktree_rebase_abort(struct got_worktree *worktree,
4394 struct got_fileindex *fileindex, struct got_repository *repo,
4395 struct got_reference *new_base_branch,
4396 got_worktree_checkout_cb progress_cb, void *progress_arg)
4398 const struct got_error *err, *unlockerr, *sync_err;
4399 struct got_reference *resolved = NULL;
4400 struct got_object_id *commit_id = NULL;
4401 char *fileindex_path = NULL;
4402 struct got_pathlist_head revertible_paths;
4403 struct got_pathlist_entry *pe;
4404 struct collect_revertible_paths_arg crp_arg;
4405 struct got_object_id *tree_id = NULL;
4407 TAILQ_INIT(&revertible_paths);
4409 err = lock_worktree(worktree, LOCK_EX);
4410 if (err)
4411 return err;
4413 err = got_ref_open(&resolved, repo,
4414 got_ref_get_symref_target(new_base_branch), 0);
4415 if (err)
4416 goto done;
4418 err = got_worktree_set_head_ref(worktree, resolved);
4419 if (err)
4420 goto done;
4423 * XXX commits to the base branch could have happened while
4424 * we were busy rebasing; should we store the original commit ID
4425 * when rebase begins and read it back here?
4427 err = got_ref_resolve(&commit_id, repo, resolved);
4428 if (err)
4429 goto done;
4431 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4432 if (err)
4433 goto done;
4435 err = got_object_id_by_path(&tree_id, repo,
4436 worktree->base_commit_id, worktree->path_prefix);
4437 if (err)
4438 goto done;
4440 err = delete_rebase_refs(worktree, repo);
4441 if (err)
4442 goto done;
4444 err = get_fileindex_path(&fileindex_path, worktree);
4445 if (err)
4446 goto done;
4448 crp_arg.revertible_paths = &revertible_paths;
4449 crp_arg.worktree = worktree;
4450 err = worktree_status(worktree, "", fileindex, repo,
4451 collect_revertible_paths, &crp_arg, NULL, NULL);
4452 if (err)
4453 goto done;
4455 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4456 err = revert_file(worktree, fileindex, pe->path,
4457 progress_cb, progress_arg, repo);
4458 if (err)
4459 goto sync;
4462 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4463 repo, progress_cb, progress_arg, NULL, NULL);
4464 sync:
4465 sync_err = sync_fileindex(fileindex, fileindex_path);
4466 if (sync_err && err == NULL)
4467 err = sync_err;
4468 done:
4469 got_ref_close(resolved);
4470 free(tree_id);
4471 free(commit_id);
4472 if (fileindex)
4473 got_fileindex_free(fileindex);
4474 free(fileindex_path);
4475 TAILQ_FOREACH(pe, &revertible_paths, entry)
4476 free((char *)pe->path);
4477 got_pathlist_free(&revertible_paths);
4479 unlockerr = lock_worktree(worktree, LOCK_SH);
4480 if (unlockerr && err == NULL)
4481 err = unlockerr;
4482 return err;
4485 const struct got_error *
4486 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4487 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4488 struct got_fileindex **fileindex, struct got_worktree *worktree,
4489 struct got_repository *repo)
4491 const struct got_error *err = NULL;
4492 char *tmp_branch_name = NULL;
4493 char *branch_ref_name = NULL;
4494 char *base_commit_ref_name = NULL;
4495 char *fileindex_path = NULL;
4496 struct check_rebase_ok_arg ok_arg;
4497 struct got_reference *wt_branch = NULL;
4498 struct got_reference *base_commit_ref = NULL;
4500 *tmp_branch = NULL;
4501 *branch_ref = NULL;
4502 *base_commit_id = NULL;
4503 *fileindex = NULL;
4505 err = lock_worktree(worktree, LOCK_EX);
4506 if (err)
4507 return err;
4509 err = open_fileindex(fileindex, &fileindex_path, worktree);
4510 if (err)
4511 goto done;
4513 ok_arg.worktree = worktree;
4514 ok_arg.repo = repo;
4515 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4516 &ok_arg);
4517 if (err)
4518 goto done;
4520 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4521 if (err)
4522 goto done;
4524 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4525 if (err)
4526 goto done;
4528 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4529 worktree);
4530 if (err)
4531 goto done;
4533 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4534 0);
4535 if (err)
4536 goto done;
4538 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4539 if (err)
4540 goto done;
4542 err = got_ref_write(*branch_ref, repo);
4543 if (err)
4544 goto done;
4546 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4547 worktree->base_commit_id);
4548 if (err)
4549 goto done;
4550 err = got_ref_write(base_commit_ref, repo);
4551 if (err)
4552 goto done;
4553 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4554 if (*base_commit_id == NULL) {
4555 err = got_error_from_errno("got_object_id_dup");
4556 goto done;
4559 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4560 worktree->base_commit_id);
4561 if (err)
4562 goto done;
4563 err = got_ref_write(*tmp_branch, repo);
4564 if (err)
4565 goto done;
4567 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4568 if (err)
4569 goto done;
4570 done:
4571 free(fileindex_path);
4572 free(tmp_branch_name);
4573 free(branch_ref_name);
4574 free(base_commit_ref_name);
4575 if (wt_branch)
4576 got_ref_close(wt_branch);
4577 if (err) {
4578 if (*branch_ref) {
4579 got_ref_close(*branch_ref);
4580 *branch_ref = NULL;
4582 if (*tmp_branch) {
4583 got_ref_close(*tmp_branch);
4584 *tmp_branch = NULL;
4586 free(*base_commit_id);
4587 if (*fileindex) {
4588 got_fileindex_free(*fileindex);
4589 *fileindex = NULL;
4591 lock_worktree(worktree, LOCK_SH);
4593 return err;
4596 const struct got_error *
4597 got_worktree_histedit_postpone(struct got_worktree *worktree,
4598 struct got_fileindex *fileindex)
4600 if (fileindex)
4601 got_fileindex_free(fileindex);
4602 return lock_worktree(worktree, LOCK_SH);
4605 const struct got_error *
4606 got_worktree_histedit_in_progress(int *in_progress,
4607 struct got_worktree *worktree)
4609 const struct got_error *err;
4610 char *tmp_branch_name = NULL;
4612 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4613 if (err)
4614 return err;
4616 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4617 free(tmp_branch_name);
4618 return NULL;
4621 const struct got_error *
4622 got_worktree_histedit_continue(struct got_object_id **commit_id,
4623 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4624 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4625 struct got_worktree *worktree, struct got_repository *repo)
4627 const struct got_error *err;
4628 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4629 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4630 struct got_reference *commit_ref = NULL;
4631 struct got_reference *base_commit_ref = NULL;
4632 char *fileindex_path = NULL;
4634 *commit_id = NULL;
4635 *tmp_branch = NULL;
4636 *base_commit_id = NULL;
4637 *fileindex = NULL;
4639 err = lock_worktree(worktree, LOCK_EX);
4640 if (err)
4641 return err;
4643 err = open_fileindex(fileindex, &fileindex_path, worktree);
4644 if (err)
4645 goto done;
4647 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4648 if (err)
4649 return err;
4651 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4652 if (err)
4653 goto done;
4655 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4656 if (err)
4657 goto done;
4659 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4660 worktree);
4661 if (err)
4662 goto done;
4664 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4665 if (err)
4666 goto done;
4668 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4669 if (err)
4670 goto done;
4671 err = got_ref_resolve(commit_id, repo, commit_ref);
4672 if (err)
4673 goto done;
4675 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4676 if (err)
4677 goto done;
4678 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4679 if (err)
4680 goto done;
4682 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4683 if (err)
4684 goto done;
4685 done:
4686 free(commit_ref_name);
4687 free(branch_ref_name);
4688 free(fileindex_path);
4689 if (commit_ref)
4690 got_ref_close(commit_ref);
4691 if (base_commit_ref)
4692 got_ref_close(base_commit_ref);
4693 if (err) {
4694 free(*commit_id);
4695 *commit_id = NULL;
4696 free(*base_commit_id);
4697 *base_commit_id = NULL;
4698 if (*tmp_branch) {
4699 got_ref_close(*tmp_branch);
4700 *tmp_branch = NULL;
4702 if (*fileindex) {
4703 got_fileindex_free(*fileindex);
4704 *fileindex = NULL;
4706 lock_worktree(worktree, LOCK_EX);
4708 return err;
4711 static const struct got_error *
4712 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4714 const struct got_error *err;
4715 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4716 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4718 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4719 if (err)
4720 goto done;
4721 err = delete_ref(tmp_branch_name, repo);
4722 if (err)
4723 goto done;
4725 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4726 worktree);
4727 if (err)
4728 goto done;
4729 err = delete_ref(base_commit_ref_name, repo);
4730 if (err)
4731 goto done;
4733 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4734 if (err)
4735 goto done;
4736 err = delete_ref(branch_ref_name, repo);
4737 if (err)
4738 goto done;
4740 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4741 if (err)
4742 goto done;
4743 err = delete_ref(commit_ref_name, repo);
4744 if (err)
4745 goto done;
4746 done:
4747 free(tmp_branch_name);
4748 free(base_commit_ref_name);
4749 free(branch_ref_name);
4750 free(commit_ref_name);
4751 return err;
4754 const struct got_error *
4755 got_worktree_histedit_abort(struct got_worktree *worktree,
4756 struct got_fileindex *fileindex, struct got_repository *repo,
4757 struct got_reference *branch, struct got_object_id *base_commit_id,
4758 got_worktree_checkout_cb progress_cb, void *progress_arg)
4760 const struct got_error *err, *unlockerr, *sync_err;
4761 struct got_reference *resolved = NULL;
4762 char *fileindex_path = NULL;
4763 struct got_pathlist_head revertible_paths;
4764 struct got_pathlist_entry *pe;
4765 struct collect_revertible_paths_arg crp_arg;
4766 struct got_object_id *tree_id = NULL;
4768 TAILQ_INIT(&revertible_paths);
4770 err = lock_worktree(worktree, LOCK_EX);
4771 if (err)
4772 return err;
4774 err = got_ref_open(&resolved, repo,
4775 got_ref_get_symref_target(branch), 0);
4776 if (err)
4777 goto done;
4779 err = got_worktree_set_head_ref(worktree, resolved);
4780 if (err)
4781 goto done;
4783 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4784 if (err)
4785 goto done;
4787 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4788 worktree->path_prefix);
4789 if (err)
4790 goto done;
4792 err = delete_histedit_refs(worktree, repo);
4793 if (err)
4794 goto done;
4796 err = get_fileindex_path(&fileindex_path, worktree);
4797 if (err)
4798 goto done;
4800 crp_arg.revertible_paths = &revertible_paths;
4801 crp_arg.worktree = worktree;
4802 err = worktree_status(worktree, "", fileindex, repo,
4803 collect_revertible_paths, &crp_arg, NULL, NULL);
4804 if (err)
4805 goto done;
4807 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4808 err = revert_file(worktree, fileindex, pe->path,
4809 progress_cb, progress_arg, repo);
4810 if (err)
4811 goto sync;
4814 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4815 repo, progress_cb, progress_arg, NULL, NULL);
4816 sync:
4817 sync_err = sync_fileindex(fileindex, fileindex_path);
4818 if (sync_err && err == NULL)
4819 err = sync_err;
4820 done:
4821 got_ref_close(resolved);
4822 free(tree_id);
4823 free(fileindex_path);
4824 TAILQ_FOREACH(pe, &revertible_paths, entry)
4825 free((char *)pe->path);
4826 got_pathlist_free(&revertible_paths);
4828 unlockerr = lock_worktree(worktree, LOCK_SH);
4829 if (unlockerr && err == NULL)
4830 err = unlockerr;
4831 return err;
4834 const struct got_error *
4835 got_worktree_histedit_complete(struct got_worktree *worktree,
4836 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4837 struct got_reference *edited_branch, struct got_repository *repo)
4839 const struct got_error *err, *unlockerr;
4840 struct got_object_id *new_head_commit_id = NULL;
4841 struct got_reference *resolved = NULL;
4843 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4844 if (err)
4845 return err;
4847 err = got_ref_open(&resolved, repo,
4848 got_ref_get_symref_target(edited_branch), 0);
4849 if (err)
4850 goto done;
4852 err = got_ref_change_ref(resolved, new_head_commit_id);
4853 if (err)
4854 goto done;
4856 err = got_ref_write(resolved, repo);
4857 if (err)
4858 goto done;
4860 err = got_worktree_set_head_ref(worktree, resolved);
4861 if (err)
4862 goto done;
4864 err = delete_histedit_refs(worktree, repo);
4865 done:
4866 if (fileindex)
4867 got_fileindex_free(fileindex);
4868 free(new_head_commit_id);
4869 unlockerr = lock_worktree(worktree, LOCK_SH);
4870 if (unlockerr && err == NULL)
4871 err = unlockerr;
4872 return err;
4875 const struct got_error *
4876 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4877 struct got_object_id *commit_id, struct got_repository *repo)
4879 const struct got_error *err;
4880 char *commit_ref_name;
4882 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4883 if (err)
4884 return err;
4886 err = store_commit_id(commit_ref_name, commit_id, repo);
4887 if (err)
4888 goto done;
4890 err = delete_ref(commit_ref_name, repo);
4891 done:
4892 free(commit_ref_name);
4893 return err;
4896 static const struct got_error *
4897 stage_path(const char *relpath, const char *ondisk_path,
4898 const char *path_content, struct got_worktree *worktree,
4899 struct got_fileindex *fileindex, struct got_repository *repo,
4900 got_worktree_status_cb status_cb, void *status_arg)
4902 const struct got_error *err = NULL;
4903 struct got_fileindex_entry *ie;
4904 unsigned char status;
4905 struct stat sb;
4906 struct got_object_id blob_id, *staged_blob_id = NULL;
4907 uint32_t stage;
4909 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
4910 if (ie == NULL) {
4911 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4912 goto done;
4915 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4916 if (err)
4917 goto done;
4919 switch (status) {
4920 case GOT_STATUS_ADD:
4921 case GOT_STATUS_MODIFY:
4922 err = got_object_blob_create(&staged_blob_id,
4923 path_content ? path_content : ondisk_path, repo);
4924 if (err)
4925 goto done;
4926 memcpy(&blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
4927 memcpy(ie->staged_blob_sha1, staged_blob_id->sha1,
4928 SHA1_DIGEST_LENGTH);
4929 if (status == GOT_STATUS_ADD)
4930 stage = GOT_FILEIDX_STAGE_ADD;
4931 else
4932 stage = GOT_FILEIDX_STAGE_MODIFY;
4933 got_fileindex_entry_stage_set(ie, stage);
4934 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4935 get_staged_status(ie), relpath, &blob_id,
4936 staged_blob_id, NULL);
4937 break;
4938 case GOT_STATUS_DELETE:
4939 stage = GOT_FILEIDX_STAGE_DELETE;
4940 got_fileindex_entry_stage_set(ie, stage);
4941 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4942 get_staged_status(ie), relpath, NULL, NULL, NULL);
4943 break;
4944 default:
4945 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4946 break;
4948 done:
4949 free(staged_blob_id);
4950 return err;
4953 const struct got_error *
4954 got_worktree_stage(struct got_worktree *worktree,
4955 struct got_pathlist_head *paths,
4956 got_worktree_status_cb status_cb, void *status_arg,
4957 struct got_repository *repo)
4959 const struct got_error *err = NULL, *sync_err, *unlockerr;
4960 struct got_pathlist_entry *pe;
4961 struct got_fileindex *fileindex = NULL;
4962 char *fileindex_path = NULL;
4964 err = lock_worktree(worktree, LOCK_EX);
4965 if (err)
4966 return err;
4968 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4969 if (err)
4970 goto done;
4972 TAILQ_FOREACH(pe, paths, entry) {
4973 char *relpath;
4974 err = got_path_skip_common_ancestor(&relpath,
4975 got_worktree_get_root_path(worktree), pe->path);
4976 if (err)
4977 break;
4978 err = stage_path(relpath, pe->path,
4979 (const char *)pe->data, worktree, fileindex, repo,
4980 status_cb, status_arg);
4981 free(relpath);
4982 if (err)
4983 break;
4986 sync_err = sync_fileindex(fileindex, fileindex_path);
4987 if (sync_err && err == NULL)
4988 err = sync_err;
4989 done:
4990 free(fileindex_path);
4991 if (fileindex)
4992 got_fileindex_free(fileindex);
4993 unlockerr = lock_worktree(worktree, LOCK_SH);
4994 if (unlockerr && err == NULL)
4995 err = unlockerr;
4996 return err;