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 staged_status != GOT_STATUS_ADD) {
1089 *status = GOT_STATUS_ADD;
1090 return NULL;
1093 if (!stat_info_differs(ie, sb))
1094 return NULL;
1096 if (staged_status == GOT_STATUS_MODIFY ||
1097 staged_status == GOT_STATUS_ADD)
1098 memcpy(id.sha1, ie->staged_blob_sha1, sizeof(id.sha1));
1099 else
1100 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1102 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1103 if (err)
1104 return err;
1106 f = fopen(abspath, "r");
1107 if (f == NULL) {
1108 err = got_error_from_errno2("fopen", abspath);
1109 goto done;
1111 hdrlen = got_object_blob_get_hdrlen(blob);
1112 for (;;) {
1113 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1114 err = got_object_blob_read_block(&blen, blob);
1115 if (err)
1116 goto done;
1117 /* Skip length of blob object header first time around. */
1118 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1119 if (flen == 0 && ferror(f)) {
1120 err = got_error_from_errno("fread");
1121 goto done;
1123 if (blen == 0) {
1124 if (flen != 0)
1125 *status = GOT_STATUS_MODIFY;
1126 break;
1127 } else if (flen == 0) {
1128 if (blen != 0)
1129 *status = GOT_STATUS_MODIFY;
1130 break;
1131 } else if (blen - hdrlen == flen) {
1132 /* Skip blob object header first time around. */
1133 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1134 *status = GOT_STATUS_MODIFY;
1135 break;
1137 } else {
1138 *status = GOT_STATUS_MODIFY;
1139 break;
1141 hdrlen = 0;
1144 if (*status == GOT_STATUS_MODIFY) {
1145 rewind(f);
1146 err = get_modified_file_content_status(status, f);
1148 done:
1149 if (blob)
1150 got_object_blob_close(blob);
1151 if (f)
1152 fclose(f);
1153 return err;
1157 * Update timestamps in the file index if a file is unmodified and
1158 * we had to run a full content comparison to find out.
1160 static const struct got_error *
1161 sync_timestamps(char *ondisk_path, unsigned char status,
1162 struct got_fileindex_entry *ie, struct stat *sb)
1164 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1165 return got_fileindex_entry_update(ie, ondisk_path,
1166 ie->blob_sha1, ie->commit_sha1, 1);
1168 return NULL;
1171 static const struct got_error *
1172 update_blob(struct got_worktree *worktree,
1173 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1174 struct got_tree_entry *te, const char *path,
1175 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1176 void *progress_arg)
1178 const struct got_error *err = NULL;
1179 struct got_blob_object *blob = NULL;
1180 char *ondisk_path;
1181 unsigned char status = GOT_STATUS_NO_CHANGE;
1182 struct stat sb;
1184 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1185 return got_error_from_errno("asprintf");
1187 if (ie) {
1188 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1189 if (err)
1190 goto done;
1191 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
1192 sb.st_mode = got_fileindex_perms_to_st(ie);
1193 } else
1194 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1196 if (status == GOT_STATUS_OBSTRUCTED) {
1197 err = (*progress_cb)(progress_arg, status, path);
1198 goto done;
1201 if (ie && status != GOT_STATUS_MISSING) {
1202 if (got_fileindex_entry_has_commit(ie) &&
1203 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1204 SHA1_DIGEST_LENGTH) == 0) {
1205 err = sync_timestamps(ondisk_path, status, ie, &sb);
1206 if (err)
1207 goto done;
1208 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1209 path);
1210 goto done;
1212 if (got_fileindex_entry_has_blob(ie) &&
1213 memcmp(ie->blob_sha1, te->id->sha1,
1214 SHA1_DIGEST_LENGTH) == 0) {
1215 err = sync_timestamps(ondisk_path, status, ie, &sb);
1216 goto done;
1220 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1221 if (err)
1222 goto done;
1224 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1225 int update_timestamps;
1226 struct got_blob_object *blob2 = NULL;
1227 if (got_fileindex_entry_has_blob(ie)) {
1228 struct got_object_id id2;
1229 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1230 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1231 if (err)
1232 goto done;
1234 err = merge_blob(&update_timestamps, worktree, blob2,
1235 ondisk_path, path, sb.st_mode, blob,
1236 worktree->base_commit_id, repo,
1237 progress_cb, progress_arg);
1238 if (blob2)
1239 got_object_blob_close(blob2);
1241 * Do not update timestamps of files with local changes.
1242 * Otherwise, a future status walk would treat them as
1243 * unmodified files again.
1245 err = got_fileindex_entry_update(ie, ondisk_path,
1246 blob->id.sha1, worktree->base_commit_id->sha1,
1247 update_timestamps);
1248 } else if (status == GOT_STATUS_DELETE) {
1249 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1250 if (err)
1251 goto done;
1252 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1253 ondisk_path, path, blob, 0);
1254 if (err)
1255 goto done;
1256 } else {
1257 err = install_blob(worktree, ondisk_path, path, te->mode,
1258 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1259 repo, progress_cb, progress_arg);
1260 if (err)
1261 goto done;
1262 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1263 ondisk_path, path, blob, 1);
1264 if (err)
1265 goto done;
1267 got_object_blob_close(blob);
1268 done:
1269 free(ondisk_path);
1270 return err;
1273 static const struct got_error *
1274 remove_ondisk_file(const char *root_path, const char *path)
1276 const struct got_error *err = NULL;
1277 char *ondisk_path = NULL;
1279 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1280 return got_error_from_errno("asprintf");
1282 if (unlink(ondisk_path) == -1) {
1283 if (errno != ENOENT)
1284 err = got_error_from_errno2("unlink", ondisk_path);
1285 } else {
1286 char *parent = dirname(ondisk_path);
1287 while (parent && strcmp(parent, root_path) != 0) {
1288 if (rmdir(parent) == -1) {
1289 if (errno != ENOTEMPTY)
1290 err = got_error_from_errno2("rmdir",
1291 parent);
1292 break;
1294 parent = dirname(parent);
1297 free(ondisk_path);
1298 return err;
1301 static const struct got_error *
1302 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1303 struct got_fileindex_entry *ie, struct got_repository *repo,
1304 got_worktree_checkout_cb progress_cb, void *progress_arg)
1306 const struct got_error *err = NULL;
1307 unsigned char status;
1308 struct stat sb;
1309 char *ondisk_path;
1311 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1312 == -1)
1313 return got_error_from_errno("asprintf");
1315 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1316 if (err)
1317 return err;
1319 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1320 status == GOT_STATUS_ADD) {
1321 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1322 if (err)
1323 return err;
1325 * Preserve the working file and change the deleted blob's
1326 * entry into a schedule-add entry.
1328 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1329 0);
1330 if (err)
1331 return err;
1332 } else {
1333 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1334 if (err)
1335 return err;
1336 if (status == GOT_STATUS_NO_CHANGE) {
1337 err = remove_ondisk_file(worktree->root_path, ie->path);
1338 if (err)
1339 return err;
1341 got_fileindex_entry_remove(fileindex, ie);
1344 return err;
1347 struct diff_cb_arg {
1348 struct got_fileindex *fileindex;
1349 struct got_worktree *worktree;
1350 struct got_repository *repo;
1351 got_worktree_checkout_cb progress_cb;
1352 void *progress_arg;
1353 got_worktree_cancel_cb cancel_cb;
1354 void *cancel_arg;
1357 static const struct got_error *
1358 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1359 struct got_tree_entry *te, const char *parent_path)
1361 struct diff_cb_arg *a = arg;
1363 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1364 return got_error(GOT_ERR_CANCELLED);
1366 return update_blob(a->worktree, a->fileindex, ie, te,
1367 ie->path, a->repo, a->progress_cb, a->progress_arg);
1370 static const struct got_error *
1371 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1373 struct diff_cb_arg *a = arg;
1375 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1376 return got_error(GOT_ERR_CANCELLED);
1378 return delete_blob(a->worktree, a->fileindex, ie,
1379 a->repo, a->progress_cb, a->progress_arg);
1382 static const struct got_error *
1383 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1385 struct diff_cb_arg *a = arg;
1386 const struct got_error *err;
1387 char *path;
1389 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1390 return got_error(GOT_ERR_CANCELLED);
1392 if (asprintf(&path, "%s%s%s", parent_path,
1393 parent_path[0] ? "/" : "", te->name)
1394 == -1)
1395 return got_error_from_errno("asprintf");
1397 if (S_ISDIR(te->mode))
1398 err = add_dir_on_disk(a->worktree, path);
1399 else
1400 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1401 a->repo, a->progress_cb, a->progress_arg);
1403 free(path);
1404 return err;
1407 static const struct got_error *
1408 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1410 const struct got_error *err = NULL;
1411 char *uuidstr = NULL;
1412 uint32_t uuid_status;
1414 *refname = NULL;
1416 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1417 if (uuid_status != uuid_s_ok)
1418 return got_error_uuid(uuid_status);
1420 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1421 == -1) {
1422 err = got_error_from_errno("asprintf");
1423 *refname = NULL;
1425 free(uuidstr);
1426 return err;
1429 const struct got_error *
1430 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1432 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1435 static const struct got_error *
1436 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1438 return get_ref_name(refname, worktree,
1439 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1442 static const struct got_error *
1443 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1445 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1448 static const struct got_error *
1449 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1451 return get_ref_name(refname, worktree,
1452 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1455 static const struct got_error *
1456 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1458 return get_ref_name(refname, worktree,
1459 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1462 static const struct got_error *
1463 get_histedit_tmp_ref_name(char **refname, struct got_worktree *worktree)
1465 return get_ref_name(refname, worktree,
1466 GOT_WORKTREE_HISTEDIT_TMP_REF_PREFIX);
1469 static const struct got_error *
1470 get_histedit_branch_symref_name(char **refname, struct got_worktree *worktree)
1472 return get_ref_name(refname, worktree,
1473 GOT_WORKTREE_HISTEDIT_BRANCH_REF_PREFIX);
1476 static const struct got_error *
1477 get_histedit_base_commit_ref_name(char **refname, struct got_worktree *worktree)
1479 return get_ref_name(refname, worktree,
1480 GOT_WORKTREE_HISTEDIT_BASE_COMMIT_REF_PREFIX);
1483 static const struct got_error *
1484 get_histedit_commit_ref_name(char **refname, struct got_worktree *worktree)
1486 return get_ref_name(refname, worktree,
1487 GOT_WORKTREE_HISTEDIT_COMMIT_REF_PREFIX);
1490 const struct got_error *
1491 got_worktree_get_histedit_script_path(char **path,
1492 struct got_worktree *worktree)
1494 if (asprintf(path, "%s/%s/%s", worktree->root_path,
1495 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_HISTEDIT_SCRIPT) == -1) {
1496 *path = NULL;
1497 return got_error_from_errno("asprintf");
1499 return NULL;
1503 * Prevent Git's garbage collector from deleting our base commit by
1504 * setting a reference to our base commit's ID.
1506 static const struct got_error *
1507 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1509 const struct got_error *err = NULL;
1510 struct got_reference *ref = NULL;
1511 char *refname;
1513 err = got_worktree_get_base_ref_name(&refname, worktree);
1514 if (err)
1515 return err;
1517 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1518 if (err)
1519 goto done;
1521 err = got_ref_write(ref, repo);
1522 done:
1523 free(refname);
1524 if (ref)
1525 got_ref_close(ref);
1526 return err;
1529 static const struct got_error *
1530 get_fileindex_path(char **fileindex_path, struct got_worktree *worktree)
1532 const struct got_error *err = NULL;
1534 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1535 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1536 err = got_error_from_errno("asprintf");
1537 *fileindex_path = NULL;
1539 return err;
1543 static const struct got_error *
1544 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1545 struct got_worktree *worktree)
1547 const struct got_error *err = NULL;
1548 FILE *index = NULL;
1550 *fileindex_path = NULL;
1551 *fileindex = got_fileindex_alloc();
1552 if (*fileindex == NULL)
1553 return got_error_from_errno("got_fileindex_alloc");
1555 err = get_fileindex_path(fileindex_path, worktree);
1556 if (err)
1557 goto done;
1559 index = fopen(*fileindex_path, "rb");
1560 if (index == NULL) {
1561 if (errno != ENOENT)
1562 err = got_error_from_errno2("fopen", *fileindex_path);
1563 } else {
1564 err = got_fileindex_read(*fileindex, index);
1565 if (fclose(index) != 0 && err == NULL)
1566 err = got_error_from_errno("fclose");
1568 done:
1569 if (err) {
1570 free(*fileindex_path);
1571 *fileindex_path = NULL;
1572 got_fileindex_free(*fileindex);
1573 *fileindex = NULL;
1575 return err;
1578 struct bump_base_commit_id_arg {
1579 struct got_object_id *base_commit_id;
1580 const char *path;
1581 size_t path_len;
1582 const char *entry_name;
1583 got_worktree_checkout_cb progress_cb;
1584 void *progress_arg;
1587 /* Bump base commit ID of all files within an updated part of the work tree. */
1588 static const struct got_error *
1589 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1591 const struct got_error *err;
1592 struct bump_base_commit_id_arg *a = arg;
1594 if (a->entry_name) {
1595 if (strcmp(ie->path, a->path) != 0)
1596 return NULL;
1597 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1598 return NULL;
1600 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1601 SHA1_DIGEST_LENGTH) == 0)
1602 return NULL;
1604 if (a->progress_cb) {
1605 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1606 ie->path);
1607 if (err)
1608 return err;
1610 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1611 return NULL;
1614 static const struct got_error *
1615 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1617 const struct got_error *err = NULL;
1618 char *new_fileindex_path = NULL;
1619 FILE *new_index = NULL;
1621 err = got_opentemp_named(&new_fileindex_path, &new_index,
1622 fileindex_path);
1623 if (err)
1624 goto done;
1626 err = got_fileindex_write(fileindex, new_index);
1627 if (err)
1628 goto done;
1630 if (rename(new_fileindex_path, fileindex_path) != 0) {
1631 err = got_error_from_errno3("rename", new_fileindex_path,
1632 fileindex_path);
1633 unlink(new_fileindex_path);
1635 done:
1636 if (new_index)
1637 fclose(new_index);
1638 free(new_fileindex_path);
1639 return err;
1642 static const struct got_error *
1643 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1644 struct got_object_id **tree_id, const char *wt_relpath,
1645 struct got_worktree *worktree, struct got_repository *repo)
1647 const struct got_error *err = NULL;
1648 struct got_object_id *id = NULL;
1649 char *in_repo_path = NULL;
1650 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1652 *entry_type = GOT_OBJ_TYPE_ANY;
1653 *tree_relpath = NULL;
1654 *tree_id = NULL;
1656 if (wt_relpath[0] == '\0') {
1657 /* Check out all files within the work tree. */
1658 *entry_type = GOT_OBJ_TYPE_TREE;
1659 *tree_relpath = strdup("");
1660 if (*tree_relpath == NULL) {
1661 err = got_error_from_errno("strdup");
1662 goto done;
1664 err = got_object_id_by_path(tree_id, repo,
1665 worktree->base_commit_id, worktree->path_prefix);
1666 if (err)
1667 goto done;
1668 return NULL;
1671 /* Check out a subset of files in the work tree. */
1673 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1674 is_root_wt ? "" : "/", wt_relpath) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 goto done;
1679 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1680 in_repo_path);
1681 if (err)
1682 goto done;
1684 free(in_repo_path);
1685 in_repo_path = NULL;
1687 err = got_object_get_type(entry_type, repo, id);
1688 if (err)
1689 goto done;
1691 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1692 /* Check out a single file. */
1693 if (strchr(wt_relpath, '/') == NULL) {
1694 /* Check out a single file in work tree's root dir. */
1695 in_repo_path = strdup(worktree->path_prefix);
1696 if (in_repo_path == NULL) {
1697 err = got_error_from_errno("strdup");
1698 goto done;
1700 *tree_relpath = strdup("");
1701 if (*tree_relpath == NULL) {
1702 err = got_error_from_errno("strdup");
1703 goto done;
1705 } else {
1706 /* Check out a single file in a subdirectory. */
1707 err = got_path_dirname(tree_relpath, wt_relpath);
1708 if (err)
1709 return err;
1710 if (asprintf(&in_repo_path, "%s%s%s",
1711 worktree->path_prefix, is_root_wt ? "" : "/",
1712 *tree_relpath) == -1) {
1713 err = got_error_from_errno("asprintf");
1714 goto done;
1717 err = got_object_id_by_path(tree_id, repo,
1718 worktree->base_commit_id, in_repo_path);
1719 } else {
1720 /* Check out all files within a subdirectory. */
1721 *tree_id = got_object_id_dup(id);
1722 if (*tree_id == NULL) {
1723 err = got_error_from_errno("got_object_id_dup");
1724 goto done;
1726 *tree_relpath = strdup(wt_relpath);
1727 if (*tree_relpath == NULL) {
1728 err = got_error_from_errno("strdup");
1729 goto done;
1732 done:
1733 free(id);
1734 free(in_repo_path);
1735 if (err) {
1736 *entry_type = GOT_OBJ_TYPE_ANY;
1737 free(*tree_relpath);
1738 *tree_relpath = NULL;
1739 free(*tree_id);
1740 *tree_id = NULL;
1742 return err;
1745 static const struct got_error *
1746 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1747 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1748 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1749 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1751 const struct got_error *err = NULL;
1752 struct got_commit_object *commit = NULL;
1753 struct got_tree_object *tree = NULL;
1754 struct got_fileindex_diff_tree_cb diff_cb;
1755 struct diff_cb_arg arg;
1757 err = ref_base_commit(worktree, repo);
1758 if (err)
1759 goto done;
1761 err = got_object_open_as_commit(&commit, repo,
1762 worktree->base_commit_id);
1763 if (err)
1764 goto done;
1766 err = got_object_open_as_tree(&tree, repo, tree_id);
1767 if (err)
1768 goto done;
1770 if (entry_name &&
1771 got_object_tree_find_entry(tree, entry_name) == NULL) {
1772 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1773 goto done;
1776 diff_cb.diff_old_new = diff_old_new;
1777 diff_cb.diff_old = diff_old;
1778 diff_cb.diff_new = diff_new;
1779 arg.fileindex = fileindex;
1780 arg.worktree = worktree;
1781 arg.repo = repo;
1782 arg.progress_cb = progress_cb;
1783 arg.progress_arg = progress_arg;
1784 arg.cancel_cb = cancel_cb;
1785 arg.cancel_arg = cancel_arg;
1786 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1787 entry_name, repo, &diff_cb, &arg);
1788 done:
1789 if (tree)
1790 got_object_tree_close(tree);
1791 if (commit)
1792 got_object_commit_close(commit);
1793 return err;
1796 const struct got_error *
1797 got_worktree_checkout_files(struct got_worktree *worktree,
1798 struct got_pathlist_head *paths, struct got_repository *repo,
1799 got_worktree_checkout_cb progress_cb, void *progress_arg,
1800 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1802 const struct got_error *err = NULL, *sync_err, *unlockerr;
1803 struct got_commit_object *commit = NULL;
1804 struct got_tree_object *tree = NULL;
1805 struct got_fileindex *fileindex = NULL;
1806 char *fileindex_path = NULL;
1807 struct got_pathlist_entry *pe;
1808 struct tree_path_data {
1809 SIMPLEQ_ENTRY(tree_path_data) entry;
1810 struct got_object_id *tree_id;
1811 int entry_type;
1812 char *relpath;
1813 char *entry_name;
1814 } *tpd = NULL;
1815 SIMPLEQ_HEAD(tree_paths, tree_path_data) tree_paths;
1817 SIMPLEQ_INIT(&tree_paths);
1819 err = lock_worktree(worktree, LOCK_EX);
1820 if (err)
1821 return err;
1823 /* Map all specified paths to in-repository trees. */
1824 TAILQ_FOREACH(pe, paths, entry) {
1825 tpd = malloc(sizeof(*tpd));
1826 if (tpd == NULL) {
1827 err = got_error_from_errno("malloc");
1828 goto done;
1831 err = find_tree_entry_for_checkout(&tpd->entry_type,
1832 &tpd->relpath, &tpd->tree_id, pe->path, worktree, repo);
1833 if (err) {
1834 free(tpd);
1835 goto done;
1838 if (tpd->entry_type == GOT_OBJ_TYPE_BLOB) {
1839 err = got_path_basename(&tpd->entry_name, pe->path);
1840 if (err) {
1841 free(tpd->relpath);
1842 free(tpd->tree_id);
1843 free(tpd);
1844 goto done;
1846 } else
1847 tpd->entry_name = NULL;
1849 SIMPLEQ_INSERT_TAIL(&tree_paths, tpd, entry);
1853 * Read the file index.
1854 * Checking out files is supposed to be an idempotent operation.
1855 * If the on-disk file index is incomplete we will try to complete it.
1857 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1858 if (err)
1859 goto done;
1861 tpd = SIMPLEQ_FIRST(&tree_paths);
1862 TAILQ_FOREACH(pe, paths, entry) {
1863 struct bump_base_commit_id_arg bbc_arg;
1865 err = checkout_files(worktree, fileindex, tpd->relpath,
1866 tpd->tree_id, tpd->entry_name, repo,
1867 progress_cb, progress_arg, cancel_cb, cancel_arg);
1868 if (err)
1869 break;
1871 bbc_arg.base_commit_id = worktree->base_commit_id;
1872 bbc_arg.entry_name = tpd->entry_name;
1873 bbc_arg.path = pe->path;
1874 bbc_arg.path_len = pe->path_len;
1875 bbc_arg.progress_cb = progress_cb;
1876 bbc_arg.progress_arg = progress_arg;
1877 err = got_fileindex_for_each_entry_safe(fileindex,
1878 bump_base_commit_id, &bbc_arg);
1879 if (err)
1880 break;
1882 tpd = SIMPLEQ_NEXT(tpd, entry);
1884 sync_err = sync_fileindex(fileindex, fileindex_path);
1885 if (sync_err && err == NULL)
1886 err = sync_err;
1887 done:
1888 free(fileindex_path);
1889 if (tree)
1890 got_object_tree_close(tree);
1891 if (commit)
1892 got_object_commit_close(commit);
1893 if (fileindex)
1894 got_fileindex_free(fileindex);
1895 while (!SIMPLEQ_EMPTY(&tree_paths)) {
1896 tpd = SIMPLEQ_FIRST(&tree_paths);
1897 SIMPLEQ_REMOVE_HEAD(&tree_paths, entry);
1898 free(tpd->relpath);
1899 free(tpd->tree_id);
1900 free(tpd);
1902 unlockerr = lock_worktree(worktree, LOCK_SH);
1903 if (unlockerr && err == NULL)
1904 err = unlockerr;
1905 return err;
1908 struct merge_file_cb_arg {
1909 struct got_worktree *worktree;
1910 struct got_fileindex *fileindex;
1911 got_worktree_checkout_cb progress_cb;
1912 void *progress_arg;
1913 got_worktree_cancel_cb cancel_cb;
1914 void *cancel_arg;
1915 struct got_object_id *commit_id2;
1918 static const struct got_error *
1919 merge_file_cb(void *arg, struct got_blob_object *blob1,
1920 struct got_blob_object *blob2, struct got_object_id *id1,
1921 struct got_object_id *id2, const char *path1, const char *path2,
1922 struct got_repository *repo)
1924 static const struct got_error *err = NULL;
1925 struct merge_file_cb_arg *a = arg;
1926 struct got_fileindex_entry *ie;
1927 char *ondisk_path = NULL;
1928 struct stat sb;
1929 unsigned char status;
1930 int local_changes_subsumed;
1932 if (blob1 && blob2) {
1933 ie = got_fileindex_entry_get(a->fileindex, path2,
1934 strlen(path2));
1935 if (ie == NULL)
1936 return (*a->progress_cb)(a->progress_arg,
1937 GOT_STATUS_MISSING, path2);
1939 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1940 path2) == -1)
1941 return got_error_from_errno("asprintf");
1943 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1944 if (err)
1945 goto done;
1947 if (status == GOT_STATUS_DELETE) {
1948 err = (*a->progress_cb)(a->progress_arg,
1949 GOT_STATUS_MERGE, path2);
1950 goto done;
1952 if (status != GOT_STATUS_NO_CHANGE &&
1953 status != GOT_STATUS_MODIFY &&
1954 status != GOT_STATUS_CONFLICT &&
1955 status != GOT_STATUS_ADD) {
1956 err = (*a->progress_cb)(a->progress_arg, status, path2);
1957 goto done;
1960 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1961 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1962 a->progress_cb, a->progress_arg);
1963 } else if (blob1) {
1964 ie = got_fileindex_entry_get(a->fileindex, path1,
1965 strlen(path1));
1966 if (ie == NULL)
1967 return (*a->progress_cb)(a->progress_arg,
1968 GOT_STATUS_MISSING, path2);
1970 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1971 path1) == -1)
1972 return got_error_from_errno("asprintf");
1974 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1975 if (err)
1976 goto done;
1978 switch (status) {
1979 case GOT_STATUS_NO_CHANGE:
1980 err = (*a->progress_cb)(a->progress_arg,
1981 GOT_STATUS_DELETE, path1);
1982 if (err)
1983 goto done;
1984 err = remove_ondisk_file(a->worktree->root_path, path1);
1985 if (err)
1986 goto done;
1987 if (ie)
1988 got_fileindex_entry_mark_deleted_from_disk(ie);
1989 break;
1990 case GOT_STATUS_DELETE:
1991 case GOT_STATUS_MISSING:
1992 err = (*a->progress_cb)(a->progress_arg,
1993 GOT_STATUS_DELETE, path1);
1994 if (err)
1995 goto done;
1996 if (ie)
1997 got_fileindex_entry_mark_deleted_from_disk(ie);
1998 break;
1999 case GOT_STATUS_ADD:
2000 case GOT_STATUS_MODIFY:
2001 case GOT_STATUS_CONFLICT:
2002 err = (*a->progress_cb)(a->progress_arg,
2003 GOT_STATUS_CANNOT_DELETE, path1);
2004 if (err)
2005 goto done;
2006 break;
2007 case GOT_STATUS_OBSTRUCTED:
2008 err = (*a->progress_cb)(a->progress_arg, status, path1);
2009 if (err)
2010 goto done;
2011 break;
2012 default:
2013 break;
2015 } else if (blob2) {
2016 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
2017 path2) == -1)
2018 return got_error_from_errno("asprintf");
2019 ie = got_fileindex_entry_get(a->fileindex, path2,
2020 strlen(path2));
2021 if (ie) {
2022 err = get_file_status(&status, &sb, ie, ondisk_path,
2023 repo);
2024 if (err)
2025 goto done;
2026 if (status != GOT_STATUS_NO_CHANGE &&
2027 status != GOT_STATUS_MODIFY &&
2028 status != GOT_STATUS_CONFLICT &&
2029 status != GOT_STATUS_ADD) {
2030 err = (*a->progress_cb)(a->progress_arg,
2031 status, path2);
2032 goto done;
2034 err = merge_blob(&local_changes_subsumed, a->worktree,
2035 NULL, ondisk_path, path2, sb.st_mode, blob2,
2036 a->commit_id2, repo,
2037 a->progress_cb, a->progress_arg);
2038 if (status == GOT_STATUS_DELETE) {
2039 err = update_blob_fileindex_entry(a->worktree,
2040 a->fileindex, ie, ondisk_path, ie->path,
2041 blob2, 0);
2042 if (err)
2043 goto done;
2045 } else {
2046 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2047 err = install_blob(a->worktree, ondisk_path, path2,
2048 /* XXX get this from parent tree! */
2049 GOT_DEFAULT_FILE_MODE,
2050 sb.st_mode, blob2, 0, 0, repo,
2051 a->progress_cb, a->progress_arg);
2052 if (err)
2053 goto done;
2054 err = got_fileindex_entry_alloc(&ie,
2055 ondisk_path, path2, NULL, NULL);
2056 if (err)
2057 goto done;
2058 err = got_fileindex_entry_add(a->fileindex, ie);
2059 if (err) {
2060 got_fileindex_entry_free(ie);
2061 goto done;
2065 done:
2066 free(ondisk_path);
2067 return err;
2070 struct check_merge_ok_arg {
2071 struct got_worktree *worktree;
2072 struct got_repository *repo;
2075 static const struct got_error *
2076 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
2078 const struct got_error *err = NULL;
2079 struct check_merge_ok_arg *a = arg;
2080 unsigned char status;
2081 struct stat sb;
2082 char *ondisk_path;
2084 /* Reject merges into a work tree with mixed base commits. */
2085 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
2086 SHA1_DIGEST_LENGTH))
2087 return got_error(GOT_ERR_MIXED_COMMITS);
2089 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
2090 == -1)
2091 return got_error_from_errno("asprintf");
2093 /* Reject merges into a work tree with conflicted files. */
2094 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
2095 if (err)
2096 return err;
2097 if (status == GOT_STATUS_CONFLICT)
2098 return got_error(GOT_ERR_CONFLICTS);
2100 return NULL;
2103 static const struct got_error *
2104 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
2105 const char *fileindex_path, struct got_object_id *commit_id1,
2106 struct got_object_id *commit_id2, struct got_repository *repo,
2107 got_worktree_checkout_cb progress_cb, void *progress_arg,
2108 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2110 const struct got_error *err = NULL, *sync_err;
2111 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2112 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2113 struct merge_file_cb_arg arg;
2115 if (commit_id1) {
2116 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2117 worktree->path_prefix);
2118 if (err)
2119 goto done;
2121 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2122 if (err)
2123 goto done;
2126 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2127 worktree->path_prefix);
2128 if (err)
2129 goto done;
2131 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2132 if (err)
2133 goto done;
2135 arg.worktree = worktree;
2136 arg.fileindex = fileindex;
2137 arg.progress_cb = progress_cb;
2138 arg.progress_arg = progress_arg;
2139 arg.cancel_cb = cancel_cb;
2140 arg.cancel_arg = cancel_arg;
2141 arg.commit_id2 = commit_id2;
2142 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg, 1);
2143 sync_err = sync_fileindex(fileindex, fileindex_path);
2144 if (sync_err && err == NULL)
2145 err = sync_err;
2146 done:
2147 if (tree1)
2148 got_object_tree_close(tree1);
2149 if (tree2)
2150 got_object_tree_close(tree2);
2151 return err;
2154 const struct got_error *
2155 got_worktree_merge_files(struct got_worktree *worktree,
2156 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2157 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2158 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2160 const struct got_error *err, *unlockerr;
2161 char *fileindex_path = NULL;
2162 struct got_fileindex *fileindex = NULL;
2163 struct check_merge_ok_arg mok_arg;
2165 err = lock_worktree(worktree, LOCK_EX);
2166 if (err)
2167 return err;
2169 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2170 if (err)
2171 goto done;
2173 mok_arg.worktree = worktree;
2174 mok_arg.repo = repo;
2175 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2176 &mok_arg);
2177 if (err)
2178 goto done;
2180 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2181 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2182 done:
2183 if (fileindex)
2184 got_fileindex_free(fileindex);
2185 free(fileindex_path);
2186 unlockerr = lock_worktree(worktree, LOCK_SH);
2187 if (unlockerr && err == NULL)
2188 err = unlockerr;
2189 return err;
2192 struct diff_dir_cb_arg {
2193 struct got_fileindex *fileindex;
2194 struct got_worktree *worktree;
2195 const char *status_path;
2196 size_t status_path_len;
2197 struct got_repository *repo;
2198 got_worktree_status_cb status_cb;
2199 void *status_arg;
2200 got_worktree_cancel_cb cancel_cb;
2201 void *cancel_arg;
2204 static const struct got_error *
2205 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2206 got_worktree_status_cb status_cb, void *status_arg,
2207 struct got_repository *repo)
2209 const struct got_error *err = NULL;
2210 unsigned char status = GOT_STATUS_NO_CHANGE;
2211 unsigned char staged_status = get_staged_status(ie);
2212 struct stat sb;
2213 struct got_object_id blob_id, commit_id, staged_blob_id;
2215 err = get_file_status(&status, &sb, ie, abspath, repo);
2216 if (err == NULL && (status != GOT_STATUS_NO_CHANGE ||
2217 staged_status != GOT_STATUS_NO_CHANGE)) {
2218 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2219 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2220 if (staged_status == GOT_STATUS_ADD ||
2221 staged_status == GOT_STATUS_MODIFY) {
2222 memcpy(staged_blob_id.sha1, ie->staged_blob_sha1,
2223 SHA1_DIGEST_LENGTH);
2224 err = (*status_cb)(status_arg, status, staged_status,
2225 ie->path, &blob_id, &staged_blob_id, &commit_id);
2226 } else
2227 err = (*status_cb)(status_arg, status, staged_status,
2228 ie->path, &blob_id, NULL, &commit_id);
2230 return err;
2233 static const struct got_error *
2234 status_old_new(void *arg, struct got_fileindex_entry *ie,
2235 struct dirent *de, const char *parent_path)
2237 const struct got_error *err = NULL;
2238 struct diff_dir_cb_arg *a = arg;
2239 char *abspath;
2241 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2242 return got_error(GOT_ERR_CANCELLED);
2244 if (got_path_cmp(parent_path, a->status_path,
2245 strlen(parent_path), a->status_path_len) != 0 &&
2246 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2247 return NULL;
2249 if (parent_path[0]) {
2250 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2251 parent_path, de->d_name) == -1)
2252 return got_error_from_errno("asprintf");
2253 } else {
2254 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2255 de->d_name) == -1)
2256 return got_error_from_errno("asprintf");
2259 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2260 a->repo);
2261 free(abspath);
2262 return err;
2265 static const struct got_error *
2266 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2268 struct diff_dir_cb_arg *a = arg;
2269 struct got_object_id blob_id, commit_id;
2270 unsigned char status;
2272 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2273 return got_error(GOT_ERR_CANCELLED);
2275 if (!got_path_is_child(ie->path, a->status_path, a->status_path_len))
2276 return NULL;
2278 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2279 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2280 if (got_fileindex_entry_has_file_on_disk(ie))
2281 status = GOT_STATUS_MISSING;
2282 else
2283 status = GOT_STATUS_DELETE;
2284 return (*a->status_cb)(a->status_arg, status, get_staged_status(ie),
2285 ie->path, &blob_id, NULL, &commit_id);
2288 static const struct got_error *
2289 status_new(void *arg, struct dirent *de, const char *parent_path)
2291 const struct got_error *err = NULL;
2292 struct diff_dir_cb_arg *a = arg;
2293 char *path = NULL;
2295 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2296 return got_error(GOT_ERR_CANCELLED);
2298 if (de->d_type == DT_DIR)
2299 return NULL;
2301 /* XXX ignore symlinks for now */
2302 if (de->d_type == DT_LNK)
2303 return NULL;
2305 if (parent_path[0]) {
2306 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2307 return got_error_from_errno("asprintf");
2308 } else {
2309 path = de->d_name;
2312 if (got_path_is_child(path, a->status_path, a->status_path_len))
2313 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED,
2314 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2315 if (parent_path[0])
2316 free(path);
2317 return err;
2320 static const struct got_error *
2321 report_single_file_status(const char *path, const char *ondisk_path,
2322 struct got_fileindex *fileindex, got_worktree_status_cb status_cb,
2323 void *status_arg, struct got_repository *repo)
2325 struct got_fileindex_entry *ie;
2326 struct stat sb;
2328 ie = got_fileindex_entry_get(fileindex, path, strlen(path));
2329 if (ie)
2330 return report_file_status(ie, ondisk_path, status_cb,
2331 status_arg, repo);
2333 if (lstat(ondisk_path, &sb) == -1) {
2334 if (errno != ENOENT)
2335 return got_error_from_errno2("lstat", ondisk_path);
2336 return NULL;
2339 if (S_ISREG(sb.st_mode))
2340 return (*status_cb)(status_arg, GOT_STATUS_UNVERSIONED,
2341 GOT_STATUS_NO_CHANGE, path, NULL, NULL, NULL);
2343 return NULL;
2346 static const struct got_error *
2347 worktree_status(struct got_worktree *worktree, const char *path,
2348 struct got_fileindex *fileindex, struct got_repository *repo,
2349 got_worktree_status_cb status_cb, void *status_arg,
2350 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2352 const struct got_error *err = NULL;
2353 DIR *workdir = NULL;
2354 struct got_fileindex_diff_dir_cb fdiff_cb;
2355 struct diff_dir_cb_arg arg;
2356 char *ondisk_path = NULL;
2358 if (asprintf(&ondisk_path, "%s%s%s",
2359 worktree->root_path, path[0] ? "/" : "", path) == -1)
2360 return got_error_from_errno("asprintf");
2362 workdir = opendir(ondisk_path);
2363 if (workdir == NULL) {
2364 if (errno != ENOTDIR && errno != ENOENT)
2365 err = got_error_from_errno2("opendir", ondisk_path);
2366 else
2367 err = report_single_file_status(path, ondisk_path,
2368 fileindex, status_cb, status_arg, repo);
2369 } else {
2370 fdiff_cb.diff_old_new = status_old_new;
2371 fdiff_cb.diff_old = status_old;
2372 fdiff_cb.diff_new = status_new;
2373 arg.fileindex = fileindex;
2374 arg.worktree = worktree;
2375 arg.status_path = path;
2376 arg.status_path_len = strlen(path);
2377 arg.repo = repo;
2378 arg.status_cb = status_cb;
2379 arg.status_arg = status_arg;
2380 arg.cancel_cb = cancel_cb;
2381 arg.cancel_arg = cancel_arg;
2382 err = got_fileindex_diff_dir(fileindex, workdir,
2383 worktree->root_path, path, repo, &fdiff_cb, &arg);
2386 if (workdir)
2387 closedir(workdir);
2388 free(ondisk_path);
2389 return err;
2392 const struct got_error *
2393 got_worktree_status(struct got_worktree *worktree,
2394 struct got_pathlist_head *paths, struct got_repository *repo,
2395 got_worktree_status_cb status_cb, void *status_arg,
2396 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2398 const struct got_error *err = NULL;
2399 char *fileindex_path = NULL;
2400 struct got_fileindex *fileindex = NULL;
2401 struct got_pathlist_entry *pe;
2403 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2404 if (err)
2405 return err;
2407 TAILQ_FOREACH(pe, paths, entry) {
2408 err = worktree_status(worktree, pe->path, fileindex, repo,
2409 status_cb, status_arg, cancel_cb, cancel_arg);
2410 if (err)
2411 break;
2413 free(fileindex_path);
2414 got_fileindex_free(fileindex);
2415 return err;
2418 const struct got_error *
2419 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2420 const char *arg)
2422 const struct got_error *err = NULL;
2423 char *resolved, *cwd = NULL, *path = NULL;
2424 size_t len;
2426 *wt_path = NULL;
2428 resolved = realpath(arg, NULL);
2429 if (resolved == NULL) {
2430 if (errno != ENOENT)
2431 return got_error_from_errno2("realpath", arg);
2432 cwd = getcwd(NULL, 0);
2433 if (cwd == NULL)
2434 return got_error_from_errno("getcwd");
2435 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2436 err = got_error_from_errno("asprintf");
2437 goto done;
2441 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2442 strlen(got_worktree_get_root_path(worktree)))) {
2443 err = got_error(GOT_ERR_BAD_PATH);
2444 goto done;
2447 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2448 err = got_path_skip_common_ancestor(&path,
2449 got_worktree_get_root_path(worktree), resolved);
2450 if (err)
2451 goto done;
2452 } else {
2453 path = strdup("");
2454 if (path == NULL) {
2455 err = got_error_from_errno("strdup");
2456 goto done;
2460 /* XXX status walk can't deal with trailing slash! */
2461 len = strlen(path);
2462 while (len > 0 && path[len - 1] == '/') {
2463 path[len - 1] = '\0';
2464 len--;
2466 done:
2467 free(resolved);
2468 free(cwd);
2469 if (err == NULL)
2470 *wt_path = path;
2471 else
2472 free(path);
2473 return err;
2476 static const struct got_error *
2477 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2478 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2479 struct got_repository *repo)
2481 const struct got_error *err = NULL;
2482 struct got_fileindex_entry *ie;
2484 /* Re-adding an existing entry is a no-op. */
2485 if (got_fileindex_entry_get(fileindex, relpath, strlen(relpath)))
2486 return NULL;
2488 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2489 if (err)
2490 return err;
2492 err = got_fileindex_entry_add(fileindex, ie);
2493 if (err) {
2494 got_fileindex_entry_free(ie);
2495 return err;
2498 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2501 const struct got_error *
2502 got_worktree_schedule_add(struct got_worktree *worktree,
2503 struct got_pathlist_head *ondisk_paths,
2504 got_worktree_status_cb status_cb, void *status_arg,
2505 struct got_repository *repo)
2507 struct got_fileindex *fileindex = NULL;
2508 char *fileindex_path = NULL;
2509 const struct got_error *err = NULL, *sync_err, *unlockerr;
2510 struct got_pathlist_entry *pe;
2512 err = lock_worktree(worktree, LOCK_EX);
2513 if (err)
2514 return err;
2516 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2517 if (err)
2518 goto done;
2520 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2521 char *relpath;
2522 err = got_path_skip_common_ancestor(&relpath,
2523 got_worktree_get_root_path(worktree), pe->path);
2524 if (err)
2525 break;
2526 err = schedule_addition(pe->path, fileindex, relpath,
2527 status_cb, status_arg, repo);
2528 free(relpath);
2529 if (err)
2530 break;
2532 sync_err = sync_fileindex(fileindex, fileindex_path);
2533 if (sync_err && err == NULL)
2534 err = sync_err;
2535 done:
2536 free(fileindex_path);
2537 if (fileindex)
2538 got_fileindex_free(fileindex);
2539 unlockerr = lock_worktree(worktree, LOCK_SH);
2540 if (unlockerr && err == NULL)
2541 err = unlockerr;
2542 return err;
2545 static const struct got_error *
2546 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2547 const char *relpath, int delete_local_mods,
2548 got_worktree_status_cb status_cb, void *status_arg,
2549 struct got_repository *repo)
2551 const struct got_error *err = NULL;
2552 struct got_fileindex_entry *ie = NULL;
2553 unsigned char status;
2554 struct stat sb;
2556 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2557 if (ie == NULL)
2558 return got_error(GOT_ERR_BAD_PATH);
2560 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2561 if (err)
2562 return err;
2564 if (status != GOT_STATUS_NO_CHANGE) {
2565 if (status == GOT_STATUS_DELETE)
2566 return NULL;
2567 if (status != GOT_STATUS_MODIFY)
2568 return got_error(GOT_ERR_FILE_STATUS);
2569 if (!delete_local_mods)
2570 return got_error(GOT_ERR_FILE_MODIFIED);
2573 if (unlink(ondisk_path) != 0)
2574 return got_error_from_errno2("unlink", ondisk_path);
2576 got_fileindex_entry_mark_deleted_from_disk(ie);
2577 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2580 const struct got_error *
2581 got_worktree_schedule_delete(struct got_worktree *worktree,
2582 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2583 got_worktree_status_cb status_cb, void *status_arg,
2584 struct got_repository *repo)
2586 struct got_fileindex *fileindex = NULL;
2587 char *fileindex_path = NULL;
2588 const struct got_error *err = NULL, *sync_err, *unlockerr;
2589 struct got_pathlist_entry *pe;
2591 err = lock_worktree(worktree, LOCK_EX);
2592 if (err)
2593 return err;
2595 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2596 if (err)
2597 goto done;
2599 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2600 char *relpath;
2601 err = got_path_skip_common_ancestor(&relpath,
2602 got_worktree_get_root_path(worktree), pe->path);
2603 if (err)
2604 break;
2605 err = schedule_for_deletion(pe->path, fileindex, relpath,
2606 delete_local_mods, status_cb, status_arg, repo);
2607 free(relpath);
2608 if (err)
2609 break;
2611 sync_err = sync_fileindex(fileindex, fileindex_path);
2612 if (sync_err && err == NULL)
2613 err = sync_err;
2614 done:
2615 free(fileindex_path);
2616 if (fileindex)
2617 got_fileindex_free(fileindex);
2618 unlockerr = lock_worktree(worktree, LOCK_SH);
2619 if (unlockerr && err == NULL)
2620 err = unlockerr;
2621 return err;
2624 static const struct got_error *
2625 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2626 const char *ondisk_path,
2627 got_worktree_checkout_cb progress_cb, void *progress_arg,
2628 struct got_repository *repo)
2630 const struct got_error *err = NULL;
2631 char *relpath = NULL, *parent_path = NULL;
2632 struct got_fileindex_entry *ie;
2633 struct got_tree_object *tree = NULL;
2634 struct got_object_id *tree_id = NULL;
2635 const struct got_tree_entry *te;
2636 char *tree_path = NULL, *te_name;
2637 struct got_blob_object *blob = NULL;
2638 unsigned char status;
2639 struct stat sb;
2641 err = got_path_skip_common_ancestor(&relpath,
2642 got_worktree_get_root_path(worktree), ondisk_path);
2643 if (err)
2644 goto done;
2646 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
2647 if (ie == NULL) {
2648 err = got_error(GOT_ERR_BAD_PATH);
2649 goto done;
2652 /* Construct in-repository path of tree which contains this blob. */
2653 err = got_path_dirname(&parent_path, ie->path);
2654 if (err) {
2655 if (err->code != GOT_ERR_BAD_PATH)
2656 goto done;
2657 parent_path = strdup("/");
2658 if (parent_path == NULL) {
2659 err = got_error_from_errno("strdup");
2660 goto done;
2663 if (got_path_is_root_dir(worktree->path_prefix)) {
2664 tree_path = strdup(parent_path);
2665 if (tree_path == NULL) {
2666 err = got_error_from_errno("strdup");
2667 goto done;
2669 } else {
2670 if (got_path_is_root_dir(parent_path)) {
2671 tree_path = strdup(worktree->path_prefix);
2672 if (tree_path == NULL) {
2673 err = got_error_from_errno("strdup");
2674 goto done;
2676 } else {
2677 if (asprintf(&tree_path, "%s/%s",
2678 worktree->path_prefix, parent_path) == -1) {
2679 err = got_error_from_errno("asprintf");
2680 goto done;
2685 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2686 if (err)
2687 goto done;
2688 if (status == GOT_STATUS_MISSING || status == GOT_STATUS_DELETE)
2689 sb.st_mode = got_fileindex_perms_to_st(ie);
2691 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2692 tree_path);
2693 if (err) {
2694 if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
2695 status == GOT_STATUS_ADD))
2696 goto done;
2697 } else {
2698 err = got_object_open_as_tree(&tree, repo, tree_id);
2699 if (err)
2700 goto done;
2702 te_name = basename(ie->path);
2703 if (te_name == NULL) {
2704 err = got_error_from_errno2("basename", ie->path);
2705 goto done;
2708 te = got_object_tree_find_entry(tree, te_name);
2709 if (te == NULL && status != GOT_STATUS_ADD) {
2710 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2711 goto done;
2715 switch (status) {
2716 case GOT_STATUS_ADD:
2717 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2718 if (err)
2719 goto done;
2720 got_fileindex_entry_remove(fileindex, ie);
2721 break;
2722 case GOT_STATUS_DELETE:
2723 case GOT_STATUS_MODIFY:
2724 case GOT_STATUS_CONFLICT:
2725 case GOT_STATUS_MISSING: {
2726 struct got_object_id id;
2727 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2728 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2729 if (err)
2730 goto done;
2731 err = install_blob(worktree, ondisk_path, ie->path,
2732 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2733 progress_arg);
2734 if (err)
2735 goto done;
2736 if (status == GOT_STATUS_DELETE) {
2737 err = update_blob_fileindex_entry(worktree,
2738 fileindex, ie, ondisk_path, ie->path, blob, 1);
2739 if (err)
2740 goto done;
2742 break;
2744 default:
2745 goto done;
2747 done:
2748 free(relpath);
2749 free(parent_path);
2750 free(tree_path);
2751 if (blob)
2752 got_object_blob_close(blob);
2753 if (tree)
2754 got_object_tree_close(tree);
2755 free(tree_id);
2756 return err;
2759 const struct got_error *
2760 got_worktree_revert(struct got_worktree *worktree,
2761 struct got_pathlist_head *ondisk_paths,
2762 got_worktree_checkout_cb progress_cb, void *progress_arg,
2763 struct got_repository *repo)
2765 struct got_fileindex *fileindex = NULL;
2766 char *fileindex_path = NULL;
2767 const struct got_error *err = NULL, *unlockerr = NULL;
2768 const struct got_error *sync_err = NULL;
2769 struct got_pathlist_entry *pe;
2771 err = lock_worktree(worktree, LOCK_EX);
2772 if (err)
2773 return err;
2775 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2776 if (err)
2777 goto done;
2779 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2780 err = revert_file(worktree, fileindex, pe->path,
2781 progress_cb, progress_arg, repo);
2782 if (err)
2783 break;
2785 sync_err = sync_fileindex(fileindex, fileindex_path);
2786 if (sync_err && err == NULL)
2787 err = sync_err;
2788 done:
2789 free(fileindex_path);
2790 if (fileindex)
2791 got_fileindex_free(fileindex);
2792 unlockerr = lock_worktree(worktree, LOCK_SH);
2793 if (unlockerr && err == NULL)
2794 err = unlockerr;
2795 return err;
2798 static void
2799 free_commitable(struct got_commitable *ct)
2801 free(ct->path);
2802 free(ct->in_repo_path);
2803 free(ct->ondisk_path);
2804 free(ct->blob_id);
2805 free(ct->base_blob_id);
2806 free(ct->base_commit_id);
2807 free(ct);
2810 struct collect_commitables_arg {
2811 struct got_pathlist_head *commitable_paths;
2812 struct got_repository *repo;
2813 struct got_worktree *worktree;
2816 static const struct got_error *
2817 collect_commitables(void *arg, unsigned char status,
2818 unsigned char staged_status, const char *relpath,
2819 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
2820 struct got_object_id *commit_id)
2822 struct collect_commitables_arg *a = arg;
2823 const struct got_error *err = NULL;
2824 struct got_commitable *ct = NULL;
2825 struct got_pathlist_entry *new = NULL;
2826 char *parent_path = NULL, *path = NULL;
2827 struct stat sb;
2829 if (status == GOT_STATUS_CONFLICT)
2830 return got_error(GOT_ERR_COMMIT_CONFLICT);
2832 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2833 status != GOT_STATUS_DELETE)
2834 return NULL;
2836 if (asprintf(&path, "/%s", relpath) == -1) {
2837 err = got_error_from_errno("asprintf");
2838 goto done;
2840 if (strcmp(path, "/") == 0) {
2841 parent_path = strdup("");
2842 if (parent_path == NULL)
2843 return got_error_from_errno("strdup");
2844 } else {
2845 err = got_path_dirname(&parent_path, path);
2846 if (err)
2847 return err;
2850 ct = calloc(1, sizeof(*ct));
2851 if (ct == NULL) {
2852 err = got_error_from_errno("calloc");
2853 goto done;
2856 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2857 relpath) == -1) {
2858 err = got_error_from_errno("asprintf");
2859 goto done;
2861 if (status == GOT_STATUS_DELETE) {
2862 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2863 } else {
2864 if (lstat(ct->ondisk_path, &sb) != 0) {
2865 err = got_error_from_errno2("lstat", ct->ondisk_path);
2866 goto done;
2868 ct->mode = sb.st_mode;
2871 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2872 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2873 relpath) == -1) {
2874 err = got_error_from_errno("asprintf");
2875 goto done;
2878 ct->status = status;
2879 ct->blob_id = NULL; /* will be filled in when blob gets created */
2880 if (ct->status != GOT_STATUS_ADD) {
2881 ct->base_blob_id = got_object_id_dup(blob_id);
2882 if (ct->base_blob_id == NULL) {
2883 err = got_error_from_errno("got_object_id_dup");
2884 goto done;
2886 ct->base_commit_id = got_object_id_dup(commit_id);
2887 if (ct->base_commit_id == NULL) {
2888 err = got_error_from_errno("got_object_id_dup");
2889 goto done;
2892 ct->path = strdup(path);
2893 if (ct->path == NULL) {
2894 err = got_error_from_errno("strdup");
2895 goto done;
2897 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2898 done:
2899 if (ct && (err || new == NULL))
2900 free_commitable(ct);
2901 free(parent_path);
2902 free(path);
2903 return err;
2906 static const struct got_error *write_tree(struct got_object_id **,
2907 struct got_tree_object *, const char *, struct got_pathlist_head *,
2908 got_worktree_status_cb status_cb, void *status_arg,
2909 struct got_repository *);
2911 static const struct got_error *
2912 write_subtree(struct got_object_id **new_subtree_id,
2913 struct got_tree_entry *te, const char *parent_path,
2914 struct got_pathlist_head *commitable_paths,
2915 got_worktree_status_cb status_cb, void *status_arg,
2916 struct got_repository *repo)
2918 const struct got_error *err = NULL;
2919 struct got_tree_object *subtree;
2920 char *subpath;
2922 if (asprintf(&subpath, "%s%s%s", parent_path,
2923 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2924 return got_error_from_errno("asprintf");
2926 err = got_object_open_as_tree(&subtree, repo, te->id);
2927 if (err)
2928 return err;
2930 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2931 status_cb, status_arg, repo);
2932 got_object_tree_close(subtree);
2933 free(subpath);
2934 return err;
2937 static const struct got_error *
2938 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2940 const struct got_error *err = NULL;
2941 char *ct_parent_path = NULL;
2943 *match = 0;
2945 if (strchr(ct->in_repo_path, '/') == NULL) {
2946 *match = got_path_is_root_dir(path);
2947 return NULL;
2950 err = got_path_dirname(&ct_parent_path, ct->in_repo_path);
2951 if (err)
2952 return err;
2953 *match = (strcmp(path, ct_parent_path) == 0);
2954 free(ct_parent_path);
2955 return err;
2958 static mode_t
2959 get_ct_file_mode(struct got_commitable *ct)
2961 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2964 static const struct got_error *
2965 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2966 struct got_tree_entry *te, struct got_commitable *ct)
2968 const struct got_error *err = NULL;
2970 *new_te = NULL;
2972 err = got_object_tree_entry_dup(new_te, te);
2973 if (err)
2974 goto done;
2976 (*new_te)->mode = get_ct_file_mode(ct);
2978 free((*new_te)->id);
2979 (*new_te)->id = got_object_id_dup(ct->blob_id);
2980 if ((*new_te)->id == NULL) {
2981 err = got_error_from_errno("got_object_id_dup");
2982 goto done;
2984 done:
2985 if (err && *new_te) {
2986 got_object_tree_entry_close(*new_te);
2987 *new_te = NULL;
2989 return err;
2992 static const struct got_error *
2993 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2994 struct got_commitable *ct)
2996 const struct got_error *err = NULL;
2997 char *ct_name;
2999 *new_te = NULL;
3001 *new_te = calloc(1, sizeof(**new_te));
3002 if (*new_te == NULL)
3003 return got_error_from_errno("calloc");
3005 ct_name = basename(ct->path);
3006 if (ct_name == NULL) {
3007 err = got_error_from_errno2("basename", ct->path);
3008 goto done;
3010 (*new_te)->name = strdup(ct_name);
3011 if ((*new_te)->name == NULL) {
3012 err = got_error_from_errno("strdup");
3013 goto done;
3016 (*new_te)->mode = get_ct_file_mode(ct);
3018 (*new_te)->id = got_object_id_dup(ct->blob_id);
3019 if ((*new_te)->id == NULL) {
3020 err = got_error_from_errno("got_object_id_dup");
3021 goto done;
3023 done:
3024 if (err && *new_te) {
3025 got_object_tree_entry_close(*new_te);
3026 *new_te = NULL;
3028 return err;
3031 static const struct got_error *
3032 insert_tree_entry(struct got_tree_entry *new_te,
3033 struct got_pathlist_head *paths)
3035 const struct got_error *err = NULL;
3036 struct got_pathlist_entry *new_pe;
3038 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
3039 if (err)
3040 return err;
3041 if (new_pe == NULL)
3042 return got_error(GOT_ERR_TREE_DUP_ENTRY);
3043 return NULL;
3046 static const struct got_error *
3047 report_ct_status(struct got_commitable *ct,
3048 got_worktree_status_cb status_cb, void *status_arg)
3050 const char *ct_path = ct->path;
3051 while (ct_path[0] == '/')
3052 ct_path++;
3053 return (*status_cb)(status_arg, ct->status, GOT_STATUS_NO_CHANGE,
3054 ct_path, ct->blob_id, NULL, NULL);
3057 static const struct got_error *
3058 match_modified_subtree(int *modified, struct got_tree_entry *te,
3059 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
3061 const struct got_error *err = NULL;
3062 struct got_pathlist_entry *pe;
3063 char *te_path;
3065 *modified = 0;
3067 if (asprintf(&te_path, "%s%s%s", base_tree_path,
3068 got_path_is_root_dir(base_tree_path) ? "" : "/",
3069 te->name) == -1)
3070 return got_error_from_errno("asprintf");
3072 TAILQ_FOREACH(pe, commitable_paths, entry) {
3073 struct got_commitable *ct = pe->data;
3074 *modified = got_path_is_child(ct->in_repo_path, te_path,
3075 strlen(te_path));
3076 if (*modified)
3077 break;
3080 free(te_path);
3081 return err;
3084 static const struct got_error *
3085 match_deleted_or_modified_ct(struct got_commitable **ctp,
3086 struct got_tree_entry *te, const char *base_tree_path,
3087 struct got_pathlist_head *commitable_paths)
3089 const struct got_error *err = NULL;
3090 struct got_pathlist_entry *pe;
3092 *ctp = NULL;
3094 TAILQ_FOREACH(pe, commitable_paths, entry) {
3095 struct got_commitable *ct = pe->data;
3096 char *ct_name = NULL;
3097 int path_matches;
3099 if (ct->status != GOT_STATUS_MODIFY &&
3100 ct->status != GOT_STATUS_DELETE)
3101 continue;
3103 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
3104 continue;
3106 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
3107 if (err)
3108 return err;
3109 if (!path_matches)
3110 continue;
3112 ct_name = basename(pe->path);
3113 if (ct_name == NULL)
3114 return got_error_from_errno2("basename", pe->path);
3116 if (strcmp(te->name, ct_name) != 0)
3117 continue;
3119 *ctp = ct;
3120 break;
3123 return err;
3126 static const struct got_error *
3127 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
3128 const char *child_path, const char *path_base_tree,
3129 struct got_pathlist_head *commitable_paths,
3130 got_worktree_status_cb status_cb, void *status_arg,
3131 struct got_repository *repo)
3133 const struct got_error *err = NULL;
3134 struct got_tree_entry *new_te;
3135 char *subtree_path;
3137 *new_tep = NULL;
3139 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
3140 got_path_is_root_dir(path_base_tree) ? "" : "/",
3141 child_path) == -1)
3142 return got_error_from_errno("asprintf");
3144 new_te = calloc(1, sizeof(*new_te));
3145 new_te->mode = S_IFDIR;
3146 new_te->name = strdup(child_path);
3147 if (new_te->name == NULL) {
3148 err = got_error_from_errno("strdup");
3149 got_object_tree_entry_close(new_te);
3150 goto done;
3152 err = write_tree(&new_te->id, NULL, subtree_path,
3153 commitable_paths, status_cb, status_arg, repo);
3154 if (err) {
3155 got_object_tree_entry_close(new_te);
3156 goto done;
3158 done:
3159 free(subtree_path);
3160 if (err == NULL)
3161 *new_tep = new_te;
3162 return err;
3165 static const struct got_error *
3166 write_tree(struct got_object_id **new_tree_id,
3167 struct got_tree_object *base_tree, const char *path_base_tree,
3168 struct got_pathlist_head *commitable_paths,
3169 got_worktree_status_cb status_cb, void *status_arg,
3170 struct got_repository *repo)
3172 const struct got_error *err = NULL;
3173 const struct got_tree_entries *base_entries = NULL;
3174 struct got_pathlist_head paths;
3175 struct got_tree_entries new_tree_entries;
3176 struct got_tree_entry *te, *new_te = NULL;
3177 struct got_pathlist_entry *pe;
3179 TAILQ_INIT(&paths);
3180 new_tree_entries.nentries = 0;
3181 SIMPLEQ_INIT(&new_tree_entries.head);
3183 /* Insert, and recurse into, newly added entries first. */
3184 TAILQ_FOREACH(pe, commitable_paths, entry) {
3185 struct got_commitable *ct = pe->data;
3186 char *child_path = NULL, *slash;
3188 if (ct->status != GOT_STATUS_ADD ||
3189 (ct->flags & GOT_COMMITABLE_ADDED))
3190 continue;
3192 if (!got_path_is_child(pe->path, path_base_tree,
3193 strlen(path_base_tree)))
3194 continue;
3196 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3197 pe->path);
3198 if (err)
3199 goto done;
3201 slash = strchr(child_path, '/');
3202 if (slash == NULL) {
3203 err = alloc_added_blob_tree_entry(&new_te, ct);
3204 if (err)
3205 goto done;
3206 err = report_ct_status(ct, status_cb, status_arg);
3207 if (err)
3208 goto done;
3209 ct->flags |= GOT_COMMITABLE_ADDED;
3210 err = insert_tree_entry(new_te, &paths);
3211 if (err)
3212 goto done;
3213 } else {
3214 *slash = '\0'; /* trim trailing path components */
3215 if (base_tree == NULL ||
3216 got_object_tree_find_entry(base_tree, child_path)
3217 == NULL) {
3218 err = make_subtree_for_added_blob(&new_te,
3219 child_path, path_base_tree,
3220 commitable_paths, status_cb, status_arg,
3221 repo);
3222 if (err)
3223 goto done;
3224 err = insert_tree_entry(new_te, &paths);
3225 if (err)
3226 goto done;
3231 if (base_tree) {
3232 /* Handle modified and deleted entries. */
3233 base_entries = got_object_tree_get_entries(base_tree);
3234 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3235 struct got_commitable *ct = NULL;
3237 if (S_ISDIR(te->mode)) {
3238 int modified;
3239 err = got_object_tree_entry_dup(&new_te, te);
3240 if (err)
3241 goto done;
3242 err = match_modified_subtree(&modified, te,
3243 path_base_tree, commitable_paths);
3244 if (err)
3245 goto done;
3246 /* Avoid recursion into unmodified subtrees. */
3247 if (modified) {
3248 free(new_te->id);
3249 err = write_subtree(&new_te->id, te,
3250 path_base_tree, commitable_paths,
3251 status_cb, status_arg, repo);
3252 if (err)
3253 goto done;
3255 err = insert_tree_entry(new_te, &paths);
3256 if (err)
3257 goto done;
3258 continue;
3261 err = match_deleted_or_modified_ct(&ct, te,
3262 path_base_tree, commitable_paths);
3263 if (ct) {
3264 /* NB: Deleted entries get dropped here. */
3265 if (ct->status == GOT_STATUS_MODIFY) {
3266 err = alloc_modified_blob_tree_entry(
3267 &new_te, te, ct);
3268 if (err)
3269 goto done;
3270 err = insert_tree_entry(new_te, &paths);
3271 if (err)
3272 goto done;
3274 err = report_ct_status(ct, status_cb,
3275 status_arg);
3276 if (err)
3277 goto done;
3278 } else {
3279 /* Entry is unchanged; just copy it. */
3280 err = got_object_tree_entry_dup(&new_te, te);
3281 if (err)
3282 goto done;
3283 err = insert_tree_entry(new_te, &paths);
3284 if (err)
3285 goto done;
3290 /* Write new list of entries; deleted entries have been dropped. */
3291 TAILQ_FOREACH(pe, &paths, entry) {
3292 struct got_tree_entry *te = pe->data;
3293 new_tree_entries.nentries++;
3294 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3296 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3297 done:
3298 got_object_tree_entries_close(&new_tree_entries);
3299 got_pathlist_free(&paths);
3300 return err;
3303 static const struct got_error *
3304 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3305 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3307 const struct got_error *err = NULL;
3308 struct got_pathlist_entry *pe;
3310 TAILQ_FOREACH(pe, commitable_paths, entry) {
3311 struct got_fileindex_entry *ie;
3312 struct got_commitable *ct = pe->data;
3314 ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
3315 if (ie) {
3316 if (ct->status == GOT_STATUS_DELETE) {
3317 got_fileindex_entry_remove(fileindex, ie);
3318 got_fileindex_entry_free(ie);
3319 } else
3320 err = got_fileindex_entry_update(ie,
3321 ct->ondisk_path, ct->blob_id->sha1,
3322 new_base_commit_id->sha1, 1);
3323 } else {
3324 err = got_fileindex_entry_alloc(&ie,
3325 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3326 new_base_commit_id->sha1);
3327 if (err)
3328 break;
3329 err = got_fileindex_entry_add(fileindex, ie);
3330 if (err)
3331 break;
3334 return err;
3337 static const struct got_error *
3338 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3339 struct got_object_id *head_commit_id)
3341 const struct got_error *err = NULL;
3342 struct got_object_id *id = NULL;
3343 struct got_commit_object *commit = NULL;
3344 const char *ct_path = ct->in_repo_path;
3346 while (ct_path[0] == '/')
3347 ct_path++;
3349 if (ct->status != GOT_STATUS_ADD) {
3350 /* Trivial case: base commit == head commit */
3351 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3352 return NULL;
3354 * Ensure file content which local changes were based
3355 * on matches file content in the branch head.
3357 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3358 if (err) {
3359 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3360 goto done;
3361 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3362 goto done;
3363 } else if (got_object_id_cmp(id, ct->base_blob_id) != 0)
3364 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3365 } else {
3366 /* Require that added files don't exist in the branch head. */
3367 err = got_object_id_by_path(&id, repo, head_commit_id, ct_path);
3368 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3369 goto done;
3370 err = id ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3372 done:
3373 if (commit)
3374 got_object_commit_close(commit);
3375 free(id);
3376 return err;
3379 const struct got_error *
3380 commit_worktree(struct got_object_id **new_commit_id,
3381 struct got_pathlist_head *commitable_paths,
3382 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3383 const char *author, const char *committer,
3384 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3385 got_worktree_status_cb status_cb, void *status_arg,
3386 struct got_repository *repo)
3388 const struct got_error *err = NULL, *unlockerr = NULL;
3389 struct got_pathlist_entry *pe;
3390 const char *head_ref_name = NULL;
3391 struct got_commit_object *head_commit = NULL;
3392 struct got_reference *head_ref2 = NULL;
3393 struct got_object_id *head_commit_id2 = NULL;
3394 struct got_tree_object *head_tree = NULL;
3395 struct got_object_id *new_tree_id = NULL;
3396 struct got_object_id_queue parent_ids;
3397 struct got_object_qid *pid = NULL;
3398 char *logmsg = NULL;
3400 *new_commit_id = NULL;
3402 SIMPLEQ_INIT(&parent_ids);
3404 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3405 if (err)
3406 goto done;
3408 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3409 if (err)
3410 goto done;
3412 if (commit_msg_cb != NULL) {
3413 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3414 if (err)
3415 goto done;
3418 if (logmsg == NULL || strlen(logmsg) == 0) {
3419 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3420 goto done;
3423 /* Create blobs from added and modified files and record their IDs. */
3424 TAILQ_FOREACH(pe, commitable_paths, entry) {
3425 struct got_commitable *ct = pe->data;
3426 char *ondisk_path;
3428 if (ct->status != GOT_STATUS_ADD &&
3429 ct->status != GOT_STATUS_MODIFY)
3430 continue;
3432 if (asprintf(&ondisk_path, "%s/%s",
3433 worktree->root_path, pe->path) == -1) {
3434 err = got_error_from_errno("asprintf");
3435 goto done;
3437 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3438 free(ondisk_path);
3439 if (err)
3440 goto done;
3443 /* Recursively write new tree objects. */
3444 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3445 status_cb, status_arg, repo);
3446 if (err)
3447 goto done;
3449 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3450 if (err)
3451 goto done;
3452 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3453 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3454 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3455 got_object_qid_free(pid);
3456 if (logmsg != NULL)
3457 free(logmsg);
3458 if (err)
3459 goto done;
3461 /* Check if a concurrent commit to our branch has occurred. */
3462 head_ref_name = got_worktree_get_head_ref_name(worktree);
3463 if (head_ref_name == NULL) {
3464 err = got_error_from_errno("got_worktree_get_head_ref_name");
3465 goto done;
3467 /* Lock the reference here to prevent concurrent modification. */
3468 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3469 if (err)
3470 goto done;
3471 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3472 if (err)
3473 goto done;
3474 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3475 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3476 goto done;
3478 /* Update branch head in repository. */
3479 err = got_ref_change_ref(head_ref2, *new_commit_id);
3480 if (err)
3481 goto done;
3482 err = got_ref_write(head_ref2, repo);
3483 if (err)
3484 goto done;
3486 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3487 if (err)
3488 goto done;
3490 err = ref_base_commit(worktree, repo);
3491 if (err)
3492 goto done;
3493 done:
3494 if (head_tree)
3495 got_object_tree_close(head_tree);
3496 if (head_commit)
3497 got_object_commit_close(head_commit);
3498 free(head_commit_id2);
3499 if (head_ref2) {
3500 unlockerr = got_ref_unlock(head_ref2);
3501 if (unlockerr && err == NULL)
3502 err = unlockerr;
3503 got_ref_close(head_ref2);
3505 return err;
3508 static const struct got_error *
3509 check_path_is_commitable(const char *path,
3510 struct got_pathlist_head *commitable_paths)
3512 struct got_pathlist_entry *cpe = NULL;
3513 size_t path_len = strlen(path);
3515 TAILQ_FOREACH(cpe, commitable_paths, entry) {
3516 struct got_commitable *ct = cpe->data;
3517 const char *ct_path = ct->path;
3519 while (ct_path[0] == '/')
3520 ct_path++;
3522 if (strcmp(path, ct_path) == 0 ||
3523 got_path_is_child(ct_path, path, path_len))
3524 break;
3527 if (cpe == NULL)
3528 return got_error_path(path, GOT_ERR_BAD_PATH);
3530 return NULL;
3533 const struct got_error *
3534 got_worktree_commit(struct got_object_id **new_commit_id,
3535 struct got_worktree *worktree, struct got_pathlist_head *paths,
3536 const char *author, const char *committer,
3537 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3538 got_worktree_status_cb status_cb, void *status_arg,
3539 struct got_repository *repo)
3541 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3542 struct got_fileindex *fileindex = NULL;
3543 char *fileindex_path = NULL;
3544 struct got_pathlist_head commitable_paths;
3545 struct collect_commitables_arg cc_arg;
3546 struct got_pathlist_entry *pe;
3547 struct got_reference *head_ref = NULL;
3548 struct got_object_id *head_commit_id = NULL;
3550 *new_commit_id = NULL;
3552 TAILQ_INIT(&commitable_paths);
3554 err = lock_worktree(worktree, LOCK_EX);
3555 if (err)
3556 goto done;
3558 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3559 if (err)
3560 goto done;
3562 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3563 if (err)
3564 goto done;
3566 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3567 if (err)
3568 goto done;
3570 cc_arg.commitable_paths = &commitable_paths;
3571 cc_arg.worktree = worktree;
3572 cc_arg.repo = repo;
3573 TAILQ_FOREACH(pe, paths, entry) {
3574 err = worktree_status(worktree, pe->path, fileindex, repo,
3575 collect_commitables, &cc_arg, NULL, NULL);
3576 if (err)
3577 goto done;
3580 if (TAILQ_EMPTY(&commitable_paths)) {
3581 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3582 goto done;
3585 TAILQ_FOREACH(pe, paths, entry) {
3586 err = check_path_is_commitable(pe->path, &commitable_paths);
3587 if (err)
3588 goto done;
3591 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3592 struct got_commitable *ct = pe->data;
3593 err = check_ct_out_of_date(ct, repo, head_commit_id);
3594 if (err)
3595 goto done;
3598 err = commit_worktree(new_commit_id, &commitable_paths,
3599 head_commit_id, worktree, author, committer,
3600 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3601 if (err)
3602 goto done;
3604 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3605 fileindex);
3606 sync_err = sync_fileindex(fileindex, fileindex_path);
3607 if (sync_err && err == NULL)
3608 err = sync_err;
3609 done:
3610 if (fileindex)
3611 got_fileindex_free(fileindex);
3612 free(fileindex_path);
3613 unlockerr = lock_worktree(worktree, LOCK_SH);
3614 if (unlockerr && err == NULL)
3615 err = unlockerr;
3616 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3617 struct got_commitable *ct = pe->data;
3618 free_commitable(ct);
3620 got_pathlist_free(&commitable_paths);
3621 return err;
3624 const char *
3625 got_commitable_get_path(struct got_commitable *ct)
3627 return ct->path;
3630 unsigned int
3631 got_commitable_get_status(struct got_commitable *ct)
3633 return ct->status;
3636 struct check_rebase_ok_arg {
3637 struct got_worktree *worktree;
3638 struct got_repository *repo;
3641 static const struct got_error *
3642 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3644 const struct got_error *err = NULL;
3645 struct check_rebase_ok_arg *a = arg;
3646 unsigned char status;
3647 struct stat sb;
3648 char *ondisk_path;
3650 /* Reject rebase of a work tree with mixed base commits. */
3651 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3652 SHA1_DIGEST_LENGTH))
3653 return got_error(GOT_ERR_MIXED_COMMITS);
3655 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3656 == -1)
3657 return got_error_from_errno("asprintf");
3659 /* Reject rebase of a work tree with modified or conflicted files. */
3660 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3661 free(ondisk_path);
3662 if (err)
3663 return err;
3665 if (status != GOT_STATUS_NO_CHANGE)
3666 return got_error(GOT_ERR_MODIFIED);
3668 return NULL;
3671 const struct got_error *
3672 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3673 struct got_reference **tmp_branch, struct got_fileindex **fileindex,
3674 struct got_worktree *worktree, struct got_reference *branch,
3675 struct got_repository *repo)
3677 const struct got_error *err = NULL;
3678 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3679 char *branch_ref_name = NULL;
3680 char *fileindex_path = NULL;
3681 struct check_rebase_ok_arg ok_arg;
3682 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3684 *new_base_branch_ref = NULL;
3685 *tmp_branch = NULL;
3686 *fileindex = NULL;
3688 err = lock_worktree(worktree, LOCK_EX);
3689 if (err)
3690 return err;
3692 err = open_fileindex(fileindex, &fileindex_path, worktree);
3693 if (err)
3694 goto done;
3696 ok_arg.worktree = worktree;
3697 ok_arg.repo = repo;
3698 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
3699 &ok_arg);
3700 if (err)
3701 goto done;
3703 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3704 if (err)
3705 goto done;
3707 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3708 if (err)
3709 goto done;
3711 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3712 if (err)
3713 goto done;
3715 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3716 0);
3717 if (err)
3718 goto done;
3720 err = got_ref_alloc_symref(new_base_branch_ref,
3721 new_base_branch_ref_name, wt_branch);
3722 if (err)
3723 goto done;
3724 err = got_ref_write(*new_base_branch_ref, repo);
3725 if (err)
3726 goto done;
3728 /* TODO Lock original branch's ref while rebasing? */
3730 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3731 if (err)
3732 goto done;
3734 err = got_ref_write(branch_ref, repo);
3735 if (err)
3736 goto done;
3738 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3739 worktree->base_commit_id);
3740 if (err)
3741 goto done;
3742 err = got_ref_write(*tmp_branch, repo);
3743 if (err)
3744 goto done;
3746 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3747 if (err)
3748 goto done;
3749 done:
3750 free(fileindex_path);
3751 free(tmp_branch_name);
3752 free(new_base_branch_ref_name);
3753 free(branch_ref_name);
3754 if (branch_ref)
3755 got_ref_close(branch_ref);
3756 if (wt_branch)
3757 got_ref_close(wt_branch);
3758 if (err) {
3759 if (*new_base_branch_ref) {
3760 got_ref_close(*new_base_branch_ref);
3761 *new_base_branch_ref = NULL;
3763 if (*tmp_branch) {
3764 got_ref_close(*tmp_branch);
3765 *tmp_branch = NULL;
3767 if (*fileindex) {
3768 got_fileindex_free(*fileindex);
3769 *fileindex = NULL;
3771 lock_worktree(worktree, LOCK_SH);
3773 return err;
3776 const struct got_error *
3777 got_worktree_rebase_continue(struct got_object_id **commit_id,
3778 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3779 struct got_reference **branch, struct got_fileindex **fileindex,
3780 struct got_worktree *worktree, struct got_repository *repo)
3782 const struct got_error *err;
3783 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3784 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3785 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3786 char *fileindex_path = NULL;
3788 *commit_id = NULL;
3789 *new_base_branch = NULL;
3790 *tmp_branch = NULL;
3791 *branch = NULL;
3792 *fileindex = NULL;
3794 err = lock_worktree(worktree, LOCK_EX);
3795 if (err)
3796 return err;
3798 err = open_fileindex(fileindex, &fileindex_path, worktree);
3799 if (err)
3800 goto done;
3802 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3803 if (err)
3804 return err;
3806 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3807 if (err)
3808 goto done;
3810 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3811 if (err)
3812 goto done;
3814 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3815 if (err)
3816 goto done;
3818 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3819 if (err)
3820 goto done;
3822 err = got_ref_open(branch, repo,
3823 got_ref_get_symref_target(branch_ref), 0);
3824 if (err)
3825 goto done;
3827 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3828 if (err)
3829 goto done;
3831 err = got_ref_resolve(commit_id, repo, commit_ref);
3832 if (err)
3833 goto done;
3835 err = got_ref_open(new_base_branch, repo,
3836 new_base_branch_ref_name, 0);
3837 if (err)
3838 goto done;
3840 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3841 if (err)
3842 goto done;
3843 done:
3844 free(commit_ref_name);
3845 free(branch_ref_name);
3846 free(fileindex_path);
3847 if (commit_ref)
3848 got_ref_close(commit_ref);
3849 if (branch_ref)
3850 got_ref_close(branch_ref);
3851 if (err) {
3852 free(*commit_id);
3853 *commit_id = NULL;
3854 if (*tmp_branch) {
3855 got_ref_close(*tmp_branch);
3856 *tmp_branch = NULL;
3858 if (*new_base_branch) {
3859 got_ref_close(*new_base_branch);
3860 *new_base_branch = NULL;
3862 if (*branch) {
3863 got_ref_close(*branch);
3864 *branch = NULL;
3866 if (*fileindex) {
3867 got_fileindex_free(*fileindex);
3868 *fileindex = NULL;
3870 lock_worktree(worktree, LOCK_SH);
3872 return err;
3875 const struct got_error *
3876 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3878 const struct got_error *err;
3879 char *tmp_branch_name = NULL;
3881 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3882 if (err)
3883 return err;
3885 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3886 free(tmp_branch_name);
3887 return NULL;
3890 static const struct got_error *
3891 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3892 char **logmsg, void *arg)
3894 *logmsg = arg;
3895 return NULL;
3898 static const struct got_error *
3899 rebase_status(void *arg, unsigned char status, unsigned char staged_status,
3900 const char *path, struct got_object_id *blob_id,
3901 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3903 return NULL;
3906 struct collect_merged_paths_arg {
3907 got_worktree_checkout_cb progress_cb;
3908 void *progress_arg;
3909 struct got_pathlist_head *merged_paths;
3912 static const struct got_error *
3913 collect_merged_paths(void *arg, unsigned char status, const char *path)
3915 const struct got_error *err;
3916 struct collect_merged_paths_arg *a = arg;
3917 char *p;
3918 struct got_pathlist_entry *new;
3920 err = (*a->progress_cb)(a->progress_arg, status, path);
3921 if (err)
3922 return err;
3924 if (status != GOT_STATUS_MERGE &&
3925 status != GOT_STATUS_ADD &&
3926 status != GOT_STATUS_DELETE &&
3927 status != GOT_STATUS_CONFLICT)
3928 return NULL;
3930 p = strdup(path);
3931 if (p == NULL)
3932 return got_error_from_errno("strdup");
3934 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3935 if (err || new == NULL)
3936 free(p);
3937 return err;
3940 void
3941 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3943 struct got_pathlist_entry *pe;
3945 TAILQ_FOREACH(pe, merged_paths, entry)
3946 free((char *)pe->path);
3948 got_pathlist_free(merged_paths);
3951 static const struct got_error *
3952 store_commit_id(const char *commit_ref_name, struct got_object_id *commit_id,
3953 struct got_repository *repo)
3955 const struct got_error *err;
3956 struct got_reference *commit_ref = NULL;
3958 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3959 if (err) {
3960 if (err->code != GOT_ERR_NOT_REF)
3961 goto done;
3962 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3963 if (err)
3964 goto done;
3965 err = got_ref_write(commit_ref, repo);
3966 if (err)
3967 goto done;
3968 } else {
3969 struct got_object_id *stored_id;
3970 int cmp;
3972 err = got_ref_resolve(&stored_id, repo, commit_ref);
3973 if (err)
3974 goto done;
3975 cmp = got_object_id_cmp(commit_id, stored_id);
3976 free(stored_id);
3977 if (cmp != 0) {
3978 err = got_error(GOT_ERR_REBASE_COMMITID);
3979 goto done;
3982 done:
3983 if (commit_ref)
3984 got_ref_close(commit_ref);
3985 return err;
3988 static const struct got_error *
3989 rebase_merge_files(struct got_pathlist_head *merged_paths,
3990 const char *commit_ref_name, struct got_worktree *worktree,
3991 struct got_fileindex *fileindex, struct got_object_id *parent_commit_id,
3992 struct got_object_id *commit_id, struct got_repository *repo,
3993 got_worktree_checkout_cb progress_cb, void *progress_arg,
3994 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3996 const struct got_error *err;
3997 struct got_reference *commit_ref = NULL;
3998 struct collect_merged_paths_arg cmp_arg;
3999 char *fileindex_path;
4001 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4003 err = get_fileindex_path(&fileindex_path, worktree);
4004 if (err)
4005 return err;
4007 cmp_arg.progress_cb = progress_cb;
4008 cmp_arg.progress_arg = progress_arg;
4009 cmp_arg.merged_paths = merged_paths;
4010 err = merge_files(worktree, fileindex, fileindex_path,
4011 parent_commit_id, commit_id, repo, collect_merged_paths,
4012 &cmp_arg, cancel_cb, cancel_arg);
4013 if (commit_ref)
4014 got_ref_close(commit_ref);
4015 return err;
4018 const struct got_error *
4019 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
4020 struct got_worktree *worktree, struct got_fileindex *fileindex,
4021 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4022 struct got_repository *repo,
4023 got_worktree_checkout_cb progress_cb, void *progress_arg,
4024 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4026 const struct got_error *err;
4027 char *commit_ref_name;
4029 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4030 if (err)
4031 return err;
4033 err = store_commit_id(commit_ref_name, commit_id, repo);
4034 if (err)
4035 goto done;
4037 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4038 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4039 progress_arg, cancel_cb, cancel_arg);
4040 done:
4041 free(commit_ref_name);
4042 return err;
4045 const struct got_error *
4046 got_worktree_histedit_merge_files(struct got_pathlist_head *merged_paths,
4047 struct got_worktree *worktree, struct got_fileindex *fileindex,
4048 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
4049 struct got_repository *repo,
4050 got_worktree_checkout_cb progress_cb, void *progress_arg,
4051 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
4053 const struct got_error *err;
4054 char *commit_ref_name;
4056 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4057 if (err)
4058 return err;
4060 err = store_commit_id(commit_ref_name, commit_id, repo);
4061 if (err)
4062 goto done;
4064 err = rebase_merge_files(merged_paths, commit_ref_name, worktree,
4065 fileindex, parent_commit_id, commit_id, repo, progress_cb,
4066 progress_arg, cancel_cb, cancel_arg);
4067 done:
4068 free(commit_ref_name);
4069 return err;
4072 static const struct got_error *
4073 rebase_commit(struct got_object_id **new_commit_id,
4074 struct got_pathlist_head *merged_paths, struct got_reference *commit_ref,
4075 struct got_worktree *worktree, struct got_fileindex *fileindex,
4076 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
4077 const char *new_logmsg, struct got_repository *repo)
4079 const struct got_error *err, *sync_err;
4080 struct got_pathlist_head commitable_paths;
4081 struct collect_commitables_arg cc_arg;
4082 char *fileindex_path = NULL;
4083 struct got_reference *head_ref = NULL;
4084 struct got_object_id *head_commit_id = NULL;
4085 char *logmsg = NULL;
4087 TAILQ_INIT(&commitable_paths);
4088 *new_commit_id = NULL;
4090 /* Work tree is locked/unlocked during rebase preparation/teardown. */
4092 err = get_fileindex_path(&fileindex_path, worktree);
4093 if (err)
4094 return err;
4096 cc_arg.commitable_paths = &commitable_paths;
4097 cc_arg.worktree = worktree;
4098 cc_arg.repo = repo;
4100 * If possible get the status of individual files directly to
4101 * avoid crawling the entire work tree once per rebased commit.
4102 * TODO: Ideally, merged_paths would contain a list of commitables
4103 * we could use so we could skip worktree_status() entirely.
4105 if (merged_paths) {
4106 struct got_pathlist_entry *pe;
4107 if (TAILQ_EMPTY(merged_paths)) {
4108 err = got_error(GOT_ERR_NO_MERGED_PATHS);
4109 goto done;
4111 TAILQ_FOREACH(pe, merged_paths, entry) {
4112 err = worktree_status(worktree, pe->path, fileindex,
4113 repo, collect_commitables, &cc_arg, NULL, NULL);
4114 if (err)
4115 goto done;
4117 } else {
4118 err = worktree_status(worktree, "", fileindex, repo,
4119 collect_commitables, &cc_arg, NULL, NULL);
4120 if (err)
4121 goto done;
4124 if (TAILQ_EMPTY(&commitable_paths)) {
4125 /* No-op change; commit will be elided. */
4126 err = got_ref_delete(commit_ref, repo);
4127 if (err)
4128 goto done;
4129 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
4130 goto done;
4133 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
4134 if (err)
4135 goto done;
4137 err = got_ref_resolve(&head_commit_id, repo, head_ref);
4138 if (err)
4139 goto done;
4141 if (new_logmsg)
4142 logmsg = strdup(new_logmsg);
4143 else
4144 logmsg = strdup(got_object_commit_get_logmsg(orig_commit));
4145 if (logmsg == NULL)
4146 return got_error_from_errno("strdup");
4148 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
4149 worktree, got_object_commit_get_author(orig_commit),
4150 got_object_commit_get_committer(orig_commit),
4151 collect_rebase_commit_msg, logmsg, rebase_status, NULL, repo);
4152 if (err)
4153 goto done;
4155 err = got_ref_change_ref(tmp_branch, *new_commit_id);
4156 if (err)
4157 goto done;
4159 err = got_ref_delete(commit_ref, repo);
4160 if (err)
4161 goto done;
4163 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
4164 fileindex);
4165 sync_err = sync_fileindex(fileindex, fileindex_path);
4166 if (sync_err && err == NULL)
4167 err = sync_err;
4168 done:
4169 free(fileindex_path);
4170 free(head_commit_id);
4171 if (head_ref)
4172 got_ref_close(head_ref);
4173 if (err) {
4174 free(*new_commit_id);
4175 *new_commit_id = NULL;
4177 return err;
4180 const struct got_error *
4181 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
4182 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4183 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4184 struct got_commit_object *orig_commit,
4185 struct got_object_id *orig_commit_id, struct got_repository *repo)
4187 const struct got_error *err;
4188 char *commit_ref_name;
4189 struct got_reference *commit_ref = NULL;
4190 struct got_object_id *commit_id = NULL;
4192 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4193 if (err)
4194 return err;
4196 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4197 if (err)
4198 goto done;
4199 err = got_ref_resolve(&commit_id, repo, commit_ref);
4200 if (err)
4201 goto done;
4202 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4203 err = got_error(GOT_ERR_REBASE_COMMITID);
4204 goto done;
4207 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4208 worktree, fileindex, tmp_branch, orig_commit, NULL, repo);
4209 done:
4210 if (commit_ref)
4211 got_ref_close(commit_ref);
4212 free(commit_ref_name);
4213 free(commit_id);
4214 return err;
4217 const struct got_error *
4218 got_worktree_histedit_commit(struct got_object_id **new_commit_id,
4219 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
4220 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4221 struct got_commit_object *orig_commit,
4222 struct got_object_id *orig_commit_id, const char *new_logmsg,
4223 struct got_repository *repo)
4225 const struct got_error *err;
4226 char *commit_ref_name;
4227 struct got_reference *commit_ref = NULL;
4228 struct got_object_id *commit_id = NULL;
4230 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4231 if (err)
4232 return err;
4234 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4235 if (err)
4236 goto done;
4237 err = got_ref_resolve(&commit_id, repo, commit_ref);
4238 if (err)
4239 goto done;
4240 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
4241 err = got_error(GOT_ERR_HISTEDIT_COMMITID);
4242 goto done;
4245 err = rebase_commit(new_commit_id, merged_paths, commit_ref,
4246 worktree, fileindex, tmp_branch, orig_commit, new_logmsg, repo);
4247 done:
4248 if (commit_ref)
4249 got_ref_close(commit_ref);
4250 free(commit_ref_name);
4251 free(commit_id);
4252 return err;
4255 const struct got_error *
4256 got_worktree_rebase_postpone(struct got_worktree *worktree,
4257 struct got_fileindex *fileindex)
4259 if (fileindex)
4260 got_fileindex_free(fileindex);
4261 return lock_worktree(worktree, LOCK_SH);
4264 static const struct got_error *
4265 delete_ref(const char *name, struct got_repository *repo)
4267 const struct got_error *err;
4268 struct got_reference *ref;
4270 err = got_ref_open(&ref, repo, name, 0);
4271 if (err) {
4272 if (err->code == GOT_ERR_NOT_REF)
4273 return NULL;
4274 return err;
4277 err = got_ref_delete(ref, repo);
4278 got_ref_close(ref);
4279 return err;
4282 static const struct got_error *
4283 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4285 const struct got_error *err;
4286 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4287 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4289 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4290 if (err)
4291 goto done;
4292 err = delete_ref(tmp_branch_name, repo);
4293 if (err)
4294 goto done;
4296 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4297 if (err)
4298 goto done;
4299 err = delete_ref(new_base_branch_ref_name, repo);
4300 if (err)
4301 goto done;
4303 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4304 if (err)
4305 goto done;
4306 err = delete_ref(branch_ref_name, repo);
4307 if (err)
4308 goto done;
4310 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4311 if (err)
4312 goto done;
4313 err = delete_ref(commit_ref_name, repo);
4314 if (err)
4315 goto done;
4317 done:
4318 free(tmp_branch_name);
4319 free(new_base_branch_ref_name);
4320 free(branch_ref_name);
4321 free(commit_ref_name);
4322 return err;
4325 const struct got_error *
4326 got_worktree_rebase_complete(struct got_worktree *worktree,
4327 struct got_fileindex *fileindex, struct got_reference *new_base_branch,
4328 struct got_reference *tmp_branch, struct got_reference *rebased_branch,
4329 struct got_repository *repo)
4331 const struct got_error *err, *unlockerr;
4332 struct got_object_id *new_head_commit_id = NULL;
4334 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4335 if (err)
4336 return err;
4338 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4339 if (err)
4340 goto done;
4342 err = got_ref_write(rebased_branch, repo);
4343 if (err)
4344 goto done;
4346 err = got_worktree_set_head_ref(worktree, rebased_branch);
4347 if (err)
4348 goto done;
4350 err = delete_rebase_refs(worktree, repo);
4351 done:
4352 if (fileindex)
4353 got_fileindex_free(fileindex);
4354 free(new_head_commit_id);
4355 unlockerr = lock_worktree(worktree, LOCK_SH);
4356 if (unlockerr && err == NULL)
4357 err = unlockerr;
4358 return err;
4361 struct collect_revertible_paths_arg {
4362 struct got_pathlist_head *revertible_paths;
4363 struct got_worktree *worktree;
4366 static const struct got_error *
4367 collect_revertible_paths(void *arg, unsigned char status,
4368 unsigned char staged_status, const char *relpath,
4369 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4370 struct got_object_id *commit_id)
4372 struct collect_revertible_paths_arg *a = arg;
4373 const struct got_error *err = NULL;
4374 struct got_pathlist_entry *new = NULL;
4375 char *path = NULL;
4377 if (status != GOT_STATUS_ADD &&
4378 status != GOT_STATUS_DELETE &&
4379 status != GOT_STATUS_MODIFY &&
4380 status != GOT_STATUS_CONFLICT &&
4381 status != GOT_STATUS_MISSING)
4382 return NULL;
4384 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4385 return got_error_from_errno("asprintf");
4387 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4388 if (err || new == NULL)
4389 free(path);
4390 return err;
4393 const struct got_error *
4394 got_worktree_rebase_abort(struct got_worktree *worktree,
4395 struct got_fileindex *fileindex, struct got_repository *repo,
4396 struct got_reference *new_base_branch,
4397 got_worktree_checkout_cb progress_cb, void *progress_arg)
4399 const struct got_error *err, *unlockerr, *sync_err;
4400 struct got_reference *resolved = NULL;
4401 struct got_object_id *commit_id = NULL;
4402 char *fileindex_path = NULL;
4403 struct got_pathlist_head revertible_paths;
4404 struct got_pathlist_entry *pe;
4405 struct collect_revertible_paths_arg crp_arg;
4406 struct got_object_id *tree_id = NULL;
4408 TAILQ_INIT(&revertible_paths);
4410 err = lock_worktree(worktree, LOCK_EX);
4411 if (err)
4412 return err;
4414 err = got_ref_open(&resolved, repo,
4415 got_ref_get_symref_target(new_base_branch), 0);
4416 if (err)
4417 goto done;
4419 err = got_worktree_set_head_ref(worktree, resolved);
4420 if (err)
4421 goto done;
4424 * XXX commits to the base branch could have happened while
4425 * we were busy rebasing; should we store the original commit ID
4426 * when rebase begins and read it back here?
4428 err = got_ref_resolve(&commit_id, repo, resolved);
4429 if (err)
4430 goto done;
4432 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4433 if (err)
4434 goto done;
4436 err = got_object_id_by_path(&tree_id, repo,
4437 worktree->base_commit_id, worktree->path_prefix);
4438 if (err)
4439 goto done;
4441 err = delete_rebase_refs(worktree, repo);
4442 if (err)
4443 goto done;
4445 err = get_fileindex_path(&fileindex_path, worktree);
4446 if (err)
4447 goto done;
4449 crp_arg.revertible_paths = &revertible_paths;
4450 crp_arg.worktree = worktree;
4451 err = worktree_status(worktree, "", fileindex, repo,
4452 collect_revertible_paths, &crp_arg, NULL, NULL);
4453 if (err)
4454 goto done;
4456 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4457 err = revert_file(worktree, fileindex, pe->path,
4458 progress_cb, progress_arg, repo);
4459 if (err)
4460 goto sync;
4463 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4464 repo, progress_cb, progress_arg, NULL, NULL);
4465 sync:
4466 sync_err = sync_fileindex(fileindex, fileindex_path);
4467 if (sync_err && err == NULL)
4468 err = sync_err;
4469 done:
4470 got_ref_close(resolved);
4471 free(tree_id);
4472 free(commit_id);
4473 if (fileindex)
4474 got_fileindex_free(fileindex);
4475 free(fileindex_path);
4476 TAILQ_FOREACH(pe, &revertible_paths, entry)
4477 free((char *)pe->path);
4478 got_pathlist_free(&revertible_paths);
4480 unlockerr = lock_worktree(worktree, LOCK_SH);
4481 if (unlockerr && err == NULL)
4482 err = unlockerr;
4483 return err;
4486 const struct got_error *
4487 got_worktree_histedit_prepare(struct got_reference **tmp_branch,
4488 struct got_reference **branch_ref, struct got_object_id **base_commit_id,
4489 struct got_fileindex **fileindex, struct got_worktree *worktree,
4490 struct got_repository *repo)
4492 const struct got_error *err = NULL;
4493 char *tmp_branch_name = NULL;
4494 char *branch_ref_name = NULL;
4495 char *base_commit_ref_name = NULL;
4496 char *fileindex_path = NULL;
4497 struct check_rebase_ok_arg ok_arg;
4498 struct got_reference *wt_branch = NULL;
4499 struct got_reference *base_commit_ref = NULL;
4501 *tmp_branch = NULL;
4502 *branch_ref = NULL;
4503 *base_commit_id = NULL;
4504 *fileindex = NULL;
4506 err = lock_worktree(worktree, LOCK_EX);
4507 if (err)
4508 return err;
4510 err = open_fileindex(fileindex, &fileindex_path, worktree);
4511 if (err)
4512 goto done;
4514 ok_arg.worktree = worktree;
4515 ok_arg.repo = repo;
4516 err = got_fileindex_for_each_entry_safe(*fileindex, check_rebase_ok,
4517 &ok_arg);
4518 if (err)
4519 goto done;
4521 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4522 if (err)
4523 goto done;
4525 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4526 if (err)
4527 goto done;
4529 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4530 worktree);
4531 if (err)
4532 goto done;
4534 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
4535 0);
4536 if (err)
4537 goto done;
4539 err = got_ref_alloc_symref(branch_ref, branch_ref_name, wt_branch);
4540 if (err)
4541 goto done;
4543 err = got_ref_write(*branch_ref, repo);
4544 if (err)
4545 goto done;
4547 err = got_ref_alloc(&base_commit_ref, base_commit_ref_name,
4548 worktree->base_commit_id);
4549 if (err)
4550 goto done;
4551 err = got_ref_write(base_commit_ref, repo);
4552 if (err)
4553 goto done;
4554 *base_commit_id = got_object_id_dup(worktree->base_commit_id);
4555 if (*base_commit_id == NULL) {
4556 err = got_error_from_errno("got_object_id_dup");
4557 goto done;
4560 err = got_ref_alloc(tmp_branch, tmp_branch_name,
4561 worktree->base_commit_id);
4562 if (err)
4563 goto done;
4564 err = got_ref_write(*tmp_branch, repo);
4565 if (err)
4566 goto done;
4568 err = got_worktree_set_head_ref(worktree, *tmp_branch);
4569 if (err)
4570 goto done;
4571 done:
4572 free(fileindex_path);
4573 free(tmp_branch_name);
4574 free(branch_ref_name);
4575 free(base_commit_ref_name);
4576 if (wt_branch)
4577 got_ref_close(wt_branch);
4578 if (err) {
4579 if (*branch_ref) {
4580 got_ref_close(*branch_ref);
4581 *branch_ref = NULL;
4583 if (*tmp_branch) {
4584 got_ref_close(*tmp_branch);
4585 *tmp_branch = NULL;
4587 free(*base_commit_id);
4588 if (*fileindex) {
4589 got_fileindex_free(*fileindex);
4590 *fileindex = NULL;
4592 lock_worktree(worktree, LOCK_SH);
4594 return err;
4597 const struct got_error *
4598 got_worktree_histedit_postpone(struct got_worktree *worktree,
4599 struct got_fileindex *fileindex)
4601 if (fileindex)
4602 got_fileindex_free(fileindex);
4603 return lock_worktree(worktree, LOCK_SH);
4606 const struct got_error *
4607 got_worktree_histedit_in_progress(int *in_progress,
4608 struct got_worktree *worktree)
4610 const struct got_error *err;
4611 char *tmp_branch_name = NULL;
4613 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4614 if (err)
4615 return err;
4617 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
4618 free(tmp_branch_name);
4619 return NULL;
4622 const struct got_error *
4623 got_worktree_histedit_continue(struct got_object_id **commit_id,
4624 struct got_reference **tmp_branch, struct got_reference **branch_ref,
4625 struct got_object_id **base_commit_id, struct got_fileindex **fileindex,
4626 struct got_worktree *worktree, struct got_repository *repo)
4628 const struct got_error *err;
4629 char *commit_ref_name = NULL, *base_commit_ref_name = NULL;
4630 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
4631 struct got_reference *commit_ref = NULL;
4632 struct got_reference *base_commit_ref = NULL;
4633 char *fileindex_path = NULL;
4635 *commit_id = NULL;
4636 *tmp_branch = NULL;
4637 *base_commit_id = NULL;
4638 *fileindex = NULL;
4640 err = lock_worktree(worktree, LOCK_EX);
4641 if (err)
4642 return err;
4644 err = open_fileindex(fileindex, &fileindex_path, worktree);
4645 if (err)
4646 goto done;
4648 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4649 if (err)
4650 return err;
4652 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4653 if (err)
4654 goto done;
4656 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4657 if (err)
4658 goto done;
4660 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4661 worktree);
4662 if (err)
4663 goto done;
4665 err = got_ref_open(branch_ref, repo, branch_ref_name, 0);
4666 if (err)
4667 goto done;
4669 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
4670 if (err)
4671 goto done;
4672 err = got_ref_resolve(commit_id, repo, commit_ref);
4673 if (err)
4674 goto done;
4676 err = got_ref_open(&base_commit_ref, repo, base_commit_ref_name, 0);
4677 if (err)
4678 goto done;
4679 err = got_ref_resolve(base_commit_id, repo, base_commit_ref);
4680 if (err)
4681 goto done;
4683 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
4684 if (err)
4685 goto done;
4686 done:
4687 free(commit_ref_name);
4688 free(branch_ref_name);
4689 free(fileindex_path);
4690 if (commit_ref)
4691 got_ref_close(commit_ref);
4692 if (base_commit_ref)
4693 got_ref_close(base_commit_ref);
4694 if (err) {
4695 free(*commit_id);
4696 *commit_id = NULL;
4697 free(*base_commit_id);
4698 *base_commit_id = NULL;
4699 if (*tmp_branch) {
4700 got_ref_close(*tmp_branch);
4701 *tmp_branch = NULL;
4703 if (*fileindex) {
4704 got_fileindex_free(*fileindex);
4705 *fileindex = NULL;
4707 lock_worktree(worktree, LOCK_EX);
4709 return err;
4712 static const struct got_error *
4713 delete_histedit_refs(struct got_worktree *worktree, struct got_repository *repo)
4715 const struct got_error *err;
4716 char *tmp_branch_name = NULL, *base_commit_ref_name = NULL;
4717 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4719 err = get_histedit_tmp_ref_name(&tmp_branch_name, worktree);
4720 if (err)
4721 goto done;
4722 err = delete_ref(tmp_branch_name, repo);
4723 if (err)
4724 goto done;
4726 err = get_histedit_base_commit_ref_name(&base_commit_ref_name,
4727 worktree);
4728 if (err)
4729 goto done;
4730 err = delete_ref(base_commit_ref_name, repo);
4731 if (err)
4732 goto done;
4734 err = get_histedit_branch_symref_name(&branch_ref_name, worktree);
4735 if (err)
4736 goto done;
4737 err = delete_ref(branch_ref_name, repo);
4738 if (err)
4739 goto done;
4741 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4742 if (err)
4743 goto done;
4744 err = delete_ref(commit_ref_name, repo);
4745 if (err)
4746 goto done;
4747 done:
4748 free(tmp_branch_name);
4749 free(base_commit_ref_name);
4750 free(branch_ref_name);
4751 free(commit_ref_name);
4752 return err;
4755 const struct got_error *
4756 got_worktree_histedit_abort(struct got_worktree *worktree,
4757 struct got_fileindex *fileindex, struct got_repository *repo,
4758 struct got_reference *branch, struct got_object_id *base_commit_id,
4759 got_worktree_checkout_cb progress_cb, void *progress_arg)
4761 const struct got_error *err, *unlockerr, *sync_err;
4762 struct got_reference *resolved = NULL;
4763 char *fileindex_path = NULL;
4764 struct got_pathlist_head revertible_paths;
4765 struct got_pathlist_entry *pe;
4766 struct collect_revertible_paths_arg crp_arg;
4767 struct got_object_id *tree_id = NULL;
4769 TAILQ_INIT(&revertible_paths);
4771 err = lock_worktree(worktree, LOCK_EX);
4772 if (err)
4773 return err;
4775 err = got_ref_open(&resolved, repo,
4776 got_ref_get_symref_target(branch), 0);
4777 if (err)
4778 goto done;
4780 err = got_worktree_set_head_ref(worktree, resolved);
4781 if (err)
4782 goto done;
4784 err = got_worktree_set_base_commit_id(worktree, repo, base_commit_id);
4785 if (err)
4786 goto done;
4788 err = got_object_id_by_path(&tree_id, repo, base_commit_id,
4789 worktree->path_prefix);
4790 if (err)
4791 goto done;
4793 err = delete_histedit_refs(worktree, repo);
4794 if (err)
4795 goto done;
4797 err = get_fileindex_path(&fileindex_path, worktree);
4798 if (err)
4799 goto done;
4801 crp_arg.revertible_paths = &revertible_paths;
4802 crp_arg.worktree = worktree;
4803 err = worktree_status(worktree, "", fileindex, repo,
4804 collect_revertible_paths, &crp_arg, NULL, NULL);
4805 if (err)
4806 goto done;
4808 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4809 err = revert_file(worktree, fileindex, pe->path,
4810 progress_cb, progress_arg, repo);
4811 if (err)
4812 goto sync;
4815 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4816 repo, progress_cb, progress_arg, NULL, NULL);
4817 sync:
4818 sync_err = sync_fileindex(fileindex, fileindex_path);
4819 if (sync_err && err == NULL)
4820 err = sync_err;
4821 done:
4822 got_ref_close(resolved);
4823 free(tree_id);
4824 free(fileindex_path);
4825 TAILQ_FOREACH(pe, &revertible_paths, entry)
4826 free((char *)pe->path);
4827 got_pathlist_free(&revertible_paths);
4829 unlockerr = lock_worktree(worktree, LOCK_SH);
4830 if (unlockerr && err == NULL)
4831 err = unlockerr;
4832 return err;
4835 const struct got_error *
4836 got_worktree_histedit_complete(struct got_worktree *worktree,
4837 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4838 struct got_reference *edited_branch, struct got_repository *repo)
4840 const struct got_error *err, *unlockerr;
4841 struct got_object_id *new_head_commit_id = NULL;
4842 struct got_reference *resolved = NULL;
4844 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4845 if (err)
4846 return err;
4848 err = got_ref_open(&resolved, repo,
4849 got_ref_get_symref_target(edited_branch), 0);
4850 if (err)
4851 goto done;
4853 err = got_ref_change_ref(resolved, new_head_commit_id);
4854 if (err)
4855 goto done;
4857 err = got_ref_write(resolved, repo);
4858 if (err)
4859 goto done;
4861 err = got_worktree_set_head_ref(worktree, resolved);
4862 if (err)
4863 goto done;
4865 err = delete_histedit_refs(worktree, repo);
4866 done:
4867 if (fileindex)
4868 got_fileindex_free(fileindex);
4869 free(new_head_commit_id);
4870 unlockerr = lock_worktree(worktree, LOCK_SH);
4871 if (unlockerr && err == NULL)
4872 err = unlockerr;
4873 return err;
4876 const struct got_error *
4877 got_worktree_histedit_skip_commit(struct got_worktree *worktree,
4878 struct got_object_id *commit_id, struct got_repository *repo)
4880 const struct got_error *err;
4881 char *commit_ref_name;
4883 err = get_histedit_commit_ref_name(&commit_ref_name, worktree);
4884 if (err)
4885 return err;
4887 err = store_commit_id(commit_ref_name, commit_id, repo);
4888 if (err)
4889 goto done;
4891 err = delete_ref(commit_ref_name, repo);
4892 done:
4893 free(commit_ref_name);
4894 return err;
4897 static const struct got_error *
4898 stage_path(const char *relpath, const char *ondisk_path,
4899 const char *path_content, struct got_worktree *worktree,
4900 struct got_fileindex *fileindex, struct got_repository *repo,
4901 got_worktree_status_cb status_cb, void *status_arg)
4903 const struct got_error *err = NULL;
4904 struct got_fileindex_entry *ie;
4905 unsigned char status;
4906 struct stat sb;
4907 struct got_object_id blob_id, *staged_blob_id = NULL;
4908 uint32_t stage;
4910 ie = got_fileindex_entry_get(fileindex, relpath, strlen(relpath));
4911 if (ie == NULL) {
4912 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4913 goto done;
4916 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
4917 if (err)
4918 goto done;
4920 switch (status) {
4921 case GOT_STATUS_ADD:
4922 case GOT_STATUS_MODIFY:
4923 err = got_object_blob_create(&staged_blob_id,
4924 path_content ? path_content : ondisk_path, repo);
4925 if (err)
4926 goto done;
4927 memcpy(&blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
4928 memcpy(ie->staged_blob_sha1, staged_blob_id->sha1,
4929 SHA1_DIGEST_LENGTH);
4930 if (status == GOT_STATUS_ADD)
4931 stage = GOT_FILEIDX_STAGE_ADD;
4932 else
4933 stage = GOT_FILEIDX_STAGE_MODIFY;
4934 got_fileindex_entry_stage_set(ie, stage);
4935 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4936 get_staged_status(ie), relpath, &blob_id,
4937 staged_blob_id, NULL);
4938 break;
4939 case GOT_STATUS_DELETE:
4940 stage = GOT_FILEIDX_STAGE_DELETE;
4941 got_fileindex_entry_stage_set(ie, stage);
4942 err = (*status_cb)(status_arg, GOT_STATUS_NO_CHANGE,
4943 get_staged_status(ie), relpath, NULL, NULL, NULL);
4944 break;
4945 default:
4946 err = got_error_path(relpath, GOT_ERR_FILE_STATUS);
4947 break;
4949 done:
4950 free(staged_blob_id);
4951 return err;
4954 const struct got_error *
4955 got_worktree_stage(struct got_worktree *worktree,
4956 struct got_pathlist_head *paths,
4957 got_worktree_status_cb status_cb, void *status_arg,
4958 struct got_repository *repo)
4960 const struct got_error *err = NULL, *sync_err, *unlockerr;
4961 struct got_pathlist_entry *pe;
4962 struct got_fileindex *fileindex = NULL;
4963 char *fileindex_path = NULL;
4965 err = lock_worktree(worktree, LOCK_EX);
4966 if (err)
4967 return err;
4969 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4970 if (err)
4971 goto done;
4973 TAILQ_FOREACH(pe, paths, entry) {
4974 char *relpath;
4975 err = got_path_skip_common_ancestor(&relpath,
4976 got_worktree_get_root_path(worktree), pe->path);
4977 if (err)
4978 break;
4979 err = stage_path(relpath, pe->path,
4980 (const char *)pe->data, worktree, fileindex, repo,
4981 status_cb, status_arg);
4982 free(relpath);
4983 if (err)
4984 break;
4987 sync_err = sync_fileindex(fileindex, fileindex_path);
4988 if (sync_err && err == NULL)
4989 err = sync_err;
4990 done:
4991 free(fileindex_path);
4992 if (fileindex)
4993 got_fileindex_free(fileindex);
4994 unlockerr = lock_worktree(worktree, LOCK_SH);
4995 if (unlockerr && err == NULL)
4996 err = unlockerr;
4997 return err;