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;
66 int fd = -1;
68 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
69 err = got_error_from_errno("asprintf");
70 path = NULL;
71 goto done;
72 }
74 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
75 GOT_DEFAULT_FILE_MODE);
76 if (fd == -1) {
77 err = got_error_from_errno2("open", path);
78 goto done;
79 }
81 if (content) {
82 int len = dprintf(fd, "%s\n", content);
83 if (len != strlen(content) + 1) {
84 err = got_error_from_errno("dprintf");
85 goto done;
86 }
87 }
89 done:
90 if (fd != -1 && close(fd) == -1 && err == NULL)
91 err = got_error_from_errno("close");
92 free(path);
93 return err;
94 }
96 static const struct got_error *
97 update_meta_file(const char *path_got, const char *name, const char *content)
98 {
99 const struct got_error *err = NULL;
100 FILE *tmpfile = NULL;
101 char *tmppath = NULL;
102 char *path = NULL;
104 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
105 err = got_error_from_errno("asprintf");
106 path = NULL;
107 goto done;
110 err = got_opentemp_named(&tmppath, &tmpfile, path);
111 if (err)
112 goto done;
114 if (content) {
115 int len = fprintf(tmpfile, "%s\n", content);
116 if (len != strlen(content) + 1) {
117 err = got_error_from_errno2("fprintf", tmppath);
118 goto done;
122 if (rename(tmppath, path) != 0) {
123 err = got_error_from_errno3("rename", tmppath, path);
124 unlink(tmppath);
125 goto done;
128 done:
129 if (fclose(tmpfile) != 0 && err == NULL)
130 err = got_error_from_errno2("fclose", tmppath);
131 free(tmppath);
132 return err;
135 static const struct got_error *
136 read_meta_file(char **content, const char *path_got, const char *name)
138 const struct got_error *err = NULL;
139 char *path;
140 int fd = -1;
141 ssize_t n;
142 struct stat sb;
144 *content = NULL;
146 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
147 err = got_error_from_errno("asprintf");
148 path = NULL;
149 goto done;
152 fd = open(path, O_RDONLY | O_NOFOLLOW);
153 if (fd == -1) {
154 if (errno == ENOENT)
155 err = got_error(GOT_ERR_WORKTREE_META);
156 else
157 err = got_error_from_errno2("open", path);
158 goto done;
160 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
161 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
162 : got_error_from_errno2("flock", path));
163 goto done;
166 if (lstat(path, &sb) != 0) {
167 err = got_error_from_errno2("lstat", path);
168 goto done;
170 *content = calloc(1, sb.st_size);
171 if (*content == NULL) {
172 err = got_error_from_errno("calloc");
173 goto done;
176 n = read(fd, *content, sb.st_size);
177 if (n != sb.st_size) {
178 err = (n == -1 ? got_error_from_errno2("read", path) :
179 got_error(GOT_ERR_WORKTREE_META));
180 goto done;
182 if ((*content)[sb.st_size - 1] != '\n') {
183 err = got_error(GOT_ERR_WORKTREE_META);
184 goto done;
186 (*content)[sb.st_size - 1] = '\0';
188 done:
189 if (fd != -1 && close(fd) == -1 && err == NULL)
190 err = got_error_from_errno2("close", path_got);
191 free(path);
192 if (err) {
193 free(*content);
194 *content = NULL;
196 return err;
199 static const struct got_error *
200 write_head_ref(const char *path_got, struct got_reference *head_ref)
202 const struct got_error *err = NULL;
203 char *refstr = NULL;
205 if (got_ref_is_symbolic(head_ref)) {
206 refstr = got_ref_to_str(head_ref);
207 if (refstr == NULL)
208 return got_error_from_errno("got_ref_to_str");
209 } else {
210 refstr = strdup(got_ref_get_name(head_ref));
211 if (refstr == NULL)
212 return got_error_from_errno("strdup");
214 err = update_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
215 free(refstr);
216 return err;
219 const struct got_error *
220 got_worktree_init(const char *path, struct got_reference *head_ref,
221 const char *prefix, struct got_repository *repo)
223 const struct got_error *err = NULL;
224 struct got_object_id *commit_id = NULL;
225 uuid_t uuid;
226 uint32_t uuid_status;
227 int obj_type;
228 char *path_got = NULL;
229 char *formatstr = NULL;
230 char *absprefix = NULL;
231 char *basestr = NULL;
232 char *uuidstr = NULL;
234 if (strcmp(path, got_repo_get_path(repo)) == 0) {
235 err = got_error(GOT_ERR_WORKTREE_REPO);
236 goto done;
239 err = got_ref_resolve(&commit_id, repo, head_ref);
240 if (err)
241 return err;
242 err = got_object_get_type(&obj_type, repo, commit_id);
243 if (err)
244 return err;
245 if (obj_type != GOT_OBJ_TYPE_COMMIT)
246 return got_error(GOT_ERR_OBJ_TYPE);
248 if (!got_path_is_absolute(prefix)) {
249 if (asprintf(&absprefix, "/%s", prefix) == -1)
250 return got_error_from_errno("asprintf");
253 /* Create top-level directory (may already exist). */
254 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
255 err = got_error_from_errno2("mkdir", path);
256 goto done;
259 /* Create .got directory (may already exist). */
260 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
261 err = got_error_from_errno("asprintf");
262 goto done;
264 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
265 err = got_error_from_errno2("mkdir", path_got);
266 goto done;
269 /* Create an empty lock file. */
270 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
271 if (err)
272 goto done;
274 /* Create an empty file index. */
275 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
276 if (err)
277 goto done;
279 /* Write the HEAD reference. */
280 err = write_head_ref(path_got, head_ref);
281 if (err)
282 goto done;
284 /* Record our base commit. */
285 err = got_object_id_str(&basestr, commit_id);
286 if (err)
287 goto done;
288 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
289 if (err)
290 goto done;
292 /* Store path to repository. */
293 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
294 got_repo_get_path(repo));
295 if (err)
296 goto done;
298 /* Store in-repository path prefix. */
299 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
300 absprefix ? absprefix : prefix);
301 if (err)
302 goto done;
304 /* Generate UUID. */
305 uuid_create(&uuid, &uuid_status);
306 if (uuid_status != uuid_s_ok) {
307 err = got_error_uuid(uuid_status);
308 goto done;
310 uuid_to_string(&uuid, &uuidstr, &uuid_status);
311 if (uuid_status != uuid_s_ok) {
312 err = got_error_uuid(uuid_status);
313 goto done;
315 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
316 if (err)
317 goto done;
319 /* Stamp work tree with format file. */
320 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
321 err = got_error_from_errno("asprintf");
322 goto done;
324 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
325 if (err)
326 goto done;
328 done:
329 free(commit_id);
330 free(path_got);
331 free(formatstr);
332 free(absprefix);
333 free(basestr);
334 free(uuidstr);
335 return err;
338 static const struct got_error *
339 open_worktree(struct got_worktree **worktree, const char *path)
341 const struct got_error *err = NULL;
342 char *path_got;
343 char *formatstr = NULL;
344 char *uuidstr = NULL;
345 char *path_lock = NULL;
346 char *base_commit_id_str = NULL;
347 int version, fd = -1;
348 const char *errstr;
349 struct got_repository *repo = NULL;
350 uint32_t uuid_status;
352 *worktree = NULL;
354 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
355 err = got_error_from_errno("asprintf");
356 path_got = NULL;
357 goto done;
360 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
361 err = got_error_from_errno("asprintf");
362 path_lock = NULL;
363 goto done;
366 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
367 if (fd == -1) {
368 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
369 : got_error_from_errno2("open", path_lock));
370 goto done;
373 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
374 if (err)
375 goto done;
377 version = strtonum(formatstr, 1, INT_MAX, &errstr);
378 if (errstr) {
379 err = got_error(GOT_ERR_WORKTREE_META);
380 goto done;
382 if (version != GOT_WORKTREE_FORMAT_VERSION) {
383 err = got_error(GOT_ERR_WORKTREE_VERS);
384 goto done;
387 *worktree = calloc(1, sizeof(**worktree));
388 if (*worktree == NULL) {
389 err = got_error_from_errno("calloc");
390 goto done;
392 (*worktree)->lockfd = -1;
394 (*worktree)->root_path = strdup(path);
395 if ((*worktree)->root_path == NULL) {
396 err = got_error_from_errno("strdup");
397 goto done;
399 err = read_meta_file(&(*worktree)->repo_path, path_got,
400 GOT_WORKTREE_REPOSITORY);
401 if (err)
402 goto done;
404 err = read_meta_file(&(*worktree)->path_prefix, path_got,
405 GOT_WORKTREE_PATH_PREFIX);
406 if (err)
407 goto done;
409 err = read_meta_file(&base_commit_id_str, path_got,
410 GOT_WORKTREE_BASE_COMMIT);
411 if (err)
412 goto done;
414 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
415 if (err)
416 goto done;
417 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
418 if (uuid_status != uuid_s_ok) {
419 err = got_error_uuid(uuid_status);
420 goto done;
423 err = got_repo_open(&repo, (*worktree)->repo_path);
424 if (err)
425 goto done;
427 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
428 base_commit_id_str);
429 if (err)
430 goto done;
432 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
433 GOT_WORKTREE_HEAD_REF);
434 done:
435 if (repo)
436 got_repo_close(repo);
437 free(path_got);
438 free(path_lock);
439 free(base_commit_id_str);
440 free(uuidstr);
441 free(formatstr);
442 if (err) {
443 if (fd != -1)
444 close(fd);
445 if (*worktree != NULL)
446 got_worktree_close(*worktree);
447 *worktree = NULL;
448 } else
449 (*worktree)->lockfd = fd;
451 return err;
454 const struct got_error *
455 got_worktree_open(struct got_worktree **worktree, const char *path)
457 const struct got_error *err = NULL;
459 do {
460 err = open_worktree(worktree, path);
461 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
462 return err;
463 if (*worktree)
464 return NULL;
465 path = dirname(path);
466 if (path == NULL)
467 return got_error_from_errno2("dirname", path);
468 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
470 return got_error(GOT_ERR_NOT_WORKTREE);
473 const struct got_error *
474 got_worktree_close(struct got_worktree *worktree)
476 const struct got_error *err = NULL;
477 free(worktree->root_path);
478 free(worktree->repo_path);
479 free(worktree->path_prefix);
480 free(worktree->base_commit_id);
481 free(worktree->head_ref_name);
482 if (worktree->lockfd != -1)
483 if (close(worktree->lockfd) != 0)
484 err = got_error_from_errno2("close",
485 got_worktree_get_root_path(worktree));
486 free(worktree);
487 return err;
490 const char *
491 got_worktree_get_root_path(struct got_worktree *worktree)
493 return worktree->root_path;
496 const char *
497 got_worktree_get_repo_path(struct got_worktree *worktree)
499 return worktree->repo_path;
502 const char *
503 got_worktree_get_path_prefix(struct got_worktree *worktree)
505 return worktree->path_prefix;
508 const struct got_error *
509 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
510 const char *path_prefix)
512 char *absprefix = NULL;
514 if (!got_path_is_absolute(path_prefix)) {
515 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
516 return got_error_from_errno("asprintf");
518 *match = (strcmp(absprefix ? absprefix : path_prefix,
519 worktree->path_prefix) == 0);
520 free(absprefix);
521 return NULL;
524 const char *
525 got_worktree_get_head_ref_name(struct got_worktree *worktree)
527 return worktree->head_ref_name;
530 const struct got_error *
531 got_worktree_set_head_ref(struct got_worktree *worktree,
532 struct got_reference *head_ref)
534 const struct got_error *err = NULL;
535 char *path_got = NULL, *head_ref_name = NULL;
537 if (asprintf(&path_got, "%s/%s", worktree->root_path,
538 GOT_WORKTREE_GOT_DIR) == -1) {
539 err = got_error_from_errno("asprintf");
540 path_got = NULL;
541 goto done;
544 head_ref_name = strdup(got_ref_get_name(head_ref));
545 if (head_ref_name == NULL) {
546 err = got_error_from_errno("strdup");
547 goto done;
550 err = write_head_ref(path_got, head_ref);
551 if (err)
552 goto done;
554 free(worktree->head_ref_name);
555 worktree->head_ref_name = head_ref_name;
556 done:
557 free(path_got);
558 if (err)
559 free(head_ref_name);
560 return err;
563 struct got_object_id *
564 got_worktree_get_base_commit_id(struct got_worktree *worktree)
566 return worktree->base_commit_id;
569 const struct got_error *
570 got_worktree_set_base_commit_id(struct got_worktree *worktree,
571 struct got_repository *repo, struct got_object_id *commit_id)
573 const struct got_error *err;
574 struct got_object *obj = NULL;
575 char *id_str = NULL;
576 char *path_got = NULL;
578 if (asprintf(&path_got, "%s/%s", worktree->root_path,
579 GOT_WORKTREE_GOT_DIR) == -1) {
580 err = got_error_from_errno("asprintf");
581 path_got = NULL;
582 goto done;
585 err = got_object_open(&obj, repo, commit_id);
586 if (err)
587 return err;
589 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
590 err = got_error(GOT_ERR_OBJ_TYPE);
591 goto done;
594 /* Record our base commit. */
595 err = got_object_id_str(&id_str, commit_id);
596 if (err)
597 goto done;
598 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
599 if (err)
600 goto done;
602 free(worktree->base_commit_id);
603 worktree->base_commit_id = got_object_id_dup(commit_id);
604 if (worktree->base_commit_id == NULL) {
605 err = got_error_from_errno("got_object_id_dup");
606 goto done;
608 done:
609 if (obj)
610 got_object_close(obj);
611 free(id_str);
612 free(path_got);
613 return err;
616 static const struct got_error *
617 lock_worktree(struct got_worktree *worktree, int operation)
619 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
620 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
621 : got_error_from_errno2("flock",
622 got_worktree_get_root_path(worktree)));
623 return NULL;
626 static const struct got_error *
627 add_dir_on_disk(struct got_worktree *worktree, const char *path)
629 const struct got_error *err = NULL;
630 char *abspath;
632 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
633 return got_error_from_errno("asprintf");
635 err = got_path_mkdir(abspath);
636 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
637 struct stat sb;
638 err = NULL;
639 if (lstat(abspath, &sb) == -1) {
640 err = got_error_from_errno2("lstat", abspath);
641 } else if (!S_ISDIR(sb.st_mode)) {
642 /* TODO directory is obstructed; do something */
643 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
646 free(abspath);
647 return err;
650 static const struct got_error *
651 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
653 const struct got_error *err = NULL;
654 uint8_t fbuf1[8192];
655 uint8_t fbuf2[8192];
656 size_t flen1 = 0, flen2 = 0;
658 *same = 1;
660 for (;;) {
661 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
662 if (flen1 == 0 && ferror(f1)) {
663 err = got_error_from_errno("fread");
664 break;
666 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
667 if (flen2 == 0 && ferror(f2)) {
668 err = got_error_from_errno("fread");
669 break;
671 if (flen1 == 0) {
672 if (flen2 != 0)
673 *same = 0;
674 break;
675 } else if (flen2 == 0) {
676 if (flen1 != 0)
677 *same = 0;
678 break;
679 } else if (flen1 == flen2) {
680 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
681 *same = 0;
682 break;
684 } else {
685 *same = 0;
686 break;
690 return err;
693 static const struct got_error *
694 check_files_equal(int *same, const char *f1_path, const char *f2_path)
696 const struct got_error *err = NULL;
697 struct stat sb;
698 size_t size1, size2;
699 FILE *f1 = NULL, *f2 = NULL;
701 *same = 1;
703 if (lstat(f1_path, &sb) != 0) {
704 err = got_error_from_errno2("lstat", f1_path);
705 goto done;
707 size1 = sb.st_size;
709 if (lstat(f2_path, &sb) != 0) {
710 err = got_error_from_errno2("lstat", f2_path);
711 goto done;
713 size2 = sb.st_size;
715 if (size1 != size2) {
716 *same = 0;
717 return NULL;
720 f1 = fopen(f1_path, "r");
721 if (f1 == NULL)
722 return got_error_from_errno2("open", f1_path);
724 f2 = fopen(f2_path, "r");
725 if (f2 == NULL) {
726 err = got_error_from_errno2("open", f2_path);
727 goto done;
730 err = check_file_contents_equal(same, f1, f2);
731 done:
732 if (f1 && fclose(f1) != 0 && err == NULL)
733 err = got_error_from_errno("fclose");
734 if (f2 && fclose(f2) != 0 && err == NULL)
735 err = got_error_from_errno("fclose");
737 return err;
740 /*
741 * Perform a 3-way merge where blob_orig acts as the common ancestor,
742 * blob_deriv acts as the first derived version, and the file on disk
743 * acts as the second derived version.
744 */
745 static const struct got_error *
746 merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
747 struct got_blob_object *blob_orig, const char *ondisk_path,
748 const char *path, uint16_t st_mode, struct got_blob_object *blob_deriv,
749 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
750 void *progress_arg)
752 const struct got_error *err = NULL;
753 int merged_fd = -1;
754 FILE *f_deriv = NULL, *f_orig = NULL;
755 char *blob_deriv_path = NULL, *blob_orig_path = NULL;
756 char *merged_path = NULL, *base_path = NULL;
757 char *id_str = NULL;
758 char *label1 = NULL;
759 int overlapcnt = 0;
760 char *parent;
762 *local_changes_subsumed = 0;
764 parent = dirname(ondisk_path);
765 if (parent == NULL)
766 return got_error_from_errno2("dirname", ondisk_path);
768 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
769 return got_error_from_errno("asprintf");
771 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
772 if (err)
773 goto done;
775 free(base_path);
776 if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
777 err = got_error_from_errno("asprintf");
778 base_path = NULL;
779 goto done;
782 err = got_opentemp_named(&blob_deriv_path, &f_deriv, base_path);
783 if (err)
784 goto done;
785 err = got_object_blob_dump_to_file(NULL, NULL, f_deriv, blob_deriv);
786 if (err)
787 goto done;
789 free(base_path);
790 if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
791 err = got_error_from_errno("asprintf");
792 base_path = NULL;
793 goto done;
796 err = got_opentemp_named(&blob_orig_path, &f_orig, base_path);
797 if (err)
798 goto done;
799 if (blob_orig) {
800 err = got_object_blob_dump_to_file(NULL, NULL, f_orig,
801 blob_orig);
802 if (err)
803 goto done;
804 } else {
805 /*
806 * If the file has no blob, this is an "add vs add" conflict,
807 * and we simply use an empty ancestor file to make both files
808 * appear in the merged result in their entirety.
809 */
812 err = got_object_id_str(&id_str, worktree->base_commit_id);
813 if (err)
814 goto done;
815 if (asprintf(&label1, "commit %s", id_str) == -1) {
816 err = got_error_from_errno("asprintf");
817 goto done;
820 err = got_merge_diff3(&overlapcnt, merged_fd, blob_deriv_path,
821 blob_orig_path, ondisk_path, label1, path);
822 if (err)
823 goto done;
825 (*progress_cb)(progress_arg,
826 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
828 if (fsync(merged_fd) != 0) {
829 err = got_error_from_errno("fsync");
830 goto done;
833 /* Check if a clean merge has subsumed all local changes. */
834 if (overlapcnt == 0) {
835 err = check_files_equal(local_changes_subsumed, blob_deriv_path,
836 merged_path);
837 if (err)
838 goto done;
841 if (chmod(merged_path, st_mode) != 0) {
842 err = got_error_from_errno2("chmod", merged_path);
843 goto done;
846 if (rename(merged_path, ondisk_path) != 0) {
847 err = got_error_from_errno3("rename", merged_path,
848 ondisk_path);
849 unlink(merged_path);
850 goto done;
853 done:
854 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
855 err = got_error_from_errno("close");
856 if (f_deriv && fclose(f_deriv) != 0 && err == NULL)
857 err = got_error_from_errno("fclose");
858 if (f_orig && fclose(f_orig) != 0 && err == NULL)
859 err = got_error_from_errno("fclose");
860 free(merged_path);
861 free(base_path);
862 if (blob_deriv_path) {
863 unlink(blob_deriv_path);
864 free(blob_deriv_path);
866 if (blob_orig_path) {
867 unlink(blob_orig_path);
868 free(blob_orig_path);
870 free(id_str);
871 free(label1);
872 return err;
875 static const struct got_error *
876 update_blob_fileindex_entry(struct got_worktree *worktree,
877 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
878 const char *ondisk_path, const char *path, struct got_blob_object *blob,
879 int update_timestamps)
881 const struct got_error *err = NULL;
883 if (ie == NULL)
884 ie = got_fileindex_entry_get(fileindex, path);
885 if (ie)
886 err = got_fileindex_entry_update(ie, ondisk_path,
887 blob->id.sha1, worktree->base_commit_id->sha1,
888 update_timestamps);
889 else {
890 struct got_fileindex_entry *new_ie;
891 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
892 path, blob->id.sha1, worktree->base_commit_id->sha1);
893 if (!err)
894 err = got_fileindex_entry_add(fileindex, new_ie);
896 return err;
899 static const struct got_error *
900 install_blob(struct got_worktree *worktree, const char *ondisk_path,
901 const char *path, uint16_t te_mode, uint16_t st_mode,
902 struct got_blob_object *blob, int restoring_missing_file,
903 int reverting_versioned_file, struct got_repository *repo,
904 got_worktree_checkout_cb progress_cb, void *progress_arg)
906 const struct got_error *err = NULL;
907 int fd = -1;
908 size_t len, hdrlen;
909 int update = 0;
910 char *tmppath = NULL;
912 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
913 GOT_DEFAULT_FILE_MODE);
914 if (fd == -1) {
915 if (errno == ENOENT) {
916 char *parent = dirname(path);
917 if (parent == NULL)
918 return got_error_from_errno2("dirname", path);
919 err = add_dir_on_disk(worktree, parent);
920 if (err)
921 return err;
922 fd = open(ondisk_path,
923 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
924 GOT_DEFAULT_FILE_MODE);
925 if (fd == -1)
926 return got_error_from_errno2("open",
927 ondisk_path);
928 } else if (errno == EEXIST) {
929 if (!S_ISREG(st_mode)) {
930 /* TODO file is obstructed; do something */
931 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
932 goto done;
933 } else {
934 err = got_opentemp_named_fd(&tmppath, &fd,
935 ondisk_path);
936 if (err)
937 goto done;
938 update = 1;
940 } else
941 return got_error_from_errno2("open", ondisk_path);
944 if (restoring_missing_file)
945 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
946 else if (reverting_versioned_file)
947 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
948 else
949 (*progress_cb)(progress_arg,
950 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
952 hdrlen = got_object_blob_get_hdrlen(blob);
953 do {
954 const uint8_t *buf = got_object_blob_get_read_buf(blob);
955 err = got_object_blob_read_block(&len, blob);
956 if (err)
957 break;
958 if (len > 0) {
959 /* Skip blob object header first time around. */
960 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
961 if (outlen == -1) {
962 err = got_error_from_errno("write");
963 goto done;
964 } else if (outlen != len - hdrlen) {
965 err = got_error(GOT_ERR_IO);
966 goto done;
968 hdrlen = 0;
970 } while (len != 0);
972 if (fsync(fd) != 0) {
973 err = got_error_from_errno("fsync");
974 goto done;
977 if (update) {
978 if (rename(tmppath, ondisk_path) != 0) {
979 err = got_error_from_errno3("rename", tmppath,
980 ondisk_path);
981 unlink(tmppath);
982 goto done;
986 if (te_mode & S_IXUSR) {
987 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
988 err = got_error_from_errno2("chmod", ondisk_path);
989 goto done;
991 } else {
992 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
993 err = got_error_from_errno2("chmod", ondisk_path);
994 goto done;
998 done:
999 if (fd != -1 && close(fd) != 0 && err == NULL)
1000 err = got_error_from_errno("close");
1001 free(tmppath);
1002 return err;
1005 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1006 static const struct got_error *
1007 get_modified_file_content_status(unsigned char *status, FILE *f)
1009 const struct got_error *err = NULL;
1010 const char *markers[3] = {
1011 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1012 GOT_DIFF_CONFLICT_MARKER_SEP,
1013 GOT_DIFF_CONFLICT_MARKER_END
1015 int i = 0;
1016 char *line;
1017 size_t len;
1018 const char delim[3] = {'\0', '\0', '\0'};
1020 while (*status == GOT_STATUS_MODIFY) {
1021 line = fparseln(f, &len, NULL, delim, 0);
1022 if (line == NULL) {
1023 if (feof(f))
1024 break;
1025 err = got_ferror(f, GOT_ERR_IO);
1026 break;
1029 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1030 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1031 == 0)
1032 *status = GOT_STATUS_CONFLICT;
1033 else
1034 i++;
1038 return err;
1041 static const struct got_error *
1042 get_file_status(unsigned char *status, struct stat *sb,
1043 struct got_fileindex_entry *ie, const char *abspath,
1044 struct got_repository *repo)
1046 const struct got_error *err = NULL;
1047 struct got_object_id id;
1048 size_t hdrlen;
1049 FILE *f = NULL;
1050 uint8_t fbuf[8192];
1051 struct got_blob_object *blob = NULL;
1052 size_t flen, blen;
1054 *status = GOT_STATUS_NO_CHANGE;
1056 if (lstat(abspath, sb) == -1) {
1057 if (errno == ENOENT) {
1058 if (ie) {
1059 if (got_fileindex_entry_has_file_on_disk(ie))
1060 *status = GOT_STATUS_MISSING;
1061 else
1062 *status = GOT_STATUS_DELETE;
1063 sb->st_mode =
1064 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1065 & (S_IRWXU | S_IRWXG | S_IRWXO));
1066 } else
1067 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1068 return NULL;
1070 return got_error_from_errno2("lstat", abspath);
1073 if (!S_ISREG(sb->st_mode)) {
1074 *status = GOT_STATUS_OBSTRUCTED;
1075 return NULL;
1078 if (ie == NULL)
1079 return NULL;
1081 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1082 *status = GOT_STATUS_DELETE;
1083 return NULL;
1084 } else if (!got_fileindex_entry_has_blob(ie)) {
1085 *status = GOT_STATUS_ADD;
1086 return NULL;
1089 if (ie->ctime_sec == sb->st_ctime &&
1090 ie->ctime_nsec == sb->st_ctimensec &&
1091 ie->mtime_sec == sb->st_mtime &&
1092 ie->mtime_sec == sb->st_mtime &&
1093 ie->mtime_nsec == sb->st_mtimensec &&
1094 ie->size == (sb->st_size & 0xffffffff))
1095 return NULL;
1097 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1098 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1099 if (err)
1100 return err;
1102 f = fopen(abspath, "r");
1103 if (f == NULL) {
1104 err = got_error_from_errno2("fopen", abspath);
1105 goto done;
1107 hdrlen = got_object_blob_get_hdrlen(blob);
1108 for (;;) {
1109 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1110 err = got_object_blob_read_block(&blen, blob);
1111 if (err)
1112 goto done;
1113 /* Skip length of blob object header first time around. */
1114 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1115 if (flen == 0 && ferror(f)) {
1116 err = got_error_from_errno("fread");
1117 goto done;
1119 if (blen == 0) {
1120 if (flen != 0)
1121 *status = GOT_STATUS_MODIFY;
1122 break;
1123 } else if (flen == 0) {
1124 if (blen != 0)
1125 *status = GOT_STATUS_MODIFY;
1126 break;
1127 } else if (blen - hdrlen == flen) {
1128 /* Skip blob object header first time around. */
1129 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1130 *status = GOT_STATUS_MODIFY;
1131 break;
1133 } else {
1134 *status = GOT_STATUS_MODIFY;
1135 break;
1137 hdrlen = 0;
1140 if (*status == GOT_STATUS_MODIFY) {
1141 rewind(f);
1142 err = get_modified_file_content_status(status, f);
1144 done:
1145 if (blob)
1146 got_object_blob_close(blob);
1147 if (f)
1148 fclose(f);
1149 return err;
1152 static const struct got_error *
1153 update_blob(struct got_worktree *worktree,
1154 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1155 struct got_tree_entry *te, const char *path,
1156 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1157 void *progress_arg)
1159 const struct got_error *err = NULL;
1160 struct got_blob_object *blob = NULL;
1161 char *ondisk_path;
1162 unsigned char status = GOT_STATUS_NO_CHANGE;
1163 struct stat sb;
1165 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1166 return got_error_from_errno("asprintf");
1168 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1169 if (err)
1170 goto done;
1172 if (status == GOT_STATUS_OBSTRUCTED) {
1173 (*progress_cb)(progress_arg, status, path);
1174 goto done;
1177 if (ie && status != GOT_STATUS_MISSING) {
1178 if (got_fileindex_entry_has_commit(ie) &&
1179 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1180 SHA1_DIGEST_LENGTH) == 0) {
1181 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1182 path);
1183 goto done;
1185 if (got_fileindex_entry_has_blob(ie) &&
1186 memcmp(ie->blob_sha1, te->id->sha1,
1187 SHA1_DIGEST_LENGTH) == 0)
1188 goto done;
1191 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1192 if (err)
1193 goto done;
1195 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1196 int update_timestamps;
1197 struct got_blob_object *blob2 = NULL;
1198 if (got_fileindex_entry_has_blob(ie)) {
1199 struct got_object_id id2;
1200 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1201 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1202 if (err)
1203 goto done;
1205 err = merge_blob(&update_timestamps, worktree, blob2,
1206 ondisk_path, path, sb.st_mode, blob, repo,
1207 progress_cb, progress_arg);
1208 if (blob2)
1209 got_object_blob_close(blob2);
1211 * Do not update timestamps of files with local changes.
1212 * Otherwise, a future status walk would treat them as
1213 * unmodified files again.
1215 err = got_fileindex_entry_update(ie, ondisk_path,
1216 blob->id.sha1, worktree->base_commit_id->sha1,
1217 update_timestamps);
1218 } else if (status == GOT_STATUS_DELETE) {
1219 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1220 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1221 ondisk_path, path, blob, 0);
1222 if (err)
1223 goto done;
1224 } else {
1225 err = install_blob(worktree, ondisk_path, path, te->mode,
1226 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1227 repo, progress_cb, progress_arg);
1228 if (err)
1229 goto done;
1230 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1231 ondisk_path, path, blob, 1);
1232 if (err)
1233 goto done;
1235 got_object_blob_close(blob);
1236 done:
1237 free(ondisk_path);
1238 return err;
1241 static const struct got_error *
1242 remove_ondisk_file(const char *root_path, const char *path)
1244 const struct got_error *err = NULL;
1245 char *ondisk_path = NULL;
1247 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1248 return got_error_from_errno("asprintf");
1250 if (unlink(ondisk_path) == -1) {
1251 if (errno != ENOENT)
1252 err = got_error_from_errno2("unlink", ondisk_path);
1253 } else {
1254 char *parent = dirname(ondisk_path);
1255 while (parent && strcmp(parent, root_path) != 0) {
1256 if (rmdir(parent) == -1) {
1257 if (errno != ENOTEMPTY)
1258 err = got_error_from_errno2("rmdir",
1259 parent);
1260 break;
1262 parent = dirname(parent);
1265 free(ondisk_path);
1266 return err;
1269 static const struct got_error *
1270 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1271 struct got_fileindex_entry *ie, struct got_repository *repo,
1272 got_worktree_checkout_cb progress_cb, void *progress_arg)
1274 const struct got_error *err = NULL;
1275 unsigned char status;
1276 struct stat sb;
1277 char *ondisk_path;
1279 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1280 == -1)
1281 return got_error_from_errno("asprintf");
1283 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1284 if (err)
1285 return err;
1287 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1288 status == GOT_STATUS_ADD) {
1289 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1291 * Preserve the working file and change the deleted blob's
1292 * entry into a schedule-add entry.
1294 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1295 0);
1296 if (err)
1297 return err;
1298 } else {
1299 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1300 if (status == GOT_STATUS_NO_CHANGE) {
1301 err = remove_ondisk_file(worktree->root_path, ie->path);
1302 if (err)
1303 return err;
1305 got_fileindex_entry_remove(fileindex, ie);
1308 return err;
1311 struct diff_cb_arg {
1312 struct got_fileindex *fileindex;
1313 struct got_worktree *worktree;
1314 struct got_repository *repo;
1315 got_worktree_checkout_cb progress_cb;
1316 void *progress_arg;
1317 got_worktree_cancel_cb cancel_cb;
1318 void *cancel_arg;
1321 static const struct got_error *
1322 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1323 struct got_tree_entry *te, const char *parent_path)
1325 struct diff_cb_arg *a = arg;
1327 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1328 return got_error(GOT_ERR_CANCELLED);
1330 return update_blob(a->worktree, a->fileindex, ie, te,
1331 ie->path, a->repo, a->progress_cb, a->progress_arg);
1334 static const struct got_error *
1335 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1337 struct diff_cb_arg *a = arg;
1339 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1340 return got_error(GOT_ERR_CANCELLED);
1342 return delete_blob(a->worktree, a->fileindex, ie,
1343 a->repo, a->progress_cb, a->progress_arg);
1346 static const struct got_error *
1347 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1349 struct diff_cb_arg *a = arg;
1350 const struct got_error *err;
1351 char *path;
1353 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1354 return got_error(GOT_ERR_CANCELLED);
1356 if (asprintf(&path, "%s%s%s", parent_path,
1357 parent_path[0] ? "/" : "", te->name)
1358 == -1)
1359 return got_error_from_errno("asprintf");
1361 if (S_ISDIR(te->mode))
1362 err = add_dir_on_disk(a->worktree, path);
1363 else
1364 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1365 a->repo, a->progress_cb, a->progress_arg);
1367 free(path);
1368 return err;
1371 const struct got_error *
1372 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1374 const struct got_error *err = NULL;
1375 char *uuidstr = NULL;
1376 uint32_t uuid_status;
1378 *refname = NULL;
1380 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1381 if (uuid_status != uuid_s_ok)
1382 return got_error_uuid(uuid_status);
1384 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1385 == -1) {
1386 err = got_error_from_errno("asprintf");
1387 *refname = NULL;
1389 free(uuidstr);
1390 return err;
1394 * Prevent Git's garbage collector from deleting our base commit by
1395 * setting a reference to our base commit's ID.
1397 static const struct got_error *
1398 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1400 const struct got_error *err = NULL;
1401 struct got_reference *ref = NULL;
1402 char *refname;
1404 err = got_worktree_get_base_ref_name(&refname, worktree);
1405 if (err)
1406 return err;
1408 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1409 if (err)
1410 goto done;
1412 err = got_ref_write(ref, repo);
1413 done:
1414 free(refname);
1415 if (ref)
1416 got_ref_close(ref);
1417 return err;
1420 static const struct got_error *
1421 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1422 struct got_worktree *worktree)
1424 const struct got_error *err = NULL;
1425 FILE *index = NULL;
1427 *fileindex_path = NULL;
1428 *fileindex = got_fileindex_alloc();
1429 if (*fileindex == NULL)
1430 return got_error_from_errno("got_fileindex_alloc");
1432 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1433 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1434 err = got_error_from_errno("asprintf");
1435 *fileindex_path = NULL;
1436 goto done;
1439 index = fopen(*fileindex_path, "rb");
1440 if (index == NULL) {
1441 if (errno != ENOENT)
1442 err = got_error_from_errno2("fopen", *fileindex_path);
1443 } else {
1444 err = got_fileindex_read(*fileindex, index);
1445 if (fclose(index) != 0 && err == NULL)
1446 err = got_error_from_errno("fclose");
1448 done:
1449 if (err) {
1450 free(*fileindex_path);
1451 *fileindex_path = NULL;
1452 free(*fileindex);
1453 *fileindex = NULL;
1455 return err;
1458 struct bump_base_commit_id_arg {
1459 struct got_object_id *base_commit_id;
1460 const char *path;
1461 size_t path_len;
1462 const char *entry_name;
1465 /* Bump base commit ID of all files within an updated part of the work tree. */
1466 static const struct got_error *
1467 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1469 struct bump_base_commit_id_arg *a = arg;
1471 if (a->entry_name) {
1472 if (strcmp(ie->path, a->path) != 0)
1473 return NULL;
1474 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1475 return NULL;
1477 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1478 return NULL;
1481 static const struct got_error *
1482 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1484 const struct got_error *err = NULL;
1485 char *new_fileindex_path = NULL;
1486 FILE *new_index = NULL;
1488 err = got_opentemp_named(&new_fileindex_path, &new_index,
1489 fileindex_path);
1490 if (err)
1491 goto done;
1493 err = got_fileindex_write(fileindex, new_index);
1494 if (err)
1495 goto done;
1497 if (rename(new_fileindex_path, fileindex_path) != 0) {
1498 err = got_error_from_errno3("rename", new_fileindex_path,
1499 fileindex_path);
1500 unlink(new_fileindex_path);
1502 done:
1503 if (new_index)
1504 fclose(new_index);
1505 free(new_fileindex_path);
1506 return err;
1509 const struct got_error *
1510 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1511 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1512 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1514 const struct got_error *err = NULL, *sync_err, *unlockerr;
1515 struct got_commit_object *commit = NULL;
1516 struct got_object_id *tree_id = NULL;
1517 struct got_tree_object *tree = NULL;
1518 struct got_fileindex *fileindex = NULL;
1519 char *fileindex_path = NULL;
1520 struct got_fileindex_diff_tree_cb diff_cb;
1521 struct diff_cb_arg arg;
1522 char *relpath = NULL, *entry_name = NULL;
1523 struct bump_base_commit_id_arg bbc_arg;
1525 err = lock_worktree(worktree, LOCK_EX);
1526 if (err)
1527 return err;
1530 * Read the file index.
1531 * Checking out files is supposed to be an idempotent operation.
1532 * If the on-disk file index is incomplete we will try to complete it.
1534 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1535 if (err)
1536 goto done;
1538 err = ref_base_commit(worktree, repo);
1539 if (err)
1540 goto done;
1542 err = got_object_open_as_commit(&commit, repo,
1543 worktree->base_commit_id);
1544 if (err)
1545 goto done;
1547 if (path[0]) {
1548 char *tree_path;
1549 int obj_type;
1550 relpath = strdup(path);
1551 if (relpath == NULL) {
1552 err = got_error_from_errno("strdup");
1553 goto done;
1555 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1556 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1557 path) == -1) {
1558 err = got_error_from_errno("asprintf");
1559 goto done;
1561 err = got_object_id_by_path(&tree_id, repo,
1562 worktree->base_commit_id, tree_path);
1563 free(tree_path);
1564 if (err)
1565 goto done;
1566 err = got_object_get_type(&obj_type, repo, tree_id);
1567 if (err)
1568 goto done;
1569 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1570 /* Split provided path into parent dir + entry name. */
1571 if (strchr(path, '/') == NULL) {
1572 relpath = strdup("");
1573 if (relpath == NULL) {
1574 err = got_error_from_errno("strdup");
1575 goto done;
1577 tree_path = strdup(worktree->path_prefix);
1578 if (tree_path == NULL) {
1579 err = got_error_from_errno("strdup");
1580 goto done;
1582 } else {
1583 err = got_path_dirname(&relpath, path);
1584 if (err)
1585 goto done;
1586 if (asprintf(&tree_path, "%s%s%s",
1587 worktree->path_prefix,
1588 got_path_is_root_dir(
1589 worktree->path_prefix) ? "" : "/",
1590 relpath) == -1) {
1591 err = got_error_from_errno("asprintf");
1592 goto done;
1595 err = got_object_id_by_path(&tree_id, repo,
1596 worktree->base_commit_id, tree_path);
1597 free(tree_path);
1598 if (err)
1599 goto done;
1600 entry_name = basename(path);
1601 if (entry_name == NULL) {
1602 err = got_error_from_errno2("basename", path);
1603 goto done;
1606 } else {
1607 relpath = strdup("");
1608 if (relpath == NULL) {
1609 err = got_error_from_errno("strdup");
1610 goto done;
1612 err = got_object_id_by_path(&tree_id, repo,
1613 worktree->base_commit_id, worktree->path_prefix);
1614 if (err)
1615 goto done;
1618 err = got_object_open_as_tree(&tree, repo, tree_id);
1619 if (err)
1620 goto done;
1622 if (entry_name &&
1623 got_object_tree_find_entry(tree, entry_name) == NULL) {
1624 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1625 goto done;
1628 diff_cb.diff_old_new = diff_old_new;
1629 diff_cb.diff_old = diff_old;
1630 diff_cb.diff_new = diff_new;
1631 arg.fileindex = fileindex;
1632 arg.worktree = worktree;
1633 arg.repo = repo;
1634 arg.progress_cb = progress_cb;
1635 arg.progress_arg = progress_arg;
1636 arg.cancel_cb = cancel_cb;
1637 arg.cancel_arg = cancel_arg;
1638 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1639 entry_name, repo, &diff_cb, &arg);
1640 if (err)
1641 goto sync;
1643 bbc_arg.base_commit_id = worktree->base_commit_id;
1644 bbc_arg.entry_name = entry_name;
1645 bbc_arg.path = path;
1646 bbc_arg.path_len = strlen(path);
1647 err = got_fileindex_for_each_entry_safe(fileindex,
1648 bump_base_commit_id, &bbc_arg);
1649 sync:
1650 sync_err = sync_fileindex(fileindex, fileindex_path);
1651 if (sync_err && err == NULL)
1652 err = sync_err;
1653 done:
1654 free(fileindex_path);
1655 free(relpath);
1656 if (tree)
1657 got_object_tree_close(tree);
1658 if (commit)
1659 got_object_commit_close(commit);
1660 got_fileindex_free(fileindex);
1661 unlockerr = lock_worktree(worktree, LOCK_SH);
1662 if (unlockerr && err == NULL)
1663 err = unlockerr;
1664 return err;
1667 struct merge_file_cb_arg {
1668 struct got_worktree *worktree;
1669 struct got_fileindex *fileindex;
1670 got_worktree_checkout_cb progress_cb;
1671 void *progress_arg;
1672 got_worktree_cancel_cb cancel_cb;
1673 void *cancel_arg;
1676 static const struct got_error *
1677 merge_file_cb(void *arg, struct got_blob_object *blob1,
1678 struct got_blob_object *blob2, struct got_object_id *id1,
1679 struct got_object_id *id2, const char *path1, const char *path2,
1680 struct got_repository *repo)
1682 static const struct got_error *err = NULL;
1683 struct merge_file_cb_arg *a = arg;
1684 struct got_fileindex_entry *ie;
1685 char *ondisk_path = NULL;
1686 struct stat sb;
1687 unsigned char status;
1688 int local_changes_subsumed;
1690 if (blob1 && blob2) {
1691 ie = got_fileindex_entry_get(a->fileindex, path2);
1692 if (ie == NULL) {
1693 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1694 path2);
1695 return NULL;
1698 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1699 path2) == -1)
1700 return got_error_from_errno("asprintf");
1702 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1703 if (err)
1704 goto done;
1706 if (status == GOT_STATUS_DELETE) {
1707 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1708 path2);
1709 goto done;
1711 if (status != GOT_STATUS_NO_CHANGE &&
1712 status != GOT_STATUS_MODIFY &&
1713 status != GOT_STATUS_CONFLICT &&
1714 status != GOT_STATUS_ADD) {
1715 (*a->progress_cb)(a->progress_arg, status, path2);
1716 goto done;
1719 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1720 ondisk_path, path2, sb.st_mode, blob2, repo,
1721 a->progress_cb, a->progress_arg);
1722 } else if (blob1) {
1723 ie = got_fileindex_entry_get(a->fileindex, path1);
1724 if (ie == NULL) {
1725 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1726 path2);
1727 return NULL;
1730 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1731 path1) == -1)
1732 return got_error_from_errno("asprintf");
1734 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1735 if (err)
1736 goto done;
1738 switch (status) {
1739 case GOT_STATUS_NO_CHANGE:
1740 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1741 path1);
1742 err = remove_ondisk_file(a->worktree->root_path, path1);
1743 if (err)
1744 goto done;
1745 if (ie)
1746 got_fileindex_entry_mark_deleted_from_disk(ie);
1747 break;
1748 case GOT_STATUS_DELETE:
1749 case GOT_STATUS_MISSING:
1750 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1751 path1);
1752 if (ie)
1753 got_fileindex_entry_mark_deleted_from_disk(ie);
1754 break;
1755 case GOT_STATUS_ADD:
1756 case GOT_STATUS_MODIFY:
1757 case GOT_STATUS_CONFLICT:
1758 (*a->progress_cb)(a->progress_arg,
1759 GOT_STATUS_CANNOT_DELETE, path1);
1760 break;
1761 case GOT_STATUS_OBSTRUCTED:
1762 (*a->progress_cb)(a->progress_arg, status, path1);
1763 break;
1764 default:
1765 break;
1767 } else if (blob2) {
1768 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1769 path2) == -1)
1770 return got_error_from_errno("asprintf");
1771 ie = got_fileindex_entry_get(a->fileindex, path2);
1772 if (ie) {
1773 err = get_file_status(&status, &sb, ie, ondisk_path,
1774 repo);
1775 if (err)
1776 goto done;
1777 if (status != GOT_STATUS_NO_CHANGE &&
1778 status != GOT_STATUS_MODIFY &&
1779 status != GOT_STATUS_CONFLICT &&
1780 status != GOT_STATUS_ADD) {
1781 (*a->progress_cb)(a->progress_arg, status,
1782 path2);
1783 goto done;
1785 err = merge_blob(&local_changes_subsumed, a->worktree,
1786 NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1787 a->progress_cb, a->progress_arg);
1788 if (status == GOT_STATUS_DELETE) {
1789 err = update_blob_fileindex_entry(a->worktree,
1790 a->fileindex, ie, ondisk_path, ie->path,
1791 blob2, 0);
1792 if (err)
1793 goto done;
1795 } else {
1796 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1797 err = install_blob(a->worktree, ondisk_path, path2,
1798 /* XXX get this from parent tree! */
1799 GOT_DEFAULT_FILE_MODE,
1800 sb.st_mode, blob2, 0, 0, repo,
1801 a->progress_cb, a->progress_arg);
1802 if (err)
1803 goto done;
1804 err = got_fileindex_entry_alloc(&ie,
1805 ondisk_path, path2, NULL, NULL);
1806 if (err)
1807 goto done;
1808 err = got_fileindex_entry_add(a->fileindex, ie);
1809 if (err) {
1810 got_fileindex_entry_free(ie);
1811 goto done;
1815 done:
1816 free(ondisk_path);
1817 return err;
1820 struct check_merge_ok_arg {
1821 struct got_worktree *worktree;
1822 struct got_repository *repo;
1825 static const struct got_error *
1826 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1828 const struct got_error *err = NULL;
1829 struct check_merge_ok_arg *a = arg;
1830 unsigned char status;
1831 struct stat sb;
1832 char *ondisk_path;
1834 /* Reject merges into a work tree with mixed base commits. */
1835 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1836 SHA1_DIGEST_LENGTH))
1837 return got_error(GOT_ERR_MIXED_COMMITS);
1839 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1840 == -1)
1841 return got_error_from_errno("asprintf");
1843 /* Reject merges into a work tree with conflicted files. */
1844 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1845 if (err)
1846 return err;
1847 if (status == GOT_STATUS_CONFLICT)
1848 return got_error(GOT_ERR_CONFLICTS);
1850 return NULL;
1853 const struct got_error *
1854 got_worktree_merge_files(struct got_worktree *worktree,
1855 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1856 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1857 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1859 const struct got_error *err = NULL, *sync_err, *unlockerr;
1860 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1861 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1862 struct merge_file_cb_arg arg;
1863 char *fileindex_path = NULL;
1864 struct got_fileindex *fileindex = NULL;
1865 struct check_merge_ok_arg mok_arg;
1867 err = lock_worktree(worktree, LOCK_EX);
1868 if (err)
1869 return err;
1871 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1872 if (err)
1873 goto done;
1875 mok_arg.worktree = worktree;
1876 mok_arg.repo = repo;
1877 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1878 &mok_arg);
1879 if (err)
1880 goto done;
1882 if (commit_id1) {
1883 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1884 worktree->path_prefix);
1885 if (err)
1886 goto done;
1888 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1889 if (err)
1890 goto done;
1893 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1894 worktree->path_prefix);
1895 if (err)
1896 goto done;
1898 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1899 if (err)
1900 goto done;
1902 arg.worktree = worktree;
1903 arg.fileindex = fileindex;
1904 arg.progress_cb = progress_cb;
1905 arg.progress_arg = progress_arg;
1906 arg.cancel_cb = cancel_cb;
1907 arg.cancel_arg = cancel_arg;
1908 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1909 sync_err = sync_fileindex(fileindex, fileindex_path);
1910 if (sync_err && err == NULL)
1911 err = sync_err;
1912 done:
1913 got_fileindex_free(fileindex);
1914 if (tree1)
1915 got_object_tree_close(tree1);
1916 if (tree2)
1917 got_object_tree_close(tree2);
1919 unlockerr = lock_worktree(worktree, LOCK_SH);
1920 if (unlockerr && err == NULL)
1921 err = unlockerr;
1922 return err;
1925 struct diff_dir_cb_arg {
1926 struct got_fileindex *fileindex;
1927 struct got_worktree *worktree;
1928 const char *status_path;
1929 size_t status_path_len;
1930 struct got_repository *repo;
1931 got_worktree_status_cb status_cb;
1932 void *status_arg;
1933 got_worktree_cancel_cb cancel_cb;
1934 void *cancel_arg;
1937 static const struct got_error *
1938 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1939 got_worktree_status_cb status_cb, void *status_arg,
1940 struct got_repository *repo)
1942 const struct got_error *err = NULL;
1943 unsigned char status = GOT_STATUS_NO_CHANGE;
1944 struct stat sb;
1945 struct got_object_id blob_id, commit_id;
1947 err = get_file_status(&status, &sb, ie, abspath, repo);
1948 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1949 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1950 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1951 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1952 &commit_id);
1954 return err;
1957 static const struct got_error *
1958 status_old_new(void *arg, struct got_fileindex_entry *ie,
1959 struct dirent *de, const char *parent_path)
1961 const struct got_error *err = NULL;
1962 struct diff_dir_cb_arg *a = arg;
1963 char *abspath;
1965 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1966 return got_error(GOT_ERR_CANCELLED);
1968 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1969 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1970 return NULL;
1972 if (parent_path[0]) {
1973 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1974 parent_path, de->d_name) == -1)
1975 return got_error_from_errno("asprintf");
1976 } else {
1977 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1978 de->d_name) == -1)
1979 return got_error_from_errno("asprintf");
1982 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1983 a->repo);
1984 free(abspath);
1985 return err;
1988 static const struct got_error *
1989 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1991 struct diff_dir_cb_arg *a = arg;
1992 struct got_object_id blob_id, commit_id;
1993 unsigned char status;
1995 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1996 return got_error(GOT_ERR_CANCELLED);
1998 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1999 return NULL;
2001 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2002 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2003 if (got_fileindex_entry_has_file_on_disk(ie))
2004 status = GOT_STATUS_MISSING;
2005 else
2006 status = GOT_STATUS_DELETE;
2007 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2008 &commit_id);
2011 static const struct got_error *
2012 status_new(void *arg, struct dirent *de, const char *parent_path)
2014 const struct got_error *err = NULL;
2015 struct diff_dir_cb_arg *a = arg;
2016 char *path = NULL;
2018 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2019 return got_error(GOT_ERR_CANCELLED);
2021 if (de->d_type == DT_DIR)
2022 return NULL;
2024 /* XXX ignore symlinks for now */
2025 if (de->d_type == DT_LNK)
2026 return NULL;
2028 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2029 return NULL;
2031 if (parent_path[0]) {
2032 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2033 return got_error_from_errno("asprintf");
2034 } else {
2035 path = de->d_name;
2038 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2039 NULL, NULL);
2040 if (parent_path[0])
2041 free(path);
2042 return err;
2045 const struct got_error *
2046 got_worktree_status(struct got_worktree *worktree, const char *path,
2047 struct got_repository *repo, got_worktree_status_cb status_cb,
2048 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2050 const struct got_error *err = NULL;
2051 DIR *workdir = NULL;
2052 char *fileindex_path = NULL;
2053 struct got_fileindex *fileindex = NULL;
2054 FILE *index = NULL;
2055 struct got_fileindex_diff_dir_cb fdiff_cb;
2056 struct diff_dir_cb_arg arg;
2057 char *ondisk_path = NULL;
2059 fileindex = got_fileindex_alloc();
2060 if (fileindex == NULL) {
2061 err = got_error_from_errno("got_fileindex_alloc");
2062 goto done;
2065 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2066 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2067 err = got_error_from_errno("asprintf");
2068 fileindex_path = NULL;
2069 goto done;
2072 index = fopen(fileindex_path, "rb");
2073 if (index == NULL) {
2074 if (errno != ENOENT) {
2075 err = got_error_from_errno2("fopen", fileindex_path);
2076 goto done;
2078 } else {
2079 err = got_fileindex_read(fileindex, index);
2080 fclose(index);
2081 if (err)
2082 goto done;
2085 if (asprintf(&ondisk_path, "%s%s%s",
2086 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2087 err = got_error_from_errno("asprintf");
2088 goto done;
2090 workdir = opendir(ondisk_path);
2091 if (workdir == NULL) {
2092 if (errno == ENOTDIR || errno == ENOENT) {
2093 struct got_fileindex_entry *ie;
2094 ie = got_fileindex_entry_get(fileindex, path);
2095 if (ie == NULL) {
2096 err = got_error(GOT_ERR_BAD_PATH);
2097 goto done;
2099 err = report_file_status(ie, ondisk_path,
2100 status_cb, status_arg, repo);
2101 goto done;
2102 } else {
2103 err = got_error_from_errno2("opendir", ondisk_path);
2104 goto done;
2107 fdiff_cb.diff_old_new = status_old_new;
2108 fdiff_cb.diff_old = status_old;
2109 fdiff_cb.diff_new = status_new;
2110 arg.fileindex = fileindex;
2111 arg.worktree = worktree;
2112 arg.status_path = path;
2113 arg.status_path_len = strlen(path);
2114 arg.repo = repo;
2115 arg.status_cb = status_cb;
2116 arg.status_arg = status_arg;
2117 arg.cancel_cb = cancel_cb;
2118 arg.cancel_arg = cancel_arg;
2119 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2120 path, repo, &fdiff_cb, &arg);
2121 done:
2122 if (workdir)
2123 closedir(workdir);
2124 free(ondisk_path);
2125 free(fileindex_path);
2126 got_fileindex_free(fileindex);
2127 return err;
2130 const struct got_error *
2131 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2132 const char *arg)
2134 const struct got_error *err = NULL;
2135 char *resolved, *path = NULL;
2136 size_t len;
2138 *wt_path = NULL;
2140 resolved = realpath(arg, NULL);
2141 if (resolved == NULL)
2142 return got_error_from_errno2("realpath", arg);
2144 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2145 strlen(got_worktree_get_root_path(worktree)))) {
2146 err = got_error(GOT_ERR_BAD_PATH);
2147 goto done;
2150 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2151 err = got_path_skip_common_ancestor(&path,
2152 got_worktree_get_root_path(worktree), resolved);
2153 if (err)
2154 goto done;
2155 } else {
2156 path = strdup("");
2157 if (path == NULL) {
2158 err = got_error_from_errno("strdup");
2159 goto done;
2163 /* XXX status walk can't deal with trailing slash! */
2164 len = strlen(path);
2165 while (path[len - 1] == '/') {
2166 path[len - 1] = '\0';
2167 len--;
2169 done:
2170 free(resolved);
2171 if (err == NULL)
2172 *wt_path = path;
2173 else
2174 free(path);
2175 return err;
2178 static const struct got_error *
2179 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2180 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2181 struct got_repository *repo)
2183 const struct got_error *err = NULL;
2184 struct got_fileindex_entry *ie;
2186 /* Re-adding an existing entry is a no-op. */
2187 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2188 return NULL;
2190 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2191 if (err)
2192 return err;
2194 err = got_fileindex_entry_add(fileindex, ie);
2195 if (err) {
2196 got_fileindex_entry_free(ie);
2197 return err;
2200 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2203 const struct got_error *
2204 got_worktree_schedule_add(struct got_worktree *worktree,
2205 struct got_pathlist_head *ondisk_paths,
2206 got_worktree_status_cb status_cb, void *status_arg,
2207 struct got_repository *repo)
2209 struct got_fileindex *fileindex = NULL;
2210 char *fileindex_path = NULL;
2211 FILE *index = NULL;
2212 const struct got_error *err = NULL, *sync_err, *unlockerr;
2213 struct got_pathlist_entry *pe;
2215 err = lock_worktree(worktree, LOCK_EX);
2216 if (err)
2217 return err;
2220 fileindex = got_fileindex_alloc();
2221 if (fileindex == NULL) {
2222 err = got_error_from_errno("got_fileindex_alloc");
2223 goto done;
2226 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2227 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2228 err = got_error_from_errno("asprintf");
2229 fileindex_path = NULL;
2230 goto done;
2233 index = fopen(fileindex_path, "rb");
2234 if (index == NULL) {
2235 err = got_error_from_errno2("fopen", fileindex_path);
2236 goto done;
2239 err = got_fileindex_read(fileindex, index);
2240 if (err)
2241 goto done;
2243 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2244 char *relpath;
2245 err = got_path_skip_common_ancestor(&relpath,
2246 got_worktree_get_root_path(worktree), pe->path);
2247 if (err)
2248 break;
2249 err = schedule_addition(pe->path, fileindex, relpath,
2250 status_cb, status_arg, repo);
2251 free(relpath);
2252 if (err)
2253 break;
2255 sync_err = sync_fileindex(fileindex, fileindex_path);
2256 if (sync_err && err == NULL)
2257 err = sync_err;
2258 done:
2259 if (index) {
2260 if (fclose(index) != 0 && err == NULL)
2261 err = got_error_from_errno("fclose");
2263 if (fileindex)
2264 got_fileindex_free(fileindex);
2265 unlockerr = lock_worktree(worktree, LOCK_SH);
2266 if (unlockerr && err == NULL)
2267 err = unlockerr;
2268 return err;
2271 static const struct got_error *
2272 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2273 const char *relpath, int delete_local_mods,
2274 got_worktree_status_cb status_cb, void *status_arg,
2275 struct got_repository *repo)
2277 const struct got_error *err = NULL;
2278 struct got_fileindex_entry *ie = NULL;
2279 unsigned char status;
2280 struct stat sb;
2282 ie = got_fileindex_entry_get(fileindex, relpath);
2283 if (ie == NULL)
2284 return got_error(GOT_ERR_BAD_PATH);
2286 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2287 if (err)
2288 return err;
2290 if (status != GOT_STATUS_NO_CHANGE) {
2291 if (status == GOT_STATUS_DELETE)
2292 return got_error_set_errno(ENOENT, ondisk_path);
2293 if (status != GOT_STATUS_MODIFY)
2294 return got_error(GOT_ERR_FILE_STATUS);
2295 if (!delete_local_mods)
2296 return got_error(GOT_ERR_FILE_MODIFIED);
2299 if (unlink(ondisk_path) != 0)
2300 return got_error_from_errno2("unlink", ondisk_path);
2302 got_fileindex_entry_mark_deleted_from_disk(ie);
2303 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2306 const struct got_error *
2307 got_worktree_schedule_delete(struct got_worktree *worktree,
2308 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2309 got_worktree_status_cb status_cb, void *status_arg,
2310 struct got_repository *repo)
2312 struct got_fileindex *fileindex = NULL;
2313 char *fileindex_path = NULL;
2314 FILE *index = NULL;
2315 const struct got_error *err = NULL, *sync_err, *unlockerr;
2316 struct got_pathlist_entry *pe;
2318 err = lock_worktree(worktree, LOCK_EX);
2319 if (err)
2320 return err;
2322 fileindex = got_fileindex_alloc();
2323 if (fileindex == NULL) {
2324 err = got_error_from_errno("got_fileindex_alloc");
2325 goto done;
2328 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2329 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2330 err = got_error_from_errno("asprintf");
2331 fileindex_path = NULL;
2332 goto done;
2335 index = fopen(fileindex_path, "rb");
2336 if (index == NULL) {
2337 err = got_error_from_errno2("fopen", fileindex_path);
2338 goto done;
2341 err = got_fileindex_read(fileindex, index);
2342 if (err)
2343 goto done;
2345 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2346 char *relpath;
2347 err = got_path_skip_common_ancestor(&relpath,
2348 got_worktree_get_root_path(worktree), pe->path);
2349 if (err)
2350 break;
2351 err = schedule_for_deletion(pe->path, fileindex, relpath,
2352 delete_local_mods, status_cb, status_arg, repo);
2353 free(relpath);
2354 if (err)
2355 break;
2357 sync_err = sync_fileindex(fileindex, fileindex_path);
2358 if (sync_err && err == NULL)
2359 err = sync_err;
2360 done:
2361 if (index) {
2362 if (fclose(index) != 0 && err == NULL)
2363 err = got_error_from_errno("fclose");
2365 if (fileindex)
2366 got_fileindex_free(fileindex);
2367 unlockerr = lock_worktree(worktree, LOCK_SH);
2368 if (unlockerr && err == NULL)
2369 err = unlockerr;
2370 return err;
2373 static const struct got_error *
2374 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2375 const char *ondisk_path,
2376 got_worktree_checkout_cb progress_cb, void *progress_arg,
2377 struct got_repository *repo)
2379 const struct got_error *err = NULL;
2380 char *relpath = NULL, *parent_path = NULL;
2381 struct got_fileindex_entry *ie;
2382 struct got_tree_object *tree = NULL;
2383 struct got_object_id *tree_id = NULL;
2384 const struct got_tree_entry *te;
2385 char *tree_path = NULL, *te_name;
2386 struct got_blob_object *blob = NULL;
2387 unsigned char status;
2388 struct stat sb;
2390 err = got_path_skip_common_ancestor(&relpath,
2391 got_worktree_get_root_path(worktree), ondisk_path);
2392 if (err)
2393 goto done;
2395 ie = got_fileindex_entry_get(fileindex, relpath);
2396 if (ie == NULL) {
2397 err = got_error(GOT_ERR_BAD_PATH);
2398 goto done;
2401 /* Construct in-repository path of tree which contains this blob. */
2402 err = got_path_dirname(&parent_path, ie->path);
2403 if (err) {
2404 if (err->code != GOT_ERR_BAD_PATH)
2405 goto done;
2406 parent_path = strdup("/");
2407 if (parent_path == NULL) {
2408 err = got_error_from_errno("strdup");
2409 goto done;
2412 if (got_path_is_root_dir(worktree->path_prefix)) {
2413 tree_path = strdup(parent_path);
2414 if (tree_path == NULL) {
2415 err = got_error_from_errno("strdup");
2416 goto done;
2418 } else {
2419 if (got_path_is_root_dir(parent_path)) {
2420 tree_path = strdup(worktree->path_prefix);
2421 if (tree_path == NULL) {
2422 err = got_error_from_errno("strdup");
2423 goto done;
2425 } else {
2426 if (asprintf(&tree_path, "%s/%s",
2427 worktree->path_prefix, parent_path) == -1) {
2428 err = got_error_from_errno("asprintf");
2429 goto done;
2434 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2435 tree_path);
2436 if (err)
2437 goto done;
2439 err = got_object_open_as_tree(&tree, repo, tree_id);
2440 if (err)
2441 goto done;
2443 te_name = basename(ie->path);
2444 if (te_name == NULL) {
2445 err = got_error_from_errno2("basename", ie->path);
2446 goto done;
2449 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2450 if (err)
2451 goto done;
2453 te = got_object_tree_find_entry(tree, te_name);
2454 if (te == NULL && status != GOT_STATUS_ADD) {
2455 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2456 goto done;
2459 switch (status) {
2460 case GOT_STATUS_ADD:
2461 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2462 got_fileindex_entry_remove(fileindex, ie);
2463 break;
2464 case GOT_STATUS_DELETE:
2465 case GOT_STATUS_MODIFY:
2466 case GOT_STATUS_CONFLICT:
2467 case GOT_STATUS_MISSING: {
2468 struct got_object_id id;
2469 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2470 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2471 if (err)
2472 goto done;
2473 err = install_blob(worktree, ondisk_path, ie->path,
2474 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2475 progress_arg);
2476 if (err)
2477 goto done;
2478 if (status == GOT_STATUS_DELETE) {
2479 err = update_blob_fileindex_entry(worktree,
2480 fileindex, ie, ondisk_path, ie->path, blob, 1);
2481 if (err)
2482 goto done;
2484 break;
2486 default:
2487 goto done;
2489 done:
2490 free(relpath);
2491 free(parent_path);
2492 free(tree_path);
2493 if (blob)
2494 got_object_blob_close(blob);
2495 if (tree)
2496 got_object_tree_close(tree);
2497 free(tree_id);
2498 return err;
2501 const struct got_error *
2502 got_worktree_revert(struct got_worktree *worktree,
2503 struct got_pathlist_head *ondisk_paths,
2504 got_worktree_checkout_cb progress_cb, void *progress_arg,
2505 struct got_repository *repo)
2507 struct got_fileindex *fileindex = NULL;
2508 char *fileindex_path = NULL;
2509 FILE *index = NULL;
2510 const struct got_error *err = NULL, *unlockerr = NULL;
2511 const struct got_error *sync_err = NULL;
2512 struct got_pathlist_entry *pe;
2514 err = lock_worktree(worktree, LOCK_EX);
2515 if (err)
2516 return err;
2518 fileindex = got_fileindex_alloc();
2519 if (fileindex == NULL) {
2520 err = got_error_from_errno("got_fileindex_alloc");
2521 goto done;
2524 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2525 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2526 err = got_error_from_errno("asprintf");
2527 fileindex_path = NULL;
2528 goto done;
2531 index = fopen(fileindex_path, "rb");
2532 if (index == NULL) {
2533 err = got_error_from_errno2("fopen", fileindex_path);
2534 goto done;
2537 err = got_fileindex_read(fileindex, index);
2538 if (err)
2539 goto done;
2541 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2542 err = revert_file(worktree, fileindex, pe->path,
2543 progress_cb, progress_arg, repo);
2544 if (err)
2545 break;
2547 sync_err = sync_fileindex(fileindex, fileindex_path);
2548 if (sync_err && err == NULL)
2549 err = sync_err;
2550 done:
2551 if (index) {
2552 if (fclose(index) != 0 && err == NULL)
2553 err = got_error_from_errno("fclose");
2555 if (fileindex)
2556 got_fileindex_free(fileindex);
2557 unlockerr = lock_worktree(worktree, LOCK_SH);
2558 if (unlockerr && err == NULL)
2559 err = unlockerr;
2560 return err;
2563 static void
2564 free_commitable(struct got_commitable *ct)
2566 free(ct->path);
2567 free(ct->in_repo_path);
2568 free(ct->ondisk_path);
2569 free(ct->blob_id);
2570 free(ct->base_blob_id);
2571 free(ct->base_commit_id);
2572 free(ct);
2575 struct collect_commitables_arg {
2576 struct got_pathlist_head *commitable_paths;
2577 struct got_repository *repo;
2578 struct got_worktree *worktree;
2581 static const struct got_error *
2582 collect_commitables(void *arg, unsigned char status, const char *relpath,
2583 struct got_object_id *blob_id, struct got_object_id *commit_id)
2585 struct collect_commitables_arg *a = arg;
2586 const struct got_error *err = NULL;
2587 struct got_commitable *ct = NULL;
2588 struct got_pathlist_entry *new = NULL;
2589 char *parent_path = NULL, *path = NULL;
2590 struct stat sb;
2592 if (status == GOT_STATUS_CONFLICT)
2593 return got_error(GOT_ERR_COMMIT_CONFLICT);
2595 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2596 status != GOT_STATUS_DELETE)
2597 return NULL;
2599 if (asprintf(&path, "/%s", relpath) == -1) {
2600 err = got_error_from_errno("asprintf");
2601 goto done;
2603 if (strcmp(path, "/") == 0) {
2604 parent_path = strdup("");
2605 if (parent_path == NULL)
2606 return got_error_from_errno("strdup");
2607 } else {
2608 err = got_path_dirname(&parent_path, path);
2609 if (err)
2610 return err;
2613 ct = calloc(1, sizeof(*ct));
2614 if (ct == NULL) {
2615 err = got_error_from_errno("calloc");
2616 goto done;
2619 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2620 relpath) == -1) {
2621 err = got_error_from_errno("asprintf");
2622 goto done;
2624 if (status == GOT_STATUS_DELETE) {
2625 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2626 } else {
2627 if (lstat(ct->ondisk_path, &sb) != 0) {
2628 err = got_error_from_errno2("lstat", ct->ondisk_path);
2629 goto done;
2631 ct->mode = sb.st_mode;
2634 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2635 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2636 relpath) == -1) {
2637 err = got_error_from_errno("asprintf");
2638 goto done;
2641 ct->status = status;
2642 ct->blob_id = NULL; /* will be filled in when blob gets created */
2643 if (ct->status != GOT_STATUS_ADD) {
2644 ct->base_blob_id = got_object_id_dup(blob_id);
2645 if (ct->base_blob_id == NULL) {
2646 err = got_error_from_errno("got_object_id_dup");
2647 goto done;
2649 ct->base_commit_id = got_object_id_dup(commit_id);
2650 if (ct->base_commit_id == NULL) {
2651 err = got_error_from_errno("got_object_id_dup");
2652 goto done;
2655 ct->path = strdup(path);
2656 if (ct->path == NULL) {
2657 err = got_error_from_errno("strdup");
2658 goto done;
2660 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2661 done:
2662 if (ct && (err || new == NULL))
2663 free_commitable(ct);
2664 free(parent_path);
2665 free(path);
2666 return err;
2669 static const struct got_error *write_tree(struct got_object_id **,
2670 struct got_tree_object *, const char *, struct got_pathlist_head *,
2671 got_worktree_status_cb status_cb, void *status_arg,
2672 struct got_repository *);
2674 static const struct got_error *
2675 write_subtree(struct got_object_id **new_subtree_id,
2676 struct got_tree_entry *te, const char *parent_path,
2677 struct got_pathlist_head *commitable_paths,
2678 got_worktree_status_cb status_cb, void *status_arg,
2679 struct got_repository *repo)
2681 const struct got_error *err = NULL;
2682 struct got_tree_object *subtree;
2683 char *subpath;
2685 if (asprintf(&subpath, "%s%s%s", parent_path,
2686 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2687 return got_error_from_errno("asprintf");
2689 err = got_object_open_as_tree(&subtree, repo, te->id);
2690 if (err)
2691 return err;
2693 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2694 status_cb, status_arg, repo);
2695 got_object_tree_close(subtree);
2696 free(subpath);
2697 return err;
2700 static const struct got_error *
2701 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2703 const struct got_error *err = NULL;
2704 char *ct_parent_path = NULL;
2706 *match = 0;
2708 if (strchr(ct->path, '/') == NULL) {
2709 *match = got_path_is_root_dir(path);
2710 return NULL;
2713 err = got_path_dirname(&ct_parent_path, ct->path);
2714 if (err)
2715 return err;
2716 *match = (strcmp(path, ct_parent_path) == 0);
2717 free(ct_parent_path);
2718 return err;
2721 static mode_t
2722 get_ct_file_mode(struct got_commitable *ct)
2724 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2727 static const struct got_error *
2728 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2729 struct got_tree_entry *te, struct got_commitable *ct)
2731 const struct got_error *err = NULL;
2733 *new_te = NULL;
2735 err = got_object_tree_entry_dup(new_te, te);
2736 if (err)
2737 goto done;
2739 (*new_te)->mode = get_ct_file_mode(ct);
2741 free((*new_te)->id);
2742 (*new_te)->id = got_object_id_dup(ct->blob_id);
2743 if ((*new_te)->id == NULL) {
2744 err = got_error_from_errno("got_object_id_dup");
2745 goto done;
2747 done:
2748 if (err && *new_te) {
2749 got_object_tree_entry_close(*new_te);
2750 *new_te = NULL;
2752 return err;
2755 static const struct got_error *
2756 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2757 struct got_commitable *ct)
2759 const struct got_error *err = NULL;
2760 char *ct_name;
2762 *new_te = NULL;
2764 *new_te = calloc(1, sizeof(**new_te));
2765 if (*new_te == NULL)
2766 return got_error_from_errno("calloc");
2768 ct_name = basename(ct->path);
2769 if (ct_name == NULL) {
2770 err = got_error_from_errno2("basename", ct->path);
2771 goto done;
2773 (*new_te)->name = strdup(ct_name);
2774 if ((*new_te)->name == NULL) {
2775 err = got_error_from_errno("strdup");
2776 goto done;
2779 (*new_te)->mode = get_ct_file_mode(ct);
2781 (*new_te)->id = got_object_id_dup(ct->blob_id);
2782 if ((*new_te)->id == NULL) {
2783 err = got_error_from_errno("got_object_id_dup");
2784 goto done;
2786 done:
2787 if (err && *new_te) {
2788 got_object_tree_entry_close(*new_te);
2789 *new_te = NULL;
2791 return err;
2794 static const struct got_error *
2795 insert_tree_entry(struct got_tree_entry *new_te,
2796 struct got_pathlist_head *paths)
2798 const struct got_error *err = NULL;
2799 struct got_pathlist_entry *new_pe;
2801 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2802 if (err)
2803 return err;
2804 if (new_pe == NULL)
2805 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2806 return NULL;
2809 static const struct got_error *
2810 report_ct_status(struct got_commitable *ct,
2811 got_worktree_status_cb status_cb, void *status_arg)
2813 const char *ct_path = ct->path;
2814 while (ct_path[0] == '/')
2815 ct_path++;
2816 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2819 static const struct got_error *
2820 match_modified_subtree(int *modified, struct got_tree_entry *te,
2821 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2823 const struct got_error *err = NULL;
2824 struct got_pathlist_entry *pe;
2825 char *te_path;
2827 *modified = 0;
2829 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2830 got_path_is_root_dir(base_tree_path) ? "" : "/",
2831 te->name) == -1)
2832 return got_error_from_errno("asprintf");
2834 TAILQ_FOREACH(pe, commitable_paths, entry) {
2835 struct got_commitable *ct = pe->data;
2836 *modified = got_path_is_child(ct->in_repo_path, te_path,
2837 strlen(te_path));
2838 if (*modified)
2839 break;
2842 free(te_path);
2843 return err;
2846 static const struct got_error *
2847 match_deleted_or_modified_ct(struct got_commitable **ctp,
2848 struct got_tree_entry *te, const char *base_tree_path,
2849 struct got_pathlist_head *commitable_paths)
2851 const struct got_error *err = NULL;
2852 struct got_pathlist_entry *pe;
2854 *ctp = NULL;
2856 TAILQ_FOREACH(pe, commitable_paths, entry) {
2857 struct got_commitable *ct = pe->data;
2858 char *ct_name = NULL;
2859 int path_matches;
2861 if (ct->status != GOT_STATUS_MODIFY &&
2862 ct->status != GOT_STATUS_DELETE)
2863 continue;
2865 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2866 continue;
2868 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2869 if (err)
2870 return err;
2871 if (!path_matches)
2872 continue;
2874 ct_name = basename(pe->path);
2875 if (ct_name == NULL)
2876 return got_error_from_errno2("basename", pe->path);
2878 if (strcmp(te->name, ct_name) != 0)
2879 continue;
2881 *ctp = ct;
2882 break;
2885 return err;
2888 static const struct got_error *
2889 write_tree(struct got_object_id **new_tree_id,
2890 struct got_tree_object *base_tree, const char *path_base_tree,
2891 struct got_pathlist_head *commitable_paths,
2892 got_worktree_status_cb status_cb, void *status_arg,
2893 struct got_repository *repo)
2895 const struct got_error *err = NULL;
2896 const struct got_tree_entries *base_entries = NULL;
2897 struct got_pathlist_head paths;
2898 struct got_tree_entries new_tree_entries;
2899 struct got_tree_entry *te, *new_te = NULL;
2900 struct got_pathlist_entry *pe;
2902 TAILQ_INIT(&paths);
2903 new_tree_entries.nentries = 0;
2904 SIMPLEQ_INIT(&new_tree_entries.head);
2906 /* Insert, and recurse into, newly added entries first. */
2907 TAILQ_FOREACH(pe, commitable_paths, entry) {
2908 struct got_commitable *ct = pe->data;
2909 char *child_path = NULL, *slash;
2911 if (ct->status != GOT_STATUS_ADD ||
2912 (ct->flags & GOT_COMMITABLE_ADDED))
2913 continue;
2915 if (!got_path_is_child(pe->path, path_base_tree,
2916 strlen(path_base_tree)))
2917 continue;
2919 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2920 pe->path);
2921 if (err)
2922 goto done;
2924 slash = strchr(child_path, '/');
2925 if (slash == NULL) {
2926 err = alloc_added_blob_tree_entry(&new_te, ct);
2927 if (err)
2928 goto done;
2929 err = report_ct_status(ct, status_cb, status_arg);
2930 if (err)
2931 goto done;
2932 ct->flags |= GOT_COMMITABLE_ADDED;
2933 } else {
2934 char *subtree_path;
2936 *slash = '\0'; /* trim trailing path components */
2937 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2938 got_path_is_root_dir(path_base_tree) ? "" : "/",
2939 child_path) == -1) {
2940 err = got_error_from_errno("asprintf");
2941 goto done;
2944 new_te = calloc(1, sizeof(*new_te));
2945 new_te->mode = S_IFDIR;
2946 new_te->name = strdup(child_path);
2947 if (new_te->name == NULL) {
2948 err = got_error_from_errno("strdup");
2949 got_object_tree_entry_close(new_te);
2950 new_te = NULL;
2951 goto done;
2953 err = write_tree(&new_te->id, NULL, subtree_path,
2954 commitable_paths, status_cb, status_arg, repo);
2955 free(subtree_path);
2956 if (err) {
2957 got_object_tree_entry_close(new_te);
2958 new_te = NULL;
2959 goto done;
2962 err = insert_tree_entry(new_te, &paths);
2963 if (err)
2964 goto done;
2967 if (base_tree) {
2968 /* Handle modified and deleted entries. */
2969 base_entries = got_object_tree_get_entries(base_tree);
2970 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2971 struct got_commitable *ct = NULL;
2973 if (S_ISDIR(te->mode)) {
2974 int modified;
2975 err = got_object_tree_entry_dup(&new_te, te);
2976 if (err)
2977 goto done;
2978 err = match_modified_subtree(&modified, te,
2979 path_base_tree, commitable_paths);
2980 if (err)
2981 goto done;
2982 /* Avoid recursion into unmodified subtrees. */
2983 if (modified) {
2984 free(new_te->id);
2985 err = write_subtree(&new_te->id, te,
2986 path_base_tree, commitable_paths,
2987 status_cb, status_arg, repo);
2988 if (err)
2989 goto done;
2991 err = insert_tree_entry(new_te, &paths);
2992 if (err)
2993 goto done;
2994 continue;
2997 err = match_deleted_or_modified_ct(&ct, te,
2998 path_base_tree, commitable_paths);
2999 if (ct) {
3000 /* NB: Deleted entries get dropped here. */
3001 if (ct->status == GOT_STATUS_MODIFY) {
3002 err = alloc_modified_blob_tree_entry(
3003 &new_te, te, ct);
3004 if (err)
3005 goto done;
3006 err = insert_tree_entry(new_te, &paths);
3007 if (err)
3008 goto done;
3010 err = report_ct_status(ct, status_cb,
3011 status_arg);
3012 if (err)
3013 goto done;
3014 } else {
3015 /* Entry is unchanged; just copy it. */
3016 err = got_object_tree_entry_dup(&new_te, te);
3017 if (err)
3018 goto done;
3019 err = insert_tree_entry(new_te, &paths);
3020 if (err)
3021 goto done;
3026 /* Write new list of entries; deleted entries have been dropped. */
3027 TAILQ_FOREACH(pe, &paths, entry) {
3028 struct got_tree_entry *te = pe->data;
3029 new_tree_entries.nentries++;
3030 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3032 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3033 done:
3034 got_object_tree_entries_close(&new_tree_entries);
3035 got_pathlist_free(&paths);
3036 return err;
3039 static const struct got_error *
3040 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3041 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3043 const struct got_error *err = NULL, *sync_err;
3044 char *fileindex_path = NULL;
3045 struct got_fileindex *fileindex = NULL;
3046 struct got_pathlist_entry *pe;
3048 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3049 if (err)
3050 return err;
3052 TAILQ_FOREACH(pe, commitable_paths, entry) {
3053 struct got_fileindex_entry *ie;
3054 struct got_commitable *ct = pe->data;
3056 ie = got_fileindex_entry_get(fileindex, pe->path);
3057 if (ie) {
3058 if (ct->status == GOT_STATUS_DELETE) {
3059 got_fileindex_entry_remove(fileindex, ie);
3060 got_fileindex_entry_free(ie);
3061 } else
3062 err = got_fileindex_entry_update(ie,
3063 ct->ondisk_path, ct->blob_id->sha1,
3064 new_base_commit_id->sha1, 1);
3065 } else {
3066 err = got_fileindex_entry_alloc(&ie,
3067 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3068 new_base_commit_id->sha1);
3069 if (err)
3070 break;
3071 err = got_fileindex_entry_add(fileindex, ie);
3072 if (err)
3073 break;
3076 sync_err = sync_fileindex(fileindex, fileindex_path);
3077 if (sync_err && err == NULL)
3078 err = sync_err;
3079 free(fileindex_path);
3080 got_fileindex_free(fileindex);
3081 return err;
3084 static const struct got_error *
3085 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3086 struct got_object_id *head_commit_id)
3088 const struct got_error *err = NULL;
3089 struct got_object_id *id_in_head = NULL, *id = NULL;
3090 struct got_commit_object *commit = NULL;
3091 char *path = NULL;
3092 const char *ct_path = ct->in_repo_path;
3094 while (ct_path[0] == '/')
3095 ct_path++;
3098 * Ensure that no modifications were made to files *and their parents*
3099 * in commits between the file's base commit and the branch head.
3101 * Checking the parents is important for detecting conflicting tree
3102 * configurations (files or parent folders might have been moved,
3103 * deleted, added again, etc.). Such changes need to be merged with
3104 * local changes before a commit can occur.
3106 * The implication is that the file's (parent) entry in the root
3107 * directory must have the same ID in all relevant commits.
3109 if (ct->status != GOT_STATUS_ADD) {
3110 struct got_object_qid *pid;
3111 char *slash;
3112 struct got_object_id *root_entry_id = NULL;
3114 /* Trivial case: base commit == head commit */
3115 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3116 return NULL;
3118 /* Compute the path to the root directory's entry. */
3119 path = strdup(ct_path);
3120 if (path == NULL) {
3121 err = got_error_from_errno("strdup");
3122 goto done;
3124 slash = strchr(path, '/');
3125 if (slash)
3126 *slash = '\0';
3128 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3129 if (err)
3130 goto done;
3132 err = got_object_id_by_path(&root_entry_id, repo,
3133 head_commit_id, path);
3134 if (err)
3135 goto done;
3137 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3138 while (pid) {
3139 struct got_commit_object *pcommit;
3141 err = got_object_id_by_path(&id, repo, pid->id, path);
3142 if (err) {
3143 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3144 goto done;
3145 err = NULL;
3146 break;
3149 err = got_object_id_by_path(&id, repo, pid->id, path);
3150 if (err)
3151 goto done;
3153 if (got_object_id_cmp(id, root_entry_id) != 0) {
3154 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3155 break;
3158 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3159 break; /* all relevant commits scanned */
3161 err = got_object_open_as_commit(&pcommit, repo,
3162 pid->id);
3163 if (err)
3164 goto done;
3166 got_object_commit_close(commit);
3167 commit = pcommit;
3168 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3169 commit));
3171 } else {
3172 /* Require that added files don't exist in the branch head. */
3173 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3174 ct_path);
3175 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3176 goto done;
3177 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3179 done:
3180 if (commit)
3181 got_object_commit_close(commit);
3182 free(id_in_head);
3183 free(id);
3184 free(path);
3185 return err;
3188 const struct got_error *
3189 got_worktree_commit(struct got_object_id **new_commit_id,
3190 struct got_worktree *worktree, const char *ondisk_path,
3191 const char *author, const char *committer,
3192 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3193 got_worktree_status_cb status_cb, void *status_arg,
3194 struct got_repository *repo)
3196 const struct got_error *err = NULL, *unlockerr = NULL;
3197 struct collect_commitables_arg cc_arg;
3198 struct got_pathlist_head commitable_paths;
3199 struct got_pathlist_entry *pe;
3200 char *relpath = NULL;
3201 const char *head_ref_name = NULL;
3202 struct got_reference *head_ref = NULL;
3203 struct got_commit_object *head_commit = NULL;
3204 struct got_object_id *head_commit_id = NULL;
3205 struct got_reference *head_ref2 = NULL;
3206 struct got_object_id *head_commit_id2 = NULL;
3207 struct got_tree_object *head_tree = NULL;
3208 struct got_object_id *new_tree_id = NULL;
3209 struct got_object_id_queue parent_ids;
3210 struct got_object_qid *pid = NULL;
3211 char *logmsg = NULL;
3213 *new_commit_id = NULL;
3215 TAILQ_INIT(&commitable_paths);
3216 SIMPLEQ_INIT(&parent_ids);
3218 if (ondisk_path) {
3219 err = got_path_skip_common_ancestor(&relpath,
3220 worktree->root_path, ondisk_path);
3221 if (err)
3222 return err;
3225 err = lock_worktree(worktree, LOCK_EX);
3226 if (err)
3227 goto done;
3229 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3230 if (err)
3231 goto done;
3232 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3233 if (err)
3234 goto done;
3236 cc_arg.commitable_paths = &commitable_paths;
3237 cc_arg.worktree = worktree;
3238 cc_arg.repo = repo;
3239 err = got_worktree_status(worktree, relpath ? relpath : "",
3240 repo, collect_commitables, &cc_arg, NULL, NULL);
3241 if (err)
3242 goto done;
3244 if (TAILQ_EMPTY(&commitable_paths)) {
3245 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3246 goto done;
3249 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3250 if (err)
3251 goto done;
3253 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3254 struct got_commitable *ct = pe->data;
3255 err = check_ct_out_of_date(ct, repo, head_commit_id);
3256 if (err)
3257 goto done;
3260 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3261 if (err)
3262 goto done;
3264 if (commit_msg_cb != NULL) {
3265 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3266 if (err)
3267 goto done;
3270 if (logmsg == NULL || strlen(logmsg) == 0) {
3271 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3272 goto done;
3275 /* Create blobs from added and modified files and record their IDs. */
3276 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3277 struct got_commitable *ct = pe->data;
3278 char *ondisk_path;
3280 if (ct->status != GOT_STATUS_ADD &&
3281 ct->status != GOT_STATUS_MODIFY)
3282 continue;
3284 if (asprintf(&ondisk_path, "%s/%s",
3285 worktree->root_path, pe->path) == -1) {
3286 err = got_error_from_errno("asprintf");
3287 goto done;
3289 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3290 free(ondisk_path);
3291 if (err)
3292 goto done;
3295 /* Recursively write new tree objects. */
3296 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3297 status_cb, status_arg, repo);
3298 if (err)
3299 goto done;
3301 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3302 if (err)
3303 goto done;
3304 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3305 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3306 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3307 got_object_qid_free(pid);
3308 if (logmsg != NULL)
3309 free(logmsg);
3310 if (err)
3311 goto done;
3313 /* Check if a concurrent commit to our branch has occurred. */
3314 head_ref_name = got_worktree_get_head_ref_name(worktree);
3315 if (head_ref_name == NULL) {
3316 err = got_error_from_errno("got_worktree_get_head_ref_name");
3317 goto done;
3319 /* Lock the reference here to prevent concurrent modification. */
3320 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3321 if (err)
3322 goto done;
3323 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3324 if (err)
3325 goto done;
3326 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3327 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3328 goto done;
3330 /* Update branch head in repository. */
3331 err = got_ref_change_ref(head_ref2, *new_commit_id);
3332 if (err)
3333 goto done;
3334 err = got_ref_write(head_ref2, repo);
3335 if (err)
3336 goto done;
3338 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3339 if (err)
3340 goto done;
3342 err = ref_base_commit(worktree, repo);
3343 if (err)
3344 goto done;
3346 err = update_fileindex_after_commit(&commitable_paths,
3347 *new_commit_id, worktree);
3348 if (err)
3349 goto done;
3350 done:
3351 unlockerr = lock_worktree(worktree, LOCK_SH);
3352 if (unlockerr && err == NULL)
3353 err = unlockerr;
3354 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3355 struct got_commitable *ct = pe->data;
3356 free_commitable(ct);
3358 got_pathlist_free(&commitable_paths);
3359 if (head_tree)
3360 got_object_tree_close(head_tree);
3361 if (head_commit)
3362 got_object_commit_close(head_commit);
3363 free(relpath);
3364 free(head_commit_id);
3365 free(head_commit_id2);
3366 if (head_ref)
3367 got_ref_close(head_ref);
3368 if (head_ref2) {
3369 unlockerr = got_ref_unlock(head_ref2);
3370 if (unlockerr && err == NULL)
3371 err = unlockerr;
3372 got_ref_close(head_ref2);
3374 return err;
3377 const char *
3378 got_commitable_get_path(struct got_commitable *ct)
3380 return ct->path;
3383 unsigned int
3384 got_commitable_get_status(struct got_commitable *ct)
3386 return ct->status;