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_sec == sb->st_mtime &&
1078 ie->mtime_nsec == sb->st_mtimensec &&
1079 ie->size == (sb->st_size & 0xffffffff))
1080 return NULL;
1082 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1083 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1084 if (err)
1085 return err;
1087 f = fopen(abspath, "r");
1088 if (f == NULL) {
1089 err = got_error_from_errno2("fopen", abspath);
1090 goto done;
1092 hdrlen = got_object_blob_get_hdrlen(blob);
1093 for (;;) {
1094 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1095 err = got_object_blob_read_block(&blen, blob);
1096 if (err)
1097 goto done;
1098 /* Skip length of blob object header first time around. */
1099 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1100 if (flen == 0 && ferror(f)) {
1101 err = got_error_from_errno("fread");
1102 goto done;
1104 if (blen == 0) {
1105 if (flen != 0)
1106 *status = GOT_STATUS_MODIFY;
1107 break;
1108 } else if (flen == 0) {
1109 if (blen != 0)
1110 *status = GOT_STATUS_MODIFY;
1111 break;
1112 } else if (blen - hdrlen == flen) {
1113 /* Skip blob object header first time around. */
1114 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1115 *status = GOT_STATUS_MODIFY;
1116 break;
1118 } else {
1119 *status = GOT_STATUS_MODIFY;
1120 break;
1122 hdrlen = 0;
1125 if (*status == GOT_STATUS_MODIFY) {
1126 rewind(f);
1127 err = get_modified_file_content_status(status, f);
1129 done:
1130 if (blob)
1131 got_object_blob_close(blob);
1132 if (f)
1133 fclose(f);
1134 return err;
1137 static const struct got_error *
1138 update_blob(struct got_worktree *worktree,
1139 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1140 struct got_tree_entry *te, const char *path,
1141 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1142 void *progress_arg)
1144 const struct got_error *err = NULL;
1145 struct got_blob_object *blob = NULL;
1146 char *ondisk_path;
1147 unsigned char status = GOT_STATUS_NO_CHANGE;
1148 struct stat sb;
1150 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1151 return got_error_from_errno("asprintf");
1153 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1154 if (err)
1155 goto done;
1157 if (status == GOT_STATUS_OBSTRUCTED) {
1158 err = (*progress_cb)(progress_arg, status, path);
1159 goto done;
1162 if (ie && status != GOT_STATUS_MISSING) {
1163 if (got_fileindex_entry_has_commit(ie) &&
1164 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1165 SHA1_DIGEST_LENGTH) == 0) {
1166 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1167 path);
1168 goto done;
1170 if (got_fileindex_entry_has_blob(ie) &&
1171 memcmp(ie->blob_sha1, te->id->sha1,
1172 SHA1_DIGEST_LENGTH) == 0)
1173 goto done;
1176 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1177 if (err)
1178 goto done;
1180 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1181 int update_timestamps;
1182 struct got_blob_object *blob2 = NULL;
1183 if (got_fileindex_entry_has_blob(ie)) {
1184 struct got_object_id id2;
1185 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1186 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1187 if (err)
1188 goto done;
1190 err = merge_blob(&update_timestamps, worktree, blob2,
1191 ondisk_path, path, sb.st_mode, blob,
1192 worktree->base_commit_id, repo,
1193 progress_cb, progress_arg);
1194 if (blob2)
1195 got_object_blob_close(blob2);
1197 * Do not update timestamps of files with local changes.
1198 * Otherwise, a future status walk would treat them as
1199 * unmodified files again.
1201 err = got_fileindex_entry_update(ie, ondisk_path,
1202 blob->id.sha1, worktree->base_commit_id->sha1,
1203 update_timestamps);
1204 } else if (status == GOT_STATUS_DELETE) {
1205 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1206 if (err)
1207 goto done;
1208 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1209 ondisk_path, path, blob, 0);
1210 if (err)
1211 goto done;
1212 } else {
1213 err = install_blob(worktree, ondisk_path, path, te->mode,
1214 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1215 repo, progress_cb, progress_arg);
1216 if (err)
1217 goto done;
1218 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1219 ondisk_path, path, blob, 1);
1220 if (err)
1221 goto done;
1223 got_object_blob_close(blob);
1224 done:
1225 free(ondisk_path);
1226 return err;
1229 static const struct got_error *
1230 remove_ondisk_file(const char *root_path, const char *path)
1232 const struct got_error *err = NULL;
1233 char *ondisk_path = NULL;
1235 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1236 return got_error_from_errno("asprintf");
1238 if (unlink(ondisk_path) == -1) {
1239 if (errno != ENOENT)
1240 err = got_error_from_errno2("unlink", ondisk_path);
1241 } else {
1242 char *parent = dirname(ondisk_path);
1243 while (parent && strcmp(parent, root_path) != 0) {
1244 if (rmdir(parent) == -1) {
1245 if (errno != ENOTEMPTY)
1246 err = got_error_from_errno2("rmdir",
1247 parent);
1248 break;
1250 parent = dirname(parent);
1253 free(ondisk_path);
1254 return err;
1257 static const struct got_error *
1258 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1259 struct got_fileindex_entry *ie, struct got_repository *repo,
1260 got_worktree_checkout_cb progress_cb, void *progress_arg)
1262 const struct got_error *err = NULL;
1263 unsigned char status;
1264 struct stat sb;
1265 char *ondisk_path;
1267 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1268 == -1)
1269 return got_error_from_errno("asprintf");
1271 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1272 if (err)
1273 return err;
1275 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1276 status == GOT_STATUS_ADD) {
1277 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1278 if (err)
1279 return err;
1281 * Preserve the working file and change the deleted blob's
1282 * entry into a schedule-add entry.
1284 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1285 0);
1286 if (err)
1287 return err;
1288 } else {
1289 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1290 if (err)
1291 return err;
1292 if (status == GOT_STATUS_NO_CHANGE) {
1293 err = remove_ondisk_file(worktree->root_path, ie->path);
1294 if (err)
1295 return err;
1297 got_fileindex_entry_remove(fileindex, ie);
1300 return err;
1303 struct diff_cb_arg {
1304 struct got_fileindex *fileindex;
1305 struct got_worktree *worktree;
1306 struct got_repository *repo;
1307 got_worktree_checkout_cb progress_cb;
1308 void *progress_arg;
1309 got_worktree_cancel_cb cancel_cb;
1310 void *cancel_arg;
1313 static const struct got_error *
1314 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1315 struct got_tree_entry *te, const char *parent_path)
1317 struct diff_cb_arg *a = arg;
1319 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 return got_error(GOT_ERR_CANCELLED);
1322 return update_blob(a->worktree, a->fileindex, ie, te,
1323 ie->path, a->repo, a->progress_cb, a->progress_arg);
1326 static const struct got_error *
1327 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1329 struct diff_cb_arg *a = arg;
1331 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1332 return got_error(GOT_ERR_CANCELLED);
1334 return delete_blob(a->worktree, a->fileindex, ie,
1335 a->repo, a->progress_cb, a->progress_arg);
1338 static const struct got_error *
1339 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1341 struct diff_cb_arg *a = arg;
1342 const struct got_error *err;
1343 char *path;
1345 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1346 return got_error(GOT_ERR_CANCELLED);
1348 if (asprintf(&path, "%s%s%s", parent_path,
1349 parent_path[0] ? "/" : "", te->name)
1350 == -1)
1351 return got_error_from_errno("asprintf");
1353 if (S_ISDIR(te->mode))
1354 err = add_dir_on_disk(a->worktree, path);
1355 else
1356 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1357 a->repo, a->progress_cb, a->progress_arg);
1359 free(path);
1360 return err;
1363 static const struct got_error *
1364 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1366 const struct got_error *err = NULL;
1367 char *uuidstr = NULL;
1368 uint32_t uuid_status;
1370 *refname = NULL;
1372 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1373 if (uuid_status != uuid_s_ok)
1374 return got_error_uuid(uuid_status);
1376 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1377 == -1) {
1378 err = got_error_from_errno("asprintf");
1379 *refname = NULL;
1381 free(uuidstr);
1382 return err;
1385 const struct got_error *
1386 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1388 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1391 static const struct got_error *
1392 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1394 return get_ref_name(refname, worktree,
1395 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1398 static const struct got_error *
1399 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1401 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1404 static const struct got_error *
1405 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1407 return get_ref_name(refname, worktree,
1408 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1411 static const struct got_error *
1412 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1414 return get_ref_name(refname, worktree,
1415 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1420 * Prevent Git's garbage collector from deleting our base commit by
1421 * setting a reference to our base commit's ID.
1423 static const struct got_error *
1424 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1426 const struct got_error *err = NULL;
1427 struct got_reference *ref = NULL;
1428 char *refname;
1430 err = got_worktree_get_base_ref_name(&refname, worktree);
1431 if (err)
1432 return err;
1434 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1435 if (err)
1436 goto done;
1438 err = got_ref_write(ref, repo);
1439 done:
1440 free(refname);
1441 if (ref)
1442 got_ref_close(ref);
1443 return err;
1446 static const struct got_error *
1447 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1448 struct got_worktree *worktree)
1450 const struct got_error *err = NULL;
1451 FILE *index = NULL;
1453 *fileindex_path = NULL;
1454 *fileindex = got_fileindex_alloc();
1455 if (*fileindex == NULL)
1456 return got_error_from_errno("got_fileindex_alloc");
1458 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1459 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1460 err = got_error_from_errno("asprintf");
1461 *fileindex_path = NULL;
1462 goto done;
1465 index = fopen(*fileindex_path, "rb");
1466 if (index == NULL) {
1467 if (errno != ENOENT)
1468 err = got_error_from_errno2("fopen", *fileindex_path);
1469 } else {
1470 err = got_fileindex_read(*fileindex, index);
1471 if (fclose(index) != 0 && err == NULL)
1472 err = got_error_from_errno("fclose");
1474 done:
1475 if (err) {
1476 free(*fileindex_path);
1477 *fileindex_path = NULL;
1478 got_fileindex_free(*fileindex);
1479 *fileindex = NULL;
1481 return err;
1484 struct bump_base_commit_id_arg {
1485 struct got_object_id *base_commit_id;
1486 const char *path;
1487 size_t path_len;
1488 const char *entry_name;
1489 got_worktree_checkout_cb progress_cb;
1490 void *progress_arg;
1493 /* Bump base commit ID of all files within an updated part of the work tree. */
1494 static const struct got_error *
1495 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1497 const struct got_error *err;
1498 struct bump_base_commit_id_arg *a = arg;
1500 if (a->entry_name) {
1501 if (strcmp(ie->path, a->path) != 0)
1502 return NULL;
1503 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1504 return NULL;
1506 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1507 SHA1_DIGEST_LENGTH) == 0)
1508 return NULL;
1510 if (a->progress_cb) {
1511 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1512 ie->path);
1513 if (err)
1514 return err;
1516 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1517 return NULL;
1520 static const struct got_error *
1521 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1523 const struct got_error *err = NULL;
1524 char *new_fileindex_path = NULL;
1525 FILE *new_index = NULL;
1527 err = got_opentemp_named(&new_fileindex_path, &new_index,
1528 fileindex_path);
1529 if (err)
1530 goto done;
1532 err = got_fileindex_write(fileindex, new_index);
1533 if (err)
1534 goto done;
1536 if (rename(new_fileindex_path, fileindex_path) != 0) {
1537 err = got_error_from_errno3("rename", new_fileindex_path,
1538 fileindex_path);
1539 unlink(new_fileindex_path);
1541 done:
1542 if (new_index)
1543 fclose(new_index);
1544 free(new_fileindex_path);
1545 return err;
1548 static const struct got_error *
1549 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1550 struct got_object_id **tree_id, const char *wt_relpath,
1551 struct got_worktree *worktree, struct got_repository *repo)
1553 const struct got_error *err = NULL;
1554 struct got_object_id *id = NULL;
1555 char *in_repo_path = NULL;
1556 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1558 *entry_type = GOT_OBJ_TYPE_ANY;
1559 *tree_relpath = NULL;
1560 *tree_id = NULL;
1562 if (wt_relpath[0] == '\0') {
1563 /* Check out all files within the work tree. */
1564 *entry_type = GOT_OBJ_TYPE_TREE;
1565 *tree_relpath = strdup("");
1566 if (*tree_relpath == NULL) {
1567 err = got_error_from_errno("strdup");
1568 goto done;
1570 err = got_object_id_by_path(tree_id, repo,
1571 worktree->base_commit_id, worktree->path_prefix);
1572 if (err)
1573 goto done;
1574 return NULL;
1577 /* Check out a subset of files in the work tree. */
1579 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1580 is_root_wt ? "" : "/", wt_relpath) == -1) {
1581 err = got_error_from_errno("asprintf");
1582 goto done;
1585 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1586 in_repo_path);
1587 if (err)
1588 goto done;
1590 free(in_repo_path);
1591 in_repo_path = NULL;
1593 err = got_object_get_type(entry_type, repo, id);
1594 if (err)
1595 goto done;
1597 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1598 /* Check out a single file. */
1599 if (strchr(wt_relpath, '/') == NULL) {
1600 /* Check out a single file in work tree's root dir. */
1601 in_repo_path = strdup(worktree->path_prefix);
1602 if (in_repo_path == NULL) {
1603 err = got_error_from_errno("strdup");
1604 goto done;
1606 *tree_relpath = strdup("");
1607 if (*tree_relpath == NULL) {
1608 err = got_error_from_errno("strdup");
1609 goto done;
1611 } else {
1612 /* Check out a single file in a subdirectory. */
1613 err = got_path_dirname(tree_relpath, wt_relpath);
1614 if (err)
1615 return err;
1616 if (asprintf(&in_repo_path, "%s%s%s",
1617 worktree->path_prefix, is_root_wt ? "" : "/",
1618 *tree_relpath) == -1) {
1619 err = got_error_from_errno("asprintf");
1620 goto done;
1623 err = got_object_id_by_path(tree_id, repo,
1624 worktree->base_commit_id, in_repo_path);
1625 } else {
1626 /* Check out all files within a subdirectory. */
1627 *tree_id = got_object_id_dup(id);
1628 if (*tree_id == NULL) {
1629 err = got_error_from_errno("got_object_id_dup");
1630 goto done;
1632 *tree_relpath = strdup(wt_relpath);
1633 if (*tree_relpath == NULL) {
1634 err = got_error_from_errno("strdup");
1635 goto done;
1638 done:
1639 free(id);
1640 free(in_repo_path);
1641 if (err) {
1642 *entry_type = GOT_OBJ_TYPE_ANY;
1643 free(*tree_relpath);
1644 *tree_relpath = NULL;
1645 free(*tree_id);
1646 *tree_id = NULL;
1648 return err;
1651 static const struct got_error *
1652 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1653 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1654 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1655 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1657 const struct got_error *err = NULL;
1658 struct got_commit_object *commit = NULL;
1659 struct got_tree_object *tree = NULL;
1660 struct got_fileindex_diff_tree_cb diff_cb;
1661 struct diff_cb_arg arg;
1663 err = ref_base_commit(worktree, repo);
1664 if (err)
1665 goto done;
1667 err = got_object_open_as_commit(&commit, repo,
1668 worktree->base_commit_id);
1669 if (err)
1670 goto done;
1672 err = got_object_open_as_tree(&tree, repo, tree_id);
1673 if (err)
1674 goto done;
1676 if (entry_name &&
1677 got_object_tree_find_entry(tree, entry_name) == NULL) {
1678 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1679 goto done;
1682 diff_cb.diff_old_new = diff_old_new;
1683 diff_cb.diff_old = diff_old;
1684 diff_cb.diff_new = diff_new;
1685 arg.fileindex = fileindex;
1686 arg.worktree = worktree;
1687 arg.repo = repo;
1688 arg.progress_cb = progress_cb;
1689 arg.progress_arg = progress_arg;
1690 arg.cancel_cb = cancel_cb;
1691 arg.cancel_arg = cancel_arg;
1692 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1693 entry_name, repo, &diff_cb, &arg);
1694 done:
1695 if (tree)
1696 got_object_tree_close(tree);
1697 if (commit)
1698 got_object_commit_close(commit);
1699 return err;
1702 const struct got_error *
1703 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1704 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1705 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1707 const struct got_error *err = NULL, *sync_err, *unlockerr;
1708 struct got_commit_object *commit = NULL;
1709 struct got_object_id *tree_id = NULL;
1710 struct got_tree_object *tree = NULL;
1711 struct got_fileindex *fileindex = NULL;
1712 char *fileindex_path = NULL;
1713 char *relpath = NULL, *entry_name = NULL;
1714 int entry_type;
1716 err = lock_worktree(worktree, LOCK_EX);
1717 if (err)
1718 return err;
1720 err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
1721 path, worktree, repo);
1722 if (err)
1723 goto done;
1725 if (entry_type == GOT_OBJ_TYPE_BLOB) {
1726 entry_name = basename(path);
1727 if (entry_name == NULL) {
1728 err = got_error_from_errno2("basename", path);
1729 goto done;
1734 * Read the file index.
1735 * Checking out files is supposed to be an idempotent operation.
1736 * If the on-disk file index is incomplete we will try to complete it.
1738 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1739 if (err)
1740 goto done;
1742 err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
1743 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1744 if (err == NULL) {
1745 struct bump_base_commit_id_arg bbc_arg;
1746 bbc_arg.base_commit_id = worktree->base_commit_id;
1747 bbc_arg.entry_name = entry_name;
1748 bbc_arg.path = path;
1749 bbc_arg.path_len = strlen(path);
1750 bbc_arg.progress_cb = progress_cb;
1751 bbc_arg.progress_arg = progress_arg;
1752 err = got_fileindex_for_each_entry_safe(fileindex,
1753 bump_base_commit_id, &bbc_arg);
1755 sync_err = sync_fileindex(fileindex, fileindex_path);
1756 if (sync_err && err == NULL)
1757 err = sync_err;
1758 done:
1759 free(fileindex_path);
1760 free(relpath);
1761 if (tree)
1762 got_object_tree_close(tree);
1763 if (commit)
1764 got_object_commit_close(commit);
1765 if (fileindex)
1766 got_fileindex_free(fileindex);
1767 unlockerr = lock_worktree(worktree, LOCK_SH);
1768 if (unlockerr && err == NULL)
1769 err = unlockerr;
1770 return err;
1773 struct merge_file_cb_arg {
1774 struct got_worktree *worktree;
1775 struct got_fileindex *fileindex;
1776 got_worktree_checkout_cb progress_cb;
1777 void *progress_arg;
1778 got_worktree_cancel_cb cancel_cb;
1779 void *cancel_arg;
1780 struct got_object_id *commit_id2;
1783 static const struct got_error *
1784 merge_file_cb(void *arg, struct got_blob_object *blob1,
1785 struct got_blob_object *blob2, struct got_object_id *id1,
1786 struct got_object_id *id2, const char *path1, const char *path2,
1787 struct got_repository *repo)
1789 static const struct got_error *err = NULL;
1790 struct merge_file_cb_arg *a = arg;
1791 struct got_fileindex_entry *ie;
1792 char *ondisk_path = NULL;
1793 struct stat sb;
1794 unsigned char status;
1795 int local_changes_subsumed;
1797 if (blob1 && blob2) {
1798 ie = got_fileindex_entry_get(a->fileindex, path2);
1799 if (ie == NULL)
1800 return (*a->progress_cb)(a->progress_arg,
1801 GOT_STATUS_MISSING, path2);
1803 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1804 path2) == -1)
1805 return got_error_from_errno("asprintf");
1807 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1808 if (err)
1809 goto done;
1811 if (status == GOT_STATUS_DELETE) {
1812 err = (*a->progress_cb)(a->progress_arg,
1813 GOT_STATUS_MERGE, path2);
1814 goto done;
1816 if (status != GOT_STATUS_NO_CHANGE &&
1817 status != GOT_STATUS_MODIFY &&
1818 status != GOT_STATUS_CONFLICT &&
1819 status != GOT_STATUS_ADD) {
1820 err = (*a->progress_cb)(a->progress_arg, status, path2);
1821 goto done;
1824 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1825 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1826 a->progress_cb, a->progress_arg);
1827 } else if (blob1) {
1828 ie = got_fileindex_entry_get(a->fileindex, path1);
1829 if (ie == NULL)
1830 return (*a->progress_cb)(a->progress_arg,
1831 GOT_STATUS_MISSING, path2);
1833 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1834 path1) == -1)
1835 return got_error_from_errno("asprintf");
1837 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1838 if (err)
1839 goto done;
1841 switch (status) {
1842 case GOT_STATUS_NO_CHANGE:
1843 err = (*a->progress_cb)(a->progress_arg,
1844 GOT_STATUS_DELETE, path1);
1845 if (err)
1846 goto done;
1847 err = remove_ondisk_file(a->worktree->root_path, path1);
1848 if (err)
1849 goto done;
1850 if (ie)
1851 got_fileindex_entry_mark_deleted_from_disk(ie);
1852 break;
1853 case GOT_STATUS_DELETE:
1854 case GOT_STATUS_MISSING:
1855 err = (*a->progress_cb)(a->progress_arg,
1856 GOT_STATUS_DELETE, path1);
1857 if (err)
1858 goto done;
1859 if (ie)
1860 got_fileindex_entry_mark_deleted_from_disk(ie);
1861 break;
1862 case GOT_STATUS_ADD:
1863 case GOT_STATUS_MODIFY:
1864 case GOT_STATUS_CONFLICT:
1865 err = (*a->progress_cb)(a->progress_arg,
1866 GOT_STATUS_CANNOT_DELETE, path1);
1867 if (err)
1868 goto done;
1869 break;
1870 case GOT_STATUS_OBSTRUCTED:
1871 err = (*a->progress_cb)(a->progress_arg, status, path1);
1872 if (err)
1873 goto done;
1874 break;
1875 default:
1876 break;
1878 } else if (blob2) {
1879 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1880 path2) == -1)
1881 return got_error_from_errno("asprintf");
1882 ie = got_fileindex_entry_get(a->fileindex, path2);
1883 if (ie) {
1884 err = get_file_status(&status, &sb, ie, ondisk_path,
1885 repo);
1886 if (err)
1887 goto done;
1888 if (status != GOT_STATUS_NO_CHANGE &&
1889 status != GOT_STATUS_MODIFY &&
1890 status != GOT_STATUS_CONFLICT &&
1891 status != GOT_STATUS_ADD) {
1892 err = (*a->progress_cb)(a->progress_arg,
1893 status, path2);
1894 goto done;
1896 err = merge_blob(&local_changes_subsumed, a->worktree,
1897 NULL, ondisk_path, path2, sb.st_mode, blob2,
1898 a->commit_id2, repo,
1899 a->progress_cb, a->progress_arg);
1900 if (status == GOT_STATUS_DELETE) {
1901 err = update_blob_fileindex_entry(a->worktree,
1902 a->fileindex, ie, ondisk_path, ie->path,
1903 blob2, 0);
1904 if (err)
1905 goto done;
1907 } else {
1908 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1909 err = install_blob(a->worktree, ondisk_path, path2,
1910 /* XXX get this from parent tree! */
1911 GOT_DEFAULT_FILE_MODE,
1912 sb.st_mode, blob2, 0, 0, repo,
1913 a->progress_cb, a->progress_arg);
1914 if (err)
1915 goto done;
1916 err = got_fileindex_entry_alloc(&ie,
1917 ondisk_path, path2, NULL, NULL);
1918 if (err)
1919 goto done;
1920 err = got_fileindex_entry_add(a->fileindex, ie);
1921 if (err) {
1922 got_fileindex_entry_free(ie);
1923 goto done;
1927 done:
1928 free(ondisk_path);
1929 return err;
1932 struct check_merge_ok_arg {
1933 struct got_worktree *worktree;
1934 struct got_repository *repo;
1937 static const struct got_error *
1938 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1940 const struct got_error *err = NULL;
1941 struct check_merge_ok_arg *a = arg;
1942 unsigned char status;
1943 struct stat sb;
1944 char *ondisk_path;
1946 /* Reject merges into a work tree with mixed base commits. */
1947 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1948 SHA1_DIGEST_LENGTH))
1949 return got_error(GOT_ERR_MIXED_COMMITS);
1951 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1952 == -1)
1953 return got_error_from_errno("asprintf");
1955 /* Reject merges into a work tree with conflicted files. */
1956 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1957 if (err)
1958 return err;
1959 if (status == GOT_STATUS_CONFLICT)
1960 return got_error(GOT_ERR_CONFLICTS);
1962 return NULL;
1965 static const struct got_error *
1966 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1967 const char *fileindex_path, struct got_object_id *commit_id1,
1968 struct got_object_id *commit_id2, struct got_repository *repo,
1969 got_worktree_checkout_cb progress_cb, void *progress_arg,
1970 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1972 const struct got_error *err = NULL, *sync_err;
1973 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1974 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1975 struct merge_file_cb_arg arg;
1977 if (commit_id1) {
1978 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1979 worktree->path_prefix);
1980 if (err)
1981 goto done;
1983 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1984 if (err)
1985 goto done;
1988 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1989 worktree->path_prefix);
1990 if (err)
1991 goto done;
1993 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1994 if (err)
1995 goto done;
1997 arg.worktree = worktree;
1998 arg.fileindex = fileindex;
1999 arg.progress_cb = progress_cb;
2000 arg.progress_arg = progress_arg;
2001 arg.cancel_cb = cancel_cb;
2002 arg.cancel_arg = cancel_arg;
2003 arg.commit_id2 = commit_id2;
2004 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2005 sync_err = sync_fileindex(fileindex, fileindex_path);
2006 if (sync_err && err == NULL)
2007 err = sync_err;
2008 done:
2009 if (tree1)
2010 got_object_tree_close(tree1);
2011 if (tree2)
2012 got_object_tree_close(tree2);
2013 return err;
2016 const struct got_error *
2017 got_worktree_merge_files(struct got_worktree *worktree,
2018 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2019 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2020 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2022 const struct got_error *err, *unlockerr;
2023 char *fileindex_path = NULL;
2024 struct got_fileindex *fileindex = NULL;
2025 struct check_merge_ok_arg mok_arg;
2027 err = lock_worktree(worktree, LOCK_EX);
2028 if (err)
2029 return err;
2031 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2032 if (err)
2033 goto done;
2035 mok_arg.worktree = worktree;
2036 mok_arg.repo = repo;
2037 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2038 &mok_arg);
2039 if (err)
2040 goto done;
2042 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2043 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2044 done:
2045 if (fileindex)
2046 got_fileindex_free(fileindex);
2047 free(fileindex_path);
2048 unlockerr = lock_worktree(worktree, LOCK_SH);
2049 if (unlockerr && err == NULL)
2050 err = unlockerr;
2051 return err;
2054 struct diff_dir_cb_arg {
2055 struct got_fileindex *fileindex;
2056 struct got_worktree *worktree;
2057 const char *status_path;
2058 size_t status_path_len;
2059 struct got_repository *repo;
2060 got_worktree_status_cb status_cb;
2061 void *status_arg;
2062 got_worktree_cancel_cb cancel_cb;
2063 void *cancel_arg;
2066 static const struct got_error *
2067 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2068 got_worktree_status_cb status_cb, void *status_arg,
2069 struct got_repository *repo)
2071 const struct got_error *err = NULL;
2072 unsigned char status = GOT_STATUS_NO_CHANGE;
2073 struct stat sb;
2074 struct got_object_id blob_id, commit_id;
2076 err = get_file_status(&status, &sb, ie, abspath, repo);
2077 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2078 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2079 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2080 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2081 &commit_id);
2083 return err;
2086 static const struct got_error *
2087 status_old_new(void *arg, struct got_fileindex_entry *ie,
2088 struct dirent *de, const char *parent_path)
2090 const struct got_error *err = NULL;
2091 struct diff_dir_cb_arg *a = arg;
2092 char *abspath;
2094 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2095 return got_error(GOT_ERR_CANCELLED);
2097 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2098 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2099 return NULL;
2101 if (parent_path[0]) {
2102 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2103 parent_path, de->d_name) == -1)
2104 return got_error_from_errno("asprintf");
2105 } else {
2106 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2107 de->d_name) == -1)
2108 return got_error_from_errno("asprintf");
2111 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2112 a->repo);
2113 free(abspath);
2114 return err;
2117 static const struct got_error *
2118 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2120 struct diff_dir_cb_arg *a = arg;
2121 struct got_object_id blob_id, commit_id;
2122 unsigned char status;
2124 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2125 return got_error(GOT_ERR_CANCELLED);
2127 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2128 return NULL;
2130 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2131 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2132 if (got_fileindex_entry_has_file_on_disk(ie))
2133 status = GOT_STATUS_MISSING;
2134 else
2135 status = GOT_STATUS_DELETE;
2136 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2137 &commit_id);
2140 static const struct got_error *
2141 status_new(void *arg, struct dirent *de, const char *parent_path)
2143 const struct got_error *err = NULL;
2144 struct diff_dir_cb_arg *a = arg;
2145 char *path = NULL;
2147 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2148 return got_error(GOT_ERR_CANCELLED);
2150 if (de->d_type == DT_DIR)
2151 return NULL;
2153 /* XXX ignore symlinks for now */
2154 if (de->d_type == DT_LNK)
2155 return NULL;
2157 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2158 return NULL;
2160 if (parent_path[0]) {
2161 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2162 return got_error_from_errno("asprintf");
2163 } else {
2164 path = de->d_name;
2167 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2168 NULL, NULL);
2169 if (parent_path[0])
2170 free(path);
2171 return err;
2174 static const struct got_error *
2175 worktree_status(struct got_worktree *worktree, const char *path,
2176 struct got_fileindex *fileindex, struct got_repository *repo,
2177 got_worktree_status_cb status_cb, void *status_arg,
2178 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2180 const struct got_error *err = NULL;
2181 DIR *workdir = NULL;
2182 struct got_fileindex_diff_dir_cb fdiff_cb;
2183 struct diff_dir_cb_arg arg;
2184 char *ondisk_path = NULL;
2186 if (asprintf(&ondisk_path, "%s%s%s",
2187 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2188 err = got_error_from_errno("asprintf");
2189 goto done;
2191 workdir = opendir(ondisk_path);
2192 if (workdir == NULL) {
2193 if (errno == ENOTDIR || errno == ENOENT) {
2194 struct got_fileindex_entry *ie;
2195 ie = got_fileindex_entry_get(fileindex, path);
2196 if (ie == NULL) {
2197 err = got_error(GOT_ERR_BAD_PATH);
2198 goto done;
2200 err = report_file_status(ie, ondisk_path,
2201 status_cb, status_arg, repo);
2202 goto done;
2203 } else {
2204 err = got_error_from_errno2("opendir", ondisk_path);
2205 goto done;
2208 fdiff_cb.diff_old_new = status_old_new;
2209 fdiff_cb.diff_old = status_old;
2210 fdiff_cb.diff_new = status_new;
2211 arg.fileindex = fileindex;
2212 arg.worktree = worktree;
2213 arg.status_path = path;
2214 arg.status_path_len = strlen(path);
2215 arg.repo = repo;
2216 arg.status_cb = status_cb;
2217 arg.status_arg = status_arg;
2218 arg.cancel_cb = cancel_cb;
2219 arg.cancel_arg = cancel_arg;
2220 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2221 path, repo, &fdiff_cb, &arg);
2222 done:
2223 if (workdir)
2224 closedir(workdir);
2225 free(ondisk_path);
2226 return err;
2229 const struct got_error *
2230 got_worktree_status(struct got_worktree *worktree, const char *path,
2231 struct got_repository *repo, got_worktree_status_cb status_cb,
2232 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2234 const struct got_error *err = NULL;
2235 char *fileindex_path = NULL;
2236 struct got_fileindex *fileindex = NULL;
2238 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2239 if (err)
2240 return err;
2242 err = worktree_status(worktree, path, fileindex, repo,
2243 status_cb, status_arg, cancel_cb, cancel_arg);
2244 free(fileindex_path);
2245 got_fileindex_free(fileindex);
2246 return err;
2249 const struct got_error *
2250 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2251 const char *arg)
2253 const struct got_error *err = NULL;
2254 char *resolved, *path = NULL;
2255 size_t len;
2257 *wt_path = NULL;
2259 resolved = realpath(arg, NULL);
2260 if (resolved == NULL)
2261 return got_error_from_errno2("realpath", arg);
2263 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2264 strlen(got_worktree_get_root_path(worktree)))) {
2265 err = got_error(GOT_ERR_BAD_PATH);
2266 goto done;
2269 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2270 err = got_path_skip_common_ancestor(&path,
2271 got_worktree_get_root_path(worktree), resolved);
2272 if (err)
2273 goto done;
2274 } else {
2275 path = strdup("");
2276 if (path == NULL) {
2277 err = got_error_from_errno("strdup");
2278 goto done;
2282 /* XXX status walk can't deal with trailing slash! */
2283 len = strlen(path);
2284 while (path[len - 1] == '/') {
2285 path[len - 1] = '\0';
2286 len--;
2288 done:
2289 free(resolved);
2290 if (err == NULL)
2291 *wt_path = path;
2292 else
2293 free(path);
2294 return err;
2297 static const struct got_error *
2298 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2299 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2300 struct got_repository *repo)
2302 const struct got_error *err = NULL;
2303 struct got_fileindex_entry *ie;
2305 /* Re-adding an existing entry is a no-op. */
2306 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2307 return NULL;
2309 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2310 if (err)
2311 return err;
2313 err = got_fileindex_entry_add(fileindex, ie);
2314 if (err) {
2315 got_fileindex_entry_free(ie);
2316 return err;
2319 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2322 const struct got_error *
2323 got_worktree_schedule_add(struct got_worktree *worktree,
2324 struct got_pathlist_head *ondisk_paths,
2325 got_worktree_status_cb status_cb, void *status_arg,
2326 struct got_repository *repo)
2328 struct got_fileindex *fileindex = NULL;
2329 char *fileindex_path = NULL;
2330 const struct got_error *err = NULL, *sync_err, *unlockerr;
2331 struct got_pathlist_entry *pe;
2333 err = lock_worktree(worktree, LOCK_EX);
2334 if (err)
2335 return err;
2337 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2338 if (err)
2339 goto done;
2341 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2342 char *relpath;
2343 err = got_path_skip_common_ancestor(&relpath,
2344 got_worktree_get_root_path(worktree), pe->path);
2345 if (err)
2346 break;
2347 err = schedule_addition(pe->path, fileindex, relpath,
2348 status_cb, status_arg, repo);
2349 free(relpath);
2350 if (err)
2351 break;
2353 sync_err = sync_fileindex(fileindex, fileindex_path);
2354 if (sync_err && err == NULL)
2355 err = sync_err;
2356 done:
2357 free(fileindex_path);
2358 if (fileindex)
2359 got_fileindex_free(fileindex);
2360 unlockerr = lock_worktree(worktree, LOCK_SH);
2361 if (unlockerr && err == NULL)
2362 err = unlockerr;
2363 return err;
2366 static const struct got_error *
2367 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2368 const char *relpath, int delete_local_mods,
2369 got_worktree_status_cb status_cb, void *status_arg,
2370 struct got_repository *repo)
2372 const struct got_error *err = NULL;
2373 struct got_fileindex_entry *ie = NULL;
2374 unsigned char status;
2375 struct stat sb;
2377 ie = got_fileindex_entry_get(fileindex, relpath);
2378 if (ie == NULL)
2379 return got_error(GOT_ERR_BAD_PATH);
2381 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2382 if (err)
2383 return err;
2385 if (status != GOT_STATUS_NO_CHANGE) {
2386 if (status == GOT_STATUS_DELETE)
2387 return got_error_set_errno(ENOENT, ondisk_path);
2388 if (status != GOT_STATUS_MODIFY)
2389 return got_error(GOT_ERR_FILE_STATUS);
2390 if (!delete_local_mods)
2391 return got_error(GOT_ERR_FILE_MODIFIED);
2394 if (unlink(ondisk_path) != 0)
2395 return got_error_from_errno2("unlink", ondisk_path);
2397 got_fileindex_entry_mark_deleted_from_disk(ie);
2398 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2401 const struct got_error *
2402 got_worktree_schedule_delete(struct got_worktree *worktree,
2403 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2404 got_worktree_status_cb status_cb, void *status_arg,
2405 struct got_repository *repo)
2407 struct got_fileindex *fileindex = NULL;
2408 char *fileindex_path = NULL;
2409 const struct got_error *err = NULL, *sync_err, *unlockerr;
2410 struct got_pathlist_entry *pe;
2412 err = lock_worktree(worktree, LOCK_EX);
2413 if (err)
2414 return err;
2416 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2417 if (err)
2418 goto done;
2420 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2421 char *relpath;
2422 err = got_path_skip_common_ancestor(&relpath,
2423 got_worktree_get_root_path(worktree), pe->path);
2424 if (err)
2425 break;
2426 err = schedule_for_deletion(pe->path, fileindex, relpath,
2427 delete_local_mods, status_cb, status_arg, repo);
2428 free(relpath);
2429 if (err)
2430 break;
2432 sync_err = sync_fileindex(fileindex, fileindex_path);
2433 if (sync_err && err == NULL)
2434 err = sync_err;
2435 done:
2436 free(fileindex_path);
2437 if (fileindex)
2438 got_fileindex_free(fileindex);
2439 unlockerr = lock_worktree(worktree, LOCK_SH);
2440 if (unlockerr && err == NULL)
2441 err = unlockerr;
2442 return err;
2445 static const struct got_error *
2446 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2447 const char *ondisk_path,
2448 got_worktree_checkout_cb progress_cb, void *progress_arg,
2449 struct got_repository *repo)
2451 const struct got_error *err = NULL;
2452 char *relpath = NULL, *parent_path = NULL;
2453 struct got_fileindex_entry *ie;
2454 struct got_tree_object *tree = NULL;
2455 struct got_object_id *tree_id = NULL;
2456 const struct got_tree_entry *te;
2457 char *tree_path = NULL, *te_name;
2458 struct got_blob_object *blob = NULL;
2459 unsigned char status;
2460 struct stat sb;
2462 err = got_path_skip_common_ancestor(&relpath,
2463 got_worktree_get_root_path(worktree), ondisk_path);
2464 if (err)
2465 goto done;
2467 ie = got_fileindex_entry_get(fileindex, relpath);
2468 if (ie == NULL) {
2469 err = got_error(GOT_ERR_BAD_PATH);
2470 goto done;
2473 /* Construct in-repository path of tree which contains this blob. */
2474 err = got_path_dirname(&parent_path, ie->path);
2475 if (err) {
2476 if (err->code != GOT_ERR_BAD_PATH)
2477 goto done;
2478 parent_path = strdup("/");
2479 if (parent_path == NULL) {
2480 err = got_error_from_errno("strdup");
2481 goto done;
2484 if (got_path_is_root_dir(worktree->path_prefix)) {
2485 tree_path = strdup(parent_path);
2486 if (tree_path == NULL) {
2487 err = got_error_from_errno("strdup");
2488 goto done;
2490 } else {
2491 if (got_path_is_root_dir(parent_path)) {
2492 tree_path = strdup(worktree->path_prefix);
2493 if (tree_path == NULL) {
2494 err = got_error_from_errno("strdup");
2495 goto done;
2497 } else {
2498 if (asprintf(&tree_path, "%s/%s",
2499 worktree->path_prefix, parent_path) == -1) {
2500 err = got_error_from_errno("asprintf");
2501 goto done;
2506 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2507 tree_path);
2508 if (err)
2509 goto done;
2511 err = got_object_open_as_tree(&tree, repo, tree_id);
2512 if (err)
2513 goto done;
2515 te_name = basename(ie->path);
2516 if (te_name == NULL) {
2517 err = got_error_from_errno2("basename", ie->path);
2518 goto done;
2521 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2522 if (err)
2523 goto done;
2525 te = got_object_tree_find_entry(tree, te_name);
2526 if (te == NULL && status != GOT_STATUS_ADD) {
2527 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2528 goto done;
2531 switch (status) {
2532 case GOT_STATUS_ADD:
2533 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2534 if (err)
2535 goto done;
2536 got_fileindex_entry_remove(fileindex, ie);
2537 break;
2538 case GOT_STATUS_DELETE:
2539 case GOT_STATUS_MODIFY:
2540 case GOT_STATUS_CONFLICT:
2541 case GOT_STATUS_MISSING: {
2542 struct got_object_id id;
2543 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2544 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2545 if (err)
2546 goto done;
2547 err = install_blob(worktree, ondisk_path, ie->path,
2548 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2549 progress_arg);
2550 if (err)
2551 goto done;
2552 if (status == GOT_STATUS_DELETE) {
2553 err = update_blob_fileindex_entry(worktree,
2554 fileindex, ie, ondisk_path, ie->path, blob, 1);
2555 if (err)
2556 goto done;
2558 break;
2560 default:
2561 goto done;
2563 done:
2564 free(relpath);
2565 free(parent_path);
2566 free(tree_path);
2567 if (blob)
2568 got_object_blob_close(blob);
2569 if (tree)
2570 got_object_tree_close(tree);
2571 free(tree_id);
2572 return err;
2575 const struct got_error *
2576 got_worktree_revert(struct got_worktree *worktree,
2577 struct got_pathlist_head *ondisk_paths,
2578 got_worktree_checkout_cb progress_cb, void *progress_arg,
2579 struct got_repository *repo)
2581 struct got_fileindex *fileindex = NULL;
2582 char *fileindex_path = NULL;
2583 const struct got_error *err = NULL, *unlockerr = NULL;
2584 const struct got_error *sync_err = NULL;
2585 struct got_pathlist_entry *pe;
2587 err = lock_worktree(worktree, LOCK_EX);
2588 if (err)
2589 return err;
2591 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2592 if (err)
2593 goto done;
2595 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2596 err = revert_file(worktree, fileindex, pe->path,
2597 progress_cb, progress_arg, repo);
2598 if (err)
2599 break;
2601 sync_err = sync_fileindex(fileindex, fileindex_path);
2602 if (sync_err && err == NULL)
2603 err = sync_err;
2604 done:
2605 free(fileindex_path);
2606 if (fileindex)
2607 got_fileindex_free(fileindex);
2608 unlockerr = lock_worktree(worktree, LOCK_SH);
2609 if (unlockerr && err == NULL)
2610 err = unlockerr;
2611 return err;
2614 static void
2615 free_commitable(struct got_commitable *ct)
2617 free(ct->path);
2618 free(ct->in_repo_path);
2619 free(ct->ondisk_path);
2620 free(ct->blob_id);
2621 free(ct->base_blob_id);
2622 free(ct->base_commit_id);
2623 free(ct);
2626 struct collect_commitables_arg {
2627 struct got_pathlist_head *commitable_paths;
2628 struct got_repository *repo;
2629 struct got_worktree *worktree;
2632 static const struct got_error *
2633 collect_commitables(void *arg, unsigned char status, const char *relpath,
2634 struct got_object_id *blob_id, struct got_object_id *commit_id)
2636 struct collect_commitables_arg *a = arg;
2637 const struct got_error *err = NULL;
2638 struct got_commitable *ct = NULL;
2639 struct got_pathlist_entry *new = NULL;
2640 char *parent_path = NULL, *path = NULL;
2641 struct stat sb;
2643 if (status == GOT_STATUS_CONFLICT)
2644 return got_error(GOT_ERR_COMMIT_CONFLICT);
2646 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2647 status != GOT_STATUS_DELETE)
2648 return NULL;
2650 if (asprintf(&path, "/%s", relpath) == -1) {
2651 err = got_error_from_errno("asprintf");
2652 goto done;
2654 if (strcmp(path, "/") == 0) {
2655 parent_path = strdup("");
2656 if (parent_path == NULL)
2657 return got_error_from_errno("strdup");
2658 } else {
2659 err = got_path_dirname(&parent_path, path);
2660 if (err)
2661 return err;
2664 ct = calloc(1, sizeof(*ct));
2665 if (ct == NULL) {
2666 err = got_error_from_errno("calloc");
2667 goto done;
2670 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2671 relpath) == -1) {
2672 err = got_error_from_errno("asprintf");
2673 goto done;
2675 if (status == GOT_STATUS_DELETE) {
2676 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2677 } else {
2678 if (lstat(ct->ondisk_path, &sb) != 0) {
2679 err = got_error_from_errno2("lstat", ct->ondisk_path);
2680 goto done;
2682 ct->mode = sb.st_mode;
2685 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2686 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2687 relpath) == -1) {
2688 err = got_error_from_errno("asprintf");
2689 goto done;
2692 ct->status = status;
2693 ct->blob_id = NULL; /* will be filled in when blob gets created */
2694 if (ct->status != GOT_STATUS_ADD) {
2695 ct->base_blob_id = got_object_id_dup(blob_id);
2696 if (ct->base_blob_id == NULL) {
2697 err = got_error_from_errno("got_object_id_dup");
2698 goto done;
2700 ct->base_commit_id = got_object_id_dup(commit_id);
2701 if (ct->base_commit_id == NULL) {
2702 err = got_error_from_errno("got_object_id_dup");
2703 goto done;
2706 ct->path = strdup(path);
2707 if (ct->path == NULL) {
2708 err = got_error_from_errno("strdup");
2709 goto done;
2711 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2712 done:
2713 if (ct && (err || new == NULL))
2714 free_commitable(ct);
2715 free(parent_path);
2716 free(path);
2717 return err;
2720 static const struct got_error *write_tree(struct got_object_id **,
2721 struct got_tree_object *, const char *, struct got_pathlist_head *,
2722 got_worktree_status_cb status_cb, void *status_arg,
2723 struct got_repository *);
2725 static const struct got_error *
2726 write_subtree(struct got_object_id **new_subtree_id,
2727 struct got_tree_entry *te, const char *parent_path,
2728 struct got_pathlist_head *commitable_paths,
2729 got_worktree_status_cb status_cb, void *status_arg,
2730 struct got_repository *repo)
2732 const struct got_error *err = NULL;
2733 struct got_tree_object *subtree;
2734 char *subpath;
2736 if (asprintf(&subpath, "%s%s%s", parent_path,
2737 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2738 return got_error_from_errno("asprintf");
2740 err = got_object_open_as_tree(&subtree, repo, te->id);
2741 if (err)
2742 return err;
2744 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2745 status_cb, status_arg, repo);
2746 got_object_tree_close(subtree);
2747 free(subpath);
2748 return err;
2751 static const struct got_error *
2752 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2754 const struct got_error *err = NULL;
2755 char *ct_parent_path = NULL;
2757 *match = 0;
2759 if (strchr(ct->path, '/') == NULL) {
2760 *match = got_path_is_root_dir(path);
2761 return NULL;
2764 err = got_path_dirname(&ct_parent_path, ct->path);
2765 if (err)
2766 return err;
2767 *match = (strcmp(path, ct_parent_path) == 0);
2768 free(ct_parent_path);
2769 return err;
2772 static mode_t
2773 get_ct_file_mode(struct got_commitable *ct)
2775 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2778 static const struct got_error *
2779 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2780 struct got_tree_entry *te, struct got_commitable *ct)
2782 const struct got_error *err = NULL;
2784 *new_te = NULL;
2786 err = got_object_tree_entry_dup(new_te, te);
2787 if (err)
2788 goto done;
2790 (*new_te)->mode = get_ct_file_mode(ct);
2792 free((*new_te)->id);
2793 (*new_te)->id = got_object_id_dup(ct->blob_id);
2794 if ((*new_te)->id == NULL) {
2795 err = got_error_from_errno("got_object_id_dup");
2796 goto done;
2798 done:
2799 if (err && *new_te) {
2800 got_object_tree_entry_close(*new_te);
2801 *new_te = NULL;
2803 return err;
2806 static const struct got_error *
2807 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2808 struct got_commitable *ct)
2810 const struct got_error *err = NULL;
2811 char *ct_name;
2813 *new_te = NULL;
2815 *new_te = calloc(1, sizeof(**new_te));
2816 if (*new_te == NULL)
2817 return got_error_from_errno("calloc");
2819 ct_name = basename(ct->path);
2820 if (ct_name == NULL) {
2821 err = got_error_from_errno2("basename", ct->path);
2822 goto done;
2824 (*new_te)->name = strdup(ct_name);
2825 if ((*new_te)->name == NULL) {
2826 err = got_error_from_errno("strdup");
2827 goto done;
2830 (*new_te)->mode = get_ct_file_mode(ct);
2832 (*new_te)->id = got_object_id_dup(ct->blob_id);
2833 if ((*new_te)->id == NULL) {
2834 err = got_error_from_errno("got_object_id_dup");
2835 goto done;
2837 done:
2838 if (err && *new_te) {
2839 got_object_tree_entry_close(*new_te);
2840 *new_te = NULL;
2842 return err;
2845 static const struct got_error *
2846 insert_tree_entry(struct got_tree_entry *new_te,
2847 struct got_pathlist_head *paths)
2849 const struct got_error *err = NULL;
2850 struct got_pathlist_entry *new_pe;
2852 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2853 if (err)
2854 return err;
2855 if (new_pe == NULL)
2856 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2857 return NULL;
2860 static const struct got_error *
2861 report_ct_status(struct got_commitable *ct,
2862 got_worktree_status_cb status_cb, void *status_arg)
2864 const char *ct_path = ct->path;
2865 while (ct_path[0] == '/')
2866 ct_path++;
2867 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2870 static const struct got_error *
2871 match_modified_subtree(int *modified, struct got_tree_entry *te,
2872 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2874 const struct got_error *err = NULL;
2875 struct got_pathlist_entry *pe;
2876 char *te_path;
2878 *modified = 0;
2880 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2881 got_path_is_root_dir(base_tree_path) ? "" : "/",
2882 te->name) == -1)
2883 return got_error_from_errno("asprintf");
2885 TAILQ_FOREACH(pe, commitable_paths, entry) {
2886 struct got_commitable *ct = pe->data;
2887 *modified = got_path_is_child(ct->in_repo_path, te_path,
2888 strlen(te_path));
2889 if (*modified)
2890 break;
2893 free(te_path);
2894 return err;
2897 static const struct got_error *
2898 match_deleted_or_modified_ct(struct got_commitable **ctp,
2899 struct got_tree_entry *te, const char *base_tree_path,
2900 struct got_pathlist_head *commitable_paths)
2902 const struct got_error *err = NULL;
2903 struct got_pathlist_entry *pe;
2905 *ctp = NULL;
2907 TAILQ_FOREACH(pe, commitable_paths, entry) {
2908 struct got_commitable *ct = pe->data;
2909 char *ct_name = NULL;
2910 int path_matches;
2912 if (ct->status != GOT_STATUS_MODIFY &&
2913 ct->status != GOT_STATUS_DELETE)
2914 continue;
2916 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2917 continue;
2919 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2920 if (err)
2921 return err;
2922 if (!path_matches)
2923 continue;
2925 ct_name = basename(pe->path);
2926 if (ct_name == NULL)
2927 return got_error_from_errno2("basename", pe->path);
2929 if (strcmp(te->name, ct_name) != 0)
2930 continue;
2932 *ctp = ct;
2933 break;
2936 return err;
2939 static const struct got_error *
2940 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2941 const char *child_path, const char *path_base_tree,
2942 struct got_pathlist_head *commitable_paths,
2943 got_worktree_status_cb status_cb, void *status_arg,
2944 struct got_repository *repo)
2946 const struct got_error *err = NULL;
2947 struct got_tree_entry *new_te;
2948 char *subtree_path;
2950 *new_tep = NULL;
2952 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2953 got_path_is_root_dir(path_base_tree) ? "" : "/",
2954 child_path) == -1)
2955 return got_error_from_errno("asprintf");
2957 new_te = calloc(1, sizeof(*new_te));
2958 new_te->mode = S_IFDIR;
2959 new_te->name = strdup(child_path);
2960 if (new_te->name == NULL) {
2961 err = got_error_from_errno("strdup");
2962 got_object_tree_entry_close(new_te);
2963 goto done;
2965 err = write_tree(&new_te->id, NULL, subtree_path,
2966 commitable_paths, status_cb, status_arg, repo);
2967 if (err) {
2968 got_object_tree_entry_close(new_te);
2969 goto done;
2971 done:
2972 free(subtree_path);
2973 if (err == NULL)
2974 *new_tep = new_te;
2975 return err;
2978 static const struct got_error *
2979 write_tree(struct got_object_id **new_tree_id,
2980 struct got_tree_object *base_tree, const char *path_base_tree,
2981 struct got_pathlist_head *commitable_paths,
2982 got_worktree_status_cb status_cb, void *status_arg,
2983 struct got_repository *repo)
2985 const struct got_error *err = NULL;
2986 const struct got_tree_entries *base_entries = NULL;
2987 struct got_pathlist_head paths;
2988 struct got_tree_entries new_tree_entries;
2989 struct got_tree_entry *te, *new_te = NULL;
2990 struct got_pathlist_entry *pe;
2992 TAILQ_INIT(&paths);
2993 new_tree_entries.nentries = 0;
2994 SIMPLEQ_INIT(&new_tree_entries.head);
2996 /* Insert, and recurse into, newly added entries first. */
2997 TAILQ_FOREACH(pe, commitable_paths, entry) {
2998 struct got_commitable *ct = pe->data;
2999 char *child_path = NULL, *slash;
3001 if (ct->status != GOT_STATUS_ADD ||
3002 (ct->flags & GOT_COMMITABLE_ADDED))
3003 continue;
3005 if (!got_path_is_child(pe->path, path_base_tree,
3006 strlen(path_base_tree)))
3007 continue;
3009 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3010 pe->path);
3011 if (err)
3012 goto done;
3014 slash = strchr(child_path, '/');
3015 if (slash == NULL) {
3016 err = alloc_added_blob_tree_entry(&new_te, ct);
3017 if (err)
3018 goto done;
3019 err = report_ct_status(ct, status_cb, status_arg);
3020 if (err)
3021 goto done;
3022 ct->flags |= GOT_COMMITABLE_ADDED;
3023 err = insert_tree_entry(new_te, &paths);
3024 if (err)
3025 goto done;
3026 } else {
3027 *slash = '\0'; /* trim trailing path components */
3028 if (base_tree == NULL ||
3029 got_object_tree_find_entry(base_tree, child_path)
3030 == NULL) {
3031 err = make_subtree_for_added_blob(&new_te,
3032 child_path, path_base_tree,
3033 commitable_paths, status_cb, status_arg,
3034 repo);
3035 if (err)
3036 goto done;
3037 err = insert_tree_entry(new_te, &paths);
3038 if (err)
3039 goto done;
3044 if (base_tree) {
3045 /* Handle modified and deleted entries. */
3046 base_entries = got_object_tree_get_entries(base_tree);
3047 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3048 struct got_commitable *ct = NULL;
3050 if (S_ISDIR(te->mode)) {
3051 int modified;
3052 err = got_object_tree_entry_dup(&new_te, te);
3053 if (err)
3054 goto done;
3055 err = match_modified_subtree(&modified, te,
3056 path_base_tree, commitable_paths);
3057 if (err)
3058 goto done;
3059 /* Avoid recursion into unmodified subtrees. */
3060 if (modified) {
3061 free(new_te->id);
3062 err = write_subtree(&new_te->id, te,
3063 path_base_tree, commitable_paths,
3064 status_cb, status_arg, repo);
3065 if (err)
3066 goto done;
3068 err = insert_tree_entry(new_te, &paths);
3069 if (err)
3070 goto done;
3071 continue;
3074 err = match_deleted_or_modified_ct(&ct, te,
3075 path_base_tree, commitable_paths);
3076 if (ct) {
3077 /* NB: Deleted entries get dropped here. */
3078 if (ct->status == GOT_STATUS_MODIFY) {
3079 err = alloc_modified_blob_tree_entry(
3080 &new_te, te, ct);
3081 if (err)
3082 goto done;
3083 err = insert_tree_entry(new_te, &paths);
3084 if (err)
3085 goto done;
3087 err = report_ct_status(ct, status_cb,
3088 status_arg);
3089 if (err)
3090 goto done;
3091 } else {
3092 /* Entry is unchanged; just copy it. */
3093 err = got_object_tree_entry_dup(&new_te, te);
3094 if (err)
3095 goto done;
3096 err = insert_tree_entry(new_te, &paths);
3097 if (err)
3098 goto done;
3103 /* Write new list of entries; deleted entries have been dropped. */
3104 TAILQ_FOREACH(pe, &paths, entry) {
3105 struct got_tree_entry *te = pe->data;
3106 new_tree_entries.nentries++;
3107 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3109 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3110 done:
3111 got_object_tree_entries_close(&new_tree_entries);
3112 got_pathlist_free(&paths);
3113 return err;
3116 static const struct got_error *
3117 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3118 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3120 const struct got_error *err = NULL;
3121 struct got_pathlist_entry *pe;
3123 TAILQ_FOREACH(pe, commitable_paths, entry) {
3124 struct got_fileindex_entry *ie;
3125 struct got_commitable *ct = pe->data;
3127 ie = got_fileindex_entry_get(fileindex, pe->path);
3128 if (ie) {
3129 if (ct->status == GOT_STATUS_DELETE) {
3130 got_fileindex_entry_remove(fileindex, ie);
3131 got_fileindex_entry_free(ie);
3132 } else
3133 err = got_fileindex_entry_update(ie,
3134 ct->ondisk_path, ct->blob_id->sha1,
3135 new_base_commit_id->sha1, 1);
3136 } else {
3137 err = got_fileindex_entry_alloc(&ie,
3138 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3139 new_base_commit_id->sha1);
3140 if (err)
3141 break;
3142 err = got_fileindex_entry_add(fileindex, ie);
3143 if (err)
3144 break;
3147 return err;
3150 static const struct got_error *
3151 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3152 struct got_object_id *head_commit_id)
3154 const struct got_error *err = NULL;
3155 struct got_object_id *id_in_head = NULL, *id = NULL;
3156 struct got_commit_object *commit = NULL;
3157 char *path = NULL;
3158 const char *ct_path = ct->in_repo_path;
3160 while (ct_path[0] == '/')
3161 ct_path++;
3164 * Ensure that no modifications were made to files *and their parents*
3165 * in commits between the file's base commit and the branch head.
3167 * Checking the parents is important for detecting conflicting tree
3168 * configurations (files or parent folders might have been moved,
3169 * deleted, added again, etc.). Such changes need to be merged with
3170 * local changes before a commit can occur.
3172 * The implication is that the file's (parent) entry in the root
3173 * directory must have the same ID in all relevant commits.
3175 if (ct->status != GOT_STATUS_ADD) {
3176 struct got_object_qid *pid;
3177 char *slash;
3178 struct got_object_id *root_entry_id = NULL;
3180 /* Trivial case: base commit == head commit */
3181 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3182 return NULL;
3184 /* Compute the path to the root directory's entry. */
3185 path = strdup(ct_path);
3186 if (path == NULL) {
3187 err = got_error_from_errno("strdup");
3188 goto done;
3190 slash = strchr(path, '/');
3191 if (slash)
3192 *slash = '\0';
3194 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3195 if (err)
3196 goto done;
3198 err = got_object_id_by_path(&root_entry_id, repo,
3199 head_commit_id, path);
3200 if (err)
3201 goto done;
3203 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3204 while (pid) {
3205 struct got_commit_object *pcommit;
3207 err = got_object_id_by_path(&id, repo, pid->id, path);
3208 if (err) {
3209 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3210 goto done;
3211 err = NULL;
3212 break;
3215 err = got_object_id_by_path(&id, repo, pid->id, path);
3216 if (err)
3217 goto done;
3219 if (got_object_id_cmp(id, root_entry_id) != 0) {
3220 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3221 break;
3224 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3225 break; /* all relevant commits scanned */
3227 err = got_object_open_as_commit(&pcommit, repo,
3228 pid->id);
3229 if (err)
3230 goto done;
3232 got_object_commit_close(commit);
3233 commit = pcommit;
3234 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3235 commit));
3237 } else {
3238 /* Require that added files don't exist in the branch head. */
3239 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3240 ct_path);
3241 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3242 goto done;
3243 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3245 done:
3246 if (commit)
3247 got_object_commit_close(commit);
3248 free(id_in_head);
3249 free(id);
3250 free(path);
3251 return err;
3254 const struct got_error *
3255 commit_worktree(struct got_object_id **new_commit_id,
3256 struct got_pathlist_head *commitable_paths,
3257 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3258 const char *ondisk_path, const char *author, const char *committer,
3259 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3260 got_worktree_status_cb status_cb, void *status_arg,
3261 struct got_repository *repo)
3263 const struct got_error *err = NULL, *unlockerr = NULL;
3264 struct got_pathlist_entry *pe;
3265 const char *head_ref_name = NULL;
3266 struct got_commit_object *head_commit = NULL;
3267 struct got_reference *head_ref2 = NULL;
3268 struct got_object_id *head_commit_id2 = NULL;
3269 struct got_tree_object *head_tree = NULL;
3270 struct got_object_id *new_tree_id = NULL;
3271 struct got_object_id_queue parent_ids;
3272 struct got_object_qid *pid = NULL;
3273 char *logmsg = NULL;
3275 *new_commit_id = NULL;
3277 SIMPLEQ_INIT(&parent_ids);
3279 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3280 if (err)
3281 goto done;
3283 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3284 if (err)
3285 goto done;
3287 if (commit_msg_cb != NULL) {
3288 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3289 if (err)
3290 goto done;
3293 if (logmsg == NULL || strlen(logmsg) == 0) {
3294 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3295 goto done;
3298 /* Create blobs from added and modified files and record their IDs. */
3299 TAILQ_FOREACH(pe, commitable_paths, entry) {
3300 struct got_commitable *ct = pe->data;
3301 char *ondisk_path;
3303 if (ct->status != GOT_STATUS_ADD &&
3304 ct->status != GOT_STATUS_MODIFY)
3305 continue;
3307 if (asprintf(&ondisk_path, "%s/%s",
3308 worktree->root_path, pe->path) == -1) {
3309 err = got_error_from_errno("asprintf");
3310 goto done;
3312 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3313 free(ondisk_path);
3314 if (err)
3315 goto done;
3318 /* Recursively write new tree objects. */
3319 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3320 status_cb, status_arg, repo);
3321 if (err)
3322 goto done;
3324 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3325 if (err)
3326 goto done;
3327 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3328 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3329 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3330 got_object_qid_free(pid);
3331 if (logmsg != NULL)
3332 free(logmsg);
3333 if (err)
3334 goto done;
3336 /* Check if a concurrent commit to our branch has occurred. */
3337 head_ref_name = got_worktree_get_head_ref_name(worktree);
3338 if (head_ref_name == NULL) {
3339 err = got_error_from_errno("got_worktree_get_head_ref_name");
3340 goto done;
3342 /* Lock the reference here to prevent concurrent modification. */
3343 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3344 if (err)
3345 goto done;
3346 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3347 if (err)
3348 goto done;
3349 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3350 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3351 goto done;
3353 /* Update branch head in repository. */
3354 err = got_ref_change_ref(head_ref2, *new_commit_id);
3355 if (err)
3356 goto done;
3357 err = got_ref_write(head_ref2, repo);
3358 if (err)
3359 goto done;
3361 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3362 if (err)
3363 goto done;
3365 err = ref_base_commit(worktree, repo);
3366 if (err)
3367 goto done;
3368 done:
3369 if (head_tree)
3370 got_object_tree_close(head_tree);
3371 if (head_commit)
3372 got_object_commit_close(head_commit);
3373 free(head_commit_id2);
3374 if (head_ref2) {
3375 unlockerr = got_ref_unlock(head_ref2);
3376 if (unlockerr && err == NULL)
3377 err = unlockerr;
3378 got_ref_close(head_ref2);
3380 return err;
3383 const struct got_error *
3384 got_worktree_commit(struct got_object_id **new_commit_id,
3385 struct got_worktree *worktree, const char *ondisk_path,
3386 const char *author, const char *committer,
3387 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3388 got_worktree_status_cb status_cb, void *status_arg,
3389 struct got_repository *repo)
3391 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3392 struct got_fileindex *fileindex = NULL;
3393 char *fileindex_path = NULL, *relpath = NULL;
3394 struct got_pathlist_head commitable_paths;
3395 struct collect_commitables_arg cc_arg;
3396 struct got_pathlist_entry *pe;
3397 struct got_reference *head_ref = NULL;
3398 struct got_object_id *head_commit_id = NULL;
3400 *new_commit_id = NULL;
3402 TAILQ_INIT(&commitable_paths);
3404 err = lock_worktree(worktree, LOCK_EX);
3405 if (err)
3406 goto done;
3408 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3409 if (err)
3410 goto done;
3412 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3413 if (err)
3414 goto done;
3416 if (ondisk_path) {
3417 err = got_path_skip_common_ancestor(&relpath,
3418 worktree->root_path, ondisk_path);
3419 if (err)
3420 return err;
3423 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3424 if (err)
3425 goto done;
3427 cc_arg.commitable_paths = &commitable_paths;
3428 cc_arg.worktree = worktree;
3429 cc_arg.repo = repo;
3430 err = worktree_status(worktree, relpath ? relpath : "",
3431 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3432 if (err)
3433 goto done;
3435 if (TAILQ_EMPTY(&commitable_paths)) {
3436 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3437 goto done;
3440 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3441 struct got_commitable *ct = pe->data;
3442 err = check_ct_out_of_date(ct, repo, head_commit_id);
3443 if (err)
3444 goto done;
3447 err = commit_worktree(new_commit_id, &commitable_paths,
3448 head_commit_id, worktree, ondisk_path, author, committer,
3449 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3450 if (err)
3451 goto done;
3453 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3454 fileindex);
3455 sync_err = sync_fileindex(fileindex, fileindex_path);
3456 if (sync_err && err == NULL)
3457 err = sync_err;
3458 done:
3459 if (fileindex)
3460 got_fileindex_free(fileindex);
3461 free(fileindex_path);
3462 free(relpath);
3463 unlockerr = lock_worktree(worktree, LOCK_SH);
3464 if (unlockerr && err == NULL)
3465 err = unlockerr;
3466 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3467 struct got_commitable *ct = pe->data;
3468 free_commitable(ct);
3470 got_pathlist_free(&commitable_paths);
3471 return err;
3474 const char *
3475 got_commitable_get_path(struct got_commitable *ct)
3477 return ct->path;
3480 unsigned int
3481 got_commitable_get_status(struct got_commitable *ct)
3483 return ct->status;
3486 struct check_rebase_ok_arg {
3487 struct got_worktree *worktree;
3488 struct got_repository *repo;
3489 int rebase_in_progress;
3492 static const struct got_error *
3493 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3495 const struct got_error *err = NULL;
3496 struct check_rebase_ok_arg *a = arg;
3497 unsigned char status;
3498 struct stat sb;
3499 char *ondisk_path;
3501 if (!a->rebase_in_progress) {
3502 /* Reject rebase of a work tree with mixed base commits. */
3503 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3504 SHA1_DIGEST_LENGTH))
3505 return got_error(GOT_ERR_MIXED_COMMITS);
3508 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3509 == -1)
3510 return got_error_from_errno("asprintf");
3512 /* Reject rebase of a work tree with modified or conflicted files. */
3513 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3514 free(ondisk_path);
3515 if (err)
3516 return err;
3518 if (a->rebase_in_progress) {
3519 if (status == GOT_STATUS_CONFLICT)
3520 return got_error(GOT_ERR_CONFLICTS);
3521 } else if (status != GOT_STATUS_NO_CHANGE)
3522 return got_error(GOT_ERR_MODIFIED);
3524 return NULL;
3527 const struct got_error *
3528 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3529 struct got_reference **tmp_branch, struct got_worktree *worktree,
3530 struct got_reference *branch, struct got_repository *repo)
3532 const struct got_error *err = NULL;
3533 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3534 char *branch_ref_name = NULL;
3535 struct got_fileindex *fileindex = NULL;
3536 char *fileindex_path = NULL;
3537 struct check_rebase_ok_arg ok_arg;
3538 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3540 *new_base_branch_ref = NULL;
3541 *tmp_branch = NULL;
3543 err = lock_worktree(worktree, LOCK_EX);
3544 if (err)
3545 return err;
3547 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3548 if (err)
3549 goto done;
3551 ok_arg.worktree = worktree;
3552 ok_arg.repo = repo;
3553 ok_arg.rebase_in_progress = 0;
3554 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3555 &ok_arg);
3556 if (err)
3557 goto done;
3559 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3560 if (err)
3561 goto done;
3563 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3564 if (err)
3565 goto done;
3567 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3568 if (err)
3569 goto done;
3571 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3572 0);
3573 if (err)
3574 goto done;
3576 err = got_ref_alloc_symref(new_base_branch_ref,
3577 new_base_branch_ref_name, wt_branch);
3578 if (err)
3579 goto done;
3580 err = got_ref_write(*new_base_branch_ref, repo);
3581 if (err)
3582 goto done;
3584 /* TODO Lock original branch's ref while rebasing? */
3586 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3587 if (err)
3588 goto done;
3590 err = got_ref_write(branch_ref, repo);
3591 if (err)
3592 goto done;
3594 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3595 worktree->base_commit_id);
3596 if (err)
3597 goto done;
3598 err = got_ref_write(*tmp_branch, repo);
3599 if (err)
3600 goto done;
3602 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3603 if (err)
3604 goto done;
3605 done:
3606 free(fileindex_path);
3607 if (fileindex)
3608 got_fileindex_free(fileindex);
3609 free(tmp_branch_name);
3610 free(new_base_branch_ref_name);
3611 free(branch_ref_name);
3612 if (branch_ref)
3613 got_ref_close(branch_ref);
3614 if (wt_branch)
3615 got_ref_close(wt_branch);
3616 if (err) {
3617 if (*new_base_branch_ref) {
3618 got_ref_close(*new_base_branch_ref);
3619 *new_base_branch_ref = NULL;
3621 if (*tmp_branch) {
3622 got_ref_close(*tmp_branch);
3623 *tmp_branch = NULL;
3625 lock_worktree(worktree, LOCK_SH);
3627 return err;
3630 const struct got_error *
3631 got_worktree_rebase_continue(struct got_object_id **commit_id,
3632 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3633 struct got_reference **branch, struct got_worktree *worktree,
3634 struct got_repository *repo)
3636 const struct got_error *err;
3637 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3638 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3639 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3641 *commit_id = NULL;
3643 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3644 if (err)
3645 return err;
3647 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3648 if (err)
3649 goto done;
3651 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3652 if (err)
3653 goto done;
3655 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3656 if (err)
3657 goto done;
3659 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3660 if (err)
3661 goto done;
3663 err = got_ref_open(branch, repo,
3664 got_ref_get_symref_target(branch_ref), 0);
3665 if (err)
3666 goto done;
3668 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3669 if (err)
3670 goto done;
3672 err = got_ref_resolve(commit_id, repo, commit_ref);
3673 if (err)
3674 goto done;
3676 err = got_ref_open(new_base_branch, repo,
3677 new_base_branch_ref_name, 0);
3678 if (err)
3679 goto done;
3681 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3682 if (err)
3683 goto done;
3684 done:
3685 free(commit_ref_name);
3686 free(branch_ref_name);
3687 if (commit_ref)
3688 got_ref_close(commit_ref);
3689 if (branch_ref)
3690 got_ref_close(branch_ref);
3691 if (err) {
3692 free(*commit_id);
3693 *commit_id = NULL;
3694 if (*tmp_branch) {
3695 got_ref_close(*tmp_branch);
3696 *tmp_branch = NULL;
3698 if (*new_base_branch) {
3699 got_ref_close(*new_base_branch);
3700 *new_base_branch = NULL;
3702 if (*branch) {
3703 got_ref_close(*branch);
3704 *branch = NULL;
3707 return err;
3710 const struct got_error *
3711 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3713 const struct got_error *err;
3714 char *tmp_branch_name = NULL;
3716 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3717 if (err)
3718 return err;
3720 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3721 free(tmp_branch_name);
3722 return NULL;
3725 static const struct got_error *
3726 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3727 char **logmsg, void *arg)
3729 struct got_commit_object *commit = arg;
3731 *logmsg = strdup(got_object_commit_get_logmsg(commit));
3732 if (*logmsg == NULL)
3733 return got_error_from_errno("strdup");
3735 return NULL;
3738 static const struct got_error *
3739 rebase_status(void *arg, unsigned char status, const char *path,
3740 struct got_object_id *blob_id, struct got_object_id *commit_id)
3742 return NULL;
3745 const struct got_error *
3746 got_worktree_rebase_merge_files(struct got_worktree *worktree,
3747 struct got_object_id *parent_commit_id, struct got_object_id *commit_id,
3748 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
3749 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3751 const struct got_error *err;
3752 struct got_fileindex *fileindex;
3753 char *fileindex_path, *commit_ref_name = NULL;
3754 struct got_reference *commit_ref = NULL;
3756 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3758 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3759 if (err)
3760 return err;
3762 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3763 if (err)
3764 goto done;
3765 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3766 if (err) {
3767 if (err->code != GOT_ERR_NOT_REF)
3768 goto done;
3769 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3770 if (err)
3771 goto done;
3772 err = got_ref_write(commit_ref, repo);
3773 if (err)
3774 goto done;
3775 } else {
3776 struct got_object_id *stored_id;
3777 int cmp;
3779 err = got_ref_resolve(&stored_id, repo, commit_ref);
3780 if (err)
3781 goto done;
3782 cmp = got_object_id_cmp(commit_id, stored_id);
3783 free(stored_id);
3784 if (cmp != 0) {
3785 err = got_error(GOT_ERR_REBASE_COMMITID);
3786 goto done;
3790 err = merge_files(worktree, fileindex, fileindex_path,
3791 parent_commit_id, commit_id, repo, progress_cb, progress_arg,
3792 cancel_cb, cancel_arg);
3793 done:
3794 got_fileindex_free(fileindex);
3795 free(fileindex_path);
3796 if (commit_ref)
3797 got_ref_close(commit_ref);
3798 return err;
3801 const struct got_error *
3802 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3803 struct got_worktree *worktree, struct got_reference *tmp_branch,
3804 struct got_commit_object *orig_commit,
3805 struct got_object_id *orig_commit_id, struct got_repository *repo)
3807 const struct got_error *err, *sync_err;
3808 struct got_pathlist_head commitable_paths;
3809 struct collect_commitables_arg cc_arg;
3810 struct got_fileindex *fileindex = NULL;
3811 char *fileindex_path = NULL, *commit_ref_name = NULL;
3812 struct got_reference *head_ref = NULL;
3813 struct got_object_id *head_commit_id = NULL;
3814 struct got_reference *commit_ref = NULL;
3815 struct got_object_id *commit_id = NULL;
3817 TAILQ_INIT(&commitable_paths);
3818 *new_commit_id = NULL;
3820 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3822 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3823 if (err)
3824 return err;
3825 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3826 if (err)
3827 goto done;
3828 err = got_ref_resolve(&commit_id, repo, commit_ref);
3829 if (err)
3830 goto done;
3831 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3832 err = got_error(GOT_ERR_REBASE_COMMITID);
3833 goto done;
3836 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3837 if (err)
3838 goto done;
3840 cc_arg.commitable_paths = &commitable_paths;
3841 cc_arg.worktree = worktree;
3842 cc_arg.repo = repo;
3843 err = worktree_status(worktree, "", fileindex, repo,
3844 collect_commitables, &cc_arg, NULL, NULL);
3845 if (err)
3846 goto done;
3848 if (TAILQ_EMPTY(&commitable_paths)) {
3849 /* No-op change; commit will be elided. */
3850 err = got_ref_delete(commit_ref, repo);
3851 if (err)
3852 goto done;
3853 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3854 goto done;
3857 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3858 if (err)
3859 goto done;
3861 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3862 if (err)
3863 goto done;
3865 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
3866 worktree, NULL, got_object_commit_get_author(orig_commit),
3867 got_object_commit_get_committer(orig_commit),
3868 collect_rebase_commit_msg, orig_commit,
3869 rebase_status, NULL, repo);
3870 if (err)
3871 goto done;
3873 err = got_ref_change_ref(tmp_branch, *new_commit_id);
3874 if (err)
3875 goto done;
3877 err = got_ref_delete(commit_ref, repo);
3878 if (err)
3879 goto done;
3881 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3882 fileindex);
3883 sync_err = sync_fileindex(fileindex, fileindex_path);
3884 if (sync_err && err == NULL)
3885 err = sync_err;
3886 done:
3887 if (fileindex)
3888 got_fileindex_free(fileindex);
3889 free(fileindex_path);
3890 free(commit_ref_name);
3891 if (commit_ref)
3892 got_ref_close(commit_ref);
3893 free(head_commit_id);
3894 if (head_ref)
3895 got_ref_close(head_ref);
3896 if (err) {
3897 free(*new_commit_id);
3898 *new_commit_id = NULL;
3900 return err;
3903 const struct got_error *
3904 got_worktree_rebase_postpone(struct got_worktree *worktree)
3906 return lock_worktree(worktree, LOCK_SH);
3909 static const struct got_error *
3910 delete_ref(const char *name, struct got_repository *repo)
3912 const struct got_error *err;
3913 struct got_reference *ref;
3915 err = got_ref_open(&ref, repo, name, 0);
3916 if (err) {
3917 if (err->code == GOT_ERR_NOT_REF)
3918 return NULL;
3919 return err;
3922 err = got_ref_delete(ref, repo);
3923 got_ref_close(ref);
3924 return err;
3927 static const struct got_error *
3928 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
3930 const struct got_error *err;
3931 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3932 char *branch_ref_name = NULL, *commit_ref_name = NULL;
3934 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3935 if (err)
3936 goto done;
3937 err = delete_ref(tmp_branch_name, repo);
3938 if (err)
3939 goto done;
3941 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3942 if (err)
3943 goto done;
3944 err = delete_ref(new_base_branch_ref_name, repo);
3945 if (err)
3946 goto done;
3948 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3949 if (err)
3950 goto done;
3951 err = delete_ref(branch_ref_name, repo);
3952 if (err)
3953 goto done;
3955 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3956 if (err)
3957 goto done;
3958 err = delete_ref(commit_ref_name, repo);
3959 if (err)
3960 goto done;
3962 done:
3963 free(tmp_branch_name);
3964 free(new_base_branch_ref_name);
3965 free(branch_ref_name);
3966 free(commit_ref_name);
3967 return err;
3970 const struct got_error *
3971 got_worktree_rebase_complete(struct got_worktree *worktree,
3972 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3973 struct got_reference *rebased_branch,
3974 struct got_repository *repo)
3976 const struct got_error *err, *unlockerr;
3977 struct got_object_id *new_head_commit_id = NULL;
3979 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
3980 if (err)
3981 return err;
3983 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
3984 if (err)
3985 goto done;
3987 err = got_ref_write(rebased_branch, repo);
3988 if (err)
3989 goto done;
3991 err = got_worktree_set_head_ref(worktree, rebased_branch);
3992 if (err)
3993 goto done;
3995 err = delete_rebase_refs(worktree, repo);
3996 done:
3997 free(new_head_commit_id);
3998 unlockerr = lock_worktree(worktree, LOCK_SH);
3999 if (unlockerr && err == NULL)
4000 err = unlockerr;
4001 return err;
4004 struct collect_revertible_paths_arg {
4005 struct got_pathlist_head *revertible_paths;
4006 struct got_worktree *worktree;
4009 static const struct got_error *
4010 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4011 struct got_object_id *blob_id, struct got_object_id *commit_id)
4013 struct collect_revertible_paths_arg *a = arg;
4014 const struct got_error *err = NULL;
4015 struct got_pathlist_entry *new = NULL;
4016 char *path = NULL;
4018 if (status != GOT_STATUS_ADD &&
4019 status != GOT_STATUS_DELETE &&
4020 status != GOT_STATUS_MODIFY &&
4021 status != GOT_STATUS_CONFLICT &&
4022 status != GOT_STATUS_MISSING)
4023 return NULL;
4025 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4026 return got_error_from_errno("asprintf");
4028 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4029 if (err || new == NULL)
4030 free(path);
4031 return err;
4034 const struct got_error *
4035 got_worktree_rebase_abort(struct got_worktree *worktree,
4036 struct got_repository *repo, struct got_reference *new_base_branch,
4037 got_worktree_checkout_cb progress_cb, void *progress_arg)
4039 const struct got_error *err, *unlockerr, *sync_err;
4040 struct got_reference *resolved = NULL;
4041 struct got_object_id *commit_id = NULL;
4042 struct got_fileindex *fileindex = NULL;
4043 char *fileindex_path = NULL;
4044 struct got_pathlist_head revertible_paths;
4045 struct got_pathlist_entry *pe;
4046 struct collect_revertible_paths_arg crp_arg;
4047 struct got_object_id *tree_id = NULL;
4049 TAILQ_INIT(&revertible_paths);
4051 err = lock_worktree(worktree, LOCK_EX);
4052 if (err)
4053 return err;
4055 err = got_ref_open(&resolved, repo,
4056 got_ref_get_symref_target(new_base_branch), 0);
4057 if (err)
4058 goto done;
4060 err = got_worktree_set_head_ref(worktree, resolved);
4061 if (err)
4062 goto done;
4065 * XXX commits to the base branch could have happened while
4066 * we were busy rebasing; should we store the original commit ID
4067 * when rebase begins and read it back here?
4069 err = got_ref_resolve(&commit_id, repo, resolved);
4070 if (err)
4071 goto done;
4073 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4074 if (err)
4075 goto done;
4077 err = got_object_id_by_path(&tree_id, repo,
4078 worktree->base_commit_id, worktree->path_prefix);
4079 if (err)
4080 goto done;
4082 err = delete_rebase_refs(worktree, repo);
4083 if (err)
4084 goto done;
4086 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4087 if (err)
4088 goto done;
4090 crp_arg.revertible_paths = &revertible_paths;
4091 crp_arg.worktree = worktree;
4092 err = worktree_status(worktree, "", fileindex, repo,
4093 collect_revertible_paths, &crp_arg, NULL, NULL);
4094 if (err)
4095 goto done;
4097 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4098 err = revert_file(worktree, fileindex, pe->path,
4099 progress_cb, progress_arg, repo);
4100 if (err)
4101 goto sync;
4104 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4105 repo, progress_cb, progress_arg, NULL, NULL);
4106 sync:
4107 sync_err = sync_fileindex(fileindex, fileindex_path);
4108 if (sync_err && err == NULL)
4109 err = sync_err;
4110 done:
4111 got_ref_close(resolved);
4112 free(tree_id);
4113 free(commit_id);
4114 if (fileindex)
4115 got_fileindex_free(fileindex);
4116 free(fileindex_path);
4117 TAILQ_FOREACH(pe, &revertible_paths, entry)
4118 free((char *)pe->path);
4119 got_pathlist_free(&revertible_paths);
4121 unlockerr = lock_worktree(worktree, LOCK_SH);
4122 if (unlockerr && err == NULL)
4123 err = unlockerr;
4124 return err;