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 int version, fd = -1;
334 const char *errstr;
335 struct got_repository *repo = NULL;
336 uint32_t uuid_status;
338 *worktree = NULL;
340 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
341 err = got_error_from_errno();
342 path_got = NULL;
343 goto done;
346 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
347 err = got_error_from_errno();
348 path_lock = NULL;
349 goto done;
352 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
353 if (fd == -1) {
354 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
355 : got_error_from_errno());
356 goto done;
359 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
360 if (err)
361 goto done;
363 version = strtonum(formatstr, 1, INT_MAX, &errstr);
364 if (errstr) {
365 err = got_error(GOT_ERR_WORKTREE_META);
366 goto done;
368 if (version != GOT_WORKTREE_FORMAT_VERSION) {
369 err = got_error(GOT_ERR_WORKTREE_VERS);
370 goto done;
373 *worktree = calloc(1, sizeof(**worktree));
374 if (*worktree == NULL) {
375 err = got_error_from_errno();
376 goto done;
378 (*worktree)->lockfd = -1;
380 (*worktree)->root_path = strdup(path);
381 if ((*worktree)->root_path == NULL) {
382 err = got_error_from_errno();
383 goto done;
385 err = read_meta_file(&(*worktree)->repo_path, path_got,
386 GOT_WORKTREE_REPOSITORY);
387 if (err)
388 goto done;
390 err = read_meta_file(&(*worktree)->path_prefix, path_got,
391 GOT_WORKTREE_PATH_PREFIX);
392 if (err)
393 goto done;
395 err = read_meta_file(&base_commit_id_str, path_got,
396 GOT_WORKTREE_BASE_COMMIT);
397 if (err)
398 goto done;
400 err = read_meta_file(&uuidstr, path_got, GOT_WORKTREE_UUID);
401 if (err)
402 goto done;
403 uuid_from_string(uuidstr, &(*worktree)->uuid, &uuid_status);
404 if (uuid_status != uuid_s_ok) {
405 err = got_error_uuid(uuid_status);
406 goto done;
409 err = got_repo_open(&repo, (*worktree)->repo_path);
410 if (err)
411 goto done;
413 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
414 base_commit_id_str);
415 if (err)
416 goto done;
418 err = read_meta_file(&(*worktree)->head_ref_name, path_got,
419 GOT_WORKTREE_HEAD_REF);
420 done:
421 if (repo)
422 got_repo_close(repo);
423 free(path_got);
424 free(path_lock);
425 free(base_commit_id_str);
426 free(uuidstr);
427 free(formatstr);
428 if (err) {
429 if (fd != -1)
430 close(fd);
431 if (*worktree != NULL)
432 got_worktree_close(*worktree);
433 *worktree = NULL;
434 } else
435 (*worktree)->lockfd = fd;
437 return err;
440 const struct got_error *
441 got_worktree_open(struct got_worktree **worktree, const char *path)
443 const struct got_error *err = NULL;
445 do {
446 err = open_worktree(worktree, path);
447 if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT))
448 return err;
449 if (*worktree)
450 return NULL;
451 path = dirname(path);
452 if (path == NULL)
453 return got_error_from_errno();
454 } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
456 return got_error(GOT_ERR_NOT_WORKTREE);
459 const struct got_error *
460 got_worktree_close(struct got_worktree *worktree)
462 const struct got_error *err = NULL;
463 free(worktree->root_path);
464 free(worktree->repo_path);
465 free(worktree->path_prefix);
466 free(worktree->base_commit_id);
467 free(worktree->head_ref_name);
468 if (worktree->lockfd != -1)
469 if (close(worktree->lockfd) != 0)
470 err = got_error_from_errno();
471 free(worktree);
472 return err;
475 const char *
476 got_worktree_get_root_path(struct got_worktree *worktree)
478 return worktree->root_path;
481 const char *
482 got_worktree_get_repo_path(struct got_worktree *worktree)
484 return worktree->repo_path;
487 const char *
488 got_worktree_get_path_prefix(struct got_worktree *worktree)
490 return worktree->path_prefix;
493 const struct got_error *
494 got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
495 const char *path_prefix)
497 char *absprefix = NULL;
499 if (!got_path_is_absolute(path_prefix)) {
500 if (asprintf(&absprefix, "/%s", path_prefix) == -1)
501 return got_error_from_errno();
503 *match = (strcmp(absprefix ? absprefix : path_prefix,
504 worktree->path_prefix) == 0);
505 free(absprefix);
506 return NULL;
509 const char *
510 got_worktree_get_head_ref_name(struct got_worktree *worktree)
512 return worktree->head_ref_name;
515 struct got_object_id *
516 got_worktree_get_base_commit_id(struct got_worktree *worktree)
518 return worktree->base_commit_id;
521 const struct got_error *
522 got_worktree_set_base_commit_id(struct got_worktree *worktree,
523 struct got_repository *repo, struct got_object_id *commit_id)
525 const struct got_error *err;
526 struct got_object *obj = NULL;
527 char *id_str = NULL;
528 char *path_got = NULL;
530 if (asprintf(&path_got, "%s/%s", worktree->root_path,
531 GOT_WORKTREE_GOT_DIR) == -1) {
532 err = got_error_from_errno();
533 path_got = NULL;
534 goto done;
537 err = got_object_open(&obj, repo, commit_id);
538 if (err)
539 return err;
541 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
542 err = got_error(GOT_ERR_OBJ_TYPE);
543 goto done;
546 /* Record our base commit. */
547 err = got_object_id_str(&id_str, commit_id);
548 if (err)
549 goto done;
550 err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
551 if (err)
552 goto done;
554 free(worktree->base_commit_id);
555 worktree->base_commit_id = got_object_id_dup(commit_id);
556 if (worktree->base_commit_id == NULL) {
557 err = got_error_from_errno();
558 goto done;
560 done:
561 if (obj)
562 got_object_close(obj);
563 free(id_str);
564 free(path_got);
565 return err;
568 static const struct got_error *
569 lock_worktree(struct got_worktree *worktree, int operation)
571 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
572 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
573 : got_error_from_errno());
574 return NULL;
577 static const struct got_error *
578 add_dir_on_disk(struct got_worktree *worktree, const char *path)
580 const struct got_error *err = NULL;
581 char *abspath;
583 if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
584 return got_error_from_errno();
586 err = got_path_mkdir(abspath);
587 if (err && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
588 struct stat sb;
589 err = NULL;
590 if (lstat(abspath, &sb) == -1) {
591 err = got_error_from_errno();
592 } else if (!S_ISDIR(sb.st_mode)) {
593 /* TODO directory is obstructed; do something */
594 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
597 free(abspath);
598 return err;
601 static const struct got_error *
602 check_file_contents_equal(int *same, FILE *f1, FILE *f2)
604 const struct got_error *err = NULL;
605 uint8_t fbuf1[8192];
606 uint8_t fbuf2[8192];
607 size_t flen1 = 0, flen2 = 0;
609 *same = 1;
611 while (1) {
612 flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
613 if (flen1 == 0 && ferror(f1)) {
614 err = got_error_from_errno();
615 break;
617 flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
618 if (flen2 == 0 && ferror(f2)) {
619 err = got_error_from_errno();
620 break;
622 if (flen1 == 0) {
623 if (flen2 != 0)
624 *same = 0;
625 break;
626 } else if (flen2 == 0) {
627 if (flen1 != 0)
628 *same = 0;
629 break;
630 } else if (flen1 == flen2) {
631 if (memcmp(fbuf1, fbuf2, flen2) != 0) {
632 *same = 0;
633 break;
635 } else {
636 *same = 0;
637 break;
641 return err;
644 static const struct got_error *
645 check_files_equal(int *same, const char *f1_path, const char *f2_path)
647 const struct got_error *err = NULL;
648 struct stat sb;
649 size_t size1, size2;
650 FILE *f1 = NULL, *f2 = NULL;
652 *same = 1;
654 if (lstat(f1_path, &sb) != 0) {
655 err = got_error_from_errno();
656 goto done;
658 size1 = sb.st_size;
660 if (lstat(f2_path, &sb) != 0) {
661 err = got_error_from_errno();
662 goto done;
664 size2 = sb.st_size;
666 if (size1 != size2) {
667 *same = 0;
668 return NULL;
671 f1 = fopen(f1_path, "r");
672 if (f1 == NULL)
673 return got_error_from_errno();
675 f2 = fopen(f2_path, "r");
676 if (f2 == NULL) {
677 err = got_error_from_errno();
678 goto done;
681 err = check_file_contents_equal(same, f1, f2);
682 done:
683 if (f1 && fclose(f1) != 0 && err == NULL)
684 err = got_error_from_errno();
685 if (f2 && fclose(f2) != 0 && err == NULL)
686 err = got_error_from_errno();
688 return err;
691 /*
692 * Perform a 3-way merge where the file's version in the file index (blob2)
693 * acts as the common ancestor, the incoming blob (blob1) acts as the first
694 * derived version, and the file on disk acts as the second derived version.
695 */
696 static const struct got_error *
697 merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
698 struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
699 uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
700 struct got_repository *repo,
701 got_worktree_checkout_cb progress_cb, void *progress_arg)
703 const struct got_error *err = NULL;
704 int merged_fd = -1;
705 struct got_blob_object *blob2 = NULL;
706 FILE *f1 = NULL, *f2 = NULL;
707 char *blob1_path = NULL, *blob2_path = NULL;
708 char *merged_path = NULL, *base_path = NULL;
709 char *id_str = NULL;
710 char *label1 = NULL;
711 int overlapcnt = 0, update_timestamps = 0;
712 char *parent;
714 parent = dirname(ondisk_path);
715 if (parent == NULL)
716 return got_error_from_errno();
718 if (asprintf(&base_path, "%s/got-merged", parent) == -1)
719 return got_error_from_errno();
721 err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
722 if (err)
723 goto done;
725 free(base_path);
726 if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
727 err = got_error_from_errno();
728 base_path = NULL;
729 goto done;
732 err = got_opentemp_named(&blob1_path, &f1, base_path);
733 if (err)
734 goto done;
735 err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
736 if (err)
737 goto done;
739 free(base_path);
740 if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
741 err = got_error_from_errno();
742 base_path = NULL;
743 goto done;
746 err = got_opentemp_named(&blob2_path, &f2, base_path);
747 if (err)
748 goto done;
749 if (got_fileindex_entry_has_blob(ie)) {
750 struct got_object_id id2;
751 memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
752 err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
753 if (err)
754 goto done;
755 err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
756 if (err)
757 goto done;
758 } else {
759 /*
760 * If the file has no blob, this is an "add vs add" conflict,
761 * and we simply use an empty ancestor file to make both files
762 * appear in the merged result in their entirety.
763 */
766 err = got_object_id_str(&id_str, worktree->base_commit_id);
767 if (err)
768 goto done;
769 if (asprintf(&label1, "commit %s", id_str) == -1) {
770 err = got_error_from_errno();
771 goto done;
774 err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
775 blob2_path, ondisk_path, label1, path);
776 if (err)
777 goto done;
779 (*progress_cb)(progress_arg,
780 overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
783 if (fsync(merged_fd) != 0) {
784 err = got_error_from_errno();
785 goto done;
788 /* Check if a clean merge has subsumed all local changes. */
789 if (overlapcnt == 0) {
790 err = check_files_equal(&update_timestamps, blob1_path,
791 merged_path);
792 if (err)
793 goto done;
796 if (chmod(merged_path, st_mode) != 0) {
797 err = got_error_from_errno();
798 goto done;
801 if (rename(merged_path, ondisk_path) != 0) {
802 err = got_error_from_errno();
803 unlink(merged_path);
804 goto done;
807 /*
808 * Do not update timestamps of already modified files. Otherwise,
809 * a future status walk would treat them as unmodified files again.
810 */
811 err = got_fileindex_entry_update(ie, ondisk_path,
812 blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
813 done:
814 if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
815 err = got_error_from_errno();
816 if (f1 && fclose(f1) != 0 && err == NULL)
817 err = got_error_from_errno();
818 if (f2 && fclose(f2) != 0 && err == NULL)
819 err = got_error_from_errno();
820 if (blob2)
821 got_object_blob_close(blob2);
822 free(merged_path);
823 free(base_path);
824 if (blob1_path) {
825 unlink(blob1_path);
826 free(blob1_path);
828 if (blob2_path) {
829 unlink(blob2_path);
830 free(blob2_path);
832 free(id_str);
833 free(label1);
834 return err;
837 static const struct got_error *
838 update_blob_fileindex_entry(struct got_worktree *worktree,
839 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
840 const char *ondisk_path, const char *path, struct got_blob_object *blob,
841 int update_timestamps)
843 const struct got_error *err = NULL;
845 if (ie == NULL)
846 ie = got_fileindex_entry_get(fileindex, path);
847 if (ie)
848 err = got_fileindex_entry_update(ie, ondisk_path,
849 blob->id.sha1, worktree->base_commit_id->sha1,
850 update_timestamps);
851 else {
852 struct got_fileindex_entry *new_ie;
853 err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
854 path, blob->id.sha1, worktree->base_commit_id->sha1);
855 if (!err)
856 err = got_fileindex_entry_add(fileindex, new_ie);
858 return err;
861 static const struct got_error *
862 install_blob(struct got_worktree *worktree, const char *ondisk_path,
863 const char *path, uint16_t te_mode, uint16_t st_mode,
864 struct got_blob_object *blob, int restoring_missing_file,
865 int reverting_versioned_file, struct got_repository *repo,
866 got_worktree_checkout_cb progress_cb, void *progress_arg)
868 const struct got_error *err = NULL;
869 int fd = -1;
870 size_t len, hdrlen;
871 int update = 0;
872 char *tmppath = NULL;
874 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
875 GOT_DEFAULT_FILE_MODE);
876 if (fd == -1) {
877 if (errno == ENOENT) {
878 char *parent = dirname(path);
879 if (parent == NULL)
880 return got_error_from_errno();
881 err = add_dir_on_disk(worktree, parent);
882 if (err)
883 return err;
884 fd = open(ondisk_path,
885 O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
886 GOT_DEFAULT_FILE_MODE);
887 if (fd == -1)
888 return got_error_from_errno();
889 } else if (errno == EEXIST) {
890 if (!S_ISREG(st_mode)) {
891 /* TODO file is obstructed; do something */
892 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
893 goto done;
894 } else {
895 err = got_opentemp_named_fd(&tmppath, &fd,
896 ondisk_path);
897 if (err)
898 goto done;
899 update = 1;
901 } else
902 return got_error_from_errno();
905 if (restoring_missing_file)
906 (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
907 else if (reverting_versioned_file)
908 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, path);
909 else
910 (*progress_cb)(progress_arg,
911 update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
913 hdrlen = got_object_blob_get_hdrlen(blob);
914 do {
915 const uint8_t *buf = got_object_blob_get_read_buf(blob);
916 err = got_object_blob_read_block(&len, blob);
917 if (err)
918 break;
919 if (len > 0) {
920 /* Skip blob object header first time around. */
921 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
922 if (outlen == -1) {
923 err = got_error_from_errno();
924 goto done;
925 } else if (outlen != len - hdrlen) {
926 err = got_error(GOT_ERR_IO);
927 goto done;
929 hdrlen = 0;
931 } while (len != 0);
933 if (fsync(fd) != 0) {
934 err = got_error_from_errno();
935 goto done;
938 if (update) {
939 if (rename(tmppath, ondisk_path) != 0) {
940 err = got_error_from_errno();
941 unlink(tmppath);
942 goto done;
946 if (te_mode & S_IXUSR) {
947 if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
948 err = got_error_from_errno();
949 goto done;
951 } else {
952 if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
953 err = got_error_from_errno();
954 goto done;
958 done:
959 if (fd != -1 && close(fd) != 0 && err == NULL)
960 err = got_error_from_errno();
961 free(tmppath);
962 return err;
965 /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
966 static const struct got_error *
967 get_modified_file_content_status(unsigned char *status, FILE *f)
969 const struct got_error *err = NULL;
970 const char *markers[3] = {
971 GOT_DIFF_CONFLICT_MARKER_BEGIN,
972 GOT_DIFF_CONFLICT_MARKER_SEP,
973 GOT_DIFF_CONFLICT_MARKER_END
974 };
975 int i = 0;
976 char *line;
977 size_t len;
978 const char delim[3] = {'\0', '\0', '\0'};
980 while (*status == GOT_STATUS_MODIFY) {
981 line = fparseln(f, &len, NULL, delim, 0);
982 if (line == NULL) {
983 if (feof(f))
984 break;
985 err = got_ferror(f, GOT_ERR_IO);
986 break;
989 if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
990 if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
991 *status = GOT_STATUS_CONFLICT;
992 else
993 i++;
997 return err;
1000 static const struct got_error *
1001 get_file_status(unsigned char *status, struct stat *sb,
1002 struct got_fileindex_entry *ie, const char *abspath,
1003 struct got_repository *repo)
1005 const struct got_error *err = NULL;
1006 struct got_object_id id;
1007 size_t hdrlen;
1008 FILE *f = NULL;
1009 uint8_t fbuf[8192];
1010 struct got_blob_object *blob = NULL;
1011 size_t flen, blen;
1013 *status = GOT_STATUS_NO_CHANGE;
1015 if (lstat(abspath, sb) == -1) {
1016 if (errno == ENOENT) {
1017 if (ie) {
1018 if (got_fileindex_entry_has_file_on_disk(ie))
1019 *status = GOT_STATUS_MISSING;
1020 else
1021 *status = GOT_STATUS_DELETE;
1022 sb->st_mode =
1023 ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1024 & (S_IRWXU | S_IRWXG | S_IRWXO));
1025 } else
1026 sb->st_mode = GOT_DEFAULT_FILE_MODE;
1027 return NULL;
1029 return got_error_from_errno();
1032 if (!S_ISREG(sb->st_mode)) {
1033 *status = GOT_STATUS_OBSTRUCTED;
1034 return NULL;
1037 if (ie == NULL)
1038 return NULL;
1040 if (!got_fileindex_entry_has_file_on_disk(ie)) {
1041 *status = GOT_STATUS_DELETE;
1042 return NULL;
1043 } else if (!got_fileindex_entry_has_blob(ie)) {
1044 *status = GOT_STATUS_ADD;
1045 return NULL;
1048 if (ie->ctime_sec == sb->st_ctime &&
1049 ie->ctime_nsec == sb->st_ctimensec &&
1050 ie->mtime_sec == sb->st_mtime &&
1051 ie->mtime_sec == sb->st_mtime &&
1052 ie->mtime_nsec == sb->st_mtimensec &&
1053 ie->size == (sb->st_size & 0xffffffff))
1054 return NULL;
1056 memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1057 err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1058 if (err)
1059 return err;
1061 f = fopen(abspath, "r");
1062 if (f == NULL) {
1063 err = got_error_from_errno();
1064 goto done;
1066 hdrlen = got_object_blob_get_hdrlen(blob);
1067 while (1) {
1068 const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1069 err = got_object_blob_read_block(&blen, blob);
1070 if (err)
1071 goto done;
1072 /* Skip length of blob object header first time around. */
1073 flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1074 if (flen == 0 && ferror(f)) {
1075 err = got_error_from_errno();
1076 goto done;
1078 if (blen == 0) {
1079 if (flen != 0)
1080 *status = GOT_STATUS_MODIFY;
1081 break;
1082 } else if (flen == 0) {
1083 if (blen != 0)
1084 *status = GOT_STATUS_MODIFY;
1085 break;
1086 } else if (blen - hdrlen == flen) {
1087 /* Skip blob object header first time around. */
1088 if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1089 *status = GOT_STATUS_MODIFY;
1090 break;
1092 } else {
1093 *status = GOT_STATUS_MODIFY;
1094 break;
1096 hdrlen = 0;
1099 if (*status == GOT_STATUS_MODIFY) {
1100 rewind(f);
1101 err = get_modified_file_content_status(status, f);
1103 done:
1104 if (blob)
1105 got_object_blob_close(blob);
1106 if (f)
1107 fclose(f);
1108 return err;
1111 static const struct got_error *
1112 update_blob(struct got_worktree *worktree,
1113 struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1114 struct got_tree_entry *te, const char *path,
1115 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1116 void *progress_arg)
1118 const struct got_error *err = NULL;
1119 struct got_blob_object *blob = NULL;
1120 char *ondisk_path;
1121 unsigned char status = GOT_STATUS_NO_CHANGE;
1122 struct stat sb;
1124 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1125 return got_error_from_errno();
1127 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1128 if (err)
1129 goto done;
1131 if (status == GOT_STATUS_OBSTRUCTED) {
1132 (*progress_cb)(progress_arg, status, path);
1133 goto done;
1136 if (ie && status != GOT_STATUS_MISSING) {
1137 if (got_fileindex_entry_has_commit(ie) &&
1138 memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1139 SHA1_DIGEST_LENGTH) == 0) {
1140 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1141 path);
1142 goto done;
1144 if (got_fileindex_entry_has_blob(ie) &&
1145 memcmp(ie->blob_sha1, te->id->sha1,
1146 SHA1_DIGEST_LENGTH) == 0)
1147 goto done;
1150 err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1151 if (err)
1152 goto done;
1154 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1155 err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1156 te->mode, sb.st_mode, blob, repo, progress_cb,
1157 progress_arg);
1158 else if (status == GOT_STATUS_DELETE) {
1159 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1160 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1161 ondisk_path, path, blob, 0);
1162 if (err)
1163 goto done;
1164 } else {
1165 err = install_blob(worktree, ondisk_path, path, te->mode,
1166 sb.st_mode, blob, status == GOT_STATUS_MISSING, 0,
1167 repo, progress_cb, progress_arg);
1168 if (err)
1169 goto done;
1170 err = update_blob_fileindex_entry(worktree, fileindex, ie,
1171 ondisk_path, path, blob, 1);
1172 if (err)
1173 goto done;
1175 got_object_blob_close(blob);
1176 done:
1177 free(ondisk_path);
1178 return err;
1181 static const struct got_error *
1182 remove_ondisk_file(const char *root_path, const char *path)
1184 const struct got_error *err = NULL;
1185 char *ondisk_path = NULL;
1187 if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1188 return got_error_from_errno();
1190 if (unlink(ondisk_path) == -1) {
1191 if (errno != ENOENT)
1192 err = got_error_from_errno();
1193 } else {
1194 char *parent = dirname(ondisk_path);
1195 while (parent && strcmp(parent, root_path) != 0) {
1196 if (rmdir(parent) == -1) {
1197 if (errno != ENOTEMPTY)
1198 err = got_error_from_errno();
1199 break;
1201 parent = dirname(parent);
1204 free(ondisk_path);
1205 return err;
1208 static const struct got_error *
1209 delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1210 struct got_fileindex_entry *ie, const char *parent_path,
1211 struct got_repository *repo,
1212 got_worktree_checkout_cb progress_cb, void *progress_arg)
1214 const struct got_error *err = NULL;
1215 unsigned char status;
1216 struct stat sb;
1217 char *ondisk_path;
1219 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1220 == -1)
1221 return got_error_from_errno();
1223 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1224 if (err)
1225 return err;
1227 if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1228 status == GOT_STATUS_ADD) {
1229 (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1231 * Preserve the working file and change the deleted blob's
1232 * entry into a schedule-add entry.
1234 err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1235 0);
1236 if (err)
1237 return err;
1238 } else {
1239 (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1240 if (status == GOT_STATUS_NO_CHANGE) {
1241 err = remove_ondisk_file(worktree->root_path, ie->path);
1242 if (err)
1243 return err;
1245 got_fileindex_entry_remove(fileindex, ie);
1248 return err;
1251 struct diff_cb_arg {
1252 struct got_fileindex *fileindex;
1253 struct got_worktree *worktree;
1254 struct got_repository *repo;
1255 got_worktree_checkout_cb progress_cb;
1256 void *progress_arg;
1257 got_worktree_cancel_cb cancel_cb;
1258 void *cancel_arg;
1261 static const struct got_error *
1262 diff_old_new(void *arg, struct got_fileindex_entry *ie,
1263 struct got_tree_entry *te, const char *parent_path)
1265 struct diff_cb_arg *a = arg;
1267 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1268 return got_error(GOT_ERR_CANCELLED);
1270 return update_blob(a->worktree, a->fileindex, ie, te,
1271 ie->path, a->repo, a->progress_cb, a->progress_arg);
1274 static const struct got_error *
1275 diff_old(void *arg, struct got_fileindex_entry *ie, 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 delete_blob(a->worktree, a->fileindex, ie, parent_path,
1283 a->repo, a->progress_cb, a->progress_arg);
1286 static const struct got_error *
1287 diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1289 struct diff_cb_arg *a = arg;
1290 const struct got_error *err;
1291 char *path;
1293 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1294 return got_error(GOT_ERR_CANCELLED);
1296 if (asprintf(&path, "%s%s%s", parent_path,
1297 parent_path[0] ? "/" : "", te->name)
1298 == -1)
1299 return got_error_from_errno();
1301 if (S_ISDIR(te->mode))
1302 err = add_dir_on_disk(a->worktree, path);
1303 else
1304 err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1305 a->repo, a->progress_cb, a->progress_arg);
1307 free(path);
1308 return err;
1311 const struct got_error *
1312 got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1314 const struct got_error *err = NULL;
1315 char *uuidstr = NULL;
1316 uint32_t uuid_status;
1318 *refname = NULL;
1320 uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1321 if (uuid_status != uuid_s_ok)
1322 return got_error_uuid(uuid_status);
1324 if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1325 == -1) {
1326 err = got_error_from_errno();
1327 *refname = NULL;
1329 free(uuidstr);
1330 return err;
1334 * Prevent Git's garbage collector from deleting our base commit by
1335 * setting a reference to our base commit's ID.
1337 static const struct got_error *
1338 ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1340 const struct got_error *err = NULL;
1341 struct got_reference *ref = NULL;
1342 char *refname;
1344 err = got_worktree_get_base_ref_name(&refname, worktree);
1345 if (err)
1346 return err;
1348 err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1349 if (err)
1350 goto done;
1352 err = got_ref_write(ref, repo);
1353 done:
1354 free(refname);
1355 if (ref)
1356 got_ref_close(ref);
1357 return err;
1360 static const struct got_error *
1361 open_fileindex(struct got_fileindex **fileindex, char **fileindex_path,
1362 struct got_worktree *worktree)
1364 const struct got_error *err = NULL;
1365 FILE *index = NULL;
1367 *fileindex_path = NULL;
1368 *fileindex = got_fileindex_alloc();
1369 if (*fileindex == NULL)
1370 return got_error_from_errno();
1372 if (asprintf(fileindex_path, "%s/%s/%s", worktree->root_path,
1373 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1374 err = got_error_from_errno();
1375 *fileindex_path = NULL;
1376 goto done;
1379 index = fopen(*fileindex_path, "rb");
1380 if (index == NULL) {
1381 if (errno != ENOENT)
1382 err = got_error_from_errno();
1383 } else {
1384 err = got_fileindex_read(*fileindex, index);
1385 if (fclose(index) != 0 && err == NULL)
1386 err = got_error_from_errno();
1388 done:
1389 if (err) {
1390 free(*fileindex_path);
1391 *fileindex_path = NULL;
1392 free(*fileindex);
1393 *fileindex = NULL;
1395 return err;
1398 const struct got_error *
1399 got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
1400 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1401 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1403 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1404 struct got_commit_object *commit = NULL;
1405 struct got_object_id *tree_id = NULL;
1406 struct got_tree_object *tree = NULL;
1407 char *fileindex_path = NULL, *new_fileindex_path = NULL;
1408 struct got_fileindex *fileindex = NULL;
1409 FILE *new_index = NULL;
1410 struct got_fileindex_diff_tree_cb diff_cb;
1411 struct diff_cb_arg arg;
1412 char *relpath = NULL, *entry_name = NULL;
1414 err = lock_worktree(worktree, LOCK_EX);
1415 if (err)
1416 return err;
1419 * Read the file index.
1420 * Checking out files is supposed to be an idempotent operation.
1421 * If the on-disk file index is incomplete we will try to complete it.
1423 err = open_fileindex(&fileindex, &fileindex_path, worktree);
1424 if (err)
1425 goto done;
1427 err = got_opentemp_named(&new_fileindex_path, &new_index,
1428 fileindex_path);
1429 if (err)
1430 goto done;
1432 err = ref_base_commit(worktree, repo);
1433 if (err)
1434 goto done;
1436 err = got_object_open_as_commit(&commit, repo,
1437 worktree->base_commit_id);
1438 if (err)
1439 goto done;
1441 if (path[0]) {
1442 char *tree_path;
1443 int obj_type;
1444 relpath = strdup(path);
1445 if (relpath == NULL) {
1446 err = got_error_from_errno();
1447 goto done;
1449 if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
1450 got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
1451 path) == -1) {
1452 err = got_error_from_errno();
1453 goto done;
1455 err = got_object_id_by_path(&tree_id, repo,
1456 worktree->base_commit_id, tree_path);
1457 free(tree_path);
1458 if (err)
1459 goto done;
1460 err = got_object_get_type(&obj_type, repo, tree_id);
1461 if (err)
1462 goto done;
1463 if (obj_type == GOT_OBJ_TYPE_BLOB) {
1464 /* Split provided path into parent dir + entry name. */
1465 if (strchr(path, '/') == NULL) {
1466 relpath = strdup("");
1467 if (relpath == NULL) {
1468 err = got_error_from_errno();
1469 goto done;
1471 tree_path = strdup(worktree->path_prefix);
1472 if (tree_path == NULL) {
1473 err = got_error_from_errno();
1474 goto done;
1476 } else {
1477 err = got_path_dirname(&relpath, path);
1478 if (err)
1479 goto done;
1480 if (asprintf(&tree_path, "%s%s%s",
1481 worktree->path_prefix,
1482 got_path_is_root_dir(
1483 worktree->path_prefix) ? "" : "/",
1484 relpath) == -1) {
1485 err = got_error_from_errno();
1486 goto done;
1489 err = got_object_id_by_path(&tree_id, repo,
1490 worktree->base_commit_id, tree_path);
1491 free(tree_path);
1492 if (err)
1493 goto done;
1494 entry_name = basename(path);
1495 if (entry_name == NULL) {
1496 err = got_error_from_errno();
1497 goto done;
1500 } else {
1501 relpath = strdup("");
1502 if (relpath == NULL) {
1503 err = got_error_from_errno();
1504 goto done;
1506 err = got_object_id_by_path(&tree_id, repo,
1507 worktree->base_commit_id, worktree->path_prefix);
1508 if (err)
1509 goto done;
1512 err = got_object_open_as_tree(&tree, repo, tree_id);
1513 if (err)
1514 goto done;
1516 if (entry_name &&
1517 got_object_tree_find_entry(tree, entry_name) == NULL) {
1518 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1519 goto done;
1522 diff_cb.diff_old_new = diff_old_new;
1523 diff_cb.diff_old = diff_old;
1524 diff_cb.diff_new = diff_new;
1525 arg.fileindex = fileindex;
1526 arg.worktree = worktree;
1527 arg.repo = repo;
1528 arg.progress_cb = progress_cb;
1529 arg.progress_arg = progress_arg;
1530 arg.cancel_cb = cancel_cb;
1531 arg.cancel_arg = cancel_arg;
1532 checkout_err = got_fileindex_diff_tree(fileindex, tree, relpath,
1533 entry_name, repo, &diff_cb, &arg);
1535 /* Try to sync the fileindex back to disk in any case. */
1536 err = got_fileindex_write(fileindex, new_index);
1537 if (err)
1538 goto done;
1540 if (rename(new_fileindex_path, fileindex_path) != 0) {
1541 err = got_error_from_errno();
1542 unlink(new_fileindex_path);
1543 goto done;
1546 free(new_fileindex_path);
1547 new_fileindex_path = NULL;
1549 done:
1550 free(relpath);
1551 if (tree)
1552 got_object_tree_close(tree);
1553 if (commit)
1554 got_object_commit_close(commit);
1555 if (new_fileindex_path)
1556 unlink(new_fileindex_path);
1557 if (new_index)
1558 fclose(new_index);
1559 free(new_fileindex_path);
1560 free(fileindex_path);
1561 got_fileindex_free(fileindex);
1562 if (checkout_err)
1563 err = checkout_err;
1564 unlockerr = lock_worktree(worktree, LOCK_SH);
1565 if (unlockerr && err == NULL)
1566 err = unlockerr;
1567 return err;
1570 struct diff_dir_cb_arg {
1571 struct got_fileindex *fileindex;
1572 struct got_worktree *worktree;
1573 const char *status_path;
1574 size_t status_path_len;
1575 struct got_repository *repo;
1576 got_worktree_status_cb status_cb;
1577 void *status_arg;
1578 got_worktree_cancel_cb cancel_cb;
1579 void *cancel_arg;
1582 static const struct got_error *
1583 report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1584 got_worktree_status_cb status_cb, void *status_arg,
1585 struct got_repository *repo)
1587 const struct got_error *err = NULL;
1588 unsigned char status = GOT_STATUS_NO_CHANGE;
1589 struct stat sb;
1590 struct got_object_id id;
1592 err = get_file_status(&status, &sb, ie, abspath, repo);
1593 if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1594 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1595 err = (*status_cb)(status_arg, status, ie->path, &id);
1597 return err;
1600 static const struct got_error *
1601 status_old_new(void *arg, struct got_fileindex_entry *ie,
1602 struct dirent *de, const char *parent_path)
1604 const struct got_error *err = NULL;
1605 struct diff_dir_cb_arg *a = arg;
1606 char *abspath;
1608 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1609 return got_error(GOT_ERR_CANCELLED);
1611 if (got_path_cmp(parent_path, a->status_path) != 0 &&
1612 !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1613 return NULL;
1615 if (parent_path[0]) {
1616 if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1617 parent_path, de->d_name) == -1)
1618 return got_error_from_errno();
1619 } else {
1620 if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1621 de->d_name) == -1)
1622 return got_error_from_errno();
1625 err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1626 a->repo);
1627 free(abspath);
1628 return err;
1631 static const struct got_error *
1632 status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1634 struct diff_dir_cb_arg *a = arg;
1635 struct got_object_id id;
1636 unsigned char status;
1638 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1639 return got_error(GOT_ERR_CANCELLED);
1641 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1642 return NULL;
1644 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1645 if (got_fileindex_entry_has_file_on_disk(ie))
1646 status = GOT_STATUS_MISSING;
1647 else
1648 status = GOT_STATUS_DELETE;
1649 return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1652 static const struct got_error *
1653 status_new(void *arg, struct dirent *de, const char *parent_path)
1655 const struct got_error *err = NULL;
1656 struct diff_dir_cb_arg *a = arg;
1657 char *path = NULL;
1659 if (a->cancel_cb && a->cancel_cb(a->cancel_arg))
1660 return got_error(GOT_ERR_CANCELLED);
1662 if (de->d_type == DT_DIR)
1663 return NULL;
1665 /* XXX ignore symlinks for now */
1666 if (de->d_type == DT_LNK)
1667 return NULL;
1669 if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1670 return NULL;
1672 if (parent_path[0]) {
1673 if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1674 return got_error_from_errno();
1675 } else {
1676 path = de->d_name;
1679 err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1680 NULL);
1681 if (parent_path[0])
1682 free(path);
1683 return err;
1686 const struct got_error *
1687 got_worktree_status(struct got_worktree *worktree, const char *path,
1688 struct got_repository *repo, got_worktree_status_cb status_cb,
1689 void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1691 const struct got_error *err = NULL;
1692 DIR *workdir = NULL;
1693 char *fileindex_path = NULL;
1694 struct got_fileindex *fileindex = NULL;
1695 FILE *index = NULL;
1696 struct got_fileindex_diff_dir_cb fdiff_cb;
1697 struct diff_dir_cb_arg arg;
1698 char *ondisk_path = NULL;
1700 fileindex = got_fileindex_alloc();
1701 if (fileindex == NULL) {
1702 err = got_error_from_errno();
1703 goto done;
1706 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1707 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1708 err = got_error_from_errno();
1709 fileindex_path = NULL;
1710 goto done;
1713 index = fopen(fileindex_path, "rb");
1714 if (index == NULL) {
1715 if (errno != ENOENT) {
1716 err = got_error_from_errno();
1717 goto done;
1719 } else {
1720 err = got_fileindex_read(fileindex, index);
1721 fclose(index);
1722 if (err)
1723 goto done;
1726 if (asprintf(&ondisk_path, "%s%s%s",
1727 worktree->root_path, path[0] ? "/" : "", path) == -1) {
1728 err = got_error_from_errno();
1729 goto done;
1731 workdir = opendir(ondisk_path);
1732 if (workdir == NULL) {
1733 if (errno == ENOTDIR || errno == ENOENT) {
1734 struct got_fileindex_entry *ie;
1735 ie = got_fileindex_entry_get(fileindex, path);
1736 if (ie == NULL) {
1737 err = got_error(GOT_ERR_BAD_PATH);
1738 goto done;
1740 err = report_file_status(ie, ondisk_path,
1741 status_cb, status_arg, repo);
1742 goto done;
1743 } else {
1744 err = got_error_from_errno();
1745 goto done;
1748 fdiff_cb.diff_old_new = status_old_new;
1749 fdiff_cb.diff_old = status_old;
1750 fdiff_cb.diff_new = status_new;
1751 arg.fileindex = fileindex;
1752 arg.worktree = worktree;
1753 arg.status_path = path;
1754 arg.status_path_len = strlen(path);
1755 arg.repo = repo;
1756 arg.status_cb = status_cb;
1757 arg.status_arg = status_arg;
1758 arg.cancel_cb = cancel_cb;
1759 arg.cancel_arg = cancel_arg;
1760 err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1761 path, repo, &fdiff_cb, &arg);
1762 done:
1763 if (workdir)
1764 closedir(workdir);
1765 free(ondisk_path);
1766 free(fileindex_path);
1767 got_fileindex_free(fileindex);
1768 return err;
1771 const struct got_error *
1772 got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1773 const char *arg)
1775 const struct got_error *err = NULL;
1776 char *resolved, *path = NULL;
1777 size_t len;
1779 *wt_path = NULL;
1781 resolved = realpath(arg, NULL);
1782 if (resolved == NULL)
1783 return got_error_from_errno();
1785 if (strncmp(got_worktree_get_root_path(worktree), resolved,
1786 strlen(got_worktree_get_root_path(worktree)))) {
1787 err = got_error(GOT_ERR_BAD_PATH);
1788 goto done;
1791 path = strdup(resolved +
1792 strlen(got_worktree_get_root_path(worktree)) + 1 /* skip '/' */);
1793 if (path == NULL) {
1794 err = got_error_from_errno();
1795 goto done;
1798 /* XXX status walk can't deal with trailing slash! */
1799 len = strlen(path);
1800 while (path[len - 1] == '/') {
1801 path[len - 1] = '\0';
1802 len--;
1804 done:
1805 free(resolved);
1806 if (err == NULL)
1807 *wt_path = path;
1808 else
1809 free(path);
1810 return err;
1813 const struct got_error *
1814 got_worktree_schedule_add(struct got_worktree *worktree,
1815 const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1816 struct got_repository *repo)
1818 struct got_fileindex *fileindex = NULL;
1819 struct got_fileindex_entry *ie = NULL;
1820 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1821 FILE *index = NULL, *new_index = NULL;
1822 const struct got_error *err = NULL, *unlockerr = NULL;
1823 int ie_added = 0;
1825 err = lock_worktree(worktree, LOCK_EX);
1826 if (err)
1827 return err;
1829 err = got_path_skip_common_ancestor(&relpath,
1830 got_worktree_get_root_path(worktree), ondisk_path);
1831 if (err)
1832 goto done;
1834 fileindex = got_fileindex_alloc();
1835 if (fileindex == NULL) {
1836 err = got_error_from_errno();
1837 goto done;
1840 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1841 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1842 err = got_error_from_errno();
1843 fileindex_path = NULL;
1844 goto done;
1847 index = fopen(fileindex_path, "rb");
1848 if (index == NULL) {
1849 err = got_error_from_errno();
1850 goto done;
1853 err = got_fileindex_read(fileindex, index);
1854 if (err)
1855 goto done;
1857 if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1858 err = got_error_set_errno(EEXIST);
1859 goto done;
1862 err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1863 if (err)
1864 goto done;
1866 err = got_fileindex_entry_add(fileindex, ie);
1867 if (err)
1868 goto done;
1869 ie_added = 1; /* now owned by fileindex; don't free separately */
1871 err = got_opentemp_named(&new_fileindex_path, &new_index,
1872 fileindex_path);
1873 if (err)
1874 goto done;
1876 err = got_fileindex_write(fileindex, new_index);
1877 if (err)
1878 goto done;
1880 if (rename(new_fileindex_path, fileindex_path) != 0) {
1881 err = got_error_from_errno();
1882 goto done;
1885 free(new_fileindex_path);
1886 new_fileindex_path = NULL;
1888 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1889 done:
1890 if (index) {
1891 if (fclose(index) != 0 && err == NULL)
1892 err = got_error_from_errno();
1894 if (new_fileindex_path) {
1895 if (unlink(new_fileindex_path) != 0 && err == NULL)
1896 err = got_error_from_errno();
1897 free(new_fileindex_path);
1899 if (ie && !ie_added)
1900 got_fileindex_entry_free(ie);
1901 if (fileindex)
1902 got_fileindex_free(fileindex);
1903 unlockerr = lock_worktree(worktree, LOCK_SH);
1904 if (unlockerr && err == NULL)
1905 err = unlockerr;
1906 free(relpath);
1907 return err;
1910 const struct got_error *
1911 got_worktree_schedule_delete(struct got_worktree *worktree,
1912 const char *ondisk_path, int delete_local_mods,
1913 got_worktree_status_cb status_cb, void *status_arg,
1914 struct got_repository *repo)
1916 struct got_fileindex *fileindex = NULL;
1917 struct got_fileindex_entry *ie = NULL;
1918 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1919 FILE *index = NULL, *new_index = NULL;
1920 const struct got_error *err = NULL, *unlockerr = NULL;
1921 unsigned char status;
1922 struct stat sb;
1924 err = lock_worktree(worktree, LOCK_EX);
1925 if (err)
1926 return err;
1928 err = got_path_skip_common_ancestor(&relpath,
1929 got_worktree_get_root_path(worktree), ondisk_path);
1930 if (err)
1931 goto done;
1933 fileindex = got_fileindex_alloc();
1934 if (fileindex == NULL) {
1935 err = got_error_from_errno();
1936 goto done;
1939 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1940 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1941 err = got_error_from_errno();
1942 fileindex_path = NULL;
1943 goto done;
1946 index = fopen(fileindex_path, "rb");
1947 if (index == NULL) {
1948 err = got_error_from_errno();
1949 goto done;
1952 err = got_fileindex_read(fileindex, index);
1953 if (err)
1954 goto done;
1956 ie = got_fileindex_entry_get(fileindex, relpath);
1957 if (ie == NULL) {
1958 err = got_error(GOT_ERR_BAD_PATH);
1959 goto done;
1962 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1963 if (err)
1964 goto done;
1966 if (status != GOT_STATUS_NO_CHANGE) {
1967 if (status == GOT_STATUS_DELETE) {
1968 err = got_error_set_errno(ENOENT);
1969 goto done;
1971 if (status != GOT_STATUS_MODIFY) {
1972 err = got_error(GOT_ERR_FILE_STATUS);
1973 goto done;
1975 if (!delete_local_mods) {
1976 err = got_error(GOT_ERR_FILE_MODIFIED);
1977 goto done;
1981 if (unlink(ondisk_path) != 0) {
1982 err = got_error_from_errno();
1983 goto done;
1986 got_fileindex_entry_mark_deleted_from_disk(ie);
1988 err = got_opentemp_named(&new_fileindex_path, &new_index,
1989 fileindex_path);
1990 if (err)
1991 goto done;
1993 err = got_fileindex_write(fileindex, new_index);
1994 if (err)
1995 goto done;
1997 if (rename(new_fileindex_path, fileindex_path) != 0) {
1998 err = got_error_from_errno();
1999 goto done;
2002 free(new_fileindex_path);
2003 new_fileindex_path = NULL;
2005 err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
2006 done:
2007 free(relpath);
2008 if (index) {
2009 if (fclose(index) != 0 && err == NULL)
2010 err = got_error_from_errno();
2012 if (new_fileindex_path) {
2013 if (unlink(new_fileindex_path) != 0 && err == NULL)
2014 err = got_error_from_errno();
2015 free(new_fileindex_path);
2017 if (fileindex)
2018 got_fileindex_free(fileindex);
2019 unlockerr = lock_worktree(worktree, LOCK_SH);
2020 if (unlockerr && err == NULL)
2021 err = unlockerr;
2022 return err;
2025 const struct got_error *
2026 got_worktree_revert(struct got_worktree *worktree,
2027 const char *ondisk_path,
2028 got_worktree_checkout_cb progress_cb, void *progress_arg,
2029 struct got_repository *repo)
2031 struct got_fileindex *fileindex = NULL;
2032 struct got_fileindex_entry *ie = NULL;
2033 char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
2034 char *tree_path = NULL, *parent_path, *te_name;
2035 FILE *index = NULL, *new_index = NULL;
2036 const struct got_error *err = NULL, *unlockerr = NULL;
2037 struct got_tree_object *tree = NULL;
2038 struct got_object_id id, *tree_id = NULL;
2039 const struct got_tree_entry *te;
2040 struct got_blob_object *blob = NULL;
2041 unsigned char status;
2042 struct stat sb;
2044 err = lock_worktree(worktree, LOCK_EX);
2045 if (err)
2046 return err;
2048 err = got_path_skip_common_ancestor(&relpath,
2049 got_worktree_get_root_path(worktree), ondisk_path);
2050 if (err)
2051 goto done;
2053 fileindex = got_fileindex_alloc();
2054 if (fileindex == NULL) {
2055 err = got_error_from_errno();
2056 goto done;
2059 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
2060 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
2061 err = got_error_from_errno();
2062 fileindex_path = NULL;
2063 goto done;
2066 index = fopen(fileindex_path, "rb");
2067 if (index == NULL) {
2068 err = got_error_from_errno();
2069 goto done;
2072 err = got_fileindex_read(fileindex, index);
2073 if (err)
2074 goto done;
2076 ie = got_fileindex_entry_get(fileindex, relpath);
2077 if (ie == NULL) {
2078 err = got_error(GOT_ERR_BAD_PATH);
2079 goto done;
2082 /* Construct in-repository path of tree which contains this blob. */
2083 err = got_path_dirname(&parent_path, ie->path);
2084 if (err) {
2085 if (err->code != GOT_ERR_BAD_PATH)
2086 goto done;
2087 parent_path = "/";
2089 if (got_path_is_root_dir(worktree->path_prefix)) {
2090 tree_path = strdup(parent_path);
2091 if (tree_path == NULL) {
2092 err = got_error_from_errno();
2093 goto done;
2095 } else {
2096 if (got_path_is_root_dir(parent_path)) {
2097 tree_path = strdup(worktree->path_prefix);
2098 if (tree_path == NULL) {
2099 err = got_error_from_errno();
2100 goto done;
2102 } else {
2103 if (asprintf(&tree_path, "%s/%s",
2104 worktree->path_prefix, parent_path) == -1) {
2105 err = got_error_from_errno();
2106 goto done;
2111 err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
2112 tree_path);
2113 if (err)
2114 goto done;
2116 err = got_object_open_as_tree(&tree, repo, tree_id);
2117 if (err)
2118 goto done;
2120 te_name = basename(ie->path);
2121 if (te_name == NULL) {
2122 err = got_error_from_errno();
2123 goto done;
2126 err = get_file_status(&status, &sb, ie, ondisk_path, repo);
2127 if (err)
2128 goto done;
2130 te = got_object_tree_find_entry(tree, te_name);
2131 if (te == NULL && status != GOT_STATUS_ADD) {
2132 err = got_error(GOT_ERR_NO_TREE_ENTRY);
2133 goto done;
2136 switch (status) {
2137 case GOT_STATUS_ADD:
2138 (*progress_cb)(progress_arg, GOT_STATUS_REVERT, ie->path);
2139 got_fileindex_entry_remove(fileindex, ie);
2140 break;
2141 case GOT_STATUS_DELETE:
2142 case GOT_STATUS_MODIFY:
2143 case GOT_STATUS_CONFLICT:
2144 case GOT_STATUS_MISSING:
2145 memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
2146 err = got_object_open_as_blob(&blob, repo, &id, 8192);
2147 if (err)
2148 goto done;
2149 err = install_blob(worktree, ondisk_path, ie->path,
2150 te->mode, sb.st_mode, blob, 0, 1, repo, progress_cb,
2151 progress_arg);
2152 if (err)
2153 goto done;
2154 if (status == GOT_STATUS_DELETE) {
2155 err = update_blob_fileindex_entry(worktree,
2156 fileindex, ie, ondisk_path, ie->path, blob, 1);
2157 if (err)
2158 goto done;
2160 break;
2161 default:
2162 goto done;
2165 err = got_opentemp_named(&new_fileindex_path, &new_index,
2166 fileindex_path);
2167 if (err)
2168 goto done;
2170 err = got_fileindex_write(fileindex, new_index);
2171 if (err)
2172 goto done;
2174 if (rename(new_fileindex_path, fileindex_path) != 0) {
2175 err = got_error_from_errno();
2176 goto done;
2179 free(new_fileindex_path);
2180 new_fileindex_path = NULL;
2181 done:
2182 free(relpath);
2183 free(tree_path);
2184 if (blob)
2185 got_object_blob_close(blob);
2186 if (tree)
2187 got_object_tree_close(tree);
2188 free(tree_id);
2189 if (index) {
2190 if (fclose(index) != 0 && err == NULL)
2191 err = got_error_from_errno();
2193 if (new_fileindex_path) {
2194 if (unlink(new_fileindex_path) != 0 && err == NULL)
2195 err = got_error_from_errno();
2196 free(new_fileindex_path);
2198 if (fileindex)
2199 got_fileindex_free(fileindex);
2200 unlockerr = lock_worktree(worktree, LOCK_SH);
2201 if (unlockerr && err == NULL)
2202 err = unlockerr;
2203 return err;
2206 struct commitable {
2207 char *path;
2208 char *in_repo_path;
2209 char *ondisk_path;
2210 unsigned char status;
2211 struct got_object_id *blob_id;
2212 struct got_object_id *base_id;
2213 mode_t mode;
2216 static void
2217 free_commitable(struct commitable *ct)
2219 free(ct->path);
2220 free(ct->in_repo_path);
2221 free(ct->ondisk_path);
2222 free(ct->blob_id);
2223 free(ct->base_id);
2224 free(ct);
2227 struct collect_commitables_arg {
2228 struct got_pathlist_head *commitable_paths;
2229 struct got_repository *repo;
2230 struct got_worktree *worktree;
2233 static const struct got_error *
2234 collect_commitables(void *arg, unsigned char status, const char *relpath,
2235 struct got_object_id *id)
2237 struct collect_commitables_arg *a = arg;
2238 const struct got_error *err = NULL;
2239 struct commitable *ct = NULL;
2240 struct got_pathlist_entry *new = NULL;
2241 char *parent_path = NULL, *path = NULL;
2242 struct stat sb;
2244 if (status == GOT_STATUS_CONFLICT)
2245 return got_error(GOT_ERR_COMMIT_CONFLICT);
2247 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
2248 status != GOT_STATUS_DELETE)
2249 return NULL;
2251 if (asprintf(&path, "/%s", relpath) == -1) {
2252 err = got_error_from_errno();
2253 goto done;
2255 if (strcmp(path, "/") == 0) {
2256 parent_path = strdup("");
2257 if (parent_path == NULL)
2258 return got_error_from_errno();
2259 } else {
2260 err = got_path_dirname(&parent_path, path);
2261 if (err)
2262 return err;
2265 ct = calloc(1, sizeof(*ct));
2266 if (ct == NULL) {
2267 err = got_error_from_errno();
2268 goto done;
2271 if (asprintf(&ct->ondisk_path, "%s/%s", a->worktree->root_path,
2272 relpath) == -1) {
2273 err = got_error_from_errno();
2274 goto done;
2276 if (status == GOT_STATUS_DELETE) {
2277 sb.st_mode = GOT_DEFAULT_FILE_MODE;
2278 } else {
2279 if (lstat(ct->ondisk_path, &sb) != 0) {
2280 err = got_error_from_errno();
2281 goto done;
2283 ct->mode = sb.st_mode;
2286 if (asprintf(&ct->in_repo_path, "%s%s%s", a->worktree->path_prefix,
2287 got_path_is_root_dir(a->worktree->path_prefix) ? "" : "/",
2288 relpath) == -1) {
2289 err = got_error_from_errno();
2290 goto done;
2293 ct->status = status;
2294 ct->blob_id = NULL; /* will be filled in when blob gets created */
2295 if (ct->status != GOT_STATUS_ADD) {
2296 ct->base_id = got_object_id_dup(id);
2297 if (ct->base_id == NULL) {
2298 err = got_error_from_errno();
2299 goto done;
2302 ct->path = strdup(path);
2303 if (ct->path == NULL) {
2304 err = got_error_from_errno();
2305 goto done;
2307 err = got_pathlist_insert(&new, a->commitable_paths, ct->path, ct);
2308 done:
2309 if (ct && (err || new == NULL))
2310 free_commitable(ct);
2311 free(parent_path);
2312 free(path);
2313 return err;
2316 static const struct got_error *write_tree(struct got_object_id **,
2317 struct got_tree_object *, const char *, struct got_pathlist_head *,
2318 got_worktree_status_cb status_cb, void *status_arg,
2319 struct got_repository *);
2321 static const struct got_error *
2322 write_subtree(struct got_object_id **new_subtree_id,
2323 struct got_tree_entry *te, const char *parent_path,
2324 struct got_pathlist_head *commitable_paths,
2325 got_worktree_status_cb status_cb, void *status_arg,
2326 struct got_repository *repo)
2328 const struct got_error *err = NULL;
2329 struct got_tree_object *subtree;
2330 char *subpath;
2332 if (asprintf(&subpath, "%s%s%s", parent_path,
2333 got_path_is_root_dir(parent_path) ? "" : "/", te->name) == -1)
2334 return got_error_from_errno();
2336 err = got_object_open_as_tree(&subtree, repo, te->id);
2337 if (err)
2338 return err;
2340 err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
2341 status_cb, status_arg, repo);
2342 got_object_tree_close(subtree);
2343 free(subpath);
2344 return err;
2347 static const struct got_error *
2348 match_ct_parent_path(int *match, struct commitable *ct, const char *path)
2350 const struct got_error *err = NULL;
2351 char *ct_parent_path = NULL;
2353 *match = 0;
2355 if (strchr(ct->path, '/') == NULL) {
2356 ct_parent_path = strdup("/");
2357 if (ct_parent_path == NULL)
2358 return got_error_from_errno();
2359 } else {
2360 err = got_path_dirname(&ct_parent_path, ct->path);
2361 if (err)
2362 return err;
2365 *match = (strcmp(path, ct_parent_path) == 0);
2366 free(ct_parent_path);
2367 return err;
2370 static mode_t
2371 get_ct_file_mode(struct commitable *ct)
2373 return S_IFREG | (ct->mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
2376 static const struct got_error *
2377 alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
2378 struct got_tree_entry *te, struct commitable *ct)
2380 const struct got_error *err = NULL;
2382 *new_te = NULL;
2384 err = got_object_tree_entry_dup(new_te, te);
2385 if (err)
2386 goto done;
2388 (*new_te)->mode = get_ct_file_mode(ct);
2390 free((*new_te)->id);
2391 (*new_te)->id = got_object_id_dup(ct->blob_id);
2392 if ((*new_te)->id == NULL) {
2393 err = got_error_from_errno();
2394 goto done;
2396 done:
2397 if (err && *new_te) {
2398 got_object_tree_entry_close(*new_te);
2399 *new_te = NULL;
2401 return err;
2404 static const struct got_error *
2405 alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
2406 struct commitable *ct)
2408 const struct got_error *err = NULL;
2409 char *ct_name;
2411 *new_te = NULL;
2413 *new_te = calloc(1, sizeof(*new_te));
2414 if (*new_te == NULL)
2415 return got_error_from_errno();
2417 ct_name = basename(ct->path);
2418 if (ct_name == NULL) {
2419 err = got_error_from_errno();
2420 goto done;
2422 (*new_te)->name = strdup(ct_name);
2423 if ((*new_te)->name == NULL) {
2424 err = got_error_from_errno();
2425 goto done;
2428 (*new_te)->mode = get_ct_file_mode(ct);
2430 (*new_te)->id = got_object_id_dup(ct->blob_id);
2431 if ((*new_te)->id == NULL) {
2432 err = got_error_from_errno();
2433 goto done;
2435 done:
2436 if (err && *new_te) {
2437 got_object_tree_entry_close(*new_te);
2438 *new_te = NULL;
2440 return err;
2443 static const struct got_error *
2444 insert_tree_entry(struct got_tree_entry *new_te,
2445 struct got_pathlist_head *paths)
2447 const struct got_error *err = NULL;
2448 struct got_pathlist_entry *new_pe;
2450 err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
2451 if (err)
2452 return err;
2453 if (new_pe == NULL)
2454 return got_error(GOT_ERR_TREE_DUP_ENTRY);
2455 return NULL;
2458 static const struct got_error *
2459 report_ct_status(struct commitable *ct,
2460 got_worktree_status_cb status_cb, void *status_arg)
2462 const char *ct_path = ct->path;
2463 while (ct_path[0] == '/')
2464 ct_path++;
2465 return (*status_cb)(status_arg, ct->status, ct_path, ct->blob_id);
2468 static const struct got_error *
2469 match_modified_subtree(int *modified, struct got_tree_entry *te,
2470 const char *base_tree_path, struct got_pathlist_head *commitable_paths)
2472 const struct got_error *err = NULL;
2473 struct got_pathlist_entry *pe;
2474 char *te_path;
2476 *modified = 0;
2478 if (asprintf(&te_path, "%s%s%s", base_tree_path,
2479 got_path_is_root_dir(base_tree_path) ? "" : "/",
2480 te->name) == -1)
2481 return got_error_from_errno();
2483 TAILQ_FOREACH(pe, commitable_paths, entry) {
2484 struct commitable *ct = pe->data;
2485 char *ct_path;
2487 if (asprintf(&ct_path, "%s%s%s", base_tree_path,
2488 got_path_is_root_dir(base_tree_path) ? "" : "/",
2489 te->name) == -1) {
2490 err = got_error_from_errno();
2491 break;
2494 *modified = got_path_is_child(ct->in_repo_path, te_path,
2495 strlen(te_path));
2496 if (*modified)
2497 break;
2500 free(te_path);
2501 return err;
2504 static const struct got_error *
2505 match_deleted_or_modified_ct(struct commitable **ctp,
2506 struct got_tree_entry *te, const char *base_tree_path,
2507 struct got_pathlist_head *commitable_paths)
2509 const struct got_error *err = NULL;
2510 struct got_pathlist_entry *pe;
2512 *ctp = NULL;
2514 TAILQ_FOREACH(pe, commitable_paths, entry) {
2515 struct commitable *ct = pe->data;
2516 char *ct_name = NULL;
2517 int path_matches;
2519 if (ct->status != GOT_STATUS_MODIFY &&
2520 ct->status != GOT_STATUS_DELETE)
2521 continue;
2523 if (got_object_id_cmp(ct->base_id, te->id) != 0)
2524 continue;
2526 err = match_ct_parent_path(&path_matches, ct, base_tree_path);
2527 if (err)
2528 return err;
2529 if (!path_matches)
2530 continue;
2532 ct_name = basename(pe->path);
2533 if (ct_name == NULL)
2534 return got_error_from_errno();
2536 if (strcmp(te->name, ct_name) != 0)
2537 continue;
2539 *ctp = ct;
2540 break;
2543 return err;
2546 static const struct got_error *
2547 write_tree(struct got_object_id **new_tree_id,
2548 struct got_tree_object *base_tree, const char *path_base_tree,
2549 struct got_pathlist_head *commitable_paths,
2550 got_worktree_status_cb status_cb, void *status_arg,
2551 struct got_repository *repo)
2553 const struct got_error *err = NULL;
2554 const struct got_tree_entries *base_entries = NULL;
2555 struct got_pathlist_head paths;
2556 struct got_tree_entries new_tree_entries;
2557 struct got_tree_entry *te, *new_te = NULL;
2558 struct got_pathlist_entry *pe;
2560 TAILQ_INIT(&paths);
2561 new_tree_entries.nentries = 0;
2562 SIMPLEQ_INIT(&new_tree_entries.head);
2564 /* Insert, and recurse into, newly added entries first. */
2565 TAILQ_FOREACH(pe, commitable_paths, entry) {
2566 struct commitable *ct = pe->data;
2567 char *child_path = NULL, *slash;
2569 if (ct->status != GOT_STATUS_ADD)
2570 continue;
2572 if (!got_path_is_child(pe->path, path_base_tree,
2573 strlen(path_base_tree)))
2574 continue;
2576 err = got_path_skip_common_ancestor(&child_path, path_base_tree,
2577 pe->path);
2578 if (err)
2579 goto done;
2581 slash = strchr(child_path, '/');
2582 if (slash == NULL) {
2583 err = alloc_added_blob_tree_entry(&new_te, ct);
2584 if (err)
2585 goto done;
2586 err = report_ct_status(ct, status_cb, status_arg);
2587 if (err)
2588 goto done;
2589 } else {
2590 char *subtree_path;
2591 struct got_pathlist_entry *pe2;
2592 int visited = 0;
2594 *slash = '\0'; /* trim trailing path components */
2595 if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
2596 got_path_is_root_dir(path_base_tree) ? "" : "/",
2597 child_path) == -1) {
2598 err = got_error_from_errno();
2599 goto done;
2601 TAILQ_FOREACH(pe2, &paths, entry) {
2602 if (got_path_cmp(subtree_path, pe2->path) != 0)
2603 continue;
2604 visited = 1;
2605 break;
2607 if (visited)
2608 continue;
2610 new_te = calloc(1, sizeof(*new_te));
2611 new_te->mode = S_IFDIR;
2612 new_te->name = strdup(child_path);
2613 if (new_te->name == NULL) {
2614 err = got_error_from_errno();
2615 got_object_tree_entry_close(new_te);
2616 new_te = NULL;
2617 goto done;
2619 err = write_tree(&new_te->id, NULL, subtree_path,
2620 commitable_paths, status_cb, status_arg, repo);
2621 free(subtree_path);
2622 if (err) {
2623 got_object_tree_entry_close(new_te);
2624 new_te = NULL;
2625 goto done;
2628 err = insert_tree_entry(new_te, &paths);
2629 if (err)
2630 goto done;
2633 if (base_tree) {
2634 /* Handle modified and deleted entries. */
2635 base_entries = got_object_tree_get_entries(base_tree);
2636 SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
2637 struct commitable *ct = NULL;
2639 if (S_ISDIR(te->mode)) {
2640 int modified;
2641 err = got_object_tree_entry_dup(&new_te, te);
2642 if (err)
2643 goto done;
2644 err = match_modified_subtree(&modified, te,
2645 path_base_tree, commitable_paths);
2646 if (err)
2647 goto done;
2648 /* Avoid recursion into unmodified subtrees. */
2649 if (modified) {
2650 free(new_te->id);
2651 err = write_subtree(&new_te->id, te,
2652 path_base_tree, commitable_paths,
2653 status_cb, status_arg, repo);
2654 if (err)
2655 goto done;
2657 err = insert_tree_entry(new_te, &paths);
2658 if (err)
2659 goto done;
2660 continue;
2663 err = match_deleted_or_modified_ct(&ct, te,
2664 path_base_tree, commitable_paths);
2665 if (ct) {
2666 /* NB: Deleted entries get dropped here. */
2667 if (ct->status == GOT_STATUS_MODIFY) {
2668 err = alloc_modified_blob_tree_entry(
2669 &new_te, te, ct);
2670 if (err)
2671 goto done;
2672 err = insert_tree_entry(new_te, &paths);
2673 if (err)
2674 goto done;
2676 err = report_ct_status(ct, status_cb, status_arg);
2677 if (err)
2678 goto done;
2679 } else {
2680 /* Entry is unchanged; just copy it. */
2681 err = got_object_tree_entry_dup(&new_te, te);
2682 if (err)
2683 goto done;
2684 err = insert_tree_entry(new_te, &paths);
2685 if (err)
2686 goto done;
2691 /* Write new list of entries; deleted entries have been dropped. */
2692 TAILQ_FOREACH(pe, &paths, entry) {
2693 struct got_tree_entry *te = pe->data;
2694 new_tree_entries.nentries++;
2695 SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
2697 err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
2698 done:
2699 got_object_tree_entries_close(&new_tree_entries);
2700 got_pathlist_free(&paths);
2701 return err;
2704 static const struct got_error *
2705 update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
2706 struct got_object_id *new_base_commit_id, struct got_worktree *worktree)
2708 const struct got_error *err = NULL;
2709 char *fileindex_path = NULL, *new_fileindex_path = NULL;
2710 struct got_fileindex *fileindex = NULL;
2711 FILE *new_index = NULL;
2712 struct got_pathlist_entry *pe;
2714 err = open_fileindex(&fileindex, &fileindex_path, worktree);
2715 if (err)
2716 return err;
2718 err = got_opentemp_named(&new_fileindex_path, &new_index,
2719 fileindex_path);
2720 if (err)
2721 goto done;
2723 TAILQ_FOREACH(pe, commitable_paths, entry) {
2724 struct got_fileindex_entry *ie;
2725 struct commitable *ct = pe->data;
2727 ie = got_fileindex_entry_get(fileindex, pe->path);
2728 if (ie) {
2729 if (ct->status == GOT_STATUS_DELETE) {
2730 got_fileindex_entry_remove(fileindex, ie);
2731 got_fileindex_entry_free(ie);
2732 } else
2733 err = got_fileindex_entry_update(ie,
2734 ct->ondisk_path, ct->blob_id->sha1,
2735 new_base_commit_id->sha1, 1);
2736 } else {
2737 err = got_fileindex_entry_alloc(&ie,
2738 ct->ondisk_path, pe->path, ct->blob_id->sha1,
2739 new_base_commit_id->sha1);
2740 if (err)
2741 goto done;
2742 err = got_fileindex_entry_add(fileindex, ie);
2743 if (err)
2744 goto done;
2748 err = got_fileindex_write(fileindex, new_index);
2749 if (err)
2750 goto done;
2752 if (rename(new_fileindex_path, fileindex_path) != 0) {
2753 err = got_error_from_errno();
2754 unlink(new_fileindex_path);
2755 goto done;
2758 free(new_fileindex_path);
2759 new_fileindex_path = NULL;
2761 done:
2762 if (new_fileindex_path)
2763 unlink(new_fileindex_path);
2764 if (new_index)
2765 fclose(new_index);
2766 free(new_fileindex_path);
2767 free(fileindex_path);
2768 got_fileindex_free(fileindex);
2769 return err;
2772 static const struct got_error *
2773 check_ct_out_of_date(struct commitable *ct, struct got_repository *repo,
2774 struct got_object_id *head_commit_id)
2776 const struct got_error *err = NULL;
2777 struct got_object_id *id_in_head;
2780 * XXX This should probably be checking each commit from
2781 * worktree's base_commit -> head and verify that the
2782 * same blob exists in each of these commits.
2783 * Removals+additions within this line of history could mean
2784 * that renames have occured in which case we should really
2785 * be forcing the user to run an update...
2787 err = got_object_id_by_path(&id_in_head, repo,
2788 head_commit_id, ct->in_repo_path);
2789 if (err) {
2790 if (err->code != GOT_ERR_NO_TREE_ENTRY)
2791 return err;
2792 if (ct->status != GOT_STATUS_ADD)
2793 return got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2794 err = NULL;
2795 id_in_head = NULL;
2798 if (id_in_head && got_object_id_cmp(id_in_head, ct->base_id) != 0)
2799 err = got_error(GOT_ERR_COMMIT_OUT_OF_DATE);
2801 free(id_in_head);
2802 return err;
2805 const struct got_error *
2806 got_worktree_commit(struct got_object_id **new_commit_id,
2807 struct got_worktree *worktree, const char *ondisk_path,
2808 const char *author, const char *committer, const char *logmsg,
2809 got_worktree_status_cb status_cb, void *status_arg,
2810 struct got_repository *repo)
2812 const struct got_error *err = NULL, *unlockerr = NULL;
2813 struct collect_commitables_arg cc_arg;
2814 struct got_pathlist_head commitable_paths;
2815 struct got_pathlist_entry *pe;
2816 char *relpath = NULL;
2817 const char *head_ref_name = NULL;
2818 struct got_reference *head_ref = NULL;
2819 struct got_commit_object *head_commit = NULL;
2820 struct got_object_id *head_commit_id = NULL;
2821 struct got_reference *head_ref2 = NULL;
2822 struct got_object_id *head_commit_id2 = NULL;
2823 struct got_tree_object *head_tree = NULL;
2824 struct got_object_id *new_tree_id = NULL;
2825 struct got_object_id_queue parent_ids;
2826 struct got_object_qid *pid = NULL;
2828 *new_commit_id = NULL;
2830 TAILQ_INIT(&commitable_paths);
2831 SIMPLEQ_INIT(&parent_ids);
2833 if (ondisk_path) {
2834 err = got_path_skip_common_ancestor(&relpath,
2835 worktree->root_path, ondisk_path);
2836 if (err)
2837 return err;
2840 err = lock_worktree(worktree, LOCK_EX);
2841 if (err)
2842 goto done;
2844 err = got_ref_open(&head_ref, repo, worktree->head_ref_name);
2845 if (err)
2846 goto done;
2847 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2848 if (err)
2849 goto done;
2851 cc_arg.commitable_paths = &commitable_paths;
2852 cc_arg.worktree = worktree;
2853 cc_arg.repo = repo;
2854 err = got_worktree_status(worktree, relpath ? relpath : "",
2855 repo, collect_commitables, &cc_arg, NULL, NULL);
2856 if (err)
2857 goto done;
2859 err = got_object_open_as_commit(&head_commit, repo, head_commit_id);
2860 if (err)
2861 goto done;
2863 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2864 struct commitable *ct = pe->data;
2865 err = check_ct_out_of_date(ct, repo, head_commit_id);
2866 if (err)
2867 goto done;
2870 err = got_object_open_as_tree(&head_tree, repo, head_commit->tree_id);
2871 if (err)
2872 goto done;
2874 /* TODO: collect commit message if not specified */
2876 /* Create blobs from added and modified files and record their IDs. */
2877 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2878 struct commitable *ct = pe->data;
2879 char *ondisk_path;
2881 if (ct->status != GOT_STATUS_ADD &&
2882 ct->status != GOT_STATUS_MODIFY)
2883 continue;
2885 if (asprintf(&ondisk_path, "%s/%s",
2886 worktree->root_path, pe->path) == -1) {
2887 err = got_error_from_errno();
2888 goto done;
2890 err = got_object_blob_create(&ct->blob_id, ondisk_path, repo);
2891 free(ondisk_path);
2892 if (err)
2893 goto done;
2896 /* Recursively write new tree objects. */
2897 err = write_tree(&new_tree_id, head_tree, "/", &commitable_paths,
2898 status_cb, status_arg, repo);
2899 if (err)
2900 goto done;
2902 err = got_object_qid_alloc(&pid, worktree->base_commit_id);
2903 if (err)
2904 goto done;
2905 SIMPLEQ_INSERT_TAIL(&parent_ids, pid, entry);
2906 err = got_object_commit_create(new_commit_id, new_tree_id, &parent_ids,
2907 1, author, time(NULL), committer, time(NULL), logmsg, repo);
2908 got_object_qid_free(pid);
2909 if (err)
2910 goto done;
2912 /* Check if a concurrent commit to our branch has occurred. */
2913 /* XXX ideally we'd lock the reference file here to avoid a race */
2914 head_ref_name = got_worktree_get_head_ref_name(worktree);
2915 if (head_ref_name == NULL) {
2916 err = got_error_from_errno();
2917 goto done;
2919 err = got_ref_open(&head_ref2, repo, head_ref_name);
2920 if (err)
2921 goto done;
2922 err = got_ref_resolve(&head_commit_id2, repo, head_ref2);
2923 if (err)
2924 goto done;
2925 if (got_object_id_cmp(head_commit_id, head_commit_id2) != 0) {
2926 err = got_error(GOT_ERR_COMMIT_HEAD_CHANGED);
2927 goto done;
2929 /* Update branch head in repository. */
2930 err = got_ref_change_ref(head_ref, *new_commit_id);
2931 if (err)
2932 goto done;
2933 err = got_ref_write(head_ref, repo);
2934 if (err)
2935 goto done;
2936 /* XXX race has ended here */
2938 err = got_worktree_set_base_commit_id(worktree, repo, *new_commit_id);
2939 if (err)
2940 goto done;
2942 err = ref_base_commit(worktree, repo);
2943 if (err)
2944 goto done;
2946 err = update_fileindex_after_commit(&commitable_paths,
2947 *new_commit_id, worktree);
2948 if (err)
2949 goto done;
2950 done:
2951 unlockerr = lock_worktree(worktree, LOCK_SH);
2952 if (unlockerr && err == NULL)
2953 err = unlockerr;
2954 TAILQ_FOREACH(pe, &commitable_paths, entry) {
2955 struct commitable *ct = pe->data;
2956 free_commitable(ct);
2958 got_pathlist_free(&commitable_paths);
2959 if (head_tree)
2960 got_object_tree_close(head_tree);
2961 if (head_commit)
2962 got_object_commit_close(head_commit);
2963 free(relpath);
2964 free(head_commit_id);
2965 free(head_commit_id2);
2966 if (head_ref)
2967 got_ref_close(head_ref);
2968 if (head_ref2)
2969 got_ref_close(head_ref2);
2970 return err;