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 got_worktree_checkout_cb progress_cb;
1673 void *progress_arg;
1674 got_worktree_cancel_cb cancel_cb;
1675 void *cancel_arg;
1678 static const struct got_error *
1679 merge_file_cb(void *arg, struct got_blob_object *blob1,
1680 struct got_blob_object *blob2, struct got_object_id *id1,
1681 struct got_object_id *id2, const char *path1, const char *path2,
1682 struct got_repository *repo)
1684 static const struct got_error *err = NULL;
1685 struct merge_file_cb_arg *a = arg;
1686 struct got_fileindex_entry *ie;
1687 char *ondisk_path = NULL;
1688 struct stat sb;
1689 unsigned char status;
1690 int local_changes_subsumed;
1692 if (blob1 && blob2) {
1693 ie = got_fileindex_entry_get(a->fileindex, path2);
1694 if (ie == NULL) {
1695 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1696 path2);
1697 return NULL;
1700 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1701 path2) == -1)
1702 return got_error_from_errno("asprintf");
1704 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1705 if (err)
1706 goto done;
1708 if (status == GOT_STATUS_DELETE) {
1709 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MERGE,
1710 path2);
1711 goto done;
1713 if (status != GOT_STATUS_NO_CHANGE &&
1714 status != GOT_STATUS_MODIFY &&
1715 status != GOT_STATUS_CONFLICT &&
1716 status != GOT_STATUS_ADD) {
1717 (*a->progress_cb)(a->progress_arg, status, path2);
1718 goto done;
1721 err = merge_blob(&local_changes_subsumed, a->worktree, blob1,
1722 ondisk_path, path2, sb.st_mode, blob2, repo,
1723 a->progress_cb, a->progress_arg);
1724 } else if (blob1) {
1725 ie = got_fileindex_entry_get(a->fileindex, path1);
1726 if (ie == NULL) {
1727 (*a->progress_cb)(a->progress_arg, GOT_STATUS_MISSING,
1728 path2);
1729 return NULL;
1732 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1733 path1) == -1)
1734 return got_error_from_errno("asprintf");
1736 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1737 if (err)
1738 goto done;
1740 switch (status) {
1741 case GOT_STATUS_NO_CHANGE:
1742 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1743 path1);
1744 err = remove_ondisk_file(a->worktree->root_path, path1);
1745 if (err)
1746 goto done;
1747 if (ie)
1748 got_fileindex_entry_mark_deleted_from_disk(ie);
1749 break;
1750 case GOT_STATUS_DELETE:
1751 case GOT_STATUS_MISSING:
1752 (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE,
1753 path1);
1754 if (ie)
1755 got_fileindex_entry_mark_deleted_from_disk(ie);
1756 break;
1757 case GOT_STATUS_ADD:
1758 case GOT_STATUS_MODIFY:
1759 case GOT_STATUS_CONFLICT:
1760 (*a->progress_cb)(a->progress_arg,
1761 GOT_STATUS_CANNOT_DELETE, path1);
1762 break;
1763 case GOT_STATUS_OBSTRUCTED:
1764 (*a->progress_cb)(a->progress_arg, status, path1);
1765 break;
1766 default:
1767 break;
1769 } else if (blob2) {
1770 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path,
1771 path2) == -1)
1772 return got_error_from_errno("asprintf");
1773 ie = got_fileindex_entry_get(a->fileindex, path2);
1774 if (ie) {
1775 err = get_file_status(&status, &sb, ie, ondisk_path,
1776 repo);
1777 if (err)
1778 goto done;
1779 if (status != GOT_STATUS_NO_CHANGE &&
1780 status != GOT_STATUS_MODIFY &&
1781 status != GOT_STATUS_CONFLICT &&
1782 status != GOT_STATUS_ADD) {
1783 (*a->progress_cb)(a->progress_arg, status,
1784 path2);
1785 goto done;
1787 err = merge_blob(&local_changes_subsumed, a->worktree,
1788 NULL, ondisk_path, path2, sb.st_mode, blob2, repo,
1789 a->progress_cb, a->progress_arg);
1790 if (status == GOT_STATUS_DELETE) {
1791 err = update_blob_fileindex_entry(a->worktree,
1792 a->fileindex, ie, ondisk_path, ie->path,
1793 blob2, 0);
1794 if (err)
1795 goto done;
1797 } else {
1798 sb.st_mode = GOT_DEFAULT_FILE_MODE;
1799 err = install_blob(a->worktree, ondisk_path, path2,
1800 /* XXX get this from parent tree! */
1801 GOT_DEFAULT_FILE_MODE,
1802 sb.st_mode, blob2, 0, 0, repo,
1803 a->progress_cb, a->progress_arg);
1804 if (err)
1805 goto done;
1806 err = got_fileindex_entry_alloc(&ie,
1807 ondisk_path, path2, NULL, NULL);
1808 if (err)
1809 goto done;
1810 err = got_fileindex_entry_add(a->fileindex, ie);
1811 if (err) {
1812 got_fileindex_entry_free(ie);
1813 goto done;
1817 done:
1818 free(ondisk_path);
1819 return err;
1822 struct check_merge_ok_arg {
1823 struct got_worktree *worktree;
1824 struct got_repository *repo;
1827 static const struct got_error *
1828 check_merge_ok(void *arg, struct got_fileindex_entry *ie)
1830 const struct got_error *err = NULL;
1831 struct check_merge_ok_arg *a = arg;
1832 unsigned char status;
1833 struct stat sb;
1834 char *ondisk_path;
1836 /* Reject merges into a work tree with mixed base commits. */
1837 if (memcmp(ie->commit_sha1, a->worktree->base_commit_id->sha1,
1838 SHA1_DIGEST_LENGTH))
1839 return got_error(GOT_ERR_MIXED_COMMITS);
1841 if (asprintf(&ondisk_path, "%s/%s", a->worktree->root_path, ie->path)
1842 == -1)
1843 return got_error_from_errno("asprintf");
1845 /* Reject merges into a work tree with conflicted files. */
1846 err = get_file_status(&status, &sb, ie, ondisk_path, a->repo);
1847 if (err)
1848 return err;
1849 if (status == GOT_STATUS_CONFLICT)
1850 return got_error(GOT_ERR_CONFLICTS);
1852 return NULL;
1855 const struct got_error *
1856 got_worktree_merge_files(struct got_worktree *worktree,
1857 struct got_object_id *commit_id1, struct got_object_id *commit_id2,
1858 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1859 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1861 const struct got_error *err = NULL, *unlockerr;
1862 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
1863 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1864 struct merge_file_cb_arg arg;
1865 char *fileindex_path = NULL;
1866 struct got_fileindex *fileindex = NULL;
1867 struct check_merge_ok_arg mok_arg;
1869 err = lock_worktree(worktree, LOCK_EX);
1870 if (err)
1871 return err;
1873 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1874 if (err)
1875 goto done;
1877 mok_arg.worktree = worktree;
1878 mok_arg.repo = repo;
1879 err = got_fileindex_for_each_entry_safe(fileindex, check_merge_ok,
1880 &mok_arg);
1881 if (err)
1882 goto done;
1884 if (commit_id1) {
1885 err = got_object_id_by_path(&tree_id1, repo, commit_id1,
1886 worktree->path_prefix);
1887 if (err)
1888 goto done;
1890 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1891 if (err)
1892 goto done;
1895 err = got_object_id_by_path(&tree_id2, repo, commit_id2,
1896 worktree->path_prefix);
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.progress_cb = progress_cb;
1907 arg.progress_arg = progress_arg;
1908 arg.cancel_cb = cancel_cb;
1909 arg.cancel_arg = cancel_arg;
1910 err = got_diff_tree(tree1, tree2, "", "", repo, merge_file_cb, &arg);
1911 if (err)
1912 goto done;
1914 err = sync_fileindex(fileindex, fileindex_path);
1915 done:
1916 got_fileindex_free(fileindex);
1917 if (tree1)
1918 got_object_tree_close(tree1);
1919 if (tree2)
1920 got_object_tree_close(tree2);
1922 unlockerr = lock_worktree(worktree, LOCK_SH);
1923 if (unlockerr && err == NULL)
1924 err = unlockerr;
1925 return err;
1928 struct diff_dir_cb_arg {
1929 struct got_fileindex *fileindex;
1930 struct got_worktree *worktree;
1931 const char *status_path;
1932 size_t status_path_len;
1933 struct got_repository *repo;
1934 got_worktree_status_cb status_cb;
1935 void *status_arg;
1936 got_worktree_cancel_cb cancel_cb;
1937 void *cancel_arg;
1940 static const struct got_error *
1941 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1942 got_worktree_status_cb status_cb, void *status_arg,
1943 struct got_repository *repo)
1945 const struct got_error *err = NULL;
1946 unsigned char status = GOT_STATUS_NO_CHANGE;
1947 struct stat sb;
1948 struct got_object_id blob_id, commit_id;
1950 err = get_file_status(&status, &sb, ie, abspath, repo);
1951 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1952 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1953 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
1954 err = (*status_cb)(status_arg, status, ie->path, &blob_id,
1955 &commit_id);
1957 return err;
1960 static const struct got_error *
1961 status_old_new(void *arg, struct got_fileindex_entry *ie,
1962 struct dirent *de, const char *parent_path)
1964 const struct got_error *err = NULL;
1965 struct diff_dir_cb_arg *a = arg;
1966 char *abspath;
1968 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1969 return got_error(GOT_ERR_CANCELLED);
1971 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1972 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1973 return NULL;
1975 if (parent_path[0]) {
1976 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1977 parent_path, de->d_name) == -1)
1978 return got_error_from_errno("asprintf");
1979 } else {
1980 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1981 de->d_name) == -1)
1982 return got_error_from_errno("asprintf");
1985 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1986 a->repo);
1987 free(abspath);
1988 return err;
1991 static const struct got_error *
1992 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1994 struct diff_dir_cb_arg *a = arg;
1995 struct got_object_id blob_id, commit_id;
1996 unsigned char status;
1998 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1999 return got_error(GOT_ERR_CANCELLED);
2001 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2002 return NULL;
2004 memcpy(blob_id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2005 memcpy(commit_id.sha1, ie->commit_sha1, SHA1_DIGEST_LENGTH);
2006 if (got_fileindex_entry_has_file_on_disk(ie))
2007 status = GOT_STATUS_MISSING;
2008 else
2009 status = GOT_STATUS_DELETE;
2010 return (*a->status_cb)(a->status_arg, status, ie->path, &blob_id,
2011 &commit_id);
2014 static const struct got_error *
2015 status_new(void *arg, struct dirent *de, const char *parent_path)
2017 const struct got_error *err = NULL;
2018 struct diff_dir_cb_arg *a = arg;
2019 char *path = NULL;
2021 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
2022 return got_error(GOT_ERR_CANCELLED);
2024 if (de->d_type == DT_DIR)
2025 return NULL;
2027 /* XXX ignore symlinks for now */
2028 if (de->d_type == DT_LNK)
2029 return NULL;
2031 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
2032 return NULL;
2034 if (parent_path[0]) {
2035 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
2036 return got_error_from_errno("asprintf");
2037 } else {
2038 path = de->d_name;
2041 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
2042 NULL, NULL);
2043 if (parent_path[0])
2044 free(path);
2045 return err;
2048 const struct got_error *
2049 got_worktree_status(struct got_worktree *worktree, const char *path,
2050 struct got_repository *repo, got_worktree_status_cb status_cb,
2051 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
2053 const struct got_error *err = NULL;
2054 DIR *workdir = NULL;
2055 char *fileindex_path = NULL;
2056 struct got_fileindex *fileindex = NULL;
2057 FILE *index = NULL;
2058 struct got_fileindex_diff_dir_cb fdiff_cb;
2059 struct diff_dir_cb_arg arg;
2060 char *ondisk_path = NULL;
2062 fileindex = got_fileindex_alloc();
2063 if (fileindex == NULL) {
2064 err = got_error_from_errno("got_fileindex_alloc");
2065 goto done;
2068 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2069 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2070 err = got_error_from_errno("asprintf");
2071 fileindex_path = NULL;
2072 goto done;
2075 index = fopen(fileindex_path, "rb");
2076 if (index == NULL) {
2077 if (errno != ENOENT) {
2078 err = got_error_from_errno2("fopen", fileindex_path);
2079 goto done;
2081 } else {
2082 err = got_fileindex_read(fileindex, index);
2083 fclose(index);
2084 if (err)
2085 goto done;
2088 if (asprintf(&ondisk_path, "%s%s%s",
2089 worktree->root_path, path[0] ? "/" : "", path) == -1) {
2090 err = got_error_from_errno("asprintf");
2091 goto done;
2093 workdir = opendir(ondisk_path);
2094 if (workdir == NULL) {
2095 if (errno == ENOTDIR || errno == ENOENT) {
2096 struct got_fileindex_entry *ie;
2097 ie = got_fileindex_entry_get(fileindex, path);
2098 if (ie == NULL) {
2099 err = got_error(GOT_ERR_BAD_PATH);
2100 goto done;
2102 err = report_file_status(ie, ondisk_path,
2103 status_cb, status_arg, repo);
2104 goto done;
2105 } else {
2106 err = got_error_from_errno2("opendir", ondisk_path);
2107 goto done;
2110 fdiff_cb.diff_old_new = status_old_new;
2111 fdiff_cb.diff_old = status_old;
2112 fdiff_cb.diff_new = status_new;
2113 arg.fileindex = fileindex;
2114 arg.worktree = worktree;
2115 arg.status_path = path;
2116 arg.status_path_len = strlen(path);
2117 arg.repo = repo;
2118 arg.status_cb = status_cb;
2119 arg.status_arg = status_arg;
2120 arg.cancel_cb = cancel_cb;
2121 arg.cancel_arg = cancel_arg;
2122 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
2123 path, repo, &fdiff_cb, &arg);
2124 done:
2125 if (workdir)
2126 closedir(workdir);
2127 free(ondisk_path);
2128 free(fileindex_path);
2129 got_fileindex_free(fileindex);
2130 return err;
2133 const struct got_error *
2134 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
2135 const char *arg)
2137 const struct got_error *err = NULL;
2138 char *resolved, *path = NULL;
2139 size_t len;
2141 *wt_path = NULL;
2143 resolved = realpath(arg, NULL);
2144 if (resolved == NULL)
2145 return got_error_from_errno2("realpath", arg);
2147 if (strncmp(got_worktree_get_root_path(worktree), resolved,
2148 strlen(got_worktree_get_root_path(worktree)))) {
2149 err = got_error(GOT_ERR_BAD_PATH);
2150 goto done;
2153 if (strlen(resolved) > strlen(got_worktree_get_root_path(worktree))) {
2154 err = got_path_skip_common_ancestor(&path,
2155 got_worktree_get_root_path(worktree), resolved);
2156 if (err)
2157 goto done;
2158 } else {
2159 path = strdup("");
2160 if (path == NULL) {
2161 err = got_error_from_errno("strdup");
2162 goto done;
2166 /* XXX status walk can't deal with trailing slash! */
2167 len = strlen(path);
2168 while (path[len - 1] == '/') {
2169 path[len - 1] = '\0';
2170 len--;
2172 done:
2173 free(resolved);
2174 if (err == NULL)
2175 *wt_path = path;
2176 else
2177 free(path);
2178 return err;
2181 static const struct got_error *
2182 schedule_addition(const char *ondisk_path, struct got_fileindex *fileindex,
2183 const char *relpath, got_worktree_status_cb status_cb, void *status_arg,
2184 struct got_repository *repo)
2186 const struct got_error *err = NULL;
2187 struct got_fileindex_entry *ie;
2189 /* Re-adding an existing entry is a no-op. */
2190 if (got_fileindex_entry_get(fileindex, relpath) != NULL)
2191 return NULL;
2193 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
2194 if (err)
2195 return err;
2197 err = got_fileindex_entry_add(fileindex, ie);
2198 if (err) {
2199 got_fileindex_entry_free(ie);
2200 return err;
2203 return report_file_status(ie, relpath, status_cb, status_arg, repo);
2206 const struct got_error *
2207 got_worktree_schedule_add(struct got_worktree *worktree,
2208 struct got_pathlist_head *ondisk_paths,
2209 got_worktree_status_cb status_cb, void *status_arg,
2210 struct got_repository *repo)
2212 struct got_fileindex *fileindex = NULL;
2213 char *fileindex_path = NULL;
2214 FILE *index = NULL;
2215 const struct got_error *err = NULL, *unlockerr = NULL;
2216 struct got_pathlist_entry *pe;
2218 err = lock_worktree(worktree, LOCK_EX);
2219 if (err)
2220 return err;
2223 fileindex = got_fileindex_alloc();
2224 if (fileindex == NULL) {
2225 err = got_error_from_errno("got_fileindex_alloc");
2226 goto done;
2229 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2230 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2231 err = got_error_from_errno("asprintf");
2232 fileindex_path = NULL;
2233 goto done;
2236 index = fopen(fileindex_path, "rb");
2237 if (index == NULL) {
2238 err = got_error_from_errno2("fopen", fileindex_path);
2239 goto done;
2242 err = got_fileindex_read(fileindex, index);
2243 if (err)
2244 goto done;
2246 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2247 char *relpath;
2248 err = got_path_skip_common_ancestor(&relpath,
2249 got_worktree_get_root_path(worktree), pe->path);
2250 if (err)
2251 goto done;
2252 err = schedule_addition(pe->path, fileindex, relpath,
2253 status_cb, status_arg, repo);
2254 free(relpath);
2255 if (err)
2256 goto done;
2259 err = sync_fileindex(fileindex, fileindex_path);
2260 done:
2261 if (index) {
2262 if (fclose(index) != 0 && err == NULL)
2263 err = got_error_from_errno("fclose");
2265 if (fileindex)
2266 got_fileindex_free(fileindex);
2267 unlockerr = lock_worktree(worktree, LOCK_SH);
2268 if (unlockerr && err == NULL)
2269 err = unlockerr;
2270 return err;
2273 static const struct got_error *
2274 schedule_for_deletion(const char *ondisk_path, struct got_fileindex *fileindex,
2275 const char *relpath, int delete_local_mods,
2276 got_worktree_status_cb status_cb, void *status_arg,
2277 struct got_repository *repo)
2279 const struct got_error *err = NULL;
2280 struct got_fileindex_entry *ie = NULL;
2281 unsigned char status;
2282 struct stat sb;
2284 ie = got_fileindex_entry_get(fileindex, relpath);
2285 if (ie == NULL)
2286 return got_error(GOT_ERR_BAD_PATH);
2288 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2289 if (err)
2290 return err;
2292 if (status != GOT_STATUS_NO_CHANGE) {
2293 if (status == GOT_STATUS_DELETE)
2294 return got_error_set_errno(ENOENT, ondisk_path);
2295 if (status != GOT_STATUS_MODIFY)
2296 return got_error(GOT_ERR_FILE_STATUS);
2297 if (!delete_local_mods)
2298 return got_error(GOT_ERR_FILE_MODIFIED);
2301 if (unlink(ondisk_path) != 0)
2302 return got_error_from_errno2("unlink", ondisk_path);
2304 got_fileindex_entry_mark_deleted_from_disk(ie);
2305 return report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2308 const struct got_error *
2309 got_worktree_schedule_delete(struct got_worktree *worktree,
2310 struct got_pathlist_head *ondisk_paths, int delete_local_mods,
2311 got_worktree_status_cb status_cb, void *status_arg,
2312 struct got_repository *repo)
2314 struct got_fileindex *fileindex = NULL;
2315 char *fileindex_path = NULL;
2316 FILE *index = NULL;
2317 const struct got_error *err = NULL, *unlockerr = NULL;
2318 struct got_pathlist_entry *pe;
2320 err = lock_worktree(worktree, LOCK_EX);
2321 if (err)
2322 return err;
2324 fileindex = got_fileindex_alloc();
2325 if (fileindex == NULL) {
2326 err = got_error_from_errno("got_fileindex_alloc");
2327 goto done;
2330 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2331 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2332 err = got_error_from_errno("asprintf");
2333 fileindex_path = NULL;
2334 goto done;
2337 index = fopen(fileindex_path, "rb");
2338 if (index == NULL) {
2339 err = got_error_from_errno2("fopen", fileindex_path);
2340 goto done;
2343 err = got_fileindex_read(fileindex, index);
2344 if (err)
2345 goto done;
2347 TAILQ_FOREACH(pe, ondisk_paths, entry) {
2348 char *relpath;
2349 err = got_path_skip_common_ancestor(&relpath,
2350 got_worktree_get_root_path(worktree), pe->path);
2351 if (err)
2352 goto done;
2353 err = schedule_for_deletion(pe->path, fileindex, relpath,
2354 delete_local_mods, status_cb, status_arg, repo);
2355 free(relpath);
2356 if (err)
2357 goto done;
2360 err = sync_fileindex(fileindex, fileindex_path);
2361 if (err)
2362 goto done;
2363 done:
2364 if (index) {
2365 if (fclose(index) != 0 && err == NULL)
2366 err = got_error_from_errno("fclose");
2368 if (fileindex)
2369 got_fileindex_free(fileindex);
2370 unlockerr = lock_worktree(worktree, LOCK_SH);
2371 if (unlockerr && err == NULL)
2372 err = unlockerr;
2373 return err;
2376 const struct got_error *
2377 got_worktree_revert(struct got_worktree *worktree,
2378 const char *ondisk_path,
2379 got_worktree_checkout_cb progress_cb, void *progress_arg,
2380 struct got_repository *repo)
2382 struct got_fileindex *fileindex = NULL;
2383 struct got_fileindex_entry *ie = NULL;
2384 char *relpath, *fileindex_path = NULL;
2385 char *tree_path = NULL, *parent_path, *te_name;
2386 FILE *index = NULL;
2387 const struct got_error *err = NULL, *unlockerr = NULL;
2388 struct got_tree_object *tree = NULL;
2389 struct got_object_id id, *tree_id = NULL;
2390 const struct got_tree_entry *te;
2391 struct got_blob_object *blob = NULL;
2392 unsigned char status;
2393 struct stat sb;
2395 err = lock_worktree(worktree, LOCK_EX);
2396 if (err)
2397 return err;
2399 err = got_path_skip_common_ancestor(&relpath,
2400 got_worktree_get_root_path(worktree), ondisk_path);
2401 if (err)
2402 goto done;
2404 fileindex = got_fileindex_alloc();
2405 if (fileindex == NULL) {
2406 err = got_error_from_errno("got_fileindex_alloc");
2407 goto done;
2410 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2411 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2412 err = got_error_from_errno("asprintf");
2413 fileindex_path = NULL;
2414 goto done;
2417 index = fopen(fileindex_path, "rb");
2418 if (index == NULL) {
2419 err = got_error_from_errno2("fopen", fileindex_path);
2420 goto done;
2423 err = got_fileindex_read(fileindex, index);
2424 if (err)
2425 goto done;
2427 ie = got_fileindex_entry_get(fileindex, relpath);
2428 if (ie == NULL) {
2429 err = got_error(GOT_ERR_BAD_PATH);
2430 goto done;
2433 /* Construct in-repository path of tree which contains this blob. */
2434 err = got_path_dirname(&parent_path, ie->path);
2435 if (err) {
2436 if (err->code != GOT_ERR_BAD_PATH)
2437 goto done;
2438 parent_path = "/";
2440 if (got_path_is_root_dir(worktree->path_prefix)) {
2441 tree_path = strdup(parent_path);
2442 if (tree_path == NULL) {
2443 err = got_error_from_errno("strdup");
2444 goto done;
2446 } else {
2447 if (got_path_is_root_dir(parent_path)) {
2448 tree_path = strdup(worktree->path_prefix);
2449 if (tree_path == NULL) {
2450 err = got_error_from_errno("strdup");
2451 goto done;
2453 } else {
2454 if (asprintf(&tree_path, "%s/%s",
2455 worktree->path_prefix, parent_path) == -1) {
2456 err = got_error_from_errno("asprintf");
2457 goto done;
2462 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2463 tree_path);
2464 if (err)
2465 goto done;
2467 err = got_object_open_as_tree(&tree, repo, tree_id);
2468 if (err)
2469 goto done;
2471 te_name = basename(ie->path);
2472 if (te_name == NULL) {
2473 err = got_error_from_errno2("basename", ie->path);
2474 goto done;
2477 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2478 if (err)
2479 goto done;
2481 te = got_object_tree_find_entry(tree, te_name);
2482 if (te == NULL && status != GOT_STATUS_ADD) {
2483 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2484 goto done;
2487 switch (status) {
2488 case GOT_STATUS_ADD:
2489 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2490 got_fileindex_entry_remove(fileindex, ie);
2491 break;
2492 case GOT_STATUS_DELETE:
2493 case GOT_STATUS_MODIFY:
2494 case GOT_STATUS_CONFLICT:
2495 case GOT_STATUS_MISSING:
2496 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2497 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2498 if (err)
2499 goto done;
2500 err = install_blob(worktree, ondisk_path, ie->path,
2501 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2502 progress_arg);
2503 if (err)
2504 goto done;
2505 if (status == GOT_STATUS_DELETE) {
2506 err = update_blob_fileindex_entry(worktree,
2507 fileindex, ie, ondisk_path, ie->path, blob, 1);
2508 if (err)
2509 goto done;
2511 break;
2512 default:
2513 goto done;
2516 err = sync_fileindex(fileindex, fileindex_path);
2517 if (err)
2518 goto done;
2519 done:
2520 free(relpath);
2521 free(tree_path);
2522 if (blob)
2523 got_object_blob_close(blob);
2524 if (tree)
2525 got_object_tree_close(tree);
2526 free(tree_id);
2527 if (index) {
2528 if (fclose(index) != 0 && err == NULL)
2529 err = got_error_from_errno("fclose");
2531 if (fileindex)
2532 got_fileindex_free(fileindex);
2533 unlockerr = lock_worktree(worktree, LOCK_SH);
2534 if (unlockerr && err == NULL)
2535 err = unlockerr;
2536 return err;
2539 static void
2540 free_commitable(struct got_commitable *ct)
2542 free(ct->path);
2543 free(ct->in_repo_path);
2544 free(ct->ondisk_path);
2545 free(ct->blob_id);
2546 free(ct->base_blob_id);
2547 free(ct->base_commit_id);
2548 free(ct);
2551 struct collect_commitables_arg {
2552 struct got_pathlist_head *commitable_paths;
2553 struct got_repository *repo;
2554 struct got_worktree *worktree;
2557 static const struct got_error *
2558 collect_commitables(void *arg, unsigned char status, const char *relpath,
2559 struct got_object_id *blob_id, struct got_object_id *commit_id)
2561 struct collect_commitables_arg *a = arg;
2562 const struct got_error *err = NULL;
2563 struct got_commitable *ct = NULL;
2564 struct got_pathlist_entry *new = NULL;
2565 char *parent_path = NULL, *path = NULL;
2566 struct stat sb;
2568 if (status == GOT_STATUS_CONFLICT)
2569 return got_error(GOT_ERR_COMMIT_CONFLICT);
2571 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2572 status != GOT_STATUS_DELETE)
2573 return NULL;
2575 if (asprintf(&path, "/%s", relpath) == -1) {
2576 err = got_error_from_errno("asprintf");
2577 goto done;
2579 if (strcmp(path, "/") == 0) {
2580 parent_path = strdup("");
2581 if (parent_path == NULL)
2582 return got_error_from_errno("strdup");
2583 } else {
2584 err = got_path_dirname(&parent_path, path);
2585 if (err)
2586 return err;
2589 ct = calloc(1, sizeof(*ct));
2590 if (ct == NULL) {
2591 err = got_error_from_errno("calloc");
2592 goto done;
2595 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2596 relpath) == -1) {
2597 err = got_error_from_errno("asprintf");
2598 goto done;
2600 if (status == GOT_STATUS_DELETE) {
2601 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2602 } else {
2603 if (lstat(ct->ondisk_path, &sb) != 0) {
2604 err = got_error_from_errno2("lstat", ct->ondisk_path);
2605 goto done;
2607 ct->mode = sb.st_mode;
2610 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2611 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2612 relpath) == -1) {
2613 err = got_error_from_errno("asprintf");
2614 goto done;
2617 ct->status = status;
2618 ct->blob_id = NULL; /* will be filled in when blob gets created */
2619 if (ct->status != GOT_STATUS_ADD) {
2620 ct->base_blob_id = got_object_id_dup(blob_id);
2621 if (ct->base_blob_id == NULL) {
2622 err = got_error_from_errno("got_object_id_dup");
2623 goto done;
2625 ct->base_commit_id = got_object_id_dup(commit_id);
2626 if (ct->base_commit_id == NULL) {
2627 err = got_error_from_errno("got_object_id_dup");
2628 goto done;
2631 ct->path = strdup(path);
2632 if (ct->path == NULL) {
2633 err = got_error_from_errno("strdup");
2634 goto done;
2636 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2637 done:
2638 if (ct && (err || new == NULL))
2639 free_commitable(ct);
2640 free(parent_path);
2641 free(path);
2642 return err;
2645 static const struct got_error *write_tree(struct got_object_id **,
2646 struct got_tree_object *, const char *, struct got_pathlist_head *,
2647 got_worktree_status_cb status_cb, void *status_arg,
2648 struct got_repository *);
2650 static const struct got_error *
2651 write_subtree(struct got_object_id **new_subtree_id,
2652 struct got_tree_entry *te, const char *parent_path,
2653 struct got_pathlist_head *commitable_paths,
2654 got_worktree_status_cb status_cb, void *status_arg,
2655 struct got_repository *repo)
2657 const struct got_error *err = NULL;
2658 struct got_tree_object *subtree;
2659 char *subpath;
2661 if (asprintf(&subpath, "%s%s%s", parent_path,
2662 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2663 return got_error_from_errno("asprintf");
2665 err = got_object_open_as_tree(&subtree, repo, te->id);
2666 if (err)
2667 return err;
2669 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2670 status_cb, status_arg, repo);
2671 got_object_tree_close(subtree);
2672 free(subpath);
2673 return err;
2676 static const struct got_error *
2677 match_ct_parent_path(int *match, struct got_commitable *ct, const char *path)
2679 const struct got_error *err = NULL;
2680 char *ct_parent_path = NULL;
2682 *match = 0;
2684 if (strchr(ct->path, '/') == NULL) {
2685 *match = got_path_is_root_dir(path);
2686 return NULL;
2689 err = got_path_dirname(&ct_parent_path, ct->path);
2690 if (err)
2691 return err;
2692 *match = (strcmp(path, ct_parent_path) == 0);
2693 free(ct_parent_path);
2694 return err;
2697 static mode_t
2698 get_ct_file_mode(struct got_commitable *ct)
2700 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2703 static const struct got_error *
2704 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2705 struct got_tree_entry *te, struct got_commitable *ct)
2707 const struct got_error *err = NULL;
2709 *new_te = NULL;
2711 err = got_object_tree_entry_dup(new_te, te);
2712 if (err)
2713 goto done;
2715 (*new_te)->mode = get_ct_file_mode(ct);
2717 free((*new_te)->id);
2718 (*new_te)->id = got_object_id_dup(ct->blob_id);
2719 if ((*new_te)->id == NULL) {
2720 err = got_error_from_errno("got_object_id_dup");
2721 goto done;
2723 done:
2724 if (err && *new_te) {
2725 got_object_tree_entry_close(*new_te);
2726 *new_te = NULL;
2728 return err;
2731 static const struct got_error *
2732 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2733 struct got_commitable *ct)
2735 const struct got_error *err = NULL;
2736 char *ct_name;
2738 *new_te = NULL;
2740 *new_te = calloc(1, sizeof(**new_te));
2741 if (*new_te == NULL)
2742 return got_error_from_errno("calloc");
2744 ct_name = basename(ct->path);
2745 if (ct_name == NULL) {
2746 err = got_error_from_errno2("basename", ct->path);
2747 goto done;
2749 (*new_te)->name = strdup(ct_name);
2750 if ((*new_te)->name == NULL) {
2751 err = got_error_from_errno("strdup");
2752 goto done;
2755 (*new_te)->mode = get_ct_file_mode(ct);
2757 (*new_te)->id = got_object_id_dup(ct->blob_id);
2758 if ((*new_te)->id == NULL) {
2759 err = got_error_from_errno("got_object_id_dup");
2760 goto done;
2762 done:
2763 if (err && *new_te) {
2764 got_object_tree_entry_close(*new_te);
2765 *new_te = NULL;
2767 return err;
2770 static const struct got_error *
2771 insert_tree_entry(struct got_tree_entry *new_te,
2772 struct got_pathlist_head *paths)
2774 const struct got_error *err = NULL;
2775 struct got_pathlist_entry *new_pe;
2777 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2778 if (err)
2779 return err;
2780 if (new_pe == NULL)
2781 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2782 return NULL;
2785 static const struct got_error *
2786 report_ct_status(struct got_commitable *ct,
2787 got_worktree_status_cb status_cb, void *status_arg)
2789 const char *ct_path = ct->path;
2790 while (ct_path[0] == '/')
2791 ct_path++;
2792 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id, NULL);
2795 static const struct got_error *
2796 match_modified_subtree(int *modified, struct got_tree_entry *te,
2797 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2799 const struct got_error *err = NULL;
2800 struct got_pathlist_entry *pe;
2801 char *te_path;
2803 *modified = 0;
2805 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2806 got_path_is_root_dir(base_tree_path) ? "" : "/",
2807 te->name) == -1)
2808 return got_error_from_errno("asprintf");
2810 TAILQ_FOREACH(pe, commitable_paths, entry) {
2811 struct got_commitable *ct = pe->data;
2812 *modified = got_path_is_child(ct->in_repo_path, te_path,
2813 strlen(te_path));
2814 if (*modified)
2815 break;
2818 free(te_path);
2819 return err;
2822 static const struct got_error *
2823 match_deleted_or_modified_ct(struct got_commitable **ctp,
2824 struct got_tree_entry *te, const char *base_tree_path,
2825 struct got_pathlist_head *commitable_paths)
2827 const struct got_error *err = NULL;
2828 struct got_pathlist_entry *pe;
2830 *ctp = NULL;
2832 TAILQ_FOREACH(pe, commitable_paths, entry) {
2833 struct got_commitable *ct = pe->data;
2834 char *ct_name = NULL;
2835 int path_matches;
2837 if (ct->status != GOT_STATUS_MODIFY &&
2838 ct->status != GOT_STATUS_DELETE)
2839 continue;
2841 if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
2842 continue;
2844 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2845 if (err)
2846 return err;
2847 if (!path_matches)
2848 continue;
2850 ct_name = basename(pe->path);
2851 if (ct_name == NULL)
2852 return got_error_from_errno2("basename", pe->path);
2854 if (strcmp(te->name, ct_name) != 0)
2855 continue;
2857 *ctp = ct;
2858 break;
2861 return err;
2864 static const struct got_error *
2865 write_tree(struct got_object_id **new_tree_id,
2866 struct got_tree_object *base_tree, const char *path_base_tree,
2867 struct got_pathlist_head *commitable_paths,
2868 got_worktree_status_cb status_cb, void *status_arg,
2869 struct got_repository *repo)
2871 const struct got_error *err = NULL;
2872 const struct got_tree_entries *base_entries = NULL;
2873 struct got_pathlist_head paths;
2874 struct got_tree_entries new_tree_entries;
2875 struct got_tree_entry *te, *new_te = NULL;
2876 struct got_pathlist_entry *pe;
2878 TAILQ_INIT(&paths);
2879 new_tree_entries.nentries = 0;
2880 SIMPLEQ_INIT(&new_tree_entries.head);
2882 /* Insert, and recurse into, newly added entries first. */
2883 TAILQ_FOREACH(pe, commitable_paths, entry) {
2884 struct got_commitable *ct = pe->data;
2885 char *child_path = NULL, *slash;
2887 if (ct->status != GOT_STATUS_ADD ||
2888 (ct->flags & GOT_COMMITABLE_ADDED))
2889 continue;
2891 if (!got_path_is_child(pe->path, path_base_tree,
2892 strlen(path_base_tree)))
2893 continue;
2895 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2896 pe->path);
2897 if (err)
2898 goto done;
2900 slash = strchr(child_path, '/');
2901 if (slash == NULL) {
2902 err = alloc_added_blob_tree_entry(&new_te, ct);
2903 if (err)
2904 goto done;
2905 err = report_ct_status(ct, status_cb, status_arg);
2906 if (err)
2907 goto done;
2908 ct->flags |= GOT_COMMITABLE_ADDED;
2909 } else {
2910 char *subtree_path;
2912 *slash = '\0'; /* trim trailing path components */
2913 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2914 got_path_is_root_dir(path_base_tree) ? "" : "/",
2915 child_path) == -1) {
2916 err = got_error_from_errno("asprintf");
2917 goto done;
2920 new_te = calloc(1, sizeof(*new_te));
2921 new_te->mode = S_IFDIR;
2922 new_te->name = strdup(child_path);
2923 if (new_te->name == NULL) {
2924 err = got_error_from_errno("strdup");
2925 got_object_tree_entry_close(new_te);
2926 new_te = NULL;
2927 goto done;
2929 err = write_tree(&new_te->id, NULL, subtree_path,
2930 commitable_paths, status_cb, status_arg, repo);
2931 free(subtree_path);
2932 if (err) {
2933 got_object_tree_entry_close(new_te);
2934 new_te = NULL;
2935 goto done;
2938 err = insert_tree_entry(new_te, &paths);
2939 if (err)
2940 goto done;
2943 if (base_tree) {
2944 /* Handle modified and deleted entries. */
2945 base_entries = got_object_tree_get_entries(base_tree);
2946 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2947 struct got_commitable *ct = NULL;
2949 if (S_ISDIR(te->mode)) {
2950 int modified;
2951 err = got_object_tree_entry_dup(&new_te, te);
2952 if (err)
2953 goto done;
2954 err = match_modified_subtree(&modified, te,
2955 path_base_tree, commitable_paths);
2956 if (err)
2957 goto done;
2958 /* Avoid recursion into unmodified subtrees. */
2959 if (modified) {
2960 free(new_te->id);
2961 err = write_subtree(&new_te->id, te,
2962 path_base_tree, commitable_paths,
2963 status_cb, status_arg, repo);
2964 if (err)
2965 goto done;
2967 err = insert_tree_entry(new_te, &paths);
2968 if (err)
2969 goto done;
2970 continue;
2973 err = match_deleted_or_modified_ct(&ct, te,
2974 path_base_tree, commitable_paths);
2975 if (ct) {
2976 /* NB: Deleted entries get dropped here. */
2977 if (ct->status == GOT_STATUS_MODIFY) {
2978 err = alloc_modified_blob_tree_entry(
2979 &new_te, te, ct);
2980 if (err)
2981 goto done;
2982 err = insert_tree_entry(new_te, &paths);
2983 if (err)
2984 goto done;
2986 err = report_ct_status(ct, status_cb,
2987 status_arg);
2988 if (err)
2989 goto done;
2990 } else {
2991 /* Entry is unchanged; just copy it. */
2992 err = got_object_tree_entry_dup(&new_te, te);
2993 if (err)
2994 goto done;
2995 err = insert_tree_entry(new_te, &paths);
2996 if (err)
2997 goto done;
3002 /* Write new list of entries; deleted entries have been dropped. */
3003 TAILQ_FOREACH(pe, &paths, entry) {
3004 struct got_tree_entry *te = pe->data;
3005 new_tree_entries.nentries++;
3006 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
3008 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
3009 done:
3010 got_object_tree_entries_close(&new_tree_entries);
3011 got_pathlist_free(&paths);
3012 return err;
3015 static const struct got_error *
3016 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
3017 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
3019 const struct got_error *err = NULL;
3020 char *fileindex_path = NULL;
3021 struct got_fileindex *fileindex = NULL;
3022 struct got_pathlist_entry *pe;
3024 err = open_fileindex(&fileindex, &fileindex_path, worktree);
3025 if (err)
3026 return err;
3028 TAILQ_FOREACH(pe, commitable_paths, entry) {
3029 struct got_fileindex_entry *ie;
3030 struct got_commitable *ct = pe->data;
3032 ie = got_fileindex_entry_get(fileindex, pe->path);
3033 if (ie) {
3034 if (ct->status == GOT_STATUS_DELETE) {
3035 got_fileindex_entry_remove(fileindex, ie);
3036 got_fileindex_entry_free(ie);
3037 } else
3038 err = got_fileindex_entry_update(ie,
3039 ct->ondisk_path, ct->blob_id->sha1,
3040 new_base_commit_id->sha1, 1);
3041 } else {
3042 err = got_fileindex_entry_alloc(&ie,
3043 ct->ondisk_path, pe->path, ct->blob_id->sha1,
3044 new_base_commit_id->sha1);
3045 if (err)
3046 goto done;
3047 err = got_fileindex_entry_add(fileindex, ie);
3048 if (err)
3049 goto done;
3053 err = sync_fileindex(fileindex, fileindex_path);
3054 done:
3055 free(fileindex_path);
3056 got_fileindex_free(fileindex);
3057 return err;
3060 static const struct got_error *
3061 check_ct_out_of_date(struct got_commitable *ct, struct got_repository *repo,
3062 struct got_object_id *head_commit_id)
3064 const struct got_error *err = NULL;
3065 struct got_object_id *id_in_head;
3068 * Require that modified/deleted files are based on the branch head.
3069 * This requirement could be relaxed to force less update operations
3070 * on users but, for now, we want to play it safe and see how it goes.
3072 * If this check is relaxed, it must still ensure that no modifications
3073 * were made to files *and their parents* in commits between the file's
3074 * base commit and the branch head. Otherwise, tree conflicts will not
3075 * be detected reliably.
3077 if (ct->status != GOT_STATUS_ADD) {
3078 if (got_object_id_cmp(ct->base_commit_id, head_commit_id) != 0)
3079 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3080 return NULL;
3083 /* Require that added files don't exist in the branch head. */
3084 err = got_object_id_by_path(&id_in_head, repo, head_commit_id,
3085 ct->in_repo_path);
3086 if (err && err->code != GOT_ERR_NO_TREE_ENTRY)
3087 return err;
3088 if (id_in_head) {
3089 free(id_in_head);
3090 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
3093 free(id_in_head);
3094 return NULL;
3097 const struct got_error *
3098 got_worktree_commit(struct got_object_id **new_commit_id,
3099 struct got_worktree *worktree, const char *ondisk_path,
3100 const char *author, const char *committer,
3101 got_worktree_commit_msg_cb commit_msg_cb, void *commit_arg,
3102 got_worktree_status_cb status_cb, void *status_arg,
3103 struct got_repository *repo)
3105 const struct got_error *err = NULL, *unlockerr = NULL;
3106 struct collect_commitables_arg cc_arg;
3107 struct got_pathlist_head commitable_paths;
3108 struct got_pathlist_entry *pe;
3109 char *relpath = NULL;
3110 const char *head_ref_name = NULL;
3111 struct got_reference *head_ref = NULL;
3112 struct got_commit_object *head_commit = NULL;
3113 struct got_object_id *head_commit_id = NULL;
3114 struct got_reference *head_ref2 = NULL;
3115 struct got_object_id *head_commit_id2 = NULL;
3116 struct got_tree_object *head_tree = NULL;
3117 struct got_object_id *new_tree_id = NULL;
3118 struct got_object_id_queue parent_ids;
3119 struct got_object_qid *pid = NULL;
3120 char *logmsg = NULL;
3122 *new_commit_id = NULL;
3124 TAILQ_INIT(&commitable_paths);
3125 SIMPLEQ_INIT(&parent_ids);
3127 if (ondisk_path) {
3128 err = got_path_skip_common_ancestor(&relpath,
3129 worktree->root_path, ondisk_path);
3130 if (err)
3131 return err;
3134 err = lock_worktree(worktree, LOCK_EX);
3135 if (err)
3136 goto done;
3138 err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
3139 if (err)
3140 goto done;
3141 err = got_ref_resolve(&head_commit_id, repo, head_ref);
3142 if (err)
3143 goto done;
3145 cc_arg.commitable_paths = &commitable_paths;
3146 cc_arg.worktree = worktree;
3147 cc_arg.repo = repo;
3148 err = got_worktree_status(worktree, relpath ? relpath : "",
3149 repo, collect_commitables, &cc_arg, NULL, NULL);
3150 if (err)
3151 goto done;
3153 if (TAILQ_EMPTY(&commitable_paths)) {
3154 err = got_error(GOT_ERR_COMMIT_NO_CHANGES);
3155 goto done;
3158 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
3159 if (err)
3160 goto done;
3162 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3163 struct got_commitable *ct = pe->data;
3164 err = check_ct_out_of_date(ct, repo, head_commit_id);
3165 if (err)
3166 goto done;
3169 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
3170 if (err)
3171 goto done;
3173 if (commit_msg_cb != NULL) {
3174 err = commit_msg_cb(&commitable_paths, &logmsg, commit_arg);
3175 if (err)
3176 goto done;
3179 if (logmsg == NULL || strlen(logmsg) == 0) {
3180 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
3181 goto done;
3184 /* Create blobs from added and modified files and record their IDs. */
3185 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3186 struct got_commitable *ct = pe->data;
3187 char *ondisk_path;
3189 if (ct->status != GOT_STATUS_ADD &&
3190 ct->status != GOT_STATUS_MODIFY)
3191 continue;
3193 if (asprintf(&ondisk_path, "%s/%s",
3194 worktree->root_path, pe->path) == -1) {
3195 err = got_error_from_errno("asprintf");
3196 goto done;
3198 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
3199 free(ondisk_path);
3200 if (err)
3201 goto done;
3204 /* Recursively write new tree objects. */
3205 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
3206 status_cb, status_arg, repo);
3207 if (err)
3208 goto done;
3210 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
3211 if (err)
3212 goto done;
3213 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
3214 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
3215 1, author, time(NULL), committer, time(NULL), logmsg, repo);
3216 got_object_qid_free(pid);
3217 if (logmsg != NULL)
3218 free(logmsg);
3219 if (err)
3220 goto done;
3222 /* Check if a concurrent commit to our branch has occurred. */
3223 head_ref_name = got_worktree_get_head_ref_name(worktree);
3224 if (head_ref_name == NULL) {
3225 err = got_error_from_errno("got_worktree_get_head_ref_name");
3226 goto done;
3228 /* Lock the reference here to prevent concurrent modification. */
3229 err = got_ref_open(&head_ref2, repo, head_ref_name, 1);
3230 if (err)
3231 goto done;
3232 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
3233 if (err)
3234 goto done;
3235 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
3236 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
3237 goto done;
3239 /* Update branch head in repository. */
3240 err = got_ref_change_ref(head_ref2, *new_commit_id);
3241 if (err)
3242 goto done;
3243 err = got_ref_write(head_ref2, repo);
3244 if (err)
3245 goto done;
3247 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
3248 if (err)
3249 goto done;
3251 err = ref_base_commit(worktree, repo);
3252 if (err)
3253 goto done;
3255 err = update_fileindex_after_commit(&commitable_paths,
3256 *new_commit_id, worktree);
3257 if (err)
3258 goto done;
3259 done:
3260 unlockerr = lock_worktree(worktree, LOCK_SH);
3261 if (unlockerr && err == NULL)
3262 err = unlockerr;
3263 TAILQ_FOREACH(pe, &commitable_paths, entry) {
3264 struct got_commitable *ct = pe->data;
3265 free_commitable(ct);
3267 got_pathlist_free(&commitable_paths);
3268 if (head_tree)
3269 got_object_tree_close(head_tree);
3270 if (head_commit)
3271 got_object_commit_close(head_commit);
3272 free(relpath);
3273 free(head_commit_id);
3274 free(head_commit_id2);
3275 if (head_ref)
3276 got_ref_close(head_ref);
3277 if (head_ref2) {
3278 unlockerr = got_ref_unlock(head_ref2);
3279 if (unlockerr && err == NULL)
3280 err = unlockerr;
3281 got_ref_close(head_ref2);
3283 return err;
3286 const char *
3287 got_commitable_get_path(struct got_commitable *ct)
3289 return ct->path;
3292 unsigned int
3293 got_commitable_get_status(struct got_commitable *ct)
3295 return ct->status;