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_worktree.h"
42 #include "got_opentemp.h"
44 #include "got_lib_worktree.h"
45 #include "got_lib_path.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_fileindex.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_create.h"
53 #include "got_lib_object_idset.h"
54 #include "got_lib_diff.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 static const struct got_error *
61 create_meta_file(const char *path_got, const char *name, const char *content)
62 {
63 const struct got_error *err = NULL;
64 char *path;
65 int fd = -1;
67 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
68 err = got_error_from_errno();
69 path = NULL;
70 goto done;
71 }
73 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
74 GOT_DEFAULT_FILE_MODE);
75 if (fd == -1) {
76 err = got_error_from_errno();
77 goto done;
78 }
80 if (content) {
81 int len = dprintf(fd, "%s\n", content);
82 if (len != strlen(content) + 1) {
83 err = got_error_from_errno();
84 goto done;
85 }
86 }
88 done:
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_from_errno();
91 free(path);
92 return err;
93 }
95 static const struct got_error *
96 update_meta_file(const char *path_got, const char *name, const char *content)
97 {
98 const struct got_error *err = NULL;
99 FILE *tmpfile = NULL;
100 char *tmppath = NULL;
101 char *path = NULL;
103 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
104 err = got_error_from_errno();
105 path = NULL;
106 goto done;
109 err = got_opentemp_named(&tmppath, &tmpfile, path);
110 if (err)
111 goto done;
113 if (content) {
114 int len = fprintf(tmpfile, "%s\n", content);
115 if (len != strlen(content) + 1) {
116 err = got_error_from_errno();
117 goto done;
121 if (rename(tmppath, path) != 0) {
122 err = got_error_from_errno();
123 unlink(tmppath);
124 goto done;
127 done:
128 free(tmppath);
129 if (fclose(tmpfile) != 0 && err == NULL)
130 err = got_error_from_errno();
131 return err;
134 static const struct got_error *
135 read_meta_file(char **content, const char *path_got, const char *name)
137 const struct got_error *err = NULL;
138 char *path;
139 int fd = -1;
140 ssize_t n;
141 struct stat sb;
143 *content = NULL;
145 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
146 err = got_error_from_errno();
147 path = NULL;
148 goto done;
151 fd = open(path, O_RDONLY | O_NOFOLLOW);
152 if (fd == -1) {
153 if (errno == ENOENT)
154 err = got_error(GOT_ERR_WORKTREE_META);
155 else
156 err = got_error_from_errno();
157 goto done;
159 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
160 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
161 : got_error_from_errno());
162 goto done;
165 if (lstat(path, &sb) != 0) {
166 err = got_error_from_errno();
167 goto done;
169 *content = calloc(1, sb.st_size);
170 if (*content == NULL) {
171 err = got_error_from_errno();
172 goto done;
175 n = read(fd, *content, sb.st_size);
176 if (n != sb.st_size) {
177 err = (n == -1 ? got_error_from_errno() :
178 got_error(GOT_ERR_WORKTREE_META));
179 goto done;
181 if ((*content)[sb.st_size - 1] != '\n') {
182 err = got_error(GOT_ERR_WORKTREE_META);
183 goto done;
185 (*content)[sb.st_size - 1] = '\0';
187 done:
188 if (fd != -1 && close(fd) == -1 && err == NULL)
189 err = got_error_from_errno();
190 free(path);
191 if (err) {
192 free(*content);
193 *content = NULL;
195 return err;
198 const struct got_error *
199 got_worktree_init(const char *path, struct got_reference *head_ref,
200 const char *prefix, struct got_repository *repo)
202 const struct got_error *err = NULL;
203 struct got_object_id *commit_id = NULL;
204 uuid_t uuid;
205 uint32_t uuid_status;
206 int obj_type;
207 char *path_got = NULL;
208 char *refstr = NULL;
209 char *formatstr = NULL;
210 char *absprefix = NULL;
211 char *basestr = NULL;
212 char *uuidstr = NULL;
214 if (strcmp(path, got_repo_get_path(repo)) == 0) {
215 err = got_error(GOT_ERR_WORKTREE_REPO);
216 goto done;
219 err = got_ref_resolve(&commit_id, repo, head_ref);
220 if (err)
221 return err;
222 err = got_object_get_type(&obj_type, repo, commit_id);
223 if (err)
224 return err;
225 if (obj_type != GOT_OBJ_TYPE_COMMIT)
226 return got_error(GOT_ERR_OBJ_TYPE);
228 if (!got_path_is_absolute(prefix)) {
229 if (asprintf(&absprefix, "/%s", prefix) == -1)
230 return got_error_from_errno();
233 /* Create top-level directory (may already exist). */
234 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
235 err = got_error_from_errno();
236 goto done;
239 /* Create .got directory (may already exist). */
240 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 err = got_error_from_errno();
242 goto done;
244 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
245 err = got_error_from_errno();
246 goto done;
249 /* Create an empty lock file. */
250 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
251 if (err)
252 goto done;
254 /* Create an empty file index. */
255 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
256 if (err)
257 goto done;
259 /* Write the HEAD reference. */
260 refstr = got_ref_to_str(head_ref);
261 if (refstr == NULL) {
262 err = got_error_from_errno();
263 goto done;
265 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
266 if (err)
267 goto done;
269 /* Record our base commit. */
270 err = got_object_id_str(&basestr, commit_id);
271 if (err)
272 goto done;
273 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
274 if (err)
275 goto done;
277 /* Store path to repository. */
278 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
279 got_repo_get_path(repo));
280 if (err)
281 goto done;
283 /* Store in-repository path prefix. */
284 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
285 absprefix ? absprefix : prefix);
286 if (err)
287 goto done;
289 /* Generate UUID. */
290 uuid_create(&uuid, &uuid_status);
291 if (uuid_status != uuid_s_ok) {
292 err = got_error_uuid(uuid_status);
293 goto done;
295 uuid_to_string(&uuid, &uuidstr, &uuid_status);
296 if (uuid_status != uuid_s_ok) {
297 err = got_error_uuid(uuid_status);
298 goto done;
300 err = create_meta_file(path_got, GOT_WORKTREE_UUID, uuidstr);
301 if (err)
302 goto done;
304 /* Stamp work tree with format file. */
305 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
306 err = got_error_from_errno();
307 goto done;
309 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
310 if (err)
311 goto done;
313 done:
314 free(commit_id);
315 free(path_got);
316 free(formatstr);
317 free(refstr);
318 free(absprefix);
319 free(basestr);
320 free(uuidstr);
321 return err;
324 static const struct got_error *
325 open_worktree(struct got_worktree **worktree, const char *path)
327 const struct got_error *err = NULL;
328 char *path_got;
329 char *formatstr = NULL;
330 char *uuidstr = NULL;
331 char *path_lock = NULL;
332 char *base_commit_id_str = NULL;
333 char *head_ref_str = NULL;
334 int version, fd = -1;
335 const char *errstr;
336 struct got_repository *repo = NULL;
337 uint32_t uuid_status;
339 *worktree = NULL;
341 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
342 err = got_error_from_errno();
343 path_got = NULL;
344 goto done;
347 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
348 err = got_error_from_errno();
349 path_lock = NULL;
350 goto done;
353 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
354 if (fd == -1) {
355 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
356 : got_error_from_errno());
357 goto done;
360 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
361 if (err)
362 goto done;
364 version = strtonum(formatstr, 1, INT_MAX, &errstr);
365 if (errstr) {
366 err = got_error(GOT_ERR_WORKTREE_META);
367 goto done;
369 if (version != GOT_WORKTREE_FORMAT_VERSION) {
370 err = got_error(GOT_ERR_WORKTREE_VERS);
371 goto done;
374 *worktree = calloc(1, sizeof(**worktree));
375 if (*worktree == NULL) {
376 err = got_error_from_errno();
377 goto done;
379 (*worktree)->lockfd = -1;
381 (*worktree)->root_path = strdup(path);
382 if ((*worktree)->root_path == NULL) {
383 err = got_error_from_errno();
384 goto done;
386 err = read_meta_file(&(*worktree)->repo_path, path_got,
387 GOT_WORKTREE_REPOSITORY);
388 if (err)
389 goto done;
391 err = read_meta_file(&(*worktree)->path_prefix, path_got,
392 GOT_WORKTREE_PATH_PREFIX);
393 if (err)
394 goto done;
396 err = read_meta_file(&base_commit_id_str, path_got,
397 GOT_WORKTREE_BASE_COMMIT);
398 if (err)
399 goto done;
401 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
402 if (err)
403 goto done;
404 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
405 if (uuid_status != uuid_s_ok) {
406 err = got_error_uuid(uuid_status);
407 goto done;
410 err = got_repo_open(&repo, (*worktree)->repo_path);
411 if (err)
412 goto done;
414 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
415 base_commit_id_str);
416 if (err)
417 goto done;
419 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
420 if (err)
421 goto done;
423 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
424 done:
425 if (repo)
426 got_repo_close(repo);
427 free(path_got);
428 free(path_lock);
429 free(head_ref_str);
430 free(base_commit_id_str);
431 free(uuidstr);
432 free(formatstr);
433 if (err) {
434 if (fd != -1)
435 close(fd);
436 if (*worktree != NULL)
437 got_worktree_close(*worktree);
438 *worktree = NULL;
439 } else
440 (*worktree)->lockfd = fd;
442 return err;
445 const struct got_error *
446 got_worktree_open(struct got_worktree **worktree, const char *path)
448 const struct got_error *err = NULL;
450 do {
451 err = open_worktree(worktree, path);
452 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
453 return err;
454 if (*worktree)
455 return NULL;
456 path = dirname(path);
457 if (path == NULL)
458 return got_error_from_errno();
459 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
461 return got_error(GOT_ERR_NOT_WORKTREE);
464 const struct got_error *
465 got_worktree_close(struct got_worktree *worktree)
467 const struct got_error *err = NULL;
468 free(worktree->root_path);
469 free(worktree->repo_path);
470 free(worktree->path_prefix);
471 free(worktree->base_commit_id);
472 if (worktree->head_ref)
473 got_ref_close(worktree->head_ref);
474 if (worktree->lockfd != -1)
475 if (close(worktree->lockfd) != 0)
476 err = got_error_from_errno();
477 free(worktree);
478 return err;
481 const char *
482 got_worktree_get_root_path(struct got_worktree *worktree)
484 return worktree->root_path;
487 const char *
488 got_worktree_get_repo_path(struct got_worktree *worktree)
490 return worktree->repo_path;
493 const char *
494 got_worktree_get_path_prefix(struct got_worktree *worktree)
496 return worktree->path_prefix;
499 const struct got_error *
500 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
501 const char *path_prefix)
503 char *absprefix = NULL;
505 if (!got_path_is_absolute(path_prefix)) {
506 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
507 return got_error_from_errno();
509 *match = (strcmp(absprefix ? absprefix : path_prefix,
510 worktree->path_prefix) == 0);
511 free(absprefix);
512 return NULL;
515 char *
516 got_worktree_get_head_ref_name(struct got_worktree *worktree)
518 return got_ref_to_str(worktree->head_ref);
521 struct got_reference *
522 got_worktree_get_head_ref(struct got_worktree *worktree)
524 return got_ref_dup(worktree->head_ref);
527 struct got_object_id *
528 got_worktree_get_base_commit_id(struct got_worktree *worktree)
530 return worktree->base_commit_id;
533 const struct got_error *
534 got_worktree_set_base_commit_id(struct got_worktree *worktree,
535 struct got_repository *repo, struct got_object_id *commit_id)
537 const struct got_error *err;
538 struct got_object *obj = NULL;
539 char *id_str = NULL;
540 char *path_got = NULL;
542 if (asprintf(&path_got, "%s/%s", worktree->root_path,
543 GOT_WORKTREE_GOT_DIR) == -1) {
544 err = got_error_from_errno();
545 path_got = NULL;
546 goto done;
549 err = got_object_open(&obj, repo, commit_id);
550 if (err)
551 return err;
553 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
554 err = got_error(GOT_ERR_OBJ_TYPE);
555 goto done;
558 /* Record our base commit. */
559 err = got_object_id_str(&id_str, commit_id);
560 if (err)
561 goto done;
562 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
563 if (err)
564 goto done;
566 free(worktree->base_commit_id);
567 worktree->base_commit_id = got_object_id_dup(commit_id);
568 if (worktree->base_commit_id == NULL) {
569 err = got_error_from_errno();
570 goto done;
572 done:
573 if (obj)
574 got_object_close(obj);
575 free(id_str);
576 free(path_got);
577 return err;
580 static const struct got_error *
581 lock_worktree(struct got_worktree *worktree, int operation)
583 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
584 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
585 : got_error_from_errno());
586 return NULL;
589 static const struct got_error *
590 add_dir_on_disk(struct got_worktree *worktree, const char *path)
592 const struct got_error *err = NULL;
593 char *abspath;
595 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
596 return got_error_from_errno();
598 err = got_path_mkdir(abspath);
599 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
600 struct stat sb;
601 err = NULL;
602 if (lstat(abspath, &sb) == -1) {
603 err = got_error_from_errno();
604 } else if (!S_ISDIR(sb.st_mode)) {
605 /* TODO directory is obstructed; do something */
606 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
609 free(abspath);
610 return err;
613 static const struct got_error *
614 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
616 const struct got_error *err = NULL;
617 uint8_t fbuf1[8192];
618 uint8_t fbuf2[8192];
619 size_t flen1 = 0, flen2 = 0;
621 *same = 1;
623 while (1) {
624 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
625 if (flen1 == 0 && ferror(f1)) {
626 err = got_error_from_errno();
627 break;
629 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
630 if (flen2 == 0 && ferror(f2)) {
631 err = got_error_from_errno();
632 break;
634 if (flen1 == 0) {
635 if (flen2 != 0)
636 *same = 0;
637 break;
638 } else if (flen2 == 0) {
639 if (flen1 != 0)
640 *same = 0;
641 break;
642 } else if (flen1 == flen2) {
643 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
644 *same = 0;
645 break;
647 } else {
648 *same = 0;
649 break;
653 return err;
656 static const struct got_error *
657 check_files_equal(int *same, const char *f1_path, const char *f2_path)
659 const struct got_error *err = NULL;
660 struct stat sb;
661 size_t size1, size2;
662 FILE *f1 = NULL, *f2 = NULL;
664 *same = 1;
666 if (lstat(f1_path, &sb) != 0) {
667 err = got_error_from_errno();
668 goto done;
670 size1 = sb.st_size;
672 if (lstat(f2_path, &sb) != 0) {
673 err = got_error_from_errno();
674 goto done;
676 size2 = sb.st_size;
678 if (size1 != size2) {
679 *same = 0;
680 return NULL;
683 f1 = fopen(f1_path, "r");
684 if (f1 == NULL)
685 return got_error_from_errno();
687 f2 = fopen(f2_path, "r");
688 if (f2 == NULL) {
689 err = got_error_from_errno();
690 goto done;
693 err = check_file_contents_equal(same, f1, f2);
694 done:
695 if (f1 && fclose(f1) != 0 && err == NULL)
696 err = got_error_from_errno();
697 if (f2 && fclose(f2) != 0 && err == NULL)
698 err = got_error_from_errno();
700 return err;
703 /*
704 * Perform a 3-way merge where the file's version in the file index (blob2)
705 * acts as the common ancestor, the incoming blob (blob1) acts as the first
706 * derived version, and the file on disk acts as the second derived version.
707 */
708 static const struct got_error *
709 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
710 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
711 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
712 struct got_repository *repo,
713 got_worktree_checkout_cb progress_cb, void *progress_arg)
715 const struct got_error *err = NULL;
716 int merged_fd = -1;
717 struct got_blob_object *blob2 = NULL;
718 FILE *f1 = NULL, *f2 = NULL;
719 char *blob1_path = NULL, *blob2_path = NULL;
720 char *merged_path = NULL, *base_path = NULL;
721 char *id_str = NULL;
722 char *label1 = NULL;
723 int overlapcnt = 0, update_timestamps = 0;
724 char *parent;
726 parent = dirname(ondisk_path);
727 if (parent == NULL)
728 return got_error_from_errno();
730 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
731 return got_error_from_errno();
733 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
734 if (err)
735 goto done;
737 free(base_path);
738 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
739 err = got_error_from_errno();
740 base_path = NULL;
741 goto done;
744 err = got_opentemp_named(&blob1_path, &f1, base_path);
745 if (err)
746 goto done;
747 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
748 if (err)
749 goto done;
751 free(base_path);
752 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
753 err = got_error_from_errno();
754 base_path = NULL;
755 goto done;
758 err = got_opentemp_named(&blob2_path, &f2, base_path);
759 if (err)
760 goto done;
761 if (got_fileindex_entry_has_blob(ie)) {
762 struct got_object_id id2;
763 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
764 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
765 if (err)
766 goto done;
767 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
768 if (err)
769 goto done;
770 } else {
771 /*
772 * If the file has no blob, this is an "add vs add" conflict,
773 * and we simply use an empty ancestor file to make both files
774 * appear in the merged result in their entirety.
775 */
778 err = got_object_id_str(&id_str, worktree->base_commit_id);
779 if (err)
780 goto done;
781 if (asprintf(&label1, "commit %s", id_str) == -1) {
782 err = got_error_from_errno();
783 goto done;
786 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
787 blob2_path, ondisk_path, label1, path);
788 if (err)
789 goto done;
791 (*progress_cb)(progress_arg,
792 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
795 if (fsync(merged_fd) != 0) {
796 err = got_error_from_errno();
797 goto done;
800 /* Check if a clean merge has subsumed all local changes. */
801 if (overlapcnt == 0) {
802 err = check_files_equal(&update_timestamps, blob1_path,
803 merged_path);
804 if (err)
805 goto done;
808 if (chmod(merged_path, st_mode) != 0) {
809 err = got_error_from_errno();
810 goto done;
813 if (rename(merged_path, ondisk_path) != 0) {
814 err = got_error_from_errno();
815 unlink(merged_path);
816 goto done;
819 /*
820 * Do not update timestamps of already modified files. Otherwise,
821 * a future status walk would treat them as unmodified files again.
822 */
823 err = got_fileindex_entry_update(ie, ondisk_path,
824 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
825 done:
826 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
827 err = got_error_from_errno();
828 if (f1 && fclose(f1) != 0 && err == NULL)
829 err = got_error_from_errno();
830 if (f2 && fclose(f2) != 0 && err == NULL)
831 err = got_error_from_errno();
832 if (blob2)
833 got_object_blob_close(blob2);
834 free(merged_path);
835 free(base_path);
836 if (blob1_path) {
837 unlink(blob1_path);
838 free(blob1_path);
840 if (blob2_path) {
841 unlink(blob2_path);
842 free(blob2_path);
844 free(id_str);
845 free(label1);
846 return err;
849 static const struct got_error *
850 update_blob_fileindex_entry(struct got_worktree *worktree,
851 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
852 const char *ondisk_path, const char *path, struct got_blob_object *blob,
853 int update_timestamps)
855 const struct got_error *err = NULL;
857 if (ie == NULL)
858 ie = got_fileindex_entry_get(fileindex, path);
859 if (ie)
860 err = got_fileindex_entry_update(ie, ondisk_path,
861 blob->id.sha1, worktree->base_commit_id->sha1,
862 update_timestamps);
863 else {
864 struct got_fileindex_entry *new_ie;
865 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
866 path, blob->id.sha1, worktree->base_commit_id->sha1);
867 if (!err)
868 err = got_fileindex_entry_add(fileindex, new_ie);
870 return err;
873 static const struct got_error *
874 install_blob(struct got_worktree *worktree, const char *ondisk_path,
875 const char *path, uint16_t te_mode, uint16_t st_mode,
876 struct got_blob_object *blob, int restoring_missing_file,
877 int reverting_versioned_file, struct got_repository *repo,
878 got_worktree_checkout_cb progress_cb, void *progress_arg)
880 const struct got_error *err = NULL;
881 int fd = -1;
882 size_t len, hdrlen;
883 int update = 0;
884 char *tmppath = NULL;
886 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
887 GOT_DEFAULT_FILE_MODE);
888 if (fd == -1) {
889 if (errno == ENOENT) {
890 char *parent = dirname(path);
891 if (parent == NULL)
892 return got_error_from_errno();
893 err = add_dir_on_disk(worktree, parent);
894 if (err)
895 return err;
896 fd = open(ondisk_path,
897 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
898 GOT_DEFAULT_FILE_MODE);
899 if (fd == -1)
900 return got_error_from_errno();
901 } else if (errno == EEXIST) {
902 if (!S_ISREG(st_mode)) {
903 /* TODO file is obstructed; do something */
904 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
905 goto done;
906 } else {
907 err = got_opentemp_named_fd(&tmppath, &fd,
908 ondisk_path);
909 if (err)
910 goto done;
911 update = 1;
913 } else
914 return got_error_from_errno();
917 if (restoring_missing_file)
918 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
919 else if (reverting_versioned_file)
920 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
921 else
922 (*progress_cb)(progress_arg,
923 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
925 hdrlen = got_object_blob_get_hdrlen(blob);
926 do {
927 const uint8_t *buf = got_object_blob_get_read_buf(blob);
928 err = got_object_blob_read_block(&len, blob);
929 if (err)
930 break;
931 if (len > 0) {
932 /* Skip blob object header first time around. */
933 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
934 if (outlen == -1) {
935 err = got_error_from_errno();
936 goto done;
937 } else if (outlen != len - hdrlen) {
938 err = got_error(GOT_ERR_IO);
939 goto done;
941 hdrlen = 0;
943 } while (len != 0);
945 if (fsync(fd) != 0) {
946 err = got_error_from_errno();
947 goto done;
950 if (update) {
951 if (rename(tmppath, ondisk_path) != 0) {
952 err = got_error_from_errno();
953 unlink(tmppath);
954 goto done;
958 if (te_mode & S_IXUSR) {
959 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
960 err = got_error_from_errno();
961 goto done;
963 } else {
964 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
965 err = got_error_from_errno();
966 goto done;
970 done:
971 if (fd != -1 && close(fd) != 0 && err == NULL)
972 err = got_error_from_errno();
973 free(tmppath);
974 return err;
977 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
978 static const struct got_error *
979 get_modified_file_content_status(unsigned char *status, FILE *f)
981 const struct got_error *err = NULL;
982 const char *markers[3] = {
983 GOT_DIFF_CONFLICT_MARKER_BEGIN,
984 GOT_DIFF_CONFLICT_MARKER_SEP,
985 GOT_DIFF_CONFLICT_MARKER_END
986 };
987 int i = 0;
988 char *line;
989 size_t len;
990 const char delim[3] = {'\0', '\0', '\0'};
992 while (*status == GOT_STATUS_MODIFY) {
993 line = fparseln(f, &len, NULL, delim, 0);
994 if (line == NULL) {
995 if (feof(f))
996 break;
997 err = got_ferror(f, GOT_ERR_IO);
998 break;
1001 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
1002 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
1003 *status = GOT_STATUS_CONFLICT;
1004 else
1005 i++;
1009 return err;
1012 static const struct got_error *
1013 get_file_status(unsigned char *status, struct stat *sb,
1014 struct got_fileindex_entry *ie, const char *abspath,
1015 struct got_repository *repo)
1017 const struct got_error *err = NULL;
1018 struct got_object_id id;
1019 size_t hdrlen;
1020 FILE *f = NULL;
1021 uint8_t fbuf[8192];
1022 struct got_blob_object *blob = NULL;
1023 size_t flen, blen;
1025 *status = GOT_STATUS_NO_CHANGE;
1027 if (lstat(abspath, sb) == -1) {
1028 if (errno == ENOENT) {
1029 if (ie) {
1030 if (got_fileindex_entry_has_file_on_disk(ie))
1031 *status = GOT_STATUS_MISSING;
1032 else
1033 *status = GOT_STATUS_DELETE;
1034 sb->st_mode =
1035 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1036 & (S_IRWXU | S_IRWXG | S_IRWXO));
1037 } else
1038 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1039 return NULL;
1041 return got_error_from_errno();
1044 if (!S_ISREG(sb->st_mode)) {
1045 *status = GOT_STATUS_OBSTRUCTED;
1046 return NULL;
1049 if (ie == NULL)
1050 return NULL;
1052 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1053 *status = GOT_STATUS_DELETE;
1054 return NULL;
1055 } else if (!got_fileindex_entry_has_blob(ie)) {
1056 *status = GOT_STATUS_ADD;
1057 return NULL;
1060 if (ie->ctime_sec == sb->st_ctime &&
1061 ie->ctime_nsec == sb->st_ctimensec &&
1062 ie->mtime_sec == sb->st_mtime &&
1063 ie->mtime_sec == sb->st_mtime &&
1064 ie->mtime_nsec == sb->st_mtimensec &&
1065 ie->size == (sb->st_size & 0xffffffff))
1066 return NULL;
1068 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1069 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1070 if (err)
1071 return err;
1073 f = fopen(abspath, "r");
1074 if (f == NULL) {
1075 err = got_error_from_errno();
1076 goto done;
1078 hdrlen = got_object_blob_get_hdrlen(blob);
1079 while (1) {
1080 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1081 err = got_object_blob_read_block(&blen, blob);
1082 if (err)
1083 goto done;
1084 /* Skip length of blob object header first time around. */
1085 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1086 if (flen == 0 && ferror(f)) {
1087 err = got_error_from_errno();
1088 goto done;
1090 if (blen == 0) {
1091 if (flen != 0)
1092 *status = GOT_STATUS_MODIFY;
1093 break;
1094 } else if (flen == 0) {
1095 if (blen != 0)
1096 *status = GOT_STATUS_MODIFY;
1097 break;
1098 } else if (blen - hdrlen == flen) {
1099 /* Skip blob object header first time around. */
1100 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1101 *status = GOT_STATUS_MODIFY;
1102 break;
1104 } else {
1105 *status = GOT_STATUS_MODIFY;
1106 break;
1108 hdrlen = 0;
1111 if (*status == GOT_STATUS_MODIFY) {
1112 rewind(f);
1113 err = get_modified_file_content_status(status, f);
1115 done:
1116 if (blob)
1117 got_object_blob_close(blob);
1118 if (f)
1119 fclose(f);
1120 return err;
1123 static const struct got_error *
1124 update_blob(struct got_worktree *worktree,
1125 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1126 struct got_tree_entry *te, const char *path,
1127 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1128 void *progress_arg)
1130 const struct got_error *err = NULL;
1131 struct got_blob_object *blob = NULL;
1132 char *ondisk_path;
1133 unsigned char status = GOT_STATUS_NO_CHANGE;
1134 struct stat sb;
1136 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1137 return got_error_from_errno();
1139 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1140 if (err)
1141 goto done;
1143 if (status == GOT_STATUS_OBSTRUCTED) {
1144 (*progress_cb)(progress_arg, status, path);
1145 goto done;
1148 if (ie && status != GOT_STATUS_MISSING) {
1149 if (got_fileindex_entry_has_commit(ie) &&
1150 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1151 SHA1_DIGEST_LENGTH) == 0) {
1152 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1153 path);
1154 goto done;
1156 if (got_fileindex_entry_has_blob(ie) &&
1157 memcmp(ie->blob_sha1, te->id->sha1,
1158 SHA1_DIGEST_LENGTH) == 0)
1159 goto done;
1162 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1163 if (err)
1164 goto done;
1166 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1167 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1168 te->mode, sb.st_mode, blob, repo, progress_cb,
1169 progress_arg);
1170 else if (status == GOT_STATUS_DELETE) {
1171 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1172 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1173 ondisk_path, path, blob, 0);
1174 if (err)
1175 goto done;
1176 } else {
1177 err = install_blob(worktree, ondisk_path, path, te->mode,
1178 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1179 repo, progress_cb, progress_arg);
1180 if (err)
1181 goto done;
1182 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1183 ondisk_path, path, blob, 1);
1184 if (err)
1185 goto done;
1187 got_object_blob_close(blob);
1188 done:
1189 free(ondisk_path);
1190 return err;
1193 static const struct got_error *
1194 remove_ondisk_file(const char *root_path, const char *path)
1196 const struct got_error *err = NULL;
1197 char *ondisk_path = NULL;
1199 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1200 return got_error_from_errno();
1202 if (unlink(ondisk_path) == -1) {
1203 if (errno != ENOENT)
1204 err = got_error_from_errno();
1205 } else {
1206 char *parent = dirname(ondisk_path);
1207 while (parent && strcmp(parent, root_path) != 0) {
1208 if (rmdir(parent) == -1) {
1209 if (errno != ENOTEMPTY)
1210 err = got_error_from_errno();
1211 break;
1213 parent = dirname(parent);
1216 free(ondisk_path);
1217 return err;
1220 static const struct got_error *
1221 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1222 struct got_fileindex_entry *ie, const char *parent_path,
1223 struct got_repository *repo,
1224 got_worktree_checkout_cb progress_cb, void *progress_arg)
1226 const struct got_error *err = NULL;
1227 unsigned char status;
1228 struct stat sb;
1229 char *ondisk_path;
1231 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1232 == -1)
1233 return got_error_from_errno();
1235 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1236 if (err)
1237 return err;
1239 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1240 status == GOT_STATUS_ADD) {
1241 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1243 * Preserve the working file and change the deleted blob's
1244 * entry into a schedule-add entry.
1246 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1247 0);
1248 if (err)
1249 return err;
1250 } else {
1251 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1252 if (status == GOT_STATUS_NO_CHANGE) {
1253 err = remove_ondisk_file(worktree->root_path, ie->path);
1254 if (err)
1255 return err;
1257 got_fileindex_entry_remove(fileindex, ie);
1260 return err;
1263 struct diff_cb_arg {
1264 struct got_fileindex *fileindex;
1265 struct got_worktree *worktree;
1266 struct got_repository *repo;
1267 got_worktree_checkout_cb progress_cb;
1268 void *progress_arg;
1269 got_worktree_cancel_cb cancel_cb;
1270 void *cancel_arg;
1273 static const struct got_error *
1274 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1275 struct got_tree_entry *te, const char *parent_path)
1277 struct diff_cb_arg *a = arg;
1279 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1280 return got_error(GOT_ERR_CANCELLED);
1282 return update_blob(a->worktree, a->fileindex, ie, te,
1283 ie->path, a->repo, a->progress_cb, a->progress_arg);
1286 static const struct got_error *
1287 diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1289 struct diff_cb_arg *a = arg;
1291 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1292 return got_error(GOT_ERR_CANCELLED);
1294 return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1295 a->repo, a->progress_cb, a->progress_arg);
1298 static const struct got_error *
1299 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1301 struct diff_cb_arg *a = arg;
1302 const struct got_error *err;
1303 char *path;
1305 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1306 return got_error(GOT_ERR_CANCELLED);
1308 if (asprintf(&path, "%s%s%s", parent_path,
1309 parent_path[0] ? "/" : "", te->name)
1310 == -1)
1311 return got_error_from_errno();
1313 if (S_ISDIR(te->mode))
1314 err = add_dir_on_disk(a->worktree, path);
1315 else
1316 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1317 a->repo, a->progress_cb, a->progress_arg);
1319 free(path);
1320 return err;
1323 const struct got_error *
1324 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1326 const struct got_error *err = NULL;
1327 char *uuidstr = NULL;
1328 uint32_t uuid_status;
1330 *refname = NULL;
1332 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1333 if (uuid_status != uuid_s_ok)
1334 return got_error_uuid(uuid_status);
1336 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1337 == -1) {
1338 err = got_error_from_errno();
1339 *refname = NULL;
1341 free(uuidstr);
1342 return err;
1346 * Prevent Git's garbage collector from deleting our base commit by
1347 * setting a reference to our base commit's ID.
1349 static const struct got_error *
1350 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1352 const struct got_error *err = NULL;
1353 struct got_reference *ref = NULL;
1354 char *refname;
1356 err = got_worktree_get_base_ref_name(&refname, worktree);
1357 if (err)
1358 return err;
1360 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1361 if (err)
1362 goto done;
1364 err = got_ref_write(ref, repo);
1365 done:
1366 free(refname);
1367 if (ref)
1368 got_ref_close(ref);
1369 return err;
1372 static const struct got_error *
1373 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1374 struct got_worktree *worktree)
1376 const struct got_error *err = NULL;
1377 FILE *index = NULL;
1379 *fileindex_path = NULL;
1380 *fileindex = got_fileindex_alloc();
1381 if (*fileindex == NULL)
1382 return got_error_from_errno();
1384 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1385 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1386 err = got_error_from_errno();
1387 *fileindex_path = NULL;
1388 goto done;
1391 index = fopen(*fileindex_path, "rb");
1392 if (index == NULL) {
1393 if (errno != ENOENT)
1394 err = got_error_from_errno();
1395 } else {
1396 err = got_fileindex_read(*fileindex, index);
1397 if (fclose(index) != 0 && err == NULL)
1398 err = got_error_from_errno();
1400 done:
1401 if (err) {
1402 free(*fileindex_path);
1403 *fileindex_path = NULL;
1404 free(*fileindex);
1405 *fileindex = NULL;
1407 return err;
1410 const struct got_error *
1411 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1412 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1413 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1415 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1416 struct got_commit_object *commit = NULL;
1417 struct got_object_id *tree_id = NULL;
1418 struct got_tree_object *tree = NULL;
1419 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1420 struct got_fileindex *fileindex = NULL;
1421 FILE *new_index = NULL;
1422 struct got_fileindex_diff_tree_cb diff_cb;
1423 struct diff_cb_arg arg;
1424 char *relpath = NULL, *entry_name = NULL;
1426 err = lock_worktree(worktree, LOCK_EX);
1427 if (err)
1428 return err;
1431 * Read the file index.
1432 * Checking out files is supposed to be an idempotent operation.
1433 * If the on-disk file index is incomplete we will try to complete it.
1435 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1436 if (err)
1437 goto done;
1439 err = got_opentemp_named(&new_fileindex_path, &new_index,
1440 fileindex_path);
1441 if (err)
1442 goto done;
1444 err = ref_base_commit(worktree, repo);
1445 if (err)
1446 goto done;
1448 err = got_object_open_as_commit(&commit, repo,
1449 worktree->base_commit_id);
1450 if (err)
1451 goto done;
1453 if (path[0]) {
1454 char *tree_path;
1455 int obj_type;
1456 relpath = strdup(path);
1457 if (relpath == NULL) {
1458 err = got_error_from_errno();
1459 goto done;
1461 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1462 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1463 path) == -1) {
1464 err = got_error_from_errno();
1465 goto done;
1467 err = got_object_id_by_path(&tree_id, repo,
1468 worktree->base_commit_id, tree_path);
1469 free(tree_path);
1470 if (err)
1471 goto done;
1472 err = got_object_get_type(&obj_type, repo, tree_id);
1473 if (err)
1474 goto done;
1475 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1476 /* Split provided path into parent dir + entry name. */
1477 if (strchr(path, '/') == NULL) {
1478 relpath = strdup("");
1479 if (relpath == NULL) {
1480 err = got_error_from_errno();
1481 goto done;
1483 tree_path = strdup(worktree->path_prefix);
1484 if (tree_path == NULL) {
1485 err = got_error_from_errno();
1486 goto done;
1488 } else {
1489 err = got_path_dirname(&relpath, path);
1490 if (err)
1491 goto done;
1492 if (asprintf(&tree_path, "%s%s%s",
1493 worktree->path_prefix,
1494 got_path_is_root_dir(
1495 worktree->path_prefix) ? "" : "/",
1496 relpath) == -1) {
1497 err = got_error_from_errno();
1498 goto done;
1501 err = got_object_id_by_path(&tree_id, repo,
1502 worktree->base_commit_id, tree_path);
1503 free(tree_path);
1504 if (err)
1505 goto done;
1506 entry_name = basename(path);
1507 if (entry_name == NULL) {
1508 err = got_error_from_errno();
1509 goto done;
1512 } else {
1513 relpath = strdup("");
1514 if (relpath == NULL) {
1515 err = got_error_from_errno();
1516 goto done;
1518 err = got_object_id_by_path(&tree_id, repo,
1519 worktree->base_commit_id, worktree->path_prefix);
1520 if (err)
1521 goto done;
1524 err = got_object_open_as_tree(&tree, repo, tree_id);
1525 if (err)
1526 goto done;
1528 if (entry_name &&
1529 got_object_tree_find_entry(tree, entry_name) == NULL) {
1530 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1531 goto done;
1534 diff_cb.diff_old_new = diff_old_new;
1535 diff_cb.diff_old = diff_old;
1536 diff_cb.diff_new = diff_new;
1537 arg.fileindex = fileindex;
1538 arg.worktree = worktree;
1539 arg.repo = repo;
1540 arg.progress_cb = progress_cb;
1541 arg.progress_arg = progress_arg;
1542 arg.cancel_cb = cancel_cb;
1543 arg.cancel_arg = cancel_arg;
1544 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1545 entry_name, repo, &diff_cb, &arg);
1547 /* Try to sync the fileindex back to disk in any case. */
1548 err = got_fileindex_write(fileindex, new_index);
1549 if (err)
1550 goto done;
1552 if (rename(new_fileindex_path, fileindex_path) != 0) {
1553 err = got_error_from_errno();
1554 unlink(new_fileindex_path);
1555 goto done;
1558 free(new_fileindex_path);
1559 new_fileindex_path = NULL;
1561 done:
1562 free(relpath);
1563 if (tree)
1564 got_object_tree_close(tree);
1565 if (commit)
1566 got_object_commit_close(commit);
1567 if (new_fileindex_path)
1568 unlink(new_fileindex_path);
1569 if (new_index)
1570 fclose(new_index);
1571 free(new_fileindex_path);
1572 free(fileindex_path);
1573 got_fileindex_free(fileindex);
1574 if (checkout_err)
1575 err = checkout_err;
1576 unlockerr = lock_worktree(worktree, LOCK_SH);
1577 if (unlockerr && err == NULL)
1578 err = unlockerr;
1579 return err;
1582 struct diff_dir_cb_arg {
1583 struct got_fileindex *fileindex;
1584 struct got_worktree *worktree;
1585 const char *status_path;
1586 size_t status_path_len;
1587 struct got_repository *repo;
1588 got_worktree_status_cb status_cb;
1589 void *status_arg;
1590 got_worktree_cancel_cb cancel_cb;
1591 void *cancel_arg;
1594 static const struct got_error *
1595 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1596 got_worktree_status_cb status_cb, void *status_arg,
1597 struct got_repository *repo)
1599 const struct got_error *err = NULL;
1600 unsigned char status = GOT_STATUS_NO_CHANGE;
1601 struct stat sb;
1602 struct got_object_id id;
1604 err = get_file_status(&status, &sb, ie, abspath, repo);
1605 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1606 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1607 err = (*status_cb)(status_arg, status, ie->path, &id);
1609 return err;
1612 static const struct got_error *
1613 status_old_new(void *arg, struct got_fileindex_entry *ie,
1614 struct dirent *de, const char *parent_path)
1616 const struct got_error *err = NULL;
1617 struct diff_dir_cb_arg *a = arg;
1618 char *abspath;
1620 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1621 return got_error(GOT_ERR_CANCELLED);
1623 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1624 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1625 return NULL;
1627 if (parent_path[0]) {
1628 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1629 parent_path, de->d_name) == -1)
1630 return got_error_from_errno();
1631 } else {
1632 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1633 de->d_name) == -1)
1634 return got_error_from_errno();
1637 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1638 a->repo);
1639 free(abspath);
1640 return err;
1643 static const struct got_error *
1644 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1646 struct diff_dir_cb_arg *a = arg;
1647 struct got_object_id id;
1648 unsigned char status;
1650 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1651 return got_error(GOT_ERR_CANCELLED);
1653 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1654 return NULL;
1656 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1657 if (got_fileindex_entry_has_file_on_disk(ie))
1658 status = GOT_STATUS_MISSING;
1659 else
1660 status = GOT_STATUS_DELETE;
1661 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1664 static const struct got_error *
1665 status_new(void *arg, struct dirent *de, const char *parent_path)
1667 const struct got_error *err = NULL;
1668 struct diff_dir_cb_arg *a = arg;
1669 char *path = NULL;
1671 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1672 return got_error(GOT_ERR_CANCELLED);
1674 if (de->d_type == DT_DIR)
1675 return NULL;
1677 /* XXX ignore symlinks for now */
1678 if (de->d_type == DT_LNK)
1679 return NULL;
1681 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1682 return NULL;
1684 if (parent_path[0]) {
1685 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1686 return got_error_from_errno();
1687 } else {
1688 path = de->d_name;
1691 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1692 NULL);
1693 if (parent_path[0])
1694 free(path);
1695 return err;
1698 const struct got_error *
1699 got_worktree_status(struct got_worktree *worktree, const char *path,
1700 struct got_repository *repo, got_worktree_status_cb status_cb,
1701 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1703 const struct got_error *err = NULL;
1704 DIR *workdir = NULL;
1705 char *fileindex_path = NULL;
1706 struct got_fileindex *fileindex = NULL;
1707 FILE *index = NULL;
1708 struct got_fileindex_diff_dir_cb fdiff_cb;
1709 struct diff_dir_cb_arg arg;
1710 char *ondisk_path = NULL;
1712 fileindex = got_fileindex_alloc();
1713 if (fileindex == NULL) {
1714 err = got_error_from_errno();
1715 goto done;
1718 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1719 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1720 err = got_error_from_errno();
1721 fileindex_path = NULL;
1722 goto done;
1725 index = fopen(fileindex_path, "rb");
1726 if (index == NULL) {
1727 if (errno != ENOENT) {
1728 err = got_error_from_errno();
1729 goto done;
1731 } else {
1732 err = got_fileindex_read(fileindex, index);
1733 fclose(index);
1734 if (err)
1735 goto done;
1738 if (asprintf(&ondisk_path, "%s%s%s",
1739 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1740 err = got_error_from_errno();
1741 goto done;
1743 workdir = opendir(ondisk_path);
1744 if (workdir == NULL) {
1745 if (errno == ENOTDIR || errno == ENOENT) {
1746 struct got_fileindex_entry *ie;
1747 ie = got_fileindex_entry_get(fileindex, path);
1748 if (ie == NULL) {
1749 err = got_error(GOT_ERR_BAD_PATH);
1750 goto done;
1752 err = report_file_status(ie, ondisk_path,
1753 status_cb, status_arg, repo);
1754 goto done;
1755 } else {
1756 err = got_error_from_errno();
1757 goto done;
1760 fdiff_cb.diff_old_new = status_old_new;
1761 fdiff_cb.diff_old = status_old;
1762 fdiff_cb.diff_new = status_new;
1763 arg.fileindex = fileindex;
1764 arg.worktree = worktree;
1765 arg.status_path = path;
1766 arg.status_path_len = strlen(path);
1767 arg.repo = repo;
1768 arg.status_cb = status_cb;
1769 arg.status_arg = status_arg;
1770 arg.cancel_cb = cancel_cb;
1771 arg.cancel_arg = cancel_arg;
1772 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1773 path, repo, &fdiff_cb, &arg);
1774 done:
1775 if (workdir)
1776 closedir(workdir);
1777 free(ondisk_path);
1778 free(fileindex_path);
1779 got_fileindex_free(fileindex);
1780 return err;
1783 const struct got_error *
1784 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1785 const char *arg)
1787 const struct got_error *err = NULL;
1788 char *resolved, *path = NULL;
1789 size_t len;
1791 *wt_path = NULL;
1793 resolved = realpath(arg, NULL);
1794 if (resolved == NULL)
1795 return got_error_from_errno();
1797 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1798 strlen(got_worktree_get_root_path(worktree)))) {
1799 err = got_error(GOT_ERR_BAD_PATH);
1800 goto done;
1803 path = strdup(resolved +
1804 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1805 if (path == NULL) {
1806 err = got_error_from_errno();
1807 goto done;
1810 /* XXX status walk can't deal with trailing slash! */
1811 len = strlen(path);
1812 while (path[len - 1] == '/') {
1813 path[len - 1] = '\0';
1814 len--;
1816 done:
1817 free(resolved);
1818 if (err == NULL)
1819 *wt_path = path;
1820 else
1821 free(path);
1822 return err;
1825 const struct got_error *
1826 got_worktree_schedule_add(struct got_worktree *worktree,
1827 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1828 struct got_repository *repo)
1830 struct got_fileindex *fileindex = NULL;
1831 struct got_fileindex_entry *ie = NULL;
1832 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1833 FILE *index = NULL, *new_index = NULL;
1834 const struct got_error *err = NULL, *unlockerr = NULL;
1835 int ie_added = 0;
1837 err = lock_worktree(worktree, LOCK_EX);
1838 if (err)
1839 return err;
1841 err = got_path_skip_common_ancestor(&relpath,
1842 got_worktree_get_root_path(worktree), ondisk_path);
1843 if (err)
1844 goto done;
1846 fileindex = got_fileindex_alloc();
1847 if (fileindex == NULL) {
1848 err = got_error_from_errno();
1849 goto done;
1852 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1853 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1854 err = got_error_from_errno();
1855 fileindex_path = NULL;
1856 goto done;
1859 index = fopen(fileindex_path, "rb");
1860 if (index == NULL) {
1861 err = got_error_from_errno();
1862 goto done;
1865 err = got_fileindex_read(fileindex, index);
1866 if (err)
1867 goto done;
1869 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1870 err = got_error_set_errno(EEXIST);
1871 goto done;
1874 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1875 if (err)
1876 goto done;
1878 err = got_fileindex_entry_add(fileindex, ie);
1879 if (err)
1880 goto done;
1881 ie_added = 1; /* now owned by fileindex; don't free separately */
1883 err = got_opentemp_named(&new_fileindex_path, &new_index,
1884 fileindex_path);
1885 if (err)
1886 goto done;
1888 err = got_fileindex_write(fileindex, new_index);
1889 if (err)
1890 goto done;
1892 if (rename(new_fileindex_path, fileindex_path) != 0) {
1893 err = got_error_from_errno();
1894 goto done;
1897 free(new_fileindex_path);
1898 new_fileindex_path = NULL;
1900 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1901 done:
1902 if (index) {
1903 if (fclose(index) != 0 && err == NULL)
1904 err = got_error_from_errno();
1906 if (new_fileindex_path) {
1907 if (unlink(new_fileindex_path) != 0 && err == NULL)
1908 err = got_error_from_errno();
1909 free(new_fileindex_path);
1911 if (ie && !ie_added)
1912 got_fileindex_entry_free(ie);
1913 if (fileindex)
1914 got_fileindex_free(fileindex);
1915 unlockerr = lock_worktree(worktree, LOCK_SH);
1916 if (unlockerr && err == NULL)
1917 err = unlockerr;
1918 free(relpath);
1919 return err;
1922 const struct got_error *
1923 got_worktree_schedule_delete(struct got_worktree *worktree,
1924 const char *ondisk_path, int delete_local_mods,
1925 got_worktree_status_cb status_cb, void *status_arg,
1926 struct got_repository *repo)
1928 struct got_fileindex *fileindex = NULL;
1929 struct got_fileindex_entry *ie = NULL;
1930 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1931 FILE *index = NULL, *new_index = NULL;
1932 const struct got_error *err = NULL, *unlockerr = NULL;
1933 unsigned char status;
1934 struct stat sb;
1936 err = lock_worktree(worktree, LOCK_EX);
1937 if (err)
1938 return err;
1940 err = got_path_skip_common_ancestor(&relpath,
1941 got_worktree_get_root_path(worktree), ondisk_path);
1942 if (err)
1943 goto done;
1945 fileindex = got_fileindex_alloc();
1946 if (fileindex == NULL) {
1947 err = got_error_from_errno();
1948 goto done;
1951 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1952 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1953 err = got_error_from_errno();
1954 fileindex_path = NULL;
1955 goto done;
1958 index = fopen(fileindex_path, "rb");
1959 if (index == NULL) {
1960 err = got_error_from_errno();
1961 goto done;
1964 err = got_fileindex_read(fileindex, index);
1965 if (err)
1966 goto done;
1968 ie = got_fileindex_entry_get(fileindex, relpath);
1969 if (ie == NULL) {
1970 err = got_error(GOT_ERR_BAD_PATH);
1971 goto done;
1974 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1975 if (err)
1976 goto done;
1978 if (status != GOT_STATUS_NO_CHANGE) {
1979 if (status == GOT_STATUS_DELETE) {
1980 err = got_error_set_errno(ENOENT);
1981 goto done;
1983 if (status != GOT_STATUS_MODIFY) {
1984 err = got_error(GOT_ERR_FILE_STATUS);
1985 goto done;
1987 if (!delete_local_mods) {
1988 err = got_error(GOT_ERR_FILE_MODIFIED);
1989 goto done;
1993 if (unlink(ondisk_path) != 0) {
1994 err = got_error_from_errno();
1995 goto done;
1998 got_fileindex_entry_mark_deleted_from_disk(ie);
2000 err = got_opentemp_named(&new_fileindex_path, &new_index,
2001 fileindex_path);
2002 if (err)
2003 goto done;
2005 err = got_fileindex_write(fileindex, new_index);
2006 if (err)
2007 goto done;
2009 if (rename(new_fileindex_path, fileindex_path) != 0) {
2010 err = got_error_from_errno();
2011 goto done;
2014 free(new_fileindex_path);
2015 new_fileindex_path = NULL;
2017 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2018 done:
2019 free(relpath);
2020 if (index) {
2021 if (fclose(index) != 0 && err == NULL)
2022 err = got_error_from_errno();
2024 if (new_fileindex_path) {
2025 if (unlink(new_fileindex_path) != 0 && err == NULL)
2026 err = got_error_from_errno();
2027 free(new_fileindex_path);
2029 if (fileindex)
2030 got_fileindex_free(fileindex);
2031 unlockerr = lock_worktree(worktree, LOCK_SH);
2032 if (unlockerr && err == NULL)
2033 err = unlockerr;
2034 return err;
2037 const struct got_error *
2038 got_worktree_revert(struct got_worktree *worktree,
2039 const char *ondisk_path,
2040 got_worktree_checkout_cb progress_cb, void *progress_arg,
2041 struct got_repository *repo)
2043 struct got_fileindex *fileindex = NULL;
2044 struct got_fileindex_entry *ie = NULL;
2045 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2046 char *tree_path = NULL, *parent_path, *te_name;
2047 FILE *index = NULL, *new_index = NULL;
2048 const struct got_error *err = NULL, *unlockerr = NULL;
2049 struct got_tree_object *tree = NULL;
2050 struct got_object_id id, *tree_id = NULL;
2051 const struct got_tree_entry *te;
2052 struct got_blob_object *blob = NULL;
2053 unsigned char status;
2054 struct stat sb;
2056 err = lock_worktree(worktree, LOCK_EX);
2057 if (err)
2058 return err;
2060 err = got_path_skip_common_ancestor(&relpath,
2061 got_worktree_get_root_path(worktree), ondisk_path);
2062 if (err)
2063 goto done;
2065 fileindex = got_fileindex_alloc();
2066 if (fileindex == NULL) {
2067 err = got_error_from_errno();
2068 goto done;
2071 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2072 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2073 err = got_error_from_errno();
2074 fileindex_path = NULL;
2075 goto done;
2078 index = fopen(fileindex_path, "rb");
2079 if (index == NULL) {
2080 err = got_error_from_errno();
2081 goto done;
2084 err = got_fileindex_read(fileindex, index);
2085 if (err)
2086 goto done;
2088 ie = got_fileindex_entry_get(fileindex, relpath);
2089 if (ie == NULL) {
2090 err = got_error(GOT_ERR_BAD_PATH);
2091 goto done;
2094 /* Construct in-repository path of tree which contains this blob. */
2095 err = got_path_dirname(&parent_path, ie->path);
2096 if (err) {
2097 if (err->code != GOT_ERR_BAD_PATH)
2098 goto done;
2099 parent_path = "/";
2101 if (got_path_is_root_dir(worktree->path_prefix)) {
2102 tree_path = strdup(parent_path);
2103 if (tree_path == NULL) {
2104 err = got_error_from_errno();
2105 goto done;
2107 } else {
2108 if (got_path_is_root_dir(parent_path)) {
2109 tree_path = strdup(worktree->path_prefix);
2110 if (tree_path == NULL) {
2111 err = got_error_from_errno();
2112 goto done;
2114 } else {
2115 if (asprintf(&tree_path, "%s/%s",
2116 worktree->path_prefix, parent_path) == -1) {
2117 err = got_error_from_errno();
2118 goto done;
2123 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2124 tree_path);
2125 if (err)
2126 goto done;
2128 err = got_object_open_as_tree(&tree, repo, tree_id);
2129 if (err)
2130 goto done;
2132 te_name = basename(ie->path);
2133 if (te_name == NULL) {
2134 err = got_error_from_errno();
2135 goto done;
2138 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2139 if (err)
2140 goto done;
2142 te = got_object_tree_find_entry(tree, te_name);
2143 if (te == NULL && status != GOT_STATUS_ADD) {
2144 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2145 goto done;
2148 switch (status) {
2149 case GOT_STATUS_ADD:
2150 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2151 got_fileindex_entry_remove(fileindex, ie);
2152 break;
2153 case GOT_STATUS_DELETE:
2154 case GOT_STATUS_MODIFY:
2155 case GOT_STATUS_CONFLICT:
2156 case GOT_STATUS_MISSING:
2157 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2158 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2159 if (err)
2160 goto done;
2161 err = install_blob(worktree, ondisk_path, ie->path,
2162 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2163 progress_arg);
2164 if (err)
2165 goto done;
2166 if (status == GOT_STATUS_DELETE) {
2167 err = update_blob_fileindex_entry(worktree,
2168 fileindex, ie, ondisk_path, ie->path, blob, 1);
2169 if (err)
2170 goto done;
2172 break;
2173 default:
2174 goto done;
2177 err = got_opentemp_named(&new_fileindex_path, &new_index,
2178 fileindex_path);
2179 if (err)
2180 goto done;
2182 err = got_fileindex_write(fileindex, new_index);
2183 if (err)
2184 goto done;
2186 if (rename(new_fileindex_path, fileindex_path) != 0) {
2187 err = got_error_from_errno();
2188 goto done;
2191 free(new_fileindex_path);
2192 new_fileindex_path = NULL;
2193 done:
2194 free(relpath);
2195 free(tree_path);
2196 if (blob)
2197 got_object_blob_close(blob);
2198 if (tree)
2199 got_object_tree_close(tree);
2200 free(tree_id);
2201 if (index) {
2202 if (fclose(index) != 0 && err == NULL)
2203 err = got_error_from_errno();
2205 if (new_fileindex_path) {
2206 if (unlink(new_fileindex_path) != 0 && err == NULL)
2207 err = got_error_from_errno();
2208 free(new_fileindex_path);
2210 if (fileindex)
2211 got_fileindex_free(fileindex);
2212 unlockerr = lock_worktree(worktree, LOCK_SH);
2213 if (unlockerr && err == NULL)
2214 err = unlockerr;
2215 return err;
2218 struct commitable {
2219 char *path;
2220 char *ondisk_path;
2221 unsigned char status;
2222 struct got_object_id *id;
2223 struct got_object_id *base_id;
2224 mode_t mode;
2227 static void
2228 free_commitable(struct commitable *ct)
2230 free(ct->path);
2231 free(ct->ondisk_path);
2232 free(ct->id);
2233 free(ct->base_id);
2234 free(ct);
2237 struct collect_commitables_arg {
2238 struct got_pathlist_head *commitable_paths;
2239 struct got_repository *repo;
2240 struct got_worktree *worktree;
2243 static const struct got_error *
2244 collect_commitables(void *arg, unsigned char status, const char *relpath,
2245 struct got_object_id *id)
2247 struct collect_commitables_arg *a = arg;
2248 const struct got_error *err = NULL;
2249 struct commitable *ct = NULL;
2250 struct got_pathlist_entry *new = NULL;
2251 char *parent_path = NULL, *path = NULL;
2252 struct stat sb;
2254 if (status == GOT_STATUS_CONFLICT)
2255 return got_error(GOT_ERR_COMMIT_CONFLICT);
2257 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2258 status != GOT_STATUS_DELETE)
2259 return NULL;
2261 if (asprintf(&path, "/%s", relpath) == -1) {
2262 err = got_error_from_errno();
2263 goto done;
2265 if (strcmp(path, "/") == 0) {
2266 parent_path = strdup("");
2267 if (parent_path == NULL)
2268 return got_error_from_errno();
2269 } else {
2270 err = got_path_dirname(&parent_path, path);
2271 if (err)
2272 return err;
2275 ct = calloc(1, sizeof(*ct));
2276 if (ct == NULL) {
2277 err = got_error_from_errno();
2278 goto done;
2281 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2282 relpath) == -1) {
2283 err = got_error_from_errno();
2284 goto done;
2286 if (status == GOT_STATUS_DELETE) {
2287 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2288 } else {
2289 if (lstat(ct->ondisk_path, &sb) != 0) {
2290 err = got_error_from_errno();
2291 goto done;
2293 ct->mode = sb.st_mode;
2296 ct->status = status;
2297 ct->id = NULL; /* will be filled in when blob gets created */
2298 if (ct->status != GOT_STATUS_ADD) {
2299 ct->base_id = got_object_id_dup(id);
2300 if (ct->base_id == NULL) {
2301 err = got_error_from_errno();
2302 goto done;
2305 ct->path = strdup(path);
2306 if (ct->path == NULL) {
2307 err = got_error_from_errno();
2308 goto done;
2310 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2311 done:
2312 if (ct && (err || new == NULL))
2313 free_commitable(ct);
2314 free(parent_path);
2315 free(path);
2316 return err;
2319 static const struct got_error *write_tree(struct got_object_id **,
2320 struct got_tree_object *, const char *, struct got_pathlist_head *,
2321 got_worktree_status_cb status_cb, void *status_arg,
2322 struct got_repository *);
2324 static const struct got_error *
2325 write_subtree(struct got_object_id **new_subtree_id,
2326 struct got_tree_entry *te, const char *parent_path,
2327 struct got_pathlist_head *commitable_paths,
2328 got_worktree_status_cb status_cb, void *status_arg,
2329 struct got_repository *repo)
2331 const struct got_error *err = NULL;
2332 struct got_tree_object *subtree;
2333 char *subpath;
2335 if (asprintf(&subpath, "%s%s%s", parent_path,
2336 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2337 return got_error_from_errno();
2339 err = got_object_open_as_tree(&subtree, repo, te->id);
2340 if (err)
2341 return err;
2343 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2344 status_cb, status_arg, repo);
2345 got_object_tree_close(subtree);
2346 free(subpath);
2347 return err;
2350 static const struct got_error *
2351 match_ct_parent_path(int *match, struct commitable *ct, const char *path)
2353 const struct got_error *err = NULL;
2354 char *ct_parent_path = NULL;
2356 *match = 0;
2358 if (strchr(ct->path, '/') == NULL) {
2359 ct_parent_path = strdup("/");
2360 if (ct_parent_path == NULL)
2361 return got_error_from_errno();
2362 } else {
2363 err = got_path_dirname(&ct_parent_path, ct->path);
2364 if (err)
2365 return err;
2368 *match = (strcmp(path, ct_parent_path) == 0);
2369 free(ct_parent_path);
2370 return err;
2373 static mode_t
2374 get_ct_file_mode(struct commitable *ct)
2376 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2379 static const struct got_error *
2380 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2381 struct got_tree_entry *te, struct commitable *ct)
2383 const struct got_error *err = NULL;
2385 *new_te = NULL;
2387 err = got_object_tree_entry_dup(new_te, te);
2388 if (err)
2389 goto done;
2391 (*new_te)->mode = get_ct_file_mode(ct);
2393 free((*new_te)->id);
2394 (*new_te)->id = got_object_id_dup(ct->id);
2395 if ((*new_te)->id == NULL) {
2396 err = got_error_from_errno();
2397 goto done;
2399 done:
2400 if (err && *new_te) {
2401 got_object_tree_entry_close(*new_te);
2402 *new_te = NULL;
2404 return err;
2407 static const struct got_error *
2408 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2409 struct commitable *ct)
2411 const struct got_error *err = NULL;
2412 char *ct_name;
2414 *new_te = NULL;
2416 *new_te = calloc(1, sizeof(*new_te));
2417 if (*new_te == NULL)
2418 return got_error_from_errno();
2420 ct_name = basename(ct->path);
2421 if (ct_name == NULL) {
2422 err = got_error_from_errno();
2423 goto done;
2425 (*new_te)->name = strdup(ct_name);
2426 if ((*new_te)->name == NULL) {
2427 err = got_error_from_errno();
2428 goto done;
2431 (*new_te)->mode = get_ct_file_mode(ct);
2433 (*new_te)->id = got_object_id_dup(ct->id);
2434 if ((*new_te)->id == NULL) {
2435 err = got_error_from_errno();
2436 goto done;
2438 done:
2439 if (err && *new_te) {
2440 got_object_tree_entry_close(*new_te);
2441 *new_te = NULL;
2443 return err;
2446 static const struct got_error *
2447 insert_tree_entry(struct got_tree_entry *new_te,
2448 struct got_pathlist_head *paths)
2450 const struct got_error *err = NULL;
2451 struct got_pathlist_entry *new_pe;
2453 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2454 if (err)
2455 return err;
2456 if (new_pe == NULL)
2457 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2458 return NULL;
2461 static const struct got_error *
2462 report_ct_status(struct commitable *ct,
2463 got_worktree_status_cb status_cb, void *status_arg)
2465 const char *ct_path = ct->path;
2466 while (ct_path[0] == '/')
2467 ct_path++;
2468 return (*status_cb)(status_arg, ct->status, ct_path, ct->id);
2471 static const struct got_error *
2472 match_deleted_or_modified_ct(struct commitable **ctp,
2473 struct got_tree_entry *te, const char *base_tree_path,
2474 struct got_pathlist_head *commitable_paths)
2476 const struct got_error *err = NULL;
2477 struct got_pathlist_entry *pe;
2479 *ctp = NULL;
2481 TAILQ_FOREACH(pe, commitable_paths, entry) {
2482 struct commitable *ct = pe->data;
2483 char *ct_name = NULL;
2484 int path_matches;
2486 if (ct->status != GOT_STATUS_MODIFY &&
2487 ct->status != GOT_STATUS_DELETE)
2488 continue;
2490 if (got_object_id_cmp(ct->base_id, te->id) != 0)
2491 continue;
2493 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2494 if (err)
2495 return err;
2496 if (!path_matches)
2497 continue;
2499 ct_name = basename(pe->path);
2500 if (ct_name == NULL)
2501 return got_error_from_errno();
2503 if (strcmp(te->name, ct_name) != 0)
2504 continue;
2506 *ctp = ct;
2507 break;
2510 return err;
2513 static const struct got_error *
2514 write_tree(struct got_object_id **new_tree_id,
2515 struct got_tree_object *base_tree, const char *path_base_tree,
2516 struct got_pathlist_head *commitable_paths,
2517 got_worktree_status_cb status_cb, void *status_arg,
2518 struct got_repository *repo)
2520 const struct got_error *err = NULL;
2521 const struct got_tree_entries *base_entries = NULL;
2522 struct got_pathlist_head paths;
2523 struct got_tree_entries new_tree_entries;
2524 struct got_tree_entry *te, *new_te = NULL;
2525 struct got_pathlist_entry *pe;
2527 TAILQ_INIT(&paths);
2528 new_tree_entries.nentries = 0;
2529 SIMPLEQ_INIT(&new_tree_entries.head);
2531 /* Insert, and recurse into, newly added entries first. */
2532 TAILQ_FOREACH(pe, commitable_paths, entry) {
2533 struct commitable *ct = pe->data;
2534 char *child_path = NULL, *slash;
2536 if (ct->status != GOT_STATUS_ADD)
2537 continue;
2539 if (!got_path_is_child(pe->path, path_base_tree,
2540 strlen(path_base_tree)))
2541 continue;
2543 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2544 pe->path);
2545 if (err)
2546 goto done;
2548 slash = strchr(child_path, '/');
2549 if (slash == NULL) {
2550 err = alloc_added_blob_tree_entry(&new_te, ct);
2551 if (err)
2552 goto done;
2553 err = report_ct_status(ct, status_cb, status_arg);
2554 if (err)
2555 goto done;
2556 } else {
2557 char *subtree_path;
2558 struct got_pathlist_entry *pe2;
2559 int visited = 0;
2561 *slash = '\0'; /* trim trailing path components */
2563 new_te = calloc(1, sizeof(*new_te));
2564 new_te->mode = S_IFDIR;
2565 new_te->name = strdup(child_path);
2566 if (new_te->name == NULL) {
2567 got_object_tree_entry_close(new_te);
2568 err = got_error_from_errno();
2569 goto done;
2571 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2572 got_path_is_root_dir(path_base_tree) ? "" : "/",
2573 child_path) == -1) {
2574 err = got_error_from_errno();
2575 goto done;
2577 TAILQ_FOREACH(pe2, &paths, entry) {
2578 if (got_path_cmp(subtree_path, pe2->path) != 0)
2579 continue;
2580 visited = 1;
2581 break;
2583 if (visited)
2584 continue;
2586 err = write_tree(&new_te->id, NULL, subtree_path,
2587 commitable_paths, status_cb, status_arg, repo);
2588 free(subtree_path);
2589 if (err)
2590 goto done;
2592 err = insert_tree_entry(new_te, &paths);
2593 if (err)
2594 goto done;
2597 if (base_tree) {
2598 /* Handle modified and deleted entries. */
2599 base_entries = got_object_tree_get_entries(base_tree);
2600 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2601 struct commitable *ct = NULL;
2603 if (S_ISDIR(te->mode)) {
2604 err = got_object_tree_entry_dup(&new_te, te);
2605 if (err)
2606 goto done;
2607 free(new_te->id);
2608 err = write_subtree(&new_te->id, te,
2609 path_base_tree, commitable_paths,
2610 status_cb, status_arg, repo);
2611 if (err)
2612 goto done;
2613 err = insert_tree_entry(new_te, &paths);
2614 if (err)
2615 goto done;
2616 continue;
2619 err = match_deleted_or_modified_ct(&ct, te,
2620 path_base_tree, commitable_paths);
2621 if (ct) {
2622 /* NB: Deleted entries get dropped here. */
2623 if (ct->status == GOT_STATUS_MODIFY) {
2624 err = alloc_modified_blob_tree_entry(
2625 &new_te, te, ct);
2626 if (err)
2627 goto done;
2628 err = insert_tree_entry(new_te, &paths);
2629 if (err)
2630 goto done;
2632 err = report_ct_status(ct, status_cb, status_arg);
2633 if (err)
2634 goto done;
2635 } else {
2636 /* Entry is unchanged; just copy it. */
2637 err = got_object_tree_entry_dup(&new_te, te);
2638 if (err)
2639 goto done;
2640 err = insert_tree_entry(new_te, &paths);
2641 if (err)
2642 goto done;
2647 /* Write new list of entries; deleted entries have been dropped. */
2648 TAILQ_FOREACH(pe, &paths, entry) {
2649 struct got_tree_entry *te = pe->data;
2650 new_tree_entries.nentries++;
2651 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2653 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2654 done:
2655 got_object_tree_entries_close(&new_tree_entries);
2656 got_pathlist_free(&paths);
2657 if (base_tree)
2658 got_object_tree_close(base_tree);
2659 return err;
2662 static const struct got_error *
2663 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2664 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2666 const struct got_error *err = NULL;
2667 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2668 struct got_fileindex *fileindex = NULL;
2669 FILE *new_index = NULL;
2670 struct got_pathlist_entry *pe;
2672 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2673 if (err)
2674 return err;
2676 err = got_opentemp_named(&new_fileindex_path, &new_index,
2677 fileindex_path);
2678 if (err)
2679 goto done;
2681 TAILQ_FOREACH(pe, commitable_paths, entry) {
2682 struct got_fileindex_entry *ie;
2683 struct commitable *ct = pe->data;
2685 ie = got_fileindex_entry_get(fileindex, pe->path);
2686 if (ie) {
2687 if (ct->status == GOT_STATUS_DELETE) {
2688 got_fileindex_entry_remove(fileindex, ie);
2689 got_fileindex_entry_free(ie);
2690 } else
2691 err = got_fileindex_entry_update(ie,
2692 ct->ondisk_path, ct->id->sha1,
2693 new_base_commit_id->sha1, 1);
2694 } else {
2695 err = got_fileindex_entry_alloc(&ie,
2696 ct->ondisk_path, pe->path, ct->id->sha1,
2697 new_base_commit_id->sha1);
2698 if (err)
2699 goto done;
2700 err = got_fileindex_entry_add(fileindex, ie);
2701 if (err)
2702 goto done;
2706 err = got_fileindex_write(fileindex, new_index);
2707 if (err)
2708 goto done;
2710 if (rename(new_fileindex_path, fileindex_path) != 0) {
2711 err = got_error_from_errno();
2712 unlink(new_fileindex_path);
2713 goto done;
2716 free(new_fileindex_path);
2717 new_fileindex_path = NULL;
2719 done:
2720 if (new_fileindex_path)
2721 unlink(new_fileindex_path);
2722 if (new_index)
2723 fclose(new_index);
2724 free(new_fileindex_path);
2725 free(fileindex_path);
2726 got_fileindex_free(fileindex);
2727 return err;
2730 const struct got_error *
2731 got_worktree_commit(struct got_object_id **new_commit_id,
2732 struct got_worktree *worktree, const char *ondisk_path,
2733 const char *author, const char *committer, const char *logmsg,
2734 got_worktree_status_cb status_cb, void *status_arg,
2735 struct got_repository *repo)
2737 const struct got_error *err = NULL, *unlockerr = NULL;
2738 struct collect_commitables_arg cc_arg;
2739 struct got_pathlist_head commitable_paths;
2740 struct got_pathlist_entry *pe;
2741 char *relpath = NULL;
2742 struct got_commit_object *base_commit = NULL;
2743 struct got_tree_object *base_tree = NULL;
2744 struct got_object_id *new_tree_id = NULL;
2745 struct got_object_id_queue parent_ids;
2746 struct got_object_qid *pid = NULL;
2748 *new_commit_id = NULL;
2750 TAILQ_INIT(&commitable_paths);
2751 SIMPLEQ_INIT(&parent_ids);
2753 if (ondisk_path) {
2754 err = got_path_skip_common_ancestor(&relpath,
2755 worktree->root_path, ondisk_path);
2756 if (err)
2757 return err;
2760 err = lock_worktree(worktree, LOCK_EX);
2761 if (err)
2762 goto done;
2764 cc_arg.commitable_paths = &commitable_paths;
2765 cc_arg.worktree = worktree;
2766 cc_arg.repo = repo;
2767 err = got_worktree_status(worktree, relpath ? relpath : "",
2768 repo, collect_commitables, &cc_arg, NULL, NULL);
2769 if (err)
2770 goto done;
2772 /* TODO: out-of-dateness check */
2774 /* TODO: collect commit message if not specified */
2776 /* Create blobs from added and modified files and record their IDs. */
2777 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2778 struct commitable *ct = pe->data;
2779 char *ondisk_path;
2781 if (ct->status != GOT_STATUS_ADD &&
2782 ct->status != GOT_STATUS_MODIFY)
2783 continue;
2785 if (asprintf(&ondisk_path, "%s/%s",
2786 worktree->root_path, pe->path) == -1) {
2787 err = got_error_from_errno();
2788 goto done;
2790 err = got_object_blob_create(&ct->id, ondisk_path, repo);
2791 free(ondisk_path);
2792 if (err)
2793 goto done;
2796 err = got_object_open_as_commit(&base_commit, repo,
2797 worktree->base_commit_id);
2798 if (err)
2799 goto done;
2800 err = got_object_open_as_tree(&base_tree, repo, base_commit->tree_id);
2801 if (err)
2802 goto done;
2804 /* Recursively write new tree objects. */
2805 err = write_tree(&new_tree_id, base_tree, "/", &commitable_paths,
2806 status_cb, status_arg, repo);
2807 if (err)
2808 goto done;
2810 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2811 if (err)
2812 goto done;
2813 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2814 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2815 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2816 got_object_qid_free(pid);
2817 if (err)
2818 goto done;
2820 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
2821 if (err)
2822 goto done;
2824 err = got_ref_change_ref(worktree->head_ref, *new_commit_id);
2825 if (err)
2826 goto done;
2827 err = got_ref_write(worktree->head_ref, repo);
2828 if (err)
2829 goto done;
2831 err = ref_base_commit(worktree, repo);
2832 if (err)
2833 goto done;
2835 err = update_fileindex_after_commit(&commitable_paths,
2836 *new_commit_id, worktree);
2837 if (err)
2838 goto done;
2839 done:
2840 unlockerr = lock_worktree(worktree, LOCK_SH);
2841 if (unlockerr && err == NULL)
2842 err = unlockerr;
2843 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2844 struct commitable *ct = pe->data;
2845 free_commitable(ct);
2847 got_pathlist_free(&commitable_paths);
2848 if (base_tree)
2849 got_object_tree_close(base_tree);
2850 if (base_commit)
2851 got_object_commit_close(base_commit);
2852 free(relpath);
2853 return err;