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_repository *repo, got_worktree_checkout_cb progress_cb,
729 void *progress_arg)
731 const struct got_error *err = NULL;
732 int merged_fd = -1;
733 FILE *f_deriv = NULL, *f_orig = NULL;
734 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
735 char *merged_path = NULL, *base_path = NULL;
736 char *id_str = NULL;
737 char *label1 = NULL;
738 int overlapcnt = 0;
739 char *parent;
741 *local_changes_subsumed = 0;
743 parent = dirname(ondisk_path);
744 if (parent == NULL)
745 return got_error_from_errno2("dirname", ondisk_path);
747 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
748 return got_error_from_errno("asprintf");
750 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
751 if (err)
752 goto done;
754 free(base_path);
755 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
756 err = got_error_from_errno("asprintf");
757 base_path = NULL;
758 goto done;
761 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
762 if (err)
763 goto done;
764 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_deriv,
765 blob_deriv);
766 if (err)
767 goto done;
769 free(base_path);
770 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
771 err = got_error_from_errno("asprintf");
772 base_path = NULL;
773 goto done;
776 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
777 if (err)
778 goto done;
779 if (blob_orig) {
780 err = got_object_blob_dump_to_file(NULL, NULL, NULL, f_orig,
781 blob_orig);
782 if (err)
783 goto done;
784 } else {
785 /*
786 * If the file has no blob, this is an "add vs add" conflict,
787 * and we simply use an empty ancestor file to make both files
788 * appear in the merged result in their entirety.
789 */
792 err = got_object_id_str(&id_str, worktree->base_commit_id);
793 if (err)
794 goto done;
795 if (asprintf(&label1, "commit %s", id_str) == -1) {
796 err = got_error_from_errno("asprintf");
797 goto done;
800 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
801 blob_orig_path, ondisk_path, label1, path);
802 if (err)
803 goto done;
805 (*progress_cb)(progress_arg,
806 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
808 if (fsync(merged_fd) != 0) {
809 err = got_error_from_errno("fsync");
810 goto done;
813 /* Check if a clean merge has subsumed all local changes. */
814 if (overlapcnt == 0) {
815 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
816 merged_path);
817 if (err)
818 goto done;
821 if (chmod(merged_path, st_mode) != 0) {
822 err = got_error_from_errno2("chmod", merged_path);
823 goto done;
826 if (rename(merged_path, ondisk_path) != 0) {
827 err = got_error_from_errno3("rename", merged_path,
828 ondisk_path);
829 unlink(merged_path);
830 goto done;
833 done:
834 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
835 err = got_error_from_errno("close");
836 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
837 err = got_error_from_errno("fclose");
838 if (f_orig && fclose(f_orig) != 0 && err == NULL)
839 err = got_error_from_errno("fclose");
840 free(merged_path);
841 free(base_path);
842 if (blob_deriv_path) {
843 unlink(blob_deriv_path);
844 free(blob_deriv_path);
846 if (blob_orig_path) {
847 unlink(blob_orig_path);
848 free(blob_orig_path);
850 free(id_str);
851 free(label1);
852 return err;
855 static const struct got_error *
856 update_blob_fileindex_entry(struct got_worktree *worktree,
857 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
858 const char *ondisk_path, const char *path, struct got_blob_object *blob,
859 int update_timestamps)
861 const struct got_error *err = NULL;
863 if (ie == NULL)
864 ie = got_fileindex_entry_get(fileindex, path);
865 if (ie)
866 err = got_fileindex_entry_update(ie, ondisk_path,
867 blob->id.sha1, worktree->base_commit_id->sha1,
868 update_timestamps);
869 else {
870 struct got_fileindex_entry *new_ie;
871 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
872 path, blob->id.sha1, worktree->base_commit_id->sha1);
873 if (!err)
874 err = got_fileindex_entry_add(fileindex, new_ie);
876 return err;
879 static const struct got_error *
880 install_blob(struct got_worktree *worktree, const char *ondisk_path,
881 const char *path, uint16_t te_mode, uint16_t st_mode,
882 struct got_blob_object *blob, int restoring_missing_file,
883 int reverting_versioned_file, struct got_repository *repo,
884 got_worktree_checkout_cb progress_cb, void *progress_arg)
886 const struct got_error *err = NULL;
887 int fd = -1;
888 size_t len, hdrlen;
889 int update = 0;
890 char *tmppath = NULL;
892 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
893 GOT_DEFAULT_FILE_MODE);
894 if (fd == -1) {
895 if (errno == ENOENT) {
896 char *parent = dirname(path);
897 if (parent == NULL)
898 return got_error_from_errno2("dirname", path);
899 err = add_dir_on_disk(worktree, parent);
900 if (err)
901 return err;
902 fd = open(ondisk_path,
903 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
904 GOT_DEFAULT_FILE_MODE);
905 if (fd == -1)
906 return got_error_from_errno2("open",
907 ondisk_path);
908 } else if (errno == EEXIST) {
909 if (!S_ISREG(st_mode)) {
910 /* TODO file is obstructed; do something */
911 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
912 goto done;
913 } else {
914 err = got_opentemp_named_fd(&tmppath, &fd,
915 ondisk_path);
916 if (err)
917 goto done;
918 update = 1;
920 } else
921 return got_error_from_errno2("open", ondisk_path);
924 if (restoring_missing_file)
925 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
926 else if (reverting_versioned_file)
927 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
928 else
929 (*progress_cb)(progress_arg,
930 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
932 hdrlen = got_object_blob_get_hdrlen(blob);
933 do {
934 const uint8_t *buf = got_object_blob_get_read_buf(blob);
935 err = got_object_blob_read_block(&len, blob);
936 if (err)
937 break;
938 if (len > 0) {
939 /* Skip blob object header first time around. */
940 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
941 if (outlen == -1) {
942 err = got_error_from_errno("write");
943 goto done;
944 } else if (outlen != len - hdrlen) {
945 err = got_error(GOT_ERR_IO);
946 goto done;
948 hdrlen = 0;
950 } while (len != 0);
952 if (fsync(fd) != 0) {
953 err = got_error_from_errno("fsync");
954 goto done;
957 if (update) {
958 if (rename(tmppath, ondisk_path) != 0) {
959 err = got_error_from_errno3("rename", tmppath,
960 ondisk_path);
961 unlink(tmppath);
962 goto done;
966 if (te_mode & S_IXUSR) {
967 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
968 err = got_error_from_errno2("chmod", ondisk_path);
969 goto done;
971 } else {
972 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
973 err = got_error_from_errno2("chmod", ondisk_path);
974 goto done;
978 done:
979 if (fd != -1 && close(fd) != 0 && err == NULL)
980 err = got_error_from_errno("close");
981 free(tmppath);
982 return err;
985 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
986 static const struct got_error *
987 get_modified_file_content_status(unsigned char *status, FILE *f)
989 const struct got_error *err = NULL;
990 const char *markers[3] = {
991 GOT_DIFF_CONFLICT_MARKER_BEGIN,
992 GOT_DIFF_CONFLICT_MARKER_SEP,
993 GOT_DIFF_CONFLICT_MARKER_END
994 };
995 int i = 0;
996 char *line;
997 size_t len;
998 const char delim[3] = {'\0', '\0', '\0'};
1000 while (*status == GOT_STATUS_MODIFY) {
1001 line = fparseln(f, &len, NULL, delim, 0);
1002 if (line == NULL) {
1003 if (feof(f))
1004 break;
1005 err = got_ferror(f, GOT_ERR_IO);
1006 break;
1009 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1010 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1011 == 0)
1012 *status = GOT_STATUS_CONFLICT;
1013 else
1014 i++;
1018 return err;
1021 static const struct got_error *
1022 get_file_status(unsigned char *status, struct stat *sb,
1023 struct got_fileindex_entry *ie, const char *abspath,
1024 struct got_repository *repo)
1026 const struct got_error *err = NULL;
1027 struct got_object_id id;
1028 size_t hdrlen;
1029 FILE *f = NULL;
1030 uint8_t fbuf[8192];
1031 struct got_blob_object *blob = NULL;
1032 size_t flen, blen;
1034 *status = GOT_STATUS_NO_CHANGE;
1036 if (lstat(abspath, sb) == -1) {
1037 if (errno == ENOENT) {
1038 if (ie) {
1039 if (got_fileindex_entry_has_file_on_disk(ie))
1040 *status = GOT_STATUS_MISSING;
1041 else
1042 *status = GOT_STATUS_DELETE;
1043 sb->st_mode =
1044 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1045 & (S_IRWXU | S_IRWXG | S_IRWXO));
1046 } else
1047 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1048 return NULL;
1050 return got_error_from_errno2("lstat", abspath);
1053 if (!S_ISREG(sb->st_mode)) {
1054 *status = GOT_STATUS_OBSTRUCTED;
1055 return NULL;
1058 if (ie == NULL)
1059 return NULL;
1061 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1062 *status = GOT_STATUS_DELETE;
1063 return NULL;
1064 } else if (!got_fileindex_entry_has_blob(ie)) {
1065 *status = GOT_STATUS_ADD;
1066 return NULL;
1069 if (ie->ctime_sec == sb->st_ctime &&
1070 ie->ctime_nsec == sb->st_ctimensec &&
1071 ie->mtime_sec == sb->st_mtime &&
1072 ie->mtime_sec == sb->st_mtime &&
1073 ie->mtime_nsec == sb->st_mtimensec &&
1074 ie->size == (sb->st_size & 0xffffffff))
1075 return NULL;
1077 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1078 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1079 if (err)
1080 return err;
1082 f = fopen(abspath, "r");
1083 if (f == NULL) {
1084 err = got_error_from_errno2("fopen", abspath);
1085 goto done;
1087 hdrlen = got_object_blob_get_hdrlen(blob);
1088 for (;;) {
1089 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1090 err = got_object_blob_read_block(&blen, blob);
1091 if (err)
1092 goto done;
1093 /* Skip length of blob object header first time around. */
1094 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1095 if (flen == 0 && ferror(f)) {
1096 err = got_error_from_errno("fread");
1097 goto done;
1099 if (blen == 0) {
1100 if (flen != 0)
1101 *status = GOT_STATUS_MODIFY;
1102 break;
1103 } else if (flen == 0) {
1104 if (blen != 0)
1105 *status = GOT_STATUS_MODIFY;
1106 break;
1107 } else if (blen - hdrlen == flen) {
1108 /* Skip blob object header first time around. */
1109 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1110 *status = GOT_STATUS_MODIFY;
1111 break;
1113 } else {
1114 *status = GOT_STATUS_MODIFY;
1115 break;
1117 hdrlen = 0;
1120 if (*status == GOT_STATUS_MODIFY) {
1121 rewind(f);
1122 err = get_modified_file_content_status(status, f);
1124 done:
1125 if (blob)
1126 got_object_blob_close(blob);
1127 if (f)
1128 fclose(f);
1129 return err;
1132 static const struct got_error *
1133 update_blob(struct got_worktree *worktree,
1134 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1135 struct got_tree_entry *te, const char *path,
1136 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1137 void *progress_arg)
1139 const struct got_error *err = NULL;
1140 struct got_blob_object *blob = NULL;
1141 char *ondisk_path;
1142 unsigned char status = GOT_STATUS_NO_CHANGE;
1143 struct stat sb;
1145 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1146 return got_error_from_errno("asprintf");
1148 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1149 if (err)
1150 goto done;
1152 if (status == GOT_STATUS_OBSTRUCTED) {
1153 (*progress_cb)(progress_arg, status, path);
1154 goto done;
1157 if (ie && status != GOT_STATUS_MISSING) {
1158 if (got_fileindex_entry_has_commit(ie) &&
1159 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1160 SHA1_DIGEST_LENGTH) == 0) {
1161 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1162 path);
1163 goto done;
1165 if (got_fileindex_entry_has_blob(ie) &&
1166 memcmp(ie->blob_sha1, te->id->sha1,
1167 SHA1_DIGEST_LENGTH) == 0)
1168 goto done;
1171 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1172 if (err)
1173 goto done;
1175 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1176 int update_timestamps;
1177 struct got_blob_object *blob2 = NULL;
1178 if (got_fileindex_entry_has_blob(ie)) {
1179 struct got_object_id id2;
1180 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1181 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1182 if (err)
1183 goto done;
1185 err = merge_blob(&update_timestamps, worktree, blob2,
1186 ondisk_path, path, sb.st_mode, blob, repo,
1187 progress_cb, progress_arg);
1188 if (blob2)
1189 got_object_blob_close(blob2);
1191 * Do not update timestamps of files with local changes.
1192 * Otherwise, a future status walk would treat them as
1193 * unmodified files again.
1195 err = got_fileindex_entry_update(ie, ondisk_path,
1196 blob->id.sha1, worktree->base_commit_id->sha1,
1197 update_timestamps);
1198 } else if (status == GOT_STATUS_DELETE) {
1199 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1200 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1201 ondisk_path, path, blob, 0);
1202 if (err)
1203 goto done;
1204 } else {
1205 err = install_blob(worktree, ondisk_path, path, te->mode,
1206 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1207 repo, progress_cb, progress_arg);
1208 if (err)
1209 goto done;
1210 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1211 ondisk_path, path, blob, 1);
1212 if (err)
1213 goto done;
1215 got_object_blob_close(blob);
1216 done:
1217 free(ondisk_path);
1218 return err;
1221 static const struct got_error *
1222 remove_ondisk_file(const char *root_path, const char *path)
1224 const struct got_error *err = NULL;
1225 char *ondisk_path = NULL;
1227 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1228 return got_error_from_errno("asprintf");
1230 if (unlink(ondisk_path) == -1) {
1231 if (errno != ENOENT)
1232 err = got_error_from_errno2("unlink", ondisk_path);
1233 } else {
1234 char *parent = dirname(ondisk_path);
1235 while (parent && strcmp(parent, root_path) != 0) {
1236 if (rmdir(parent) == -1) {
1237 if (errno != ENOTEMPTY)
1238 err = got_error_from_errno2("rmdir",
1239 parent);
1240 break;
1242 parent = dirname(parent);
1245 free(ondisk_path);
1246 return err;
1249 static const struct got_error *
1250 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1251 struct got_fileindex_entry *ie, struct got_repository *repo,
1252 got_worktree_checkout_cb progress_cb, void *progress_arg)
1254 const struct got_error *err = NULL;
1255 unsigned char status;
1256 struct stat sb;
1257 char *ondisk_path;
1259 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1260 == -1)
1261 return got_error_from_errno("asprintf");
1263 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1264 if (err)
1265 return err;
1267 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1268 status == GOT_STATUS_ADD) {
1269 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1271 * Preserve the working file and change the deleted blob's
1272 * entry into a schedule-add entry.
1274 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1275 0);
1276 if (err)
1277 return err;
1278 } else {
1279 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1280 if (status == GOT_STATUS_NO_CHANGE) {
1281 err = remove_ondisk_file(worktree->root_path, ie->path);
1282 if (err)
1283 return err;
1285 got_fileindex_entry_remove(fileindex, ie);
1288 return err;
1291 struct diff_cb_arg {
1292 struct got_fileindex *fileindex;
1293 struct got_worktree *worktree;
1294 struct got_repository *repo;
1295 got_worktree_checkout_cb progress_cb;
1296 void *progress_arg;
1297 got_worktree_cancel_cb cancel_cb;
1298 void *cancel_arg;
1301 static const struct got_error *
1302 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1303 struct got_tree_entry *te, const char *parent_path)
1305 struct diff_cb_arg *a = arg;
1307 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1308 return got_error(GOT_ERR_CANCELLED);
1310 return update_blob(a->worktree, a->fileindex, ie, te,
1311 ie->path, a->repo, a->progress_cb, a->progress_arg);
1314 static const struct got_error *
1315 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1317 struct diff_cb_arg *a = arg;
1319 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1320 return got_error(GOT_ERR_CANCELLED);
1322 return delete_blob(a->worktree, a->fileindex, ie,
1323 a->repo, a->progress_cb, a->progress_arg);
1326 static const struct got_error *
1327 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1329 struct diff_cb_arg *a = arg;
1330 const struct got_error *err;
1331 char *path;
1333 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1334 return got_error(GOT_ERR_CANCELLED);
1336 if (asprintf(&path, "%s%s%s", parent_path,
1337 parent_path[0] ? "/" : "", te->name)
1338 == -1)
1339 return got_error_from_errno("asprintf");
1341 if (S_ISDIR(te->mode))
1342 err = add_dir_on_disk(a->worktree, path);
1343 else
1344 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1345 a->repo, a->progress_cb, a->progress_arg);
1347 free(path);
1348 return err;
1351 const struct got_error *
1352 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1354 const struct got_error *err = NULL;
1355 char *uuidstr = NULL;
1356 uint32_t uuid_status;
1358 *refname = NULL;
1360 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1361 if (uuid_status != uuid_s_ok)
1362 return got_error_uuid(uuid_status);
1364 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1365 == -1) {
1366 err = got_error_from_errno("asprintf");
1367 *refname = NULL;
1369 free(uuidstr);
1370 return err;
1374 * Prevent Git's garbage collector from deleting our base commit by
1375 * setting a reference to our base commit's ID.
1377 static const struct got_error *
1378 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1380 const struct got_error *err = NULL;
1381 struct got_reference *ref = NULL;
1382 char *refname;
1384 err = got_worktree_get_base_ref_name(&refname, worktree);
1385 if (err)
1386 return err;
1388 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1389 if (err)
1390 goto done;
1392 err = got_ref_write(ref, repo);
1393 done:
1394 free(refname);
1395 if (ref)
1396 got_ref_close(ref);
1397 return err;
1400 static const struct got_error *
1401 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1402 struct got_worktree *worktree)
1404 const struct got_error *err = NULL;
1405 FILE *index = NULL;
1407 *fileindex_path = NULL;
1408 *fileindex = got_fileindex_alloc();
1409 if (*fileindex == NULL)
1410 return got_error_from_errno("got_fileindex_alloc");
1412 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1413 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1414 err = got_error_from_errno("asprintf");
1415 *fileindex_path = NULL;
1416 goto done;
1419 index = fopen(*fileindex_path, "rb");
1420 if (index == NULL) {
1421 if (errno != ENOENT)
1422 err = got_error_from_errno2("fopen", *fileindex_path);
1423 } else {
1424 err = got_fileindex_read(*fileindex, index);
1425 if (fclose(index) != 0 && err == NULL)
1426 err = got_error_from_errno("fclose");
1428 done:
1429 if (err) {
1430 free(*fileindex_path);
1431 *fileindex_path = NULL;
1432 free(*fileindex);
1433 *fileindex = NULL;
1435 return err;
1438 struct bump_base_commit_id_arg {
1439 struct got_object_id *base_commit_id;
1440 const char *path;
1441 size_t path_len;
1442 const char *entry_name;
1443 got_worktree_checkout_cb progress_cb;
1444 void *progress_arg;
1447 /* Bump base commit ID of all files within an updated part of the work tree. */
1448 static const struct got_error *
1449 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1451 struct bump_base_commit_id_arg *a = arg;
1453 if (a->entry_name) {
1454 if (strcmp(ie->path, a->path) != 0)
1455 return NULL;
1456 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1457 return NULL;
1459 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1460 SHA1_DIGEST_LENGTH) == 0)
1461 return NULL;
1463 (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1464 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1465 return NULL;
1468 static const struct got_error *
1469 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1471 const struct got_error *err = NULL;
1472 char *new_fileindex_path = NULL;
1473 FILE *new_index = NULL;
1475 err = got_opentemp_named(&new_fileindex_path, &new_index,
1476 fileindex_path);
1477 if (err)
1478 goto done;
1480 err = got_fileindex_write(fileindex, new_index);
1481 if (err)
1482 goto done;
1484 if (rename(new_fileindex_path, fileindex_path) != 0) {
1485 err = got_error_from_errno3("rename", new_fileindex_path,
1486 fileindex_path);
1487 unlink(new_fileindex_path);
1489 done:
1490 if (new_index)
1491 fclose(new_index);
1492 free(new_fileindex_path);
1493 return err;
1496 const struct got_error *
1497 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1498 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1499 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1501 const struct got_error *err = NULL, *sync_err, *unlockerr;
1502 struct got_commit_object *commit = NULL;
1503 struct got_object_id *tree_id = NULL;
1504 struct got_tree_object *tree = NULL;
1505 struct got_fileindex *fileindex = NULL;
1506 char *fileindex_path = NULL;
1507 struct got_fileindex_diff_tree_cb diff_cb;
1508 struct diff_cb_arg arg;
1509 char *relpath = NULL, *entry_name = NULL;
1510 struct bump_base_commit_id_arg bbc_arg;
1512 err = lock_worktree(worktree, LOCK_EX);
1513 if (err)
1514 return err;
1517 * Read the file index.
1518 * Checking out files is supposed to be an idempotent operation.
1519 * If the on-disk file index is incomplete we will try to complete it.
1521 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1522 if (err)
1523 goto done;
1525 err = ref_base_commit(worktree, repo);
1526 if (err)
1527 goto done;
1529 err = got_object_open_as_commit(&commit, repo,
1530 worktree->base_commit_id);
1531 if (err)
1532 goto done;
1534 if (path[0]) {
1535 char *tree_path;
1536 int obj_type;
1537 relpath = strdup(path);
1538 if (relpath == NULL) {
1539 err = got_error_from_errno("strdup");
1540 goto done;
1542 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1543 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1544 path) == -1) {
1545 err = got_error_from_errno("asprintf");
1546 goto done;
1548 err = got_object_id_by_path(&tree_id, repo,
1549 worktree->base_commit_id, tree_path);
1550 free(tree_path);
1551 if (err)
1552 goto done;
1553 err = got_object_get_type(&obj_type, repo, tree_id);
1554 if (err)
1555 goto done;
1556 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1557 /* Split provided path into parent dir + entry name. */
1558 if (strchr(path, '/') == NULL) {
1559 relpath = strdup("");
1560 if (relpath == NULL) {
1561 err = got_error_from_errno("strdup");
1562 goto done;
1564 tree_path = strdup(worktree->path_prefix);
1565 if (tree_path == NULL) {
1566 err = got_error_from_errno("strdup");
1567 goto done;
1569 } else {
1570 err = got_path_dirname(&relpath, path);
1571 if (err)
1572 goto done;
1573 if (asprintf(&tree_path, "%s%s%s",
1574 worktree->path_prefix,
1575 got_path_is_root_dir(
1576 worktree->path_prefix) ? "" : "/",
1577 relpath) == -1) {
1578 err = got_error_from_errno("asprintf");
1579 goto done;
1582 err = got_object_id_by_path(&tree_id, repo,
1583 worktree->base_commit_id, tree_path);
1584 free(tree_path);
1585 if (err)
1586 goto done;
1587 entry_name = basename(path);
1588 if (entry_name == NULL) {
1589 err = got_error_from_errno2("basename", path);
1590 goto done;
1593 } else {
1594 relpath = strdup("");
1595 if (relpath == NULL) {
1596 err = got_error_from_errno("strdup");
1597 goto done;
1599 err = got_object_id_by_path(&tree_id, repo,
1600 worktree->base_commit_id, worktree->path_prefix);
1601 if (err)
1602 goto done;
1605 err = got_object_open_as_tree(&tree, repo, tree_id);
1606 if (err)
1607 goto done;
1609 if (entry_name &&
1610 got_object_tree_find_entry(tree, entry_name) == NULL) {
1611 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1612 goto done;
1615 diff_cb.diff_old_new = diff_old_new;
1616 diff_cb.diff_old = diff_old;
1617 diff_cb.diff_new = diff_new;
1618 arg.fileindex = fileindex;
1619 arg.worktree = worktree;
1620 arg.repo = repo;
1621 arg.progress_cb = progress_cb;
1622 arg.progress_arg = progress_arg;
1623 arg.cancel_cb = cancel_cb;
1624 arg.cancel_arg = cancel_arg;
1625 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1626 entry_name, repo, &diff_cb, &arg);
1627 if (err)
1628 goto sync;
1630 bbc_arg.base_commit_id = worktree->base_commit_id;
1631 bbc_arg.entry_name = entry_name;
1632 bbc_arg.path = path;
1633 bbc_arg.path_len = strlen(path);
1634 bbc_arg.progress_cb = progress_cb;
1635 bbc_arg.progress_arg = progress_arg;
1636 err = got_fileindex_for_each_entry_safe(fileindex,
1637 bump_base_commit_id, &bbc_arg);
1638 sync:
1639 sync_err = sync_fileindex(fileindex, fileindex_path);
1640 if (sync_err && err == NULL)
1641 err = sync_err;
1642 done:
1643 free(fileindex_path);
1644 free(relpath);
1645 if (tree)
1646 got_object_tree_close(tree);
1647 if (commit)
1648 got_object_commit_close(commit);
1649 got_fileindex_free(fileindex);
1650 unlockerr = lock_worktree(worktree, LOCK_SH);
1651 if (unlockerr && err == NULL)
1652 err = unlockerr;
1653 return err;
1656 struct merge_file_cb_arg {
1657 struct got_worktree *worktree;
1658 struct got_fileindex *fileindex;
1659 got_worktree_checkout_cb progress_cb;
1660 void *progress_arg;
1661 got_worktree_cancel_cb cancel_cb;
1662 void *cancel_arg;
1665 static const struct got_error *
1666 merge_file_cb(void *arg, struct got_blob_object *blob1,
1667 struct got_blob_object *blob2, struct got_object_id *id1,
1668 struct got_object_id *id2, const char *path1, const char *path2,
1669 struct got_repository *repo)
1671 static const struct got_error *err = NULL;
1672 struct merge_file_cb_arg *a = arg;
1673 struct got_fileindex_entry *ie;
1674 char *ondisk_path = NULL;
1675 struct stat sb;
1676 unsigned char status;
1677 int local_changes_subsumed;
1679 if (blob1 && blob2) {
1680 ie = got_fileindex_entry_get(a->fileindex, path2);
1681 if (ie == NULL) {
1682 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1683 path2);
1684 return NULL;
1687 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1688 path2) == -1)
1689 return got_error_from_errno("asprintf");
1691 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1692 if (err)
1693 goto done;
1695 if (status == GOT_STATUS_DELETE) {
1696 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1697 path2);
1698 goto done;
1700 if (status != GOT_STATUS_NO_CHANGE &&
1701 status != GOT_STATUS_MODIFY &&
1702 status != GOT_STATUS_CONFLICT &&
1703 status != GOT_STATUS_ADD) {
1704 (*a->progress_cb)(a->progress_arg, status, path2);
1705 goto done;
1708 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1709 ondisk_path, path2, sb.st_mode, blob2, repo,
1710 a->progress_cb, a->progress_arg);
1711 } else if (blob1) {
1712 ie = got_fileindex_entry_get(a->fileindex, path1);
1713 if (ie == NULL) {
1714 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1715 path2);
1716 return NULL;
1719 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1720 path1) == -1)
1721 return got_error_from_errno("asprintf");
1723 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1724 if (err)
1725 goto done;
1727 switch (status) {
1728 case GOT_STATUS_NO_CHANGE:
1729 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1730 path1);
1731 err = remove_ondisk_file(a->worktree->root_path, path1);
1732 if (err)
1733 goto done;
1734 if (ie)
1735 got_fileindex_entry_mark_deleted_from_disk(ie);
1736 break;
1737 case GOT_STATUS_DELETE:
1738 case GOT_STATUS_MISSING:
1739 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1740 path1);
1741 if (ie)
1742 got_fileindex_entry_mark_deleted_from_disk(ie);
1743 break;
1744 case GOT_STATUS_ADD:
1745 case GOT_STATUS_MODIFY:
1746 case GOT_STATUS_CONFLICT:
1747 (*a->progress_cb)(a->progress_arg,
1748 GOT_STATUS_CANNOT_DELETE, path1);
1749 break;
1750 case GOT_STATUS_OBSTRUCTED:
1751 (*a->progress_cb)(a->progress_arg, status, path1);
1752 break;
1753 default:
1754 break;
1756 } else if (blob2) {
1757 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1758 path2) == -1)
1759 return got_error_from_errno("asprintf");
1760 ie = got_fileindex_entry_get(a->fileindex, path2);
1761 if (ie) {
1762 err = get_file_status(&status, &sb, ie, ondisk_path,
1763 repo);
1764 if (err)
1765 goto done;
1766 if (status != GOT_STATUS_NO_CHANGE &&
1767 status != GOT_STATUS_MODIFY &&
1768 status != GOT_STATUS_CONFLICT &&
1769 status != GOT_STATUS_ADD) {
1770 (*a->progress_cb)(a->progress_arg, status,
1771 path2);
1772 goto done;
1774 err = merge_blob(&local_changes_subsumed, a->worktree,
1775 NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1776 a->progress_cb, a->progress_arg);
1777 if (status == GOT_STATUS_DELETE) {
1778 err = update_blob_fileindex_entry(a->worktree,
1779 a->fileindex, ie, ondisk_path, ie->path,
1780 blob2, 0);
1781 if (err)
1782 goto done;
1784 } else {
1785 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1786 err = install_blob(a->worktree, ondisk_path, path2,
1787 /* XXX get this from parent tree! */
1788 GOT_DEFAULT_FILE_MODE,
1789 sb.st_mode, blob2, 0, 0, repo,
1790 a->progress_cb, a->progress_arg);
1791 if (err)
1792 goto done;
1793 err = got_fileindex_entry_alloc(&ie,
1794 ondisk_path, path2, NULL, NULL);
1795 if (err)
1796 goto done;
1797 err = got_fileindex_entry_add(a->fileindex, ie);
1798 if (err) {
1799 got_fileindex_entry_free(ie);
1800 goto done;
1804 done:
1805 free(ondisk_path);
1806 return err;
1809 struct check_merge_ok_arg {
1810 struct got_worktree *worktree;
1811 struct got_repository *repo;
1814 static const struct got_error *
1815 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1817 const struct got_error *err = NULL;
1818 struct check_merge_ok_arg *a = arg;
1819 unsigned char status;
1820 struct stat sb;
1821 char *ondisk_path;
1823 /* Reject merges into a work tree with mixed base commits. */
1824 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1825 SHA1_DIGEST_LENGTH))
1826 return got_error(GOT_ERR_MIXED_COMMITS);
1828 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1829 == -1)
1830 return got_error_from_errno("asprintf");
1832 /* Reject merges into a work tree with conflicted files. */
1833 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1834 if (err)
1835 return err;
1836 if (status == GOT_STATUS_CONFLICT)
1837 return got_error(GOT_ERR_CONFLICTS);
1839 return NULL;
1842 const struct got_error *
1843 got_worktree_merge_files(struct got_worktree *worktree,
1844 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1845 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1846 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1848 const struct got_error *err = NULL, *sync_err, *unlockerr;
1849 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1850 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1851 struct merge_file_cb_arg arg;
1852 char *fileindex_path = NULL;
1853 struct got_fileindex *fileindex = NULL;
1854 struct check_merge_ok_arg mok_arg;
1856 err = lock_worktree(worktree, LOCK_EX);
1857 if (err)
1858 return err;
1860 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1861 if (err)
1862 goto done;
1864 mok_arg.worktree = worktree;
1865 mok_arg.repo = repo;
1866 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1867 &mok_arg);
1868 if (err)
1869 goto done;
1871 if (commit_id1) {
1872 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1873 worktree->path_prefix);
1874 if (err)
1875 goto done;
1877 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1878 if (err)
1879 goto done;
1882 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1883 worktree->path_prefix);
1884 if (err)
1885 goto done;
1887 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1888 if (err)
1889 goto done;
1891 arg.worktree = worktree;
1892 arg.fileindex = fileindex;
1893 arg.progress_cb = progress_cb;
1894 arg.progress_arg = progress_arg;
1895 arg.cancel_cb = cancel_cb;
1896 arg.cancel_arg = cancel_arg;
1897 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1898 sync_err = sync_fileindex(fileindex, fileindex_path);
1899 if (sync_err && err == NULL)
1900 err = sync_err;
1901 done:
1902 got_fileindex_free(fileindex);
1903 if (tree1)
1904 got_object_tree_close(tree1);
1905 if (tree2)
1906 got_object_tree_close(tree2);
1908 unlockerr = lock_worktree(worktree, LOCK_SH);
1909 if (unlockerr && err == NULL)
1910 err = unlockerr;
1911 return err;
1914 struct diff_dir_cb_arg {
1915 struct got_fileindex *fileindex;
1916 struct got_worktree *worktree;
1917 const char *status_path;
1918 size_t status_path_len;
1919 struct got_repository *repo;
1920 got_worktree_status_cb status_cb;
1921 void *status_arg;
1922 got_worktree_cancel_cb cancel_cb;
1923 void *cancel_arg;
1926 static const struct got_error *
1927 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1928 got_worktree_status_cb status_cb, void *status_arg,
1929 struct got_repository *repo)
1931 const struct got_error *err = NULL;
1932 unsigned char status = GOT_STATUS_NO_CHANGE;
1933 struct stat sb;
1934 struct got_object_id blob_id, commit_id;
1936 err = get_file_status(&status, &sb, ie, abspath, repo);
1937 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1938 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1939 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1940 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1941 &commit_id);
1943 return err;
1946 static const struct got_error *
1947 status_old_new(void *arg, struct got_fileindex_entry *ie,
1948 struct dirent *de, const char *parent_path)
1950 const struct got_error *err = NULL;
1951 struct diff_dir_cb_arg *a = arg;
1952 char *abspath;
1954 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1955 return got_error(GOT_ERR_CANCELLED);
1957 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1958 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1959 return NULL;
1961 if (parent_path[0]) {
1962 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1963 parent_path, de->d_name) == -1)
1964 return got_error_from_errno("asprintf");
1965 } else {
1966 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1967 de->d_name) == -1)
1968 return got_error_from_errno("asprintf");
1971 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1972 a->repo);
1973 free(abspath);
1974 return err;
1977 static const struct got_error *
1978 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1980 struct diff_dir_cb_arg *a = arg;
1981 struct got_object_id blob_id, commit_id;
1982 unsigned char status;
1984 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1985 return got_error(GOT_ERR_CANCELLED);
1987 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1988 return NULL;
1990 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1991 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1992 if (got_fileindex_entry_has_file_on_disk(ie))
1993 status = GOT_STATUS_MISSING;
1994 else
1995 status = GOT_STATUS_DELETE;
1996 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1997 &commit_id);
2000 static const struct got_error *
2001 status_new(void *arg, struct dirent *de, const char *parent_path)
2003 const struct got_error *err = NULL;
2004 struct diff_dir_cb_arg *a = arg;
2005 char *path = NULL;
2007 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2008 return got_error(GOT_ERR_CANCELLED);
2010 if (de->d_type == DT_DIR)
2011 return NULL;
2013 /* XXX ignore symlinks for now */
2014 if (de->d_type == DT_LNK)
2015 return NULL;
2017 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2018 return NULL;
2020 if (parent_path[0]) {
2021 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2022 return got_error_from_errno("asprintf");
2023 } else {
2024 path = de->d_name;
2027 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2028 NULL, NULL);
2029 if (parent_path[0])
2030 free(path);
2031 return err;
2034 const struct got_error *
2035 got_worktree_status(struct got_worktree *worktree, const char *path,
2036 struct got_repository *repo, got_worktree_status_cb status_cb,
2037 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2039 const struct got_error *err = NULL;
2040 DIR *workdir = NULL;
2041 char *fileindex_path = NULL;
2042 struct got_fileindex *fileindex = NULL;
2043 FILE *index = NULL;
2044 struct got_fileindex_diff_dir_cb fdiff_cb;
2045 struct diff_dir_cb_arg arg;
2046 char *ondisk_path = NULL;
2048 fileindex = got_fileindex_alloc();
2049 if (fileindex == NULL) {
2050 err = got_error_from_errno("got_fileindex_alloc");
2051 goto done;
2054 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2055 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2056 err = got_error_from_errno("asprintf");
2057 fileindex_path = NULL;
2058 goto done;
2061 index = fopen(fileindex_path, "rb");
2062 if (index == NULL) {
2063 if (errno != ENOENT) {
2064 err = got_error_from_errno2("fopen", fileindex_path);
2065 goto done;
2067 } else {
2068 err = got_fileindex_read(fileindex, index);
2069 fclose(index);
2070 if (err)
2071 goto done;
2074 if (asprintf(&ondisk_path, "%s%s%s",
2075 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2076 err = got_error_from_errno("asprintf");
2077 goto done;
2079 workdir = opendir(ondisk_path);
2080 if (workdir == NULL) {
2081 if (errno == ENOTDIR || errno == ENOENT) {
2082 struct got_fileindex_entry *ie;
2083 ie = got_fileindex_entry_get(fileindex, path);
2084 if (ie == NULL) {
2085 err = got_error(GOT_ERR_BAD_PATH);
2086 goto done;
2088 err = report_file_status(ie, ondisk_path,
2089 status_cb, status_arg, repo);
2090 goto done;
2091 } else {
2092 err = got_error_from_errno2("opendir", ondisk_path);
2093 goto done;
2096 fdiff_cb.diff_old_new = status_old_new;
2097 fdiff_cb.diff_old = status_old;
2098 fdiff_cb.diff_new = status_new;
2099 arg.fileindex = fileindex;
2100 arg.worktree = worktree;
2101 arg.status_path = path;
2102 arg.status_path_len = strlen(path);
2103 arg.repo = repo;
2104 arg.status_cb = status_cb;
2105 arg.status_arg = status_arg;
2106 arg.cancel_cb = cancel_cb;
2107 arg.cancel_arg = cancel_arg;
2108 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2109 path, repo, &fdiff_cb, &arg);
2110 done:
2111 if (workdir)
2112 closedir(workdir);
2113 free(ondisk_path);
2114 free(fileindex_path);
2115 got_fileindex_free(fileindex);
2116 return err;
2119 const struct got_error *
2120 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2121 const char *arg)
2123 const struct got_error *err = NULL;
2124 char *resolved, *path = NULL;
2125 size_t len;
2127 *wt_path = NULL;
2129 resolved = realpath(arg, NULL);
2130 if (resolved == NULL)
2131 return got_error_from_errno2("realpath", arg);
2133 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2134 strlen(got_worktree_get_root_path(worktree)))) {
2135 err = got_error(GOT_ERR_BAD_PATH);
2136 goto done;
2139 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2140 err = got_path_skip_common_ancestor(&path,
2141 got_worktree_get_root_path(worktree), resolved);
2142 if (err)
2143 goto done;
2144 } else {
2145 path = strdup("");
2146 if (path == NULL) {
2147 err = got_error_from_errno("strdup");
2148 goto done;
2152 /* XXX status walk can't deal with trailing slash! */
2153 len = strlen(path);
2154 while (path[len - 1] == '/') {
2155 path[len - 1] = '\0';
2156 len--;
2158 done:
2159 free(resolved);
2160 if (err == NULL)
2161 *wt_path = path;
2162 else
2163 free(path);
2164 return err;
2167 static const struct got_error *
2168 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2169 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2170 struct got_repository *repo)
2172 const struct got_error *err = NULL;
2173 struct got_fileindex_entry *ie;
2175 /* Re-adding an existing entry is a no-op. */
2176 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2177 return NULL;
2179 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2180 if (err)
2181 return err;
2183 err = got_fileindex_entry_add(fileindex, ie);
2184 if (err) {
2185 got_fileindex_entry_free(ie);
2186 return err;
2189 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2192 const struct got_error *
2193 got_worktree_schedule_add(struct got_worktree *worktree,
2194 struct got_pathlist_head *ondisk_paths,
2195 got_worktree_status_cb status_cb, void *status_arg,
2196 struct got_repository *repo)
2198 struct got_fileindex *fileindex = NULL;
2199 char *fileindex_path = NULL;
2200 FILE *index = NULL;
2201 const struct got_error *err = NULL, *sync_err, *unlockerr;
2202 struct got_pathlist_entry *pe;
2204 err = lock_worktree(worktree, LOCK_EX);
2205 if (err)
2206 return err;
2209 fileindex = got_fileindex_alloc();
2210 if (fileindex == NULL) {
2211 err = got_error_from_errno("got_fileindex_alloc");
2212 goto done;
2215 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2216 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2217 err = got_error_from_errno("asprintf");
2218 fileindex_path = NULL;
2219 goto done;
2222 index = fopen(fileindex_path, "rb");
2223 if (index == NULL) {
2224 err = got_error_from_errno2("fopen", fileindex_path);
2225 goto done;
2228 err = got_fileindex_read(fileindex, index);
2229 if (err)
2230 goto done;
2232 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2233 char *relpath;
2234 err = got_path_skip_common_ancestor(&relpath,
2235 got_worktree_get_root_path(worktree), pe->path);
2236 if (err)
2237 break;
2238 err = schedule_addition(pe->path, fileindex, relpath,
2239 status_cb, status_arg, repo);
2240 free(relpath);
2241 if (err)
2242 break;
2244 sync_err = sync_fileindex(fileindex, fileindex_path);
2245 if (sync_err && err == NULL)
2246 err = sync_err;
2247 done:
2248 if (index) {
2249 if (fclose(index) != 0 && err == NULL)
2250 err = got_error_from_errno("fclose");
2252 if (fileindex)
2253 got_fileindex_free(fileindex);
2254 unlockerr = lock_worktree(worktree, LOCK_SH);
2255 if (unlockerr && err == NULL)
2256 err = unlockerr;
2257 return err;
2260 static const struct got_error *
2261 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2262 const char *relpath, int delete_local_mods,
2263 got_worktree_status_cb status_cb, void *status_arg,
2264 struct got_repository *repo)
2266 const struct got_error *err = NULL;
2267 struct got_fileindex_entry *ie = NULL;
2268 unsigned char status;
2269 struct stat sb;
2271 ie = got_fileindex_entry_get(fileindex, relpath);
2272 if (ie == NULL)
2273 return got_error(GOT_ERR_BAD_PATH);
2275 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2276 if (err)
2277 return err;
2279 if (status != GOT_STATUS_NO_CHANGE) {
2280 if (status == GOT_STATUS_DELETE)
2281 return got_error_set_errno(ENOENT, ondisk_path);
2282 if (status != GOT_STATUS_MODIFY)
2283 return got_error(GOT_ERR_FILE_STATUS);
2284 if (!delete_local_mods)
2285 return got_error(GOT_ERR_FILE_MODIFIED);
2288 if (unlink(ondisk_path) != 0)
2289 return got_error_from_errno2("unlink", ondisk_path);
2291 got_fileindex_entry_mark_deleted_from_disk(ie);
2292 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2295 const struct got_error *
2296 got_worktree_schedule_delete(struct got_worktree *worktree,
2297 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2298 got_worktree_status_cb status_cb, void *status_arg,
2299 struct got_repository *repo)
2301 struct got_fileindex *fileindex = NULL;
2302 char *fileindex_path = NULL;
2303 FILE *index = NULL;
2304 const struct got_error *err = NULL, *sync_err, *unlockerr;
2305 struct got_pathlist_entry *pe;
2307 err = lock_worktree(worktree, LOCK_EX);
2308 if (err)
2309 return err;
2311 fileindex = got_fileindex_alloc();
2312 if (fileindex == NULL) {
2313 err = got_error_from_errno("got_fileindex_alloc");
2314 goto done;
2317 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2318 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2319 err = got_error_from_errno("asprintf");
2320 fileindex_path = NULL;
2321 goto done;
2324 index = fopen(fileindex_path, "rb");
2325 if (index == NULL) {
2326 err = got_error_from_errno2("fopen", fileindex_path);
2327 goto done;
2330 err = got_fileindex_read(fileindex, index);
2331 if (err)
2332 goto done;
2334 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2335 char *relpath;
2336 err = got_path_skip_common_ancestor(&relpath,
2337 got_worktree_get_root_path(worktree), pe->path);
2338 if (err)
2339 break;
2340 err = schedule_for_deletion(pe->path, fileindex, relpath,
2341 delete_local_mods, status_cb, status_arg, repo);
2342 free(relpath);
2343 if (err)
2344 break;
2346 sync_err = sync_fileindex(fileindex, fileindex_path);
2347 if (sync_err && err == NULL)
2348 err = sync_err;
2349 done:
2350 if (index) {
2351 if (fclose(index) != 0 && err == NULL)
2352 err = got_error_from_errno("fclose");
2354 if (fileindex)
2355 got_fileindex_free(fileindex);
2356 unlockerr = lock_worktree(worktree, LOCK_SH);
2357 if (unlockerr && err == NULL)
2358 err = unlockerr;
2359 return err;
2362 static const struct got_error *
2363 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2364 const char *ondisk_path,
2365 got_worktree_checkout_cb progress_cb, void *progress_arg,
2366 struct got_repository *repo)
2368 const struct got_error *err = NULL;
2369 char *relpath = NULL, *parent_path = NULL;
2370 struct got_fileindex_entry *ie;
2371 struct got_tree_object *tree = NULL;
2372 struct got_object_id *tree_id = NULL;
2373 const struct got_tree_entry *te;
2374 char *tree_path = NULL, *te_name;
2375 struct got_blob_object *blob = NULL;
2376 unsigned char status;
2377 struct stat sb;
2379 err = got_path_skip_common_ancestor(&relpath,
2380 got_worktree_get_root_path(worktree), ondisk_path);
2381 if (err)
2382 goto done;
2384 ie = got_fileindex_entry_get(fileindex, relpath);
2385 if (ie == NULL) {
2386 err = got_error(GOT_ERR_BAD_PATH);
2387 goto done;
2390 /* Construct in-repository path of tree which contains this blob. */
2391 err = got_path_dirname(&parent_path, ie->path);
2392 if (err) {
2393 if (err->code != GOT_ERR_BAD_PATH)
2394 goto done;
2395 parent_path = strdup("/");
2396 if (parent_path == NULL) {
2397 err = got_error_from_errno("strdup");
2398 goto done;
2401 if (got_path_is_root_dir(worktree->path_prefix)) {
2402 tree_path = strdup(parent_path);
2403 if (tree_path == NULL) {
2404 err = got_error_from_errno("strdup");
2405 goto done;
2407 } else {
2408 if (got_path_is_root_dir(parent_path)) {
2409 tree_path = strdup(worktree->path_prefix);
2410 if (tree_path == NULL) {
2411 err = got_error_from_errno("strdup");
2412 goto done;
2414 } else {
2415 if (asprintf(&tree_path, "%s/%s",
2416 worktree->path_prefix, parent_path) == -1) {
2417 err = got_error_from_errno("asprintf");
2418 goto done;
2423 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2424 tree_path);
2425 if (err)
2426 goto done;
2428 err = got_object_open_as_tree(&tree, repo, tree_id);
2429 if (err)
2430 goto done;
2432 te_name = basename(ie->path);
2433 if (te_name == NULL) {
2434 err = got_error_from_errno2("basename", ie->path);
2435 goto done;
2438 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2439 if (err)
2440 goto done;
2442 te = got_object_tree_find_entry(tree, te_name);
2443 if (te == NULL && status != GOT_STATUS_ADD) {
2444 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2445 goto done;
2448 switch (status) {
2449 case GOT_STATUS_ADD:
2450 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2451 got_fileindex_entry_remove(fileindex, ie);
2452 break;
2453 case GOT_STATUS_DELETE:
2454 case GOT_STATUS_MODIFY:
2455 case GOT_STATUS_CONFLICT:
2456 case GOT_STATUS_MISSING: {
2457 struct got_object_id id;
2458 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2459 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2460 if (err)
2461 goto done;
2462 err = install_blob(worktree, ondisk_path, ie->path,
2463 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2464 progress_arg);
2465 if (err)
2466 goto done;
2467 if (status == GOT_STATUS_DELETE) {
2468 err = update_blob_fileindex_entry(worktree,
2469 fileindex, ie, ondisk_path, ie->path, blob, 1);
2470 if (err)
2471 goto done;
2473 break;
2475 default:
2476 goto done;
2478 done:
2479 free(relpath);
2480 free(parent_path);
2481 free(tree_path);
2482 if (blob)
2483 got_object_blob_close(blob);
2484 if (tree)
2485 got_object_tree_close(tree);
2486 free(tree_id);
2487 return err;
2490 const struct got_error *
2491 got_worktree_revert(struct got_worktree *worktree,
2492 struct got_pathlist_head *ondisk_paths,
2493 got_worktree_checkout_cb progress_cb, void *progress_arg,
2494 struct got_repository *repo)
2496 struct got_fileindex *fileindex = NULL;
2497 char *fileindex_path = NULL;
2498 FILE *index = NULL;
2499 const struct got_error *err = NULL, *unlockerr = NULL;
2500 const struct got_error *sync_err = NULL;
2501 struct got_pathlist_entry *pe;
2503 err = lock_worktree(worktree, LOCK_EX);
2504 if (err)
2505 return err;
2507 fileindex = got_fileindex_alloc();
2508 if (fileindex == NULL) {
2509 err = got_error_from_errno("got_fileindex_alloc");
2510 goto done;
2513 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2514 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2515 err = got_error_from_errno("asprintf");
2516 fileindex_path = NULL;
2517 goto done;
2520 index = fopen(fileindex_path, "rb");
2521 if (index == NULL) {
2522 err = got_error_from_errno2("fopen", fileindex_path);
2523 goto done;
2526 err = got_fileindex_read(fileindex, index);
2527 if (err)
2528 goto done;
2530 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2531 err = revert_file(worktree, fileindex, pe->path,
2532 progress_cb, progress_arg, repo);
2533 if (err)
2534 break;
2536 sync_err = sync_fileindex(fileindex, fileindex_path);
2537 if (sync_err && err == NULL)
2538 err = sync_err;
2539 done:
2540 if (index) {
2541 if (fclose(index) != 0 && err == NULL)
2542 err = got_error_from_errno("fclose");
2544 if (fileindex)
2545 got_fileindex_free(fileindex);
2546 unlockerr = lock_worktree(worktree, LOCK_SH);
2547 if (unlockerr && err == NULL)
2548 err = unlockerr;
2549 return err;
2552 static void
2553 free_commitable(struct got_commitable *ct)
2555 free(ct->path);
2556 free(ct->in_repo_path);
2557 free(ct->ondisk_path);
2558 free(ct->blob_id);
2559 free(ct->base_blob_id);
2560 free(ct->base_commit_id);
2561 free(ct);
2564 struct collect_commitables_arg {
2565 struct got_pathlist_head *commitable_paths;
2566 struct got_repository *repo;
2567 struct got_worktree *worktree;
2570 static const struct got_error *
2571 collect_commitables(void *arg, unsigned char status, const char *relpath,
2572 struct got_object_id *blob_id, struct got_object_id *commit_id)
2574 struct collect_commitables_arg *a = arg;
2575 const struct got_error *err = NULL;
2576 struct got_commitable *ct = NULL;
2577 struct got_pathlist_entry *new = NULL;
2578 char *parent_path = NULL, *path = NULL;
2579 struct stat sb;
2581 if (status == GOT_STATUS_CONFLICT)
2582 return got_error(GOT_ERR_COMMIT_CONFLICT);
2584 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2585 status != GOT_STATUS_DELETE)
2586 return NULL;
2588 if (asprintf(&path, "/%s", relpath) == -1) {
2589 err = got_error_from_errno("asprintf");
2590 goto done;
2592 if (strcmp(path, "/") == 0) {
2593 parent_path = strdup("");
2594 if (parent_path == NULL)
2595 return got_error_from_errno("strdup");
2596 } else {
2597 err = got_path_dirname(&parent_path, path);
2598 if (err)
2599 return err;
2602 ct = calloc(1, sizeof(*ct));
2603 if (ct == NULL) {
2604 err = got_error_from_errno("calloc");
2605 goto done;
2608 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2609 relpath) == -1) {
2610 err = got_error_from_errno("asprintf");
2611 goto done;
2613 if (status == GOT_STATUS_DELETE) {
2614 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2615 } else {
2616 if (lstat(ct->ondisk_path, &sb) != 0) {
2617 err = got_error_from_errno2("lstat", ct->ondisk_path);
2618 goto done;
2620 ct->mode = sb.st_mode;
2623 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2624 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2625 relpath) == -1) {
2626 err = got_error_from_errno("asprintf");
2627 goto done;
2630 ct->status = status;
2631 ct->blob_id = NULL; /* will be filled in when blob gets created */
2632 if (ct->status != GOT_STATUS_ADD) {
2633 ct->base_blob_id = got_object_id_dup(blob_id);
2634 if (ct->base_blob_id == NULL) {
2635 err = got_error_from_errno("got_object_id_dup");
2636 goto done;
2638 ct->base_commit_id = got_object_id_dup(commit_id);
2639 if (ct->base_commit_id == NULL) {
2640 err = got_error_from_errno("got_object_id_dup");
2641 goto done;
2644 ct->path = strdup(path);
2645 if (ct->path == NULL) {
2646 err = got_error_from_errno("strdup");
2647 goto done;
2649 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2650 done:
2651 if (ct && (err || new == NULL))
2652 free_commitable(ct);
2653 free(parent_path);
2654 free(path);
2655 return err;
2658 static const struct got_error *write_tree(struct got_object_id **,
2659 struct got_tree_object *, const char *, struct got_pathlist_head *,
2660 got_worktree_status_cb status_cb, void *status_arg,
2661 struct got_repository *);
2663 static const struct got_error *
2664 write_subtree(struct got_object_id **new_subtree_id,
2665 struct got_tree_entry *te, const char *parent_path,
2666 struct got_pathlist_head *commitable_paths,
2667 got_worktree_status_cb status_cb, void *status_arg,
2668 struct got_repository *repo)
2670 const struct got_error *err = NULL;
2671 struct got_tree_object *subtree;
2672 char *subpath;
2674 if (asprintf(&subpath, "%s%s%s", parent_path,
2675 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2676 return got_error_from_errno("asprintf");
2678 err = got_object_open_as_tree(&subtree, repo, te->id);
2679 if (err)
2680 return err;
2682 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2683 status_cb, status_arg, repo);
2684 got_object_tree_close(subtree);
2685 free(subpath);
2686 return err;
2689 static const struct got_error *
2690 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2692 const struct got_error *err = NULL;
2693 char *ct_parent_path = NULL;
2695 *match = 0;
2697 if (strchr(ct->path, '/') == NULL) {
2698 *match = got_path_is_root_dir(path);
2699 return NULL;
2702 err = got_path_dirname(&ct_parent_path, ct->path);
2703 if (err)
2704 return err;
2705 *match = (strcmp(path, ct_parent_path) == 0);
2706 free(ct_parent_path);
2707 return err;
2710 static mode_t
2711 get_ct_file_mode(struct got_commitable *ct)
2713 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2716 static const struct got_error *
2717 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2718 struct got_tree_entry *te, struct got_commitable *ct)
2720 const struct got_error *err = NULL;
2722 *new_te = NULL;
2724 err = got_object_tree_entry_dup(new_te, te);
2725 if (err)
2726 goto done;
2728 (*new_te)->mode = get_ct_file_mode(ct);
2730 free((*new_te)->id);
2731 (*new_te)->id = got_object_id_dup(ct->blob_id);
2732 if ((*new_te)->id == NULL) {
2733 err = got_error_from_errno("got_object_id_dup");
2734 goto done;
2736 done:
2737 if (err && *new_te) {
2738 got_object_tree_entry_close(*new_te);
2739 *new_te = NULL;
2741 return err;
2744 static const struct got_error *
2745 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2746 struct got_commitable *ct)
2748 const struct got_error *err = NULL;
2749 char *ct_name;
2751 *new_te = NULL;
2753 *new_te = calloc(1, sizeof(**new_te));
2754 if (*new_te == NULL)
2755 return got_error_from_errno("calloc");
2757 ct_name = basename(ct->path);
2758 if (ct_name == NULL) {
2759 err = got_error_from_errno2("basename", ct->path);
2760 goto done;
2762 (*new_te)->name = strdup(ct_name);
2763 if ((*new_te)->name == NULL) {
2764 err = got_error_from_errno("strdup");
2765 goto done;
2768 (*new_te)->mode = get_ct_file_mode(ct);
2770 (*new_te)->id = got_object_id_dup(ct->blob_id);
2771 if ((*new_te)->id == NULL) {
2772 err = got_error_from_errno("got_object_id_dup");
2773 goto done;
2775 done:
2776 if (err && *new_te) {
2777 got_object_tree_entry_close(*new_te);
2778 *new_te = NULL;
2780 return err;
2783 static const struct got_error *
2784 insert_tree_entry(struct got_tree_entry *new_te,
2785 struct got_pathlist_head *paths)
2787 const struct got_error *err = NULL;
2788 struct got_pathlist_entry *new_pe;
2790 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2791 if (err)
2792 return err;
2793 if (new_pe == NULL)
2794 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2795 return NULL;
2798 static const struct got_error *
2799 report_ct_status(struct got_commitable *ct,
2800 got_worktree_status_cb status_cb, void *status_arg)
2802 const char *ct_path = ct->path;
2803 while (ct_path[0] == '/')
2804 ct_path++;
2805 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2808 static const struct got_error *
2809 match_modified_subtree(int *modified, struct got_tree_entry *te,
2810 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2812 const struct got_error *err = NULL;
2813 struct got_pathlist_entry *pe;
2814 char *te_path;
2816 *modified = 0;
2818 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2819 got_path_is_root_dir(base_tree_path) ? "" : "/",
2820 te->name) == -1)
2821 return got_error_from_errno("asprintf");
2823 TAILQ_FOREACH(pe, commitable_paths, entry) {
2824 struct got_commitable *ct = pe->data;
2825 *modified = got_path_is_child(ct->in_repo_path, te_path,
2826 strlen(te_path));
2827 if (*modified)
2828 break;
2831 free(te_path);
2832 return err;
2835 static const struct got_error *
2836 match_deleted_or_modified_ct(struct got_commitable **ctp,
2837 struct got_tree_entry *te, const char *base_tree_path,
2838 struct got_pathlist_head *commitable_paths)
2840 const struct got_error *err = NULL;
2841 struct got_pathlist_entry *pe;
2843 *ctp = NULL;
2845 TAILQ_FOREACH(pe, commitable_paths, entry) {
2846 struct got_commitable *ct = pe->data;
2847 char *ct_name = NULL;
2848 int path_matches;
2850 if (ct->status != GOT_STATUS_MODIFY &&
2851 ct->status != GOT_STATUS_DELETE)
2852 continue;
2854 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2855 continue;
2857 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2858 if (err)
2859 return err;
2860 if (!path_matches)
2861 continue;
2863 ct_name = basename(pe->path);
2864 if (ct_name == NULL)
2865 return got_error_from_errno2("basename", pe->path);
2867 if (strcmp(te->name, ct_name) != 0)
2868 continue;
2870 *ctp = ct;
2871 break;
2874 return err;
2877 static const struct got_error *
2878 write_tree(struct got_object_id **new_tree_id,
2879 struct got_tree_object *base_tree, const char *path_base_tree,
2880 struct got_pathlist_head *commitable_paths,
2881 got_worktree_status_cb status_cb, void *status_arg,
2882 struct got_repository *repo)
2884 const struct got_error *err = NULL;
2885 const struct got_tree_entries *base_entries = NULL;
2886 struct got_pathlist_head paths;
2887 struct got_tree_entries new_tree_entries;
2888 struct got_tree_entry *te, *new_te = NULL;
2889 struct got_pathlist_entry *pe;
2891 TAILQ_INIT(&paths);
2892 new_tree_entries.nentries = 0;
2893 SIMPLEQ_INIT(&new_tree_entries.head);
2895 /* Insert, and recurse into, newly added entries first. */
2896 TAILQ_FOREACH(pe, commitable_paths, entry) {
2897 struct got_commitable *ct = pe->data;
2898 char *child_path = NULL, *slash;
2900 if (ct->status != GOT_STATUS_ADD ||
2901 (ct->flags & GOT_COMMITABLE_ADDED))
2902 continue;
2904 if (!got_path_is_child(pe->path, path_base_tree,
2905 strlen(path_base_tree)))
2906 continue;
2908 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2909 pe->path);
2910 if (err)
2911 goto done;
2913 slash = strchr(child_path, '/');
2914 if (slash == NULL) {
2915 err = alloc_added_blob_tree_entry(&new_te, ct);
2916 if (err)
2917 goto done;
2918 err = report_ct_status(ct, status_cb, status_arg);
2919 if (err)
2920 goto done;
2921 ct->flags |= GOT_COMMITABLE_ADDED;
2922 } else {
2923 char *subtree_path;
2925 *slash = '\0'; /* trim trailing path components */
2926 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2927 got_path_is_root_dir(path_base_tree) ? "" : "/",
2928 child_path) == -1) {
2929 err = got_error_from_errno("asprintf");
2930 goto done;
2933 new_te = calloc(1, sizeof(*new_te));
2934 new_te->mode = S_IFDIR;
2935 new_te->name = strdup(child_path);
2936 if (new_te->name == NULL) {
2937 err = got_error_from_errno("strdup");
2938 got_object_tree_entry_close(new_te);
2939 new_te = NULL;
2940 goto done;
2942 err = write_tree(&new_te->id, NULL, subtree_path,
2943 commitable_paths, status_cb, status_arg, repo);
2944 free(subtree_path);
2945 if (err) {
2946 got_object_tree_entry_close(new_te);
2947 new_te = NULL;
2948 goto done;
2951 err = insert_tree_entry(new_te, &paths);
2952 if (err)
2953 goto done;
2956 if (base_tree) {
2957 /* Handle modified and deleted entries. */
2958 base_entries = got_object_tree_get_entries(base_tree);
2959 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2960 struct got_commitable *ct = NULL;
2962 if (S_ISDIR(te->mode)) {
2963 int modified;
2964 err = got_object_tree_entry_dup(&new_te, te);
2965 if (err)
2966 goto done;
2967 err = match_modified_subtree(&modified, te,
2968 path_base_tree, commitable_paths);
2969 if (err)
2970 goto done;
2971 /* Avoid recursion into unmodified subtrees. */
2972 if (modified) {
2973 free(new_te->id);
2974 err = write_subtree(&new_te->id, te,
2975 path_base_tree, commitable_paths,
2976 status_cb, status_arg, repo);
2977 if (err)
2978 goto done;
2980 err = insert_tree_entry(new_te, &paths);
2981 if (err)
2982 goto done;
2983 continue;
2986 err = match_deleted_or_modified_ct(&ct, te,
2987 path_base_tree, commitable_paths);
2988 if (ct) {
2989 /* NB: Deleted entries get dropped here. */
2990 if (ct->status == GOT_STATUS_MODIFY) {
2991 err = alloc_modified_blob_tree_entry(
2992 &new_te, te, ct);
2993 if (err)
2994 goto done;
2995 err = insert_tree_entry(new_te, &paths);
2996 if (err)
2997 goto done;
2999 err = report_ct_status(ct, status_cb,
3000 status_arg);
3001 if (err)
3002 goto done;
3003 } else {
3004 /* Entry is unchanged; just copy it. */
3005 err = got_object_tree_entry_dup(&new_te, te);
3006 if (err)
3007 goto done;
3008 err = insert_tree_entry(new_te, &paths);
3009 if (err)
3010 goto done;
3015 /* Write new list of entries; deleted entries have been dropped. */
3016 TAILQ_FOREACH(pe, &paths, entry) {
3017 struct got_tree_entry *te = pe->data;
3018 new_tree_entries.nentries++;
3019 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3021 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3022 done:
3023 got_object_tree_entries_close(&new_tree_entries);
3024 got_pathlist_free(&paths);
3025 return err;
3028 static const struct got_error *
3029 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3030 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3032 const struct got_error *err = NULL, *sync_err;
3033 char *fileindex_path = NULL;
3034 struct got_fileindex *fileindex = NULL;
3035 struct got_pathlist_entry *pe;
3037 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3038 if (err)
3039 return err;
3041 TAILQ_FOREACH(pe, commitable_paths, entry) {
3042 struct got_fileindex_entry *ie;
3043 struct got_commitable *ct = pe->data;
3045 ie = got_fileindex_entry_get(fileindex, pe->path);
3046 if (ie) {
3047 if (ct->status == GOT_STATUS_DELETE) {
3048 got_fileindex_entry_remove(fileindex, ie);
3049 got_fileindex_entry_free(ie);
3050 } else
3051 err = got_fileindex_entry_update(ie,
3052 ct->ondisk_path, ct->blob_id->sha1,
3053 new_base_commit_id->sha1, 1);
3054 } else {
3055 err = got_fileindex_entry_alloc(&ie,
3056 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3057 new_base_commit_id->sha1);
3058 if (err)
3059 break;
3060 err = got_fileindex_entry_add(fileindex, ie);
3061 if (err)
3062 break;
3065 sync_err = sync_fileindex(fileindex, fileindex_path);
3066 if (sync_err && err == NULL)
3067 err = sync_err;
3068 free(fileindex_path);
3069 got_fileindex_free(fileindex);
3070 return err;
3073 static const struct got_error *
3074 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3075 struct got_object_id *head_commit_id)
3077 const struct got_error *err = NULL;
3078 struct got_object_id *id_in_head = NULL, *id = NULL;
3079 struct got_commit_object *commit = NULL;
3080 char *path = NULL;
3081 const char *ct_path = ct->in_repo_path;
3083 while (ct_path[0] == '/')
3084 ct_path++;
3087 * Ensure that no modifications were made to files *and their parents*
3088 * in commits between the file's base commit and the branch head.
3090 * Checking the parents is important for detecting conflicting tree
3091 * configurations (files or parent folders might have been moved,
3092 * deleted, added again, etc.). Such changes need to be merged with
3093 * local changes before a commit can occur.
3095 * The implication is that the file's (parent) entry in the root
3096 * directory must have the same ID in all relevant commits.
3098 if (ct->status != GOT_STATUS_ADD) {
3099 struct got_object_qid *pid;
3100 char *slash;
3101 struct got_object_id *root_entry_id = NULL;
3103 /* Trivial case: base commit == head commit */
3104 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3105 return NULL;
3107 /* Compute the path to the root directory's entry. */
3108 path = strdup(ct_path);
3109 if (path == NULL) {
3110 err = got_error_from_errno("strdup");
3111 goto done;
3113 slash = strchr(path, '/');
3114 if (slash)
3115 *slash = '\0';
3117 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3118 if (err)
3119 goto done;
3121 err = got_object_id_by_path(&root_entry_id, repo,
3122 head_commit_id, path);
3123 if (err)
3124 goto done;
3126 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3127 while (pid) {
3128 struct got_commit_object *pcommit;
3130 err = got_object_id_by_path(&id, repo, pid->id, path);
3131 if (err) {
3132 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3133 goto done;
3134 err = NULL;
3135 break;
3138 err = got_object_id_by_path(&id, repo, pid->id, path);
3139 if (err)
3140 goto done;
3142 if (got_object_id_cmp(id, root_entry_id) != 0) {
3143 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3144 break;
3147 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3148 break; /* all relevant commits scanned */
3150 err = got_object_open_as_commit(&pcommit, repo,
3151 pid->id);
3152 if (err)
3153 goto done;
3155 got_object_commit_close(commit);
3156 commit = pcommit;
3157 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3158 commit));
3160 } else {
3161 /* Require that added files don't exist in the branch head. */
3162 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3163 ct_path);
3164 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3165 goto done;
3166 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3168 done:
3169 if (commit)
3170 got_object_commit_close(commit);
3171 free(id_in_head);
3172 free(id);
3173 free(path);
3174 return err;
3177 const struct got_error *
3178 got_worktree_commit(struct got_object_id **new_commit_id,
3179 struct got_worktree *worktree, const char *ondisk_path,
3180 const char *author, const char *committer,
3181 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3182 got_worktree_status_cb status_cb, void *status_arg,
3183 struct got_repository *repo)
3185 const struct got_error *err = NULL, *unlockerr = NULL;
3186 struct collect_commitables_arg cc_arg;
3187 struct got_pathlist_head commitable_paths;
3188 struct got_pathlist_entry *pe;
3189 char *relpath = NULL;
3190 const char *head_ref_name = NULL;
3191 struct got_reference *head_ref = NULL;
3192 struct got_commit_object *head_commit = NULL;
3193 struct got_object_id *head_commit_id = NULL;
3194 struct got_reference *head_ref2 = NULL;
3195 struct got_object_id *head_commit_id2 = NULL;
3196 struct got_tree_object *head_tree = NULL;
3197 struct got_object_id *new_tree_id = NULL;
3198 struct got_object_id_queue parent_ids;
3199 struct got_object_qid *pid = NULL;
3200 char *logmsg = NULL;
3202 *new_commit_id = NULL;
3204 TAILQ_INIT(&commitable_paths);
3205 SIMPLEQ_INIT(&parent_ids);
3207 if (ondisk_path) {
3208 err = got_path_skip_common_ancestor(&relpath,
3209 worktree->root_path, ondisk_path);
3210 if (err)
3211 return err;
3214 err = lock_worktree(worktree, LOCK_EX);
3215 if (err)
3216 goto done;
3218 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3219 if (err)
3220 goto done;
3221 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3222 if (err)
3223 goto done;
3225 cc_arg.commitable_paths = &commitable_paths;
3226 cc_arg.worktree = worktree;
3227 cc_arg.repo = repo;
3228 err = got_worktree_status(worktree, relpath ? relpath : "",
3229 repo, collect_commitables, &cc_arg, NULL, NULL);
3230 if (err)
3231 goto done;
3233 if (TAILQ_EMPTY(&commitable_paths)) {
3234 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3235 goto done;
3238 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3239 if (err)
3240 goto done;
3242 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3243 struct got_commitable *ct = pe->data;
3244 err = check_ct_out_of_date(ct, repo, head_commit_id);
3245 if (err)
3246 goto done;
3249 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3250 if (err)
3251 goto done;
3253 if (commit_msg_cb != NULL) {
3254 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3255 if (err)
3256 goto done;
3259 if (logmsg == NULL || strlen(logmsg) == 0) {
3260 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3261 goto done;
3264 /* Create blobs from added and modified files and record their IDs. */
3265 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3266 struct got_commitable *ct = pe->data;
3267 char *ondisk_path;
3269 if (ct->status != GOT_STATUS_ADD &&
3270 ct->status != GOT_STATUS_MODIFY)
3271 continue;
3273 if (asprintf(&ondisk_path, "%s/%s",
3274 worktree->root_path, pe->path) == -1) {
3275 err = got_error_from_errno("asprintf");
3276 goto done;
3278 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3279 free(ondisk_path);
3280 if (err)
3281 goto done;
3284 /* Recursively write new tree objects. */
3285 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3286 status_cb, status_arg, repo);
3287 if (err)
3288 goto done;
3290 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3291 if (err)
3292 goto done;
3293 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3294 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3295 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3296 got_object_qid_free(pid);
3297 if (logmsg != NULL)
3298 free(logmsg);
3299 if (err)
3300 goto done;
3302 /* Check if a concurrent commit to our branch has occurred. */
3303 head_ref_name = got_worktree_get_head_ref_name(worktree);
3304 if (head_ref_name == NULL) {
3305 err = got_error_from_errno("got_worktree_get_head_ref_name");
3306 goto done;
3308 /* Lock the reference here to prevent concurrent modification. */
3309 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3310 if (err)
3311 goto done;
3312 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3313 if (err)
3314 goto done;
3315 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3316 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3317 goto done;
3319 /* Update branch head in repository. */
3320 err = got_ref_change_ref(head_ref2, *new_commit_id);
3321 if (err)
3322 goto done;
3323 err = got_ref_write(head_ref2, repo);
3324 if (err)
3325 goto done;
3327 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3328 if (err)
3329 goto done;
3331 err = ref_base_commit(worktree, repo);
3332 if (err)
3333 goto done;
3335 err = update_fileindex_after_commit(&commitable_paths,
3336 *new_commit_id, worktree);
3337 if (err)
3338 goto done;
3339 done:
3340 unlockerr = lock_worktree(worktree, LOCK_SH);
3341 if (unlockerr && err == NULL)
3342 err = unlockerr;
3343 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3344 struct got_commitable *ct = pe->data;
3345 free_commitable(ct);
3347 got_pathlist_free(&commitable_paths);
3348 if (head_tree)
3349 got_object_tree_close(head_tree);
3350 if (head_commit)
3351 got_object_commit_close(head_commit);
3352 free(relpath);
3353 free(head_commit_id);
3354 free(head_commit_id2);
3355 if (head_ref)
3356 got_ref_close(head_ref);
3357 if (head_ref2) {
3358 unlockerr = got_ref_unlock(head_ref2);
3359 if (unlockerr && err == NULL)
3360 err = unlockerr;
3361 got_ref_close(head_ref2);
3363 return err;
3366 const char *
3367 got_commitable_get_path(struct got_commitable *ct)
3369 return ct->path;
3372 unsigned int
3373 got_commitable_get_status(struct got_commitable *ct)
3375 return ct->status;