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 blob2 acts as the common ancestor,
742 * blob1 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 *blob2, const char *ondisk_path,
748 const char *path, uint16_t st_mode, struct got_blob_object *blob1,
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 *f1 = NULL, *f2 = NULL;
755 char *blob1_path = NULL, *blob2_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-blob1", parent) == -1) {
777 err = got_error_from_errno("asprintf");
778 base_path = NULL;
779 goto done;
782 err = got_opentemp_named(&blob1_path, &f1, base_path);
783 if (err)
784 goto done;
785 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
786 if (err)
787 goto done;
789 free(base_path);
790 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
791 err = got_error_from_errno("asprintf");
792 base_path = NULL;
793 goto done;
796 err = got_opentemp_named(&blob2_path, &f2, base_path);
797 if (err)
798 goto done;
799 if (blob2) {
800 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
801 if (err)
802 goto done;
803 } else {
804 /*
805 * If the file has no blob, this is an "add vs add" conflict,
806 * and we simply use an empty ancestor file to make both files
807 * appear in the merged result in their entirety.
808 */
811 err = got_object_id_str(&id_str, worktree->base_commit_id);
812 if (err)
813 goto done;
814 if (asprintf(&label1, "commit %s", id_str) == -1) {
815 err = got_error_from_errno("asprintf");
816 goto done;
819 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
820 blob2_path, ondisk_path, label1, path);
821 if (err)
822 goto done;
824 (*progress_cb)(progress_arg,
825 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
827 if (fsync(merged_fd) != 0) {
828 err = got_error_from_errno("fsync");
829 goto done;
832 /* Check if a clean merge has subsumed all local changes. */
833 if (overlapcnt == 0) {
834 err = check_files_equal(local_changes_subsumed, blob1_path,
835 merged_path);
836 if (err)
837 goto done;
840 if (chmod(merged_path, st_mode) != 0) {
841 err = got_error_from_errno2("chmod", merged_path);
842 goto done;
845 if (rename(merged_path, ondisk_path) != 0) {
846 err = got_error_from_errno3("rename", merged_path,
847 ondisk_path);
848 unlink(merged_path);
849 goto done;
852 done:
853 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
854 err = got_error_from_errno("close");
855 if (f1 && fclose(f1) != 0 && err == NULL)
856 err = got_error_from_errno("fclose");
857 if (f2 && fclose(f2) != 0 && err == NULL)
858 err = got_error_from_errno("fclose");
859 free(merged_path);
860 free(base_path);
861 if (blob1_path) {
862 unlink(blob1_path);
863 free(blob1_path);
865 if (blob2_path) {
866 unlink(blob2_path);
867 free(blob2_path);
869 free(id_str);
870 free(label1);
871 return err;
874 static const struct got_error *
875 update_blob_fileindex_entry(struct got_worktree *worktree,
876 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
877 const char *ondisk_path, const char *path, struct got_blob_object *blob,
878 int update_timestamps)
880 const struct got_error *err = NULL;
882 if (ie == NULL)
883 ie = got_fileindex_entry_get(fileindex, path);
884 if (ie)
885 err = got_fileindex_entry_update(ie, ondisk_path,
886 blob->id.sha1, worktree->base_commit_id->sha1,
887 update_timestamps);
888 else {
889 struct got_fileindex_entry *new_ie;
890 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
891 path, blob->id.sha1, worktree->base_commit_id->sha1);
892 if (!err)
893 err = got_fileindex_entry_add(fileindex, new_ie);
895 return err;
898 static const struct got_error *
899 install_blob(struct got_worktree *worktree, const char *ondisk_path,
900 const char *path, uint16_t te_mode, uint16_t st_mode,
901 struct got_blob_object *blob, int restoring_missing_file,
902 int reverting_versioned_file, struct got_repository *repo,
903 got_worktree_checkout_cb progress_cb, void *progress_arg)
905 const struct got_error *err = NULL;
906 int fd = -1;
907 size_t len, hdrlen;
908 int update = 0;
909 char *tmppath = NULL;
911 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
912 GOT_DEFAULT_FILE_MODE);
913 if (fd == -1) {
914 if (errno == ENOENT) {
915 char *parent = dirname(path);
916 if (parent == NULL)
917 return got_error_from_errno2("dirname", path);
918 err = add_dir_on_disk(worktree, parent);
919 if (err)
920 return err;
921 fd = open(ondisk_path,
922 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
923 GOT_DEFAULT_FILE_MODE);
924 if (fd == -1)
925 return got_error_from_errno2("open",
926 ondisk_path);
927 } else if (errno == EEXIST) {
928 if (!S_ISREG(st_mode)) {
929 /* TODO file is obstructed; do something */
930 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
931 goto done;
932 } else {
933 err = got_opentemp_named_fd(&tmppath, &fd,
934 ondisk_path);
935 if (err)
936 goto done;
937 update = 1;
939 } else
940 return got_error_from_errno2("open", ondisk_path);
943 if (restoring_missing_file)
944 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
945 else if (reverting_versioned_file)
946 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
947 else
948 (*progress_cb)(progress_arg,
949 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
951 hdrlen = got_object_blob_get_hdrlen(blob);
952 do {
953 const uint8_t *buf = got_object_blob_get_read_buf(blob);
954 err = got_object_blob_read_block(&len, blob);
955 if (err)
956 break;
957 if (len > 0) {
958 /* Skip blob object header first time around. */
959 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
960 if (outlen == -1) {
961 err = got_error_from_errno("write");
962 goto done;
963 } else if (outlen != len - hdrlen) {
964 err = got_error(GOT_ERR_IO);
965 goto done;
967 hdrlen = 0;
969 } while (len != 0);
971 if (fsync(fd) != 0) {
972 err = got_error_from_errno("fsync");
973 goto done;
976 if (update) {
977 if (rename(tmppath, ondisk_path) != 0) {
978 err = got_error_from_errno3("rename", tmppath,
979 ondisk_path);
980 unlink(tmppath);
981 goto done;
985 if (te_mode & S_IXUSR) {
986 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
987 err = got_error_from_errno2("chmod", ondisk_path);
988 goto done;
990 } else {
991 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
992 err = got_error_from_errno2("chmod", ondisk_path);
993 goto done;
997 done:
998 if (fd != -1 && close(fd) != 0 && err == NULL)
999 err = got_error_from_errno("close");
1000 free(tmppath);
1001 return err;
1004 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
1005 static const struct got_error *
1006 get_modified_file_content_status(unsigned char *status, FILE *f)
1008 const struct got_error *err = NULL;
1009 const char *markers[3] = {
1010 GOT_DIFF_CONFLICT_MARKER_BEGIN,
1011 GOT_DIFF_CONFLICT_MARKER_SEP,
1012 GOT_DIFF_CONFLICT_MARKER_END
1014 int i = 0;
1015 char *line;
1016 size_t len;
1017 const char delim[3] = {'\0', '\0', '\0'};
1019 while (*status == GOT_STATUS_MODIFY) {
1020 line = fparseln(f, &len, NULL, delim, 0);
1021 if (line == NULL) {
1022 if (feof(f))
1023 break;
1024 err = got_ferror(f, GOT_ERR_IO);
1025 break;
1028 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1029 if (strcmp(markers[i], GOT_DIFF_CONFLICT_MARKER_END)
1030 == 0)
1031 *status = GOT_STATUS_CONFLICT;
1032 else
1033 i++;
1037 return err;
1040 static const struct got_error *
1041 get_file_status(unsigned char *status, struct stat *sb,
1042 struct got_fileindex_entry *ie, const char *abspath,
1043 struct got_repository *repo)
1045 const struct got_error *err = NULL;
1046 struct got_object_id id;
1047 size_t hdrlen;
1048 FILE *f = NULL;
1049 uint8_t fbuf[8192];
1050 struct got_blob_object *blob = NULL;
1051 size_t flen, blen;
1053 *status = GOT_STATUS_NO_CHANGE;
1055 if (lstat(abspath, sb) == -1) {
1056 if (errno == ENOENT) {
1057 if (ie) {
1058 if (got_fileindex_entry_has_file_on_disk(ie))
1059 *status = GOT_STATUS_MISSING;
1060 else
1061 *status = GOT_STATUS_DELETE;
1062 sb->st_mode =
1063 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1064 & (S_IRWXU | S_IRWXG | S_IRWXO));
1065 } else
1066 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1067 return NULL;
1069 return got_error_from_errno2("lstat", abspath);
1072 if (!S_ISREG(sb->st_mode)) {
1073 *status = GOT_STATUS_OBSTRUCTED;
1074 return NULL;
1077 if (ie == NULL)
1078 return NULL;
1080 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1081 *status = GOT_STATUS_DELETE;
1082 return NULL;
1083 } else if (!got_fileindex_entry_has_blob(ie)) {
1084 *status = GOT_STATUS_ADD;
1085 return NULL;
1088 if (ie->ctime_sec == sb->st_ctime &&
1089 ie->ctime_nsec == sb->st_ctimensec &&
1090 ie->mtime_sec == sb->st_mtime &&
1091 ie->mtime_sec == sb->st_mtime &&
1092 ie->mtime_nsec == sb->st_mtimensec &&
1093 ie->size == (sb->st_size & 0xffffffff))
1094 return NULL;
1096 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1097 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1098 if (err)
1099 return err;
1101 f = fopen(abspath, "r");
1102 if (f == NULL) {
1103 err = got_error_from_errno2("fopen", abspath);
1104 goto done;
1106 hdrlen = got_object_blob_get_hdrlen(blob);
1107 for (;;) {
1108 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1109 err = got_object_blob_read_block(&blen, blob);
1110 if (err)
1111 goto done;
1112 /* Skip length of blob object header first time around. */
1113 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1114 if (flen == 0 && ferror(f)) {
1115 err = got_error_from_errno("fread");
1116 goto done;
1118 if (blen == 0) {
1119 if (flen != 0)
1120 *status = GOT_STATUS_MODIFY;
1121 break;
1122 } else if (flen == 0) {
1123 if (blen != 0)
1124 *status = GOT_STATUS_MODIFY;
1125 break;
1126 } else if (blen - hdrlen == flen) {
1127 /* Skip blob object header first time around. */
1128 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1129 *status = GOT_STATUS_MODIFY;
1130 break;
1132 } else {
1133 *status = GOT_STATUS_MODIFY;
1134 break;
1136 hdrlen = 0;
1139 if (*status == GOT_STATUS_MODIFY) {
1140 rewind(f);
1141 err = get_modified_file_content_status(status, f);
1143 done:
1144 if (blob)
1145 got_object_blob_close(blob);
1146 if (f)
1147 fclose(f);
1148 return err;
1151 static const struct got_error *
1152 update_blob(struct got_worktree *worktree,
1153 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1154 struct got_tree_entry *te, const char *path,
1155 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1156 void *progress_arg)
1158 const struct got_error *err = NULL;
1159 struct got_blob_object *blob = NULL;
1160 char *ondisk_path;
1161 unsigned char status = GOT_STATUS_NO_CHANGE;
1162 struct stat sb;
1164 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1165 return got_error_from_errno("asprintf");
1167 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1168 if (err)
1169 goto done;
1171 if (status == GOT_STATUS_OBSTRUCTED) {
1172 (*progress_cb)(progress_arg, status, path);
1173 goto done;
1176 if (ie && status != GOT_STATUS_MISSING) {
1177 if (got_fileindex_entry_has_commit(ie) &&
1178 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1179 SHA1_DIGEST_LENGTH) == 0) {
1180 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1181 path);
1182 goto done;
1184 if (got_fileindex_entry_has_blob(ie) &&
1185 memcmp(ie->blob_sha1, te->id->sha1,
1186 SHA1_DIGEST_LENGTH) == 0)
1187 goto done;
1190 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1191 if (err)
1192 goto done;
1194 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD) {
1195 int update_timestamps;
1196 struct got_blob_object *blob2 = NULL;
1197 if (got_fileindex_entry_has_blob(ie)) {
1198 struct got_object_id id2;
1199 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1200 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
1201 if (err)
1202 goto done;
1204 err = merge_blob(&update_timestamps, worktree, blob2,
1205 ondisk_path, path, sb.st_mode, blob, repo,
1206 progress_cb, progress_arg);
1207 if (blob2)
1208 got_object_blob_close(blob2);
1210 * Do not update timestamps of files with local changes.
1211 * Otherwise, a future status walk would treat them as
1212 * unmodified files again.
1214 err = got_fileindex_entry_update(ie, ondisk_path,
1215 blob->id.sha1, worktree->base_commit_id->sha1,
1216 update_timestamps);
1217 } else if (status == GOT_STATUS_DELETE) {
1218 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1219 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1220 ondisk_path, path, blob, 0);
1221 if (err)
1222 goto done;
1223 } else {
1224 err = install_blob(worktree, ondisk_path, path, te->mode,
1225 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1226 repo, progress_cb, progress_arg);
1227 if (err)
1228 goto done;
1229 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1230 ondisk_path, path, blob, 1);
1231 if (err)
1232 goto done;
1234 got_object_blob_close(blob);
1235 done:
1236 free(ondisk_path);
1237 return err;
1240 static const struct got_error *
1241 remove_ondisk_file(const char *root_path, const char *path)
1243 const struct got_error *err = NULL;
1244 char *ondisk_path = NULL;
1246 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1247 return got_error_from_errno("asprintf");
1249 if (unlink(ondisk_path) == -1) {
1250 if (errno != ENOENT)
1251 err = got_error_from_errno2("unlink", ondisk_path);
1252 } else {
1253 char *parent = dirname(ondisk_path);
1254 while (parent && strcmp(parent, root_path) != 0) {
1255 if (rmdir(parent) == -1) {
1256 if (errno != ENOTEMPTY)
1257 err = got_error_from_errno2("rmdir",
1258 parent);
1259 break;
1261 parent = dirname(parent);
1264 free(ondisk_path);
1265 return err;
1268 static const struct got_error *
1269 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1270 struct got_fileindex_entry *ie, struct got_repository *repo,
1271 got_worktree_checkout_cb progress_cb, void *progress_arg)
1273 const struct got_error *err = NULL;
1274 unsigned char status;
1275 struct stat sb;
1276 char *ondisk_path;
1278 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1279 == -1)
1280 return got_error_from_errno("asprintf");
1282 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1283 if (err)
1284 return err;
1286 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1287 status == GOT_STATUS_ADD) {
1288 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1290 * Preserve the working file and change the deleted blob's
1291 * entry into a schedule-add entry.
1293 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1294 0);
1295 if (err)
1296 return err;
1297 } else {
1298 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1299 if (status == GOT_STATUS_NO_CHANGE) {
1300 err = remove_ondisk_file(worktree->root_path, ie->path);
1301 if (err)
1302 return err;
1304 got_fileindex_entry_remove(fileindex, ie);
1307 return err;
1310 struct diff_cb_arg {
1311 struct got_fileindex *fileindex;
1312 struct got_worktree *worktree;
1313 struct got_repository *repo;
1314 got_worktree_checkout_cb progress_cb;
1315 void *progress_arg;
1316 got_worktree_cancel_cb cancel_cb;
1317 void *cancel_arg;
1320 static const struct got_error *
1321 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1322 struct got_tree_entry *te, const char *parent_path)
1324 struct diff_cb_arg *a = arg;
1326 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1327 return got_error(GOT_ERR_CANCELLED);
1329 return update_blob(a->worktree, a->fileindex, ie, te,
1330 ie->path, a->repo, a->progress_cb, a->progress_arg);
1333 static const struct got_error *
1334 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1336 struct diff_cb_arg *a = arg;
1338 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1339 return got_error(GOT_ERR_CANCELLED);
1341 return delete_blob(a->worktree, a->fileindex, ie,
1342 a->repo, a->progress_cb, a->progress_arg);
1345 static const struct got_error *
1346 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1348 struct diff_cb_arg *a = arg;
1349 const struct got_error *err;
1350 char *path;
1352 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1353 return got_error(GOT_ERR_CANCELLED);
1355 if (asprintf(&path, "%s%s%s", parent_path,
1356 parent_path[0] ? "/" : "", te->name)
1357 == -1)
1358 return got_error_from_errno("asprintf");
1360 if (S_ISDIR(te->mode))
1361 err = add_dir_on_disk(a->worktree, path);
1362 else
1363 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1364 a->repo, a->progress_cb, a->progress_arg);
1366 free(path);
1367 return err;
1370 const struct got_error *
1371 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1373 const struct got_error *err = NULL;
1374 char *uuidstr = NULL;
1375 uint32_t uuid_status;
1377 *refname = NULL;
1379 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1380 if (uuid_status != uuid_s_ok)
1381 return got_error_uuid(uuid_status);
1383 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1384 == -1) {
1385 err = got_error_from_errno("asprintf");
1386 *refname = NULL;
1388 free(uuidstr);
1389 return err;
1393 * Prevent Git's garbage collector from deleting our base commit by
1394 * setting a reference to our base commit's ID.
1396 static const struct got_error *
1397 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1399 const struct got_error *err = NULL;
1400 struct got_reference *ref = NULL;
1401 char *refname;
1403 err = got_worktree_get_base_ref_name(&refname, worktree);
1404 if (err)
1405 return err;
1407 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1408 if (err)
1409 goto done;
1411 err = got_ref_write(ref, repo);
1412 done:
1413 free(refname);
1414 if (ref)
1415 got_ref_close(ref);
1416 return err;
1419 static const struct got_error *
1420 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1421 struct got_worktree *worktree)
1423 const struct got_error *err = NULL;
1424 FILE *index = NULL;
1426 *fileindex_path = NULL;
1427 *fileindex = got_fileindex_alloc();
1428 if (*fileindex == NULL)
1429 return got_error_from_errno("got_fileindex_alloc");
1431 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1432 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1433 err = got_error_from_errno("asprintf");
1434 *fileindex_path = NULL;
1435 goto done;
1438 index = fopen(*fileindex_path, "rb");
1439 if (index == NULL) {
1440 if (errno != ENOENT)
1441 err = got_error_from_errno2("fopen", *fileindex_path);
1442 } else {
1443 err = got_fileindex_read(*fileindex, index);
1444 if (fclose(index) != 0 && err == NULL)
1445 err = got_error_from_errno("fclose");
1447 done:
1448 if (err) {
1449 free(*fileindex_path);
1450 *fileindex_path = NULL;
1451 free(*fileindex);
1452 *fileindex = NULL;
1454 return err;
1457 struct bump_base_commit_id_arg {
1458 struct got_object_id *base_commit_id;
1459 const char *path;
1460 size_t path_len;
1461 const char *entry_name;
1464 /* Bump base commit ID of all files within an updated part of the work tree. */
1465 static const struct got_error *
1466 bump_base_commit_id(void *arg, struct got_fileindex_entry *ie)
1468 struct bump_base_commit_id_arg *a = arg;
1470 if (a->entry_name) {
1471 if (strcmp(ie->path, a->path) != 0)
1472 return NULL;
1473 } else if (!got_path_is_child(ie->path, a->path, a->path_len))
1474 return NULL;
1476 memcpy(ie->commit_sha1, a->base_commit_id->sha1, SHA1_DIGEST_LENGTH);
1477 return NULL;
1480 static const struct got_error *
1481 sync_fileindex(struct got_fileindex *fileindex, const char *fileindex_path)
1483 const struct got_error *err = NULL;
1484 char *new_fileindex_path = NULL;
1485 FILE *new_index = NULL;
1487 err = got_opentemp_named(&new_fileindex_path, &new_index,
1488 fileindex_path);
1489 if (err)
1490 goto done;
1492 err = got_fileindex_write(fileindex, new_index);
1493 if (err)
1494 goto done;
1496 if (rename(new_fileindex_path, fileindex_path) != 0) {
1497 err = got_error_from_errno3("rename", new_fileindex_path,
1498 fileindex_path);
1499 unlink(new_fileindex_path);
1501 done:
1502 if (new_index)
1503 fclose(new_index);
1504 free(new_fileindex_path);
1505 return err;
1508 const struct got_error *
1509 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1510 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1511 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1513 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1514 struct got_commit_object *commit = NULL;
1515 struct got_object_id *tree_id = NULL;
1516 struct got_tree_object *tree = NULL;
1517 struct got_fileindex *fileindex = NULL;
1518 char *fileindex_path = NULL;
1519 struct got_fileindex_diff_tree_cb diff_cb;
1520 struct diff_cb_arg arg;
1521 char *relpath = NULL, *entry_name = NULL;
1523 err = lock_worktree(worktree, LOCK_EX);
1524 if (err)
1525 return err;
1528 * Read the file index.
1529 * Checking out files is supposed to be an idempotent operation.
1530 * If the on-disk file index is incomplete we will try to complete it.
1532 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1533 if (err)
1534 goto done;
1536 err = ref_base_commit(worktree, repo);
1537 if (err)
1538 goto done;
1540 err = got_object_open_as_commit(&commit, repo,
1541 worktree->base_commit_id);
1542 if (err)
1543 goto done;
1545 if (path[0]) {
1546 char *tree_path;
1547 int obj_type;
1548 relpath = strdup(path);
1549 if (relpath == NULL) {
1550 err = got_error_from_errno("strdup");
1551 goto done;
1553 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1554 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1555 path) == -1) {
1556 err = got_error_from_errno("asprintf");
1557 goto done;
1559 err = got_object_id_by_path(&tree_id, repo,
1560 worktree->base_commit_id, tree_path);
1561 free(tree_path);
1562 if (err)
1563 goto done;
1564 err = got_object_get_type(&obj_type, repo, tree_id);
1565 if (err)
1566 goto done;
1567 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1568 /* Split provided path into parent dir + entry name. */
1569 if (strchr(path, '/') == NULL) {
1570 relpath = strdup("");
1571 if (relpath == NULL) {
1572 err = got_error_from_errno("strdup");
1573 goto done;
1575 tree_path = strdup(worktree->path_prefix);
1576 if (tree_path == NULL) {
1577 err = got_error_from_errno("strdup");
1578 goto done;
1580 } else {
1581 err = got_path_dirname(&relpath, path);
1582 if (err)
1583 goto done;
1584 if (asprintf(&tree_path, "%s%s%s",
1585 worktree->path_prefix,
1586 got_path_is_root_dir(
1587 worktree->path_prefix) ? "" : "/",
1588 relpath) == -1) {
1589 err = got_error_from_errno("asprintf");
1590 goto done;
1593 err = got_object_id_by_path(&tree_id, repo,
1594 worktree->base_commit_id, tree_path);
1595 free(tree_path);
1596 if (err)
1597 goto done;
1598 entry_name = basename(path);
1599 if (entry_name == NULL) {
1600 err = got_error_from_errno2("basename", path);
1601 goto done;
1604 } else {
1605 relpath = strdup("");
1606 if (relpath == NULL) {
1607 err = got_error_from_errno("strdup");
1608 goto done;
1610 err = got_object_id_by_path(&tree_id, repo,
1611 worktree->base_commit_id, worktree->path_prefix);
1612 if (err)
1613 goto done;
1616 err = got_object_open_as_tree(&tree, repo, tree_id);
1617 if (err)
1618 goto done;
1620 if (entry_name &&
1621 got_object_tree_find_entry(tree, entry_name) == NULL) {
1622 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1623 goto done;
1626 diff_cb.diff_old_new = diff_old_new;
1627 diff_cb.diff_old = diff_old;
1628 diff_cb.diff_new = diff_new;
1629 arg.fileindex = fileindex;
1630 arg.worktree = worktree;
1631 arg.repo = repo;
1632 arg.progress_cb = progress_cb;
1633 arg.progress_arg = progress_arg;
1634 arg.cancel_cb = cancel_cb;
1635 arg.cancel_arg = cancel_arg;
1636 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1637 entry_name, repo, &diff_cb, &arg);
1639 if (checkout_err == NULL) {
1640 struct bump_base_commit_id_arg bbc_arg;
1641 bbc_arg.base_commit_id = worktree->base_commit_id;
1642 bbc_arg.entry_name = entry_name;
1643 bbc_arg.path = path;
1644 bbc_arg.path_len = strlen(path);
1645 err = got_fileindex_for_each_entry_safe(fileindex,
1646 bump_base_commit_id, &bbc_arg);
1647 if (err)
1648 goto done;
1651 /* Try to sync the fileindex back to disk in any case. */
1652 err = sync_fileindex(fileindex, fileindex_path);
1653 done:
1654 free(fileindex_path);
1655 free(relpath);
1656 if (tree)
1657 got_object_tree_close(tree);
1658 if (commit)
1659 got_object_commit_close(commit);
1660 got_fileindex_free(fileindex);
1661 if (checkout_err)
1662 err = checkout_err;
1663 unlockerr = lock_worktree(worktree, LOCK_SH);
1664 if (unlockerr && err == NULL)
1665 err = unlockerr;
1666 return err;
1669 struct merge_file_cb_arg {
1670 struct got_worktree *worktree;
1671 struct got_fileindex *fileindex;
1672 struct got_object_id *commit_id1;
1673 struct got_object_id *commit_id2;
1674 got_worktree_checkout_cb progress_cb;
1675 void *progress_arg;
1676 got_worktree_cancel_cb cancel_cb;
1677 void *cancel_arg;
1680 static const struct got_error *
1681 merge_file_cb(void *arg, struct got_blob_object *blob1,
1682 struct got_blob_object *blob2, struct got_object_id *id1,
1683 struct got_object_id *id2, const char *path1, const char *path2,
1684 struct got_repository *repo)
1686 static const struct got_error *err = NULL;
1687 struct merge_file_cb_arg *a = arg;
1688 struct got_fileindex_entry *ie;
1689 char *ondisk_path = NULL;
1690 struct stat sb;
1691 unsigned char status;
1692 int local_changes_subsumed;
1694 if (blob1 && blob2) {
1695 ie = got_fileindex_entry_get(a->fileindex, path2);
1696 if (ie == NULL) {
1697 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1698 path2);
1699 return NULL;
1702 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1703 path2) == -1)
1704 return got_error_from_errno("asprintf");
1706 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1707 if (err)
1708 goto done;
1710 if (status == GOT_STATUS_DELETE) {
1711 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1712 path2);
1713 goto done;
1715 if (status != GOT_STATUS_NO_CHANGE &&
1716 status != GOT_STATUS_MODIFY &&
1717 status != GOT_STATUS_CONFLICT &&
1718 status != GOT_STATUS_ADD) {
1719 (*a->progress_cb)(a->progress_arg, status, path2);
1720 goto done;
1723 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1724 ondisk_path, path2, sb.st_mode, blob2, repo,
1725 a->progress_cb, a->progress_arg);
1726 } else if (blob1) {
1727 ie = got_fileindex_entry_get(a->fileindex, path1);
1728 if (ie == NULL) {
1729 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1730 path2);
1731 return NULL;
1734 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1735 path1) == -1)
1736 return got_error_from_errno("asprintf");
1738 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1739 if (err)
1740 goto done;
1742 switch (status) {
1743 case GOT_STATUS_NO_CHANGE:
1744 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1745 path1);
1746 err = remove_ondisk_file(a->worktree->root_path, path1);
1747 if (err)
1748 goto done;
1749 if (ie)
1750 got_fileindex_entry_mark_deleted_from_disk(ie);
1751 break;
1752 case GOT_STATUS_DELETE:
1753 case GOT_STATUS_MISSING:
1754 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1755 path1);
1756 if (ie)
1757 got_fileindex_entry_mark_deleted_from_disk(ie);
1758 break;
1759 case GOT_STATUS_ADD:
1760 case GOT_STATUS_MODIFY:
1761 case GOT_STATUS_CONFLICT:
1762 (*a->progress_cb)(a->progress_arg,
1763 GOT_STATUS_CANNOT_DELETE, path1);
1764 break;
1765 case GOT_STATUS_OBSTRUCTED:
1766 (*a->progress_cb)(a->progress_arg, status, path1);
1767 break;
1768 default:
1769 break;
1771 } else if (blob2) {
1772 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1773 path2) == -1)
1774 return got_error_from_errno("asprintf");
1775 ie = got_fileindex_entry_get(a->fileindex, path2);
1776 if (ie) {
1777 err = get_file_status(&status, &sb, ie, ondisk_path,
1778 repo);
1779 if (err)
1780 goto done;
1781 if (status != GOT_STATUS_NO_CHANGE &&
1782 status != GOT_STATUS_MODIFY &&
1783 status != GOT_STATUS_CONFLICT &&
1784 status != GOT_STATUS_ADD) {
1785 (*a->progress_cb)(a->progress_arg, status,
1786 path2);
1787 goto done;
1789 err = merge_blob(&local_changes_subsumed, a->worktree,
1790 NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1791 a->progress_cb, a->progress_arg);
1792 if (status == GOT_STATUS_DELETE) {
1793 err = update_blob_fileindex_entry(a->worktree,
1794 a->fileindex, ie, ondisk_path, ie->path,
1795 blob2, 0);
1796 if (err)
1797 goto done;
1799 } else {
1800 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1801 err = install_blob(a->worktree, ondisk_path, path2,
1802 /* XXX get this from parent tree! */
1803 GOT_DEFAULT_FILE_MODE,
1804 sb.st_mode, blob2, 0, 0, repo,
1805 a->progress_cb, a->progress_arg);
1806 if (err)
1807 goto done;
1808 err = got_fileindex_entry_alloc(&ie,
1809 ondisk_path, path2, NULL, NULL);
1810 if (err)
1811 goto done;
1812 err = got_fileindex_entry_add(a->fileindex, ie);
1813 if (err) {
1814 got_fileindex_entry_free(ie);
1815 goto done;
1819 done:
1820 free(ondisk_path);
1821 return err;
1824 struct check_merge_ok_arg {
1825 struct got_worktree *worktree;
1826 struct got_repository *repo;
1829 static const struct got_error *
1830 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1832 const struct got_error *err = NULL;
1833 struct check_merge_ok_arg *a = arg;
1834 unsigned char status;
1835 struct stat sb;
1836 char *ondisk_path;
1838 /* Reject merges into a work tree with mixed base commits. */
1839 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1840 SHA1_DIGEST_LENGTH))
1841 return got_error(GOT_ERR_MIXED_COMMITS);
1843 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1844 == -1)
1845 return got_error_from_errno("asprintf");
1847 /* Reject merges into a work tree with conflicted files. */
1848 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1849 if (err)
1850 return err;
1851 if (status == GOT_STATUS_CONFLICT)
1852 return got_error(GOT_ERR_CONFLICTS);
1854 return NULL;
1857 const struct got_error *
1858 got_worktree_merge_files(struct got_worktree *worktree,
1859 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1860 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1861 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1863 const struct got_error *err = NULL, *unlockerr;
1864 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1865 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1866 struct merge_file_cb_arg arg;
1867 char *fileindex_path = NULL;
1868 struct got_fileindex *fileindex = NULL;
1869 struct check_merge_ok_arg mok_arg;
1871 err = lock_worktree(worktree, LOCK_EX);
1872 if (err)
1873 return err;
1875 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1876 if (err)
1877 goto done;
1879 mok_arg.worktree = worktree;
1880 mok_arg.repo = repo;
1881 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1882 &mok_arg);
1883 if (err)
1884 goto done;
1886 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1887 worktree->path_prefix);
1888 if (err)
1889 goto done;
1891 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1892 worktree->path_prefix);
1893 if (err)
1894 goto done;
1896 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1897 if (err)
1898 goto done;
1900 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1901 if (err)
1902 goto done;
1904 arg.worktree = worktree;
1905 arg.fileindex = fileindex;
1906 arg.commit_id1 = commit_id1;
1907 arg.commit_id2 = commit_id2;
1908 arg.progress_cb = progress_cb;
1909 arg.progress_arg = progress_arg;
1910 arg.cancel_cb = cancel_cb;
1911 arg.cancel_arg = cancel_arg;
1912 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1913 if (err)
1914 goto done;
1916 err = sync_fileindex(fileindex, fileindex_path);
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 const struct got_error *
2184 got_worktree_schedule_add(struct got_worktree *worktree,
2185 struct got_pathlist_head *ondisk_paths,
2186 got_worktree_status_cb status_cb, void *status_arg,
2187 struct got_repository *repo)
2189 struct got_fileindex *fileindex = NULL;
2190 char *fileindex_path = NULL;
2191 FILE *index = NULL;
2192 const struct got_error *err = NULL, *unlockerr = NULL;
2193 struct got_pathlist_entry *pe;
2195 err = lock_worktree(worktree, LOCK_EX);
2196 if (err)
2197 return err;
2200 fileindex = got_fileindex_alloc();
2201 if (fileindex == NULL) {
2202 err = got_error_from_errno("got_fileindex_alloc");
2203 goto done;
2206 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2207 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2208 err = got_error_from_errno("asprintf");
2209 fileindex_path = NULL;
2210 goto done;
2213 index = fopen(fileindex_path, "rb");
2214 if (index == NULL) {
2215 err = got_error_from_errno2("fopen", fileindex_path);
2216 goto done;
2219 err = got_fileindex_read(fileindex, index);
2220 if (err)
2221 goto done;
2223 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2224 struct got_fileindex_entry *ie = NULL;
2225 char *relpath;
2227 err = got_path_skip_common_ancestor(&relpath,
2228 got_worktree_get_root_path(worktree), pe->path);
2229 if (err)
2230 goto done;
2232 /* Re-adding an existing entry is a no-op. */
2233 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2234 continue;
2236 err = got_fileindex_entry_alloc(&ie, pe->path, relpath,
2237 NULL, NULL);
2238 free(relpath);
2239 if (err)
2240 goto done;
2242 err = got_fileindex_entry_add(fileindex, ie);
2243 if (err) {
2244 got_fileindex_entry_free(ie);
2245 goto done;
2248 err = report_file_status(ie, pe->path, status_cb, status_arg,
2249 repo);
2250 if (err)
2251 goto done;
2254 err = sync_fileindex(fileindex, fileindex_path);
2255 done:
2256 if (index) {
2257 if (fclose(index) != 0 && err == NULL)
2258 err = got_error_from_errno("fclose");
2260 if (fileindex)
2261 got_fileindex_free(fileindex);
2262 unlockerr = lock_worktree(worktree, LOCK_SH);
2263 if (unlockerr && err == NULL)
2264 err = unlockerr;
2265 return err;
2268 const struct got_error *
2269 got_worktree_schedule_delete(struct got_worktree *worktree,
2270 const char *ondisk_path, int delete_local_mods,
2271 got_worktree_status_cb status_cb, void *status_arg,
2272 struct got_repository *repo)
2274 struct got_fileindex *fileindex = NULL;
2275 struct got_fileindex_entry *ie = NULL;
2276 char *relpath, *fileindex_path ;
2277 FILE *index = NULL;
2278 const struct got_error *err = NULL, *unlockerr = NULL;
2279 unsigned char status;
2280 struct stat sb;
2282 err = lock_worktree(worktree, LOCK_EX);
2283 if (err)
2284 return err;
2286 err = got_path_skip_common_ancestor(&relpath,
2287 got_worktree_get_root_path(worktree), ondisk_path);
2288 if (err)
2289 goto done;
2291 fileindex = got_fileindex_alloc();
2292 if (fileindex == NULL) {
2293 err = got_error_from_errno("got_fileindex_alloc");
2294 goto done;
2297 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2298 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2299 err = got_error_from_errno("asprintf");
2300 fileindex_path = NULL;
2301 goto done;
2304 index = fopen(fileindex_path, "rb");
2305 if (index == NULL) {
2306 err = got_error_from_errno2("fopen", fileindex_path);
2307 goto done;
2310 err = got_fileindex_read(fileindex, index);
2311 if (err)
2312 goto done;
2314 ie = got_fileindex_entry_get(fileindex, relpath);
2315 if (ie == NULL) {
2316 err = got_error(GOT_ERR_BAD_PATH);
2317 goto done;
2320 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2321 if (err)
2322 goto done;
2324 if (status != GOT_STATUS_NO_CHANGE) {
2325 if (status == GOT_STATUS_DELETE) {
2326 err = got_error_set_errno(ENOENT, ondisk_path);
2327 goto done;
2329 if (status != GOT_STATUS_MODIFY) {
2330 err = got_error(GOT_ERR_FILE_STATUS);
2331 goto done;
2333 if (!delete_local_mods) {
2334 err = got_error(GOT_ERR_FILE_MODIFIED);
2335 goto done;
2339 if (unlink(ondisk_path) != 0) {
2340 err = got_error_from_errno2("unlink", ondisk_path);
2341 goto done;
2344 got_fileindex_entry_mark_deleted_from_disk(ie);
2346 err = sync_fileindex(fileindex, fileindex_path);
2347 if (err)
2348 goto done;
2350 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2351 done:
2352 free(relpath);
2353 if (index) {
2354 if (fclose(index) != 0 && err == NULL)
2355 err = got_error_from_errno("fclose");
2357 if (fileindex)
2358 got_fileindex_free(fileindex);
2359 unlockerr = lock_worktree(worktree, LOCK_SH);
2360 if (unlockerr && err == NULL)
2361 err = unlockerr;
2362 return err;
2365 const struct got_error *
2366 got_worktree_revert(struct got_worktree *worktree,
2367 const char *ondisk_path,
2368 got_worktree_checkout_cb progress_cb, void *progress_arg,
2369 struct got_repository *repo)
2371 struct got_fileindex *fileindex = NULL;
2372 struct got_fileindex_entry *ie = NULL;
2373 char *relpath, *fileindex_path = NULL;
2374 char *tree_path = NULL, *parent_path, *te_name;
2375 FILE *index = NULL;
2376 const struct got_error *err = NULL, *unlockerr = NULL;
2377 struct got_tree_object *tree = NULL;
2378 struct got_object_id id, *tree_id = NULL;
2379 const struct got_tree_entry *te;
2380 struct got_blob_object *blob = NULL;
2381 unsigned char status;
2382 struct stat sb;
2384 err = lock_worktree(worktree, LOCK_EX);
2385 if (err)
2386 return err;
2388 err = got_path_skip_common_ancestor(&relpath,
2389 got_worktree_get_root_path(worktree), ondisk_path);
2390 if (err)
2391 goto done;
2393 fileindex = got_fileindex_alloc();
2394 if (fileindex == NULL) {
2395 err = got_error_from_errno("got_fileindex_alloc");
2396 goto done;
2399 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2400 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2401 err = got_error_from_errno("asprintf");
2402 fileindex_path = NULL;
2403 goto done;
2406 index = fopen(fileindex_path, "rb");
2407 if (index == NULL) {
2408 err = got_error_from_errno2("fopen", fileindex_path);
2409 goto done;
2412 err = got_fileindex_read(fileindex, index);
2413 if (err)
2414 goto done;
2416 ie = got_fileindex_entry_get(fileindex, relpath);
2417 if (ie == NULL) {
2418 err = got_error(GOT_ERR_BAD_PATH);
2419 goto done;
2422 /* Construct in-repository path of tree which contains this blob. */
2423 err = got_path_dirname(&parent_path, ie->path);
2424 if (err) {
2425 if (err->code != GOT_ERR_BAD_PATH)
2426 goto done;
2427 parent_path = "/";
2429 if (got_path_is_root_dir(worktree->path_prefix)) {
2430 tree_path = strdup(parent_path);
2431 if (tree_path == NULL) {
2432 err = got_error_from_errno("strdup");
2433 goto done;
2435 } else {
2436 if (got_path_is_root_dir(parent_path)) {
2437 tree_path = strdup(worktree->path_prefix);
2438 if (tree_path == NULL) {
2439 err = got_error_from_errno("strdup");
2440 goto done;
2442 } else {
2443 if (asprintf(&tree_path, "%s/%s",
2444 worktree->path_prefix, parent_path) == -1) {
2445 err = got_error_from_errno("asprintf");
2446 goto done;
2451 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2452 tree_path);
2453 if (err)
2454 goto done;
2456 err = got_object_open_as_tree(&tree, repo, tree_id);
2457 if (err)
2458 goto done;
2460 te_name = basename(ie->path);
2461 if (te_name == NULL) {
2462 err = got_error_from_errno2("basename", ie->path);
2463 goto done;
2466 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2467 if (err)
2468 goto done;
2470 te = got_object_tree_find_entry(tree, te_name);
2471 if (te == NULL && status != GOT_STATUS_ADD) {
2472 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2473 goto done;
2476 switch (status) {
2477 case GOT_STATUS_ADD:
2478 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2479 got_fileindex_entry_remove(fileindex, ie);
2480 break;
2481 case GOT_STATUS_DELETE:
2482 case GOT_STATUS_MODIFY:
2483 case GOT_STATUS_CONFLICT:
2484 case GOT_STATUS_MISSING:
2485 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2486 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2487 if (err)
2488 goto done;
2489 err = install_blob(worktree, ondisk_path, ie->path,
2490 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2491 progress_arg);
2492 if (err)
2493 goto done;
2494 if (status == GOT_STATUS_DELETE) {
2495 err = update_blob_fileindex_entry(worktree,
2496 fileindex, ie, ondisk_path, ie->path, blob, 1);
2497 if (err)
2498 goto done;
2500 break;
2501 default:
2502 goto done;
2505 err = sync_fileindex(fileindex, fileindex_path);
2506 if (err)
2507 goto done;
2508 done:
2509 free(relpath);
2510 free(tree_path);
2511 if (blob)
2512 got_object_blob_close(blob);
2513 if (tree)
2514 got_object_tree_close(tree);
2515 free(tree_id);
2516 if (index) {
2517 if (fclose(index) != 0 && err == NULL)
2518 err = got_error_from_errno("fclose");
2520 if (fileindex)
2521 got_fileindex_free(fileindex);
2522 unlockerr = lock_worktree(worktree, LOCK_SH);
2523 if (unlockerr && err == NULL)
2524 err = unlockerr;
2525 return err;
2528 static void
2529 free_commitable(struct got_commitable *ct)
2531 free(ct->path);
2532 free(ct->in_repo_path);
2533 free(ct->ondisk_path);
2534 free(ct->blob_id);
2535 free(ct->base_blob_id);
2536 free(ct->base_commit_id);
2537 free(ct);
2540 struct collect_commitables_arg {
2541 struct got_pathlist_head *commitable_paths;
2542 struct got_repository *repo;
2543 struct got_worktree *worktree;
2546 static const struct got_error *
2547 collect_commitables(void *arg, unsigned char status, const char *relpath,
2548 struct got_object_id *blob_id, struct got_object_id *commit_id)
2550 struct collect_commitables_arg *a = arg;
2551 const struct got_error *err = NULL;
2552 struct got_commitable *ct = NULL;
2553 struct got_pathlist_entry *new = NULL;
2554 char *parent_path = NULL, *path = NULL;
2555 struct stat sb;
2557 if (status == GOT_STATUS_CONFLICT)
2558 return got_error(GOT_ERR_COMMIT_CONFLICT);
2560 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2561 status != GOT_STATUS_DELETE)
2562 return NULL;
2564 if (asprintf(&path, "/%s", relpath) == -1) {
2565 err = got_error_from_errno("asprintf");
2566 goto done;
2568 if (strcmp(path, "/") == 0) {
2569 parent_path = strdup("");
2570 if (parent_path == NULL)
2571 return got_error_from_errno("strdup");
2572 } else {
2573 err = got_path_dirname(&parent_path, path);
2574 if (err)
2575 return err;
2578 ct = calloc(1, sizeof(*ct));
2579 if (ct == NULL) {
2580 err = got_error_from_errno("calloc");
2581 goto done;
2584 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2585 relpath) == -1) {
2586 err = got_error_from_errno("asprintf");
2587 goto done;
2589 if (status == GOT_STATUS_DELETE) {
2590 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2591 } else {
2592 if (lstat(ct->ondisk_path, &sb) != 0) {
2593 err = got_error_from_errno2("lstat", ct->ondisk_path);
2594 goto done;
2596 ct->mode = sb.st_mode;
2599 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2600 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2601 relpath) == -1) {
2602 err = got_error_from_errno("asprintf");
2603 goto done;
2606 ct->status = status;
2607 ct->blob_id = NULL; /* will be filled in when blob gets created */
2608 if (ct->status != GOT_STATUS_ADD) {
2609 ct->base_blob_id = got_object_id_dup(blob_id);
2610 if (ct->base_blob_id == NULL) {
2611 err = got_error_from_errno("got_object_id_dup");
2612 goto done;
2614 ct->base_commit_id = got_object_id_dup(commit_id);
2615 if (ct->base_commit_id == NULL) {
2616 err = got_error_from_errno("got_object_id_dup");
2617 goto done;
2620 ct->path = strdup(path);
2621 if (ct->path == NULL) {
2622 err = got_error_from_errno("strdup");
2623 goto done;
2625 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2626 done:
2627 if (ct && (err || new == NULL))
2628 free_commitable(ct);
2629 free(parent_path);
2630 free(path);
2631 return err;
2634 static const struct got_error *write_tree(struct got_object_id **,
2635 struct got_tree_object *, const char *, struct got_pathlist_head *,
2636 got_worktree_status_cb status_cb, void *status_arg,
2637 struct got_repository *);
2639 static const struct got_error *
2640 write_subtree(struct got_object_id **new_subtree_id,
2641 struct got_tree_entry *te, const char *parent_path,
2642 struct got_pathlist_head *commitable_paths,
2643 got_worktree_status_cb status_cb, void *status_arg,
2644 struct got_repository *repo)
2646 const struct got_error *err = NULL;
2647 struct got_tree_object *subtree;
2648 char *subpath;
2650 if (asprintf(&subpath, "%s%s%s", parent_path,
2651 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2652 return got_error_from_errno("asprintf");
2654 err = got_object_open_as_tree(&subtree, repo, te->id);
2655 if (err)
2656 return err;
2658 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2659 status_cb, status_arg, repo);
2660 got_object_tree_close(subtree);
2661 free(subpath);
2662 return err;
2665 static const struct got_error *
2666 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2668 const struct got_error *err = NULL;
2669 char *ct_parent_path = NULL;
2671 *match = 0;
2673 if (strchr(ct->path, '/') == NULL) {
2674 *match = got_path_is_root_dir(path);
2675 return NULL;
2678 err = got_path_dirname(&ct_parent_path, ct->path);
2679 if (err)
2680 return err;
2681 *match = (strcmp(path, ct_parent_path) == 0);
2682 free(ct_parent_path);
2683 return err;
2686 static mode_t
2687 get_ct_file_mode(struct got_commitable *ct)
2689 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2692 static const struct got_error *
2693 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2694 struct got_tree_entry *te, struct got_commitable *ct)
2696 const struct got_error *err = NULL;
2698 *new_te = NULL;
2700 err = got_object_tree_entry_dup(new_te, te);
2701 if (err)
2702 goto done;
2704 (*new_te)->mode = get_ct_file_mode(ct);
2706 free((*new_te)->id);
2707 (*new_te)->id = got_object_id_dup(ct->blob_id);
2708 if ((*new_te)->id == NULL) {
2709 err = got_error_from_errno("got_object_id_dup");
2710 goto done;
2712 done:
2713 if (err && *new_te) {
2714 got_object_tree_entry_close(*new_te);
2715 *new_te = NULL;
2717 return err;
2720 static const struct got_error *
2721 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2722 struct got_commitable *ct)
2724 const struct got_error *err = NULL;
2725 char *ct_name;
2727 *new_te = NULL;
2729 *new_te = calloc(1, sizeof(**new_te));
2730 if (*new_te == NULL)
2731 return got_error_from_errno("calloc");
2733 ct_name = basename(ct->path);
2734 if (ct_name == NULL) {
2735 err = got_error_from_errno2("basename", ct->path);
2736 goto done;
2738 (*new_te)->name = strdup(ct_name);
2739 if ((*new_te)->name == NULL) {
2740 err = got_error_from_errno("strdup");
2741 goto done;
2744 (*new_te)->mode = get_ct_file_mode(ct);
2746 (*new_te)->id = got_object_id_dup(ct->blob_id);
2747 if ((*new_te)->id == NULL) {
2748 err = got_error_from_errno("got_object_id_dup");
2749 goto done;
2751 done:
2752 if (err && *new_te) {
2753 got_object_tree_entry_close(*new_te);
2754 *new_te = NULL;
2756 return err;
2759 static const struct got_error *
2760 insert_tree_entry(struct got_tree_entry *new_te,
2761 struct got_pathlist_head *paths)
2763 const struct got_error *err = NULL;
2764 struct got_pathlist_entry *new_pe;
2766 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2767 if (err)
2768 return err;
2769 if (new_pe == NULL)
2770 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2771 return NULL;
2774 static const struct got_error *
2775 report_ct_status(struct got_commitable *ct,
2776 got_worktree_status_cb status_cb, void *status_arg)
2778 const char *ct_path = ct->path;
2779 while (ct_path[0] == '/')
2780 ct_path++;
2781 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2784 static const struct got_error *
2785 match_modified_subtree(int *modified, struct got_tree_entry *te,
2786 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2788 const struct got_error *err = NULL;
2789 struct got_pathlist_entry *pe;
2790 char *te_path;
2792 *modified = 0;
2794 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2795 got_path_is_root_dir(base_tree_path) ? "" : "/",
2796 te->name) == -1)
2797 return got_error_from_errno("asprintf");
2799 TAILQ_FOREACH(pe, commitable_paths, entry) {
2800 struct got_commitable *ct = pe->data;
2801 *modified = got_path_is_child(ct->in_repo_path, te_path,
2802 strlen(te_path));
2803 if (*modified)
2804 break;
2807 free(te_path);
2808 return err;
2811 static const struct got_error *
2812 match_deleted_or_modified_ct(struct got_commitable **ctp,
2813 struct got_tree_entry *te, const char *base_tree_path,
2814 struct got_pathlist_head *commitable_paths)
2816 const struct got_error *err = NULL;
2817 struct got_pathlist_entry *pe;
2819 *ctp = NULL;
2821 TAILQ_FOREACH(pe, commitable_paths, entry) {
2822 struct got_commitable *ct = pe->data;
2823 char *ct_name = NULL;
2824 int path_matches;
2826 if (ct->status != GOT_STATUS_MODIFY &&
2827 ct->status != GOT_STATUS_DELETE)
2828 continue;
2830 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2831 continue;
2833 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2834 if (err)
2835 return err;
2836 if (!path_matches)
2837 continue;
2839 ct_name = basename(pe->path);
2840 if (ct_name == NULL)
2841 return got_error_from_errno2("basename", pe->path);
2843 if (strcmp(te->name, ct_name) != 0)
2844 continue;
2846 *ctp = ct;
2847 break;
2850 return err;
2853 static const struct got_error *
2854 write_tree(struct got_object_id **new_tree_id,
2855 struct got_tree_object *base_tree, const char *path_base_tree,
2856 struct got_pathlist_head *commitable_paths,
2857 got_worktree_status_cb status_cb, void *status_arg,
2858 struct got_repository *repo)
2860 const struct got_error *err = NULL;
2861 const struct got_tree_entries *base_entries = NULL;
2862 struct got_pathlist_head paths;
2863 struct got_tree_entries new_tree_entries;
2864 struct got_tree_entry *te, *new_te = NULL;
2865 struct got_pathlist_entry *pe;
2867 TAILQ_INIT(&paths);
2868 new_tree_entries.nentries = 0;
2869 SIMPLEQ_INIT(&new_tree_entries.head);
2871 /* Insert, and recurse into, newly added entries first. */
2872 TAILQ_FOREACH(pe, commitable_paths, entry) {
2873 struct got_commitable *ct = pe->data;
2874 char *child_path = NULL, *slash;
2876 if (ct->status != GOT_STATUS_ADD ||
2877 (ct->flags & GOT_COMMITABLE_ADDED))
2878 continue;
2880 if (!got_path_is_child(pe->path, path_base_tree,
2881 strlen(path_base_tree)))
2882 continue;
2884 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2885 pe->path);
2886 if (err)
2887 goto done;
2889 slash = strchr(child_path, '/');
2890 if (slash == NULL) {
2891 err = alloc_added_blob_tree_entry(&new_te, ct);
2892 if (err)
2893 goto done;
2894 err = report_ct_status(ct, status_cb, status_arg);
2895 if (err)
2896 goto done;
2897 ct->flags |= GOT_COMMITABLE_ADDED;
2898 } else {
2899 char *subtree_path;
2901 *slash = '\0'; /* trim trailing path components */
2902 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2903 got_path_is_root_dir(path_base_tree) ? "" : "/",
2904 child_path) == -1) {
2905 err = got_error_from_errno("asprintf");
2906 goto done;
2909 new_te = calloc(1, sizeof(*new_te));
2910 new_te->mode = S_IFDIR;
2911 new_te->name = strdup(child_path);
2912 if (new_te->name == NULL) {
2913 err = got_error_from_errno("strdup");
2914 got_object_tree_entry_close(new_te);
2915 new_te = NULL;
2916 goto done;
2918 err = write_tree(&new_te->id, NULL, subtree_path,
2919 commitable_paths, status_cb, status_arg, repo);
2920 free(subtree_path);
2921 if (err) {
2922 got_object_tree_entry_close(new_te);
2923 new_te = NULL;
2924 goto done;
2927 err = insert_tree_entry(new_te, &paths);
2928 if (err)
2929 goto done;
2932 if (base_tree) {
2933 /* Handle modified and deleted entries. */
2934 base_entries = got_object_tree_get_entries(base_tree);
2935 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2936 struct got_commitable *ct = NULL;
2938 if (S_ISDIR(te->mode)) {
2939 int modified;
2940 err = got_object_tree_entry_dup(&new_te, te);
2941 if (err)
2942 goto done;
2943 err = match_modified_subtree(&modified, te,
2944 path_base_tree, commitable_paths);
2945 if (err)
2946 goto done;
2947 /* Avoid recursion into unmodified subtrees. */
2948 if (modified) {
2949 free(new_te->id);
2950 err = write_subtree(&new_te->id, te,
2951 path_base_tree, commitable_paths,
2952 status_cb, status_arg, repo);
2953 if (err)
2954 goto done;
2956 err = insert_tree_entry(new_te, &paths);
2957 if (err)
2958 goto done;
2959 continue;
2962 err = match_deleted_or_modified_ct(&ct, te,
2963 path_base_tree, commitable_paths);
2964 if (ct) {
2965 /* NB: Deleted entries get dropped here. */
2966 if (ct->status == GOT_STATUS_MODIFY) {
2967 err = alloc_modified_blob_tree_entry(
2968 &new_te, te, ct);
2969 if (err)
2970 goto done;
2971 err = insert_tree_entry(new_te, &paths);
2972 if (err)
2973 goto done;
2975 err = report_ct_status(ct, status_cb,
2976 status_arg);
2977 if (err)
2978 goto done;
2979 } else {
2980 /* Entry is unchanged; just copy it. */
2981 err = got_object_tree_entry_dup(&new_te, te);
2982 if (err)
2983 goto done;
2984 err = insert_tree_entry(new_te, &paths);
2985 if (err)
2986 goto done;
2991 /* Write new list of entries; deleted entries have been dropped. */
2992 TAILQ_FOREACH(pe, &paths, entry) {
2993 struct got_tree_entry *te = pe->data;
2994 new_tree_entries.nentries++;
2995 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2997 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2998 done:
2999 got_object_tree_entries_close(&new_tree_entries);
3000 got_pathlist_free(&paths);
3001 return err;
3004 static const struct got_error *
3005 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3006 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3008 const struct got_error *err = NULL;
3009 char *fileindex_path = NULL;
3010 struct got_fileindex *fileindex = NULL;
3011 struct got_pathlist_entry *pe;
3013 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3014 if (err)
3015 return err;
3017 TAILQ_FOREACH(pe, commitable_paths, entry) {
3018 struct got_fileindex_entry *ie;
3019 struct got_commitable *ct = pe->data;
3021 ie = got_fileindex_entry_get(fileindex, pe->path);
3022 if (ie) {
3023 if (ct->status == GOT_STATUS_DELETE) {
3024 got_fileindex_entry_remove(fileindex, ie);
3025 got_fileindex_entry_free(ie);
3026 } else
3027 err = got_fileindex_entry_update(ie,
3028 ct->ondisk_path, ct->blob_id->sha1,
3029 new_base_commit_id->sha1, 1);
3030 } else {
3031 err = got_fileindex_entry_alloc(&ie,
3032 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3033 new_base_commit_id->sha1);
3034 if (err)
3035 goto done;
3036 err = got_fileindex_entry_add(fileindex, ie);
3037 if (err)
3038 goto done;
3042 err = sync_fileindex(fileindex, fileindex_path);
3043 done:
3044 free(fileindex_path);
3045 got_fileindex_free(fileindex);
3046 return err;
3049 static const struct got_error *
3050 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3051 struct got_object_id *head_commit_id)
3053 const struct got_error *err = NULL;
3054 struct got_object_id *id_in_head;
3057 * Require that modified/deleted files are based on the branch head.
3058 * This requirement could be relaxed to force less update operations
3059 * on users but, for now, we want to play it safe and see how it goes.
3061 * If this check is relaxed, it must still ensure that no modifications
3062 * were made to files *and their parents* in commits between the file's
3063 * base commit and the branch head. Otherwise, tree conflicts will not
3064 * be detected reliably.
3066 if (ct->status != GOT_STATUS_ADD) {
3067 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
3068 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3069 return NULL;
3072 /* Require that added files don't exist in the branch head. */
3073 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3074 ct->in_repo_path);
3075 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3076 return err;
3077 if (id_in_head) {
3078 free(id_in_head);
3079 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3082 free(id_in_head);
3083 return NULL;
3086 const struct got_error *
3087 got_worktree_commit(struct got_object_id **new_commit_id,
3088 struct got_worktree *worktree, const char *ondisk_path,
3089 const char *author, const char *committer,
3090 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3091 got_worktree_status_cb status_cb, void *status_arg,
3092 struct got_repository *repo)
3094 const struct got_error *err = NULL, *unlockerr = NULL;
3095 struct collect_commitables_arg cc_arg;
3096 struct got_pathlist_head commitable_paths;
3097 struct got_pathlist_entry *pe;
3098 char *relpath = NULL;
3099 const char *head_ref_name = NULL;
3100 struct got_reference *head_ref = NULL;
3101 struct got_commit_object *head_commit = NULL;
3102 struct got_object_id *head_commit_id = NULL;
3103 struct got_reference *head_ref2 = NULL;
3104 struct got_object_id *head_commit_id2 = NULL;
3105 struct got_tree_object *head_tree = NULL;
3106 struct got_object_id *new_tree_id = NULL;
3107 struct got_object_id_queue parent_ids;
3108 struct got_object_qid *pid = NULL;
3109 char *logmsg = NULL;
3111 *new_commit_id = NULL;
3113 TAILQ_INIT(&commitable_paths);
3114 SIMPLEQ_INIT(&parent_ids);
3116 if (ondisk_path) {
3117 err = got_path_skip_common_ancestor(&relpath,
3118 worktree->root_path, ondisk_path);
3119 if (err)
3120 return err;
3123 err = lock_worktree(worktree, LOCK_EX);
3124 if (err)
3125 goto done;
3127 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3128 if (err)
3129 goto done;
3130 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3131 if (err)
3132 goto done;
3134 cc_arg.commitable_paths = &commitable_paths;
3135 cc_arg.worktree = worktree;
3136 cc_arg.repo = repo;
3137 err = got_worktree_status(worktree, relpath ? relpath : "",
3138 repo, collect_commitables, &cc_arg, NULL, NULL);
3139 if (err)
3140 goto done;
3142 if (TAILQ_EMPTY(&commitable_paths)) {
3143 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3144 goto done;
3147 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3148 if (err)
3149 goto done;
3151 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3152 struct got_commitable *ct = pe->data;
3153 err = check_ct_out_of_date(ct, repo, head_commit_id);
3154 if (err)
3155 goto done;
3158 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3159 if (err)
3160 goto done;
3162 if (commit_msg_cb != NULL) {
3163 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3164 if (err)
3165 goto done;
3168 if (logmsg == NULL || strlen(logmsg) == 0) {
3169 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3170 goto done;
3173 /* Create blobs from added and modified files and record their IDs. */
3174 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3175 struct got_commitable *ct = pe->data;
3176 char *ondisk_path;
3178 if (ct->status != GOT_STATUS_ADD &&
3179 ct->status != GOT_STATUS_MODIFY)
3180 continue;
3182 if (asprintf(&ondisk_path, "%s/%s",
3183 worktree->root_path, pe->path) == -1) {
3184 err = got_error_from_errno("asprintf");
3185 goto done;
3187 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3188 free(ondisk_path);
3189 if (err)
3190 goto done;
3193 /* Recursively write new tree objects. */
3194 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3195 status_cb, status_arg, repo);
3196 if (err)
3197 goto done;
3199 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3200 if (err)
3201 goto done;
3202 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3203 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3204 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3205 got_object_qid_free(pid);
3206 if (logmsg != NULL)
3207 free(logmsg);
3208 if (err)
3209 goto done;
3211 /* Check if a concurrent commit to our branch has occurred. */
3212 head_ref_name = got_worktree_get_head_ref_name(worktree);
3213 if (head_ref_name == NULL) {
3214 err = got_error_from_errno("got_worktree_get_head_ref_name");
3215 goto done;
3217 /* Lock the reference here to prevent concurrent modification. */
3218 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3219 if (err)
3220 goto done;
3221 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3222 if (err)
3223 goto done;
3224 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3225 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3226 goto done;
3228 /* Update branch head in repository. */
3229 err = got_ref_change_ref(head_ref2, *new_commit_id);
3230 if (err)
3231 goto done;
3232 err = got_ref_write(head_ref2, repo);
3233 if (err)
3234 goto done;
3236 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3237 if (err)
3238 goto done;
3240 err = ref_base_commit(worktree, repo);
3241 if (err)
3242 goto done;
3244 err = update_fileindex_after_commit(&commitable_paths,
3245 *new_commit_id, worktree);
3246 if (err)
3247 goto done;
3248 done:
3249 unlockerr = lock_worktree(worktree, LOCK_SH);
3250 if (unlockerr && err == NULL)
3251 err = unlockerr;
3252 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3253 struct got_commitable *ct = pe->data;
3254 free_commitable(ct);
3256 got_pathlist_free(&commitable_paths);
3257 if (head_tree)
3258 got_object_tree_close(head_tree);
3259 if (head_commit)
3260 got_object_commit_close(head_commit);
3261 free(relpath);
3262 free(head_commit_id);
3263 free(head_commit_id2);
3264 if (head_ref)
3265 got_ref_close(head_ref);
3266 if (head_ref2) {
3267 unlockerr = got_ref_unlock(head_ref2);
3268 if (unlockerr && err == NULL)
3269 err = unlockerr;
3270 got_ref_close(head_ref2);
3272 return err;
3275 const char *
3276 got_commitable_get_path(struct got_commitable *ct)
3278 return ct->path;
3281 unsigned int
3282 got_commitable_get_status(struct got_commitable *ct)
3284 return ct->status;