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, f_deriv, blob_deriv);
765 if (err)
766 goto done;
768 free(base_path);
769 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
770 err = got_error_from_errno("asprintf");
771 base_path = NULL;
772 goto done;
775 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
776 if (err)
777 goto done;
778 if (blob_orig) {
779 err = got_object_blob_dump_to_file(NULL, NULL, f_orig,
780 blob_orig);
781 if (err)
782 goto done;
783 } else {
784 /*
785 * If the file has no blob, this is an "add vs add" conflict,
786 * and we simply use an empty ancestor file to make both files
787 * appear in the merged result in their entirety.
788 */
791 err = got_object_id_str(&id_str, worktree->base_commit_id);
792 if (err)
793 goto done;
794 if (asprintf(&label1, "commit %s", id_str) == -1) {
795 err = got_error_from_errno("asprintf");
796 goto done;
799 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
800 blob_orig_path, ondisk_path, label1, path);
801 if (err)
802 goto done;
804 (*progress_cb)(progress_arg,
805 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
807 if (fsync(merged_fd) != 0) {
808 err = got_error_from_errno("fsync");
809 goto done;
812 /* Check if a clean merge has subsumed all local changes. */
813 if (overlapcnt == 0) {
814 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
815 merged_path);
816 if (err)
817 goto done;
820 if (chmod(merged_path, st_mode) != 0) {
821 err = got_error_from_errno2("chmod", merged_path);
822 goto done;
825 if (rename(merged_path, ondisk_path) != 0) {
826 err = got_error_from_errno3("rename", merged_path,
827 ondisk_path);
828 unlink(merged_path);
829 goto done;
832 done:
833 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
834 err = got_error_from_errno("close");
835 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
836 err = got_error_from_errno("fclose");
837 if (f_orig && fclose(f_orig) != 0 && err == NULL)
838 err = got_error_from_errno("fclose");
839 free(merged_path);
840 free(base_path);
841 if (blob_deriv_path) {
842 unlink(blob_deriv_path);
843 free(blob_deriv_path);
845 if (blob_orig_path) {
846 unlink(blob_orig_path);
847 free(blob_orig_path);
849 free(id_str);
850 free(label1);
851 return err;
854 static const struct got_error *
855 update_blob_fileindex_entry(struct got_worktree *worktree,
856 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
857 const char *ondisk_path, const char *path, struct got_blob_object *blob,
858 int update_timestamps)
860 const struct got_error *err = NULL;
862 if (ie == NULL)
863 ie = got_fileindex_entry_get(fileindex, path);
864 if (ie)
865 err = got_fileindex_entry_update(ie, ondisk_path,
866 blob->id.sha1, worktree->base_commit_id->sha1,
867 update_timestamps);
868 else {
869 struct got_fileindex_entry *new_ie;
870 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
871 path, blob->id.sha1, worktree->base_commit_id->sha1);
872 if (!err)
873 err = got_fileindex_entry_add(fileindex, new_ie);
875 return err;
878 static const struct got_error *
879 install_blob(struct got_worktree *worktree, const char *ondisk_path,
880 const char *path, uint16_t te_mode, uint16_t st_mode,
881 struct got_blob_object *blob, int restoring_missing_file,
882 int reverting_versioned_file, struct got_repository *repo,
883 got_worktree_checkout_cb progress_cb, void *progress_arg)
885 const struct got_error *err = NULL;
886 int fd = -1;
887 size_t len, hdrlen;
888 int update = 0;
889 char *tmppath = NULL;
891 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
892 GOT_DEFAULT_FILE_MODE);
893 if (fd == -1) {
894 if (errno == ENOENT) {
895 char *parent = dirname(path);
896 if (parent == NULL)
897 return got_error_from_errno2("dirname", path);
898 err = add_dir_on_disk(worktree, parent);
899 if (err)
900 return err;
901 fd = open(ondisk_path,
902 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
903 GOT_DEFAULT_FILE_MODE);
904 if (fd == -1)
905 return got_error_from_errno2("open",
906 ondisk_path);
907 } else if (errno == EEXIST) {
908 if (!S_ISREG(st_mode)) {
909 /* TODO file is obstructed; do something */
910 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
911 goto done;
912 } else {
913 err = got_opentemp_named_fd(&tmppath, &fd,
914 ondisk_path);
915 if (err)
916 goto done;
917 update = 1;
919 } else
920 return got_error_from_errno2("open", ondisk_path);
923 if (restoring_missing_file)
924 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
925 else if (reverting_versioned_file)
926 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
927 else
928 (*progress_cb)(progress_arg,
929 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
931 hdrlen = got_object_blob_get_hdrlen(blob);
932 do {
933 const uint8_t *buf = got_object_blob_get_read_buf(blob);
934 err = got_object_blob_read_block(&len, blob);
935 if (err)
936 break;
937 if (len > 0) {
938 /* Skip blob object header first time around. */
939 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
940 if (outlen == -1) {
941 err = got_error_from_errno("write");
942 goto done;
943 } else if (outlen != len - hdrlen) {
944 err = got_error(GOT_ERR_IO);
945 goto done;
947 hdrlen = 0;
949 } while (len != 0);
951 if (fsync(fd) != 0) {
952 err = got_error_from_errno("fsync");
953 goto done;
956 if (update) {
957 if (rename(tmppath, ondisk_path) != 0) {
958 err = got_error_from_errno3("rename", tmppath,
959 ondisk_path);
960 unlink(tmppath);
961 goto done;
965 if (te_mode & S_IXUSR) {
966 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
967 err = got_error_from_errno2("chmod", ondisk_path);
968 goto done;
970 } else {
971 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
972 err = got_error_from_errno2("chmod", ondisk_path);
973 goto done;
977 done:
978 if (fd != -1 && close(fd) != 0 && err == NULL)
979 err = got_error_from_errno("close");
980 free(tmppath);
981 return err;
984 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
985 static const struct got_error *
986 get_modified_file_content_status(unsigned char *status, FILE *f)
988 const struct got_error *err = NULL;
989 const char *markers[3] = {
990 GOT_DIFF_CONFLICT_MARKER_BEGIN,
991 GOT_DIFF_CONFLICT_MARKER_SEP,
992 GOT_DIFF_CONFLICT_MARKER_END
993 };
994 int i = 0;
995 char *line;
996 size_t len;
997 const char delim[3] = {'\0', '\0', '\0'};
999 while (*status == GOT_STATUS_MODIFY) {
1000 line = fparseln(f, &len, NULL, delim, 0);
1001 if (line == NULL) {
1002 if (feof(f))
1003 break;
1004 err = got_ferror(f, GOT_ERR_IO);
1005 break;
1008 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1009 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1010 == 0)
1011 *status = GOT_STATUS_CONFLICT;
1012 else
1013 i++;
1017 return err;
1020 static const struct got_error *
1021 get_file_status(unsigned char *status, struct stat *sb,
1022 struct got_fileindex_entry *ie, const char *abspath,
1023 struct got_repository *repo)
1025 const struct got_error *err = NULL;
1026 struct got_object_id id;
1027 size_t hdrlen;
1028 FILE *f = NULL;
1029 uint8_t fbuf[8192];
1030 struct got_blob_object *blob = NULL;
1031 size_t flen, blen;
1033 *status = GOT_STATUS_NO_CHANGE;
1035 if (lstat(abspath, sb) == -1) {
1036 if (errno == ENOENT) {
1037 if (ie) {
1038 if (got_fileindex_entry_has_file_on_disk(ie))
1039 *status = GOT_STATUS_MISSING;
1040 else
1041 *status = GOT_STATUS_DELETE;
1042 sb->st_mode =
1043 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1044 & (S_IRWXU | S_IRWXG | S_IRWXO));
1045 } else
1046 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1047 return NULL;
1049 return got_error_from_errno2("lstat", abspath);
1052 if (!S_ISREG(sb->st_mode)) {
1053 *status = GOT_STATUS_OBSTRUCTED;
1054 return NULL;
1057 if (ie == NULL)
1058 return NULL;
1060 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1061 *status = GOT_STATUS_DELETE;
1062 return NULL;
1063 } else if (!got_fileindex_entry_has_blob(ie)) {
1064 *status = GOT_STATUS_ADD;
1065 return NULL;
1068 if (ie->ctime_sec == sb->st_ctime &&
1069 ie->ctime_nsec == sb->st_ctimensec &&
1070 ie->mtime_sec == sb->st_mtime &&
1071 ie->mtime_sec == sb->st_mtime &&
1072 ie->mtime_nsec == sb->st_mtimensec &&
1073 ie->size == (sb->st_size & 0xffffffff))
1074 return NULL;
1076 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1077 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1078 if (err)
1079 return err;
1081 f = fopen(abspath, "r");
1082 if (f == NULL) {
1083 err = got_error_from_errno2("fopen", abspath);
1084 goto done;
1086 hdrlen = got_object_blob_get_hdrlen(blob);
1087 for (;;) {
1088 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1089 err = got_object_blob_read_block(&blen, blob);
1090 if (err)
1091 goto done;
1092 /* Skip length of blob object header first time around. */
1093 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1094 if (flen == 0 && ferror(f)) {
1095 err = got_error_from_errno("fread");
1096 goto done;
1098 if (blen == 0) {
1099 if (flen != 0)
1100 *status = GOT_STATUS_MODIFY;
1101 break;
1102 } else if (flen == 0) {
1103 if (blen != 0)
1104 *status = GOT_STATUS_MODIFY;
1105 break;
1106 } else if (blen - hdrlen == flen) {
1107 /* Skip blob object header first time around. */
1108 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1109 *status = GOT_STATUS_MODIFY;
1110 break;
1112 } else {
1113 *status = GOT_STATUS_MODIFY;
1114 break;
1116 hdrlen = 0;
1119 if (*status == GOT_STATUS_MODIFY) {
1120 rewind(f);
1121 err = get_modified_file_content_status(status, f);
1123 done:
1124 if (blob)
1125 got_object_blob_close(blob);
1126 if (f)
1127 fclose(f);
1128 return err;
1131 static const struct got_error *
1132 update_blob(struct got_worktree *worktree,
1133 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1134 struct got_tree_entry *te, const char *path,
1135 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1136 void *progress_arg)
1138 const struct got_error *err = NULL;
1139 struct got_blob_object *blob = NULL;
1140 char *ondisk_path;
1141 unsigned char status = GOT_STATUS_NO_CHANGE;
1142 struct stat sb;
1144 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1145 return got_error_from_errno("asprintf");
1147 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1148 if (err)
1149 goto done;
1151 if (status == GOT_STATUS_OBSTRUCTED) {
1152 (*progress_cb)(progress_arg, status, path);
1153 goto done;
1156 if (ie && status != GOT_STATUS_MISSING) {
1157 if (got_fileindex_entry_has_commit(ie) &&
1158 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1159 SHA1_DIGEST_LENGTH) == 0) {
1160 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1161 path);
1162 goto done;
1164 if (got_fileindex_entry_has_blob(ie) &&
1165 memcmp(ie->blob_sha1, te->id->sha1,
1166 SHA1_DIGEST_LENGTH) == 0)
1167 goto done;
1170 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1171 if (err)
1172 goto done;
1174 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1175 int update_timestamps;
1176 struct got_blob_object *blob2 = NULL;
1177 if (got_fileindex_entry_has_blob(ie)) {
1178 struct got_object_id id2;
1179 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1180 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1181 if (err)
1182 goto done;
1184 err = merge_blob(&update_timestamps, worktree, blob2,
1185 ondisk_path, path, sb.st_mode, blob, repo,
1186 progress_cb, progress_arg);
1187 if (blob2)
1188 got_object_blob_close(blob2);
1190 * Do not update timestamps of files with local changes.
1191 * Otherwise, a future status walk would treat them as
1192 * unmodified files again.
1194 err = got_fileindex_entry_update(ie, ondisk_path,
1195 blob->id.sha1, worktree->base_commit_id->sha1,
1196 update_timestamps);
1197 } else if (status == GOT_STATUS_DELETE) {
1198 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1199 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1200 ondisk_path, path, blob, 0);
1201 if (err)
1202 goto done;
1203 } else {
1204 err = install_blob(worktree, ondisk_path, path, te->mode,
1205 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1206 repo, progress_cb, progress_arg);
1207 if (err)
1208 goto done;
1209 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1210 ondisk_path, path, blob, 1);
1211 if (err)
1212 goto done;
1214 got_object_blob_close(blob);
1215 done:
1216 free(ondisk_path);
1217 return err;
1220 static const struct got_error *
1221 remove_ondisk_file(const char *root_path, const char *path)
1223 const struct got_error *err = NULL;
1224 char *ondisk_path = NULL;
1226 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1227 return got_error_from_errno("asprintf");
1229 if (unlink(ondisk_path) == -1) {
1230 if (errno != ENOENT)
1231 err = got_error_from_errno2("unlink", ondisk_path);
1232 } else {
1233 char *parent = dirname(ondisk_path);
1234 while (parent && strcmp(parent, root_path) != 0) {
1235 if (rmdir(parent) == -1) {
1236 if (errno != ENOTEMPTY)
1237 err = got_error_from_errno2("rmdir",
1238 parent);
1239 break;
1241 parent = dirname(parent);
1244 free(ondisk_path);
1245 return err;
1248 static const struct got_error *
1249 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1250 struct got_fileindex_entry *ie, struct got_repository *repo,
1251 got_worktree_checkout_cb progress_cb, void *progress_arg)
1253 const struct got_error *err = NULL;
1254 unsigned char status;
1255 struct stat sb;
1256 char *ondisk_path;
1258 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1259 == -1)
1260 return got_error_from_errno("asprintf");
1262 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1263 if (err)
1264 return err;
1266 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1267 status == GOT_STATUS_ADD) {
1268 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1270 * Preserve the working file and change the deleted blob's
1271 * entry into a schedule-add entry.
1273 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1274 0);
1275 if (err)
1276 return err;
1277 } else {
1278 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1279 if (status == GOT_STATUS_NO_CHANGE) {
1280 err = remove_ondisk_file(worktree->root_path, ie->path);
1281 if (err)
1282 return err;
1284 got_fileindex_entry_remove(fileindex, ie);
1287 return err;
1290 struct diff_cb_arg {
1291 struct got_fileindex *fileindex;
1292 struct got_worktree *worktree;
1293 struct got_repository *repo;
1294 got_worktree_checkout_cb progress_cb;
1295 void *progress_arg;
1296 got_worktree_cancel_cb cancel_cb;
1297 void *cancel_arg;
1300 static const struct got_error *
1301 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1302 struct got_tree_entry *te, const char *parent_path)
1304 struct diff_cb_arg *a = arg;
1306 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1307 return got_error(GOT_ERR_CANCELLED);
1309 return update_blob(a->worktree, a->fileindex, ie, te,
1310 ie->path, a->repo, a->progress_cb, a->progress_arg);
1313 static const struct got_error *
1314 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1316 struct diff_cb_arg *a = arg;
1318 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1319 return got_error(GOT_ERR_CANCELLED);
1321 return delete_blob(a->worktree, a->fileindex, ie,
1322 a->repo, a->progress_cb, a->progress_arg);
1325 static const struct got_error *
1326 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1328 struct diff_cb_arg *a = arg;
1329 const struct got_error *err;
1330 char *path;
1332 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1333 return got_error(GOT_ERR_CANCELLED);
1335 if (asprintf(&path, "%s%s%s", parent_path,
1336 parent_path[0] ? "/" : "", te->name)
1337 == -1)
1338 return got_error_from_errno("asprintf");
1340 if (S_ISDIR(te->mode))
1341 err = add_dir_on_disk(a->worktree, path);
1342 else
1343 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1344 a->repo, a->progress_cb, a->progress_arg);
1346 free(path);
1347 return err;
1350 const struct got_error *
1351 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1353 const struct got_error *err = NULL;
1354 char *uuidstr = NULL;
1355 uint32_t uuid_status;
1357 *refname = NULL;
1359 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1360 if (uuid_status != uuid_s_ok)
1361 return got_error_uuid(uuid_status);
1363 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1364 == -1) {
1365 err = got_error_from_errno("asprintf");
1366 *refname = NULL;
1368 free(uuidstr);
1369 return err;
1373 * Prevent Git's garbage collector from deleting our base commit by
1374 * setting a reference to our base commit's ID.
1376 static const struct got_error *
1377 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1379 const struct got_error *err = NULL;
1380 struct got_reference *ref = NULL;
1381 char *refname;
1383 err = got_worktree_get_base_ref_name(&refname, worktree);
1384 if (err)
1385 return err;
1387 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1388 if (err)
1389 goto done;
1391 err = got_ref_write(ref, repo);
1392 done:
1393 free(refname);
1394 if (ref)
1395 got_ref_close(ref);
1396 return err;
1399 static const struct got_error *
1400 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1401 struct got_worktree *worktree)
1403 const struct got_error *err = NULL;
1404 FILE *index = NULL;
1406 *fileindex_path = NULL;
1407 *fileindex = got_fileindex_alloc();
1408 if (*fileindex == NULL)
1409 return got_error_from_errno("got_fileindex_alloc");
1411 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1412 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1413 err = got_error_from_errno("asprintf");
1414 *fileindex_path = NULL;
1415 goto done;
1418 index = fopen(*fileindex_path, "rb");
1419 if (index == NULL) {
1420 if (errno != ENOENT)
1421 err = got_error_from_errno2("fopen", *fileindex_path);
1422 } else {
1423 err = got_fileindex_read(*fileindex, index);
1424 if (fclose(index) != 0 && err == NULL)
1425 err = got_error_from_errno("fclose");
1427 done:
1428 if (err) {
1429 free(*fileindex_path);
1430 *fileindex_path = NULL;
1431 free(*fileindex);
1432 *fileindex = NULL;
1434 return err;
1437 struct bump_base_commit_id_arg {
1438 struct got_object_id *base_commit_id;
1439 const char *path;
1440 size_t path_len;
1441 const char *entry_name;
1442 got_worktree_checkout_cb progress_cb;
1443 void *progress_arg;
1446 /* Bump base commit ID of all files within an updated part of the work tree. */
1447 static const struct got_error *
1448 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1450 struct bump_base_commit_id_arg *a = arg;
1452 if (a->entry_name) {
1453 if (strcmp(ie->path, a->path) != 0)
1454 return NULL;
1455 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1456 return NULL;
1458 if (memcmp(ie->commit_sha1, a->base_commit_id->sha1,
1459 SHA1_DIGEST_LENGTH) == 0)
1460 return NULL;
1462 (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1463 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1464 return NULL;
1467 static const struct got_error *
1468 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1470 const struct got_error *err = NULL;
1471 char *new_fileindex_path = NULL;
1472 FILE *new_index = NULL;
1474 err = got_opentemp_named(&new_fileindex_path, &new_index,
1475 fileindex_path);
1476 if (err)
1477 goto done;
1479 err = got_fileindex_write(fileindex, new_index);
1480 if (err)
1481 goto done;
1483 if (rename(new_fileindex_path, fileindex_path) != 0) {
1484 err = got_error_from_errno3("rename", new_fileindex_path,
1485 fileindex_path);
1486 unlink(new_fileindex_path);
1488 done:
1489 if (new_index)
1490 fclose(new_index);
1491 free(new_fileindex_path);
1492 return err;
1495 const struct got_error *
1496 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1497 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1498 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1500 const struct got_error *err = NULL, *sync_err, *unlockerr;
1501 struct got_commit_object *commit = NULL;
1502 struct got_object_id *tree_id = NULL;
1503 struct got_tree_object *tree = NULL;
1504 struct got_fileindex *fileindex = NULL;
1505 char *fileindex_path = NULL;
1506 struct got_fileindex_diff_tree_cb diff_cb;
1507 struct diff_cb_arg arg;
1508 char *relpath = NULL, *entry_name = NULL;
1509 struct bump_base_commit_id_arg bbc_arg;
1511 err = lock_worktree(worktree, LOCK_EX);
1512 if (err)
1513 return err;
1516 * Read the file index.
1517 * Checking out files is supposed to be an idempotent operation.
1518 * If the on-disk file index is incomplete we will try to complete it.
1520 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1521 if (err)
1522 goto done;
1524 err = ref_base_commit(worktree, repo);
1525 if (err)
1526 goto done;
1528 err = got_object_open_as_commit(&commit, repo,
1529 worktree->base_commit_id);
1530 if (err)
1531 goto done;
1533 if (path[0]) {
1534 char *tree_path;
1535 int obj_type;
1536 relpath = strdup(path);
1537 if (relpath == NULL) {
1538 err = got_error_from_errno("strdup");
1539 goto done;
1541 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1542 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1543 path) == -1) {
1544 err = got_error_from_errno("asprintf");
1545 goto done;
1547 err = got_object_id_by_path(&tree_id, repo,
1548 worktree->base_commit_id, tree_path);
1549 free(tree_path);
1550 if (err)
1551 goto done;
1552 err = got_object_get_type(&obj_type, repo, tree_id);
1553 if (err)
1554 goto done;
1555 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1556 /* Split provided path into parent dir + entry name. */
1557 if (strchr(path, '/') == NULL) {
1558 relpath = strdup("");
1559 if (relpath == NULL) {
1560 err = got_error_from_errno("strdup");
1561 goto done;
1563 tree_path = strdup(worktree->path_prefix);
1564 if (tree_path == NULL) {
1565 err = got_error_from_errno("strdup");
1566 goto done;
1568 } else {
1569 err = got_path_dirname(&relpath, path);
1570 if (err)
1571 goto done;
1572 if (asprintf(&tree_path, "%s%s%s",
1573 worktree->path_prefix,
1574 got_path_is_root_dir(
1575 worktree->path_prefix) ? "" : "/",
1576 relpath) == -1) {
1577 err = got_error_from_errno("asprintf");
1578 goto done;
1581 err = got_object_id_by_path(&tree_id, repo,
1582 worktree->base_commit_id, tree_path);
1583 free(tree_path);
1584 if (err)
1585 goto done;
1586 entry_name = basename(path);
1587 if (entry_name == NULL) {
1588 err = got_error_from_errno2("basename", path);
1589 goto done;
1592 } else {
1593 relpath = strdup("");
1594 if (relpath == NULL) {
1595 err = got_error_from_errno("strdup");
1596 goto done;
1598 err = got_object_id_by_path(&tree_id, repo,
1599 worktree->base_commit_id, worktree->path_prefix);
1600 if (err)
1601 goto done;
1604 err = got_object_open_as_tree(&tree, repo, tree_id);
1605 if (err)
1606 goto done;
1608 if (entry_name &&
1609 got_object_tree_find_entry(tree, entry_name) == NULL) {
1610 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1611 goto done;
1614 diff_cb.diff_old_new = diff_old_new;
1615 diff_cb.diff_old = diff_old;
1616 diff_cb.diff_new = diff_new;
1617 arg.fileindex = fileindex;
1618 arg.worktree = worktree;
1619 arg.repo = repo;
1620 arg.progress_cb = progress_cb;
1621 arg.progress_arg = progress_arg;
1622 arg.cancel_cb = cancel_cb;
1623 arg.cancel_arg = cancel_arg;
1624 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1625 entry_name, repo, &diff_cb, &arg);
1626 if (err)
1627 goto sync;
1629 bbc_arg.base_commit_id = worktree->base_commit_id;
1630 bbc_arg.entry_name = entry_name;
1631 bbc_arg.path = path;
1632 bbc_arg.path_len = strlen(path);
1633 bbc_arg.progress_cb = progress_cb;
1634 bbc_arg.progress_arg = progress_arg;
1635 err = got_fileindex_for_each_entry_safe(fileindex,
1636 bump_base_commit_id, &bbc_arg);
1637 sync:
1638 sync_err = sync_fileindex(fileindex, fileindex_path);
1639 if (sync_err && err == NULL)
1640 err = sync_err;
1641 done:
1642 free(fileindex_path);
1643 free(relpath);
1644 if (tree)
1645 got_object_tree_close(tree);
1646 if (commit)
1647 got_object_commit_close(commit);
1648 got_fileindex_free(fileindex);
1649 unlockerr = lock_worktree(worktree, LOCK_SH);
1650 if (unlockerr && err == NULL)
1651 err = unlockerr;
1652 return err;
1655 struct merge_file_cb_arg {
1656 struct got_worktree *worktree;
1657 struct got_fileindex *fileindex;
1658 got_worktree_checkout_cb progress_cb;
1659 void *progress_arg;
1660 got_worktree_cancel_cb cancel_cb;
1661 void *cancel_arg;
1664 static const struct got_error *
1665 merge_file_cb(void *arg, struct got_blob_object *blob1,
1666 struct got_blob_object *blob2, struct got_object_id *id1,
1667 struct got_object_id *id2, const char *path1, const char *path2,
1668 struct got_repository *repo)
1670 static const struct got_error *err = NULL;
1671 struct merge_file_cb_arg *a = arg;
1672 struct got_fileindex_entry *ie;
1673 char *ondisk_path = NULL;
1674 struct stat sb;
1675 unsigned char status;
1676 int local_changes_subsumed;
1678 if (blob1 && blob2) {
1679 ie = got_fileindex_entry_get(a->fileindex, path2);
1680 if (ie == NULL) {
1681 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1682 path2);
1683 return NULL;
1686 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1687 path2) == -1)
1688 return got_error_from_errno("asprintf");
1690 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1691 if (err)
1692 goto done;
1694 if (status == GOT_STATUS_DELETE) {
1695 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1696 path2);
1697 goto done;
1699 if (status != GOT_STATUS_NO_CHANGE &&
1700 status != GOT_STATUS_MODIFY &&
1701 status != GOT_STATUS_CONFLICT &&
1702 status != GOT_STATUS_ADD) {
1703 (*a->progress_cb)(a->progress_arg, status, path2);
1704 goto done;
1707 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1708 ondisk_path, path2, sb.st_mode, blob2, repo,
1709 a->progress_cb, a->progress_arg);
1710 } else if (blob1) {
1711 ie = got_fileindex_entry_get(a->fileindex, path1);
1712 if (ie == NULL) {
1713 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1714 path2);
1715 return NULL;
1718 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1719 path1) == -1)
1720 return got_error_from_errno("asprintf");
1722 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1723 if (err)
1724 goto done;
1726 switch (status) {
1727 case GOT_STATUS_NO_CHANGE:
1728 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1729 path1);
1730 err = remove_ondisk_file(a->worktree->root_path, path1);
1731 if (err)
1732 goto done;
1733 if (ie)
1734 got_fileindex_entry_mark_deleted_from_disk(ie);
1735 break;
1736 case GOT_STATUS_DELETE:
1737 case GOT_STATUS_MISSING:
1738 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1739 path1);
1740 if (ie)
1741 got_fileindex_entry_mark_deleted_from_disk(ie);
1742 break;
1743 case GOT_STATUS_ADD:
1744 case GOT_STATUS_MODIFY:
1745 case GOT_STATUS_CONFLICT:
1746 (*a->progress_cb)(a->progress_arg,
1747 GOT_STATUS_CANNOT_DELETE, path1);
1748 break;
1749 case GOT_STATUS_OBSTRUCTED:
1750 (*a->progress_cb)(a->progress_arg, status, path1);
1751 break;
1752 default:
1753 break;
1755 } else if (blob2) {
1756 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1757 path2) == -1)
1758 return got_error_from_errno("asprintf");
1759 ie = got_fileindex_entry_get(a->fileindex, path2);
1760 if (ie) {
1761 err = get_file_status(&status, &sb, ie, ondisk_path,
1762 repo);
1763 if (err)
1764 goto done;
1765 if (status != GOT_STATUS_NO_CHANGE &&
1766 status != GOT_STATUS_MODIFY &&
1767 status != GOT_STATUS_CONFLICT &&
1768 status != GOT_STATUS_ADD) {
1769 (*a->progress_cb)(a->progress_arg, status,
1770 path2);
1771 goto done;
1773 err = merge_blob(&local_changes_subsumed, a->worktree,
1774 NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1775 a->progress_cb, a->progress_arg);
1776 if (status == GOT_STATUS_DELETE) {
1777 err = update_blob_fileindex_entry(a->worktree,
1778 a->fileindex, ie, ondisk_path, ie->path,
1779 blob2, 0);
1780 if (err)
1781 goto done;
1783 } else {
1784 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1785 err = install_blob(a->worktree, ondisk_path, path2,
1786 /* XXX get this from parent tree! */
1787 GOT_DEFAULT_FILE_MODE,
1788 sb.st_mode, blob2, 0, 0, repo,
1789 a->progress_cb, a->progress_arg);
1790 if (err)
1791 goto done;
1792 err = got_fileindex_entry_alloc(&ie,
1793 ondisk_path, path2, NULL, NULL);
1794 if (err)
1795 goto done;
1796 err = got_fileindex_entry_add(a->fileindex, ie);
1797 if (err) {
1798 got_fileindex_entry_free(ie);
1799 goto done;
1803 done:
1804 free(ondisk_path);
1805 return err;
1808 struct check_merge_ok_arg {
1809 struct got_worktree *worktree;
1810 struct got_repository *repo;
1813 static const struct got_error *
1814 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1816 const struct got_error *err = NULL;
1817 struct check_merge_ok_arg *a = arg;
1818 unsigned char status;
1819 struct stat sb;
1820 char *ondisk_path;
1822 /* Reject merges into a work tree with mixed base commits. */
1823 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1824 SHA1_DIGEST_LENGTH))
1825 return got_error(GOT_ERR_MIXED_COMMITS);
1827 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1828 == -1)
1829 return got_error_from_errno("asprintf");
1831 /* Reject merges into a work tree with conflicted files. */
1832 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1833 if (err)
1834 return err;
1835 if (status == GOT_STATUS_CONFLICT)
1836 return got_error(GOT_ERR_CONFLICTS);
1838 return NULL;
1841 const struct got_error *
1842 got_worktree_merge_files(struct got_worktree *worktree,
1843 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1844 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1845 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1847 const struct got_error *err = NULL, *sync_err, *unlockerr;
1848 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1849 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1850 struct merge_file_cb_arg arg;
1851 char *fileindex_path = NULL;
1852 struct got_fileindex *fileindex = NULL;
1853 struct check_merge_ok_arg mok_arg;
1855 err = lock_worktree(worktree, LOCK_EX);
1856 if (err)
1857 return err;
1859 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1860 if (err)
1861 goto done;
1863 mok_arg.worktree = worktree;
1864 mok_arg.repo = repo;
1865 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1866 &mok_arg);
1867 if (err)
1868 goto done;
1870 if (commit_id1) {
1871 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1872 worktree->path_prefix);
1873 if (err)
1874 goto done;
1876 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1877 if (err)
1878 goto done;
1881 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1882 worktree->path_prefix);
1883 if (err)
1884 goto done;
1886 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1887 if (err)
1888 goto done;
1890 arg.worktree = worktree;
1891 arg.fileindex = fileindex;
1892 arg.progress_cb = progress_cb;
1893 arg.progress_arg = progress_arg;
1894 arg.cancel_cb = cancel_cb;
1895 arg.cancel_arg = cancel_arg;
1896 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1897 sync_err = sync_fileindex(fileindex, fileindex_path);
1898 if (sync_err && err == NULL)
1899 err = sync_err;
1900 done:
1901 got_fileindex_free(fileindex);
1902 if (tree1)
1903 got_object_tree_close(tree1);
1904 if (tree2)
1905 got_object_tree_close(tree2);
1907 unlockerr = lock_worktree(worktree, LOCK_SH);
1908 if (unlockerr && err == NULL)
1909 err = unlockerr;
1910 return err;
1913 struct diff_dir_cb_arg {
1914 struct got_fileindex *fileindex;
1915 struct got_worktree *worktree;
1916 const char *status_path;
1917 size_t status_path_len;
1918 struct got_repository *repo;
1919 got_worktree_status_cb status_cb;
1920 void *status_arg;
1921 got_worktree_cancel_cb cancel_cb;
1922 void *cancel_arg;
1925 static const struct got_error *
1926 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1927 got_worktree_status_cb status_cb, void *status_arg,
1928 struct got_repository *repo)
1930 const struct got_error *err = NULL;
1931 unsigned char status = GOT_STATUS_NO_CHANGE;
1932 struct stat sb;
1933 struct got_object_id blob_id, commit_id;
1935 err = get_file_status(&status, &sb, ie, abspath, repo);
1936 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1937 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1938 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1939 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1940 &commit_id);
1942 return err;
1945 static const struct got_error *
1946 status_old_new(void *arg, struct got_fileindex_entry *ie,
1947 struct dirent *de, const char *parent_path)
1949 const struct got_error *err = NULL;
1950 struct diff_dir_cb_arg *a = arg;
1951 char *abspath;
1953 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1954 return got_error(GOT_ERR_CANCELLED);
1956 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1957 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1958 return NULL;
1960 if (parent_path[0]) {
1961 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1962 parent_path, de->d_name) == -1)
1963 return got_error_from_errno("asprintf");
1964 } else {
1965 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1966 de->d_name) == -1)
1967 return got_error_from_errno("asprintf");
1970 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1971 a->repo);
1972 free(abspath);
1973 return err;
1976 static const struct got_error *
1977 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1979 struct diff_dir_cb_arg *a = arg;
1980 struct got_object_id blob_id, commit_id;
1981 unsigned char status;
1983 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1984 return got_error(GOT_ERR_CANCELLED);
1986 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1987 return NULL;
1989 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1990 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1991 if (got_fileindex_entry_has_file_on_disk(ie))
1992 status = GOT_STATUS_MISSING;
1993 else
1994 status = GOT_STATUS_DELETE;
1995 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
1996 &commit_id);
1999 static const struct got_error *
2000 status_new(void *arg, struct dirent *de, const char *parent_path)
2002 const struct got_error *err = NULL;
2003 struct diff_dir_cb_arg *a = arg;
2004 char *path = NULL;
2006 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2007 return got_error(GOT_ERR_CANCELLED);
2009 if (de->d_type == DT_DIR)
2010 return NULL;
2012 /* XXX ignore symlinks for now */
2013 if (de->d_type == DT_LNK)
2014 return NULL;
2016 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2017 return NULL;
2019 if (parent_path[0]) {
2020 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2021 return got_error_from_errno("asprintf");
2022 } else {
2023 path = de->d_name;
2026 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2027 NULL, NULL);
2028 if (parent_path[0])
2029 free(path);
2030 return err;
2033 const struct got_error *
2034 got_worktree_status(struct got_worktree *worktree, const char *path,
2035 struct got_repository *repo, got_worktree_status_cb status_cb,
2036 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2038 const struct got_error *err = NULL;
2039 DIR *workdir = NULL;
2040 char *fileindex_path = NULL;
2041 struct got_fileindex *fileindex = NULL;
2042 FILE *index = NULL;
2043 struct got_fileindex_diff_dir_cb fdiff_cb;
2044 struct diff_dir_cb_arg arg;
2045 char *ondisk_path = NULL;
2047 fileindex = got_fileindex_alloc();
2048 if (fileindex == NULL) {
2049 err = got_error_from_errno("got_fileindex_alloc");
2050 goto done;
2053 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2054 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2055 err = got_error_from_errno("asprintf");
2056 fileindex_path = NULL;
2057 goto done;
2060 index = fopen(fileindex_path, "rb");
2061 if (index == NULL) {
2062 if (errno != ENOENT) {
2063 err = got_error_from_errno2("fopen", fileindex_path);
2064 goto done;
2066 } else {
2067 err = got_fileindex_read(fileindex, index);
2068 fclose(index);
2069 if (err)
2070 goto done;
2073 if (asprintf(&ondisk_path, "%s%s%s",
2074 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2075 err = got_error_from_errno("asprintf");
2076 goto done;
2078 workdir = opendir(ondisk_path);
2079 if (workdir == NULL) {
2080 if (errno == ENOTDIR || errno == ENOENT) {
2081 struct got_fileindex_entry *ie;
2082 ie = got_fileindex_entry_get(fileindex, path);
2083 if (ie == NULL) {
2084 err = got_error(GOT_ERR_BAD_PATH);
2085 goto done;
2087 err = report_file_status(ie, ondisk_path,
2088 status_cb, status_arg, repo);
2089 goto done;
2090 } else {
2091 err = got_error_from_errno2("opendir", ondisk_path);
2092 goto done;
2095 fdiff_cb.diff_old_new = status_old_new;
2096 fdiff_cb.diff_old = status_old;
2097 fdiff_cb.diff_new = status_new;
2098 arg.fileindex = fileindex;
2099 arg.worktree = worktree;
2100 arg.status_path = path;
2101 arg.status_path_len = strlen(path);
2102 arg.repo = repo;
2103 arg.status_cb = status_cb;
2104 arg.status_arg = status_arg;
2105 arg.cancel_cb = cancel_cb;
2106 arg.cancel_arg = cancel_arg;
2107 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2108 path, repo, &fdiff_cb, &arg);
2109 done:
2110 if (workdir)
2111 closedir(workdir);
2112 free(ondisk_path);
2113 free(fileindex_path);
2114 got_fileindex_free(fileindex);
2115 return err;
2118 const struct got_error *
2119 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2120 const char *arg)
2122 const struct got_error *err = NULL;
2123 char *resolved, *path = NULL;
2124 size_t len;
2126 *wt_path = NULL;
2128 resolved = realpath(arg, NULL);
2129 if (resolved == NULL)
2130 return got_error_from_errno2("realpath", arg);
2132 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2133 strlen(got_worktree_get_root_path(worktree)))) {
2134 err = got_error(GOT_ERR_BAD_PATH);
2135 goto done;
2138 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2139 err = got_path_skip_common_ancestor(&path,
2140 got_worktree_get_root_path(worktree), resolved);
2141 if (err)
2142 goto done;
2143 } else {
2144 path = strdup("");
2145 if (path == NULL) {
2146 err = got_error_from_errno("strdup");
2147 goto done;
2151 /* XXX status walk can't deal with trailing slash! */
2152 len = strlen(path);
2153 while (path[len - 1] == '/') {
2154 path[len - 1] = '\0';
2155 len--;
2157 done:
2158 free(resolved);
2159 if (err == NULL)
2160 *wt_path = path;
2161 else
2162 free(path);
2163 return err;
2166 static const struct got_error *
2167 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2168 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2169 struct got_repository *repo)
2171 const struct got_error *err = NULL;
2172 struct got_fileindex_entry *ie;
2174 /* Re-adding an existing entry is a no-op. */
2175 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2176 return NULL;
2178 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2179 if (err)
2180 return err;
2182 err = got_fileindex_entry_add(fileindex, ie);
2183 if (err) {
2184 got_fileindex_entry_free(ie);
2185 return err;
2188 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2191 const struct got_error *
2192 got_worktree_schedule_add(struct got_worktree *worktree,
2193 struct got_pathlist_head *ondisk_paths,
2194 got_worktree_status_cb status_cb, void *status_arg,
2195 struct got_repository *repo)
2197 struct got_fileindex *fileindex = NULL;
2198 char *fileindex_path = NULL;
2199 FILE *index = NULL;
2200 const struct got_error *err = NULL, *sync_err, *unlockerr;
2201 struct got_pathlist_entry *pe;
2203 err = lock_worktree(worktree, LOCK_EX);
2204 if (err)
2205 return err;
2208 fileindex = got_fileindex_alloc();
2209 if (fileindex == NULL) {
2210 err = got_error_from_errno("got_fileindex_alloc");
2211 goto done;
2214 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2215 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2216 err = got_error_from_errno("asprintf");
2217 fileindex_path = NULL;
2218 goto done;
2221 index = fopen(fileindex_path, "rb");
2222 if (index == NULL) {
2223 err = got_error_from_errno2("fopen", fileindex_path);
2224 goto done;
2227 err = got_fileindex_read(fileindex, index);
2228 if (err)
2229 goto done;
2231 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2232 char *relpath;
2233 err = got_path_skip_common_ancestor(&relpath,
2234 got_worktree_get_root_path(worktree), pe->path);
2235 if (err)
2236 break;
2237 err = schedule_addition(pe->path, fileindex, relpath,
2238 status_cb, status_arg, repo);
2239 free(relpath);
2240 if (err)
2241 break;
2243 sync_err = sync_fileindex(fileindex, fileindex_path);
2244 if (sync_err && err == NULL)
2245 err = sync_err;
2246 done:
2247 if (index) {
2248 if (fclose(index) != 0 && err == NULL)
2249 err = got_error_from_errno("fclose");
2251 if (fileindex)
2252 got_fileindex_free(fileindex);
2253 unlockerr = lock_worktree(worktree, LOCK_SH);
2254 if (unlockerr && err == NULL)
2255 err = unlockerr;
2256 return err;
2259 static const struct got_error *
2260 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2261 const char *relpath, int delete_local_mods,
2262 got_worktree_status_cb status_cb, void *status_arg,
2263 struct got_repository *repo)
2265 const struct got_error *err = NULL;
2266 struct got_fileindex_entry *ie = NULL;
2267 unsigned char status;
2268 struct stat sb;
2270 ie = got_fileindex_entry_get(fileindex, relpath);
2271 if (ie == NULL)
2272 return got_error(GOT_ERR_BAD_PATH);
2274 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2275 if (err)
2276 return err;
2278 if (status != GOT_STATUS_NO_CHANGE) {
2279 if (status == GOT_STATUS_DELETE)
2280 return got_error_set_errno(ENOENT, ondisk_path);
2281 if (status != GOT_STATUS_MODIFY)
2282 return got_error(GOT_ERR_FILE_STATUS);
2283 if (!delete_local_mods)
2284 return got_error(GOT_ERR_FILE_MODIFIED);
2287 if (unlink(ondisk_path) != 0)
2288 return got_error_from_errno2("unlink", ondisk_path);
2290 got_fileindex_entry_mark_deleted_from_disk(ie);
2291 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2294 const struct got_error *
2295 got_worktree_schedule_delete(struct got_worktree *worktree,
2296 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2297 got_worktree_status_cb status_cb, void *status_arg,
2298 struct got_repository *repo)
2300 struct got_fileindex *fileindex = NULL;
2301 char *fileindex_path = NULL;
2302 FILE *index = NULL;
2303 const struct got_error *err = NULL, *sync_err, *unlockerr;
2304 struct got_pathlist_entry *pe;
2306 err = lock_worktree(worktree, LOCK_EX);
2307 if (err)
2308 return err;
2310 fileindex = got_fileindex_alloc();
2311 if (fileindex == NULL) {
2312 err = got_error_from_errno("got_fileindex_alloc");
2313 goto done;
2316 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2317 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2318 err = got_error_from_errno("asprintf");
2319 fileindex_path = NULL;
2320 goto done;
2323 index = fopen(fileindex_path, "rb");
2324 if (index == NULL) {
2325 err = got_error_from_errno2("fopen", fileindex_path);
2326 goto done;
2329 err = got_fileindex_read(fileindex, index);
2330 if (err)
2331 goto done;
2333 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2334 char *relpath;
2335 err = got_path_skip_common_ancestor(&relpath,
2336 got_worktree_get_root_path(worktree), pe->path);
2337 if (err)
2338 break;
2339 err = schedule_for_deletion(pe->path, fileindex, relpath,
2340 delete_local_mods, status_cb, status_arg, repo);
2341 free(relpath);
2342 if (err)
2343 break;
2345 sync_err = sync_fileindex(fileindex, fileindex_path);
2346 if (sync_err && err == NULL)
2347 err = sync_err;
2348 done:
2349 if (index) {
2350 if (fclose(index) != 0 && err == NULL)
2351 err = got_error_from_errno("fclose");
2353 if (fileindex)
2354 got_fileindex_free(fileindex);
2355 unlockerr = lock_worktree(worktree, LOCK_SH);
2356 if (unlockerr && err == NULL)
2357 err = unlockerr;
2358 return err;
2361 static const struct got_error *
2362 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2363 const char *ondisk_path,
2364 got_worktree_checkout_cb progress_cb, void *progress_arg,
2365 struct got_repository *repo)
2367 const struct got_error *err = NULL;
2368 char *relpath = NULL, *parent_path = NULL;
2369 struct got_fileindex_entry *ie;
2370 struct got_tree_object *tree = NULL;
2371 struct got_object_id *tree_id = NULL;
2372 const struct got_tree_entry *te;
2373 char *tree_path = NULL, *te_name;
2374 struct got_blob_object *blob = NULL;
2375 unsigned char status;
2376 struct stat sb;
2378 err = got_path_skip_common_ancestor(&relpath,
2379 got_worktree_get_root_path(worktree), ondisk_path);
2380 if (err)
2381 goto done;
2383 ie = got_fileindex_entry_get(fileindex, relpath);
2384 if (ie == NULL) {
2385 err = got_error(GOT_ERR_BAD_PATH);
2386 goto done;
2389 /* Construct in-repository path of tree which contains this blob. */
2390 err = got_path_dirname(&parent_path, ie->path);
2391 if (err) {
2392 if (err->code != GOT_ERR_BAD_PATH)
2393 goto done;
2394 parent_path = strdup("/");
2395 if (parent_path == NULL) {
2396 err = got_error_from_errno("strdup");
2397 goto done;
2400 if (got_path_is_root_dir(worktree->path_prefix)) {
2401 tree_path = strdup(parent_path);
2402 if (tree_path == NULL) {
2403 err = got_error_from_errno("strdup");
2404 goto done;
2406 } else {
2407 if (got_path_is_root_dir(parent_path)) {
2408 tree_path = strdup(worktree->path_prefix);
2409 if (tree_path == NULL) {
2410 err = got_error_from_errno("strdup");
2411 goto done;
2413 } else {
2414 if (asprintf(&tree_path, "%s/%s",
2415 worktree->path_prefix, parent_path) == -1) {
2416 err = got_error_from_errno("asprintf");
2417 goto done;
2422 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2423 tree_path);
2424 if (err)
2425 goto done;
2427 err = got_object_open_as_tree(&tree, repo, tree_id);
2428 if (err)
2429 goto done;
2431 te_name = basename(ie->path);
2432 if (te_name == NULL) {
2433 err = got_error_from_errno2("basename", ie->path);
2434 goto done;
2437 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2438 if (err)
2439 goto done;
2441 te = got_object_tree_find_entry(tree, te_name);
2442 if (te == NULL && status != GOT_STATUS_ADD) {
2443 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2444 goto done;
2447 switch (status) {
2448 case GOT_STATUS_ADD:
2449 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2450 got_fileindex_entry_remove(fileindex, ie);
2451 break;
2452 case GOT_STATUS_DELETE:
2453 case GOT_STATUS_MODIFY:
2454 case GOT_STATUS_CONFLICT:
2455 case GOT_STATUS_MISSING: {
2456 struct got_object_id id;
2457 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2458 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2459 if (err)
2460 goto done;
2461 err = install_blob(worktree, ondisk_path, ie->path,
2462 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2463 progress_arg);
2464 if (err)
2465 goto done;
2466 if (status == GOT_STATUS_DELETE) {
2467 err = update_blob_fileindex_entry(worktree,
2468 fileindex, ie, ondisk_path, ie->path, blob, 1);
2469 if (err)
2470 goto done;
2472 break;
2474 default:
2475 goto done;
2477 done:
2478 free(relpath);
2479 free(parent_path);
2480 free(tree_path);
2481 if (blob)
2482 got_object_blob_close(blob);
2483 if (tree)
2484 got_object_tree_close(tree);
2485 free(tree_id);
2486 return err;
2489 const struct got_error *
2490 got_worktree_revert(struct got_worktree *worktree,
2491 struct got_pathlist_head *ondisk_paths,
2492 got_worktree_checkout_cb progress_cb, void *progress_arg,
2493 struct got_repository *repo)
2495 struct got_fileindex *fileindex = NULL;
2496 char *fileindex_path = NULL;
2497 FILE *index = NULL;
2498 const struct got_error *err = NULL, *unlockerr = NULL;
2499 const struct got_error *sync_err = NULL;
2500 struct got_pathlist_entry *pe;
2502 err = lock_worktree(worktree, LOCK_EX);
2503 if (err)
2504 return err;
2506 fileindex = got_fileindex_alloc();
2507 if (fileindex == NULL) {
2508 err = got_error_from_errno("got_fileindex_alloc");
2509 goto done;
2512 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2513 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2514 err = got_error_from_errno("asprintf");
2515 fileindex_path = NULL;
2516 goto done;
2519 index = fopen(fileindex_path, "rb");
2520 if (index == NULL) {
2521 err = got_error_from_errno2("fopen", fileindex_path);
2522 goto done;
2525 err = got_fileindex_read(fileindex, index);
2526 if (err)
2527 goto done;
2529 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2530 err = revert_file(worktree, fileindex, pe->path,
2531 progress_cb, progress_arg, repo);
2532 if (err)
2533 break;
2535 sync_err = sync_fileindex(fileindex, fileindex_path);
2536 if (sync_err && err == NULL)
2537 err = sync_err;
2538 done:
2539 if (index) {
2540 if (fclose(index) != 0 && err == NULL)
2541 err = got_error_from_errno("fclose");
2543 if (fileindex)
2544 got_fileindex_free(fileindex);
2545 unlockerr = lock_worktree(worktree, LOCK_SH);
2546 if (unlockerr && err == NULL)
2547 err = unlockerr;
2548 return err;
2551 static void
2552 free_commitable(struct got_commitable *ct)
2554 free(ct->path);
2555 free(ct->in_repo_path);
2556 free(ct->ondisk_path);
2557 free(ct->blob_id);
2558 free(ct->base_blob_id);
2559 free(ct->base_commit_id);
2560 free(ct);
2563 struct collect_commitables_arg {
2564 struct got_pathlist_head *commitable_paths;
2565 struct got_repository *repo;
2566 struct got_worktree *worktree;
2569 static const struct got_error *
2570 collect_commitables(void *arg, unsigned char status, const char *relpath,
2571 struct got_object_id *blob_id, struct got_object_id *commit_id)
2573 struct collect_commitables_arg *a = arg;
2574 const struct got_error *err = NULL;
2575 struct got_commitable *ct = NULL;
2576 struct got_pathlist_entry *new = NULL;
2577 char *parent_path = NULL, *path = NULL;
2578 struct stat sb;
2580 if (status == GOT_STATUS_CONFLICT)
2581 return got_error(GOT_ERR_COMMIT_CONFLICT);
2583 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2584 status != GOT_STATUS_DELETE)
2585 return NULL;
2587 if (asprintf(&path, "/%s", relpath) == -1) {
2588 err = got_error_from_errno("asprintf");
2589 goto done;
2591 if (strcmp(path, "/") == 0) {
2592 parent_path = strdup("");
2593 if (parent_path == NULL)
2594 return got_error_from_errno("strdup");
2595 } else {
2596 err = got_path_dirname(&parent_path, path);
2597 if (err)
2598 return err;
2601 ct = calloc(1, sizeof(*ct));
2602 if (ct == NULL) {
2603 err = got_error_from_errno("calloc");
2604 goto done;
2607 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2608 relpath) == -1) {
2609 err = got_error_from_errno("asprintf");
2610 goto done;
2612 if (status == GOT_STATUS_DELETE) {
2613 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2614 } else {
2615 if (lstat(ct->ondisk_path, &sb) != 0) {
2616 err = got_error_from_errno2("lstat", ct->ondisk_path);
2617 goto done;
2619 ct->mode = sb.st_mode;
2622 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2623 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2624 relpath) == -1) {
2625 err = got_error_from_errno("asprintf");
2626 goto done;
2629 ct->status = status;
2630 ct->blob_id = NULL; /* will be filled in when blob gets created */
2631 if (ct->status != GOT_STATUS_ADD) {
2632 ct->base_blob_id = got_object_id_dup(blob_id);
2633 if (ct->base_blob_id == NULL) {
2634 err = got_error_from_errno("got_object_id_dup");
2635 goto done;
2637 ct->base_commit_id = got_object_id_dup(commit_id);
2638 if (ct->base_commit_id == NULL) {
2639 err = got_error_from_errno("got_object_id_dup");
2640 goto done;
2643 ct->path = strdup(path);
2644 if (ct->path == NULL) {
2645 err = got_error_from_errno("strdup");
2646 goto done;
2648 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2649 done:
2650 if (ct && (err || new == NULL))
2651 free_commitable(ct);
2652 free(parent_path);
2653 free(path);
2654 return err;
2657 static const struct got_error *write_tree(struct got_object_id **,
2658 struct got_tree_object *, const char *, struct got_pathlist_head *,
2659 got_worktree_status_cb status_cb, void *status_arg,
2660 struct got_repository *);
2662 static const struct got_error *
2663 write_subtree(struct got_object_id **new_subtree_id,
2664 struct got_tree_entry *te, const char *parent_path,
2665 struct got_pathlist_head *commitable_paths,
2666 got_worktree_status_cb status_cb, void *status_arg,
2667 struct got_repository *repo)
2669 const struct got_error *err = NULL;
2670 struct got_tree_object *subtree;
2671 char *subpath;
2673 if (asprintf(&subpath, "%s%s%s", parent_path,
2674 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2675 return got_error_from_errno("asprintf");
2677 err = got_object_open_as_tree(&subtree, repo, te->id);
2678 if (err)
2679 return err;
2681 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2682 status_cb, status_arg, repo);
2683 got_object_tree_close(subtree);
2684 free(subpath);
2685 return err;
2688 static const struct got_error *
2689 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2691 const struct got_error *err = NULL;
2692 char *ct_parent_path = NULL;
2694 *match = 0;
2696 if (strchr(ct->path, '/') == NULL) {
2697 *match = got_path_is_root_dir(path);
2698 return NULL;
2701 err = got_path_dirname(&ct_parent_path, ct->path);
2702 if (err)
2703 return err;
2704 *match = (strcmp(path, ct_parent_path) == 0);
2705 free(ct_parent_path);
2706 return err;
2709 static mode_t
2710 get_ct_file_mode(struct got_commitable *ct)
2712 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2715 static const struct got_error *
2716 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2717 struct got_tree_entry *te, struct got_commitable *ct)
2719 const struct got_error *err = NULL;
2721 *new_te = NULL;
2723 err = got_object_tree_entry_dup(new_te, te);
2724 if (err)
2725 goto done;
2727 (*new_te)->mode = get_ct_file_mode(ct);
2729 free((*new_te)->id);
2730 (*new_te)->id = got_object_id_dup(ct->blob_id);
2731 if ((*new_te)->id == NULL) {
2732 err = got_error_from_errno("got_object_id_dup");
2733 goto done;
2735 done:
2736 if (err && *new_te) {
2737 got_object_tree_entry_close(*new_te);
2738 *new_te = NULL;
2740 return err;
2743 static const struct got_error *
2744 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2745 struct got_commitable *ct)
2747 const struct got_error *err = NULL;
2748 char *ct_name;
2750 *new_te = NULL;
2752 *new_te = calloc(1, sizeof(**new_te));
2753 if (*new_te == NULL)
2754 return got_error_from_errno("calloc");
2756 ct_name = basename(ct->path);
2757 if (ct_name == NULL) {
2758 err = got_error_from_errno2("basename", ct->path);
2759 goto done;
2761 (*new_te)->name = strdup(ct_name);
2762 if ((*new_te)->name == NULL) {
2763 err = got_error_from_errno("strdup");
2764 goto done;
2767 (*new_te)->mode = get_ct_file_mode(ct);
2769 (*new_te)->id = got_object_id_dup(ct->blob_id);
2770 if ((*new_te)->id == NULL) {
2771 err = got_error_from_errno("got_object_id_dup");
2772 goto done;
2774 done:
2775 if (err && *new_te) {
2776 got_object_tree_entry_close(*new_te);
2777 *new_te = NULL;
2779 return err;
2782 static const struct got_error *
2783 insert_tree_entry(struct got_tree_entry *new_te,
2784 struct got_pathlist_head *paths)
2786 const struct got_error *err = NULL;
2787 struct got_pathlist_entry *new_pe;
2789 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2790 if (err)
2791 return err;
2792 if (new_pe == NULL)
2793 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2794 return NULL;
2797 static const struct got_error *
2798 report_ct_status(struct got_commitable *ct,
2799 got_worktree_status_cb status_cb, void *status_arg)
2801 const char *ct_path = ct->path;
2802 while (ct_path[0] == '/')
2803 ct_path++;
2804 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2807 static const struct got_error *
2808 match_modified_subtree(int *modified, struct got_tree_entry *te,
2809 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2811 const struct got_error *err = NULL;
2812 struct got_pathlist_entry *pe;
2813 char *te_path;
2815 *modified = 0;
2817 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2818 got_path_is_root_dir(base_tree_path) ? "" : "/",
2819 te->name) == -1)
2820 return got_error_from_errno("asprintf");
2822 TAILQ_FOREACH(pe, commitable_paths, entry) {
2823 struct got_commitable *ct = pe->data;
2824 *modified = got_path_is_child(ct->in_repo_path, te_path,
2825 strlen(te_path));
2826 if (*modified)
2827 break;
2830 free(te_path);
2831 return err;
2834 static const struct got_error *
2835 match_deleted_or_modified_ct(struct got_commitable **ctp,
2836 struct got_tree_entry *te, const char *base_tree_path,
2837 struct got_pathlist_head *commitable_paths)
2839 const struct got_error *err = NULL;
2840 struct got_pathlist_entry *pe;
2842 *ctp = NULL;
2844 TAILQ_FOREACH(pe, commitable_paths, entry) {
2845 struct got_commitable *ct = pe->data;
2846 char *ct_name = NULL;
2847 int path_matches;
2849 if (ct->status != GOT_STATUS_MODIFY &&
2850 ct->status != GOT_STATUS_DELETE)
2851 continue;
2853 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2854 continue;
2856 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2857 if (err)
2858 return err;
2859 if (!path_matches)
2860 continue;
2862 ct_name = basename(pe->path);
2863 if (ct_name == NULL)
2864 return got_error_from_errno2("basename", pe->path);
2866 if (strcmp(te->name, ct_name) != 0)
2867 continue;
2869 *ctp = ct;
2870 break;
2873 return err;
2876 static const struct got_error *
2877 write_tree(struct got_object_id **new_tree_id,
2878 struct got_tree_object *base_tree, const char *path_base_tree,
2879 struct got_pathlist_head *commitable_paths,
2880 got_worktree_status_cb status_cb, void *status_arg,
2881 struct got_repository *repo)
2883 const struct got_error *err = NULL;
2884 const struct got_tree_entries *base_entries = NULL;
2885 struct got_pathlist_head paths;
2886 struct got_tree_entries new_tree_entries;
2887 struct got_tree_entry *te, *new_te = NULL;
2888 struct got_pathlist_entry *pe;
2890 TAILQ_INIT(&paths);
2891 new_tree_entries.nentries = 0;
2892 SIMPLEQ_INIT(&new_tree_entries.head);
2894 /* Insert, and recurse into, newly added entries first. */
2895 TAILQ_FOREACH(pe, commitable_paths, entry) {
2896 struct got_commitable *ct = pe->data;
2897 char *child_path = NULL, *slash;
2899 if (ct->status != GOT_STATUS_ADD ||
2900 (ct->flags & GOT_COMMITABLE_ADDED))
2901 continue;
2903 if (!got_path_is_child(pe->path, path_base_tree,
2904 strlen(path_base_tree)))
2905 continue;
2907 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2908 pe->path);
2909 if (err)
2910 goto done;
2912 slash = strchr(child_path, '/');
2913 if (slash == NULL) {
2914 err = alloc_added_blob_tree_entry(&new_te, ct);
2915 if (err)
2916 goto done;
2917 err = report_ct_status(ct, status_cb, status_arg);
2918 if (err)
2919 goto done;
2920 ct->flags |= GOT_COMMITABLE_ADDED;
2921 } else {
2922 char *subtree_path;
2924 *slash = '\0'; /* trim trailing path components */
2925 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2926 got_path_is_root_dir(path_base_tree) ? "" : "/",
2927 child_path) == -1) {
2928 err = got_error_from_errno("asprintf");
2929 goto done;
2932 new_te = calloc(1, sizeof(*new_te));
2933 new_te->mode = S_IFDIR;
2934 new_te->name = strdup(child_path);
2935 if (new_te->name == NULL) {
2936 err = got_error_from_errno("strdup");
2937 got_object_tree_entry_close(new_te);
2938 new_te = NULL;
2939 goto done;
2941 err = write_tree(&new_te->id, NULL, subtree_path,
2942 commitable_paths, status_cb, status_arg, repo);
2943 free(subtree_path);
2944 if (err) {
2945 got_object_tree_entry_close(new_te);
2946 new_te = NULL;
2947 goto done;
2950 err = insert_tree_entry(new_te, &paths);
2951 if (err)
2952 goto done;
2955 if (base_tree) {
2956 /* Handle modified and deleted entries. */
2957 base_entries = got_object_tree_get_entries(base_tree);
2958 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2959 struct got_commitable *ct = NULL;
2961 if (S_ISDIR(te->mode)) {
2962 int modified;
2963 err = got_object_tree_entry_dup(&new_te, te);
2964 if (err)
2965 goto done;
2966 err = match_modified_subtree(&modified, te,
2967 path_base_tree, commitable_paths);
2968 if (err)
2969 goto done;
2970 /* Avoid recursion into unmodified subtrees. */
2971 if (modified) {
2972 free(new_te->id);
2973 err = write_subtree(&new_te->id, te,
2974 path_base_tree, commitable_paths,
2975 status_cb, status_arg, repo);
2976 if (err)
2977 goto done;
2979 err = insert_tree_entry(new_te, &paths);
2980 if (err)
2981 goto done;
2982 continue;
2985 err = match_deleted_or_modified_ct(&ct, te,
2986 path_base_tree, commitable_paths);
2987 if (ct) {
2988 /* NB: Deleted entries get dropped here. */
2989 if (ct->status == GOT_STATUS_MODIFY) {
2990 err = alloc_modified_blob_tree_entry(
2991 &new_te, te, ct);
2992 if (err)
2993 goto done;
2994 err = insert_tree_entry(new_te, &paths);
2995 if (err)
2996 goto done;
2998 err = report_ct_status(ct, status_cb,
2999 status_arg);
3000 if (err)
3001 goto done;
3002 } else {
3003 /* Entry is unchanged; just copy it. */
3004 err = got_object_tree_entry_dup(&new_te, te);
3005 if (err)
3006 goto done;
3007 err = insert_tree_entry(new_te, &paths);
3008 if (err)
3009 goto done;
3014 /* Write new list of entries; deleted entries have been dropped. */
3015 TAILQ_FOREACH(pe, &paths, entry) {
3016 struct got_tree_entry *te = pe->data;
3017 new_tree_entries.nentries++;
3018 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3020 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3021 done:
3022 got_object_tree_entries_close(&new_tree_entries);
3023 got_pathlist_free(&paths);
3024 return err;
3027 static const struct got_error *
3028 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3029 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3031 const struct got_error *err = NULL, *sync_err;
3032 char *fileindex_path = NULL;
3033 struct got_fileindex *fileindex = NULL;
3034 struct got_pathlist_entry *pe;
3036 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3037 if (err)
3038 return err;
3040 TAILQ_FOREACH(pe, commitable_paths, entry) {
3041 struct got_fileindex_entry *ie;
3042 struct got_commitable *ct = pe->data;
3044 ie = got_fileindex_entry_get(fileindex, pe->path);
3045 if (ie) {
3046 if (ct->status == GOT_STATUS_DELETE) {
3047 got_fileindex_entry_remove(fileindex, ie);
3048 got_fileindex_entry_free(ie);
3049 } else
3050 err = got_fileindex_entry_update(ie,
3051 ct->ondisk_path, ct->blob_id->sha1,
3052 new_base_commit_id->sha1, 1);
3053 } else {
3054 err = got_fileindex_entry_alloc(&ie,
3055 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3056 new_base_commit_id->sha1);
3057 if (err)
3058 break;
3059 err = got_fileindex_entry_add(fileindex, ie);
3060 if (err)
3061 break;
3064 sync_err = sync_fileindex(fileindex, fileindex_path);
3065 if (sync_err && err == NULL)
3066 err = sync_err;
3067 free(fileindex_path);
3068 got_fileindex_free(fileindex);
3069 return err;
3072 static const struct got_error *
3073 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3074 struct got_object_id *head_commit_id)
3076 const struct got_error *err = NULL;
3077 struct got_object_id *id_in_head = NULL, *id = NULL;
3078 struct got_commit_object *commit = NULL;
3079 char *path = NULL;
3080 const char *ct_path = ct->in_repo_path;
3082 while (ct_path[0] == '/')
3083 ct_path++;
3086 * Ensure that no modifications were made to files *and their parents*
3087 * in commits between the file's base commit and the branch head.
3089 * Checking the parents is important for detecting conflicting tree
3090 * configurations (files or parent folders might have been moved,
3091 * deleted, added again, etc.). Such changes need to be merged with
3092 * local changes before a commit can occur.
3094 * The implication is that the file's (parent) entry in the root
3095 * directory must have the same ID in all relevant commits.
3097 if (ct->status != GOT_STATUS_ADD) {
3098 struct got_object_qid *pid;
3099 char *slash;
3100 struct got_object_id *root_entry_id = NULL;
3102 /* Trivial case: base commit == head commit */
3103 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3104 return NULL;
3106 /* Compute the path to the root directory's entry. */
3107 path = strdup(ct_path);
3108 if (path == NULL) {
3109 err = got_error_from_errno("strdup");
3110 goto done;
3112 slash = strchr(path, '/');
3113 if (slash)
3114 *slash = '\0';
3116 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3117 if (err)
3118 goto done;
3120 err = got_object_id_by_path(&root_entry_id, repo,
3121 head_commit_id, path);
3122 if (err)
3123 goto done;
3125 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3126 while (pid) {
3127 struct got_commit_object *pcommit;
3129 err = got_object_id_by_path(&id, repo, pid->id, path);
3130 if (err) {
3131 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3132 goto done;
3133 err = NULL;
3134 break;
3137 err = got_object_id_by_path(&id, repo, pid->id, path);
3138 if (err)
3139 goto done;
3141 if (got_object_id_cmp(id, root_entry_id) != 0) {
3142 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3143 break;
3146 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3147 break; /* all relevant commits scanned */
3149 err = got_object_open_as_commit(&pcommit, repo,
3150 pid->id);
3151 if (err)
3152 goto done;
3154 got_object_commit_close(commit);
3155 commit = pcommit;
3156 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3157 commit));
3159 } else {
3160 /* Require that added files don't exist in the branch head. */
3161 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3162 ct_path);
3163 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3164 goto done;
3165 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3167 done:
3168 if (commit)
3169 got_object_commit_close(commit);
3170 free(id_in_head);
3171 free(id);
3172 free(path);
3173 return err;
3176 const struct got_error *
3177 got_worktree_commit(struct got_object_id **new_commit_id,
3178 struct got_worktree *worktree, const char *ondisk_path,
3179 const char *author, const char *committer,
3180 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3181 got_worktree_status_cb status_cb, void *status_arg,
3182 struct got_repository *repo)
3184 const struct got_error *err = NULL, *unlockerr = NULL;
3185 struct collect_commitables_arg cc_arg;
3186 struct got_pathlist_head commitable_paths;
3187 struct got_pathlist_entry *pe;
3188 char *relpath = NULL;
3189 const char *head_ref_name = NULL;
3190 struct got_reference *head_ref = NULL;
3191 struct got_commit_object *head_commit = NULL;
3192 struct got_object_id *head_commit_id = NULL;
3193 struct got_reference *head_ref2 = NULL;
3194 struct got_object_id *head_commit_id2 = NULL;
3195 struct got_tree_object *head_tree = NULL;
3196 struct got_object_id *new_tree_id = NULL;
3197 struct got_object_id_queue parent_ids;
3198 struct got_object_qid *pid = NULL;
3199 char *logmsg = NULL;
3201 *new_commit_id = NULL;
3203 TAILQ_INIT(&commitable_paths);
3204 SIMPLEQ_INIT(&parent_ids);
3206 if (ondisk_path) {
3207 err = got_path_skip_common_ancestor(&relpath,
3208 worktree->root_path, ondisk_path);
3209 if (err)
3210 return err;
3213 err = lock_worktree(worktree, LOCK_EX);
3214 if (err)
3215 goto done;
3217 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3218 if (err)
3219 goto done;
3220 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3221 if (err)
3222 goto done;
3224 cc_arg.commitable_paths = &commitable_paths;
3225 cc_arg.worktree = worktree;
3226 cc_arg.repo = repo;
3227 err = got_worktree_status(worktree, relpath ? relpath : "",
3228 repo, collect_commitables, &cc_arg, NULL, NULL);
3229 if (err)
3230 goto done;
3232 if (TAILQ_EMPTY(&commitable_paths)) {
3233 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3234 goto done;
3237 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3238 if (err)
3239 goto done;
3241 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3242 struct got_commitable *ct = pe->data;
3243 err = check_ct_out_of_date(ct, repo, head_commit_id);
3244 if (err)
3245 goto done;
3248 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3249 if (err)
3250 goto done;
3252 if (commit_msg_cb != NULL) {
3253 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3254 if (err)
3255 goto done;
3258 if (logmsg == NULL || strlen(logmsg) == 0) {
3259 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3260 goto done;
3263 /* Create blobs from added and modified files and record their IDs. */
3264 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3265 struct got_commitable *ct = pe->data;
3266 char *ondisk_path;
3268 if (ct->status != GOT_STATUS_ADD &&
3269 ct->status != GOT_STATUS_MODIFY)
3270 continue;
3272 if (asprintf(&ondisk_path, "%s/%s",
3273 worktree->root_path, pe->path) == -1) {
3274 err = got_error_from_errno("asprintf");
3275 goto done;
3277 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3278 free(ondisk_path);
3279 if (err)
3280 goto done;
3283 /* Recursively write new tree objects. */
3284 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3285 status_cb, status_arg, repo);
3286 if (err)
3287 goto done;
3289 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3290 if (err)
3291 goto done;
3292 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3293 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3294 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3295 got_object_qid_free(pid);
3296 if (logmsg != NULL)
3297 free(logmsg);
3298 if (err)
3299 goto done;
3301 /* Check if a concurrent commit to our branch has occurred. */
3302 head_ref_name = got_worktree_get_head_ref_name(worktree);
3303 if (head_ref_name == NULL) {
3304 err = got_error_from_errno("got_worktree_get_head_ref_name");
3305 goto done;
3307 /* Lock the reference here to prevent concurrent modification. */
3308 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3309 if (err)
3310 goto done;
3311 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3312 if (err)
3313 goto done;
3314 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3315 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3316 goto done;
3318 /* Update branch head in repository. */
3319 err = got_ref_change_ref(head_ref2, *new_commit_id);
3320 if (err)
3321 goto done;
3322 err = got_ref_write(head_ref2, repo);
3323 if (err)
3324 goto done;
3326 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3327 if (err)
3328 goto done;
3330 err = ref_base_commit(worktree, repo);
3331 if (err)
3332 goto done;
3334 err = update_fileindex_after_commit(&commitable_paths,
3335 *new_commit_id, worktree);
3336 if (err)
3337 goto done;
3338 done:
3339 unlockerr = lock_worktree(worktree, LOCK_SH);
3340 if (unlockerr && err == NULL)
3341 err = unlockerr;
3342 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3343 struct got_commitable *ct = pe->data;
3344 free_commitable(ct);
3346 got_pathlist_free(&commitable_paths);
3347 if (head_tree)
3348 got_object_tree_close(head_tree);
3349 if (head_commit)
3350 got_object_commit_close(head_commit);
3351 free(relpath);
3352 free(head_commit_id);
3353 free(head_commit_id2);
3354 if (head_ref)
3355 got_ref_close(head_ref);
3356 if (head_ref2) {
3357 unlockerr = got_ref_unlock(head_ref2);
3358 if (unlockerr && err == NULL)
3359 err = unlockerr;
3360 got_ref_close(head_ref2);
3362 return err;
3365 const char *
3366 got_commitable_get_path(struct got_commitable *ct)
3368 return ct->path;
3371 unsigned int
3372 got_commitable_get_status(struct got_commitable *ct)
3374 return ct->status;