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;
1463 got_worktree_checkout_cb progress_cb;
1464 void *progress_arg;
1467 /* Bump base commit ID of all files within an updated part of the work tree. */
1468 static const struct got_error *
1469 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1471 struct bump_base_commit_id_arg *a = arg;
1473 if (a->entry_name) {
1474 if (strcmp(ie->path, a->path) != 0)
1475 return NULL;
1476 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1477 return NULL;
1479 (*a->progress_cb)(a->progress_arg, GOT_STATUS_BUMP_BASE, ie->path);
1480 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1481 return NULL;
1484 static const struct got_error *
1485 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1487 const struct got_error *err = NULL;
1488 char *new_fileindex_path = NULL;
1489 FILE *new_index = NULL;
1491 err = got_opentemp_named(&new_fileindex_path, &new_index,
1492 fileindex_path);
1493 if (err)
1494 goto done;
1496 err = got_fileindex_write(fileindex, new_index);
1497 if (err)
1498 goto done;
1500 if (rename(new_fileindex_path, fileindex_path) != 0) {
1501 err = got_error_from_errno3("rename", new_fileindex_path,
1502 fileindex_path);
1503 unlink(new_fileindex_path);
1505 done:
1506 if (new_index)
1507 fclose(new_index);
1508 free(new_fileindex_path);
1509 return err;
1512 const struct got_error *
1513 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1514 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1515 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1517 const struct got_error *err = NULL, *sync_err, *unlockerr;
1518 struct got_commit_object *commit = NULL;
1519 struct got_object_id *tree_id = NULL;
1520 struct got_tree_object *tree = NULL;
1521 struct got_fileindex *fileindex = NULL;
1522 char *fileindex_path = NULL;
1523 struct got_fileindex_diff_tree_cb diff_cb;
1524 struct diff_cb_arg arg;
1525 char *relpath = NULL, *entry_name = NULL;
1526 struct bump_base_commit_id_arg bbc_arg;
1528 err = lock_worktree(worktree, LOCK_EX);
1529 if (err)
1530 return err;
1533 * Read the file index.
1534 * Checking out files is supposed to be an idempotent operation.
1535 * If the on-disk file index is incomplete we will try to complete it.
1537 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1538 if (err)
1539 goto done;
1541 err = ref_base_commit(worktree, repo);
1542 if (err)
1543 goto done;
1545 err = got_object_open_as_commit(&commit, repo,
1546 worktree->base_commit_id);
1547 if (err)
1548 goto done;
1550 if (path[0]) {
1551 char *tree_path;
1552 int obj_type;
1553 relpath = strdup(path);
1554 if (relpath == NULL) {
1555 err = got_error_from_errno("strdup");
1556 goto done;
1558 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1559 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1560 path) == -1) {
1561 err = got_error_from_errno("asprintf");
1562 goto done;
1564 err = got_object_id_by_path(&tree_id, repo,
1565 worktree->base_commit_id, tree_path);
1566 free(tree_path);
1567 if (err)
1568 goto done;
1569 err = got_object_get_type(&obj_type, repo, tree_id);
1570 if (err)
1571 goto done;
1572 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1573 /* Split provided path into parent dir + entry name. */
1574 if (strchr(path, '/') == NULL) {
1575 relpath = strdup("");
1576 if (relpath == NULL) {
1577 err = got_error_from_errno("strdup");
1578 goto done;
1580 tree_path = strdup(worktree->path_prefix);
1581 if (tree_path == NULL) {
1582 err = got_error_from_errno("strdup");
1583 goto done;
1585 } else {
1586 err = got_path_dirname(&relpath, path);
1587 if (err)
1588 goto done;
1589 if (asprintf(&tree_path, "%s%s%s",
1590 worktree->path_prefix,
1591 got_path_is_root_dir(
1592 worktree->path_prefix) ? "" : "/",
1593 relpath) == -1) {
1594 err = got_error_from_errno("asprintf");
1595 goto done;
1598 err = got_object_id_by_path(&tree_id, repo,
1599 worktree->base_commit_id, tree_path);
1600 free(tree_path);
1601 if (err)
1602 goto done;
1603 entry_name = basename(path);
1604 if (entry_name == NULL) {
1605 err = got_error_from_errno2("basename", path);
1606 goto done;
1609 } else {
1610 relpath = strdup("");
1611 if (relpath == NULL) {
1612 err = got_error_from_errno("strdup");
1613 goto done;
1615 err = got_object_id_by_path(&tree_id, repo,
1616 worktree->base_commit_id, worktree->path_prefix);
1617 if (err)
1618 goto done;
1621 err = got_object_open_as_tree(&tree, repo, tree_id);
1622 if (err)
1623 goto done;
1625 if (entry_name &&
1626 got_object_tree_find_entry(tree, entry_name) == NULL) {
1627 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1628 goto done;
1631 diff_cb.diff_old_new = diff_old_new;
1632 diff_cb.diff_old = diff_old;
1633 diff_cb.diff_new = diff_new;
1634 arg.fileindex = fileindex;
1635 arg.worktree = worktree;
1636 arg.repo = repo;
1637 arg.progress_cb = progress_cb;
1638 arg.progress_arg = progress_arg;
1639 arg.cancel_cb = cancel_cb;
1640 arg.cancel_arg = cancel_arg;
1641 err = got_fileindex_diff_tree(fileindex, tree, relpath,
1642 entry_name, repo, &diff_cb, &arg);
1643 if (err)
1644 goto sync;
1646 bbc_arg.base_commit_id = worktree->base_commit_id;
1647 bbc_arg.entry_name = entry_name;
1648 bbc_arg.path = path;
1649 bbc_arg.path_len = strlen(path);
1650 bbc_arg.progress_cb = progress_cb;
1651 bbc_arg.progress_arg = progress_arg;
1652 err = got_fileindex_for_each_entry_safe(fileindex,
1653 bump_base_commit_id, &bbc_arg);
1654 sync:
1655 sync_err = sync_fileindex(fileindex, fileindex_path);
1656 if (sync_err && err == NULL)
1657 err = sync_err;
1658 done:
1659 free(fileindex_path);
1660 free(relpath);
1661 if (tree)
1662 got_object_tree_close(tree);
1663 if (commit)
1664 got_object_commit_close(commit);
1665 got_fileindex_free(fileindex);
1666 unlockerr = lock_worktree(worktree, LOCK_SH);
1667 if (unlockerr && err == NULL)
1668 err = unlockerr;
1669 return err;
1672 struct merge_file_cb_arg {
1673 struct got_worktree *worktree;
1674 struct got_fileindex *fileindex;
1675 got_worktree_checkout_cb progress_cb;
1676 void *progress_arg;
1677 got_worktree_cancel_cb cancel_cb;
1678 void *cancel_arg;
1681 static const struct got_error *
1682 merge_file_cb(void *arg, struct got_blob_object *blob1,
1683 struct got_blob_object *blob2, struct got_object_id *id1,
1684 struct got_object_id *id2, const char *path1, const char *path2,
1685 struct got_repository *repo)
1687 static const struct got_error *err = NULL;
1688 struct merge_file_cb_arg *a = arg;
1689 struct got_fileindex_entry *ie;
1690 char *ondisk_path = NULL;
1691 struct stat sb;
1692 unsigned char status;
1693 int local_changes_subsumed;
1695 if (blob1 && blob2) {
1696 ie = got_fileindex_entry_get(a->fileindex, path2);
1697 if (ie == NULL) {
1698 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1699 path2);
1700 return NULL;
1703 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1704 path2) == -1)
1705 return got_error_from_errno("asprintf");
1707 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1708 if (err)
1709 goto done;
1711 if (status == GOT_STATUS_DELETE) {
1712 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1713 path2);
1714 goto done;
1716 if (status != GOT_STATUS_NO_CHANGE &&
1717 status != GOT_STATUS_MODIFY &&
1718 status != GOT_STATUS_CONFLICT &&
1719 status != GOT_STATUS_ADD) {
1720 (*a->progress_cb)(a->progress_arg, status, path2);
1721 goto done;
1724 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1725 ondisk_path, path2, sb.st_mode, blob2, repo,
1726 a->progress_cb, a->progress_arg);
1727 } else if (blob1) {
1728 ie = got_fileindex_entry_get(a->fileindex, path1);
1729 if (ie == NULL) {
1730 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1731 path2);
1732 return NULL;
1735 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1736 path1) == -1)
1737 return got_error_from_errno("asprintf");
1739 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1740 if (err)
1741 goto done;
1743 switch (status) {
1744 case GOT_STATUS_NO_CHANGE:
1745 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1746 path1);
1747 err = remove_ondisk_file(a->worktree->root_path, path1);
1748 if (err)
1749 goto done;
1750 if (ie)
1751 got_fileindex_entry_mark_deleted_from_disk(ie);
1752 break;
1753 case GOT_STATUS_DELETE:
1754 case GOT_STATUS_MISSING:
1755 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1756 path1);
1757 if (ie)
1758 got_fileindex_entry_mark_deleted_from_disk(ie);
1759 break;
1760 case GOT_STATUS_ADD:
1761 case GOT_STATUS_MODIFY:
1762 case GOT_STATUS_CONFLICT:
1763 (*a->progress_cb)(a->progress_arg,
1764 GOT_STATUS_CANNOT_DELETE, path1);
1765 break;
1766 case GOT_STATUS_OBSTRUCTED:
1767 (*a->progress_cb)(a->progress_arg, status, path1);
1768 break;
1769 default:
1770 break;
1772 } else if (blob2) {
1773 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1774 path2) == -1)
1775 return got_error_from_errno("asprintf");
1776 ie = got_fileindex_entry_get(a->fileindex, path2);
1777 if (ie) {
1778 err = get_file_status(&status, &sb, ie, ondisk_path,
1779 repo);
1780 if (err)
1781 goto done;
1782 if (status != GOT_STATUS_NO_CHANGE &&
1783 status != GOT_STATUS_MODIFY &&
1784 status != GOT_STATUS_CONFLICT &&
1785 status != GOT_STATUS_ADD) {
1786 (*a->progress_cb)(a->progress_arg, status,
1787 path2);
1788 goto done;
1790 err = merge_blob(&local_changes_subsumed, a->worktree,
1791 NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1792 a->progress_cb, a->progress_arg);
1793 if (status == GOT_STATUS_DELETE) {
1794 err = update_blob_fileindex_entry(a->worktree,
1795 a->fileindex, ie, ondisk_path, ie->path,
1796 blob2, 0);
1797 if (err)
1798 goto done;
1800 } else {
1801 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1802 err = install_blob(a->worktree, ondisk_path, path2,
1803 /* XXX get this from parent tree! */
1804 GOT_DEFAULT_FILE_MODE,
1805 sb.st_mode, blob2, 0, 0, repo,
1806 a->progress_cb, a->progress_arg);
1807 if (err)
1808 goto done;
1809 err = got_fileindex_entry_alloc(&ie,
1810 ondisk_path, path2, NULL, NULL);
1811 if (err)
1812 goto done;
1813 err = got_fileindex_entry_add(a->fileindex, ie);
1814 if (err) {
1815 got_fileindex_entry_free(ie);
1816 goto done;
1820 done:
1821 free(ondisk_path);
1822 return err;
1825 struct check_merge_ok_arg {
1826 struct got_worktree *worktree;
1827 struct got_repository *repo;
1830 static const struct got_error *
1831 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1833 const struct got_error *err = NULL;
1834 struct check_merge_ok_arg *a = arg;
1835 unsigned char status;
1836 struct stat sb;
1837 char *ondisk_path;
1839 /* Reject merges into a work tree with mixed base commits. */
1840 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1841 SHA1_DIGEST_LENGTH))
1842 return got_error(GOT_ERR_MIXED_COMMITS);
1844 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1845 == -1)
1846 return got_error_from_errno("asprintf");
1848 /* Reject merges into a work tree with conflicted files. */
1849 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1850 if (err)
1851 return err;
1852 if (status == GOT_STATUS_CONFLICT)
1853 return got_error(GOT_ERR_CONFLICTS);
1855 return NULL;
1858 const struct got_error *
1859 got_worktree_merge_files(struct got_worktree *worktree,
1860 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1861 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1862 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1864 const struct got_error *err = NULL, *sync_err, *unlockerr;
1865 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1866 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1867 struct merge_file_cb_arg arg;
1868 char *fileindex_path = NULL;
1869 struct got_fileindex *fileindex = NULL;
1870 struct check_merge_ok_arg mok_arg;
1872 err = lock_worktree(worktree, LOCK_EX);
1873 if (err)
1874 return err;
1876 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1877 if (err)
1878 goto done;
1880 mok_arg.worktree = worktree;
1881 mok_arg.repo = repo;
1882 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1883 &mok_arg);
1884 if (err)
1885 goto done;
1887 if (commit_id1) {
1888 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1889 worktree->path_prefix);
1890 if (err)
1891 goto done;
1893 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1894 if (err)
1895 goto done;
1898 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1899 worktree->path_prefix);
1900 if (err)
1901 goto done;
1903 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1904 if (err)
1905 goto done;
1907 arg.worktree = worktree;
1908 arg.fileindex = fileindex;
1909 arg.progress_cb = progress_cb;
1910 arg.progress_arg = progress_arg;
1911 arg.cancel_cb = cancel_cb;
1912 arg.cancel_arg = cancel_arg;
1913 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1914 sync_err = sync_fileindex(fileindex, fileindex_path);
1915 if (sync_err && err == NULL)
1916 err = sync_err;
1917 done:
1918 got_fileindex_free(fileindex);
1919 if (tree1)
1920 got_object_tree_close(tree1);
1921 if (tree2)
1922 got_object_tree_close(tree2);
1924 unlockerr = lock_worktree(worktree, LOCK_SH);
1925 if (unlockerr && err == NULL)
1926 err = unlockerr;
1927 return err;
1930 struct diff_dir_cb_arg {
1931 struct got_fileindex *fileindex;
1932 struct got_worktree *worktree;
1933 const char *status_path;
1934 size_t status_path_len;
1935 struct got_repository *repo;
1936 got_worktree_status_cb status_cb;
1937 void *status_arg;
1938 got_worktree_cancel_cb cancel_cb;
1939 void *cancel_arg;
1942 static const struct got_error *
1943 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1944 got_worktree_status_cb status_cb, void *status_arg,
1945 struct got_repository *repo)
1947 const struct got_error *err = NULL;
1948 unsigned char status = GOT_STATUS_NO_CHANGE;
1949 struct stat sb;
1950 struct got_object_id blob_id, commit_id;
1952 err = get_file_status(&status, &sb, ie, abspath, repo);
1953 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1954 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1955 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1956 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1957 &commit_id);
1959 return err;
1962 static const struct got_error *
1963 status_old_new(void *arg, struct got_fileindex_entry *ie,
1964 struct dirent *de, const char *parent_path)
1966 const struct got_error *err = NULL;
1967 struct diff_dir_cb_arg *a = arg;
1968 char *abspath;
1970 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1971 return got_error(GOT_ERR_CANCELLED);
1973 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1974 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1975 return NULL;
1977 if (parent_path[0]) {
1978 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1979 parent_path, de->d_name) == -1)
1980 return got_error_from_errno("asprintf");
1981 } else {
1982 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1983 de->d_name) == -1)
1984 return got_error_from_errno("asprintf");
1987 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1988 a->repo);
1989 free(abspath);
1990 return err;
1993 static const struct got_error *
1994 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1996 struct diff_dir_cb_arg *a = arg;
1997 struct got_object_id blob_id, commit_id;
1998 unsigned char status;
2000 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2001 return got_error(GOT_ERR_CANCELLED);
2003 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2004 return NULL;
2006 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2007 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2008 if (got_fileindex_entry_has_file_on_disk(ie))
2009 status = GOT_STATUS_MISSING;
2010 else
2011 status = GOT_STATUS_DELETE;
2012 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2013 &commit_id);
2016 static const struct got_error *
2017 status_new(void *arg, struct dirent *de, const char *parent_path)
2019 const struct got_error *err = NULL;
2020 struct diff_dir_cb_arg *a = arg;
2021 char *path = NULL;
2023 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2024 return got_error(GOT_ERR_CANCELLED);
2026 if (de->d_type == DT_DIR)
2027 return NULL;
2029 /* XXX ignore symlinks for now */
2030 if (de->d_type == DT_LNK)
2031 return NULL;
2033 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2034 return NULL;
2036 if (parent_path[0]) {
2037 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2038 return got_error_from_errno("asprintf");
2039 } else {
2040 path = de->d_name;
2043 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2044 NULL, NULL);
2045 if (parent_path[0])
2046 free(path);
2047 return err;
2050 const struct got_error *
2051 got_worktree_status(struct got_worktree *worktree, const char *path,
2052 struct got_repository *repo, got_worktree_status_cb status_cb,
2053 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2055 const struct got_error *err = NULL;
2056 DIR *workdir = NULL;
2057 char *fileindex_path = NULL;
2058 struct got_fileindex *fileindex = NULL;
2059 FILE *index = NULL;
2060 struct got_fileindex_diff_dir_cb fdiff_cb;
2061 struct diff_dir_cb_arg arg;
2062 char *ondisk_path = NULL;
2064 fileindex = got_fileindex_alloc();
2065 if (fileindex == NULL) {
2066 err = got_error_from_errno("got_fileindex_alloc");
2067 goto done;
2070 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2071 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2072 err = got_error_from_errno("asprintf");
2073 fileindex_path = NULL;
2074 goto done;
2077 index = fopen(fileindex_path, "rb");
2078 if (index == NULL) {
2079 if (errno != ENOENT) {
2080 err = got_error_from_errno2("fopen", fileindex_path);
2081 goto done;
2083 } else {
2084 err = got_fileindex_read(fileindex, index);
2085 fclose(index);
2086 if (err)
2087 goto done;
2090 if (asprintf(&ondisk_path, "%s%s%s",
2091 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2092 err = got_error_from_errno("asprintf");
2093 goto done;
2095 workdir = opendir(ondisk_path);
2096 if (workdir == NULL) {
2097 if (errno == ENOTDIR || errno == ENOENT) {
2098 struct got_fileindex_entry *ie;
2099 ie = got_fileindex_entry_get(fileindex, path);
2100 if (ie == NULL) {
2101 err = got_error(GOT_ERR_BAD_PATH);
2102 goto done;
2104 err = report_file_status(ie, ondisk_path,
2105 status_cb, status_arg, repo);
2106 goto done;
2107 } else {
2108 err = got_error_from_errno2("opendir", ondisk_path);
2109 goto done;
2112 fdiff_cb.diff_old_new = status_old_new;
2113 fdiff_cb.diff_old = status_old;
2114 fdiff_cb.diff_new = status_new;
2115 arg.fileindex = fileindex;
2116 arg.worktree = worktree;
2117 arg.status_path = path;
2118 arg.status_path_len = strlen(path);
2119 arg.repo = repo;
2120 arg.status_cb = status_cb;
2121 arg.status_arg = status_arg;
2122 arg.cancel_cb = cancel_cb;
2123 arg.cancel_arg = cancel_arg;
2124 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2125 path, repo, &fdiff_cb, &arg);
2126 done:
2127 if (workdir)
2128 closedir(workdir);
2129 free(ondisk_path);
2130 free(fileindex_path);
2131 got_fileindex_free(fileindex);
2132 return err;
2135 const struct got_error *
2136 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2137 const char *arg)
2139 const struct got_error *err = NULL;
2140 char *resolved, *path = NULL;
2141 size_t len;
2143 *wt_path = NULL;
2145 resolved = realpath(arg, NULL);
2146 if (resolved == NULL)
2147 return got_error_from_errno2("realpath", arg);
2149 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2150 strlen(got_worktree_get_root_path(worktree)))) {
2151 err = got_error(GOT_ERR_BAD_PATH);
2152 goto done;
2155 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2156 err = got_path_skip_common_ancestor(&path,
2157 got_worktree_get_root_path(worktree), resolved);
2158 if (err)
2159 goto done;
2160 } else {
2161 path = strdup("");
2162 if (path == NULL) {
2163 err = got_error_from_errno("strdup");
2164 goto done;
2168 /* XXX status walk can't deal with trailing slash! */
2169 len = strlen(path);
2170 while (path[len - 1] == '/') {
2171 path[len - 1] = '\0';
2172 len--;
2174 done:
2175 free(resolved);
2176 if (err == NULL)
2177 *wt_path = path;
2178 else
2179 free(path);
2180 return err;
2183 static const struct got_error *
2184 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2185 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2186 struct got_repository *repo)
2188 const struct got_error *err = NULL;
2189 struct got_fileindex_entry *ie;
2191 /* Re-adding an existing entry is a no-op. */
2192 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2193 return NULL;
2195 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2196 if (err)
2197 return err;
2199 err = got_fileindex_entry_add(fileindex, ie);
2200 if (err) {
2201 got_fileindex_entry_free(ie);
2202 return err;
2205 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2208 const struct got_error *
2209 got_worktree_schedule_add(struct got_worktree *worktree,
2210 struct got_pathlist_head *ondisk_paths,
2211 got_worktree_status_cb status_cb, void *status_arg,
2212 struct got_repository *repo)
2214 struct got_fileindex *fileindex = NULL;
2215 char *fileindex_path = NULL;
2216 FILE *index = NULL;
2217 const struct got_error *err = NULL, *sync_err, *unlockerr;
2218 struct got_pathlist_entry *pe;
2220 err = lock_worktree(worktree, LOCK_EX);
2221 if (err)
2222 return err;
2225 fileindex = got_fileindex_alloc();
2226 if (fileindex == NULL) {
2227 err = got_error_from_errno("got_fileindex_alloc");
2228 goto done;
2231 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2232 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2233 err = got_error_from_errno("asprintf");
2234 fileindex_path = NULL;
2235 goto done;
2238 index = fopen(fileindex_path, "rb");
2239 if (index == NULL) {
2240 err = got_error_from_errno2("fopen", fileindex_path);
2241 goto done;
2244 err = got_fileindex_read(fileindex, index);
2245 if (err)
2246 goto done;
2248 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2249 char *relpath;
2250 err = got_path_skip_common_ancestor(&relpath,
2251 got_worktree_get_root_path(worktree), pe->path);
2252 if (err)
2253 break;
2254 err = schedule_addition(pe->path, fileindex, relpath,
2255 status_cb, status_arg, repo);
2256 free(relpath);
2257 if (err)
2258 break;
2260 sync_err = sync_fileindex(fileindex, fileindex_path);
2261 if (sync_err && err == NULL)
2262 err = sync_err;
2263 done:
2264 if (index) {
2265 if (fclose(index) != 0 && err == NULL)
2266 err = got_error_from_errno("fclose");
2268 if (fileindex)
2269 got_fileindex_free(fileindex);
2270 unlockerr = lock_worktree(worktree, LOCK_SH);
2271 if (unlockerr && err == NULL)
2272 err = unlockerr;
2273 return err;
2276 static const struct got_error *
2277 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2278 const char *relpath, int delete_local_mods,
2279 got_worktree_status_cb status_cb, void *status_arg,
2280 struct got_repository *repo)
2282 const struct got_error *err = NULL;
2283 struct got_fileindex_entry *ie = NULL;
2284 unsigned char status;
2285 struct stat sb;
2287 ie = got_fileindex_entry_get(fileindex, relpath);
2288 if (ie == NULL)
2289 return got_error(GOT_ERR_BAD_PATH);
2291 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2292 if (err)
2293 return err;
2295 if (status != GOT_STATUS_NO_CHANGE) {
2296 if (status == GOT_STATUS_DELETE)
2297 return got_error_set_errno(ENOENT, ondisk_path);
2298 if (status != GOT_STATUS_MODIFY)
2299 return got_error(GOT_ERR_FILE_STATUS);
2300 if (!delete_local_mods)
2301 return got_error(GOT_ERR_FILE_MODIFIED);
2304 if (unlink(ondisk_path) != 0)
2305 return got_error_from_errno2("unlink", ondisk_path);
2307 got_fileindex_entry_mark_deleted_from_disk(ie);
2308 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2311 const struct got_error *
2312 got_worktree_schedule_delete(struct got_worktree *worktree,
2313 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2314 got_worktree_status_cb status_cb, void *status_arg,
2315 struct got_repository *repo)
2317 struct got_fileindex *fileindex = NULL;
2318 char *fileindex_path = NULL;
2319 FILE *index = NULL;
2320 const struct got_error *err = NULL, *sync_err, *unlockerr;
2321 struct got_pathlist_entry *pe;
2323 err = lock_worktree(worktree, LOCK_EX);
2324 if (err)
2325 return err;
2327 fileindex = got_fileindex_alloc();
2328 if (fileindex == NULL) {
2329 err = got_error_from_errno("got_fileindex_alloc");
2330 goto done;
2333 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2334 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2335 err = got_error_from_errno("asprintf");
2336 fileindex_path = NULL;
2337 goto done;
2340 index = fopen(fileindex_path, "rb");
2341 if (index == NULL) {
2342 err = got_error_from_errno2("fopen", fileindex_path);
2343 goto done;
2346 err = got_fileindex_read(fileindex, index);
2347 if (err)
2348 goto done;
2350 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2351 char *relpath;
2352 err = got_path_skip_common_ancestor(&relpath,
2353 got_worktree_get_root_path(worktree), pe->path);
2354 if (err)
2355 break;
2356 err = schedule_for_deletion(pe->path, fileindex, relpath,
2357 delete_local_mods, status_cb, status_arg, repo);
2358 free(relpath);
2359 if (err)
2360 break;
2362 sync_err = sync_fileindex(fileindex, fileindex_path);
2363 if (sync_err && err == NULL)
2364 err = sync_err;
2365 done:
2366 if (index) {
2367 if (fclose(index) != 0 && err == NULL)
2368 err = got_error_from_errno("fclose");
2370 if (fileindex)
2371 got_fileindex_free(fileindex);
2372 unlockerr = lock_worktree(worktree, LOCK_SH);
2373 if (unlockerr && err == NULL)
2374 err = unlockerr;
2375 return err;
2378 static const struct got_error *
2379 revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
2380 const char *ondisk_path,
2381 got_worktree_checkout_cb progress_cb, void *progress_arg,
2382 struct got_repository *repo)
2384 const struct got_error *err = NULL;
2385 char *relpath = NULL, *parent_path = NULL;
2386 struct got_fileindex_entry *ie;
2387 struct got_tree_object *tree = NULL;
2388 struct got_object_id *tree_id = NULL;
2389 const struct got_tree_entry *te;
2390 char *tree_path = NULL, *te_name;
2391 struct got_blob_object *blob = NULL;
2392 unsigned char status;
2393 struct stat sb;
2395 err = got_path_skip_common_ancestor(&relpath,
2396 got_worktree_get_root_path(worktree), ondisk_path);
2397 if (err)
2398 goto done;
2400 ie = got_fileindex_entry_get(fileindex, relpath);
2401 if (ie == NULL) {
2402 err = got_error(GOT_ERR_BAD_PATH);
2403 goto done;
2406 /* Construct in-repository path of tree which contains this blob. */
2407 err = got_path_dirname(&parent_path, ie->path);
2408 if (err) {
2409 if (err->code != GOT_ERR_BAD_PATH)
2410 goto done;
2411 parent_path = strdup("/");
2412 if (parent_path == NULL) {
2413 err = got_error_from_errno("strdup");
2414 goto done;
2417 if (got_path_is_root_dir(worktree->path_prefix)) {
2418 tree_path = strdup(parent_path);
2419 if (tree_path == NULL) {
2420 err = got_error_from_errno("strdup");
2421 goto done;
2423 } else {
2424 if (got_path_is_root_dir(parent_path)) {
2425 tree_path = strdup(worktree->path_prefix);
2426 if (tree_path == NULL) {
2427 err = got_error_from_errno("strdup");
2428 goto done;
2430 } else {
2431 if (asprintf(&tree_path, "%s/%s",
2432 worktree->path_prefix, parent_path) == -1) {
2433 err = got_error_from_errno("asprintf");
2434 goto done;
2439 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2440 tree_path);
2441 if (err)
2442 goto done;
2444 err = got_object_open_as_tree(&tree, repo, tree_id);
2445 if (err)
2446 goto done;
2448 te_name = basename(ie->path);
2449 if (te_name == NULL) {
2450 err = got_error_from_errno2("basename", ie->path);
2451 goto done;
2454 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2455 if (err)
2456 goto done;
2458 te = got_object_tree_find_entry(tree, te_name);
2459 if (te == NULL && status != GOT_STATUS_ADD) {
2460 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2461 goto done;
2464 switch (status) {
2465 case GOT_STATUS_ADD:
2466 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2467 got_fileindex_entry_remove(fileindex, ie);
2468 break;
2469 case GOT_STATUS_DELETE:
2470 case GOT_STATUS_MODIFY:
2471 case GOT_STATUS_CONFLICT:
2472 case GOT_STATUS_MISSING: {
2473 struct got_object_id id;
2474 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2475 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2476 if (err)
2477 goto done;
2478 err = install_blob(worktree, ondisk_path, ie->path,
2479 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2480 progress_arg);
2481 if (err)
2482 goto done;
2483 if (status == GOT_STATUS_DELETE) {
2484 err = update_blob_fileindex_entry(worktree,
2485 fileindex, ie, ondisk_path, ie->path, blob, 1);
2486 if (err)
2487 goto done;
2489 break;
2491 default:
2492 goto done;
2494 done:
2495 free(relpath);
2496 free(parent_path);
2497 free(tree_path);
2498 if (blob)
2499 got_object_blob_close(blob);
2500 if (tree)
2501 got_object_tree_close(tree);
2502 free(tree_id);
2503 return err;
2506 const struct got_error *
2507 got_worktree_revert(struct got_worktree *worktree,
2508 struct got_pathlist_head *ondisk_paths,
2509 got_worktree_checkout_cb progress_cb, void *progress_arg,
2510 struct got_repository *repo)
2512 struct got_fileindex *fileindex = NULL;
2513 char *fileindex_path = NULL;
2514 FILE *index = NULL;
2515 const struct got_error *err = NULL, *unlockerr = NULL;
2516 const struct got_error *sync_err = NULL;
2517 struct got_pathlist_entry *pe;
2519 err = lock_worktree(worktree, LOCK_EX);
2520 if (err)
2521 return err;
2523 fileindex = got_fileindex_alloc();
2524 if (fileindex == NULL) {
2525 err = got_error_from_errno("got_fileindex_alloc");
2526 goto done;
2529 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2530 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2531 err = got_error_from_errno("asprintf");
2532 fileindex_path = NULL;
2533 goto done;
2536 index = fopen(fileindex_path, "rb");
2537 if (index == NULL) {
2538 err = got_error_from_errno2("fopen", fileindex_path);
2539 goto done;
2542 err = got_fileindex_read(fileindex, index);
2543 if (err)
2544 goto done;
2546 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2547 err = revert_file(worktree, fileindex, pe->path,
2548 progress_cb, progress_arg, repo);
2549 if (err)
2550 break;
2552 sync_err = sync_fileindex(fileindex, fileindex_path);
2553 if (sync_err && err == NULL)
2554 err = sync_err;
2555 done:
2556 if (index) {
2557 if (fclose(index) != 0 && err == NULL)
2558 err = got_error_from_errno("fclose");
2560 if (fileindex)
2561 got_fileindex_free(fileindex);
2562 unlockerr = lock_worktree(worktree, LOCK_SH);
2563 if (unlockerr && err == NULL)
2564 err = unlockerr;
2565 return err;
2568 static void
2569 free_commitable(struct got_commitable *ct)
2571 free(ct->path);
2572 free(ct->in_repo_path);
2573 free(ct->ondisk_path);
2574 free(ct->blob_id);
2575 free(ct->base_blob_id);
2576 free(ct->base_commit_id);
2577 free(ct);
2580 struct collect_commitables_arg {
2581 struct got_pathlist_head *commitable_paths;
2582 struct got_repository *repo;
2583 struct got_worktree *worktree;
2586 static const struct got_error *
2587 collect_commitables(void *arg, unsigned char status, const char *relpath,
2588 struct got_object_id *blob_id, struct got_object_id *commit_id)
2590 struct collect_commitables_arg *a = arg;
2591 const struct got_error *err = NULL;
2592 struct got_commitable *ct = NULL;
2593 struct got_pathlist_entry *new = NULL;
2594 char *parent_path = NULL, *path = NULL;
2595 struct stat sb;
2597 if (status == GOT_STATUS_CONFLICT)
2598 return got_error(GOT_ERR_COMMIT_CONFLICT);
2600 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2601 status != GOT_STATUS_DELETE)
2602 return NULL;
2604 if (asprintf(&path, "/%s", relpath) == -1) {
2605 err = got_error_from_errno("asprintf");
2606 goto done;
2608 if (strcmp(path, "/") == 0) {
2609 parent_path = strdup("");
2610 if (parent_path == NULL)
2611 return got_error_from_errno("strdup");
2612 } else {
2613 err = got_path_dirname(&parent_path, path);
2614 if (err)
2615 return err;
2618 ct = calloc(1, sizeof(*ct));
2619 if (ct == NULL) {
2620 err = got_error_from_errno("calloc");
2621 goto done;
2624 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2625 relpath) == -1) {
2626 err = got_error_from_errno("asprintf");
2627 goto done;
2629 if (status == GOT_STATUS_DELETE) {
2630 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2631 } else {
2632 if (lstat(ct->ondisk_path, &sb) != 0) {
2633 err = got_error_from_errno2("lstat", ct->ondisk_path);
2634 goto done;
2636 ct->mode = sb.st_mode;
2639 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2640 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2641 relpath) == -1) {
2642 err = got_error_from_errno("asprintf");
2643 goto done;
2646 ct->status = status;
2647 ct->blob_id = NULL; /* will be filled in when blob gets created */
2648 if (ct->status != GOT_STATUS_ADD) {
2649 ct->base_blob_id = got_object_id_dup(blob_id);
2650 if (ct->base_blob_id == NULL) {
2651 err = got_error_from_errno("got_object_id_dup");
2652 goto done;
2654 ct->base_commit_id = got_object_id_dup(commit_id);
2655 if (ct->base_commit_id == NULL) {
2656 err = got_error_from_errno("got_object_id_dup");
2657 goto done;
2660 ct->path = strdup(path);
2661 if (ct->path == NULL) {
2662 err = got_error_from_errno("strdup");
2663 goto done;
2665 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2666 done:
2667 if (ct && (err || new == NULL))
2668 free_commitable(ct);
2669 free(parent_path);
2670 free(path);
2671 return err;
2674 static const struct got_error *write_tree(struct got_object_id **,
2675 struct got_tree_object *, const char *, struct got_pathlist_head *,
2676 got_worktree_status_cb status_cb, void *status_arg,
2677 struct got_repository *);
2679 static const struct got_error *
2680 write_subtree(struct got_object_id **new_subtree_id,
2681 struct got_tree_entry *te, const char *parent_path,
2682 struct got_pathlist_head *commitable_paths,
2683 got_worktree_status_cb status_cb, void *status_arg,
2684 struct got_repository *repo)
2686 const struct got_error *err = NULL;
2687 struct got_tree_object *subtree;
2688 char *subpath;
2690 if (asprintf(&subpath, "%s%s%s", parent_path,
2691 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2692 return got_error_from_errno("asprintf");
2694 err = got_object_open_as_tree(&subtree, repo, te->id);
2695 if (err)
2696 return err;
2698 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2699 status_cb, status_arg, repo);
2700 got_object_tree_close(subtree);
2701 free(subpath);
2702 return err;
2705 static const struct got_error *
2706 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2708 const struct got_error *err = NULL;
2709 char *ct_parent_path = NULL;
2711 *match = 0;
2713 if (strchr(ct->path, '/') == NULL) {
2714 *match = got_path_is_root_dir(path);
2715 return NULL;
2718 err = got_path_dirname(&ct_parent_path, ct->path);
2719 if (err)
2720 return err;
2721 *match = (strcmp(path, ct_parent_path) == 0);
2722 free(ct_parent_path);
2723 return err;
2726 static mode_t
2727 get_ct_file_mode(struct got_commitable *ct)
2729 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2732 static const struct got_error *
2733 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2734 struct got_tree_entry *te, struct got_commitable *ct)
2736 const struct got_error *err = NULL;
2738 *new_te = NULL;
2740 err = got_object_tree_entry_dup(new_te, te);
2741 if (err)
2742 goto done;
2744 (*new_te)->mode = get_ct_file_mode(ct);
2746 free((*new_te)->id);
2747 (*new_te)->id = got_object_id_dup(ct->blob_id);
2748 if ((*new_te)->id == NULL) {
2749 err = got_error_from_errno("got_object_id_dup");
2750 goto done;
2752 done:
2753 if (err && *new_te) {
2754 got_object_tree_entry_close(*new_te);
2755 *new_te = NULL;
2757 return err;
2760 static const struct got_error *
2761 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2762 struct got_commitable *ct)
2764 const struct got_error *err = NULL;
2765 char *ct_name;
2767 *new_te = NULL;
2769 *new_te = calloc(1, sizeof(**new_te));
2770 if (*new_te == NULL)
2771 return got_error_from_errno("calloc");
2773 ct_name = basename(ct->path);
2774 if (ct_name == NULL) {
2775 err = got_error_from_errno2("basename", ct->path);
2776 goto done;
2778 (*new_te)->name = strdup(ct_name);
2779 if ((*new_te)->name == NULL) {
2780 err = got_error_from_errno("strdup");
2781 goto done;
2784 (*new_te)->mode = get_ct_file_mode(ct);
2786 (*new_te)->id = got_object_id_dup(ct->blob_id);
2787 if ((*new_te)->id == NULL) {
2788 err = got_error_from_errno("got_object_id_dup");
2789 goto done;
2791 done:
2792 if (err && *new_te) {
2793 got_object_tree_entry_close(*new_te);
2794 *new_te = NULL;
2796 return err;
2799 static const struct got_error *
2800 insert_tree_entry(struct got_tree_entry *new_te,
2801 struct got_pathlist_head *paths)
2803 const struct got_error *err = NULL;
2804 struct got_pathlist_entry *new_pe;
2806 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2807 if (err)
2808 return err;
2809 if (new_pe == NULL)
2810 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2811 return NULL;
2814 static const struct got_error *
2815 report_ct_status(struct got_commitable *ct,
2816 got_worktree_status_cb status_cb, void *status_arg)
2818 const char *ct_path = ct->path;
2819 while (ct_path[0] == '/')
2820 ct_path++;
2821 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2824 static const struct got_error *
2825 match_modified_subtree(int *modified, struct got_tree_entry *te,
2826 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2828 const struct got_error *err = NULL;
2829 struct got_pathlist_entry *pe;
2830 char *te_path;
2832 *modified = 0;
2834 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2835 got_path_is_root_dir(base_tree_path) ? "" : "/",
2836 te->name) == -1)
2837 return got_error_from_errno("asprintf");
2839 TAILQ_FOREACH(pe, commitable_paths, entry) {
2840 struct got_commitable *ct = pe->data;
2841 *modified = got_path_is_child(ct->in_repo_path, te_path,
2842 strlen(te_path));
2843 if (*modified)
2844 break;
2847 free(te_path);
2848 return err;
2851 static const struct got_error *
2852 match_deleted_or_modified_ct(struct got_commitable **ctp,
2853 struct got_tree_entry *te, const char *base_tree_path,
2854 struct got_pathlist_head *commitable_paths)
2856 const struct got_error *err = NULL;
2857 struct got_pathlist_entry *pe;
2859 *ctp = NULL;
2861 TAILQ_FOREACH(pe, commitable_paths, entry) {
2862 struct got_commitable *ct = pe->data;
2863 char *ct_name = NULL;
2864 int path_matches;
2866 if (ct->status != GOT_STATUS_MODIFY &&
2867 ct->status != GOT_STATUS_DELETE)
2868 continue;
2870 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2871 continue;
2873 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2874 if (err)
2875 return err;
2876 if (!path_matches)
2877 continue;
2879 ct_name = basename(pe->path);
2880 if (ct_name == NULL)
2881 return got_error_from_errno2("basename", pe->path);
2883 if (strcmp(te->name, ct_name) != 0)
2884 continue;
2886 *ctp = ct;
2887 break;
2890 return err;
2893 static const struct got_error *
2894 write_tree(struct got_object_id **new_tree_id,
2895 struct got_tree_object *base_tree, const char *path_base_tree,
2896 struct got_pathlist_head *commitable_paths,
2897 got_worktree_status_cb status_cb, void *status_arg,
2898 struct got_repository *repo)
2900 const struct got_error *err = NULL;
2901 const struct got_tree_entries *base_entries = NULL;
2902 struct got_pathlist_head paths;
2903 struct got_tree_entries new_tree_entries;
2904 struct got_tree_entry *te, *new_te = NULL;
2905 struct got_pathlist_entry *pe;
2907 TAILQ_INIT(&paths);
2908 new_tree_entries.nentries = 0;
2909 SIMPLEQ_INIT(&new_tree_entries.head);
2911 /* Insert, and recurse into, newly added entries first. */
2912 TAILQ_FOREACH(pe, commitable_paths, entry) {
2913 struct got_commitable *ct = pe->data;
2914 char *child_path = NULL, *slash;
2916 if (ct->status != GOT_STATUS_ADD ||
2917 (ct->flags & GOT_COMMITABLE_ADDED))
2918 continue;
2920 if (!got_path_is_child(pe->path, path_base_tree,
2921 strlen(path_base_tree)))
2922 continue;
2924 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2925 pe->path);
2926 if (err)
2927 goto done;
2929 slash = strchr(child_path, '/');
2930 if (slash == NULL) {
2931 err = alloc_added_blob_tree_entry(&new_te, ct);
2932 if (err)
2933 goto done;
2934 err = report_ct_status(ct, status_cb, status_arg);
2935 if (err)
2936 goto done;
2937 ct->flags |= GOT_COMMITABLE_ADDED;
2938 } else {
2939 char *subtree_path;
2941 *slash = '\0'; /* trim trailing path components */
2942 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2943 got_path_is_root_dir(path_base_tree) ? "" : "/",
2944 child_path) == -1) {
2945 err = got_error_from_errno("asprintf");
2946 goto done;
2949 new_te = calloc(1, sizeof(*new_te));
2950 new_te->mode = S_IFDIR;
2951 new_te->name = strdup(child_path);
2952 if (new_te->name == NULL) {
2953 err = got_error_from_errno("strdup");
2954 got_object_tree_entry_close(new_te);
2955 new_te = NULL;
2956 goto done;
2958 err = write_tree(&new_te->id, NULL, subtree_path,
2959 commitable_paths, status_cb, status_arg, repo);
2960 free(subtree_path);
2961 if (err) {
2962 got_object_tree_entry_close(new_te);
2963 new_te = NULL;
2964 goto done;
2967 err = insert_tree_entry(new_te, &paths);
2968 if (err)
2969 goto done;
2972 if (base_tree) {
2973 /* Handle modified and deleted entries. */
2974 base_entries = got_object_tree_get_entries(base_tree);
2975 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2976 struct got_commitable *ct = NULL;
2978 if (S_ISDIR(te->mode)) {
2979 int modified;
2980 err = got_object_tree_entry_dup(&new_te, te);
2981 if (err)
2982 goto done;
2983 err = match_modified_subtree(&modified, te,
2984 path_base_tree, commitable_paths);
2985 if (err)
2986 goto done;
2987 /* Avoid recursion into unmodified subtrees. */
2988 if (modified) {
2989 free(new_te->id);
2990 err = write_subtree(&new_te->id, te,
2991 path_base_tree, commitable_paths,
2992 status_cb, status_arg, repo);
2993 if (err)
2994 goto done;
2996 err = insert_tree_entry(new_te, &paths);
2997 if (err)
2998 goto done;
2999 continue;
3002 err = match_deleted_or_modified_ct(&ct, te,
3003 path_base_tree, commitable_paths);
3004 if (ct) {
3005 /* NB: Deleted entries get dropped here. */
3006 if (ct->status == GOT_STATUS_MODIFY) {
3007 err = alloc_modified_blob_tree_entry(
3008 &new_te, te, ct);
3009 if (err)
3010 goto done;
3011 err = insert_tree_entry(new_te, &paths);
3012 if (err)
3013 goto done;
3015 err = report_ct_status(ct, status_cb,
3016 status_arg);
3017 if (err)
3018 goto done;
3019 } else {
3020 /* Entry is unchanged; just copy it. */
3021 err = got_object_tree_entry_dup(&new_te, te);
3022 if (err)
3023 goto done;
3024 err = insert_tree_entry(new_te, &paths);
3025 if (err)
3026 goto done;
3031 /* Write new list of entries; deleted entries have been dropped. */
3032 TAILQ_FOREACH(pe, &paths, entry) {
3033 struct got_tree_entry *te = pe->data;
3034 new_tree_entries.nentries++;
3035 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3037 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3038 done:
3039 got_object_tree_entries_close(&new_tree_entries);
3040 got_pathlist_free(&paths);
3041 return err;
3044 static const struct got_error *
3045 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3046 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3048 const struct got_error *err = NULL, *sync_err;
3049 char *fileindex_path = NULL;
3050 struct got_fileindex *fileindex = NULL;
3051 struct got_pathlist_entry *pe;
3053 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3054 if (err)
3055 return err;
3057 TAILQ_FOREACH(pe, commitable_paths, entry) {
3058 struct got_fileindex_entry *ie;
3059 struct got_commitable *ct = pe->data;
3061 ie = got_fileindex_entry_get(fileindex, pe->path);
3062 if (ie) {
3063 if (ct->status == GOT_STATUS_DELETE) {
3064 got_fileindex_entry_remove(fileindex, ie);
3065 got_fileindex_entry_free(ie);
3066 } else
3067 err = got_fileindex_entry_update(ie,
3068 ct->ondisk_path, ct->blob_id->sha1,
3069 new_base_commit_id->sha1, 1);
3070 } else {
3071 err = got_fileindex_entry_alloc(&ie,
3072 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3073 new_base_commit_id->sha1);
3074 if (err)
3075 break;
3076 err = got_fileindex_entry_add(fileindex, ie);
3077 if (err)
3078 break;
3081 sync_err = sync_fileindex(fileindex, fileindex_path);
3082 if (sync_err && err == NULL)
3083 err = sync_err;
3084 free(fileindex_path);
3085 got_fileindex_free(fileindex);
3086 return err;
3089 static const struct got_error *
3090 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3091 struct got_object_id *head_commit_id)
3093 const struct got_error *err = NULL;
3094 struct got_object_id *id_in_head = NULL, *id = NULL;
3095 struct got_commit_object *commit = NULL;
3096 char *path = NULL;
3097 const char *ct_path = ct->in_repo_path;
3099 while (ct_path[0] == '/')
3100 ct_path++;
3103 * Ensure that no modifications were made to files *and their parents*
3104 * in commits between the file's base commit and the branch head.
3106 * Checking the parents is important for detecting conflicting tree
3107 * configurations (files or parent folders might have been moved,
3108 * deleted, added again, etc.). Such changes need to be merged with
3109 * local changes before a commit can occur.
3111 * The implication is that the file's (parent) entry in the root
3112 * directory must have the same ID in all relevant commits.
3114 if (ct->status != GOT_STATUS_ADD) {
3115 struct got_object_qid *pid;
3116 char *slash;
3117 struct got_object_id *root_entry_id = NULL;
3119 /* Trivial case: base commit == head commit */
3120 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) == 0)
3121 return NULL;
3123 /* Compute the path to the root directory's entry. */
3124 path = strdup(ct_path);
3125 if (path == NULL) {
3126 err = got_error_from_errno("strdup");
3127 goto done;
3129 slash = strchr(path, '/');
3130 if (slash)
3131 *slash = '\0';
3133 err = got_object_open_as_commit(&commit, repo, head_commit_id);
3134 if (err)
3135 goto done;
3137 err = got_object_id_by_path(&root_entry_id, repo,
3138 head_commit_id, path);
3139 if (err)
3140 goto done;
3142 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3143 while (pid) {
3144 struct got_commit_object *pcommit;
3146 err = got_object_id_by_path(&id, repo, pid->id, path);
3147 if (err) {
3148 if (err->code != GOT_ERR_NO_TREE_ENTRY)
3149 goto done;
3150 err = NULL;
3151 break;
3154 err = got_object_id_by_path(&id, repo, pid->id, path);
3155 if (err)
3156 goto done;
3158 if (got_object_id_cmp(id, root_entry_id) != 0) {
3159 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3160 break;
3163 if (got_object_id_cmp(pid->id, ct->base_commit_id) == 0)
3164 break; /* all relevant commits scanned */
3166 err = got_object_open_as_commit(&pcommit, repo,
3167 pid->id);
3168 if (err)
3169 goto done;
3171 got_object_commit_close(commit);
3172 commit = pcommit;
3173 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(
3174 commit));
3176 } else {
3177 /* Require that added files don't exist in the branch head. */
3178 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3179 ct_path);
3180 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3181 goto done;
3182 err = id_in_head ? got_error(GOT_ERR_COMMIT_OUT_OF_DATE) : NULL;
3184 done:
3185 if (commit)
3186 got_object_commit_close(commit);
3187 free(id_in_head);
3188 free(id);
3189 free(path);
3190 return err;
3193 const struct got_error *
3194 got_worktree_commit(struct got_object_id **new_commit_id,
3195 struct got_worktree *worktree, const char *ondisk_path,
3196 const char *author, const char *committer,
3197 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3198 got_worktree_status_cb status_cb, void *status_arg,
3199 struct got_repository *repo)
3201 const struct got_error *err = NULL, *unlockerr = NULL;
3202 struct collect_commitables_arg cc_arg;
3203 struct got_pathlist_head commitable_paths;
3204 struct got_pathlist_entry *pe;
3205 char *relpath = NULL;
3206 const char *head_ref_name = NULL;
3207 struct got_reference *head_ref = NULL;
3208 struct got_commit_object *head_commit = NULL;
3209 struct got_object_id *head_commit_id = NULL;
3210 struct got_reference *head_ref2 = NULL;
3211 struct got_object_id *head_commit_id2 = NULL;
3212 struct got_tree_object *head_tree = NULL;
3213 struct got_object_id *new_tree_id = NULL;
3214 struct got_object_id_queue parent_ids;
3215 struct got_object_qid *pid = NULL;
3216 char *logmsg = NULL;
3218 *new_commit_id = NULL;
3220 TAILQ_INIT(&commitable_paths);
3221 SIMPLEQ_INIT(&parent_ids);
3223 if (ondisk_path) {
3224 err = got_path_skip_common_ancestor(&relpath,
3225 worktree->root_path, ondisk_path);
3226 if (err)
3227 return err;
3230 err = lock_worktree(worktree, LOCK_EX);
3231 if (err)
3232 goto done;
3234 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3235 if (err)
3236 goto done;
3237 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3238 if (err)
3239 goto done;
3241 cc_arg.commitable_paths = &commitable_paths;
3242 cc_arg.worktree = worktree;
3243 cc_arg.repo = repo;
3244 err = got_worktree_status(worktree, relpath ? relpath : "",
3245 repo, collect_commitables, &cc_arg, NULL, NULL);
3246 if (err)
3247 goto done;
3249 if (TAILQ_EMPTY(&commitable_paths)) {
3250 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3251 goto done;
3254 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3255 if (err)
3256 goto done;
3258 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3259 struct got_commitable *ct = pe->data;
3260 err = check_ct_out_of_date(ct, repo, head_commit_id);
3261 if (err)
3262 goto done;
3265 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3266 if (err)
3267 goto done;
3269 if (commit_msg_cb != NULL) {
3270 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3271 if (err)
3272 goto done;
3275 if (logmsg == NULL || strlen(logmsg) == 0) {
3276 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3277 goto done;
3280 /* Create blobs from added and modified files and record their IDs. */
3281 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3282 struct got_commitable *ct = pe->data;
3283 char *ondisk_path;
3285 if (ct->status != GOT_STATUS_ADD &&
3286 ct->status != GOT_STATUS_MODIFY)
3287 continue;
3289 if (asprintf(&ondisk_path, "%s/%s",
3290 worktree->root_path, pe->path) == -1) {
3291 err = got_error_from_errno("asprintf");
3292 goto done;
3294 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3295 free(ondisk_path);
3296 if (err)
3297 goto done;
3300 /* Recursively write new tree objects. */
3301 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3302 status_cb, status_arg, repo);
3303 if (err)
3304 goto done;
3306 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3307 if (err)
3308 goto done;
3309 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3310 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3311 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3312 got_object_qid_free(pid);
3313 if (logmsg != NULL)
3314 free(logmsg);
3315 if (err)
3316 goto done;
3318 /* Check if a concurrent commit to our branch has occurred. */
3319 head_ref_name = got_worktree_get_head_ref_name(worktree);
3320 if (head_ref_name == NULL) {
3321 err = got_error_from_errno("got_worktree_get_head_ref_name");
3322 goto done;
3324 /* Lock the reference here to prevent concurrent modification. */
3325 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3326 if (err)
3327 goto done;
3328 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3329 if (err)
3330 goto done;
3331 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3332 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3333 goto done;
3335 /* Update branch head in repository. */
3336 err = got_ref_change_ref(head_ref2, *new_commit_id);
3337 if (err)
3338 goto done;
3339 err = got_ref_write(head_ref2, repo);
3340 if (err)
3341 goto done;
3343 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3344 if (err)
3345 goto done;
3347 err = ref_base_commit(worktree, repo);
3348 if (err)
3349 goto done;
3351 err = update_fileindex_after_commit(&commitable_paths,
3352 *new_commit_id, worktree);
3353 if (err)
3354 goto done;
3355 done:
3356 unlockerr = lock_worktree(worktree, LOCK_SH);
3357 if (unlockerr && err == NULL)
3358 err = unlockerr;
3359 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3360 struct got_commitable *ct = pe->data;
3361 free_commitable(ct);
3363 got_pathlist_free(&commitable_paths);
3364 if (head_tree)
3365 got_object_tree_close(head_tree);
3366 if (head_commit)
3367 got_object_commit_close(head_commit);
3368 free(relpath);
3369 free(head_commit_id);
3370 free(head_commit_id2);
3371 if (head_ref)
3372 got_ref_close(head_ref);
3373 if (head_ref2) {
3374 unlockerr = got_ref_unlock(head_ref2);
3375 if (unlockerr && err == NULL)
3376 err = unlockerr;
3377 got_ref_close(head_ref2);
3379 return err;
3382 const char *
3383 got_commitable_get_path(struct got_commitable *ct)
3385 return ct->path;
3388 unsigned int
3389 got_commitable_get_status(struct got_commitable *ct)
3391 return ct->status;