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;
1373 const struct got_error *
1374 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1375 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1376 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1378 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1379 struct got_commit_object *commit = NULL;
1380 struct got_object_id *tree_id = NULL;
1381 struct got_tree_object *tree = NULL;
1382 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1383 struct got_fileindex *fileindex = NULL;
1384 FILE *index = NULL, *new_index = NULL;
1385 struct got_fileindex_diff_tree_cb diff_cb;
1386 struct diff_cb_arg arg;
1387 char *relpath = NULL, *entry_name = NULL;
1389 err = lock_worktree(worktree, LOCK_EX);
1390 if (err)
1391 return err;
1393 fileindex = got_fileindex_alloc();
1394 if (fileindex == NULL) {
1395 err = got_error_from_errno();
1396 goto done;
1399 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1400 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1401 err = got_error_from_errno();
1402 fileindex_path = NULL;
1403 goto done;
1407 * Read the file index.
1408 * Checking out files is supposed to be an idempotent operation.
1409 * If the on-disk file index is incomplete we will try to complete it.
1411 index = fopen(fileindex_path, "rb");
1412 if (index == NULL) {
1413 if (errno != ENOENT) {
1414 err = got_error_from_errno();
1415 goto done;
1417 } else {
1418 err = got_fileindex_read(fileindex, index);
1419 fclose(index);
1420 if (err)
1421 goto done;
1424 err = got_opentemp_named(&new_fileindex_path, &new_index,
1425 fileindex_path);
1426 if (err)
1427 goto done;
1429 err = ref_base_commit(worktree, repo);
1430 if (err)
1431 goto done;
1433 err = got_object_open_as_commit(&commit, repo,
1434 worktree->base_commit_id);
1435 if (err)
1436 goto done;
1438 if (path[0]) {
1439 char *tree_path;
1440 int obj_type;
1441 relpath = strdup(path);
1442 if (relpath == NULL) {
1443 err = got_error_from_errno();
1444 goto done;
1446 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1447 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1448 path) == -1) {
1449 err = got_error_from_errno();
1450 goto done;
1452 err = got_object_id_by_path(&tree_id, repo,
1453 worktree->base_commit_id, tree_path);
1454 free(tree_path);
1455 if (err)
1456 goto done;
1457 err = got_object_get_type(&obj_type, repo, tree_id);
1458 if (err)
1459 goto done;
1460 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1461 /* Split provided path into parent dir + entry name. */
1462 if (strchr(path, '/') == NULL) {
1463 relpath = strdup("");
1464 if (relpath == NULL) {
1465 err = got_error_from_errno();
1466 goto done;
1468 tree_path = strdup(worktree->path_prefix);
1469 if (tree_path == NULL) {
1470 err = got_error_from_errno();
1471 goto done;
1473 } else {
1474 err = got_path_dirname(&relpath, path);
1475 if (err)
1476 goto done;
1477 if (asprintf(&tree_path, "%s%s%s",
1478 worktree->path_prefix,
1479 got_path_is_root_dir(
1480 worktree->path_prefix) ? "" : "/",
1481 relpath) == -1) {
1482 err = got_error_from_errno();
1483 goto done;
1486 err = got_object_id_by_path(&tree_id, repo,
1487 worktree->base_commit_id, tree_path);
1488 free(tree_path);
1489 if (err)
1490 goto done;
1491 entry_name = basename(path);
1492 if (entry_name == NULL) {
1493 err = got_error_from_errno();
1494 goto done;
1497 } else {
1498 relpath = strdup("");
1499 if (relpath == NULL) {
1500 err = got_error_from_errno();
1501 goto done;
1503 err = got_object_id_by_path(&tree_id, repo,
1504 worktree->base_commit_id, worktree->path_prefix);
1505 if (err)
1506 goto done;
1509 err = got_object_open_as_tree(&tree, repo, tree_id);
1510 if (err)
1511 goto done;
1513 if (entry_name &&
1514 got_object_tree_find_entry(tree, entry_name) == NULL) {
1515 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1516 goto done;
1519 diff_cb.diff_old_new = diff_old_new;
1520 diff_cb.diff_old = diff_old;
1521 diff_cb.diff_new = diff_new;
1522 arg.fileindex = fileindex;
1523 arg.worktree = worktree;
1524 arg.repo = repo;
1525 arg.progress_cb = progress_cb;
1526 arg.progress_arg = progress_arg;
1527 arg.cancel_cb = cancel_cb;
1528 arg.cancel_arg = cancel_arg;
1529 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1530 entry_name, repo, &diff_cb, &arg);
1532 /* Try to sync the fileindex back to disk in any case. */
1533 err = got_fileindex_write(fileindex, new_index);
1534 if (err)
1535 goto done;
1537 if (rename(new_fileindex_path, fileindex_path) != 0) {
1538 err = got_error_from_errno();
1539 unlink(new_fileindex_path);
1540 goto done;
1543 free(new_fileindex_path);
1544 new_fileindex_path = NULL;
1546 done:
1547 free(relpath);
1548 if (tree)
1549 got_object_tree_close(tree);
1550 if (commit)
1551 got_object_commit_close(commit);
1552 if (new_fileindex_path)
1553 unlink(new_fileindex_path);
1554 if (new_index)
1555 fclose(new_index);
1556 free(new_fileindex_path);
1557 free(fileindex_path);
1558 got_fileindex_free(fileindex);
1559 if (checkout_err)
1560 err = checkout_err;
1561 unlockerr = lock_worktree(worktree, LOCK_SH);
1562 if (unlockerr && err == NULL)
1563 err = unlockerr;
1564 return err;
1567 struct diff_dir_cb_arg {
1568 struct got_fileindex *fileindex;
1569 struct got_worktree *worktree;
1570 const char *status_path;
1571 size_t status_path_len;
1572 struct got_repository *repo;
1573 got_worktree_status_cb status_cb;
1574 void *status_arg;
1575 got_worktree_cancel_cb cancel_cb;
1576 void *cancel_arg;
1579 static const struct got_error *
1580 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1581 got_worktree_status_cb status_cb, void *status_arg,
1582 struct got_repository *repo)
1584 const struct got_error *err = NULL;
1585 unsigned char status = GOT_STATUS_NO_CHANGE;
1586 struct stat sb;
1587 struct got_object_id id;
1589 err = get_file_status(&status, &sb, ie, abspath, repo);
1590 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1591 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1592 err = (*status_cb)(status_arg, status, ie->path, &id);
1594 return err;
1597 static const struct got_error *
1598 status_old_new(void *arg, struct got_fileindex_entry *ie,
1599 struct dirent *de, const char *parent_path)
1601 const struct got_error *err = NULL;
1602 struct diff_dir_cb_arg *a = arg;
1603 char *abspath;
1605 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1606 return got_error(GOT_ERR_CANCELLED);
1608 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1609 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1610 return NULL;
1612 if (parent_path[0]) {
1613 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1614 parent_path, de->d_name) == -1)
1615 return got_error_from_errno();
1616 } else {
1617 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1618 de->d_name) == -1)
1619 return got_error_from_errno();
1622 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1623 a->repo);
1624 free(abspath);
1625 return err;
1628 static const struct got_error *
1629 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1631 struct diff_dir_cb_arg *a = arg;
1632 struct got_object_id id;
1633 unsigned char status;
1635 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1636 return got_error(GOT_ERR_CANCELLED);
1638 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1639 return NULL;
1641 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1642 if (got_fileindex_entry_has_file_on_disk(ie))
1643 status = GOT_STATUS_MISSING;
1644 else
1645 status = GOT_STATUS_DELETE;
1646 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1649 static const struct got_error *
1650 status_new(void *arg, struct dirent *de, const char *parent_path)
1652 const struct got_error *err = NULL;
1653 struct diff_dir_cb_arg *a = arg;
1654 char *path = NULL;
1656 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1657 return got_error(GOT_ERR_CANCELLED);
1659 if (de->d_type == DT_DIR)
1660 return NULL;
1662 /* XXX ignore symlinks for now */
1663 if (de->d_type == DT_LNK)
1664 return NULL;
1666 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1667 return NULL;
1669 if (parent_path[0]) {
1670 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1671 return got_error_from_errno();
1672 } else {
1673 path = de->d_name;
1676 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1677 NULL);
1678 if (parent_path[0])
1679 free(path);
1680 return err;
1683 const struct got_error *
1684 got_worktree_status(struct got_worktree *worktree, const char *path,
1685 struct got_repository *repo, got_worktree_status_cb status_cb,
1686 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1688 const struct got_error *err = NULL;
1689 DIR *workdir = NULL;
1690 char *fileindex_path = NULL;
1691 struct got_fileindex *fileindex = NULL;
1692 FILE *index = NULL;
1693 struct got_fileindex_diff_dir_cb fdiff_cb;
1694 struct diff_dir_cb_arg arg;
1695 char *ondisk_path = NULL;
1697 fileindex = got_fileindex_alloc();
1698 if (fileindex == NULL) {
1699 err = got_error_from_errno();
1700 goto done;
1703 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1704 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1705 err = got_error_from_errno();
1706 fileindex_path = NULL;
1707 goto done;
1710 index = fopen(fileindex_path, "rb");
1711 if (index == NULL) {
1712 if (errno != ENOENT) {
1713 err = got_error_from_errno();
1714 goto done;
1716 } else {
1717 err = got_fileindex_read(fileindex, index);
1718 fclose(index);
1719 if (err)
1720 goto done;
1723 if (asprintf(&ondisk_path, "%s%s%s",
1724 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1725 err = got_error_from_errno();
1726 goto done;
1728 workdir = opendir(ondisk_path);
1729 if (workdir == NULL) {
1730 if (errno == ENOTDIR || errno == ENOENT) {
1731 struct got_fileindex_entry *ie;
1732 ie = got_fileindex_entry_get(fileindex, path);
1733 if (ie == NULL) {
1734 err = got_error(GOT_ERR_BAD_PATH);
1735 goto done;
1737 err = report_file_status(ie, ondisk_path,
1738 status_cb, status_arg, repo);
1739 goto done;
1740 } else {
1741 err = got_error_from_errno();
1742 goto done;
1745 fdiff_cb.diff_old_new = status_old_new;
1746 fdiff_cb.diff_old = status_old;
1747 fdiff_cb.diff_new = status_new;
1748 arg.fileindex = fileindex;
1749 arg.worktree = worktree;
1750 arg.status_path = path;
1751 arg.status_path_len = strlen(path);
1752 arg.repo = repo;
1753 arg.status_cb = status_cb;
1754 arg.status_arg = status_arg;
1755 arg.cancel_cb = cancel_cb;
1756 arg.cancel_arg = cancel_arg;
1757 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1758 path, repo, &fdiff_cb, &arg);
1759 done:
1760 if (workdir)
1761 closedir(workdir);
1762 free(ondisk_path);
1763 free(fileindex_path);
1764 got_fileindex_free(fileindex);
1765 return err;
1768 const struct got_error *
1769 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1770 const char *arg)
1772 const struct got_error *err = NULL;
1773 char *resolved, *path = NULL;
1774 size_t len;
1776 *wt_path = NULL;
1778 resolved = realpath(arg, NULL);
1779 if (resolved == NULL)
1780 return got_error_from_errno();
1782 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1783 strlen(got_worktree_get_root_path(worktree)))) {
1784 err = got_error(GOT_ERR_BAD_PATH);
1785 goto done;
1788 path = strdup(resolved +
1789 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1790 if (path == NULL) {
1791 err = got_error_from_errno();
1792 goto done;
1795 /* XXX status walk can't deal with trailing slash! */
1796 len = strlen(path);
1797 while (path[len - 1] == '/') {
1798 path[len - 1] = '\0';
1799 len--;
1801 done:
1802 free(resolved);
1803 if (err == NULL)
1804 *wt_path = path;
1805 else
1806 free(path);
1807 return err;
1810 const struct got_error *
1811 got_worktree_schedule_add(struct got_worktree *worktree,
1812 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1813 struct got_repository *repo)
1815 struct got_fileindex *fileindex = NULL;
1816 struct got_fileindex_entry *ie = NULL;
1817 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1818 FILE *index = NULL, *new_index = NULL;
1819 const struct got_error *err = NULL, *unlockerr = NULL;
1820 int ie_added = 0;
1822 err = lock_worktree(worktree, LOCK_EX);
1823 if (err)
1824 return err;
1826 err = got_path_skip_common_ancestor(&relpath,
1827 got_worktree_get_root_path(worktree), ondisk_path);
1828 if (err)
1829 goto done;
1831 fileindex = got_fileindex_alloc();
1832 if (fileindex == NULL) {
1833 err = got_error_from_errno();
1834 goto done;
1837 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1838 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1839 err = got_error_from_errno();
1840 fileindex_path = NULL;
1841 goto done;
1844 index = fopen(fileindex_path, "rb");
1845 if (index == NULL) {
1846 err = got_error_from_errno();
1847 goto done;
1850 err = got_fileindex_read(fileindex, index);
1851 if (err)
1852 goto done;
1854 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1855 err = got_error_set_errno(EEXIST);
1856 goto done;
1859 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1860 if (err)
1861 goto done;
1863 err = got_fileindex_entry_add(fileindex, ie);
1864 if (err)
1865 goto done;
1866 ie_added = 1; /* now owned by fileindex; don't free separately */
1868 err = got_opentemp_named(&new_fileindex_path, &new_index,
1869 fileindex_path);
1870 if (err)
1871 goto done;
1873 err = got_fileindex_write(fileindex, new_index);
1874 if (err)
1875 goto done;
1877 if (rename(new_fileindex_path, fileindex_path) != 0) {
1878 err = got_error_from_errno();
1879 goto done;
1882 free(new_fileindex_path);
1883 new_fileindex_path = NULL;
1885 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1886 done:
1887 if (index) {
1888 if (fclose(index) != 0 && err == NULL)
1889 err = got_error_from_errno();
1891 if (new_fileindex_path) {
1892 if (unlink(new_fileindex_path) != 0 && err == NULL)
1893 err = got_error_from_errno();
1894 free(new_fileindex_path);
1896 if (ie && !ie_added)
1897 got_fileindex_entry_free(ie);
1898 if (fileindex)
1899 got_fileindex_free(fileindex);
1900 unlockerr = lock_worktree(worktree, LOCK_SH);
1901 if (unlockerr && err == NULL)
1902 err = unlockerr;
1903 free(relpath);
1904 return err;
1907 const struct got_error *
1908 got_worktree_schedule_delete(struct got_worktree *worktree,
1909 const char *ondisk_path, int delete_local_mods,
1910 got_worktree_status_cb status_cb, void *status_arg,
1911 struct got_repository *repo)
1913 struct got_fileindex *fileindex = NULL;
1914 struct got_fileindex_entry *ie = NULL;
1915 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1916 FILE *index = NULL, *new_index = NULL;
1917 const struct got_error *err = NULL, *unlockerr = NULL;
1918 unsigned char status;
1919 struct stat sb;
1921 err = lock_worktree(worktree, LOCK_EX);
1922 if (err)
1923 return err;
1925 err = got_path_skip_common_ancestor(&relpath,
1926 got_worktree_get_root_path(worktree), ondisk_path);
1927 if (err)
1928 goto done;
1930 fileindex = got_fileindex_alloc();
1931 if (fileindex == NULL) {
1932 err = got_error_from_errno();
1933 goto done;
1936 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1937 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1938 err = got_error_from_errno();
1939 fileindex_path = NULL;
1940 goto done;
1943 index = fopen(fileindex_path, "rb");
1944 if (index == NULL) {
1945 err = got_error_from_errno();
1946 goto done;
1949 err = got_fileindex_read(fileindex, index);
1950 if (err)
1951 goto done;
1953 ie = got_fileindex_entry_get(fileindex, relpath);
1954 if (ie == NULL) {
1955 err = got_error(GOT_ERR_BAD_PATH);
1956 goto done;
1959 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1960 if (err)
1961 goto done;
1963 if (status != GOT_STATUS_NO_CHANGE) {
1964 if (status == GOT_STATUS_DELETE) {
1965 err = got_error_set_errno(ENOENT);
1966 goto done;
1968 if (status != GOT_STATUS_MODIFY) {
1969 err = got_error(GOT_ERR_FILE_STATUS);
1970 goto done;
1972 if (!delete_local_mods) {
1973 err = got_error(GOT_ERR_FILE_MODIFIED);
1974 goto done;
1978 if (unlink(ondisk_path) != 0) {
1979 err = got_error_from_errno();
1980 goto done;
1983 got_fileindex_entry_mark_deleted_from_disk(ie);
1985 err = got_opentemp_named(&new_fileindex_path, &new_index,
1986 fileindex_path);
1987 if (err)
1988 goto done;
1990 err = got_fileindex_write(fileindex, new_index);
1991 if (err)
1992 goto done;
1994 if (rename(new_fileindex_path, fileindex_path) != 0) {
1995 err = got_error_from_errno();
1996 goto done;
1999 free(new_fileindex_path);
2000 new_fileindex_path = NULL;
2002 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2003 done:
2004 free(relpath);
2005 if (index) {
2006 if (fclose(index) != 0 && err == NULL)
2007 err = got_error_from_errno();
2009 if (new_fileindex_path) {
2010 if (unlink(new_fileindex_path) != 0 && err == NULL)
2011 err = got_error_from_errno();
2012 free(new_fileindex_path);
2014 if (fileindex)
2015 got_fileindex_free(fileindex);
2016 unlockerr = lock_worktree(worktree, LOCK_SH);
2017 if (unlockerr && err == NULL)
2018 err = unlockerr;
2019 return err;
2022 const struct got_error *
2023 got_worktree_revert(struct got_worktree *worktree,
2024 const char *ondisk_path,
2025 got_worktree_checkout_cb progress_cb, void *progress_arg,
2026 struct got_repository *repo)
2028 struct got_fileindex *fileindex = NULL;
2029 struct got_fileindex_entry *ie = NULL;
2030 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2031 char *tree_path = NULL, *parent_path, *te_name;
2032 FILE *index = NULL, *new_index = NULL;
2033 const struct got_error *err = NULL, *unlockerr = NULL;
2034 struct got_tree_object *tree = NULL;
2035 struct got_object_id id, *tree_id = NULL;
2036 const struct got_tree_entry *te;
2037 struct got_blob_object *blob = NULL;
2038 unsigned char status;
2039 struct stat sb;
2041 err = lock_worktree(worktree, LOCK_EX);
2042 if (err)
2043 return err;
2045 err = got_path_skip_common_ancestor(&relpath,
2046 got_worktree_get_root_path(worktree), ondisk_path);
2047 if (err)
2048 goto done;
2050 fileindex = got_fileindex_alloc();
2051 if (fileindex == NULL) {
2052 err = got_error_from_errno();
2053 goto done;
2056 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2057 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2058 err = got_error_from_errno();
2059 fileindex_path = NULL;
2060 goto done;
2063 index = fopen(fileindex_path, "rb");
2064 if (index == NULL) {
2065 err = got_error_from_errno();
2066 goto done;
2069 err = got_fileindex_read(fileindex, index);
2070 if (err)
2071 goto done;
2073 ie = got_fileindex_entry_get(fileindex, relpath);
2074 if (ie == NULL) {
2075 err = got_error(GOT_ERR_BAD_PATH);
2076 goto done;
2079 /* Construct in-repository path of tree which contains this blob. */
2080 err = got_path_dirname(&parent_path, ie->path);
2081 if (err) {
2082 if (err->code != GOT_ERR_BAD_PATH)
2083 goto done;
2084 parent_path = "/";
2086 if (got_path_is_root_dir(worktree->path_prefix)) {
2087 tree_path = strdup(parent_path);
2088 if (tree_path == NULL) {
2089 err = got_error_from_errno();
2090 goto done;
2092 } else {
2093 if (got_path_is_root_dir(parent_path)) {
2094 tree_path = strdup(worktree->path_prefix);
2095 if (tree_path == NULL) {
2096 err = got_error_from_errno();
2097 goto done;
2099 } else {
2100 if (asprintf(&tree_path, "%s/%s",
2101 worktree->path_prefix, parent_path) == -1) {
2102 err = got_error_from_errno();
2103 goto done;
2108 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2109 tree_path);
2110 if (err)
2111 goto done;
2113 err = got_object_open_as_tree(&tree, repo, tree_id);
2114 if (err)
2115 goto done;
2117 te_name = basename(ie->path);
2118 if (te_name == NULL) {
2119 err = got_error_from_errno();
2120 goto done;
2123 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2124 if (err)
2125 goto done;
2127 te = got_object_tree_find_entry(tree, te_name);
2128 if (te == NULL && status != GOT_STATUS_ADD) {
2129 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2130 goto done;
2133 switch (status) {
2134 case GOT_STATUS_ADD:
2135 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2136 got_fileindex_entry_remove(fileindex, ie);
2137 break;
2138 case GOT_STATUS_DELETE:
2139 case GOT_STATUS_MODIFY:
2140 case GOT_STATUS_CONFLICT:
2141 case GOT_STATUS_MISSING:
2142 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2143 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2144 if (err)
2145 goto done;
2146 err = install_blob(worktree, ondisk_path, ie->path,
2147 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2148 progress_arg);
2149 if (err)
2150 goto done;
2151 if (status == GOT_STATUS_DELETE) {
2152 err = update_blob_fileindex_entry(worktree,
2153 fileindex, ie, ondisk_path, ie->path, blob, 1);
2154 if (err)
2155 goto done;
2157 break;
2158 default:
2159 goto done;
2162 err = got_opentemp_named(&new_fileindex_path, &new_index,
2163 fileindex_path);
2164 if (err)
2165 goto done;
2167 err = got_fileindex_write(fileindex, new_index);
2168 if (err)
2169 goto done;
2171 if (rename(new_fileindex_path, fileindex_path) != 0) {
2172 err = got_error_from_errno();
2173 goto done;
2176 free(new_fileindex_path);
2177 new_fileindex_path = NULL;
2178 done:
2179 free(relpath);
2180 free(tree_path);
2181 if (blob)
2182 got_object_blob_close(blob);
2183 if (tree)
2184 got_object_tree_close(tree);
2185 free(tree_id);
2186 if (index) {
2187 if (fclose(index) != 0 && err == NULL)
2188 err = got_error_from_errno();
2190 if (new_fileindex_path) {
2191 if (unlink(new_fileindex_path) != 0 && err == NULL)
2192 err = got_error_from_errno();
2193 free(new_fileindex_path);
2195 if (fileindex)
2196 got_fileindex_free(fileindex);
2197 unlockerr = lock_worktree(worktree, LOCK_SH);
2198 if (unlockerr && err == NULL)
2199 err = unlockerr;
2200 return err;
2203 struct commitable {
2204 char *path;
2205 unsigned char status;
2206 struct got_object_id *id;
2207 struct got_object_id *tree_id;
2210 static void
2211 free_commitable(struct commitable *ct)
2213 free(ct->path);
2214 free(ct->id);
2215 free(ct->tree_id);
2216 free(ct);
2219 struct collect_commitables_arg {
2220 struct got_pathlist_head *paths;
2221 struct got_repository *repo;
2222 struct got_worktree *worktree;
2225 static const struct got_error *
2226 collect_commitables(void *arg, unsigned char status, const char *path,
2227 struct got_object_id *id)
2229 struct collect_commitables_arg *a = arg;
2230 const struct got_error *err = NULL;
2231 struct commitable *ct = NULL;
2232 struct got_pathlist_entry *new = NULL;
2233 char *parent_path = NULL;
2235 if (status == GOT_STATUS_CONFLICT)
2236 return got_error(GOT_ERR_COMMIT_CONFLICT);
2238 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2239 status != GOT_STATUS_DELETE)
2240 return NULL;
2242 if (strchr(path, '/') == NULL) {
2243 parent_path = strdup("/");
2244 if (parent_path == NULL)
2245 return got_error_from_errno();
2246 } else {
2247 err = got_path_dirname(&parent_path, path);
2248 if (err)
2249 return err;
2252 ct = malloc(sizeof(*ct));
2253 if (ct == NULL) {
2254 err = got_error_from_errno();
2255 goto done;
2258 ct->status = status;
2259 ct->id = NULL;
2260 err = got_object_id_by_path(&ct->tree_id, a->repo,
2261 a->worktree->base_commit_id, parent_path);
2262 if (err)
2263 goto done;
2264 ct->path = strdup(path);
2265 if (ct->path == NULL) {
2266 err = got_error_from_errno();
2267 goto done;
2269 err = got_pathlist_insert(&new, a->paths, ct->path, ct);
2270 done:
2271 if (ct && (err || new == NULL))
2272 free_commitable(ct);
2273 free(parent_path);
2274 return err;
2277 struct write_tree_arg {
2278 struct got_pathlist_head *commitable_paths;
2279 struct got_object_idset *affected_trees;
2280 struct got_repository *repo;
2283 static const struct got_error *
2284 write_tree(struct got_object_id *base_tree_id, void *data, void *arg)
2286 const struct got_error *err = NULL;
2287 struct write_tree_arg *a = arg;
2288 const struct got_tree_entries *base_entries = NULL;
2289 struct got_pathlist_head new_entries;
2290 struct got_tree_entries new_tree_entries;
2291 struct got_tree_object *base_tree = NULL;
2292 struct got_tree_entry *te;
2293 struct got_pathlist_entry *pe;
2294 struct got_object_id *new_tree_id = NULL;
2296 TAILQ_INIT(&new_entries);
2298 err = got_object_open_as_tree(&base_tree, a->repo, base_tree_id);
2299 if (err)
2300 return err;
2302 base_entries = got_object_tree_get_entries(base_tree);
2304 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2305 TAILQ_FOREACH(pe, a->commitable_paths, entry) {
2306 struct commitable *ct = NULL;
2307 struct got_tree_entry *new_te = NULL;
2308 struct got_pathlist_entry *new_pe = NULL;
2309 char *ct_name = NULL;
2311 ct = pe->data;
2313 if (got_object_id_cmp(ct->tree_id, te->id) != 0)
2314 continue; /* not part of this tree */
2316 ct_name = basename(pe->path);
2317 if (ct_name == NULL) {
2318 err = got_error_from_errno();
2319 goto done;
2321 /* Commitable and tree entry must correspond. */
2322 if (strcmp(te->name, ct_name) != 0)
2323 continue;
2325 if (ct->status == GOT_STATUS_DELETE) {
2326 /* Deleted entries disappear. */
2327 continue;
2330 /* Modified entries get updated mode and ID. */
2331 if (ct->status == GOT_STATUS_MODIFY) {
2332 err = got_object_tree_entry_dup(&new_te, te);
2333 if (err)
2334 goto done;
2335 new_te->mode = GOT_DEFAULT_FILE_MODE; /* XXX */
2336 free(new_te->id);
2337 } else if (ct->status == GOT_STATUS_ADD) {
2338 /* Added entries get... well, added. */
2339 new_te = calloc(1, sizeof(*new_te));
2340 if (new_te == NULL) {
2341 err = got_error_from_errno();
2342 goto done;
2344 new_te->mode = GOT_DEFAULT_FILE_MODE; /* XXX */
2345 new_te->name = strdup(ct_name);
2346 if (new_te->name == NULL) {
2347 err = got_error_from_errno();
2348 goto done;
2351 new_te->id = got_object_id_dup(ct->id);
2352 if (new_te->id == NULL) {
2353 err = got_error_from_errno();
2354 goto done;
2357 err = got_pathlist_insert(&new_pe, &new_entries,
2358 te->name, new_te);
2359 if (err)
2360 goto done;
2361 if (new_pe == NULL) {
2362 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
2363 goto done;
2368 new_tree_entries.nentries = 0;
2369 SIMPLEQ_INIT(&new_tree_entries.head);
2370 TAILQ_FOREACH(pe, &new_entries, entry) {
2371 struct got_tree_entry *te = pe->data;
2372 new_tree_entries.nentries++;
2373 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2376 err = got_object_tree_create(&new_tree_id, &new_tree_entries, a->repo);
2377 done:
2378 free(new_tree_id);
2379 if (base_tree)
2380 got_object_tree_close(base_tree);
2381 got_pathlist_free(&new_entries);
2382 got_object_tree_entries_close(&new_tree_entries);
2383 return err;
2386 const struct got_error *
2387 got_worktree_commit(struct got_object_id **new_commit_id,
2388 struct got_worktree *worktree, const char *ondisk_path,
2389 const char *logmsg, struct got_repository *repo)
2391 const struct got_error *err = NULL, *unlockerr = NULL;
2392 struct collect_commitables_arg cc_arg;
2393 struct write_tree_arg wt_arg;
2394 struct got_pathlist_head paths;
2395 struct got_pathlist_entry *pe;
2396 char *relpath = NULL;
2397 struct got_commit_object *base_commit = NULL;
2398 struct got_tree_object *base_tree = NULL;
2399 struct got_object_idset *tree_ids = NULL;
2401 *new_commit_id = NULL;
2403 TAILQ_INIT(&paths);
2405 if (ondisk_path) {
2406 err = got_path_skip_common_ancestor(&relpath,
2407 worktree->root_path, ondisk_path);
2408 if (err)
2409 return err;
2412 tree_ids = got_object_idset_alloc();
2413 if (tree_ids == NULL)
2414 return got_error_from_errno();
2416 err = lock_worktree(worktree, LOCK_EX);
2417 if (err)
2418 goto done;
2420 cc_arg.paths = &paths;
2421 cc_arg.worktree = worktree;
2422 cc_arg.repo = repo;
2423 err = got_worktree_status(worktree, relpath ? relpath : "",
2424 repo, collect_commitables, &cc_arg, NULL, NULL);
2425 if (err)
2426 goto done;
2428 /* TODO: collect commit message if not specified */
2430 /* Collect IDs of affected leaf trees. */
2431 TAILQ_FOREACH(pe, &paths, entry) {
2432 struct commitable *ct = pe->data;
2434 if (got_object_idset_contains(tree_ids, ct->tree_id))
2435 continue;
2437 err = got_object_idset_add(tree_ids, ct->tree_id, NULL);
2438 if (err)
2439 goto done;
2442 /* Create blobs from added and modified files and record their IDs. */
2443 TAILQ_FOREACH(pe, &paths, entry) {
2444 struct commitable *ct = pe->data;
2445 char *ondisk_path;
2447 if (ct->status != GOT_STATUS_ADD &&
2448 ct->status != GOT_STATUS_MODIFY)
2449 continue;
2451 if (asprintf(&ondisk_path, "%s/%s",
2452 worktree->root_path, pe->path) == -1) {
2453 err = got_error_from_errno();
2454 goto done;
2456 err = got_object_blob_create(&ct->id, ondisk_path, repo);
2457 free(ondisk_path);
2458 if (err)
2459 goto done;
2462 /* Write new leaf tree objects. */
2463 wt_arg.commitable_paths = &paths;
2464 wt_arg.affected_trees = got_object_idset_alloc();
2465 if (wt_arg.affected_trees == NULL) {
2466 err = got_error_from_errno();
2467 goto done;
2469 wt_arg.repo = repo;
2470 err = got_object_idset_for_each(tree_ids, write_tree, &wt_arg);
2471 if (err)
2472 goto done;
2474 err = got_object_open_as_commit(&base_commit, repo,
2475 worktree->base_commit_id);
2476 if (err)
2477 goto done;
2478 err = got_object_open_as_tree(&base_tree, repo,
2479 base_commit->tree_id);
2480 if (err)
2481 goto done;
2482 done:
2483 unlockerr = lock_worktree(worktree, LOCK_SH);
2484 if (unlockerr && err == NULL)
2485 err = unlockerr;
2486 TAILQ_FOREACH(pe, &paths, entry) {
2487 struct commitable *ct = pe->data;
2488 free_commitable(ct);
2490 got_object_idset_free(tree_ids);
2491 got_pathlist_free(&paths);
2492 if (base_tree)
2493 got_object_tree_close(base_tree);
2494 free(relpath);
2495 return err;