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(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 (lstat(path, &sb) != 0) {
146 err = got_error_from_errno2("lstat", 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(GOT_ERR_WORKTREE_META));
159 goto done;
161 if ((*content)[sb.st_size - 1] != '\n') {
162 err = got_error(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(GOT_ERR_WORKTREE_META);
359 goto done;
361 if (version != GOT_WORKTREE_FORMAT_VERSION) {
362 err = got_error(GOT_ERR_WORKTREE_VERS);
363 goto done;
366 *worktree = calloc(1, sizeof(**worktree));
367 if (*worktree == NULL) {
368 err = got_error_from_errno("calloc");
369 goto done;
371 (*worktree)->lockfd = -1;
373 (*worktree)->root_path = strdup(path);
374 if ((*worktree)->root_path == NULL) {
375 err = got_error_from_errno("strdup");
376 goto done;
378 err = read_meta_file(&(*worktree)->repo_path, path_got,
379 GOT_WORKTREE_REPOSITORY);
380 if (err)
381 goto done;
383 err = read_meta_file(&(*worktree)->path_prefix, path_got,
384 GOT_WORKTREE_PATH_PREFIX);
385 if (err)
386 goto done;
388 err = read_meta_file(&base_commit_id_str, path_got,
389 GOT_WORKTREE_BASE_COMMIT);
390 if (err)
391 goto done;
393 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
394 if (err)
395 goto done;
396 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
397 if (uuid_status != uuid_s_ok) {
398 err = got_error_uuid(uuid_status);
399 goto done;
402 err = got_repo_open(&repo, (*worktree)->repo_path);
403 if (err)
404 goto done;
406 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
407 base_commit_id_str);
408 if (err)
409 goto done;
411 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
412 GOT_WORKTREE_HEAD_REF);
413 done:
414 if (repo)
415 got_repo_close(repo);
416 free(path_got);
417 free(path_lock);
418 free(base_commit_id_str);
419 free(uuidstr);
420 free(formatstr);
421 if (err) {
422 if (fd != -1)
423 close(fd);
424 if (*worktree != NULL)
425 got_worktree_close(*worktree);
426 *worktree = NULL;
427 } else
428 (*worktree)->lockfd = fd;
430 return err;
433 const struct got_error *
434 got_worktree_open(struct got_worktree **worktree, const char *path)
436 const struct got_error *err = NULL;
438 do {
439 err = open_worktree(worktree, path);
440 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
441 return err;
442 if (*worktree)
443 return NULL;
444 path = dirname(path);
445 if (path == NULL)
446 return got_error_from_errno2("dirname", path);
447 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
449 return got_error(GOT_ERR_NOT_WORKTREE);
452 const struct got_error *
453 got_worktree_close(struct got_worktree *worktree)
455 const struct got_error *err = NULL;
456 free(worktree->root_path);
457 free(worktree->repo_path);
458 free(worktree->path_prefix);
459 free(worktree->base_commit_id);
460 free(worktree->head_ref_name);
461 if (worktree->lockfd != -1)
462 if (close(worktree->lockfd) != 0)
463 err = got_error_from_errno2("close",
464 got_worktree_get_root_path(worktree));
465 free(worktree);
466 return err;
469 const char *
470 got_worktree_get_root_path(struct got_worktree *worktree)
472 return worktree->root_path;
475 const char *
476 got_worktree_get_repo_path(struct got_worktree *worktree)
478 return worktree->repo_path;
481 const char *
482 got_worktree_get_path_prefix(struct got_worktree *worktree)
484 return worktree->path_prefix;
487 const struct got_error *
488 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
489 const char *path_prefix)
491 char *absprefix = NULL;
493 if (!got_path_is_absolute(path_prefix)) {
494 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
495 return got_error_from_errno("asprintf");
497 *match = (strcmp(absprefix ? absprefix : path_prefix,
498 worktree->path_prefix) == 0);
499 free(absprefix);
500 return NULL;
503 const char *
504 got_worktree_get_head_ref_name(struct got_worktree *worktree)
506 return worktree->head_ref_name;
509 const struct got_error *
510 got_worktree_set_head_ref(struct got_worktree *worktree,
511 struct got_reference *head_ref)
513 const struct got_error *err = NULL;
514 char *path_got = NULL, *head_ref_name = NULL;
516 if (asprintf(&path_got, "%s/%s", worktree->root_path,
517 GOT_WORKTREE_GOT_DIR) == -1) {
518 err = got_error_from_errno("asprintf");
519 path_got = NULL;
520 goto done;
523 head_ref_name = strdup(got_ref_get_name(head_ref));
524 if (head_ref_name == NULL) {
525 err = got_error_from_errno("strdup");
526 goto done;
529 err = write_head_ref(path_got, head_ref);
530 if (err)
531 goto done;
533 free(worktree->head_ref_name);
534 worktree->head_ref_name = head_ref_name;
535 done:
536 free(path_got);
537 if (err)
538 free(head_ref_name);
539 return err;
542 struct got_object_id *
543 got_worktree_get_base_commit_id(struct got_worktree *worktree)
545 return worktree->base_commit_id;
548 const struct got_error *
549 got_worktree_set_base_commit_id(struct got_worktree *worktree,
550 struct got_repository *repo, struct got_object_id *commit_id)
552 const struct got_error *err;
553 struct got_object *obj = NULL;
554 char *id_str = NULL;
555 char *path_got = NULL;
557 if (asprintf(&path_got, "%s/%s", worktree->root_path,
558 GOT_WORKTREE_GOT_DIR) == -1) {
559 err = got_error_from_errno("asprintf");
560 path_got = NULL;
561 goto done;
564 err = got_object_open(&obj, repo, commit_id);
565 if (err)
566 return err;
568 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
569 err = got_error(GOT_ERR_OBJ_TYPE);
570 goto done;
573 /* Record our base commit. */
574 err = got_object_id_str(&id_str, commit_id);
575 if (err)
576 goto done;
577 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
578 if (err)
579 goto done;
581 free(worktree->base_commit_id);
582 worktree->base_commit_id = got_object_id_dup(commit_id);
583 if (worktree->base_commit_id == NULL) {
584 err = got_error_from_errno("got_object_id_dup");
585 goto done;
587 done:
588 if (obj)
589 got_object_close(obj);
590 free(id_str);
591 free(path_got);
592 return err;
595 static const struct got_error *
596 lock_worktree(struct got_worktree *worktree, int operation)
598 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
599 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
600 : got_error_from_errno2("flock",
601 got_worktree_get_root_path(worktree)));
602 return NULL;
605 static const struct got_error *
606 add_dir_on_disk(struct got_worktree *worktree, const char *path)
608 const struct got_error *err = NULL;
609 char *abspath;
611 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
612 return got_error_from_errno("asprintf");
614 err = got_path_mkdir(abspath);
615 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
616 struct stat sb;
617 err = NULL;
618 if (lstat(abspath, &sb) == -1) {
619 err = got_error_from_errno2("lstat", abspath);
620 } else if (!S_ISDIR(sb.st_mode)) {
621 /* TODO directory is obstructed; do something */
622 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
625 free(abspath);
626 return err;
629 static const struct got_error *
630 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
632 const struct got_error *err = NULL;
633 uint8_t fbuf1[8192];
634 uint8_t fbuf2[8192];
635 size_t flen1 = 0, flen2 = 0;
637 *same = 1;
639 for (;;) {
640 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
641 if (flen1 == 0 && ferror(f1)) {
642 err = got_error_from_errno("fread");
643 break;
645 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
646 if (flen2 == 0 && ferror(f2)) {
647 err = got_error_from_errno("fread");
648 break;
650 if (flen1 == 0) {
651 if (flen2 != 0)
652 *same = 0;
653 break;
654 } else if (flen2 == 0) {
655 if (flen1 != 0)
656 *same = 0;
657 break;
658 } else if (flen1 == flen2) {
659 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
660 *same = 0;
661 break;
663 } else {
664 *same = 0;
665 break;
669 return err;
672 static const struct got_error *
673 check_files_equal(int *same, const char *f1_path, const char *f2_path)
675 const struct got_error *err = NULL;
676 struct stat sb;
677 size_t size1, size2;
678 FILE *f1 = NULL, *f2 = NULL;
680 *same = 1;
682 if (lstat(f1_path, &sb) != 0) {
683 err = got_error_from_errno2("lstat", f1_path);
684 goto done;
686 size1 = sb.st_size;
688 if (lstat(f2_path, &sb) != 0) {
689 err = got_error_from_errno2("lstat", f2_path);
690 goto done;
692 size2 = sb.st_size;
694 if (size1 != size2) {
695 *same = 0;
696 return NULL;
699 f1 = fopen(f1_path, "r");
700 if (f1 == NULL)
701 return got_error_from_errno2("open", f1_path);
703 f2 = fopen(f2_path, "r");
704 if (f2 == NULL) {
705 err = got_error_from_errno2("open", f2_path);
706 goto done;
709 err = check_file_contents_equal(same, f1, f2);
710 done:
711 if (f1 && fclose(f1) != 0 && err == NULL)
712 err = got_error_from_errno("fclose");
713 if (f2 && fclose(f2) != 0 && err == NULL)
714 err = got_error_from_errno("fclose");
716 return err;
719 /*
720 * Perform a 3-way merge where blob_orig acts as the common ancestor,
721 * blob_deriv acts as the first derived version, and the file on disk
722 * acts as the second derived version.
723 */
724 static const struct got_error *
725 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
726 struct got_blob_object *blob_orig, const char *ondisk_path,
727 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
728 struct got_object_id *deriv_base_commit_id,
729 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
730 void *progress_arg)
732 const struct got_error *err = NULL;
733 int merged_fd = -1;
734 FILE *f_deriv = NULL, *f_orig = NULL;
735 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
736 char *merged_path = NULL, *base_path = NULL;
737 char *id_str = NULL;
738 char *label_deriv = NULL;
739 int overlapcnt = 0;
740 char *parent;
742 *local_changes_subsumed = 0;
744 parent = dirname(ondisk_path);
745 if (parent == NULL)
746 return got_error_from_errno2("dirname", ondisk_path);
748 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
749 return got_error_from_errno("asprintf");
751 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
752 if (err)
753 goto done;
755 free(base_path);
756 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
757 err = got_error_from_errno("asprintf");
758 base_path = NULL;
759 goto done;
762 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
763 if (err)
764 goto done;
765 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
766 blob_deriv);
767 if (err)
768 goto done;
770 free(base_path);
771 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
772 err = got_error_from_errno("asprintf");
773 base_path = NULL;
774 goto done;
777 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
778 if (err)
779 goto done;
780 if (blob_orig) {
781 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
782 blob_orig);
783 if (err)
784 goto done;
785 } else {
786 /*
787 * If the file has no blob, this is an "add vs add" conflict,
788 * and we simply use an empty ancestor file to make both files
789 * appear in the merged result in their entirety.
790 */
793 err = got_object_id_str(&id_str, deriv_base_commit_id);
794 if (err)
795 goto done;
796 if (asprintf(&label_deriv, "commit %s", id_str) == -1) {
797 err = got_error_from_errno("asprintf");
798 goto done;
801 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
802 blob_orig_path, ondisk_path, label_deriv, path);
803 if (err)
804 goto done;
806 err = (*progress_cb)(progress_arg,
807 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
808 if (err)
809 goto done;
811 if (fsync(merged_fd) != 0) {
812 err = got_error_from_errno("fsync");
813 goto done;
816 /* Check if a clean merge has subsumed all local changes. */
817 if (overlapcnt == 0) {
818 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
819 merged_path);
820 if (err)
821 goto done;
824 if (chmod(merged_path, st_mode) != 0) {
825 err = got_error_from_errno2("chmod", merged_path);
826 goto done;
829 if (rename(merged_path, ondisk_path) != 0) {
830 err = got_error_from_errno3("rename", merged_path,
831 ondisk_path);
832 unlink(merged_path);
833 goto done;
836 done:
837 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
838 err = got_error_from_errno("close");
839 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
840 err = got_error_from_errno("fclose");
841 if (f_orig && fclose(f_orig) != 0 && err == NULL)
842 err = got_error_from_errno("fclose");
843 free(merged_path);
844 free(base_path);
845 if (blob_deriv_path) {
846 unlink(blob_deriv_path);
847 free(blob_deriv_path);
849 if (blob_orig_path) {
850 unlink(blob_orig_path);
851 free(blob_orig_path);
853 free(id_str);
854 free(label_deriv);
855 return err;
858 static const struct got_error *
859 update_blob_fileindex_entry(struct got_worktree *worktree,
860 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
861 const char *ondisk_path, const char *path, struct got_blob_object *blob,
862 int update_timestamps)
864 const struct got_error *err = NULL;
866 if (ie == NULL)
867 ie = got_fileindex_entry_get(fileindex, path);
868 if (ie)
869 err = got_fileindex_entry_update(ie, ondisk_path,
870 blob->id.sha1, worktree->base_commit_id->sha1,
871 update_timestamps);
872 else {
873 struct got_fileindex_entry *new_ie;
874 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
875 path, blob->id.sha1, worktree->base_commit_id->sha1);
876 if (!err)
877 err = got_fileindex_entry_add(fileindex, new_ie);
879 return err;
882 static const struct got_error *
883 install_blob(struct got_worktree *worktree, const char *ondisk_path,
884 const char *path, uint16_t te_mode, uint16_t st_mode,
885 struct got_blob_object *blob, int restoring_missing_file,
886 int reverting_versioned_file, struct got_repository *repo,
887 got_worktree_checkout_cb progress_cb, void *progress_arg)
889 const struct got_error *err = NULL;
890 int fd = -1;
891 size_t len, hdrlen;
892 int update = 0;
893 char *tmppath = NULL;
895 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
896 GOT_DEFAULT_FILE_MODE);
897 if (fd == -1) {
898 if (errno == ENOENT) {
899 char *parent = dirname(path);
900 if (parent == NULL)
901 return got_error_from_errno2("dirname", path);
902 err = add_dir_on_disk(worktree, parent);
903 if (err)
904 return err;
905 fd = open(ondisk_path,
906 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
907 GOT_DEFAULT_FILE_MODE);
908 if (fd == -1)
909 return got_error_from_errno2("open",
910 ondisk_path);
911 } else if (errno == EEXIST) {
912 if (!S_ISREG(st_mode)) {
913 /* TODO file is obstructed; do something */
914 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
915 goto done;
916 } else {
917 err = got_opentemp_named_fd(&tmppath, &fd,
918 ondisk_path);
919 if (err)
920 goto done;
921 update = 1;
923 } else
924 return got_error_from_errno2("open", ondisk_path);
927 if (restoring_missing_file)
928 err = (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
929 else if (reverting_versioned_file)
930 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
931 else
932 err = (*progress_cb)(progress_arg,
933 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
934 if (err)
935 goto done;
937 hdrlen = got_object_blob_get_hdrlen(blob);
938 do {
939 const uint8_t *buf = got_object_blob_get_read_buf(blob);
940 err = got_object_blob_read_block(&len, blob);
941 if (err)
942 break;
943 if (len > 0) {
944 /* Skip blob object header first time around. */
945 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
946 if (outlen == -1) {
947 err = got_error_from_errno("write");
948 goto done;
949 } else if (outlen != len - hdrlen) {
950 err = got_error(GOT_ERR_IO);
951 goto done;
953 hdrlen = 0;
955 } while (len != 0);
957 if (fsync(fd) != 0) {
958 err = got_error_from_errno("fsync");
959 goto done;
962 if (update) {
963 if (rename(tmppath, ondisk_path) != 0) {
964 err = got_error_from_errno3("rename", tmppath,
965 ondisk_path);
966 unlink(tmppath);
967 goto done;
971 if (te_mode & S_IXUSR) {
972 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
973 err = got_error_from_errno2("chmod", ondisk_path);
974 goto done;
976 } else {
977 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
978 err = got_error_from_errno2("chmod", ondisk_path);
979 goto done;
983 done:
984 if (fd != -1 && close(fd) != 0 && err == NULL)
985 err = got_error_from_errno("close");
986 free(tmppath);
987 return err;
990 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
991 static const struct got_error *
992 get_modified_file_content_status(unsigned char *status, FILE *f)
994 const struct got_error *err = NULL;
995 const char *markers[3] = {
996 GOT_DIFF_CONFLICT_MARKER_BEGIN,
997 GOT_DIFF_CONFLICT_MARKER_SEP,
998 GOT_DIFF_CONFLICT_MARKER_END
999 };
1000 int i = 0;
1001 char *line;
1002 size_t len;
1003 const char delim[3] = {'\0', '\0', '\0'};
1005 while (*status == GOT_STATUS_MODIFY) {
1006 line = fparseln(f, &len, NULL, delim, 0);
1007 if (line == NULL) {
1008 if (feof(f))
1009 break;
1010 err = got_ferror(f, GOT_ERR_IO);
1011 break;
1014 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1015 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1016 == 0)
1017 *status = GOT_STATUS_CONFLICT;
1018 else
1019 i++;
1023 return err;
1026 static const struct got_error *
1027 get_file_status(unsigned char *status, struct stat *sb,
1028 struct got_fileindex_entry *ie, const char *abspath,
1029 struct got_repository *repo)
1031 const struct got_error *err = NULL;
1032 struct got_object_id id;
1033 size_t hdrlen;
1034 FILE *f = NULL;
1035 uint8_t fbuf[8192];
1036 struct got_blob_object *blob = NULL;
1037 size_t flen, blen;
1039 *status = GOT_STATUS_NO_CHANGE;
1041 if (lstat(abspath, sb) == -1) {
1042 if (errno == ENOENT) {
1043 if (ie) {
1044 if (got_fileindex_entry_has_file_on_disk(ie))
1045 *status = GOT_STATUS_MISSING;
1046 else
1047 *status = GOT_STATUS_DELETE;
1048 sb->st_mode =
1049 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1050 & (S_IRWXU | S_IRWXG | S_IRWXO));
1051 } else
1052 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1053 return NULL;
1055 return got_error_from_errno2("lstat", abspath);
1058 if (!S_ISREG(sb->st_mode)) {
1059 *status = GOT_STATUS_OBSTRUCTED;
1060 return NULL;
1063 if (ie == NULL)
1064 return NULL;
1066 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1067 *status = GOT_STATUS_DELETE;
1068 return NULL;
1069 } else if (!got_fileindex_entry_has_blob(ie)) {
1070 *status = GOT_STATUS_ADD;
1071 return NULL;
1074 if (ie->ctime_sec == sb->st_ctime &&
1075 ie->ctime_nsec == sb->st_ctimensec &&
1076 ie->mtime_sec == sb->st_mtime &&
1077 ie->mtime_nsec == sb->st_mtimensec &&
1078 ie->size == (sb->st_size & 0xffffffff))
1079 return NULL;
1081 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1082 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1083 if (err)
1084 return err;
1086 f = fopen(abspath, "r");
1087 if (f == NULL) {
1088 err = got_error_from_errno2("fopen", abspath);
1089 goto done;
1091 hdrlen = got_object_blob_get_hdrlen(blob);
1092 for (;;) {
1093 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1094 err = got_object_blob_read_block(&blen, blob);
1095 if (err)
1096 goto done;
1097 /* Skip length of blob object header first time around. */
1098 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1099 if (flen == 0 && ferror(f)) {
1100 err = got_error_from_errno("fread");
1101 goto done;
1103 if (blen == 0) {
1104 if (flen != 0)
1105 *status = GOT_STATUS_MODIFY;
1106 break;
1107 } else if (flen == 0) {
1108 if (blen != 0)
1109 *status = GOT_STATUS_MODIFY;
1110 break;
1111 } else if (blen - hdrlen == flen) {
1112 /* Skip blob object header first time around. */
1113 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1114 *status = GOT_STATUS_MODIFY;
1115 break;
1117 } else {
1118 *status = GOT_STATUS_MODIFY;
1119 break;
1121 hdrlen = 0;
1124 if (*status == GOT_STATUS_MODIFY) {
1125 rewind(f);
1126 err = get_modified_file_content_status(status, f);
1128 done:
1129 if (blob)
1130 got_object_blob_close(blob);
1131 if (f)
1132 fclose(f);
1133 return err;
1136 static const struct got_error *
1137 update_blob(struct got_worktree *worktree,
1138 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1139 struct got_tree_entry *te, const char *path,
1140 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1141 void *progress_arg)
1143 const struct got_error *err = NULL;
1144 struct got_blob_object *blob = NULL;
1145 char *ondisk_path;
1146 unsigned char status = GOT_STATUS_NO_CHANGE;
1147 struct stat sb;
1149 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1150 return got_error_from_errno("asprintf");
1152 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1153 if (err)
1154 goto done;
1156 if (status == GOT_STATUS_OBSTRUCTED) {
1157 err = (*progress_cb)(progress_arg, status, path);
1158 goto done;
1161 if (ie && status != GOT_STATUS_MISSING) {
1162 if (got_fileindex_entry_has_commit(ie) &&
1163 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1164 SHA1_DIGEST_LENGTH) == 0) {
1165 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1166 path);
1167 goto done;
1169 if (got_fileindex_entry_has_blob(ie) &&
1170 memcmp(ie->blob_sha1, te->id->sha1,
1171 SHA1_DIGEST_LENGTH) == 0)
1172 goto done;
1175 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1176 if (err)
1177 goto done;
1179 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1180 int update_timestamps;
1181 struct got_blob_object *blob2 = NULL;
1182 if (got_fileindex_entry_has_blob(ie)) {
1183 struct got_object_id id2;
1184 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1185 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1186 if (err)
1187 goto done;
1189 err = merge_blob(&update_timestamps, worktree, blob2,
1190 ondisk_path, path, sb.st_mode, blob,
1191 worktree->base_commit_id, repo,
1192 progress_cb, progress_arg);
1193 if (blob2)
1194 got_object_blob_close(blob2);
1196 * Do not update timestamps of files with local changes.
1197 * Otherwise, a future status walk would treat them as
1198 * unmodified files again.
1200 err = got_fileindex_entry_update(ie, ondisk_path,
1201 blob->id.sha1, worktree->base_commit_id->sha1,
1202 update_timestamps);
1203 } else if (status == GOT_STATUS_DELETE) {
1204 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1205 if (err)
1206 goto done;
1207 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1208 ondisk_path, path, blob, 0);
1209 if (err)
1210 goto done;
1211 } else {
1212 err = install_blob(worktree, ondisk_path, path, te->mode,
1213 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1214 repo, progress_cb, progress_arg);
1215 if (err)
1216 goto done;
1217 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1218 ondisk_path, path, blob, 1);
1219 if (err)
1220 goto done;
1222 got_object_blob_close(blob);
1223 done:
1224 free(ondisk_path);
1225 return err;
1228 static const struct got_error *
1229 remove_ondisk_file(const char *root_path, const char *path)
1231 const struct got_error *err = NULL;
1232 char *ondisk_path = NULL;
1234 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1235 return got_error_from_errno("asprintf");
1237 if (unlink(ondisk_path) == -1) {
1238 if (errno != ENOENT)
1239 err = got_error_from_errno2("unlink", ondisk_path);
1240 } else {
1241 char *parent = dirname(ondisk_path);
1242 while (parent && strcmp(parent, root_path) != 0) {
1243 if (rmdir(parent) == -1) {
1244 if (errno != ENOTEMPTY)
1245 err = got_error_from_errno2("rmdir",
1246 parent);
1247 break;
1249 parent = dirname(parent);
1252 free(ondisk_path);
1253 return err;
1256 static const struct got_error *
1257 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1258 struct got_fileindex_entry *ie, struct got_repository *repo,
1259 got_worktree_checkout_cb progress_cb, void *progress_arg)
1261 const struct got_error *err = NULL;
1262 unsigned char status;
1263 struct stat sb;
1264 char *ondisk_path;
1266 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1267 == -1)
1268 return got_error_from_errno("asprintf");
1270 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1271 if (err)
1272 return err;
1274 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1275 status == GOT_STATUS_ADD) {
1276 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1277 if (err)
1278 return err;
1280 * Preserve the working file and change the deleted blob's
1281 * entry into a schedule-add entry.
1283 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1284 0);
1285 if (err)
1286 return err;
1287 } else {
1288 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1289 if (err)
1290 return err;
1291 if (status == GOT_STATUS_NO_CHANGE) {
1292 err = remove_ondisk_file(worktree->root_path, ie->path);
1293 if (err)
1294 return err;
1296 got_fileindex_entry_remove(fileindex, ie);
1299 return err;
1302 struct diff_cb_arg {
1303 struct got_fileindex *fileindex;
1304 struct got_worktree *worktree;
1305 struct got_repository *repo;
1306 got_worktree_checkout_cb progress_cb;
1307 void *progress_arg;
1308 got_worktree_cancel_cb cancel_cb;
1309 void *cancel_arg;
1312 static const struct got_error *
1313 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1314 struct got_tree_entry *te, const char *parent_path)
1316 struct diff_cb_arg *a = arg;
1318 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1319 return got_error(GOT_ERR_CANCELLED);
1321 return update_blob(a->worktree, a->fileindex, ie, te,
1322 ie->path, a->repo, a->progress_cb, a->progress_arg);
1325 static const struct got_error *
1326 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1328 struct diff_cb_arg *a = arg;
1330 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1331 return got_error(GOT_ERR_CANCELLED);
1333 return delete_blob(a->worktree, a->fileindex, ie,
1334 a->repo, a->progress_cb, a->progress_arg);
1337 static const struct got_error *
1338 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1340 struct diff_cb_arg *a = arg;
1341 const struct got_error *err;
1342 char *path;
1344 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1345 return got_error(GOT_ERR_CANCELLED);
1347 if (asprintf(&path, "%s%s%s", parent_path,
1348 parent_path[0] ? "/" : "", te->name)
1349 == -1)
1350 return got_error_from_errno("asprintf");
1352 if (S_ISDIR(te->mode))
1353 err = add_dir_on_disk(a->worktree, path);
1354 else
1355 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1356 a->repo, a->progress_cb, a->progress_arg);
1358 free(path);
1359 return err;
1362 static const struct got_error *
1363 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1365 const struct got_error *err = NULL;
1366 char *uuidstr = NULL;
1367 uint32_t uuid_status;
1369 *refname = NULL;
1371 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1372 if (uuid_status != uuid_s_ok)
1373 return got_error_uuid(uuid_status);
1375 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1376 == -1) {
1377 err = got_error_from_errno("asprintf");
1378 *refname = NULL;
1380 free(uuidstr);
1381 return err;
1384 const struct got_error *
1385 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1387 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1390 static const struct got_error *
1391 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1393 return get_ref_name(refname, worktree,
1394 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1397 static const struct got_error *
1398 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1400 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1403 static const struct got_error *
1404 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1406 return get_ref_name(refname, worktree,
1407 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1410 static const struct got_error *
1411 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1413 return get_ref_name(refname, worktree,
1414 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1419 * Prevent Git's garbage collector from deleting our base commit by
1420 * setting a reference to our base commit's ID.
1422 static const struct got_error *
1423 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1425 const struct got_error *err = NULL;
1426 struct got_reference *ref = NULL;
1427 char *refname;
1429 err = got_worktree_get_base_ref_name(&refname, worktree);
1430 if (err)
1431 return err;
1433 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1434 if (err)
1435 goto done;
1437 err = got_ref_write(ref, repo);
1438 done:
1439 free(refname);
1440 if (ref)
1441 got_ref_close(ref);
1442 return err;
1445 static const struct got_error *
1446 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1447 struct got_worktree *worktree)
1449 const struct got_error *err = NULL;
1450 FILE *index = NULL;
1452 *fileindex_path = NULL;
1453 *fileindex = got_fileindex_alloc();
1454 if (*fileindex == NULL)
1455 return got_error_from_errno("got_fileindex_alloc");
1457 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1458 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1459 err = got_error_from_errno("asprintf");
1460 *fileindex_path = NULL;
1461 goto done;
1464 index = fopen(*fileindex_path, "rb");
1465 if (index == NULL) {
1466 if (errno != ENOENT)
1467 err = got_error_from_errno2("fopen", *fileindex_path);
1468 } else {
1469 err = got_fileindex_read(*fileindex, index);
1470 if (fclose(index) != 0 && err == NULL)
1471 err = got_error_from_errno("fclose");
1473 done:
1474 if (err) {
1475 free(*fileindex_path);
1476 *fileindex_path = NULL;
1477 got_fileindex_free(*fileindex);
1478 *fileindex = NULL;
1480 return err;
1483 struct bump_base_commit_id_arg {
1484 struct got_object_id *base_commit_id;
1485 const char *path;
1486 size_t path_len;
1487 const char *entry_name;
1488 got_worktree_checkout_cb progress_cb;
1489 void *progress_arg;
1492 /* Bump base commit ID of all files within an updated part of the work tree. */
1493 static const struct got_error *
1494 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1496 const struct got_error *err;
1497 struct bump_base_commit_id_arg *a = arg;
1499 if (a->entry_name) {
1500 if (strcmp(ie->path, a->path) != 0)
1501 return NULL;
1502 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1503 return NULL;
1505 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1506 SHA1_DIGEST_LENGTH) == 0)
1507 return NULL;
1509 if (a->progress_cb) {
1510 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1511 ie->path);
1512 if (err)
1513 return err;
1515 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1516 return NULL;
1519 static const struct got_error *
1520 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1522 const struct got_error *err = NULL;
1523 char *new_fileindex_path = NULL;
1524 FILE *new_index = NULL;
1526 err = got_opentemp_named(&new_fileindex_path, &new_index,
1527 fileindex_path);
1528 if (err)
1529 goto done;
1531 err = got_fileindex_write(fileindex, new_index);
1532 if (err)
1533 goto done;
1535 if (rename(new_fileindex_path, fileindex_path) != 0) {
1536 err = got_error_from_errno3("rename", new_fileindex_path,
1537 fileindex_path);
1538 unlink(new_fileindex_path);
1540 done:
1541 if (new_index)
1542 fclose(new_index);
1543 free(new_fileindex_path);
1544 return err;
1547 static const struct got_error *
1548 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1549 struct got_object_id **tree_id, const char *wt_relpath,
1550 struct got_worktree *worktree, struct got_repository *repo)
1552 const struct got_error *err = NULL;
1553 struct got_object_id *id = NULL;
1554 char *in_repo_path = NULL;
1555 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1557 *entry_type = GOT_OBJ_TYPE_ANY;
1558 *tree_relpath = NULL;
1559 *tree_id = NULL;
1561 if (wt_relpath[0] == '\0') {
1562 /* Check out all files within the work tree. */
1563 *entry_type = GOT_OBJ_TYPE_TREE;
1564 *tree_relpath = strdup("");
1565 if (*tree_relpath == NULL) {
1566 err = got_error_from_errno("strdup");
1567 goto done;
1569 err = got_object_id_by_path(tree_id, repo,
1570 worktree->base_commit_id, worktree->path_prefix);
1571 if (err)
1572 goto done;
1573 return NULL;
1576 /* Check out a subset of files in the work tree. */
1578 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1579 is_root_wt ? "" : "/", wt_relpath) == -1) {
1580 err = got_error_from_errno("asprintf");
1581 goto done;
1584 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1585 in_repo_path);
1586 if (err)
1587 goto done;
1589 free(in_repo_path);
1590 in_repo_path = NULL;
1592 err = got_object_get_type(entry_type, repo, id);
1593 if (err)
1594 goto done;
1596 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1597 /* Check out a single file. */
1598 if (strchr(wt_relpath, '/') == NULL) {
1599 /* Check out a single file in work tree's root dir. */
1600 in_repo_path = strdup(worktree->path_prefix);
1601 if (in_repo_path == NULL) {
1602 err = got_error_from_errno("strdup");
1603 goto done;
1605 *tree_relpath = strdup("");
1606 if (*tree_relpath == NULL) {
1607 err = got_error_from_errno("strdup");
1608 goto done;
1610 } else {
1611 /* Check out a single file in a subdirectory. */
1612 err = got_path_dirname(tree_relpath, wt_relpath);
1613 if (err)
1614 return err;
1615 if (asprintf(&in_repo_path, "%s%s%s",
1616 worktree->path_prefix, is_root_wt ? "" : "/",
1617 *tree_relpath) == -1) {
1618 err = got_error_from_errno("asprintf");
1619 goto done;
1622 err = got_object_id_by_path(tree_id, repo,
1623 worktree->base_commit_id, in_repo_path);
1624 } else {
1625 /* Check out all files within a subdirectory. */
1626 *tree_id = got_object_id_dup(id);
1627 if (*tree_id == NULL) {
1628 err = got_error_from_errno("got_object_id_dup");
1629 goto done;
1631 *tree_relpath = strdup(wt_relpath);
1632 if (*tree_relpath == NULL) {
1633 err = got_error_from_errno("strdup");
1634 goto done;
1637 done:
1638 free(id);
1639 free(in_repo_path);
1640 if (err) {
1641 *entry_type = GOT_OBJ_TYPE_ANY;
1642 free(*tree_relpath);
1643 *tree_relpath = NULL;
1644 free(*tree_id);
1645 *tree_id = NULL;
1647 return err;
1650 static const struct got_error *
1651 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1652 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1653 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1654 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1656 const struct got_error *err = NULL;
1657 struct got_commit_object *commit = NULL;
1658 struct got_tree_object *tree = NULL;
1659 struct got_fileindex_diff_tree_cb diff_cb;
1660 struct diff_cb_arg arg;
1662 err = ref_base_commit(worktree, repo);
1663 if (err)
1664 goto done;
1666 err = got_object_open_as_commit(&commit, repo,
1667 worktree->base_commit_id);
1668 if (err)
1669 goto done;
1671 err = got_object_open_as_tree(&tree, repo, tree_id);
1672 if (err)
1673 goto done;
1675 if (entry_name &&
1676 got_object_tree_find_entry(tree, entry_name) == NULL) {
1677 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1678 goto done;
1681 diff_cb.diff_old_new = diff_old_new;
1682 diff_cb.diff_old = diff_old;
1683 diff_cb.diff_new = diff_new;
1684 arg.fileindex = fileindex;
1685 arg.worktree = worktree;
1686 arg.repo = repo;
1687 arg.progress_cb = progress_cb;
1688 arg.progress_arg = progress_arg;
1689 arg.cancel_cb = cancel_cb;
1690 arg.cancel_arg = cancel_arg;
1691 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1692 entry_name, repo, &diff_cb, &arg);
1693 done:
1694 if (tree)
1695 got_object_tree_close(tree);
1696 if (commit)
1697 got_object_commit_close(commit);
1698 return err;
1701 const struct got_error *
1702 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1703 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1704 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1706 const struct got_error *err = NULL, *sync_err, *unlockerr;
1707 struct got_commit_object *commit = NULL;
1708 struct got_object_id *tree_id = NULL;
1709 struct got_tree_object *tree = NULL;
1710 struct got_fileindex *fileindex = NULL;
1711 char *fileindex_path = NULL;
1712 char *relpath = NULL, *entry_name = NULL;
1713 int entry_type;
1715 err = lock_worktree(worktree, LOCK_EX);
1716 if (err)
1717 return err;
1719 err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
1720 path, worktree, repo);
1721 if (err)
1722 goto done;
1724 if (entry_type == GOT_OBJ_TYPE_BLOB) {
1725 entry_name = basename(path);
1726 if (entry_name == NULL) {
1727 err = got_error_from_errno2("basename", path);
1728 goto done;
1733 * Read the file index.
1734 * Checking out files is supposed to be an idempotent operation.
1735 * If the on-disk file index is incomplete we will try to complete it.
1737 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1738 if (err)
1739 goto done;
1741 err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
1742 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1743 if (err == NULL) {
1744 struct bump_base_commit_id_arg bbc_arg;
1745 bbc_arg.base_commit_id = worktree->base_commit_id;
1746 bbc_arg.entry_name = entry_name;
1747 bbc_arg.path = path;
1748 bbc_arg.path_len = strlen(path);
1749 bbc_arg.progress_cb = progress_cb;
1750 bbc_arg.progress_arg = progress_arg;
1751 err = got_fileindex_for_each_entry_safe(fileindex,
1752 bump_base_commit_id, &bbc_arg);
1754 sync_err = sync_fileindex(fileindex, fileindex_path);
1755 if (sync_err && err == NULL)
1756 err = sync_err;
1757 done:
1758 free(fileindex_path);
1759 free(relpath);
1760 if (tree)
1761 got_object_tree_close(tree);
1762 if (commit)
1763 got_object_commit_close(commit);
1764 if (fileindex)
1765 got_fileindex_free(fileindex);
1766 unlockerr = lock_worktree(worktree, LOCK_SH);
1767 if (unlockerr && err == NULL)
1768 err = unlockerr;
1769 return err;
1772 struct merge_file_cb_arg {
1773 struct got_worktree *worktree;
1774 struct got_fileindex *fileindex;
1775 got_worktree_checkout_cb progress_cb;
1776 void *progress_arg;
1777 got_worktree_cancel_cb cancel_cb;
1778 void *cancel_arg;
1779 struct got_object_id *commit_id2;
1782 static const struct got_error *
1783 merge_file_cb(void *arg, struct got_blob_object *blob1,
1784 struct got_blob_object *blob2, struct got_object_id *id1,
1785 struct got_object_id *id2, const char *path1, const char *path2,
1786 struct got_repository *repo)
1788 static const struct got_error *err = NULL;
1789 struct merge_file_cb_arg *a = arg;
1790 struct got_fileindex_entry *ie;
1791 char *ondisk_path = NULL;
1792 struct stat sb;
1793 unsigned char status;
1794 int local_changes_subsumed;
1796 if (blob1 && blob2) {
1797 ie = got_fileindex_entry_get(a->fileindex, path2);
1798 if (ie == NULL)
1799 return (*a->progress_cb)(a->progress_arg,
1800 GOT_STATUS_MISSING, path2);
1802 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1803 path2) == -1)
1804 return got_error_from_errno("asprintf");
1806 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1807 if (err)
1808 goto done;
1810 if (status == GOT_STATUS_DELETE) {
1811 err = (*a->progress_cb)(a->progress_arg,
1812 GOT_STATUS_MERGE, path2);
1813 goto done;
1815 if (status != GOT_STATUS_NO_CHANGE &&
1816 status != GOT_STATUS_MODIFY &&
1817 status != GOT_STATUS_CONFLICT &&
1818 status != GOT_STATUS_ADD) {
1819 err = (*a->progress_cb)(a->progress_arg, status, path2);
1820 goto done;
1823 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1824 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1825 a->progress_cb, a->progress_arg);
1826 } else if (blob1) {
1827 ie = got_fileindex_entry_get(a->fileindex, path1);
1828 if (ie == NULL)
1829 return (*a->progress_cb)(a->progress_arg,
1830 GOT_STATUS_MISSING, path2);
1832 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1833 path1) == -1)
1834 return got_error_from_errno("asprintf");
1836 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1837 if (err)
1838 goto done;
1840 switch (status) {
1841 case GOT_STATUS_NO_CHANGE:
1842 err = (*a->progress_cb)(a->progress_arg,
1843 GOT_STATUS_DELETE, path1);
1844 if (err)
1845 goto done;
1846 err = remove_ondisk_file(a->worktree->root_path, path1);
1847 if (err)
1848 goto done;
1849 if (ie)
1850 got_fileindex_entry_mark_deleted_from_disk(ie);
1851 break;
1852 case GOT_STATUS_DELETE:
1853 case GOT_STATUS_MISSING:
1854 err = (*a->progress_cb)(a->progress_arg,
1855 GOT_STATUS_DELETE, path1);
1856 if (err)
1857 goto done;
1858 if (ie)
1859 got_fileindex_entry_mark_deleted_from_disk(ie);
1860 break;
1861 case GOT_STATUS_ADD:
1862 case GOT_STATUS_MODIFY:
1863 case GOT_STATUS_CONFLICT:
1864 err = (*a->progress_cb)(a->progress_arg,
1865 GOT_STATUS_CANNOT_DELETE, path1);
1866 if (err)
1867 goto done;
1868 break;
1869 case GOT_STATUS_OBSTRUCTED:
1870 err = (*a->progress_cb)(a->progress_arg, status, path1);
1871 if (err)
1872 goto done;
1873 break;
1874 default:
1875 break;
1877 } else if (blob2) {
1878 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1879 path2) == -1)
1880 return got_error_from_errno("asprintf");
1881 ie = got_fileindex_entry_get(a->fileindex, path2);
1882 if (ie) {
1883 err = get_file_status(&status, &sb, ie, ondisk_path,
1884 repo);
1885 if (err)
1886 goto done;
1887 if (status != GOT_STATUS_NO_CHANGE &&
1888 status != GOT_STATUS_MODIFY &&
1889 status != GOT_STATUS_CONFLICT &&
1890 status != GOT_STATUS_ADD) {
1891 err = (*a->progress_cb)(a->progress_arg,
1892 status, path2);
1893 goto done;
1895 err = merge_blob(&local_changes_subsumed, a->worktree,
1896 NULL, ondisk_path, path2, sb.st_mode, blob2,
1897 a->commit_id2, repo,
1898 a->progress_cb, a->progress_arg);
1899 if (status == GOT_STATUS_DELETE) {
1900 err = update_blob_fileindex_entry(a->worktree,
1901 a->fileindex, ie, ondisk_path, ie->path,
1902 blob2, 0);
1903 if (err)
1904 goto done;
1906 } else {
1907 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1908 err = install_blob(a->worktree, ondisk_path, path2,
1909 /* XXX get this from parent tree! */
1910 GOT_DEFAULT_FILE_MODE,
1911 sb.st_mode, blob2, 0, 0, repo,
1912 a->progress_cb, a->progress_arg);
1913 if (err)
1914 goto done;
1915 err = got_fileindex_entry_alloc(&ie,
1916 ondisk_path, path2, NULL, NULL);
1917 if (err)
1918 goto done;
1919 err = got_fileindex_entry_add(a->fileindex, ie);
1920 if (err) {
1921 got_fileindex_entry_free(ie);
1922 goto done;
1926 done:
1927 free(ondisk_path);
1928 return err;
1931 struct check_merge_ok_arg {
1932 struct got_worktree *worktree;
1933 struct got_repository *repo;
1936 static const struct got_error *
1937 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1939 const struct got_error *err = NULL;
1940 struct check_merge_ok_arg *a = arg;
1941 unsigned char status;
1942 struct stat sb;
1943 char *ondisk_path;
1945 /* Reject merges into a work tree with mixed base commits. */
1946 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1947 SHA1_DIGEST_LENGTH))
1948 return got_error(GOT_ERR_MIXED_COMMITS);
1950 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1951 == -1)
1952 return got_error_from_errno("asprintf");
1954 /* Reject merges into a work tree with conflicted files. */
1955 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1956 if (err)
1957 return err;
1958 if (status == GOT_STATUS_CONFLICT)
1959 return got_error(GOT_ERR_CONFLICTS);
1961 return NULL;
1964 static const struct got_error *
1965 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1966 const char *fileindex_path, struct got_object_id *commit_id1,
1967 struct got_object_id *commit_id2, struct got_repository *repo,
1968 got_worktree_checkout_cb progress_cb, void *progress_arg,
1969 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1971 const struct got_error *err = NULL, *sync_err;
1972 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1973 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1974 struct merge_file_cb_arg arg;
1976 if (commit_id1) {
1977 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1978 worktree->path_prefix);
1979 if (err)
1980 goto done;
1982 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1983 if (err)
1984 goto done;
1987 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1988 worktree->path_prefix);
1989 if (err)
1990 goto done;
1992 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1993 if (err)
1994 goto done;
1996 arg.worktree = worktree;
1997 arg.fileindex = fileindex;
1998 arg.progress_cb = progress_cb;
1999 arg.progress_arg = progress_arg;
2000 arg.cancel_cb = cancel_cb;
2001 arg.cancel_arg = cancel_arg;
2002 arg.commit_id2 = commit_id2;
2003 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2004 sync_err = sync_fileindex(fileindex, fileindex_path);
2005 if (sync_err && err == NULL)
2006 err = sync_err;
2007 done:
2008 if (tree1)
2009 got_object_tree_close(tree1);
2010 if (tree2)
2011 got_object_tree_close(tree2);
2012 return err;
2015 const struct got_error *
2016 got_worktree_merge_files(struct got_worktree *worktree,
2017 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2018 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2019 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2021 const struct got_error *err, *unlockerr;
2022 char *fileindex_path = NULL;
2023 struct got_fileindex *fileindex = NULL;
2024 struct check_merge_ok_arg mok_arg;
2026 err = lock_worktree(worktree, LOCK_EX);
2027 if (err)
2028 return err;
2030 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2031 if (err)
2032 goto done;
2034 mok_arg.worktree = worktree;
2035 mok_arg.repo = repo;
2036 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2037 &mok_arg);
2038 if (err)
2039 goto done;
2041 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2042 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2043 done:
2044 if (fileindex)
2045 got_fileindex_free(fileindex);
2046 free(fileindex_path);
2047 unlockerr = lock_worktree(worktree, LOCK_SH);
2048 if (unlockerr && err == NULL)
2049 err = unlockerr;
2050 return err;
2053 struct diff_dir_cb_arg {
2054 struct got_fileindex *fileindex;
2055 struct got_worktree *worktree;
2056 const char *status_path;
2057 size_t status_path_len;
2058 struct got_repository *repo;
2059 got_worktree_status_cb status_cb;
2060 void *status_arg;
2061 got_worktree_cancel_cb cancel_cb;
2062 void *cancel_arg;
2065 static const struct got_error *
2066 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2067 got_worktree_status_cb status_cb, void *status_arg,
2068 struct got_repository *repo)
2070 const struct got_error *err = NULL;
2071 unsigned char status = GOT_STATUS_NO_CHANGE;
2072 struct stat sb;
2073 struct got_object_id blob_id, commit_id;
2075 err = get_file_status(&status, &sb, ie, abspath, repo);
2076 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2077 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2078 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2079 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2080 &commit_id);
2082 return err;
2085 static const struct got_error *
2086 status_old_new(void *arg, struct got_fileindex_entry *ie,
2087 struct dirent *de, const char *parent_path)
2089 const struct got_error *err = NULL;
2090 struct diff_dir_cb_arg *a = arg;
2091 char *abspath;
2093 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2094 return got_error(GOT_ERR_CANCELLED);
2096 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2097 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2098 return NULL;
2100 if (parent_path[0]) {
2101 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2102 parent_path, de->d_name) == -1)
2103 return got_error_from_errno("asprintf");
2104 } else {
2105 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2106 de->d_name) == -1)
2107 return got_error_from_errno("asprintf");
2110 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2111 a->repo);
2112 free(abspath);
2113 return err;
2116 static const struct got_error *
2117 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2119 struct diff_dir_cb_arg *a = arg;
2120 struct got_object_id blob_id, commit_id;
2121 unsigned char status;
2123 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2124 return got_error(GOT_ERR_CANCELLED);
2126 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2127 return NULL;
2129 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2130 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2131 if (got_fileindex_entry_has_file_on_disk(ie))
2132 status = GOT_STATUS_MISSING;
2133 else
2134 status = GOT_STATUS_DELETE;
2135 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2136 &commit_id);
2139 static const struct got_error *
2140 status_new(void *arg, struct dirent *de, const char *parent_path)
2142 const struct got_error *err = NULL;
2143 struct diff_dir_cb_arg *a = arg;
2144 char *path = NULL;
2146 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2147 return got_error(GOT_ERR_CANCELLED);
2149 if (de->d_type == DT_DIR)
2150 return NULL;
2152 /* XXX ignore symlinks for now */
2153 if (de->d_type == DT_LNK)
2154 return NULL;
2156 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2157 return NULL;
2159 if (parent_path[0]) {
2160 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2161 return got_error_from_errno("asprintf");
2162 } else {
2163 path = de->d_name;
2166 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2167 NULL, NULL);
2168 if (parent_path[0])
2169 free(path);
2170 return err;
2173 static const struct got_error *
2174 worktree_status(struct got_worktree *worktree, const char *path,
2175 struct got_fileindex *fileindex, struct got_repository *repo,
2176 got_worktree_status_cb status_cb, void *status_arg,
2177 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2179 const struct got_error *err = NULL;
2180 DIR *workdir = NULL;
2181 struct got_fileindex_diff_dir_cb fdiff_cb;
2182 struct diff_dir_cb_arg arg;
2183 char *ondisk_path = NULL;
2185 if (asprintf(&ondisk_path, "%s%s%s",
2186 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2187 err = got_error_from_errno("asprintf");
2188 goto done;
2190 workdir = opendir(ondisk_path);
2191 if (workdir == NULL) {
2192 if (errno == ENOTDIR || errno == ENOENT) {
2193 struct got_fileindex_entry *ie;
2194 ie = got_fileindex_entry_get(fileindex, path);
2195 if (ie == NULL) {
2196 err = got_error(GOT_ERR_BAD_PATH);
2197 goto done;
2199 err = report_file_status(ie, ondisk_path,
2200 status_cb, status_arg, repo);
2201 goto done;
2202 } else {
2203 err = got_error_from_errno2("opendir", ondisk_path);
2204 goto done;
2207 fdiff_cb.diff_old_new = status_old_new;
2208 fdiff_cb.diff_old = status_old;
2209 fdiff_cb.diff_new = status_new;
2210 arg.fileindex = fileindex;
2211 arg.worktree = worktree;
2212 arg.status_path = path;
2213 arg.status_path_len = strlen(path);
2214 arg.repo = repo;
2215 arg.status_cb = status_cb;
2216 arg.status_arg = status_arg;
2217 arg.cancel_cb = cancel_cb;
2218 arg.cancel_arg = cancel_arg;
2219 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2220 path, repo, &fdiff_cb, &arg);
2221 done:
2222 if (workdir)
2223 closedir(workdir);
2224 free(ondisk_path);
2225 return err;
2228 const struct got_error *
2229 got_worktree_status(struct got_worktree *worktree, const char *path,
2230 struct got_repository *repo, got_worktree_status_cb status_cb,
2231 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2233 const struct got_error *err = NULL;
2234 char *fileindex_path = NULL;
2235 struct got_fileindex *fileindex = NULL;
2237 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2238 if (err)
2239 return err;
2241 err = worktree_status(worktree, path, fileindex, repo,
2242 status_cb, status_arg, cancel_cb, cancel_arg);
2243 free(fileindex_path);
2244 got_fileindex_free(fileindex);
2245 return err;
2248 const struct got_error *
2249 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2250 const char *arg)
2252 const struct got_error *err = NULL;
2253 char *resolved, *path = NULL;
2254 size_t len;
2256 *wt_path = NULL;
2258 resolved = realpath(arg, NULL);
2259 if (resolved == NULL)
2260 return got_error_from_errno2("realpath", arg);
2262 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2263 strlen(got_worktree_get_root_path(worktree)))) {
2264 err = got_error(GOT_ERR_BAD_PATH);
2265 goto done;
2268 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2269 err = got_path_skip_common_ancestor(&path,
2270 got_worktree_get_root_path(worktree), resolved);
2271 if (err)
2272 goto done;
2273 } else {
2274 path = strdup("");
2275 if (path == NULL) {
2276 err = got_error_from_errno("strdup");
2277 goto done;
2281 /* XXX status walk can't deal with trailing slash! */
2282 len = strlen(path);
2283 while (path[len - 1] == '/') {
2284 path[len - 1] = '\0';
2285 len--;
2287 done:
2288 free(resolved);
2289 if (err == NULL)
2290 *wt_path = path;
2291 else
2292 free(path);
2293 return err;
2296 static const struct got_error *
2297 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2298 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2299 struct got_repository *repo)
2301 const struct got_error *err = NULL;
2302 struct got_fileindex_entry *ie;
2304 /* Re-adding an existing entry is a no-op. */
2305 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2306 return NULL;
2308 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2309 if (err)
2310 return err;
2312 err = got_fileindex_entry_add(fileindex, ie);
2313 if (err) {
2314 got_fileindex_entry_free(ie);
2315 return err;
2318 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2321 const struct got_error *
2322 got_worktree_schedule_add(struct got_worktree *worktree,
2323 struct got_pathlist_head *ondisk_paths,
2324 got_worktree_status_cb status_cb, void *status_arg,
2325 struct got_repository *repo)
2327 struct got_fileindex *fileindex = NULL;
2328 char *fileindex_path = NULL;
2329 const struct got_error *err = NULL, *sync_err, *unlockerr;
2330 struct got_pathlist_entry *pe;
2332 err = lock_worktree(worktree, LOCK_EX);
2333 if (err)
2334 return err;
2336 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2337 if (err)
2338 goto done;
2340 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2341 char *relpath;
2342 err = got_path_skip_common_ancestor(&relpath,
2343 got_worktree_get_root_path(worktree), pe->path);
2344 if (err)
2345 break;
2346 err = schedule_addition(pe->path, fileindex, relpath,
2347 status_cb, status_arg, repo);
2348 free(relpath);
2349 if (err)
2350 break;
2352 sync_err = sync_fileindex(fileindex, fileindex_path);
2353 if (sync_err && err == NULL)
2354 err = sync_err;
2355 done:
2356 free(fileindex_path);
2357 if (fileindex)
2358 got_fileindex_free(fileindex);
2359 unlockerr = lock_worktree(worktree, LOCK_SH);
2360 if (unlockerr && err == NULL)
2361 err = unlockerr;
2362 return err;
2365 static const struct got_error *
2366 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2367 const char *relpath, int delete_local_mods,
2368 got_worktree_status_cb status_cb, void *status_arg,
2369 struct got_repository *repo)
2371 const struct got_error *err = NULL;
2372 struct got_fileindex_entry *ie = NULL;
2373 unsigned char status;
2374 struct stat sb;
2376 ie = got_fileindex_entry_get(fileindex, relpath);
2377 if (ie == NULL)
2378 return got_error(GOT_ERR_BAD_PATH);
2380 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2381 if (err)
2382 return err;
2384 if (status != GOT_STATUS_NO_CHANGE) {
2385 if (status == GOT_STATUS_DELETE)
2386 return got_error_set_errno(ENOENT, ondisk_path);
2387 if (status != GOT_STATUS_MODIFY)
2388 return got_error(GOT_ERR_FILE_STATUS);
2389 if (!delete_local_mods)
2390 return got_error(GOT_ERR_FILE_MODIFIED);
2393 if (unlink(ondisk_path) != 0)
2394 return got_error_from_errno2("unlink", ondisk_path);
2396 got_fileindex_entry_mark_deleted_from_disk(ie);
2397 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2400 const struct got_error *
2401 got_worktree_schedule_delete(struct got_worktree *worktree,
2402 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2403 got_worktree_status_cb status_cb, void *status_arg,
2404 struct got_repository *repo)
2406 struct got_fileindex *fileindex = NULL;
2407 char *fileindex_path = NULL;
2408 const struct got_error *err = NULL, *sync_err, *unlockerr;
2409 struct got_pathlist_entry *pe;
2411 err = lock_worktree(worktree, LOCK_EX);
2412 if (err)
2413 return err;
2415 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2416 if (err)
2417 goto done;
2419 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2420 char *relpath;
2421 err = got_path_skip_common_ancestor(&relpath,
2422 got_worktree_get_root_path(worktree), pe->path);
2423 if (err)
2424 break;
2425 err = schedule_for_deletion(pe->path, fileindex, relpath,
2426 delete_local_mods, status_cb, status_arg, repo);
2427 free(relpath);
2428 if (err)
2429 break;
2431 sync_err = sync_fileindex(fileindex, fileindex_path);
2432 if (sync_err && err == NULL)
2433 err = sync_err;
2434 done:
2435 free(fileindex_path);
2436 if (fileindex)
2437 got_fileindex_free(fileindex);
2438 unlockerr = lock_worktree(worktree, LOCK_SH);
2439 if (unlockerr && err == NULL)
2440 err = unlockerr;
2441 return err;
2444 static const struct got_error *
2445 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2446 const char *ondisk_path,
2447 got_worktree_checkout_cb progress_cb, void *progress_arg,
2448 struct got_repository *repo)
2450 const struct got_error *err = NULL;
2451 char *relpath = NULL, *parent_path = NULL;
2452 struct got_fileindex_entry *ie;
2453 struct got_tree_object *tree = NULL;
2454 struct got_object_id *tree_id = NULL;
2455 const struct got_tree_entry *te;
2456 char *tree_path = NULL, *te_name;
2457 struct got_blob_object *blob = NULL;
2458 unsigned char status;
2459 struct stat sb;
2461 err = got_path_skip_common_ancestor(&relpath,
2462 got_worktree_get_root_path(worktree), ondisk_path);
2463 if (err)
2464 goto done;
2466 ie = got_fileindex_entry_get(fileindex, relpath);
2467 if (ie == NULL) {
2468 err = got_error(GOT_ERR_BAD_PATH);
2469 goto done;
2472 /* Construct in-repository path of tree which contains this blob. */
2473 err = got_path_dirname(&parent_path, ie->path);
2474 if (err) {
2475 if (err->code != GOT_ERR_BAD_PATH)
2476 goto done;
2477 parent_path = strdup("/");
2478 if (parent_path == NULL) {
2479 err = got_error_from_errno("strdup");
2480 goto done;
2483 if (got_path_is_root_dir(worktree->path_prefix)) {
2484 tree_path = strdup(parent_path);
2485 if (tree_path == NULL) {
2486 err = got_error_from_errno("strdup");
2487 goto done;
2489 } else {
2490 if (got_path_is_root_dir(parent_path)) {
2491 tree_path = strdup(worktree->path_prefix);
2492 if (tree_path == NULL) {
2493 err = got_error_from_errno("strdup");
2494 goto done;
2496 } else {
2497 if (asprintf(&tree_path, "%s/%s",
2498 worktree->path_prefix, parent_path) == -1) {
2499 err = got_error_from_errno("asprintf");
2500 goto done;
2505 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2506 tree_path);
2507 if (err)
2508 goto done;
2510 err = got_object_open_as_tree(&tree, repo, tree_id);
2511 if (err)
2512 goto done;
2514 te_name = basename(ie->path);
2515 if (te_name == NULL) {
2516 err = got_error_from_errno2("basename", ie->path);
2517 goto done;
2520 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2521 if (err)
2522 goto done;
2524 te = got_object_tree_find_entry(tree, te_name);
2525 if (te == NULL && status != GOT_STATUS_ADD) {
2526 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2527 goto done;
2530 switch (status) {
2531 case GOT_STATUS_ADD:
2532 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2533 if (err)
2534 goto done;
2535 got_fileindex_entry_remove(fileindex, ie);
2536 break;
2537 case GOT_STATUS_DELETE:
2538 case GOT_STATUS_MODIFY:
2539 case GOT_STATUS_CONFLICT:
2540 case GOT_STATUS_MISSING: {
2541 struct got_object_id id;
2542 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2543 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2544 if (err)
2545 goto done;
2546 err = install_blob(worktree, ondisk_path, ie->path,
2547 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2548 progress_arg);
2549 if (err)
2550 goto done;
2551 if (status == GOT_STATUS_DELETE) {
2552 err = update_blob_fileindex_entry(worktree,
2553 fileindex, ie, ondisk_path, ie->path, blob, 1);
2554 if (err)
2555 goto done;
2557 break;
2559 default:
2560 goto done;
2562 done:
2563 free(relpath);
2564 free(parent_path);
2565 free(tree_path);
2566 if (blob)
2567 got_object_blob_close(blob);
2568 if (tree)
2569 got_object_tree_close(tree);
2570 free(tree_id);
2571 return err;
2574 const struct got_error *
2575 got_worktree_revert(struct got_worktree *worktree,
2576 struct got_pathlist_head *ondisk_paths,
2577 got_worktree_checkout_cb progress_cb, void *progress_arg,
2578 struct got_repository *repo)
2580 struct got_fileindex *fileindex = NULL;
2581 char *fileindex_path = NULL;
2582 const struct got_error *err = NULL, *unlockerr = NULL;
2583 const struct got_error *sync_err = NULL;
2584 struct got_pathlist_entry *pe;
2586 err = lock_worktree(worktree, LOCK_EX);
2587 if (err)
2588 return err;
2590 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2591 if (err)
2592 goto done;
2594 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2595 err = revert_file(worktree, fileindex, pe->path,
2596 progress_cb, progress_arg, repo);
2597 if (err)
2598 break;
2600 sync_err = sync_fileindex(fileindex, fileindex_path);
2601 if (sync_err && err == NULL)
2602 err = sync_err;
2603 done:
2604 free(fileindex_path);
2605 if (fileindex)
2606 got_fileindex_free(fileindex);
2607 unlockerr = lock_worktree(worktree, LOCK_SH);
2608 if (unlockerr && err == NULL)
2609 err = unlockerr;
2610 return err;
2613 static void
2614 free_commitable(struct got_commitable *ct)
2616 free(ct->path);
2617 free(ct->in_repo_path);
2618 free(ct->ondisk_path);
2619 free(ct->blob_id);
2620 free(ct->base_blob_id);
2621 free(ct->base_commit_id);
2622 free(ct);
2625 struct collect_commitables_arg {
2626 struct got_pathlist_head *commitable_paths;
2627 struct got_repository *repo;
2628 struct got_worktree *worktree;
2631 static const struct got_error *
2632 collect_commitables(void *arg, unsigned char status, const char *relpath,
2633 struct got_object_id *blob_id, struct got_object_id *commit_id)
2635 struct collect_commitables_arg *a = arg;
2636 const struct got_error *err = NULL;
2637 struct got_commitable *ct = NULL;
2638 struct got_pathlist_entry *new = NULL;
2639 char *parent_path = NULL, *path = NULL;
2640 struct stat sb;
2642 if (status == GOT_STATUS_CONFLICT)
2643 return got_error(GOT_ERR_COMMIT_CONFLICT);
2645 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2646 status != GOT_STATUS_DELETE)
2647 return NULL;
2649 if (asprintf(&path, "/%s", relpath) == -1) {
2650 err = got_error_from_errno("asprintf");
2651 goto done;
2653 if (strcmp(path, "/") == 0) {
2654 parent_path = strdup("");
2655 if (parent_path == NULL)
2656 return got_error_from_errno("strdup");
2657 } else {
2658 err = got_path_dirname(&parent_path, path);
2659 if (err)
2660 return err;
2663 ct = calloc(1, sizeof(*ct));
2664 if (ct == NULL) {
2665 err = got_error_from_errno("calloc");
2666 goto done;
2669 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2670 relpath) == -1) {
2671 err = got_error_from_errno("asprintf");
2672 goto done;
2674 if (status == GOT_STATUS_DELETE) {
2675 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2676 } else {
2677 if (lstat(ct->ondisk_path, &sb) != 0) {
2678 err = got_error_from_errno2("lstat", ct->ondisk_path);
2679 goto done;
2681 ct->mode = sb.st_mode;
2684 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2685 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2686 relpath) == -1) {
2687 err = got_error_from_errno("asprintf");
2688 goto done;
2691 ct->status = status;
2692 ct->blob_id = NULL; /* will be filled in when blob gets created */
2693 if (ct->status != GOT_STATUS_ADD) {
2694 ct->base_blob_id = got_object_id_dup(blob_id);
2695 if (ct->base_blob_id == NULL) {
2696 err = got_error_from_errno("got_object_id_dup");
2697 goto done;
2699 ct->base_commit_id = got_object_id_dup(commit_id);
2700 if (ct->base_commit_id == NULL) {
2701 err = got_error_from_errno("got_object_id_dup");
2702 goto done;
2705 ct->path = strdup(path);
2706 if (ct->path == NULL) {
2707 err = got_error_from_errno("strdup");
2708 goto done;
2710 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2711 done:
2712 if (ct && (err || new == NULL))
2713 free_commitable(ct);
2714 free(parent_path);
2715 free(path);
2716 return err;
2719 static const struct got_error *write_tree(struct got_object_id **,
2720 struct got_tree_object *, const char *, struct got_pathlist_head *,
2721 got_worktree_status_cb status_cb, void *status_arg,
2722 struct got_repository *);
2724 static const struct got_error *
2725 write_subtree(struct got_object_id **new_subtree_id,
2726 struct got_tree_entry *te, const char *parent_path,
2727 struct got_pathlist_head *commitable_paths,
2728 got_worktree_status_cb status_cb, void *status_arg,
2729 struct got_repository *repo)
2731 const struct got_error *err = NULL;
2732 struct got_tree_object *subtree;
2733 char *subpath;
2735 if (asprintf(&subpath, "%s%s%s", parent_path,
2736 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2737 return got_error_from_errno("asprintf");
2739 err = got_object_open_as_tree(&subtree, repo, te->id);
2740 if (err)
2741 return err;
2743 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2744 status_cb, status_arg, repo);
2745 got_object_tree_close(subtree);
2746 free(subpath);
2747 return err;
2750 static const struct got_error *
2751 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2753 const struct got_error *err = NULL;
2754 char *ct_parent_path = NULL;
2756 *match = 0;
2758 if (strchr(ct->path, '/') == NULL) {
2759 *match = got_path_is_root_dir(path);
2760 return NULL;
2763 err = got_path_dirname(&ct_parent_path, ct->path);
2764 if (err)
2765 return err;
2766 *match = (strcmp(path, ct_parent_path) == 0);
2767 free(ct_parent_path);
2768 return err;
2771 static mode_t
2772 get_ct_file_mode(struct got_commitable *ct)
2774 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2777 static const struct got_error *
2778 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2779 struct got_tree_entry *te, struct got_commitable *ct)
2781 const struct got_error *err = NULL;
2783 *new_te = NULL;
2785 err = got_object_tree_entry_dup(new_te, te);
2786 if (err)
2787 goto done;
2789 (*new_te)->mode = get_ct_file_mode(ct);
2791 free((*new_te)->id);
2792 (*new_te)->id = got_object_id_dup(ct->blob_id);
2793 if ((*new_te)->id == NULL) {
2794 err = got_error_from_errno("got_object_id_dup");
2795 goto done;
2797 done:
2798 if (err && *new_te) {
2799 got_object_tree_entry_close(*new_te);
2800 *new_te = NULL;
2802 return err;
2805 static const struct got_error *
2806 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2807 struct got_commitable *ct)
2809 const struct got_error *err = NULL;
2810 char *ct_name;
2812 *new_te = NULL;
2814 *new_te = calloc(1, sizeof(**new_te));
2815 if (*new_te == NULL)
2816 return got_error_from_errno("calloc");
2818 ct_name = basename(ct->path);
2819 if (ct_name == NULL) {
2820 err = got_error_from_errno2("basename", ct->path);
2821 goto done;
2823 (*new_te)->name = strdup(ct_name);
2824 if ((*new_te)->name == NULL) {
2825 err = got_error_from_errno("strdup");
2826 goto done;
2829 (*new_te)->mode = get_ct_file_mode(ct);
2831 (*new_te)->id = got_object_id_dup(ct->blob_id);
2832 if ((*new_te)->id == NULL) {
2833 err = got_error_from_errno("got_object_id_dup");
2834 goto done;
2836 done:
2837 if (err && *new_te) {
2838 got_object_tree_entry_close(*new_te);
2839 *new_te = NULL;
2841 return err;
2844 static const struct got_error *
2845 insert_tree_entry(struct got_tree_entry *new_te,
2846 struct got_pathlist_head *paths)
2848 const struct got_error *err = NULL;
2849 struct got_pathlist_entry *new_pe;
2851 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2852 if (err)
2853 return err;
2854 if (new_pe == NULL)
2855 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2856 return NULL;
2859 static const struct got_error *
2860 report_ct_status(struct got_commitable *ct,
2861 got_worktree_status_cb status_cb, void *status_arg)
2863 const char *ct_path = ct->path;
2864 while (ct_path[0] == '/')
2865 ct_path++;
2866 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2869 static const struct got_error *
2870 match_modified_subtree(int *modified, struct got_tree_entry *te,
2871 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2873 const struct got_error *err = NULL;
2874 struct got_pathlist_entry *pe;
2875 char *te_path;
2877 *modified = 0;
2879 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2880 got_path_is_root_dir(base_tree_path) ? "" : "/",
2881 te->name) == -1)
2882 return got_error_from_errno("asprintf");
2884 TAILQ_FOREACH(pe, commitable_paths, entry) {
2885 struct got_commitable *ct = pe->data;
2886 *modified = got_path_is_child(ct->in_repo_path, te_path,
2887 strlen(te_path));
2888 if (*modified)
2889 break;
2892 free(te_path);
2893 return err;
2896 static const struct got_error *
2897 match_deleted_or_modified_ct(struct got_commitable **ctp,
2898 struct got_tree_entry *te, const char *base_tree_path,
2899 struct got_pathlist_head *commitable_paths)
2901 const struct got_error *err = NULL;
2902 struct got_pathlist_entry *pe;
2904 *ctp = NULL;
2906 TAILQ_FOREACH(pe, commitable_paths, entry) {
2907 struct got_commitable *ct = pe->data;
2908 char *ct_name = NULL;
2909 int path_matches;
2911 if (ct->status != GOT_STATUS_MODIFY &&
2912 ct->status != GOT_STATUS_DELETE)
2913 continue;
2915 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2916 continue;
2918 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2919 if (err)
2920 return err;
2921 if (!path_matches)
2922 continue;
2924 ct_name = basename(pe->path);
2925 if (ct_name == NULL)
2926 return got_error_from_errno2("basename", pe->path);
2928 if (strcmp(te->name, ct_name) != 0)
2929 continue;
2931 *ctp = ct;
2932 break;
2935 return err;
2938 static const struct got_error *
2939 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2940 const char *child_path, const char *path_base_tree,
2941 struct got_pathlist_head *commitable_paths,
2942 got_worktree_status_cb status_cb, void *status_arg,
2943 struct got_repository *repo)
2945 const struct got_error *err = NULL;
2946 struct got_tree_entry *new_te;
2947 char *subtree_path;
2949 *new_tep = NULL;
2951 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2952 got_path_is_root_dir(path_base_tree) ? "" : "/",
2953 child_path) == -1)
2954 return got_error_from_errno("asprintf");
2956 new_te = calloc(1, sizeof(*new_te));
2957 new_te->mode = S_IFDIR;
2958 new_te->name = strdup(child_path);
2959 if (new_te->name == NULL) {
2960 err = got_error_from_errno("strdup");
2961 got_object_tree_entry_close(new_te);
2962 goto done;
2964 err = write_tree(&new_te->id, NULL, subtree_path,
2965 commitable_paths, status_cb, status_arg, repo);
2966 if (err) {
2967 got_object_tree_entry_close(new_te);
2968 goto done;
2970 done:
2971 free(subtree_path);
2972 if (err == NULL)
2973 *new_tep = new_te;
2974 return err;
2977 static const struct got_error *
2978 write_tree(struct got_object_id **new_tree_id,
2979 struct got_tree_object *base_tree, const char *path_base_tree,
2980 struct got_pathlist_head *commitable_paths,
2981 got_worktree_status_cb status_cb, void *status_arg,
2982 struct got_repository *repo)
2984 const struct got_error *err = NULL;
2985 const struct got_tree_entries *base_entries = NULL;
2986 struct got_pathlist_head paths;
2987 struct got_tree_entries new_tree_entries;
2988 struct got_tree_entry *te, *new_te = NULL;
2989 struct got_pathlist_entry *pe;
2991 TAILQ_INIT(&paths);
2992 new_tree_entries.nentries = 0;
2993 SIMPLEQ_INIT(&new_tree_entries.head);
2995 /* Insert, and recurse into, newly added entries first. */
2996 TAILQ_FOREACH(pe, commitable_paths, entry) {
2997 struct got_commitable *ct = pe->data;
2998 char *child_path = NULL, *slash;
3000 if (ct->status != GOT_STATUS_ADD ||
3001 (ct->flags & GOT_COMMITABLE_ADDED))
3002 continue;
3004 if (!got_path_is_child(pe->path, path_base_tree,
3005 strlen(path_base_tree)))
3006 continue;
3008 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3009 pe->path);
3010 if (err)
3011 goto done;
3013 slash = strchr(child_path, '/');
3014 if (slash == NULL) {
3015 err = alloc_added_blob_tree_entry(&new_te, ct);
3016 if (err)
3017 goto done;
3018 err = report_ct_status(ct, status_cb, status_arg);
3019 if (err)
3020 goto done;
3021 ct->flags |= GOT_COMMITABLE_ADDED;
3022 err = insert_tree_entry(new_te, &paths);
3023 if (err)
3024 goto done;
3025 } else {
3026 *slash = '\0'; /* trim trailing path components */
3027 if (base_tree == NULL ||
3028 got_object_tree_find_entry(base_tree, child_path)
3029 == NULL) {
3030 err = make_subtree_for_added_blob(&new_te,
3031 child_path, path_base_tree,
3032 commitable_paths, status_cb, status_arg,
3033 repo);
3034 if (err)
3035 goto done;
3036 err = insert_tree_entry(new_te, &paths);
3037 if (err)
3038 goto done;
3043 if (base_tree) {
3044 /* Handle modified and deleted entries. */
3045 base_entries = got_object_tree_get_entries(base_tree);
3046 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3047 struct got_commitable *ct = NULL;
3049 if (S_ISDIR(te->mode)) {
3050 int modified;
3051 err = got_object_tree_entry_dup(&new_te, te);
3052 if (err)
3053 goto done;
3054 err = match_modified_subtree(&modified, te,
3055 path_base_tree, commitable_paths);
3056 if (err)
3057 goto done;
3058 /* Avoid recursion into unmodified subtrees. */
3059 if (modified) {
3060 free(new_te->id);
3061 err = write_subtree(&new_te->id, te,
3062 path_base_tree, commitable_paths,
3063 status_cb, status_arg, repo);
3064 if (err)
3065 goto done;
3067 err = insert_tree_entry(new_te, &paths);
3068 if (err)
3069 goto done;
3070 continue;
3073 err = match_deleted_or_modified_ct(&ct, te,
3074 path_base_tree, commitable_paths);
3075 if (ct) {
3076 /* NB: Deleted entries get dropped here. */
3077 if (ct->status == GOT_STATUS_MODIFY) {
3078 err = alloc_modified_blob_tree_entry(
3079 &new_te, te, ct);
3080 if (err)
3081 goto done;
3082 err = insert_tree_entry(new_te, &paths);
3083 if (err)
3084 goto done;
3086 err = report_ct_status(ct, status_cb,
3087 status_arg);
3088 if (err)
3089 goto done;
3090 } else {
3091 /* Entry is unchanged; just copy it. */
3092 err = got_object_tree_entry_dup(&new_te, te);
3093 if (err)
3094 goto done;
3095 err = insert_tree_entry(new_te, &paths);
3096 if (err)
3097 goto done;
3102 /* Write new list of entries; deleted entries have been dropped. */
3103 TAILQ_FOREACH(pe, &paths, entry) {
3104 struct got_tree_entry *te = pe->data;
3105 new_tree_entries.nentries++;
3106 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3108 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3109 done:
3110 got_object_tree_entries_close(&new_tree_entries);
3111 got_pathlist_free(&paths);
3112 return err;
3115 static const struct got_error *
3116 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3117 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3119 const struct got_error *err = NULL;
3120 struct got_pathlist_entry *pe;
3122 TAILQ_FOREACH(pe, commitable_paths, entry) {
3123 struct got_fileindex_entry *ie;
3124 struct got_commitable *ct = pe->data;
3126 ie = got_fileindex_entry_get(fileindex, pe->path);
3127 if (ie) {
3128 if (ct->status == GOT_STATUS_DELETE) {
3129 got_fileindex_entry_remove(fileindex, ie);
3130 got_fileindex_entry_free(ie);
3131 } else
3132 err = got_fileindex_entry_update(ie,
3133 ct->ondisk_path, ct->blob_id->sha1,
3134 new_base_commit_id->sha1, 1);
3135 } else {
3136 err = got_fileindex_entry_alloc(&ie,
3137 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3138 new_base_commit_id->sha1);
3139 if (err)
3140 break;
3141 err = got_fileindex_entry_add(fileindex, ie);
3142 if (err)
3143 break;
3146 return err;
3149 static const struct got_error *
3150 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3151 struct got_object_id *head_commit_id)
3153 const struct got_error *err = NULL;
3154 struct got_object_id *id_in_head = NULL, *id = NULL;
3155 struct got_commit_object *commit = NULL;
3156 char *path = NULL;
3157 const char *ct_path = ct->in_repo_path;
3159 while (ct_path[0] == '/')
3160 ct_path++;
3163 * Ensure that no modifications were made to files *and their parents*
3164 * in commits between the file's base commit and the branch head.
3166 * Checking the parents is important for detecting conflicting tree
3167 * configurations (files or parent folders might have been moved,
3168 * deleted, added again, etc.). Such changes need to be merged with
3169 * local changes before a commit can occur.
3171 * The implication is that the file's (parent) entry in the root
3172 * directory must have the same ID in all relevant commits.
3174 if (ct->status != GOT_STATUS_ADD) {
3175 struct got_object_qid *pid;
3176 char *slash;
3177 struct got_object_id *root_entry_id = NULL;
3179 /* Trivial case: base commit == head commit */
3180 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3181 return NULL;
3183 /* Compute the path to the root directory's entry. */
3184 path = strdup(ct_path);
3185 if (path == NULL) {
3186 err = got_error_from_errno("strdup");
3187 goto done;
3189 slash = strchr(path, '/');
3190 if (slash)
3191 *slash = '\0';
3193 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3194 if (err)
3195 goto done;
3197 err = got_object_id_by_path(&root_entry_id, repo,
3198 head_commit_id, path);
3199 if (err)
3200 goto done;
3202 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3203 while (pid) {
3204 struct got_commit_object *pcommit;
3206 err = got_object_id_by_path(&id, repo, pid->id, path);
3207 if (err) {
3208 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3209 goto done;
3210 err = NULL;
3211 break;
3214 err = got_object_id_by_path(&id, repo, pid->id, path);
3215 if (err)
3216 goto done;
3218 if (got_object_id_cmp(id, root_entry_id) != 0) {
3219 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3220 break;
3223 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3224 break; /* all relevant commits scanned */
3226 err = got_object_open_as_commit(&pcommit, repo,
3227 pid->id);
3228 if (err)
3229 goto done;
3231 got_object_commit_close(commit);
3232 commit = pcommit;
3233 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3234 commit));
3236 } else {
3237 /* Require that added files don't exist in the branch head. */
3238 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3239 ct_path);
3240 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3241 goto done;
3242 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3244 done:
3245 if (commit)
3246 got_object_commit_close(commit);
3247 free(id_in_head);
3248 free(id);
3249 free(path);
3250 return err;
3253 const struct got_error *
3254 commit_worktree(struct got_object_id **new_commit_id,
3255 struct got_pathlist_head *commitable_paths,
3256 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3257 const char *ondisk_path, const char *author, const char *committer,
3258 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3259 got_worktree_status_cb status_cb, void *status_arg,
3260 struct got_repository *repo)
3262 const struct got_error *err = NULL, *unlockerr = NULL;
3263 struct got_pathlist_entry *pe;
3264 const char *head_ref_name = NULL;
3265 struct got_commit_object *head_commit = NULL;
3266 struct got_reference *head_ref2 = NULL;
3267 struct got_object_id *head_commit_id2 = NULL;
3268 struct got_tree_object *head_tree = NULL;
3269 struct got_object_id *new_tree_id = NULL;
3270 struct got_object_id_queue parent_ids;
3271 struct got_object_qid *pid = NULL;
3272 char *logmsg = NULL;
3274 *new_commit_id = NULL;
3276 SIMPLEQ_INIT(&parent_ids);
3278 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3279 if (err)
3280 goto done;
3282 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3283 if (err)
3284 goto done;
3286 if (commit_msg_cb != NULL) {
3287 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3288 if (err)
3289 goto done;
3292 if (logmsg == NULL || strlen(logmsg) == 0) {
3293 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3294 goto done;
3297 /* Create blobs from added and modified files and record their IDs. */
3298 TAILQ_FOREACH(pe, commitable_paths, entry) {
3299 struct got_commitable *ct = pe->data;
3300 char *ondisk_path;
3302 if (ct->status != GOT_STATUS_ADD &&
3303 ct->status != GOT_STATUS_MODIFY)
3304 continue;
3306 if (asprintf(&ondisk_path, "%s/%s",
3307 worktree->root_path, pe->path) == -1) {
3308 err = got_error_from_errno("asprintf");
3309 goto done;
3311 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3312 free(ondisk_path);
3313 if (err)
3314 goto done;
3317 /* Recursively write new tree objects. */
3318 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3319 status_cb, status_arg, repo);
3320 if (err)
3321 goto done;
3323 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3324 if (err)
3325 goto done;
3326 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3327 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3328 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3329 got_object_qid_free(pid);
3330 if (logmsg != NULL)
3331 free(logmsg);
3332 if (err)
3333 goto done;
3335 /* Check if a concurrent commit to our branch has occurred. */
3336 head_ref_name = got_worktree_get_head_ref_name(worktree);
3337 if (head_ref_name == NULL) {
3338 err = got_error_from_errno("got_worktree_get_head_ref_name");
3339 goto done;
3341 /* Lock the reference here to prevent concurrent modification. */
3342 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3343 if (err)
3344 goto done;
3345 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3346 if (err)
3347 goto done;
3348 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3349 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3350 goto done;
3352 /* Update branch head in repository. */
3353 err = got_ref_change_ref(head_ref2, *new_commit_id);
3354 if (err)
3355 goto done;
3356 err = got_ref_write(head_ref2, repo);
3357 if (err)
3358 goto done;
3360 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3361 if (err)
3362 goto done;
3364 err = ref_base_commit(worktree, repo);
3365 if (err)
3366 goto done;
3367 done:
3368 if (head_tree)
3369 got_object_tree_close(head_tree);
3370 if (head_commit)
3371 got_object_commit_close(head_commit);
3372 free(head_commit_id2);
3373 if (head_ref2) {
3374 unlockerr = got_ref_unlock(head_ref2);
3375 if (unlockerr && err == NULL)
3376 err = unlockerr;
3377 got_ref_close(head_ref2);
3379 return err;
3382 const struct got_error *
3383 got_worktree_commit(struct got_object_id **new_commit_id,
3384 struct got_worktree *worktree, const char *ondisk_path,
3385 const char *author, const char *committer,
3386 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3387 got_worktree_status_cb status_cb, void *status_arg,
3388 struct got_repository *repo)
3390 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3391 struct got_fileindex *fileindex = NULL;
3392 char *fileindex_path = NULL, *relpath = NULL;
3393 struct got_pathlist_head commitable_paths;
3394 struct collect_commitables_arg cc_arg;
3395 struct got_pathlist_entry *pe;
3396 struct got_reference *head_ref = NULL;
3397 struct got_object_id *head_commit_id = NULL;
3399 *new_commit_id = NULL;
3401 TAILQ_INIT(&commitable_paths);
3403 err = lock_worktree(worktree, LOCK_EX);
3404 if (err)
3405 goto done;
3407 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3408 if (err)
3409 goto done;
3411 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3412 if (err)
3413 goto done;
3415 if (ondisk_path) {
3416 err = got_path_skip_common_ancestor(&relpath,
3417 worktree->root_path, ondisk_path);
3418 if (err)
3419 return err;
3422 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3423 if (err)
3424 goto done;
3426 cc_arg.commitable_paths = &commitable_paths;
3427 cc_arg.worktree = worktree;
3428 cc_arg.repo = repo;
3429 err = worktree_status(worktree, relpath ? relpath : "",
3430 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3431 if (err)
3432 goto done;
3434 if (TAILQ_EMPTY(&commitable_paths)) {
3435 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3436 goto done;
3439 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3440 struct got_commitable *ct = pe->data;
3441 err = check_ct_out_of_date(ct, repo, head_commit_id);
3442 if (err)
3443 goto done;
3446 err = commit_worktree(new_commit_id, &commitable_paths,
3447 head_commit_id, worktree, ondisk_path, author, committer,
3448 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3449 if (err)
3450 goto done;
3452 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3453 fileindex);
3454 sync_err = sync_fileindex(fileindex, fileindex_path);
3455 if (sync_err && err == NULL)
3456 err = sync_err;
3457 done:
3458 if (fileindex)
3459 got_fileindex_free(fileindex);
3460 free(fileindex_path);
3461 free(relpath);
3462 unlockerr = lock_worktree(worktree, LOCK_SH);
3463 if (unlockerr && err == NULL)
3464 err = unlockerr;
3465 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3466 struct got_commitable *ct = pe->data;
3467 free_commitable(ct);
3469 got_pathlist_free(&commitable_paths);
3470 return err;
3473 const char *
3474 got_commitable_get_path(struct got_commitable *ct)
3476 return ct->path;
3479 unsigned int
3480 got_commitable_get_status(struct got_commitable *ct)
3482 return ct->status;
3485 struct check_rebase_ok_arg {
3486 struct got_worktree *worktree;
3487 struct got_repository *repo;
3488 int rebase_in_progress;
3491 static const struct got_error *
3492 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3494 const struct got_error *err = NULL;
3495 struct check_rebase_ok_arg *a = arg;
3496 unsigned char status;
3497 struct stat sb;
3498 char *ondisk_path;
3500 if (!a->rebase_in_progress) {
3501 /* Reject rebase of a work tree with mixed base commits. */
3502 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3503 SHA1_DIGEST_LENGTH))
3504 return got_error(GOT_ERR_MIXED_COMMITS);
3507 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3508 == -1)
3509 return got_error_from_errno("asprintf");
3511 /* Reject rebase of a work tree with modified or conflicted files. */
3512 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3513 free(ondisk_path);
3514 if (err)
3515 return err;
3517 if (a->rebase_in_progress) {
3518 if (status == GOT_STATUS_CONFLICT)
3519 return got_error(GOT_ERR_CONFLICTS);
3520 } else if (status != GOT_STATUS_NO_CHANGE)
3521 return got_error(GOT_ERR_MODIFIED);
3523 return NULL;
3526 const struct got_error *
3527 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3528 struct got_reference **tmp_branch, struct got_worktree *worktree,
3529 struct got_reference *branch, struct got_repository *repo)
3531 const struct got_error *err = NULL;
3532 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3533 char *branch_ref_name = NULL;
3534 struct got_fileindex *fileindex = NULL;
3535 char *fileindex_path = NULL;
3536 struct check_rebase_ok_arg ok_arg;
3537 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3539 *new_base_branch_ref = NULL;
3540 *tmp_branch = NULL;
3542 err = lock_worktree(worktree, LOCK_EX);
3543 if (err)
3544 return err;
3546 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3547 if (err)
3548 goto done;
3550 ok_arg.worktree = worktree;
3551 ok_arg.repo = repo;
3552 ok_arg.rebase_in_progress = 0;
3553 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3554 &ok_arg);
3555 if (err)
3556 goto done;
3558 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3559 if (err)
3560 goto done;
3562 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3563 if (err)
3564 goto done;
3566 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3567 if (err)
3568 goto done;
3570 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3571 0);
3572 if (err)
3573 goto done;
3575 err = got_ref_alloc_symref(new_base_branch_ref,
3576 new_base_branch_ref_name, wt_branch);
3577 if (err)
3578 goto done;
3579 err = got_ref_write(*new_base_branch_ref, repo);
3580 if (err)
3581 goto done;
3583 /* TODO Lock original branch's ref while rebasing? */
3585 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3586 if (err)
3587 goto done;
3589 err = got_ref_write(branch_ref, repo);
3590 if (err)
3591 goto done;
3593 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3594 worktree->base_commit_id);
3595 if (err)
3596 goto done;
3597 err = got_ref_write(*tmp_branch, repo);
3598 if (err)
3599 goto done;
3601 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3602 if (err)
3603 goto done;
3604 done:
3605 free(fileindex_path);
3606 if (fileindex)
3607 got_fileindex_free(fileindex);
3608 free(tmp_branch_name);
3609 free(new_base_branch_ref_name);
3610 free(branch_ref_name);
3611 if (branch_ref)
3612 got_ref_close(branch_ref);
3613 if (wt_branch)
3614 got_ref_close(wt_branch);
3615 if (err) {
3616 if (*new_base_branch_ref) {
3617 got_ref_close(*new_base_branch_ref);
3618 *new_base_branch_ref = NULL;
3620 if (*tmp_branch) {
3621 got_ref_close(*tmp_branch);
3622 *tmp_branch = NULL;
3624 lock_worktree(worktree, LOCK_SH);
3626 return err;
3629 const struct got_error *
3630 got_worktree_rebase_continue(struct got_object_id **commit_id,
3631 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3632 struct got_reference **branch, struct got_worktree *worktree,
3633 struct got_repository *repo)
3635 const struct got_error *err;
3636 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3637 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3638 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3640 *commit_id = NULL;
3642 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3643 if (err)
3644 return err;
3646 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3647 if (err)
3648 goto done;
3650 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3651 if (err)
3652 goto done;
3654 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3655 if (err)
3656 goto done;
3658 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3659 if (err)
3660 goto done;
3662 err = got_ref_open(branch, repo,
3663 got_ref_get_symref_target(branch_ref), 0);
3664 if (err)
3665 goto done;
3667 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3668 if (err)
3669 goto done;
3671 err = got_ref_resolve(commit_id, repo, commit_ref);
3672 if (err)
3673 goto done;
3675 err = got_ref_open(new_base_branch, repo,
3676 new_base_branch_ref_name, 0);
3677 if (err)
3678 goto done;
3680 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3681 if (err)
3682 goto done;
3683 done:
3684 free(commit_ref_name);
3685 free(branch_ref_name);
3686 if (commit_ref)
3687 got_ref_close(commit_ref);
3688 if (branch_ref)
3689 got_ref_close(branch_ref);
3690 if (err) {
3691 free(*commit_id);
3692 *commit_id = NULL;
3693 if (*tmp_branch) {
3694 got_ref_close(*tmp_branch);
3695 *tmp_branch = NULL;
3697 if (*new_base_branch) {
3698 got_ref_close(*new_base_branch);
3699 *new_base_branch = NULL;
3701 if (*branch) {
3702 got_ref_close(*branch);
3703 *branch = NULL;
3706 return err;
3709 const struct got_error *
3710 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3712 const struct got_error *err;
3713 char *tmp_branch_name = NULL;
3715 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3716 if (err)
3717 return err;
3719 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3720 free(tmp_branch_name);
3721 return NULL;
3724 static const struct got_error *
3725 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3726 char **logmsg, void *arg)
3728 struct got_commit_object *commit = arg;
3730 *logmsg = strdup(got_object_commit_get_logmsg(commit));
3731 if (*logmsg == NULL)
3732 return got_error_from_errno("strdup");
3734 return NULL;
3737 static const struct got_error *
3738 rebase_status(void *arg, unsigned char status, const char *path,
3739 struct got_object_id *blob_id, struct got_object_id *commit_id)
3741 return NULL;
3744 struct collect_merged_paths_arg {
3745 got_worktree_checkout_cb progress_cb;
3746 void *progress_arg;
3747 struct got_pathlist_head *merged_paths;
3750 static const struct got_error *
3751 collect_merged_paths(void *arg, unsigned char status, const char *path)
3753 const struct got_error *err;
3754 struct collect_merged_paths_arg *a = arg;
3755 char *p;
3756 struct got_pathlist_entry *new;
3758 err = (*a->progress_cb)(a->progress_arg, status, path);
3759 if (err)
3760 return err;
3762 if (status != GOT_STATUS_MERGE &&
3763 status != GOT_STATUS_ADD &&
3764 status != GOT_STATUS_DELETE &&
3765 status != GOT_STATUS_CONFLICT)
3766 return NULL;
3768 p = strdup(path);
3769 if (p == NULL)
3770 return got_error_from_errno("strdup");
3772 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3773 if (err || new == NULL)
3774 free(p);
3775 return err;
3778 void
3779 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3781 struct got_pathlist_entry *pe;
3783 TAILQ_FOREACH(pe, merged_paths, entry)
3784 free((char *)pe->path);
3786 got_pathlist_free(merged_paths);
3789 const struct got_error *
3790 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3791 struct got_worktree *worktree, struct got_object_id *parent_commit_id,
3792 struct got_object_id *commit_id, struct got_repository *repo,
3793 got_worktree_checkout_cb progress_cb, void *progress_arg,
3794 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3796 const struct got_error *err;
3797 struct got_fileindex *fileindex;
3798 char *fileindex_path, *commit_ref_name = NULL;
3799 struct got_reference *commit_ref = NULL;
3800 struct collect_merged_paths_arg cmp_arg;
3802 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3804 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3805 if (err)
3806 return err;
3808 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3809 if (err)
3810 goto done;
3811 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3812 if (err) {
3813 if (err->code != GOT_ERR_NOT_REF)
3814 goto done;
3815 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3816 if (err)
3817 goto done;
3818 err = got_ref_write(commit_ref, repo);
3819 if (err)
3820 goto done;
3821 } else {
3822 struct got_object_id *stored_id;
3823 int cmp;
3825 err = got_ref_resolve(&stored_id, repo, commit_ref);
3826 if (err)
3827 goto done;
3828 cmp = got_object_id_cmp(commit_id, stored_id);
3829 free(stored_id);
3830 if (cmp != 0) {
3831 err = got_error(GOT_ERR_REBASE_COMMITID);
3832 goto done;
3836 cmp_arg.progress_cb = progress_cb;
3837 cmp_arg.progress_arg = progress_arg;
3838 cmp_arg.merged_paths = merged_paths;
3839 err = merge_files(worktree, fileindex, fileindex_path,
3840 parent_commit_id, commit_id, repo, collect_merged_paths,
3841 &cmp_arg, cancel_cb, cancel_arg);
3842 done:
3843 got_fileindex_free(fileindex);
3844 free(fileindex_path);
3845 if (commit_ref)
3846 got_ref_close(commit_ref);
3847 return err;
3850 const struct got_error *
3851 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3852 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
3853 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
3854 struct got_object_id *orig_commit_id, struct got_repository *repo)
3856 const struct got_error *err, *sync_err;
3857 struct got_pathlist_head commitable_paths;
3858 struct collect_commitables_arg cc_arg;
3859 struct got_fileindex *fileindex = NULL;
3860 char *fileindex_path = NULL, *commit_ref_name = NULL;
3861 struct got_reference *head_ref = NULL;
3862 struct got_object_id *head_commit_id = NULL;
3863 struct got_reference *commit_ref = NULL;
3864 struct got_object_id *commit_id = NULL;
3866 TAILQ_INIT(&commitable_paths);
3867 *new_commit_id = NULL;
3869 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3871 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3872 if (err)
3873 return err;
3874 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3875 if (err)
3876 goto done;
3877 err = got_ref_resolve(&commit_id, repo, commit_ref);
3878 if (err)
3879 goto done;
3880 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3881 err = got_error(GOT_ERR_REBASE_COMMITID);
3882 goto done;
3885 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3886 if (err)
3887 goto done;
3889 cc_arg.commitable_paths = &commitable_paths;
3890 cc_arg.worktree = worktree;
3891 cc_arg.repo = repo;
3893 * If possible get the status of individual files directly to
3894 * avoid crawling the entire work tree once per rebased commit.
3895 * TODO: Ideally, merged_paths would contain a list of commitables
3896 * we could use so we could skip worktree_status() entirely.
3898 if (merged_paths) {
3899 struct got_pathlist_entry *pe;
3900 TAILQ_FOREACH(pe, merged_paths, entry) {
3901 err = worktree_status(worktree, pe->path, fileindex,
3902 repo, collect_commitables, &cc_arg, NULL, NULL);
3903 if (err)
3904 goto done;
3906 } else {
3907 err = worktree_status(worktree, "", fileindex, repo,
3908 collect_commitables, &cc_arg, NULL, NULL);
3909 if (err)
3910 goto done;
3913 if (TAILQ_EMPTY(&commitable_paths)) {
3914 /* No-op change; commit will be elided. */
3915 err = got_ref_delete(commit_ref, repo);
3916 if (err)
3917 goto done;
3918 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3919 goto done;
3922 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3923 if (err)
3924 goto done;
3926 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3927 if (err)
3928 goto done;
3930 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
3931 worktree, NULL, got_object_commit_get_author(orig_commit),
3932 got_object_commit_get_committer(orig_commit),
3933 collect_rebase_commit_msg, orig_commit,
3934 rebase_status, NULL, repo);
3935 if (err)
3936 goto done;
3938 err = got_ref_change_ref(tmp_branch, *new_commit_id);
3939 if (err)
3940 goto done;
3942 err = got_ref_delete(commit_ref, repo);
3943 if (err)
3944 goto done;
3946 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3947 fileindex);
3948 sync_err = sync_fileindex(fileindex, fileindex_path);
3949 if (sync_err && err == NULL)
3950 err = sync_err;
3951 done:
3952 if (fileindex)
3953 got_fileindex_free(fileindex);
3954 free(fileindex_path);
3955 free(commit_ref_name);
3956 if (commit_ref)
3957 got_ref_close(commit_ref);
3958 free(head_commit_id);
3959 if (head_ref)
3960 got_ref_close(head_ref);
3961 if (err) {
3962 free(*new_commit_id);
3963 *new_commit_id = NULL;
3965 return err;
3968 const struct got_error *
3969 got_worktree_rebase_postpone(struct got_worktree *worktree)
3971 return lock_worktree(worktree, LOCK_SH);
3974 static const struct got_error *
3975 delete_ref(const char *name, struct got_repository *repo)
3977 const struct got_error *err;
3978 struct got_reference *ref;
3980 err = got_ref_open(&ref, repo, name, 0);
3981 if (err) {
3982 if (err->code == GOT_ERR_NOT_REF)
3983 return NULL;
3984 return err;
3987 err = got_ref_delete(ref, repo);
3988 got_ref_close(ref);
3989 return err;
3992 static const struct got_error *
3993 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
3995 const struct got_error *err;
3996 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3997 char *branch_ref_name = NULL, *commit_ref_name = NULL;
3999 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4000 if (err)
4001 goto done;
4002 err = delete_ref(tmp_branch_name, repo);
4003 if (err)
4004 goto done;
4006 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4007 if (err)
4008 goto done;
4009 err = delete_ref(new_base_branch_ref_name, repo);
4010 if (err)
4011 goto done;
4013 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4014 if (err)
4015 goto done;
4016 err = delete_ref(branch_ref_name, repo);
4017 if (err)
4018 goto done;
4020 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4021 if (err)
4022 goto done;
4023 err = delete_ref(commit_ref_name, repo);
4024 if (err)
4025 goto done;
4027 done:
4028 free(tmp_branch_name);
4029 free(new_base_branch_ref_name);
4030 free(branch_ref_name);
4031 free(commit_ref_name);
4032 return err;
4035 const struct got_error *
4036 got_worktree_rebase_complete(struct got_worktree *worktree,
4037 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
4038 struct got_reference *rebased_branch,
4039 struct got_repository *repo)
4041 const struct got_error *err, *unlockerr;
4042 struct got_object_id *new_head_commit_id = NULL;
4044 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4045 if (err)
4046 return err;
4048 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4049 if (err)
4050 goto done;
4052 err = got_ref_write(rebased_branch, repo);
4053 if (err)
4054 goto done;
4056 err = got_worktree_set_head_ref(worktree, rebased_branch);
4057 if (err)
4058 goto done;
4060 err = delete_rebase_refs(worktree, repo);
4061 done:
4062 free(new_head_commit_id);
4063 unlockerr = lock_worktree(worktree, LOCK_SH);
4064 if (unlockerr && err == NULL)
4065 err = unlockerr;
4066 return err;
4069 struct collect_revertible_paths_arg {
4070 struct got_pathlist_head *revertible_paths;
4071 struct got_worktree *worktree;
4074 static const struct got_error *
4075 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4076 struct got_object_id *blob_id, struct got_object_id *commit_id)
4078 struct collect_revertible_paths_arg *a = arg;
4079 const struct got_error *err = NULL;
4080 struct got_pathlist_entry *new = NULL;
4081 char *path = NULL;
4083 if (status != GOT_STATUS_ADD &&
4084 status != GOT_STATUS_DELETE &&
4085 status != GOT_STATUS_MODIFY &&
4086 status != GOT_STATUS_CONFLICT &&
4087 status != GOT_STATUS_MISSING)
4088 return NULL;
4090 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4091 return got_error_from_errno("asprintf");
4093 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4094 if (err || new == NULL)
4095 free(path);
4096 return err;
4099 const struct got_error *
4100 got_worktree_rebase_abort(struct got_worktree *worktree,
4101 struct got_repository *repo, struct got_reference *new_base_branch,
4102 got_worktree_checkout_cb progress_cb, void *progress_arg)
4104 const struct got_error *err, *unlockerr, *sync_err;
4105 struct got_reference *resolved = NULL;
4106 struct got_object_id *commit_id = NULL;
4107 struct got_fileindex *fileindex = NULL;
4108 char *fileindex_path = NULL;
4109 struct got_pathlist_head revertible_paths;
4110 struct got_pathlist_entry *pe;
4111 struct collect_revertible_paths_arg crp_arg;
4112 struct got_object_id *tree_id = NULL;
4114 TAILQ_INIT(&revertible_paths);
4116 err = lock_worktree(worktree, LOCK_EX);
4117 if (err)
4118 return err;
4120 err = got_ref_open(&resolved, repo,
4121 got_ref_get_symref_target(new_base_branch), 0);
4122 if (err)
4123 goto done;
4125 err = got_worktree_set_head_ref(worktree, resolved);
4126 if (err)
4127 goto done;
4130 * XXX commits to the base branch could have happened while
4131 * we were busy rebasing; should we store the original commit ID
4132 * when rebase begins and read it back here?
4134 err = got_ref_resolve(&commit_id, repo, resolved);
4135 if (err)
4136 goto done;
4138 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4139 if (err)
4140 goto done;
4142 err = got_object_id_by_path(&tree_id, repo,
4143 worktree->base_commit_id, worktree->path_prefix);
4144 if (err)
4145 goto done;
4147 err = delete_rebase_refs(worktree, repo);
4148 if (err)
4149 goto done;
4151 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4152 if (err)
4153 goto done;
4155 crp_arg.revertible_paths = &revertible_paths;
4156 crp_arg.worktree = worktree;
4157 err = worktree_status(worktree, "", fileindex, repo,
4158 collect_revertible_paths, &crp_arg, NULL, NULL);
4159 if (err)
4160 goto done;
4162 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4163 err = revert_file(worktree, fileindex, pe->path,
4164 progress_cb, progress_arg, repo);
4165 if (err)
4166 goto sync;
4169 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4170 repo, progress_cb, progress_arg, NULL, NULL);
4171 sync:
4172 sync_err = sync_fileindex(fileindex, fileindex_path);
4173 if (sync_err && err == NULL)
4174 err = sync_err;
4175 done:
4176 got_ref_close(resolved);
4177 free(tree_id);
4178 free(commit_id);
4179 if (fileindex)
4180 got_fileindex_free(fileindex);
4181 free(fileindex_path);
4182 TAILQ_FOREACH(pe, &revertible_paths, entry)
4183 free((char *)pe->path);
4184 got_pathlist_free(&revertible_paths);
4186 unlockerr = lock_worktree(worktree, LOCK_SH);
4187 if (unlockerr && err == NULL)
4188 err = unlockerr;
4189 return err;