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 int
1027 stat_info_differs(struct got_fileindex_entry *ie, struct stat *sb)
1029 return !(ie->ctime_sec == sb->st_ctime &&
1030 ie->ctime_nsec == sb->st_ctimensec &&
1031 ie->mtime_sec == sb->st_mtime &&
1032 ie->mtime_nsec == sb->st_mtimensec &&
1033 ie->size == (sb->st_size & 0xffffffff));
1036 static const struct got_error *
1037 get_file_status(unsigned char *status, struct stat *sb,
1038 struct got_fileindex_entry *ie, const char *abspath,
1039 struct got_repository *repo)
1041 const struct got_error *err = NULL;
1042 struct got_object_id id;
1043 size_t hdrlen;
1044 FILE *f = NULL;
1045 uint8_t fbuf[8192];
1046 struct got_blob_object *blob = NULL;
1047 size_t flen, blen;
1049 *status = GOT_STATUS_NO_CHANGE;
1051 if (lstat(abspath, sb) == -1) {
1052 if (errno == ENOENT) {
1053 if (ie) {
1054 if (got_fileindex_entry_has_file_on_disk(ie))
1055 *status = GOT_STATUS_MISSING;
1056 else
1057 *status = GOT_STATUS_DELETE;
1058 sb->st_mode =
1059 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1060 & (S_IRWXU | S_IRWXG | S_IRWXO));
1061 } else
1062 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1063 return NULL;
1065 return got_error_from_errno2("lstat", abspath);
1068 if (!S_ISREG(sb->st_mode)) {
1069 *status = GOT_STATUS_OBSTRUCTED;
1070 return NULL;
1073 if (ie == NULL)
1074 return NULL;
1076 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1077 *status = GOT_STATUS_DELETE;
1078 return NULL;
1079 } else if (!got_fileindex_entry_has_blob(ie)) {
1080 *status = GOT_STATUS_ADD;
1081 return NULL;
1084 if (!stat_info_differs(ie, sb))
1085 return NULL;
1087 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1088 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1089 if (err)
1090 return err;
1092 f = fopen(abspath, "r");
1093 if (f == NULL) {
1094 err = got_error_from_errno2("fopen", abspath);
1095 goto done;
1097 hdrlen = got_object_blob_get_hdrlen(blob);
1098 for (;;) {
1099 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1100 err = got_object_blob_read_block(&blen, blob);
1101 if (err)
1102 goto done;
1103 /* Skip length of blob object header first time around. */
1104 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1105 if (flen == 0 && ferror(f)) {
1106 err = got_error_from_errno("fread");
1107 goto done;
1109 if (blen == 0) {
1110 if (flen != 0)
1111 *status = GOT_STATUS_MODIFY;
1112 break;
1113 } else if (flen == 0) {
1114 if (blen != 0)
1115 *status = GOT_STATUS_MODIFY;
1116 break;
1117 } else if (blen - hdrlen == flen) {
1118 /* Skip blob object header first time around. */
1119 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1120 *status = GOT_STATUS_MODIFY;
1121 break;
1123 } else {
1124 *status = GOT_STATUS_MODIFY;
1125 break;
1127 hdrlen = 0;
1130 if (*status == GOT_STATUS_MODIFY) {
1131 rewind(f);
1132 err = get_modified_file_content_status(status, f);
1134 done:
1135 if (blob)
1136 got_object_blob_close(blob);
1137 if (f)
1138 fclose(f);
1139 return err;
1143 * Update timestamps in the file index if a file is unmodified and
1144 * we had to run a full content comparison to find out.
1146 static const struct got_error *
1147 sync_timestamps(char *ondisk_path, unsigned char status,
1148 struct got_fileindex_entry *ie, struct stat *sb)
1150 if (status == GOT_STATUS_NO_CHANGE && stat_info_differs(ie, sb))
1151 return got_fileindex_entry_update(ie, ondisk_path,
1152 ie->blob_sha1, ie->commit_sha1, 1);
1154 return NULL;
1157 static const struct got_error *
1158 update_blob(struct got_worktree *worktree,
1159 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1160 struct got_tree_entry *te, const char *path,
1161 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1162 void *progress_arg)
1164 const struct got_error *err = NULL;
1165 struct got_blob_object *blob = NULL;
1166 char *ondisk_path;
1167 unsigned char status = GOT_STATUS_NO_CHANGE;
1168 struct stat sb;
1170 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1171 return got_error_from_errno("asprintf");
1173 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1174 if (err)
1175 goto done;
1177 if (status == GOT_STATUS_OBSTRUCTED) {
1178 err = (*progress_cb)(progress_arg, status, path);
1179 goto done;
1182 if (ie && status != GOT_STATUS_MISSING) {
1183 if (got_fileindex_entry_has_commit(ie) &&
1184 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1185 SHA1_DIGEST_LENGTH) == 0) {
1186 err = sync_timestamps(ondisk_path, status, ie, &sb);
1187 if (err)
1188 goto done;
1189 err = (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1190 path);
1191 goto done;
1193 if (got_fileindex_entry_has_blob(ie) &&
1194 memcmp(ie->blob_sha1, te->id->sha1,
1195 SHA1_DIGEST_LENGTH) == 0) {
1196 err = sync_timestamps(ondisk_path, status, ie, &sb);
1197 goto done;
1201 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1202 if (err)
1203 goto done;
1205 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1206 int update_timestamps;
1207 struct got_blob_object *blob2 = NULL;
1208 if (got_fileindex_entry_has_blob(ie)) {
1209 struct got_object_id id2;
1210 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1211 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1212 if (err)
1213 goto done;
1215 err = merge_blob(&update_timestamps, worktree, blob2,
1216 ondisk_path, path, sb.st_mode, blob,
1217 worktree->base_commit_id, repo,
1218 progress_cb, progress_arg);
1219 if (blob2)
1220 got_object_blob_close(blob2);
1222 * Do not update timestamps of files with local changes.
1223 * Otherwise, a future status walk would treat them as
1224 * unmodified files again.
1226 err = got_fileindex_entry_update(ie, ondisk_path,
1227 blob->id.sha1, worktree->base_commit_id->sha1,
1228 update_timestamps);
1229 } else if (status == GOT_STATUS_DELETE) {
1230 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1231 if (err)
1232 goto done;
1233 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1234 ondisk_path, path, blob, 0);
1235 if (err)
1236 goto done;
1237 } else {
1238 err = install_blob(worktree, ondisk_path, path, te->mode,
1239 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1240 repo, progress_cb, progress_arg);
1241 if (err)
1242 goto done;
1243 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1244 ondisk_path, path, blob, 1);
1245 if (err)
1246 goto done;
1248 got_object_blob_close(blob);
1249 done:
1250 free(ondisk_path);
1251 return err;
1254 static const struct got_error *
1255 remove_ondisk_file(const char *root_path, const char *path)
1257 const struct got_error *err = NULL;
1258 char *ondisk_path = NULL;
1260 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1261 return got_error_from_errno("asprintf");
1263 if (unlink(ondisk_path) == -1) {
1264 if (errno != ENOENT)
1265 err = got_error_from_errno2("unlink", ondisk_path);
1266 } else {
1267 char *parent = dirname(ondisk_path);
1268 while (parent && strcmp(parent, root_path) != 0) {
1269 if (rmdir(parent) == -1) {
1270 if (errno != ENOTEMPTY)
1271 err = got_error_from_errno2("rmdir",
1272 parent);
1273 break;
1275 parent = dirname(parent);
1278 free(ondisk_path);
1279 return err;
1282 static const struct got_error *
1283 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1284 struct got_fileindex_entry *ie, struct got_repository *repo,
1285 got_worktree_checkout_cb progress_cb, void *progress_arg)
1287 const struct got_error *err = NULL;
1288 unsigned char status;
1289 struct stat sb;
1290 char *ondisk_path;
1292 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1293 == -1)
1294 return got_error_from_errno("asprintf");
1296 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1297 if (err)
1298 return err;
1300 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1301 status == GOT_STATUS_ADD) {
1302 err = (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1303 if (err)
1304 return err;
1306 * Preserve the working file and change the deleted blob's
1307 * entry into a schedule-add entry.
1309 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1310 0);
1311 if (err)
1312 return err;
1313 } else {
1314 err = (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1315 if (err)
1316 return err;
1317 if (status == GOT_STATUS_NO_CHANGE) {
1318 err = remove_ondisk_file(worktree->root_path, ie->path);
1319 if (err)
1320 return err;
1322 got_fileindex_entry_remove(fileindex, ie);
1325 return err;
1328 struct diff_cb_arg {
1329 struct got_fileindex *fileindex;
1330 struct got_worktree *worktree;
1331 struct got_repository *repo;
1332 got_worktree_checkout_cb progress_cb;
1333 void *progress_arg;
1334 got_worktree_cancel_cb cancel_cb;
1335 void *cancel_arg;
1338 static const struct got_error *
1339 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1340 struct got_tree_entry *te, const char *parent_path)
1342 struct diff_cb_arg *a = arg;
1344 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1345 return got_error(GOT_ERR_CANCELLED);
1347 return update_blob(a->worktree, a->fileindex, ie, te,
1348 ie->path, a->repo, a->progress_cb, a->progress_arg);
1351 static const struct got_error *
1352 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1354 struct diff_cb_arg *a = arg;
1356 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1357 return got_error(GOT_ERR_CANCELLED);
1359 return delete_blob(a->worktree, a->fileindex, ie,
1360 a->repo, a->progress_cb, a->progress_arg);
1363 static const struct got_error *
1364 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1366 struct diff_cb_arg *a = arg;
1367 const struct got_error *err;
1368 char *path;
1370 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1371 return got_error(GOT_ERR_CANCELLED);
1373 if (asprintf(&path, "%s%s%s", parent_path,
1374 parent_path[0] ? "/" : "", te->name)
1375 == -1)
1376 return got_error_from_errno("asprintf");
1378 if (S_ISDIR(te->mode))
1379 err = add_dir_on_disk(a->worktree, path);
1380 else
1381 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1382 a->repo, a->progress_cb, a->progress_arg);
1384 free(path);
1385 return err;
1388 static const struct got_error *
1389 get_ref_name(char **refname, struct got_worktree *worktree, const char *prefix)
1391 const struct got_error *err = NULL;
1392 char *uuidstr = NULL;
1393 uint32_t uuid_status;
1395 *refname = NULL;
1397 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1398 if (uuid_status != uuid_s_ok)
1399 return got_error_uuid(uuid_status);
1401 if (asprintf(refname, "%s-%s", prefix, uuidstr)
1402 == -1) {
1403 err = got_error_from_errno("asprintf");
1404 *refname = NULL;
1406 free(uuidstr);
1407 return err;
1410 const struct got_error *
1411 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1413 return get_ref_name(refname, worktree, GOT_WORKTREE_BASE_REF_PREFIX);
1416 static const struct got_error *
1417 get_rebase_tmp_ref_name(char **refname, struct got_worktree *worktree)
1419 return get_ref_name(refname, worktree,
1420 GOT_WORKTREE_REBASE_TMP_REF_PREFIX);
1423 static const struct got_error *
1424 get_newbase_symref_name(char **refname, struct got_worktree *worktree)
1426 return get_ref_name(refname, worktree, GOT_WORKTREE_NEWBASE_REF_PREFIX);
1429 static const struct got_error *
1430 get_rebase_branch_symref_name(char **refname, struct got_worktree *worktree)
1432 return get_ref_name(refname, worktree,
1433 GOT_WORKTREE_REBASE_BRANCH_REF_PREFIX);
1436 static const struct got_error *
1437 get_rebase_commit_ref_name(char **refname, struct got_worktree *worktree)
1439 return get_ref_name(refname, worktree,
1440 GOT_WORKTREE_REBASE_COMMIT_REF_PREFIX);
1445 * Prevent Git's garbage collector from deleting our base commit by
1446 * setting a reference to our base commit's ID.
1448 static const struct got_error *
1449 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1451 const struct got_error *err = NULL;
1452 struct got_reference *ref = NULL;
1453 char *refname;
1455 err = got_worktree_get_base_ref_name(&refname, worktree);
1456 if (err)
1457 return err;
1459 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1460 if (err)
1461 goto done;
1463 err = got_ref_write(ref, repo);
1464 done:
1465 free(refname);
1466 if (ref)
1467 got_ref_close(ref);
1468 return err;
1471 static const struct got_error *
1472 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1473 struct got_worktree *worktree)
1475 const struct got_error *err = NULL;
1476 FILE *index = NULL;
1478 *fileindex_path = NULL;
1479 *fileindex = got_fileindex_alloc();
1480 if (*fileindex == NULL)
1481 return got_error_from_errno("got_fileindex_alloc");
1483 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1484 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1485 err = got_error_from_errno("asprintf");
1486 *fileindex_path = NULL;
1487 goto done;
1490 index = fopen(*fileindex_path, "rb");
1491 if (index == NULL) {
1492 if (errno != ENOENT)
1493 err = got_error_from_errno2("fopen", *fileindex_path);
1494 } else {
1495 err = got_fileindex_read(*fileindex, index);
1496 if (fclose(index) != 0 && err == NULL)
1497 err = got_error_from_errno("fclose");
1499 done:
1500 if (err) {
1501 free(*fileindex_path);
1502 *fileindex_path = NULL;
1503 got_fileindex_free(*fileindex);
1504 *fileindex = NULL;
1506 return err;
1509 struct bump_base_commit_id_arg {
1510 struct got_object_id *base_commit_id;
1511 const char *path;
1512 size_t path_len;
1513 const char *entry_name;
1514 got_worktree_checkout_cb progress_cb;
1515 void *progress_arg;
1518 /* Bump base commit ID of all files within an updated part of the work tree. */
1519 static const struct got_error *
1520 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1522 const struct got_error *err;
1523 struct bump_base_commit_id_arg *a = arg;
1525 if (a->entry_name) {
1526 if (strcmp(ie->path, a->path) != 0)
1527 return NULL;
1528 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1529 return NULL;
1531 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1532 SHA1_DIGEST_LENGTH) == 0)
1533 return NULL;
1535 if (a->progress_cb) {
1536 err = (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE,
1537 ie->path);
1538 if (err)
1539 return err;
1541 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1542 return NULL;
1545 static const struct got_error *
1546 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1548 const struct got_error *err = NULL;
1549 char *new_fileindex_path = NULL;
1550 FILE *new_index = NULL;
1552 err = got_opentemp_named(&new_fileindex_path, &new_index,
1553 fileindex_path);
1554 if (err)
1555 goto done;
1557 err = got_fileindex_write(fileindex, new_index);
1558 if (err)
1559 goto done;
1561 if (rename(new_fileindex_path, fileindex_path) != 0) {
1562 err = got_error_from_errno3("rename", new_fileindex_path,
1563 fileindex_path);
1564 unlink(new_fileindex_path);
1566 done:
1567 if (new_index)
1568 fclose(new_index);
1569 free(new_fileindex_path);
1570 return err;
1573 static const struct got_error *
1574 find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
1575 struct got_object_id **tree_id, const char *wt_relpath,
1576 struct got_worktree *worktree, struct got_repository *repo)
1578 const struct got_error *err = NULL;
1579 struct got_object_id *id = NULL;
1580 char *in_repo_path = NULL;
1581 int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
1583 *entry_type = GOT_OBJ_TYPE_ANY;
1584 *tree_relpath = NULL;
1585 *tree_id = NULL;
1587 if (wt_relpath[0] == '\0') {
1588 /* Check out all files within the work tree. */
1589 *entry_type = GOT_OBJ_TYPE_TREE;
1590 *tree_relpath = strdup("");
1591 if (*tree_relpath == NULL) {
1592 err = got_error_from_errno("strdup");
1593 goto done;
1595 err = got_object_id_by_path(tree_id, repo,
1596 worktree->base_commit_id, worktree->path_prefix);
1597 if (err)
1598 goto done;
1599 return NULL;
1602 /* Check out a subset of files in the work tree. */
1604 if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
1605 is_root_wt ? "" : "/", wt_relpath) == -1) {
1606 err = got_error_from_errno("asprintf");
1607 goto done;
1610 err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
1611 in_repo_path);
1612 if (err)
1613 goto done;
1615 free(in_repo_path);
1616 in_repo_path = NULL;
1618 err = got_object_get_type(entry_type, repo, id);
1619 if (err)
1620 goto done;
1622 if (*entry_type == GOT_OBJ_TYPE_BLOB) {
1623 /* Check out a single file. */
1624 if (strchr(wt_relpath, '/') == NULL) {
1625 /* Check out a single file in work tree's root dir. */
1626 in_repo_path = strdup(worktree->path_prefix);
1627 if (in_repo_path == NULL) {
1628 err = got_error_from_errno("strdup");
1629 goto done;
1631 *tree_relpath = strdup("");
1632 if (*tree_relpath == NULL) {
1633 err = got_error_from_errno("strdup");
1634 goto done;
1636 } else {
1637 /* Check out a single file in a subdirectory. */
1638 err = got_path_dirname(tree_relpath, wt_relpath);
1639 if (err)
1640 return err;
1641 if (asprintf(&in_repo_path, "%s%s%s",
1642 worktree->path_prefix, is_root_wt ? "" : "/",
1643 *tree_relpath) == -1) {
1644 err = got_error_from_errno("asprintf");
1645 goto done;
1648 err = got_object_id_by_path(tree_id, repo,
1649 worktree->base_commit_id, in_repo_path);
1650 } else {
1651 /* Check out all files within a subdirectory. */
1652 *tree_id = got_object_id_dup(id);
1653 if (*tree_id == NULL) {
1654 err = got_error_from_errno("got_object_id_dup");
1655 goto done;
1657 *tree_relpath = strdup(wt_relpath);
1658 if (*tree_relpath == NULL) {
1659 err = got_error_from_errno("strdup");
1660 goto done;
1663 done:
1664 free(id);
1665 free(in_repo_path);
1666 if (err) {
1667 *entry_type = GOT_OBJ_TYPE_ANY;
1668 free(*tree_relpath);
1669 *tree_relpath = NULL;
1670 free(*tree_id);
1671 *tree_id = NULL;
1673 return err;
1676 static const struct got_error *
1677 checkout_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1678 const char *relpath, struct got_object_id *tree_id, const char *entry_name,
1679 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1680 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1682 const struct got_error *err = NULL;
1683 struct got_commit_object *commit = NULL;
1684 struct got_tree_object *tree = NULL;
1685 struct got_fileindex_diff_tree_cb diff_cb;
1686 struct diff_cb_arg arg;
1688 err = ref_base_commit(worktree, repo);
1689 if (err)
1690 goto done;
1692 err = got_object_open_as_commit(&commit, repo,
1693 worktree->base_commit_id);
1694 if (err)
1695 goto done;
1697 err = got_object_open_as_tree(&tree, repo, tree_id);
1698 if (err)
1699 goto done;
1701 if (entry_name &&
1702 got_object_tree_find_entry(tree, entry_name) == NULL) {
1703 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1704 goto done;
1707 diff_cb.diff_old_new = diff_old_new;
1708 diff_cb.diff_old = diff_old;
1709 diff_cb.diff_new = diff_new;
1710 arg.fileindex = fileindex;
1711 arg.worktree = worktree;
1712 arg.repo = repo;
1713 arg.progress_cb = progress_cb;
1714 arg.progress_arg = progress_arg;
1715 arg.cancel_cb = cancel_cb;
1716 arg.cancel_arg = cancel_arg;
1717 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1718 entry_name, repo, &diff_cb, &arg);
1719 done:
1720 if (tree)
1721 got_object_tree_close(tree);
1722 if (commit)
1723 got_object_commit_close(commit);
1724 return err;
1727 const struct got_error *
1728 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1729 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1730 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1732 const struct got_error *err = NULL, *sync_err, *unlockerr;
1733 struct got_commit_object *commit = NULL;
1734 struct got_object_id *tree_id = NULL;
1735 struct got_tree_object *tree = NULL;
1736 struct got_fileindex *fileindex = NULL;
1737 char *fileindex_path = NULL;
1738 char *relpath = NULL, *entry_name = NULL;
1739 int entry_type;
1741 err = lock_worktree(worktree, LOCK_EX);
1742 if (err)
1743 return err;
1745 err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
1746 path, worktree, repo);
1747 if (err)
1748 goto done;
1750 if (entry_type == GOT_OBJ_TYPE_BLOB) {
1751 entry_name = basename(path);
1752 if (entry_name == NULL) {
1753 err = got_error_from_errno2("basename", path);
1754 goto done;
1759 * Read the file index.
1760 * Checking out files is supposed to be an idempotent operation.
1761 * If the on-disk file index is incomplete we will try to complete it.
1763 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1764 if (err)
1765 goto done;
1767 err = checkout_files(worktree, fileindex, relpath, tree_id, entry_name,
1768 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1769 if (err == NULL) {
1770 struct bump_base_commit_id_arg bbc_arg;
1771 bbc_arg.base_commit_id = worktree->base_commit_id;
1772 bbc_arg.entry_name = entry_name;
1773 bbc_arg.path = path;
1774 bbc_arg.path_len = strlen(path);
1775 bbc_arg.progress_cb = progress_cb;
1776 bbc_arg.progress_arg = progress_arg;
1777 err = got_fileindex_for_each_entry_safe(fileindex,
1778 bump_base_commit_id, &bbc_arg);
1780 sync_err = sync_fileindex(fileindex, fileindex_path);
1781 if (sync_err && err == NULL)
1782 err = sync_err;
1783 done:
1784 free(fileindex_path);
1785 free(relpath);
1786 if (tree)
1787 got_object_tree_close(tree);
1788 if (commit)
1789 got_object_commit_close(commit);
1790 if (fileindex)
1791 got_fileindex_free(fileindex);
1792 unlockerr = lock_worktree(worktree, LOCK_SH);
1793 if (unlockerr && err == NULL)
1794 err = unlockerr;
1795 return err;
1798 struct merge_file_cb_arg {
1799 struct got_worktree *worktree;
1800 struct got_fileindex *fileindex;
1801 got_worktree_checkout_cb progress_cb;
1802 void *progress_arg;
1803 got_worktree_cancel_cb cancel_cb;
1804 void *cancel_arg;
1805 struct got_object_id *commit_id2;
1808 static const struct got_error *
1809 merge_file_cb(void *arg, struct got_blob_object *blob1,
1810 struct got_blob_object *blob2, struct got_object_id *id1,
1811 struct got_object_id *id2, const char *path1, const char *path2,
1812 struct got_repository *repo)
1814 static const struct got_error *err = NULL;
1815 struct merge_file_cb_arg *a = arg;
1816 struct got_fileindex_entry *ie;
1817 char *ondisk_path = NULL;
1818 struct stat sb;
1819 unsigned char status;
1820 int local_changes_subsumed;
1822 if (blob1 && blob2) {
1823 ie = got_fileindex_entry_get(a->fileindex, path2);
1824 if (ie == NULL)
1825 return (*a->progress_cb)(a->progress_arg,
1826 GOT_STATUS_MISSING, path2);
1828 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1829 path2) == -1)
1830 return got_error_from_errno("asprintf");
1832 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1833 if (err)
1834 goto done;
1836 if (status == GOT_STATUS_DELETE) {
1837 err = (*a->progress_cb)(a->progress_arg,
1838 GOT_STATUS_MERGE, path2);
1839 goto done;
1841 if (status != GOT_STATUS_NO_CHANGE &&
1842 status != GOT_STATUS_MODIFY &&
1843 status != GOT_STATUS_CONFLICT &&
1844 status != GOT_STATUS_ADD) {
1845 err = (*a->progress_cb)(a->progress_arg, status, path2);
1846 goto done;
1849 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1850 ondisk_path, path2, sb.st_mode, blob2, a->commit_id2, repo,
1851 a->progress_cb, a->progress_arg);
1852 } else if (blob1) {
1853 ie = got_fileindex_entry_get(a->fileindex, path1);
1854 if (ie == NULL)
1855 return (*a->progress_cb)(a->progress_arg,
1856 GOT_STATUS_MISSING, path2);
1858 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1859 path1) == -1)
1860 return got_error_from_errno("asprintf");
1862 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1863 if (err)
1864 goto done;
1866 switch (status) {
1867 case GOT_STATUS_NO_CHANGE:
1868 err = (*a->progress_cb)(a->progress_arg,
1869 GOT_STATUS_DELETE, path1);
1870 if (err)
1871 goto done;
1872 err = remove_ondisk_file(a->worktree->root_path, path1);
1873 if (err)
1874 goto done;
1875 if (ie)
1876 got_fileindex_entry_mark_deleted_from_disk(ie);
1877 break;
1878 case GOT_STATUS_DELETE:
1879 case GOT_STATUS_MISSING:
1880 err = (*a->progress_cb)(a->progress_arg,
1881 GOT_STATUS_DELETE, path1);
1882 if (err)
1883 goto done;
1884 if (ie)
1885 got_fileindex_entry_mark_deleted_from_disk(ie);
1886 break;
1887 case GOT_STATUS_ADD:
1888 case GOT_STATUS_MODIFY:
1889 case GOT_STATUS_CONFLICT:
1890 err = (*a->progress_cb)(a->progress_arg,
1891 GOT_STATUS_CANNOT_DELETE, path1);
1892 if (err)
1893 goto done;
1894 break;
1895 case GOT_STATUS_OBSTRUCTED:
1896 err = (*a->progress_cb)(a->progress_arg, status, path1);
1897 if (err)
1898 goto done;
1899 break;
1900 default:
1901 break;
1903 } else if (blob2) {
1904 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1905 path2) == -1)
1906 return got_error_from_errno("asprintf");
1907 ie = got_fileindex_entry_get(a->fileindex, path2);
1908 if (ie) {
1909 err = get_file_status(&status, &sb, ie, ondisk_path,
1910 repo);
1911 if (err)
1912 goto done;
1913 if (status != GOT_STATUS_NO_CHANGE &&
1914 status != GOT_STATUS_MODIFY &&
1915 status != GOT_STATUS_CONFLICT &&
1916 status != GOT_STATUS_ADD) {
1917 err = (*a->progress_cb)(a->progress_arg,
1918 status, path2);
1919 goto done;
1921 err = merge_blob(&local_changes_subsumed, a->worktree,
1922 NULL, ondisk_path, path2, sb.st_mode, blob2,
1923 a->commit_id2, repo,
1924 a->progress_cb, a->progress_arg);
1925 if (status == GOT_STATUS_DELETE) {
1926 err = update_blob_fileindex_entry(a->worktree,
1927 a->fileindex, ie, ondisk_path, ie->path,
1928 blob2, 0);
1929 if (err)
1930 goto done;
1932 } else {
1933 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1934 err = install_blob(a->worktree, ondisk_path, path2,
1935 /* XXX get this from parent tree! */
1936 GOT_DEFAULT_FILE_MODE,
1937 sb.st_mode, blob2, 0, 0, repo,
1938 a->progress_cb, a->progress_arg);
1939 if (err)
1940 goto done;
1941 err = got_fileindex_entry_alloc(&ie,
1942 ondisk_path, path2, NULL, NULL);
1943 if (err)
1944 goto done;
1945 err = got_fileindex_entry_add(a->fileindex, ie);
1946 if (err) {
1947 got_fileindex_entry_free(ie);
1948 goto done;
1952 done:
1953 free(ondisk_path);
1954 return err;
1957 struct check_merge_ok_arg {
1958 struct got_worktree *worktree;
1959 struct got_repository *repo;
1962 static const struct got_error *
1963 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1965 const struct got_error *err = NULL;
1966 struct check_merge_ok_arg *a = arg;
1967 unsigned char status;
1968 struct stat sb;
1969 char *ondisk_path;
1971 /* Reject merges into a work tree with mixed base commits. */
1972 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1973 SHA1_DIGEST_LENGTH))
1974 return got_error(GOT_ERR_MIXED_COMMITS);
1976 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1977 == -1)
1978 return got_error_from_errno("asprintf");
1980 /* Reject merges into a work tree with conflicted files. */
1981 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1982 if (err)
1983 return err;
1984 if (status == GOT_STATUS_CONFLICT)
1985 return got_error(GOT_ERR_CONFLICTS);
1987 return NULL;
1990 static const struct got_error *
1991 merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
1992 const char *fileindex_path, struct got_object_id *commit_id1,
1993 struct got_object_id *commit_id2, struct got_repository *repo,
1994 got_worktree_checkout_cb progress_cb, void *progress_arg,
1995 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1997 const struct got_error *err = NULL, *sync_err;
1998 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1999 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2000 struct merge_file_cb_arg arg;
2002 if (commit_id1) {
2003 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
2004 worktree->path_prefix);
2005 if (err)
2006 goto done;
2008 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2009 if (err)
2010 goto done;
2013 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
2014 worktree->path_prefix);
2015 if (err)
2016 goto done;
2018 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2019 if (err)
2020 goto done;
2022 arg.worktree = worktree;
2023 arg.fileindex = fileindex;
2024 arg.progress_cb = progress_cb;
2025 arg.progress_arg = progress_arg;
2026 arg.cancel_cb = cancel_cb;
2027 arg.cancel_arg = cancel_arg;
2028 arg.commit_id2 = commit_id2;
2029 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
2030 sync_err = sync_fileindex(fileindex, fileindex_path);
2031 if (sync_err && err == NULL)
2032 err = sync_err;
2033 done:
2034 if (tree1)
2035 got_object_tree_close(tree1);
2036 if (tree2)
2037 got_object_tree_close(tree2);
2038 return err;
2041 const struct got_error *
2042 got_worktree_merge_files(struct got_worktree *worktree,
2043 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
2044 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
2045 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2047 const struct got_error *err, *unlockerr;
2048 char *fileindex_path = NULL;
2049 struct got_fileindex *fileindex = NULL;
2050 struct check_merge_ok_arg mok_arg;
2052 err = lock_worktree(worktree, LOCK_EX);
2053 if (err)
2054 return err;
2056 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2057 if (err)
2058 goto done;
2060 mok_arg.worktree = worktree;
2061 mok_arg.repo = repo;
2062 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
2063 &mok_arg);
2064 if (err)
2065 goto done;
2067 err = merge_files(worktree, fileindex, fileindex_path, commit_id1,
2068 commit_id2, repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
2069 done:
2070 if (fileindex)
2071 got_fileindex_free(fileindex);
2072 free(fileindex_path);
2073 unlockerr = lock_worktree(worktree, LOCK_SH);
2074 if (unlockerr && err == NULL)
2075 err = unlockerr;
2076 return err;
2079 struct diff_dir_cb_arg {
2080 struct got_fileindex *fileindex;
2081 struct got_worktree *worktree;
2082 const char *status_path;
2083 size_t status_path_len;
2084 struct got_repository *repo;
2085 got_worktree_status_cb status_cb;
2086 void *status_arg;
2087 got_worktree_cancel_cb cancel_cb;
2088 void *cancel_arg;
2091 static const struct got_error *
2092 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
2093 got_worktree_status_cb status_cb, void *status_arg,
2094 struct got_repository *repo)
2096 const struct got_error *err = NULL;
2097 unsigned char status = GOT_STATUS_NO_CHANGE;
2098 struct stat sb;
2099 struct got_object_id blob_id, commit_id;
2101 err = get_file_status(&status, &sb, ie, abspath, repo);
2102 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
2103 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2104 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2105 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
2106 &commit_id);
2108 return err;
2111 static const struct got_error *
2112 status_old_new(void *arg, struct got_fileindex_entry *ie,
2113 struct dirent *de, const char *parent_path)
2115 const struct got_error *err = NULL;
2116 struct diff_dir_cb_arg *a = arg;
2117 char *abspath;
2119 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2120 return got_error(GOT_ERR_CANCELLED);
2122 if (got_path_cmp(parent_path, a->status_path) != 0 &&
2123 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
2124 return NULL;
2126 if (parent_path[0]) {
2127 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
2128 parent_path, de->d_name) == -1)
2129 return got_error_from_errno("asprintf");
2130 } else {
2131 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
2132 de->d_name) == -1)
2133 return got_error_from_errno("asprintf");
2136 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
2137 a->repo);
2138 free(abspath);
2139 return err;
2142 static const struct got_error *
2143 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
2145 struct diff_dir_cb_arg *a = arg;
2146 struct got_object_id blob_id, commit_id;
2147 unsigned char status;
2149 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2150 return got_error(GOT_ERR_CANCELLED);
2152 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2153 return NULL;
2155 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2156 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2157 if (got_fileindex_entry_has_file_on_disk(ie))
2158 status = GOT_STATUS_MISSING;
2159 else
2160 status = GOT_STATUS_DELETE;
2161 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2162 &commit_id);
2165 static const struct got_error *
2166 status_new(void *arg, struct dirent *de, const char *parent_path)
2168 const struct got_error *err = NULL;
2169 struct diff_dir_cb_arg *a = arg;
2170 char *path = NULL;
2172 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2173 return got_error(GOT_ERR_CANCELLED);
2175 if (de->d_type == DT_DIR)
2176 return NULL;
2178 /* XXX ignore symlinks for now */
2179 if (de->d_type == DT_LNK)
2180 return NULL;
2182 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2183 return NULL;
2185 if (parent_path[0]) {
2186 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2187 return got_error_from_errno("asprintf");
2188 } else {
2189 path = de->d_name;
2192 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2193 NULL, NULL);
2194 if (parent_path[0])
2195 free(path);
2196 return err;
2199 static const struct got_error *
2200 worktree_status(struct got_worktree *worktree, const char *path,
2201 struct got_fileindex *fileindex, struct got_repository *repo,
2202 got_worktree_status_cb status_cb, void *status_arg,
2203 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2205 const struct got_error *err = NULL;
2206 DIR *workdir = NULL;
2207 struct got_fileindex_diff_dir_cb fdiff_cb;
2208 struct diff_dir_cb_arg arg;
2209 char *ondisk_path = NULL;
2211 if (asprintf(&ondisk_path, "%s%s%s",
2212 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2213 err = got_error_from_errno("asprintf");
2214 goto done;
2216 workdir = opendir(ondisk_path);
2217 if (workdir == NULL) {
2218 if (errno == ENOTDIR || errno == ENOENT) {
2219 struct got_fileindex_entry *ie;
2220 ie = got_fileindex_entry_get(fileindex, path);
2221 if (ie == NULL) {
2222 err = got_error(GOT_ERR_BAD_PATH);
2223 goto done;
2225 err = report_file_status(ie, ondisk_path,
2226 status_cb, status_arg, repo);
2227 goto done;
2228 } else {
2229 err = got_error_from_errno2("opendir", ondisk_path);
2230 goto done;
2233 fdiff_cb.diff_old_new = status_old_new;
2234 fdiff_cb.diff_old = status_old;
2235 fdiff_cb.diff_new = status_new;
2236 arg.fileindex = fileindex;
2237 arg.worktree = worktree;
2238 arg.status_path = path;
2239 arg.status_path_len = strlen(path);
2240 arg.repo = repo;
2241 arg.status_cb = status_cb;
2242 arg.status_arg = status_arg;
2243 arg.cancel_cb = cancel_cb;
2244 arg.cancel_arg = cancel_arg;
2245 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2246 path, repo, &fdiff_cb, &arg);
2247 done:
2248 if (workdir)
2249 closedir(workdir);
2250 free(ondisk_path);
2251 return err;
2254 const struct got_error *
2255 got_worktree_status(struct got_worktree *worktree, const char *path,
2256 struct got_repository *repo, got_worktree_status_cb status_cb,
2257 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2259 const struct got_error *err = NULL;
2260 char *fileindex_path = NULL;
2261 struct got_fileindex *fileindex = NULL;
2263 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2264 if (err)
2265 return err;
2267 err = worktree_status(worktree, path, fileindex, repo,
2268 status_cb, status_arg, cancel_cb, cancel_arg);
2269 free(fileindex_path);
2270 got_fileindex_free(fileindex);
2271 return err;
2274 const struct got_error *
2275 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2276 const char *arg)
2278 const struct got_error *err = NULL;
2279 char *resolved, *cwd = NULL, *path = NULL;
2280 size_t len;
2282 *wt_path = NULL;
2284 resolved = realpath(arg, NULL);
2285 if (resolved == NULL) {
2286 if (errno != ENOENT)
2287 return got_error_from_errno2("realpath", arg);
2288 cwd = getcwd(NULL, 0);
2289 if (cwd == NULL)
2290 return got_error_from_errno("getcwd");
2291 if (asprintf(&resolved, "%s/%s", cwd, arg) == -1) {
2292 err = got_error_from_errno("asprintf");
2293 goto done;
2297 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2298 strlen(got_worktree_get_root_path(worktree)))) {
2299 err = got_error(GOT_ERR_BAD_PATH);
2300 goto done;
2303 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2304 err = got_path_skip_common_ancestor(&path,
2305 got_worktree_get_root_path(worktree), resolved);
2306 if (err)
2307 goto done;
2308 } else {
2309 path = strdup("");
2310 if (path == NULL) {
2311 err = got_error_from_errno("strdup");
2312 goto done;
2316 /* XXX status walk can't deal with trailing slash! */
2317 len = strlen(path);
2318 while (path[len - 1] == '/') {
2319 path[len - 1] = '\0';
2320 len--;
2322 done:
2323 free(resolved);
2324 free(cwd);
2325 if (err == NULL)
2326 *wt_path = path;
2327 else
2328 free(path);
2329 return err;
2332 static const struct got_error *
2333 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2334 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2335 struct got_repository *repo)
2337 const struct got_error *err = NULL;
2338 struct got_fileindex_entry *ie;
2340 /* Re-adding an existing entry is a no-op. */
2341 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2342 return NULL;
2344 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2345 if (err)
2346 return err;
2348 err = got_fileindex_entry_add(fileindex, ie);
2349 if (err) {
2350 got_fileindex_entry_free(ie);
2351 return err;
2354 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2357 const struct got_error *
2358 got_worktree_schedule_add(struct got_worktree *worktree,
2359 struct got_pathlist_head *ondisk_paths,
2360 got_worktree_status_cb status_cb, void *status_arg,
2361 struct got_repository *repo)
2363 struct got_fileindex *fileindex = NULL;
2364 char *fileindex_path = NULL;
2365 const struct got_error *err = NULL, *sync_err, *unlockerr;
2366 struct got_pathlist_entry *pe;
2368 err = lock_worktree(worktree, LOCK_EX);
2369 if (err)
2370 return err;
2372 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2373 if (err)
2374 goto done;
2376 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2377 char *relpath;
2378 err = got_path_skip_common_ancestor(&relpath,
2379 got_worktree_get_root_path(worktree), pe->path);
2380 if (err)
2381 break;
2382 err = schedule_addition(pe->path, fileindex, relpath,
2383 status_cb, status_arg, repo);
2384 free(relpath);
2385 if (err)
2386 break;
2388 sync_err = sync_fileindex(fileindex, fileindex_path);
2389 if (sync_err && err == NULL)
2390 err = sync_err;
2391 done:
2392 free(fileindex_path);
2393 if (fileindex)
2394 got_fileindex_free(fileindex);
2395 unlockerr = lock_worktree(worktree, LOCK_SH);
2396 if (unlockerr && err == NULL)
2397 err = unlockerr;
2398 return err;
2401 static const struct got_error *
2402 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2403 const char *relpath, int delete_local_mods,
2404 got_worktree_status_cb status_cb, void *status_arg,
2405 struct got_repository *repo)
2407 const struct got_error *err = NULL;
2408 struct got_fileindex_entry *ie = NULL;
2409 unsigned char status;
2410 struct stat sb;
2412 ie = got_fileindex_entry_get(fileindex, relpath);
2413 if (ie == NULL)
2414 return got_error(GOT_ERR_BAD_PATH);
2416 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2417 if (err)
2418 return err;
2420 if (status != GOT_STATUS_NO_CHANGE) {
2421 if (status == GOT_STATUS_DELETE)
2422 return got_error_set_errno(ENOENT, ondisk_path);
2423 if (status != GOT_STATUS_MODIFY)
2424 return got_error(GOT_ERR_FILE_STATUS);
2425 if (!delete_local_mods)
2426 return got_error(GOT_ERR_FILE_MODIFIED);
2429 if (unlink(ondisk_path) != 0)
2430 return got_error_from_errno2("unlink", ondisk_path);
2432 got_fileindex_entry_mark_deleted_from_disk(ie);
2433 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2436 const struct got_error *
2437 got_worktree_schedule_delete(struct got_worktree *worktree,
2438 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2439 got_worktree_status_cb status_cb, void *status_arg,
2440 struct got_repository *repo)
2442 struct got_fileindex *fileindex = NULL;
2443 char *fileindex_path = NULL;
2444 const struct got_error *err = NULL, *sync_err, *unlockerr;
2445 struct got_pathlist_entry *pe;
2447 err = lock_worktree(worktree, LOCK_EX);
2448 if (err)
2449 return err;
2451 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2452 if (err)
2453 goto done;
2455 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2456 char *relpath;
2457 err = got_path_skip_common_ancestor(&relpath,
2458 got_worktree_get_root_path(worktree), pe->path);
2459 if (err)
2460 break;
2461 err = schedule_for_deletion(pe->path, fileindex, relpath,
2462 delete_local_mods, status_cb, status_arg, repo);
2463 free(relpath);
2464 if (err)
2465 break;
2467 sync_err = sync_fileindex(fileindex, fileindex_path);
2468 if (sync_err && err == NULL)
2469 err = sync_err;
2470 done:
2471 free(fileindex_path);
2472 if (fileindex)
2473 got_fileindex_free(fileindex);
2474 unlockerr = lock_worktree(worktree, LOCK_SH);
2475 if (unlockerr && err == NULL)
2476 err = unlockerr;
2477 return err;
2480 static const struct got_error *
2481 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2482 const char *ondisk_path,
2483 got_worktree_checkout_cb progress_cb, void *progress_arg,
2484 struct got_repository *repo)
2486 const struct got_error *err = NULL;
2487 char *relpath = NULL, *parent_path = NULL;
2488 struct got_fileindex_entry *ie;
2489 struct got_tree_object *tree = NULL;
2490 struct got_object_id *tree_id = NULL;
2491 const struct got_tree_entry *te;
2492 char *tree_path = NULL, *te_name;
2493 struct got_blob_object *blob = NULL;
2494 unsigned char status;
2495 struct stat sb;
2497 err = got_path_skip_common_ancestor(&relpath,
2498 got_worktree_get_root_path(worktree), ondisk_path);
2499 if (err)
2500 goto done;
2502 ie = got_fileindex_entry_get(fileindex, relpath);
2503 if (ie == NULL) {
2504 err = got_error(GOT_ERR_BAD_PATH);
2505 goto done;
2508 /* Construct in-repository path of tree which contains this blob. */
2509 err = got_path_dirname(&parent_path, ie->path);
2510 if (err) {
2511 if (err->code != GOT_ERR_BAD_PATH)
2512 goto done;
2513 parent_path = strdup("/");
2514 if (parent_path == NULL) {
2515 err = got_error_from_errno("strdup");
2516 goto done;
2519 if (got_path_is_root_dir(worktree->path_prefix)) {
2520 tree_path = strdup(parent_path);
2521 if (tree_path == NULL) {
2522 err = got_error_from_errno("strdup");
2523 goto done;
2525 } else {
2526 if (got_path_is_root_dir(parent_path)) {
2527 tree_path = strdup(worktree->path_prefix);
2528 if (tree_path == NULL) {
2529 err = got_error_from_errno("strdup");
2530 goto done;
2532 } else {
2533 if (asprintf(&tree_path, "%s/%s",
2534 worktree->path_prefix, parent_path) == -1) {
2535 err = got_error_from_errno("asprintf");
2536 goto done;
2541 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2542 tree_path);
2543 if (err)
2544 goto done;
2546 err = got_object_open_as_tree(&tree, repo, tree_id);
2547 if (err)
2548 goto done;
2550 te_name = basename(ie->path);
2551 if (te_name == NULL) {
2552 err = got_error_from_errno2("basename", ie->path);
2553 goto done;
2556 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2557 if (err)
2558 goto done;
2560 te = got_object_tree_find_entry(tree, te_name);
2561 if (te == NULL && status != GOT_STATUS_ADD) {
2562 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2563 goto done;
2566 switch (status) {
2567 case GOT_STATUS_ADD:
2568 err = (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2569 if (err)
2570 goto done;
2571 got_fileindex_entry_remove(fileindex, ie);
2572 break;
2573 case GOT_STATUS_DELETE:
2574 case GOT_STATUS_MODIFY:
2575 case GOT_STATUS_CONFLICT:
2576 case GOT_STATUS_MISSING: {
2577 struct got_object_id id;
2578 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2579 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2580 if (err)
2581 goto done;
2582 err = install_blob(worktree, ondisk_path, ie->path,
2583 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2584 progress_arg);
2585 if (err)
2586 goto done;
2587 if (status == GOT_STATUS_DELETE) {
2588 err = update_blob_fileindex_entry(worktree,
2589 fileindex, ie, ondisk_path, ie->path, blob, 1);
2590 if (err)
2591 goto done;
2593 break;
2595 default:
2596 goto done;
2598 done:
2599 free(relpath);
2600 free(parent_path);
2601 free(tree_path);
2602 if (blob)
2603 got_object_blob_close(blob);
2604 if (tree)
2605 got_object_tree_close(tree);
2606 free(tree_id);
2607 return err;
2610 const struct got_error *
2611 got_worktree_revert(struct got_worktree *worktree,
2612 struct got_pathlist_head *ondisk_paths,
2613 got_worktree_checkout_cb progress_cb, void *progress_arg,
2614 struct got_repository *repo)
2616 struct got_fileindex *fileindex = NULL;
2617 char *fileindex_path = NULL;
2618 const struct got_error *err = NULL, *unlockerr = NULL;
2619 const struct got_error *sync_err = NULL;
2620 struct got_pathlist_entry *pe;
2622 err = lock_worktree(worktree, LOCK_EX);
2623 if (err)
2624 return err;
2626 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2627 if (err)
2628 goto done;
2630 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2631 err = revert_file(worktree, fileindex, pe->path,
2632 progress_cb, progress_arg, repo);
2633 if (err)
2634 break;
2636 sync_err = sync_fileindex(fileindex, fileindex_path);
2637 if (sync_err && err == NULL)
2638 err = sync_err;
2639 done:
2640 free(fileindex_path);
2641 if (fileindex)
2642 got_fileindex_free(fileindex);
2643 unlockerr = lock_worktree(worktree, LOCK_SH);
2644 if (unlockerr && err == NULL)
2645 err = unlockerr;
2646 return err;
2649 static void
2650 free_commitable(struct got_commitable *ct)
2652 free(ct->path);
2653 free(ct->in_repo_path);
2654 free(ct->ondisk_path);
2655 free(ct->blob_id);
2656 free(ct->base_blob_id);
2657 free(ct->base_commit_id);
2658 free(ct);
2661 struct collect_commitables_arg {
2662 struct got_pathlist_head *commitable_paths;
2663 struct got_repository *repo;
2664 struct got_worktree *worktree;
2667 static const struct got_error *
2668 collect_commitables(void *arg, unsigned char status, const char *relpath,
2669 struct got_object_id *blob_id, struct got_object_id *commit_id)
2671 struct collect_commitables_arg *a = arg;
2672 const struct got_error *err = NULL;
2673 struct got_commitable *ct = NULL;
2674 struct got_pathlist_entry *new = NULL;
2675 char *parent_path = NULL, *path = NULL;
2676 struct stat sb;
2678 if (status == GOT_STATUS_CONFLICT)
2679 return got_error(GOT_ERR_COMMIT_CONFLICT);
2681 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2682 status != GOT_STATUS_DELETE)
2683 return NULL;
2685 if (asprintf(&path, "/%s", relpath) == -1) {
2686 err = got_error_from_errno("asprintf");
2687 goto done;
2689 if (strcmp(path, "/") == 0) {
2690 parent_path = strdup("");
2691 if (parent_path == NULL)
2692 return got_error_from_errno("strdup");
2693 } else {
2694 err = got_path_dirname(&parent_path, path);
2695 if (err)
2696 return err;
2699 ct = calloc(1, sizeof(*ct));
2700 if (ct == NULL) {
2701 err = got_error_from_errno("calloc");
2702 goto done;
2705 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2706 relpath) == -1) {
2707 err = got_error_from_errno("asprintf");
2708 goto done;
2710 if (status == GOT_STATUS_DELETE) {
2711 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2712 } else {
2713 if (lstat(ct->ondisk_path, &sb) != 0) {
2714 err = got_error_from_errno2("lstat", ct->ondisk_path);
2715 goto done;
2717 ct->mode = sb.st_mode;
2720 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2721 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2722 relpath) == -1) {
2723 err = got_error_from_errno("asprintf");
2724 goto done;
2727 ct->status = status;
2728 ct->blob_id = NULL; /* will be filled in when blob gets created */
2729 if (ct->status != GOT_STATUS_ADD) {
2730 ct->base_blob_id = got_object_id_dup(blob_id);
2731 if (ct->base_blob_id == NULL) {
2732 err = got_error_from_errno("got_object_id_dup");
2733 goto done;
2735 ct->base_commit_id = got_object_id_dup(commit_id);
2736 if (ct->base_commit_id == NULL) {
2737 err = got_error_from_errno("got_object_id_dup");
2738 goto done;
2741 ct->path = strdup(path);
2742 if (ct->path == NULL) {
2743 err = got_error_from_errno("strdup");
2744 goto done;
2746 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2747 done:
2748 if (ct && (err || new == NULL))
2749 free_commitable(ct);
2750 free(parent_path);
2751 free(path);
2752 return err;
2755 static const struct got_error *write_tree(struct got_object_id **,
2756 struct got_tree_object *, const char *, struct got_pathlist_head *,
2757 got_worktree_status_cb status_cb, void *status_arg,
2758 struct got_repository *);
2760 static const struct got_error *
2761 write_subtree(struct got_object_id **new_subtree_id,
2762 struct got_tree_entry *te, const char *parent_path,
2763 struct got_pathlist_head *commitable_paths,
2764 got_worktree_status_cb status_cb, void *status_arg,
2765 struct got_repository *repo)
2767 const struct got_error *err = NULL;
2768 struct got_tree_object *subtree;
2769 char *subpath;
2771 if (asprintf(&subpath, "%s%s%s", parent_path,
2772 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2773 return got_error_from_errno("asprintf");
2775 err = got_object_open_as_tree(&subtree, repo, te->id);
2776 if (err)
2777 return err;
2779 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2780 status_cb, status_arg, repo);
2781 got_object_tree_close(subtree);
2782 free(subpath);
2783 return err;
2786 static const struct got_error *
2787 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2789 const struct got_error *err = NULL;
2790 char *ct_parent_path = NULL;
2792 *match = 0;
2794 if (strchr(ct->path, '/') == NULL) {
2795 *match = got_path_is_root_dir(path);
2796 return NULL;
2799 err = got_path_dirname(&ct_parent_path, ct->path);
2800 if (err)
2801 return err;
2802 *match = (strcmp(path, ct_parent_path) == 0);
2803 free(ct_parent_path);
2804 return err;
2807 static mode_t
2808 get_ct_file_mode(struct got_commitable *ct)
2810 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2813 static const struct got_error *
2814 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2815 struct got_tree_entry *te, struct got_commitable *ct)
2817 const struct got_error *err = NULL;
2819 *new_te = NULL;
2821 err = got_object_tree_entry_dup(new_te, te);
2822 if (err)
2823 goto done;
2825 (*new_te)->mode = get_ct_file_mode(ct);
2827 free((*new_te)->id);
2828 (*new_te)->id = got_object_id_dup(ct->blob_id);
2829 if ((*new_te)->id == NULL) {
2830 err = got_error_from_errno("got_object_id_dup");
2831 goto done;
2833 done:
2834 if (err && *new_te) {
2835 got_object_tree_entry_close(*new_te);
2836 *new_te = NULL;
2838 return err;
2841 static const struct got_error *
2842 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2843 struct got_commitable *ct)
2845 const struct got_error *err = NULL;
2846 char *ct_name;
2848 *new_te = NULL;
2850 *new_te = calloc(1, sizeof(**new_te));
2851 if (*new_te == NULL)
2852 return got_error_from_errno("calloc");
2854 ct_name = basename(ct->path);
2855 if (ct_name == NULL) {
2856 err = got_error_from_errno2("basename", ct->path);
2857 goto done;
2859 (*new_te)->name = strdup(ct_name);
2860 if ((*new_te)->name == NULL) {
2861 err = got_error_from_errno("strdup");
2862 goto done;
2865 (*new_te)->mode = get_ct_file_mode(ct);
2867 (*new_te)->id = got_object_id_dup(ct->blob_id);
2868 if ((*new_te)->id == NULL) {
2869 err = got_error_from_errno("got_object_id_dup");
2870 goto done;
2872 done:
2873 if (err && *new_te) {
2874 got_object_tree_entry_close(*new_te);
2875 *new_te = NULL;
2877 return err;
2880 static const struct got_error *
2881 insert_tree_entry(struct got_tree_entry *new_te,
2882 struct got_pathlist_head *paths)
2884 const struct got_error *err = NULL;
2885 struct got_pathlist_entry *new_pe;
2887 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2888 if (err)
2889 return err;
2890 if (new_pe == NULL)
2891 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2892 return NULL;
2895 static const struct got_error *
2896 report_ct_status(struct got_commitable *ct,
2897 got_worktree_status_cb status_cb, void *status_arg)
2899 const char *ct_path = ct->path;
2900 while (ct_path[0] == '/')
2901 ct_path++;
2902 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2905 static const struct got_error *
2906 match_modified_subtree(int *modified, struct got_tree_entry *te,
2907 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2909 const struct got_error *err = NULL;
2910 struct got_pathlist_entry *pe;
2911 char *te_path;
2913 *modified = 0;
2915 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2916 got_path_is_root_dir(base_tree_path) ? "" : "/",
2917 te->name) == -1)
2918 return got_error_from_errno("asprintf");
2920 TAILQ_FOREACH(pe, commitable_paths, entry) {
2921 struct got_commitable *ct = pe->data;
2922 *modified = got_path_is_child(ct->in_repo_path, te_path,
2923 strlen(te_path));
2924 if (*modified)
2925 break;
2928 free(te_path);
2929 return err;
2932 static const struct got_error *
2933 match_deleted_or_modified_ct(struct got_commitable **ctp,
2934 struct got_tree_entry *te, const char *base_tree_path,
2935 struct got_pathlist_head *commitable_paths)
2937 const struct got_error *err = NULL;
2938 struct got_pathlist_entry *pe;
2940 *ctp = NULL;
2942 TAILQ_FOREACH(pe, commitable_paths, entry) {
2943 struct got_commitable *ct = pe->data;
2944 char *ct_name = NULL;
2945 int path_matches;
2947 if (ct->status != GOT_STATUS_MODIFY &&
2948 ct->status != GOT_STATUS_DELETE)
2949 continue;
2951 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2952 continue;
2954 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2955 if (err)
2956 return err;
2957 if (!path_matches)
2958 continue;
2960 ct_name = basename(pe->path);
2961 if (ct_name == NULL)
2962 return got_error_from_errno2("basename", pe->path);
2964 if (strcmp(te->name, ct_name) != 0)
2965 continue;
2967 *ctp = ct;
2968 break;
2971 return err;
2974 static const struct got_error *
2975 make_subtree_for_added_blob(struct got_tree_entry **new_tep,
2976 const char *child_path, const char *path_base_tree,
2977 struct got_pathlist_head *commitable_paths,
2978 got_worktree_status_cb status_cb, void *status_arg,
2979 struct got_repository *repo)
2981 const struct got_error *err = NULL;
2982 struct got_tree_entry *new_te;
2983 char *subtree_path;
2985 *new_tep = NULL;
2987 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2988 got_path_is_root_dir(path_base_tree) ? "" : "/",
2989 child_path) == -1)
2990 return got_error_from_errno("asprintf");
2992 new_te = calloc(1, sizeof(*new_te));
2993 new_te->mode = S_IFDIR;
2994 new_te->name = strdup(child_path);
2995 if (new_te->name == NULL) {
2996 err = got_error_from_errno("strdup");
2997 got_object_tree_entry_close(new_te);
2998 goto done;
3000 err = write_tree(&new_te->id, NULL, subtree_path,
3001 commitable_paths, status_cb, status_arg, repo);
3002 if (err) {
3003 got_object_tree_entry_close(new_te);
3004 goto done;
3006 done:
3007 free(subtree_path);
3008 if (err == NULL)
3009 *new_tep = new_te;
3010 return err;
3013 static const struct got_error *
3014 write_tree(struct got_object_id **new_tree_id,
3015 struct got_tree_object *base_tree, const char *path_base_tree,
3016 struct got_pathlist_head *commitable_paths,
3017 got_worktree_status_cb status_cb, void *status_arg,
3018 struct got_repository *repo)
3020 const struct got_error *err = NULL;
3021 const struct got_tree_entries *base_entries = NULL;
3022 struct got_pathlist_head paths;
3023 struct got_tree_entries new_tree_entries;
3024 struct got_tree_entry *te, *new_te = NULL;
3025 struct got_pathlist_entry *pe;
3027 TAILQ_INIT(&paths);
3028 new_tree_entries.nentries = 0;
3029 SIMPLEQ_INIT(&new_tree_entries.head);
3031 /* Insert, and recurse into, newly added entries first. */
3032 TAILQ_FOREACH(pe, commitable_paths, entry) {
3033 struct got_commitable *ct = pe->data;
3034 char *child_path = NULL, *slash;
3036 if (ct->status != GOT_STATUS_ADD ||
3037 (ct->flags & GOT_COMMITABLE_ADDED))
3038 continue;
3040 if (!got_path_is_child(pe->path, path_base_tree,
3041 strlen(path_base_tree)))
3042 continue;
3044 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
3045 pe->path);
3046 if (err)
3047 goto done;
3049 slash = strchr(child_path, '/');
3050 if (slash == NULL) {
3051 err = alloc_added_blob_tree_entry(&new_te, ct);
3052 if (err)
3053 goto done;
3054 err = report_ct_status(ct, status_cb, status_arg);
3055 if (err)
3056 goto done;
3057 ct->flags |= GOT_COMMITABLE_ADDED;
3058 err = insert_tree_entry(new_te, &paths);
3059 if (err)
3060 goto done;
3061 } else {
3062 *slash = '\0'; /* trim trailing path components */
3063 if (base_tree == NULL ||
3064 got_object_tree_find_entry(base_tree, child_path)
3065 == NULL) {
3066 err = make_subtree_for_added_blob(&new_te,
3067 child_path, path_base_tree,
3068 commitable_paths, status_cb, status_arg,
3069 repo);
3070 if (err)
3071 goto done;
3072 err = insert_tree_entry(new_te, &paths);
3073 if (err)
3074 goto done;
3079 if (base_tree) {
3080 /* Handle modified and deleted entries. */
3081 base_entries = got_object_tree_get_entries(base_tree);
3082 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
3083 struct got_commitable *ct = NULL;
3085 if (S_ISDIR(te->mode)) {
3086 int modified;
3087 err = got_object_tree_entry_dup(&new_te, te);
3088 if (err)
3089 goto done;
3090 err = match_modified_subtree(&modified, te,
3091 path_base_tree, commitable_paths);
3092 if (err)
3093 goto done;
3094 /* Avoid recursion into unmodified subtrees. */
3095 if (modified) {
3096 free(new_te->id);
3097 err = write_subtree(&new_te->id, te,
3098 path_base_tree, commitable_paths,
3099 status_cb, status_arg, repo);
3100 if (err)
3101 goto done;
3103 err = insert_tree_entry(new_te, &paths);
3104 if (err)
3105 goto done;
3106 continue;
3109 err = match_deleted_or_modified_ct(&ct, te,
3110 path_base_tree, commitable_paths);
3111 if (ct) {
3112 /* NB: Deleted entries get dropped here. */
3113 if (ct->status == GOT_STATUS_MODIFY) {
3114 err = alloc_modified_blob_tree_entry(
3115 &new_te, te, ct);
3116 if (err)
3117 goto done;
3118 err = insert_tree_entry(new_te, &paths);
3119 if (err)
3120 goto done;
3122 err = report_ct_status(ct, status_cb,
3123 status_arg);
3124 if (err)
3125 goto done;
3126 } else {
3127 /* Entry is unchanged; just copy it. */
3128 err = got_object_tree_entry_dup(&new_te, te);
3129 if (err)
3130 goto done;
3131 err = insert_tree_entry(new_te, &paths);
3132 if (err)
3133 goto done;
3138 /* Write new list of entries; deleted entries have been dropped. */
3139 TAILQ_FOREACH(pe, &paths, entry) {
3140 struct got_tree_entry *te = pe->data;
3141 new_tree_entries.nentries++;
3142 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3144 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3145 done:
3146 got_object_tree_entries_close(&new_tree_entries);
3147 got_pathlist_free(&paths);
3148 return err;
3151 static const struct got_error *
3152 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3153 struct got_object_id *new_base_commit_id, struct got_fileindex *fileindex)
3155 const struct got_error *err = NULL;
3156 struct got_pathlist_entry *pe;
3158 TAILQ_FOREACH(pe, commitable_paths, entry) {
3159 struct got_fileindex_entry *ie;
3160 struct got_commitable *ct = pe->data;
3162 ie = got_fileindex_entry_get(fileindex, pe->path);
3163 if (ie) {
3164 if (ct->status == GOT_STATUS_DELETE) {
3165 got_fileindex_entry_remove(fileindex, ie);
3166 got_fileindex_entry_free(ie);
3167 } else
3168 err = got_fileindex_entry_update(ie,
3169 ct->ondisk_path, ct->blob_id->sha1,
3170 new_base_commit_id->sha1, 1);
3171 } else {
3172 err = got_fileindex_entry_alloc(&ie,
3173 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3174 new_base_commit_id->sha1);
3175 if (err)
3176 break;
3177 err = got_fileindex_entry_add(fileindex, ie);
3178 if (err)
3179 break;
3182 return err;
3185 static const struct got_error *
3186 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3187 struct got_object_id *head_commit_id)
3189 const struct got_error *err = NULL;
3190 struct got_object_id *id_in_head = NULL, *id = NULL;
3191 struct got_commit_object *commit = NULL;
3192 char *path = NULL;
3193 const char *ct_path = ct->in_repo_path;
3195 while (ct_path[0] == '/')
3196 ct_path++;
3199 * Ensure that no modifications were made to files *and their parents*
3200 * in commits between the file's base commit and the branch head.
3202 * Checking the parents is important for detecting conflicting tree
3203 * configurations (files or parent folders might have been moved,
3204 * deleted, added again, etc.). Such changes need to be merged with
3205 * local changes before a commit can occur.
3207 * The implication is that the file's (parent) entry in the root
3208 * directory must have the same ID in all relevant commits.
3210 if (ct->status != GOT_STATUS_ADD) {
3211 struct got_object_qid *pid;
3212 char *slash;
3213 struct got_object_id *root_entry_id = NULL;
3215 /* Trivial case: base commit == head commit */
3216 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3217 return NULL;
3219 /* Compute the path to the root directory's entry. */
3220 path = strdup(ct_path);
3221 if (path == NULL) {
3222 err = got_error_from_errno("strdup");
3223 goto done;
3225 slash = strchr(path, '/');
3226 if (slash)
3227 *slash = '\0';
3229 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3230 if (err)
3231 goto done;
3233 err = got_object_id_by_path(&root_entry_id, repo,
3234 head_commit_id, path);
3235 if (err)
3236 goto done;
3238 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3239 while (pid) {
3240 struct got_commit_object *pcommit;
3242 err = got_object_id_by_path(&id, repo, pid->id, path);
3243 if (err) {
3244 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3245 goto done;
3246 err = NULL;
3247 break;
3250 err = got_object_id_by_path(&id, repo, pid->id, path);
3251 if (err)
3252 goto done;
3254 if (got_object_id_cmp(id, root_entry_id) != 0) {
3255 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3256 break;
3259 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3260 break; /* all relevant commits scanned */
3262 err = got_object_open_as_commit(&pcommit, repo,
3263 pid->id);
3264 if (err)
3265 goto done;
3267 got_object_commit_close(commit);
3268 commit = pcommit;
3269 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3270 commit));
3272 } else {
3273 /* Require that added files don't exist in the branch head. */
3274 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3275 ct_path);
3276 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3277 goto done;
3278 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3280 done:
3281 if (commit)
3282 got_object_commit_close(commit);
3283 free(id_in_head);
3284 free(id);
3285 free(path);
3286 return err;
3289 const struct got_error *
3290 commit_worktree(struct got_object_id **new_commit_id,
3291 struct got_pathlist_head *commitable_paths,
3292 struct got_object_id *head_commit_id, struct got_worktree *worktree,
3293 const char *ondisk_path, const char *author, const char *committer,
3294 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3295 got_worktree_status_cb status_cb, void *status_arg,
3296 struct got_repository *repo)
3298 const struct got_error *err = NULL, *unlockerr = NULL;
3299 struct got_pathlist_entry *pe;
3300 const char *head_ref_name = NULL;
3301 struct got_commit_object *head_commit = NULL;
3302 struct got_reference *head_ref2 = NULL;
3303 struct got_object_id *head_commit_id2 = NULL;
3304 struct got_tree_object *head_tree = NULL;
3305 struct got_object_id *new_tree_id = NULL;
3306 struct got_object_id_queue parent_ids;
3307 struct got_object_qid *pid = NULL;
3308 char *logmsg = NULL;
3310 *new_commit_id = NULL;
3312 SIMPLEQ_INIT(&parent_ids);
3314 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3315 if (err)
3316 goto done;
3318 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3319 if (err)
3320 goto done;
3322 if (commit_msg_cb != NULL) {
3323 err = commit_msg_cb(commitable_paths, &logmsg, commit_arg);
3324 if (err)
3325 goto done;
3328 if (logmsg == NULL || strlen(logmsg) == 0) {
3329 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3330 goto done;
3333 /* Create blobs from added and modified files and record their IDs. */
3334 TAILQ_FOREACH(pe, commitable_paths, entry) {
3335 struct got_commitable *ct = pe->data;
3336 char *ondisk_path;
3338 if (ct->status != GOT_STATUS_ADD &&
3339 ct->status != GOT_STATUS_MODIFY)
3340 continue;
3342 if (asprintf(&ondisk_path, "%s/%s",
3343 worktree->root_path, pe->path) == -1) {
3344 err = got_error_from_errno("asprintf");
3345 goto done;
3347 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3348 free(ondisk_path);
3349 if (err)
3350 goto done;
3353 /* Recursively write new tree objects. */
3354 err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
3355 status_cb, status_arg, repo);
3356 if (err)
3357 goto done;
3359 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3360 if (err)
3361 goto done;
3362 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3363 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3364 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3365 got_object_qid_free(pid);
3366 if (logmsg != NULL)
3367 free(logmsg);
3368 if (err)
3369 goto done;
3371 /* Check if a concurrent commit to our branch has occurred. */
3372 head_ref_name = got_worktree_get_head_ref_name(worktree);
3373 if (head_ref_name == NULL) {
3374 err = got_error_from_errno("got_worktree_get_head_ref_name");
3375 goto done;
3377 /* Lock the reference here to prevent concurrent modification. */
3378 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3379 if (err)
3380 goto done;
3381 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3382 if (err)
3383 goto done;
3384 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3385 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3386 goto done;
3388 /* Update branch head in repository. */
3389 err = got_ref_change_ref(head_ref2, *new_commit_id);
3390 if (err)
3391 goto done;
3392 err = got_ref_write(head_ref2, repo);
3393 if (err)
3394 goto done;
3396 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3397 if (err)
3398 goto done;
3400 err = ref_base_commit(worktree, repo);
3401 if (err)
3402 goto done;
3403 done:
3404 if (head_tree)
3405 got_object_tree_close(head_tree);
3406 if (head_commit)
3407 got_object_commit_close(head_commit);
3408 free(head_commit_id2);
3409 if (head_ref2) {
3410 unlockerr = got_ref_unlock(head_ref2);
3411 if (unlockerr && err == NULL)
3412 err = unlockerr;
3413 got_ref_close(head_ref2);
3415 return err;
3418 const struct got_error *
3419 got_worktree_commit(struct got_object_id **new_commit_id,
3420 struct got_worktree *worktree, const char *ondisk_path,
3421 const char *author, const char *committer,
3422 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3423 got_worktree_status_cb status_cb, void *status_arg,
3424 struct got_repository *repo)
3426 const struct got_error *err = NULL, *unlockerr = NULL, *sync_err;
3427 struct got_fileindex *fileindex = NULL;
3428 char *fileindex_path = NULL, *relpath = NULL;
3429 struct got_pathlist_head commitable_paths;
3430 struct collect_commitables_arg cc_arg;
3431 struct got_pathlist_entry *pe;
3432 struct got_reference *head_ref = NULL;
3433 struct got_object_id *head_commit_id = NULL;
3435 *new_commit_id = NULL;
3437 TAILQ_INIT(&commitable_paths);
3439 err = lock_worktree(worktree, LOCK_EX);
3440 if (err)
3441 goto done;
3443 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3444 if (err)
3445 goto done;
3447 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3448 if (err)
3449 goto done;
3451 if (ondisk_path) {
3452 err = got_path_skip_common_ancestor(&relpath,
3453 worktree->root_path, ondisk_path);
3454 if (err)
3455 return err;
3458 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3459 if (err)
3460 goto done;
3462 cc_arg.commitable_paths = &commitable_paths;
3463 cc_arg.worktree = worktree;
3464 cc_arg.repo = repo;
3465 err = worktree_status(worktree, relpath ? relpath : "",
3466 fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
3467 if (err)
3468 goto done;
3470 if (TAILQ_EMPTY(&commitable_paths)) {
3471 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3472 goto done;
3475 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3476 struct got_commitable *ct = pe->data;
3477 err = check_ct_out_of_date(ct, repo, head_commit_id);
3478 if (err)
3479 goto done;
3482 err = commit_worktree(new_commit_id, &commitable_paths,
3483 head_commit_id, worktree, ondisk_path, author, committer,
3484 commit_msg_cb, commit_arg, status_cb, status_arg, repo);
3485 if (err)
3486 goto done;
3488 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3489 fileindex);
3490 sync_err = sync_fileindex(fileindex, fileindex_path);
3491 if (sync_err && err == NULL)
3492 err = sync_err;
3493 done:
3494 if (fileindex)
3495 got_fileindex_free(fileindex);
3496 free(fileindex_path);
3497 free(relpath);
3498 unlockerr = lock_worktree(worktree, LOCK_SH);
3499 if (unlockerr && err == NULL)
3500 err = unlockerr;
3501 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3502 struct got_commitable *ct = pe->data;
3503 free_commitable(ct);
3505 got_pathlist_free(&commitable_paths);
3506 return err;
3509 const char *
3510 got_commitable_get_path(struct got_commitable *ct)
3512 return ct->path;
3515 unsigned int
3516 got_commitable_get_status(struct got_commitable *ct)
3518 return ct->status;
3521 struct check_rebase_ok_arg {
3522 struct got_worktree *worktree;
3523 struct got_repository *repo;
3524 int rebase_in_progress;
3527 static const struct got_error *
3528 check_rebase_ok(void *arg, struct got_fileindex_entry *ie)
3530 const struct got_error *err = NULL;
3531 struct check_rebase_ok_arg *a = arg;
3532 unsigned char status;
3533 struct stat sb;
3534 char *ondisk_path;
3536 if (!a->rebase_in_progress) {
3537 /* Reject rebase of a work tree with mixed base commits. */
3538 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
3539 SHA1_DIGEST_LENGTH))
3540 return got_error(GOT_ERR_MIXED_COMMITS);
3543 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
3544 == -1)
3545 return got_error_from_errno("asprintf");
3547 /* Reject rebase of a work tree with modified or conflicted files. */
3548 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
3549 free(ondisk_path);
3550 if (err)
3551 return err;
3553 if (a->rebase_in_progress) {
3554 if (status == GOT_STATUS_CONFLICT)
3555 return got_error(GOT_ERR_CONFLICTS);
3556 } else if (status != GOT_STATUS_NO_CHANGE)
3557 return got_error(GOT_ERR_MODIFIED);
3559 return NULL;
3562 const struct got_error *
3563 got_worktree_rebase_prepare(struct got_reference **new_base_branch_ref,
3564 struct got_reference **tmp_branch, struct got_worktree *worktree,
3565 struct got_reference *branch, struct got_repository *repo)
3567 const struct got_error *err = NULL;
3568 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
3569 char *branch_ref_name = NULL;
3570 struct got_fileindex *fileindex = NULL;
3571 char *fileindex_path = NULL;
3572 struct check_rebase_ok_arg ok_arg;
3573 struct got_reference *wt_branch = NULL, *branch_ref = NULL;
3575 *new_base_branch_ref = NULL;
3576 *tmp_branch = NULL;
3578 err = lock_worktree(worktree, LOCK_EX);
3579 if (err)
3580 return err;
3582 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3583 if (err)
3584 goto done;
3586 ok_arg.worktree = worktree;
3587 ok_arg.repo = repo;
3588 ok_arg.rebase_in_progress = 0;
3589 err = got_fileindex_for_each_entry_safe(fileindex, check_rebase_ok,
3590 &ok_arg);
3591 if (err)
3592 goto done;
3594 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3595 if (err)
3596 goto done;
3598 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3599 if (err)
3600 goto done;
3602 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3603 if (err)
3604 goto done;
3606 err = got_ref_open(&wt_branch, repo, worktree->head_ref_name,
3607 0);
3608 if (err)
3609 goto done;
3611 err = got_ref_alloc_symref(new_base_branch_ref,
3612 new_base_branch_ref_name, wt_branch);
3613 if (err)
3614 goto done;
3615 err = got_ref_write(*new_base_branch_ref, repo);
3616 if (err)
3617 goto done;
3619 /* TODO Lock original branch's ref while rebasing? */
3621 err = got_ref_alloc_symref(&branch_ref, branch_ref_name, branch);
3622 if (err)
3623 goto done;
3625 err = got_ref_write(branch_ref, repo);
3626 if (err)
3627 goto done;
3629 err = got_ref_alloc(tmp_branch, tmp_branch_name,
3630 worktree->base_commit_id);
3631 if (err)
3632 goto done;
3633 err = got_ref_write(*tmp_branch, repo);
3634 if (err)
3635 goto done;
3637 err = got_worktree_set_head_ref(worktree, *tmp_branch);
3638 if (err)
3639 goto done;
3640 done:
3641 free(fileindex_path);
3642 if (fileindex)
3643 got_fileindex_free(fileindex);
3644 free(tmp_branch_name);
3645 free(new_base_branch_ref_name);
3646 free(branch_ref_name);
3647 if (branch_ref)
3648 got_ref_close(branch_ref);
3649 if (wt_branch)
3650 got_ref_close(wt_branch);
3651 if (err) {
3652 if (*new_base_branch_ref) {
3653 got_ref_close(*new_base_branch_ref);
3654 *new_base_branch_ref = NULL;
3656 if (*tmp_branch) {
3657 got_ref_close(*tmp_branch);
3658 *tmp_branch = NULL;
3660 lock_worktree(worktree, LOCK_SH);
3662 return err;
3665 const struct got_error *
3666 got_worktree_rebase_continue(struct got_object_id **commit_id,
3667 struct got_reference **new_base_branch, struct got_reference **tmp_branch,
3668 struct got_reference **branch, struct got_worktree *worktree,
3669 struct got_repository *repo)
3671 const struct got_error *err;
3672 char *commit_ref_name = NULL, *new_base_branch_ref_name = NULL;
3673 char *tmp_branch_name = NULL, *branch_ref_name = NULL;
3674 struct got_reference *commit_ref = NULL, *branch_ref = NULL;
3676 *commit_id = NULL;
3678 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3679 if (err)
3680 return err;
3682 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
3683 if (err)
3684 goto done;
3686 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3687 if (err)
3688 goto done;
3690 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
3691 if (err)
3692 goto done;
3694 err = got_ref_open(&branch_ref, repo, branch_ref_name, 0);
3695 if (err)
3696 goto done;
3698 err = got_ref_open(branch, repo,
3699 got_ref_get_symref_target(branch_ref), 0);
3700 if (err)
3701 goto done;
3703 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3704 if (err)
3705 goto done;
3707 err = got_ref_resolve(commit_id, repo, commit_ref);
3708 if (err)
3709 goto done;
3711 err = got_ref_open(new_base_branch, repo,
3712 new_base_branch_ref_name, 0);
3713 if (err)
3714 goto done;
3716 err = got_ref_open(tmp_branch, repo, tmp_branch_name, 0);
3717 if (err)
3718 goto done;
3719 done:
3720 free(commit_ref_name);
3721 free(branch_ref_name);
3722 if (commit_ref)
3723 got_ref_close(commit_ref);
3724 if (branch_ref)
3725 got_ref_close(branch_ref);
3726 if (err) {
3727 free(*commit_id);
3728 *commit_id = NULL;
3729 if (*tmp_branch) {
3730 got_ref_close(*tmp_branch);
3731 *tmp_branch = NULL;
3733 if (*new_base_branch) {
3734 got_ref_close(*new_base_branch);
3735 *new_base_branch = NULL;
3737 if (*branch) {
3738 got_ref_close(*branch);
3739 *branch = NULL;
3742 return err;
3745 const struct got_error *
3746 got_worktree_rebase_in_progress(int *in_progress, struct got_worktree *worktree)
3748 const struct got_error *err;
3749 char *tmp_branch_name = NULL;
3751 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
3752 if (err)
3753 return err;
3755 *in_progress = (strcmp(tmp_branch_name, worktree->head_ref_name) == 0);
3756 free(tmp_branch_name);
3757 return NULL;
3760 static const struct got_error *
3761 collect_rebase_commit_msg(struct got_pathlist_head *commitable_paths,
3762 char **logmsg, void *arg)
3764 struct got_commit_object *commit = arg;
3766 *logmsg = strdup(got_object_commit_get_logmsg(commit));
3767 if (*logmsg == NULL)
3768 return got_error_from_errno("strdup");
3770 return NULL;
3773 static const struct got_error *
3774 rebase_status(void *arg, unsigned char status, const char *path,
3775 struct got_object_id *blob_id, struct got_object_id *commit_id)
3777 return NULL;
3780 struct collect_merged_paths_arg {
3781 got_worktree_checkout_cb progress_cb;
3782 void *progress_arg;
3783 struct got_pathlist_head *merged_paths;
3786 static const struct got_error *
3787 collect_merged_paths(void *arg, unsigned char status, const char *path)
3789 const struct got_error *err;
3790 struct collect_merged_paths_arg *a = arg;
3791 char *p;
3792 struct got_pathlist_entry *new;
3794 err = (*a->progress_cb)(a->progress_arg, status, path);
3795 if (err)
3796 return err;
3798 if (status != GOT_STATUS_MERGE &&
3799 status != GOT_STATUS_ADD &&
3800 status != GOT_STATUS_DELETE &&
3801 status != GOT_STATUS_CONFLICT)
3802 return NULL;
3804 p = strdup(path);
3805 if (p == NULL)
3806 return got_error_from_errno("strdup");
3808 err = got_pathlist_insert(&new, a->merged_paths, p, NULL);
3809 if (err || new == NULL)
3810 free(p);
3811 return err;
3814 void
3815 got_worktree_rebase_pathlist_free(struct got_pathlist_head *merged_paths)
3817 struct got_pathlist_entry *pe;
3819 TAILQ_FOREACH(pe, merged_paths, entry)
3820 free((char *)pe->path);
3822 got_pathlist_free(merged_paths);
3825 const struct got_error *
3826 got_worktree_rebase_merge_files(struct got_pathlist_head *merged_paths,
3827 struct got_worktree *worktree, struct got_object_id *parent_commit_id,
3828 struct got_object_id *commit_id, struct got_repository *repo,
3829 got_worktree_checkout_cb progress_cb, void *progress_arg,
3830 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
3832 const struct got_error *err;
3833 struct got_fileindex *fileindex;
3834 char *fileindex_path, *commit_ref_name = NULL;
3835 struct got_reference *commit_ref = NULL;
3836 struct collect_merged_paths_arg cmp_arg;
3838 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3840 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3841 if (err)
3842 return err;
3844 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3845 if (err)
3846 goto done;
3847 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3848 if (err) {
3849 if (err->code != GOT_ERR_NOT_REF)
3850 goto done;
3851 err = got_ref_alloc(&commit_ref, commit_ref_name, commit_id);
3852 if (err)
3853 goto done;
3854 err = got_ref_write(commit_ref, repo);
3855 if (err)
3856 goto done;
3857 } else {
3858 struct got_object_id *stored_id;
3859 int cmp;
3861 err = got_ref_resolve(&stored_id, repo, commit_ref);
3862 if (err)
3863 goto done;
3864 cmp = got_object_id_cmp(commit_id, stored_id);
3865 free(stored_id);
3866 if (cmp != 0) {
3867 err = got_error(GOT_ERR_REBASE_COMMITID);
3868 goto done;
3872 cmp_arg.progress_cb = progress_cb;
3873 cmp_arg.progress_arg = progress_arg;
3874 cmp_arg.merged_paths = merged_paths;
3875 err = merge_files(worktree, fileindex, fileindex_path,
3876 parent_commit_id, commit_id, repo, collect_merged_paths,
3877 &cmp_arg, cancel_cb, cancel_arg);
3878 done:
3879 got_fileindex_free(fileindex);
3880 free(fileindex_path);
3881 if (commit_ref)
3882 got_ref_close(commit_ref);
3883 return err;
3886 const struct got_error *
3887 got_worktree_rebase_commit(struct got_object_id **new_commit_id,
3888 struct got_pathlist_head *merged_paths, struct got_worktree *worktree,
3889 struct got_reference *tmp_branch, struct got_commit_object *orig_commit,
3890 struct got_object_id *orig_commit_id, struct got_repository *repo)
3892 const struct got_error *err, *sync_err;
3893 struct got_pathlist_head commitable_paths;
3894 struct collect_commitables_arg cc_arg;
3895 struct got_fileindex *fileindex = NULL;
3896 char *fileindex_path = NULL, *commit_ref_name = NULL;
3897 struct got_reference *head_ref = NULL;
3898 struct got_object_id *head_commit_id = NULL;
3899 struct got_reference *commit_ref = NULL;
3900 struct got_object_id *commit_id = NULL;
3902 TAILQ_INIT(&commitable_paths);
3903 *new_commit_id = NULL;
3905 /* Work tree is locked/unlocked during rebase preparation/teardown. */
3907 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
3908 if (err)
3909 return err;
3910 err = got_ref_open(&commit_ref, repo, commit_ref_name, 0);
3911 if (err)
3912 goto done;
3913 err = got_ref_resolve(&commit_id, repo, commit_ref);
3914 if (err)
3915 goto done;
3916 if (got_object_id_cmp(commit_id, orig_commit_id) != 0) {
3917 err = got_error(GOT_ERR_REBASE_COMMITID);
3918 goto done;
3921 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3922 if (err)
3923 goto done;
3925 cc_arg.commitable_paths = &commitable_paths;
3926 cc_arg.worktree = worktree;
3927 cc_arg.repo = repo;
3929 * If possible get the status of individual files directly to
3930 * avoid crawling the entire work tree once per rebased commit.
3931 * TODO: Ideally, merged_paths would contain a list of commitables
3932 * we could use so we could skip worktree_status() entirely.
3934 if (merged_paths) {
3935 struct got_pathlist_entry *pe;
3936 TAILQ_FOREACH(pe, merged_paths, entry) {
3937 err = worktree_status(worktree, pe->path, fileindex,
3938 repo, collect_commitables, &cc_arg, NULL, NULL);
3939 if (err)
3940 goto done;
3942 } else {
3943 err = worktree_status(worktree, "", fileindex, repo,
3944 collect_commitables, &cc_arg, NULL, NULL);
3945 if (err)
3946 goto done;
3949 if (TAILQ_EMPTY(&commitable_paths)) {
3950 /* No-op change; commit will be elided. */
3951 err = got_ref_delete(commit_ref, repo);
3952 if (err)
3953 goto done;
3954 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3955 goto done;
3958 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3959 if (err)
3960 goto done;
3962 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3963 if (err)
3964 goto done;
3966 err = commit_worktree(new_commit_id, &commitable_paths, head_commit_id,
3967 worktree, NULL, got_object_commit_get_author(orig_commit),
3968 got_object_commit_get_committer(orig_commit),
3969 collect_rebase_commit_msg, orig_commit,
3970 rebase_status, NULL, repo);
3971 if (err)
3972 goto done;
3974 err = got_ref_change_ref(tmp_branch, *new_commit_id);
3975 if (err)
3976 goto done;
3978 err = got_ref_delete(commit_ref, repo);
3979 if (err)
3980 goto done;
3982 err = update_fileindex_after_commit(&commitable_paths, *new_commit_id,
3983 fileindex);
3984 sync_err = sync_fileindex(fileindex, fileindex_path);
3985 if (sync_err && err == NULL)
3986 err = sync_err;
3987 done:
3988 if (fileindex)
3989 got_fileindex_free(fileindex);
3990 free(fileindex_path);
3991 free(commit_ref_name);
3992 if (commit_ref)
3993 got_ref_close(commit_ref);
3994 free(head_commit_id);
3995 if (head_ref)
3996 got_ref_close(head_ref);
3997 if (err) {
3998 free(*new_commit_id);
3999 *new_commit_id = NULL;
4001 return err;
4004 const struct got_error *
4005 got_worktree_rebase_postpone(struct got_worktree *worktree)
4007 return lock_worktree(worktree, LOCK_SH);
4010 static const struct got_error *
4011 delete_ref(const char *name, struct got_repository *repo)
4013 const struct got_error *err;
4014 struct got_reference *ref;
4016 err = got_ref_open(&ref, repo, name, 0);
4017 if (err) {
4018 if (err->code == GOT_ERR_NOT_REF)
4019 return NULL;
4020 return err;
4023 err = got_ref_delete(ref, repo);
4024 got_ref_close(ref);
4025 return err;
4028 static const struct got_error *
4029 delete_rebase_refs(struct got_worktree *worktree, struct got_repository *repo)
4031 const struct got_error *err;
4032 char *tmp_branch_name = NULL, *new_base_branch_ref_name = NULL;
4033 char *branch_ref_name = NULL, *commit_ref_name = NULL;
4035 err = get_rebase_tmp_ref_name(&tmp_branch_name, worktree);
4036 if (err)
4037 goto done;
4038 err = delete_ref(tmp_branch_name, repo);
4039 if (err)
4040 goto done;
4042 err = get_newbase_symref_name(&new_base_branch_ref_name, worktree);
4043 if (err)
4044 goto done;
4045 err = delete_ref(new_base_branch_ref_name, repo);
4046 if (err)
4047 goto done;
4049 err = get_rebase_branch_symref_name(&branch_ref_name, worktree);
4050 if (err)
4051 goto done;
4052 err = delete_ref(branch_ref_name, repo);
4053 if (err)
4054 goto done;
4056 err = get_rebase_commit_ref_name(&commit_ref_name, worktree);
4057 if (err)
4058 goto done;
4059 err = delete_ref(commit_ref_name, repo);
4060 if (err)
4061 goto done;
4063 done:
4064 free(tmp_branch_name);
4065 free(new_base_branch_ref_name);
4066 free(branch_ref_name);
4067 free(commit_ref_name);
4068 return err;
4071 const struct got_error *
4072 got_worktree_rebase_complete(struct got_worktree *worktree,
4073 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
4074 struct got_reference *rebased_branch,
4075 struct got_repository *repo)
4077 const struct got_error *err, *unlockerr;
4078 struct got_object_id *new_head_commit_id = NULL;
4080 err = got_ref_resolve(&new_head_commit_id, repo, tmp_branch);
4081 if (err)
4082 return err;
4084 err = got_ref_change_ref(rebased_branch, new_head_commit_id);
4085 if (err)
4086 goto done;
4088 err = got_ref_write(rebased_branch, repo);
4089 if (err)
4090 goto done;
4092 err = got_worktree_set_head_ref(worktree, rebased_branch);
4093 if (err)
4094 goto done;
4096 err = delete_rebase_refs(worktree, repo);
4097 done:
4098 free(new_head_commit_id);
4099 unlockerr = lock_worktree(worktree, LOCK_SH);
4100 if (unlockerr && err == NULL)
4101 err = unlockerr;
4102 return err;
4105 struct collect_revertible_paths_arg {
4106 struct got_pathlist_head *revertible_paths;
4107 struct got_worktree *worktree;
4110 static const struct got_error *
4111 collect_revertible_paths(void *arg, unsigned char status, const char *relpath,
4112 struct got_object_id *blob_id, struct got_object_id *commit_id)
4114 struct collect_revertible_paths_arg *a = arg;
4115 const struct got_error *err = NULL;
4116 struct got_pathlist_entry *new = NULL;
4117 char *path = NULL;
4119 if (status != GOT_STATUS_ADD &&
4120 status != GOT_STATUS_DELETE &&
4121 status != GOT_STATUS_MODIFY &&
4122 status != GOT_STATUS_CONFLICT &&
4123 status != GOT_STATUS_MISSING)
4124 return NULL;
4126 if (asprintf(&path, "%s/%s", a->worktree->root_path, relpath) == -1)
4127 return got_error_from_errno("asprintf");
4129 err = got_pathlist_insert(&new, a->revertible_paths, path, NULL);
4130 if (err || new == NULL)
4131 free(path);
4132 return err;
4135 const struct got_error *
4136 got_worktree_rebase_abort(struct got_worktree *worktree,
4137 struct got_repository *repo, struct got_reference *new_base_branch,
4138 got_worktree_checkout_cb progress_cb, void *progress_arg)
4140 const struct got_error *err, *unlockerr, *sync_err;
4141 struct got_reference *resolved = NULL;
4142 struct got_object_id *commit_id = NULL;
4143 struct got_fileindex *fileindex = NULL;
4144 char *fileindex_path = NULL;
4145 struct got_pathlist_head revertible_paths;
4146 struct got_pathlist_entry *pe;
4147 struct collect_revertible_paths_arg crp_arg;
4148 struct got_object_id *tree_id = NULL;
4150 TAILQ_INIT(&revertible_paths);
4152 err = lock_worktree(worktree, LOCK_EX);
4153 if (err)
4154 return err;
4156 err = got_ref_open(&resolved, repo,
4157 got_ref_get_symref_target(new_base_branch), 0);
4158 if (err)
4159 goto done;
4161 err = got_worktree_set_head_ref(worktree, resolved);
4162 if (err)
4163 goto done;
4166 * XXX commits to the base branch could have happened while
4167 * we were busy rebasing; should we store the original commit ID
4168 * when rebase begins and read it back here?
4170 err = got_ref_resolve(&commit_id, repo, resolved);
4171 if (err)
4172 goto done;
4174 err = got_worktree_set_base_commit_id(worktree, repo, commit_id);
4175 if (err)
4176 goto done;
4178 err = got_object_id_by_path(&tree_id, repo,
4179 worktree->base_commit_id, worktree->path_prefix);
4180 if (err)
4181 goto done;
4183 err = delete_rebase_refs(worktree, repo);
4184 if (err)
4185 goto done;
4187 err = open_fileindex(&fileindex, &fileindex_path, worktree);
4188 if (err)
4189 goto done;
4191 crp_arg.revertible_paths = &revertible_paths;
4192 crp_arg.worktree = worktree;
4193 err = worktree_status(worktree, "", fileindex, repo,
4194 collect_revertible_paths, &crp_arg, NULL, NULL);
4195 if (err)
4196 goto done;
4198 TAILQ_FOREACH(pe, &revertible_paths, entry) {
4199 err = revert_file(worktree, fileindex, pe->path,
4200 progress_cb, progress_arg, repo);
4201 if (err)
4202 goto sync;
4205 err = checkout_files(worktree, fileindex, "", tree_id, NULL,
4206 repo, progress_cb, progress_arg, NULL, NULL);
4207 sync:
4208 sync_err = sync_fileindex(fileindex, fileindex_path);
4209 if (sync_err && err == NULL)
4210 err = sync_err;
4211 done:
4212 got_ref_close(resolved);
4213 free(tree_id);
4214 free(commit_id);
4215 if (fileindex)
4216 got_fileindex_free(fileindex);
4217 free(fileindex_path);
4218 TAILQ_FOREACH(pe, &revertible_paths, entry)
4219 free((char *)pe->path);
4220 got_pathlist_free(&revertible_paths);
4222 unlockerr = lock_worktree(worktree, LOCK_SH);
4223 if (unlockerr && err == NULL)
4224 err = unlockerr;
4225 return err;